Skip to content
Back to search

loki-mode

MarketplaceWorks with Claude CodeGitHub

Autonomi marketplace: Loki Mode, the autonomous spec-to-product build system with a built-in trust layer.

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today

Check it deeper on your machine

It has changed since its scan on 11 Nov 2026. Its scripts were only partly scanned.

As a Claude Code plugin, install it and then ask Claude Code:

/plugin marketplace add keithmackay/mackayi/plugin install scanskillsafety@mackayiDo a deep scan of https://github.com/asklokesh/loki-mode with scanskillsafety

Or from a terminal, clone it and run it:

git clone https://github.com/keithmackay/scanskillsafety-skill ~/.claude/skills/scanskillsafetynode ~/.claude/skills/scanskillsafety/cli.cjs --deep 'https://github.com/asklokesh/loki-mode'

It checks the exact commit: every file, its packages against OSV (api.osv.dev), how it compares with this page's scan (findsafeskills.com receives the repository's address), and, if you agree, Claude reading every file with your own Claude subscription. It never runs the code.

Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/asklokesh--loki-mode/badge.svg)](/listings/asklokesh--loki-mode)

HTML

<a href="/listings/asklokesh--loki-mode"><img src="/api/listings/asklokesh--loki-mode/badge.svg" alt="findsafeskills safety rating" /></a>

Install

Install instructions from the README

As written by the repo's author. findsafeskills has not verified these steps.

npm install -g loki-mode

The npm package installs the Bun runtime the engine needs as an optional dependency. Other ways in: `bun install -g loki-mode`, `brew tap asklokesh/tap && brew install loki-mode`, or `docker pull asklokesh/loki-mode:latest` ([Docker guide](docs/DOCKER.md)). Upgrade with `loki self-update`.

Want to see a result before you install or spend anything?

npx loki-mode tour    # prints a real receipt from a past build: no key, no spend, no network

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

.claude-plugin/marketplace.json · Manifest · 40 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

This file has changed since it was scanned, so this is not exactly the text that was scanned, and no line is marked. The findings are listed with the safety scan above.

  1. {
  2. "$schema": "https://json.schemastore.org/claude-code-marketplace.json",
  3. "name": "loki-mode",
  4. "owner": {
  5. "name": "Autonomi"
  6. },
  7. "description": "Autonomi marketplace: Loki Mode, the autonomous spec-to-product build system with a built-in trust layer.",
  8. "plugins": [
  9. {
  10. "name": "loki-mode",
  11. "source": "./plugins/loki-mode",
  12. "description": "Autonomous spec-to-product build system with a built-in trust layer. Ships Loki's spec-hardening (grill), living-spec drift detection, and deterministic PR verification commands, plus the Loki MCP server (memory, task queue, code search, build management). Requires the loki-mode CLI on PATH (npm install -g loki-mode or brew install).",
  13. "homepage": "https://github.com/asklokesh/loki-mode",
  14. "repository": "https://github.com/asklokesh/loki-mode",
  15. "license": "SEE LICENSE IN LICENSE",
  16. "category": "automation",
  17. "keywords": [
  18. "autonomous",
  19. "agent",
  20. "spec-driven",
  21. "verification",
  22. "code-review",
  23. "mcp",
  24. "loki"
  25. ]
  26. },
  27. {
  28. "name": "loki-seal",
  29. "source": "./packages/loki-seal",
  30. "description": "Stop hook that refuses 'done' while tests are red or were deleted, skipped, xfailed or weakened. Runs locally, no model calls.",
  31. "homepage": "https://github.com/asklokesh/loki-mode",
  32. "category": "testing",
  33. "keywords": [
  34. "verification",
  35. "tests",
  36. "hooks"
  37. ]
  38. }
  39. ]
  40. }
README.md · README · 190 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

This file has changed since it was scanned, so this is not exactly the text that was scanned, and no line is marked. The findings are listed with the safety scan above.

  1. # Loki Mode
  2. Your agent says done. Loki proves it: an autonomous coding agent that turns a task or issue into a verified pull request with a signed receipt you can check yourself.
  3. [![npm version](https://img.shields.io/npm/v/loki-mode?style=for-the-badge&logo=npm&logoColor=white&color=553DE9)](https://www.npmjs.com/package/loki-mode)
  4. [![npm downloads](https://img.shields.io/npm/dt/loki-mode?style=for-the-badge&logo=npm&logoColor=white&color=1FC5A8&label=downloads)](https://www.npmjs.com/package/loki-mode)
  5. [![Docker Pulls](https://img.shields.io/docker/pulls/asklokesh/loki-mode?style=for-the-badge&logo=docker&logoColor=white&color=2F71E3)](https://hub.docker.com/r/asklokesh/loki-mode)
  6. [![License](https://img.shields.io/badge/License-BUSL--1.1-36342E?style=for-the-badge)](LICENSE)
  7. [Website](https://www.autonomi.dev/) | [Quickstart](docs/QUICKSTART.md) | [Install guide](docs/INSTALLATION.md) | [User guide](docs/v10/GUIDE.md) | [CLI reference](docs/CLI-CORE-VERBS.md) | [Compare with opencode](docs/COMPARE-OPENCODE.md) | [Changelog](CHANGELOG.md)
  8. <!-- generated:facts -->
  9. The npm badge above tracks the current release; see [CHANGELOG.md](CHANGELOG.md) for release notes.
  10. CLI reference: [docs/CLI-REFERENCE.md](docs/CLI-REFERENCE.md) (generated from the command registry).
  11. <!-- /generated -->
  12. ## Install
  13. ```bash
  14. npm install -g loki-mode
  15. ```
  16. The npm package installs the Bun runtime the engine needs as an optional dependency. Other ways in: `bun install -g loki-mode`, `brew tap asklokesh/tap && brew install loki-mode`, or `docker pull asklokesh/loki-mode:latest` ([Docker guide](docs/DOCKER.md)). Upgrade with `loki self-update`.
  17. Want to see a result before you install or spend anything?
  18. ```bash
  19. npx loki-mode start # no git repo: a real receipt, no key, no spend
  20. npx loki-mode tour # the same receipt when you are already inside a repo
  21. ```
  22. `npx` downloads the package the first time, so that step needs the network. The receipt itself replays offline and checks with no provider and no key.
  23. ```bash
  24. npx loki-mode wow # a provider is ready: one tiny real task in a temp repo, its own receipt
  25. ```
  26. ## First run
  27. Loki needs a model. [Claude Code](https://docs.claude.com/en/docs/claude-code) (`claude`) is the default provider: install it and log in. The bundled Agent SDK with an `ANTHROPIC_API_KEY` can stand in for the CLI on the main path. Other coding CLIs are optional. See [Requirements](#requirements).
  28. ```bash
  29. cd your-repo
  30. loki setup # first-run wizard (or: loki doctor --fix)
  31. loki start "add rate limiting to /api/login" # the primary path: build, verify, seal a receipt, open a PR
  32. loki start prd.md # same, from a PRD file
  33. loki start owner/repo#123 # same, from a GitHub, GitLab or Jira issue
  34. loki "fix #123" # an issue of this checkout; a copied issue or PR URL works as is
  35. loki edit "rename fooBar to foo_bar" # one small change, no loop
  36. loki chat # interactive alternative: Plan and Build modes
  37. ```
  38. `loki start`, `loki "<task>"`, `loki owner/repo#N` and `loki quick "<task>"` all run on the Loki 10 engine by default, whatever the flag order or input form. <!-- loki10-default --> Add `--no-pr` to build and verify without opening a pull request, `--max-cost <usd>` to cap spend, and `--provider <name>` to pick another coding CLI. The spend cap defaults to $100 when `ANTHROPIC_API_KEY` or `OPENAI_API_KEY` is set and to no dollar cap on a subscription login; `budgets.per_run` in `loki.yaml` also sets it. Inspect the route without running: `loki route start prd.md` or `--explain-route`. New here? The [5-minute quickstart](docs/QUICKSTART.md) walks through it.
  39. ## What you get
  40. Every run starts from a delivery contract derived from your task, writes checks from the task alone before touching code, implements, verifies, and ends in exactly one outcome: VERIFIED, ALREADY_SATISFIED, FAILED, BUDGET_STOP, BLOCKED or STALLED ([exit codes](docs/exit-codes.md)). If the contract cannot be satisfied as written, the run stops and asks one question instead of guessing.
  41. - **A pull request** written for a quick review: what the issue asked, what changed, how it was tested, and what was NOT PROVEN. A VERIFIED run opens a ready PR; a PARTIAL run opens a draft PR titled `[PARTIAL]` that leads with what is not proven; a FAILED run with committed work opens a draft PR titled `[FAILED: <check>]` that leads with the checks still failing. The exception is a security scan secret match: that branch is never pushed, and Loki prints the file and line it matched and the push command to run after review. Any other outcome keeps the branch local and prints where it is.
  42. - **A signed receipt** at `.loki/runs/<run-id>/receipt.json`, signed with a local Ed25519 key created on first run.
  43. - **A specialist review** when the review council runs: it selects reviewers from a specialist pool (`agents/types.json`, scored by `run.sh:FOCUS_KEYWORDS`) by matching the changed files and diff against each reviewer's keywords.
  44. ```bash
  45. loki verify # newest run; or: loki verify <run-id>
  46. ```
  47. `loki verify` re-hashes the receipt, checks its signature and checks it against the run's event log. It prints a verdict and exits 0 (VERIFIED), 1 (TAMPERED), 2 (UNCHECKED), 3 (UNSIGNED, refused unless `--allow-unsigned`), 4 (run outcome not verified) or 66 (no runs); `[ $rc -eq 0 ]` is the only safe test for "verified". It also checks that your checkout still matches the sealed code: a run that opened a PR returns you to your starting branch, so verify it from the run's branch (`git fetch origin loki/<run-id> && git checkout --detach FETCH_HEAD`), or it reads UNCHECKED and says so. `loki start` also requires fail-to-pass for a declared behavior change: a Wall test must fail on the old code and pass after, or the verdict stays PARTIAL (`LOKI_MUTATION_STRICT=0` opts out). Full table: [docs/exit-codes.md](docs/exit-codes.md). On another machine, export the public key with `loki keys export` and pass it with `loki verify --pubkey <file>`. A receipt claims only what its checks ran, and lists what they did not; signing proves who produced it, not that the code is bug-free. Details: [docs/SIGNED-RECEIPTS.md](docs/SIGNED-RECEIPTS.md).
  48. For a multi-repo change set, `loki start --workspace <name> "<brief>"` seals one signed `composite.json` over every child receipt, and `loki verify <run-dir>/composite.json` re-checks it offline ([docs/WORKSPACES.md](docs/WORKSPACES.md)). A composite never reads higher than its weakest child.
  49. Two vocabularies, not one. Receipt verdicts (sealed, 5): VERIFIED, PARTIAL, ALREADY_SATISFIED, SPEC_CONFLICT, FAILED. Run outcomes (printed, 8): VERIFIED, ALREADY_SATISFIED, PARTIAL, FAILED, BUDGET_STOP, BLOCKED, STALLED, CANCELLED. BUDGET_STOP, BLOCKED, STALLED and CANCELLED are run outcomes, never receipt verdicts.
  50. | Run outcome | Receipt verdict | `loki start` exit |
  51. |---|---|---|
  52. | VERIFIED, ALREADY_SATISFIED | same | 0 |
  53. | FAILED | same | 1 |
  54. | PARTIAL | same | 6 |
  55. | BUDGET_STOP | none (stopped at a cap) | 3 |
  56. | BLOCKED | SPEC_CONFLICT | 4 |
  57. | STALLED | none | 5 |
  58. | CANCELLED | none | 130 |
  59. Composite: every child VERIFIED or ALREADY_SATISFIED gives VERIFIED (all ALREADY_SATISFIED stays ALREADY_SATISFIED, any SPEC_CONFLICT child keeps SPEC_CONFLICT); one PARTIAL child caps it at PARTIAL; a FAILED child fails it. `loki verify` on a composite exits 0 VERIFIED, 1 TAMPERED, 2 UNCHECKED, 3 UNSIGNED.
  60. Autonomy and deploy: `.loki/policies.json` `autonomy: propose|execute|operate` governs what `loki start` may do (propose opens nothing, execute opens a PR, operate also allows deploy); no file keeps today's behavior. `loki trust` prints advisory graduation advice from your receipts and never writes the policy. Loki deploys only behind `loki deploy --execute` (or `loki start --deploy`) plus a VERIFIED, HEAD-anchored receipt and the policy permission; `--health-url` and `--auto-rollback` add a post-deploy probe and a documented provider rollback. A Sentry alert webhook can enqueue a receipt-gated fix PR ([docs/sentry-intake.md](docs/sentry-intake.md)).
  61. A finished run prints a short summary (every run on both engines also ends with a unified Result, Files, Cost, Elapsed, Receipt and `next:` block; `--quiet` prints only that block, `--json` adds a `summary` object):
  62. ```
  63. Outcome: VERIFIED
  64. PR: https://github.com/owner/repo/pull/1
  65. Receipt: sha256:2f9a41c7e0b3...
  66. NOT PROVEN: full suite, app boot, security scan
  67. Cost: $0.42 (claude, 212k tokens)
  68. Time: 4m12s (intake 11s, plan 20s, implement 3m10s, verify 31s)
  69. ```
  70. ## Core commands
  71. | Command | What it does |
  72. |---------|--------------|
  73. | `loki start "<task>"` | The command you run: same provider model as a raw session, then the harness re-runs the checks and seals a receipt (`--no-pr`, `--provider`, `--max-cost`, `--haiku` to run implement and fix on Haiku, climbing to Sonnet on a failed check). `loki "<task>"` and `loki owner/repo#N` are shorthand. No task and no git repo shows a receipt and spends nothing |
  74. | `loki edit "<change>"` | One targeted change in one pass, no PR, plan or council (`--verify`, `--receipt`, `--commit`, `--dry-run`) |
  75. | `loki quick "<task>"` | One small task with a verify loop, no PR |
  76. | `loki chat` | Interactive alternative: Plan and Build modes, slash commands, `/run` starts a `loki start` run, `--attach` joins one |
  77. | `loki route start <args>` | Show the engine, provider, model and reason without running (also `--explain-route`) |
  78. | `loki setup` | First-run wizard: tools, Claude login, provider, model route, spend cap |
  79. | `loki config list\|get\|set\|path` | Read and write settings |
  80. | `loki models [--local]` | List model routes (OpenRouter, Ollama, LM Studio, llama.cpp, vLLM, custom); discover local models |
  81. | `loki agents list\|new\|show\|export --claude` | Custom child agents in `.loki/agents/*.md` |
  82. | `loki status [run-id]` | Status of the latest run, or of a given run |
  83. | `loki verify [run-id]` | Check a receipt's hash, signature and event log |
  84. | `loki answer [run-id] "<text>"` | Answer a BLOCKED run; it continues under the same run id (also `--text "..."`) |
  85. | `loki doctor [--fix] [--json]` | Check prerequisites, providers and login; `--fix` applies safe fixes |
  86. | `loki control serve` | Start the Control Plane (see below) |
  87. | `loki dashboard` | Open the Control Plane UI, starting it if needed |
  88. | `loki tour` | Show a sample receipt offline. `loki start` does this when you are not in a git repo and did not pass a task |
  89. | `loki wow` | Same live mini-task as a normal start, in a temp repo. It says it makes model calls and what it can cost, and asks y/N first (`--yes` skips; with no terminal it spends nothing). Offline sample when no provider is ready |
  90. | `loki factory "<task>"` | Same as `loki start`. The policy line is printed by start |
  91. | `loki --help` | Everything else; `loki help <command>` for one command |
  92. Core verbs: [docs/CLI-CORE-VERBS.md](docs/CLI-CORE-VERBS.md). Every command (generated): [docs/CLI-REFERENCE.md](docs/CLI-REFERENCE.md). Engine guide, provider table, model routes, custom agents and chat: [docs/v10/GUIDE.md](docs/v10/GUIDE.md). Honest comparison with opencode: [docs/COMPARE-OPENCODE.md](docs/COMPARE-OPENCODE.md).
  93. ### Models, providers and local models
  94. Two providers are supported. Claude Code is the default Tier-1 provider (the `claude` CLI, or, with only `ANTHROPIC_API_KEY`, the Anthropic Agent SDK). `--provider opencode` (or `LOKI_PROVIDER=opencode`) runs opencode, which reaches any model, including OpenRouter: export `OPENROUTER_API_KEY` and pass `--model openrouter/<vendor>/<model>`; the Loki 10 engine enforces the wall-clock limit and a post-iteration guard scan there. codex, cline, aider and gemini were removed and exit 2 with a one-line migration message. To keep the Claude harness on OpenRouter or a local model, set `LOKI_MODEL_ROUTE=<kind>/<id>` (kinds: `openrouter`, `ollama`, `lmstudio`, `llamacpp`, `vllm`, `custom`) before `loki start`; `loki models --local` finds local runtimes and prints ready-to-paste values.
  95. ```bash
  96. LOKI_MODEL_ROUTE=ollama/qwen3-coder loki start prd.md
  97. OPENROUTER_API_KEY=sk-or-... loki start --provider opencode --model openrouter/deepseek/deepseek-v3.2 prd.md
  98. ```
  99. ## Control Plane
  100. A local UI and API for all your runs: outcomes, receipts, costs, and an answer box for BLOCKED runs. It is on by default (`LOKI_CONTROL=0` turns it off) and listens on 127.0.0.1 only.
  101. ```bash
  102. loki control serve # http://127.0.0.1:47821 (--port N, --db PATH)
  103. loki control backfill . # load existing ./.loki/runs
  104. loki control status # reachable? how many runs?
  105. ```
  106. While it is running, runs on this machine ship to it automatically. Guides: [container, Helm and ECS](docs/control-plane-container.md), [migrating from the old dashboard](docs/control-plane-migration.md).
  107. ## Requirements
  108. `loki doctor` enforces this list.
  109. Required:
  110. - Claude Code (`claude`), installed and logged in: the default provider and the only one with every feature. Or only `ANTHROPIC_API_KEY`: `loki "<goal>"` then runs on the bundled Agent SDK with no CLI and no extra setting, but `loki edit` and `loki chat` call the `claude` CLI.
  111. - Node.js 20+ (`node`; 22 LTS is the CI target)
  112. - Git 2.x (`git`)
  113. - `curl`
  114. Optional:
  115. - Python 3.8+ (`python3`): deep scans, Python repos and group runs use it. `loki "<goal>"` runs without it.
  116. - `jq`: `loki status` and the bash fallback CLI use it.
  117. - opencode (`--provider opencode`) for any model, including OpenRouter. Ollama or an OpenRouter key for model routes.
  118. - Bun 1.3+ (`bun`) on your PATH (the npm package also bundles it).
  119. - Docker, to run Loki from the published image.
  120. macOS and Linux are supported.
  121. ## More
  122. - [Installation](docs/INSTALLATION.md), [environment variables](docs/environment-variables.md), [exit codes](docs/exit-codes.md), [known issues](docs/KNOWN-ISSUES.md), [privacy and telemetry](docs/PRIVACY.md)
  123. - Agent Change Receipt format: [docs/AGENT-CHANGE-RECEIPT.md](docs/AGENT-CHANGE-RECEIPT.md)
  124. - Label an issue, get a pull request (GitHub Actions, no hosted service, about 15 lines of workflow): [setup](docs/v10/GUIDE.md#triggers-without-a-cloud), [full example](docs/examples/loki-issue-to-pr.yml.example)
  125. - Jira and Linear intake: [docs/trackers.md](docs/trackers.md). Two-way Slack: [docs/slack.md](docs/slack.md). Multi-repo runs and composite receipts: [docs/WORKSPACES.md](docs/WORKSPACES.md). Deploy: [docs/DEPLOY-PLAN.md](docs/DEPLOY-PLAN.md)
  126. - MCP: the bundled server exposes 48 tools over stdio (`loki mcp`), including starting a run, reading its status and verifying its receipt. One-click install: [Cursor](cursor://anysphere.cursor-deeplink/mcp/install?name=loki-mode&config=eyJjb21tYW5kIjoibnB4IiwiYXJncyI6WyIteSIsImxva2ktbW9kZSIsIm1jcCJdfQ==), [VS Code](vscode:mcp/install?%7B%22name%22%3A%22loki-mode%22%2C%22command%22%3A%22npx%22%2C%22args%22%3A%5B%22-y%22%2C%22loki-mode%22%2C%22mcp%22%5D%7D). Or run `loki mcp install --client claude|cursor|vscode|windsurf`.
  127. - Claude Code plugin: `claude plugin marketplace add asklokesh/loki-mode`, then `claude plugin install loki-mode@loki-mode`. The same marketplace ships [loki-seal](packages/loki-seal/README.md), a Stop hook that refuses "done" when tests newly fail or were weakened.
  128. - Architecture: [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md). Upgrading: [UPGRADING.md](UPGRADING.md)
  129. ## Contributing
  130. ```bash
  131. git clone https://github.com/asklokesh/loki-mode.git && cd loki-mode
  132. npm install && npm test
  133. ```
  134. See [CONTRIBUTING.md](.github/CONTRIBUTING.md), [TESTING.md](docs/TESTING.md) and the [Code of Conduct](.github/CODE_OF_CONDUCT.md).
  135. ## License
  136. [Business Source License 1.1](LICENSE). Free for personal, internal, academic and non-commercial use; it converts to Apache 2.0 on March 19, 2030. Contact founder@autonomi.dev for other arrangements.
  137. Loki Mode is built by [Autonomi](https://www.autonomi.dev/).
autonomy/hooks/validate-bash.sh · Bundled script · 1,424 lines · 5 findings marked
  1. #!/usr/bin/env bash
  2. # Loki Mode PreToolUse Hook - Bash Command Validation
  3. # Blocks dangerous commands, logs all executions
  4. set -uo pipefail
  5. INPUT=$(cat)
  6. deny() {
  7. local reason="${1:-Blocked by Loki command policy}"
  8. if command -v python3 >/dev/null 2>&1; then
  9. printf '%s' "$reason" | python3 -c '
  10. import json, sys
  11. print(json.dumps({"hookSpecificOutput": {
  12. "hookEventName": "PreToolUse",
  13. "permissionDecision": "deny",
  14. "permissionDecisionReason": sys.stdin.read(),
  15. }}))
  16. '
  17. else
  18. printf '%s' '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"Blocked by Loki command policy"}}'
  19. fi
  20. exit 2
  21. }
  22. HOST_GUARD=false
  23. case "${LOKI_HOST_GUARD:-0}" in
  24. 1|true|yes|on) HOST_GUARD=true ;;
  25. esac
  26. # P12-26d: when the resolved policy is compiled (explicit compiled, or unset with an
  27. # operator claude_permissions policy present) the compiled allow/deny list
  28. # and permission mode decide. An explicit hook "allow" skips the permission
  29. # prompt for every command that passes the blocklist, which would widen the
  30. # compiled policy, so the hook never emits one and fails closed on bad input.
  31. COMPILED_POLICY=false
  32. case "${LOKI_PERMISSION_POLICY:-}" in
  33. legacy) ;;
  34. "")
  35. _vb_helper="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." 2>/dev/null && pwd)/src/policies/claude_permissions.js"
  36. if command -v node >/dev/null 2>&1 && [ -f "$_vb_helper" ] \
  37. && [ "$(node "$_vb_helper" --has-operator-policy 2>/dev/null)" = "1" ]; then
  38. COMPILED_POLICY=true
  39. fi
  40. ;;
  41. *) COMPILED_POLICY=true ;;
  42. esac
  43. if ! command -v python3 >/dev/null 2>&1; then
  44. "$HOST_GUARD" && deny "LOKI_HOST_GUARD: python3 is unavailable, so the Bash command cannot be inspected safely."
  45. "$COMPILED_POLICY" && deny "LOKI_PERMISSION_POLICY=compiled: python3 is unavailable, so the Bash command cannot be inspected safely."
  46. printf '%s' '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"allow"}}'
  47. exit 0
  48. fi
  49. if ! COMMAND=$(printf '%s' "$INPUT" | python3 -c '
  50. import json, sys
  51. try:
  52. data = json.load(sys.stdin)
  53. command = (data.get("tool_input") or {}).get("command")
  54. except Exception:
  55. raise SystemExit(2)
  56. if not isinstance(command, str) or not command.strip():
  57. raise SystemExit(2)
  58. # Join backslash-newline continuations as bash does (an odd run of backslashes
  59. # before the newline), so every later split and pattern match sees one command.
  60. import re
  61. command = re.sub(r"(?<!\\)((?:\\\\)*)\\\r?\n", r"\1", command)
  62. sys.stdout.write(command)
  63. '); then
  64. "$HOST_GUARD" && deny "LOKI_HOST_GUARD: malformed or missing Bash command input was denied."
  65. "$COMPILED_POLICY" && deny "LOKI_PERMISSION_POLICY=compiled: malformed or missing Bash command input was denied."
  66. printf '%s' '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"allow"}}'
  67. exit 0
  68. fi
  69. CWD=$(printf '%s' "$INPUT" | python3 -c '
  70. import json, sys
  71. try:
  72. cwd = json.load(sys.stdin).get("cwd", "")
  73. except Exception:
  74. cwd = ""
  75. sys.stdout.write(cwd if isinstance(cwd, str) else "")
  76. ' 2>/dev/null || true)
  77. if "$HOST_GUARD"; then
  78. GUARD_ROOT="${LOKI_TARGET_DIR:-$PWD}"
  79. if [ -z "$CWD" ] || ! _LOKI_GUARD_CWD="$CWD" _LOKI_GUARD_ROOT="$GUARD_ROOT" python3 -c '
  80. import os
  81. cwd = os.path.realpath(os.environ["_LOKI_GUARD_CWD"])
  82. root = os.path.realpath(os.environ["_LOKI_GUARD_ROOT"])
  83. raise SystemExit(0 if cwd == root or cwd.startswith(root + os.sep) else 1)
  84. '; then
  85. deny "LOKI_HOST_GUARD: Bash command cwd is outside the assigned workspace."
  86. fi
  87. fi
  88. CWD="${CWD:-${LOKI_TARGET_DIR:-$PWD}}"
  89. # Hosted builds run on a shared host. This guard prevents recognizable direct
  90. # command mistakes involving host containers, processes, and listening ports.
  91. # It is not process isolation: an allowed package script or interpreter can
  92. # perform the same operations internally. Production builds still require a
  93. # separate container or microVM with no host Docker socket, restricted
  94. # credentials, and network policy. This parser only inspects text.
  95. if "$HOST_GUARD"; then
  96. HOST_GUARD_REASON=$(_LOKI_GUARD_COMMAND="$COMMAND" _LOKI_GUARD_INPUT="$INPUT" python3 - <<'PY'
  97. import os
  98. import re
  99. import shlex
  100. command = os.environ.get("_LOKI_GUARD_COMMAND", "")
  101. blocked = {
  102. "docker": "host Docker access",
  103. "docker-compose": "host Docker access",
  104. "kill": "host process control",
  105. "killall": "host process control",
  106. "pkill": "host process control",
  107. "sudo": "host privilege escalation",
  108. "doas": "host privilege escalation",
  109. "launchctl": "host service control",
  110. "service": "host service control",
  111. "systemctl": "host service control",
  112. }
  113. wrappers = {"command", "env", "exec", "nohup", "time"}
  114. wrapper_value_options = {
  115. "env": {"-u", "--unset", "-C", "--chdir"},
  116. "exec": {"-a"},
  117. "time": {"-f", "--format", "-o", "--output"},
  118. }
  119. shells = {"bash", "dash", "ksh", "sh", "zsh"}
  120. separators = {";", "&", "&&", "|", "||", "(", ")"}
  121. assignment = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*=.*$", re.S)
  122. def basename(value):
  123. return value.rstrip("/").rsplit("/", 1)[-1]
  124. def words(value):
  125. lexer = shlex.shlex(value.replace("\n", " ; "), posix=True,
  126. punctuation_chars=";&|()")
  127. lexer.whitespace_split = True
  128. lexer.commenters = ""
  129. return list(lexer)
  130. def first_command(segment):
  131. index = 0
  132. while index < len(segment) and assignment.match(segment[index]):
  133. index += 1
  134. while index < len(segment):
  135. name = basename(segment[index])
  136. if name not in wrappers:
  137. return name, index
  138. index += 1
  139. while index < len(segment):
  140. token = segment[index]
  141. if assignment.match(token):
  142. index += 1
  143. continue
  144. if token == "--":
  145. index += 1
  146. break
  147. if name == "env" and token in {"-S", "--split-string"}:
  148. return "__uninspectable_wrapper__", index
  149. if token in wrapper_value_options.get(name, set()):
  150. index += 2
  151. continue
  152. if token.startswith("-"):
  153. index += 1
  154. continue
  155. break
  156. return "", index
  157. def classify(value, depth=0):
  158. if depth > 2:
  159. return "command nesting exceeds the safe inspection limit"
  160. try:
  161. tokens = words(value)
  162. except ValueError:
  163. return "command text could not be parsed safely"
  164. segment = []
  165. segments = []
  166. for token in tokens:
  167. if token in separators:
  168. if segment:
  169. segments.append(segment)
  170. segment = []
  171. else:
  172. segment.append(token)
  173. if segment:
  174. segments.append(segment)
  175. for part in segments:
  176. name, index = first_command(part)
  177. if name == "__uninspectable_wrapper__":
  178. return "command wrapper cannot be inspected safely"
  179. if name in blocked:
  180. return blocked[name]
  181. args = part[index + 1:]
  182. if name in shells:
  183. for pos, arg in enumerate(args[:-1]):
  184. if arg.startswith("-") and "c" in arg:
  185. nested = classify(args[pos + 1], depth + 1)
  186. if nested:
  187. return nested
  188. if name == "xargs":
  189. for arg in args:
  190. if arg.startswith("-"):
  191. continue
  192. target = basename(arg)
  193. if target in blocked:
  194. return blocked[target]
  195. break
  196. if name == "find":
  197. for pos, arg in enumerate(args[:-1]):
  198. if arg in {"-exec", "-execdir", "-ok", "-okdir"}:
  199. target = basename(args[pos + 1])
  200. if target in blocked:
  201. return blocked[target]
  202. if name in {"bunx", "npx"} and args:
  203. target = basename(next((arg for arg in args if not arg.startswith("-")), ""))
  204. if target in blocked:
  205. return blocked[target]
  206. if name in {"npm", "pnpm", "yarn"} and args and args[0] in {"exec", "x"}:
  207. target = basename(next((arg for arg in args[1:] if not arg.startswith("-")), ""))
  208. if target in blocked:
  209. return blocked[target]
  210. if name == "brew" and args and args[0] == "services":
  211. return "host service control"
  212. if name in {"nc", "ncat", "netcat"} and any(arg == "-l" or arg.startswith("-l") for arg in args):
  213. return "host port listener"
  214. if name == "socat" and any("LISTEN:" in arg.upper() for arg in args):
  215. return "host port listener"
  216. port_assignment = re.search(
  217. r"(?<![A-Za-z0-9_])(?:PORT|HOST_PORT|LISTEN_PORT)\s*=\s*['\"]?([0-9]+)",
  218. command,
  219. )
  220. if port_assignment and int(port_assignment.group(1)) != 0:
  221. return "explicit host port claim"
  222. port_flag = re.search(
  223. r"(?:^|\s)(?:--port|--publish)(?:=|\s+)([0-9]+)",
  224. command,
  225. )
  226. if port_flag and int(port_flag.group(1)) != 0:
  227. return "explicit host port claim"
  228. http_server = re.search(r"\bhttp\.server\s+([0-9]+)", command)
  229. if http_server and int(http_server.group(1)) != 0:
  230. return "host port listener"
  231. return ""
  232. # Q1 HEREDOC-MASK: the body of a heredoc whose delimiter is quoted (<<'EOF', <<"EOF", <<-'EOF') is data: bash
  233. # expands nothing in it. Tokenizing it made a file write whose body holds one apostrophe or one double quote
  234. # unparseable, and the guard refused it ("command text could not be parsed safely"). The masker drops such a body
  235. # (and its terminator line) only when the body cannot become a command: the command reading it is cat or tee, its
  236. # output is not piped on, and no command line names a shell, eval, source, ., exec, xargs, ssh or coproc. Every
  237. # command line and every unquoted body (which expands $(...)) is kept and checked as before. It works on the
  238. # command as the tool sent it, before continuations are joined: inside a quoted body a backslash-newline is
  239. # literal, so a joined copy would end the body at a different line than bash does. On anything it does not model
  240. # (an unclosed quote, a backtick, $(, ((, <( or >( on a command line, another delimiter form, a body with no
  241. # terminator line) it gives up and the command is classified exactly as before.
  242. QUOTED_HEREDOC = re.compile(r"<<(-?)[ \t]*(['\"])([A-Za-z_][A-Za-z0-9_]*)\2(?=[\s;&|()<>]|$)")
  243. UNQUOTED_HEREDOC = re.compile(r"<<(-?)[ \t]*([A-Za-z_][A-Za-z0-9_]*)(?=[\s;&|()<>]|$)")
  244. DATA_READERS = {"cat", "tee"}
  245. RUNS_TEXT = re.compile(r"(?:^|[\s;&|()/])(?:bash|sh|zsh|dash|ksh|eval|source|exec|xargs|ssh|coproc)(?=$|[\s;&|()])|(?:^|[\s;&|()])\.(?=[ \t])")
  246. def mask_quoted_heredocs(raw):
  247. n, i, out, pending = len(raw), 0, [], []
  248. seg_start, no_pipe = 0, False
  249. while i < n:
  250. c = raw[i]
  251. if c == "\\":
  252. out.append(raw[i:i + 2])
  253. i += 2
  254. continue
  255. if c == "`" or raw.startswith(("$(", "((", "<(", ">("), i):
  256. return None
  257. if c == "'":
  258. j = raw.find("'", i + 1)
  259. if j < 0:
  260. return None
  261. out.append(raw[i:j + 1])
  262. i = j + 1
  263. continue
  264. if c == '"':
  265. j = i + 1
  266. while j < n and raw[j] != '"':
  267. if raw[j] == "`" or raw.startswith("$(", j):
  268. return None
  269. j += 2 if raw[j] == "\\" else 1
  270. if j >= n:
  271. return None
  272. out.append(raw[i:j + 1])
  273. i = j + 1
  274. continue
  275. if c == "#" and (i == 0 or raw[i - 1] in " \t\n;&|()<>"):
  276. j = raw.find("\n", i)
  277. j = n if j < 0 else j
  278. out.append(raw[i:j])
  279. i = j
  280. continue
  281. if raw.startswith("<<<", i):
  282. out.append("<<<")
  283. i += 3
  284. continue
  285. if raw.startswith("<<", i):
  286. m = QUOTED_HEREDOC.match(raw, i)
  287. if m:
  288. try:
  289. head = words(raw[seg_start:i])
  290. except ValueError:
  291. return None
  292. while head and assignment.match(head[0]):
  293. head = head[1:]
  294. if not head or basename(head[0]) not in DATA_READERS:
  295. return None
  296. pending.append((m.group(3), m.group(1) == "-", False))
  297. no_pipe = True
  298. else:
  299. m = UNQUOTED_HEREDOC.match(raw, i)
  300. if not m:
  301. return None
  302. pending.append((m.group(2), m.group(1) == "-", True))
  303. out.append(m.group(0))
  304. i = m.end()
  305. continue
  306. if c in ";&|()\n":
  307. if c == "|" and no_pipe and not raw.startswith("||", i):
  308. return None
  309. if c == "|" and raw.startswith("||", i):
  310. out.append("||")
  311. i += 2
  312. seg_start, no_pipe = i, False
  313. continue
  314. seg_start, no_pipe = i + 1, False
  315. out.append(c)
  316. i += 1
  317. if c == "\n" and pending:
  318. for delim, strip, keep in pending:
  319. while True:
  320. if i >= n:
  321. return None
  322. nl = raw.find("\n", i)
  323. stop = n if nl < 0 else nl + 1
  324. line = raw[i:stop].rstrip("\n")
  325. if keep:
  326. out.append(raw[i:stop])
  327. i = stop
  328. if (line.lstrip("\t") if strip else line) == delim:
  329. break
  330. pending = []
  331. seg_start = i
  332. if pending:
  333. return None
  334. masked = "".join(out)
  335. return None if RUNS_TEXT.search(masked) else masked
  336. def to_classify():
  337. try:
  338. import json
  339. raw = (json.loads(os.environ.get("_LOKI_GUARD_INPUT", "")).get("tool_input") or {}).get("command")
  340. except Exception:
  341. return command
  342. masked = mask_quoted_heredocs(raw.replace("\0", "")) if isinstance(raw, str) else None # bash drops NUL bytes from $COMMAND; so does this copy
  343. if masked is None:
  344. return command
  345. return re.sub(r"(?<!\\)((?:\\\\)*)\\\r?\n", r"\1", masked) # the continuation join the input step applies
  346. reason = classify(to_classify())
  347. if reason:
  348. print("LOKI_HOST_GUARD: refusing " + reason + ". Use the platform sandbox and lifecycle APIs instead.")
  349. PY
  350. )
  351. if [ -n "$HOST_GUARD_REASON" ]; then
  352. deny "$HOST_GUARD_REASON"
  353. fi
  354. fi
  355. # The supervised simple-web worker prepares dependencies and owns preview,
  356. # browser, proof, and process lifecycle. Model-launched installs, watchers, dev
  357. # servers, and Git loops only add latency or escape the bounded execution plan.
  358. # Enforce that contract at the command boundary so it applies to every model,
  359. # not only to models that follow the prompt perfectly.
  360. if [ "${LOKI_SUPERVISED_BUILD:-0}" = "1" ] \
  361. && [ "${LOKI_BUILD_PROFILE:-}" = "simple-web" ]; then
  362. SIMPLE_WEB_GUARD_REASON=$(_LOKI_SIMPLE_COMMAND="$COMMAND" \
  363. _LOKI_SIMPLE_CWD="$CWD" python3 - <<'PY'
  364. import json
  365. import os
  366. import re
  367. command = os.environ.get("_LOKI_SIMPLE_COMMAND", "")
  368. cwd = os.environ.get("_LOKI_SIMPLE_CWD", "")
  369. rules = (
  370. (
  371. re.compile(r"(?:^|[;&|()]|&&|\|\|)\s*(?:env\s+[^;&|()]*\s+)?(?:npm|pnpm|yarn|bun)\s+(?:i|install|add|update|upgrade)\b", re.I),
  372. "dependency changes are owned by the prepared scaffold",
  373. ),
  374. (
  375. re.compile(r"(?:^|[;&|()]|&&|\|\|)\s*(?:npm\s+run\s+dev|pnpm\s+(?:run\s+)?dev|yarn\s+dev|bun\s+run\s+dev|next\s+dev|vite(?:\s|$))", re.I),
  376. "development servers are owned by the preview harness",
  377. ),
  378. (
  379. re.compile(r"(?:^|[;&|()]|&&|\|\|)\s*git(?:\s|$)", re.I),
  380. "Git operations are outside the hosted simple-web implementation step",
  381. ),
  382. (
  383. re.compile(r"(?:^|\s)--watch(?:All)?(?:=\S+)?(?:\s|$)", re.I),
  384. "watch-mode tests do not terminate",
  385. ),
  386. )
  387. for pattern, reason in rules:
  388. if pattern.search(command):
  389. print(reason)
  390. raise SystemExit(0)
  391. # `npm test` can hide a watcher in package.json. Inspect only the assigned
  392. # workspace manifest and direct the model to a one-shot script when available.
  393. if re.search(r"(?:^|[;&|()]|&&|\|\|)\s*npm\s+(?:run\s+)?test(?:\s|$)", command, re.I):
  394. try:
  395. with open(os.path.join(cwd, "package.json"), encoding="utf-8") as handle:
  396. script = str((json.load(handle).get("scripts") or {}).get("test") or "")
  397. except (OSError, ValueError, TypeError):
  398. script = ""
  399. if re.search(r"(?:^|\s)--watch(?:All)?(?:=\S+)?(?:\s|$)", script, re.I):
  400. print("the package test script starts a watcher; use test:ci or a one-shot runner")
  401. raise SystemExit(0)
  402. print("")
  403. PY
  404. )
  405. if [ -n "$SIMPLE_WEB_GUARD_REASON" ]; then
  406. deny "LOKI_SIMPLE_WEB_GUARD: ${SIMPLE_WEB_GUARD_REASON}."
  407. fi
  408. fi
  409. # Dangerous command patterns (matched anywhere in the command string)
  410. # Safe paths like /tmp/ and relative paths (./) are excluded below
  411. # NOTE: This is defense-in-depth, not a security boundary. Motivated attackers
  412. # can bypass with advanced techniques (heredocs, printf, arbitrary string building).
  413. # This hook catches common mistakes and simple bypass attempts.
  414. BLOCKED_PATTERNS=(
  415. "rm -rf /"
    • Note: Quoted as data in code: Runs `rm -rf` against a root or home-directory path
    • Line 459 on GitHub
  416. "rm -rf ~"
    • Note: Quoted as data in code: Runs `rm -rf` against a root or home-directory path
    • Line 460 on GitHub
  417. "rm -rf \\\$HOME"
  418. "> /dev/sd"
  419. "mkfs[. ]"
  420. "dd if=/dev/zero"
  421. "chmod -R 777 /"
    • Note: Quoted as data in code: Sets world-writable permissions on a root path (chmod 777 /)
    • Line 465 on GitHub
  422. "eval.*base64"
  423. "base64.*\|.*sh"
  424. "base64.*\|.*bash"
  425. "\$\(base64"
  426. "eval.*\\\$\("
  427. "curl.*\|.*sh"
  428. "wget.*\|.*sh"
  429. "curl.*\|.*bash"
  430. "wget.*\|.*bash"
  431. # Config self-protection: prevent agents from corrupting internal state
  432. "rm -rf \.loki"
  433. "rm -rf \./\.loki"
  434. "rm .*\.loki/council/"
  435. "rm .*\.loki/config\.yaml"
  436. "rm .*\.loki/logs/bash-audit"
  437. "rm .*\.loki/session\.lock"
  438. "> \.loki/council/"
  439. "> \.loki/config\.yaml"
  440. # Fork bomb patterns
  441. ":\(\)\{.*\|.*&"
  442. ":\(\) *\{.*\|.*&"
  443. )
  444. # Safe path patterns that override blocked pattern matches. They are matched
  445. # per SEGMENT (the command split on ; & | newline CR), never against the whole
  446. # string: an unanchored or per-line match let `rm -rf /tmp/x; rm -rf /` and
    • Note: Quoted as data in code: Runs `rm -rf` against a root or home-directory path
    • Line 491 on GitHub
  447. # `rm -rf /tmp/x<newline>rm -rf /` ride the override. Only the segment that hit
    • Note: Quoted as data in code: Runs `rm -rf` against a root or home-directory path
    • Line 492 on GitHub
  448. # a blocked pattern must be safe; an optional trailing /dev/null redirection is
  449. # allowed, and `*` and `$$` in /tmp names. A `..` anywhere in the segment is
  450. # never safe.
  451. _SAFE_REDIR='([[:space:]]+[0-9]*>>?[[:space:]]*/dev/null)*[[:space:]]*'
  452. _SAFE_ENV='([ABCDEFGHIJKLMNOPQRSTUVWXYZ_][ABCDEFGHIJKLMNOPQRSTUVWXYZ0-9_]*=[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0-9._/:-]*[[:space:]]+)*'
  453. # A dot-leading path component with a glob char (`/tmp/.*` expands to `/tmp/..`).
  454. _DOT_GLOB='/\.[^/[:space:]]*[*?[]'
  455. SAFE_PATTERNS=(
  456. '^[[:space:]]*'"$_SAFE_ENV"'rm -rf (/tmp/([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0-9._/*-]|\$\$)+[[:space:]]*)+'"$_SAFE_REDIR"'$'
  457. '^[[:space:]]*'"$_SAFE_ENV"'rm -rf \.loki/queue/dead-letter[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0-9._/-]*'"$_SAFE_REDIR"'$'
  458. )
  459. # Env names that change what the allowed command executes or loads; a prefix
  460. # naming one never rides the safe-path override.
  461. _env_prefix_is_risky() {
  462. local rest="$1" name
  463. while [[ "$rest" =~ ^[[:space:]]*([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_][abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0-9_]*)=[^[:space:]]*(.*)$ ]]; do
  464. name="${BASH_REMATCH[1]}"
  465. rest="${BASH_REMATCH[2]}"
  466. case "$name" in
  467. PATH|LD_*|DYLD_*|BASH_ENV|ENV|IFS|PS4|SHELLOPTS|BASHOPTS|PROMPT_COMMAND|GIT_SSH_COMMAND|GIT_EXEC_PATH|NODE_OPTIONS|PYTHONPATH|PERL5OPT|GCONV_PATH|LOCPATH|EXECIGNORE) return 0 ;;
  468. esac
  469. done
  470. return 1
  471. }
  472. # Allowlist: the safe-path override applies only when EVERY segment of the
  473. # command is blank, an accepted safe rm, or a bare `NAME=value` assignment
  474. # (uppercase name not on the risky list, value free of quoting, expansion,
  475. # grouping and redirection characters). Anything else (export, declare, unset,
  476. # eval, source, hash, alias, function, subshell, echo, cd, ...) can change how a
  477. # later rm resolves, so it disables the override. Over-denial is intended.
  478. _ASSIGN_ONLY='^[[:space:]]*([ABCDEFGHIJKLMNOPQRSTUVWXYZ_][ABCDEFGHIJKLMNOPQRSTUVWXYZ_0-9]*)=([^][\"'"'"'\\$`(){}<>[:space:]]*)[[:space:]]*$'
  479. _segment_is_inert() {
  480. local seg="$1"
  481. [[ "$seg" =~ ^[[:space:]]*(true|false|:)?[[:space:]]*$ ]] && return 0
  482. if [[ "$seg" =~ $_ASSIGN_ONLY ]]; then
  483. _env_prefix_is_risky "$seg" && return 1
  484. return 0
  485. fi
  486. return 1
  487. }
  488. _all_segments_allowlisted() {
  489. local seg
  490. while IFS= read -r seg; do
  491. _segment_is_inert "$seg" && continue
  492. _segment_is_safe "$seg" || return 1
  493. done <<< "$1"
  494. return 0
  495. }
  496. _segment_is_safe() {
  497. local seg="$1" safe
  498. [[ "$seg" == *".."* ]] && return 1
  499. _env_prefix_is_risky "$seg" && return 1
  500. [[ "$seg" =~ $_DOT_GLOB ]] && return 1
  501. for safe in "${SAFE_PATTERNS[@]}"; do
  502. [[ "$seg" =~ $safe ]] && return 0
  503. done
  504. return 1
  505. }
  506. # Succeeds when every segment hitting $1 is safe and at least one segment hits
  507. # (a pattern that only matches across segments, like `curl x | sh`, is unsafe).
  508. _blocked_hits_all_safe() {
  509. local pat="$1" flat seg hit=false
  510. # Strip fd duplication (2>&1) first: its `&` is not a command separator, and removing it hides no command.
  511. flat=$(printf '%s' "$COMMAND" | sed -E 's/[0-9]*>&[0-9]+//g' | tr ';&|\r' '\n')
  512. _all_segments_allowlisted "$flat" || return 1
  513. while IFS= read -r seg; do
  514. printf '%s' "$seg" | grep -qE "$pat" || continue
  515. hit=true
  516. _segment_is_safe "$seg" || return 1
  517. done <<< "$flat"
  518. "$hit"
  519. }
  520. # A recursive rm whose target IS a temp root, or a bare glob directly under
  521. # one, wipes every tenant of the temp dir. The target is normalized first
  522. # (quotes and backslashes dropped, a glued trailing redirect cut, repeated and
  523. # trailing slashes and `/./` collapsed, `..` resolved lexically) so `//tmp`,
  524. # `/tmp/x/../*`, `/t\mp` and `"$TMPDIR"/*` cannot hide. Single-level brace
  525. # alternatives are expanded and every alternative is checked. A named child
  526. # (`/tmp/loki-run.abc123`, `/tmp/foo*`) is not a bare glob and stays ok.
  527. _normalize_rm_target() {
  528. local t="${1%%[<>]*}" prev="" comp joined="" sc
  529. local -a parts=() stack=()
  530. t="${t//[\"\'\\]/}"
  531. while [ "$t" != "$prev" ]; do
  532. prev="$t"
  533. t="${t//\/\//\/}"
  534. t="${t//\/.\//\/}"
  535. done
  536. if [[ "$t" == */* ]]; then
  537. IFS=/ read -r -a parts <<< "$t"
  538. for comp in ${parts[@]+"${parts[@]}"}; do
  539. case "$comp" in
  540. ''|.) ;;
  541. ..)
  542. # A dotdot that would pop a temp root component is
  543. # unresolvable lexically ($TMPDIR/.. is the root's parent
  544. # on some hosts): report the temp root itself (deny).
  545. joined=""
  546. for sc in ${stack[@]+"${stack[@]}"}; do joined="${joined}/${sc}"; done
  547. case "$joined" in
  548. /tmp|/var/tmp|/private/tmp|/private/var/tmp|'/$TMPDIR'|'/${TMPDIR}')
  549. printf '%s' "/tmp"
  550. return 0
  551. ;;
  552. esac
  553. joined=""
  554. if [ "${#stack[@]}" -gt 1 ] || { [ "${#stack[@]}" -eq 1 ] && [[ "$t" == /* ]]; }; then
  555. unset 'stack[${#stack[@]}-1]'
  556. fi
  557. ;;
  558. *) stack+=("$comp") ;;
  559. esac
  560. done
  561. for comp in ${stack[@]+"${stack[@]}"}; do joined="${joined}/${comp}"; done
  562. if [[ "$t" == /* ]]; then
  563. t="${joined:-/}"
  564. else
  565. t="${joined#/}"
  566. fi
  567. fi
  568. while [[ "$t" == */. && "$t" != "/." ]]; do t="${t%/.}"; done
  569. while [[ "$t" == */ && "$t" != "/" ]]; do t="${t%/}"; done
  570. printf '%s' "$t"
  571. }
  572. # Expand single-level comma brace groups into the global _ALTS array without
  573. # a subshell. Fails closed (returns 1) on nested braces, `{x..y}` ranges, or
  574. # more than 64 alternatives; the caller denies.
  575. _BRACE_CAP=64
  576. _expand_braces() {
  577. local s="$1" cur pre body post b alt
  578. local -a work=("$s")
  579. local temp=false
  580. _ALTS=()
  581. # Fail-closed rules (nested braces, ranges, cap, residual groups) apply to
  582. # every target except a plain relative path (no leading / ~ $ {, no `..`
  583. # component). The run-owned LOKI_RUN_TMP variable is also exempt.
  584. local q="${s//[\"\'\\]/}" stripped
  585. temp=true
  586. case "$q" in
  587. '$LOKI_RUN_TMP'|'$LOKI_RUN_TMP/'*|'${LOKI_RUN_TMP}'|'${LOKI_RUN_TMP}/'*) temp=false ;;
  588. /*|'~'*|'$'*|'{'*) ;;
  589. *)
  590. if [[ "/${q}/" != *"/../"* ]]; then temp=false; fi
  591. ;;
  592. esac
  593. if $temp; then
  594. # Drop parameter expansions (innermost first) so `${A:-${B}}` is not nesting.
  595. stripped="$s"
  596. while [[ "$stripped" =~ \$\{[^\{\}]*\} ]]; do
  597. stripped="${stripped/"${BASH_REMATCH[0]}"/}"
  598. done
  599. # A numeric range under a non-TMPDIR variable only appends digits, so it
  600. # cannot build a temp root (`$BUILD_DIR/shard{1..4}`).
  601. if [[ "$q" =~ ^\$\{?([A-Za-z_][A-Za-z0-9_]*) && "${BASH_REMATCH[1]}" != TMPDIR ]]; then
  602. while [[ "$stripped" =~ \{-?[0-9]+\.\.-?[0-9]+\} ]]; do
  603. stripped="${stripped/"${BASH_REMATCH[0]}"/}"
  604. done
  605. fi
  606. if [[ "$stripped" =~ \{[^\}]*\{ ]] || [[ "$stripped" =~ \{[^\}]*\.\.[^\}]*\} ]]; then
  607. return 1
  608. fi
  609. fi
  610. while [ "${#work[@]}" -gt 0 ]; do
  611. cur="${work[0]}"
  612. work=("${work[@]:1}")
  613. if [[ "$cur" =~ ^((\$\{[^\}]*\}|[^\{])*)\{([^\}]*,[^\}]*)\}(.*)$ ]]; then
  614. pre="${BASH_REMATCH[1]}"; body="${BASH_REMATCH[3]}"; post="${BASH_REMATCH[4]}"
  615. b="$body"
  616. while :; do
  617. alt="${b%%,*}"
  618. work+=("${pre}${alt}${post}")
  619. [[ "$b" == *,* ]] || break
  620. b="${b#*,}"
  621. done
  622. [ $(( ${#work[@]} + ${#_ALTS[@]} )) -le "$_BRACE_CAP" ] || { $temp && return 1; return 2; }
  623. else
  624. # A comma group the regex could not peel off stays unexpanded.
  625. if $temp && [[ "$cur" =~ \{[^\}]*,[^\}]*\} ]]; then
  626. return 1
  627. fi
  628. _ALTS+=("$cur")
  629. [ $(( ${#work[@]} + ${#_ALTS[@]} )) -le "$_BRACE_CAP" ] || { $temp && return 1; return 2; }
  630. fi
  631. done
  632. return 0
  633. }
  634. # A component is glob-only when it is only `*`, `?`, `.`, or when it holds a
  635. # `[` with no literal prefix before it (bracket parsing is ambiguous: `[!]]`,
  636. # `[]a]`, `[=a=]`, so fail closed).
  637. _glob_only() {
  638. local c="$1" pre
  639. [[ "$c" =~ ^[*?.]+$ ]] && return 0
  640. if [[ "$c" == *"["* ]]; then
  641. pre="${c%%\[*}"
  642. [[ "$pre" =~ ^[*?.]*$ ]] && return 0
  643. fi
  644. return 1
  645. }
  646. _temp_target_is_root_or_glob() {
  647. local t rest
  648. t=$(_normalize_rm_target "$1")
  649. case "$t" in
  650. /tmp|/var/tmp|/private/tmp|/private/var/tmp|'$TMPDIR'|'${TMPDIR}') return 0 ;;
  651. /private/var/tmp/*) rest="${t#/private/var/tmp/}" ;;
  652. /private/tmp/*) rest="${t#/private/tmp/}" ;;
  653. /var/tmp/*) rest="${t#/var/tmp/}" ;;
  654. /tmp/*) rest="${t#/tmp/}" ;;
  655. '$TMPDIR'/*) rest="${t#\$TMPDIR/}" ;;
  656. '${TMPDIR}'/*) rest="${t#\$\{TMPDIR\}/}" ;;
  657. /private/var/tmp*) rest="${t#/private/var/tmp}"; _glob_only "$rest"; return ;;
  658. /private/tmp*) rest="${t#/private/tmp}"; _glob_only "$rest"; return ;;
  659. /var/tmp*) rest="${t#/var/tmp}"; _glob_only "$rest"; return ;;
  660. /tmp*) rest="${t#/tmp}"; _glob_only "$rest"; return ;;
  661. '$TMPDIR'*) rest="${t#\$TMPDIR}"; _glob_only "$rest"; return ;;
  662. '${TMPDIR}'*) rest="${t#\$\{TMPDIR\}}"; _glob_only "$rest"; return ;;
  663. *) return 1 ;;
  664. esac
  665. # first component made only of glob characters, dots and bracket expressions
  666. _glob_only "${rest%%/*}" && return 0
  667. return 1
  668. }
  669. _rm_target_is_temp_root_or_glob() {
  670. local alt
  671. if [[ "$1" == *"{"* ]]; then
  672. _expand_braces "$1" || {
  673. # 1 = fail closed (temp target); 2 = over cap on a non-temp target
  674. [ "$?" -eq 2 ] && return 1
  675. return 0
  676. }
  677. for alt in ${_ALTS[@]+"${_ALTS[@]}"}; do
  678. _temp_target_is_root_or_glob "$alt" && return 0
  679. done
  680. return 1
  681. fi
  682. _temp_target_is_root_or_glob "$1"
  683. }
  684. # P12-26g-c: residual shapes that reach a temp root or the filesystem root
  685. # without naming it. Each helper returns 0 to DENY (fail closed).
  686. _RUN_TMP_RE='^/([^/]+/)*loki-run\.[A-Za-z0-9]+$'
  687. _run_tmp_valid() {
  688. local v="${LOKI_RUN_TMP:-}"
  689. [[ "$v" =~ $_RUN_TMP_RE ]] || return 1
  690. [[ "/${v}/" != *"/../"* && "/${v}/" != *"/./"* ]]
  691. }
  692. # A component that can expand to `..`: a literal `..`, one built by gluing
  693. # variables onto dots (`..$X`, `.$X.`; X may be empty or `.`), or a dot-leading
  694. # glob (`.?`, `.*`, `..*`, `.[.]`; bash < 5.2 has no globskipdots, so these
  695. # match `..`). Worst case is assumed everywhere.
  696. _is_dotdotish() {
  697. local c="$1" s="$1"
  698. [[ "$c" == *.* ]] || return 1
  699. while [[ "$s" =~ (\$\{[^\}]*\}|\$[[:alpha:]_][[:alnum:]_]*) ]]; do
  700. s="${s/"${BASH_REMATCH[0]}"/}"
  701. done
  702. [ "$s" = "." ] && [ "$s" = "$c" ] && return 1
  703. [[ "$s" =~ ^\.[.*?]*$ ]] || [[ "$s" =~ ^\.[.*?]*\[ ]]
  704. }
  705. _has_dotdot_component() {
  706. local c
  707. local -a p=()
  708. [[ "$1" == *.* ]] || return 1
  709. IFS=/ read -r -a p <<< "$1"
  710. for c in ${p[@]+"${p[@]}"}; do _is_dotdotish "$c" && return 0; done
  711. return 1
  712. }
  713. # Lexical resolve of an absolute path; `..` (or a possible `..`) at the root
  714. # stays at the root.
  715. _lex_resolve() {
  716. local comp out=""
  717. local -a parts=() stack=()
  718. IFS=/ read -r -a parts <<< "$1"
  719. for comp in ${parts[@]+"${parts[@]}"}; do
  720. case "$comp" in
  721. ''|.) ;;
  722. *)
  723. if _is_dotdotish "$comp"; then
  724. [ "${#stack[@]}" -gt 0 ] && unset 'stack[${#stack[@]}-1]'
  725. else
  726. stack+=("$comp")
  727. fi
  728. ;;
  729. esac
  730. done
  731. for comp in ${stack[@]+"${stack[@]}"}; do out="${out}/${comp}"; done
  732. printf '%s' "${out:-/}"
  733. }
  734. # Resolved path is a temp root, a bare temp glob, or an ancestor of a temp root.
  735. _resolved_reaches_temp() {
  736. case "$1" in
  737. /|/tmp|/var|/var/tmp|/private|/private/tmp|/private/var|/private/var/tmp) return 0 ;;
  738. esac
  739. _temp_target_is_root_or_glob "$1"
  740. }
  741. # `..` climbing from a known base: deny when the result leaves the base or
  742. # reaches a temp root; an unknown or relative base is unresolvable (deny).
  743. _dotdot_escapes_base() {
  744. local base="$1" rel="$2" res
  745. [[ "$base" == /* ]] || return 0
  746. base=$(_lex_resolve "$base")
  747. res=$(_lex_resolve "${base}/${rel}")
  748. _resolved_reaches_temp "$res" && return 0
  749. [[ "$res" == "$base" || "$res" == "$base"/* ]] || return 0
  750. return 1
  751. }
  752. # Absolute path an empty or defaulted variable can build: deny when it is, or
  753. # climbs to, a temp root or the filesystem root, or holds a glob root.
  754. _abs_path_deny() {
  755. _resolved_reaches_temp "$(_lex_resolve "$1")" && return 0
  756. _alt_extra_deny "$1"
  757. }
  758. _alt_extra_deny() {
  759. local a="$1" name="" mod="" rest="" rest0="" r n c1 c2 c3 w combined
  760. local multi=false
  761. if [[ "$a" =~ ^\$\{([A-Za-z_][A-Za-z0-9_]*)([^\}]*)\}(.*)$ ]]; then
  762. name="${BASH_REMATCH[1]}"; mod="${BASH_REMATCH[2]}"; rest="${BASH_REMATCH[3]}"
  763. elif [[ "$a" =~ ^\$([A-Za-z_][A-Za-z0-9_]*)(.*)$ ]]; then
  764. name="${BASH_REMATCH[1]}"; rest="${BASH_REMATCH[2]}"
  765. fi
  766. rest0="$rest"
  767. if [ -n "$name" ]; then
  768. # Strip every further leading expansion ($X$Y/..., "$X""$Y"/...).
  769. while [[ "$rest" =~ ^(\$\{[[:alpha:]_][[:alnum:]_]*[^\}]*\}|\$[[:alpha:]_][[:alnum:]_]*)(.*)$ ]]; do
  770. multi=true
  771. rest="${BASH_REMATCH[2]}"
  772. done
  773. # A default or alternative word can supply the root: ${X:-/}tmp/*,
  774. # ${X:-/tmp}. Check the word plus the rest as the path it can build.
  775. if [ -n "$mod" ] && [ "$name" != "LOKI_RUN_TMP" ]; then
  776. case "$mod" in
  777. :-*|:=*|:+*) w="${mod:2}" ;;
  778. -*|=*|+*) w="${mod:1}" ;;
  779. *) w="" ;;
  780. esac
  781. combined="${w}${rest0}"
  782. if [[ "$combined" == /* ]]; then
  783. _abs_path_deny "$combined" && return 0
  784. fi
  785. fi
  786. case "$name" in
  787. LOKI_RUN_TMP*)
  788. [ "$name" = "LOKI_RUN_TMP" ] || return 0
  789. [ -z "$mod" ] || return 0
  790. "$multi" && return 0
  791. _run_tmp_valid || return 0
  792. [ -z "$rest" ] && return 1
  793. [[ "$rest" == /* ]] || return 0
  794. _has_dotdot_component "$rest" && return 0
  795. [[ "$rest" =~ $_DOT_GLOB ]] && return 0
  796. return 1
  797. ;;
  798. TMPDIR) "$multi" || return 1 ;;
  799. HOME)
  800. if ! "$multi"; then
  801. _has_dotdot_component "$rest" || return 1
  802. _dotdot_escapes_base "${HOME:-}" "$rest"
  803. return
  804. fi
  805. ;;
  806. esac
  807. _has_dotdot_component "$rest" && return 0
  808. if [[ "$rest" == /* ]]; then
  809. r="$rest"
  810. while [[ "$r" == /* || "$r" == ./* ]]; do r="${r#/}"; r="${r#./}"; done
  811. case "$r" in ''|'*'*|'?'*|'['*|'{'*) return 0 ;; esac
  812. # An unset variable makes the rest an absolute path: deny when it
  813. # is, or climbs to, a temp root, or any component holds a glob.
  814. # The only brace left after expansion that stays ok is a numeric
  815. # range glued to a literal prefix in the last component.
  816. local lastc="${rest##*/}" headp="${rest%/*}"
  817. [[ "$headp" == *[\*\?\[\{]* ]] && return 0
  818. [[ "$lastc" == *[\*\?\[]* ]] && return 0
  819. if [[ "$lastc" == *"{"* ]]; then
  820. [[ "$lastc" =~ ^[^\{]+\{-?[0-9]+\.\.-?[0-9]+\}$ ]] || return 0
  821. fi
  822. _resolved_reaches_temp "$(_lex_resolve "$rest")" && return 0
  823. fi
  824. return 1
  825. fi
  826. # shellcheck disable=SC2088 # literal tilde in a string being inspected
  827. case "$a" in
  828. '~'|'~/'*)
  829. rest="${a#\~}"
  830. _has_dotdot_component "$rest" || return 1
  831. _dotdot_escapes_base "${HOME:-}" "$rest"
  832. return
  833. ;;
  834. '~+'*|'~-'*)
  835. # PWD / OLDPWD: OLDPWD is unknown, so a glob, dotdot or bare form denies.
  836. rest="${a#\~?}"
  837. [ -z "$rest" ] && return 0
  838. [[ "$rest" == /* ]] || return 1
  839. r="${rest#/}"
  840. case "$r" in ''|'*'*|'?'*|'['*|'{'*) return 0 ;; esac
  841. _has_dotdot_component "$rest" && return 0
  842. return 1
  843. ;;
  844. '$'*|'{'*) return 1 ;;
  845. /*)
  846. n="$a"
  847. r=""
  848. while [ "$n" != "$r" ]; do r="$n"; n="${n//\/\//\/}"; n="${n//\/.\//\/}"; done
  849. if _has_dotdot_component "$n"; then
  850. n=$(_lex_resolve "$n")
  851. _resolved_reaches_temp "$n" && return 0
  852. fi
  853. n="${n#/}"
  854. c1="${n%%/*}"
  855. c2=""
  856. [[ "$n" == */* ]] && { c2="${n#*/}"; c2="${c2%%/*}"; }
  857. case "$c1" in *[\*\?\[]*) return 0 ;; esac
  858. case "$c2" in *[\*\?\[]*) [ "$c1" = "tmp" ] || return 0 ;; esac
  859. if [ "$c1" = "private" ] && [ "$c2" = "var" ]; then
  860. c3="${n#*/}"; c3="${c3#*/}"; c3="${c3%%/*}"
  861. case "$c3" in *[\*\?\[]*) return 0 ;; esac
  862. fi
  863. return 1
  864. ;;
  865. esac
  866. _has_dotdot_component "$a" || return 1
  867. _dotdot_escapes_base "$CWD" "$a"
  868. }
  869. # Over-cap groups cannot be enumerated: deny when any dot touches a brace,
  870. # comma, glob, bracket or variable (a possible `..`), or a `*` alternative.
  871. _OVERCAP_RE='\.\.|[{,]\.|\.[{},]|\.[^/,{}]*[*?[$]|[{,]\*[,}]'
  872. _rm_target_extra_deny() {
  873. local q="${1%%[<>]*}" alt rc=0
  874. q="${q//[\"\'\\]/}"
  875. local -a alts=("$q")
  876. if [[ "$q" == *"{"* ]]; then
  877. _expand_braces "$q" || rc=$?
  878. [ "$rc" -eq 1 ] && return 0
  879. # Over cap (rc 2): a `.` or `..` alternative or a dot glued to a group
  880. # can build a dotdot component bash will expand; fail closed.
  881. if [ "$rc" -eq 2 ] && [[ "$q" =~ $_OVERCAP_RE ]]; then
  882. return 0
  883. fi
  884. [ "$rc" -eq 0 ] && alts=(${_ALTS[@]+"${_ALTS[@]}"})
  885. fi
  886. local stripped
  887. for alt in ${alts[@]+"${alts[@]}"}; do
  888. # A comma group the expander could not peel (a parameter expansion
  889. # inside it) is unenumerable: fail closed.
  890. if [ "$rc" -eq 0 ] && [[ "$alt" == *"{"* ]]; then
  891. stripped="$alt"
  892. while [[ "$stripped" =~ \$\{[^\{\}]*\} ]]; do
  893. stripped="${stripped/"${BASH_REMATCH[0]}"/}"
  894. done
  895. [[ "$stripped" =~ \{[^\}]*,[^\}]*\} ]] && return 0
  896. fi
  897. _alt_extra_deny "$alt" && return 0
  898. done
  899. return 1
  900. }
  901. # P12-26g-d: allow-grammar for recursive rm targets. A target is allowed only
  902. # when, after quotes and braces are removed, it matches one of the shapes
  903. # below; anything else denies (fail closed). It runs BEFORE the older
  904. # temp-root checks, which still run on every target the grammar lets through.
  905. # (a) plain relative path: first component not a glob, never climbs above start
  906. # (b) absolute path outside every temp root and its ancestors, no glob in the
  907. # first two components (except /tmp/<name>*), no expansion inside it
  908. # (c) "$LOKI_RUN_TMP"[/...] with a valid LOKI_RUN_TMP
  909. # (d) $TMPDIR or ${TMPDIR} with no modifier, then /<non-glob name>
  910. # (e) $HOME or ~ then /..., staying inside HOME and off every temp root
  911. # (f) another named variable, then /<non-glob, non-dotdot name>
  912. # Denied outright: positional or special parameters, ${!X}, ${#X}, $'..',
  913. # $"..", command substitution, nested ${..${..}}, any ${X<modifier>} other than
  914. # a whole-target :- or - default, and any expansion inside an absolute path.
  915. # Every function below returns 0 to DENY.
  916. _G_CAP=64
  917. _g_expand() {
  918. local cur pre body post lo hi i span b alt
  919. local -a work=("$1")
  920. _G_ALTS=()
  921. while [ "${#work[@]}" -gt 0 ]; do
  922. cur="${work[0]}"
  923. work=("${work[@]:1}")
  924. # A leading sentinel keeps the regex free of an empty alternation.
  925. if [[ "@${cur}" =~ ^(.*[^$])\{([^\{\}]*)\}(.*)$ ]]; then
  926. pre="${BASH_REMATCH[1]#@}"; body="${BASH_REMATCH[2]}"; post="${BASH_REMATCH[3]}"
  927. if [[ "$body" =~ ^(-?[0-9]+)\.\.(-?[0-9]+)$ ]]; then
  928. lo="${BASH_REMATCH[1]}"; hi="${BASH_REMATCH[2]}"
  929. [ "${#lo}" -le 6 ] && [ "${#hi}" -le 6 ] || return 2
  930. case "$lo" in -*) lo=$(( -(10#${lo#-}) )) ;; *) lo=$(( 10#$lo )) ;; esac
  931. case "$hi" in -*) hi=$(( -(10#${hi#-}) )) ;; *) hi=$(( 10#$hi )) ;; esac
  932. span=$(( hi - lo )); [ "$span" -lt 0 ] && span=$(( -span ))
  933. [ "$span" -lt "$_G_CAP" ] || return 2
  934. i="$lo"
  935. while :; do
  936. work+=("${pre}${i}${post}")
  937. [ "$i" -eq "$hi" ] && break
  938. if [ "$lo" -le "$hi" ]; then i=$(( i + 1 )); else i=$(( i - 1 )); fi
  939. done
  940. elif [[ "$body" == *,* ]]; then
  941. b="$body"
  942. while :; do
  943. alt="${b%%,*}"
  944. work+=("${pre}${alt}${post}")
  945. [[ "$b" == *,* ]] || break
  946. b="${b#*,}"
  947. done
  948. else
  949. return 1
  950. fi
  951. else
  952. [[ "$cur" =~ (^|[^$])\{ ]] && return 1
  953. _G_ALTS+=("$cur")
  954. fi
  955. [ $(( ${#work[@]} + ${#_G_ALTS[@]} )) -le "$_G_CAP" ] || return 2
  956. done
  957. return 0
  958. }
  959. # After the first variable, a component that is exactly $name or ${name}
  960. # (not TMPDIR or HOME) is folded to a plain word; the glob and dotdot checks
  961. # then judge the folded path.
  962. _g_fold_vars() {
  963. local rest="$1" c out="" n
  964. local -a p=()
  965. [[ "$rest" == /* ]] || { printf '%s' "$rest"; return; }
  966. IFS=/ read -r -a p <<< "${rest#/}"
  967. for c in ${p[@]+"${p[@]}"}; do
  968. if [[ "$c" =~ ^\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?$ ]]; then
  969. n="${BASH_REMATCH[1]}"
  970. case "$n" in TMPDIR|HOME) ;; *) c="v" ;; esac
  971. fi
  972. out="${out}/${c}"
  973. done
  974. [[ "$rest" == */ ]] && out="${out}/"
  975. printf '%s' "$out"
  976. }
  977. # Rest after a variable: "/" then a non-glob first component, and no
  978. # dotdot-capable component anywhere.
  979. _g_named_rest_deny() {
  980. local rest="$1" c first=true
  981. local -a p=()
  982. [[ "$rest" == /* ]] || return 0
  983. IFS=/ read -r -a p <<< "${rest#/}"
  984. [ "${#p[@]}" -gt 0 ] || return 0
  985. for c in "${p[@]}"; do
  986. _is_dotdotish "$c" && return 0
  987. if $first; then
  988. [ -n "$c" ] || return 0
  989. [[ "$c" == *[\*\?\[]* ]] && return 0
  990. first=false
  991. fi
  992. done
  993. return 1
  994. }
  995. _g_home_deny() {
  996. local rest="$1" home="${HOME:-}" hr res
  997. [[ "$home" == /* ]] || return 0
  998. [ -n "$rest" ] || return 0
  999. [[ "$rest" == /* ]] || return 0
  1000. hr=$(_lex_resolve "$home")
  1001. [ "$hr" != "/" ] || return 0
  1002. _resolved_reaches_temp "$hr" && return 0
  1003. res=$(_lex_resolve "${hr}${rest}")
  1004. _resolved_reaches_temp "$res" && return 0
  1005. [[ "$res" == "$hr"/* ]] || return 0
  1006. return 1
  1007. }
  1008. _g_abs_deny() {
  1009. local a="$1" r n c1 c2 c3
  1010. [[ "$a" == *'$'* ]] && return 0
  1011. [[ "$a" =~ $_DOT_GLOB ]] && return 0
  1012. r=$(_lex_resolve "$a")
  1013. _resolved_reaches_temp "$r" && return 0
  1014. n="${r#/}"
  1015. c1="${n%%/*}"
  1016. c2=""
  1017. [[ "$n" == */* ]] && { c2="${n#*/}"; c2="${c2%%/*}"; }
  1018. case "$c1" in *[\*\?\[]*) return 0 ;; esac
  1019. case "$c2" in *[\*\?\[]*) [ "$c1" = "tmp" ] || return 0 ;; esac
  1020. if [ "$c1" = "private" ] && [ "$c2" = "var" ]; then
  1021. c3="${n#*/}"; c3="${c3#*/}"; c3="${c3%%/*}"
  1022. case "$c3" in *[\*\?\[]*) return 0 ;; esac
  1023. fi
  1024. return 1
  1025. }
  1026. _g_rel_deny() {
  1027. local a="$1" c depth=0 seen=false
  1028. local -a p=()
  1029. [[ "$a" == *'$'* ]] && return 0
  1030. IFS=/ read -r -a p <<< "$a"
  1031. for c in ${p[@]+"${p[@]}"}; do
  1032. case "$c" in
  1033. '') continue ;;
  1034. .) seen=true; continue ;;
  1035. ..) depth=$(( depth - 1 )); [ "$depth" -ge 0 ] || return 0; continue ;;
  1036. esac
  1037. _is_dotdotish "$c" && return 0
  1038. if ! $seen; then
  1039. seen=true
  1040. case "$c" in [\*\?\[]*) return 0 ;; esac
  1041. fi
  1042. depth=$(( depth + 1 ))
  1043. done
  1044. [ "$depth" -gt 0 ] && return 1
  1045. return 0
  1046. }
  1047. _g_alt_deny() {
  1048. local a="$1" name rest w
  1049. [ -n "$a" ] || return 0
  1050. # Whole-target default: ${N:-W} or ${N-W}, N not TMPDIR, W a variable, a
  1051. # relative word with no leading glob, or an allowed absolute path.
  1052. if [[ "$a" =~ ^\$\{([A-Za-z_][A-Za-z0-9_]*)(:-|-)(.*)\}$ ]]; then
  1053. name="${BASH_REMATCH[1]}"; w="${BASH_REMATCH[3]}"
  1054. [ "$name" != "TMPDIR" ] && [ "$name" != "HOME" ] || return 0
  1055. [ -n "$w" ] || return 0
  1056. if [[ "$w" =~ ^\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?$ ]]; then
  1057. [ "${BASH_REMATCH[1]}" != "TMPDIR" ] && [ "${BASH_REMATCH[1]}" != "HOME" ] || return 0
  1058. return 1
  1059. fi
  1060. [[ "$w" == *'$'* || "$w" == *'{'* || "$w" == *'}'* ]] && return 0
  1061. _g_alt_deny "$w"
  1062. return
  1063. fi
  1064. # ${TMPDIR:-<temp root>}/<named child>
  1065. if [[ "$a" =~ ^\$\{TMPDIR:-(/[^\}\$]*)\}(/.*)$ ]]; then
  1066. w="${BASH_REMATCH[1]}"; rest="${BASH_REMATCH[2]}"
  1067. case "$(_lex_resolve "$w")" in
  1068. /tmp|/var/tmp|/private/tmp|/private/var/tmp) ;;
  1069. *) return 0 ;;
  1070. esac
  1071. [[ "$rest" == *'$'* ]] && return 0
  1072. _g_named_rest_deny "$rest"
  1073. return
  1074. fi
  1075. # ${N:?} and ${N:?msg} guards behave exactly like a bare ${N}.
  1076. if [[ "$a" =~ ^\$\{([A-Za-z_][A-Za-z0-9_]*):\?[^\}\$\{]*\}(.*)$ ]]; then
  1077. a="\${${BASH_REMATCH[1]}}${BASH_REMATCH[2]}"
  1078. fi
  1079. # shellcheck disable=SC2088 # literal tilde in a string being inspected
  1080. case "$a" in
  1081. '$'*)
  1082. if [[ "$a" =~ ^\$\{([A-Za-z_][A-Za-z0-9_]*)\}(.*)$ ]] || [[ "$a" =~ ^\$([A-Za-z_][A-Za-z0-9_]*)(.*)$ ]]; then
  1083. name="${BASH_REMATCH[1]}"; rest="${BASH_REMATCH[2]}"
  1084. else
  1085. return 0
  1086. fi
  1087. case "$name" in
  1088. LOKI_RUN_TMP|HOME|TMPDIR) ;;
  1089. *) rest=$(_g_fold_vars "$rest") ;;
  1090. esac
  1091. [[ "$rest" == *'$'* || "$rest" == *'{'* || "$rest" == *'}'* ]] && return 0
  1092. [ -z "$rest" ] || [[ "$rest" == /* ]] || return 0
  1093. case "$name" in
  1094. LOKI_RUN_TMP)
  1095. _run_tmp_valid || return 0
  1096. [ -z "$rest" ] && return 1
  1097. _has_dotdot_component "$rest" && return 0
  1098. [[ "$rest" =~ $_DOT_GLOB ]] && return 0
  1099. return 1
  1100. ;;
  1101. HOME) _g_home_deny "$rest"; return ;;
  1102. *)
  1103. # A whole-target named variable (rm -r "$dir") is allowed;
  1104. # TMPDIR bare names the temp root itself.
  1105. if [ -z "$rest" ]; then
  1106. [ "$name" != "TMPDIR" ] || return 0
  1107. return 1
  1108. fi
  1109. _g_named_rest_deny "$rest"
  1110. return
  1111. ;;
  1112. esac
  1113. ;;
  1114. '~'|'~/'*)
  1115. [[ "$a" == *'$'* ]] && return 0
  1116. _g_home_deny "${a#\~}"
  1117. return
  1118. ;;
  1119. '~'*|'{'*|*'{'*|*'}'*) return 0 ;;
  1120. /*) _g_abs_deny "$a"; return ;;
  1121. esac
  1122. _g_rel_deny "$a"
  1123. }
  1124. _rm_grammar_deny() {
  1125. local t="${1%%[<>]*}" q alt rc=0
  1126. [ -n "$t" ] || return 1
  1127. # $' and $" open ANSI-C / locale quoting; a $$ before the quote is a PID.
  1128. [[ "$t" =~ (^|[^$])\$[\'\"] ]] && return 0
  1129. q="${t//[\"\'\\]/}"
  1130. # find -exec rm -r {} + / \; : a bare {} is find's placeholder, not a brace.
  1131. [ "$q" = "{}" ] && return 1
  1132. [[ "$q" == *'`'* || "$q" == *'$('* ]] && return 0
  1133. # $$ is a PID only mid-component (/tmp/test-$$); a leading $$ denies below.
  1134. while [[ "$q" =~ ^(.*[^/\$])\$\$(.*)$ ]]; do q="${BASH_REMATCH[1]}0${BASH_REMATCH[2]}"; done
  1135. local -a alts=("$q")
  1136. if [[ "$q" == *"{"* ]]; then
  1137. _g_expand "$q" || rc=$?
  1138. [ "$rc" -eq 1 ] && return 0
  1139. if [ "$rc" -eq 2 ]; then
  1140. # Over the cap: only a named relative first component, no
  1141. # expansion and no dot trick, can be left unexpanded.
  1142. [[ "$q" =~ ^[A-Za-z0-9_-][A-Za-z0-9._-]*/ ]] || return 0
  1143. [[ "$q" == *'$'* ]] && return 0
  1144. [[ "$q" =~ $_OVERCAP_RE ]] && return 0
  1145. return 1
  1146. fi
  1147. alts=(${_G_ALTS[@]+"${_G_ALTS[@]}"})
  1148. fi
  1149. for alt in ${alts[@]+"${alts[@]}"}; do
  1150. _g_alt_deny "$alt" && return 0
  1151. done
  1152. return 1
  1153. }
  1154. # `rm`, `/bin/rm`, `\rm`, `"rm"` all name rm.
  1155. _is_rm_token() {
  1156. local x="${1//[\"\'\\]/}"
  1157. [ "${x##*/}" = "rm" ]
  1158. }
  1159. _segment_rm_hits_temp_root() {
  1160. local seg="$1" tok recursive=false seen_rm=false
  1161. local -a words
  1162. # Command substitution and grouping open a new command word: split on them.
  1163. [[ "$seg" == *'`'* ]] && seg="${seg//\`/ \$( }"
  1164. seg="${seg//[()\`]/ }"
  1165. read -r -a words <<< "$seg"
  1166. for tok in "${words[@]}"; do
  1167. if ! $seen_rm; then
  1168. _is_rm_token "$tok" && seen_rm=true
  1169. continue
  1170. fi
  1171. case "$tok" in
  1172. --recursive) recursive=true ;;
  1173. -*) [[ "$tok" =~ ^-[[:alpha:]]*[rR] ]] && recursive=true ;;
  1174. esac
  1175. done
  1176. $seen_rm && $recursive || return 1
  1177. seen_rm=false
  1178. for tok in "${words[@]}"; do
  1179. if ! $seen_rm; then
  1180. _is_rm_token "$tok" && seen_rm=true
  1181. continue
  1182. fi
  1183. case "$tok" in -*) continue ;; esac
  1184. [ -n "${tok//[\"\'\\]/}" ] || continue
  1185. # Fail closed before the hook timeout (Claude Code treats it as allow).
  1186. [ $((SECONDS - _RM_T0)) -ge "$_RM_BUDGET" ] && return 0
  1187. _rm_grammar_deny "$tok" && return 0
  1188. _rm_target_is_temp_root_or_glob "$tok" && return 0
  1189. _rm_target_extra_deny "$tok" && return 0
  1190. done
  1191. return 1
  1192. }
  1193. _RM_T0=$SECONDS
  1194. _RM_BUDGET="${LOKI_RM_GUARD_BUDGET_SECS:-20}"
  1195. case "$_RM_BUDGET" in ""|*[!0-9]*) _RM_BUDGET=20 ;; esac
  1196. # P12-26g-d: inspect only real command words. Quoted data of a final-in-pipeline
  1197. # printf/echo/cat (and a cat heredoc body) is masked; any construct that can run
  1198. # data as code (eval, shells, $( ), backticks, pipes into them) disables masking.
  1199. _INSPECT=$(_LOKI_MASK_CMD="$COMMAND" python3 - <<'PY'
  1200. import os
  1201. import re
  1202. import sys
  1203. cmd = os.environ.get("_LOKI_MASK_CMD", "")
  1204. DATA = {"printf", "echo", "cat"}
  1205. RISKY_WORD = re.compile(
  1206. r"(^|[^\w./-])(eval|source|sh|bash|zsh|dash|ksh|xargs|exec|python3?|perl|ruby|node|ssh|find|parallel)(?![\w-])"
  1207. )
  1208. DOT_CMD = re.compile(r"(^|[;&|\n(]\s*)\.\s")
  1209. def emit(text):
  1210. sys.stdout.write(text)
  1211. raise SystemExit(0)
  1212. # Any construct that can turn data into code disables masking entirely.
  1213. # ANSI-C / locale quoting ($' $") hides quote boundaries from this walker.
  1214. if re.search(r"\x60|\$\(|<\(|>\(|\$['\"]", cmd) or RISKY_WORD.search(cmd) or DOT_CMD.search(cmd):
  1215. emit(cmd)
  1216. n = len(cmd)
  1217. out = list(cmd)
  1218. HEREDOC = re.compile(r"(?<!<)<<(?!<)-?\s*([\"']?)([A-Za-z_]\w*)\1")
  1219. ASSIGN = re.compile(r"^\s*[A-Za-z_]\w*=\S*\s+")
  1220. def is_data(text):
  1221. while ASSIGN.match(text):
  1222. text = ASSIGN.sub("", text, count=1)
  1223. m = re.match(r"\s*(\S+)", text)
  1224. if not m or m.group(1) not in DATA:
  1225. return False
  1226. if m.group(1) == "printf" and re.search(r"(^|\s)-v", text):
  1227. return False
  1228. return True
  1229. def mask(s, e):
  1230. for k in range(s, e):
  1231. if out[k] != "\n":
  1232. out[k] = "x"
  1233. i = 0
  1234. unit_start = 0
  1235. spans = []
  1236. pending = [] # (delimiter, strip_tabs, mask_body)
  1237. unit_hd = [] # heredocs opened by an unquoted << in the current unit
  1238. def end_unit(end, piped):
  1239. data = is_data(cmd[unit_start:end]) and not piped
  1240. if data:
  1241. for s, e in spans:
  1242. mask(s, e)
  1243. for delim, strip in unit_hd:
  1244. pending.append((delim, strip, data))
  1245. while i < n:
  1246. c = cmd[i]
  1247. if c == "\\":
  1248. i += 2
  1249. continue
  1250. if c == "#" and (i == 0 or cmd[i - 1] in " \t;&|()\n"):
  1251. j = cmd.find("\n", i)
  1252. i = n if j < 0 else j
  1253. continue
  1254. if c == "<" and cmd.startswith("<<", i):
  1255. m = HEREDOC.match(cmd, i)
  1256. if m:
  1257. unit_hd.append((m.group(2), cmd.startswith("<<-", i)))
  1258. i = m.end()
  1259. continue
  1260. if c == "'":
  1261. j = cmd.find("'", i + 1)
  1262. if j < 0:
  1263. emit(cmd)
  1264. spans.append((i + 1, j))
  1265. i = j + 1
  1266. continue
  1267. if c == '"':
  1268. j = i + 1
  1269. while j < n and cmd[j] != '"':
  1270. j += 2 if cmd[j] == "\\" else 1
  1271. if j >= n:
  1272. emit(cmd)
  1273. spans.append((i + 1, j))
  1274. i = j + 1
  1275. continue
  1276. if c in ";&|\n":
  1277. piped = c == "|" and cmd[i + 1:i + 2] != "|"
  1278. end_unit(i, piped)
  1279. spans = []
  1280. unit_hd = []
  1281. if c == "\n" and pending:
  1282. pos = i + 1
  1283. for delim, strip, do_mask in pending:
  1284. while pos < n:
  1285. nl = cmd.find("\n", pos)
  1286. stop = n if nl < 0 else nl
  1287. line = cmd[pos:stop]
  1288. if (line.lstrip("\t") if strip else line) == delim:
  1289. pos = stop
  1290. break
  1291. if do_mask:
  1292. mask(pos, stop)
  1293. pos = stop + 1
  1294. pending = []
  1295. i = pos
  1296. unit_start = i
  1297. continue
  1298. i += 1
  1299. unit_start = i
  1300. continue
  1301. i += 1
  1302. end_unit(n, False)
  1303. emit("".join(out))
  1304. PY
  1305. ) || _INSPECT="$COMMAND"
  1306. [ -n "$_INSPECT" ] || _INSPECT="$COMMAND"
  1307. _flat_for_temp_root=$(printf '%s' "$_INSPECT" | sed -E 's/[0-9]*>&[0-9]+//g' | tr ';&|\r' '\n')
  1308. while IFS= read -r _seg; do
  1309. if _segment_rm_hits_temp_root "$_seg"; then
  1310. deny "Blocked: recursive delete of a temp root or a bare glob under it"
  1311. fi
  1312. done <<< "$_flat_for_temp_root"
  1313. # Check for blocked patterns
  1314. for pattern in "${BLOCKED_PATTERNS[@]}"; do
  1315. if echo "$COMMAND" | grep -qE "$pattern"; then
  1316. _blocked_hits_all_safe "$pattern" && continue
  1317. deny "Blocked: potentially dangerous command pattern detected"
  1318. fi
  1319. done
  1320. # Log command to audit trail
  1321. LOG_DIR="$CWD/.loki/logs"
  1322. mkdir -p "$LOG_DIR"
  1323. printf '%s' "{\"timestamp\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"command\":$(echo "$COMMAND" | python3 -c 'import sys,json; print(json.dumps(sys.stdin.read()))')}" >> "$LOG_DIR/bash-audit.jsonl"
  1324. echo >> "$LOG_DIR/bash-audit.jsonl"
  1325. # Allow command. Compiled policy: no decision, the permission flow decides.
  1326. if "$COMPILED_POLICY"; then
  1327. exit 0
  1328. fi
  1329. printf '%s' '{"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "allow"}}'
  1330. exit 0
autonomy/lib/voter-agents.sh · Bundled script · 584 lines · 1 finding marked
  1. #!/usr/bin/env bash
  2. # autonomy/lib/voter-agents.sh -- Phase C (v7.5.20) bash side.
  3. #
  4. # Builds the JSON declaration of the council voter agents and orchestrates a
  5. # single `claude --agents <json> --json-schema <inline-content>` dispatch per iteration.
  6. # Replaces the ad-hoc per-voter heuristic loop in council_aggregate_votes when
  7. # the locally installed Claude CLI supports both flags.
  8. #
  9. # Vote enum (per architect, BINDING):
  10. # APPROVE | REJECT | CANNOT_VALIDATE
  11. #
  12. # Translation to the legacy aggregator shape used by council_evaluate and
  13. # council_write_transcript:
  14. # APPROVE -> COMPLETE
  15. # REJECT -> CONTINUE
  16. # CANNOT_VALIDATE -> CONTINUE (mirrors managed council _vote_to_legacy)
  17. #
  18. # Public API (all functions are pure: read env + filesystem, write stdout).
  19. # loki_voter_agents_json -- emits the 3-voter agents JSON
  20. # loki_devils_advocate_json <summary> -- emits a one-key JSON for the DA
  21. # loki_finding_schema_path -- echoes absolute path to schema
  22. # loki_council_dispatch_agents <iter> <prd_path>
  23. # -- runs the single claude call,
  24. # writes verdict files and the
  25. # aggregator round file. Returns 0
  26. # on success, 1 on any fallback
  27. # condition (caller falls through
  28. # to existing heuristic dispatch).
  29. #
  30. # Inline Python: the dispatch runs with the cwd inside the agent's repo, so
  31. # every python3 here drops '' and '.' from sys.path before any other import; a
  32. # committed json.py or sitecustomize.py cannot read the votes.
  33. # Pinned by tests/test-voter-agents-json.sh (Case D).
  34. #
  35. # S-202 (BACKLOG 54): -E alone still loads the user site-packages, whose .pth
  36. # "import" lines run before that scrub and can forge json.loads (3 REJECT
  37. # findings read as a COMPLETE round). Every call site resolves its interpreter
  38. # through _loki_snapshot_py_tool and runs it -I -S; when none resolves, the
  39. # dispatch returns 1 and completion-council's fail-closed path applies. run.sh
  40. # is the source of truth for the helper; the guarded copy below only serves
  41. # callers that source this file on its own. Keep it byte-identical to run.sh's.
  42. # Pinned by tests/test-voter-agents-no-user-site-pth.sh.
  43. # Guard against double-source.
  44. if [ "${__LOKI_VOTER_AGENTS_SH_LOADED:-0}" = "1" ]; then
  45. return 0 2>/dev/null || true
  46. fi
  47. __LOKI_VOTER_AGENTS_SH_LOADED=1
  48. declare -F _loki_snapshot_py_tool >/dev/null 2>&1 || \
  49. _loki_snapshot_py_tool() {
  50. local c
  51. for c in /usr/bin/python3 /bin/python3; do
  52. [ -x "$c" ] && [ ! -d "$c" ] && "$c" -I -S -c '' >/dev/null 2>&1 && { printf '%s\n' "$c"; return 0; }
  53. done
  54. local dir
  55. local IFS=:
  56. for dir in $PATH; do
  57. case "$dir" in
  58. /*) ;;
  59. *) continue ;;
  60. esac
  61. if [ -x "$dir/python3" ] && [ ! -d "$dir/python3" ] \
  62. && "$dir/python3" -I -S -c '' >/dev/null 2>&1; then
  63. printf '%s\n' "$dir/python3"
  64. return 0
  65. fi
  66. done
  67. return 1
  68. }
  69. # Resolve repo root once. This file lives at autonomy/lib/voter-agents.sh.
  70. __LOKI_VA_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd)"
  71. __LOKI_VA_REPO_ROOT="$(cd "${__LOKI_VA_LIB_DIR}/../.." 2>/dev/null && pwd)"
  72. # ---------- Voter agents JSON ----------
  73. # Emits a top-level JSON object with exactly 3 keys (the 3 base voters).
  74. # Required env:
  75. # LOKI_ITER -- iteration number (int-ish)
  76. # LOKI_PRD_PATH -- PRD path (may be empty)
  77. # Optional env:
  78. # LOKI_RARV_TIER -- planning|development|fast (informational only)
  79. #
  80. # Returns 0 always. Emits `{}` when required env missing.
  81. loki_voter_agents_json() {
  82. local iter="${LOKI_ITER:-}"
  83. if [ -z "$iter" ]; then
  84. printf '%s' '{}'
  85. return 0
  86. fi
  87. local prd="${LOKI_PRD_PATH:-}"
  88. local tier="${LOKI_RARV_TIER:-development}"
  89. local _va_py
  90. _va_py="$(_loki_snapshot_py_tool)" || { printf '%s' '{}'; return 0; }
  91. _VA_ITER="$iter" \
  92. _VA_PRD="$prd" \
  93. _VA_TIER="$tier" \
  94. _VA_COMPLEXITY="${LOKI_COMPLEXITY:-standard}" \
  95. "$_va_py" -I -S -c 'import sys; sys.path[:] = [p for p in sys.path if p not in ("", ".")]
  96. import json, os
  97. iter_n = os.environ.get("_VA_ITER", "0")
  98. prd = os.environ.get("_VA_PRD", "")
  99. tier = os.environ.get("_VA_TIER", "development")
  100. complexity = os.environ.get("_VA_COMPLEXITY", "standard")
  101. # Effort selection mirrors Bun route loki-ts/src/providers/claude_flags.ts::effortForTier
  102. # so bash and Bun emit the same effort levels under all LOKI_COMPLEXITY values.
  103. # Architect roster (standard complexity): requirements-verifier=high, test-auditor=high,
  104. # convergence-voter=medium. Under complex, each shifts up one notch (medium->high, high->xhigh).
  105. _effort_bump = {"low": "medium", "medium": "high", "high": "xhigh"}
  106. def _effort(base):
  107. return _effort_bump.get(base, base) if complexity == "complex" else base
  108. # Cost S7: the three base voters run on haiku for small/standard work; the
  109. # complex tier keeps the original opus/sonnet roster. haiku takes no effort knob.
  110. _lean = complexity != "complex"
  111. def _model(default):
  112. return "haiku" if _lean else default
  113. prd_clause = (
  114. f"PRD at {prd}. Read it for ground-truth requirements."
  115. if prd else
  116. "No PRD path provided; rely on session context."
  117. )
  118. agents = {
  119. "requirements-verifier": {
  120. "description": "Verifies the iteration delivered the PRD-required behavior.",
  121. "model": _model("opus"),
  122. "tools": ["Read", "Grep", "Bash"],
  123. "prompt": (
  124. f"You are the requirements-verifier for iteration {iter_n}. "
  125. f"{prd_clause} Compare current code+tests against requirements. "
  126. "Emit a single finding object with role=requirements-verifier, "
  127. "vote in {APPROVE,REJECT,CANNOT_VALIDATE}, terse reason, "
  128. "confidence float in [0,1]."
  129. ),
  130. },
  131. "test-auditor": {
  132. "description": "Audits test coverage and pass status for the iteration.",
  133. "model": _model("sonnet"),
  134. "tools": ["Read", "Grep", "Bash"],
  135. "prompt": (
  136. f"You are the test-auditor for iteration {iter_n}. "
  137. "Inspect test logs, coverage, and pass/fail counts. "
  138. "Emit a single finding object with role=test-auditor, "
  139. "vote in {APPROVE,REJECT,CANNOT_VALIDATE}, terse reason, "
  140. "confidence float in [0,1]."
  141. ),
  142. },
  143. "convergence-voter": {
  144. "description": "Checks code churn and progress signals to judge convergence.",
  145. "model": _model("sonnet"),
  146. "tools": ["Read", "Grep", "Bash"],
  147. "prompt": (
  148. f"You are the convergence-voter for iteration {iter_n} on tier {tier}. "
  149. "Examine git diff trends, queue depth, and stagnation signals. "
  150. "Emit a single finding object with role=convergence-voter, "
  151. "vote in {APPROVE,REJECT,CANNOT_VALIDATE}, terse reason, "
  152. "confidence float in [0,1]."
  153. ),
  154. },
  155. }
  156. if not _lean:
  157. for _k, _b in (("requirements-verifier", "high"), ("test-auditor", "high"), ("convergence-voter", "medium")):
  158. agents[_k]["effort"] = _effort(_b)
  159. print(json.dumps(agents, separators=(",", ":")))
  160. '
  161. return 0
  162. }
  163. # ---------- Devil's advocate agent JSON ----------
  164. # Emits a single-key JSON object describing the conditional 4th voter.
  165. # Arg $1: terse base findings summary used to brief the DA prompt.
  166. loki_devils_advocate_json() {
  167. local summary="${1:-}"
  168. local _va_py
  169. _va_py="$(_loki_snapshot_py_tool)" || { printf '%s' '{}'; return 0; }
  170. _VA_SUMMARY="$summary" "$_va_py" -I -S -c 'import sys; sys.path[:] = [p for p in sys.path if p not in ("", ".")]
  171. import json, os
  172. summary = os.environ.get("_VA_SUMMARY", "")[:1000]
  173. agents = {
  174. "devils-advocate": {
  175. "description": "Skeptical re-review when the first three voters unanimously approve.",
  176. "model": "opus",
  177. "effort": "xhigh",
  178. "tools": ["Read", "Grep", "Bash"],
  179. "prompt": (
  180. "You are the devils-advocate. The first three voters unanimously "
  181. "voted APPROVE. Find the strongest reason this is wrong. "
  182. "Base voter summary:\n"
  183. f"{summary}\n"
  184. "Emit one finding with role=devils-advocate, "
  185. "vote in {APPROVE,REJECT,CANNOT_VALIDATE}, terse reason, "
  186. "confidence float in [0,1]."
  187. ),
  188. },
  189. }
  190. print(json.dumps(agents, separators=(",", ":")))
  191. '
  192. return 0
  193. }
  194. # ---------- Finding schema path ----------
  195. # Echo the absolute path to the schema file Dev-A maintains.
  196. # Returns 1 if the file is missing; the path string is still printed so callers
  197. # and tests can assert on the resolved location.
  198. loki_finding_schema_path() {
  199. local schema="${__LOKI_VA_REPO_ROOT}/loki-ts/data/finding-schema.json"
  200. printf '%s' "$schema"
  201. if [ -f "$schema" ]; then
  202. return 0
  203. fi
  204. return 1
  205. }
  206. # ---------- Council dispatch via --agents + --json-schema ----------
  207. # Orchestrator. Args:
  208. # $1 -- iteration number (required, int-ish)
  209. # $2 -- prd path (may be empty)
  210. #
  211. # Behavior:
  212. # 1. Source autonomy/lib/claude-flags.sh; require both --agents and
  213. # --json-schema in `claude --help`. Either missing -> return 1 (fallback).
  214. # 2. Build the agents JSON via loki_voter_agents_json.
  215. # 3. Invoke `claude --dangerously-skip-permissions -p <prompt>
  216. # --agents <json> --json-schema <inline-content>` and capture stdout.
  217. # Any non-zero exit or empty stdout -> return 1 (fallback).
  218. # 4. Parse the response via python3 (no jq dependency): extract
  219. # findings[].{role, vote, reason, confidence}. On parse failure -> 1.
  220. # 5. Write per-voter verdicts/<role>-iter-<iter>.json AND the aggregator
  221. # round file votes/round-<iter>.json in the existing shape consumed by
  222. # council_evaluate + council_write_transcript.
  223. # 6. Return 0 on success.
  224. loki_council_dispatch_agents() {
  225. local iteration="${1:-}"
  226. local prd_path="${2:-}"
  227. if [ -z "$iteration" ]; then
  228. return 1
  229. fi
  230. if [ -z "${COUNCIL_STATE_DIR:-}" ]; then
  231. return 1
  232. fi
  233. # 1a. Custom council size gate (added per v7.5.21 Phase C reviewer council
  234. # Opus #2 HIGH finding): the Phase C dispatch hardcodes 3 voters, so when a
  235. # user sets LOKI_COUNCIL_SIZE != 3 (documented public env in Docker README,
  236. # wiki Environment-Variables, certification lesson), the dispatch helper
  237. # would silently emit total_members=3 + threshold=2, breaking the unanimous
  238. # devil's-advocate check at completion-council.sh that compares
  239. # complete_count -eq COUNCIL_SIZE. Cheapest correct fix: fall back to the
  240. # heuristic path which respects COUNCIL_SIZE, until a multi-voter dispatch
  241. # ships in a later phase. Default COUNCIL_SIZE=3 -> Phase C dispatch active.
  242. if [ "${COUNCIL_SIZE:-3}" != "3" ]; then
  243. return 1
  244. fi
  245. # 1b. Managed-council bypass gate (Opus #2 MEDIUM finding): if the
  246. # experimental managed council is enabled, defer to it -- otherwise its
  247. # member output would be shadowed by the round file the dispatch writes.
  248. if [ "${LOKI_EXPERIMENTAL_MANAGED_COUNCIL:-false}" = "true" ]; then
  249. return 1
  250. fi
  251. # 1c. Flag support gate.
  252. # shellcheck disable=SC1091
  253. . "${__LOKI_VA_LIB_DIR}/claude-flags.sh" 2>/dev/null || return 1
  254. if ! loki_claude_flag_supported "--agents"; then
  255. return 1
  256. fi
  257. if ! loki_claude_flag_supported "--json-schema"; then
  258. return 1
  259. fi
  260. # 2. Build agents JSON. Empty result means we cannot proceed.
  261. local agents_json
  262. agents_json=$(LOKI_ITER="$iteration" LOKI_PRD_PATH="$prd_path" \
  263. LOKI_RARV_TIER="${LOKI_RARV_TIER:-development}" \
  264. loki_voter_agents_json)
  265. if [ -z "$agents_json" ] || [ "$agents_json" = "{}" ]; then
  266. return 1
  267. fi
  268. local schema_path
  269. schema_path=$(loki_finding_schema_path) || return 1
  270. # v8.x FIX: `claude --json-schema` takes the schema JSON as INLINE CONTENT,
  271. # not a file path. Live-verified on CLI 2.1.207: passing a path errors with
  272. # "--json-schema is not valid JSON: Unrecognized token '/'" -> rc!=0 ->
  273. # fail-closed to the heuristic path, so this structured dispatch was SILENTLY
  274. # DEAD (never actually schema-constrained the council). Read the file and pass
  275. # its content. Fail-closed if unreadable/empty (return 1 -> heuristic path).
  276. local schema_content
  277. schema_content=$(cat "$schema_path" 2>/dev/null) || return 1
  278. [ -n "$schema_content" ] || return 1
  279. local prompt
  280. prompt=$(printf 'Loki council iteration %s. Run each declared agent against the current workspace, return one finding per agent matching the provided JSON Schema. Be terse, be honest.' "$iteration")
  281. # Cost S7: hand the council the PRD path so voters stop reporting "No PRD was
  282. # provided". A missing/unreadable PRD adds nothing; voters then vote
  283. # CANNOT_VALIDATE, which never approves.
  284. if [ -n "$prd_path" ] && [ -r "$prd_path" ]; then
  285. prompt="${prompt} PRD file: ${prd_path} (read it for ground-truth requirements)."
  286. fi
  287. # Shared dispatch state (declared once, before either the SDK or claude arm).
  288. local response=""
  289. local rc=0
  290. local stderr_log="$COUNCIL_STATE_DIR/votes/dispatch-stderr-${iteration}.log"
  291. mkdir -p "$(dirname "$stderr_log")" 2>/dev/null || true
  292. local _va_sdk_done=0
  293. # v8 RAW-SDK COUNCIL PATH (opt-in LOKI_SDK_VOTER_AGENTS=1). The bash route
  294. # uses `claude --agents <json> --json-schema` (one dispatch fanning out to N
  295. # named agents). The raw @anthropic-ai/sdk has no --agents primitive, so we
  296. # inline the agent roster into the prompt and constrain the SAME
  297. # finding-schema.json via the sdk-judge bridge -- the model returns the same
  298. # {findings:[...]} object the downstream parser (line 302+) consumes, one
  299. # finding per declared agent. Runs BEFORE the claude-binary guard so the
  300. # no-binary deploy win holds. Fail-closed: on ANY miss (flag off, no key,
  301. # bun/entrypoint absent, non-zero, empty) fall through to the claude path
  302. # below, then to the heuristic council on a claude miss -- a hung/failed
  303. # council can never become a false COMPLETE (same guarantee as the claude arm).
  304. if [ "${LOKI_SDK_VOTER_AGENTS:-0}" = "1" ]; then
  305. local _va_root _va_loki _va_schema_f _va_pf _va_out _va_rc
  306. _va_root="$(cd "${__LOKI_VA_REPO_ROOT}" 2>/dev/null && pwd)" || _va_root="${__LOKI_VA_REPO_ROOT}"
  307. _va_loki="${_va_root}/bin/loki"
  308. _va_schema_f="${_va_root}/loki-ts/data/finding-schema.json"
  309. if [ -x "$_va_loki" ] && [ -f "$_va_schema_f" ] && command -v bun >/dev/null 2>&1; then
  310. _va_pf="$(mktemp 2>/dev/null)" || _va_pf=""
  311. if [ -n "$_va_pf" ]; then
  312. # inline the agent roster so a single schema-constrained call
  313. # produces one finding per declared council agent.
  314. printf '%s\n\nDeclared council agents (produce exactly one finding per agent, in this order):\n%s\n' \
  315. "$prompt" "$agents_json" > "$_va_pf"
  316. _va_rc=0
  317. local _va_to_s="${LOKI_COUNCIL_REVIEW_TIMEOUT:-600}"
  318. local _va_wrap
  319. if command -v timeout >/dev/null 2>&1; then _va_wrap="timeout $(( _va_to_s + 15 ))"
  320. elif command -v gtimeout >/dev/null 2>&1; then _va_wrap="gtimeout $(( _va_to_s + 15 ))"
  321. else _va_wrap=""; fi
  322. _va_out="$($_va_wrap "$_va_loki" internal sdk-judge \
  323. --prompt-file "$_va_pf" --schema-file "$_va_schema_f" \
  324. --model "${LOKI_SDK_COUNCIL_MODEL:-sonnet}" --effort high \
  325. --timeout-ms "$(( _va_to_s * 1000 ))" 2>"$COUNCIL_STATE_DIR/votes/dispatch-stderr-${iteration}.log")" || _va_rc=$?
  326. rm -f "$_va_pf" 2>/dev/null || true
  327. if [ "$_va_rc" -eq 0 ] && [ -n "$_va_out" ]; then
  328. # feed the SDK object into the SAME parse+materialize path
  329. response="$_va_out"
  330. rc=0
  331. _va_sdk_done=1 # skip the claude dispatch below
  332. fi
  333. fi
  334. fi
  335. # fall through to the claude path (fail-closed) if not _va_sdk_done
  336. fi
  337. # 3. Invoke claude (only when the SDK path did not already produce a response).
  338. # Guard against absent binary or non-zero exit.
  339. if [ "$_va_sdk_done" != "1" ]; then
  340. # Only the provider the operator chose may see a prompt (V10 P5): with any
  341. # other provider this falls back to the heuristic council. Policy (and the
  342. # LOKI_ALLOW_CLAUDE_SIDECALLS=1 opt-in) lives in providers/loader.sh.
  343. # ponytail: upgrade path is a provider-neutral structured dispatch.
  344. type loki_claude_sidecall_allowed >/dev/null 2>&1 \
  345. || . "${__LOKI_VA_REPO_ROOT}/providers/loader.sh" 2>/dev/null
  346. loki_claude_sidecall_allowed 2>/dev/null || return 1
  347. command -v claude >/dev/null 2>&1 || return 1
  348. # caveman HARD-SUPPRESS (parsed output, v7.41.0): the response is parsed for
  349. # findings[].vote against the JSON Schema. A globally-active caveman would
  350. # compress/reword it and break the schema match or flip a vote. The tree-wide
  351. # default-off export in claude-flags.sh (sourced above) already covers this;
  352. # the inline prefix is belt-and-suspenders, self-documenting, and a no-op when
  353. # caveman is absent.
  354. #
  355. # timeout-guard the dispatch (parity with the heuristic council path in
  356. # completion-council.sh:2074 and :2200, same LOKI_COUNCIL_REVIEW_TIMEOUT:-600
  357. # knob). The heuristic loop this dispatch replaced wrapped every claude
  358. # subcall in `timeout`; this helper did not, so a hung `claude --agents`
  359. # would stall the entire run indefinitely. `timeout` precedes the env-var
  360. # assignment, so `env` is used to set CAVEMAN_DEFAULT_MODE for the child.
  361. # On timeout, `timeout` exits 124; that exit is the command-substitution exit
  362. # (no pipe here), captured into rc via `|| rc=$?`. The existing rc check below
  363. # then routes any non-zero exit (timeout 124 or any other claude failure) to
  364. # `return 1` -- the heuristic fallback the caller falls through to
  365. # (completion-council.sh:2792). Fail-closed: a hung or timed-out council can
  366. # never become a false COMPLETE; it always degrades to the heuristic path
  367. # (which has its own timeout + conservative defaults).
  368. _LOKI_PERM_ARGV=(--dangerously-skip-permissions)
  369. ! type _loki_claude_permission_argv >/dev/null 2>&1 || _loki_claude_permission_argv
  370. # Cost S7 lean child: council role flags (no hooks/MCP/tools on the main
  371. # child) and --output-format json so usage lands in the child ledger. The
  372. # envelope is unwrapped back to the {findings:[...]} object below.
  373. local _va_lean=() _va_lf
  374. if type loki_lean_child_flags >/dev/null 2>&1; then
  375. while IFS= read -r _va_lf; do
  376. [ -n "$_va_lf" ] && _va_lean+=("$_va_lf")
  377. done < <(loki_lean_child_flags council 2>/dev/null)
  378. fi
  379. local _va_fmt=()
  380. if loki_claude_flag_supported "--output-format"; then
  381. _va_fmt=(--output-format json)
  382. fi
  383. response=$(timeout "${LOKI_COUNCIL_REVIEW_TIMEOUT:-600}" \
  384. env "SHELL=$(_loki_claude_shell_value 2>/dev/null || printf '%s' "${SHELL-}")" CAVEMAN_DEFAULT_MODE=off claude "${_LOKI_PERM_ARGV[@]}" \
  385. ${_va_lean[@]+"${_va_lean[@]}"} \
  386. ${_va_fmt[@]+"${_va_fmt[@]}"} \
  387. -p "$prompt" \
  388. --agents "$agents_json" \
  389. --json-schema "$schema_content" 2>"$stderr_log") || rc=$?
  390. if [ "$rc" -eq 0 ] && [ -n "$response" ] && [ "${#_va_fmt[@]}" -gt 0 ]; then
  391. if type loki_child_ledger_append >/dev/null 2>&1; then
  392. loki_child_ledger_append council "$response" >/dev/null 2>&1 || true
  393. fi
  394. local _va_unwrapped _va_upy
  395. if _va_upy="$(_loki_snapshot_py_tool)"; then
  396. # Unwrap the CLI envelope: structured_output, else a JSON string in
  397. # result. A bare {findings:[...]} object passes through unchanged. Any
  398. # other shape leaves the response as-is, so the parser fails closed.
  399. _va_unwrapped=$(printf '%s' "$response" | "$_va_upy" -I -S -c 'import sys; sys.path[:] = [p for p in sys.path if p not in ("", ".")]; import json
  400. try:
  401. e = json.load(sys.stdin)
  402. if isinstance(e, dict) and "findings" in e:
  403. p = e
  404. else:
  405. p = e.get("structured_output") if isinstance(e, dict) else None
  406. if not isinstance(p, dict):
  407. r = e.get("result") if isinstance(e, dict) else None
  408. p = json.loads(r) if isinstance(r, str) else None
  409. if isinstance(p, dict):
  410. sys.stdout.write(json.dumps(p))
  411. else:
  412. sys.exit(1)
  413. except Exception:
  414. sys.exit(1)' 2>/dev/null) || _va_unwrapped=""
  415. if [ -n "$_va_unwrapped" ]; then response="$_va_unwrapped"; fi
  416. fi
  417. fi
  418. fi # end claude-dispatch guard (SDK path skips it, already has $response)
  419. # Shared fail-closed check for BOTH paths: any non-zero exit (claude/SDK
  420. # timeout 124 or failure) or empty response -> return 1 -> heuristic fallback.
  421. if [ "$rc" -ne 0 ] || [ -z "$response" ]; then
  422. return 1
  423. fi
  424. # 4 + 5. Parse + materialize state files.
  425. local verdicts_dir="$COUNCIL_STATE_DIR/verdicts"
  426. local votes_dir="$COUNCIL_STATE_DIR/votes"
  427. mkdir -p "$verdicts_dir" "$votes_dir" 2>/dev/null || return 1
  428. local _va_py
  429. _va_py="$(_loki_snapshot_py_tool)" || return 1
  430. _VA_RESP="$response" \
  431. _VA_ITER="$iteration" \
  432. _VA_VDIR="$verdicts_dir" \
  433. _VA_RFILE="$votes_dir/round-${iteration}.json" \
  434. _VA_EXPECTED="${COUNCIL_SIZE:-3}" \
  435. "$_va_py" -I -S -c 'import sys; sys.path[:] = [p for p in sys.path if p not in ("", ".")]
  436. import json, os, sys
  437. from datetime import datetime, timezone
  438. try:
  439. resp = json.loads(os.environ["_VA_RESP"])
  440. except Exception:
  441. sys.exit(2)
  442. findings = resp.get("findings") if isinstance(resp, dict) else None
  443. if not isinstance(findings, list) or not findings:
  444. sys.exit(3)
  445. it = int(os.environ.get("_VA_ITER", "0") or 0)
  446. vdir = os.environ["_VA_VDIR"]
  447. rfile = os.environ["_VA_RFILE"]
  448. # WAVE13 CRITICAL quorum fix: the quorum denominator MUST be the EXPECTED
  449. # council size (COUNCIL_SIZE), never the number of findings the model happened
  450. # to return. Pre-fix this parser computed threshold = (returned*2+2)//3, so a
  451. # degraded response with a single APPROVE finding (returned=1) yielded
  452. # threshold=1 and a COMPLETE verdict from a SINGLE voter, with the missing
  453. # voters silently dropped. That fails OPEN on the completion-detection trust
  454. # core. We now fail CLOSED: any undercount (returned < expected) forces a
  455. # CONTINUE verdict so a partial/degraded model response can never reach
  456. # COMPLETE on the returned subset. Design choice (Option 2): compute the
  457. # verdict in-path (rather than sys.exit -> heuristic fallback) so the round
  458. # file always records the actual returned count in total_members, making the
  459. # downstream quorum assertion in completion-council.sh meaningful and locally
  460. # testable without depending on the heuristic-path disk-state behavior.
  461. try:
  462. expected_count = int(os.environ.get("_VA_EXPECTED", "3") or 3)
  463. except (TypeError, ValueError):
  464. expected_count = 3
  465. if expected_count < 1:
  466. expected_count = 1
  467. def to_legacy(vote: str) -> str:
  468. v = (vote or "").upper()
  469. if v == "APPROVE":
  470. return "COMPLETE"
  471. return "CONTINUE"
  472. votes = []
  473. complete = 0
  474. total = 0
  475. for idx, f in enumerate(findings, start=1):
  476. if not isinstance(f, dict):
  477. continue
  478. role = str(f.get("role") or f.get("name") or f"voter-{idx}")
  479. raw_vote = str(f.get("vote") or "CANNOT_VALIDATE").upper()
  480. if raw_vote not in ("APPROVE", "REJECT", "CANNOT_VALIDATE"):
  481. raw_vote = "CANNOT_VALIDATE"
  482. reason = (f.get("reason") or "").strip()
  483. try:
  484. confidence = float(f.get("confidence", 0.0))
  485. except Exception:
  486. confidence = 0.0
  487. legacy = to_legacy(raw_vote)
  488. total += 1
  489. if legacy == "COMPLETE":
  490. complete += 1
  491. # Per-voter file (architect-specified shape).
  492. per = {
  493. "role": role,
  494. "vote": raw_vote,
  495. "reason": reason,
  496. "confidence": confidence,
  497. "iteration": it,
  498. }
  499. try:
  500. with open(os.path.join(vdir, f"{role}-iter-{it}.json"), "w") as fp:
  501. json.dump(per, fp, indent=2)
  502. except OSError:
  503. sys.exit(4)
  504. votes.append({
  505. "member": idx,
  506. "role": role,
  507. "vote": legacy,
  508. "reason": reason,
  509. })
  510. if total == 0:
  511. sys.exit(5)
  512. # Quorum-aware threshold (WAVE13). threshold is computed against the EXPECTED
  513. # council size so it can never shrink to 1 on a degraded response. Absent
  514. # voters (total < expected) are treated as non-approval: the round is forced
  515. # to CONTINUE and can never reach COMPLETE on the returned subset. With
  516. # expected=3, threshold = ceil(2/3 * 3) = 2, so 1-of-3 (or any single voter)
  517. # is structurally incapable of producing COMPLETE.
  518. threshold = (expected_count * 2 + 2) // 3
  519. if total != expected_count:
  520. # Fail closed on ANY quorum mismatch:
  521. # - undercount (total < expected): missing voters count as non-approval.
  522. # - overcount (total > expected): extra/unprompted findings (e.g. a model
  523. # adding a 4th finding) would otherwise let a low-approval-ratio response
  524. # clear the fixed threshold. A degraded response in either direction must
  525. # never reach COMPLETE on the returned subset.
  526. verdict = "CONTINUE"
  527. else:
  528. verdict = "COMPLETE" if complete >= threshold else "CONTINUE"
  529. round_data = {
  530. "round": it,
  531. "timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
  532. "complete_votes": complete,
  533. "continue_votes": total - complete,
  534. # total_members records the ACTUAL number of voters that responded (not the
  535. # expected size) so the completion-council quorum assertion can detect an
  536. # undercount. expected_members records the size the verdict was judged
  537. # against.
  538. "total_members": total,
  539. "expected_members": expected_count,
  540. "threshold": threshold,
  541. "verdict": verdict,
  542. "votes": votes,
  543. "source": "voter-agents-dispatch",
  544. }
  545. try:
  546. with open(rfile, "w") as fp:
  547. json.dump(round_data, fp, indent=2)
  548. except OSError:
  549. sys.exit(6)
  550. ' || return 1
  551. return 0
  552. }
autonomy/run.sh · Bundled script · 11,065 lines · 2 findings marked
  1. #!/usr/bin/env bash
  2. # shellcheck disable=SC2034 # Many variables are used by sourced scripts
  3. # shellcheck disable=SC2155 # Declare and assign separately (acceptable in this codebase)
  4. # shellcheck disable=SC2329 # Functions may be invoked indirectly or via dynamic dispatch
  5. # shellcheck disable=SC2086 # Word splitting is intentional in some contexts
  6. #===============================================================================
  7. # Loki Mode - Autonomous Runner
  8. # Single script that handles prerequisites, setup, and autonomous execution
  9. #
  10. # Usage:
  11. # ./autonomy/run.sh [OPTIONS] [PRD_PATH]
  12. # ./autonomy/run.sh ./docs/requirements.md
  13. # ./autonomy/run.sh # Interactive mode
  14. # ./autonomy/run.sh --parallel # Parallel mode with git worktrees
  15. # ./autonomy/run.sh --parallel ./prd.md # Parallel mode with PRD
  16. #
  17. # Environment Variables:
  18. # LOKI_PROVIDER - AI provider: claude (default) or opencode
  19. # LOKI_MAX_RETRIES - Max retry attempts (default: 50)
  20. # LOKI_BASE_WAIT - Base wait time in seconds (default: 60)
  21. # LOKI_MAX_WAIT - Max wait time in seconds (default: 3600)
  22. # LOKI_SKIP_PREREQS - Skip prerequisite checks (default: false)
  23. # LOKI_DASHBOARD - Enable web dashboard (default: true)
  24. # LOKI_DASHBOARD_PORT - Dashboard port (default: 57374)
  25. # LOKI_TLS_CERT - Path to PEM certificate (enables HTTPS for dashboard)
  26. # LOKI_TLS_KEY - Path to PEM private key (enables HTTPS for dashboard)
  27. #
  28. # Resource Monitoring (prevents system overload):
  29. # LOKI_RESOURCE_CHECK_INTERVAL - Check resources every N seconds (default: 300 = 5min)
  30. # LOKI_RESOURCE_CPU_THRESHOLD - CPU % threshold to warn (default: 80)
  31. # LOKI_RESOURCE_MEM_THRESHOLD - Memory % threshold to warn (default: 80)
  32. #
  33. # Budget / Cost Limits (opt-in):
  34. # LOKI_BUDGET_LIMIT - Max USD spend before auto-pause (default: loki-ts/data/model-pricing.json default_max_cost_usd, 100 USD; empty = unlimited)
  35. # Example: "50.00" pauses session when estimated cost >= $50
  36. #
  37. # Security & Autonomy Controls (Enterprise):
  38. # LOKI_STAGED_AUTONOMY - Require approval before execution (default: false)
  39. # LOKI_AUDIT_LOG - Enable audit logging (default: true)
  40. # LOKI_AUDIT_DISABLED - Disable audit logging (default: false)
  41. # LOKI_MAX_PARALLEL_AGENTS - Limit concurrent agent spawning (default: 10)
  42. # LOKI_SANDBOX_MODE - Run in sandboxed container (default: false, requires Docker)
  43. # LOKI_ALLOWED_PATHS - Comma-separated path allowlist. PARTIAL enforcement
  44. # (honest scope): when set, run.sh rejects a sandbox
  45. # custom --mount whose host path is outside the
  46. # allowlist (a write surface run.sh itself controls).
  47. # It does NOT and CANNOT restrict provider-driven
  48. # agent file writes: claude/opencode write
  49. # files directly, not through run.sh, so run.sh never
  50. # sees those writes. The containment for agent writes
  51. # is the OS user + the LOKI_SANDBOX_MODE container
  52. # (cap-drop, seccomp, read-only mounts), not this var.
  53. # Treat ALLOWED_PATHS as defense-in-depth on the
  54. # mount surface, not a complete write sandbox.
  55. # LOKI_BLOCKED_COMMANDS - Comma-separated blocked shell commands (default: rm -rf /)
    • Note: Quoted as data in code: Runs `rm -rf` against a root or home-directory path
    • Line 55 on GitHub
  56. #
  57. # OIDC / SSO Authentication (optional, works alongside token auth):
  58. # LOKI_OIDC_ISSUER - OIDC issuer URL (e.g., https://accounts.google.com)
  59. # LOKI_OIDC_CLIENT_ID - OIDC client/application ID
  60. # LOKI_OIDC_AUDIENCE - Expected JWT audience (default: same as client_id)
  61. #
  62. # SDLC Phase Controls (all enabled by default, set to 'false' to skip):
  63. # LOKI_PHASE_UNIT_TESTS - Run unit tests (default: true)
  64. # LOKI_PHASE_API_TESTS - Functional API testing (default: true)
  65. # LOKI_PHASE_E2E_TESTS - E2E/UI testing with Playwright (default: true)
  66. # LOKI_PHASE_SECURITY - Security scanning OWASP/auth (default: true)
  67. # LOKI_PHASE_INTEGRATION - Integration tests SAML/OIDC/SSO (default: true)
  68. # LOKI_PHASE_CODE_REVIEW - 3-reviewer parallel code review (default: true)
  69. # LOKI_PHASE_WEB_RESEARCH - Competitor/feature gap research (default: true)
  70. # LOKI_PHASE_PERFORMANCE - Load/performance testing (default: true)
  71. # LOKI_PHASE_ACCESSIBILITY - WCAG compliance testing (default: true)
  72. # LOKI_PHASE_REGRESSION - Regression testing (default: true)
  73. # LOKI_PHASE_UAT - UAT simulation (default: true)
  74. #
  75. # Autonomous Loop Controls (Ralph Wiggum Mode):
  76. # LOKI_COMPLETION_PROMISE - EXPLICIT stop condition text (default: none - runs forever)
  77. # Example: "ALL TESTS PASSING 100%"
  78. # Only stops when the AI provider outputs this EXACT text
  79. # LOKI_MAX_ITERATIONS - Max loop iterations before exit (default: 1000)
  80. # LOKI_PERPETUAL_MODE - Ignore ALL completion signals (default: false)
  81. # Set to 'true' for truly infinite operation
  82. #
  83. # Completion Council (v5.25.0) - Multi-agent completion verification:
  84. # LOKI_COUNCIL_ENABLED - Enable completion council (default: true)
  85. # LOKI_COUNCIL_SIZE - Number of council members (default: 3)
  86. # LOKI_COUNCIL_THRESHOLD - Votes needed for completion (default: 2)
  87. # LOKI_COUNCIL_CHECK_INTERVAL - Check every N iterations (default: 5)
  88. # LOKI_COUNCIL_MIN_ITERATIONS - Min iterations before council runs (default: 3)
  89. # LOKI_COUNCIL_STAGNATION_LIMIT - Max iterations with no git changes (default: 5)
  90. #
  91. # Model Selection:
  92. # LOKI_ALLOW_HAIKU - Enable Haiku model for fast tier (default: false)
  93. # When false: Opus for dev/bugfix, Sonnet for tests/docs
  94. # When true: Sonnet for dev, Haiku for tests/docs (original)
  95. # Use --allow-haiku flag or set to 'true'
  96. #
  97. # 2026 Research Enhancements:
  98. # LOKI_PROMPT_REPETITION - Enable prompt repetition for Haiku agents (default: true)
  99. # arXiv 2512.14982v1: Improves accuracy 4-5x on structured tasks
  100. # LOKI_CONFIDENCE_ROUTING - Enable confidence-based routing (default: true)
  101. # HN Production: 4-tier routing (auto-approve, direct, supervisor, escalate)
  102. # LOKI_AUTONOMY_MODE - Autonomy level (default: perpetual)
  103. # Options: perpetual, checkpoint, supervised
  104. # Tim Dettmers: "Shorter bursts of autonomy with feedback loops"
  105. #
  106. # Parallel Workflows (Git Worktrees):
  107. # LOKI_PARALLEL_MODE - Enable git worktree-based parallelism (default: false)
  108. # Use --parallel flag or set to 'true'
  109. # LOKI_MAX_WORKTREES - Maximum parallel worktrees (default: 5)
  110. # LOKI_MAX_PARALLEL_SESSIONS - Maximum concurrent AI sessions (default: 3)
  111. # LOKI_PARALLEL_TESTING - Run testing stream in parallel (default: true)
  112. # LOKI_PARALLEL_DOCS - Run documentation stream in parallel (default: true)
  113. # LOKI_PARALLEL_BLOG - Run blog stream if site has blog (default: false)
  114. # LOKI_AUTO_MERGE - Auto-merge completed features (default: true)
  115. #
  116. # Complexity Tiers (Auto-Claude pattern):
  117. # LOKI_COMPLEXITY - Force complexity tier (default: auto)
  118. # Options: auto, simple, standard, complex
  119. # Simple (3 phases): 1-2 files, single service, UI fixes, text changes
  120. # Standard (6 phases): 3-10 files, 1-2 services, features, bug fixes
  121. # Complex (8 phases): 10+ files, multiple services, external integrations
  122. #
  123. # GitHub Integration (v4.1.0):
  124. # LOKI_GITHUB_IMPORT - Import open issues as tasks (default: false)
  125. # LOKI_GITHUB_PR - Create PR when feature complete (default: false)
  126. # LOKI_GITHUB_SYNC - Sync status back to issues (default: false)
  127. # LOKI_GITHUB_REPO - Override repo detection (default: from git remote)
  128. # LOKI_GITHUB_LABELS - Filter by labels (comma-separated)
  129. # LOKI_GITHUB_MILESTONE - Filter by milestone
  130. # LOKI_GITHUB_ASSIGNEE - Filter by assignee
  131. # LOKI_GITHUB_LIMIT - Max issues to import (default: 100)
  132. # LOKI_GITHUB_PR_LABEL - Label for PRs (default: none, avoids error if label missing)
  133. #
  134. # Desktop Notifications (v4.1.0):
  135. # LOKI_NOTIFICATIONS - Enable desktop notifications (default: true)
  136. # LOKI_NOTIFICATION_SOUND - Play sound with notifications (default: true)
  137. #
  138. # Uncertainty-Gated Escalation (v7.19.2, default-on):
  139. # LOKI_UNCERTAINTY_ESCALATION - Master on/off for proactive stuck-escalation (default: 1; set 0 to
  140. # disable; byte-identical when off). Decision lives in
  141. # completion-council.sh (uncertainty_should_escalate); action in run.sh.
  142. # NOTE: AUTONOMY_MODE defaults to "perpetual"; in perpetual mode PAUSE
  143. # is auto-cleared by check_human_intervention, so escalation degrades
  144. # to notify-only (notification fires, run does NOT halt).
  145. # LOKI_UNCERTAINTY_ROUNDS - Consecutive rounds where >=2 of 3 proxies must co-occur before
  146. # escalating (default: 2; recommended range 2-3). Debounces transient
  147. # noise: a single hot proxy never escalates alone.
  148. # LOKI_UNCERTAINTY_NOCHANGE_MIN - Proxy 1 threshold: consecutive_no_change value that marks p1 hot.
  149. # (default: COUNCIL_STAGNATION_LIMIT - 1, i.e. one below the circuit-
  150. # breaker limit so escalation fires before the breaker ends the run).
  151. # Floored at 1 at runtime.
  152. # LOKI_UNCERTAINTY_SPLIT_ROUNDS - Proxy 3 threshold: number of consecutive trailing council verdicts
  153. # that must be REJECTED-with-approver (split) to mark p3 hot
  154. # (default: 2). Between council votes p3 may be stale; it is always
  155. # fresh when proxy 1 is hot because proxy 1 hot forces a circuit-
  156. # breaker vote that refreshes verdicts.
  157. #
  158. # Human Intervention (Auto-Claude pattern):
  159. # PAUSE file: touch .loki/PAUSE - pauses after current session
  160. # HUMAN_INPUT.md: echo "instructions" > .loki/HUMAN_INPUT.md
  161. # STOP file: touch .loki/STOP - graceful; read at the TOP of each
  162. # iteration, so a STOP written mid-dispatch waits for that
  163. # provider call to return (bounded by
  164. # LOKI_PROVIDER_CALL_TIMEOUT, default 7200s). It said
  165. # "stops immediately", which was false. For an immediate
  166. # stop use `loki stop` (process-group SIGTERM, 1s grace,
  167. # then SIGKILL). See docs/stop-latency.md.
  168. # Ctrl+C (once): Pauses execution, shows options
  169. # Ctrl+C (twice): Exits immediately
  170. #
  171. # Security (Enterprise):
  172. # LOKI_PROMPT_INJECTION - Enable HUMAN_INPUT.md processing (default: false)
  173. # Set to "true" only in trusted environments
  174. #
  175. # Branch Protection (agent isolation):
  176. # LOKI_BRANCH_PROTECTION - Create feature branch for agent changes (default: true)
  177. # Agent works on loki/session-<timestamp>-<pid> branch
  178. # Set to "false" to opt out and work on the current branch
  179. # LOKI_AUTO_PR - Auto push + open a PR on session end (default: off)
  180. # Default behavior PRINTS the PR command (advisory, no push)
  181. #
  182. # Process Supervision (opt-in):
  183. # LOKI_WATCHDOG - Enable process health monitoring (default: false)
  184. # LOKI_WATCHDOG_INTERVAL - Check interval in seconds (default: 30)
  185. #===============================================================================
  186. #
  187. # Compatibility: bash 3.2+ (macOS default), bash 4+ (Linux), WSL
  188. # Parallel mode (--parallel) requires bash 4.0+ for associative arrays
  189. #===============================================================================
  190. set -uo pipefail
  191. # Compatibility check: Ensure we're running in bash (not sh, dash, zsh)
  192. if [ -z "${BASH_VERSION:-}" ]; then
  193. echo "[ERROR] This script requires bash. Please run with: bash $0" >&2
  194. exit 1
  195. fi
  196. # Extract major version for feature checks
  197. BASH_VERSION_MAJOR="${BASH_VERSION%%.*}"
  198. BASH_VERSION_MINOR="${BASH_VERSION#*.}"
  199. BASH_VERSION_MINOR="${BASH_VERSION_MINOR%%.*}"
  200. # Warn if bash version is very old (< 3.2)
  201. if [ "$BASH_VERSION_MAJOR" -lt 3 ] || { [ "$BASH_VERSION_MAJOR" -eq 3 ] && [ "$BASH_VERSION_MINOR" -lt 2 ]; }; then
  202. echo "[WARN] Bash version $BASH_VERSION is old. Recommend bash 3.2+ for full compatibility." >&2
  203. echo "[WARN] Some features may not work correctly." >&2
  204. fi
  205. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  206. PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  207. #===============================================================================
  208. # Self-Copy Protection
  209. # Bash reads scripts incrementally, so editing a running script corrupts execution.
  210. # Solution: Copy ourselves to /tmp and run from there. The original can be safely edited.
  211. #===============================================================================
  212. if [[ -z "${LOKI_RUNNING_FROM_TEMP:-}" ]] && [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  213. TEMP_SCRIPT=$(mktemp /tmp/loki-run-XXXXXX)
  214. mv "$TEMP_SCRIPT" "${TEMP_SCRIPT}.sh"
  215. TEMP_SCRIPT="${TEMP_SCRIPT}.sh"
  216. cp "${BASH_SOURCE[0]}" "$TEMP_SCRIPT"
  217. chmod 700 "$TEMP_SCRIPT"
  218. # BUG-XC-011: Set trap BEFORE exec so the temp file gets cleaned up
  219. trap 'rm -f "$TEMP_SCRIPT"' EXIT
  220. export LOKI_RUNNING_FROM_TEMP=1
  221. # Record the EXACT temp-copy path so the post-exec cleanup trap deletes THIS
  222. # temp file and NEVER the canonical source (root cause of the recurring
  223. # "run.sh self-deleted on build spawn" bug: an inherited LOKI_RUNNING_FROM_TEMP
  224. # skips the self-copy block, leaving BASH_SOURCE[0]=the real run.sh).
  225. export LOKI_TEMP_SCRIPT_PATH="$TEMP_SCRIPT"
  226. export LOKI_ORIGINAL_SCRIPT_DIR="$SCRIPT_DIR"
  227. export LOKI_ORIGINAL_PROJECT_DIR="$PROJECT_DIR"
  228. exec "$TEMP_SCRIPT" "$@"
  229. fi
  230. # Restore original paths when running from temp
  231. SCRIPT_DIR="${LOKI_ORIGINAL_SCRIPT_DIR:-$SCRIPT_DIR}"
  232. PROJECT_DIR="${LOKI_ORIGINAL_PROJECT_DIR:-$PROJECT_DIR}"
  233. # A-134: `loki quick` is quiet unless LOKI_VERBOSE=1. The run re-executes itself with
  234. # stdout in .loki/quick-run.log; this shell prints the inner run's compact receipt and
  235. # a warning count. Errors go to stderr.
  236. if [ -z "${LOKI_QUICK_INNER:-}" ] && [ "${LOKI_VERBOSE:-0}" != "1" ] && [ "${BASH_SOURCE[0]}" = "${0}" ]; then
  237. case "${1:-}" in */quick-prd-*.md | quick-prd-*.md)
  238. _qd="$(cd "$(dirname "$1")" && pwd -P)"; rm -f "$_qd/quick-receipt.txt"
  239. # Async with default INT/QUIT (a bare `&` child ignores them), signals forwarded.
  240. # A Node/Bun parent starts us with SIGPIPE ignored, and bash cannot reset a
  241. # signal ignored on entry; every `echo big | head -1` then prints "echo: write
  242. # error: Broken pipe" onto the user's terminal. Re-exec the inner run through
  243. # python3 with SIGPIPE back at default (SIG_DFL survives exec).
  244. _qexec=("$BASH" "$0" "$@")
  245. # Env differences under python (not bash): LC_CTYPE may be set when the locale is
  246. # C, macOS adds __CF_USER_TEXT_ENCODING, and SIGXFSZ stays ignored.
  247. # The import probe guards a python3 that exists but is broken (CLT stub, pyenv shim).
  248. if command -v python3 >/dev/null 2>&1 && python3 -c 'import os,signal' >/dev/null 2>&1; then
  249. _qexec=(python3 -c 'import os,signal,sys;signal.signal(signal.SIGPIPE,signal.SIG_DFL);os.execv(sys.argv[1],sys.argv[1:])' "${_qexec[@]}")
  250. fi
  251. # A background job gets stdin=/dev/null. Only a TTY needs it back (pause-mode
  252. # keypress resume); a pipe would block the provider, which reads inherited stdin.
  253. if [ -t 0 ]; then exec 3<&0; else exec 3</dev/null; fi
  254. ( trap - INT QUIT; LOKI_QUICK_DIR="$_qd" LOKI_QUICK_OUTER_PID=$$ LOKI_QUICK_INNER=1 exec "${_qexec[@]}" >"$_qd/quick-run.log" <&3 3<&- ) &
  255. _qpid=$!
  256. trap 'kill -INT "$_qpid" 2>/dev/null' INT
  257. trap 'kill -TERM "$_qpid" 2>/dev/null' TERM HUP
  258. wait "$_qpid"; _qrc=$?
  259. kill -0 "$_qpid" 2>/dev/null && { wait "$_qpid"; _qrc=$?; }
  260. trap - INT TERM HUP
  261. _qw=$(grep -cF '[WARN]' "$_qd/quick-run.log" 2>/dev/null); _qw="${_qw:-0}"
  262. if [ "$_qrc" -ne 0 ] || [ ! -s "$_qd/quick-receipt.txt" ]; then
  263. # Anchored to log_error/log_warn lines (optional ANSI prefix) and capped at 200
  264. # chars so a long prompt line mentioning "failed" is never dumped.
  265. _qr=$(grep -E $'^(\033\\[[0-9;]*m)?\\[(ERROR|WARN)\\]' "$_qd/quick-run.log" 2>/dev/null | tail -n 3 | cut -c1-200)
  266. echo "${_qr:-$(tail -n 3 "$_qd/quick-run.log" 2>/dev/null | cut -c1-200)}"
  267. fi
  268. [ "$_qw" -gt 0 ] && echo "Warnings: $_qw (see $_qd/quick-run.log)"
  269. cat "$_qd/quick-receipt.txt" 2>/dev/null
  270. exit "$_qrc" ;;
  271. esac
  272. fi
  273. # Clean up ONLY the recorded temp copy, and ONLY if it is a real temp file.
  274. # Deleting BASH_SOURCE[0] here was the bug: an inherited LOKI_RUNNING_FROM_TEMP
  275. # made BASH_SOURCE[0] the canonical source, so run.sh deleted itself on spawned
  276. # builds. Guard on the recorded path being a real /tmp/loki-run-* file.
  277. if [[ "${LOKI_RUNNING_FROM_TEMP:-}" == "1" ]] \
  278. && [[ -n "${LOKI_TEMP_SCRIPT_PATH:-}" ]] \
  279. && [[ "${LOKI_TEMP_SCRIPT_PATH}" == /tmp/loki-run-* || "${LOKI_TEMP_SCRIPT_PATH}" == "${TMPDIR:-/tmp}"loki-run-* ]]; then
  280. trap 'rm -f "${LOKI_TEMP_SCRIPT_PATH}" 2>/dev/null' EXIT
  281. fi
  282. #===============================================================================
  283. # Configuration File Support (v4.1.0)
  284. # Loads settings from config file, environment variables take precedence
  285. #===============================================================================
  286. # #691: source the canonical config mapping + shared per-key export helper. This
  287. # is the SAME lib the loki CLI pre-pass uses, so the YAML/config-file mapping
  288. # cannot drift between the CLI and the runner. Side-effect-free on source. The
  289. # parsers below delegate to loki_config_export_key with override=0, preserving
  290. # the shipped env-wins auto-discovery contract byte-for-byte.
  291. _LOKI_CONFIG_MAP_LIB="$SCRIPT_DIR/lib/config-map.sh"
  292. if [ -f "$_LOKI_CONFIG_MAP_LIB" ]; then
  293. # shellcheck source=lib/config-map.sh
  294. source "$_LOKI_CONFIG_MAP_LIB"
  295. fi
  296. # v8.1: one-switch SDK activation. LOKI_SDK_MODE=off|judges|full (default off)
  297. # sets the default for the 8 per-site LOKI_SDK_* flags via a write-once resolver;
  298. # per-site flags still win. Sourced + resolved here, before any judge lib or the
  299. # main loop reads a flag. No-op (byte-identical) when the mode is unset.
  300. _LOKI_SDK_MODE_LIB="$SCRIPT_DIR/lib/sdk-mode.sh"
  301. if [ -f "$_LOKI_SDK_MODE_LIB" ]; then
  302. # shellcheck source=lib/sdk-mode.sh
  303. source "$_LOKI_SDK_MODE_LIB"
  304. loki_sdk_resolve_mode
  305. fi
  306. load_config_file() {
  307. local config_file=""
  308. # Search for config file in order of priority
  309. # Security: Reject symlinks to prevent path traversal attacks
  310. # 1. Project-local config
  311. if [ -f ".loki/config.yaml" ] && [ ! -L ".loki/config.yaml" ]; then
  312. config_file=".loki/config.yaml"
  313. elif [ -f ".loki/config.yml" ] && [ ! -L ".loki/config.yml" ]; then
  314. config_file=".loki/config.yml"
  315. # 2. User-global config (symlinks allowed in home dir - user controls it)
  316. elif [ -f "${HOME}/.config/loki-mode/config.yaml" ]; then
  317. config_file="${HOME}/.config/loki-mode/config.yaml"
  318. elif [ -f "${HOME}/.config/loki-mode/config.yml" ]; then
  319. config_file="${HOME}/.config/loki-mode/config.yml"
  320. fi
  321. # If no config file found, return silently
  322. if [ -z "$config_file" ]; then
  323. return 0
  324. fi
  325. # Check for yq (YAML parser)
  326. if ! command -v yq &> /dev/null; then
  327. # Fallback: parse simple YAML with sed/grep
  328. parse_simple_yaml "$config_file"
  329. return 0
  330. fi
  331. # Use yq for proper YAML parsing
  332. parse_yaml_with_yq "$config_file"
  333. }
  334. # Fallback YAML parser for simple key: value format.
  335. # #691: iterates the canonical LOKI_CONFIG_MAP (single source of truth) instead
  336. # of 64 hand-maintained set_from_yaml calls. override=0 -> ambient env still
  337. # wins (auto-discovery contract unchanged). Falls back to nothing if the lib is
  338. # absent (defensive; the lib ships alongside run.sh).
  339. parse_simple_yaml() {
  340. local file="$1"
  341. if ! declare -p LOKI_CONFIG_MAP >/dev/null 2>&1; then
  342. return 0
  343. fi
  344. local mapping yaml_path env_var
  345. for mapping in "${LOKI_CONFIG_MAP[@]}"; do
  346. yaml_path="${mapping%%:*}"
  347. env_var="${mapping##*:}"
  348. set_from_yaml "$file" "$yaml_path" "$env_var"
  349. done
  350. }
  351. # Validate YAML value to prevent injection attacks
  352. validate_yaml_value() {
  353. local value="$1"
  354. local max_length="${2:-1000}"
  355. # Reject empty values
  356. if [ -z "$value" ]; then
  357. return 1
  358. fi
  359. # Reject values with dangerous shell metacharacters.
  360. # SECURITY FIX (#691 wave): the previous [[ "$value" =~ [\$\`...] ]]
  361. # bracket-regex matched NOTHING -- backslash escapes inside a bash regex
  362. # bracket class are taken literally, so the guard ACCEPTED $(...), backticks,
  363. # pipes, etc. (verified behaviorally: it returned 0 for "$(touch /tmp/x)").
  364. # A `case` glob class has no such ambiguity and actually rejects the metachars.
  365. # Allow alphanumeric, spaces, dots, dashes, underscores, slashes, colons,
  366. # commas, @.
  367. case "$value" in
  368. *'$'* | *'`'* | *'|'* | *';'* | *'&'* | *'>'* | *'<'* | *'('* | *')'* \
  369. | *'{'* | *'}'* | *'['* | *']'* | *'\'* )
  370. return 1 ;;
  371. esac
  372. # Reject values that are too long (DoS protection)
  373. if [ "${#value}" -gt "$max_length" ]; then
  374. return 1
  375. fi
  376. # Reject values with newlines (could corrupt variables)
  377. if [[ "$value" == *$'\n'* ]]; then
  378. return 1
  379. fi
  380. return 0
  381. }
  382. # Escape regex metacharacters for safe grep usage
  383. escape_regex() {
  384. local input="$1"
  385. # Escape: . * ? + [ ] ^ $ { } | ( ) \
  386. printf '%s' "$input" | sed 's/[.[\*?+^${}|()\\]/\\&/g'
  387. }
  388. # Helper: Extract value from YAML and set env var if not already set
  389. set_from_yaml() {
  390. local file="$1"
  391. local yaml_path="$2"
  392. local env_var="$3"
  393. # Skip if env var is already set (env-wins guard; the shared helper also
  394. # enforces this with override=0, but checking here avoids the extraction).
  395. if [ -n "${!env_var:-}" ]; then
  396. return 0
  397. fi
  398. # Extract value using grep and sed (handles simple YAML)
  399. # Convert yaml path like "core.max_retries" to search pattern
  400. local value=""
  401. local key="${yaml_path##*.}" # Get last part of path
  402. # Escape regex metacharacters in key for safe grep
  403. local escaped_key
  404. escaped_key=$(escape_regex "$key")
  405. # Simple grep for the key (works for flat or indented YAML)
  406. # Use read to avoid xargs command execution risks
  407. value=$(grep -E "^\s*${escaped_key}:" "$file" 2>/dev/null | head -1 | sed -E 's/.*:\s*//' | sed 's/#.*//' | sed 's/^["\x27]//;s/["\x27]$//' | tr -d '\n' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
  408. # #691: delegate the env-wins guard + ${VAR}-expand + validate + export to the
  409. # single shared helper (override=0 -> ambient env wins). Falls back to the
  410. # original inline export if the lib is unavailable, so behavior is identical
  411. # either way.
  412. if declare -f loki_config_export_key >/dev/null 2>&1; then
  413. loki_config_export_key "$env_var" "$value" 0 || true
  414. elif [ -n "$value" ] && [ "$value" != "null" ] && validate_yaml_value "$value"; then
  415. export "$env_var=$value"
  416. fi
  417. }
  418. # Parse YAML using yq (proper parser).
  419. # #691: iterates the canonical LOKI_CONFIG_MAP and delegates the env-wins guard +
  420. # expand + validate + export to the single shared helper (override=0 -> ambient
  421. # env wins; auto-discovery contract unchanged). The inline 61-entry table that
  422. # used to drift from parse_simple_yaml's 64 is gone.
  423. parse_yaml_with_yq() {
  424. local file="$1"
  425. if ! declare -p LOKI_CONFIG_MAP >/dev/null 2>&1; then
  426. return 0
  427. fi
  428. local mapping yaml_path env_var value
  429. for mapping in "${LOKI_CONFIG_MAP[@]}"; do
  430. yaml_path="${mapping%%:*}"
  431. env_var="${mapping##*:}"
  432. # Extract value using yq (env-wins guard is enforced by the shared helper).
  433. value=$(yq eval ".$yaml_path // \"\"" "$file" 2>/dev/null)
  434. if declare -f loki_config_export_key >/dev/null 2>&1; then
  435. loki_config_export_key "$env_var" "$value" 0 || true
  436. elif [ -n "$value" ] && [ "$value" != "null" ] && [ "$value" != "" ] \
  437. && [ -z "${!env_var:-}" ] && validate_yaml_value "$value"; then
  438. export "$env_var=$value"
  439. fi
  440. done
  441. }
  442. # Load config file before setting defaults
  443. load_config_file
  444. # Load JSON settings from loki config set (v6.0.0)
  445. #
  446. # SECURITY NOTE (v7.5.10, L12#2 audit): The eval below is intentional and safe.
  447. # The Python script's output is constrained to a fixed template:
  448. # [ -z "${VAR:-}" ] && export VAR=<value>
  449. # where:
  450. # - VAR is a hardcoded env var name from the `mapping` dict (NOT user-controlled).
  451. # - <value> is produced by shlex.quote(), which emits POSIX-shell-safe single-
  452. # quoted strings even for adversarial input (e.g. quotes, semicolons, $()).
  453. # - Non-string values from settings.json are skipped (isinstance check).
  454. # Therefore no user-controlled bytes can break out of the quoted value or alter
  455. # the surrounding shell syntax. Do NOT remove the shlex.quote() call or relax
  456. # the isinstance(val, str) guard without re-auditing this eval.
  457. _load_json_settings() {
  458. local settings_file="${TARGET_DIR:-.}/.loki/config/settings.json"
  459. [ -f "$settings_file" ] || return 0
  460. eval "$(_LOKI_SETTINGS_FILE="$settings_file" python3 -c "
  461. import json, sys, os, shlex
  462. def get_nested(d, key):
  463. \"\"\"Resolve dotted keys through nested dicts (model.planning -> data['model']['planning'])\"\"\"
  464. parts = key.split('.')
  465. cur = d
  466. for p in parts:
  467. if isinstance(cur, dict):
  468. cur = cur.get(p)
  469. else:
  470. return None
  471. return cur
  472. try:
  473. with open(os.environ['_LOKI_SETTINGS_FILE']) as f:
  474. data = json.load(f)
  475. except Exception:
  476. sys.exit(0)
  477. mapping = {
  478. 'maxTier': 'LOKI_MAX_TIER',
  479. 'model.planning': 'LOKI_MODEL_PLANNING',
  480. 'model.development': 'LOKI_MODEL_DEVELOPMENT',
  481. 'model.fast': 'LOKI_MODEL_FAST',
  482. 'notify.slack': 'LOKI_SLACK_WEBHOOK',
  483. 'notify.discord': 'LOKI_DISCORD_WEBHOOK',
  484. 'provider': 'LOKI_PROVIDER',
  485. 'issue.provider': 'LOKI_ISSUE_PROVIDER',
  486. 'blind_validation': 'LOKI_BLIND_VALIDATION',
  487. 'adversarial_testing': 'LOKI_ADVERSARIAL_TESTING',
  488. 'spawn_timeout': 'LOKI_SPAWN_TIMEOUT',
  489. 'spawn_retries': 'LOKI_SPAWN_RETRIES',
  490. 'budget': 'LOKI_BUDGET_LIMIT',
  491. }
  492. for key, env_var in mapping.items():
  493. # Try nested dict lookup first, then flat key, then underscore variant
  494. val = get_nested(data, key) or data.get(key) or data.get(key.replace('.', '_'))
  495. if val and isinstance(val, str):
  496. safe_val = shlex.quote(val)
  497. print(f'[ -z \"\${{{env_var}:-}}\" ] && export {env_var}={safe_val}' + (\" && export LOKI_BUDGET_SOURCE=config\" if env_var == 'LOKI_BUDGET_LIMIT' else ''))
  498. " 2>/dev/null)" 2>/dev/null || true
  499. }
  500. _LOKI_SETTINGS_FILE="${TARGET_DIR:-.}/.loki/config/settings.json" _load_json_settings
  501. # Configuration
  502. MAX_RETRIES=${LOKI_MAX_RETRIES:-50}
  503. BASE_WAIT=${LOKI_BASE_WAIT:-60}
  504. MAX_WAIT=${LOKI_MAX_WAIT:-3600}
  505. SKIP_PREREQS=${LOKI_SKIP_PREREQS:-false}
  506. ENABLE_DASHBOARD=${LOKI_DASHBOARD:-true}
  507. DASHBOARD_PORT=${LOKI_DASHBOARD_PORT:-57374}
  508. RESOURCE_CHECK_INTERVAL=${LOKI_RESOURCE_CHECK_INTERVAL:-300} # Check every 5 minutes
  509. RESOURCE_CPU_THRESHOLD=${LOKI_RESOURCE_CPU_THRESHOLD:-80} # CPU % threshold
  510. RESOURCE_MEM_THRESHOLD=${LOKI_RESOURCE_MEM_THRESHOLD:-80} # Memory % threshold
  511. # Budget / Cost Limit.
  512. #
  513. # DEFAULT-ON as of v9.42.0. This shipped as `""` (unlimited), and run.sh's own
  514. # F4 analysis names it as one of three runaway valves that all shipped
  515. # DISABLED: LOKI_BUDGET_LIMIT="" returns immediately, LOKI_MAX_DURATION=0 never
  516. # stops, leaving LOKI_MAX_ITERATIONS=1000 (a measured 8.3-DAY ceiling) as the
  517. # only backstop. A user who mistypes a spec, or hits a loop, had no cost
  518. # backstop at all.
  519. #
  520. # 100 USD, not a tight number. Measured envelope from 79 recorded benchmark
  521. # trials: median 0.48 USD/trial, max 3.06. A real build is far under this, so
  522. # the cap catches runaways, not ordinary work.
  523. #
  524. # SAFE BY CONSTRUCTION: breaching PAUSES (writes .loki/PAUSE and saves state);
  525. # it never kills work or discards a deliverable. The user removes .loki/PAUSE
  526. # to resume, or raises LOKI_BUDGET_LIMIT. Set LOKI_BUDGET_LIMIT="" to restore
  527. # the old unlimited behavior explicitly.
  528. # LM-002: the default value is read from default_max_cost_usd in loki-ts/data/model-pricing.json, the same
  529. # file the Loki 10 engine reads (docs/cost-controls.md). "100.00" is only the
  530. # fallback when that file is absent, and budget_cap.test.ts pins it to the file.
  531. _loki_budget_default_from() {
  532. local f="$1" v=""
  533. if [ ! -e "$f" ]; then
  534. printf '%s\n' "100.00"
  535. return 0
  536. fi
  537. v="$(sed -n 's/.*"default_max_cost_usd"[[:space:]]*:[[:space:]]*\([0-9][0-9.]*\).*/\1/p' "$f" 2>/dev/null | head -n 1)"
  538. case "$v" in
  539. '' | *[!0-9.]*)
  540. printf '%s\n' "loki: warning: cannot read default_max_cost_usd from $f (unreadable or no parsable key); using fallback 100.00" >&2
  541. printf '%s\n' "100.00"
  542. ;;
  543. *) awk -v v="$v" 'BEGIN{printf "%.2f\n", v}' ;;
  544. esac
  545. }
  546. _loki_budget_default="$(_loki_budget_default_from "${SCRIPT_DIR}/../loki-ts/data/model-pricing.json")"
  547. # Where the active cap came from: flag (loki start --budget), config (settings.json),
  548. # env (LOKI_BUDGET_LIMIT set by the caller), or default. Printed once at run start.
  549. if [ "${LOKI_BUDGET_LIMIT+set}" = "set" ]; then
  550. BUDGET_SOURCE="${LOKI_BUDGET_SOURCE:-env}"
  551. else
  552. BUDGET_SOURCE="default"
  553. fi
  554. BUDGET_LIMIT=${LOKI_BUDGET_LIMIT-"$_loki_budget_default"} # USD. Note `-` not `:-`: an
  555. # explicit empty value means the
  556. # operator chose unlimited, and is
  557. # honored.
  558. # Background Mode
  559. BACKGROUND_MODE=${LOKI_BACKGROUND:-false} # Run in background
  560. # Security & Autonomy Controls
  561. STAGED_AUTONOMY=${LOKI_STAGED_AUTONOMY:-false} # Require plan approval
  562. AUDIT_LOG_ENABLED=${LOKI_AUDIT_LOG:-true} # Enable audit logging (on by default)
  563. MAX_PARALLEL_AGENTS=${LOKI_MAX_PARALLEL_AGENTS:-10} # Limit concurrent agents
  564. SANDBOX_MODE=${LOKI_SANDBOX_MODE:-false} # Docker sandbox mode (informational; the real dispatch reads LOKI_SANDBOX_MODE at autonomy/loki:1965 and execs sandbox.sh -- this var is not consumed in run.sh)
  565. ALLOWED_PATHS=${LOKI_ALLOWED_PATHS:-""} # PARTIAL enforcement. When set, a sandbox custom --mount host path outside the allowlist is rejected in sandbox.sh (_sandbox_path_within_allowed), NOT in run.sh. Does NOT restrict provider-driven agent writes (run.sh never sees them); the sandbox container is the containment for those. Default empty = no restriction.
  566. BLOCKED_COMMANDS=${LOKI_BLOCKED_COMMANDS:-"rm -rf /,dd if=,mkfs,:(){ :|:& };:"}
  567. # LOKI_SESSION_ID is woven into many filesystem paths (.loki/sessions/<id>/...,
  568. # session locks, PID files, per-session state). It is operator/issue-derived
  569. # (trusted-ish), but a value like "../../tmp/x" would escape the .loki/ tree.
  570. # Sanitize ONCE here at intake: any char outside [A-Za-z0-9._-] is replaced with
  571. # "_" and a leading run of dots/dashes is neutralized, so every downstream
  572. # session path is contained. Unset stays unset (default behavior unchanged).
  573. if [ -n "${LOKI_SESSION_ID:-}" ]; then
  574. _loki_sid_raw="$LOKI_SESSION_ID"
  575. _loki_sid_safe="${_loki_sid_raw//[^A-Za-z0-9._-]/_}"
  576. # Strip leading dots/dashes so "../x" -> "x" cannot reference a parent dir
  577. # or look like an option; collapse a now-empty result to a safe literal.
  578. _loki_sid_safe="${_loki_sid_safe#"${_loki_sid_safe%%[!.-]*}"}"
  579. [ -n "$_loki_sid_safe" ] || _loki_sid_safe="session"
  580. if [ "$_loki_sid_safe" != "$_loki_sid_raw" ]; then
  581. printf 'loki: WARNING sanitized unsafe LOKI_SESSION_ID %s -> %s\n' "$_loki_sid_raw" "$_loki_sid_safe" >&2
  582. fi
  583. export LOKI_SESSION_ID="$_loki_sid_safe"
  584. unset _loki_sid_raw _loki_sid_safe
  585. fi
  586. # GENERIC TIER VOCABULARY (small|medium|high). A user should be able to ask for
  587. # a capability class without naming a vendor model, and get that provider's
  588. # latest model in the class. LOKI_SESSION_MODEL is the knob that already does
  589. # this -- it accepts the raw tier names planning|development|fast alongside the
  590. # Claude aliases -- so the generic words are normalized ONTO it here rather than
  591. # becoming a fourth spelling. LOKI_MAX_TIER (a cost CEILING) and LOKI_TIER (the
  592. # OSS/enterprise licensing seam) mean different things and are left alone.
  593. #
  594. # WHY NORMALIZE AT THE ENTRY POINT: the session-pin case block is byte-mirrored
  595. # in the estimator (autonomy/loki) and the dashboard (dashboard/server.py), and
  596. # every one of those mirrors is locked by a parity test. Translating here means
  597. # they keep seeing only the three canonical tier names and none of them change.
  598. #
  599. # The mapping is loki_tier_alias() in providers/models.sh -- the single source
  600. # of truth, not a second copy. Inlined as a case because run.sh must not source
  601. # a provider file this early in startup. Kept in lockstep by
  602. # tests/test-generic-tiers.sh.
  603. #
  604. # ONLY the three new words are translated. sonnet/haiku/opus/fable and the raw
  605. # tier names pass through untouched, so an unset LOKI_SESSION_MODEL still
  606. # defaults to sonnet and "medium" resolves to the same development-tier model
  607. # today's builds already use. This changes no existing run's model.
  608. # NORMALIZATION IS TRIM-ONLY + LOWERCASE, matching the estimator and dashboard
  609. # mirrors exactly. Interior whitespace is deliberately PRESERVED, so " med ium "
  610. # stays junk here just as it does there. Stripping interior spaces would make
  611. # this reader accept a value the other two reject, which is the precise kind of
  612. # divergence the session-pin parity tests exist to catch.
  613. _loki_generic_tier="${LOKI_SESSION_MODEL:-}"
  614. _loki_generic_tier="${_loki_generic_tier#"${_loki_generic_tier%%[![:space:]]*}"}"
  615. _loki_generic_tier="${_loki_generic_tier%"${_loki_generic_tier##*[![:space:]]}"}"
  616. case "$(printf '%s' "$_loki_generic_tier" | tr '[:upper:]' '[:lower:]')" in
  617. small) LOKI_SESSION_MODEL="fast" ; export LOKI_SESSION_MODEL ;;
  618. medium) LOKI_SESSION_MODEL="development" ; export LOKI_SESSION_MODEL ;;
  619. high) LOKI_SESSION_MODEL="planning" ; export LOKI_SESSION_MODEL ;;
  620. esac
  621. unset _loki_generic_tier
  622. # Process Supervision (opt-in)
  623. WATCHDOG_ENABLED=${LOKI_WATCHDOG:-"false"} # Enable process health monitoring
  624. WATCHDOG_INTERVAL=${LOKI_WATCHDOG_INTERVAL:-30} # Check interval in seconds
  625. LAST_WATCHDOG_CHECK=0
  626. STATUS_MONITOR_PID=""
  627. DASHBOARD_PID=""
  628. DASHBOARD_LAST_ALIVE=0
  629. _DASHBOARD_RESTARTING=false
  630. RESOURCE_MONITOR_PID=""
  631. _LOKI_NODE_PROBE_STATUS="not_checked"
  632. _LOKI_NODE_PROBE_VERSION=""
  633. _LOKI_NPM_PROBE_STATUS="not_checked"
  634. _LOKI_NPM_PROBE_VERSION=""
  635. # Build-profile fast-path (v7.129+). LOKI_BUILD_PROFILE selects a gate profile.
  636. # Default unset = current full behavior (every phase runs). "simple-web" is a
  637. # fast profile for static marketing/landing builds: it drops the phases that
  638. # are irrelevant to a static frontend (API tests, SAML/OIDC/SSO integration,
  639. # load/performance, regression, UAT, competitor web research) while KEEPING the
  640. # gates that catch real defects on such a page: E2E/Playwright (catches the
  641. # broken-reveal / blank-page class of bug), CODE_REVIEW, SECURITY, and
  642. # ACCESSIBILITY (a landing page must be a11y-clean). The completion council and
  643. # the evidence/proof gate are NOT phases and are untouched -- the moat stays.
  644. # Pure env-defaulting: it only fills a LOKI_PHASE_* that the operator has NOT
  645. # already set, so an explicit operator LOKI_PHASE_* override always wins.
  646. loki_is_supervised_simple_web() {
  647. [ "${LOKI_SUPERVISED_BUILD:-0}" = "1" ] \
  648. && [ "${LOKI_BUILD_PROFILE:-}" = "simple-web" ]
  649. }
  650. _loki_bind_supervised_spec_sha() {
  651. local spec_path="${1:-}"
  652. loki_is_supervised_simple_web || return 0
  653. [ -n "$spec_path" ] && [ -f "$spec_path" ] && [ -r "$spec_path" ] || {
  654. log_error "Supervised build specification is missing or unreadable."
  655. return 1
  656. }
  657. local computed_sha supplied_sha="${LOKI_SPEC_SHA256:-}"
  658. computed_sha=$(python3 - "$spec_path" <<'PY'
  659. import hashlib
  660. import sys
  661. with open(sys.argv[1], "rb") as handle:
  662. print(hashlib.sha256(handle.read()).hexdigest())
  663. PY
  664. ) || return 1
  665. [[ "$computed_sha" =~ ^[0-9a-f]{64}$ ]] || return 1
  666. if [ -n "$supplied_sha" ] && [ "$supplied_sha" != "$computed_sha" ]; then
  667. log_error "Supervised build specification changed before execution."
  668. return 1
  669. fi
  670. LOKI_SPEC_SHA256="$computed_sha"
  671. export LOKI_SPEC_SHA256
  672. }
  673. # Scoped-change profile: a bug fix or a single feature in an EXISTING repo.
  674. #
  675. # Measured on a real user's run: a 322-word GitHub issue against an existing
  676. # codebase spent 25+ minutes still inside iteration 1. The work itself was
  677. # genuine, but the build was also running competitor web research, load and
  678. # performance testing, regression simulation and UAT -- all of which default to
  679. # true, and none of which a scoped issue fix needs. That is the difference
  680. # between a 5-minute fix and a 30-minute one.
  681. #
  682. # What this NEVER touches: code review, security, tests, E2E, and the
  683. # completion council all stay on. Speed here comes from not running phases that
  684. # are irrelevant to the change, never from skipping verification. A greenfield
  685. # build or a whole-repo refactor does not match this profile and keeps the full
  686. # suite.
  687. #
  688. # Auto-detected rather than another flag the user has to know: an existing git
  689. # repo with real history, plus a spec that reads as a scoped change. Set
  690. # LOKI_SCOPED_CHANGE=0 to force the full suite, or =1 to force this profile.
  691. loki_detect_scoped_change() {
  692. # Explicit operator intent always wins, in both directions.
  693. case "${LOKI_SCOPED_CHANGE:-}" in
  694. 0|false) return 1 ;;
  695. 1|true) return 0 ;;
  696. esac
  697. # Greenfield is not a scoped change: no repo, or a repo with almost no
  698. # history, means we are building something new.
  699. #
  700. # Ask git whether this is a work tree rather than testing for a .git
  701. # DIRECTORY: in a git worktree (and in a submodule) .git is a FILE, so the
  702. # old -d test rejected every worktree-based run -- including the parallel
  703. # workflow streams this project runs by default. rev-parse is true for a
  704. # plain clone, a worktree, and a submodule alike, and replaces two
  705. # subprocesses' worth of checking with one.
  706. local target="${TARGET_DIR:-.}"
  707. git -C "$target" rev-parse --is-inside-work-tree >/dev/null 2>&1 || return 1
  708. local commits
  709. commits="$(git -C "$target" rev-list --count HEAD 2>/dev/null || echo 0)"
  710. [ "${commits:-0}" -ge 5 ] || return 1
  711. # An issue-sourced spec is the canonical scoped change: someone filed a
  712. # discrete request against code that already exists.
  713. #
  714. # The spec path is passed in by the caller ($1). It used to be read only
  715. # from LOKI_PRD_FILE / LOKI_ISSUE_REF, but `loki start <issue>` writes
  716. # .loki/prd-issue-N.md and hands run.sh that path as a POSITIONAL argument,
  717. # so neither variable was ever set and this check could not fire.
  718. local spec="${1:-${LOKI_PRD_FILE:-}}"
  719. [ -n "${LOKI_ISSUE_REF:-}" ] && return 0
  720. case "$spec" in
  721. *prd-issue-*) return 0 ;;
  722. esac
  723. return 1
  724. }
  725. loki_apply_scoped_change_profile() {
  726. loki_detect_scoped_change "${1:-}" || return 0
  727. # Off: cannot affect the correctness of a scoped change to existing code.
  728. : "${LOKI_PHASE_WEB_RESEARCH:=false}"
  729. : "${LOKI_PHASE_PERFORMANCE:=false}"
  730. : "${LOKI_PHASE_REGRESSION:=false}"
  731. : "${LOKI_PHASE_UAT:=false}"
  732. # On: every trust gate, unchanged. These are the moat.
  733. : "${LOKI_PHASE_CODE_REVIEW:=true}"
  734. : "${LOKI_PHASE_SECURITY:=true}"
  735. : "${LOKI_PHASE_UNIT_TESTS:=true}"
  736. : "${LOKI_PHASE_E2E_TESTS:=true}"
  737. export LOKI_PHASE_WEB_RESEARCH LOKI_PHASE_PERFORMANCE LOKI_PHASE_REGRESSION
  738. export LOKI_PHASE_UAT LOKI_PHASE_CODE_REVIEW LOKI_PHASE_SECURITY
  739. export LOKI_PHASE_UNIT_TESTS LOKI_PHASE_E2E_TESTS
  740. export LOKI_SCOPED_CHANGE_ACTIVE=1
  741. }
  742. # Small-task fast path (cost S5). Applied AFTER detect_complexity, because the
  743. # tier is not known when this file is sourced. Trigger: DETECTED_COMPLEXITY is
  744. # simple and, for a scoped change in an existing repo, the working-tree diff
  745. # fits LOKI_SMALL_DIFF_BUDGET changed lines (default 400). LOKI_SMALL_PROFILE=0
  746. # opts out. It never overrides another explicit profile.
  747. #
  748. # What this NEVER touches: the deterministic gates, holdout and Seal. It only
  749. # drops model-bearing orchestration that has no reader on a small task. Every
  750. # knob uses ":=" so explicit user env always beats the profile.
  751. loki_small_profile_eligible() {
  752. [ "${LOKI_SMALL_PROFILE:-1}" != "0" ] || return 1
  753. [ "${DETECTED_COMPLEXITY:-}" = "simple" ] || return 1
  754. case "${LOKI_BUILD_PROFILE:-}" in
  755. ""|small) ;;
  756. *) return 1 ;;
  757. esac
  758. if [ "${LOKI_SCOPED_CHANGE_ACTIVE:-0}" = "1" ]; then
  759. local changed budget="${LOKI_SMALL_DIFF_BUDGET:-400}"
  760. changed="$(git -C "${TARGET_DIR:-.}" diff --numstat HEAD 2>/dev/null \
  761. | awk '{ if ($1 ~ /^[0-9]+$/) n += $1; if ($2 ~ /^[0-9]+$/) n += $2 } END { print n + 0 }')"
  762. [ "${changed:-0}" -le "$budget" ] 2>/dev/null || return 1
  763. fi
  764. return 0
  765. }
  766. loki_apply_small_profile() {
  767. : "${LOKI_SPEC_GRILL:=0}"
  768. : "${LOKI_PRD_ENRICH:=0}"
  769. : "${LOKI_WIKI_AUTO:=0}"
  770. : "${LOKI_AUTO_DOCS:=false}"
  771. : "${LOKI_GATE_DEVILS_ADVOCATE:=false}"
  772. : "${LOKI_COUNCIL_MODEL_VOTERS:=0}"
  773. : "${LOKI_ADVISOR_MODEL:=haiku}"
  774. : "${LOKI_SIMPLE:=1}"
  775. : "${LOKI_DOCS_TIER:=small}"
  776. export LOKI_SPEC_GRILL LOKI_PRD_ENRICH LOKI_WIKI_AUTO LOKI_AUTO_DOCS
  777. export LOKI_GATE_DEVILS_ADVOCATE
  778. export LOKI_COUNCIL_MODEL_VOTERS LOKI_ADVISOR_MODEL LOKI_SIMPLE LOKI_DOCS_TIER
  779. LOKI_SMALL_PROFILE_ACTIVE=1
  780. export LOKI_SMALL_PROFILE_ACTIVE
  781. }
  782. # Main-session lean flags, small profile only. Fills _LOKI_SMALL_MAIN_ARGV from
  783. # loki_lean_child_flags (one argument per line) and widens the OAuth-host
  784. # "--setting-sources project" to "project,local" so project-local settings
  785. # still apply. Returns 1 (array empty) on any other profile, so the caller
  786. # keeps its existing "user,project,local" pin. LOKI_SMALL_LEAN_MAIN=0 opts out.
  787. # "--bare" is never passed to the main session: it skips hooks and the host
  788. # command guard. Lean flags apply to child sessions only.
  789. loki_small_main_session_flags() {
  790. _LOKI_SMALL_MAIN_ARGV=()
  791. [ "${LOKI_SMALL_PROFILE_ACTIVE:-0}" = "1" ] || return 1
  792. [ "${LOKI_SMALL_LEAN_MAIN:-1}" != "0" ] || return 1
  793. type loki_lean_child_flags >/dev/null 2>&1 || return 1
  794. local _line _prev=""
  795. while IFS= read -r _line; do
  796. [ -n "$_line" ] || continue
  797. [ "$_line" != "--bare" ] || continue
  798. if [ "$_prev" = "--setting-sources" ] && [ "$_line" = "project" ]; then
  799. _line="project,local"
  800. fi
  801. _LOKI_SMALL_MAIN_ARGV+=("$_line")
  802. _prev="$_line"
  803. done <<<"$(loki_lean_child_flags main)"
  804. [ "${#_LOKI_SMALL_MAIN_ARGV[@]}" -gt 0 ]
  805. }
  806. loki_maybe_apply_small_profile() {
  807. loki_small_profile_eligible || return 0
  808. LOKI_BUILD_PROFILE=small
  809. export LOKI_BUILD_PROFILE
  810. loki_apply_build_profile
  811. }
  812. loki_apply_build_profile() {
  813. if [ "${LOKI_BUILD_PROFILE:-}" = "small" ]; then
  814. loki_apply_small_profile
  815. return 0
  816. fi
  817. [ "${LOKI_BUILD_PROFILE:-}" = "simple-web" ] || return 0
  818. : "${LOKI_PHASE_API_TESTS:=false}"
  819. : "${LOKI_PHASE_INTEGRATION:=false}"
  820. : "${LOKI_PHASE_PERFORMANCE:=false}"
  821. : "${LOKI_PHASE_REGRESSION:=false}"
  822. : "${LOKI_PHASE_UAT:=false}"
  823. : "${LOKI_PHASE_WEB_RESEARCH:=false}"
  824. : "${LOKI_PHASE_E2E_TESTS:=true}"
  825. : "${LOKI_PHASE_CODE_REVIEW:=true}"
  826. : "${LOKI_PHASE_SECURITY:=true}"
  827. : "${LOKI_PHASE_ACCESSIBILITY:=true}"
  828. # The hosted simple-web path is a measured execution policy, not an
  829. # operator preset. It removes only orchestration that cannot affect the
  830. # generated app, pins every model-bearing subcall to the selected model,
  831. # and keeps all applicable verification and proof gates fail closed.
  832. loki_is_supervised_simple_web || return 0
  833. local selected_model sdk_model
  834. selected_model="$(printf '%s' "${LOKI_SESSION_MODEL:-sonnet}" | tr '[:upper:]' '[:lower:]')"
  835. case "$selected_model" in
  836. haiku|sonnet|opus) ;;
  837. *) selected_model="sonnet" ;;
  838. esac
  839. sdk_model="$selected_model"
  840. if command -v python3 >/dev/null 2>&1 && [ -r "$PROJECT_DIR/providers/model_catalog.json" ]; then
  841. sdk_model="$(_LOKI_SELECTED_MODEL="$selected_model" _LOKI_MODEL_CATALOG="$PROJECT_DIR/providers/model_catalog.json" python3 -c '
  842. import json, os
  843. with open(os.environ["_LOKI_MODEL_CATALOG"], encoding="utf-8") as f:
  844. catalog = json.load(f)
  845. print(catalog["providers"]["claude"]["cli_aliases"].get(os.environ["_LOKI_SELECTED_MODEL"], ""))
  846. ' 2>/dev/null)" || sdk_model=""
  847. [ -n "$sdk_model" ] || sdk_model="$selected_model"
  848. fi
  849. LOKI_PHASE_UNIT_TESTS=true
  850. LOKI_PHASE_E2E_TESTS=true
  851. LOKI_PHASE_CODE_REVIEW=true
  852. LOKI_PHASE_SECURITY=true
  853. LOKI_PHASE_ACCESSIBILITY=true
  854. LOKI_COUNCIL_ENABLED=true
  855. LOKI_EVIDENCE_GATE=1
  856. LOKI_PROOF_GATE=1
  857. LOKI_PROOF=1
  858. LOKI_DASHBOARD=false
  859. LOKI_PARALLEL_MODE=false
  860. LOKI_MAX_PARALLEL_AGENTS=1
  861. LOKI_MAX_ITERATIONS=2
  862. # MAX_RETRIES is the loop attempt ceiling, so 2 means an initial attempt
  863. # plus one targeted repair attempt.
  864. LOKI_MAX_RETRIES=2
  865. LOKI_BASE_WAIT=2
  866. LOKI_MAX_WAIT=10
  867. LOKI_PROVIDER_CALL_TIMEOUT=240
  868. LOKI_PROVIDER_IDLE_TIMEOUT=60
  869. LOKI_DRAFT_EFFORT=medium
  870. # One low-effort requirements review gets a hard 90 second ceiling. The
  871. # prior 75 second ceiling clipped valid Haiku responses, while a second
  872. # identical full review doubled latency without adding evidence.
  873. LOKI_REVIEW_CALL_TIMEOUT=90
  874. LOKI_SDK_REVIEW_TIMEOUT=90
  875. LOKI_COUNCIL_REVIEW_TIMEOUT=90
  876. LOKI_COUNCIL_TIMEOUT_MS=90000
  877. LOKI_REVIEW_EFFORT=low
  878. LOKI_GATE_TIMEOUT=60
  879. LOKI_DEPENDENCY_SETUP_TIMEOUT=60
  880. LOKI_COUNCIL_MIN_ITERATIONS=1
  881. LOKI_COUNCIL_CHECK_INTERVAL=1
  882. LOKI_SPEC_GRILL=0
  883. LOKI_DONE_RECOGNITION=0
  884. LOKI_INTELLIGENT_USAGE=0
  885. LOKI_PRD_ENRICH=0
  886. LOKI_AUTO_DOCS=false
  887. LOKI_GATE_DOC_COVERAGE=false
  888. LOKI_GATE_MAGIC_DEBATE=false
  889. LOKI_WIKI_AUTO=0
  890. LOKI_AUTO_FALLBACK=off
  891. LOKI_CAVEMAN=0
  892. LOKI_CAVEMAN_AUTO_BOOTSTRAP=0
  893. CAVEMAN_DEFAULT_MODE=off
  894. LOKI_FABLE_ARCHITECT=0
  895. LOKI_LEGACY_TIER_SWITCHING=false
  896. LOKI_REVIEW_RETRY=0
  897. LOKI_REVIEW_JSON_SCHEMA=off
  898. LOKI_REVIEW_REQUIREMENTS_ONLY=1
  899. LOKI_GATE_DEVILS_ADVOCATE=false
  900. LOKI_REVIEW_INCONCLUSIVE_BLOCK=1
  901. LOKI_EXPERIMENTAL_MANAGED_AGENTS=false
  902. LOKI_EXPERIMENTAL_MANAGED_REVIEW=false
  903. LOKI_EXPERIMENTAL_MANAGED_COUNCIL=false
  904. LOKI_COUNCIL_MODEL_VOTERS=0
  905. LOKI_OVERRIDE_COUNCIL=0
  906. LOKI_BUILD_CHECK_BLOCK=1
  907. LOKI_SESSION_MODEL="$selected_model"
  908. LOKI_MAX_TIER="$selected_model"
  909. LOKI_MODEL_OVERRIDE="$selected_model"
  910. [ "$selected_model" = "haiku" ] && LOKI_ALLOW_HAIKU=true
  911. LOKI_ADVISOR_MODEL="$selected_model"
  912. LOKI_GRILL_MODEL="$selected_model"
  913. LOKI_CLAUDE_MODEL_PLANNING="$selected_model"
  914. LOKI_CLAUDE_MODEL_DEVELOPMENT="$selected_model"
  915. LOKI_CLAUDE_MODEL_FAST="$selected_model"
  916. LOKI_SDK_COUNCIL_MODEL="$sdk_model"
  917. LOKI_SDK_GRILL_MODEL="$sdk_model"
  918. LOKI_SDK_JUDGE_MODEL="$sdk_model"
  919. LOKI_SDK_PRD_ENRICH_MODEL="$sdk_model"
  920. LOKI_SDK_REVIEW_MODEL="$sdk_model"
  921. MAX_RETRIES="$LOKI_MAX_RETRIES"
  922. BASE_WAIT="$LOKI_BASE_WAIT"
  923. MAX_WAIT="$LOKI_MAX_WAIT"
  924. ENABLE_DASHBOARD="$LOKI_DASHBOARD"
  925. MAX_PARALLEL_AGENTS="$LOKI_MAX_PARALLEL_AGENTS"
  926. export LOKI_PHASE_UNIT_TESTS LOKI_PHASE_E2E_TESTS
  927. export LOKI_PHASE_CODE_REVIEW LOKI_PHASE_SECURITY LOKI_PHASE_ACCESSIBILITY
  928. # EXPORT THE PHASES THIS PROFILE TURNS OFF, not only the ones it leaves on.
  929. # The receipt derives quality_gates.disabled_phases by scanning the
  930. # environment for LOKI_PHASE_* set to false (proof-generator.py:470-478), so
  931. # a phase that is disabled but never exported is INVISIBLE to it: the receipt
  932. # reported disabled_phases [] and all_phases_enabled true while six phases
  933. # were switched off. That defeats the field's stated purpose -- "a receipt
  934. # must be able to say what was NOT checked" -- and makes a narrowed run look
  935. # identical to a full one. The sibling loki_apply_scoped_change_profile
  936. # already exports the phases it changes; this one exported only its enables.
  937. export LOKI_PHASE_API_TESTS LOKI_PHASE_INTEGRATION LOKI_PHASE_PERFORMANCE
  938. export LOKI_PHASE_REGRESSION LOKI_PHASE_UAT LOKI_PHASE_WEB_RESEARCH
  939. export LOKI_COUNCIL_ENABLED LOKI_EVIDENCE_GATE LOKI_PROOF_GATE LOKI_PROOF
  940. export LOKI_DASHBOARD LOKI_PARALLEL_MODE LOKI_MAX_PARALLEL_AGENTS
  941. export LOKI_MAX_ITERATIONS LOKI_MAX_RETRIES LOKI_BASE_WAIT LOKI_MAX_WAIT
  942. export LOKI_PROVIDER_CALL_TIMEOUT LOKI_PROVIDER_IDLE_TIMEOUT LOKI_DRAFT_EFFORT
  943. export LOKI_REVIEW_CALL_TIMEOUT LOKI_SDK_REVIEW_TIMEOUT LOKI_REVIEW_EFFORT
  944. export LOKI_COUNCIL_REVIEW_TIMEOUT LOKI_COUNCIL_TIMEOUT_MS LOKI_GATE_TIMEOUT
  945. export LOKI_DEPENDENCY_SETUP_TIMEOUT
  946. export LOKI_COUNCIL_MIN_ITERATIONS LOKI_COUNCIL_CHECK_INTERVAL
  947. export LOKI_SPEC_GRILL LOKI_DONE_RECOGNITION LOKI_INTELLIGENT_USAGE LOKI_PRD_ENRICH
  948. export LOKI_AUTO_DOCS LOKI_GATE_DOC_COVERAGE LOKI_GATE_MAGIC_DEBATE
  949. export LOKI_WIKI_AUTO LOKI_AUTO_FALLBACK LOKI_CAVEMAN
  950. export LOKI_CAVEMAN_AUTO_BOOTSTRAP CAVEMAN_DEFAULT_MODE
  951. export LOKI_FABLE_ARCHITECT LOKI_LEGACY_TIER_SWITCHING LOKI_REVIEW_RETRY
  952. export LOKI_REVIEW_JSON_SCHEMA LOKI_REVIEW_REQUIREMENTS_ONLY LOKI_GATE_DEVILS_ADVOCATE
  953. export LOKI_REVIEW_INCONCLUSIVE_BLOCK LOKI_EXPERIMENTAL_MANAGED_AGENTS
  954. export LOKI_EXPERIMENTAL_MANAGED_REVIEW LOKI_EXPERIMENTAL_MANAGED_COUNCIL
  955. export LOKI_COUNCIL_MODEL_VOTERS LOKI_OVERRIDE_COUNCIL LOKI_BUILD_CHECK_BLOCK
  956. export LOKI_SESSION_MODEL LOKI_MAX_TIER LOKI_MODEL_OVERRIDE LOKI_ALLOW_HAIKU
  957. export LOKI_ADVISOR_MODEL LOKI_GRILL_MODEL
  958. export LOKI_CLAUDE_MODEL_PLANNING LOKI_CLAUDE_MODEL_DEVELOPMENT
  959. export LOKI_CLAUDE_MODEL_FAST LOKI_SDK_COUNCIL_MODEL LOKI_SDK_GRILL_MODEL
  960. export LOKI_SDK_JUDGE_MODEL LOKI_SDK_PRD_ENRICH_MODEL LOKI_SDK_REVIEW_MODEL
  961. }
  962. loki_apply_build_profile
  963. loki_apply_scoped_change_profile
  964. # Default hang guard for EVERY build, not just simple-web.
  965. #
  966. # The two timeouts above are set inside loki_apply_build_profile(), which
  967. # returns immediately unless LOKI_BUILD_PROFILE=simple-web. So on a normal
  968. # build both resolved to 0, and 0 means no guard at all -- verified by running
  969. # the deadline helper directly: `deadline.py 0 0 3 -- sleep 5` runs to
  970. # completion unkilled. A provider that hung had nothing to stop it.
  971. #
  972. # IDLE only, and no retry. That is what keeps this compatible with the standing
  973. # objection recorded above (search: "former invoke_with_timeout"), whose two
  974. # reasons remain correct:
  975. #
  976. # 1. "No safe generous default" applies to a fixed TOTAL timeout, which
  977. # cannot tell a long legitimate iteration from a hang. An idle timeout
  978. # can: it measures silence, not duration. Verified both directions --
  979. # `sleep 600` under a 120s idle cap dies, while a process emitting output
  980. # every second survives indefinitely. A coding agent streams constantly;
  981. # one silent for two minutes is not working.
  982. # 2. "Wrong retry semantics" stands, so nothing here retries. The call is
  983. # killed, and the existing failure path handles it. Re-running an agent
  984. # that may have already edited files remains off the table.
  985. #
  986. # 7200s hard ceiling is a backstop against a process that streams forever
  987. # without converging; the idle cap is the load-bearing guard. Both are
  988. # overridable, and setting either to 0 restores the old unguarded behaviour.
  989. : "${LOKI_PROVIDER_IDLE_TIMEOUT:=120}"
  990. : "${LOKI_PROVIDER_CALL_TIMEOUT:=7200}"
  991. export LOKI_PROVIDER_IDLE_TIMEOUT LOKI_PROVIDER_CALL_TIMEOUT
  992. loki_background_services_enabled() {
  993. ! loki_is_supervised_simple_web
  994. }
  995. _loki_with_deadline() {
  996. local seconds="${1:-0}"
  997. local kill_grace="${LOKI_DEADLINE_KILL_GRACE:-2}"
  998. local idle_seconds="${LOKI_DEADLINE_IDLE_TIMEOUT:-0}"
  999. shift
  1000. case "$seconds" in
  1001. ''|*[!0-9]*) seconds=0 ;;
  1002. esac
  1003. case "$kill_grace" in
  1004. ''|*[!0-9]*) kill_grace=2 ;;
  1005. esac
  1006. case "$idle_seconds" in
  1007. ''|*[!0-9]*) idle_seconds=0 ;;
  1008. esac
  1009. [ "$kill_grace" -gt 0 ] 2>/dev/null || kill_grace=2
  1010. [ "$idle_seconds" -ge 0 ] 2>/dev/null || idle_seconds=0
  1011. if [ "$seconds" -le 0 ] 2>/dev/null; then
  1012. "$@"
  1013. elif command -v python3 >/dev/null 2>&1 \
  1014. && [ -r "${SCRIPT_DIR}/lib/deadline.py" ]; then
  1015. python3 "${SCRIPT_DIR}/lib/deadline.py" \
  1016. "$seconds" "$kill_grace" "$idle_seconds" -- "$@"
  1017. else
  1018. log_error "Required command deadline cannot be enforced: deadline helper is unavailable"
  1019. return 125
  1020. fi
  1021. }
  1022. _loki_with_deadline_stdin_text() {
  1023. local seconds="${1:-0}"
  1024. local input_text="${2:-}"
  1025. local input_file rc=0
  1026. shift 2
  1027. input_file="$(mktemp "${TMPDIR:-/tmp}/loki-review-input.XXXXXX")" || return 125
  1028. chmod 600 "$input_file" 2>/dev/null || {
  1029. rm -f "$input_file" 2>/dev/null || true
  1030. return 125
  1031. }
  1032. if ! printf '%s' "$input_text" > "$input_file"; then
  1033. rm -f "$input_file" 2>/dev/null || true
  1034. return 125
  1035. fi
  1036. _loki_with_deadline "$seconds" "$@" < "$input_file" || rc=$?
  1037. rm -f "$input_file" 2>/dev/null || true
  1038. return "$rc"
  1039. }
  1040. _loki_snapshot_workspace_tree() {
  1041. local tree="${1:-${TARGET_DIR:-.}}"
  1042. local snapshot_dir snapshot_index index_tree snapshot_tree rc=0
  1043. snapshot_dir="$(mktemp -d "${TMPDIR:-/tmp}/loki-iter-index.XXXXXX")" || return 1
  1044. snapshot_index="$snapshot_dir/index"
  1045. index_tree=$(git -C "$tree" write-tree 2>/dev/null) || rc=$?
  1046. if [ "$rc" -eq 0 ]; then
  1047. GIT_INDEX_FILE="$snapshot_index" git -C "$tree" read-tree "$index_tree" \
  1048. >/dev/null 2>&1 || rc=$?
  1049. fi
  1050. if [ "$rc" -eq 0 ]; then
  1051. GIT_INDEX_FILE="$snapshot_index" git -C "$tree" add -A -- . ':(exclude).loki/' \
  1052. >/dev/null 2>&1 || rc=$?
  1053. fi
  1054. if [ "$rc" -eq 0 ]; then
  1055. snapshot_tree=$(GIT_INDEX_FILE="$snapshot_index" git -C "$tree" write-tree 2>/dev/null) || rc=$?
  1056. fi
  1057. rm -f "$snapshot_index" "$snapshot_index.lock" 2>/dev/null || true
  1058. rmdir "$snapshot_dir" 2>/dev/null || true
  1059. [ "$rc" -eq 0 ] && [ -n "${snapshot_tree:-}" ] || return 1
  1060. printf '%s\n' "$snapshot_tree"
  1061. }
  1062. _loki_provider_pipeline_exit_code() {
  1063. local provider_rc="${1:-125}"
  1064. local tee_rc="${2:-125}"
  1065. local parser_rc="${3:-0}"
  1066. if [ "$provider_rc" -ne 0 ]; then
  1067. printf '%s\n' "$provider_rc"
  1068. elif [ "$tee_rc" -ne 0 ]; then
  1069. printf '%s\n' "$tee_rc"
  1070. else
  1071. printf '%s\n' "$parser_rc"
  1072. fi
  1073. }
  1074. # Invoke through a provider's argv-builder seam while preserving the main
  1075. # loop's deadline, fan-out logging, and provider-stage exit status.
  1076. _loki_invoke_argv_provider() {
  1077. local tier="${1:-development}"
  1078. local prompt="${2:-}"
  1079. local log_file="${3:-}"
  1080. local agent_log="${4:-}"
  1081. local iter_output="${5:-}"
  1082. local -a _loki_argv_pipe_status=()
  1083. if ! type provider_invoke_argv >/dev/null 2>&1; then
  1084. log_error "Provider ${PROVIDER_NAME:-unknown} does not implement provider_invoke_argv"
  1085. return 125
  1086. fi
  1087. if ! provider_invoke_argv "$tier" "$prompt" \
  1088. || [ "${#_LOKI_INVOKE_ARGV[@]}" -eq 0 ]; then
  1089. log_error "Provider ${PROVIDER_NAME:-unknown} failed to build invocation argv"
  1090. return 125
  1091. fi
  1092. LOKI_DEADLINE_IDLE_TIMEOUT="${LOKI_PROVIDER_IDLE_TIMEOUT:-0}" \
  1093. _loki_with_deadline "${LOKI_PROVIDER_CALL_TIMEOUT:-0}" \
  1094. "${_LOKI_INVOKE_ARGV[@]}" < /dev/null 2>&1 \
  1095. | tee -a "$log_file" "$agent_log" "$iter_output"
  1096. _loki_argv_pipe_status=("${PIPESTATUS[@]}")
  1097. return "$(_loki_provider_pipeline_exit_code \
  1098. "${_loki_argv_pipe_status[0]:-125}" \
  1099. "${_loki_argv_pipe_status[1]:-125}" 0)"
  1100. }
  1101. _LOKI_DEPENDENCY_SETUP_LIB="${SCRIPT_DIR}/lib/dependency-setup.sh"
  1102. if [ -r "$_LOKI_DEPENDENCY_SETUP_LIB" ]; then
  1103. # shellcheck source=lib/dependency-setup.sh
  1104. source "$_LOKI_DEPENDENCY_SETUP_LIB"
  1105. fi
  1106. _loki_workspace_changed_since_iteration() {
  1107. local start_sha="$1"
  1108. local tree="${TARGET_DIR:-.}"
  1109. [ -n "$start_sha" ] || return 0
  1110. git -C "$tree" rev-parse --verify "$start_sha" >/dev/null 2>&1 || return 0
  1111. git -C "$tree" diff --quiet "$start_sha" -- >/dev/null 2>&1
  1112. case "$?" in
  1113. 1) return 0 ;;
  1114. 0) ;;
  1115. *) return 0 ;;
  1116. esac
  1117. local untracked path
  1118. untracked=$(git -C "$tree" ls-files --others --exclude-standard 2>/dev/null) || return 0
  1119. while IFS= read -r path; do
  1120. [ -n "$path" ] || continue
  1121. case "$path" in
  1122. .loki|.loki/*) continue ;;
  1123. *) return 0 ;;
  1124. esac
  1125. done << UNTRACKED_EOF
  1126. $untracked
  1127. UNTRACKED_EOF
  1128. return 1
  1129. }
  1130. _loki_supervised_claude_isolation_ready() {
  1131. loki_is_supervised_simple_web || return 1
  1132. type loki_claude_flag_supported >/dev/null 2>&1 || return 1
  1133. loki_claude_flag_supported "--setting-sources" || return 1
  1134. loki_claude_flag_supported "--disallowedTools" || return 1
  1135. loki_claude_flag_supported "--tools" || return 1
  1136. loki_claude_flag_supported "--mcp-config" || return 1
  1137. loki_claude_flag_supported "--strict-mcp-config" || return 1
  1138. loki_claude_flag_supported "--disable-slash-commands" || return 1
  1139. }
  1140. _loki_write_supervised_simple_web_policy() {
  1141. loki_is_supervised_simple_web || return 0
  1142. local loki_dir="${LOKI_DIR:-${TARGET_DIR:-.}/.loki}"
  1143. local policy_file="$loki_dir/state/execution-policy.json"
  1144. mkdir -p "$(dirname "$policy_file")" || return 1
  1145. _LOKI_POLICY_FILE="$policy_file" \
  1146. _LOKI_POLICY_MODEL="${LOKI_SESSION_MODEL:-}" \
  1147. _LOKI_POLICY_MODEL_ID="${LOKI_SDK_REVIEW_MODEL:-}" \
  1148. _LOKI_POLICY_LEARNINGS="$loki_dir" \
  1149. _LOKI_POLICY_GIT_CONFIG="${GIT_CONFIG_GLOBAL:-}" \
  1150. _LOKI_POLICY_NODE_STATUS="$_LOKI_NODE_PROBE_STATUS" \
  1151. _LOKI_POLICY_NODE_VERSION="$_LOKI_NODE_PROBE_VERSION" \
  1152. _LOKI_POLICY_NPM_STATUS="$_LOKI_NPM_PROBE_STATUS" \
  1153. _LOKI_POLICY_NPM_VERSION="$_LOKI_NPM_PROBE_VERSION" \
  1154. python3 -c '
  1155. import json, os
  1156. def number(name):
  1157. try:
  1158. return int(os.environ.get(name, "0"))
  1159. except ValueError:
  1160. return 0
  1161. target = os.environ["_LOKI_POLICY_FILE"]
  1162. policy = {
  1163. "schema_version": 1,
  1164. "policy": "supervised-simple-web",
  1165. "active": True,
  1166. "fail_closed": True,
  1167. "build_profile": "simple-web",
  1168. "verification_status": "required_not_yet_proven",
  1169. "model": {
  1170. "alias": os.environ.get("_LOKI_POLICY_MODEL", ""),
  1171. "sdk_id": os.environ.get("_LOKI_POLICY_MODEL_ID", ""),
  1172. "max_tier": os.environ.get("LOKI_MAX_TIER", ""),
  1173. "nested_task_tool": False,
  1174. "fallback": False,
  1175. },
  1176. "limits": {
  1177. "iterations": number("LOKI_MAX_ITERATIONS"),
  1178. "attempts": number("LOKI_MAX_RETRIES"),
  1179. "provider_hard_seconds": number("LOKI_PROVIDER_CALL_TIMEOUT"),
  1180. "provider_idle_seconds": number("LOKI_PROVIDER_IDLE_TIMEOUT"),
  1181. "review_seconds": number("LOKI_REVIEW_CALL_TIMEOUT"),
  1182. "council_seconds": number("LOKI_COUNCIL_REVIEW_TIMEOUT"),
  1183. "gate_seconds": number("LOKI_GATE_TIMEOUT"),
  1184. },
  1185. "skipped": [
  1186. "dashboard", "status_monitor", "resource_monitor", "enterprise_services",
  1187. "spec_grill", "done_recognition", "prd_model_enrichment", "auto_docs",
  1188. "auto_wiki", "caveman", "completion_model_voters",
  1189. ],
  1190. "required_gates": [
  1191. "production_build", "unit_tests", "browser_e2e", "accessibility",
  1192. "security", "code_review", "completion_checklist", "completion_heldout",
  1193. "completion_evidence", "completion_assumptions", "proof",
  1194. ],
  1195. "state": {
  1196. "learnings_root": os.environ.get("_LOKI_POLICY_LEARNINGS", ""),
  1197. "git_config_global": os.environ.get("_LOKI_POLICY_GIT_CONFIG", ""),
  1198. "git_baseline_required": True,
  1199. },
  1200. "runtime_probes": {
  1201. "node": {"status": os.environ.get("_LOKI_POLICY_NODE_STATUS", ""), "version": os.environ.get("_LOKI_POLICY_NODE_VERSION", "")},
  1202. "npm": {"status": os.environ.get("_LOKI_POLICY_NPM_STATUS", ""), "version": os.environ.get("_LOKI_POLICY_NPM_VERSION", "")},
  1203. },
  1204. }
  1205. tmp = target + ".tmp." + str(os.getpid())
  1206. with open(tmp, "w", encoding="utf-8") as f:
  1207. json.dump(policy, f, indent=2, sort_keys=True)
  1208. f.write("\n")
  1209. os.replace(tmp, target)
  1210. ' || return 1
  1211. log_info "Execution policy: $policy_file"
  1212. }
  1213. # SDLC Phase Controls (all enabled by default)
  1214. PHASE_UNIT_TESTS=${LOKI_PHASE_UNIT_TESTS:-true}
  1215. PHASE_API_TESTS=${LOKI_PHASE_API_TESTS:-true}
  1216. PHASE_E2E_TESTS=${LOKI_PHASE_E2E_TESTS:-true}
  1217. PHASE_SECURITY=${LOKI_PHASE_SECURITY:-true}
  1218. PHASE_INTEGRATION=${LOKI_PHASE_INTEGRATION:-true}
  1219. PHASE_CODE_REVIEW=${LOKI_PHASE_CODE_REVIEW:-true}
  1220. PHASE_WEB_RESEARCH=${LOKI_PHASE_WEB_RESEARCH:-true}
  1221. PHASE_PERFORMANCE=${LOKI_PHASE_PERFORMANCE:-true}
  1222. PHASE_ACCESSIBILITY=${LOKI_PHASE_ACCESSIBILITY:-true}
  1223. PHASE_REGRESSION=${LOKI_PHASE_REGRESSION:-true}
  1224. PHASE_UAT=${LOKI_PHASE_UAT:-true}
  1225. # Autonomous Loop Controls (Ralph Wiggum Mode)
  1226. # Default: No auto-completion - runs until max iterations or explicit promise
  1227. COMPLETION_PROMISE=${LOKI_COMPLETION_PROMISE:-""}
  1228. MAX_ITERATIONS=${LOKI_MAX_ITERATIONS:-1000}
  1229. ITERATION_COUNT=0
  1230. # If this is an auto-fix task, allow more iterations
  1231. if [[ "${LOKI_AUTO_FIX:-}" == "true" ]]; then
  1232. MAX_ITERATIONS="${LOKI_MAX_ITERATIONS:-5}"
  1233. fi
  1234. # Perpetual mode: never stop unless max iterations (ignores all completion signals)
  1235. PERPETUAL_MODE=${LOKI_PERPETUAL_MODE:-false}
  1236. # F4: bound the runaway ceiling for ORDINARY runs.
  1237. #
  1238. # MEASURED, not chosen. Real per-iteration wall clock across every recorded run
  1239. # on this machine: median 718s, max 1746s. So the 1000 default is an 8.3-DAY
  1240. # ceiling. When this analysis was written it was also the ONLY backstop,
  1241. # because the other two valves shipped disabled. That is no longer true of the
  1242. # budget: since v9.42.0 LOKI_BUDGET_LIMIT defaults to "100.00" (see :527 --
  1243. # note `-` not `:-`, so only an EXPLICIT "" means unlimited), and breaching it
  1244. # pauses the run. LOKI_MAX_DURATION does still default to 0 (check_max_duration
  1245. # returns "never stop" at :18278).
  1246. #
  1247. # It also contradicts our own documentation. SETUP.md tells users to RAISE the
  1248. # budget for large work with LOKI_MAX_ITERATIONS=40, and the demo uses 10 -- so
  1249. # the shipped default is 25x the documented "large" setting.
  1250. #
  1251. # What real runs actually use: 1, 1, 3, 4. Every one terminated `completed` via
  1252. # council approval or a completion promise; NONE hit a cap. Those are the
  1253. # evidence-driven terminals, and they are unaffected by this -- the cap is a
  1254. # backstop, not the mechanism.
  1255. #
  1256. # 25 is deliberately generous against that evidence (6x the observed maximum),
  1257. # because the research is explicit that a too-small cap fails runs whose
  1258. # approach was sound: 1-2 caps fail even when the agent was on track, and the
  1259. # recommended range is 5-10. This is not a first-pass target; F0 (v8.45.0)
  1260. # already stops a doomed run at its CAUSE, which is the better instrument.
  1261. #
  1262. # TWO GUARDS, both load-bearing:
  1263. # - an explicit LOKI_MAX_ITERATIONS always wins, so nobody's setting changes
  1264. # - PERPETUAL_MODE is untouched: it deliberately ignores every completion
  1265. # signal and relies on max-iterations as its ONLY stop, so lowering the cap
  1266. # there would silently truncate exactly the runs that opted out of stopping
  1267. if [ -z "${LOKI_MAX_ITERATIONS:-}" ] \
  1268. && [ "$PERPETUAL_MODE" != "true" ] \
  1269. && [ "${LOKI_AUTO_FIX:-}" != "true" ]; then
  1270. MAX_ITERATIONS="${LOKI_MAX_ITERATIONS_DEFAULT:-25}"
  1271. fi
  1272. # Enterprise background service PIDs (OTEL bridge, audit subscriber, integration sync)
  1273. ENTERPRISE_PIDS=()
  1274. # Portable lock helper (v7.5.12) -- mkdir-mutex replacement for flock(1).
  1275. # Provides safe_acquire_lock / safe_release_lock / safe_with_lock so bash
  1276. # callers no longer need a Linux-only flock binary. Macs do not ship
  1277. # flock; pre-7.5.12 the fallback was a non-atomic PID check that emitted
  1278. # "[WARN] flock not available - using non-atomic PID check ...".
  1279. LOCK_LIB="$SCRIPT_DIR/lib/lock.sh"
  1280. if [ -f "$LOCK_LIB" ]; then
  1281. # shellcheck source=lib/lock.sh
  1282. source "$LOCK_LIB"
  1283. fi
  1284. # Guarded browser opener (loki_open_url): the only way run.sh opens a browser.
  1285. if [ -f "$SCRIPT_DIR/lib/browser-open.sh" ]; then
  1286. # shellcheck source=lib/browser-open.sh
  1287. source "$SCRIPT_DIR/lib/browser-open.sh"
  1288. fi
  1289. # Git PR advisory (shared print-only helper for create_session_pr and loki deploy)
  1290. GIT_PR_ADVISORY_LIB="$SCRIPT_DIR/lib/git-pr-advisory.sh"
  1291. if [ -f "$GIT_PR_ADVISORY_LIB" ]; then
  1292. # shellcheck source=lib/git-pr-advisory.sh
  1293. source "$GIT_PR_ADVISORY_LIB"
  1294. fi
  1295. # Proven PR (Loop 6): shared print-only Evidence Receipt renderer for PR bodies.
  1296. # render_evidence_receipt_md prints the run's honest headline + facts +
  1297. # verify-yourself block into the PR body. Pure, never pushes/PRs/mutates.
  1298. PROOF_PR_LIB="$SCRIPT_DIR/lib/proof-pr.sh"
  1299. if [ -f "$PROOF_PR_LIB" ]; then
  1300. # shellcheck source=lib/proof-pr.sh
  1301. source "$PROOF_PR_LIB"
  1302. fi
  1303. # Proven PR (Loop 6 / Slice B): optional advisory verified-completion check-run.
  1304. # Owned by Slice B (autonomy/lib/proof-check.sh); sourced guarded so this slice
  1305. # is correct whether or not the file is present in the tree, and the single call
  1306. # site is itself guarded on declare -f + LOKI_PROVEN_PR_CHECK.
  1307. PROOF_CHECK_LIB="$SCRIPT_DIR/lib/proof-check.sh"
  1308. if [ -f "$PROOF_CHECK_LIB" ]; then
  1309. # shellcheck source=lib/proof-check.sh
  1310. source "$PROOF_CHECK_LIB"
  1311. fi
  1312. # Completion Council (v5.25.0) - Multi-agent completion verification
  1313. # Source completion council module
  1314. COUNCIL_SCRIPT="$SCRIPT_DIR/completion-council.sh"
  1315. if [ -f "$COUNCIL_SCRIPT" ]; then
  1316. # shellcheck source=completion-council.sh
  1317. source "$COUNCIL_SCRIPT"
  1318. fi
  1319. # PRD Checklist module (v5.44.0)
  1320. if [ -f "${SCRIPT_DIR}/prd-checklist.sh" ]; then
  1321. # shellcheck source=prd-checklist.sh
  1322. source "${SCRIPT_DIR}/prd-checklist.sh"
  1323. fi
  1324. # App Runner module (v5.45.0)
  1325. if [ -f "${SCRIPT_DIR}/app-runner.sh" ]; then
  1326. # shellcheck source=app-runner.sh
  1327. source "${SCRIPT_DIR}/app-runner.sh"
  1328. fi
  1329. # Build-time HOME isolation (F49).
  1330. #
  1331. # When Loki executes/tests the GENERATED app during a build (the app-runner
  1332. # server launch, restarts, and the project's own test suite in
  1333. # enforce_test_coverage), the child process inherits Loki's environment --
  1334. # including the user's REAL $HOME. A generated app that defaults its state file
  1335. # to a HOME-relative path (e.g. a todo CLI writing ~/.todo.json) would then
  1336. # litter the user's home directory with Loki's in-build test data. These two
  1337. # helpers give those in-build executions an isolated HOME/XDG/TMPDIR rooted
  1338. # under .loki/ so the app can run normally but cannot write into the real home.
  1339. #
  1340. # Scope: ONLY Loki's own in-build test executions are sandboxed. The user's
  1341. # later manual `npm start`/run of the app is unaffected -- they invoke it
  1342. # themselves in their own shell with their own HOME.
  1343. # Lazily create and echo a persistent isolated HOME directory under .loki.
  1344. # Persistent (not a per-call mktemp) so an app launched in one iteration and
  1345. # restarted in a later one keeps a stable home, and so its state survives
  1346. # across the build the same way a real run would.
  1347. _loki_app_sandbox_dir() {
  1348. local _base="${TARGET_DIR:-.}/.loki/app-sandbox"
  1349. if [ ! -d "$_base/home" ]; then
  1350. mkdir -p "$_base/home" "$_base/config" "$_base/data" \
  1351. "$_base/cache" "$_base/state" "$_base/tmp" 2>/dev/null || return 1
  1352. fi
  1353. printf '%s\n' "$_base"
  1354. }
  1355. # Run a command with the build-time app sandbox env applied, then restore the
  1356. # previous environment. Works for both shell functions (app_runner_start) and
  1357. # external commands because it only mutates env vars around the call. The
  1358. # background app launched by app_runner_start captures the sandbox HOME at fork
  1359. # time and keeps it for its lifetime; restoring here ensures the orchestrator
  1360. # (and the next iteration's provider invocation, which needs the REAL HOME for
  1361. # OAuth/credentials) is never left pointing at the sandbox.
  1362. _loki_with_app_sandbox() {
  1363. local _sb
  1364. _sb=$(_loki_app_sandbox_dir 2>/dev/null) || _sb=""
  1365. # If the sandbox could not be created, run unsandboxed rather than failing
  1366. # the build -- correctness of the app run takes priority over isolation.
  1367. if [ -z "$_sb" ]; then
  1368. "$@"
  1369. return $?
  1370. fi
  1371. # Resolve to an absolute path so child `cd` into the target dir cannot make
  1372. # a relative HOME point somewhere unexpected.
  1373. local _abs
  1374. _abs=$(cd "$_sb" 2>/dev/null && pwd) || _abs="$_sb"
  1375. # Save current values plus their set/unset state so restore is exact.
  1376. local _had_home="${HOME+x}" _old_home="${HOME:-}"
  1377. local _had_xch="${XDG_CONFIG_HOME+x}" _old_xch="${XDG_CONFIG_HOME:-}"
  1378. local _had_xdh="${XDG_DATA_HOME+x}" _old_xdh="${XDG_DATA_HOME:-}"
  1379. local _had_xcache="${XDG_CACHE_HOME+x}" _old_xcache="${XDG_CACHE_HOME:-}"
  1380. local _had_xsh="${XDG_STATE_HOME+x}" _old_xsh="${XDG_STATE_HOME:-}"
  1381. local _had_tmp="${TMPDIR+x}" _old_tmp="${TMPDIR:-}"
  1382. export HOME="$_abs/home"
  1383. export XDG_CONFIG_HOME="$_abs/config"
  1384. export XDG_DATA_HOME="$_abs/data"
  1385. export XDG_CACHE_HOME="$_abs/cache"
  1386. export XDG_STATE_HOME="$_abs/state"
  1387. export TMPDIR="$_abs/tmp"
  1388. local _rc=0
  1389. "$@" || _rc=$?
  1390. # Restore exactly (re-export prior value, or unset if it was unset before).
  1391. if [ -n "$_had_home" ]; then export HOME="$_old_home"; else unset HOME; fi
  1392. if [ -n "$_had_xch" ]; then export XDG_CONFIG_HOME="$_old_xch"; else unset XDG_CONFIG_HOME; fi
  1393. if [ -n "$_had_xdh" ]; then export XDG_DATA_HOME="$_old_xdh"; else unset XDG_DATA_HOME; fi
  1394. if [ -n "$_had_xcache" ]; then export XDG_CACHE_HOME="$_old_xcache"; else unset XDG_CACHE_HOME; fi
  1395. if [ -n "$_had_xsh" ]; then export XDG_STATE_HOME="$_old_xsh"; else unset XDG_STATE_HOME; fi
  1396. if [ -n "$_had_tmp" ]; then export TMPDIR="$_old_tmp"; else unset TMPDIR; fi
  1397. return "$_rc"
  1398. }
  1399. # Playwright Smoke Test module (v5.46.0)
  1400. if [ -f "${SCRIPT_DIR}/playwright-verify.sh" ]; then
  1401. # shellcheck source=playwright-verify.sh
  1402. source "${SCRIPT_DIR}/playwright-verify.sh"
  1403. fi
  1404. # Anonymous usage telemetry (opt-out: LOKI_TELEMETRY_DISABLED=true or DO_NOT_TRACK=1)
  1405. # Also check persistent opt-out from ~/.loki/config (#77)
  1406. if [ -f "${HOME}/.loki/config" ] && grep -q "^TELEMETRY_DISABLED=true" "${HOME}/.loki/config" 2>/dev/null; then
  1407. LOKI_TELEMETRY_DISABLED=true
  1408. export LOKI_TELEMETRY_DISABLED
  1409. unset LOKI_OTEL_ENDPOINT
  1410. fi
  1411. TELEMETRY_SCRIPT="$SCRIPT_DIR/telemetry.sh"
  1412. if [ -f "$TELEMETRY_SCRIPT" ]; then
  1413. # shellcheck source=telemetry.sh
  1414. source "$TELEMETRY_SCRIPT"
  1415. fi
  1416. # Crash-reporting helpers (Phase 0: local-only, zero egress).
  1417. # Provides loki_collection_enabled (unified opt-out), loki_crash_capture,
  1418. # loki_crash_friction, loki_show_disclosure_once.
  1419. CRASH_SCRIPT="$SCRIPT_DIR/crash.sh"
  1420. if [ -f "$CRASH_SCRIPT" ]; then
  1421. # shellcheck source=crash.sh
  1422. source "$CRASH_SCRIPT"
  1423. fi
  1424. # 2026 Research Enhancements (minimal additions)
  1425. PROMPT_REPETITION=${LOKI_PROMPT_REPETITION:-true}
  1426. CONFIDENCE_ROUTING=${LOKI_CONFIDENCE_ROUTING:-true}
  1427. AUTONOMY_MODE=${LOKI_AUTONOMY_MODE:-perpetual} # perpetual|checkpoint|supervised
  1428. # Session-pinned model (S0.1): pin a single tier for the whole main loop.
  1429. # Default is "sonnet" -> resolved via the "development" tier in the abstract
  1430. # tier map. Set LOKI_LEGACY_TIER_SWITCHING=true to restore the old
  1431. # per-iteration RARV-driven tier rotation in the main loop. The
  1432. # get_rarv_tier function is preserved for subagent dispatch regardless.
  1433. LOKI_SESSION_MODEL="${LOKI_SESSION_MODEL:-sonnet}"
  1434. LOKI_LEGACY_TIER_SWITCHING="${LOKI_LEGACY_TIER_SWITCHING:-false}"
  1435. export LOKI_SESSION_MODEL LOKI_LEGACY_TIER_SWITCHING
  1436. # Managed Agents (v6.83.0 Phase 1): opt-in integration with Claude Managed Agents
  1437. # Memory store backend. Parent flag gates everything; child flags gate features.
  1438. # Both off (default) => zero behavior change from v6.82.0.
  1439. LOKI_MANAGED_AGENTS="${LOKI_MANAGED_AGENTS:-false}"
  1440. LOKI_MANAGED_MEMORY="${LOKI_MANAGED_MEMORY:-false}"
  1441. # v7.0.0 Phase 2: remote->local hydrate on session boot (grandchild of MEMORY).
  1442. # Pulls semantic patterns + procedural skills once at init_loki_dir time.
  1443. LOKI_MANAGED_MEMORY_HYDRATE="${LOKI_MANAGED_MEMORY_HYDRATE:-false}"
  1444. # v7.0.0 Phase 3+4 foundation: umbrella flag for the multiagent-session path.
  1445. # Gates every providers/managed.py entry point (run_council,
  1446. # run_completion_council). Off by default because the Managed Agents
  1447. # multiagent surface is a research preview.
  1448. LOKI_EXPERIMENTAL_MANAGED_AGENTS="${LOKI_EXPERIMENTAL_MANAGED_AGENTS:-false}"
  1449. # v7.0.0 Phase 3 (T5): managed code-review council. Routes run_code_review
  1450. # through providers/managed.py::run_council when true. Requires parent +
  1451. # umbrella.
  1452. LOKI_EXPERIMENTAL_MANAGED_REVIEW="${LOKI_EXPERIMENTAL_MANAGED_REVIEW:-false}"
  1453. # v7.0.0 Phase 4 (T6): managed completion council. Routes council_should_stop
  1454. # through providers/managed.py::run_completion_council when true. Requires
  1455. # parent + umbrella.
  1456. LOKI_EXPERIMENTAL_MANAGED_COUNCIL="${LOKI_EXPERIMENTAL_MANAGED_COUNCIL:-false}"
  1457. export LOKI_MANAGED_AGENTS LOKI_MANAGED_MEMORY LOKI_MANAGED_MEMORY_HYDRATE LOKI_EXPERIMENTAL_MANAGED_AGENTS LOKI_EXPERIMENTAL_MANAGED_REVIEW LOKI_EXPERIMENTAL_MANAGED_COUNCIL
  1458. # Fail-fast: child on with parent off is a misconfiguration.
  1459. if [ "$LOKI_MANAGED_MEMORY" = "true" ] && [ "$LOKI_MANAGED_AGENTS" != "true" ]; then
  1460. echo "ERROR: LOKI_MANAGED_MEMORY=true requires LOKI_MANAGED_AGENTS=true" >&2
  1461. exit 2
  1462. fi
  1463. # Phase 2 fail-fast: HYDRATE is a grandchild of MEMORY.
  1464. if [ "$LOKI_MANAGED_MEMORY_HYDRATE" = "true" ] && [ "$LOKI_MANAGED_MEMORY" != "true" ]; then
  1465. echo "ERROR: LOKI_MANAGED_MEMORY_HYDRATE=true requires LOKI_MANAGED_MEMORY=true" >&2
  1466. exit 2
  1467. fi
  1468. # Same fail-fast for the experimental multiagent session path.
  1469. if [ "$LOKI_EXPERIMENTAL_MANAGED_AGENTS" = "true" ] && [ "$LOKI_MANAGED_AGENTS" != "true" ]; then
  1470. echo "ERROR: LOKI_EXPERIMENTAL_MANAGED_AGENTS=true requires LOKI_MANAGED_AGENTS=true" >&2
  1471. exit 2
  1472. fi
  1473. # Phase 3 fail-fast: REVIEW requires parent AND umbrella.
  1474. if [ "$LOKI_EXPERIMENTAL_MANAGED_REVIEW" = "true" ]; then
  1475. if [ "$LOKI_MANAGED_AGENTS" != "true" ]; then
  1476. echo "ERROR: LOKI_EXPERIMENTAL_MANAGED_REVIEW=true requires LOKI_MANAGED_AGENTS=true" >&2
  1477. exit 2
  1478. fi
  1479. if [ "$LOKI_EXPERIMENTAL_MANAGED_AGENTS" != "true" ]; then
  1480. echo "ERROR: LOKI_EXPERIMENTAL_MANAGED_REVIEW=true requires LOKI_EXPERIMENTAL_MANAGED_AGENTS=true" >&2
  1481. exit 2
  1482. fi
  1483. fi
  1484. # Phase 4 fail-fast: COUNCIL requires parent AND umbrella.
  1485. if [ "$LOKI_EXPERIMENTAL_MANAGED_COUNCIL" = "true" ]; then
  1486. if [ "$LOKI_MANAGED_AGENTS" != "true" ]; then
  1487. echo "ERROR: LOKI_EXPERIMENTAL_MANAGED_COUNCIL=true requires LOKI_MANAGED_AGENTS=true" >&2
  1488. exit 2
  1489. fi
  1490. if [ "$LOKI_EXPERIMENTAL_MANAGED_AGENTS" != "true" ]; then
  1491. echo "ERROR: LOKI_EXPERIMENTAL_MANAGED_COUNCIL=true requires LOKI_EXPERIMENTAL_MANAGED_AGENTS=true" >&2
  1492. exit 2
  1493. fi
  1494. fi
  1495. # Research-preview warning banner.
  1496. if [ "$LOKI_EXPERIMENTAL_MANAGED_AGENTS" = "true" ]; then
  1497. echo "WARN: LOKI_EXPERIMENTAL_MANAGED_AGENTS uses Managed Agents research preview; expect beta churn." >&2
  1498. fi
  1499. # Parallel Workflows (Git Worktrees)
  1500. PARALLEL_MODE=${LOKI_PARALLEL_MODE:-false}
  1501. MAX_WORKTREES=${LOKI_MAX_WORKTREES:-5}
  1502. MAX_PARALLEL_SESSIONS=${LOKI_MAX_PARALLEL_SESSIONS:-3}
  1503. # Nested-agent guard: the fixed testing/docs sub-streams each spawn a FURTHER
  1504. # nested `claude` session in a NON-namespaced worktree (`<project>-testing`,
  1505. # `<project>-docs`). When Loki itself runs inside another agent session (Claude
  1506. # Code sets CLAUDECODE; the Loki Mode skill, or a user driving the CLI as a
  1507. # background process from an agent), those nested spawns fight over the shared
  1508. # checkout/worktree paths and flood the log -- the reported "N issues in one dir"
  1509. # failure. So default the sub-streams OFF when nested; the core issue work still
  1510. # runs. Explicit LOKI_PARALLEL_TESTING/DOCS=true overrides (opt back in). This
  1511. # does NOT disable the user's own --parallel/--pr issue work, only the auxiliary
  1512. # testing/docs fan-out that is unsafe to nest.
  1513. _loki_nested_agent=false
  1514. if [ -n "${CLAUDECODE:-}" ] || [ -n "${LOKI_NESTED_AGENT:-}" ]; then
  1515. _loki_nested_agent=true
  1516. fi
  1517. if [ "$_loki_nested_agent" = "true" ]; then
  1518. PARALLEL_TESTING=${LOKI_PARALLEL_TESTING:-false}
  1519. PARALLEL_DOCS=${LOKI_PARALLEL_DOCS:-false}
  1520. else
  1521. PARALLEL_TESTING=${LOKI_PARALLEL_TESTING:-true}
  1522. PARALLEL_DOCS=${LOKI_PARALLEL_DOCS:-true}
  1523. fi
  1524. # Dynamic resource-aware session concurrency (Release 3, slice 3).
  1525. # DEFAULT OFF: when LOKI_DYNAMIC_CONCURRENCY is unset, effective_session_cap()
  1526. # returns exactly MAX_PARALLEL_SESSIONS, so behavior is identical to before.
  1527. # Opt in with LOKI_DYNAMIC_CONCURRENCY=1 to scale the session cap down when
  1528. # system CPU or memory is under pressure (read from .loki/state/resources.json).
  1529. DYNAMIC_CONCURRENCY=${LOKI_DYNAMIC_CONCURRENCY:-0}
  1530. # Optional higher ceiling on capable machines. Only takes effect with dynamic
  1531. # concurrency enabled; still resource-gated. Defaults to MAX_PARALLEL_SESSIONS.
  1532. MAX_PARALLEL_SESSIONS_CEILING=${LOKI_MAX_PARALLEL_SESSIONS_CEILING:-$MAX_PARALLEL_SESSIONS}
  1533. # Usage thresholds (percent). At/above CPU or MEM threshold the cap is halved.
  1534. CONCURRENCY_CPU_THRESHOLD=${LOKI_CONCURRENCY_CPU_THRESHOLD:-85}
  1535. CONCURRENCY_MEM_THRESHOLD=${LOKI_CONCURRENCY_MEM_THRESHOLD:-85}
  1536. # Critical threshold (percent). At/above this the cap is forced to 1.
  1537. CONCURRENCY_CRITICAL_THRESHOLD=${LOKI_CONCURRENCY_CRITICAL_THRESHOLD:-95}
  1538. # Gate Escalation Ladder (v6.10.0). LOKI_GATE_CLEAR_LIMIT is retained for
  1539. # compatibility, but it is now the repeated-blocker escalation threshold.
  1540. # A blocking gate is never cleared into a pass merely because it repeated.
  1541. GATE_CLEAR_LIMIT=${LOKI_GATE_CLEAR_LIMIT:-3}
  1542. GATE_ESCALATE_LIMIT=${LOKI_GATE_ESCALATE_LIMIT:-5}
  1543. GATE_PAUSE_LIMIT=${LOKI_GATE_PAUSE_LIMIT:-10}
  1544. # Workspace resolution: the build runs against TARGET_DIR, defaulting to the
  1545. # launch cwd. A caller can pin a per-build workspace by exporting
  1546. # LOKI_TARGET_DIR (and the matching LOKI_DIR=<dir>/.loki); the dashboard
  1547. # /api/control/start `workspace` param (Track-1 S1) does exactly this so a
  1548. # hosted build runs in its own dir instead of the engine's repo. Every
  1549. # $TARGET_DIR/.loki reference below then resolves under that workspace.
  1550. TARGET_DIR="${LOKI_TARGET_DIR:-$(pwd)}"
  1551. PARALLEL_BLOG=${LOKI_PARALLEL_BLOG:-false}
  1552. AUTO_MERGE=${LOKI_AUTO_MERGE:-true}
  1553. # Tier-aware harness policy (model-equivalence experiment, Planned/Stage-3).
  1554. # Given the resolved tier, export the steering-lever env vars UNLESS the operator
  1555. # already set them (operator override ALWAYS wins). Gated on
  1556. # LOKI_TIER_HARNESS_POLICY (default 0 = off) until Stage-2 ablation data justifies
  1557. # the numbers. Pure env-export, no control flow. The table below is a STUB --
  1558. # replace with Stage-2 ablation-derived numbers (see
  1559. # docs/MODEL-EQUIVALENCE-HARNESS-PLAN.md section 3).
  1560. loki_apply_tier_harness_policy() {
  1561. [ "${LOKI_TIER_HARNESS_POLICY:-0}" = "1" ] || return 0
  1562. local tier="$1" _iters _council _heal
  1563. # STUB - replace with Stage-2 ablation-derived numbers.
  1564. case "$tier" in
  1565. haiku|fast) _iters=8; _council=true; _heal=1 ;;
  1566. sonnet|development) _iters=5; _council=true; _heal=1 ;;
  1567. opus|planning) _iters=3; _council=true; _heal=0 ;;
  1568. *) return 0 ;;
  1569. esac
  1570. [ -z "${LOKI_MAX_ITERATIONS:-}" ] && export LOKI_MAX_ITERATIONS="$_iters"
  1571. [ -z "${LOKI_COUNCIL_ENABLED:-}" ] && export LOKI_COUNCIL_ENABLED="$_council"
  1572. [ -z "${LOKI_SELF_HEAL:-}" ] && export LOKI_SELF_HEAL="$_heal"
  1573. }
  1574. # Multi-project registry (v7.7.29): register this running project in the
  1575. # machine-global registry (~/.loki/dashboard/projects.json) so the dashboard
  1576. # can list and switch between projects running in different folders. Records
  1577. # the absolute path, pid, port, and status. Fully non-blocking and
  1578. # failure-swallowed: registry problems must never affect a build. Marked
  1579. # inactive again on exit via the trap below.
  1580. loki_register_running_project() {
  1581. local _status="${1:-running}"
  1582. [ -n "${LOKI_SKIP_PROJECT_REGISTRY:-}" ] && return 0
  1583. command -v python3 >/dev/null 2>&1 || return 0
  1584. local _skill="${LOKI_SKILL_DIR:-${PROJECT_DIR:-$SCRIPT_DIR/..}}"
  1585. LOKI_REG_TARGET="$TARGET_DIR" LOKI_REG_SKILL="$_skill" \
  1586. LOKI_REG_PID="$$" LOKI_REG_PORT="${LOKI_DASHBOARD_PORT:-57374}" \
  1587. LOKI_REG_STATUS="$_status" \
  1588. python3 - <<'PYREG' >/dev/null 2>&1 || true
  1589. import os, sys
  1590. sys.path.insert(0, os.environ.get("LOKI_REG_SKILL", "."))
  1591. try:
  1592. from dashboard import registry
  1593. target = os.path.abspath(os.environ["LOKI_REG_TARGET"])
  1594. entry = registry.register_project(target)
  1595. # Enrich with runtime fields the dashboard switcher uses.
  1596. reg = registry._load_registry()
  1597. pid = entry.get("id") or registry._generate_project_id(target)
  1598. if pid in reg.get("projects", {}):
  1599. reg["projects"][pid]["pid"] = int(os.environ.get("LOKI_REG_PID", "0") or 0)
  1600. reg["projects"][pid]["port"] = int(os.environ.get("LOKI_REG_PORT", "57374") or 57374)
  1601. reg["projects"][pid]["status"] = os.environ.get("LOKI_REG_STATUS", "running")
  1602. registry._save_registry(reg)
  1603. except Exception:
  1604. pass
  1605. PYREG
  1606. }
  1607. # v7.7.30: deliberate-exit teardown for the shared dashboard + registry.
  1608. # Marks THIS project (abspath of TARGET_DIR) stopped in the machine-global
  1609. # registry, then decides whether the shared standalone dashboard at
  1610. # ~/.loki/dashboard/dashboard.pid should be killed. The shared dashboard is
  1611. # killed ONLY when no other registered project still has a live pid (CLEAR);
  1612. # if any other project is still running (KEEP) it is left up. NEVER uses a
  1613. # blanket pkill and NEVER touches another folder's pids. Best-effort and
  1614. # failure-swallowed: teardown bookkeeping must never block a clean exit.
  1615. # Is this pid positively identified as our dashboard, rather than a recycled
  1616. # number now naming something else? Ownership-sensitive cleanup must fail closed:
  1617. # an absent `ps` result is not evidence, and a generic python/loki substring is
  1618. # not specific enough to authorize a signal.
  1619. _loki_pid_looks_like_dashboard() {
  1620. local _p="$1" _cmd
  1621. case "$_p" in ''|*[!0-9]*) return 1 ;; esac
  1622. [ "$_p" -gt 1 ] 2>/dev/null || return 1 # never signal pid 0/1
  1623. _cmd="$(ps -o command= -p "$_p" 2>/dev/null)"
  1624. [ -n "$_cmd" ] || return 1
  1625. # run.sh and `loki dashboard start` both launch this exact module form.
  1626. # Anchor the interpreter and module tokens so an unrelated command merely
  1627. # mentioning "dashboard.server" in an argument cannot forge identity.
  1628. [[ "$_cmd" =~ ^[[:space:]]*([^[:space:]]*/)?[Pp]ython([0-9]+([.][0-9]+)*)?[[:space:]]+-m[[:space:]]+dashboard[.]server([[:space:]]|$) ]]
  1629. }
  1630. loki_mark_project_stopped_and_maybe_kill_shared_dashboard() {
  1631. local _skill="${LOKI_SKILL_DIR:-${PROJECT_DIR:-$SCRIPT_DIR/..}}"
  1632. local _shared_pidfile="${HOME}/.loki/dashboard/dashboard.pid"
  1633. local _decision="CLEAR"
  1634. if [ -z "${LOKI_SKIP_PROJECT_REGISTRY:-}" ] && command -v python3 >/dev/null 2>&1; then
  1635. # (a) Mark this project stopped in the shared registry.
  1636. LOKI_REG_TARGET="$TARGET_DIR" LOKI_REG_SKILL="$_skill" \
  1637. python3 - <<'PYSTOP' >/dev/null 2>&1 || true
  1638. import os, sys
  1639. sys.path.insert(0, os.environ.get("LOKI_REG_SKILL", "."))
  1640. try:
  1641. from dashboard import registry
  1642. registry.mark_project_stopped(os.path.abspath(os.environ["LOKI_REG_TARGET"]))
  1643. except Exception:
  1644. pass
  1645. PYSTOP
  1646. # (b) CLEAR/KEEP check: any OTHER project still alive keeps the
  1647. # shared dashboard up (this project is already marked stopped above).
  1648. _decision="$(LOKI_REG_SKILL="$_skill" python3 - <<'PYCHECK' 2>/dev/null || echo CLEAR
  1649. import os, sys
  1650. sys.path.insert(0, os.environ.get("LOKI_REG_SKILL", "."))
  1651. try:
  1652. from dashboard import registry
  1653. alive = 0
  1654. for p in registry.list_projects(include_inactive=True):
  1655. pid = p.get("pid")
  1656. if isinstance(pid, int) and pid > 0:
  1657. try:
  1658. os.kill(pid, 0)
  1659. alive += 1
  1660. except OSError:
  1661. pass
  1662. print("CLEAR" if alive == 0 else "KEEP")
  1663. except Exception:
  1664. print("CLEAR")
  1665. PYCHECK
  1666. )"
  1667. fi
  1668. # (c) Only tear down the SHARED dashboard when no other project remains
  1669. # (CLEAR), or when python3 was unavailable (legacy fallback: avoid leaking
  1670. # the shared dashboard on minimal systems).
  1671. if [ "$_decision" = "CLEAR" ]; then
  1672. if [ -f "$_shared_pidfile" ]; then
  1673. local _shared_pid
  1674. _shared_pid=$(cat "$_shared_pidfile" 2>/dev/null)
  1675. # Identity check before signalling. dashboard.pid is removed only on
  1676. # the explicit stop paths (:16608, :16658, :17399) -- nothing covers
  1677. # a crash, so the file outlives its dashboard. Measured in this very
  1678. # checkout: .loki/dashboard/dashboard.pid held a DEAD 87992 with an
  1679. # 8-day-old mtime. PIDs recycle, so a stale number eventually names
  1680. # an unrelated LIVE process and this `kill -9` hits it.
  1681. #
  1682. # kill -0 is NOT sufficient: a recycled pid is alive by definition,
  1683. # which is exactly the insufficiency the loki.pgid self-check had.
  1684. # This guard fails CLOSED: if `ps` says nothing, or says something
  1685. # that is not the dashboard's own command, we do not signal. The
  1686. # cost of failing closed is a leaked dashboard the next explicit
  1687. # stop reaps; the cost of failing open was signalling a stranger.
  1688. if [ -n "$_shared_pid" ] && _loki_pid_looks_like_dashboard "$_shared_pid"; then
  1689. kill "$_shared_pid" 2>/dev/null || true
  1690. sleep 0.5
  1691. kill -9 "$_shared_pid" 2>/dev/null || true
  1692. fi
  1693. rm -f "$_shared_pidfile" 2>/dev/null || true
  1694. fi
  1695. # A port number and an HTTP response are not ownership evidence. In
  1696. # particular, a 404 can be an unrelated healthy service. The shared PID
  1697. # file plus positive command identity above is the only authority to
  1698. # signal a process; absent or forged ownership leaves every port holder
  1699. # untouched.
  1700. fi
  1701. }
  1702. # Register as running now. We deliberately do NOT install an EXIT trap to
  1703. # flip it to idle: a top-level EXIT trap here would be clobbered by the
  1704. # lock-release EXIT trap installed later in the main path (and could
  1705. # interfere with it). Instead the dashboard determines live vs stale by
  1706. # checking whether the recorded pid is still alive (registry stores pid),
  1707. # which is robust even on hard kills where a trap would never fire.
  1708. loki_register_running_project running
  1709. # Complexity Tiers (Auto-Claude pattern)
  1710. # auto = detect from PRD/codebase, simple = 3 phases, standard = 6 phases, complex = 8 phases
  1711. COMPLEXITY_TIER=${LOKI_COMPLEXITY:-auto}
  1712. DETECTED_COMPLEXITY=""
  1713. # Multi-Provider Support (v5.0.0)
  1714. # Provider: auto-detected when unset; claude > opencode.
  1715. #
  1716. # WHY NOT `:-claude`. That default was a hard failure for anyone who has opencode
  1717. # but not Claude: run.sh would select a provider that is not installed and die,
  1718. # even though auto_detect_provider() has existed in providers/loader.sh -- with
  1719. # the right priority order and its own passing test -- since v5.0.0. Nothing in
  1720. # production ever called it. Detection was built, tested, and never wired.
  1721. #
  1722. # An EXPLICIT choice still wins: this only fills an unset value, so
  1723. # LOKI_PROVIDER=opencode and --provider opencode are untouched.
  1724. _LOKI_PROVIDER_WAS_EXPLICIT=1
  1725. [ -z "${LOKI_PROVIDER:-}" ] && _LOKI_PROVIDER_WAS_EXPLICIT=0
  1726. # Source provider configuration
  1727. PROVIDERS_DIR="$PROJECT_DIR/providers"
  1728. if [ -f "$PROVIDERS_DIR/loader.sh" ]; then
  1729. # shellcheck source=/dev/null
  1730. source "$PROVIDERS_DIR/loader.sh"
  1731. # Detect only when the operator expressed no preference. Sourcing the
  1732. # loader first is required -- auto_detect_provider() is defined there.
  1733. if [ "$_LOKI_PROVIDER_WAS_EXPLICIT" -eq 0 ]; then
  1734. _detected="$(auto_detect_provider 2>/dev/null || true)"
  1735. if [ -n "$_detected" ]; then
  1736. LOKI_PROVIDER="$_detected"
  1737. [ -n "${LOKI_QUICK_INNER:-}" ] || echo "[loki] provider: $LOKI_PROVIDER (auto-detected)" >&2
  1738. else
  1739. # Nothing installed. Keep the historical default so the existing
  1740. # "not installed" error path reports claude, which is the actionable
  1741. # message -- rather than an empty provider name.
  1742. LOKI_PROVIDER=claude
  1743. fi
  1744. fi
  1745. # Validate provider. A removed provider (REMOVED_PROVIDERS, providers/loader.sh) gets
  1746. # the one migration line from providers/loader.sh and exit 2.
  1747. if loki_is_removed_provider "$LOKI_PROVIDER"; then
  1748. loki_removed_provider_message "$LOKI_PROVIDER" >&2
  1749. exit 2
  1750. fi
  1751. if ! validate_provider "$LOKI_PROVIDER"; then
  1752. echo "ERROR: Unknown provider: $LOKI_PROVIDER" >&2
  1753. echo "Supported providers: ${SUPPORTED_PROVIDERS[*]}" >&2
  1754. exit 1
  1755. fi
  1756. # Load provider config
  1757. if ! load_provider "$LOKI_PROVIDER"; then
  1758. echo "ERROR: Failed to load provider config: $LOKI_PROVIDER" >&2
  1759. exit 1
  1760. fi
  1761. # Save provider for future runs (if .loki dir exists or will be created)
  1762. if [ -d ".loki/state" ] || mkdir -p ".loki/state" 2>/dev/null; then
  1763. echo "$LOKI_PROVIDER" > ".loki/state/provider"
  1764. fi
  1765. else
  1766. # Fallback: Claude-only mode (backwards compatibility)
  1767. PROVIDER_NAME="claude"
  1768. PROVIDER_CLI="claude"
  1769. PROVIDER_AUTONOMOUS_FLAG="--dangerously-skip-permissions"
  1770. PROVIDER_PROMPT_FLAG="-p"
  1771. PROVIDER_DEGRADED=false
  1772. PROVIDER_DISPLAY_NAME="Claude Code"
  1773. PROVIDER_HAS_PARALLEL=true
  1774. PROVIDER_HAS_SUBAGENTS=true
  1775. PROVIDER_HAS_TASK_TOOL=true
  1776. PROVIDER_HAS_MCP=true
  1777. PROVIDER_PROMPT_POSITIONAL=false
  1778. fi
  1779. # Track worktree PIDs for cleanup (requires bash 4+ for associative arrays)
  1780. # BASH_VERSION_MAJOR is defined at script startup
  1781. if [ "$BASH_VERSION_MAJOR" -ge 4 ] 2>/dev/null; then
  1782. declare -A WORKTREE_PIDS=()
  1783. declare -A WORKTREE_PATHS=()
  1784. declare -A WORKTREE_BASE_SHAS=()
  1785. else
  1786. # Fallback: parallel mode will check and warn
  1787. # shellcheck disable=SC2178
  1788. WORKTREE_PIDS=""
  1789. # shellcheck disable=SC2178
  1790. WORKTREE_PATHS=""
  1791. # shellcheck disable=SC2178
  1792. WORKTREE_BASE_SHAS=""
  1793. fi
  1794. # Track background install PIDs for cleanup (indexed array, works on all bash versions)
  1795. WORKTREE_INSTALL_PIDS=()
  1796. # Colors
  1797. RED='\033[0;31m'
  1798. GREEN='\033[0;32m'
  1799. YELLOW='\033[1;33m'
  1800. BLUE='\033[0;34m'
  1801. CYAN='\033[0;36m'
  1802. BOLD='\033[1m'
  1803. DIM='\033[2m'
  1804. NC='\033[0m'
  1805. #===============================================================================
  1806. # Logging Functions
  1807. #===============================================================================
  1808. log_header() {
  1809. echo ""
  1810. echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}"
  1811. echo -e "${BLUE}║${NC} ${BOLD}$1${NC}"
  1812. echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}"
  1813. }
  1814. # VERBOSITY. LOKI_LOG_LEVEL is one of debug|info|warn|error (default info);
  1815. # LOKI_QUIET=1 is shorthand for warn.
  1816. #
  1817. # Implemented at these five functions rather than at 527 call sites, which is
  1818. # both the smaller diff and the only version that cannot drift -- a new
  1819. # log_info added next year is covered without anyone remembering to gate it.
  1820. #
  1821. # WHY THIS IS NEEDED DESPITE GOOD TTY DETECTION. The heavy decoration (HUD,
  1822. # completion card, start headline) is already `[ -t 1 ]`-guarded and vanishes
  1823. # off a TTY, so CI output was never the wall of banners it might have been.
  1824. # But 527 log_info/log_step calls are unguarded and print regardless, and a
  1825. # pipeline that wants only warnings had no way to ask. log_debug already
  1826. # honored LOKI_DEBUG; the other levels honored nothing.
  1827. #
  1828. # ERRORS ARE NEVER SUPPRESSED. `error` is the floor: the quietest setting still
  1829. # prints failures. A verbosity flag that can hide the reason a build failed is
  1830. # a footgun, not a feature.
  1831. _loki_log_threshold() {
  1832. case "${LOKI_LOG_LEVEL:-$([ "${LOKI_QUIET:-0}" = "1" ] && echo warn || echo info)}" in
  1833. debug) echo 0 ;;
  1834. info) echo 1 ;;
  1835. warn) echo 2 ;;
  1836. error) echo 3 ;;
  1837. *) echo 1 ;; # unrecognized value behaves as the default, never silences
  1838. esac
  1839. }
  1840. _loki_log_enabled() { [ "$1" -ge "$(_loki_log_threshold)" ]; }
  1841. log_info() { _loki_log_enabled 1 && echo -e "${GREEN}[INFO]${NC} $*" || true; }
  1842. log_warn() { _loki_log_enabled 2 && echo -e "${YELLOW}[WARN]${NC} $*" || true; }
  1843. log_warning() { log_warn "$@"; } # Alias for backwards compatibility
  1844. log_error() { local _fd=1; [ -z "${LOKI_QUICK_INNER:-}" ] || _fd=2; echo -e "${RED}[ERROR]${NC} $*" >&"$_fd"; }
  1845. log_step() { _loki_log_enabled 1 && echo -e "${CYAN}[STEP]${NC} $*" || true; }
  1846. log_debug() { [[ "${LOKI_DEBUG:-}" == "true" ]] && echo -e "${CYAN}[DEBUG]${NC} $*" >&2 || true; }
  1847. #===============================================================================
  1848. # Failure diagnosis helpers (T2.4 / T2.5 / T2.6)
  1849. #
  1850. # These make a failing or crashed build self-explanatory: a copy-pasteable
  1851. # "loki why" hint on any non-zero exit, a durable CLASSIFIED LAST_ERROR record
  1852. # on a failed iteration, and a best-effort terminal record on an untrapped
  1853. # death. Every one is best-effort and NEVER alters the build's exit code.
  1854. #===============================================================================
  1855. # _loki_surface_why_hint (T2.4): print the "loki why" hint to stderr and write
  1856. # it to .loki/NEXT_STEPS.txt. Called once from main() finalization when the run
  1857. # failed (result != 0). Best-effort: never crashes, never changes exit code.
  1858. _loki_surface_why_hint() {
  1859. local loki_dir="${TARGET_DIR:-.}/.loki"
  1860. local hint="For a plain-language diagnosis of what happened, run: loki why"
  1861. printf '%s\n' "$hint" >&2 || true
  1862. mkdir -p "$loki_dir" 2>/dev/null || true
  1863. printf '%s\n' "$hint" > "$loki_dir/NEXT_STEPS.txt" 2>/dev/null || true
  1864. return 0
  1865. }
  1866. # _loki_restore_base_branch (FC-EMPTY-RUN-BRANCH-LEFT-CHECKED-OUT, legacy sibling
  1867. # of engine10 finishRunBranch): when a session ended non-zero and its branch
  1868. # tip is still the recorded start commit, nothing was committed. Put the checkout
  1869. # back on the starting branch and delete that empty session branch. Narrower than
  1870. # the engine10 side, which also bundles a branch with commits into .loki/runs/
  1871. # before removing it: here a branch with any commit is never touched, a dirty tracked
  1872. # tree keeps the branch and prints the way back, and a stop signal (130, 143) is
  1873. # left alone because the cleanup trap owns it. Best-effort, never changes the
  1874. # exit code.
  1875. _loki_restore_base_branch() {
  1876. local result="${1:-0}" td="${TARGET_DIR:-.}"
  1877. case "$result" in 0|130|143) return 0 ;; esac
  1878. local base_file="$td/.loki/state/base-branch.txt" sha_file="$td/.loki/state/session-start-sha" br_file="$td/.loki/state/agent-branch.txt"
  1879. [ -s "$base_file" ] && [ -s "$sha_file" ] && [ -s "$br_file" ] || return 0
  1880. local base branch start tip cur
  1881. base="$(cat "$base_file" 2>/dev/null)"; branch="$(cat "$br_file" 2>/dev/null)"; start="$(cat "$sha_file" 2>/dev/null)"
  1882. case "$branch" in loki/session-*) ;; *) return 0 ;; esac
  1883. [ -n "$base" ] && [ "$base" != "$branch" ] || return 0
  1884. tip="$(git -C "$td" rev-parse --verify -q "refs/heads/$branch" 2>/dev/null)" || return 0
  1885. [ "$tip" = "$start" ] || return 0
  1886. cur="$(git -C "$td" symbolic-ref --short -q HEAD 2>/dev/null || true)"
  1887. if [ "$cur" = "$branch" ]; then
  1888. if [ -n "$(git -C "$td" status --porcelain --untracked-files=no 2>/dev/null)" ]; then
  1889. printf 'loki: tree left on %s, return with: git checkout %s\n' "$branch" "$base" >&2
  1890. return 0
  1891. fi
  1892. git -C "$td" checkout -q "$base" 2>/dev/null || { printf 'loki: tree left on %s, return with: git checkout %s\n' "$branch" "$base" >&2; return 0; }
  1893. cur="$base"
  1894. fi
  1895. [ "$cur" = "$base" ] || return 0
  1896. git -C "$td" branch -d "$branch" >/dev/null 2>&1 && rm -f "$br_file" 2>/dev/null
  1897. return 0
  1898. }
  1899. # _loki_write_last_error (T2.5): write a durable, classified failure record to
  1900. # .loki/state/LAST_ERROR.json. Schema:
  1901. # {
  1902. # "iteration": <int>,
  1903. # "error_class": "provider_empty_output"|"build_timeout"|"rate_limited"
  1904. # |"auth_error"|"unknown",
  1905. # "brief": "<one honest sentence>",
  1906. # "timestamp": "<UTC ISO-8601>"
  1907. # }
  1908. # This is what `loki why` (in autonomy/loki, NOT owned here) can later read.
  1909. # Built via python3 so the free-text brief can never break the JSON. Entirely
  1910. # best-effort: any failure is swallowed and the build is never crashed.
  1911. # Usage: _loki_write_last_error <iteration> <error_class> <brief>
  1912. _loki_write_last_error() {
  1913. local iteration="${1:-0}"
  1914. local error_class="${2:-unknown}"
  1915. local brief="${3:-An iteration failed.}"
  1916. local loki_dir="${TARGET_DIR:-.}/.loki"
  1917. local state_dir="$loki_dir/state"
  1918. mkdir -p "$state_dir" 2>/dev/null || true
  1919. LOKI_LE_ITER="$iteration" \
  1920. LOKI_LE_CLASS="$error_class" \
  1921. LOKI_LE_BRIEF="$brief" \
  1922. LOKI_LE_FILE="$state_dir/LAST_ERROR.json" \
  1923. python3 -c "
  1924. import json, os, tempfile
  1925. try:
  1926. rec = {
  1927. 'iteration': int(os.environ.get('LOKI_LE_ITER', '0') or 0),
  1928. 'error_class': os.environ.get('LOKI_LE_CLASS', 'unknown'),
  1929. 'brief': os.environ.get('LOKI_LE_BRIEF', ''),
  1930. 'timestamp': __import__('datetime').datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
  1931. }
  1932. target = os.environ['LOKI_LE_FILE']
  1933. d = os.path.dirname(target)
  1934. fd, tmp = tempfile.mkstemp(dir=d, suffix='.json')
  1935. with os.fdopen(fd, 'w') as f:
  1936. json.dump(rec, f)
  1937. os.replace(tmp, target)
  1938. except Exception:
  1939. pass
  1940. " 2>/dev/null || true
  1941. return 0
  1942. }
  1943. # _loki_archive_last_error <last_error_json> <history_jsonl>
  1944. # LEARN-FORWARD: append a prior run's LAST_ERROR record to an append-only,
  1945. # bounded failure-history so the next run can detect a REPEATED failure signature
  1946. # (same error_class recurring) even though the single LAST_ERROR.json is cleared
  1947. # each run. No-op when there is no prior error. Bounded to the most recent 50
  1948. # entries so it never grows unbounded. Best-effort; never fails the run.
  1949. _loki_archive_last_error() {
  1950. local src="${1:-}"
  1951. local hist="${2:-}"
  1952. [ -n "$src" ] && [ -n "$hist" ] || return 0
  1953. [ -f "$src" ] || return 0
  1954. LOKI_AE_SRC="$src" LOKI_AE_HIST="$hist" python3 -c "
  1955. import json, os, tempfile
  1956. src = os.environ['LOKI_AE_SRC']
  1957. hist = os.environ['LOKI_AE_HIST']
  1958. try:
  1959. with open(src) as f:
  1960. rec = json.load(f)
  1961. if not isinstance(rec, dict):
  1962. raise ValueError('bad record')
  1963. lines = []
  1964. if os.path.exists(hist):
  1965. try:
  1966. with open(hist) as hf:
  1967. lines = [ln for ln in hf.read().splitlines() if ln.strip()]
  1968. except Exception:
  1969. lines = []
  1970. lines.append(json.dumps(rec, separators=(',', ':')))
  1971. lines = lines[-50:]
  1972. d = os.path.dirname(hist) or '.'
  1973. fd, tmp = tempfile.mkstemp(dir=d, suffix='.jsonl')
  1974. with os.fdopen(fd, 'w') as f:
  1975. f.write('\n'.join(lines) + '\n')
  1976. os.replace(tmp, hist)
  1977. except Exception:
  1978. pass
  1979. " 2>/dev/null || true
  1980. return 0
  1981. }
  1982. # _loki_classify_iteration_error (T2.5 helper): map an iteration's signals to one
  1983. # of the LAST_ERROR error_class values. Conservative: only returns a specific
  1984. # class when a signal confidently supports it, else "unknown". Never fabricates
  1985. # build_timeout (no timeout signal is detected here, so it is reserved for a
  1986. # caller that has one). Echoes the class on stdout.
  1987. # Usage: _loki_classify_iteration_error <iter_output_file> <empty_output_flag 0|1>
  1988. _loki_classify_iteration_error() {
  1989. local iter_output="${1:-}"
  1990. local empty_flag="${2:-0}"
  1991. if [ "$empty_flag" = "1" ]; then
  1992. echo "provider_empty_output"
  1993. return 0
  1994. fi
  1995. # A failed provider start (for example claude -p exiting 1) leaves a 0-byte log.
  1996. if [ -n "$iter_output" ] && [ -f "$iter_output" ] && [ ! -s "$iter_output" ]; then
  1997. echo "provider_empty_output"
  1998. return 0
  1999. fi
  2000. # Rate limit: reuse the same detector the retry path uses.
  2001. if [ -n "$iter_output" ] && [ -f "$iter_output" ] && detect_rate_limit "$iter_output" 2>/dev/null | grep -qE '^[1-9]'; then
  2002. echo "rate_limited"
  2003. return 0
  2004. fi
  2005. # Auth error: a clear 401/403/unauthorized in the output tail.
  2006. if [ -n "$iter_output" ] && [ -f "$iter_output" ]; then
  2007. local _tail
  2008. _tail="$(tail -n 40 "$iter_output" 2>/dev/null || true)"
  2009. if printf '%s\n' "$_tail" | grep -qiE '(http[ /]?40[13]|status[: ]+40[13]|unauthorized|invalid api key|authentication[_ ]error|401 )' 2>/dev/null; then
  2010. echo "auth_error"
  2011. return 0
  2012. fi
  2013. fi
  2014. echo "unknown"
  2015. return 0
  2016. }
  2017. # _loki_build_self_heal_hint (SLICE 6c): route the PRIOR iteration's classified
  2018. # error signature into the NEXT iteration's prompt so the loop fixes forward.
  2019. # Reads .loki/state/LAST_ERROR.json (written by _loki_write_last_error on a
  2020. # failed iteration), emits a concise structured heal hint naming the error_class
  2021. # + brief on stdout, then ARCHIVES-then-CLEARS the record so the hint injects
  2022. # exactly ONCE (reuses the existing archive-then-clear pattern; it does not
  2023. # repeat forever). Best-effort: any failure emits nothing and never crashes the
  2024. # build.
  2025. #
  2026. # Gated by the caller on LOKI_SELF_HEAL (default 0 -- opt-in, stock runs
  2027. # unaffected). "unknown" is treated as NOT actionable (no specific class to
  2028. # target), so an unclassified failure produces no hint.
  2029. # Usage: _loki_build_self_heal_hint (echoes hint or nothing)
  2030. _loki_build_self_heal_hint() {
  2031. local loki_dir="${TARGET_DIR:-.}/.loki"
  2032. local src="$loki_dir/state/LAST_ERROR.json"
  2033. [ -f "$src" ] || return 0
  2034. local hint
  2035. hint="$(_LOKI_SH_SRC="$src" python3 -c "
  2036. import json, os
  2037. try:
  2038. with open(os.environ['_LOKI_SH_SRC']) as f:
  2039. rec = json.load(f)
  2040. if not isinstance(rec, dict):
  2041. raise SystemExit
  2042. ec = str(rec.get('error_class', '') or '').strip()
  2043. if ec in ('', 'unknown'):
  2044. raise SystemExit
  2045. brief = str(rec.get('brief', '') or '').strip()
  2046. it = rec.get('iteration', '?')
  2047. print('SELF_HEAL_HINT: the previous iteration (#%s) failed with error_class=%s. %s Address this specific failure FIRST before any new work.' % (it, ec, brief))
  2048. except SystemExit:
  2049. pass
  2050. except Exception:
  2051. pass
  2052. " 2>/dev/null || true)"
  2053. [ -n "$hint" ] || return 0
  2054. # Archive-then-clear so it injects once (LEARN-FORWARD: the lesson still
  2055. # lands in failure-history.jsonl before the single record is removed).
  2056. _loki_archive_last_error "$src" "$loki_dir/state/failure-history.jsonl" 2>/dev/null || true
  2057. rm -f "$src" 2>/dev/null || true
  2058. printf '%s' "$hint"
  2059. return 0
  2060. }
  2061. # _loki_terminal_record (T2.6, SAFE SUBSET): on an untrapped exit where the
  2062. # persisted run status is still "running" (a true mid-provider-call crash on a
  2063. # trappable signal -- SIGTERM/SIGINT/SIGHUP via the lock-release trap), leave a
  2064. # best-effort, classified LAST_ERROR record so a post-crash `loki why` is not
  2065. # stale. Piggybacks the existing lock-release EXIT trap (see main()) -- it does
  2066. # NOT install a new broad EXIT trap.
  2067. #
  2068. # IMPORTANT (why this does NOT rewrite autonomy-state.json): the resume-detection
  2069. # block in this file (search "Durable resume") keys the ENT-2 pod-loss resume on
  2070. # prev_status == "running". Flipping the status to "exited" here would make a
  2071. # crashed-but-resumable build (LOKI_DURABLE_STATE=1) reset to iteration 0 on the
  2072. # next start, destroying durable progress. So this writes ONLY the LAST_ERROR
  2073. # side-record (which is what `loki why` reads) and deliberately leaves the
  2074. # status untouched. SIGKILL / power-loss are uncatchable (no trap fires); the
  2075. # ENT-2 durable-resume path covers those. Never alters the exit code; best-effort.
  2076. _loki_terminal_record() {
  2077. local state_file
  2078. state_file="$(_loki_state_file 2>/dev/null)" || return 0
  2079. [ -n "$state_file" ] || return 0
  2080. [ -f "$state_file" ] || return 0
  2081. local _status
  2082. _status="$(LOKI_TR_FILE="$state_file" python3 -c "
  2083. import json, os
  2084. try:
  2085. print(json.load(open(os.environ['LOKI_TR_FILE'])).get('status','unknown'))
  2086. except Exception:
  2087. print('unknown')
  2088. " 2>/dev/null || echo "unknown")"
  2089. # Only act on a genuinely mid-flight "running" status. Any settled status
  2090. # (council_approved, failed, exited, paused, ...) is left untouched.
  2091. [ "$_status" = "running" ] || return 0
  2092. # Leave a classified LAST_ERROR so `loki why` has something honest -- WITHOUT
  2093. # touching autonomy-state.json (preserving the ENT-2 "running" resume signal).
  2094. _loki_write_last_error "${ITERATION_COUNT:-0}" "unknown" \
  2095. "The build process exited unexpectedly before finishing (possible crash or kill)." 2>/dev/null || true
  2096. return 0
  2097. }
  2098. # _loki_write_rate_limit_signal (T2.7): write a best-effort
  2099. # .loki/signals/RATE_LIMITED file. This is FORWARD-LAID infrastructure: no
  2100. # consumer reads it yet (a future dashboard / external watcher could, to tell a
  2101. # normal provider rate-limit wait apart from a hang). The user-visible signal
  2102. # today is the log_info line at the wait site; this file is the durable record. Schema:
  2103. # {"rate_limited": true, "wait_seconds": <int>, "reset_time": "<string>"}
  2104. # Built via python3 so the reset-time string can never break the JSON. Entirely
  2105. # best-effort: never crashes, never alters the build.
  2106. # Usage: _loki_write_rate_limit_signal <wait_seconds> <reset_time>
  2107. _loki_write_rate_limit_signal() {
  2108. local wait_seconds="${1:-0}"
  2109. local reset_time="${2:-}"
  2110. local signals_dir="${TARGET_DIR:-.}/.loki/signals"
  2111. mkdir -p "$signals_dir" 2>/dev/null || true
  2112. LOKI_RL_WAIT="$wait_seconds" \
  2113. LOKI_RL_RESET="$reset_time" \
  2114. LOKI_RL_FILE="$signals_dir/RATE_LIMITED" \
  2115. python3 -c "
  2116. import json, os, tempfile
  2117. try:
  2118. w = os.environ.get('LOKI_RL_WAIT', '0')
  2119. try:
  2120. w = int(w)
  2121. except Exception:
  2122. w = 0
  2123. rec = {'rate_limited': True, 'wait_seconds': w, 'reset_time': os.environ.get('LOKI_RL_RESET', '')}
  2124. target = os.environ['LOKI_RL_FILE']
  2125. d = os.path.dirname(target)
  2126. fd, tmp = tempfile.mkstemp(dir=d, suffix='.json')
  2127. with os.fdopen(fd, 'w') as f:
  2128. json.dump(rec, f)
  2129. os.replace(tmp, target)
  2130. except Exception:
  2131. pass
  2132. " 2>/dev/null || true
  2133. return 0
  2134. }
  2135. # Live Build HUD (v7.71.0): a single append-only per-iteration status line on the
  2136. # interactive TTY path. Pure additive stdout decoration -- never piped into any
  2137. # tee, so the dashboard agent.log and the stream-json parser are untouched. The
  2138. # whole function is structured so any internal failure still returns 0; it can
  2139. # never abort the iteration loop. Gated: TTY + not background + LOKI_HUD != 0.
  2140. # Usage: render_build_hud <iter> <phase> <duration_secs>
  2141. render_build_hud() {
  2142. # Gate first: emit nothing unless interactive TTY, not --bg, and not opted out.
  2143. # Inverted into an early return so the off-TTY path is byte-identical (no output).
  2144. if ! { [ -t 1 ] && [ "${BACKGROUND_MODE:-false}" != "true" ] && [ "${LOKI_HUD:-1}" != "0" ]; }; then
  2145. return 0
  2146. fi
  2147. local _iter="${1:-0}" _phase="${2:-?}" _dur="${3:-0}"
  2148. [ -z "$_phase" ] && _phase="?"
  2149. # Sanitize numerics so set -u arithmetic/format below can never error out.
  2150. case "$_dur" in (*[!0-9]*|'') _dur=0 ;; esac
  2151. local _max="${MAX_ITERATIONS:-0}"
  2152. case "$_max" in (*[!0-9]*|'') _max=0 ;; esac
  2153. # Field list: each field is appended only when its data is present. Missing
  2154. # data => the field is omitted entirely (never a fabricated value).
  2155. local _fields="$_phase"
  2156. _fields="${_fields} | iter ${_iter}/${_max}"
  2157. # Cost from the context tracker totals (the same field the dashboard shows).
  2158. # NOTE: tracking.json totals accumulate across runs in the same dir (not reset
  2159. # on a fresh `loki start`), so this is cumulative cost for the project dir, not
  2160. # strictly this single run. Labeled plainly "$" / "cost" to avoid overclaiming.
  2161. # OMIT entirely when empty/missing/non-Claude.
  2162. local _cost=""
  2163. if command -v python3 >/dev/null 2>&1; then
  2164. _cost="$(python3 -c "
  2165. import json
  2166. try:
  2167. t = json.load(open('.loki/context/tracking.json'))
  2168. c = t.get('totals', {}).get('total_cost_usd', 0)
  2169. c = float(c)
  2170. if c > 0:
  2171. print('%.2f' % c)
  2172. except Exception:
  2173. pass
  2174. " 2>/dev/null || true)"
  2175. fi
  2176. [ -n "$_cost" ] && _fields="${_fields} | \$${_cost}"
  2177. # Files changed (+ins/-del and file count) vs the run start SHA. Reuse the
  2178. # build_completion_summary diff approach incl. the .loki/.git exclude pathspec.
  2179. # OMIT when no start sha, not a git repo, or no diff data.
  2180. local _start_sha="${_LOKI_RUN_START_SHA:-}"
  2181. if [ -n "$_start_sha" ]; then
  2182. local _shortstat _ins _del _files
  2183. _shortstat="$( (cd "${TARGET_DIR:-.}" && git diff --shortstat "${_start_sha}..HEAD" -- . ':(exclude).loki/' ':(exclude).git/' ':(exclude)**/.loki/**') 2>/dev/null || true )"
  2184. if [ -n "$_shortstat" ]; then
  2185. _files="$(printf '%s\n' "$_shortstat" | grep -oE '[0-9]+ file' | grep -oE '[0-9]+' | head -1 2>/dev/null || true)"
  2186. _ins="$(printf '%s\n' "$_shortstat" | grep -oE '[0-9]+ insertion' | grep -oE '[0-9]+' | head -1 2>/dev/null || true)"
  2187. _del="$(printf '%s\n' "$_shortstat" | grep -oE '[0-9]+ deletion' | grep -oE '[0-9]+' | head -1 2>/dev/null || true)"
  2188. [ -z "$_files" ] && _files=0
  2189. [ -z "$_ins" ] && _ins=0
  2190. [ -z "$_del" ] && _del=0
  2191. _fields="${_fields} | +${_ins}/-${_del} (${_files} files)"
  2192. fi
  2193. fi
  2194. # Per-iteration time (the duration arg). Labeled "took" (not "iter") so it
  2195. # does not collide with the "iter N/max" iteration-count field above.
  2196. _fields="${_fields} | took $(_hud_fmt_secs "$_dur")"
  2197. # Elapsed time for the whole run, from the once-captured run-start epoch.
  2198. local _start_epoch="${_LOKI_RUN_START_EPOCH:-}"
  2199. case "$_start_epoch" in (*[!0-9]*|'') _start_epoch="" ;; esac
  2200. if [ -n "$_start_epoch" ]; then
  2201. local _now _elapsed
  2202. _now="$(date +%s 2>/dev/null || true)"
  2203. case "$_now" in (*[!0-9]*|'') _now="" ;; esac
  2204. if [ -n "$_now" ] && [ "$_now" -ge "$_start_epoch" ] 2>/dev/null; then
  2205. _elapsed=$(( _now - _start_epoch ))
  2206. _fields="${_fields} | elapsed $(_hud_fmt_secs "$_elapsed")"
  2207. fi
  2208. fi
  2209. # ETA (PO lock #1): omit by default. Only a SMALL user-set LOKI_MAX_ITERATIONS
  2210. # (not the 1000 default) yields a meaningful target, so that is the single ETA
  2211. # trigger here. A LOKI_BUDGET_LIMIT cap is also an allowed trigger per the
  2212. # plan, but a budget-derived ETA needs cost-rate math that is easy to get
  2213. # wrong; per "keep it simple and safe, when unsure omit", budget ETA is left
  2214. # out rather than rendered approximately.
  2215. local _lmi="${LOKI_MAX_ITERATIONS:-}"
  2216. case "$_lmi" in (*[!0-9]*|'') _lmi="" ;; esac
  2217. if [ -n "$_lmi" ] && [ "$_lmi" -gt 0 ] 2>/dev/null && [ "$_lmi" -lt 1000 ] 2>/dev/null \
  2218. && [ "$_iter" -gt 0 ] 2>/dev/null && [ "$_iter" -lt "$_lmi" ] 2>/dev/null \
  2219. && [ -n "$_start_epoch" ]; then
  2220. local _now2 _el2 _per _remain_iters _eta
  2221. _now2="$(date +%s 2>/dev/null || true)"
  2222. case "$_now2" in (*[!0-9]*|'') _now2="" ;; esac
  2223. if [ -n "$_now2" ] && [ "$_now2" -ge "$_start_epoch" ] 2>/dev/null; then
  2224. _el2=$(( _now2 - _start_epoch ))
  2225. _per=$(( _el2 / _iter ))
  2226. _remain_iters=$(( _lmi - _iter ))
  2227. _eta=$(( _per * _remain_iters ))
  2228. [ "$_eta" -gt 0 ] 2>/dev/null && _fields="${_fields} | eta ~$(_hud_fmt_secs "$_eta")"
  2229. fi
  2230. fi
  2231. echo -e "${CYAN}[HUD]${NC} ${_fields}"
  2232. return 0
  2233. }
  2234. # Format a whole-seconds integer as a compact duration: 37s, 2m11s, 1h03m.
  2235. # Best-effort and set -u safe; any odd input degrades to "0s".
  2236. _hud_fmt_secs() {
  2237. local _s="${1:-0}"
  2238. case "$_s" in (*[!0-9]*|'') _s=0 ;; esac
  2239. if [ "$_s" -lt 60 ] 2>/dev/null; then
  2240. printf '%ds' "$_s"
  2241. elif [ "$_s" -lt 3600 ] 2>/dev/null; then
  2242. printf '%dm%02ds' "$(( _s / 60 ))" "$(( _s % 60 ))"
  2243. else
  2244. printf '%dh%02dm' "$(( _s / 3600 ))" "$(( (_s % 3600) / 60 ))"
  2245. fi
  2246. return 0
  2247. }
  2248. #===============================================================================
  2249. # Process Registry (PID Supervisor)
  2250. # Central registry of all spawned child processes for reliable cleanup
  2251. #===============================================================================
  2252. PID_REGISTRY_DIR=""
  2253. # Initialize the PID registry directory
  2254. init_pid_registry() {
  2255. PID_REGISTRY_DIR="${TARGET_DIR:-.}/.loki/pids"
  2256. mkdir -p "$PID_REGISTRY_DIR"
  2257. }
  2258. # Parse a field from a JSON registry entry (python3 with shell fallback)
  2259. # Usage: _parse_json_field <file> <field>
  2260. _parse_json_field() {
  2261. local file="$1" field="$2"
  2262. if command -v python3 >/dev/null 2>&1; then
  2263. python3 -c "import json,sys; print(json.load(open(sys.argv[1])).get(sys.argv[2],''))" "$file" "$field" 2>/dev/null
  2264. else
  2265. # Shell fallback (no python3): extract value for simple flat JSON. Handles
  2266. # BOTH quoted string values ("kind":"wrapper") and bare numeric values
  2267. # ("ppid":206). The prior impl stripped the leading quote's content to empty
  2268. # for string fields -- broke "kind" parsing on python3-less hosts, which
  2269. # would drop wrapper entries into the legacy child path (loki-mode #92).
  2270. sed 's/.*"'"$field"'":[[:space:]]*//' "$file" 2>/dev/null \
  2271. | sed 's/^"//' \
  2272. | sed 's/[",}].*//' | head -1
  2273. fi
  2274. }
  2275. # Register a spawned process in the central registry
  2276. # Usage: register_pid <pid> <label> [<extra_info>]
  2277. # Example: register_pid $! "dashboard" "port=57374"
  2278. register_pid() {
  2279. local pid="$1"
  2280. # Sanitize label and extra for JSON safety (escape backslash first, then double-quote, strip newlines)
  2281. local label="${2//\\/\\\\}"
  2282. label="${label//\"/\\\"}"
  2283. label="$(printf '%s' "$label" | tr -d '\n\r')"
  2284. local extra="${3:-}"
  2285. extra="${extra//\\/\\\\}"
  2286. extra="${extra//\"/\\\"}"
  2287. extra="$(printf '%s' "$extra" | tr -d '\n\r')"
  2288. [ -z "$PID_REGISTRY_DIR" ] && init_pid_registry
  2289. local entry_file="$PID_REGISTRY_DIR/${pid}.json"
  2290. cat > "$entry_file" << EOF
  2291. {"pid":$pid,"label":"$label","started":"$(date -u +%Y-%m-%dT%H:%M:%SZ)","ppid":$$,"extra":"$extra"}
  2292. EOF
  2293. }
  2294. # Unregister a process from the registry (called on clean shutdown)
  2295. # Usage: unregister_pid <pid>
  2296. unregister_pid() {
  2297. local pid="$1"
  2298. [ -z "$PID_REGISTRY_DIR" ] && init_pid_registry
  2299. rm -f "$PID_REGISTRY_DIR/${pid}.json" 2>/dev/null
  2300. }
  2301. # Kill a registered process with SIGTERM -> wait -> SIGKILL escalation
  2302. # Usage: kill_registered_pid <pid>
  2303. kill_registered_pid() {
  2304. local pid="$1"
  2305. if kill -0 "$pid" 2>/dev/null; then
  2306. kill "$pid" 2>/dev/null || true
  2307. # Wait up to 2 seconds for graceful exit
  2308. local waited=0
  2309. while [ $waited -lt 4 ] && kill -0 "$pid" 2>/dev/null; do
  2310. sleep 0.5
  2311. waited=$((waited + 1))
  2312. done
  2313. # Escalate to SIGKILL if still alive
  2314. if kill -0 "$pid" 2>/dev/null; then
  2315. kill -9 "$pid" 2>/dev/null || true
  2316. fi
  2317. fi
  2318. unregister_pid "$pid"
  2319. }
  2320. # Register the CURRENTLY-RUNNING loki-run wrapper itself in the registry.
  2321. # The wrapper registers its children but historically never itself, so a wrapper
  2322. # whose launching session dies is never reaped (loki-mode #92). We record the
  2323. # LAUNCHER's mortal pid (NOT $$ -- $$ is the wrapper's own pid, which would make
  2324. # the parent-death check always succeed and the reaper INERT; NOT $PPID either --
  2325. # in the detached setsid/nohup path bash caches getppid()==1 at exec, and kill -0 1
  2326. # is always true -> also inert). The launcher exports LOKI_LAUNCHER_PID=$$ at each
  2327. # backgrounding site; that launcher shell exits immediately after backgrounding, so
  2328. # the recorded ppid genuinely dies and the parent-death precondition CAN fire. For
  2329. # the foreground path LOKI_LAUNCHER_PID is unset -> $PPID (the user's shell) is the
  2330. # correct mortal fallback. Reap-vs-spare is then decided by the LIVENESS predicate,
  2331. # not by parentage alone. Uses a "kind":"wrapper" tag so cleanup_orphan_pids routes
  2332. # only wrapper entries through the predicate and keeps child entries byte-identical.
  2333. register_self_wrapper() {
  2334. [ -z "$PID_REGISTRY_DIR" ] && init_pid_registry
  2335. local launcher_ppid="${LOKI_LAUNCHER_PID:-$PPID}"
  2336. case "$launcher_ppid" in ''|*[!0-9]*) launcher_ppid="$PPID" ;; esac
  2337. local entry_file="$PID_REGISTRY_DIR/$$.json"
  2338. cat > "$entry_file" << EOF
  2339. {"pid":$$,"label":"loki-wrapper","started":"$(date -u +%Y-%m-%dT%H:%M:%SZ)","ppid":$launcher_ppid,"kind":"wrapper","extra":""}
  2340. EOF
  2341. }
  2342. # Pure, side-effect-free reap predicate (loki-mode #92). ALL inputs are
  2343. # pre-computed scalars (0/1 booleans or integer ms); NO stat/kill/pgrep inside so
  2344. # it is hermetically unit-testable. Prints "1" (reap) or "0" (spare); returns 0.
  2345. # Reap ONLY when: parent dead AND no live engine child AND idle past the budget.
  2346. # Mirrors the SaaS BFF sweepStuckBuilds and the #91 worker "no-activity != dead"
  2347. # symmetry: a genuinely-detached run that keeps writing .loki activity is spared.
  2348. # parent_alive: 1 if recorded ppid is still alive, else 0
  2349. # last_activity_ms: epoch-ms of most recent .loki activity (0 if none found)
  2350. # now_ms: current epoch-ms
  2351. # idle_budget_ms: generous idle budget (default 900000 = 15 min)
  2352. # has_live_child: 1 if wrapper has a live ENGINE child (claude/node), else 0
  2353. shouldReapOrphan() {
  2354. local parent_alive="$1" last_activity_ms="$2" now_ms="$3" idle_budget_ms="$4" has_live_child="$5"
  2355. if [ "$parent_alive" = "0" ] && [ "$has_live_child" = "0" ] \
  2356. && [ $(( now_ms - last_activity_ms )) -ge "$idle_budget_ms" ]; then
  2357. echo 1
  2358. else
  2359. echo 0
  2360. fi
  2361. }
  2362. # Most-recent .loki activity as epoch-ms across events.jsonl, signals/*,
  2363. # autonomy-state.json. Cross-platform stat (macOS -f %m / Linux -c %Y). On a stat
  2364. # PARSE FAILURE we default to now_ms (SPARE), never 0 -- a wrong flag must not make a
  2365. # live run look maximally idle and get reaped. "No activity files exist" legitimately
  2366. # yields 0 (reap-eligible); that is distinct from a stat error.
  2367. _loki_last_activity_ms() {
  2368. local loki_dir="${TARGET_DIR:-.}/.loki"
  2369. local newest=0 f mt stat_ok=0
  2370. local candidates=()
  2371. [ -f "$loki_dir/events.jsonl" ] && candidates+=("$loki_dir/events.jsonl")
  2372. [ -f "$loki_dir/autonomy-state.json" ] && candidates+=("$loki_dir/autonomy-state.json")
  2373. if [ -d "$loki_dir/signals" ]; then
  2374. for f in "$loki_dir/signals"/*; do [ -e "$f" ] && candidates+=("$f"); done
  2375. fi
  2376. for f in "${candidates[@]:-}"; do
  2377. [ -e "$f" ] || continue
  2378. mt=$(stat -f %m "$f" 2>/dev/null) || mt=""
  2379. [ -z "$mt" ] && { mt=$(stat -c %Y "$f" 2>/dev/null) || mt=""; }
  2380. case "$mt" in ''|*[!0-9]*) continue ;; esac
  2381. stat_ok=1
  2382. [ "$mt" -gt "$newest" ] && newest="$mt"
  2383. done
  2384. # candidates present but every stat failed -> parse failure -> spare (now_ms)
  2385. if [ "${#candidates[@]}" -gt 0 ] && [ "$stat_ok" = "0" ]; then
  2386. echo "$(( $(date +%s) * 1000 ))"
  2387. return 0
  2388. fi
  2389. echo "$(( newest * 1000 ))"
  2390. }
  2391. # Count LIVE ENGINE children of a wrapper (claude/node), EXCLUDING sleep and the
  2392. # status/resource monitors. The observed 3h orphan had only sleep children, so a
  2393. # naive `pgrep -P` (any child) would falsely spare it; the comm filter is what makes
  2394. # the reaper actually fire on the real orphan shape. Prints 1 (has engine child) or 0.
  2395. _loki_has_live_engine_child() {
  2396. local wrapper_pid="$1" c cc
  2397. for c in $(pgrep -P "$wrapper_pid" 2>/dev/null); do
  2398. cc=$(ps -o comm= -p "$c" 2>/dev/null)
  2399. case "$cc" in *claude*|*node*) echo 1; return 0 ;; esac
  2400. done
  2401. echo 0
  2402. }
  2403. # Scan registry for orphaned processes and kill them
  2404. # Called on startup and by `loki cleanup`
  2405. # Returns: number of orphans killed
  2406. cleanup_orphan_pids() {
  2407. [ -z "$PID_REGISTRY_DIR" ] && init_pid_registry
  2408. local orphan_count=0
  2409. if [ ! -d "$PID_REGISTRY_DIR" ]; then
  2410. echo "0"
  2411. return 0
  2412. fi
  2413. for entry_file in "$PID_REGISTRY_DIR"/*.json; do
  2414. [ -f "$entry_file" ] || continue
  2415. local pid
  2416. pid=$(basename "$entry_file" .json)
  2417. # Skip non-numeric filenames
  2418. case "$pid" in
  2419. ''|*[!0-9]*) continue ;;
  2420. esac
  2421. # Never reap the currently-running wrapper itself (loki-mode #92 self-skip).
  2422. [ "$pid" = "$$" ] && continue
  2423. if kill -0 "$pid" 2>/dev/null; then
  2424. # Process is alive -- check if its parent session is dead
  2425. local ppid_val=""
  2426. ppid_val=$(_parse_json_field "$entry_file" "ppid") || true
  2427. # Validate ppid_val is numeric before using with kill
  2428. case "$ppid_val" in ''|*[!0-9]*) ppid_val="" ;; esac
  2429. # Route WRAPPER entries through the liveness predicate; CHILD entries
  2430. # (no "kind" field) keep the exact parent-death-only behavior (#92).
  2431. local kind=""
  2432. kind=$(_parse_json_field "$entry_file" "kind") || true
  2433. if [ "$kind" = "wrapper" ]; then
  2434. # Wrapper: reap only if orphaned AND idle-past-budget AND no live
  2435. # engine child. Compute the impure inputs here (predicate stays pure).
  2436. if [ -n "$ppid_val" ]; then
  2437. local parent_alive=0
  2438. kill -0 "$ppid_val" 2>/dev/null && parent_alive=1
  2439. local last_activity_ms now_ms has_live_child idle_budget_ms
  2440. last_activity_ms=$(_loki_last_activity_ms)
  2441. now_ms=$(( $(date +%s) * 1000 ))
  2442. has_live_child=$(_loki_has_live_engine_child "$pid")
  2443. idle_budget_ms="${LOKI_WRAPPER_IDLE_BUDGET_MS:-900000}"
  2444. if [ "$(shouldReapOrphan "$parent_alive" "$last_activity_ms" "$now_ms" "$idle_budget_ms" "$has_live_child")" = "1" ]; then
  2445. log_warn "Reaping idle orphaned loki wrapper PID=$pid (parent $ppid_val dead, idle >=$((idle_budget_ms/60000))m, no engine child)" >&2
  2446. kill_registered_pid "$pid"
  2447. orphan_count=$((orphan_count + 1))
  2448. fi
  2449. fi
  2450. elif [ -n "$ppid_val" ] && [ "$ppid_val" != "$$" ]; then
  2451. if ! kill -0 "$ppid_val" 2>/dev/null; then
  2452. # Parent is dead -- this is an orphan
  2453. local label=""
  2454. label=$(_parse_json_field "$entry_file" "label") || label="unknown"
  2455. log_warn "Killing orphaned process: PID=$pid label=$label (parent $ppid_val is dead)" >&2
  2456. kill_registered_pid "$pid"
  2457. orphan_count=$((orphan_count + 1))
  2458. fi
  2459. fi
  2460. else
  2461. # Process is dead -- clean up stale registry entry
  2462. rm -f "$entry_file" 2>/dev/null
  2463. fi
  2464. done
  2465. echo "$orphan_count"
  2466. }
  2467. # Kill ALL registered processes (used during full shutdown)
  2468. kill_all_registered() {
  2469. [ -z "$PID_REGISTRY_DIR" ] && init_pid_registry
  2470. if [ ! -d "$PID_REGISTRY_DIR" ]; then
  2471. return 0
  2472. fi
  2473. for entry_file in "$PID_REGISTRY_DIR"/*.json; do
  2474. [ -f "$entry_file" ] || continue
  2475. local pid
  2476. pid=$(basename "$entry_file" .json)
  2477. case "$pid" in
  2478. ''|*[!0-9]*) continue ;;
  2479. esac
  2480. # Never SIGKILL the running wrapper itself mid-shutdown (loki-mode #92):
  2481. # it now self-registers, so it would otherwise appear in this sweep.
  2482. [ "$pid" = "$$" ] && continue
  2483. kill_registered_pid "$pid"
  2484. done
  2485. }
  2486. #===============================================================================
  2487. # Event Emission (Dashboard Integration)
  2488. # Writes events to .loki/events.jsonl for dashboard consumption
  2489. #===============================================================================
  2490. emit_event() {
  2491. local event_type="$1"
  2492. shift
  2493. local event_data="$*"
  2494. local events_file=".loki/events.jsonl"
  2495. local timestamp
  2496. timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
  2497. mkdir -p .loki
  2498. # Build JSON event with proper escaping
  2499. local json_event
  2500. json_event=$(python3 -c "
  2501. import json, sys
  2502. event = {
  2503. 'timestamp': sys.argv[1],
  2504. 'type': sys.argv[2],
  2505. 'data': sys.argv[3]
  2506. }
  2507. print(json.dumps(event))
  2508. " "$timestamp" "$event_type" "$event_data" 2>/dev/null)
  2509. # Fallback to simple JSON if python fails
  2510. if [ -z "$json_event" ]; then
  2511. # Escape quotes and special chars for JSON
  2512. local escaped_data
  2513. escaped_data=$(printf '%s' "$event_data" | sed 's/\\/\\\\/g; s/"/\\"/g; s/ /\\t/g' | tr -d '\n')
  2514. json_event="{\"timestamp\":\"$timestamp\",\"type\":\"$event_type\",\"data\":\"$escaped_data\"}"
  2515. fi
  2516. echo "$json_event" >> "$events_file"
  2517. # Also log for debugging
  2518. log_debug "Event: $event_type - $event_data"
  2519. }
  2520. # Emit structured event with key-value pairs
  2521. # Record that the operator's explicitly-pinned model was NOT the model dispatched.
  2522. #
  2523. # WHY THIS EXISTS: a tier resolution that quietly returns something other than the
  2524. # pin is indistinguishable, in the receipt, from a run that got what it asked for.
  2525. # The substitution itself is often legitimate; the SILENCE is the defect. This
  2526. # emits both an event (machine-readable) and an audit line (the hash-chained
  2527. # chain), so the swap and its stated reason can be audited and, if wrong,
  2528. # refuted by whoever has the evidence to refute it.
  2529. #
  2530. # Never changes the dispatched model and never fails the caller: an observability
  2531. # record that can break a run would be worse than the silence it replaces.
  2532. emit_model_substituted() {
  2533. local pinned="$1" dispatched="$2" reason="$3" site="$4"
  2534. # Nothing to report when the pin was honored.
  2535. [ "$pinned" = "$dispatched" ] && return 0
  2536. emit_event_json "model_substituted" \
  2537. "pinned=$pinned" \
  2538. "dispatched=$dispatched" \
  2539. "reason=$reason" \
  2540. "site=$site" 2>/dev/null || true
  2541. if type audit_agent_action >/dev/null 2>&1; then
  2542. audit_agent_action "model_substituted" \
  2543. "Pinned model '$pinned' dispatched as '$dispatched' ($reason)" \
  2544. "site=$site" 2>/dev/null || true
  2545. fi
  2546. log_warn "Model pin '$pinned' dispatched as '$dispatched' ($reason)" 2>/dev/null || true
  2547. return 0
  2548. }
  2549. emit_event_json() {
  2550. local event_type="$1"
  2551. shift
  2552. local events_file=".loki/events.jsonl"
  2553. local timestamp
  2554. timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
  2555. mkdir -p .loki
  2556. # Build JSON from remaining args as key=value pairs
  2557. local json_data="{"
  2558. local first=true
  2559. while [ $# -gt 0 ]; do
  2560. local key="${1%%=*}"
  2561. local value="${1#*=}"
  2562. if [ "$first" = true ]; then
  2563. first=false
  2564. else
  2565. json_data+=","
  2566. fi
  2567. # Quote string values, leave numbers/booleans/floats as-is
  2568. # BUG-NEW-004: Also match floats (e.g., cost=3.14) not just integers
  2569. if [[ "$value" =~ ^[0-9]+\.?[0-9]*$ ]] || [[ "$value" =~ ^(true|false|null)$ ]]; then
  2570. json_data+="\"$key\":$value"
  2571. else
  2572. # Escape backslashes, quotes, and special chars in value
  2573. value=$(printf '%s' "$value" | sed 's/\\/\\\\/g; s/"/\\"/g; s/ /\\t/g')
  2574. json_data+="\"$key\":\"$value\""
  2575. fi
  2576. shift
  2577. done
  2578. json_data+="}"
  2579. local json_event="{\"timestamp\":\"$timestamp\",\"type\":\"$event_type\",\"data\":$json_data}"
  2580. echo "$json_event" >> "$events_file"
  2581. log_debug "Event: $event_type - $json_data"
  2582. }
  2583. # Per-stage timeline event (v7.91.x). Emits one stage_complete record per
  2584. # quality-gate / build stage so the SaaS timeline can show where build time
  2585. # goes within an iteration (the provider call itself is already bracketed by
  2586. # iteration_start/iteration_complete). This is purely ADDITIVE: it appends one
  2587. # event line via emit_event_json and never changes a gate verdict, gate exit
  2588. # code, or control flow. Duration is computed by the caller (whole-second
  2589. # resolution, sufficient for multi-second gates) and carried on the event so
  2590. # the SaaS does not have to infer it from coarse ISO timestamps.
  2591. # emit_stage_complete <stage_name> <status: pass|fail|not_run> <start_epoch_seconds>
  2592. # Event shape:
  2593. # {type:"stage_complete", timestamp, data:{stage, status, duration_s, iteration}}
  2594. # Best-effort: any failure is swallowed so it can never block the build.
  2595. emit_stage_complete() {
  2596. local stage="$1"
  2597. local status="$2"
  2598. local t0="$3"
  2599. local now dur
  2600. now=$(date +%s 2>/dev/null) || return 0
  2601. [ -n "$t0" ] || return 0
  2602. dur=$(( now - t0 ))
  2603. [ "$dur" -ge 0 ] 2>/dev/null || dur=0
  2604. emit_event_json "stage_complete" \
  2605. "stage=$stage" \
  2606. "status=$status" \
  2607. "duration_s=$dur" \
  2608. "iteration=${ITERATION_COUNT:-0}" 2>/dev/null || true
  2609. }
  2610. # Trust-layer metrics event writer (benchmark program section 3). Appends one
  2611. # durable record per trust event to .loki/metrics/trust-events.jsonl via the
  2612. # Python writer (single source of truth for the JSONL schema). This is ADDITIVE
  2613. # and purely a side effect: it writes nothing to stdout, ignores all errors, and
  2614. # never alters control flow or any caller's return value. The single-state
  2615. # control files (evidence-block.json, gate-failure-count.json) are untouched;
  2616. # this log exists because those files are erased on the successful-run path,
  2617. # losing exactly the self-correction events the trust metrics publish.
  2618. # Resolve a stable, UNIQUE-PER-RUN id for the trust event log. The cross-run
  2619. # denominators (block rate, gate distribution) require ids that are distinct per
  2620. # run. A persisted per-run file is the source of truth, NOT LOKI_SESSION_ID:
  2621. # - On `loki start ./prd.md`, LOKI_SESSION_ID is unset entirely.
  2622. # - On `loki run <issue>`, LOKI_SESSION_ID is the issue NUMBER, which is stable
  2623. # across re-runs by design (so `loki stop <n>` works); using it would merge
  2624. # every re-run of the same issue into one bucket and skew the rates.
  2625. # So a fresh run always MINTS a new unique id into .loki/state/trust-run-id, and
  2626. # every later event in that run reads it back. LOKI_SESSION_ID is only a
  2627. # last-resort fallback when no minted file exists (e.g. an event fired before
  2628. # any run_start, which the aggregator then treats as un-instrumented anyway).
  2629. # Events never join to proof.json (Metrics 1-3 are events-only, Metric 4 is
  2630. # proofs-only), so intra-log uniqueness is the only requirement.
  2631. # Usage: _loki_trust_run_id [--new]
  2632. _loki_trust_run_id() {
  2633. local loki_dir="${LOKI_DIR:-${TARGET_DIR:-.}/.loki}"
  2634. local id_file="$loki_dir/state/trust-run-id"
  2635. if [ "${1:-}" = "--new" ]; then
  2636. # Fresh run: mint a new unique id (epoch + pid + short random) and
  2637. # persist it as the source of truth for this run's events.
  2638. local new_id
  2639. new_id="run-$(date -u +%Y%m%d%H%M%S)-$$-${RANDOM:-0}"
  2640. mkdir -p "$loki_dir/state" 2>/dev/null || true
  2641. printf '%s' "$new_id" > "$id_file" 2>/dev/null || true
  2642. printf '%s' "$new_id"
  2643. return 0
  2644. fi
  2645. # Read path: the minted per-run file wins over LOKI_SESSION_ID so a resume
  2646. # in a separate process (no exported LOKI_TRUST_RUN_ID) still resolves to
  2647. # the same run, and a stable issue-number session id never collapses re-runs.
  2648. if [ -s "$id_file" ]; then
  2649. cat "$id_file" 2>/dev/null || true
  2650. return 0
  2651. fi
  2652. if [ -n "${LOKI_SESSION_ID:-}" ]; then
  2653. printf '%s' "$LOKI_SESSION_ID"
  2654. return 0
  2655. fi
  2656. # No persisted id and no session id: empty -> writer records "unknown".
  2657. printf '%s' ""
  2658. }
  2659. # _advance_current_phase: atomically advance currentPhase in orchestrator.json.
  2660. # This is the single source of truth for the build dashboard and the BFF
  2661. # reconciliation gate (isTerminalPhase). The engine initialises it to "BOOTSTRAP"
  2662. # at session start; call this to advance through the SDLC lifecycle phases.
  2663. # Args: $1 = the new phase value (REASONING, BUILDING, VERIFYING, COMPLETED, etc.)
  2664. _advance_current_phase() {
  2665. local new_phase="${1:?phase required}"
  2666. local loki_dir="${LOKI_DIR:-${TARGET_DIR:-.}/.loki}"
  2667. local orch="$loki_dir/state/orchestrator.json"
  2668. [ -f "$orch" ] || return 0
  2669. # Values are passed via argv (not interpolated into the source) so a phase or
  2670. # path containing quotes can never break or inject into the python.
  2671. python3 -E -c "import sys; sys.path[:] = [p for p in sys.path if p not in ('', '.')]
  2672. import json, sys
  2673. f, phase = sys.argv[1], sys.argv[2]
  2674. try:
  2675. d = json.load(open(f))
  2676. d['currentPhase'] = phase
  2677. json.dump(d, open(f, 'w'))
  2678. except (json.JSONDecodeError, OSError):
  2679. pass
  2680. " "$orch" "$new_phase" 2>/dev/null || true
  2681. }
  2682. # Usage: record_trust_event_bash <event_type> [key=value ...]
  2683. # Pass LOKI_TRUST_RUN_ID in the environment to override the resolved id (the
  2684. # run_start site sets it to the freshly minted id so the first event matches).
  2685. record_trust_event_bash() {
  2686. local event_type="$1"
  2687. shift || true
  2688. local tm_mod="$SCRIPT_DIR/lib/trust_metrics.py"
  2689. [ -f "$tm_mod" ] || return 0
  2690. command -v python3 >/dev/null 2>&1 || return 0
  2691. local loki_dir="${LOKI_DIR:-${TARGET_DIR:-.}/.loki}"
  2692. local run_id="${LOKI_TRUST_RUN_ID:-$(_loki_trust_run_id)}"
  2693. # Pass kv pairs as argv so Python parses (no shell JSON building). All
  2694. # values stay strings except where the reader coerces (iteration -> int).
  2695. _TM_LOKI_DIR="$loki_dir" \
  2696. _TM_MOD_PATH="$tm_mod" \
  2697. _TM_EVENT_TYPE="$event_type" \
  2698. _TM_RUN_ID="$run_id" \
  2699. _TM_ITERATION="${ITERATION_COUNT:-0}" \
  2700. python3 - "$@" <<'TRUST_EVENT_PY' >/dev/null 2>&1 || true
  2701. import os, sys, importlib.util
  2702. spec = importlib.util.spec_from_file_location("trust_metrics", os.environ["_TM_MOD_PATH"])
  2703. tm = importlib.util.module_from_spec(spec)
  2704. spec.loader.exec_module(tm)
  2705. fields = {}
  2706. for arg in sys.argv[1:]:
  2707. if "=" in arg:
  2708. k, v = arg.split("=", 1)
  2709. fields[k] = v
  2710. tm.record_trust_event(
  2711. os.environ["_TM_LOKI_DIR"],
  2712. os.environ["_TM_EVENT_TYPE"],
  2713. run_id=os.environ.get("_TM_RUN_ID", "") or None,
  2714. iteration=os.environ.get("_TM_ITERATION", "0"),
  2715. **fields,
  2716. )
  2717. TRUST_EVENT_PY
  2718. }
  2719. # v7.0.2: Bash helper to emit a managed-agents event to the dashboard's
  2720. # managed event log (.loki/managed/events.ndjson). Mirrors the Python
  2721. # emit_managed_event helper so bash callers can land events in the same
  2722. # stream the dashboard reads. Schema: {ts, type, payload}.
  2723. emit_managed_event_bash() {
  2724. local event_type="$1"
  2725. shift
  2726. local target_dir="${TARGET_DIR:-.}"
  2727. local events_file="$target_dir/.loki/managed/events.ndjson"
  2728. mkdir -p "$target_dir/.loki/managed"
  2729. local timestamp
  2730. timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
  2731. # Build payload JSON from key=value args (same convention as emit_event_json)
  2732. local payload="{"
  2733. local first=true
  2734. while [ $# -gt 0 ]; do
  2735. local key="${1%%=*}"
  2736. local value="${1#*=}"
  2737. if [ "$first" = true ]; then first=false; else payload+=","; fi
  2738. if [[ "$value" =~ ^[0-9]+\.?[0-9]*$ ]] || [[ "$value" =~ ^(true|false|null)$ ]]; then
  2739. payload+="\"$key\":$value"
  2740. else
  2741. value=$(printf '%s' "$value" | sed 's/\\/\\\\/g; s/"/\\"/g; s/ /\\t/g')
  2742. payload+="\"$key\":\"$value\""
  2743. fi
  2744. shift
  2745. done
  2746. payload+="}"
  2747. local json_event="{\"ts\":\"$timestamp\",\"type\":\"$event_type\",\"payload\":$payload}"
  2748. echo "$json_event" >> "$events_file"
  2749. }
  2750. # Emit event to .loki/events/pending/ directory (for event bus subscribers)
  2751. # Used by OTEL bridge and other enterprise services that watch the pending dir.
  2752. # Usage: emit_event_pending <type> [key=value ...]
  2753. emit_event_pending() {
  2754. local event_type="$1"
  2755. shift
  2756. local events_dir=".loki/events/pending"
  2757. mkdir -p "$events_dir"
  2758. local timestamp
  2759. timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
  2760. local event_id
  2761. event_id=$(head -c 4 /dev/urandom | od -An -tx1 | tr -d ' \n')
  2762. # Build payload JSON from key=value args
  2763. local payload="{"
  2764. local first=true
  2765. while [ $# -gt 0 ]; do
  2766. local key="${1%%=*}"
  2767. local value="${1#*=}"
  2768. if [ "$first" = true ]; then
  2769. first=false
  2770. else
  2771. payload+=","
  2772. fi
  2773. value=$(printf '%s' "$value" | sed 's/\\/\\\\/g; s/"/\\"/g; s/ /\\t/g')
  2774. payload+="\"$key\":\"$value\""
  2775. shift
  2776. done
  2777. payload+="}"
  2778. local event_file="$events_dir/${timestamp//:/-}_${event_id}.json"
  2779. printf '{"id":"%s","type":"%s","timestamp":"%s","payload":%s,"version":"1.0"}\n' \
  2780. "$event_id" "$event_type" "$timestamp" "$payload" > "${event_file}.tmp" && mv "${event_file}.tmp" "$event_file"
  2781. }
  2782. #===============================================================================
  2783. # Enterprise Process Manager
  2784. # Manages background services: OTEL bridge, audit subscriber, integration sync
  2785. #===============================================================================
  2786. # Start enterprise background services
  2787. start_enterprise_services() {
  2788. loki_background_services_enabled || return 0
  2789. log_info "Starting enterprise services..."
  2790. # OTEL Bridge (requires LOKI_OTEL_ENDPOINT and node)
  2791. if [ -n "${LOKI_OTEL_ENDPOINT:-}" ] && command -v node >/dev/null 2>&1; then
  2792. LOKI_TRACE_ID=$(node -e "console.log(require('crypto').randomBytes(16).toString('hex'))")
  2793. export LOKI_TRACE_ID
  2794. local bridge_script="${SCRIPT_DIR}/../src/observability/otel-bridge.js"
  2795. if [ -f "$bridge_script" ]; then
  2796. LOKI_OTEL_ENDPOINT="$LOKI_OTEL_ENDPOINT" \
  2797. LOKI_TRACE_ID="$LOKI_TRACE_ID" \
  2798. LOKI_DIR=".loki" \
  2799. node "$bridge_script" &
  2800. ENTERPRISE_PIDS+=($!)
  2801. log_info "Started OTEL bridge (PID: ${ENTERPRISE_PIDS[-1]})"
  2802. else
  2803. log_warn "OTEL bridge script not found: $bridge_script"
  2804. fi
  2805. fi
  2806. # Audit subscriber (P0.5-3)
  2807. if [ "${LOKI_AUDIT_ENABLED:-false}" = "true" ]; then
  2808. if command -v node >/dev/null 2>&1; then
  2809. node "${SCRIPT_DIR}/../src/audit/subscriber.js" &
  2810. ENTERPRISE_PIDS+=($!)
  2811. log_info "Started audit subscriber (PID: ${ENTERPRISE_PIDS[-1]})"
  2812. fi
  2813. fi
  2814. }
  2815. # Stop all enterprise background services
  2816. stop_enterprise_services() {
  2817. if [ ${#ENTERPRISE_PIDS[@]} -eq 0 ]; then
  2818. return
  2819. fi
  2820. log_info "Stopping enterprise services (${#ENTERPRISE_PIDS[@]} processes)..."
  2821. for pid in "${ENTERPRISE_PIDS[@]}"; do
  2822. if kill -0 "$pid" 2>/dev/null; then
  2823. kill -TERM "$pid" 2>/dev/null || true
  2824. # Wait briefly for graceful shutdown
  2825. local wait_count=0
  2826. while kill -0 "$pid" 2>/dev/null && [ $wait_count -lt 10 ]; do
  2827. sleep 0.1
  2828. ((wait_count++))
  2829. done
  2830. # Force kill if still running
  2831. if kill -0 "$pid" 2>/dev/null; then
  2832. kill -9 "$pid" 2>/dev/null || true
  2833. log_warn "Force-killed enterprise service PID $pid"
  2834. else
  2835. log_info "Enterprise service PID $pid stopped gracefully"
  2836. fi
  2837. fi
  2838. done
  2839. ENTERPRISE_PIDS=()
  2840. }
  2841. # Policy engine check wrapper (P0.5-2)
  2842. # Evaluates policies via Node.js CLI and returns appropriate exit codes.
  2843. # Exit 0 = ALLOW, Exit 1 = DENY, Exit 2 = REQUIRE_APPROVAL (logged but allowed for now)
  2844. check_policy() {
  2845. local enforcement_point="$1"
  2846. # Default to a valid empty-object JSON. Do NOT inline `${2:-{}}`: the
  2847. # closing brace of the parameter expansion eats the first `}` of the
  2848. # `{}` default, so a non-empty $2 like {"a":1} would pass through as
  2849. # {"a":1}} (invalid JSON -> check.js JSON.parse fails -> exit 1 DENY
  2850. # every iteration). Split the default assignment to avoid the footgun.
  2851. local context_json="${2:-}"
  2852. [ -z "$context_json" ] && context_json='{}'
  2853. # Only check if a policy file exists. This gate MUST agree with what
  2854. # src/policies/engine.js:_init actually loads, or the two disagree silently:
  2855. # engine.js falls back to the global ~/.loki policy (which is the only place
  2856. # the dashboard writes, api_v2.py:75,105), and a project-local-only test
  2857. # here would short-circuit before that fallback was ever consulted.
  2858. local _pol_global_dir="${LOKI_DATA_DIR:-$HOME/.loki}"
  2859. if [ ! -f ".loki/policies.json" ] && [ ! -f ".loki/policies.yaml" ] \
  2860. && [ ! -f "$_pol_global_dir/policies.json" ] \
  2861. && [ ! -f "$_pol_global_dir/policies.yaml" ]; then
  2862. return 0
  2863. fi
  2864. # Requires Node.js. Reaching here means a policy file EXISTS, so the
  2865. # operator has expressed intent to enforce. Returning 0 for a missing
  2866. # runtime silently disables that enforcement and reports nothing: the run
  2867. # proceeds exactly as if every action were permitted. That is the worst
  2868. # shape a security control can take, because the operator believes it is on.
  2869. #
  2870. # Fail CLOSED instead, matching what check.js already does for a corrupt
  2871. # policy file (tests/test-policy-failclosed.sh). LOKI_POLICY_REQUIRE_NODE=0
  2872. # restores the old fail-open for a host that genuinely cannot install node
  2873. # and accepts running unenforced.
  2874. if ! command -v node >/dev/null 2>&1; then
  2875. if [ "${LOKI_POLICY_REQUIRE_NODE:-1}" = "0" ]; then
  2876. log_warn "Policy file present but node is unavailable; enforcement SKIPPED (LOKI_POLICY_REQUIRE_NODE=0)"
  2877. return 0
  2878. fi
  2879. log_error "Policy file present but node is unavailable: cannot evaluate policy, refusing the action (fail-closed). Set LOKI_POLICY_REQUIRE_NODE=0 to run unenforced."
  2880. # Recorded like any other block. A refusal that leaves no trace is
  2881. # indistinguishable from a run that was never gated, and the receipt
  2882. # must be able to say WHY the action did not happen. The reason is
  2883. # distinct from a DENY: the policy was never evaluated.
  2884. audit_agent_action "policy_unevaluable" "Policy present but node unavailable" "enforcement=$enforcement_point"
  2885. emit_event_json "policy_unevaluable" \
  2886. "enforcement=$enforcement_point" \
  2887. "reason=node_unavailable"
  2888. return 1
  2889. fi
  2890. local result
  2891. result=$(LOKI_PROJECT_DIR="$(pwd)" node "${SCRIPT_DIR}/../src/policies/check.js" "$enforcement_point" "$context_json" 2>/dev/null)
  2892. local exit_code=$?
  2893. if [ $exit_code -eq 1 ]; then
  2894. log_error "Policy DENIED: $result"
  2895. audit_agent_action "policy_denied" "Policy denied execution" "enforcement=$enforcement_point"
  2896. emit_event_json "policy_denied" \
  2897. "enforcement=$enforcement_point" \
  2898. "result=$result"
  2899. return 1
  2900. elif [ $exit_code -eq 2 ]; then
  2901. log_warn "Policy requires APPROVAL: $result"
  2902. audit_agent_action "policy_approval_required" "Policy requires approval" "enforcement=$enforcement_point"
  2903. # P3-3 (v7.51.0): honor the approval requirement when the operator has
  2904. # opted into enforcement. This is OPT-IN and changes NOTHING for existing
  2905. # users: the wait fires only when staged autonomy is on
  2906. # (LOKI_STAGED_AUTONOMY=true) or the explicit
  2907. # LOKI_POLICY_APPROVAL_ENFORCE=1 knob is set. Otherwise it stays advisory
  2908. # (log + proceed), preserving the historical default behavior. The wait
  2909. # reuses the same .loki/signals/ file-signal mechanism as staged-autonomy
  2910. # plan approval (check_staged_autonomy), extended with a reject arm so an
  2911. # operator can deny (deny == policy DENIED == return 1).
  2912. if [ "$STAGED_AUTONOMY" = "true" ] || [ "${LOKI_POLICY_APPROVAL_ENFORCE:-0}" = "1" ]; then
  2913. local _approve_sig=".loki/signals/POLICY_APPROVED"
  2914. local _reject_sig=".loki/signals/POLICY_REJECTED"
  2915. log_warn "Policy enforcement: waiting for approval at enforcement point '$enforcement_point'."
  2916. log_warn " Approve: create $_approve_sig | Reject: create $_reject_sig"
  2917. audit_agent_action "policy_approval_wait" "Waiting for policy approval signal" "enforcement=$enforcement_point"
  2918. while [ ! -f "$_approve_sig" ] && [ ! -f "$_reject_sig" ]; do
  2919. sleep 5
  2920. done
  2921. if [ -f "$_reject_sig" ]; then
  2922. rm -f "$_reject_sig" "$_approve_sig" 2>/dev/null || true
  2923. log_error "Policy REJECTED by operator at enforcement point '$enforcement_point'"
  2924. audit_agent_action "policy_approval_rejected" "Operator rejected policy approval" "enforcement=$enforcement_point"
  2925. emit_event_json "policy_denied" \
  2926. "enforcement=$enforcement_point" \
  2927. "result=operator_rejected"
  2928. return 1
  2929. fi
  2930. rm -f "$_approve_sig" 2>/dev/null || true
  2931. log_info "Policy approved by operator at enforcement point '$enforcement_point'; continuing."
  2932. audit_agent_action "policy_approval_granted" "Operator approved policy" "enforcement=$enforcement_point"
  2933. return 0
  2934. fi
  2935. # Default (no staged autonomy, no enforce knob): advisory only -- log and
  2936. # proceed. This preserves the historical behavior for existing users.
  2937. return 0
  2938. fi
  2939. return 0
  2940. }
  2941. #===============================================================================
  2942. # Learning Signal Emission (SYN-018)
  2943. # Emits learning signals for cross-tool learning system
  2944. #===============================================================================
  2945. # Path to learning signal emitter
  2946. LEARNING_EMIT_SH="$SCRIPT_DIR/../learning/emit.sh"
  2947. # Emit learning signal (non-blocking)
  2948. # Usage: emit_learning_signal <signal_type> [options]
  2949. emit_learning_signal() {
  2950. if [ -f "$LEARNING_EMIT_SH" ]; then
  2951. # Run in background to be non-blocking
  2952. (LOKI_DIR=".loki" LOKI_SKILL_DIR="$PROJECT_DIR" "$LEARNING_EMIT_SH" "$@" >/dev/null 2>&1 &)
  2953. fi
  2954. }
  2955. # Track iteration timing for efficiency signals
  2956. ITERATION_START_MS=""
  2957. # Get current time in milliseconds (portable: works on macOS BSD date and GNU date)
  2958. _now_ms() {
  2959. local ms
  2960. ms=$(date +%s%3N 2>/dev/null)
  2961. # macOS BSD date doesn't support %N -- outputs literal "N" or "%3N"
  2962. # Detect non-numeric output and fall back to seconds * 1000
  2963. case "$ms" in
  2964. *[!0-9]*) echo $(( $(date +%s) * 1000 )) ;;
  2965. *) echo "$ms" ;;
  2966. esac
  2967. }
  2968. record_iteration_start() {
  2969. ITERATION_START_MS=$(_now_ms)
  2970. }
  2971. # Get iteration duration in milliseconds
  2972. get_iteration_duration_ms() {
  2973. if [ -n "$ITERATION_START_MS" ]; then
  2974. local end_ms
  2975. end_ms=$(_now_ms)
  2976. echo $((end_ms - ITERATION_START_MS))
  2977. else
  2978. echo "0"
  2979. fi
  2980. }
  2981. #===============================================================================
  2982. # API Key Validation
  2983. # Validates required API key is set for the selected provider.
  2984. # Supports Docker/K8s secret file mounts as fallback.
  2985. #===============================================================================
  2986. # Zero-friction preflight helpers (T1.1). These run for ALL environments
  2987. # (not just Docker/K8s) BEFORE the build starts, via validate_api_keys. git is a
  2988. # genuine hard requirement (the build inits a repo) so a missing git BLOCKS with a
  2989. # copy-pasteable fix; node-version and network reachability are ADVISORY (warn and
  2990. # continue, fail-open) so a probabilistic or optional signal never blocks a working
  2991. # user. The goal: surface real problems early without ever wrongly refusing to start.
  2992. #
  2993. # _loki_check_node_version: ADVISORY only. If node is present and its major
  2994. # version is < 18, log a warning (node only matters for node-based builds) and
  2995. # continue. If node is absent entirely this is a NO-OP. Always returns 0 -- it
  2996. # never blocks the build (fail-open); the real node call, if any, is the test.
  2997. _loki_check_node_version() {
  2998. command -v node >/dev/null 2>&1 || return 0
  2999. local node_version major
  3000. node_version="$(node --version 2>/dev/null || echo '')"
  3001. # node --version -> "v20.11.0"; extract the leading major integer.
  3002. major="$(printf '%s' "$node_version" | sed -E 's/^v?([0-9]+).*/\1/')"
  3003. # Advisory only: node is OPTIONAL (absence is a no-op above, and many builds -
  3004. # Python/Go/Rust - never touch node). A present-but-old node only matters for
  3005. # node-based builds, so WARN and continue (fail-open); never hard-block a
  3006. # working user. The actual node call (only for JS/TS work) is the real test.
  3007. if [ -n "$major" ] && [[ "$major" =~ ^[0-9]+$ ]] && [ "$major" -lt 18 ]; then
  3008. log_warn "Node.js >= 18 recommended for node-based builds; found ${node_version:-unknown}. Upgrade if your project uses node: https://nodejs.org"
  3009. fi
  3010. return 0
  3011. }
  3012. # _loki_check_git_present: the build initializes a git repo, so git is required.
  3013. _loki_check_git_present() {
  3014. if ! command -v git >/dev/null 2>&1; then
  3015. log_error "Git is required (the build initializes a repo). Install: https://git-scm.com/downloads"
  3016. return 1
  3017. fi
  3018. return 0
  3019. }
  3020. # _loki_claude_login_state: report the Claude Code login state WITHOUT a network
  3021. # call, printing exactly one of: loggedin | expired | loggedout | unknown.
  3022. #
  3023. # v7.104.2: the previous preflight assumed OAuth credentials always live in
  3024. # ~/.claude/.credentials.json. That is false for the native install (Claude Code
  3025. # 2.x on macOS), which stores the login in the macOS Keychain under the service
  3026. # "Claude Code-credentials" and creates NO .credentials.json. A genuinely logged
  3027. # in subscription user was therefore falsely told "installed but not logged in"
  3028. # and blocked from every build -- the exact opposite of the zero-friction intent.
  3029. #
  3030. # Design: prefer the durable, CLI-owned signal (`claude auth status`, local +
  3031. # fast + non-interactive, JSON with "loggedIn"), because the credential STORE
  3032. # location has now moved twice (file -> keychain) and will move again. Fall back
  3033. # to the file, then the keychain, for older CLIs that lack `auth status`. Crucial
  3034. # rule (inconclusive-never-false-fails applied to auth): only ever return a hard
  3035. # "loggedout" on POSITIVE evidence of no login; when we cannot tell, return
  3036. # "unknown" and let the caller fail OPEN (warn, proceed, let the real call
  3037. # decide) -- never hard-block a paying user on uncertainty.
  3038. _loki_claude_login_state() {
  3039. # 1) Primary: the CLI's own local auth-status (zero-network, ~0.2s). Newer
  3040. # claude CLIs print JSON with a boolean "loggedIn"; parse defensively.
  3041. if command -v claude >/dev/null 2>&1; then
  3042. local _status_json
  3043. _status_json="$(claude auth status 2>/dev/null)"
  3044. if [[ -n "$_status_json" ]]; then
  3045. local _parsed
  3046. _parsed="$(printf '%s' "$_status_json" | python3 -c "
  3047. import json, sys
  3048. try:
  3049. d = json.load(sys.stdin)
  3050. v = d.get('loggedIn')
  3051. # ONLY the two explicit booleans are trusted. Anything else -- a missing
  3052. # field, a renamed schema, a JSON error object -- is inconclusive and MUST
  3053. # fall through to the file/keychain checks, never a hard 'loggedout' (that
  3054. # would block a genuinely logged-in user with a valid creds file, violating
  3055. # fail-open). Mirrors the bun doctor's === true / === false / else shape.
  3056. if v is True:
  3057. print('loggedin')
  3058. elif v is False:
  3059. print('loggedout')
  3060. # else: print nothing -> fall through
  3061. except Exception:
  3062. pass # not JSON (older CLI / different output) -> stay silent, fall through
  3063. " 2>/dev/null)"
  3064. if [[ "$_parsed" == "loggedin" ]]; then
  3065. echo "loggedin"; return 0
  3066. elif [[ "$_parsed" == "loggedout" ]]; then
  3067. echo "loggedout"; return 0
  3068. fi
  3069. # auth status ran but was inconclusive (unparseable, or valid JSON
  3070. # without an explicit loggedIn boolean) -> do NOT trust it as a
  3071. # negative; fall through to the credential-store checks below.
  3072. fi
  3073. fi
  3074. # 2) Fallback: a VALID (non-expired) OAuth credentials file (older CLIs write
  3075. # it here). Compute the expiry once; a valid file is a positive login.
  3076. # IMPORTANT: we do NOT conclude "expired" here yet -- a stale expired file
  3077. # can sit next to a live macOS-Keychain login (the native install writes
  3078. # the file once, then rotates only the Keychain). Concluding "expired" off
  3079. # the file before consulting the Keychain would wrongly block a genuinely
  3080. # logged-in user -- the exact class of bug this whole change fixes. So the
  3081. # Keychain (step 3, a POSITIVE signal) is checked BEFORE we ever return
  3082. # "expired" (step 4). Unknown schema -> treat as valid (fail open).
  3083. local _creds="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/.credentials.json"
  3084. local _file_state="" # ""=absent, "valid"=present+fresh, "expired"=present+stale
  3085. if [[ -s "$_creds" ]]; then
  3086. _file_state="$(python3 -c "
  3087. import json, sys, time
  3088. try:
  3089. d = json.load(open(sys.argv[1]))
  3090. exp = d.get('claudeAiOauth', {}).get('expiresAt')
  3091. if isinstance(exp, (int, float)) and exp > 0 and (exp / 1000.0) <= (time.time() + 60):
  3092. print('expired')
  3093. else:
  3094. print('valid')
  3095. except Exception:
  3096. print('valid') # unreadable/unknown -> fail open (treat as valid)
  3097. " "$_creds" 2>/dev/null || echo valid)"
  3098. fi
  3099. if [[ "$_file_state" == "valid" ]]; then
  3100. echo "loggedin"; return 0
  3101. fi
  3102. # 3) macOS Keychain (native install stores the login here, often with NO file
  3103. # at all, or alongside a now-stale file). A present Keychain entry is a
  3104. # POSITIVE login signal and OVERRIDES an expired file. Existence check
  3105. # ONLY -- never `-w` (reading the secret can pop a GUI keychain prompt that
  3106. # would hang a headless/CI build). Guarded to Darwin.
  3107. if [[ "$(uname 2>/dev/null)" == "Darwin" ]] && command -v security >/dev/null 2>&1; then
  3108. if security find-generic-password -s "Claude Code-credentials" >/dev/null 2>&1; then
  3109. echo "loggedin"; return 0
  3110. fi
  3111. fi
  3112. # 4) Only now, with no valid file and no Keychain login, is an expired file a
  3113. # real "expired" state (genuine expiry that would 401 mid-build).
  3114. if [[ "$_file_state" == "expired" ]]; then
  3115. echo "expired"; return 0
  3116. fi
  3117. # 5) No positive login evidence at all. On macOS (where the file AND the
  3118. # Keychain are the two real stores, both absent) with the CLI present, that
  3119. # is confident "logged out". Anywhere else we cannot prove absence of a
  3120. # login (no Keychain to consult), so stay "unknown" and let the caller fail
  3121. # open.
  3122. if [[ "$(uname 2>/dev/null)" == "Darwin" ]] && command -v claude >/dev/null 2>&1; then
  3123. echo "loggedout"; return 0
  3124. fi
  3125. echo "unknown"; return 0
  3126. }
  3127. # _loki_check_workspace_writable: the build writes state, proofs, and source into
  3128. # the working directory and its .loki/ subtree on every iteration. A read-only
  3129. # volume, a permission-denied directory, or a full disk would otherwise fail
  3130. # silently deep inside the loop (every state write swallows errors with
  3131. # `2>/dev/null || true`), leaving the user with a confusing half-built run. Catch
  3132. # it once, up front, with one actionable message instead. HARD requirement.
  3133. _loki_check_workspace_writable() {
  3134. local dir="${TARGET_DIR:-$(pwd)}"
  3135. local probe="${dir}/.loki/.write-probe.$$"
  3136. # Ensure the .loki subtree can be created and written to.
  3137. if ! mkdir -p "${dir}/.loki" 2>/dev/null; then
  3138. if [ ! -w "$dir" ]; then
  3139. log_error "Cannot create ${dir}/.loki -- the working directory is not writable. Fix permissions (e.g. chmod u+w '${dir}') or run from a directory you own, then retry."
  3140. else
  3141. log_error "Cannot create ${dir}/.loki. If the disk is full, free space (df -h '${dir}'); if it is a read-only mount, run from a writable location, then retry."
  3142. fi
  3143. return 1
  3144. fi
  3145. # Confirm we can actually write a byte (catches full disk / read-only mount
  3146. # where the directory exists but writes fail).
  3147. if ! (: > "$probe") 2>/dev/null; then
  3148. rm -f "$probe" 2>/dev/null || true
  3149. log_error "Cannot write to ${dir}/.loki -- check free disk space (df -h '${dir}') and that the volume is not read-only, then retry."
  3150. return 1
  3151. fi
  3152. rm -f "$probe" 2>/dev/null || true
  3153. return 0
  3154. }
  3155. # _loki_check_network_reachable: ADVISORY only. A fast (3s) reachability probe to
  3156. # the active provider endpoint that WARNS and continues if it cannot reach it -- a
  3157. # curl failure does not prove the provider CLI cannot connect (3s timeout under
  3158. # load, transient DNS, or a proxy set for the CLI but not the shell all curl-fail
  3159. # while the real build succeeds). Always returns 0 (fail-open); the actual
  3160. # provider call is the authoritative connectivity test. Skipped entirely when curl
  3161. # is missing, when LOKI_SKIP_NET_PREFLIGHT=1, when ANTHROPIC_BASE_URL is set (alt
  3162. # provider endpoint we cannot assume), or for any provider whose endpoint we do
  3163. # not know. It NEVER blocks the build.
  3164. # Alt-provider guard: ANTHROPIC_BASE_URL routes Loki at a non-Anthropic endpoint
  3165. # (OpenRouter, Ollama, LiteLLM, vLLM, self-hosted). The tier aliases Loki resolves
  3166. # by default -- opus / sonnet / haiku -- are Anthropic-only names, and most
  3167. # alt-providers reject them outright, so the run dies at the FIRST model call with
  3168. # a provider-side error that is hard to trace back to the missing override.
  3169. #
  3170. # LOKI_MODEL_OVERRIDE is what makes that path work (providers/claude.sh:520 and
  3171. # loki-ts/src/runner/providers.ts:328 both apply it only when BOTH vars are set).
  3172. # `loki doctor` already warns on this, but doctor is opt-in and the run is not.
  3173. #
  3174. # NON-FATAL by design: a mapping proxy (LiteLLM can do this) legitimately resolves
  3175. # the aliases server-side, so this is a warning and never a block. Emitted once per
  3176. # run, and silent unless an alt endpoint is actually configured.
  3177. _loki_warn_alt_provider_model_alias() {
  3178. [ -n "${ANTHROPIC_BASE_URL:-}" ] || return 0
  3179. [ -z "${LOKI_MODEL_OVERRIDE:-}" ] || return 0
  3180. log_warn "ANTHROPIC_BASE_URL is set (${ANTHROPIC_BASE_URL}) but LOKI_MODEL_OVERRIDE is not."
  3181. log_warn " Loki will ask for the Anthropic tier aliases (opus/sonnet/haiku), which most"
  3182. log_warn " alt-providers do not serve. Set the exact model id your endpoint expects:"
  3183. log_warn " export LOKI_MODEL_OVERRIDE=<model-id> # e.g. from 'ollama list' or your provider's model page"
  3184. log_warn " Ignore this if your gateway maps those aliases for you (LiteLLM can)."
  3185. return 0
  3186. }
  3187. _loki_check_network_reachable() {
  3188. local provider="${1:-claude}"
  3189. [ "${LOKI_SKIP_NET_PREFLIGHT:-}" = "1" ] && return 0
  3190. command -v curl >/dev/null 2>&1 || return 0
  3191. # Alternate provider base URL set -> do not assume the default endpoint.
  3192. [ -n "${ANTHROPIC_BASE_URL:-}" ] && return 0
  3193. local endpoint=""
  3194. case "$provider" in
  3195. claude) endpoint="https://api.anthropic.com" ;;
  3196. *) return 0 ;; # unknown endpoint -> fail open, never guess
  3197. esac
  3198. # Advisory only: a fast curl probe failing does NOT prove the provider CLI
  3199. # cannot connect (a 3s timeout under load, transient DNS, or a proxy set for
  3200. # the CLI but not the shell all curl-fail while the real build succeeds). WARN
  3201. # and continue - the actual provider call is the authoritative connectivity
  3202. # test. Silence this with LOKI_SKIP_NET_PREFLIGHT=1. Never hard-block here.
  3203. if ! curl -sS -m 3 -o /dev/null "$endpoint" 2>/dev/null; then
  3204. log_warn "Could not verify network reachability to the AI provider (firewall/VPN/transient?). Continuing; the provider call will be the real test. Silence with LOKI_SKIP_NET_PREFLIGHT=1."
  3205. fi
  3206. return 0
  3207. }
  3208. validate_api_keys() {
  3209. local provider="${LOKI_PROVIDER:-claude}"
  3210. # Zero-friction preflight (T1.1): toolchain + reachability checks that apply
  3211. # to EVERY environment, run BEFORE the Docker/K8s early-return below so they
  3212. # are not silently skipped in the common local case. Node/git are genuinely
  3213. # required (node only when present-but-too-old); the network probe is
  3214. # fail-open and opt-out (LOKI_SKIP_NET_PREFLIGHT=1).
  3215. if ! _loki_check_node_version; then
  3216. return 1
  3217. fi
  3218. if ! _loki_check_git_present; then
  3219. return 1
  3220. fi
  3221. if ! _loki_check_workspace_writable; then
  3222. return 1
  3223. fi
  3224. if ! _loki_check_network_reachable "$provider"; then
  3225. return 1
  3226. fi
  3227. _loki_warn_alt_provider_model_alias
  3228. # Zero-friction auth preflight for LOCAL runs (must run BEFORE the early
  3229. # return below, which exits for non-Docker/K8s envs). When claude is the
  3230. # provider and there is no ANTHROPIC_API_KEY, ask the login-state helper
  3231. # (auth-status -> file -> keychain, all zero-network) whether the user is
  3232. # actually logged in. A never-logged-in user would otherwise enter the build,
  3233. # make a failing call, and 401 -- the worst first impression -- so we fail
  3234. # fast with the one-step fix. But we ONLY hard-block on a confident
  3235. # "loggedout"; an "expired" login gets its own message; "unknown" (we cannot
  3236. # prove the login state, e.g. an older CLI on a non-macOS box with no file)
  3237. # fails OPEN so a genuinely logged-in user is never blocked. This is the
  3238. # v7.104.2 fix for the native/Keychain login being misread as logged out.
  3239. # Opt out entirely with LOKI_SKIP_AUTH_PREFLIGHT=1.
  3240. if [[ "$provider" == "claude" && "${LOKI_SKIP_AUTH_PREFLIGHT:-}" != "1" && -z "${ANTHROPIC_API_KEY:-}" ]]; then
  3241. local _login_state
  3242. _login_state="$(_loki_claude_login_state)"
  3243. # Both branches report the blocker before returning. This is the wall a
  3244. # user hits AFTER answering every quickstart prompt and confirming the
  3245. # spend, and until now it emitted nothing -- so the funnel showed a first
  3246. # run attempted, then silence, indistinguishable from a successful build.
  3247. # Bounded enum only (`not_logged_in`), never the login state, path or
  3248. # credential; backgrounded and non-fatal so a diagnostic can never break
  3249. # the refusal it is describing.
  3250. if [[ "$_login_state" == "loggedout" ]]; then
  3251. if declare -f loki_emit_first_run_blocked >/dev/null 2>&1; then
  3252. ( loki_emit_first_run_blocked "not_logged_in" >/dev/null 2>&1 </dev/null & ) 2>/dev/null || true
  3253. fi
  3254. log_error "Claude Code is installed but not logged in -- the build would stall instead of running."
  3255. log_error "Log in once, then retry:"
  3256. log_error " claude auth login"
  3257. log_error "(or set ANTHROPIC_API_KEY, or LOKI_SKIP_AUTH_PREFLIGHT=1 to bypass this check)"
  3258. return 1
  3259. elif [[ "$_login_state" == "expired" ]]; then
  3260. if declare -f loki_emit_first_run_blocked >/dev/null 2>&1; then
  3261. ( loki_emit_first_run_blocked "not_logged_in" >/dev/null 2>&1 </dev/null & ) 2>/dev/null || true
  3262. fi
  3263. log_error "Your Claude Code login has expired -- the build would stall instead of running."
  3264. log_error "Fix it in one step, then retry:"
  3265. log_error " claude auth login"
  3266. log_error "(or set ANTHROPIC_API_KEY, or LOKI_SKIP_AUTH_PREFLIGHT=1 to bypass this check)"
  3267. return 1
  3268. fi
  3269. # "loggedin" or "unknown" -> proceed (fail open on uncertainty).
  3270. fi
  3271. # CLI tools (claude, opencode) use their own login sessions.
  3272. # Only require API keys inside Docker/K8s where CLI login isn't available.
  3273. if [[ ! -f "/.dockerenv" ]] && [[ -z "${KUBERNETES_SERVICE_HOST:-}" ]]; then
  3274. return 0
  3275. fi
  3276. local key_var=""
  3277. case "$provider" in
  3278. claude)
  3279. # Inside Docker, the Claude Code CLI can authenticate either via
  3280. # ANTHROPIC_API_KEY OR via a mounted OAuth credentials file (the
  3281. # zero-friction `loki docker` wrapper mounts the host login at
  3282. # ~/.claude/.credentials.json). If that file is present, the CLI
  3283. # has a valid login and we must NOT block on the env var -- doing
  3284. # so was the bug that made OAuth-based Docker runs exit at
  3285. # pre-flight with "Required API key ... is not set". Mirror the
  3286. # OAuth-aware doctor check (run.sh:9268).
  3287. if [[ -z "${ANTHROPIC_API_KEY:-}" ]]; then
  3288. local _claude_creds="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/.credentials.json"
  3289. if [[ -s "$_claude_creds" ]]; then
  3290. log_info "Claude Code OAuth credentials detected ($_claude_creds); using CLI login."
  3291. return 0
  3292. fi
  3293. fi
  3294. key_var="ANTHROPIC_API_KEY"
  3295. ;;
  3296. esac
  3297. if [[ -z "$key_var" ]]; then
  3298. return 0
  3299. fi
  3300. local key_value="${!key_var:-}"
  3301. # Try loading from secret file mounts (Docker/K8s)
  3302. if [[ -z "$key_value" ]]; then
  3303. local lower_name
  3304. lower_name=$(echo "$key_var" | tr '[:upper:]' '[:lower:]')
  3305. for mount_path in /run/secrets /var/run/secrets; do
  3306. if [[ -f "$mount_path/$lower_name" ]]; then
  3307. key_value=$(cat "$mount_path/$lower_name" 2>/dev/null | tr -d '[:space:]')
  3308. if [[ -n "$key_value" ]]; then
  3309. export "$key_var=$key_value"
  3310. log_info "Loaded $key_var from secret file: $mount_path/$lower_name"
  3311. break
  3312. fi
  3313. fi
  3314. done
  3315. fi
  3316. if [[ -z "$key_value" ]]; then
  3317. log_error "Required API key $key_var is not set for provider $provider"
  3318. log_error "Set via environment variable or Docker/K8s secret mount"
  3319. return 1
  3320. fi
  3321. # Log masked key for debugging
  3322. local masked="${key_value:0:8}...${key_value: -4}"
  3323. log_info "API key $key_var: $masked (${#key_value} chars)"
  3324. # Fail-fast auth preflight (v7.91): for Claude OAuth logins, a present-but-
  3325. # EXPIRED token passes the presence check above but then 401s on the first
  3326. # provider call, leaving the build stalled at BOOTSTRAP with no clear cause.
  3327. # Catch that here with a zero-network, zero-token local expiry check so the
  3328. # user gets an instant, copy-pasteable fix instead of a silent stall.
  3329. # Opt out with LOKI_SKIP_AUTH_PREFLIGHT=1 (offline/odd setups).
  3330. if [[ "$provider" == "claude" && "${LOKI_SKIP_AUTH_PREFLIGHT:-}" != "1" && -z "${ANTHROPIC_API_KEY:-}" ]]; then
  3331. local _creds="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/.credentials.json"
  3332. if [[ -s "$_creds" ]]; then
  3333. local _expired
  3334. _expired=$(python3 -c "
  3335. import json, sys, time
  3336. try:
  3337. d = json.load(open(sys.argv[1]))
  3338. exp = d.get('claudeAiOauth', {}).get('expiresAt')
  3339. # expiresAt is epoch milliseconds; compare with a 60s safety margin.
  3340. if isinstance(exp, (int, float)) and exp > 0 and (exp / 1000.0) <= (time.time() + 60):
  3341. print('expired')
  3342. except Exception:
  3343. pass # unreadable/unknown schema -> do not block (fail open, let the call decide)
  3344. " "$_creds" 2>/dev/null || true)
  3345. if [[ "$_expired" == "expired" ]]; then
  3346. log_error "Your Claude Code login has expired -- the build would stall instead of running."
  3347. log_error "Fix it in one step, then retry:"
  3348. log_error " claude auth login"
  3349. log_error "(or set ANTHROPIC_API_KEY, or LOKI_SKIP_AUTH_PREFLIGHT=1 to bypass this check)"
  3350. return 1
  3351. fi
  3352. fi
  3353. # NOTE: the never-logged-in (no credentials file) case is handled earlier,
  3354. # BEFORE the local early-return, so it covers local runs too (this Docker/
  3355. # K8s-only block would otherwise be unreachable for that case).
  3356. fi
  3357. return 0
  3358. }
  3359. #===============================================================================
  3360. # Complexity Tier Detection (Auto-Claude pattern)
  3361. #===============================================================================
  3362. # Detect project complexity from PRD and codebase
  3363. detect_complexity() {
  3364. local prd_path="${1:-}"
  3365. local target_dir="${TARGET_DIR:-.}"
  3366. # If forced, use that
  3367. if [ "$COMPLEXITY_TIER" != "auto" ]; then
  3368. DETECTED_COMPLEXITY="$COMPLEXITY_TIER"
  3369. return 0
  3370. fi
  3371. # Count files in project (excluding common non-source dirs)
  3372. local file_count=0
  3373. file_count=$(find "$target_dir" -type f \
  3374. \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" \
  3375. -o -name "*.py" -o -name "*.go" -o -name "*.rs" -o -name "*.java" \
  3376. -o -name "*.rb" -o -name "*.php" -o -name "*.swift" -o -name "*.kt" \) \
  3377. ! -path "*/node_modules/*" ! -path "*/.git/*" ! -path "*/vendor/*" \
  3378. ! -path "*/dist/*" ! -path "*/build/*" ! -path "*/__pycache__/*" \
  3379. 2>/dev/null | wc -l | tr -d ' ')
  3380. # Validate file_count is numeric (guard against empty/malformed pipeline output)
  3381. file_count="${file_count:-0}"
  3382. file_count="${file_count//[^0-9]/}"
  3383. # Check for external integrations
  3384. local has_external=false
  3385. if grep -rq "oauth\|SAML\|OIDC\|stripe\|twilio\|aws-sdk\|@google-cloud\|azure" \
  3386. "$target_dir" --include="*.json" --include="*.ts" --include="*.js" 2>/dev/null; then
  3387. has_external=true
  3388. fi
  3389. # Check for multiple services (docker-compose, k8s)
  3390. local has_microservices=false
  3391. if [ -f "$target_dir/docker-compose.yml" ] || [ -d "$target_dir/k8s" ] || \
  3392. [ -f "$target_dir/docker-compose.yaml" ]; then
  3393. has_microservices=true
  3394. fi
  3395. # Analyze PRD if provided
  3396. local prd_complexity="standard"
  3397. if [ -n "$prd_path" ] && [ -f "$prd_path" ]; then
  3398. local prd_words=$(wc -w < "$prd_path" | tr -d ' ')
  3399. local feature_count=0
  3400. local prd_lines=$(wc -l < "$prd_path" | tr -d ' ')
  3401. # Detect PRD format and count features accordingly
  3402. if [[ "$prd_path" == *.json ]]; then
  3403. # JSON PRD: count features, requirements, tasks arrays
  3404. if command -v jq &>/dev/null; then
  3405. feature_count=$(jq '
  3406. [.features, .requirements, .tasks, .user_stories, .epics] |
  3407. map(select(. != null) | if type == "array" then length else 0 end) |
  3408. add // 0
  3409. ' "$prd_path" 2>/dev/null || echo "0")
  3410. else
  3411. # Fallback: count array elements by pattern
  3412. feature_count=$(grep -c '"title"\|"name"\|"feature"\|"requirement"' "$prd_path" 2>/dev/null || echo "0")
  3413. fi
  3414. else
  3415. # Markdown PRD: count headers and checkboxes
  3416. feature_count=$(grep -c "^##\|^- \[" "$prd_path" 2>/dev/null || echo "0")
  3417. fi
  3418. # WAVE8 FIX run.sh-provider-F1 (HIGH): grep -c prints "0" AND exits 1 on
  3419. # zero matches; with the '|| echo "0"' fallback that yields "0\n0", which
  3420. # crashes the integer tests below ([: 0\n0: integer expression expected)
  3421. # and silently drops complexity from simple->standard. Strip to digits
  3422. # after every assignment path (jq, both greps), mirroring file_count:1688.
  3423. # "0\n0" -> "00" -> arithmetically 0.
  3424. feature_count="${feature_count:-0}"
  3425. feature_count="${feature_count//[^0-9]/}"
  3426. feature_count="${feature_count:-0}"
  3427. # Count distinct sections (h2/h3 headers) for structural complexity (#74)
  3428. local section_count=0
  3429. if [[ "$prd_path" != *.json ]]; then
  3430. section_count=$(grep -c "^##\|^###" "$prd_path" 2>/dev/null || echo "0")
  3431. fi
  3432. # WAVE8 FIX run.sh-provider-F1: same grep -c double-output guard.
  3433. section_count="${section_count:-0}"
  3434. section_count="${section_count//[^0-9]/}"
  3435. section_count="${section_count:-0}"
  3436. # PRD complexity uses content length, feature count, AND structural depth (#74)
  3437. # A PRD with multiple sections or substantial content is not "simple" even with few project files
  3438. if [ "$prd_words" -lt 200 ] && [ "$feature_count" -lt 5 ] && [ "$section_count" -lt 3 ]; then
  3439. prd_complexity="simple"
  3440. elif [ "$prd_words" -gt 1000 ] || [ "$feature_count" -gt 15 ] || [ "$section_count" -gt 10 ]; then
  3441. prd_complexity="complex"
  3442. fi
  3443. fi
  3444. # Determine final complexity
  3445. # A non-simple PRD always prevents "simple" classification regardless of file count (#74)
  3446. if [ "$file_count" -le 5 ] && [ "$prd_complexity" = "simple" ] && \
  3447. [ "$has_external" = "false" ] && [ "$has_microservices" = "false" ]; then
  3448. DETECTED_COMPLEXITY="simple"
  3449. elif [ "$file_count" -gt 50 ] || [ "$has_microservices" = "true" ] || \
  3450. [ "$has_external" = "true" ] || [ "$prd_complexity" = "complex" ]; then
  3451. DETECTED_COMPLEXITY="complex"
  3452. else
  3453. DETECTED_COMPLEXITY="standard"
  3454. fi
  3455. log_info "Detected complexity: $DETECTED_COMPLEXITY (files: $file_count, prd: $prd_complexity, external: $has_external, microservices: $has_microservices)"
  3456. }
  3457. # Get phases based on complexity tier
  3458. # Get phase names based on complexity tier
  3459. #===============================================================================
  3460. # Dynamic Tier Selection (RARV-aware model routing)
  3461. #===============================================================================
  3462. # Maps RARV cycle phases to model tiers. IMPORTANT (post-v7.104.0): this rotation
  3463. # is OFF by default -- CURRENT_TIER is pinned once from LOKI_SESSION_MODEL and held
  3464. # constant every iteration; get_rarv_tier() only fires under
  3465. # LOKI_LEGACY_TIER_SWITCHING=true (see the pin logic near run.sh:17150). And even
  3466. # when it does fire, planning/development/fast ALL resolve to the SAME model on
  3467. # stock config (CLAUDE_DEFAULT_*=sonnet), so the rotation changes only the
  3468. # --effort flag, not the model. The tier -> effort map (loki_effort_for_tier,
  3469. # autonomy/lib/claude-flags.sh):
  3470. # - Reason phase -> planning tier (effort xhigh)
  3471. # - Act phase -> development tier (effort high)
  3472. # - Reflect phase -> development tier (effort high)
  3473. # - Verify phase -> fast tier (effort medium)
  3474. # Global tier for current iteration (set by get_rarv_tier)
  3475. CURRENT_TIER="development"
  3476. # Export for provider helper functions (e.g., provider_get_current_model)
  3477. LOKI_CURRENT_TIER="$CURRENT_TIER"
  3478. export LOKI_CURRENT_TIER
  3479. # Get the appropriate tier based on RARV cycle step
  3480. # Args: iteration_count (defaults to ITERATION_COUNT)
  3481. # Returns: tier name (planning, development, fast)
  3482. get_rarv_tier() {
  3483. local iteration="${1:-$ITERATION_COUNT}"
  3484. local rarv_step=$((iteration % 4))
  3485. case $rarv_step in
  3486. 0) # Reason phase - planning/architecture
  3487. echo "planning"
  3488. ;;
  3489. 1) # Act phase - implementation
  3490. echo "development"
  3491. ;;
  3492. 2) # Reflect phase - review/analysis
  3493. echo "development"
  3494. ;;
  3495. 3) # Verify phase - testing/validation
  3496. echo "fast"
  3497. ;;
  3498. *) # Fallback to development
  3499. echo "development"
  3500. ;;
  3501. esac
  3502. }
  3503. # Get RARV phase name for logging
  3504. get_rarv_phase_name() {
  3505. local iteration="${1:-$ITERATION_COUNT}"
  3506. local rarv_step=$((iteration % 4))
  3507. case $rarv_step in
  3508. 0) echo "REASON" ;;
  3509. 1) echo "ACT" ;;
  3510. 2) echo "REFLECT" ;;
  3511. 3) echo "VERIFY" ;;
  3512. *) echo "UNKNOWN" ;;
  3513. esac
  3514. }
  3515. # Get provider-specific tier parameter based on current tier
  3516. # v6.0.0: Delegates to resolve_model_for_tier() if available (dynamic resolution).
  3517. # Falls back to static mapping for backward compatibility.
  3518. get_provider_tier_param() {
  3519. local tier="${1:-$CURRENT_TIER}"
  3520. # v6.0.0: Use dynamic resolution if provider has resolve_model_for_tier
  3521. if type resolve_model_for_tier &>/dev/null; then
  3522. local resolved
  3523. resolved=$(resolve_model_for_tier "$tier")
  3524. echo "$resolved"
  3525. return
  3526. fi
  3527. # Legacy fallback: static tier mapping
  3528. case "${PROVIDER_NAME:-claude}" in
  3529. claude)
  3530. case "$tier" in
  3531. planning)
  3532. # Evidence-based routing (scoped): the official model-config
  3533. # docs explicitly name "architecture decisions" and
  3534. # "root-cause investigations" as where Fable 5's extra
  3535. # investigation and self-verification pay off. So the
  3536. # planning/architecture tier may opt in to Fable via
  3537. # LOKI_FABLE_ARCHITECT=1. Default OFF because Fable is 2x
  3538. # Opus per token; reserve it for the REASON/architecture
  3539. # iterations the user explicitly wants. An explicit
  3540. # PROVIDER_MODEL_PLANNING still wins (operator override).
  3541. if [ -n "${PROVIDER_MODEL_PLANNING:-}" ]; then
  3542. echo "${PROVIDER_MODEL_PLANNING}"
  3543. elif [ "${LOKI_FABLE_ARCHITECT:-0}" = "1" ]; then
  3544. # fable unavailable, collapse to opus. Claude Fable 5 is
  3545. # not available at the Claude API ("use Opus 4.8"); the
  3546. # architect opt-in now runs opus. Matches claude.sh
  3547. # resolve_model_for_tier and the estimator/dashboard.
  3548. echo "opus"
  3549. else
  3550. # v7.104.0: default planning to sonnet (was opus) to match
  3551. # the sourced claude.sh default. This bare fallback only
  3552. # fires when claude.sh is unsourced.
  3553. echo "sonnet"
  3554. fi
  3555. ;;
  3556. development) echo "${PROVIDER_MODEL_DEVELOPMENT:-sonnet}" ;;
  3557. fast) echo "${PROVIDER_MODEL_FAST:-sonnet}" ;;
  3558. # fable unavailable, collapse to opus. Without this arm an
  3559. # unsourced-claude.sh environment (this static fallback) would
  3560. # silently downgrade a fable-pinned tier to sonnet via the `*`
  3561. # default. Matches resolve_model_for_tier's explicit fable) arm,
  3562. # which now resolves to opus (Fable 5 unavailable at the API).
  3563. fable) echo "opus" ;;
  3564. *) echo "sonnet" ;;
  3565. esac
  3566. ;;
  3567. *)
  3568. echo "development"
  3569. ;;
  3570. esac
  3571. }
  3572. #===============================================================================
  3573. # Provider Spawn Timeout (removed WAVE9 / provider-F2)
  3574. #
  3575. # A former invoke_with_timeout() helper (v6.0.0) wrapped a command in
  3576. # `timeout <s> "$@"` with a retry loop. It was never wired to the main
  3577. # provider invocation and is intentionally not revived, for two reasons:
  3578. #
  3579. # 1. No safe generous default. The main provider call is a long-running
  3580. # autonomous coding agent. Any fixed timeout short enough to catch a
  3581. # hang would also kill legitimate multi-minute iterations, and there is
  3582. # no "generous enough" value that is both safe and useful by default.
  3583. # 2. Wrong retry semantics. The helper re-ran the same command on timeout.
  3584. # Re-running a coding agent mid-work (it may have already edited files)
  3585. # is actively harmful, not protective.
  3586. #
  3587. # The main invocation is also a pipeline (`claude | tee | python3`), which a
  3588. # positional-arg `timeout "$@"` wrapper cannot wrap at all. Interrupting a
  3589. # hung provider is handled by the SIGINT trap (kill_provider_child) instead.
  3590. #
  3591. # The `loki config spawn_timeout` / `spawn_retries` knobs (autonomy/loki) and
  3592. # the config->env mapping in this file (`'spawn_timeout':'LOKI_SPAWN_TIMEOUT'`
  3593. # in the config loader) still export LOKI_SPAWN_TIMEOUT / LOKI_SPAWN_RETRIES,
  3594. # but nothing consumes them now. The mapping line is intentionally left in place
  3595. # (a config-schema test may enumerate it); the full inert-knob removal spans
  3596. # autonomy/loki too and is a separate cross-file follow-up.
  3597. #===============================================================================
  3598. #===============================================================================
  3599. # GitHub Integration Functions (v4.1.0)
  3600. #===============================================================================
  3601. # GitHub integration settings
  3602. GITHUB_IMPORT=${LOKI_GITHUB_IMPORT:-false}
  3603. GITHUB_PR=${LOKI_GITHUB_PR:-false}
  3604. GITHUB_SYNC=${LOKI_GITHUB_SYNC:-false}
  3605. GITHUB_REPO=${LOKI_GITHUB_REPO:-""}
  3606. GITHUB_LABELS=${LOKI_GITHUB_LABELS:-""}
  3607. GITHUB_MILESTONE=${LOKI_GITHUB_MILESTONE:-""}
  3608. GITHUB_ASSIGNEE=${LOKI_GITHUB_ASSIGNEE:-""}
  3609. GITHUB_LIMIT=${LOKI_GITHUB_LIMIT:-100}
  3610. GITHUB_PR_LABEL=${LOKI_GITHUB_PR_LABEL:-""}
  3611. # Check if gh CLI is available and authenticated
  3612. check_github_cli() {
  3613. if ! command -v gh &> /dev/null; then
  3614. log_warn "gh CLI not found. Install with: brew install gh"
  3615. return 1
  3616. fi
  3617. if ! gh auth status &> /dev/null; then
  3618. log_warn "gh CLI not authenticated. Run: gh auth login"
  3619. return 1
  3620. fi
  3621. return 0
  3622. }
  3623. # Get current repo from git remote or LOKI_GITHUB_REPO
  3624. get_github_repo() {
  3625. if [ -n "$GITHUB_REPO" ]; then
  3626. echo "$GITHUB_REPO"
  3627. return
  3628. fi
  3629. # Try to detect from git remote
  3630. local remote_url
  3631. remote_url=$(git remote get-url origin 2>/dev/null || echo "")
  3632. if [ -z "$remote_url" ]; then
  3633. return 1
  3634. fi
  3635. # Extract owner/repo from various URL formats
  3636. # https://github.com/owner/repo.git
  3637. # git@github.com:owner/repo.git
  3638. local repo
  3639. repo=$(echo "$remote_url" | sed -E 's/.*github.com[:/]([^/]+\/[^/]+)(\.git)?$/\1/')
  3640. repo="${repo%.git}"
  3641. if [ -n "$repo" ] && [[ "$repo" == *"/"* ]]; then
  3642. echo "$repo"
  3643. return 0
  3644. fi
  3645. return 1
  3646. }
  3647. # Import issues from GitHub as tasks
  3648. import_github_issues() {
  3649. if [ "$GITHUB_IMPORT" != "true" ]; then
  3650. return 0
  3651. fi
  3652. if ! check_github_cli; then
  3653. return 1
  3654. fi
  3655. local repo
  3656. repo=$(get_github_repo)
  3657. if [ -z "$repo" ]; then
  3658. log_error "Could not determine GitHub repo. Set LOKI_GITHUB_REPO=owner/repo"
  3659. return 1
  3660. fi
  3661. log_info "Importing issues from GitHub: $repo"
  3662. # Build gh issue list command with filters
  3663. local gh_args=("issue" "list" "--repo" "$repo" "--state" "open" "--limit" "$GITHUB_LIMIT" "--json" "number,title,body,labels,url,milestone,assignees")
  3664. if [ -n "$GITHUB_LABELS" ]; then
  3665. IFS=',' read -ra LABELS <<< "$GITHUB_LABELS"
  3666. for label in "${LABELS[@]}"; do
  3667. # Trim whitespace from label
  3668. label=$(echo "$label" | xargs)
  3669. gh_args+=("--label" "$label")
  3670. done
  3671. fi
  3672. if [ -n "$GITHUB_MILESTONE" ]; then
  3673. gh_args+=("--milestone" "$GITHUB_MILESTONE")
  3674. fi
  3675. if [ -n "$GITHUB_ASSIGNEE" ]; then
  3676. gh_args+=("--assignee" "$GITHUB_ASSIGNEE")
  3677. fi
  3678. # Fetch issues with error capture
  3679. local issues gh_error
  3680. if ! issues=$(gh "${gh_args[@]}" 2>&1); then
  3681. gh_error="$issues"
  3682. if echo "$gh_error" | grep -q "rate limit"; then
  3683. log_error "GitHub API rate limit exceeded. Wait and retry."
  3684. else
  3685. log_error "Failed to fetch issues: $gh_error"
  3686. fi
  3687. return 1
  3688. fi
  3689. if [ -z "$issues" ] || [ "$issues" == "[]" ]; then
  3690. log_info "No open issues found matching filters"
  3691. return 0
  3692. fi
  3693. # Convert issues to tasks
  3694. local pending_file=".loki/queue/pending.json"
  3695. local task_count=0
  3696. # BUG #14 fix: Normalize to bare [] format (consistent with init_loki_dir
  3697. # and all other queue consumers). Previously used {"tasks":[]} wrapper here
  3698. # but bare [] everywhere else, causing format mismatch.
  3699. if [ ! -f "$pending_file" ]; then
  3700. echo '[]' > "$pending_file"
  3701. elif jq -e 'type == "object"' "$pending_file" &>/dev/null; then
  3702. # Normalize {"tasks":[...]} wrapper to bare array
  3703. local _tmp_normalize
  3704. _tmp_normalize=$(mktemp)
  3705. jq 'if type == "object" then .tasks // [] else . end' "$pending_file" > "$_tmp_normalize" && mv "$_tmp_normalize" "$pending_file"
  3706. rm -f "$_tmp_normalize"
  3707. fi
  3708. # Parse issues in the current shell so task_count remains accurate without
  3709. # relying on descriptor-backed process substitution inside confinement.
  3710. local issue_rows
  3711. issue_rows=$(printf '%s\n' "$issues" | jq -c '.[]') || return 1
  3712. while read -r issue; do
  3713. [ -n "$issue" ] || continue
  3714. local number title body full_body url labels
  3715. number=$(echo "$issue" | jq -r '.number')
  3716. title=$(echo "$issue" | jq -r '.title')
  3717. full_body=$(echo "$issue" | jq -r '.body // ""')
  3718. # Truncate body with indicator if needed
  3719. if [ ${#full_body} -gt 500 ]; then
  3720. body="${full_body:0:497}..."
  3721. else
  3722. body="$full_body"
  3723. fi
  3724. url=$(echo "$issue" | jq -r '.url')
  3725. labels=$(echo "$issue" | jq -c '[.labels[].name]')
  3726. # Check if task already exists (bare array format)
  3727. if jq -e ".[] | select(.github_issue == $number)" "$pending_file" &>/dev/null; then
  3728. log_info "Issue #$number already imported, skipping"
  3729. continue
  3730. fi
  3731. # Determine priority from labels
  3732. local priority="normal"
  3733. if echo "$labels" | grep -qE '"(priority:critical|P0)"'; then
  3734. priority="critical"
  3735. elif echo "$labels" | grep -qE '"(priority:high|P1)"'; then
  3736. priority="high"
  3737. elif echo "$labels" | grep -qE '"(priority:medium|P2)"'; then
  3738. priority="medium"
  3739. elif echo "$labels" | grep -qE '"(priority:low|P3)"'; then
  3740. priority="low"
  3741. fi
  3742. # Add task to pending queue
  3743. local task_id="github-$number"
  3744. local task_json
  3745. task_json=$(jq -n \
  3746. --arg id "$task_id" \
  3747. --arg title "$title" \
  3748. --arg desc "GitHub Issue #$number: $body" \
  3749. --argjson num "$number" \
  3750. --arg url "$url" \
  3751. --argjson labels "$labels" \
  3752. --arg priority "$priority" \
  3753. --arg created "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
  3754. '{
  3755. id: $id,
  3756. title: $title,
  3757. description: $desc,
  3758. source: "github",
  3759. github_issue: $num,
  3760. github_url: $url,
  3761. labels: $labels,
  3762. priority: $priority,
  3763. status: "pending",
  3764. created_at: $created
  3765. }')
  3766. # BUG-XC-010: Create temp file in same directory as target (avoids cross-filesystem mv).
  3767. # v7.5.12: replace flock-only queue lock with portable mkdir-mutex via
  3768. # safe_acquire_lock (works on macOS without util-linux flock).
  3769. local temp_file
  3770. temp_file=$(mktemp ".loki/queue/pending.json.tmp.XXXXXX")
  3771. local lockfile=".loki/queue/.pending.lock"
  3772. if type safe_acquire_lock >/dev/null 2>&1 && safe_acquire_lock "$lockfile" 5; then
  3773. if jq ". += [$task_json]" "$pending_file" > "$temp_file" && mv "$temp_file" "$pending_file"; then
  3774. log_info "Imported issue #$number: $title"
  3775. task_count=$((task_count + 1))
  3776. else
  3777. log_warn "Failed to import issue #$number"
  3778. fi
  3779. safe_release_lock "$lockfile"
  3780. else
  3781. log_warn "Could not acquire queue lock for issue #$number, skipping"
  3782. fi
  3783. rm -f "$temp_file"
  3784. done << GITHUB_ISSUES_EOF
  3785. $issue_rows
  3786. GITHUB_ISSUES_EOF
  3787. log_info "Imported $task_count issues from GitHub"
  3788. }
  3789. # Create PR for completed feature
  3790. create_github_pr() {
  3791. local feature_name="$1"
  3792. local branch_name="${2:-$(git rev-parse --abbrev-ref HEAD)}"
  3793. if [ "$GITHUB_PR" != "true" ]; then
  3794. return 0
  3795. fi
  3796. if ! check_github_cli; then
  3797. return 1
  3798. fi
  3799. local repo
  3800. repo=$(get_github_repo)
  3801. if [ -z "$repo" ]; then
  3802. log_error "Could not determine GitHub repo"
  3803. return 1
  3804. fi
  3805. log_info "Creating PR for: $feature_name"
  3806. # Generate PR body from completed tasks
  3807. local pr_body=".loki/reports/pr-body.md"
  3808. mkdir -p "$(dirname "$pr_body")"
  3809. local version
  3810. version=$(cat "${SCRIPT_DIR%/*}/VERSION" 2>/dev/null || echo "unknown")
  3811. cat > "$pr_body" << EOF
  3812. ## Summary
  3813. Automated implementation by Loki Mode v$version ($ITERATION_COUNT iterations, provider: ${PROVIDER_NAME:-claude})
  3814. ### Feature: $feature_name
  3815. ### Tasks Completed
  3816. EOF
  3817. # Add completed tasks from ledger
  3818. if [ -f ".loki/ledger.json" ]; then
  3819. jq -r '.completed_tasks[]? | "- [x] \(.title // .id)"' .loki/ledger.json >> "$pr_body" 2>/dev/null || true
  3820. fi
  3821. cat >> "$pr_body" << EOF
  3822. ### Quality Gates
  3823. - Static Analysis: $([ -f ".loki/quality/static-analysis.pass" ] && echo "PASS" || echo "PENDING")
  3824. - Unit Tests: $([ -f ".loki/quality/unit-tests.pass" ] && echo "PASS" || echo "PENDING")
  3825. - Code Review: $([ -f ".loki/quality/code-review.pass" ] && echo "PASS" || echo "PENDING")
  3826. ### Related Issues
  3827. EOF
  3828. # Find related GitHub issues
  3829. if [ -f ".loki/ledger.json" ]; then
  3830. jq -r '.completed_tasks[]? | select(.github_issue) | "Closes #\(.github_issue)"' .loki/ledger.json >> "$pr_body" 2>/dev/null || true
  3831. fi
  3832. # Proven PR (Loop 6): append the Evidence Receipt to the body file before
  3833. # gh pr create --body-file. Default-on; LOKI_PROVEN_PR=0 -> body file bytes
  3834. # byte-identical to before. Empty expected_head_sha by design (R-DET-1
  3835. # run_id pointer is the anti-stale guard; the branch head is offset by the
  3836. # session commit). Best-effort: a missing proof appends nothing extra here.
  3837. if [ "${LOKI_PROVEN_PR:-1}" != "0" ] && declare -f render_evidence_receipt_md >/dev/null 2>&1; then
  3838. local _bf_proof=""
  3839. _bf_proof="$(_loki_proof_json_for_pr 2>/dev/null || true)"
  3840. if [ -n "$_bf_proof" ]; then
  3841. { printf '\n'; render_evidence_receipt_md "$_bf_proof" "" "" 2>/dev/null; } >> "$pr_body" 2>/dev/null || true
  3842. fi
  3843. fi
  3844. # Build PR create command
  3845. local pr_args=("pr" "create" "--repo" "$repo" "--title" "[Loki Mode] $feature_name" "--body-file" "$pr_body")
  3846. # Add label only if specified (avoids error if label doesn't exist)
  3847. if [ -n "$GITHUB_PR_LABEL" ]; then
  3848. pr_args+=("--label" "$GITHUB_PR_LABEL")
  3849. fi
  3850. # Create PR and capture output
  3851. local pr_url
  3852. if ! pr_url=$(gh "${pr_args[@]}" 2>&1); then
  3853. log_error "Failed to create PR: $pr_url"
  3854. return 1
  3855. fi
  3856. log_info "PR created: $pr_url"
  3857. }
  3858. # Sync task status to GitHub issue
  3859. sync_github_status() {
  3860. local task_id="$1"
  3861. local status="$2"
  3862. local message="${3:-}"
  3863. if [ "$GITHUB_SYNC" != "true" ]; then
  3864. return 0
  3865. fi
  3866. if ! check_github_cli; then
  3867. return 1
  3868. fi
  3869. # Extract issue number from task_id (format: github-123)
  3870. local issue_number
  3871. issue_number=$(echo "$task_id" | sed 's/github-//')
  3872. if ! [[ "$issue_number" =~ ^[0-9]+$ ]]; then
  3873. return 0 # Not a GitHub-sourced task
  3874. fi
  3875. local repo
  3876. repo=$(get_github_repo)
  3877. if [ -z "$repo" ]; then
  3878. return 1
  3879. fi
  3880. # Track synced issues to avoid duplicate comments
  3881. mkdir -p .loki/github
  3882. local sync_log=".loki/github/synced.log"
  3883. local sync_key="${issue_number}:${status}"
  3884. if [ -f "$sync_log" ] && grep -qF "$sync_key" "$sync_log" 2>/dev/null; then
  3885. return 0 # Already synced this status
  3886. fi
  3887. case "$status" in
  3888. "in_progress")
  3889. gh issue comment "$issue_number" --repo "$repo" \
  3890. --body "**Loki Mode** -- Working on this issue (iteration $ITERATION_COUNT)" \
  3891. 2>/dev/null || true
  3892. ;;
  3893. "completed")
  3894. local branch
  3895. branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "main")
  3896. local commit
  3897. commit=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
  3898. gh issue comment "$issue_number" --repo "$repo" \
  3899. --body "**Loki Mode** -- Implementation complete on \`$branch\` ($commit). ${message:-}" \
  3900. 2>/dev/null || true
  3901. ;;
  3902. "closed")
  3903. gh issue close "$issue_number" --repo "$repo" \
  3904. --reason "completed" \
  3905. --comment "**Loki Mode** -- Resolved. ${message:-}" \
  3906. 2>/dev/null || true
  3907. ;;
  3908. esac
  3909. # Record sync to avoid duplicates
  3910. echo "$sync_key" >> "$sync_log"
  3911. }
  3912. # Sync all completed GitHub-sourced tasks back to their issues
  3913. # Called after each iteration and at session end
  3914. sync_github_completed_tasks() {
  3915. if [ "$GITHUB_SYNC" != "true" ]; then
  3916. return 0
  3917. fi
  3918. if ! check_github_cli; then
  3919. return 0
  3920. fi
  3921. local completed_file=".loki/queue/completed.json"
  3922. if [ ! -f "$completed_file" ]; then
  3923. return 0
  3924. fi
  3925. # Find GitHub-sourced tasks in completed queue that haven't been synced
  3926. python3 -c "
  3927. import json, sys
  3928. try:
  3929. with open('$completed_file') as f:
  3930. tasks = json.load(f)
  3931. for t in tasks:
  3932. tid = t.get('id', '')
  3933. if tid.startswith('github-'):
  3934. print(tid)
  3935. except Exception:
  3936. pass
  3937. " 2>/dev/null | while read -r task_id; do
  3938. sync_github_status "$task_id" "completed"
  3939. done
  3940. }
  3941. # Sync GitHub-sourced tasks currently in-progress
  3942. sync_github_in_progress_tasks() {
  3943. if [ "$GITHUB_SYNC" != "true" ]; then
  3944. return 0
  3945. fi
  3946. if ! check_github_cli; then
  3947. return 0
  3948. fi
  3949. local pending_file=".loki/queue/pending.json"
  3950. if [ ! -f "$pending_file" ]; then
  3951. return 0
  3952. fi
  3953. # Find GitHub-sourced tasks in pending queue (about to be worked on)
  3954. python3 -c "
  3955. import json
  3956. try:
  3957. with open('$pending_file') as f:
  3958. data = json.load(f)
  3959. tasks = data.get('tasks', data) if isinstance(data, dict) else data
  3960. for t in tasks:
  3961. tid = t.get('id', '')
  3962. if tid.startswith('github-'):
  3963. print(tid)
  3964. except Exception:
  3965. pass
  3966. " 2>/dev/null | while read -r task_id; do
  3967. sync_github_status "$task_id" "in_progress"
  3968. done
  3969. }
  3970. # Export tasks to GitHub issues (reverse sync)
  3971. export_tasks_to_github() {
  3972. if ! check_github_cli; then
  3973. return 1
  3974. fi
  3975. local repo
  3976. repo=$(get_github_repo)
  3977. if [ -z "$repo" ]; then
  3978. log_error "Could not determine GitHub repo"
  3979. return 1
  3980. fi
  3981. local pending_file=".loki/queue/pending.json"
  3982. if [ ! -f "$pending_file" ]; then
  3983. log_warn "No pending tasks to export"
  3984. return 0
  3985. fi
  3986. # Export non-GitHub tasks as issues (handles both bare array and wrapper formats)
  3987. jq -c 'if type == "object" then .tasks // [] else . end | .[] | select(.source != "github")' "$pending_file" 2>/dev/null | while read -r task; do
  3988. local title desc
  3989. title=$(echo "$task" | jq -r '.title')
  3990. desc=$(echo "$task" | jq -r '.description // ""')
  3991. log_info "Creating issue: $title"
  3992. # BUG-GH-009: Check if label exists before using --label; skip if absent
  3993. local label_flag=""
  3994. if gh label list --repo "$repo" 2>/dev/null | grep -q "loki-mode"; then
  3995. label_flag="--label loki-mode"
  3996. fi
  3997. gh issue create --repo "$repo" \
  3998. --title "$title" \
  3999. --body "$desc" \
  4000. $label_flag \
  4001. 2>/dev/null || log_warn "Failed to create issue: $title"
  4002. done
  4003. }
  4004. #===============================================================================
  4005. # Desktop Notifications (v4.1.0)
  4006. #===============================================================================
  4007. # Notification settings
  4008. NOTIFICATIONS_ENABLED=${LOKI_NOTIFICATIONS:-true}
  4009. NOTIFICATION_SOUND=${LOKI_NOTIFICATION_SOUND:-true}
  4010. # Send desktop notification (cross-platform)
  4011. send_notification() {
  4012. local title="$1"
  4013. local message="$2"
  4014. local urgency="${3:-normal}" # low, normal, critical
  4015. if [ "$NOTIFICATIONS_ENABLED" != "true" ]; then
  4016. return 0
  4017. fi
  4018. # Validate inputs - skip empty notifications
  4019. if [ -z "$title" ] && [ -z "$message" ]; then
  4020. return 0
  4021. fi
  4022. title="${title:-Notification}" # Default title if empty
  4023. # macOS: use osascript
  4024. if command -v osascript &> /dev/null; then
  4025. # Escape backslashes first, then double quotes for AppleScript
  4026. local escaped_title="${title//\\/\\\\}"
  4027. escaped_title="${escaped_title//\"/\\\"}"
  4028. local escaped_message="${message//\\/\\\\}"
  4029. escaped_message="${escaped_message//\"/\\\"}"
  4030. osascript -e "display notification \"$escaped_message\" with title \"Loki Mode\" subtitle \"$escaped_title\"" 2>/dev/null || true
  4031. # Play sound if enabled (low urgency intentionally silent)
  4032. if [ "$NOTIFICATION_SOUND" = "true" ]; then
  4033. case "$urgency" in
  4034. critical)
  4035. osascript -e 'beep 3' 2>/dev/null || true
  4036. ;;
  4037. normal)
  4038. osascript -e 'beep' 2>/dev/null || true
  4039. ;;
  4040. low)
  4041. # Intentionally no sound for low urgency notifications
  4042. ;;
  4043. esac
  4044. fi
  4045. return 0
  4046. fi
  4047. # Linux: use notify-send
  4048. if command -v notify-send &> /dev/null; then
  4049. local notify_urgency="normal"
  4050. case "$urgency" in
  4051. critical) notify_urgency="critical" ;;
  4052. low) notify_urgency="low" ;;
  4053. *) notify_urgency="normal" ;;
  4054. esac
  4055. # Escape markup characters for notify-send (supports basic Pango)
  4056. local safe_title="${title//&/&amp;}"
  4057. safe_title="${safe_title//</&lt;}"
  4058. safe_title="${safe_title//>/&gt;}"
  4059. local safe_message="${message//&/&amp;}"
  4060. safe_message="${safe_message//</&lt;}"
  4061. safe_message="${safe_message//>/&gt;}"
  4062. notify-send -u "$notify_urgency" "Loki Mode: $safe_title" "$safe_message" 2>/dev/null || true
  4063. return 0
  4064. fi
  4065. # Fallback: terminal bell for critical notifications
  4066. if [ "$urgency" = "critical" ]; then
  4067. printf '\a' # Bell character
  4068. fi
  4069. return 0
  4070. }
  4071. notify_intervention_needed() {
  4072. local reason="$1"
  4073. # Delegate-then-notify: this helper ONLY fires the (gated) desktop ping. It
  4074. # deliberately does NOT write the durable COMPLETION.txt / completion.json
  4075. # record. Reason: notify_intervention_needed is also called from NON-terminal
  4076. # sites (the perpetual-mode PAUSE auto-clear branch, uncertainty escalation)
  4077. # where the run keeps going. Writing a "Needs input" durable file there would
  4078. # falsely tell a detached user the run is done / blocked when it is not. The
  4079. # durable intervention write now lives only at the genuinely blocking pause
  4080. # sites (immediately before handle_pause), so the durable state matches the
  4081. # actual run state.
  4082. send_notification "Intervention Needed" "$reason" "critical"
  4083. }
  4084. notify_rate_limit() {
  4085. local wait_time="$1"
  4086. send_notification "Rate Limited" "Waiting ${wait_time}s before retry" "normal"
  4087. }
  4088. #===============================================================================
  4089. # Delegate-then-notify: completion summary (Release 2, "delegate then notify")
  4090. #
  4091. # build_completion_summary <outcome> writes two durable files that survive a
  4092. # detached (--bg) run where the terminal is gone and a bell would be useless:
  4093. # .loki/COMPLETION.txt human plain text (no emojis, no dashes)
  4094. # .loki/state/completion.json machine-readable record of the same facts
  4095. # It also exports two strings for send_notification to consume:
  4096. # _LOKI_SUMMARY_TITLE short notification subtitle
  4097. # _LOKI_SUMMARY_BODY short notification body (outcome + branch + file count)
  4098. #
  4099. # All git reads are best-effort and non-fatal. The diff window is the run-start
  4100. # SHA captured once at runner init (_LOKI_RUN_START_SHA); we REUSE it and never
  4101. # recapture, so the reported diff matches the evidence gate's window exactly.
  4102. #
  4103. # This function NEVER sends a notification and NEVER gates on
  4104. # NOTIFICATIONS_ENABLED: the files are state, not a notification, and must be
  4105. # written even when desktop notifications are disabled. emit_completion_summary
  4106. # below is the wrapper that writes the files AND (gated) fires the desktop ping.
  4107. #===============================================================================
  4108. # Read the gate escalation signal into one human line, or print nothing.
  4109. # Shared by COMPLETION.txt's per-outcome guidance and PAUSED.md so the two never
  4110. # disagree about why a run stopped. Best-effort: a missing or corrupt signal
  4111. # yields an empty string, and the caller degrades to generic guidance.
  4112. _loki_summary_gate_reason() {
  4113. local loki_dir="${1:-${TARGET_DIR:-.}/.loki}"
  4114. [ -s "$loki_dir/signals/GATE_ESCALATION.json" ] || return 0
  4115. _LOKI_GR_FILE="$loki_dir/signals/GATE_ESCALATION.json" python3 -c '
  4116. import json, os, sys
  4117. try:
  4118. d = json.load(open(os.environ["_LOKI_GR_FILE"]))
  4119. except Exception:
  4120. sys.exit(0)
  4121. if not isinstance(d, dict):
  4122. sys.exit(0)
  4123. gate = str(d.get("gate", "") or "").strip()
  4124. if not gate:
  4125. sys.exit(0)
  4126. count = d.get("count")
  4127. thr = d.get("threshold")
  4128. if isinstance(count, int) and isinstance(thr, int):
  4129. print("%s (failed %d times, threshold %d)" % (gate, count, thr))
  4130. else:
  4131. print(gate)
  4132. ' 2>/dev/null || true
  4133. }
  4134. # _loki_receipt_facts <loki_dir>
  4135. #
  4136. # Echo three tab-separated facts about THIS run's Evidence Receipt, or nothing
  4137. # at all: "<run_id>\t<proof_dir>\t<headline>".
  4138. #
  4139. # WHY THIS EXISTS (#209). The receipt is generated automatically and opt-OUT
  4140. # (LOKI_PROOF defaults to 1), but the only place its location ever reached a
  4141. # user was the TTFV first-run block, which fires solely on a zero-config first
  4142. # run. A user who does not already know `loki proof` exists could therefore
  4143. # finish a successful run without ever learning a checkable receipt was written.
  4144. #
  4145. # HONESTY RULES, deliberately fail-silent:
  4146. # - Resolved from the PERSISTED pointer (.loki/state/last-proof-id.txt),
  4147. # never newest-by-mtime, so it names the run the generator actually wrote.
  4148. # - Emits NOTHING unless the pointer, the proof dir, and proof.json all exist.
  4149. # the summary builder (build_completion_summary) is itself invoked mid-pause
  4150. # before the teardown receipt on the success path, where no receipt exists
  4151. # yet -- printing a path there would be a promise, not a fact.
  4152. # - The headline is honesty.headline, which the generator computes
  4153. # DETERMINISTICALLY from recorded facts (VERIFIED / VERIFIED WITH GAPS /
  4154. # NOT VERIFIED). It is never the council verdict, which the generator
  4155. # itself labels "AI judgment, not deterministic proof". A missing or
  4156. # unreadable headline yields an empty third field, and the callers then
  4157. # print the path without a verdict rather than inventing one.
  4158. # Best-effort: never fails a run.
  4159. _loki_receipt_facts() {
  4160. local loki_dir="${1:-}"
  4161. [ -n "$loki_dir" ] || return 0
  4162. local id_file="$loki_dir/state/last-proof-id.txt"
  4163. [ -s "$id_file" ] || return 0
  4164. local rid=""
  4165. rid="$(cat "$id_file" 2>/dev/null || true)"
  4166. # Confine the run id to the alphabet the generator actually mints. The
  4167. # headline is sanitized below; the id was not, so an interior newline or a
  4168. # tab in the pointer split the tab-separated record and handed the consumers
  4169. # a field boundary that is not there. Reject rather than repair: a pointer
  4170. # outside this alphabet is corrupt, and a repaired id would name a directory
  4171. # that does not exist.
  4172. case "$rid" in
  4173. ''|*[!A-Za-z0-9._-]*) return 0 ;;
  4174. esac
  4175. [ -n "$rid" ] || return 0
  4176. local pj="$loki_dir/proofs/$rid/proof.json"
  4177. [ -f "$pj" ] || return 0
  4178. local headline=""
  4179. headline="$(python3 -E -c "import sys; sys.path[:] = [p for p in sys.path if p not in ('', '.')]
  4180. import json, sys
  4181. try:
  4182. d = json.load(open(sys.argv[1]))
  4183. h = (d.get('honesty') or {}).get('headline') or ''
  4184. print(str(h).replace('\t', ' ').replace('\n', ' ').strip())
  4185. except Exception:
  4186. print('')" "$pj" 2>/dev/null || true)"
  4187. printf '%s\t%s\t%s\n' "$rid" "$loki_dir/proofs/$rid" "$headline"
  4188. return 0
  4189. }
  4190. build_completion_summary() {
  4191. local outcome="${1:-complete}"
  4192. local loki_dir="${TARGET_DIR:-.}/.loki"
  4193. mkdir -p "$loki_dir/state" 2>/dev/null || true
  4194. # Human-readable outcome label and notification title.
  4195. local outcome_label notify_title
  4196. case "$outcome" in
  4197. complete) outcome_label="Completed"; notify_title="Run complete" ;;
  4198. max_iterations) outcome_label="Max iterations"; notify_title="Run stopped (max iterations)" ;;
  4199. stopped) outcome_label="Stopped"; notify_title="Run stopped" ;;
  4200. failed) outcome_label="Failed"; notify_title="Run failed" ;;
  4201. intervention) outcome_label="Needs input"; notify_title="Input needed" ;;
  4202. # Every outcome below reached this case and fell through to the `*` arm,
  4203. # so the user's headline label was the raw enum string --
  4204. # "council_force_approved", "max_duration" -- at the exact moment they
  4205. # were deciding whether to trust the build. The guidance block further
  4206. # down already handles several of these properly; only the label was
  4207. # missing.
  4208. force_stopped) outcome_label="Stopped without approval"
  4209. notify_title="Run stopped (not approved)" ;;
  4210. budget_exceeded) outcome_label="Stopped at spend cap"
  4211. notify_title="Run stopped (budget cap)" ;;
  4212. max_duration) outcome_label="Time limit reached"
  4213. notify_title="Run stopped (time limit)" ;;
  4214. max_retries_exceeded) outcome_label="Retries exhausted"
  4215. notify_title="Run stopped (retries exhausted)" ;;
  4216. inconclusive_spec_contradiction) outcome_label="Spec contradiction"
  4217. notify_title="Run stopped (spec contradiction)" ;;
  4218. # Force-approval is NOT the same as council approval, and labelling both
  4219. # "Completed" hid the difference on a product whose whole claim is a
  4220. # checkable receipt. Name it.
  4221. council_force_approved) outcome_label="Completed (force-approved)"
  4222. notify_title="Run complete (force-approved)" ;;
  4223. # The three gate-stuck terminals. Spelled out one literal arm each
  4224. # rather than as a single wildcard pattern:
  4225. # tests/test-completion-outcome-labels.sh derives outcomes from the call
  4226. # sites and matches `^ *<outcome>\)`, so a wildcard arm renders correctly
  4227. # at runtime while reading as "no label arm" to the guard. Literal arms
  4228. # keep that guard strictly literal, which is what makes a NEWLY-added
  4229. # outcome fail there instead of shipping as a raw enum. Same reason the
  4230. # block above names each outcome.
  4231. gate_stuck_static_analysis) outcome_label="Stopped (static analysis gate would not clear)"
  4232. notify_title="Run stopped (gate not clearing)" ;;
  4233. gate_stuck_mock_integrity) outcome_label="Stopped (mock integrity gate would not clear)"
  4234. notify_title="Run stopped (gate not clearing)" ;;
  4235. gate_stuck_mutation_integrity) outcome_label="Stopped (mutation integrity gate would not clear)"
  4236. notify_title="Run stopped (gate not clearing)" ;;
  4237. *) outcome_label="$outcome"; notify_title="Run finished" ;;
  4238. esac
  4239. # Live app URL (best-effort): if the app runner has a running app, surface
  4240. # where the user can try it. Reads .loki/app-runner/state.json written by
  4241. # app-runner.sh. Empty when no app is running.
  4242. local live_app_url=""
  4243. local _app_state_file="$loki_dir/app-runner/state.json"
  4244. if [ -f "$_app_state_file" ]; then
  4245. live_app_url="$(python3 -c "import json,sys
  4246. try:
  4247. d=json.load(open(sys.argv[1]))
  4248. print(d.get('url','') if d.get('status')=='running' else '')
  4249. except Exception:
  4250. print('')" "$_app_state_file" 2>/dev/null)"
  4251. fi
  4252. # Time to first preview: how long until the user could SEE something running.
  4253. #
  4254. # WRITTEN BUT NEVER READ, until now. app-runner.sh:177 has recorded
  4255. # seconds_to_first_preview since v8, write-once and atomically, and NOTHING
  4256. # consumed it -- not this summary, not the receipt, not the dashboard. A
  4257. # measurement nobody surfaces cannot influence anything, so the number that
  4258. # research identifies as the delight peak of this whole category (a working
  4259. # preview at roughly four minutes) was invisible to the person who waited for
  4260. # it and to us.
  4261. #
  4262. # Read-only and best-effort by design: absent file means the app never
  4263. # previewed (or the run predates the writer), and we render nothing rather
  4264. # than a zero. A fabricated "0s to preview" would be worse than silence.
  4265. local first_preview_s=""
  4266. local _fp_file="$loki_dir/app-runner/first-preview.json"
  4267. if [ -f "$_fp_file" ]; then
  4268. first_preview_s="$(python3 -c "import json,sys
  4269. try:
  4270. v=json.load(open(sys.argv[1])).get('seconds_to_first_preview')
  4271. print(int(v) if isinstance(v,(int,float)) and v>=0 else '')
  4272. except Exception:
  4273. print('')" "$_fp_file" 2>/dev/null)"
  4274. fi
  4275. # Time to first CODE CHANGE. Companion to first_preview_s for the run that
  4276. # never previews anything, which is most of them: a scoped issue fix has no
  4277. # app to bring up, so the preview number is empty and the user had no signal
  4278. # at all until the run ended.
  4279. local first_artifact_s=""
  4280. local _fa_read="$loki_dir/state/first-artifact.json"
  4281. if [ -f "$_fa_read" ]; then
  4282. first_artifact_s="$(python3 -c "import json,sys
  4283. try:
  4284. v=json.load(open(sys.argv[1])).get('seconds_to_first_artifact')
  4285. print(int(v) if isinstance(v,(int,float)) and v>=0 else '')
  4286. except Exception:
  4287. print('')" "$_fa_read" 2>/dev/null)"
  4288. fi
  4289. # Where the time went, per stage. Same "written but never read" story as
  4290. # first-preview above: emit_stage_complete (run.sh:2413) has appended a
  4291. # stage_complete record -- stage, status, duration_s, iteration -- to
  4292. # events.jsonl since v7.91.x, and NOTHING consumed it. A user watching a
  4293. # 322-word issue take 25+ minutes inside iteration 1 had no way to see which
  4294. # step ate it, because the measurement existed and was never surfaced.
  4295. #
  4296. # Read-only aggregation over a file the run already wrote: no new subprocess
  4297. # per stage, no new writer, one python3 pass at terminal time. We deliberately
  4298. # render the AGENT remainder (wall clock minus summed stages) rather than
  4299. # stages alone. The 9 emit_stage_complete sites are all post-iteration gates,
  4300. # which sum to seconds; a table of only those would print "gates: 90s" on a
  4301. # 25-minute run and still not answer the question. The remainder is the
  4302. # provider/agent time, and it is usually the answer.
  4303. #
  4304. # Best-effort and honest about absence: no events file, no stage records, or
  4305. # unparseable lines render NOTHING rather than a fabricated zero -- same
  4306. # reasoning as first_preview_s. A wrong timing table is worse than silence.
  4307. # Rework split for the user-facing summary. Reuses the SAME
  4308. # iteration_attribution.py the prompt-side eval trend calls, so the number
  4309. # the user reads and the number the agent steers on cannot drift apart.
  4310. # Emits nothing unless the split is actually known.
  4311. local _LOKI_REWORK_LINE=""
  4312. if [ -r "${SCRIPT_DIR:-}/lib/iteration_attribution.py" ]; then
  4313. _LOKI_REWORK_LINE="$(python3 "${SCRIPT_DIR}/lib/iteration_attribution.py" \
  4314. --loki-dir "$loki_dir" --json 2>/dev/null | python3 -c "
  4315. import json, sys
  4316. try:
  4317. d = json.load(sys.stdin)
  4318. except Exception:
  4319. sys.exit(0)
  4320. prog, rew = d.get('progress', {}), d.get('rework', {})
  4321. pc, rc = prog.get('count', 0), rew.get('count', 0)
  4322. # Unknown-only runs render nothing: a 0/0 split is not evidence of no rework,
  4323. # it is absence of attribution, and printing it would read as a clean bill.
  4324. if pc + rc == 0:
  4325. sys.exit(0)
  4326. cost = rew.get('cost_usd', 0.0) or 0.0
  4327. line = 'Rework: %d of %d iteration(s) redid earlier work' % (rc, pc + rc)
  4328. if cost > 0:
  4329. line += ' (\$%.2f)' % cost
  4330. print(line)
  4331. " 2>/dev/null || true)"
  4332. fi
  4333. local stage_timing=""
  4334. local _ev_file="$loki_dir/events.jsonl"
  4335. if [ -f "$_ev_file" ]; then
  4336. stage_timing="$(LOKI_RUN_START_EPOCH="${_LOKI_RUN_START_EPOCH:-}" python3 -c "
  4337. import json, os, sys
  4338. tot = {}
  4339. order = []
  4340. first = last = None
  4341. # events.jsonl is NEVER truncated between runs (no rm/rotate anywhere in the
  4342. # tree), so a second 'loki start' in the same workspace would otherwise sum
  4343. # stages from every previous run against THIS run's wall clock -- inflating
  4344. # staged past wall and silently killing the total/remainder rows. Filter to
  4345. # records at or after this run's start. The ISO timestamp is already on every
  4346. # record, so this costs nothing extra.
  4347. run_start_iso = None
  4348. _es = (os.environ.get('LOKI_RUN_START_EPOCH') or '').strip()
  4349. if _es:
  4350. try:
  4351. from datetime import datetime, timezone
  4352. run_start_iso = datetime.fromtimestamp(float(_es), timezone.utc)
  4353. except Exception:
  4354. run_start_iso = None
  4355. def _in_run(ts):
  4356. if run_start_iso is None or not isinstance(ts, str) or not ts:
  4357. return True # no reliable boundary: keep (old behavior)
  4358. try:
  4359. from datetime import datetime
  4360. t = datetime.fromisoformat(ts.replace('Z', '+00:00'))
  4361. return t >= run_start_iso
  4362. except Exception:
  4363. return True
  4364. try:
  4365. with open(sys.argv[1], errors='replace') as fh:
  4366. for line in fh:
  4367. line = line.strip()
  4368. if not line:
  4369. continue
  4370. try:
  4371. rec = json.loads(line)
  4372. except Exception:
  4373. continue # malformed line: skip, never abort the summary
  4374. if not isinstance(rec, dict):
  4375. continue
  4376. ts = rec.get('timestamp')
  4377. if not _in_run(ts):
  4378. continue # record belongs to an earlier run in this workspace
  4379. if isinstance(ts, str) and ts:
  4380. if first is None:
  4381. first = ts
  4382. last = ts
  4383. if rec.get('type') != 'stage_complete':
  4384. continue
  4385. d = rec.get('data') or {}
  4386. if not isinstance(d, dict):
  4387. continue
  4388. name = d.get('stage')
  4389. dur = d.get('duration_s')
  4390. if not name or not isinstance(dur, (int, float)) or dur < 0:
  4391. continue
  4392. if name not in tot:
  4393. tot[name] = 0.0
  4394. order.append(name)
  4395. tot[name] += float(dur)
  4396. except Exception:
  4397. sys.exit(0)
  4398. if not tot:
  4399. sys.exit(0)
  4400. def human(s):
  4401. s = int(round(s))
  4402. return '%dm %02ds' % (s // 60, s % 60) if s >= 60 else '%ds' % s
  4403. # Wall clock: prefer the run-start epoch the runner exported; else derive from
  4404. # the first/last event timestamps. Absent both, we print stages with no total
  4405. # rather than inventing a denominator.
  4406. wall = None
  4407. env_start = os.environ.get('LOKI_RUN_START_EPOCH') or ''
  4408. try:
  4409. if env_start.strip():
  4410. import time
  4411. wall = time.time() - float(env_start)
  4412. except Exception:
  4413. wall = None
  4414. if wall is None and first and last:
  4415. try:
  4416. from datetime import datetime
  4417. f = datetime.fromisoformat(first.replace('Z', '+00:00'))
  4418. l = datetime.fromisoformat(last.replace('Z', '+00:00'))
  4419. wall = (l - f).total_seconds()
  4420. except Exception:
  4421. wall = None
  4422. staged = sum(tot.values())
  4423. out = []
  4424. for name in sorted(order, key=lambda n: -tot[n]):
  4425. out.append(' %-22s %s' % (name.replace('_', ' '), human(tot[name])))
  4426. if wall is not None and wall >= staged:
  4427. rem = wall - staged
  4428. # The bucket that answers 'where did the 25 minutes go'.
  4429. # NOT labeled 'agent': the provider call is itself a bracketed stage above,
  4430. # so this remainder is everything else (checklist verification, app runner,
  4431. # playwright, council, memory). Calling it 'agent' would print two different
  4432. # measurements under one name.
  4433. out.append(' %-22s %s' % ('other (unaccounted)', human(rem)))
  4434. out.append(' %-22s %s' % ('total', human(wall)))
  4435. print('\n'.join(out))
  4436. " "$_ev_file" 2>/dev/null)"
  4437. fi
  4438. # Branch + diff stats vs the run-start SHA (best-effort; non-git or empty
  4439. # baseline yields empty values, which we render as "unknown"/"0").
  4440. local start_sha="${_LOKI_RUN_START_SHA:-}"
  4441. local branch="" head_sha="" diff_stat="" files_changed=0 insertions=0 deletions=0 review_cmd=""
  4442. branch="$( (cd "${TARGET_DIR:-.}" && git rev-parse --abbrev-ref HEAD) 2>/dev/null || true )"
  4443. [ -z "$branch" ] && branch="unknown"
  4444. head_sha="$( (cd "${TARGET_DIR:-.}" && git rev-parse HEAD) 2>/dev/null || true )"
  4445. # Finding #596 (HIGH): exclude .loki/ and .git/ from the summary diff/stat and
  4446. # from the "Review the work" command we print, so the user is never told to
  4447. # review a .loki-bloated diff and the displayed counts match the gated diff.
  4448. local _summary_pathspec=(-- . ':(exclude).loki/' ':(exclude).git/' ':(exclude)**/.loki/**')
  4449. if [ -n "$start_sha" ]; then
  4450. diff_stat="$( (cd "${TARGET_DIR:-.}" && git diff --stat "${start_sha}..HEAD" "${_summary_pathspec[@]}") 2>/dev/null || true )"
  4451. # Parse the git diff --shortstat tail for counts (locale-stable enough
  4452. # for our display; failures leave the zeros in place).
  4453. local shortstat
  4454. shortstat="$( (cd "${TARGET_DIR:-.}" && git diff --shortstat "${start_sha}..HEAD" "${_summary_pathspec[@]}") 2>/dev/null || true )"
  4455. if [ -n "$shortstat" ]; then
  4456. files_changed="$(printf '%s\n' "$shortstat" | grep -oE '[0-9]+ file' | grep -oE '[0-9]+' | head -1)"
  4457. insertions="$(printf '%s\n' "$shortstat" | grep -oE '[0-9]+ insertion' | grep -oE '[0-9]+' | head -1)"
  4458. deletions="$(printf '%s\n' "$shortstat" | grep -oE '[0-9]+ deletion' | grep -oE '[0-9]+' | head -1)"
  4459. fi
  4460. review_cmd="git diff ${start_sha}..HEAD -- . ':(exclude).loki/'"
  4461. else
  4462. # No baseline SHA. This is the GREENFIELD case: the run started in a repo
  4463. # with no commits, so `git rev-parse --verify HEAD` correctly produced
  4464. # nothing (see the start-sha capture). Everything the run built is new.
  4465. #
  4466. # Without this branch the counts stayed at zero and a user was told
  4467. # "files_changed: 0" about a run that had produced real, committed,
  4468. # working code -- observed on a PRD benchmark that built 4 files and
  4469. # passed 28/28 of its own tests, yet reported 0. Diff against the empty
  4470. # tree so the summary reports what was actually created.
  4471. local _empty_tree
  4472. _empty_tree="$( (cd "${TARGET_DIR:-.}" && git hash-object -t tree /dev/null) 2>/dev/null || true )"
  4473. if [ -n "$_empty_tree" ] && (cd "${TARGET_DIR:-.}" && git rev-parse --verify HEAD >/dev/null 2>&1); then
  4474. diff_stat="$( (cd "${TARGET_DIR:-.}" && git diff --stat "${_empty_tree}..HEAD" "${_summary_pathspec[@]}") 2>/dev/null || true )"
  4475. local _shortstat_new
  4476. _shortstat_new="$( (cd "${TARGET_DIR:-.}" && git diff --shortstat "${_empty_tree}..HEAD" "${_summary_pathspec[@]}") 2>/dev/null || true )"
  4477. if [ -n "$_shortstat_new" ]; then
  4478. files_changed="$(printf '%s\n' "$_shortstat_new" | grep -oE '[0-9]+ file' | grep -oE '[0-9]+' | head -1)"
  4479. insertions="$(printf '%s\n' "$_shortstat_new" | grep -oE '[0-9]+ insertion' | grep -oE '[0-9]+' | head -1)"
  4480. deletions="$(printf '%s\n' "$_shortstat_new" | grep -oE '[0-9]+ deletion' | grep -oE '[0-9]+' | head -1)"
  4481. fi
  4482. review_cmd="git diff ${_empty_tree}..HEAD -- . ':(exclude).loki/'"
  4483. else
  4484. review_cmd="git diff HEAD -- . ':(exclude).loki/'"
  4485. fi
  4486. fi
  4487. [ -z "$files_changed" ] && files_changed=0
  4488. [ -z "$insertions" ] && insertions=0
  4489. [ -z "$deletions" ] && deletions=0
  4490. # Task counts: reuse the SAME queue reads as update_status_file.
  4491. local pending=0 in_progress=0 completed=0 failed=0
  4492. [ -f "$loki_dir/queue/pending.json" ] && pending=$(python3 -c "import json; print(len(json.load(open('$loki_dir/queue/pending.json'))))" 2>/dev/null || echo "0")
  4493. [ -f "$loki_dir/queue/in-progress.json" ] && in_progress=$(python3 -c "import json; print(len(json.load(open('$loki_dir/queue/in-progress.json'))))" 2>/dev/null || echo "0")
  4494. [ -f "$loki_dir/queue/completed.json" ] && completed=$(python3 -c "import json; print(len(json.load(open('$loki_dir/queue/completed.json'))))" 2>/dev/null || echo "0")
  4495. [ -f "$loki_dir/queue/failed.json" ] && failed=$(python3 -c "import json; print(len(json.load(open('$loki_dir/queue/failed.json'))))" 2>/dev/null || echo "0")
  4496. # Optional delegate-mode extras populated by Slice 3 (branch isolation / PR).
  4497. local delegate_branch="${_LOKI_DELEGATE_BRANCH_NAME:-}"
  4498. local pr_url="${_LOKI_DELEGATE_PR_URL:-}"
  4499. local ts
  4500. ts="$(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date)"
  4501. # v7.28.0: evidence-gate inconclusive line. When the evidence gate could not
  4502. # establish a diff baseline (no git repo, or no run-start SHA), it records a
  4503. # durable .loki/state/evidence-inconclusive.json instead of silently passing.
  4504. # Surface one honest line so the user knows completion was not independently
  4505. # verified. The record is removed by the gate on any conclusive run.
  4506. local evidence_inconclusive_line=""
  4507. local _inc_file="$loki_dir/state/evidence-inconclusive.json"
  4508. if [ -f "$_inc_file" ]; then
  4509. local _inc_reason
  4510. _inc_reason="$(python3 -c "import json,sys
  4511. try:
  4512. d=json.load(open(sys.argv[1]))
  4513. print(d.get('reason','') if d.get('inconclusive') else '')
  4514. except Exception:
  4515. print('')" "$_inc_file" 2>/dev/null)"
  4516. if [ -n "$_inc_reason" ]; then
  4517. evidence_inconclusive_line="Evidence gate: inconclusive (${_inc_reason}) - completion not independently verified"
  4518. fi
  4519. fi
  4520. # P2-2: assumption-ledger summary for proof-of-done. "done" means "done, plus
  4521. # here are the N places your spec was ambiguous and what Loki assumed."
  4522. # spec_ledger_counts echoes "<total> <high>"; defined when spec-interrogation.sh
  4523. # is sourced (run.sh sources it in DISCOVERY). Guarded for safety.
  4524. local assumptions_total=0 assumptions_high=0 assumption_lines=""
  4525. if type spec_ledger_counts &>/dev/null; then
  4526. local _ac
  4527. _ac="$(spec_ledger_counts 2>/dev/null || echo '0 0')"
  4528. assumptions_total="${_ac%% *}"
  4529. assumptions_high="${_ac##* }"
  4530. case "$assumptions_total" in ''|*[!0-9]*) assumptions_total=0 ;; esac
  4531. case "$assumptions_high" in ''|*[!0-9]*) assumptions_high=0 ;; esac
  4532. if [ "$assumptions_total" != "0" ]; then
  4533. local _ledger_md="$loki_dir/assumptions/ledger.md"
  4534. if [ -f "$_ledger_md" ]; then
  4535. # Pull just the "## <id> ..." headings as a terse one-per-line list.
  4536. assumption_lines="$(grep '^## ' "$_ledger_md" 2>/dev/null | sed 's/^## / - /' || true)"
  4537. fi
  4538. fi
  4539. fi
  4540. # ---- Durable human-readable file: .loki/COMPLETION.txt --------------------
  4541. # Presentation-only receipt: a single fixed-width label column, the live-app
  4542. # URL elevated right under the outcome headline, and a consistent rule line.
  4543. # Stays pure ASCII (no emoji, no dashes, no color codes) so it pastes cleanly
  4544. # into a PR description or a chat. Same facts and values as before.
  4545. {
  4546. echo "Loki Mode run summary"
  4547. echo "====================="
  4548. echo ""
  4549. printf '%-14s %s\n' "Outcome:" "$outcome_label"
  4550. if [ -n "$live_app_url" ]; then
  4551. # Compute the dashboard scheme the same way start_dashboard does
  4552. # (url_scheme is local to that function, not visible here).
  4553. local _dash_scheme="http"
  4554. [ -n "${LOKI_TLS_CERT:-}" ] && [ -n "${LOKI_TLS_KEY:-}" ] && _dash_scheme="https"
  4555. printf '%-14s %s\n' "Live app:" "$live_app_url (served locally on this machine)"
  4556. printf '%-14s %s\n' "Dashboard:" "${_dash_scheme}://127.0.0.1:${DASHBOARD_PORT:-57374}/ (App Runner -> Live App)"
  4557. fi
  4558. printf '%-14s %s\n' "Branch:" "$branch"
  4559. printf '%-14s %s\n' "Files:" "$files_changed (+$insertions / -$deletions)"
  4560. # Only rendered when the app actually previewed. Silence beats a
  4561. # fabricated zero for a run where nothing ever came up.
  4562. if [ -n "$first_preview_s" ]; then
  4563. printf '%-14s %ss\n' "First preview:" "$first_preview_s"
  4564. fi
  4565. # First code change, for the (far more common) run with no preview at
  4566. # all -- a scoped issue fix produces no running app, so first_preview_s
  4567. # is empty and the user had no signal of any kind until the end. Same
  4568. # discipline: rendered only when actually recorded, never fabricated.
  4569. if [ -n "$first_artifact_s" ]; then
  4570. printf '%-14s %ss\n' "First change:" "$first_artifact_s"
  4571. fi
  4572. printf '%-14s %s\n' "Finished:" "$ts"
  4573. if [ -n "$delegate_branch" ]; then
  4574. printf '%-14s %s\n' "Delegate:" "$delegate_branch"
  4575. fi
  4576. if [ -n "$pr_url" ]; then
  4577. printf '%-14s %s\n' "Pull request:" "$pr_url"
  4578. elif [ "$outcome" = "complete" ]; then
  4579. printf '%-14s %s\n' "Pull request:" "not opened (no GitHub remote, gh not authed, or on a default branch)"
  4580. fi
  4581. printf '%-14s %s\n' "Tasks:" "pending=$pending in_progress=$in_progress completed=$completed failed=$failed"
  4582. echo ""
  4583. if [ -n "$stage_timing" ]; then
  4584. echo "Where the time went:"
  4585. echo "$stage_timing"
  4586. echo ""
  4587. fi
  4588. # Rework attribution. The AGENT has been steering on this since the eval
  4589. # trend shipped, but the user never saw it -- and it is the number that
  4590. # answers "why did this cost so much".
  4591. #
  4592. # iterations alone cannot distinguish real work from a gate false
  4593. # positive that forced redos. That is not hypothetical: a measured run
  4594. # had the agent claim done on EVERY iteration while a mock-integrity
  4595. # false positive blocked all six, so the run looked like six iterations
  4596. # of work and was one iteration of work plus five of harness bug.
  4597. #
  4598. # Renders NOTHING when the split is unknown, rather than a fabricated
  4599. # zero -- same rule as the timing table above. A wrong attribution is
  4600. # worse than silence because it points the user at the wrong culprit.
  4601. if [ -n "${_LOKI_REWORK_LINE:-}" ]; then
  4602. echo "$_LOKI_REWORK_LINE"
  4603. echo ""
  4604. fi
  4605. # What to DO about it. A terminal that names a bound without naming the
  4606. # lever leaves the user to guess which knob applies, and the wrong guess
  4607. # is expensive: raising an iteration cap on a run that was thrashing
  4608. # buys more thrashing at full price.
  4609. #
  4610. # The rework split above already distinguishes the two cases, so this
  4611. # points at the lever that actually matches the outcome rather than
  4612. # listing every environment variable.
  4613. case "$outcome" in
  4614. max_iterations)
  4615. echo "Next: this run hit its iteration ceiling, not a verdict."
  4616. echo " Converging (low rework above)? Raise it: LOKI_MAX_ITERATIONS=<n>"
  4617. echo " Thrashing (high rework above)? Raising the cap buys more"
  4618. echo " of the same. Read the last reviewer findings first:"
  4619. echo " loki why"
  4620. echo ""
  4621. ;;
  4622. budget_exceeded)
  4623. echo "Next: the spend cap stopped this run, not a failed gate."
  4624. echo " Raise it with LOKI_BUDGET_LIMIT=<usd>, or check the cache"
  4625. echo " hit ratio first -- a cold cache is the usual cause of a"
  4626. echo " surprising bill:"
  4627. echo " loki memory economics"
  4628. echo ""
  4629. ;;
  4630. force_stopped)
  4631. echo "Next: the council stopped this run WITHOUT approving it."
  4632. echo " The work is not verified complete. See what blocked it:"
  4633. echo " loki why"
  4634. echo ""
  4635. ;;
  4636. esac
  4637. if [ -n "$evidence_inconclusive_line" ]; then
  4638. echo "$evidence_inconclusive_line"
  4639. echo ""
  4640. fi
  4641. if [ "$assumptions_total" != "0" ]; then
  4642. echo "Spec assumptions recorded: $assumptions_total ($assumptions_high high-severity)"
  4643. echo " These are places your spec was ambiguous and what Loki assumed. See .loki/assumptions/ledger.md"
  4644. if [ -n "$assumption_lines" ]; then
  4645. echo "$assumption_lines"
  4646. fi
  4647. echo ""
  4648. fi
  4649. echo "Review the work:"
  4650. echo " $review_cmd"
  4651. echo ""
  4652. if [ -n "$diff_stat" ]; then
  4653. echo "Diff stat:"
  4654. echo "$diff_stat"
  4655. else
  4656. echo "Diff stat: (no changes detected vs run start, or git unavailable)"
  4657. fi
  4658. # Empty-diff remedy (mirrors emit_completion_summary's on-screen line) so
  4659. # a --bg or dashboard user reading COMPLETION.txt gets the same honest,
  4660. # actionable guidance as a foreground user. Never implies success.
  4661. case "$files_changed" in
  4662. ''|0)
  4663. case "$outcome" in
  4664. complete|max_iterations)
  4665. echo ""
  4666. echo "No file changes were produced this run. Likely the spec was too vague"
  4667. echo "or already satisfied. Next: re-run with a more concrete spec, e.g."
  4668. echo " loki start \"<one concrete, testable change you want>\""
  4669. echo "or inspect what happened: loki why"
  4670. ;;
  4671. esac
  4672. ;;
  4673. esac
  4674. # Per-outcome next step. Deliberately NOT nested inside the
  4675. # files_changed==0 case above: a run that stops for a gate reason has
  4676. # usually built plenty, and that is exactly the run whose user most needs
  4677. # to be told what to do. Keying this on an empty diff would silence it
  4678. # for the case that motivated it.
  4679. #
  4680. # Measured on the PRD benchmark: outcome=intervention with 9 files built
  4681. # and 28/28 tests passing produced NO guidance at all, because the old
  4682. # block covered only complete|max_iterations.
  4683. case "$outcome" in
  4684. intervention)
  4685. echo ""
  4686. echo "Loki stopped and needs a decision from you."
  4687. _cs_gate="$(_loki_summary_gate_reason "$loki_dir")"
  4688. if [ -n "$_cs_gate" ]; then
  4689. echo " Blocked by: $_cs_gate"
  4690. echo " The findings say exactly what to change. Fix those, then resume:"
  4691. else
  4692. echo " See .loki/PAUSED.md for the reason and .loki/CONTINUITY.md for context."
  4693. echo " Resume with:"
  4694. fi
  4695. echo " rm .loki/PAUSE # resume this run"
  4696. echo " touch .loki/STOP # end it instead"
  4697. echo " Resuming without addressing the finding will stop at the same gate again."
  4698. ;;
  4699. failed)
  4700. echo ""
  4701. echo "The run failed. What went wrong:"
  4702. echo " loki why # the classified cause, in one line"
  4703. echo " .loki/logs/ # the full session log"
  4704. echo "Anything already committed is listed above and is yours to keep or discard."
  4705. ;;
  4706. stopped|force_stopped)
  4707. echo ""
  4708. echo "The run was stopped before it finished."
  4709. echo "Work committed up to that point is listed above. To continue from here:"
  4710. echo " loki start <your spec> # a fresh run over the current tree"
  4711. ;;
  4712. inconclusive_spec_contradiction)
  4713. echo ""
  4714. echo "Loki could not reconcile parts of the spec, so it stopped rather than"
  4715. echo "guess. The specific contradictions are in .loki/assumptions/ledger.md."
  4716. echo "Resolve those in the spec, then re-run."
  4717. ;;
  4718. esac
  4719. # Evidence Receipt (#209). The receipt is written automatically on every
  4720. # run, success or failure, but its location only ever reached the user
  4721. # through the zero-config first-run block. State it here, where every
  4722. # terminal outcome passes, so a user who has never heard of `loki proof`
  4723. # still learns a checkable receipt exists and how to re-check it.
  4724. # Prints NOTHING when no receipt has been written yet (mid-pause, or the
  4725. # first summary on the success path): a path that does not exist is a
  4726. # promise, not a fact. The verdict line is the generator's deterministic
  4727. # headline, omitted entirely when unreadable rather than invented.
  4728. _cs_receipt="$(_loki_receipt_facts "$loki_dir" 2>/dev/null || true)"
  4729. if [ -n "$_cs_receipt" ]; then
  4730. _cs_rid="${_cs_receipt%% *}"
  4731. _cs_rest="${_cs_receipt#* }"
  4732. _cs_dir="${_cs_rest%% *}"
  4733. _cs_headline="${_cs_rest#* }"
  4734. echo ""
  4735. echo "Evidence Receipt (this run):"
  4736. if [ -n "$_cs_headline" ]; then
  4737. echo " Verdict: $_cs_headline"
  4738. fi
  4739. # Gated on the PAGE, not the receipt. The helper gates on
  4740. # proof.json, but the generator writes proof.json and THEN renders
  4741. # index.html unwrapped (proof-generator.py), so a render failure
  4742. # leaves the data present and the page absent. Naming a page that
  4743. # is not there is the same class proof.ts:118 refuses ("worse than
  4744. # an absent one") and `loki proof open` refuses with "Proof page
  4745. # not found". Gate only this line: proof verify reads proof.json,
  4746. # so the id and the re-check below still work without the page.
  4747. if [ -f "$_cs_dir/index.html" ]; then
  4748. echo " Receipt: $_cs_dir/index.html"
  4749. fi
  4750. echo " Re-check it yourself, do not take our word for it:"
  4751. # Cwd-independent: the receipt is resolved from the run's target
  4752. # dir, which need not be where the user is standing. A bare
  4753. # `loki proof verify <id>` then fails for exactly the user we just
  4754. # told to check our work.
  4755. if [ "$(cd "$loki_dir/.." 2>/dev/null && pwd -P)" = "$(pwd -P)" ]; then
  4756. echo " loki proof verify $_cs_rid"
  4757. else
  4758. echo " (cd $(cd "$loki_dir/.." 2>/dev/null && pwd -P) && loki proof verify $_cs_rid)"
  4759. fi
  4760. fi
  4761. } > "$loki_dir/COMPLETION.txt" 2>/dev/null || true
  4762. # ---- Durable machine-readable file: .loki/state/completion.json -----------
  4763. _LOKI_CS_OUTCOME="$outcome" \
  4764. _LOKI_CS_BRANCH="$branch" \
  4765. _LOKI_CS_START_SHA="$start_sha" \
  4766. _LOKI_CS_HEAD_SHA="$head_sha" \
  4767. _LOKI_CS_FILES="$files_changed" \
  4768. _LOKI_CS_INS="$insertions" \
  4769. _LOKI_CS_DEL="$deletions" \
  4770. _LOKI_CS_REVIEW="$review_cmd" \
  4771. _LOKI_CS_DELEGATE_BRANCH="$delegate_branch" \
  4772. _LOKI_CS_PR_URL="$pr_url" \
  4773. _LOKI_CS_TS="$ts" \
  4774. _LOKI_CS_ASSUMPTIONS_TOTAL="$assumptions_total" \
  4775. _LOKI_CS_ASSUMPTIONS_HIGH="$assumptions_high" \
  4776. _LOKI_CS_OUT_FILE="$loki_dir/state/completion.json" \
  4777. _LOKI_CS_LAST_ERROR="$loki_dir/state/LAST_ERROR.json" \
  4778. python3 -E -c "import sys; sys.path[:] = [p for p in sys.path if p not in ('', '.')]
  4779. import json, os, tempfile
  4780. out = os.environ['_LOKI_CS_OUT_FILE']
  4781. def i(v):
  4782. try: return int(v)
  4783. except (TypeError, ValueError): return 0
  4784. # Carry the CLASSIFIED failure reason (error_class + brief) into the machine-
  4785. # readable completion record, from the side-record loki wrote before this summary
  4786. # (.loki/state/LAST_ERROR.json). Without it a terminal outcome like
  4787. # 'inconclusive_spec_contradiction' is only a label with no cause a downstream
  4788. # (operator, next run, bench) can act on. None on a clean run.
  4789. _err_class = None
  4790. _err_brief = None
  4791. try:
  4792. with open(os.environ.get('_LOKI_CS_LAST_ERROR', '')) as _ef:
  4793. _er = json.load(_ef)
  4794. if isinstance(_er, dict):
  4795. _err_class = _er.get('error_class')
  4796. _err_brief = _er.get('brief')
  4797. except Exception:
  4798. pass
  4799. rec = {
  4800. 'outcome': os.environ.get('_LOKI_CS_OUTCOME', ''),
  4801. 'error_class': _err_class,
  4802. 'error_brief': _err_brief,
  4803. 'branch': os.environ.get('_LOKI_CS_BRANCH', ''),
  4804. 'start_sha': os.environ.get('_LOKI_CS_START_SHA', ''),
  4805. 'head_sha': os.environ.get('_LOKI_CS_HEAD_SHA', ''),
  4806. 'files_changed': i(os.environ.get('_LOKI_CS_FILES')),
  4807. 'insertions': i(os.environ.get('_LOKI_CS_INS')),
  4808. 'deletions': i(os.environ.get('_LOKI_CS_DEL')),
  4809. 'review_cmd': os.environ.get('_LOKI_CS_REVIEW', ''),
  4810. 'delegate_branch': os.environ.get('_LOKI_CS_DELEGATE_BRANCH', ''),
  4811. 'pr_url': os.environ.get('_LOKI_CS_PR_URL', ''),
  4812. 'timestamp': os.environ.get('_LOKI_CS_TS', ''),
  4813. 'assumptions_total': i(os.environ.get('_LOKI_CS_ASSUMPTIONS_TOTAL')),
  4814. 'assumptions_high': i(os.environ.get('_LOKI_CS_ASSUMPTIONS_HIGH')),
  4815. }
  4816. d = os.path.dirname(out)
  4817. fd, tmp = tempfile.mkstemp(dir=d, suffix='.json')
  4818. with os.fdopen(fd, 'w') as f:
  4819. json.dump(rec, f, indent=2)
  4820. os.replace(tmp, out)
  4821. " 2>/dev/null || true
  4822. # ---- P3-5: run manifest / bill-of-materials: .loki/loki-run.json ----------
  4823. # Best-effort, auditable + reproducible record of this run. Emitted from
  4824. # build_completion_summary because that fires on EVERY terminal path
  4825. # (complete / max_iterations / stopped / intervention / failed), including the
  4826. # intervention/stopped calls that bypass emit_completion_summary. The whole
  4827. # block is wrapped so a failure can NEVER abort the run.
  4828. {
  4829. # Portable sha256 (macOS shasum, Linux sha256sum). Echoes empty on miss.
  4830. _loki_sha256() {
  4831. local _f="$1"
  4832. [ -f "$_f" ] || { printf ''; return 0; }
  4833. if command -v shasum >/dev/null 2>&1; then
  4834. shasum -a 256 "$_f" 2>/dev/null | awk '{print $1}'
  4835. elif command -v sha256sum >/dev/null 2>&1; then
  4836. sha256sum "$_f" 2>/dev/null | awk '{print $1}'
  4837. else
  4838. printf ''
  4839. fi
  4840. }
  4841. local _loki_ver
  4842. _loki_ver="$(cat "$PROJECT_DIR/VERSION" 2>/dev/null || echo "unknown")"
  4843. local _spec_path="${PRD_PATH:-}"
  4844. [ -z "$_spec_path" ] && _spec_path="none"
  4845. local _spec_hash=""
  4846. [ "$_spec_path" != "none" ] && _spec_hash="$(_loki_sha256 "$_spec_path")"
  4847. # Evidence files we reference + hash (existing artifacts, not new ones).
  4848. local _ev_tests="$loki_dir/quality/test-results.json"
  4849. local _ev_cov="$loki_dir/quality/coverage.json"
  4850. local _ev_completion="$loki_dir/state/completion.json"
  4851. _LOKI_RM_OUT="$loki_dir/loki-run.json" \
  4852. _LOKI_RM_VERSION="$_loki_ver" \
  4853. _LOKI_RM_SPEC_PATH="$_spec_path" \
  4854. _LOKI_RM_SPEC_HASH="$_spec_hash" \
  4855. _LOKI_RM_PROVIDER="${PROVIDER_NAME:-${LOKI_PROVIDER:-claude}}" \
  4856. _LOKI_RM_TIER="${CURRENT_TIER:-unknown}" \
  4857. _LOKI_RM_OUTCOME="$outcome" \
  4858. _LOKI_RM_BRANCH="$branch" \
  4859. _LOKI_RM_START_SHA="$start_sha" \
  4860. _LOKI_RM_HEAD_SHA="$head_sha" \
  4861. _LOKI_RM_ITERS="${ITERATION_COUNT:-0}" \
  4862. _LOKI_RM_TS="$ts" \
  4863. _LOKI_RM_EV_TESTS="$_ev_tests" \
  4864. _LOKI_RM_EV_TESTS_HASH="$(_loki_sha256 "$_ev_tests")" \
  4865. _LOKI_RM_EV_COV="$_ev_cov" \
  4866. _LOKI_RM_EV_COV_HASH="$(_loki_sha256 "$_ev_cov")" \
  4867. _LOKI_RM_EV_COMPLETION="$_ev_completion" \
  4868. _LOKI_RM_NODE="$(node --version 2>/dev/null || echo '')" \
  4869. _LOKI_RM_PYTHON="$(python3 --version 2>&1 | awk '{print $2}' || echo '')" \
  4870. _LOKI_RM_GIT="$(git --version 2>/dev/null | awk '{print $3}' || echo '')" \
  4871. _LOKI_RM_BUN="$(bun --version 2>/dev/null || echo '')" \
  4872. python3 -c "
  4873. import json, os, tempfile
  4874. out=os.environ['_LOKI_RM_OUT']
  4875. def s(k): return os.environ.get(k,'')
  4876. def i(k):
  4877. try: return int(os.environ.get(k,'0'))
  4878. except (TypeError, ValueError): return 0
  4879. def ev(path_k, hash_k=None):
  4880. p=s(path_k)
  4881. rec={'path': p, 'exists': bool(p) and os.path.isfile(p)}
  4882. if hash_k:
  4883. h=s(hash_k)
  4884. if h: rec['sha256']=h
  4885. return rec
  4886. manifest={
  4887. 'schema': 'loki-run-manifest/v1',
  4888. 'loki_version': s('_LOKI_RM_VERSION'),
  4889. 'timestamp': s('_LOKI_RM_TS'),
  4890. 'outcome': s('_LOKI_RM_OUTCOME'),
  4891. 'iterations': i('_LOKI_RM_ITERS'),
  4892. 'provider': s('_LOKI_RM_PROVIDER'),
  4893. # Tier cycles per RARV iteration (R/A/R/V); this is the LAST tier set, not
  4894. # the only tier used. Recorded honestly as last_tier, not a single 'model'.
  4895. 'last_tier': s('_LOKI_RM_TIER'),
  4896. 'spec': {
  4897. 'path': s('_LOKI_RM_SPEC_PATH'),
  4898. 'sha256': s('_LOKI_RM_SPEC_HASH') or None,
  4899. },
  4900. 'git': {
  4901. 'branch': s('_LOKI_RM_BRANCH'),
  4902. 'start_sha': s('_LOKI_RM_START_SHA') or None,
  4903. 'head_sha': s('_LOKI_RM_HEAD_SHA') or None,
  4904. },
  4905. 'tool_versions': {
  4906. 'node': s('_LOKI_RM_NODE') or None,
  4907. 'python': s('_LOKI_RM_PYTHON') or None,
  4908. 'git': s('_LOKI_RM_GIT') or None,
  4909. 'bun': s('_LOKI_RM_BUN') or None,
  4910. },
  4911. 'evidence': {
  4912. 'test_results': ev('_LOKI_RM_EV_TESTS','_LOKI_RM_EV_TESTS_HASH'),
  4913. 'coverage': ev('_LOKI_RM_EV_COV','_LOKI_RM_EV_COV_HASH'),
  4914. 'completion': ev('_LOKI_RM_EV_COMPLETION'),
  4915. },
  4916. }
  4917. d=os.path.dirname(out)
  4918. fd, tmp=tempfile.mkstemp(dir=d, suffix='.json')
  4919. with os.fdopen(fd,'w') as f:
  4920. json.dump(manifest, f, indent=2)
  4921. os.replace(tmp, out)
  4922. " 2>/dev/null || true
  4923. unset -f _loki_sha256 2>/dev/null || true
  4924. } || true
  4925. # ---- Short strings for the desktop notification --------------------------
  4926. # Desktop body stays terse; full detail lives in COMPLETION.txt.
  4927. _LOKI_SUMMARY_TITLE="$notify_title"
  4928. _LOKI_SUMMARY_BODY="${outcome_label} on ${branch}: ${files_changed} files changed"
  4929. if [ -n "$pr_url" ]; then
  4930. _LOKI_SUMMARY_BODY="${_LOKI_SUMMARY_BODY}. PR: ${pr_url}"
  4931. fi
  4932. export _LOKI_SUMMARY_TITLE _LOKI_SUMMARY_BODY
  4933. return 0
  4934. }
  4935. #===============================================================================
  4936. # emit_completion_summary <outcome> [urgency]
  4937. #
  4938. # The single entry point every terminal state calls. It ALWAYS writes the
  4939. # durable summary files (state, not a notification) and then fires ONE desktop
  4940. # notification gated by the existing LOKI_NOTIFICATIONS flag (send_notification
  4941. # already short-circuits when disabled, so the gate is implicit but explicit
  4942. # here for clarity). Centralizing this keeps the success-only PR side effect
  4943. # (Slice 3) in one place and prevents duplicate notifications.
  4944. #===============================================================================
  4945. emit_completion_summary() {
  4946. local outcome="${1:-complete}"
  4947. local urgency="${2:-normal}"
  4948. build_completion_summary "$outcome"
  4949. # Render the screenshot-worthy completion card inline on a foreground TTY run,
  4950. # AFTER the durable files are written. The card re-reads the persisted
  4951. # completion.json (never recomputes) so it can never diverge from the file.
  4952. print_completion_card
  4953. send_notification "${_LOKI_SUMMARY_TITLE:-Run finished}" "${_LOKI_SUMMARY_BODY:-}" "$urgency"
  4954. return 0
  4955. }
  4956. #===============================================================================
  4957. # print_completion_card (visible-delight completion card)
  4958. #
  4959. # Display-only. Renders a boxed summary card to the interactive TTY at the close
  4960. # of a foreground run: outcome, branch, files changed (+ins/-del), the live-app
  4961. # URL with a "try it" line when an app is running, the copy-pasteable review
  4962. # command, and the recorded-assumptions count. This is the single most
  4963. # screenshot-worthy moment of a run, which was previously only written to a file.
  4964. #
  4965. # It reads ONLY from the already-persisted .loki/state/completion.json (written
  4966. # by build_completion_summary just before this is called) and the app-runner
  4967. # state, so the card and the durable file are guaranteed identical. Nothing is
  4968. # computed or written here.
  4969. #
  4970. # Gate (same shape as the HUD at the colors block): interactive stdout, not
  4971. # --bg, and not opted out via LOKI_COMPLETION_CARD=0. Off-TTY / --bg / --json
  4972. # paths emit nothing, so machine output stays byte-identical. Wrapped so any
  4973. # internal failure still returns 0 and can never abort the completion path.
  4974. #===============================================================================
  4975. print_completion_card() {
  4976. # Gate first: emit nothing unless interactive TTY, not background, not opted out.
  4977. if ! { [ -t 1 ] && [ "${BACKGROUND_MODE:-false}" != "true" ] && [ "${LOKI_COMPLETION_CARD:-1}" != "0" ]; }; then
  4978. return 0
  4979. fi
  4980. local loki_dir="${TARGET_DIR:-.}/.loki"
  4981. local _cj="$loki_dir/state/completion.json"
  4982. [ -f "$_cj" ] || return 0
  4983. # Pull the fields we render straight from the persisted record. The python3
  4984. # call emits one field per line in a fixed order; we read them line by line
  4985. # so empty fields (e.g. no pr_url) keep their positions (a single delimiter
  4986. # split would collapse adjacent empties). Any failure leaves the card
  4987. # unrendered. Trailing-newline guard: NUL-free, fields are single-line.
  4988. local _fields
  4989. _fields="$(python3 -E -c "import sys; sys.path[:] = [p for p in sys.path if p not in ('', '.')]
  4990. import json,sys
  4991. try:
  4992. d=json.load(open(sys.argv[1]))
  4993. except Exception:
  4994. sys.exit(0)
  4995. def g(k):
  4996. v=d.get(k,'')
  4997. return '' if v is None else str(v).replace('\n',' ')
  4998. for k in ['outcome','branch','files_changed','insertions','deletions',
  4999. 'review_cmd','pr_url','assumptions_total','assumptions_high']:
  5000. print(g(k))
  5001. " "$_cj" 2>/dev/null)" || return 0
  5002. [ -z "$_fields" ] && return 0
  5003. local _outcome _branch _files _ins _del _review _pr _atotal _ahigh
  5004. {
  5005. IFS= read -r _outcome
  5006. IFS= read -r _branch
  5007. IFS= read -r _files
  5008. IFS= read -r _ins
  5009. IFS= read -r _del
  5010. IFS= read -r _review
  5011. IFS= read -r _pr
  5012. IFS= read -r _atotal
  5013. IFS= read -r _ahigh
  5014. } <<EOF
  5015. $_fields
  5016. EOF
  5017. # Human outcome label (mirror build_completion_summary's mapping).
  5018. local _label
  5019. case "$_outcome" in
  5020. complete) _label="Completed" ;;
  5021. max_iterations) _label="Max iterations" ;;
  5022. stopped) _label="Stopped" ;;
  5023. force_stopped) _label="Stopped (not verified-complete)" ;;
  5024. failed) _label="Failed" ;;
  5025. intervention) _label="Needs input" ;;
  5026. # Mirror of build_completion_summary's gate-stuck arms. Both must move
  5027. # together or the card and COMPLETION.txt disagree on the same run.
  5028. # Literal, not a glob, for the guard reason recorded there.
  5029. gate_stuck_static_analysis) _label="Stopped (static analysis gate would not clear)" ;;
  5030. gate_stuck_mock_integrity) _label="Stopped (mock integrity gate would not clear)" ;;
  5031. gate_stuck_mutation_integrity) _label="Stopped (mutation integrity gate would not clear)" ;;
  5032. *) _label="$_outcome" ;;
  5033. esac
  5034. # Live app URL (best-effort), same read as build_completion_summary.
  5035. local _url="" _app_state="$loki_dir/app-runner/state.json"
  5036. if [ -f "$_app_state" ]; then
  5037. _url="$(python3 -c "import json,sys
  5038. try:
  5039. d=json.load(open(sys.argv[1]))
  5040. print(d.get('url','') if d.get('status')=='running' else '')
  5041. except Exception:
  5042. print('')" "$_app_state" 2>/dev/null)" || _url=""
  5043. fi
  5044. # Box width matches log_header (66 inner columns). Render lines that fit;
  5045. # this is decoration, so over-wide content is simply not boxed-truncated
  5046. # (the durable file carries the full text).
  5047. echo ""
  5048. echo -e "${GREEN}+================================================================+${NC}"
  5049. echo -e "${GREEN}|${NC} ${BOLD}Loki Mode: ${_label}${NC}"
  5050. echo -e "${GREEN}|${NC}"
  5051. if [ -n "$_url" ]; then
  5052. echo -e "${GREEN}|${NC} ${BOLD}${CYAN}Your app is live at ${_url}${NC} ${DIM}- open it to try it${NC}"
  5053. echo -e "${GREEN}|${NC}"
  5054. fi
  5055. echo -e "${GREEN}|${NC} Branch: ${BOLD}${_branch}${NC}"
  5056. echo -e "${GREEN}|${NC} Files: ${BOLD}${_files}${NC} changed ${GREEN}+${_ins}${NC} / ${RED}-${_del}${NC}"
  5057. # Empty-diff guard (honest, not fake-green): a run that ended without
  5058. # producing ANY file change is the classic confusing new-user outcome
  5059. # ("it said done, but nothing happened"). Never imply success on a 0-file
  5060. # run -- surface it plainly with one actionable next step. Only for the
  5061. # non-failure outcomes (a "failed"/"stopped" box already explains itself).
  5062. case "$_files" in
  5063. ''|0)
  5064. case "$_outcome" in
  5065. complete|max_iterations)
  5066. echo -e "${GREEN}|${NC}"
  5067. echo -e "${GREEN}|${NC} ${YELLOW}No file changes were produced this run.${NC} Likely the spec was too vague"
  5068. echo -e "${GREEN}|${NC} or already satisfied. Next: re-run with a more concrete spec, e.g."
  5069. echo -e "${GREEN}|${NC} ${BOLD}loki start \"<one concrete, testable change you want>\"${NC}"
  5070. echo -e "${GREEN}|${NC} or inspect what happened: ${BOLD}loki why${NC}"
  5071. ;;
  5072. esac
  5073. ;;
  5074. esac
  5075. case "$_atotal" in ''|0) : ;; *)
  5076. echo -e "${GREEN}|${NC} Spec assumptions recorded: ${BOLD}${_atotal}${NC} (${_ahigh} high) ${DIM}see .loki/assumptions/ledger.md${NC}"
  5077. ;;
  5078. esac
  5079. if [ -n "$_pr" ]; then
  5080. echo -e "${GREEN}|${NC} Pull request: ${_pr}"
  5081. fi
  5082. echo -e "${GREEN}|${NC}"
  5083. echo -e "${GREEN}|${NC} ${DIM}Review the work:${NC}"
  5084. echo -e "${GREEN}|${NC} ${_review}"
  5085. # NO Evidence Receipt line here, deliberately. The card renders from inside
  5086. # emit_completion_summary, which is reached only from within run_autonomous
  5087. # -- thousands of lines BEFORE the teardown that generates this run's
  5088. # receipt. Announcing here would therefore be silent on a normal success
  5089. # run (no proof exists yet), and on a SECOND run in the same directory it
  5090. # would have been actively wrong before #211: .loki/state/last-proof-id.txt
  5091. # was never cleared at run start, so the card would read the PREVIOUS run's
  5092. # pointer and print that receipt, with that run's verdict, as though it
  5093. # described this run. The pointer is now cleared during run init (search
  5094. # "Same reasoning for the proof pointer"), which closes the cross-run leak,
  5095. # but this site stays silent anyway: it renders thousands of lines before
  5096. # THIS run's receipt exists, so it would print nothing on a normal success
  5097. # run. The announcement lives at the teardown instead, after the final
  5098. # generate_proof_of_run, where the pointer is guaranteed current.
  5099. echo -e "${GREEN}+================================================================+${NC}"
  5100. echo ""
  5101. return 0
  5102. }
  5103. #===============================================================================
  5104. # Rule of Two (moat P9): Loki never passes the GitHub token into the agent's environment via the
  5105. # IMPLICIT resolution paths this module withholds (env vars, gh config store,
  5106. # git credential.helper, SSH agent/ssh command). Not an absolute claim -- see
  5107. # the disclosed residuals at the end of this header (an explicit named-account
  5108. # keyring read, a direct ssh/hosts.yml/keychain read outside git); the actual
  5109. # security boundary is a CI job holding no write token and no SSH agent while
  5110. # the agent runs.
  5111. #
  5112. # The run reads untrusted issue text and spawns agents that act on it, so a
  5113. # token in the exported environment is one prompt injection away from a push.
  5114. # main() un-exports every GitHub token it inherited (the shell keeps the value).
  5115. # Every provider, reviewer, helper and test the agent writes then starts without
  5116. # it. Loki's own trusted GitHub calls get it back for the one command:
  5117. # - direct `gh ...` calls, through the gh() wrapper defined below;
  5118. # - _loki_with_github_tokens <cmd...>, for the post-session push/PR paths.
  5119. # LOKI_ALLOW_AGENT_GITHUB_TOKEN=1 (exact value) restores the old inheritance
  5120. # and prints one stderr warning that the agent holds the token.
  5121. #
  5122. # BACKLOG 149 (round 2, REJECT rework): the previous fix scoped GH_CONFIG_DIR
  5123. # to a fresh empty directory but a reviewer confirmed, live, that this does not
  5124. # close the vulnerability. Two bypasses, both reproduced empirically against
  5125. # real gh 2.92 + a real macOS Keychain-backed `gh auth login` on 2026-09-27:
  5126. #
  5127. # 1. `GH_CONFIG_DIR=<empty dir> gh auth token` (real $HOME, no env token)
  5128. # still exits 0: gh's resolution is config-dir file, THEN OS keyring
  5129. # fallback (Keychain on macOS, libsecret on Linux). An empty config dir
  5130. # does not stop the keyring lookup.
  5131. # 2. git's own `credential.helper` (`gh auth setup-git` wires
  5132. # `credential.https://github.com.helper = !gh auth git-credential`, and a
  5133. # plain `osxkeychain`/`libsecret`/etc. helper may ALSO be configured,
  5134. # unscoped, ahead of or behind it) is invoked directly by git on any
  5135. # `git push`/`git credential fill` over HTTPS. This path is git-invoked,
  5136. # not GH_CONFIG_DIR-mediated at all -- scoping GH_CONFIG_DIR does nothing
  5137. # to it, and it resolves through the same keyring fallback OR a
  5138. # completely separate OS-keychain entry that git's own helper (not gh's)
  5139. # maintains independently.
  5140. #
  5141. # Fix, verified against `gh help environment` and empirically against both
  5142. # bypasses (see the S-18 rework session transcript for the repro/counter-repro
  5143. # pairs; no real credential value was ever printed during that verification):
  5144. #
  5145. # (a) GH_TOKEN/GITHUB_TOKEN/GH_ENTERPRISE_TOKEN/GITHUB_ENTERPRISE_TOKEN are
  5146. # no longer merely unset -- each is set to a fresh per-process garbage
  5147. # value (SENTINEL below). `gh help environment` documents the env token
  5148. # as taking "precedence over previously stored credentials", and this
  5149. # was confirmed live: with the sentinel present, `gh auth token` AND
  5150. # `gh auth git-credential get` both echo back the garbage value, never
  5151. # falling through to the keyring. `gh auth git-credential get` is a pure
  5152. # read/print in this path -- it does not write to GH_CONFIG_DIR or touch
  5153. # the keychain, so planting a sentinel has no destructive side effect;
  5154. # a `git push` that ends up using it simply gets a 401 from GitHub, the
  5155. # same as any other wrong password.
  5156. # (b) The sentinel alone does not close a plain `osxkeychain` (or similar)
  5157. # helper that is configured UNSCOPED (`credential.helper = osxkeychain`,
  5158. # no gh involved at all) and holds its own independently-cached
  5159. # credential -- confirmed present as a THIRD, gh-independent store on
  5160. # the verification machine (git's own keychain entry, distinct from
  5161. # gh's). GH_TOKEN only feeds gh's own resolution; it does not touch
  5162. # git's credential-helper chain. So the session additionally gets
  5163. # `credential.helper=` (empty string) injected via
  5164. # GIT_CONFIG_COUNT/GIT_CONFIG_KEY_n/GIT_CONFIG_VALUE_n, appended after
  5165. # any GIT_CONFIG_COUNT the operator already has set. Per gitcredentials(7):
  5166. # "If credential.helper is configured to the empty string, this resets
  5167. # the helper list to empty" -- this is why the reset must be UNSCOPED
  5168. # (plain `credential.helper`, not `credential.<url>.helper`, since a
  5169. # URL-scoped reset does not necessarily out-order every already
  5170. # configured helper) and appended LAST (env-var config values override
  5171. # every config FILE, per `git help config` on GIT_CONFIG_COUNT, and later
  5172. # GIT_CONFIG_KEY_n entries are read after earlier ones). Confirmed live:
  5173. # with this override present, `git credential fill` against github.com
  5174. # fails closed ("could not read Username") instead of returning a
  5175. # password line, with no fallback to any other helper. This closes the
  5176. # git-invoked path directly, independent of GH_CONFIG_DIR or any
  5177. # GH_TOKEN-family value.
  5178. #
  5179. # GH_CONFIG_DIR is still scoped to a fresh empty directory (unconditionally,
  5180. # same as before) -- it remains correct for the plaintext-hosts.yml-read case
  5181. # and for any gh subcommand that does not consult GH_TOKEN. $HOME itself stays
  5182. # untouched, so Claude's own OAuth (F49) is unaffected -- nothing here reads
  5183. # or writes $HOME/.claude or touches any Anthropic credential path.
  5184. #
  5185. # BACKLOG 149 (round 3, REJECT rework): round 2's fix is HTTPS-only. A
  5186. # round-3 reviewer confirmed, live, that none of the above touches
  5187. # SSH_AUTH_SOCK or GIT_SSH_COMMAND -- the provider session's real, untouched
  5188. # environment always has a reachable SSH agent (by this design's own stated
  5189. # intent of keeping the environment live for Claude's own OAuth), so
  5190. # `git remote set-url origin git@github.com:<owner>/<repo>.git && git push`
  5191. # authenticates exactly as an unrestricted session. Reproduced with a
  5192. # synthetic-only ssh-agent and a throwaway key (never a real credential):
  5193. # `ssh-add -l` against the inherited SSH_AUTH_SOCK lists the identity, and any
  5194. # ssh-transport git operation would authenticate with it.
  5195. #
  5196. # Fix: SSH_AUTH_SOCK is unset (not sentineled) for the withheld session.
  5197. # Unlike the GH_TOKEN-family vars, a present-but-wrong SSH_AUTH_SOCK does NOT
  5198. # stop ssh from falling back: with no usable agent, ssh(1) still tries the
  5199. # default key files (~/.ssh/id_*) and any `IdentityAgent` socket named in
  5200. # ~/.ssh/config (and on macOS a launchd agent socket is rediscoverable). A
  5201. # sentinel therefore buys nothing over unset, so unset is the simpler choice
  5202. # for THIS var. Those fallbacks are why GIT_SSH_COMMAND=false (below) is the
  5203. # part that actually closes git's ssh transport: git never runs ssh at all.
  5204. # An agent that explicitly removes it (`env -u GIT_SSH_COMMAND git push
  5205. # git@...`) gets ssh back with those fallbacks -- same class as `env -u
  5206. # GH_TOKEN`, disclosed in the residual list at the end of this header. That
  5207. # does not contradict round 2's reasoning for sentineling
  5208. # the token vars, it applies that same reasoning to a var with different
  5209. # fallback semantics. GIT_SSH_COMMAND is set to `false` (a POSIX builtin that
  5210. # ignores all arguments and always exits 1): git invokes it in place of ssh
  5211. # for any ssh-transport fetch/push, and git's own docs (`git help git-config`)
  5212. # confirm the env var "is overridden when the environment variable is set" --
  5213. # i.e. GIT_SSH_COMMAND (env) unconditionally outranks core.sshCommand
  5214. # (config), so setting only the env var closes both an operator-configured
  5215. # core.sshCommand AND an agent-configured one, with no separate
  5216. # GIT_CONFIG_COUNT-based core.sshCommand reset needed. This means even an
  5217. # explicit `ssh -i <path>` invocation made THROUGH git (git fetch/push/clone
  5218. # over an ssh:// or git@ URL) fails; it does NOT stop an agent from invoking
  5219. # the real `/usr/bin/ssh` binary directly outside of git, which is a
  5220. # disclosed residual (see below), the same class as the already-disclosed
  5221. # `gh auth token -u <username>` and on-disk hosts.yml/keychain reads.
  5222. #
  5223. # Loki's own trusted calls (gh() below, _loki_with_github_tokens) restore the
  5224. # REAL env values (or absence) for the 4 token vars, the real GH_CONFIG_DIR
  5225. # (or absence), the real GIT_CONFIG_COUNT/KEY/VALUE state (or absence), and
  5226. # the real SSH_AUTH_SOCK/GIT_SSH_COMMAND state (or absence) around the one
  5227. # trusted command, then re-apply the withhold afterward -- this repo's own
  5228. # trusted git operations (create_session_pr, on_run_complete's push,
  5229. # proof-check.sh) push to an https or ssh GitHub origin, so an SSH-origin
  5230. # user's Loki-initiated push must still work; the SSH sentinel needs the same
  5231. # re-grant path as the token vars, not a simpler one.
  5232. #
  5233. # Restoring credentials is only safe if the credentialed process does not
  5234. # also load code the agent controls (round 5). Until round 5 it did: the
  5235. # trusted push was a `git push` inside the agent's working tree, so the
  5236. # agent's repo config (.git/hooks, core.hooksPath, core.sshCommand, a
  5237. # repo-local credential.helper, core.fsmonitor, url.*insteadOf, include/
  5238. # includeIf) ran holding every restored credential. Now the push goes through
  5239. # _loki_trusted_push (fresh Loki-owned repo, origin URL read as data and
  5240. # accepted only as a github.com https/ssh URL) and every trusted gh call runs
  5241. # from / with GH_REPO (_loki_run_neutral), so no credentialed git or gh process
  5242. # loads the agent's repo config. What the trusted step still trusts:
  5243. # - the operator's GLOBAL and SYSTEM git config (they carry the real
  5244. # credential helper). An agent that writes ~/.gitconfig, /etc/gitconfig or
  5245. # an included file there gets the same code execution back; this is the
  5246. # same-user filesystem class as editing ~/.ssh or a shell rc file.
  5247. # - the branch contents, which are the agent's work by design (Loki pushes
  5248. # them for review; nothing in them executes during the push).
  5249. # - the origin OWNER/REPO the agent's config names: the agent can point the
  5250. # push at another github.com repository. GitHub still enforces the
  5251. # operator token's permissions there.
  5252. # A non-GitHub origin is no longer pushed at all (LOKI_AUTO_PR=1 used to push
  5253. # to any origin).
  5254. #
  5255. # This is hygiene against a naive injection, not an isolation boundary: code
  5256. # running as the same user can still read the parent's environment block
  5257. # (/proc/<pid>/environ on Linux, sudo on a hosted runner), the hosts.yml file
  5258. # directly off disk (F49 keeps $HOME live), or invoke `git -c
  5259. # credential.helper=...` / `security find-generic-password` /
  5260. # `env -u GH_TOKEN -u GITHUB_TOKEN gh auth token` / `gh auth token -u
  5261. # <username>` / `env -u GIT_SSH_COMMAND git push git@...` (ssh then falls back
  5262. # to ~/.ssh/id_* default keys and any ~/.ssh/config IdentityAgent) / the real
  5263. # `ssh` binary directly with an explicit `-i` key path or a re-discovered agent
  5264. # socket (e.g. `launchctl getenv SSH_AUTH_SOCK` on macOS) / git's `ext::`
  5265. # transport (`git -c protocol.ext.allow=always` with an `ext::<command>` URL,
  5266. # which runs an arbitrary command as the transport) / GIT_ASKPASS (VS Code's
  5267. # integrated terminal sets it to a helper that can answer git's credential
  5268. # prompt over IPC; it is left untouched here) explicitly to route around
  5269. # this. The boundary is a CI job that holds
  5270. # no write token and no SSH agent while the agent runs (see
  5271. # docs/examples/loki-issue-to-pr.yml.example).
  5272. #===============================================================================
  5273. _LOKI_WITHHELD_TOKENS=""
  5274. _LOKI_GIT_CONFIG_COUNT_UNSUPPORTED=""
  5275. _LOKI_GH_TOKEN_REAL_HAD=""
  5276. _LOKI_GH_TOKEN_REAL_VAL=""
  5277. _LOKI_GITHUB_TOKEN_REAL_HAD=""
  5278. _LOKI_GITHUB_TOKEN_REAL_VAL=""
  5279. _LOKI_GH_ENT_TOKEN_REAL_HAD=""
  5280. _LOKI_GH_ENT_TOKEN_REAL_VAL=""
  5281. _LOKI_GITHUB_ENT_TOKEN_REAL_HAD=""
  5282. _LOKI_GITHUB_ENT_TOKEN_REAL_VAL=""
  5283. _LOKI_GH_CONFIG_SCOPED=""
  5284. _LOKI_GH_CONFIG_DIR_HAD=""
  5285. _LOKI_GH_CONFIG_DIR_OLD=""
  5286. _LOKI_GIT_CRED_SCOPED=""
  5287. _LOKI_GIT_CONFIG_COUNT_OLD=""
  5288. _LOKI_GIT_CONFIG_COUNT_HAD=""
  5289. _LOKI_GIT_CRED_INDEX=""
  5290. _LOKI_GH_SENTINEL=""
  5291. _LOKI_SSH_AUTH_SOCK_HAD=""
  5292. _LOKI_SSH_AUTH_SOCK_OLD=""
  5293. _LOKI_GIT_SSH_COMMAND_HAD=""
  5294. _LOKI_GIT_SSH_COMMAND_OLD=""
  5295. _LOKI_SSH_WITHHELD=""
  5296. _LOKI_GH_INHERITED=""
  5297. # Restore the real value (or absence) of one of the 4 token vars, using the
  5298. # per-var _HAD/_VAL pair captured by _loki_gh_capture. Bash has no portable
  5299. # array-of-names-by-reference here, so this is spelled out per var rather than
  5300. # looped over a dynamic name. Also used by the --bg relaunch path to hand the
  5301. # REAL values to the relaunched child (see call site below).
  5302. _loki_restore_one_token() {
  5303. local _var="$1" _had="$2" _val="$3"
  5304. if [ -n "$_had" ]; then
  5305. export "$_var=$_val"
  5306. else
  5307. unset "$_var"
  5308. fi
  5309. }
  5310. # Capture the REAL pre-withhold state exactly once per process: the 4 token
  5311. # vars, GH_CONFIG_DIR, and GIT_CONFIG_COUNT. Idempotent via _LOKI_WITHHELD_TOKENS
  5312. # as the guard, so a later _loki_gh_apply (called after a trusted command
  5313. # restores and re-applies) never re-captures its own sentinel/scoped state as
  5314. # if it were the original.
  5315. _loki_gh_capture() {
  5316. [ -z "$_LOKI_WITHHELD_TOKENS" ] || return 0
  5317. local _v
  5318. # GIT_CONFIG_COUNT/GIT_CONFIG_KEY_n/GIT_CONFIG_VALUE_n were introduced in
  5319. # git 2.31.0 (2021-03-15); an older git silently ignores them, leaving the
  5320. # credential.helper reset a no-op with no error -- the exact
  5321. # fail-open-with-no-signal this fix must not have. Detected once per
  5322. # process (not on every apply) from `git --version`; a `git version
  5323. # X.Y.Z` line below 2.31 sets the flag so _loki_withhold_github_tokens can
  5324. # warn once. Ubuntu 24.04 (2.43) and Debian bookworm (2.39), the two base
  5325. # images this project ships (Dockerfile, Dockerfile.sandbox), are both
  5326. # well above this floor; the warning matters for an operator's own older
  5327. # git (e.g. RHEL 8 ships 2.18).
  5328. if command -v git >/dev/null 2>&1; then
  5329. local _gv _gv_major _gv_minor
  5330. _gv="$(git --version 2>/dev/null)"
  5331. case "$_gv" in
  5332. git\ version\ [0-9]*.[0-9]*.*)
  5333. _gv="${_gv#git version }"
  5334. _gv_major="${_gv%%.*}"
  5335. _gv="${_gv#*.}"
  5336. _gv_minor="${_gv%%.*}"
  5337. case "$_gv_major" in *[!0-9]*) _gv_major="" ;; esac
  5338. case "$_gv_minor" in *[!0-9]*) _gv_minor="" ;; esac
  5339. if [ -n "$_gv_major" ] && [ -n "$_gv_minor" ]; then
  5340. if [ "$_gv_major" -lt 2 ] || { [ "$_gv_major" -eq 2 ] && [ "$_gv_minor" -lt 31 ]; }; then
  5341. _LOKI_GIT_CONFIG_COUNT_UNSUPPORTED=1
  5342. fi
  5343. fi
  5344. ;;
  5345. esac
  5346. fi
  5347. # Cross-runtime/nested-`loki start` guard: a bash child spawned by an
  5348. # ALREADY-withheld process (Bun's delegateToBash forwarding
  5349. # {...process.env}, or an agent literally invoking `loki start` inside its
  5350. # own withheld session) inherits the PARENT's sentinel/scoped state as
  5351. # ordinary env vars. Without this guard this process would capture that
  5352. # inherited state as if it were the operator's real original and restore
  5353. # to it for its own trusted calls -- i.e. Loki's own push/PR would use the
  5354. # inherited garbage sentinel instead of a real credential. Detect the
  5355. # sentinel shape (see _LOKI_GH_SENTINEL below) and treat it, and the
  5356. # scoped GH_CONFIG_DIR/credential.helper reset that came with it, as
  5357. # ABSENT rather than as the real original.
  5358. local _inherited_sentinel=""
  5359. case "${GH_TOKEN:-}" in ghp_LOKIWITHHELDsentinel*INVALID) _inherited_sentinel=1 ;; esac
  5360. # Nested-run rule (BACKLOG 149 round 4): an inherited sentinel means this
  5361. # run was started from INSIDE an already-withheld session -- an agent
  5362. # running `loki start` in its own session, or a build started from the
  5363. # dashboard that start_dashboard spawned after the withhold. Such a run has
  5364. # no real credentials to restore on ANY channel, and reopening any of them
  5365. # for its "trusted" push would hand an agent-chosen repo/branch the
  5366. # operator's credential helper or default SSH keys. So it fails CLOSED on
  5367. # every channel: _loki_gh_restore becomes a no-op and Loki's own push/PR
  5368. # from a nested run runs with the withheld state (and fails). Before this
  5369. # rule the channels disagreed (HTTPS helper chain and default SSH keys
  5370. # reopened, agent socket closed).
  5371. [ -z "$_inherited_sentinel" ] || _LOKI_GH_INHERITED=1
  5372. if [ -n "$_inherited_sentinel" ]; then
  5373. : # GH_TOKEN/GITHUB_TOKEN/GH_ENTERPRISE_TOKEN/GITHUB_ENTERPRISE_TOKEN
  5374. # all get the SAME sentinel per _loki_gh_apply, so one check covers
  5375. # all 4 -- _LOKI_*_REAL_HAD stays unset for each, meaning "restore to
  5376. # absent", not "restore to this garbage value". The same inherited
  5377. # process also had SSH_AUTH_SOCK unset and GIT_SSH_COMMAND=false by
  5378. # this same apply, so _LOKI_SSH_AUTH_SOCK_HAD/_LOKI_GIT_SSH_COMMAND_HAD
  5379. # stay unset too (restore to absent, not to `false`) -- see the SSH
  5380. # capture below, gated on this same flag.
  5381. else
  5382. if [ -n "${GH_TOKEN+x}" ]; then _LOKI_GH_TOKEN_REAL_HAD=1; _LOKI_GH_TOKEN_REAL_VAL="$GH_TOKEN"; fi
  5383. if [ -n "${GITHUB_TOKEN+x}" ]; then _LOKI_GITHUB_TOKEN_REAL_HAD=1; _LOKI_GITHUB_TOKEN_REAL_VAL="$GITHUB_TOKEN"; fi
  5384. if [ -n "${GH_ENTERPRISE_TOKEN+x}" ]; then _LOKI_GH_ENT_TOKEN_REAL_HAD=1; _LOKI_GH_ENT_TOKEN_REAL_VAL="$GH_ENTERPRISE_TOKEN"; fi
  5385. if [ -n "${GITHUB_ENTERPRISE_TOKEN+x}" ]; then _LOKI_GITHUB_ENT_TOKEN_REAL_HAD=1; _LOKI_GITHUB_ENT_TOKEN_REAL_VAL="$GITHUB_ENTERPRISE_TOKEN"; fi
  5386. fi
  5387. # SSH sentinel (BACKLOG 149 round 3): capture the real pre-withhold state
  5388. # of SSH_AUTH_SOCK and GIT_SSH_COMMAND, same inherited-sentinel guard as
  5389. # the token vars above (GIT_SSH_COMMAND=false is this fix's own withheld
  5390. # value, never an operator's real config -- an operator who genuinely
  5391. # wants `false` as their ssh command is not a real scenario worth
  5392. # preserving across the guard).
  5393. if [ -z "$_inherited_sentinel" ]; then
  5394. if [ -n "${SSH_AUTH_SOCK+x}" ]; then _LOKI_SSH_AUTH_SOCK_HAD=1; _LOKI_SSH_AUTH_SOCK_OLD="$SSH_AUTH_SOCK"; fi
  5395. if [ -n "${GIT_SSH_COMMAND+x}" ] && [ "$GIT_SSH_COMMAND" != "false" ]; then
  5396. _LOKI_GIT_SSH_COMMAND_HAD=1
  5397. _LOKI_GIT_SSH_COMMAND_OLD="$GIT_SSH_COMMAND"
  5398. fi
  5399. fi
  5400. for _v in GH_TOKEN GITHUB_TOKEN GH_ENTERPRISE_TOKEN GITHUB_ENTERPRISE_TOKEN; do
  5401. _LOKI_WITHHELD_TOKENS="${_LOKI_WITHHELD_TOKENS:+$_LOKI_WITHHELD_TOKENS }$_v"
  5402. done
  5403. # An inherited scoped GH_CONFIG_DIR (bash's loki-gh-config.* or the Bun
  5404. # route's loki-gh-config-*) is likewise not the operator's real original.
  5405. # TMPDIR is normalized first: on macOS it ends in "/", so bash's own
  5406. # mktemp path has "//" while Node's tmpdir() (Bun route) strips the slash.
  5407. # Without accepting both, Bun's scoped dir was mistaken for the operator's.
  5408. local _tmp_root="${TMPDIR:-/tmp}"
  5409. _tmp_root="${_tmp_root%/}"
  5410. case "${GH_CONFIG_DIR:-}" in
  5411. "$_tmp_root"/loki-gh-config.* | "$_tmp_root"//loki-gh-config.* \
  5412. | "$_tmp_root"/loki-gh-config-* | "$_tmp_root"//loki-gh-config-*) ;;
  5413. *)
  5414. if [ -n "${GH_CONFIG_DIR+x}" ]; then
  5415. _LOKI_GH_CONFIG_DIR_HAD=1
  5416. _LOKI_GH_CONFIG_DIR_OLD="$GH_CONFIG_DIR"
  5417. fi
  5418. ;;
  5419. esac
  5420. # An inherited credential.helper reset: if the sentinel was inherited AND
  5421. # the LAST GIT_CONFIG_KEY/VALUE pair is exactly this fix's reset
  5422. # (credential.helper=""), treat the count as if that pair were never
  5423. # added -- i.e. capture the state BENEATH it as the real original, so
  5424. # restore correctly removes only the inherited pair rather than an
  5425. # operator-configured one at the same index.
  5426. if [ -n "${GIT_CONFIG_COUNT:-}" ] && [ "${GIT_CONFIG_COUNT}" -eq "${GIT_CONFIG_COUNT}" ] 2>/dev/null; then
  5427. local _n=$((GIT_CONFIG_COUNT - 1)) _last_key_var _last_val_var
  5428. _last_key_var="GIT_CONFIG_KEY_${_n}"
  5429. _last_val_var="GIT_CONFIG_VALUE_${_n}"
  5430. if [ -n "$_inherited_sentinel" ] && [ "$_n" -ge 0 ] \
  5431. && [ "${!_last_key_var:-}" = "credential.helper" ] && [ -z "${!_last_val_var:-}" ]; then
  5432. if [ "$_n" -gt 0 ]; then
  5433. _LOKI_GIT_CONFIG_COUNT_HAD=1
  5434. _LOKI_GIT_CONFIG_COUNT_OLD="$_n"
  5435. fi
  5436. _LOKI_GIT_CRED_INDEX="$_n"
  5437. else
  5438. _LOKI_GIT_CONFIG_COUNT_HAD=1
  5439. _LOKI_GIT_CONFIG_COUNT_OLD="$GIT_CONFIG_COUNT"
  5440. _LOKI_GIT_CRED_INDEX="$GIT_CONFIG_COUNT"
  5441. fi
  5442. else
  5443. _LOKI_GIT_CRED_INDEX=0
  5444. fi
  5445. # Fixed for the process lifetime once captured, so restore-then-reapply
  5446. # (the trusted-call cycle) always re-exports the SAME sentinel rather than
  5447. # minting a new one that a caller mid-command could not have seen.
  5448. # Alphanumeric-only after the ghp_ prefix (no underscores): matches the
  5449. # real gh token shape and the existing token-redaction regex in
  5450. # autonomy/lib/proof_redact.py (gh[pousr]_[A-Za-z0-9]{20,}), so if this
  5451. # sentinel ever leaked into a proof/receipt artifact it would still be
  5452. # caught by the existing redaction filter rather than passing it by shape.
  5453. _LOKI_GH_SENTINEL="ghp_LOKIWITHHELDsentinel$$${RANDOM}${RANDOM}INVALID"
  5454. }
  5455. # Unconditionally (re-)export the withheld/scoped state. Never captures --
  5456. # _loki_gh_capture must run first. Silent: callers that want the one-time
  5457. # operator-facing log line print it themselves.
  5458. _loki_gh_apply() {
  5459. export GH_TOKEN="$_LOKI_GH_SENTINEL" GITHUB_TOKEN="$_LOKI_GH_SENTINEL" \
  5460. GH_ENTERPRISE_TOKEN="$_LOKI_GH_SENTINEL" GITHUB_ENTERPRISE_TOKEN="$_LOKI_GH_SENTINEL"
  5461. if [ -z "$_LOKI_GH_CONFIG_SCOPED" ]; then
  5462. local _empty_cfg
  5463. _empty_cfg="$(mktemp -d "${TMPDIR:-/tmp}/loki-gh-config.XXXXXX" 2>/dev/null)" || _empty_cfg=""
  5464. [ -n "$_empty_cfg" ] && _LOKI_GH_CONFIG_SCOPED="$_empty_cfg"
  5465. fi
  5466. [ -n "$_LOKI_GH_CONFIG_SCOPED" ] && export GH_CONFIG_DIR="$_LOKI_GH_CONFIG_SCOPED"
  5467. export "GIT_CONFIG_KEY_${_LOKI_GIT_CRED_INDEX}=credential.helper"
  5468. export "GIT_CONFIG_VALUE_${_LOKI_GIT_CRED_INDEX}="
  5469. export GIT_CONFIG_COUNT=$((_LOKI_GIT_CRED_INDEX + 1))
  5470. _LOKI_GIT_CRED_SCOPED=1
  5471. # SSH sentinel (BACKLOG 149 round 3): no reachable SSH agent, and any
  5472. # ssh-transport git operation fails closed via GIT_SSH_COMMAND=false
  5473. # (env var, so it overrides any core.sshCommand -- see the header comment
  5474. # above _LOKI_WITHHELD_TOKENS for the git-docs citation).
  5475. unset SSH_AUTH_SOCK
  5476. export GIT_SSH_COMMAND=false
  5477. _LOKI_SSH_WITHHELD=1
  5478. if command -v gh >/dev/null 2>&1; then
  5479. # Repo read as data BEFORE the credentials come back; gh then runs
  5480. # from / (round 5: gh's own git calls must not load the agent's repo
  5481. # config while holding the real credentials).
  5482. gh() {
  5483. local _gh_repo
  5484. _gh_repo="$(_loki_trusted_repo)"
  5485. _loki_with_github_tokens _loki_run_neutral "$_gh_repo" command gh "$@"
  5486. }
  5487. fi
  5488. }
  5489. # Put back the REAL pre-withhold state around one trusted command. A no-op in
  5490. # a run that inherited the sentinel: there is no real state to put back, and
  5491. # the nested-run rule in _loki_gh_capture is to fail closed on every channel.
  5492. _loki_gh_restore() {
  5493. [ -z "$_LOKI_GH_INHERITED" ] || return 0
  5494. _loki_restore_one_token GH_TOKEN "$_LOKI_GH_TOKEN_REAL_HAD" "$_LOKI_GH_TOKEN_REAL_VAL"
  5495. _loki_restore_one_token GITHUB_TOKEN "$_LOKI_GITHUB_TOKEN_REAL_HAD" "$_LOKI_GITHUB_TOKEN_REAL_VAL"
  5496. _loki_restore_one_token GH_ENTERPRISE_TOKEN "$_LOKI_GH_ENT_TOKEN_REAL_HAD" "$_LOKI_GH_ENT_TOKEN_REAL_VAL"
  5497. _loki_restore_one_token GITHUB_ENTERPRISE_TOKEN "$_LOKI_GITHUB_ENT_TOKEN_REAL_HAD" "$_LOKI_GITHUB_ENT_TOKEN_REAL_VAL"
  5498. if [ -n "$_LOKI_GH_CONFIG_DIR_HAD" ]; then
  5499. export GH_CONFIG_DIR="$_LOKI_GH_CONFIG_DIR_OLD"
  5500. else
  5501. unset GH_CONFIG_DIR
  5502. fi
  5503. if [ -n "$_LOKI_GIT_CONFIG_COUNT_HAD" ]; then
  5504. export GIT_CONFIG_COUNT="$_LOKI_GIT_CONFIG_COUNT_OLD"
  5505. else
  5506. unset GIT_CONFIG_COUNT
  5507. fi
  5508. unset "GIT_CONFIG_KEY_${_LOKI_GIT_CRED_INDEX}" "GIT_CONFIG_VALUE_${_LOKI_GIT_CRED_INDEX}"
  5509. # SSH sentinel restore: give the trusted command back the real SSH agent
  5510. # and ssh command, so Loki's own push/PR still works over an SSH origin.
  5511. _loki_restore_one_token SSH_AUTH_SOCK "$_LOKI_SSH_AUTH_SOCK_HAD" "$_LOKI_SSH_AUTH_SOCK_OLD"
  5512. _loki_restore_one_token GIT_SSH_COMMAND "$_LOKI_GIT_SSH_COMMAND_HAD" "$_LOKI_GIT_SSH_COMMAND_OLD"
  5513. }
  5514. _LOKI_GH_TRUSTED_DEPTH=0
  5515. # Reentrancy: a trusted function (create_session_pr) calls `gh` directly one
  5516. # or more times; since the gh() wrapper (_loki_gh_apply) IS
  5517. # _loki_with_github_tokens, each of those calls re-enters this function while
  5518. # the OUTER call is still in progress. Without depth tracking, the FIRST
  5519. # nested `gh` call's own apply-on-exit would re-scope the environment before
  5520. # the outer caller's second `gh` call (or any bare `git` call) runs, breaking
  5521. # it silently -- caught in this session before landing: create_session_pr
  5522. # calls `gh pr list` then `gh pr create`, and the nested apply between them
  5523. # would have handed the second call sentineled/scoped state instead of real
  5524. # credentials. Only the OUTERMOST call restores on entry and re-applies on
  5525. # exit; a nested call finds the depth already > 0 and is a pure passthrough
  5526. # (real credentials are already in place from the outer restore).
  5527. _loki_with_github_tokens() {
  5528. local _rc=0 _outermost=0
  5529. if [ "$_LOKI_GH_TRUSTED_DEPTH" -eq 0 ]; then
  5530. _outermost=1
  5531. if [ -n "$_LOKI_WITHHELD_TOKENS" ]; then
  5532. _loki_gh_restore
  5533. fi
  5534. fi
  5535. _LOKI_GH_TRUSTED_DEPTH=$((_LOKI_GH_TRUSTED_DEPTH + 1))
  5536. "$@" || _rc=$?
  5537. _LOKI_GH_TRUSTED_DEPTH=$((_LOKI_GH_TRUSTED_DEPTH - 1))
  5538. if [ "$_outermost" -eq 1 ] && [ -n "$_LOKI_WITHHELD_TOKENS" ]; then
  5539. _loki_gh_apply
  5540. fi
  5541. return "$_rc"
  5542. }
  5543. #-------------------------------------------------------------------------------
  5544. # BACKLOG 149 round 5: a credentialed git or gh process never loads the agent's
  5545. # repo config.
  5546. #
  5547. # _loki_with_github_tokens hands the real credentials back for one command. If
  5548. # that command is `git push` run inside the agent's working tree, git loads the
  5549. # agent-writable repo config first: .git/hooks/pre-push, core.hooksPath,
  5550. # core.sshCommand, a repo-local credential.helper, core.fsmonitor,
  5551. # url.insteadOf/pushInsteadOf, include/includeIf. Two reviewers reproduced a
  5552. # planted pre-push hook reading the real GH_TOKEN and SSH_AUTH_SOCK, and
  5553. # `git credential fill` returning the operator's password. `-c
  5554. # core.hooksPath=/dev/null` alone does not stop core.sshCommand, and a list of
  5555. # per-key overrides would have to stay complete forever.
  5556. #
  5557. # So the credentialed step never runs with that config loaded:
  5558. # - push: read remote.origin.url as DATA (no credentials), accept only a
  5559. # github.com https/ssh URL, fetch the branch into a fresh Loki-owned repo
  5560. # (the fetch runs upload-pack in the agent repo WITHOUT credentials), then
  5561. # push from that repo with the credentials restored.
  5562. # - gh: run from / with GH_REPO set to the validated origin, so gh's own git
  5563. # calls find no repository. An explicit --repo/-R still overrides GH_REPO.
  5564. # The operator's global/system git config still loads (it holds the real
  5565. # credential helper); an agent that can write ~/.gitconfig is a same-user
  5566. # filesystem residual, disclosed in the header above.
  5567. #-------------------------------------------------------------------------------
  5568. # Echo OWNER/REPO for a github.com origin URL, else fail. Accepted forms:
  5569. # https://github.com/O/R[.git][/], git@github.com:O/R[.git][/],
  5570. # ssh://git@github.com/O/R[.git][/], host matched case-insensitively. Anything
  5571. # else (other hosts, SSH host aliases such as github.com-work, embedded
  5572. # credentials, extra path segments, odd characters) is rejected;
  5573. # _loki_origin_refusal says why.
  5574. _loki_github_repo_from_url() {
  5575. local _u="${1:-}" _lc _pre _p
  5576. _lc="$(printf '%s' "$_u" | tr '[:upper:]' '[:lower:]')"
  5577. case "$_lc" in
  5578. https://github.com/*) _pre="https://github.com/" ;;
  5579. git@github.com:*) _pre="git@github.com:" ;;
  5580. ssh://git@github.com/*) _pre="ssh://git@github.com/" ;;
  5581. *) return 1 ;;
  5582. esac
  5583. _p="${_u:${#_pre}}"
  5584. _p="${_p%/}"
  5585. _p="${_p%.git}"
  5586. case "$_p" in
  5587. */*/* | /* | */ | */.* | .* | *..* | *[!A-Za-z0-9._/-]*) return 1 ;;
  5588. ?*/?*) printf '%s\n' "$_p" ;;
  5589. *) return 1 ;;
  5590. esac
  5591. }
  5592. # One line saying why an origin is refused. Never echoes the URL itself (it
  5593. # may carry embedded credentials).
  5594. _loki_origin_refusal() {
  5595. local _lc
  5596. _lc="$(printf '%s' "${1:-}" | tr '[:upper:]' '[:lower:]')"
  5597. case "$_lc" in
  5598. '') echo "this repository has no origin remote" ;;
  5599. *@github.com[!:/]*|*://github.com[!/]*|*@github.com[!:/]*:*)
  5600. echo "origin uses an SSH host alias or look-alike of github.com (for example github.com-work from ~/.ssh/config); Loki pushes only to a literal github.com origin, so the push never depends on agent-editable ssh config" ;;
  5601. *github.com*)
  5602. echo "origin names github.com but is not a plain https or ssh OWNER/REPO URL (embedded credentials, extra path segments or unusual characters are refused)" ;;
  5603. *)
  5604. echo "origin is not on github.com; Loki currently pushes only to github.com (GitHub Enterprise and GitLab are planned)" ;;
  5605. esac
  5606. }
  5607. # Origin pin (round 6, reviewer F1): the agent could otherwise repoint origin
  5608. # at another github.com repository and have Loki push its branch there with
  5609. # the operator's credentials. Loki never changes origin itself, so the value
  5610. # recorded before the first iteration (possibly "no origin") is the only one
  5611. # a trusted push accepts. Recorded once per process.
  5612. _LOKI_ORIGIN_PINNED=""
  5613. _LOKI_PINNED_ORIGIN=""
  5614. _loki_pin_origin() {
  5615. [ -z "$_LOKI_ORIGIN_PINNED" ] || return 0
  5616. _LOKI_PINNED_ORIGIN="$(git -C "${TARGET_DIR:-.}" config --get remote.origin.url 2>/dev/null || true)"
  5617. _LOKI_ORIGIN_PINNED=1
  5618. }
  5619. # OWNER/REPO of TARGET_DIR's origin, read as data, or nothing.
  5620. _loki_trusted_repo() {
  5621. _loki_github_repo_from_url "$(git -C "${TARGET_DIR:-.}" config --get remote.origin.url 2>/dev/null)" 2>/dev/null || true
  5622. }
  5623. # _loki_run_neutral <owner/repo|""> <cmd...>: run cmd from / so no repository
  5624. # (and so no repo config) is discovered, with GH_REPO naming the repo for gh.
  5625. # A subshell, so functions stay callable; callers pass `command gh` to reach
  5626. # the binary.
  5627. _loki_run_neutral() {
  5628. local _repo="$1"
  5629. shift
  5630. (
  5631. cd / || exit 1
  5632. unset GIT_DIR GIT_WORK_TREE
  5633. if [ -n "$_repo" ] && [ -z "${GH_REPO:-}" ]; then
  5634. export GH_REPO="$_repo"
  5635. fi
  5636. "$@"
  5637. )
  5638. }
  5639. # _loki_trusted_push <runner> <repo-dir> <branch>: push <branch> of the agent
  5640. # repo to its github.com origin without loading the agent repo's config in
  5641. # the credentialed process. <runner> is the credential re-grant wrapper
  5642. # (_loki_with_github_tokens, or on_run_complete's timed _loki_net). The agent
  5643. # repo's branch gets no upstream set (it did with `push -u`); nothing reads it.
  5644. # Optional <dest> OWNER/REPO (engine10-push.sh fork mode): the pinned origin is
  5645. # still validated, then the push goes to https://github.com/<dest>.git, a fork
  5646. # the push child resolved from gh and checked, never a value from the tree.
  5647. _loki_trusted_push() {
  5648. local _runner="$1" _dir="$2" _branch="$3" _dest="${4:-}" _url _tmp _rc=1
  5649. _dir="$(cd "$_dir" 2>/dev/null && pwd -P)" || return 1
  5650. git check-ref-format --branch "$_branch" >/dev/null 2>&1 || return 1
  5651. _url="$(git -C "$_dir" config --get remote.origin.url 2>/dev/null)" || _url=""
  5652. if ! _loki_github_repo_from_url "$_url" >/dev/null; then
  5653. log_warn "Not pushing branch '$_branch': $(_loki_origin_refusal "$_url")."
  5654. return 2
  5655. fi
  5656. if [ -n "$_LOKI_ORIGIN_PINNED" ] && [ "$_url" != "$_LOKI_PINNED_ORIGIN" ]; then
  5657. log_warn "Not pushing branch '$_branch': origin changed during the run (it was $(_loki_github_repo_from_url "$_LOKI_PINNED_ORIGIN" 2>/dev/null || echo "not a GitHub repository") when the run started). Loki does not push to a destination the agent could have chosen."
  5658. return 2
  5659. fi
  5660. # never push a default branch, for every caller (on_run_complete,
  5661. # create_session_pr): the branch name can come from agent-writable state.
  5662. # The default branch is resolved for the origin just validated (the pinned
  5663. # one when pinned), gh run from / with an explicit OWNER/REPO. An
  5664. # unresolvable default branch refuses: fail closed.
  5665. local _repo _def=""
  5666. case "$_branch" in
  5667. main|master|HEAD)
  5668. log_warn "Not pushing branch '$_branch': Loki never pushes directly to a default branch."
  5669. return 2 ;;
  5670. esac
  5671. _repo="$(_loki_github_repo_from_url "$_url")"
  5672. local _origin_url="$_url"
  5673. if [ -n "$_dest" ]; then
  5674. if ! _loki_github_repo_from_url "https://github.com/${_dest}.git" >/dev/null; then
  5675. log_warn "Not pushing branch '$_branch': fork destination '$_dest' is not a plain OWNER/REPO."
  5676. return 2
  5677. fi
  5678. _repo="$_dest" _url="https://github.com/${_dest}.git"
  5679. fi
  5680. _def="$(_loki_run_neutral "$_repo" "$_runner" gh repo view "$_repo" --json defaultBranchRef --jq '.defaultBranchRef.name' 2>/dev/null)" || _def=""
  5681. if [ -z "$_def" ]; then
  5682. log_warn "Not pushing branch '$_branch': could not resolve the default branch of $_repo, and Loki never risks a direct push to it."
  5683. return 2
  5684. fi
  5685. if [ "$_branch" = "$_def" ]; then
  5686. log_warn "Not pushing branch '$_branch': it is the default branch of $_repo, and Loki never pushes directly to the default branch."
  5687. return 2
  5688. fi
  5689. _tmp="$(mktemp -d "${TMPDIR:-/tmp}/loki-push.XXXXXX")" || return 1
  5690. # FC-TRUSTED-PUSH-PARTIAL-CLONE: a partial agent clone (extensions.partialclone,
  5691. # or a remote.<n>.promisor true/yes/on/1 on newer git; both read as data)
  5692. # holds objects it never downloaded, and upload-pack does not lazy-fetch
  5693. # them, so the branch fetch below dies. Seed the temp repo with the
  5694. # validated origin (_origin_url, captured before a fork _dest overwrites
  5695. # _url) as a promisor remote and fetch its refs first. The agent pack is
  5696. # thin (deltas against blobs the temp repo only knows by id), so the client
  5697. # lazy-fetches those delta bases from loki-base during the agent fetch: that
  5698. # fetch therefore runs under "$_runner" too. The credentials stay on the
  5699. # client side only: the agent-dir upload-pack, the one process that reads
  5700. # agent config, is started through `env -i` with no credentials, no helper
  5701. # config, no HOME and no lazy fetch. GIT_TERMINAL_PROMPT=0 so a refused
  5702. # token never waits on a username. Non-partial repos skip all of this.
  5703. local _partial="" _up=""
  5704. if [ -n "$(git -C "$_dir" config --get extensions.partialclone 2>/dev/null)" ] \
  5705. || git -C "$_dir" config --get-regexp '^remote\..*\.promisor$' 2>/dev/null | grep -Eqi ' (true|yes|on|1)$'; then
  5706. _partial=1
  5707. _up="/usr/bin/env -i PATH=/usr/bin:/bin GIT_NO_LAZY_FETCH=1 $(printf '%q' "$(command -v git)") upload-pack"
  5708. fi
  5709. # --update-shallow: a shallow agent repo's history ends in shallow roots,
  5710. # which a plain fetch rejects while still exiting 0. The rev-parse makes a
  5711. # silently empty fetch fail here instead of at the push.
  5712. if git init -q --template= "$_tmp" >/dev/null 2>&1 \
  5713. && { [ -z "$_partial" ] || {
  5714. git -C "$_tmp" config core.repositoryformatversion 1 \
  5715. && git -C "$_tmp" config extensions.partialclone loki-base \
  5716. && git -C "$_tmp" config remote.loki-base.url "$_origin_url" \
  5717. && git -C "$_tmp" config remote.loki-base.promisor true \
  5718. && git -C "$_tmp" config remote.loki-base.partialclonefilter blob:none \
  5719. && GIT_TERMINAL_PROMPT=0 "$_runner" git -C "$_tmp" fetch -q --no-tags --filter=blob:none loki-base "+refs/heads/*:refs/remotes/loki-base/*" >/dev/null 2>&1; }; } \
  5720. && { if [ -n "$_partial" ]; then
  5721. GIT_TERMINAL_PROMPT=0 "$_runner" git -C "$_tmp" fetch -q --no-tags --update-shallow --upload-pack "$_up" "$_dir" "+refs/heads/$_branch:refs/heads/$_branch" >/dev/null 2>&1
  5722. else
  5723. git -C "$_tmp" fetch -q --no-tags --update-shallow "$_dir" "+refs/heads/$_branch:refs/heads/$_branch" >/dev/null 2>&1
  5724. fi; } \
  5725. && git -C "$_tmp" rev-parse -q --verify "refs/heads/$_branch" >/dev/null 2>&1; then
  5726. _rc=0
  5727. # `-C "$_tmp"`: git starts repository discovery in the fresh repo, so
  5728. # the agent repo is never found, whatever the caller's cwd.
  5729. "$_runner" git -C "$_tmp" push -q "$_url" "refs/heads/$_branch:refs/heads/$_branch" || _rc=$?
  5730. fi
  5731. rm -rf "$_tmp"
  5732. return "$_rc"
  5733. }
  5734. _loki_withhold_github_tokens() {
  5735. local _v _held=""
  5736. # Before any provider runs, so the pin is the operator's origin, not one
  5737. # an agent set. Also under the opt-out: it guards the push destination.
  5738. _loki_pin_origin
  5739. # Operator opt-out (exact value 1): keep the earlier behavior, where the
  5740. # agent inherits the token and the real gh config. That is a Rule of Two
  5741. # exposure, so say so. No GH_CONFIG_DIR/credential.helper scoping under
  5742. # the opt-out either -- the point of this knob is the old, fully-inherited
  5743. # behavior.
  5744. if [ "${LOKI_ALLOW_AGENT_GITHUB_TOKEN:-}" = "1" ]; then
  5745. for _v in GH_TOKEN GITHUB_TOKEN GH_ENTERPRISE_TOKEN GITHUB_ENTERPRISE_TOKEN; do
  5746. [ -n "${!_v:-}" ] && _held="${_held:+$_held }$_v"
  5747. done
  5748. [ -z "$_held" ] || printf '%s\n' "WARNING: LOKI_ALLOW_AGENT_GITHUB_TOKEN=1: the agent session holds the GitHub token ($_held); an injected prompt can push with it (Rule of Two exposure)." >&2
  5749. return 0
  5750. fi
  5751. local _first_time=""
  5752. [ -n "$_LOKI_WITHHELD_TOKENS" ] || _first_time=1
  5753. # gh documents (`gh help environment`) that an env token "takes precedence
  5754. # over previously stored credentials", so a garbage-but-present value
  5755. # makes gh's own resolution (both `gh auth token` and `gh auth
  5756. # git-credential`, the latter reachable directly by git's
  5757. # credential.helper) return the garbage value instead of falling through
  5758. # to a keyring/hosts.yml lookup. GH_CONFIG_DIR stays scoped to a fresh
  5759. # empty dir (the plaintext-hosts.yml-read case). credential.helper is
  5760. # additionally reset to empty (gitcredentials(7)): this is git-invoked,
  5761. # not gh-mediated, so the sentinel does not reach a plain
  5762. # osxkeychain/libsecret helper on its own.
  5763. _loki_gh_capture
  5764. _loki_gh_apply
  5765. if [ -n "$_first_time" ]; then
  5766. if [ -n "$_LOKI_GH_INHERITED" ]; then
  5767. printf '%s\n' "WARNING: this run was started from inside an already-withheld Loki session (inherited Rule of Two sentinel). It has no real GitHub or SSH credentials to restore, so its own push and PR steps will fail closed. Start builds that need to push from a normal shell." >&2
  5768. else
  5769. log_info "Withheld from agent sessions (Rule of Two): $_LOKI_WITHHELD_TOKENS (sentineled, not merely unset), gh config store, git credential.helper, SSH agent (SSH_AUTH_SOCK unset, GIT_SSH_COMMAND=false). Loki's own push and PR steps still use the real credentials."
  5770. fi
  5771. if [ -n "$_LOKI_GIT_CONFIG_COUNT_UNSUPPORTED" ]; then
  5772. printf '%s\n' "WARNING: git < 2.31 detected -- GIT_CONFIG_COUNT/GIT_CONFIG_KEY/GIT_CONFIG_VALUE (used to reset credential.helper) are silently ignored on this git version. The git-invoked credential-helper bypass (BACKLOG 149 round 2) is NOT closed on this host; upgrade git to 2.31+ to close it. The GH_TOKEN-family sentinel, GH_CONFIG_DIR scoping, and the SSH_AUTH_SOCK/GIT_SSH_COMMAND withhold above are unaffected and still apply (GIT_SSH_COMMAND is a plain env var, not a GIT_CONFIG_* mechanism, so it needs no version floor)." >&2
  5773. fi
  5774. fi
  5775. }
  5776. #===============================================================================
  5777. # on_run_complete (Slice 3: opt-in local git output on success)
  5778. #
  5779. # Called from every SUCCESS exit BEFORE emit_completion_summary so the PR url it
  5780. # discovers is folded into the summary. Default behavior is a no-op: it only
  5781. # acts when LOKI_DELEGATE_PR=1.
  5782. #
  5783. # LOKI_DELEGATE_PR=1 opens a LOCAL pull request from the user's machine, only if:
  5784. # - this is a GitHub repo (gh + a github.com remote), AND
  5785. # - `gh auth status` succeeds, AND
  5786. # - the current branch is not main/master (never PR a default branch to itself)
  5787. # It mirrors the proven pattern at autonomy/loki:5524-5527: push the branch,
  5788. # then `gh pr create --head <branch>`. NO auto-merge. Every call is best-effort
  5789. # (`|| true`); failures never block completion. This is a single sanctioned
  5790. # local network call, never CI.
  5791. #
  5792. # Reconciliation with the existing GITHUB_PR path (run.sh create_github_pr,
  5793. # invoked after run_autonomous returns when LOKI_GITHUB_PR=true): if GITHUB_PR
  5794. # is already true we DEFER to that path and do nothing here, so a user who set
  5795. # both knobs never gets a double PR.
  5796. #===============================================================================
  5797. # Write the PR url where an OUT-OF-PROCESS caller can read it.
  5798. #
  5799. # _LOKI_DELEGATE_PR_URL is exported, which reaches children but NOT a sibling
  5800. # step. A GitHub composite action runs `loki start` in one step and reports the
  5801. # result in the next, so an exported variable is invisible to it and the action
  5802. # would have to re-derive the url and could get it wrong. Persisting it means a
  5803. # caller reports what ACTUALLY happened.
  5804. #
  5805. # Best-effort by construction: a failure here must never affect a run whose PR
  5806. # was already opened successfully.
  5807. _loki_persist_pr_url() {
  5808. local _u="${1:-}"
  5809. [ -n "$_u" ] || return 0
  5810. mkdir -p ".loki/state" 2>/dev/null || return 0
  5811. printf '%s\n' "$_u" > ".loki/state/pr-url.txt" 2>/dev/null || true
  5812. return 0
  5813. }
  5814. on_run_complete() {
  5815. # DEFAULT ON as of v9.43.0.
  5816. #
  5817. # This shipped OFF, and the product literally printed "Pull request: not
  5818. # opened (set LOKI_DELEGATE_PR=1 to open one)" -- it knew what the user
  5819. # wanted and asked them to go read documentation instead of doing it. For
  5820. # the core use case ("I give it a GitHub issue and it resolves"), the PR IS
  5821. # the deliverable, so shipping it off meant shipping the product off.
  5822. #
  5823. # Safe to default on because every guard below was already built and is
  5824. # unchanged: requires a GitHub repo AND `gh auth status` AND a non-default
  5825. # branch; it opens a PR and NEVER merges; every call is best-effort so a
  5826. # failure cannot block completion. Opt out with LOKI_DELEGATE_PR=0.
  5827. if [ "${LOKI_DELEGATE_PR:-1}" != "1" ]; then
  5828. return 0
  5829. fi
  5830. # Defer to the existing dedicated PR path to avoid a double PR.
  5831. if [ "${GITHUB_PR:-false}" = "true" ]; then
  5832. return 0
  5833. fi
  5834. # Network-call timeout guard: a stalled network / auth prompt would
  5835. # otherwise hang the completion path indefinitely in --bg. Run each network
  5836. # call through `timeout 30` when available; fall back to the bare call if
  5837. # timeout is not installed (a local wrapper keeps this set -u safe on bash
  5838. # 3.2, where an empty array expansion would error). Keeps every existing
  5839. # `|| true` non-fatal behavior.
  5840. # The push and gh calls below are the trusted post-session step, so they
  5841. # get the withheld GitHub token back (a plain `timeout 30 gh` would exec
  5842. # the binary and bypass the gh() wrapper).
  5843. # Round 5: every call runs from / (_loki_run_neutral) with GH_REPO read as
  5844. # data first, so neither gh nor git loads the agent's repo config while
  5845. # holding the credentials; the push goes through _loki_trusted_push.
  5846. _loki_net() {
  5847. local _net_repo
  5848. _net_repo="$(_loki_trusted_repo)"
  5849. if command -v timeout >/dev/null 2>&1; then
  5850. _loki_with_github_tokens _loki_run_neutral "$_net_repo" timeout 30 "$@"
  5851. else
  5852. _loki_with_github_tokens _loki_run_neutral "$_net_repo" "$@"
  5853. fi
  5854. }
  5855. # Require gh + auth.
  5856. if ! command -v gh >/dev/null 2>&1; then
  5857. return 0
  5858. fi
  5859. if ! (cd "${TARGET_DIR:-.}" && _loki_net gh auth status) >/dev/null 2>&1; then
  5860. return 0
  5861. fi
  5862. # Require a GitHub remote (skip silently on non-GitHub repos).
  5863. local remote_url
  5864. remote_url="$( (cd "${TARGET_DIR:-.}" && git config --get remote.origin.url) 2>/dev/null || true )"
  5865. case "$remote_url" in
  5866. *github.com*) : ;;
  5867. *) return 0 ;;
  5868. esac
  5869. # Resolve current branch; never PR a default branch to itself.
  5870. local branch
  5871. branch="$( (cd "${TARGET_DIR:-.}" && git rev-parse --abbrev-ref HEAD) 2>/dev/null || true )"
  5872. case "$branch" in
  5873. ""|main|master|HEAD) return 0 ;;
  5874. esac
  5875. # the repository's ACTUAL default branch (develop, trunk, ...) is
  5876. # refused inside _loki_trusted_push, for every caller.
  5877. log_info "LOKI_DELEGATE_PR=1: opening a local pull request for branch '$branch'..."
  5878. # Push, then create. Non-interactive (no tty in --bg). Best-effort, each
  5879. # network call bounded by the timeout guard above.
  5880. # A refused or failed push is reported (the refusal names its reason on
  5881. # stderr) and ends the delegate step: there is nothing to open a PR from.
  5882. local _push_rc=0
  5883. _loki_trusted_push _loki_net "${TARGET_DIR:-.}" "$branch" >/dev/null || _push_rc=$?
  5884. if [ "$_push_rc" -ne 0 ]; then
  5885. [ "$_push_rc" -eq 2 ] || log_warn "LOKI_DELEGATE_PR=1: pushing branch '$branch' failed; not opening a pull request."
  5886. return 0
  5887. fi
  5888. # Explicit --repo on every gh call (not only GH_REPO): the repo the push
  5889. # just validated, so no gh subcommand can fall back to cwd detection.
  5890. local _pr_repo
  5891. _pr_repo="$(_loki_trusted_repo)"
  5892. local pr_title
  5893. pr_title="Loki Mode: ${branch}"
  5894. local pr_url=""
  5895. # ENT-4 (idempotent PR): reuse an existing OPEN PR for this head instead of
  5896. # attempting a second create on a platform retry / resume.
  5897. local existing_pr
  5898. existing_pr="$( (cd "${TARGET_DIR:-.}" && _loki_net gh pr list --repo "$_pr_repo" --head "$branch" --state open --json url --jq '.[0].url') 2>/dev/null || true )"
  5899. if [ -n "$existing_pr" ]; then
  5900. _LOKI_DELEGATE_PR_URL="$existing_pr"
  5901. export _LOKI_DELEGATE_PR_URL
  5902. _loki_persist_pr_url "$existing_pr"
  5903. log_info "LOKI_DELEGATE_PR=1: PR already exists for branch '$branch': $existing_pr (skipping create)."
  5904. return 0
  5905. fi
  5906. # Proven PR (Loop 6): append the Evidence Receipt to the inline body.
  5907. # Default-on; LOKI_PROVEN_PR=0 -> body byte-identical to before. This path
  5908. # runs gh from a `cd "${TARGET_DIR:-.}"` subshell, so resolve the proof
  5909. # relative to TARGET_DIR (not the bare-relative helper). Empty
  5910. # expected_head_sha by design (R-DET-1 run_id pointer is the anti-stale guard).
  5911. local _del_body="Opened by Loki Mode (delegate mode). Review locally before merge."
  5912. if [ "${LOKI_PROVEN_PR:-1}" != "0" ] && declare -f render_evidence_receipt_md >/dev/null 2>&1; then
  5913. local _del_loki="${TARGET_DIR:-.}/.loki"
  5914. local _del_idfile="$_del_loki/state/last-proof-id.txt"
  5915. if [ -s "$_del_idfile" ]; then
  5916. local _del_rid=""
  5917. _del_rid="$(cat "$_del_idfile" 2>/dev/null || true)"
  5918. if [ -n "$_del_rid" ] && [ -f "$_del_loki/proofs/$_del_rid/proof.json" ]; then
  5919. local _del_receipt=""
  5920. _del_receipt="$(render_evidence_receipt_md "$_del_loki/proofs/$_del_rid/proof.json" "" "" 2>/dev/null || true)"
  5921. if [ -n "$_del_receipt" ]; then
  5922. _del_body="${_del_body}
  5923. ${_del_receipt}"
  5924. fi
  5925. fi
  5926. fi
  5927. fi
  5928. pr_url="$( (cd "${TARGET_DIR:-.}" && _loki_net gh pr create --repo "$_pr_repo" --title "$pr_title" --body "$_del_body" --head "$branch") 2>/dev/null || true )"
  5929. if [ -n "$pr_url" ]; then
  5930. # Export so build_completion_summary folds the url into the summary.
  5931. _LOKI_DELEGATE_PR_URL="$pr_url"
  5932. export _LOKI_DELEGATE_PR_URL
  5933. _loki_persist_pr_url "$pr_url"
  5934. log_info "Pull request opened: $pr_url"
  5935. else
  5936. log_warn "LOKI_DELEGATE_PR=1: gh pr create did not return a URL (a PR may already exist for this branch)."
  5937. fi
  5938. return 0
  5939. }
  5940. #===============================================================================
  5941. # Parallel Workflow Functions (Git Worktrees)
  5942. #===============================================================================
  5943. # Production bridge for the Bun execution-manifest intelligence. The feature is
  5944. # explicitly opt-in; with LOKI_EXEC_MANIFEST unset this performs no I/O and the
  5945. # legacy parallel workflow remains byte-for-byte on its old path.
  5946. _loki_exec_manifest() {
  5947. [ "${LOKI_EXEC_MANIFEST:-0}" = "1" ] || return 0
  5948. bun --no-env-file --config=/dev/null "${SCRIPT_DIR}/../loki-ts/dist/loki.js" internal exec-manifest "$@"
  5949. }
  5950. init_exec_manifest() {
  5951. [ "${LOKI_EXEC_MANIFEST:-0}" = "1" ] || return 0
  5952. local base_sha plan_file
  5953. # create_worktree's built-in streams branch from main (with HEAD only as its
  5954. # final fallback), so the manifest must pin that exact production base.
  5955. base_sha=$(git -C "$TARGET_DIR" rev-parse main 2>/dev/null) || \
  5956. base_sha=$(git -C "$TARGET_DIR" rev-parse HEAD 2>/dev/null) || return 1
  5957. mkdir -p "${TARGET_DIR}/.loki"
  5958. plan_file=$(mktemp "${TARGET_DIR}/.loki/.exec-manifest-plan.XXXXXX") || return 1
  5959. LOKI_PLAN_FILE="$plan_file" LOKI_BASE_SHA="$base_sha" \
  5960. LOKI_PARALLEL_TESTING_VALUE="$PARALLEL_TESTING" \
  5961. LOKI_PARALLEL_DOCS_VALUE="$PARALLEL_DOCS" \
  5962. LOKI_PARALLEL_BLOG_VALUE="$PARALLEL_BLOG" python3 <<'PY'
  5963. import json, os
  5964. streams = []
  5965. if os.environ["LOKI_PARALLEL_TESTING_VALUE"] == "true":
  5966. streams.append({"name": "testing", "paths": ["tests", "loki-ts/tests"],
  5967. "acceptance": "test stream exits successfully"})
  5968. if os.environ["LOKI_PARALLEL_DOCS_VALUE"] == "true":
  5969. streams.append({"name": "docs", "paths": ["docs", "README.md", "CHANGELOG.md"],
  5970. "acceptance": "documentation stream exits successfully"})
  5971. if os.environ["LOKI_PARALLEL_BLOG_VALUE"] == "true":
  5972. streams.append({"name": "blog", "paths": ["blog"],
  5973. "acceptance": "blog stream exits successfully"})
  5974. with open(os.environ["LOKI_PLAN_FILE"], "w") as f:
  5975. json.dump({"baseSha": os.environ["LOKI_BASE_SHA"],
  5976. "integrationOwner": "parallel-orchestrator",
  5977. "streams": streams,
  5978. "env": {"LOKI_EXEC_MANIFEST": "1"}}, f)
  5979. PY
  5980. _loki_exec_manifest plan "$plan_file" "${TARGET_DIR}/.loki" >/dev/null
  5981. local rc=$?
  5982. rm -f "$plan_file"
  5983. return "$rc"
  5984. }
  5985. validate_exec_manifest_result() {
  5986. local stream_name="$1" branch="$2"
  5987. [ "${LOKI_EXEC_MANIFEST:-0}" = "1" ] || return 0
  5988. local base_sha result_file
  5989. base_sha="${WORKTREE_BASE_SHAS[$stream_name]:-}"
  5990. [ -n "$base_sha" ] || base_sha=$(git -C "$TARGET_DIR" merge-base "$branch" HEAD 2>/dev/null) || return 1
  5991. result_file=$(mktemp "${TARGET_DIR}/.loki/.exec-manifest-result.XXXXXX") || return 1
  5992. LOKI_RESULT_FILE="$result_file" LOKI_RESULT_STREAM="$stream_name" \
  5993. LOKI_RESULT_BASE="$base_sha" LOKI_RESULT_BRANCH="$branch" \
  5994. LOKI_RESULT_REPO="$TARGET_DIR" python3 -E - <<'PY'
  5995. import sys; sys.path[:] = [p for p in sys.path if p not in ("", ".")]
  5996. import json, os, subprocess
  5997. paths = subprocess.check_output(
  5998. ["git", "-C", os.environ["LOKI_RESULT_REPO"], "diff", "--name-only",
  5999. f'{os.environ["LOKI_RESULT_BASE"]}..{os.environ["LOKI_RESULT_BRANCH"]}'],
  6000. text=True).splitlines()
  6001. with open(os.environ["LOKI_RESULT_FILE"], "w") as f:
  6002. json.dump({"name": os.environ["LOKI_RESULT_STREAM"],
  6003. "baseSha": os.environ["LOKI_RESULT_BASE"],
  6004. "changedPaths": paths, "acceptanceMet": True}, f)
  6005. PY
  6006. local outcome rc=0
  6007. outcome=$(_loki_exec_manifest validate "$result_file" "${TARGET_DIR}/.loki" 2>&1) || rc=$?
  6008. rm -f "$result_file"
  6009. if [ "$rc" -ne 0 ]; then
  6010. log_error "Execution manifest rejected $stream_name: $outcome"
  6011. return 1
  6012. fi
  6013. log_info "Execution manifest accepted: $stream_name"
  6014. return 0
  6015. }
  6016. # Check if parallel mode is supported (bash 4+ required for associative arrays)
  6017. check_parallel_support() {
  6018. if [ "$BASH_VERSION_MAJOR" -lt 4 ] 2>/dev/null; then
  6019. log_error "Parallel mode requires bash 4.0+ (current: $BASH_VERSION)"
  6020. log_error "Parallel mode uses associative arrays which require bash 4+"
  6021. log_error ""
  6022. log_error "How to upgrade:"
  6023. log_error " macOS: brew install bash && sudo chsh -s /opt/homebrew/bin/bash"
  6024. log_error " Ubuntu: sudo apt install bash"
  6025. log_error " WSL: Usually has bash 4+ by default"
  6026. log_error ""
  6027. log_error "Or run without --parallel flag for sequential mode (works with bash 3.2+)"
  6028. return 1
  6029. fi
  6030. return 0
  6031. }
  6032. # npm install cache keyed on sha256(package-lock.json + package.json),
  6033. # stored under the project's .loki/cache/install/. Hit: copy cached node_modules.
  6034. # Miss or unusable entry: real install, then publish the entry atomically
  6035. # (temp dir + rename) so concurrent worktrees never see a partial entry.
  6036. # ponytail: npm only; pip installs into the active interpreter, so there is no
  6037. # per-worktree venv to cache. No lockfile means no cache (always install).
  6038. _loki_npm_install_cached() {
  6039. local cache_root="$1" wt="$2" key entry tmp aside old
  6040. if [ ! -f "$wt/package-lock.json" ]; then
  6041. (cd "$wt" && npm install --silent 2>/dev/null) || true
  6042. return 0
  6043. fi
  6044. # node version + platform in the key: native addons are ABI specific
  6045. key="$({ cat "$wt/package-lock.json" "$wt/package.json" 2>/dev/null; node -v 2>/dev/null; uname -sm; } | { shasum -a 256 2>/dev/null || sha256sum; } | cut -d' ' -f1)"
  6046. if [ -n "$key" ]; then
  6047. entry="$cache_root/$key"
  6048. if [ -f "$entry/.complete" ] && [ -d "$entry/node_modules" ]; then
  6049. rm -rf "$wt/node_modules"
  6050. if cp -R "$entry/node_modules" "$wt/node_modules" 2>/dev/null; then
  6051. return 0
  6052. fi
  6053. rm -rf "$wt/node_modules"
  6054. fi
  6055. # Evict only an unusable entry (never a complete one another creator
  6056. # just published), renamed aside first so the removal is not racy.
  6057. if [ -d "$entry" ] && { [ ! -f "$entry/.complete" ] || [ ! -d "$entry/node_modules" ]; }; then
  6058. aside="$cache_root/.evict.$key.$$.$RANDOM"
  6059. mv "$entry" "$aside" 2>/dev/null && rm -rf "$aside" 2>/dev/null
  6060. fi
  6061. fi
  6062. (cd "$wt" && npm install --silent 2>/dev/null) || true
  6063. if [ -n "$key" ] && [ -d "$wt/node_modules" ]; then
  6064. mkdir -p "$cache_root" 2>/dev/null || return 0
  6065. tmp="$(mktemp -d "$cache_root/.tmp.XXXXXX" 2>/dev/null)" || return 0
  6066. if cp -R "$wt/node_modules" "$tmp/node_modules" 2>/dev/null && : >"$tmp/.complete"; then
  6067. # mv onto an existing directory would nest tmp inside it, so
  6068. # publish only when absent, then clean any nesting from a lost race.
  6069. if [ -e "$entry" ]; then
  6070. rm -rf "$tmp"
  6071. else
  6072. mv "$tmp" "$entry" 2>/dev/null
  6073. rm -rf "${entry:?}/${tmp##*/}" "$tmp" 2>/dev/null
  6074. fi
  6075. # ponytail: keep the 3 newest entries by mtime; no LRU touch on hit
  6076. for old in $(ls -t "$cache_root" 2>/dev/null | tail -n +4); do
  6077. aside="$cache_root/.tmp.evict.$old.$$"
  6078. mv "$cache_root/$old" "$aside" 2>/dev/null && rm -rf "$aside"
  6079. done
  6080. else
  6081. rm -rf "$tmp"
  6082. fi
  6083. fi
  6084. return 0
  6085. }
  6086. # copy .loki into a worktree without the install cache (the cache is
  6087. # often hundreds of MB; copy-then-delete would cost more than it saves).
  6088. # Children are copied one by one, so no tar/rsync flavour issues (bash 3.2 safe).
  6089. _loki_copy_state_no_install_cache() {
  6090. local src="$1" dst="$2" e c
  6091. mkdir -p "$dst" || return 1
  6092. for e in "$src"/* "$src"/.[!.]*; do
  6093. [ -e "$e" ] || [ -L "$e" ] || continue
  6094. if [ "${e##*/}" = "cache" ] && [ -d "$e" ] && [ ! -L "$e" ]; then
  6095. mkdir -p "$dst/cache"
  6096. for c in "$e"/* "$e"/.[!.]*; do
  6097. [ -e "$c" ] || [ -L "$c" ] || continue
  6098. [ "${c##*/}" = "install" ] && continue
  6099. cp -R "$c" "$dst/cache/" 2>/dev/null || true
  6100. done
  6101. else
  6102. cp -R "$e" "$dst/" 2>/dev/null || true
  6103. fi
  6104. done
  6105. return 0
  6106. }
  6107. # Create a worktree for a specific stream
  6108. create_worktree() {
  6109. local stream_name="$1"
  6110. local branch_name="${2:-}"
  6111. local project_name=$(basename "$TARGET_DIR")
  6112. local worktree_path="${TARGET_DIR}/../${project_name}-${stream_name}"
  6113. if [ -d "$worktree_path" ]; then
  6114. log_info "Worktree already exists: $stream_name"
  6115. WORKTREE_PATHS[$stream_name]="$worktree_path"
  6116. if [ "${LOKI_EXEC_MANIFEST:-0}" = "1" ]; then
  6117. WORKTREE_BASE_SHAS[$stream_name]="$(python3 -c "import json; print(json.load(open('${TARGET_DIR}/.loki/manifest/exec-manifest.json'))['base_sha'])" 2>/dev/null)"
  6118. fi
  6119. return 0
  6120. fi
  6121. log_step "Creating worktree: $stream_name"
  6122. local wt_exit=1
  6123. if [ -n "$branch_name" ]; then
  6124. # Create new branch
  6125. git -C "$TARGET_DIR" worktree add "$worktree_path" -b "$branch_name" 2>/dev/null && wt_exit=0 || \
  6126. { git -C "$TARGET_DIR" worktree add "$worktree_path" "$branch_name" 2>/dev/null && wt_exit=0; }
  6127. else
  6128. # BUG-PAR-001: Testing/docs worktrees use -b parallel-<stream> main (not bare main checkout)
  6129. # This avoids "already checked out" errors and keeps each worktree on its own branch
  6130. git -C "$TARGET_DIR" worktree add "$worktree_path" -b "parallel-${stream_name}" main 2>/dev/null && wt_exit=0 || \
  6131. { git -C "$TARGET_DIR" worktree add "$worktree_path" "parallel-${stream_name}" 2>/dev/null && wt_exit=0; } || \
  6132. { git -C "$TARGET_DIR" worktree add "$worktree_path" HEAD 2>/dev/null && wt_exit=0; }
  6133. fi
  6134. if [ $wt_exit -eq 0 ]; then
  6135. WORKTREE_PATHS[$stream_name]="$worktree_path"
  6136. WORKTREE_BASE_SHAS[$stream_name]="$(git -C "$worktree_path" rev-parse HEAD 2>/dev/null)"
  6137. # Copy .loki state to worktree
  6138. if [ -d "$TARGET_DIR/.loki" ]; then
  6139. # never copy the install cache into the worktree
  6140. _loki_copy_state_no_install_cache "$TARGET_DIR/.loki" "$worktree_path/.loki"
  6141. fi
  6142. # Initialize environment (detect and run appropriate install)
  6143. (
  6144. cd "$worktree_path" || exit 1
  6145. if [ -f "package.json" ]; then
  6146. _loki_npm_install_cached "${TARGET_DIR}/.loki/cache/install" "$worktree_path"
  6147. elif [ -f "requirements.txt" ]; then
  6148. pip install -r requirements.txt -q 2>/dev/null || true
  6149. elif [ -f "Cargo.toml" ]; then
  6150. cargo build --quiet 2>/dev/null || true
  6151. fi
  6152. ) &
  6153. # Capture install PID for cleanup on exit
  6154. WORKTREE_INSTALL_PIDS+=($!)
  6155. register_pid "$!" "worktree-install" "stream=$stream_name"
  6156. log_info "Created worktree: $worktree_path"
  6157. return 0
  6158. else
  6159. log_error "Failed to create worktree: $stream_name"
  6160. # BUG-PU-001: Clean up partial worktree on creation failure
  6161. if [ -d "$worktree_path" ]; then
  6162. git -C "$TARGET_DIR" worktree remove "$worktree_path" --force 2>/dev/null || \
  6163. rm -rf "$worktree_path" 2>/dev/null || true
  6164. fi
  6165. # Clean up any orphaned branch created during the attempt
  6166. if [ -n "$branch_name" ]; then
  6167. git -C "$TARGET_DIR" branch -D "$branch_name" 2>/dev/null || true
  6168. else
  6169. git -C "$TARGET_DIR" branch -D "parallel-${stream_name}" 2>/dev/null || true
  6170. fi
  6171. return 1
  6172. fi
  6173. }
  6174. # Remove a worktree
  6175. remove_worktree() {
  6176. local stream_name="$1"
  6177. local worktree_path="${WORKTREE_PATHS[$stream_name]:-}"
  6178. if [ -z "$worktree_path" ] || [ ! -d "$worktree_path" ]; then
  6179. return 0
  6180. fi
  6181. log_step "Removing worktree: $stream_name"
  6182. # Kill any running Claude session
  6183. local pid="${WORKTREE_PIDS[$stream_name]:-}"
  6184. if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
  6185. kill "$pid" 2>/dev/null || true
  6186. wait "$pid" 2>/dev/null || true
  6187. fi
  6188. # Remove worktree (with safety check for rm -rf)
  6189. git -C "$TARGET_DIR" worktree remove "$worktree_path" --force 2>/dev/null || {
  6190. # BUG-PAR-005: Safety check uses dirname with trailing / to prevent prefix-match false positives
  6191. # e.g. TARGET_DIR=/foo/bar must not match /foo/bar-other
  6192. local parent_dir
  6193. parent_dir="$(dirname "$TARGET_DIR")/"
  6194. if [[ -n "$worktree_path" && "$worktree_path" != "/" && "$worktree_path" == "${parent_dir}"* ]]; then
  6195. rm -rf "$worktree_path" 2>/dev/null
  6196. else
  6197. log_warn "Skipping unsafe rm -rf for path: $worktree_path"
  6198. fi
  6199. }
  6200. unset "WORKTREE_PATHS[$stream_name]"
  6201. unset "WORKTREE_PIDS[$stream_name]"
  6202. unset "WORKTREE_BASE_SHAS[$stream_name]"
  6203. log_info "Removed worktree: $stream_name"
  6204. }
  6205. # Compute the effective parallel-session cap for the current scheduling pass.
  6206. # Default-off contract: when LOKI_DYNAMIC_CONCURRENCY is not "1" this echoes
  6207. # exactly MAX_PARALLEL_SESSIONS with zero file reads and zero subprocesses, so
  6208. # the spawn decision is byte-identical to the pre-feature behavior.
  6209. # When enabled, it starts from the configured ceiling and scales DOWN based on
  6210. # .loki/state/resources.json. All reads are best-effort: a missing, empty, or
  6211. # unparseable file (or non-numeric values) leaves the cap at the ceiling. The
  6212. # result is always clamped to the range [1, ceiling] and never exceeds it.
  6213. effective_session_cap() {
  6214. # Fast default-off path: identical to today, no I/O, no subprocesses.
  6215. if [ "${DYNAMIC_CONCURRENCY:-0}" != "1" ]; then
  6216. echo "$MAX_PARALLEL_SESSIONS"
  6217. return 0
  6218. fi
  6219. # Ceiling is the upper bound when dynamic scaling is on.
  6220. local ceiling="${MAX_PARALLEL_SESSIONS_CEILING:-$MAX_PARALLEL_SESSIONS}"
  6221. # Guard against a non-numeric or sub-1 ceiling override.
  6222. case "$ceiling" in
  6223. ''|*[!0-9]*) ceiling="$MAX_PARALLEL_SESSIONS" ;;
  6224. esac
  6225. [ "$ceiling" -lt 1 ] 2>/dev/null && ceiling=1
  6226. local cap="$ceiling"
  6227. local resources_file=".loki/state/resources.json"
  6228. # No resource data -> best-effort, leave at ceiling.
  6229. if [ ! -f "$resources_file" ]; then
  6230. echo "$cap"
  6231. return 0
  6232. fi
  6233. # Read usage and status best-effort. Defaults keep the cap at the ceiling
  6234. # if the file is empty, malformed, or missing keys.
  6235. local cpu_usage mem_usage status
  6236. cpu_usage=$(python3 -c "import json; print(json.load(open('$resources_file')).get('cpu', {}).get('usage_percent', 0))" 2>/dev/null || echo "0")
  6237. mem_usage=$(python3 -c "import json; print(json.load(open('$resources_file')).get('memory', {}).get('usage_percent', 0))" 2>/dev/null || echo "0")
  6238. status=$(python3 -c "import json; print(json.load(open('$resources_file')).get('overall_status', 'ok'))" 2>/dev/null || echo "ok")
  6239. # usage_percent can be a float (e.g. 85.3). Reduce to an integer part for
  6240. # comparison and fall back to 0 if anything is non-numeric.
  6241. cpu_usage="${cpu_usage%%.*}"
  6242. mem_usage="${mem_usage%%.*}"
  6243. case "$cpu_usage" in ''|*[!0-9]*) cpu_usage=0 ;; esac
  6244. case "$mem_usage" in ''|*[!0-9]*) mem_usage=0 ;; esac
  6245. local crit="${CONCURRENCY_CRITICAL_THRESHOLD:-95}"
  6246. local cpu_thr="${CONCURRENCY_CPU_THRESHOLD:-85}"
  6247. local mem_thr="${CONCURRENCY_MEM_THRESHOLD:-85}"
  6248. if [ "$cpu_usage" -ge "$crit" ] || [ "$mem_usage" -ge "$crit" ]; then
  6249. # Critical pressure: drop to a single session.
  6250. cap=1
  6251. elif [ "$cpu_usage" -ge "$cpu_thr" ] || [ "$mem_usage" -ge "$mem_thr" ] || [ "$status" != "ok" ]; then
  6252. # Elevated pressure or a non-ok overall status: halve (integer floor).
  6253. cap=$(( ceiling / 2 ))
  6254. fi
  6255. # Clamp to [1, ceiling]. Never runaway, never zero.
  6256. [ "$cap" -lt 1 ] && cap=1
  6257. [ "$cap" -gt "$ceiling" ] && cap="$ceiling"
  6258. echo "$cap"
  6259. return 0
  6260. }
  6261. # Auto-flag parity for parallel worktree Claude sessions (wave-5 fix).
  6262. # The main RARV loop applies adaptive cost/resilience flags to its claude
  6263. # invocation (run.sh:16007-16030 effort/max-budget/fallback) plus an MCP
  6264. # bundle, but the parallel worktree spawn shipped a bare invocation that missed
  6265. # them entirely. This populates the global _LOKI_WT_AUTO_FLAGS array with ONLY
  6266. # the flags that are safe on the plain (non stream-json) worktree invocation:
  6267. # --effort adaptive reasoning depth (dev tier)
  6268. # --max-budget-usd per-call hard backstop
  6269. # --fallback-model resilience to model overload/unavailability
  6270. # --mcp-config (+ --strict-mcp-config) the same MCP bundle the Bun
  6271. # route emits; a SUPERSET of the bash main loop, which does
  6272. # NOT emit --mcp-config (see ledger run.sh:15990-15996).
  6273. # Worktree dev streams benefit from the bundled MCP servers.
  6274. # We deliberately do NOT replicate the stream-json-coupled flags
  6275. # (--include-hook-events, --include-partial-messages) nor --output-format /
  6276. # --session-id: this invocation logs free-form text, not parsed stream-json, so
  6277. # those would be invalid or unwanted here. Each flag is gated on CLI support +
  6278. # its opt-out env var, matching the main loop. Extracted from spawn_worktree_session
  6279. # so it is unit-testable (tests/test-worktree-auto-flags.sh).
  6280. _loki_build_worktree_claude_flags() {
  6281. _LOKI_WT_AUTO_FLAGS=()
  6282. # Non-claude providers never receive these flags.
  6283. if [ "${PROVIDER_NAME:-claude}" != "claude" ]; then
  6284. return 0
  6285. fi
  6286. # Dev-tier model param drives the fallback derivation (worktree streams are
  6287. # development work). Resolve via the provider helper when available.
  6288. local _loki_wt_primary=""
  6289. if type provider_get_tier_param >/dev/null 2>&1; then
  6290. _loki_wt_primary="$(provider_get_tier_param development 2>/dev/null || true)"
  6291. fi
  6292. if [ "${LOKI_AUTO_EFFORT:-on}" != "off" ] \
  6293. && type loki_effort_for_tier >/dev/null 2>&1 \
  6294. && type loki_claude_flag_supported >/dev/null 2>&1 \
  6295. && loki_claude_flag_supported "--effort"; then
  6296. local _loki_wt_effort
  6297. _loki_wt_effort="$(loki_effort_for_tier development "${DETECTED_COMPLEXITY:-${LOKI_COMPLEXITY:-standard}}")"
  6298. [ -n "$_loki_wt_effort" ] && _LOKI_WT_AUTO_FLAGS+=("--effort" "$_loki_wt_effort")
  6299. fi
  6300. if [ "${LOKI_AUTO_BUDGET:-on}" != "off" ] \
  6301. && type loki_remaining_budget >/dev/null 2>&1 \
  6302. && type loki_claude_flag_supported >/dev/null 2>&1 \
  6303. && loki_claude_flag_supported "--max-budget-usd"; then
  6304. local _loki_wt_rem
  6305. _loki_wt_rem="$(loki_remaining_budget)"
  6306. [ -n "$_loki_wt_rem" ] && _LOKI_WT_AUTO_FLAGS+=("--max-budget-usd" "$_loki_wt_rem")
  6307. fi
  6308. if [ "${LOKI_AUTO_FALLBACK:-on}" != "off" ] \
  6309. && [ -n "$_loki_wt_primary" ] \
  6310. && type loki_fallback_for_primary >/dev/null 2>&1 \
  6311. && type loki_claude_flag_supported >/dev/null 2>&1 \
  6312. && loki_claude_flag_supported "--fallback-model"; then
  6313. local _loki_wt_fb
  6314. _loki_wt_fb="$(loki_fallback_for_primary "$_loki_wt_primary")"
  6315. [ -n "$_loki_wt_fb" ] && _LOKI_WT_AUTO_FLAGS+=("--fallback-model" "$_loki_wt_fb")
  6316. fi
  6317. if type loki_mcp_config_argv >/dev/null 2>&1 \
  6318. && type loki_claude_flag_supported >/dev/null 2>&1 \
  6319. && loki_claude_flag_supported "--mcp-config"; then
  6320. local _loki_wt_mcp
  6321. if _loki_wt_mcp="$(loki_mcp_config_argv)" && [ -n "$_loki_wt_mcp" ]; then
  6322. _LOKI_WT_AUTO_FLAGS+=("--mcp-config")
  6323. local _loki_wt_mcp_path
  6324. for _loki_wt_mcp_path in $_loki_wt_mcp; do
  6325. _LOKI_WT_AUTO_FLAGS+=("$_loki_wt_mcp_path")
  6326. done
  6327. # --strict-mcp-config only alongside a real bundle, never bare.
  6328. if [ "${LOKI_STRICT_MCP:-1}" != "0" ] \
  6329. && loki_claude_flag_supported "--strict-mcp-config"; then
  6330. _LOKI_WT_AUTO_FLAGS+=("--strict-mcp-config")
  6331. fi
  6332. fi
  6333. fi
  6334. return 0
  6335. }
  6336. # Spawn a Claude session in a worktree
  6337. spawn_worktree_session() {
  6338. local stream_name="$1"
  6339. local task_prompt="$2"
  6340. local worktree_path="${WORKTREE_PATHS[$stream_name]:-}"
  6341. if [ -z "$worktree_path" ] || [ ! -d "$worktree_path" ]; then
  6342. log_error "Worktree not found: $stream_name"
  6343. return 1
  6344. fi
  6345. # Check if session limit reached
  6346. local active_count=0
  6347. for pid in "${WORKTREE_PIDS[@]}"; do
  6348. if kill -0 "$pid" 2>/dev/null; then
  6349. ((active_count++))
  6350. fi
  6351. done
  6352. local session_cap
  6353. session_cap=$(effective_session_cap)
  6354. if [ "$active_count" -ge "$session_cap" ]; then
  6355. # BUG-PAR-014: Max-sessions rejection queues spawn for retry
  6356. log_warn "Max parallel sessions reached ($session_cap). Queuing $stream_name for retry."
  6357. mkdir -p "${TARGET_DIR:-.}/.loki/signals"
  6358. echo "{\"stream\":\"$stream_name\",\"task\":\"$(echo "$task_prompt" | head -c 200)\",\"timestamp\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"}" \
  6359. > "${TARGET_DIR:-.}/.loki/signals/SPAWN_QUEUED_${stream_name}"
  6360. return 1
  6361. fi
  6362. local log_file="$worktree_path/.loki/logs/session-${stream_name}.log"
  6363. mkdir -p "$(dirname "$log_file")"
  6364. # Check provider parallel support
  6365. if [ "${PROVIDER_HAS_PARALLEL:-false}" != "true" ]; then
  6366. log_warn "Provider ${PROVIDER_NAME:-unknown} does not support parallel sessions"
  6367. log_warn "Running sequentially instead (degraded mode)"
  6368. return 1
  6369. fi
  6370. log_step "Spawning ${PROVIDER_DISPLAY_NAME:-Claude} session: $stream_name"
  6371. # Build the worktree auto-flag set (effort/max-budget/fallback/mcp-config)
  6372. # BEFORE the ( subshell so it is inherited (a `bash -c` would not). Populates
  6373. # the global _LOKI_WT_AUTO_FLAGS array. See _loki_build_worktree_claude_flags.
  6374. _loki_build_worktree_claude_flags
  6375. (
  6376. cd "$worktree_path" || exit 1
  6377. _wt_exit=0
  6378. # Provider-specific invocation for parallel sessions
  6379. case "${PROVIDER_NAME:-claude}" in
  6380. claude)
  6381. # caveman ACTIVATION (free-form): a parallel worktree dev stream
  6382. # is free-form generation; its output goes to a log, not a parsed
  6383. # sentinel. Activate compression at the configured level when
  6384. # warranted (empty -> bare invocation, byte-identical to before).
  6385. # Type-guarded; inlined per-invocation (not exported).
  6386. local _loki_wt_cm=""
  6387. if type loki_caveman_activate_env >/dev/null 2>&1; then
  6388. _loki_wt_cm="$(loki_caveman_activate_env)"
  6389. fi
  6390. # Expand the auto-flag array with the bash-3.2 empty-array guard
  6391. # (${arr[@]+...}) so a bare "${arr[@]}" under set -u does not
  6392. # abort with "unbound variable" when no flags were collected.
  6393. _LOKI_PERM_ARGV=(--dangerously-skip-permissions)
  6394. ! type _loki_claude_permission_argv >/dev/null 2>&1 || _loki_claude_permission_argv
  6395. if [ -n "$_loki_wt_cm" ]; then
  6396. SHELL="$(_loki_claude_shell_value)" CAVEMAN_DEFAULT_MODE="$_loki_wt_cm" claude "${_LOKI_PERM_ARGV[@]}" \
  6397. "${_LOKI_WT_AUTO_FLAGS[@]+"${_LOKI_WT_AUTO_FLAGS[@]}"}" \
  6398. -p "Loki Mode: $task_prompt. Read .loki/CONTINUITY.md for context." \
  6399. >> "$log_file" 2>&1 || _wt_exit=$?
  6400. else
  6401. SHELL="$(_loki_claude_shell_value)" claude "${_LOKI_PERM_ARGV[@]}" \
  6402. "${_LOKI_WT_AUTO_FLAGS[@]+"${_LOKI_WT_AUTO_FLAGS[@]}"}" \
  6403. -p "Loki Mode: $task_prompt. Read .loki/CONTINUITY.md for context." \
  6404. >> "$log_file" 2>&1 || _wt_exit=$?
  6405. fi
  6406. ;;
  6407. *)
  6408. log_error "Unknown provider: ${PROVIDER_NAME}"
  6409. _wt_exit=1
  6410. ;;
  6411. esac
  6412. # Completion signaling (v6.7.0)
  6413. if [ $_wt_exit -eq 0 ]; then
  6414. # BUG-PAR-006: git add excludes .env, *.key, *.pem, credentials*
  6415. git -C "$worktree_path" add -A \
  6416. ':!.env' ':!*.key' ':!*.pem' ':!credentials*' 2>/dev/null
  6417. git -C "$worktree_path" commit -m "feat($stream_name): worktree work complete" 2>/dev/null || true
  6418. # BUG-PAR-008: Signal files written atomically (temp + mv)
  6419. mkdir -p "${TARGET_DIR:-.}/.loki/signals"
  6420. local _sig_tmp
  6421. _sig_tmp=$(mktemp "${TARGET_DIR:-.}/.loki/signals/.tmp.XXXXXX") || true
  6422. cat > "$_sig_tmp" <<EOSIG
  6423. {"stream":"$stream_name","branch":"$(git -C "$worktree_path" branch --show-current 2>/dev/null)","worktree":"$worktree_path","timestamp":"$(date -u +%Y-%m-%dT%H:%M:%SZ)","exit_code":$_wt_exit}
  6424. EOSIG
  6425. mv "$_sig_tmp" "${TARGET_DIR:-.}/.loki/signals/MERGE_REQUESTED_${stream_name}" 2>/dev/null || \
  6426. cp "$_sig_tmp" "${TARGET_DIR:-.}/.loki/signals/MERGE_REQUESTED_${stream_name}" 2>/dev/null
  6427. rm -f "$_sig_tmp" 2>/dev/null
  6428. echo "WORKTREE_COMPLETE: $stream_name" >> "$log_file"
  6429. else
  6430. # BUG-PAR-008: Signal files written atomically (temp + mv)
  6431. mkdir -p "${TARGET_DIR:-.}/.loki/signals"
  6432. local _fail_tmp
  6433. _fail_tmp=$(mktemp "${TARGET_DIR:-.}/.loki/signals/.tmp.XXXXXX") || true
  6434. echo "{\"stream\":\"$stream_name\",\"status\":\"failed\",\"exit_code\":$_wt_exit,\"timestamp\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"}" \
  6435. > "$_fail_tmp"
  6436. mv "$_fail_tmp" "${TARGET_DIR:-.}/.loki/signals/WORKTREE_FAILED_${stream_name}" 2>/dev/null || \
  6437. cp "$_fail_tmp" "${TARGET_DIR:-.}/.loki/signals/WORKTREE_FAILED_${stream_name}" 2>/dev/null
  6438. rm -f "$_fail_tmp" 2>/dev/null
  6439. fi
  6440. ) &
  6441. local pid=$!
  6442. WORKTREE_PIDS[$stream_name]=$pid
  6443. register_pid "$pid" "worktree-session" "stream=$stream_name"
  6444. log_info "Session spawned: $stream_name (PID: $pid)"
  6445. return 0
  6446. }
  6447. # Merge a completed worktree back to main branch (v6.7.0)
  6448. # Usage: merge_worktree <stream_name>
  6449. merge_worktree() {
  6450. local stream_name="$1"
  6451. local signal_file="${TARGET_DIR:-.}/.loki/signals/MERGE_REQUESTED_${stream_name}"
  6452. if [ ! -f "$signal_file" ]; then
  6453. log_error "No merge signal found for: $stream_name"
  6454. return 1
  6455. fi
  6456. # BUG-PAR-013: Signal file parsing falls back to jq when python3 unavailable
  6457. local branch worktree_path
  6458. branch=$(python3 -c "import json; print(json.load(open('$signal_file'))['branch'])" 2>/dev/null) || \
  6459. branch=$(jq -r '.branch' "$signal_file" 2>/dev/null) || true
  6460. worktree_path=$(python3 -c "import json; print(json.load(open('$signal_file'))['worktree'])" 2>/dev/null) || \
  6461. worktree_path=$(jq -r '.worktree' "$signal_file" 2>/dev/null) || true
  6462. if [ -z "$branch" ]; then
  6463. log_error "Could not determine branch for: $stream_name"
  6464. return 1
  6465. fi
  6466. log_step "Merging worktree: $stream_name (branch: $branch)"
  6467. # Validate the actual branch result at the integration seam, immediately
  6468. # before any checkout or merge can mutate the integration tree.
  6469. if ! validate_exec_manifest_result "$stream_name" "$branch"; then
  6470. return 1
  6471. fi
  6472. # BUG-PAR-009: Verify git checkout main before merge
  6473. local current_branch
  6474. current_branch=$(git -C "${TARGET_DIR:-.}" branch --show-current 2>/dev/null)
  6475. if [ "$current_branch" != "main" ]; then
  6476. log_info "Switching to main before merge (was on: $current_branch)"
  6477. if ! git -C "${TARGET_DIR:-.}" checkout main 2>/dev/null; then
  6478. log_error "Failed to checkout main for merge: $stream_name"
  6479. return 1
  6480. fi
  6481. fi
  6482. if git -C "${TARGET_DIR:-.}" merge --no-ff "$branch" -m "merge($stream_name): auto-merge from parallel worktree" 2>&1; then
  6483. log_info "Merge successful: $stream_name"
  6484. # Clean up signal and worktree
  6485. rm -f "$signal_file"
  6486. if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then
  6487. git -C "${TARGET_DIR:-.}" worktree remove "$worktree_path" --force 2>/dev/null || true
  6488. git -C "${TARGET_DIR:-.}" branch -d "$branch" 2>/dev/null || true
  6489. fi
  6490. return 0
  6491. else
  6492. log_error "Merge conflict for $stream_name - manual resolution needed"
  6493. git -C "${TARGET_DIR:-.}" merge --abort 2>/dev/null || true
  6494. return 1
  6495. fi
  6496. }
  6497. # Check and process all pending merge signals (v6.7.0)
  6498. process_pending_merges() {
  6499. local signals_dir="${TARGET_DIR:-.}/.loki/signals"
  6500. local merged=0
  6501. local failed=0
  6502. for signal_file in "$signals_dir"/MERGE_REQUESTED_*; do
  6503. case "$signal_file" in *.ack) continue ;; esac
  6504. [ -f "$signal_file" ] || continue
  6505. local stream_name
  6506. stream_name=$(basename "$signal_file" | sed 's/MERGE_REQUESTED_//')
  6507. if merge_worktree "$stream_name"; then
  6508. ((merged++))
  6509. else
  6510. ((failed++))
  6511. fi
  6512. done
  6513. if [ $merged -gt 0 ] || [ $failed -gt 0 ]; then
  6514. log_info "Merge results: $merged successful, $failed failed"
  6515. fi
  6516. }
  6517. # List all active worktrees
  6518. list_worktrees() {
  6519. log_header "Active Worktrees"
  6520. git -C "$TARGET_DIR" worktree list 2>/dev/null
  6521. echo ""
  6522. log_info "Tracked sessions:"
  6523. for stream in "${!WORKTREE_PIDS[@]}"; do
  6524. local pid="${WORKTREE_PIDS[$stream]}"
  6525. local status="stopped"
  6526. if kill -0 "$pid" 2>/dev/null; then
  6527. status="running"
  6528. fi
  6529. echo " [$stream] PID: $pid - $status"
  6530. done
  6531. }
  6532. # Check for completed features ready to merge
  6533. check_merge_queue() {
  6534. local signals_dir="$TARGET_DIR/.loki/signals"
  6535. if [ ! -d "$signals_dir" ]; then
  6536. return 0
  6537. fi
  6538. for signal in "$signals_dir"/MERGE_REQUESTED_*; do
  6539. # Skip our own acknowledgement markers (*.ack) so they are not treated
  6540. # as signals themselves.
  6541. case "$signal" in *.ack) continue ;; esac
  6542. if [ -f "$signal" ]; then
  6543. local feature=$(basename "$signal" | sed 's/MERGE_REQUESTED_//')
  6544. if [ "$AUTO_MERGE" = "true" ]; then
  6545. # Auto-merge consumes the signal (merge_feature rm's it), so the
  6546. # log line fires exactly once per completed feature.
  6547. log_info "Merge requested: $feature"
  6548. merge_feature "$feature"
  6549. else
  6550. # Auto-merge is OFF (e.g. `--pr` without `--ship`): the PR is left
  6551. # for a human to merge, so the signal is INTENTIONALLY not consumed.
  6552. # Log ONCE (guarded by a sibling .ack marker) instead of re-logging
  6553. # the same "Merge requested" line every orchestrator pass -- that
  6554. # re-log was a 90-minute "looks stuck" spam while real work was
  6555. # already done. The signal itself is preserved untouched.
  6556. if [ ! -f "${signal}.ack" ]; then
  6557. log_info "Merge requested: $feature (PR ready; auto-merge off -- merge the PR, or use --ship to auto-merge)"
  6558. : > "${signal}.ack" 2>/dev/null || true
  6559. fi
  6560. fi
  6561. fi
  6562. done
  6563. }
  6564. # AI-powered conflict resolution (inspired by Auto-Claude)
  6565. resolve_conflicts_with_ai() {
  6566. local feature="$1"
  6567. local conflict_files=$(git diff --name-only --diff-filter=U 2>/dev/null)
  6568. if [ -z "$conflict_files" ]; then
  6569. return 0
  6570. fi
  6571. log_step "AI-powered conflict resolution for: $feature"
  6572. for file in $conflict_files; do
  6573. log_info "Resolving conflicts in: $file"
  6574. # Get conflict markers
  6575. local conflict_content=$(cat "$file")
  6576. # Use AI to resolve conflict (provider-aware)
  6577. local resolution=""
  6578. local conflict_prompt="You are resolving a git merge conflict. The file below contains conflict markers.
  6579. Your task is to merge both changes intelligently, preserving functionality from both sides.
  6580. FILE: $file
  6581. CONTENT:
  6582. $conflict_content
  6583. Output ONLY the resolved file content with no conflict markers. No explanations."
  6584. case "${PROVIDER_NAME:-claude}" in
  6585. claude)
  6586. # EMBED 2 (v7.33.0): --bare on this cheap NON-MAIN subcall.
  6587. # Reasoning: $conflict_prompt is fully self-contained -- it
  6588. # carries the complete instruction set AND the entire conflicted
  6589. # file content inline, and the agent's output is captured to a
  6590. # variable (the shell, not the agent, writes the resolved file).
  6591. # It needs no hooks, LSP, CLAUDE.md auto-discovery, or MCP, so
  6592. # --bare is safe and cheaper. Gated + opt-out LOKI_BARE_SUBCALLS=0.
  6593. _LOKI_PERM_ARGV=(--dangerously-skip-permissions)
  6594. ! type _loki_claude_permission_argv >/dev/null 2>&1 || _loki_claude_permission_argv
  6595. local _cr_argv=("${_LOKI_PERM_ARGV[@]}")
  6596. if type loki_subcall_bare_enabled >/dev/null 2>&1 && loki_subcall_bare_enabled; then
  6597. _cr_argv+=("--bare")
  6598. fi
  6599. # caveman HARD-SUPPRESS (parsed output): the output is captured as
  6600. # the EXACT resolved file content (the shell writes it verbatim).
  6601. # Compressing prose into the merged source would corrupt the file,
  6602. # so disable caveman unconditionally here. No-op when absent.
  6603. resolution=$(SHELL="$(_loki_claude_shell_value)" CAVEMAN_DEFAULT_MODE=off claude "${_cr_argv[@]}" -p "$conflict_prompt" --output-format text < /dev/null 2>/dev/null)
  6604. ;;
  6605. *)
  6606. log_error "Unknown provider: ${PROVIDER_NAME}"
  6607. return 1
  6608. ;;
  6609. esac
  6610. if [ -n "$resolution" ]; then
  6611. echo "$resolution" > "$file"
  6612. git add "$file"
  6613. log_info "Resolved: $file"
  6614. else
  6615. log_error "AI resolution failed for: $file"
  6616. return 1
  6617. fi
  6618. done
  6619. return 0
  6620. }
  6621. # Merge a completed feature branch (with AI conflict resolution)
  6622. # BUG-PAR-011: Not in a subshell -- uses git -C instead of cd
  6623. # BUG-PAR-003: Strips feature- prefix to avoid feature/feature-auth double-prefix
  6624. merge_feature() {
  6625. local feature="$1"
  6626. # BUG-PAR-003: Strip feature- prefix if present to avoid double-prefix (feature/feature-auth)
  6627. local clean_feature="${feature#feature-}"
  6628. local branch="feature/$clean_feature"
  6629. # The session signal is authoritative for worktree branches (for example
  6630. # parallel-testing). The historical feature/<name> convention remains the
  6631. # fallback for legacy feature streams.
  6632. local _mf_signal="$TARGET_DIR/.loki/signals/MERGE_REQUESTED_$feature"
  6633. if [ -f "$_mf_signal" ]; then
  6634. local _mf_branch=""
  6635. _mf_branch=$(LOKI_SIGNAL_FILE="$_mf_signal" python3 -c \
  6636. "import json,os; print(json.load(open(os.environ['LOKI_SIGNAL_FILE'])).get('branch',''))" 2>/dev/null || true)
  6637. [ -n "$_mf_branch" ] && branch="$_mf_branch"
  6638. fi
  6639. log_step "Merging feature: $clean_feature"
  6640. # This is the autonomous orchestrator's real integration seam. Reject a
  6641. # stale, out-of-scope, unknown, or unsuccessful stream before checkout/merge.
  6642. if ! validate_exec_manifest_result "$feature" "$branch"; then
  6643. return 1
  6644. fi
  6645. # BUG-PAR-011: Ensure we're on main using git -C (no subshell)
  6646. git -C "$TARGET_DIR" checkout main 2>/dev/null
  6647. # Attempt merge with no-ff for clear history
  6648. if git -C "$TARGET_DIR" merge "$branch" --no-ff -m "feat: Merge $clean_feature" 2>/dev/null; then
  6649. log_info "Merged cleanly: $clean_feature"
  6650. else
  6651. # Merge has conflicts - try AI resolution
  6652. log_warn "Merge conflicts detected - attempting AI resolution"
  6653. if resolve_conflicts_with_ai "$clean_feature"; then
  6654. # AI resolved conflicts, commit the merge
  6655. git -C "$TARGET_DIR" commit -m "feat: Merge $clean_feature (AI-resolved conflicts)"
  6656. audit_agent_action "git_commit" "Committed changes" "merge=$clean_feature,resolution=ai"
  6657. log_info "Merged with AI conflict resolution: $clean_feature"
  6658. else
  6659. # AI resolution failed, abort merge
  6660. log_error "AI conflict resolution failed: $clean_feature"
  6661. git -C "$TARGET_DIR" merge --abort 2>/dev/null || true
  6662. return 1
  6663. fi
  6664. fi
  6665. # Remove signal (and its log-once .ack marker, if any)
  6666. rm -f "$TARGET_DIR/.loki/signals/MERGE_REQUESTED_$feature" \
  6667. "$TARGET_DIR/.loki/signals/MERGE_REQUESTED_$feature.ack"
  6668. # Remove worktree
  6669. remove_worktree "feature-$clean_feature"
  6670. # Delete branch
  6671. git -C "$TARGET_DIR" branch -d "$branch" 2>/dev/null || true
  6672. # DOCS_NEEDED signal: triggers the parallel docs worktree to run `loki docs update`
  6673. # and regenerate documentation for recently changed files.
  6674. mkdir -p "$TARGET_DIR/.loki/signals"
  6675. touch "$TARGET_DIR/.loki/signals/DOCS_NEEDED"
  6676. }
  6677. # Initialize parallel workflow streams
  6678. init_parallel_streams() {
  6679. # Check bash version
  6680. if ! check_parallel_support; then
  6681. return 1
  6682. fi
  6683. log_header "Initializing Parallel Workflows"
  6684. if ! init_exec_manifest; then
  6685. log_error "Failed to initialize execution manifest"
  6686. return 1
  6687. fi
  6688. local active_streams=0
  6689. # Create testing worktree (always tracks main)
  6690. if [ "$PARALLEL_TESTING" = "true" ]; then
  6691. create_worktree "testing"
  6692. ((active_streams++))
  6693. fi
  6694. # Create documentation worktree
  6695. if [ "$PARALLEL_DOCS" = "true" ]; then
  6696. create_worktree "docs"
  6697. ((active_streams++))
  6698. fi
  6699. # Create blog worktree if enabled
  6700. if [ "$PARALLEL_BLOG" = "true" ]; then
  6701. create_worktree "blog"
  6702. ((active_streams++))
  6703. fi
  6704. log_info "Initialized $active_streams parallel streams"
  6705. list_worktrees
  6706. }
  6707. # Spawn feature worktree from task
  6708. # Cleanup all worktrees on exit
  6709. cleanup_parallel_streams() {
  6710. log_header "Cleaning Up Parallel Streams"
  6711. # Kill background install processes
  6712. for pid in "${WORKTREE_INSTALL_PIDS[@]}"; do
  6713. if kill -0 "$pid" 2>/dev/null; then
  6714. kill "$pid" 2>/dev/null || true
  6715. fi
  6716. unregister_pid "$pid"
  6717. done
  6718. WORKTREE_INSTALL_PIDS=()
  6719. # Kill all sessions
  6720. for stream in "${!WORKTREE_PIDS[@]}"; do
  6721. local pid="${WORKTREE_PIDS[$stream]}"
  6722. if kill -0 "$pid" 2>/dev/null; then
  6723. log_step "Stopping session: $stream"
  6724. kill "$pid" 2>/dev/null || true
  6725. fi
  6726. unregister_pid "$pid"
  6727. done
  6728. # Wait for all to finish
  6729. wait 2>/dev/null || true
  6730. # Optionally remove worktrees (keep by default for inspection)
  6731. # Uncomment to auto-cleanup:
  6732. # for stream in "${!WORKTREE_PATHS[@]}"; do
  6733. # remove_worktree "$stream"
  6734. # done
  6735. log_info "Parallel streams stopped"
  6736. }
  6737. # Orchestrator loop for parallel mode
  6738. run_parallel_orchestrator() {
  6739. log_header "Parallel Orchestrator Started"
  6740. # Initialize streams - exit if bash version is too old
  6741. if ! init_parallel_streams; then
  6742. log_error "Failed to initialize parallel streams"
  6743. log_error "Falling back to sequential mode"
  6744. PARALLEL_MODE=false
  6745. return 1
  6746. fi
  6747. # Spawn testing session
  6748. if [ "$PARALLEL_TESTING" = "true" ] && [ -n "${WORKTREE_PATHS[testing]:-}" ]; then
  6749. spawn_worktree_session "testing" "Run all tests continuously. Watch for changes. Report failures to .loki/state/test-results.json"
  6750. fi
  6751. # Spawn docs session
  6752. if [ "$PARALLEL_DOCS" = "true" ] && [ -n "${WORKTREE_PATHS[docs]:-}" ]; then
  6753. spawn_worktree_session "docs" "Documentation maintenance stream. Steps: 1) Run 'loki docs generate' if .loki/docs/ does not exist. 2) Watch for .loki/signals/DOCS_NEEDED file. When found, run 'loki docs update' and remove the signal file. 3) After each doc update, run 'loki docs check' and report coverage. 4) Focus on documenting new files, changed APIs, and architectural decisions."
  6754. fi
  6755. # Main orchestrator loop
  6756. local running=true
  6757. # BUG-PAR-004: Orchestrator trap handles SIGTERM properly (cleanup + restore global trap + exit)
  6758. trap 'running=false; cleanup_parallel_streams; trap cleanup INT TERM; exit 0' TERM
  6759. trap 'running=false; cleanup_parallel_streams' INT
  6760. while $running; do
  6761. # Check for merge requests
  6762. check_merge_queue
  6763. # BUG-PAR-014: Retry queued spawns when sessions free up
  6764. local active_count=0
  6765. for _qpid in "${WORKTREE_PIDS[@]}"; do
  6766. if kill -0 "$_qpid" 2>/dev/null; then
  6767. ((active_count++))
  6768. fi
  6769. done
  6770. local _session_cap
  6771. _session_cap=$(effective_session_cap)
  6772. if [ "$active_count" -lt "$_session_cap" ]; then
  6773. for queued_signal in "${TARGET_DIR:-.}"/.loki/signals/SPAWN_QUEUED_*; do
  6774. [ -f "$queued_signal" ] || continue
  6775. local queued_stream
  6776. queued_stream=$(basename "$queued_signal" | sed 's/SPAWN_QUEUED_//')
  6777. local queued_task=""
  6778. queued_task=$(python3 -c "import json; print(json.load(open('$queued_signal'))['task'])" 2>/dev/null) || \
  6779. queued_task=$(jq -r '.task' "$queued_signal" 2>/dev/null) || true
  6780. if [ -n "$queued_task" ] && [ -n "${WORKTREE_PATHS[$queued_stream]:-}" ]; then
  6781. rm -f "$queued_signal"
  6782. spawn_worktree_session "$queued_stream" "$queued_task" && \
  6783. log_info "Retried queued spawn: $queued_stream"
  6784. fi
  6785. done
  6786. fi
  6787. # Check session health
  6788. for stream in "${!WORKTREE_PIDS[@]}"; do
  6789. local pid="${WORKTREE_PIDS[$stream]}"
  6790. if ! kill -0 "$pid" 2>/dev/null; then
  6791. log_warn "Session ended: $stream"
  6792. unset "WORKTREE_PIDS[$stream]"
  6793. fi
  6794. done
  6795. # Update orchestrator state
  6796. local state_file="$TARGET_DIR/.loki/state/parallel-streams.json"
  6797. mkdir -p "$(dirname "$state_file")"
  6798. # BUG-PAR-007: Empty worktree map produces valid JSON
  6799. local worktree_json=""
  6800. if [ ${#WORKTREE_PATHS[@]} -gt 0 ]; then
  6801. worktree_json=$(for stream in "${!WORKTREE_PATHS[@]}"; do
  6802. local path="${WORKTREE_PATHS[$stream]}"
  6803. local pid="null"
  6804. if [ -n "${WORKTREE_PIDS[$stream]+x}" ]; then
  6805. pid="${WORKTREE_PIDS[$stream]}"
  6806. fi
  6807. local status="stopped"
  6808. if [ "$pid" != "null" ] && kill -0 "$pid" 2>/dev/null; then
  6809. status="running"
  6810. fi
  6811. echo " \"$stream\": {\"path\": \"$path\", \"pid\": $pid, \"status\": \"$status\"},"
  6812. done | sed '$ s/,$//')
  6813. fi
  6814. cat > "$state_file" << EOF
  6815. {
  6816. "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  6817. "worktrees": {
  6818. ${worktree_json}
  6819. },
  6820. "active_sessions": ${#WORKTREE_PIDS[@]},
  6821. "max_sessions": $MAX_PARALLEL_SESSIONS
  6822. }
  6823. EOF
  6824. sleep 30
  6825. done
  6826. }
  6827. #===============================================================================
  6828. # Prerequisites Check
  6829. #===============================================================================
  6830. check_prerequisites() {
  6831. log_header "Checking Prerequisites"
  6832. local missing=()
  6833. # Check Provider CLI (uses PROVIDER_CLI from loaded provider config)
  6834. local cli_name="${PROVIDER_CLI:-claude}"
  6835. local display_name="${PROVIDER_DISPLAY_NAME:-Claude Code}"
  6836. log_step "Checking $display_name CLI..."
  6837. if command -v "$cli_name" &> /dev/null; then
  6838. local version=$("$cli_name" --version 2>/dev/null | head -1 || echo "unknown")
  6839. log_info "$display_name CLI: $version"
  6840. else
  6841. missing+=("$cli_name")
  6842. log_error "$display_name CLI not found"
  6843. case "$cli_name" in
  6844. claude)
  6845. log_info "Install: https://claude.ai/code or npm install -g @anthropic-ai/claude-code"
  6846. ;;
  6847. opencode)
  6848. log_info "Install: npm install -g opencode-ai"
  6849. ;;
  6850. *)
  6851. log_info "Install the $cli_name CLI for your provider"
  6852. ;;
  6853. esac
  6854. fi
  6855. # Check Python 3
  6856. log_step "Checking Python 3..."
  6857. if command -v python3 &> /dev/null; then
  6858. local py_version=$(python3 --version 2>&1)
  6859. log_info "Python: $py_version"
  6860. else
  6861. missing+=("python3")
  6862. log_error "Python 3 not found"
  6863. fi
  6864. # Check Git
  6865. log_step "Checking Git..."
  6866. if command -v git &> /dev/null; then
  6867. local git_version=$(git --version)
  6868. log_info "Git: $git_version"
  6869. else
  6870. missing+=("git")
  6871. log_error "Git not found"
  6872. fi
  6873. # Check Node.js (optional but recommended)
  6874. log_step "Checking Node.js (optional)..."
  6875. if command -v node &> /dev/null; then
  6876. local node_version=""
  6877. if node_version=$(node --version 2>/dev/null); then
  6878. _LOKI_NODE_PROBE_STATUS="available"
  6879. _LOKI_NODE_PROBE_VERSION="$node_version"
  6880. log_info "Node.js: $node_version"
  6881. else
  6882. _LOKI_NODE_PROBE_STATUS="probe_failed"
  6883. log_warn "Node.js executable found, but its version probe failed"
  6884. fi
  6885. else
  6886. _LOKI_NODE_PROBE_STATUS="missing"
  6887. log_warn "Node.js not found (optional, needed for some builds)"
  6888. fi
  6889. # Check npm (optional)
  6890. if command -v npm &> /dev/null; then
  6891. local npm_version=""
  6892. if npm_version=$(npm --version 2>/dev/null); then
  6893. _LOKI_NPM_PROBE_STATUS="available"
  6894. _LOKI_NPM_PROBE_VERSION="$npm_version"
  6895. log_info "npm: $npm_version"
  6896. else
  6897. _LOKI_NPM_PROBE_STATUS="probe_failed"
  6898. log_warn "npm executable found, but its version probe failed"
  6899. fi
  6900. else
  6901. _LOKI_NPM_PROBE_STATUS="missing"
  6902. fi
  6903. # Check curl (for web fetches)
  6904. log_step "Checking curl..."
  6905. if command -v curl &> /dev/null; then
  6906. log_info "curl: available"
  6907. else
  6908. missing+=("curl")
  6909. log_error "curl not found"
  6910. fi
  6911. # Check jq (optional but helpful)
  6912. log_step "Checking jq (optional)..."
  6913. if command -v jq &> /dev/null; then
  6914. log_info "jq: available"
  6915. else
  6916. log_warn "jq not found (optional, for JSON parsing)"
  6917. fi
  6918. # Summary
  6919. echo ""
  6920. if [ ${#missing[@]} -gt 0 ]; then
  6921. log_error "Missing required tools: ${missing[*]}"
  6922. log_info "Please install the missing tools and try again."
  6923. return 1
  6924. else
  6925. log_info "All required prerequisites are installed!"
  6926. return 0
  6927. fi
  6928. }
  6929. #===============================================================================
  6930. # Skill Installation Check
  6931. #===============================================================================
  6932. check_skill_installed() {
  6933. log_header "Checking Loki Mode Skill"
  6934. # Build skill locations array dynamically based on provider
  6935. local skill_locations=()
  6936. # Add provider-specific skill directory if set (e.g., ~/.claude/skills for Claude)
  6937. if [ -n "${PROVIDER_SKILL_DIR:-}" ]; then
  6938. skill_locations+=("${PROVIDER_SKILL_DIR}/loki-mode/SKILL.md")
  6939. fi
  6940. # Add local project skill locations
  6941. skill_locations+=(
  6942. ".claude/skills/loki-mode/SKILL.md"
  6943. "$PROJECT_DIR/SKILL.md"
  6944. )
  6945. for loc in "${skill_locations[@]}"; do
  6946. if [ -f "$loc" ]; then
  6947. log_info "Skill found: $loc"
  6948. return 0
  6949. fi
  6950. done
  6951. # For providers without a native skill directory, this is expected
  6952. if [ -z "${PROVIDER_SKILL_DIR:-}" ]; then
  6953. log_info "Provider ${PROVIDER_NAME:-unknown} has no native skill directory"
  6954. log_info "Skill will be passed via prompt injection"
  6955. else
  6956. log_warn "Loki Mode skill not found in standard locations"
  6957. fi
  6958. log_info "The skill will be used from: $PROJECT_DIR/SKILL.md"
  6959. if [ -f "$PROJECT_DIR/SKILL.md" ]; then
  6960. log_info "Using skill from project directory"
  6961. return 0
  6962. else
  6963. log_error "SKILL.md not found!"
  6964. return 1
  6965. fi
  6966. }
  6967. #===============================================================================
  6968. # Initialize Loki Directory
  6969. #===============================================================================
  6970. init_loki_dir() {
  6971. log_header "Initializing Loki Mode Directory"
  6972. # Clean up stale control files ONLY if no other session is running.
  6973. # Deleting these while another session is active would destroy its signals.
  6974. #
  6975. # v7.5.12: PID-liveness probe replaces flock-based "is the lock held?"
  6976. # check. The mkdir-mutex used by safe_acquire_lock is not introspectable
  6977. # the same way (no FD to non-blocking-poll), but the PID file is the
  6978. # source of truth for liveness anyway -- a stale lockdir without a
  6979. # live owner means the session is gone, so cleanup is safe.
  6980. #
  6981. # Per-session locking (v6.4.0): When LOKI_SESSION_ID is set, only clean up
  6982. # that session's files. Global control files (PAUSE/STOP) are only cleaned
  6983. # when NO sessions are active.
  6984. local lock_file can_cleanup=false
  6985. if [ -n "${LOKI_SESSION_ID:-}" ]; then
  6986. # Per-session: PID-liveness probe
  6987. lock_file=".loki/sessions/${LOKI_SESSION_ID}/session.lock"
  6988. local session_pid_file=".loki/sessions/${LOKI_SESSION_ID}/loki.pid"
  6989. local existing_pid=""
  6990. if [ -f "$session_pid_file" ]; then
  6991. existing_pid=$(cat "$session_pid_file" 2>/dev/null)
  6992. fi
  6993. if [ -z "$existing_pid" ] || ! kill -0 "$existing_pid" 2>/dev/null; then
  6994. can_cleanup=true
  6995. fi
  6996. if [ "$can_cleanup" = "true" ]; then
  6997. rm -f "$session_pid_file" 2>/dev/null
  6998. rm -f "$lock_file" 2>/dev/null
  6999. rm -rf "${lock_file}.lockdir" 2>/dev/null
  7000. fi
  7001. else
  7002. # Global: PID-liveness probe
  7003. lock_file=".loki/session.lock"
  7004. local existing_pid=""
  7005. if [ -f ".loki/loki.pid" ]; then
  7006. existing_pid=$(cat ".loki/loki.pid" 2>/dev/null)
  7007. fi
  7008. if [ -z "$existing_pid" ] || ! kill -0 "$existing_pid" 2>/dev/null; then
  7009. can_cleanup=true
  7010. fi
  7011. if [ "$can_cleanup" = "true" ]; then
  7012. # v7.4.16: extended stale-signal cleanup. Pre-v7.4.16 only
  7013. # PAUSE / STOP / HUMAN_INPUT.md were cleaned -- but
  7014. # PAUSE_AT_CHECKPOINT, PAUSED.md, and COMPLETED were added
  7015. # to the signal-file family later without updating this
  7016. # cleanup. A stale PAUSE_AT_CHECKPOINT from a prior session
  7017. # (created by Ctrl+C in checkpoint mode) caused fresh
  7018. # `loki start` to pause immediately when PRD-driven mode
  7019. # auto-switched to checkpoint. User-reported regression.
  7020. rm -f .loki/PAUSE .loki/STOP .loki/HUMAN_INPUT.md 2>/dev/null
  7021. rm -f .loki/PAUSE_AT_CHECKPOINT .loki/PAUSED.md .loki/COMPLETED 2>/dev/null
  7022. rm -f .loki/loki.pid 2>/dev/null
  7023. rm -f .loki/session.lock 2>/dev/null
  7024. rm -rf .loki/session.lock.lockdir 2>/dev/null
  7025. fi
  7026. fi
  7027. # crash.sh's disclosure sentinel can have left a regular FILE at .loki/config
  7028. # (LOKI_DIR is the project .loki); mkdir -p of the config DIRECTORY then fails
  7029. # with "File exists". Fold the file into the directory (back-compat sentinel only),
  7030. # but ONLY when it holds nothing except that sentinel, and NEVER for ~/.loki (a run
  7031. # started from $HOME): there config is the user's real settings FILE (telemetry opt-out).
  7032. if [ -f .loki/config ] && [ ! -L .loki/config ] \
  7033. && [ "$(cd .loki 2>/dev/null && pwd -P)" != "$(cd "${HOME:-/nonexistent}/.loki" 2>/dev/null && pwd -P)" ] \
  7034. && [ -z "$(grep -v -e '^DISCLOSURE_SHOWN=true$' -e '^[[:space:]]*$' .loki/config 2>/dev/null)" ]; then
  7035. mv .loki/config .loki/config.disclosure.tmp 2>/dev/null \
  7036. && mkdir -p .loki/config 2>/dev/null \
  7037. && mv .loki/config.disclosure.tmp .loki/config/disclosure 2>/dev/null
  7038. fi
  7039. # Retry once: a concurrent creator of the same dir can make the first mkdir -p
  7040. # fail with "File exists" (seen on CI, leaked to the quiet quick terminal).
  7041. mkdir -p .loki/{state,queue,messages,logs,config,prompts,artifacts,scripts} 2>/dev/null \
  7042. || mkdir -p .loki/{state,queue,messages,logs,config,prompts,artifacts,scripts}
  7043. mkdir -p .loki/queue
  7044. mkdir -p .loki/state/checkpoints
  7045. mkdir -p .loki/artifacts/{releases,reports,backups}
  7046. mkdir -p .loki/memory/{ledgers,handoffs,learnings,episodic,semantic,skills}
  7047. # metrics/rewards was created and emptied every run and NOTHING ever wrote
  7048. # or read it: the only reference in the whole codebase was the rm -f below.
  7049. # A directory that exists only to be deleted is not a feature, and
  7050. # documenting it as one told the agent something false.
  7051. mkdir -p .loki/metrics/efficiency
  7052. # Clear stale metrics from previous sessions so loki metrics shows current run data (#75)
  7053. rm -f .loki/metrics/efficiency/iteration-*.json 2>/dev/null || true
  7054. # Same reasoning for the proof pointer (#211). .loki/state/last-proof-id.txt
  7055. # is written by generate_proof_of_run and was never cleared, so a run that
  7056. # died before generating a proof left the PREVIOUS run's id behind. Every
  7057. # reader then resolved a receipt describing different work, and
  7058. # the summary builder (build_completion_summary) would print it under the
  7059. # literal heading "Evidence
  7060. # Receipt (this run):". A receipt naming the wrong run is worse than no
  7061. # receipt: absence reads as "no data", a stale one reads as evidence.
  7062. rm -f .loki/state/last-proof-id.txt 2>/dev/null || true
  7063. mkdir -p .loki/rules
  7064. mkdir -p .loki/signals
  7065. # BUG-XC-008: Initialize queue files only if missing or invalid JSON
  7066. for queue in pending in-progress completed failed dead-letter; do
  7067. local qfile=".loki/queue/${queue}.json"
  7068. if [ ! -f "$qfile" ] || ! python3 -c "import json; json.load(open('$qfile'))" 2>/dev/null; then
  7069. echo "[]" > "$qfile"
  7070. fi
  7071. done
  7072. # Initialize orchestrator state if it doesn't exist
  7073. if [ ! -f ".loki/state/orchestrator.json" ]; then
  7074. cat > ".loki/state/orchestrator.json" << EOF
  7075. {
  7076. "version": "$(cat "$PROJECT_DIR/VERSION" 2>/dev/null || echo "2.2.0")",
  7077. "currentPhase": "BOOTSTRAP",
  7078. "startedAt": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  7079. "agents": {},
  7080. "metrics": {
  7081. "tasksCompleted": 0,
  7082. "tasksFailed": 0,
  7083. "retries": 0
  7084. }
  7085. }
  7086. EOF
  7087. fi
  7088. # Write pricing.json with provider-specific model rates
  7089. _write_pricing_json
  7090. # Write budget.json if a budget limit is configured
  7091. if [ -n "$BUDGET_LIMIT" ]; then
  7092. # Validate budget limit is numeric before writing JSON
  7093. if ! echo "$BUDGET_LIMIT" | grep -qE '^[0-9]+(\.[0-9]+)?$'; then
  7094. log_warn "Invalid BUDGET_LIMIT '$BUDGET_LIMIT', ignoring (no cap set)"
  7095. BUDGET_LIMIT=""
  7096. fi
  7097. # Mirror the CLI guard: a non-positive cap (0/0.00) would make
  7098. # check_budget_limit pause before any work runs. Treat it as "no cap"
  7099. # rather than a silent pre-work pause.
  7100. if [ -n "$BUDGET_LIMIT" ] && ! awk -v b="$BUDGET_LIMIT" 'BEGIN{exit !(b+0 > 0)}'; then
  7101. log_warn "BUDGET_LIMIT '$BUDGET_LIMIT' is not greater than 0, ignoring (no cap set)"
  7102. BUDGET_LIMIT=""
  7103. fi
  7104. fi
  7105. if [ -n "$BUDGET_LIMIT" ]; then
  7106. cat > ".loki/metrics/budget.json" << BUDGET_EOF
  7107. {
  7108. "limit": $BUDGET_LIMIT,
  7109. "budget_limit": $BUDGET_LIMIT,
  7110. "budget_used": 0,
  7111. "created_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
  7112. }
  7113. BUDGET_EOF
  7114. log_info "Budget limit set: \$$BUDGET_LIMIT (source: ${BUDGET_SOURCE:-default})"
  7115. fi
  7116. # v7.0.0 Phase 2: remote->local hydrate. Runs ONCE at session boot (not
  7117. # per iteration) to pull semantic patterns + procedural skills from the
  7118. # managed store into .loki/memory/semantic/patterns.json and
  7119. # .loki/memory/skills/*.json. Gated on parent + MEMORY + HYDRATE; all
  7120. # three must be "true". 10s hard timeout so a slow remote never blocks
  7121. # startup. Idempotent (sentinel: .loki/managed/hydrate.lock).
  7122. if [ "$LOKI_MANAGED_AGENTS" = "true" ] \
  7123. && [ "$LOKI_MANAGED_MEMORY" = "true" ] \
  7124. && [ "$LOKI_MANAGED_MEMORY_HYDRATE" = "true" ]; then
  7125. local _hydrate_target="${TARGET_DIR:-$(pwd)}"
  7126. local _hydrate_out
  7127. _hydrate_out=$(
  7128. cd "$PROJECT_DIR" 2>/dev/null && \
  7129. LOKI_TARGET_DIR="$_hydrate_target" \
  7130. timeout 10 python3 -m memory.managed_memory.retrieve --hydrate 2>/dev/null || true
  7131. )
  7132. if [ -n "$_hydrate_out" ]; then
  7133. log_info "Managed hydrate: $_hydrate_out"
  7134. else
  7135. LOKI_TARGET_DIR="$_hydrate_target" \
  7136. python3 -c "from memory.managed_memory.events import emit_managed_event; emit_managed_event('managed_memory_hydrate_timeout', {'phase': 'init'})" 2>/dev/null || true
  7137. fi
  7138. fi
  7139. log_info "Loki directory initialized: .loki/"
  7140. }
  7141. # Write .loki/pricing.json. The model rows derive from loki-ts/data/model-pricing.json
  7142. # (single source of truth, also read by budget.ts); nothing is hand-kept here.
  7143. # Readers: dashboard/server.py (_load_pricing_from_file, /api/pricing).
  7144. # Fail-safe: an unreadable source (or no python3) writes an empty models map, which the dashboard
  7145. # treats as "use my static defaults".
  7146. _write_pricing_json() {
  7147. local provider="${LOKI_PROVIDER:-claude}"
  7148. local updated
  7149. updated=$(date -u +%Y-%m-%d)
  7150. PRICING_SRC="${SCRIPT_DIR}/../loki-ts/data/model-pricing.json" \
  7151. PRICING_PROVIDER="$provider" PRICING_UPDATED="$updated" \
  7152. python3 - > ".loki/pricing.json" << 'PRICING_PY' || printf '{"provider": "%s", "models": {}}\n' "$provider" > ".loki/pricing.json"
  7153. import json, os, sys
  7154. src = os.environ["PRICING_SRC"]
  7155. models = {}
  7156. try:
  7157. with open(src, encoding="utf-8") as fh:
  7158. rows = json.load(fh)["pricing"]
  7159. for key, row in rows.items():
  7160. if not isinstance(row, dict) or "input" not in row or "output" not in row:
  7161. continue
  7162. owner = row.get("provider") or ("opencode" if key.startswith("gpt-") else "claude")
  7163. out = {"input": row["input"], "output": row["output"]}
  7164. if owner == "claude":
  7165. for field in ("cache_read", "cache_write", "cache_write_1h"):
  7166. if field in row:
  7167. out[field] = row[field]
  7168. tier = row.get("over_100k")
  7169. if isinstance(tier, dict) and "input" in tier and "output" in tier:
  7170. out["over_100k"] = {"input": tier["input"], "output": tier["output"]}
  7171. out["label"] = row.get("label", key)
  7172. out["provider"] = owner
  7173. models[key] = out
  7174. except (OSError, ValueError, KeyError, AttributeError) as exc:
  7175. sys.stderr.write("loki: warning: cannot derive .loki/pricing.json models from %s: %s\n" % (src, exc))
  7176. models = {}
  7177. print(json.dumps({
  7178. "provider": os.environ["PRICING_PROVIDER"],
  7179. "updated": os.environ["PRICING_UPDATED"],
  7180. "source": "static",
  7181. "models": models,
  7182. }, indent=2))
  7183. PRICING_PY
  7184. log_info "Pricing data written: .loki/pricing.json (provider: ${provider})"
  7185. }
  7186. #===============================================================================
  7187. # Copy Skill Files to Project Directory
  7188. #===============================================================================
  7189. copy_skill_files() {
  7190. # Copy skill files from the CLI package to the project's .loki/ directory.
  7191. # This makes the CLI self-contained - no need to install Claude Code skill separately.
  7192. # All providers (Claude, opencode) use the same .loki/skills/ location.
  7193. local skills_src="$PROJECT_DIR/skills"
  7194. local skills_dst=".loki/skills"
  7195. if [ ! -d "$skills_src" ]; then
  7196. log_warn "Skills directory not found at $skills_src"
  7197. return 1
  7198. fi
  7199. # Create destination and copy skill files
  7200. mkdir -p "$skills_dst"
  7201. # Copy all skill markdown files
  7202. local copied=0
  7203. for skill_file in "$skills_src"/*.md; do
  7204. if [ -f "$skill_file" ]; then
  7205. cp "$skill_file" "$skills_dst/"
  7206. ((copied++))
  7207. fi
  7208. done
  7209. # Also copy SKILL.md to .loki/ and rewrite paths for workspace access
  7210. if [ -f "$PROJECT_DIR/SKILL.md" ]; then
  7211. # Rewrite skill paths from skills/ to .loki/skills/
  7212. # ONE regex, not a hardcoded filename list. The old form named 8 skills
  7213. # explicitly plus a `Read skills/` catchall, so any NEW skill silently
  7214. # kept an unrewritten path, and three bare paths in SKILL.md survived
  7215. # because they say "See", not "Read". The negated class avoids rewriting
  7216. # an already-correct `.loki/skills/`.
  7217. # Sentinel-protect, rewrite, restore. This is PORTABLE and idempotent.
  7218. #
  7219. # The old form named 8 skills explicitly plus a `Read skills/` catchall,
  7220. # so any NEW skill silently kept an unrewritten path, and bare paths
  7221. # introduced by "See skills/..." survived because they are not "Read".
  7222. # A `sed -E 's|(^|[^.])skills/|...'` one-liner looks tidier but BSD sed
  7223. # rejects it ("RE error: parentheses not balanced"), which would fail
  7224. # SILENTLY on macOS and leave every path unrewritten -- verified by
  7225. # running it. Protecting the already-correct prefixes first is what lets
  7226. # the bare rewrite be unconditional.
  7227. sed -e 's|\.loki/skills/|@@LOKI_S@@|g' \
  7228. -e 's|\.loki/references/|@@LOKI_R@@|g' \
  7229. -e 's|skills/|.loki/skills/|g' \
  7230. -e 's|references/|.loki/references/|g' \
  7231. -e 's|@@LOKI_S@@|.loki/skills/|g' \
  7232. -e 's|@@LOKI_R@@|.loki/references/|g' \
  7233. "$PROJECT_DIR/SKILL.md" > ".loki/SKILL.md"
  7234. fi
  7235. # ALSO copy references/. The copied skills cite references/*.md 21 times
  7236. # across 8 files, every one of which existed in the repo and was NEVER
  7237. # copied, so the agent followed 21 dead paths and silently lost the guidance
  7238. # this function believes it shipped. Copying skills without their references
  7239. # is shipping half a manual.
  7240. local refs_src="$PROJECT_DIR/references"
  7241. local refs_dst=".loki/references"
  7242. local refs_copied=0
  7243. if [ -d "$refs_src" ]; then
  7244. mkdir -p "$refs_dst"
  7245. for ref_file in "$refs_src"/*.md; do
  7246. if [ -f "$ref_file" ]; then
  7247. cp "$ref_file" "$refs_dst/" && refs_copied=$((refs_copied + 1))
  7248. fi
  7249. done
  7250. fi
  7251. log_info "Copied $copied skill files to .loki/skills/ and $refs_copied references to .loki/references/"
  7252. }
  7253. #===============================================================================
  7254. # Task Status Monitor
  7255. #===============================================================================
  7256. update_status_file() {
  7257. # Create a human-readable status file
  7258. local status_file=".loki/STATUS.txt"
  7259. # Get current phase
  7260. local current_phase="UNKNOWN"
  7261. if [ -f ".loki/state/orchestrator.json" ]; then
  7262. current_phase=$(python3 -c "import json; print(json.load(open('.loki/state/orchestrator.json')).get('currentPhase', 'UNKNOWN'))" 2>/dev/null || echo "UNKNOWN")
  7263. fi
  7264. # Count tasks in each queue
  7265. local pending=0 in_progress=0 completed=0 failed=0
  7266. [ -f ".loki/queue/pending.json" ] && pending=$(python3 -c "import json; print(len(json.load(open('.loki/queue/pending.json'))))" 2>/dev/null || echo "0")
  7267. [ -f ".loki/queue/in-progress.json" ] && in_progress=$(python3 -c "import json; print(len(json.load(open('.loki/queue/in-progress.json'))))" 2>/dev/null || echo "0")
  7268. [ -f ".loki/queue/completed.json" ] && completed=$(python3 -c "import json; print(len(json.load(open('.loki/queue/completed.json'))))" 2>/dev/null || echo "0")
  7269. [ -f ".loki/queue/failed.json" ] && failed=$(python3 -c "import json; print(len(json.load(open('.loki/queue/failed.json'))))" 2>/dev/null || echo "0")
  7270. cat > "$status_file" << EOF
  7271. ╔════════════════════════════════════════════════════════════════╗
  7272. ║ LOKI MODE STATUS ║
  7273. ╚════════════════════════════════════════════════════════════════╝
  7274. Updated: $(date)
  7275. Phase: $current_phase
  7276. Tasks:
  7277. ├─ Pending: $pending
  7278. ├─ In Progress: $in_progress
  7279. ├─ Completed: $completed
  7280. └─ Failed: $failed
  7281. Monitor: watch -n 2 cat .loki/STATUS.txt
  7282. EOF
  7283. }
  7284. #===============================================================================
  7285. # Phase Management (Dashboard Integration)
  7286. #===============================================================================
  7287. # Track last known phase to detect changes
  7288. LAST_KNOWN_PHASE=""
  7289. #===============================================================================
  7290. # Dashboard State Writer (Real-time sync with web dashboard)
  7291. #===============================================================================
  7292. write_dashboard_state() {
  7293. # Write comprehensive dashboard state to JSON for web dashboard consumption
  7294. local output_file=".loki/dashboard-state.json"
  7295. # Get current phase and version
  7296. local current_phase="BOOTSTRAP"
  7297. local version="unknown"
  7298. local started_at=""
  7299. local tasks_completed=0
  7300. local tasks_failed=0
  7301. if [ -f ".loki/state/orchestrator.json" ]; then
  7302. _dv=$(python3 -c "import json; print(json.load(open('.loki/state/orchestrator.json')).get('currentPhase', 'BOOTSTRAP'))" 2>/dev/null) && current_phase="$_dv"
  7303. _dv=$(python3 -c "import json; print(json.load(open('.loki/state/orchestrator.json')).get('version', 'unknown'))" 2>/dev/null) && version="$_dv"
  7304. _dv=$(python3 -c "import json; print(json.load(open('.loki/state/orchestrator.json')).get('startedAt', ''))" 2>/dev/null) && started_at="$_dv"
  7305. _dv=$(python3 -c "import json; print(json.load(open('.loki/state/orchestrator.json')).get('metrics', {}).get('tasksCompleted', 0))" 2>/dev/null) && tasks_completed="$_dv"
  7306. _dv=$(python3 -c "import json; print(json.load(open('.loki/state/orchestrator.json')).get('metrics', {}).get('tasksFailed', 0))" 2>/dev/null) && tasks_failed="$_dv"
  7307. fi
  7308. # Emit phase change event if phase has changed (checked in background monitor loop)
  7309. if [ -n "$LAST_KNOWN_PHASE" ] && [ "$current_phase" != "$LAST_KNOWN_PHASE" ]; then
  7310. emit_event_json "phase_change" \
  7311. "from=$LAST_KNOWN_PHASE" \
  7312. "to=$current_phase" \
  7313. "iteration=$ITERATION_COUNT"
  7314. fi
  7315. LAST_KNOWN_PHASE="$current_phase"
  7316. # Get task counts from queues
  7317. local pending_tasks="[]"
  7318. local in_progress_tasks="[]"
  7319. local completed_tasks="[]"
  7320. local failed_tasks="[]"
  7321. local review_tasks="[]"
  7322. # Read queue files, normalizing {"tasks":[...]} format to plain array
  7323. [ -f ".loki/queue/pending.json" ] && pending_tasks=$(jq 'if type == "object" then .tasks // [] else . end' ".loki/queue/pending.json" 2>/dev/null || echo "[]")
  7324. [ -f ".loki/queue/in-progress.json" ] && in_progress_tasks=$(jq 'if type == "object" then .tasks // [] else . end' ".loki/queue/in-progress.json" 2>/dev/null || echo "[]")
  7325. [ -f ".loki/queue/completed.json" ] && completed_tasks=$(jq 'if type == "object" then .tasks // [] else . end' ".loki/queue/completed.json" 2>/dev/null || echo "[]")
  7326. [ -f ".loki/queue/failed.json" ] && failed_tasks=$(jq 'if type == "object" then .tasks // [] else . end' ".loki/queue/failed.json" 2>/dev/null || echo "[]")
  7327. [ -f ".loki/queue/review.json" ] && review_tasks=$(jq 'if type == "object" then .tasks // [] else . end' ".loki/queue/review.json" 2>/dev/null || echo "[]")
  7328. # Get agents state
  7329. local agents="[]"
  7330. [ -f ".loki/state/agents.json" ] && agents=$(cat ".loki/state/agents.json" 2>/dev/null || echo "[]")
  7331. # Get resources state
  7332. local cpu_usage=0
  7333. local mem_usage=0
  7334. local resource_status="ok"
  7335. if [ -f ".loki/state/resources.json" ]; then
  7336. cpu_usage=$(python3 -c "import json; print(json.load(open('.loki/state/resources.json')).get('cpu', {}).get('usage_percent', 0))" 2>/dev/null || echo "0")
  7337. mem_usage=$(python3 -c "import json; print(json.load(open('.loki/state/resources.json')).get('memory', {}).get('usage_percent', 0))" 2>/dev/null || echo "0")
  7338. resource_status=$(python3 -c "import json; print(json.load(open('.loki/state/resources.json')).get('overall_status', 'ok'))" 2>/dev/null || echo "ok")
  7339. fi
  7340. # Check human intervention signals
  7341. local mode="autonomous"
  7342. if [ -f ".loki/PAUSE" ]; then
  7343. mode="paused"
  7344. elif [ -f ".loki/STOP" ]; then
  7345. mode="stopped"
  7346. fi
  7347. # Get complexity tier
  7348. local complexity="${DETECTED_COMPLEXITY:-standard}"
  7349. # Get RARV cycle step from actual phase tracking (falls back to iteration-based)
  7350. local rarv_step=${RARV_CURRENT_STEP:-$((ITERATION_COUNT % 4))}
  7351. local rarv_stages='["reason", "act", "reflect", "verify"]'
  7352. # Get memory system stats (if available)
  7353. local episodic_count=0
  7354. local semantic_count=0
  7355. local procedural_count=0
  7356. [ -d ".loki/memory/episodic" ] && episodic_count=$(find ".loki/memory/episodic" -type f -name "*.json" 2>/dev/null | wc -l | tr -d ' ')
  7357. [ -d ".loki/memory/semantic" ] && semantic_count=$(find ".loki/memory/semantic" -type f -name "*.json" 2>/dev/null | wc -l | tr -d ' ')
  7358. [ -d ".loki/memory/skills" ] && procedural_count=$(find ".loki/memory/skills" -type f -name "*.json" 2>/dev/null | wc -l | tr -d ' ')
  7359. # Get quality gates status (if available)
  7360. local quality_gates='null'
  7361. if [ -f ".loki/state/quality-gates.json" ]; then
  7362. quality_gates=$(cat ".loki/state/quality-gates.json" 2>/dev/null || echo 'null')
  7363. fi
  7364. # Get Completion Council state (v5.25.0)
  7365. local council_state='{"enabled":false}'
  7366. if [ -f ".loki/council/state.json" ]; then
  7367. council_state=$(cat ".loki/council/state.json" 2>/dev/null || echo '{"enabled":false}')
  7368. fi
  7369. # PRD Checklist summary (v5.44.0)
  7370. local checklist_summary='null'
  7371. if [ -f ".loki/checklist/verification-results.json" ]; then
  7372. checklist_summary=$(cat ".loki/checklist/verification-results.json" 2>/dev/null || echo "null")
  7373. fi
  7374. # App Runner state (v5.45.0)
  7375. local app_runner_state='{"status":"not_initialized"}'
  7376. if [ -f ".loki/app-runner/state.json" ]; then
  7377. app_runner_state=$(cat ".loki/app-runner/state.json" 2>/dev/null || echo '{"status":"error"}')
  7378. fi
  7379. # Playwright verification results (v5.46.0)
  7380. local playwright_results='null'
  7381. if [ -f ".loki/verification/playwright-results.json" ]; then
  7382. playwright_results=$(cat ".loki/verification/playwright-results.json" 2>/dev/null || echo "null")
  7383. fi
  7384. # Get budget status (if configured)
  7385. local budget_json="null"
  7386. if [ -f ".loki/metrics/budget.json" ]; then
  7387. budget_json=$(cat ".loki/metrics/budget.json" 2>/dev/null || echo "null")
  7388. fi
  7389. # Get context window tracking state (v5.40.0)
  7390. local context_state="null"
  7391. if [ -f ".loki/context/tracking.json" ]; then
  7392. context_state=$(cat ".loki/context/tracking.json" 2>/dev/null || echo "null")
  7393. fi
  7394. # Get notification summary (v5.40.0)
  7395. local notification_summary='{"total":0,"unacknowledged":0,"critical":0,"warning":0,"info":0}'
  7396. if [ -f ".loki/notifications/active.json" ]; then
  7397. notification_summary=$(python3 -c "
  7398. import json,sys
  7399. try:
  7400. data=json.load(open('.loki/notifications/active.json'))
  7401. print(json.dumps(data.get('summary',{'total':0,'unacknowledged':0})))
  7402. except: print('{\"total\":0,\"unacknowledged\":0}')
  7403. " 2>/dev/null || echo '{"total":0,"unacknowledged":0}')
  7404. fi
  7405. # Write comprehensive JSON state (atomic via temp file + mv).
  7406. # v7.7.5 fix: previously used `${output_file}.tmp` (no PID suffix). When two
  7407. # background processes both called write_dashboard_state concurrently, they
  7408. # raced on the same .tmp filename -- one would clobber the other's content,
  7409. # the loser's `mv` would fail with "No such file or directory" because the
  7410. # winner already moved the shared .tmp away. This flooded the agent output
  7411. # with `mv: rename .loki/dashboard-state.json.tmp ...` errors and made
  7412. # Loki sessions appear broken. Now each process gets a unique tmp suffix.
  7413. local project_name=$(basename "$(pwd)")
  7414. local project_path=$(pwd)
  7415. local _tmp_state="${output_file}.tmp.$$.$RANDOM"
  7416. # BUG #49 fix: Escape project path/name for JSON to handle special chars
  7417. # (spaces, quotes, backslashes in directory names)
  7418. local project_name_escaped
  7419. local project_path_escaped
  7420. project_name_escaped=$(printf '%s' "$project_name" | sed 's/\\/\\\\/g; s/"/\\"/g')
  7421. project_path_escaped=$(printf '%s' "$project_path" | sed 's/\\/\\\\/g; s/"/\\"/g')
  7422. cat > "$_tmp_state" << EOF
  7423. {
  7424. "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  7425. "version": "$version",
  7426. "project": {
  7427. "name": "$project_name_escaped",
  7428. "path": "$project_path_escaped"
  7429. },
  7430. "mode": "$mode",
  7431. "provider": "${PROVIDER_NAME:-claude}",
  7432. "phase": "$current_phase",
  7433. "complexity": "$complexity",
  7434. "iteration": $ITERATION_COUNT,
  7435. "startedAt": "$started_at",
  7436. "rarv": {
  7437. "currentStep": $rarv_step,
  7438. "stages": $rarv_stages
  7439. },
  7440. "tasks": {
  7441. "pending": $pending_tasks,
  7442. "inProgress": $in_progress_tasks,
  7443. "review": $review_tasks,
  7444. "completed": $completed_tasks,
  7445. "failed": $failed_tasks
  7446. },
  7447. "agents": $agents,
  7448. "metrics": {
  7449. "tasksCompleted": $tasks_completed,
  7450. "tasksFailed": $tasks_failed,
  7451. "cpuUsage": $cpu_usage,
  7452. "memoryUsage": $mem_usage,
  7453. "resourceStatus": "$resource_status"
  7454. },
  7455. "memory": {
  7456. "episodic": $episodic_count,
  7457. "semantic": $semantic_count,
  7458. "procedural": $procedural_count
  7459. },
  7460. "qualityGates": $quality_gates,
  7461. "council": $council_state,
  7462. "checklist": $checklist_summary,
  7463. "appRunner": $app_runner_state,
  7464. "playwright": $playwright_results,
  7465. "budget": $budget_json,
  7466. "context": $context_state,
  7467. "tokens": $(python3 -c "
  7468. import json
  7469. try:
  7470. t = json.load(open('.loki/context/tracking.json'))
  7471. totals = t.get('totals', {})
  7472. print(json.dumps({'input': totals.get('total_input', 0), 'output': totals.get('total_output', 0), 'cost_usd': totals.get('total_cost_usd', 0)}))
  7473. except: print('null')
  7474. " 2>/dev/null || echo "null"),
  7475. "notifications": $notification_summary
  7476. }
  7477. EOF
  7478. # v7.7.5 fix: silence mv stderr so any residual race (different process
  7479. # already swapped a newer file in) doesn't flood the agent output. The
  7480. # PID-suffixed tmp eliminates the race; the 2>/dev/null is belt-and-
  7481. # suspenders. `|| rm -f "$_tmp_state" 2>/dev/null` cleans up the tmp
  7482. # on the rare failure rather than leaking it.
  7483. mv "$_tmp_state" "$output_file" 2>/dev/null || rm -f "$_tmp_state" 2>/dev/null
  7484. }
  7485. #===============================================================================
  7486. # Context Window Tracking (v5.40.0)
  7487. #===============================================================================
  7488. # Track context window usage (provider-agnostic)
  7489. track_context_usage() {
  7490. local iteration="$1"
  7491. mkdir -p .loki/context
  7492. local provider_arg="${LOKI_PROVIDER:-claude}"
  7493. local window_arg="${LOKI_CONTEXT_WINDOW_SIZE:-0}"
  7494. python3 "${SCRIPT_DIR}/context-tracker.py" \
  7495. --iteration "$iteration" \
  7496. --loki-dir ".loki" \
  7497. --provider "$provider_arg" \
  7498. --window-size "$window_arg" 2>/dev/null || true
  7499. }
  7500. # Check notification triggers against current state
  7501. check_notification_triggers() {
  7502. local iteration="$1"
  7503. mkdir -p .loki/notifications
  7504. python3 "${SCRIPT_DIR}/notification-checker.py" \
  7505. --iteration "$iteration" \
  7506. --loki-dir ".loki" 2>/dev/null || true
  7507. }
  7508. #===============================================================================
  7509. # Task Queue Auto-Tracking (for degraded mode providers)
  7510. #===============================================================================
  7511. # Derive a plain-language, honest summary of the spec this run is building from,
  7512. # for the dashboard iteration card (Task G). Mirrors the "Building:" start
  7513. # headline (run.sh:17061) and proof-generator's source resolution so the card,
  7514. # the banner, and the proof artifact all describe the same real intent.
  7515. #
  7516. # Echoes two tab-separated fields: <human spec label>\t<spec kind>, where kind is
  7517. # one of: brief | prd | codebase-analysis. The label is plain English with no
  7518. # RARV jargon. Best-effort and never fails (used only to enrich a card).
  7519. _loki_iteration_spec_summary() {
  7520. local prd="${1:-}"
  7521. local label="" kind=""
  7522. # 1. A recorded one-line brief (`loki start "<brief>"`) is the user's own
  7523. # words; show them verbatim, truncated to stay on one tidy line.
  7524. if [ -f ".loki/state/brief.txt" ]; then
  7525. local _brief
  7526. _brief="$(tr '\n' ' ' < .loki/state/brief.txt 2>/dev/null | sed 's/ */ /g; s/^ //; s/ $//')"
  7527. if [ -n "$_brief" ]; then
  7528. if [ "${#_brief}" -gt 80 ]; then
  7529. _brief="${_brief:0:77}..."
  7530. fi
  7531. printf '%s\t%s' "$_brief" "brief"
  7532. return 0
  7533. fi
  7534. fi
  7535. # 2. A generated PRD means there was no user spec; we are reverse-engineering
  7536. # one from the code. Say exactly that rather than naming the internal file.
  7537. case "$prd" in
  7538. ""|*.loki/generated-prd.md|*.loki/generated-prd.json)
  7539. label="the codebase (no spec provided)"
  7540. kind="codebase-analysis"
  7541. ;;
  7542. *)
  7543. # 3. A real user PRD / spec file: name it by basename, no path noise.
  7544. label="$(basename "$prd" 2>/dev/null || printf '%s' "$prd")"
  7545. kind="prd"
  7546. ;;
  7547. esac
  7548. printf '%s\t%s' "$label" "$kind"
  7549. }
  7550. # Track iteration start - create task in in-progress queue
  7551. track_iteration_start() {
  7552. local iteration="$1"
  7553. local prd="${2:-}"
  7554. local task_id="iteration-$iteration"
  7555. mkdir -p .loki/queue
  7556. # Record iteration start time for efficiency tracking (SYN-018)
  7557. record_iteration_start
  7558. # Emit iteration start event for dashboard
  7559. emit_event_json "iteration_start" \
  7560. "iteration=$iteration" \
  7561. "provider=${PROVIDER_NAME:-claude}" \
  7562. "prd=${prd:-Codebase Analysis}"
  7563. # Also emit to pending dir for OTEL bridge
  7564. if [ -n "${LOKI_OTEL_ENDPOINT:-}" ]; then
  7565. emit_event_pending "iteration_start" \
  7566. "iteration=$iteration" \
  7567. "provider=${PROVIDER_NAME:-claude}"
  7568. fi
  7569. # Read next pending task for context (enrich iteration with PRD task details)
  7570. local next_task_context=""
  7571. if [[ -f ".loki/queue/pending.json" ]]; then
  7572. next_task_context=$(python3 -c "
  7573. import json
  7574. try:
  7575. with open('.loki/queue/pending.json') as f:
  7576. tasks = json.load(f)
  7577. if isinstance(tasks, dict):
  7578. tasks = tasks.get('tasks', [])
  7579. pending = [t for t in tasks if isinstance(t, dict) and t.get('status','pending') == 'pending']
  7580. if pending:
  7581. t = pending[0]
  7582. print(json.dumps({
  7583. 'current_task': t.get('title',''),
  7584. 'description': t.get('description',''),
  7585. 'acceptance_criteria': t.get('acceptance_criteria', []),
  7586. 'user_story': t.get('user_story', ''),
  7587. 'source': t.get('source', ''),
  7588. 'project': t.get('project', '')
  7589. }))
  7590. except: pass
  7591. " 2>/dev/null || true)
  7592. fi
  7593. # Create task entry (escape PRD path for safe JSON embedding)
  7594. local prd_escaped
  7595. prd_escaped=$(printf '%s' "${prd:-Codebase Analysis}" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\t/\\t/g')
  7596. # Task G: derive a plain-language, honest summary of what this run is building
  7597. # from, so the card title/description describe the real work instead of the
  7598. # generic "Iteration N" / "RARV iteration N" placeholder. Best-effort.
  7599. local _spec_label="" _spec_kind=""
  7600. local _spec_summary
  7601. _spec_summary=$(_loki_iteration_spec_summary "$prd")
  7602. _spec_label="${_spec_summary%%$'\t'*}"
  7603. _spec_kind="${_spec_summary##*$'\t'}"
  7604. # Escape for safe embedding in the python -c literals below.
  7605. local _spec_label_esc _spec_kind_esc
  7606. _spec_label_esc=$(printf '%s' "$_spec_label" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\t/\\t/g')
  7607. _spec_kind_esc=$(printf '%s' "$_spec_kind" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\t/\\t/g')
  7608. # Build enriched task JSON with pending task context.
  7609. # Must initialize to empty: this script runs under `set -u` (line 152),
  7610. # so `local task_json` without a value leaves it unset. When the pending
  7611. # queue is empty, the enrichment `if` is skipped and the `-z` check below
  7612. # would fire on an unset variable and kill the run.
  7613. local task_json=""
  7614. if [[ -n "${next_task_context:-}" ]]; then
  7615. task_json=$(python3 -c "
  7616. import json, sys
  7617. ctx = json.loads('''$next_task_context''')
  7618. # Task G: the card must describe the REAL work in plain language, not the
  7619. # generic 'Iteration N' / 'RARV iteration N' placeholder. Prefer the pending
  7620. # PRD task's own title/description/criteria. When those are absent, fall back to
  7621. # a plain-language summary derived from the spec this run is building from, and
  7622. # OMIT acceptance criteria rather than show RARV phase-name boilerplate (only
  7623. # state what is really known).
  7624. spec_label = '${_spec_label_esc}'
  7625. spec_kind = '${_spec_kind_esc}'
  7626. if spec_kind == 'codebase-analysis':
  7627. fallback_title = 'Analyzing the codebase and generating a spec'
  7628. fallback_desc = 'Reading the existing code to reverse-engineer a spec, then building against it.'
  7629. elif spec_kind == 'brief':
  7630. fallback_title = 'Building: ' + spec_label
  7631. fallback_desc = 'Building from your brief: ' + spec_label
  7632. else:
  7633. fallback_title = 'Building from ' + spec_label
  7634. fallback_desc = 'Implementing the spec in ' + spec_label + ' and verifying it.'
  7635. task = {
  7636. 'id': 'iteration-$iteration',
  7637. 'type': 'iteration',
  7638. 'title': ctx.get('current_task') or fallback_title,
  7639. 'description': ctx.get('description') or fallback_desc,
  7640. 'status': 'in_progress',
  7641. 'priority': 'medium',
  7642. 'startedAt': '$(date -u +%Y-%m-%dT%H:%M:%SZ)',
  7643. 'provider': '${PROVIDER_NAME:-claude}',
  7644. 'acceptance_criteria': ctx.get('acceptance_criteria') or [],
  7645. 'notes': [],
  7646. 'logs': [{
  7647. 'timestamp': '$(date -u +%Y-%m-%dT%H:%M:%SZ)',
  7648. 'iteration': $iteration,
  7649. 'level': 'info',
  7650. 'phase': 'BOOTSTRAP',
  7651. 'message': 'Iteration $iteration started'
  7652. }]
  7653. }
  7654. if ctx.get('user_story'):
  7655. task['user_story'] = ctx['user_story']
  7656. if ctx.get('source'):
  7657. task['source'] = ctx['source']
  7658. if ctx.get('project'):
  7659. task['project'] = ctx['project']
  7660. print(json.dumps(task, indent=2))
  7661. " 2>/dev/null) || task_json=""
  7662. fi
  7663. # Fallback when there is no pending PRD task (codebase-analysis run or empty
  7664. # queue). Task G: this was the worst placeholder card -- 'Iteration N' /
  7665. # 'RARV iteration N' with RARV-phase-name acceptance criteria, which tells a
  7666. # watching user nothing about the real work. Build an honest, plain-language
  7667. # card from the spec summary instead, and omit acceptance criteria (we have no
  7668. # real per-iteration criteria here -- do not invent RARV jargon).
  7669. if [[ -z "${task_json:-}" ]]; then
  7670. local _start_ts="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
  7671. task_json=$(python3 -c "
  7672. import json
  7673. spec_label = '${_spec_label_esc}'
  7674. spec_kind = '${_spec_kind_esc}'
  7675. if spec_kind == 'codebase-analysis':
  7676. title = 'Analyzing the codebase and generating a spec'
  7677. desc = 'Reading the existing code to reverse-engineer a spec, then building against it.'
  7678. elif spec_kind == 'brief':
  7679. title = 'Building: ' + spec_label
  7680. desc = 'Building from your brief: ' + spec_label
  7681. else:
  7682. title = 'Building from ' + spec_label
  7683. desc = 'Implementing the spec in ' + spec_label + ' and verifying it.'
  7684. task = {
  7685. 'id': '$task_id',
  7686. 'type': 'iteration',
  7687. 'title': title,
  7688. 'description': desc,
  7689. 'status': 'in_progress',
  7690. 'priority': 'medium',
  7691. 'startedAt': '$_start_ts',
  7692. 'provider': '${PROVIDER_NAME:-claude}',
  7693. 'acceptance_criteria': [],
  7694. 'notes': [],
  7695. 'logs': [{
  7696. 'timestamp': '$_start_ts',
  7697. 'iteration': $iteration,
  7698. 'level': 'info',
  7699. 'phase': 'BOOTSTRAP',
  7700. 'message': 'Iteration $iteration started'
  7701. }]
  7702. }
  7703. print(json.dumps(task, indent=2))
  7704. " 2>/dev/null) || task_json=""
  7705. fi
  7706. # Last-resort safety net if python is unavailable. Still avoids the generic
  7707. # 'RARV iteration' wording and omits boilerplate acceptance criteria.
  7708. if [[ -z "${task_json:-}" ]]; then
  7709. local _start_ts2="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
  7710. local _safe_title
  7711. case "$_spec_kind" in
  7712. codebase-analysis) _safe_title="Analyzing the codebase and generating a spec" ;;
  7713. brief) _safe_title="Building from your brief" ;;
  7714. *) _safe_title="Building from the spec" ;;
  7715. esac
  7716. task_json=$(cat <<EOF
  7717. {
  7718. "id": "$task_id",
  7719. "type": "iteration",
  7720. "title": "$_safe_title",
  7721. "description": "$_safe_title (iteration $iteration).",
  7722. "status": "in_progress",
  7723. "priority": "medium",
  7724. "startedAt": "$_start_ts2",
  7725. "provider": "${PROVIDER_NAME:-claude}",
  7726. "acceptance_criteria": [],
  7727. "notes": [],
  7728. "logs": [
  7729. {
  7730. "timestamp": "$_start_ts2",
  7731. "iteration": $iteration,
  7732. "level": "info",
  7733. "phase": "BOOTSTRAP",
  7734. "message": "Iteration $iteration started"
  7735. }
  7736. ]
  7737. }
  7738. EOF
  7739. )
  7740. fi
  7741. # Add to in-progress queue
  7742. # BUG-XC-003: atomic queue modification.
  7743. # v7.5.12: portable mkdir-mutex via safe_acquire_lock (no flock needed).
  7744. # v7.5.12 Dev11 (R1 HIGH): gate the read-modify-write on acquire SUCCESS.
  7745. # The prior `safe_acquire_lock ... || true` then unconditional
  7746. # `safe_release_lock` mutated state on timeout AND released the OTHER
  7747. # holder's lock -- a mutex correctness violation. Mirror the working
  7748. # pattern at line 1845 (acquire-success guarded RMW + release inside).
  7749. local in_progress_file=".loki/queue/in-progress.json"
  7750. local lockfile=".loki/queue/.in-progress.lock"
  7751. if type safe_acquire_lock >/dev/null 2>&1 && safe_acquire_lock "$lockfile" 5; then
  7752. if [ -f "$in_progress_file" ]; then
  7753. local existing=$(cat "$in_progress_file")
  7754. if [ "$existing" = "[]" ] || [ -z "$existing" ]; then
  7755. echo "[$task_json]" > "$in_progress_file"
  7756. else
  7757. # Append to existing array
  7758. echo "$existing" | python3 -c "
  7759. import sys, json
  7760. data = json.load(sys.stdin)
  7761. data.append($task_json)
  7762. print(json.dumps(data, indent=2))
  7763. " > "$in_progress_file" 2>/dev/null || echo "[$task_json]" > "$in_progress_file"
  7764. fi
  7765. else
  7766. echo "[$task_json]" > "$in_progress_file"
  7767. fi
  7768. safe_release_lock "$lockfile"
  7769. else
  7770. log_warn "could not acquire in-progress lock; skipping update"
  7771. fi
  7772. # BUG-ST-014: Atomic current-task.json update via temp file + mv
  7773. local ct_tmp=".loki/queue/current-task.json.tmp.$$"
  7774. echo "$task_json" > "$ct_tmp"
  7775. mv -f "$ct_tmp" .loki/queue/current-task.json
  7776. }
  7777. # Track iteration completion - move task to completed queue
  7778. # v7.8.1: staleness-aware generated-PRD reuse helpers.
  7779. # Hash stdin with whatever digest tool is available (mirrors the existing
  7780. # stat -f%z || stat -c%s dual-probe portability pattern). Echoes a short hash.
  7781. _loki_hash_stdin() {
  7782. if command -v shasum >/dev/null 2>&1; then
  7783. shasum -a 256 | cut -c1-16
  7784. elif command -v sha256sum >/dev/null 2>&1; then
  7785. sha256sum | cut -c1-16
  7786. else
  7787. cksum | tr -d ' ' | cut -c1-16
  7788. fi
  7789. }
  7790. # Compute a cheap, clone-stable signature of the codebase so we can tell whether
  7791. # it changed since the generated PRD was last written. Git repos: HEAD sha +
  7792. # dirty flag (.loki/.git churn filtered out). Non-git: a hash of sorted
  7793. # path+size pairs PLUS file content (v7.32.3, #569: path+size alone was blind to
  7794. # a same-size content edit, so a stale PRD could be silently reused with a
  7795. # false "codebase unchanged" disclosure). Content hashing is clone-stable
  7796. # (mtime is not, which is why mtime was never used). Three content tiers:
  7797. # 1. full content hash ("files:") when the tree is both at-or-under
  7798. # LOKI_PRD_SIG_CONTENT_BUDGET bytes (default 50MB) AND at-or-under
  7799. # LOKI_PRD_SIG_CONTENT_MAXFILES files (default 20000). Detects any edit.
  7800. # 2. sampled content hash ("files-sampled:", #171) when the tree exceeds
  7801. # either bound: hashes the head + tail (first 4096 + last 4096 bytes) of
  7802. # every file. Catches same-size edits at the start or end of a file
  7803. # without a full read of a huge tree. Residual honest gap: a same-size
  7804. # edit in the MIDDLE of a large file (>8192 bytes) that touches neither
  7805. # 4KB window is still invisible. Far narrower than the old size-blind
  7806. # fallback, which missed ALL same-size edits.
  7807. # 3. (historical) "files-shallow:" was the old content-blind fallback. It is
  7808. # no longer emitted, but is still ACCEPTED when read from a stored pre-#171
  7809. # signature so the first post-upgrade run reuses instead of falsely
  7810. # claiming "codebase changed" (one-run format-transition, see consumer).
  7811. # Echoes the signature.
  7812. compute_codebase_signature() {
  7813. local dir="${1:-.}"
  7814. ( cd "$dir" 2>/dev/null || exit 0
  7815. if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  7816. # Content-identity signature (gitc:): the hash is over the WORKING-TREE
  7817. # CONTENT of every tracked + untracked-not-ignored file, independent of
  7818. # the commit boundary. Whether a file is committed or sitting dirty in
  7819. # the worktree yields the SAME value. This is what makes reuse robust to
  7820. # Loki's OWN session commit (commit_session_changes, default-on as of
  7821. # v7.73.0): a rerun whose only "change" is that the prior run committed
  7822. # work it had already analyzed still classifies as reuse, not a spurious
  7823. # "codebase changed -> update". A genuine source edit (committed OR
  7824. # uncommitted) changes a blob hash and is still detected, and a new
  7825. # untracked file is detected too. .loki/ is excluded (runtime state).
  7826. # Paths are enumerated NUL-safe, .loki dropped, sorted, then hashed in
  7827. # ONE batched `git hash-object --stdin-paths` pass (order-preserving),
  7828. # so cost is one git process regardless of file count.
  7829. local gitc gitc_paths gitc_deleted gitc_paths_file gitc_deleted_file
  7830. # Tracked files removed from the worktree (but not staged): they have no
  7831. # content to hash and would make the batched hash-object abort mid-list,
  7832. # truncating the output and misaligning the path<->hash pairing. Drop
  7833. # them; their removal from the list is itself the detected change.
  7834. gitc_deleted=$(git ls-files --deleted -z 2>/dev/null | tr '\0' '\n')
  7835. # Exclude .loki/ AND Loki's own session-end artifacts (HANDOFF.md,
  7836. # USAGE.md) written+committed to the repo ROOT by the completion path
  7837. # (LOKI_HANDOFF / commit_session_changes, both default-on). Those are
  7838. # runtime OUTPUT of the session, not user codebase; counting them would
  7839. # make the next PRD-reuse check see a spurious "codebase changed" and
  7840. # flip reuse->update (the signature is persisted per-iteration BEFORE
  7841. # the after-loop HANDOFF write, so it can never account for them).
  7842. gitc_paths=$( { git ls-files -z 2>/dev/null; git ls-files --others --exclude-standard -z 2>/dev/null; } \
  7843. | tr '\0' '\n' | grep -vE '(^|/)\.loki(/|$)|^HANDOFF\.md$|^USAGE\.md$' | LC_ALL=C sort -u )
  7844. mkdir -p .loki/tmp 2>/dev/null || { echo "gitc:"; exit 0; }
  7845. gitc_paths_file=$(mktemp ".loki/tmp/signature-paths.XXXXXX") || { echo "gitc:"; exit 0; }
  7846. gitc_deleted_file=$(mktemp ".loki/tmp/signature-deleted.XXXXXX") || {
  7847. rm -f "$gitc_paths_file"
  7848. echo "gitc:"
  7849. exit 0
  7850. }
  7851. printf '%s\n' "$gitc_paths" > "$gitc_paths_file"
  7852. printf '%s\n' "$gitc_deleted" > "$gitc_deleted_file"
  7853. if [ -n "$gitc_deleted" ]; then
  7854. gitc_paths=$(grep -vxF -f "$gitc_deleted_file" "$gitc_paths_file" || true)
  7855. printf '%s\n' "$gitc_paths" > "$gitc_paths_file"
  7856. fi
  7857. if [ -z "$gitc_paths" ]; then
  7858. # No tracked or untracked content (empty/fresh repo): a stable
  7859. # constant so two empty-tree runs still compare equal (reuse).
  7860. gitc=$(printf '' | _loki_hash_stdin)
  7861. else
  7862. gitc=$(printf '%s\n' "$gitc_paths" | git hash-object --stdin-paths 2>/dev/null \
  7863. | paste -d'\t' - "$gitc_paths_file" \
  7864. | LC_ALL=C sort | _loki_hash_stdin)
  7865. fi
  7866. rm -f "$gitc_paths_file" "$gitc_deleted_file"
  7867. echo "gitc:${gitc}"
  7868. else
  7869. local listing count total_sz budget maxfiles
  7870. listing=$(find . \
  7871. -type d \( -name .loki -o -name .git -o -name node_modules -o -name dist \
  7872. -o -name build -o -name .next -o -name target -o -name vendor \
  7873. -o -name __pycache__ -o -name .venv -o -name venv \) -prune -o \
  7874. -type f -print 2>/dev/null \
  7875. | grep -vE '^\./(HANDOFF|USAGE)\.md$' \
  7876. | while IFS= read -r f; do
  7877. local sz
  7878. sz=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f" 2>/dev/null || echo 0)
  7879. printf '%s\t%s\n' "$f" "$sz"
  7880. done | LC_ALL=C sort)
  7881. # grep -c prints 0 itself on no match (exit 1); '|| true' avoids the
  7882. # old '|| echo 0' double-zero that embedded a newline on empty trees
  7883. count=$(printf '%s\n' "$listing" | grep -c . || true)
  7884. total_sz=$(printf '%s\n' "$listing" | awk -F'\t' '{s+=$2} END {printf "%d", s}')
  7885. budget="${LOKI_PRD_SIG_CONTENT_BUDGET:-52428800}"
  7886. maxfiles="${LOKI_PRD_SIG_CONTENT_MAXFILES:-20000}"
  7887. if [ "${total_sz:-0}" -le "$budget" ] 2>/dev/null \
  7888. && [ "${count:-0}" -le "$maxfiles" ] 2>/dev/null; then
  7889. # Tier 1 -- full content pass: stream all file contents through one
  7890. # hash in the same sorted order as the listing. Detects any edit,
  7891. # including same-size ones. xargs -0 batches the reads into a
  7892. # handful of cat invocations, so cost scales with BYTES (which the
  7893. # budget above bounds), not file count: a fork-per-file loop here
  7894. # measured ~38s of added startup on a 30k-small-file tree. Renames
  7895. # and content swaps are still caught by the listing hash below.
  7896. local content_hash
  7897. content_hash=$(printf '%s\n' "$listing" | cut -f1 | tr '\n' '\0' \
  7898. | xargs -0 cat 2>/dev/null | _loki_hash_stdin)
  7899. echo "files:$(printf '%s' "$listing" | _loki_hash_stdin):${count}:${content_hash}"
  7900. else
  7901. # Tier 2 -- sampled content pass (#171): the tree is over the byte
  7902. # budget OR over the file-count cap, so a full read would be slow.
  7903. # Hash the head + tail (first 4096 + last 4096 bytes) of every file
  7904. # instead. This catches same-size edits at the start or end of a
  7905. # file (which the old size-blind "files-shallow:" missed entirely),
  7906. # at a fixed <=8KB-per-file cost. -n 64 batches files per sh fork
  7907. # to avoid a fork-per-file storm on large trees. Same sorted order
  7908. # as the listing keeps the hash deterministic. Residual honest gap:
  7909. # a same-size edit in the middle of a >8KB file is still invisible.
  7910. local sample_hash
  7911. sample_hash=$(printf '%s\n' "$listing" | cut -f1 | tr '\n' '\0' \
  7912. | xargs -0 -n 64 sh -c 'for f in "$@"; do head -c 4096 -- "$f" 2>/dev/null; tail -c 4096 -- "$f" 2>/dev/null; done' _ 2>/dev/null \
  7913. | _loki_hash_stdin)
  7914. echo "files-sampled:$(printf '%s' "$listing" | _loki_hash_stdin):${count}:${sample_hash}"
  7915. fi
  7916. fi
  7917. )
  7918. }
  7919. # Recompute the PRE-content-hash git-mode signature ("git:<HEAD>:<dirty>") for a
  7920. # one-time format transition: a signature recorded by an older Loki (HEAD +
  7921. # porcelain) must still be comparable on the first run after the upgrade to the
  7922. # new content-identity "gitc:" format, or decide would falsely flip to "update".
  7923. # Echoes the legacy-format value, or "" when not inside a git work tree.
  7924. _loki_compute_legacy_git_signature() {
  7925. local dir="${1:-.}"
  7926. ( cd "$dir" 2>/dev/null || exit 0
  7927. git rev-parse --is-inside-work-tree >/dev/null 2>&1 || exit 0
  7928. local head dirty porcelain
  7929. head=$(git rev-parse HEAD 2>/dev/null || echo "nohead")
  7930. porcelain=$(git status --porcelain 2>/dev/null | grep -vE '(^...?\.loki/|/\.loki/| \.loki/|\.git/)' || true)
  7931. if [ -z "$porcelain" ]; then
  7932. dirty="clean"
  7933. else
  7934. dirty=$(printf '%s' "$porcelain" | _loki_hash_stdin)
  7935. fi
  7936. echo "git:${head}:${dirty}"
  7937. )
  7938. }
  7939. # Content hash of the generated PRD file itself (NOT the codebase). Used to
  7940. # detect that a user hand-edited the generated PRD: when the file no longer
  7941. # matches the prd_sha Loki recorded after it last wrote the file, the PRD is
  7942. # user-owned and must be used as-is, never silently overwritten. Echoes "" when
  7943. # no generated PRD file is present.
  7944. _loki_prd_file_hash() {
  7945. local loki_dir="${1:-.}/.loki"
  7946. local f=""
  7947. if [ -f "$loki_dir/generated-prd.md" ]; then
  7948. f="$loki_dir/generated-prd.md"
  7949. elif [ -f "$loki_dir/generated-prd.json" ]; then
  7950. f="$loki_dir/generated-prd.json"
  7951. fi
  7952. [ -n "$f" ] || { echo ""; return 0; }
  7953. _loki_hash_stdin < "$f"
  7954. }
  7955. # Decide what to do with a previously generated PRD on a no-PRD run.
  7956. # Echoes one of: reuse | update | generate | user_owned. Never fails the run.
  7957. # Precedence: force-regen > user_owned (hand-edited) > reuse/update > generate.
  7958. # - LOKI_PRD_REGEN=1 (or --regen-prd/--fresh-prd, which set it) -> generate.
  7959. # - no generated PRD present -> generate (first run).
  7960. # - generated PRD present but its content hash differs from the recorded
  7961. # prd_sha -> user_owned (the user hand-edited it; use as-is, do not rewrite).
  7962. # - generated PRD present, no recorded signature -> update (have a PRD but no
  7963. # provenance: reconcile incrementally rather than trust-blindly or discard).
  7964. # - signature matches current codebase -> reuse (unchanged).
  7965. # - signature differs -> update (codebase changed; update incrementally).
  7966. decide_generated_prd_action() {
  7967. local loki_dir="${TARGET_DIR:-.}/.loki"
  7968. if [ "${LOKI_PRD_REGEN:-}" = "1" ]; then
  7969. echo "generate"; return 0
  7970. fi
  7971. if [ ! -f "$loki_dir/generated-prd.md" ] && [ ! -f "$loki_dir/generated-prd.json" ]; then
  7972. echo "generate"; return 0
  7973. fi
  7974. local sig_file="$loki_dir/state/prd-signature.json"
  7975. if [ ! -f "$sig_file" ]; then
  7976. echo "update"; return 0
  7977. fi
  7978. # source:"user" short-circuit (LOCK 2): an explicit user-provided PRD was
  7979. # persisted into the canonical slot. Always use it as-is, never enter the
  7980. # signature-diff update path -- even if the file hash drifted (hand-edit) or
  7981. # the codebase changed since. --fresh-prd/LOKI_PRD_REGEN (checked above)
  7982. # still wins and forces a regenerate. A missing/empty source falls through
  7983. # to the generated-PRD logic below (defensive: correctness never depends on
  7984. # a backfilled field).
  7985. local prd_source
  7986. prd_source=$(LOKI_SIG_FILE="$sig_file" python3 -c "
  7987. import json, os
  7988. try:
  7989. print(json.load(open(os.environ['LOKI_SIG_FILE'])).get('source',''))
  7990. except Exception:
  7991. print('')
  7992. " 2>/dev/null)
  7993. if [ "$prd_source" = "user" ]; then
  7994. echo "user_owned"; return 0
  7995. fi
  7996. local stored stored_prd_sha current cur_prd_sha
  7997. stored=$(LOKI_SIG_FILE="$sig_file" python3 -c "
  7998. import json, os
  7999. try:
  8000. print(json.load(open(os.environ['LOKI_SIG_FILE'])).get('signature',''))
  8001. except Exception:
  8002. print('')
  8003. " 2>/dev/null)
  8004. stored_prd_sha=$(LOKI_SIG_FILE="$sig_file" python3 -c "
  8005. import json, os
  8006. try:
  8007. print(json.load(open(os.environ['LOKI_SIG_FILE'])).get('prd_sha',''))
  8008. except Exception:
  8009. print('')
  8010. " 2>/dev/null)
  8011. [ -z "$stored" ] && { echo "update"; return 0; }
  8012. # Hand-edit detection (precedence above reuse/update): if we recorded a
  8013. # prd_sha and the file no longer matches it, the user edited it themselves.
  8014. # Treat as user-owned: use as-is, never regenerate over their changes.
  8015. if [ -n "$stored_prd_sha" ]; then
  8016. cur_prd_sha=$(_loki_prd_file_hash "${TARGET_DIR:-.}")
  8017. if [ -n "$cur_prd_sha" ] && [ "$cur_prd_sha" != "$stored_prd_sha" ]; then
  8018. echo "user_owned"; return 0
  8019. fi
  8020. fi
  8021. current=$(compute_codebase_signature "${TARGET_DIR:-.}")
  8022. if [ "$stored" = "$current" ]; then
  8023. echo "reuse"
  8024. else
  8025. # v7.32.3 format transition (#569): a stored pre-content-hash signature
  8026. # ("files:<listing>:<count>", 3 fields) compared against the new 4-field
  8027. # format would falsely claim "codebase changed" on the first post-upgrade
  8028. # run. When the new signature extends the stored one (same listing
  8029. # fields), the tree is unchanged at the old format's trust level: reuse,
  8030. # honestly. The next persist upgrades the stored format. A same-size edit
  8031. # made BEFORE the upgrade stays invisible for this one run, exactly as it
  8032. # was on the old version (no regression, no false disclosure).
  8033. case "$stored" in
  8034. files:*:*)
  8035. # Require the legitimate old 3-field format (files:HASH:COUNT,
  8036. # exactly 2 colons), not a truncated/corrupted 2-field value
  8037. # (council hardening: corruption must fall to update, as before).
  8038. if [ "$(printf '%s' "$stored" | tr -dc ':' | wc -c | tr -d ' ')" = "2" ] \
  8039. && [ "${current#"${stored}":}" != "$current" ]; then
  8040. echo "reuse"; return 0
  8041. fi
  8042. ;;
  8043. files-shallow:*:*)
  8044. # #171 format transition: a stored pre-#171 size-blind signature
  8045. # ("files-shallow:<listing>:<count>", 3 fields) compared against
  8046. # the new sampled signature ("files-sampled:<listing>:<count>:
  8047. # <samplehash>") would falsely claim "codebase changed" on the
  8048. # first post-upgrade run. When the listing-hash and count match
  8049. # (i.e. the stored value with its prefix swapped to files-sampled:
  8050. # is a prefix of the current sampled signature), the tree is
  8051. # unchanged at the OLD format's trust level (paths+sizes): reuse,
  8052. # honestly. The next persist upgrades the stored format to the
  8053. # sampled tier. A same-size edit made BEFORE the upgrade stays
  8054. # invisible for this one run, exactly as on the old version (no
  8055. # regression, no false disclosure).
  8056. if [ "$(printf '%s' "$stored" | tr -dc ':' | wc -c | tr -d ' ')" = "2" ]; then
  8057. local stored_sampled="files-sampled:${stored#files-shallow:}"
  8058. case "$current" in
  8059. files-sampled:*)
  8060. if [ "${current#"${stored_sampled}":}" != "$current" ]; then
  8061. echo "reuse"; return 0
  8062. fi
  8063. ;;
  8064. esac
  8065. fi
  8066. ;;
  8067. git:*)
  8068. # git-mode format transition: a stored pre-content-hash signature
  8069. # ("git:<HEAD>:<dirty>") cannot be compared directly against the
  8070. # new content-identity "gitc:" format, so the first run after the
  8071. # upgrade would falsely claim "codebase changed". Recompute the
  8072. # OLD-format signature and compare against the stored value: if it
  8073. # matches, the tree is unchanged at the old format's trust level
  8074. # (HEAD + dirty porcelain) -> reuse, honestly. The next persist
  8075. # upgrades the stored format to "gitc:". Only honor this when the
  8076. # current signature is the new git format (a real format change),
  8077. # never when both are old git: (that path already matched above).
  8078. case "$current" in
  8079. gitc:*)
  8080. local legacy
  8081. legacy=$(_loki_compute_legacy_git_signature "${TARGET_DIR:-.}")
  8082. if [ -n "$legacy" ] && [ "$legacy" = "$stored" ]; then
  8083. echo "reuse"; return 0
  8084. fi
  8085. ;;
  8086. esac
  8087. ;;
  8088. esac
  8089. echo "update"
  8090. fi
  8091. }
  8092. # Persist the current codebase signature after a clean no-PRD iteration that has
  8093. # a generated PRD, so the next run can decide reuse vs update. Best-effort; never
  8094. # fails the run. Only records on exit_code==0 (do not bless a broken iteration).
  8095. persist_prd_signature_if_present() {
  8096. local exit_code="${1:-0}"
  8097. [ "$exit_code" = "0" ] || return 0
  8098. # Hand-edited (user-owned) PRD: do NOT rewrite the signature. Re-hashing the
  8099. # user's edited file would re-baseline its content as the new prd_sha, so the
  8100. # next run would fall through to plain reuse with the wrong (non-user-owned)
  8101. # disclosure. Preserve the prior Loki-authored prd_sha/generated_at so every
  8102. # subsequent run keeps detecting user_owned until --fresh-prd forces a regen.
  8103. [ "${GENERATED_PRD_ACTION:-}" = "user_owned" ] && return 0
  8104. # only for no-PRD runs whose generated PRD exists
  8105. case "${prd_path:-}" in
  8106. ""|*.loki/generated-prd.md|*.loki/generated-prd.json) ;;
  8107. *) return 0 ;;
  8108. esac
  8109. local loki_dir="${TARGET_DIR:-.}/.loki"
  8110. [ -f "$loki_dir/generated-prd.md" ] || [ -f "$loki_dir/generated-prd.json" ] || return 0
  8111. local sig
  8112. sig=$(compute_codebase_signature "${TARGET_DIR:-.}")
  8113. [ -n "$sig" ] || return 0
  8114. mkdir -p "$loki_dir/state" 2>/dev/null || return 0
  8115. local mode="files"; case "$sig" in git:*|gitc:*) mode="git" ;; esac
  8116. # Record the content hash of the PRD file Loki just wrote so a later
  8117. # hand-edit by the user is detectable (decide_generated_prd_action). This
  8118. # runs AFTER the agent's own PRD writes, so Loki's updates are not mistaken
  8119. # for user edits.
  8120. local prd_sha; prd_sha=$(_loki_prd_file_hash "${TARGET_DIR:-.}")
  8121. local tmp="$loki_dir/state/.prd-signature.json.tmp.$$"
  8122. # git-mode format upgrade (old "git:<HEAD>:<dirty>" -> new content-identity
  8123. # "gitc:..."): when this run is a reuse honored via the decide transition
  8124. # (the recomputed legacy signature still matches the stored one), the PRD
  8125. # content did not change, so the generated_at date must be preserved across
  8126. # the upgrade, exactly like the files: -> files-sampled: upgrade clauses.
  8127. # Recompute the legacy value once and pass a match flag to the persist below.
  8128. local git_upgrade_match=""
  8129. case "$sig" in
  8130. gitc:*)
  8131. local _stored_sig
  8132. _stored_sig=$(LOKI_SIG_FILE="$loki_dir/state/prd-signature.json" python3 -c "
  8133. import json, os
  8134. try:
  8135. print(json.load(open(os.environ['LOKI_SIG_FILE'])).get('signature',''))
  8136. except Exception:
  8137. print('')
  8138. " 2>/dev/null)
  8139. case "$_stored_sig" in
  8140. git:*)
  8141. local _legacy
  8142. _legacy=$(_loki_compute_legacy_git_signature "${TARGET_DIR:-.}")
  8143. [ -n "$_legacy" ] && [ "$_legacy" = "$_stored_sig" ] && git_upgrade_match=1
  8144. ;;
  8145. esac
  8146. ;;
  8147. esac
  8148. # Preserve generated_at when the codebase signature is unchanged so the
  8149. # reuse disclosure ("generated on <date>") stays honest across reuse runs;
  8150. # only stamp a new date when the PRD content actually changed (sig differs).
  8151. LOKI_SIG="$sig" LOKI_SIG_MODE="$mode" LOKI_SIG_VER="$(get_version 2>/dev/null || echo unknown)" \
  8152. LOKI_PRD_SHA="$prd_sha" LOKI_SIG_FILE="$loki_dir/state/prd-signature.json" \
  8153. LOKI_GIT_UPGRADE_MATCH="$git_upgrade_match" \
  8154. python3 -c "
  8155. import json, os, datetime
  8156. sig = os.environ['LOKI_SIG']
  8157. prev = {}
  8158. try:
  8159. prev = json.load(open(os.environ['LOKI_SIG_FILE']))
  8160. except Exception:
  8161. prev = {}
  8162. prev_at = prev.get('generated_at') if isinstance(prev, dict) else None
  8163. prev_sig = prev.get('signature') if isinstance(prev, dict) else None
  8164. # Unchanged, OR the v7.32.3 files-signature format upgrade (#569): the new
  8165. # 4-field signature extends an old 3-field one whose listing fields match.
  8166. # Preserve the date in both cases; the PRD content did not change.
  8167. _legacy_upgrade = (
  8168. isinstance(prev_sig, str) and prev_sig.startswith('files:')
  8169. and prev_sig.count(':') == 2
  8170. and sig.startswith(prev_sig + ':')
  8171. )
  8172. # #171 format upgrade: a stored pre-#171 size-blind 'files-shallow:<listing>:
  8173. # <count>' (3 fields) reused into the new sampled 'files-sampled:<listing>:
  8174. # <count>:<samplehash>' whose listing fields match. Same trust level (the
  8175. # decide returned reuse), so the PRD content did not change: preserve the date.
  8176. _sampled_upgrade = (
  8177. isinstance(prev_sig, str) and prev_sig.startswith('files-shallow:')
  8178. and prev_sig.count(':') == 2
  8179. and sig.startswith('files-sampled:' + prev_sig[len('files-shallow:'):] + ':')
  8180. )
  8181. # git-mode format upgrade (old 'git:<HEAD>:<dirty>' -> new content-identity
  8182. # 'gitc:...'): the caller recomputed the legacy signature and confirmed it still
  8183. # matches the stored one (decide returned reuse), so the PRD content did not
  8184. # change: preserve the date across the one-time upgrade.
  8185. _git_upgrade = (
  8186. bool(os.environ.get('LOKI_GIT_UPGRADE_MATCH'))
  8187. and isinstance(prev_sig, str) and prev_sig.startswith('git:')
  8188. and sig.startswith('gitc:')
  8189. )
  8190. if prev_at and (prev_sig == sig or _legacy_upgrade or _sampled_upgrade or _git_upgrade):
  8191. generated_at = prev_at
  8192. else:
  8193. generated_at = datetime.datetime.now(datetime.timezone.utc).isoformat().replace('+00:00','Z')
  8194. rec = {
  8195. 'signature': sig,
  8196. 'generated_at': generated_at,
  8197. 'prd_path': '.loki/generated-prd.md',
  8198. 'prd_sha': os.environ.get('LOKI_PRD_SHA',''),
  8199. 'mode': os.environ['LOKI_SIG_MODE'],
  8200. 'loki_version': os.environ['LOKI_SIG_VER'],
  8201. 'source': 'generated',
  8202. }
  8203. print(json.dumps(rec))
  8204. " > "$tmp" 2>/dev/null && mv -f "$tmp" "$loki_dir/state/prd-signature.json" 2>/dev/null || rm -f "$tmp" 2>/dev/null
  8205. }
  8206. # Persist an explicit user-provided PRD into the canonical generated-PRD slot so
  8207. # later no-file runs continue from it (brownfield reuse), and stamp source:"user"
  8208. # so it is always treated as user-owned (reuse/use-as-is), never incrementally
  8209. # updated. Echoes the canonical relative path (".loki/generated-prd.md") on a
  8210. # successful persist so the caller can repoint prd_path; echoes nothing (empty)
  8211. # and changes no state on any failure (the caller then keeps the original path).
  8212. #
  8213. # $1 = the original user PRD file path (the arg passed to run_autonomous).
  8214. # Reads/writes under "${TARGET_DIR:-.}/.loki" to stay aligned with
  8215. # decide_generated_prd_action and _loki_prd_file_hash (which anchor there too).
  8216. persist_user_prd() {
  8217. local src="$1"
  8218. [ -n "$src" ] || { echo ""; return 0; }
  8219. [ -f "$src" ] || { echo ""; return 0; }
  8220. # Skip when the source already IS the canonical generated PRD (a no-op copy,
  8221. # and the no-file reuse path owns that case). Mirrors the persist guard.
  8222. case "$src" in
  8223. *.loki/generated-prd.md|*.loki/generated-prd.json) echo ""; return 0 ;;
  8224. esac
  8225. local loki_dir="${TARGET_DIR:-.}/.loki"
  8226. mkdir -p "$loki_dir" "$loki_dir/state" 2>/dev/null || { echo ""; return 0; }
  8227. # Atomic copy: write to a temp file in the destination dir, then mv into
  8228. # place so a concurrent reader never sees a half-written PRD.
  8229. local dest="$loki_dir/generated-prd.md"
  8230. local tmp_prd="$loki_dir/.generated-prd.md.tmp.$$"
  8231. cp -f "$src" "$tmp_prd" 2>/dev/null || { rm -f "$tmp_prd" 2>/dev/null; echo ""; return 0; }
  8232. mv -f "$tmp_prd" "$dest" 2>/dev/null || { rm -f "$tmp_prd" 2>/dev/null; echo ""; return 0; }
  8233. # Content hash of the PRD we just wrote (over the copied file) + the current
  8234. # codebase signature, recorded directly (NOT via persist_prd_signature_if_present,
  8235. # whose guards skip non-canonical/user paths and whose user_owned early-return
  8236. # would skip it anyway). source:"user" makes decide_generated_prd_action short
  8237. # circuit to user_owned on every later no-file run (LOCK 2).
  8238. local prd_sha sig mode
  8239. prd_sha=$(_loki_prd_file_hash "${TARGET_DIR:-.}")
  8240. sig=$(compute_codebase_signature "${TARGET_DIR:-.}")
  8241. mode="files"; case "$sig" in git:*|gitc:*) mode="git" ;; esac
  8242. local sig_tmp="$loki_dir/state/.prd-signature.json.tmp.$$"
  8243. LOKI_SIG="$sig" LOKI_SIG_MODE="$mode" \
  8244. LOKI_SIG_VER="$(get_version 2>/dev/null || echo unknown)" \
  8245. LOKI_PRD_SHA="$prd_sha" LOKI_ORIGIN_PATH="$src" \
  8246. python3 -c "
  8247. import json, os, datetime
  8248. rec = {
  8249. 'signature': os.environ.get('LOKI_SIG',''),
  8250. 'generated_at': datetime.datetime.now(datetime.timezone.utc).isoformat().replace('+00:00','Z'),
  8251. 'prd_path': '.loki/generated-prd.md',
  8252. 'prd_sha': os.environ.get('LOKI_PRD_SHA',''),
  8253. 'mode': os.environ.get('LOKI_SIG_MODE','files'),
  8254. 'loki_version': os.environ.get('LOKI_SIG_VER','unknown'),
  8255. 'source': 'user',
  8256. 'origin_path': os.environ.get('LOKI_ORIGIN_PATH',''),
  8257. }
  8258. print(json.dumps(rec))
  8259. " > "$sig_tmp" 2>/dev/null \
  8260. && mv -f "$sig_tmp" "$loki_dir/state/prd-signature.json" 2>/dev/null \
  8261. || rm -f "$sig_tmp" 2>/dev/null
  8262. echo ".loki/generated-prd.md"
  8263. }
  8264. # generate_proof_of_run: thin fire-and-forget wrapper around the standalone
  8265. # proof-of-run generator (autonomy/lib/proof-generator.py). Runs on both
  8266. # success and failure session ends. The generator owns the schema, redaction
  8267. # chokepoint, and HTML rendering; this wrapper only resolves the path and
  8268. # invokes python3. Never fails the session (|| true at the call site).
  8269. # NOTE: no inline python here on purpose -- keep this wrapper apostrophe-free
  8270. # to avoid the bash single-quote trap.
  8271. generate_proof_of_run() {
  8272. local _result="${1:-0}"
  8273. local gen="$SCRIPT_DIR/lib/proof-generator.py"
  8274. [ -f "$gen" ] || return 0
  8275. local loki_dir="${TARGET_DIR:-.}/.loki"
  8276. [ -d "$loki_dir" ] || return 0
  8277. local ver provider
  8278. ver="$(get_version 2>/dev/null || echo unknown)"
  8279. provider="${PROVIDER_NAME:-claude}"
  8280. # Proven PR (Loop 6 / Slice A2): resolve a deterministic run_id so the
  8281. # proof + the PR Evidence Receipt agree, and persist a stable pointer the PR
  8282. # sites read (never newest-by-mtime). The generator otherwise mints a fresh
  8283. # _gen_run_id() when LOKI_SESSION_ID is unset (the `loki start ./prd.md`
  8284. # case), which the later PR step could not know. Gated on the SAME flag as the
  8285. # receipt so LOKI_PROVEN_PR=0 is a byte-identical no-op (no --run-id passed,
  8286. # no pointer written): the pointer is only ever READ by the renderer, which
  8287. # is itself off under that flag.
  8288. if [ "${LOKI_PROVEN_PR:-1}" != "0" ]; then
  8289. local _rid=""
  8290. if declare -f _loki_trust_run_id >/dev/null 2>&1; then
  8291. _rid="$(_loki_trust_run_id 2>/dev/null || true)"
  8292. fi
  8293. # Fall back to a locally minted id when no persisted per-run id exists.
  8294. # Do NOT call _loki_trust_run_id --new here: that would clobber the
  8295. # trust-events run-id file; this is a read-or-mint-local resolution.
  8296. if [ -z "$_rid" ]; then
  8297. _rid="proof-$(date -u +%Y%m%d%H%M%S 2>/dev/null || echo 0)-$$-${RANDOM:-0}"
  8298. fi
  8299. ITERATION_COUNT="${ITERATION_COUNT:-0}" \
  8300. PROVIDER_NAME="$provider" \
  8301. PRD_PATH="${prd_path:-}" \
  8302. python3 -E "$gen" \
  8303. --loki-dir "$loki_dir" \
  8304. --loki-version "$ver" \
  8305. --provider "$provider" \
  8306. --session-exit-code "$_result" \
  8307. --run-id "$_rid" \
  8308. --quiet >/dev/null 2>&1 || true
  8309. # Persist the resolved run_id atomically (.tmp + mv) so the PR sites read
  8310. # the exact run the generator wrote, never an mtime guess.
  8311. local _id_dir="$loki_dir/state"
  8312. local _id_file="$_id_dir/last-proof-id.txt"
  8313. mkdir -p "$_id_dir" 2>/dev/null || true
  8314. if printf '%s' "$_rid" > "${_id_file}.tmp" 2>/dev/null; then
  8315. mv -f "${_id_file}.tmp" "$_id_file" 2>/dev/null || rm -f "${_id_file}.tmp" 2>/dev/null || true
  8316. fi
  8317. # Opt-in build-outcome analytics (default OFF; strict second-layer gate in
  8318. # telemetry.sh). Reads the just-written receipt's allowlisted scalars only;
  8319. # trust-core untouched. Fire-and-forget, never fails the session.
  8320. if declare -f loki_emit_build_verified >/dev/null 2>&1; then
  8321. loki_emit_build_verified "$loki_dir/proofs/$_rid/proof.json" 2>/dev/null || true
  8322. fi
  8323. return 0
  8324. fi
  8325. ITERATION_COUNT="${ITERATION_COUNT:-0}" \
  8326. PROVIDER_NAME="$provider" \
  8327. PRD_PATH="${prd_path:-}" \
  8328. python3 -E "$gen" \
  8329. --loki-dir "$loki_dir" \
  8330. --loki-version "$ver" \
  8331. --provider "$provider" \
  8332. --session-exit-code "$_result" \
  8333. --quiet >/dev/null 2>&1 || true
  8334. return 0
  8335. }
  8336. # capture_preedit_snapshot: freeze the agent's raw diff BEFORE anything else
  8337. # touches the tree, so quality numbers measure the agent and not the
  8338. # agent-plus-whoever-fixed-it (autonomy/lib/preedit_snapshot.py owns the schema
  8339. # and the write-once rule; this is only the call site).
  8340. #
  8341. # WHY THIS IS NOT INSIDE generate_proof_of_run, even though the receipt is the
  8342. # obvious neighbour. Two reasons, both measured in this file:
  8343. #
  8344. # 1. TOO LATE AT THE LATE PROOF SITES. commit_session_changes commits the
  8345. # session's work, and the module's default baseline is `git diff HEAD`.
  8346. # After that commit `git diff HEAD` is EMPTY, so a capture at the teardown
  8347. # proof site would freeze an empty diff -- and because the snapshot is
  8348. # write-once by design, that empty capture would be permanent and
  8349. # unrecoverable. run.sh already documents this mutation window itself: the
  8350. # comment above the final generate_proof_of_run call says "HANDOFF.md and
  8351. # commit_session_changes can change the worktree after the earlier receipt".
  8352. # The receipt can be regenerated against a later tree; the snapshot cannot.
  8353. # 2. WRONG GATE. Every generate_proof_of_run call site is gated on
  8354. # LOKI_PROOF!=0, and the run_id resolution only exists inside its
  8355. # LOKI_PROVEN_PR!=0 branch. Authorship evidence and shareable proofs are
  8356. # different concerns, so a user who turns off proofs must not silently lose
  8357. # the ability to tell agent output from human edits.
  8358. #
  8359. # So the capture happens EARLIER, immediately after run_autonomous returns,
  8360. # before any post-processing step can modify the diff.
  8361. #
  8362. # Baseline: _LOKI_RUN_START_SHA (exported at runner init, persisted to
  8363. # .loki/state/start-sha) is passed when available, so the snapshot is anchored
  8364. # to the run's own starting commit rather than to a moving HEAD. That makes the
  8365. # capture correct even if a later caller fires after a commit. Falls back to the
  8366. # module's `git diff HEAD` default when no baseline resolved (greenfield repos
  8367. # with no commits write an empty file there by design).
  8368. #
  8369. # run_id: read-path _loki_trust_run_id ONLY, never --new (minting here would
  8370. # clobber the trust-events id file). If it resolves empty we SKIP: a snapshot
  8371. # filed under an id nothing else references is worse than no snapshot, because
  8372. # verdict.py would count it as authorship evidence that no receipt can join to.
  8373. #
  8374. # Guarded and non-fatal throughout: a diagnostic must never break the run it is
  8375. # diagnosing. Write-once makes repeat calls free (later ones return "exists"),
  8376. # so the earliest caller wins and extra call sites cost nothing.
  8377. capture_preedit_snapshot() {
  8378. local snap="$SCRIPT_DIR/lib/preedit_snapshot.py"
  8379. [ -f "$snap" ] || return 0
  8380. command -v python3 >/dev/null 2>&1 || return 0
  8381. # Match _loki_trust_run_id's dir expression, not generate_proof_of_run's:
  8382. # with LOKI_DIR set, ${TARGET_DIR:-.}/.loki would write the snapshot beside
  8383. # a run-id file that lives somewhere else.
  8384. local loki_dir="${LOKI_DIR:-${TARGET_DIR:-.}/.loki}"
  8385. [ -d "$loki_dir" ] || return 0
  8386. local _rid=""
  8387. if declare -f _loki_trust_run_id >/dev/null 2>&1; then
  8388. _rid="$(_loki_trust_run_id 2>/dev/null || true)"
  8389. fi
  8390. [ -n "$_rid" ] || return 0
  8391. # Resolve the baseline from the PERSISTED file when the exported variable is
  8392. # not visible. _LOKI_RUN_START_SHA is exported inside run_autonomous, and in
  8393. # PARALLEL_MODE run_autonomous runs in a subshell -- an export from a
  8394. # subshell never reaches the parent, so at that call site the variable is
  8395. # empty and only the file survives. Same read the pause path already does.
  8396. # Without this the parallel branch would silently fall back to `git diff
  8397. # HEAD` instead of the anchored baseline this function documents.
  8398. local _base="${_LOKI_RUN_START_SHA:-}"
  8399. [ -n "$_base" ] || _base="$(cat "$loki_dir/state/start-sha" 2>/dev/null || true)"
  8400. # LOKI_PREEDIT_CWD is load-bearing: the module defaults cwd to os.getcwd(),
  8401. # and if that is not the target repo capture returns not_a_git_repo and
  8402. # writes nothing SILENTLY -- a call site that looks wired but never fires.
  8403. LOKI_DIR="$loki_dir" \
  8404. LOKI_PREEDIT_CWD="${TARGET_DIR:-.}" \
  8405. LOKI_RUN_START_SHA="$_base" \
  8406. python3 "$snap" capture "$_rid" >/dev/null 2>&1 || true
  8407. return 0
  8408. }
  8409. # print_ttfv_next_steps: R7 zero-config first-run "what next / go deeper"
  8410. # message. The wording MUST match what actually ran, so it branches on the mode:
  8411. # - brief: a one-line brief ran on the lightweight profile (council off,
  8412. # simple tier, capped iterations). Proof contains diffs, cost, time
  8413. # (council verdicts are absent because the council was disabled).
  8414. # - repo: a no-arg in-repo run analyzed the codebase and ran at full depth
  8415. # (council on). Proof contains diffs, cost, time, and council
  8416. # verdicts.
  8417. # This function only prints; the caller owns the TTY gate. Never fails the run.
  8418. # Usage: print_ttfv_next_steps <mode> <result>
  8419. print_ttfv_next_steps() {
  8420. local mode="${1:-}"
  8421. local result="${2:-0}"
  8422. local loki_dir="${TARGET_DIR:-.}/.loki"
  8423. local proofs_dir="$loki_dir/proofs"
  8424. echo ""
  8425. echo "============================================================"
  8426. if [ "$result" = "0" ]; then
  8427. echo " First pass complete. Here is what you have:"
  8428. else
  8429. echo " First pass ended early. Here is what was produced:"
  8430. fi
  8431. echo "============================================================"
  8432. echo ""
  8433. echo " What I did:"
  8434. if [ "$mode" = "brief" ] || [ "$mode" = "repo-fast" ]; then
  8435. if [ "$mode" = "repo-fast" ]; then
  8436. echo " - Inspected your repository on a fast, lightweight first"
  8437. echo " pass (council off, simple tier, capped iterations)."
  8438. else
  8439. echo " - Worked from your one-line brief on a fast, lightweight first"
  8440. echo " pass (council off, simple tier, capped iterations)."
  8441. fi
  8442. echo " - Generated a proof-of-run (diffs, cost, time)."
  8443. else
  8444. echo " - Analyzed your codebase and generated a PRD, then ran a full"
  8445. echo " first pass (council on, full RARV-C depth)."
  8446. echo " - Generated a proof-of-run (diffs, cost, time, council verdicts)."
  8447. fi
  8448. echo ""
  8449. echo " See the visible artifact (proof-of-run):"
  8450. if [ -d "$proofs_dir" ]; then
  8451. local latest
  8452. latest=$(ls -1t "$proofs_dir" 2>/dev/null | head -1)
  8453. if [ -n "$latest" ]; then
  8454. echo " loki proof open $latest"
  8455. echo " (or open $proofs_dir/$latest/index.html)"
  8456. else
  8457. echo " loki proof list"
  8458. fi
  8459. else
  8460. echo " loki proof list"
  8461. fi
  8462. echo ""
  8463. if [ "$mode" = "brief" ] || [ "$mode" = "repo-fast" ]; then
  8464. echo " Go deeper (full RARV-C depth, council-gated):"
  8465. echo " loki start # continue / harden this project"
  8466. echo " loki start ./prd.md # build from a full PRD"
  8467. else
  8468. echo " Next steps:"
  8469. echo " loki start ./prd.md # build from a full PRD"
  8470. echo " loki start \"<one line>\" # fast first pass from a brief"
  8471. fi
  8472. # Frictionless discovery: surface 'loki why' so users learn the
  8473. # what-happened/what-to-do diagnosis without having to know it exists.
  8474. echo " loki why # explain this outcome + what to do next"
  8475. echo ""
  8476. return 0
  8477. }
  8478. # _read_iteration_cost <iteration>
  8479. # Emit "input output cost cache_read cache_creation" for the given iteration,
  8480. # preferring the authoritative result-cost file written by the embedded stream
  8481. # parser (Claude'\''s own total_cost_usd + usage, slug/symlink-independent) over
  8482. # the context-tracker-derived estimate in tracking.json. Falls back to
  8483. # tracking.json when no result-cost file exists, and to all zeros otherwise.
  8484. # Best-effort: any parse failure yields "0 0 0 0 0" and never aborts.
  8485. _read_iteration_cost() {
  8486. local iteration="$1"
  8487. local result_cost_file=".loki/metrics/result-cost-${iteration}.json"
  8488. if [ -f "$result_cost_file" ]; then
  8489. python3 -c "
  8490. import json
  8491. def _c(v):
  8492. return v if isinstance(v, (int, float)) and not isinstance(v, bool) else 'unmeasured'
  8493. try:
  8494. d = json.load(open('$result_cost_file'))
  8495. print(
  8496. d.get('input_tokens', 0) or 0,
  8497. d.get('output_tokens', 0) or 0,
  8498. _c(d.get('total_cost_usd')),
  8499. d.get('cache_read_tokens', 0) or 0,
  8500. d.get('cache_creation_tokens', 0) or 0,
  8501. )
  8502. except Exception:
  8503. print(0, 0, 'unmeasured', 0, 0)
  8504. " 2>/dev/null || echo "0 0 unmeasured 0 0"
  8505. elif [ -f ".loki/context/tracking.json" ]; then
  8506. python3 -c "
  8507. import json
  8508. def _c(v):
  8509. return v if isinstance(v, (int, float)) and not isinstance(v, bool) else 'unmeasured'
  8510. try:
  8511. t = json.load(open('.loki/context/tracking.json'))
  8512. iters = t.get('per_iteration', [])
  8513. match = [i for i in iters if i.get('iteration') == $iteration]
  8514. if match:
  8515. m = match[-1]
  8516. print(
  8517. m.get('input_tokens', 0),
  8518. m.get('output_tokens', 0),
  8519. _c(m.get('cost_usd')),
  8520. m.get('cache_read_tokens', 0),
  8521. m.get('cache_creation_tokens', 0),
  8522. )
  8523. else:
  8524. print(0, 0, 'unmeasured', 0, 0)
  8525. except Exception:
  8526. print(0, 0, 'unmeasured', 0, 0)
  8527. " 2>/dev/null || echo "0 0 unmeasured 0 0"
  8528. else
  8529. echo "0 0 unmeasured 0 0"
  8530. fi
  8531. }
  8532. track_iteration_complete() {
  8533. local iteration="$1"
  8534. local exit_code="${2:-0}"
  8535. local task_id="iteration-$iteration"
  8536. mkdir -p .loki/queue
  8537. # Calculate iteration duration (SYN-018)
  8538. local duration_ms
  8539. duration_ms=$(get_iteration_duration_ms)
  8540. # Emit iteration complete event for dashboard
  8541. local status_str
  8542. [ "$exit_code" = "0" ] && status_str="completed" || status_str="failed"
  8543. emit_event_json "iteration_complete" \
  8544. "iteration=$iteration" \
  8545. "status=$status_str" \
  8546. "exitCode=$exit_code" \
  8547. "provider=${PROVIDER_NAME:-claude}"
  8548. # Also emit to pending dir for OTEL bridge
  8549. if [ -n "${LOKI_OTEL_ENDPOINT:-}" ]; then
  8550. emit_event_pending "iteration_complete" \
  8551. "iteration=$iteration" \
  8552. "status=$status_str" \
  8553. "exit_code=$exit_code"
  8554. fi
  8555. # Emit learning signals based on outcome (SYN-018)
  8556. if [ "$exit_code" = "0" ]; then
  8557. # Success pattern for completed iteration
  8558. emit_learning_signal success_pattern \
  8559. --source cli \
  8560. --action "iteration_complete" \
  8561. --pattern-name "rarv_iteration" \
  8562. --action-sequence '["reason", "act", "reflect", "verify"]' \
  8563. --duration "$((duration_ms / 1000))" \
  8564. --outcome success \
  8565. --context "{\"iteration\":$iteration,\"provider\":\"${PROVIDER_NAME:-claude}\"}"
  8566. # Tool efficiency signal
  8567. emit_learning_signal tool_efficiency \
  8568. --source cli \
  8569. --action "iteration_complete" \
  8570. --tool-name "${PROVIDER_NAME:-claude}" \
  8571. --execution-time-ms "$duration_ms" \
  8572. --outcome success \
  8573. --context "{\"iteration\":$iteration}"
  8574. else
  8575. # Error pattern for failed iteration
  8576. emit_learning_signal error_pattern \
  8577. --source cli \
  8578. --action "iteration_complete" \
  8579. --error-type "IterationFailure" \
  8580. --error-message "Iteration $iteration failed with exit code $exit_code" \
  8581. --recovery-steps '["Check logs", "Review error output", "Retry iteration"]' \
  8582. --context "{\"iteration\":$iteration,\"provider\":\"${PROVIDER_NAME:-claude}\",\"exit_code\":$exit_code}"
  8583. # Tool efficiency signal with failure
  8584. emit_learning_signal tool_efficiency \
  8585. --source cli \
  8586. --action "iteration_failed" \
  8587. --tool-name "${PROVIDER_NAME:-claude}" \
  8588. --execution-time-ms "$duration_ms" \
  8589. --outcome failure \
  8590. --context "{\"iteration\":$iteration,\"exit_code\":$exit_code}"
  8591. fi
  8592. # Track context window usage FIRST to get token data (v5.42.0)
  8593. track_context_usage "$iteration"
  8594. # Write efficiency tracking file for /api/cost endpoint.
  8595. # The recorded model MUST be the one that actually ran this iteration. The
  8596. # dispatch exports LOKI_CURRENT_MODEL="$tier_param" (the exact --model value)
  8597. # right before the provider call, AFTER every mutation (opus-pin force,
  8598. # LOKI_MAX_TIER clamp, mid-flight override). That is the single source of
  8599. # truth -- reading it avoids re-deriving the model here and drifting from
  8600. # dispatch. Hardcoding PROVIDER_MODEL_DEVELOPMENT (the old code) mislabeled
  8601. # every non-development iteration: a haiku/fast pin was recorded "sonnet" and
  8602. # an opus pin (tier_param forced to opus) was recorded "sonnet" too, making
  8603. # the model-equivalence bench unfalsifiable. Resolver is the fallback for the
  8604. # policy-blocked path where no dispatch ran (LOKI_CURRENT_MODEL unset).
  8605. mkdir -p .loki/metrics/efficiency
  8606. local model_tier="${LOKI_CURRENT_MODEL:-}"
  8607. if [ -z "$model_tier" ]; then
  8608. model_tier="$(get_provider_tier_param "${CURRENT_TIER:-development}" 2>/dev/null)"
  8609. fi
  8610. # Fallback to the old per-provider default only if both the dispatched model
  8611. # and the resolver are unavailable (e.g. provider config not sourced) so we
  8612. # never write an empty model.
  8613. if [ -z "$model_tier" ]; then
  8614. model_tier="${PROVIDER_MODEL_DEVELOPMENT:-sonnet}"
  8615. fi
  8616. local phase="${LAST_KNOWN_PHASE:-}"
  8617. [ -z "$phase" ] && phase=$(python3 -c "import json; print(json.load(open('.loki/state/orchestrator.json')).get('currentPhase', 'unknown'))" 2>/dev/null || echo "unknown")
  8618. # Read token data, preferring Claude'\''s authoritative result-cost file over
  8619. # the context-tracker estimate (v7.28.0 cost-capture fix). See
  8620. # _read_iteration_cost for precedence rationale.
  8621. # v6.82.0: also capture cache_read_tokens / cache_creation_tokens for
  8622. # prompt-cache hit-rate analysis (S1.1 prompt restructure).
  8623. local iter_input=0 iter_output=0 iter_cost=0 iter_cost_line=""
  8624. local iter_cache_read=0 iter_cache_creation=0
  8625. iter_cost_line="$(_read_iteration_cost "$iteration")"
  8626. read -r iter_input iter_output iter_cost iter_cache_read iter_cache_creation << ITER_COST_EOF
  8627. $iter_cost_line
  8628. ITER_COST_EOF
  8629. # Unknown cost is omitted with a reason, never written as 0 (readers treat absence as unmeasured).
  8630. local iter_cost_json
  8631. case "${iter_cost:-unmeasured}" in
  8632. unmeasured) iter_cost_json=' "cost_unmeasured_reason": "no dollar figure recorded for this iteration",
  8633. ' ;;
  8634. *) iter_cost_json=" \"cost_usd\": ${iter_cost},
  8635. " ;;
  8636. esac
  8637. cat > ".loki/metrics/efficiency/iteration-${iteration}.json" << EFF_EOF
  8638. {
  8639. "iteration": $iteration,
  8640. "model": "$model_tier",
  8641. "phase": "$phase",
  8642. "duration_ms": $duration_ms,
  8643. "provider": "${PROVIDER_NAME:-claude}",
  8644. "status": "$status_str",
  8645. "input_tokens": ${iter_input:-0},
  8646. "output_tokens": ${iter_output:-0},
  8647. "cache_read_tokens": ${iter_cache_read:-0},
  8648. "cache_creation_tokens": ${iter_cache_creation:-0},
  8649. ${iter_cost_json} "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
  8650. }
  8651. EFF_EOF
  8652. # Check notification triggers (v5.40.0)
  8653. check_notification_triggers "$iteration"
  8654. # Sync completed GitHub tasks back to issues (v5.41.0)
  8655. sync_github_completed_tasks
  8656. # Get task from in-progress
  8657. local in_progress_file=".loki/queue/in-progress.json"
  8658. local completed_file=".loki/queue/completed.json"
  8659. local failed_file=".loki/queue/failed.json"
  8660. # Initialize files if needed
  8661. [ ! -f "$completed_file" ] && echo "[]" > "$completed_file"
  8662. [ ! -f "$failed_file" ] && echo "[]" > "$failed_file"
  8663. # Build the completed iteration record + move it out of in-progress, ATOMICALLY.
  8664. # v7.104.3 task-list accuracy fix: the old writer emitted a THIN body
  8665. # {id,type,title:"Iteration N",status,exitCode,provider} with no description
  8666. # and no logs, and then DELETED the rich in-progress record -- so the done
  8667. # column showed empty cards. We now LIFT the honest per-iteration parts (logs,
  8668. # startedAt) from the in-progress record BEFORE removing it, and give the card
  8669. # an HONEST iteration-scoped title/description derived from values in scope
  8670. # ($phase/$exit_code/$duration_ms). We deliberately do NOT reuse the borrowed
  8671. # PRD-story title: because pending stories never leave the queue, the
  8672. # in-progress title is always pending[0] ("server.js..."), so carrying it onto
  8673. # N done cards would falsely imply that story was built N times (fake-green).
  8674. # We also upsert-by-id (no cross-sub-run "iteration-1 x5" accumulation) and
  8675. # keep completed/failed mutually exclusive per id.
  8676. local target_file="$completed_file"
  8677. local other_file="$failed_file"
  8678. [ "$exit_code" != "0" ] && { target_file="$failed_file"; other_file="$completed_file"; }
  8679. local _completed_ts; _completed_ts="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
  8680. _LOKI_TASK_ID="$task_id" \
  8681. _LOKI_ITER="$iteration" \
  8682. _LOKI_PHASE="$phase" \
  8683. _LOKI_EXIT="$exit_code" \
  8684. _LOKI_DUR="$duration_ms" \
  8685. _LOKI_PROVIDER="${PROVIDER_NAME:-claude}" \
  8686. _LOKI_COMPLETED_TS="$_completed_ts" \
  8687. _LOKI_TARGET="$target_file" \
  8688. _LOKI_OTHER="$other_file" \
  8689. _LOKI_INPROG="$in_progress_file" \
  8690. python3 -c "
  8691. import json, os
  8692. tid = os.environ['_LOKI_TASK_ID']
  8693. it = os.environ['_LOKI_ITER']
  8694. phase = os.environ.get('_LOKI_PHASE') or 'unknown'
  8695. exit_code = os.environ.get('_LOKI_EXIT', '1')
  8696. dur = os.environ.get('_LOKI_DUR', '')
  8697. provider = os.environ.get('_LOKI_PROVIDER', 'claude')
  8698. completed_ts = os.environ['_LOKI_COMPLETED_TS']
  8699. target = os.environ['_LOKI_TARGET']
  8700. other = os.environ['_LOKI_OTHER']
  8701. inprog = os.environ['_LOKI_INPROG']
  8702. ok = (exit_code == '0')
  8703. def load(p):
  8704. try:
  8705. with open(p) as f:
  8706. d = json.load(f)
  8707. return d if isinstance(d, list) else (d.get('tasks', []) if isinstance(d, dict) else [])
  8708. except Exception:
  8709. return []
  8710. # Lift the honest body from the in-progress record before removing it.
  8711. prior = next((t for t in load(inprog) if isinstance(t, dict) and t.get('id') == tid), {})
  8712. logs = prior.get('logs') if isinstance(prior.get('logs'), list) else []
  8713. started = prior.get('startedAt')
  8714. dur_s = ''
  8715. try:
  8716. if dur not in ('', None):
  8717. _ms = int(float(dur))
  8718. dur_s = f\", {_ms}ms\" if _ms < 1000 else f\", {round(_ms/1000)}s\"
  8719. except Exception:
  8720. dur_s = ''
  8721. if ok:
  8722. title = f'Iteration {it} complete - {phase}'
  8723. desc = f'Iteration {it} finished cleanly in the {phase} phase (exit 0{dur_s}).'
  8724. else:
  8725. title = f'Iteration {it} failed (exit {exit_code})'
  8726. desc = f'Iteration {it} ended in the {phase} phase with exit code {exit_code}{dur_s}.'
  8727. entry = {
  8728. 'id': tid,
  8729. 'type': 'iteration',
  8730. 'title': title,
  8731. 'description': desc,
  8732. 'status': 'completed' if ok else 'failed',
  8733. 'phase': phase,
  8734. 'completedAt': completed_ts,
  8735. 'exitCode': int(exit_code) if str(exit_code).lstrip('-').isdigit() else exit_code,
  8736. 'provider': provider,
  8737. 'logs': logs,
  8738. }
  8739. if started:
  8740. entry['startedAt'] = started
  8741. # Upsert-by-id into target (drop any stale same-id), cap at 50.
  8742. data = [t for t in load(target) if not (isinstance(t, dict) and t.get('id') == tid)]
  8743. data.append(entry)
  8744. data = data[-50:]
  8745. with open(target, 'w') as f:
  8746. json.dump(data, f, indent=2)
  8747. # Mutual exclusion: remove this id from the OTHER terminal file.
  8748. odata = [t for t in load(other) if not (isinstance(t, dict) and t.get('id') == tid)]
  8749. with open(other, 'w') as f:
  8750. json.dump(odata, f, indent=2)
  8751. # Remove from in-progress.
  8752. idata = [t for t in load(inprog) if not (isinstance(t, dict) and t.get('id') == tid)]
  8753. with open(inprog, 'w') as f:
  8754. json.dump(idata, f, indent=2)
  8755. " 2>/dev/null || echo "[{\"id\":\"$task_id\",\"type\":\"iteration\",\"title\":\"Iteration $iteration\",\"status\":\"$([ "$exit_code" = "0" ] && echo completed || echo failed)\",\"completedAt\":\"$_completed_ts\",\"exitCode\":$exit_code,\"provider\":\"${PROVIDER_NAME:-claude}\"}]" > "$target_file"
  8756. # BUG-ST-014: Atomic current-task.json clear via temp file + mv
  8757. local ct_tmp=".loki/queue/current-task.json.tmp.$$"
  8758. echo "{}" > "$ct_tmp"
  8759. mv -f "$ct_tmp" .loki/queue/current-task.json
  8760. # Write-back completed BMAD stories to source artifacts (v6.29.0)
  8761. if [ "$exit_code" = "0" ]; then
  8762. bmad_write_back
  8763. fi
  8764. }
  8765. start_status_monitor() {
  8766. loki_background_services_enabled || return 0
  8767. log_step "Starting status monitor..."
  8768. # Initial update
  8769. update_status_file
  8770. update_agents_state
  8771. write_dashboard_state
  8772. # Background update loop (2-second interval for realtime dashboard)
  8773. (
  8774. while true; do
  8775. update_status_file
  8776. update_agents_state
  8777. write_dashboard_state
  8778. sleep 2
  8779. done
  8780. ) &
  8781. STATUS_MONITOR_PID=$!
  8782. register_pid "$STATUS_MONITOR_PID" "status-monitor"
  8783. log_info "Status monitor started"
  8784. log_info "Monitor progress: ${CYAN}watch -n 2 cat .loki/STATUS.txt${NC}"
  8785. }
  8786. stop_status_monitor() {
  8787. if [ -n "$STATUS_MONITOR_PID" ]; then
  8788. kill "$STATUS_MONITOR_PID" 2>/dev/null || true
  8789. wait "$STATUS_MONITOR_PID" 2>/dev/null || true
  8790. unregister_pid "$STATUS_MONITOR_PID"
  8791. fi
  8792. stop_resource_monitor
  8793. }
  8794. #===============================================================================
  8795. # Web Dashboard
  8796. #===============================================================================
  8797. update_agents_state() {
  8798. # Aggregate agent information from .agent/sub-agents/*.json into .loki/state/agents.json
  8799. local agents_dir=".agent/sub-agents"
  8800. local output_file=".loki/state/agents.json"
  8801. # Initialize empty array if no agents directory
  8802. if [ ! -d "$agents_dir" ]; then
  8803. echo "[]" > "$output_file"
  8804. return
  8805. fi
  8806. # Find all agent JSON files and aggregate them
  8807. local agents_json="["
  8808. local first=true
  8809. for agent_file in "$agents_dir"/*.json; do
  8810. # Skip if no JSON files exist
  8811. [ -e "$agent_file" ] || continue
  8812. # Read agent JSON
  8813. local agent_data=$(cat "$agent_file" 2>/dev/null)
  8814. if [ -n "$agent_data" ]; then
  8815. # Add comma separator for all but first entry
  8816. if [ "$first" = true ]; then
  8817. first=false
  8818. else
  8819. agents_json="${agents_json},"
  8820. fi
  8821. agents_json="${agents_json}${agent_data}"
  8822. fi
  8823. done
  8824. agents_json="${agents_json}]"
  8825. # Write aggregated data (atomic via temp file + mv)
  8826. local tmp_file="${output_file}.tmp.$$"
  8827. echo "$agents_json" > "$tmp_file"
  8828. mv -f "$tmp_file" "$output_file" 2>/dev/null || rm -f "$tmp_file"
  8829. }
  8830. #===============================================================================
  8831. # Resource Monitoring
  8832. #===============================================================================
  8833. check_system_resources() {
  8834. # Check CPU and memory usage and write status to .loki/state/resources.json
  8835. local output_file=".loki/state/resources.json"
  8836. # Get CPU usage (average across all cores)
  8837. local cpu_usage=0
  8838. if [[ "$OSTYPE" == "darwin"* ]]; then
  8839. # macOS: get CPU idle from top header, calculate usage = 100 - idle
  8840. local idle=$(top -l 2 -n 0 | grep "CPU usage" | tail -1 | awk -F'[:,]' '{for(i=1;i<=NF;i++) if($i ~ /idle/) print $(i)}' | awk '{print int($1)}')
  8841. cpu_usage=$((100 - ${idle:-0}))
  8842. elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
  8843. # Linux: use top or mpstat
  8844. cpu_usage=$(top -bn2 | grep "Cpu(s)" | tail -1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print int(100 - $1)}')
  8845. else
  8846. cpu_usage=0
  8847. fi
  8848. # Get memory usage
  8849. local mem_usage=0
  8850. if [[ "$OSTYPE" == "darwin"* ]]; then
  8851. # macOS: use vm_stat
  8852. local page_size=$(pagesize)
  8853. local vm_stat=$(vm_stat)
  8854. local pages_free=$(echo "$vm_stat" | awk '/Pages free/ {print $3}' | tr -d '.')
  8855. local pages_active=$(echo "$vm_stat" | awk '/Pages active/ {print $3}' | tr -d '.')
  8856. local pages_inactive=$(echo "$vm_stat" | awk '/Pages inactive/ {print $3}' | tr -d '.')
  8857. local pages_speculative=$(echo "$vm_stat" | awk '/Pages speculative/ {print $3}' | tr -d '.')
  8858. local pages_wired=$(echo "$vm_stat" | awk '/Pages wired down/ {print $4}' | tr -d '.')
  8859. local total_pages=$((pages_free + pages_active + pages_inactive + pages_speculative + pages_wired))
  8860. local used_pages=$((pages_active + pages_wired))
  8861. mem_usage=$((used_pages * 100 / total_pages))
  8862. elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
  8863. # Linux: use free
  8864. mem_usage=$(free | grep Mem | awk '{print int($3/$2 * 100)}')
  8865. else
  8866. mem_usage=0
  8867. fi
  8868. # Determine status
  8869. local cpu_status="ok"
  8870. local mem_status="ok"
  8871. local overall_status="ok"
  8872. local warning_message=""
  8873. if [ "$cpu_usage" -ge "$RESOURCE_CPU_THRESHOLD" ]; then
  8874. cpu_status="high"
  8875. overall_status="warning"
  8876. warning_message="CPU usage is ${cpu_usage}% (threshold: ${RESOURCE_CPU_THRESHOLD}%). Consider reducing parallel agent count or pausing non-critical tasks."
  8877. fi
  8878. if [ "$mem_usage" -ge "$RESOURCE_MEM_THRESHOLD" ]; then
  8879. mem_status="high"
  8880. overall_status="warning"
  8881. if [ -n "$warning_message" ]; then
  8882. warning_message="${warning_message} Memory usage is ${mem_usage}% (threshold: ${RESOURCE_MEM_THRESHOLD}%)."
  8883. else
  8884. warning_message="Memory usage is ${mem_usage}% (threshold: ${RESOURCE_MEM_THRESHOLD}%). Consider reducing parallel agent count or cleaning up resources."
  8885. fi
  8886. fi
  8887. # Write JSON status
  8888. cat > "$output_file" << EOF
  8889. {
  8890. "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  8891. "cpu": {
  8892. "usage_percent": $cpu_usage,
  8893. "threshold_percent": $RESOURCE_CPU_THRESHOLD,
  8894. "status": "$cpu_status"
  8895. },
  8896. "memory": {
  8897. "usage_percent": $mem_usage,
  8898. "threshold_percent": $RESOURCE_MEM_THRESHOLD,
  8899. "status": "$mem_status"
  8900. },
  8901. "overall_status": "$overall_status",
  8902. "warning_message": "$warning_message"
  8903. }
  8904. EOF
  8905. # Log warning if resources are high
  8906. if [ "$overall_status" = "warning" ]; then
  8907. log_warn "RESOURCE WARNING: $warning_message"
  8908. fi
  8909. }
  8910. start_resource_monitor() {
  8911. loki_background_services_enabled || return 0
  8912. log_step "Starting resource monitor (checks every ${RESOURCE_CHECK_INTERVAL}s)..."
  8913. # Initial check
  8914. check_system_resources
  8915. # Background monitoring loop. BACKLOG 22: a bare `sleep N` inside this
  8916. # subshell survives a TERM to the subshell -- caught traps do not carry
  8917. # into a foreground child, so the sleep reparents to init and outlives the
  8918. # run for up to RESOURCE_CHECK_INTERVAL seconds. Backgrounding the sleep
  8919. # and recording its PID lets a TERM trap kill it directly; `wait` returns
  8920. # immediately once the trap fires, so shutdown is not delayed either.
  8921. (
  8922. _loki_rm_sleep_pid=""
  8923. trap '[ -n "$_loki_rm_sleep_pid" ] && kill "$_loki_rm_sleep_pid" 2>/dev/null; exit 0' TERM
  8924. while true; do
  8925. sleep "$RESOURCE_CHECK_INTERVAL" &
  8926. _loki_rm_sleep_pid=$!
  8927. wait "$_loki_rm_sleep_pid"
  8928. _loki_rm_sleep_pid=""
  8929. check_system_resources
  8930. done
  8931. ) &
  8932. RESOURCE_MONITOR_PID=$!
  8933. register_pid "$RESOURCE_MONITOR_PID" "resource-monitor"
  8934. log_info "Resource monitor started (CPU threshold: ${RESOURCE_CPU_THRESHOLD}%, Memory threshold: ${RESOURCE_MEM_THRESHOLD}%)"
  8935. log_info "Check status: ${CYAN}cat .loki/state/resources.json${NC}"
  8936. }
  8937. stop_resource_monitor() {
  8938. if [ -n "$RESOURCE_MONITOR_PID" ]; then
  8939. kill "$RESOURCE_MONITOR_PID" 2>/dev/null || true
  8940. wait "$RESOURCE_MONITOR_PID" 2>/dev/null || true
  8941. unregister_pid "$RESOURCE_MONITOR_PID"
  8942. fi
  8943. }
  8944. #===============================================================================
  8945. # Audit Logging (Enterprise Security)
  8946. #===============================================================================
  8947. audit_log() {
  8948. # Log security-relevant events for enterprise compliance
  8949. local event_type="$1"
  8950. local event_data="$2"
  8951. local audit_file=".loki/logs/audit-$(date +%Y%m%d).jsonl"
  8952. if [ "$AUDIT_LOG_ENABLED" != "true" ]; then
  8953. return
  8954. fi
  8955. mkdir -p .loki/logs
  8956. local log_entry
  8957. if command -v jq >/dev/null 2>&1; then
  8958. log_entry=$(jq -n --arg ts "$(date -u +%Y-%m-%dT%H:%M:%SZ)" --arg evt "$event_type" --arg data "$event_data" --arg user "$(whoami)" --argjson pid "$$" '{timestamp:$ts,event:$evt,data:$data,user:$user,pid:$pid}')
  8959. else
  8960. local safe_type safe_data
  8961. safe_type=$(printf '%s' "$event_type" | sed 's/["\\]/\\&/g; s/\n/\\n/g')
  8962. safe_data=$(printf '%s' "$event_data" | sed 's/["\\]/\\&/g; s/\n/\\n/g')
  8963. log_entry="{\"timestamp\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"event\":\"$safe_type\",\"data\":\"$safe_data\",\"user\":\"$(whoami)\",\"pid\":$$}"
  8964. fi
  8965. echo "$log_entry" >> "$audit_file"
  8966. }
  8967. #===============================================================================
  8968. # Engine-owned workspace git-init (Plan #16, Option A-1)
  8969. #===============================================================================
  8970. # Resolve a path to its physical absolute form (symlinks + .. collapsed) using
  8971. # whatever is available; falls back to the input unchanged. Portable across the
  8972. # BSD (macOS) and GNU userlands the engine runs on.
  8973. _loki_resolve_path() {
  8974. local p="${1:-}"
  8975. [ -n "$p" ] || { printf '%s' ""; return 0; }
  8976. if command -v realpath >/dev/null 2>&1; then
  8977. realpath "$p" 2>/dev/null && return 0
  8978. fi
  8979. # python3 is a hard engine dependency; use it as the portable fallback.
  8980. python3 - "$p" <<'PYRESOLVE' 2>/dev/null && return 0
  8981. import os, sys
  8982. print(os.path.realpath(sys.argv[1]))
  8983. PYRESOLVE
  8984. printf '%s' "$p"
  8985. }
  8986. # True (0) when TARGET_DIR is an engine-owned, freshly-minted build workspace
  8987. # that Loki may auto-git-init without surprising a user. Two honest signals:
  8988. # 1. LOKI_AUTO_GIT_INIT=1 -- explicit opt-in (non-SaaS automation).
  8989. # 2. LOKI_TARGET_DIR is set AND realpath-contained under one of the
  8990. # colon-separated LOKI_WORKSPACE_ROOTS dirs (the v7.91.0 SaaS route: the
  8991. # BFF mints <root>/<buildId> and the server pins LOKI_TARGET_DIR to it).
  8992. # A user's own folder (`loki start ./prd.md`, no workspace, roots unset) is
  8993. # NEVER engine-owned -- it must not get a silent .git. Realpath containment (not
  8994. # a prefix string match) so /root/build-other does not match /root/build.
  8995. _loki_workspace_is_engine_owned() {
  8996. [ "${LOKI_AUTO_GIT_INIT:-0}" = "1" ] && return 0
  8997. local roots_raw="${LOKI_WORKSPACE_ROOTS:-}"
  8998. [ -n "${LOKI_TARGET_DIR:-}" ] || return 1
  8999. [ -n "$roots_raw" ] || return 1
  9000. local ws_real root_real
  9001. ws_real="$(_loki_resolve_path "${TARGET_DIR:-.}")"
  9002. [ -n "$ws_real" ] || return 1
  9003. local IFS=':'
  9004. local root
  9005. for root in $roots_raw; do
  9006. [ -n "$root" ] || continue
  9007. root_real="$(_loki_resolve_path "$root")"
  9008. [ -n "$root_real" ] || continue
  9009. # Exact match or contained: ws == root, or ws starts with root + "/".
  9010. if [ "$ws_real" = "$root_real" ] || case "$ws_real" in "$root_real"/*) true ;; *) false ;; esac; then
  9011. return 0
  9012. fi
  9013. done
  9014. return 1
  9015. }
  9016. # Engine-owned workspaces share their host with the Autonomi control plane.
  9017. # Claude Bash commands in those workspaces must load Loki's trusted PreToolUse
  9018. # guard explicitly, rather than relying on mutable project settings.
  9019. _loki_host_guard_required() {
  9020. case "${LOKI_HOST_GUARD:-0}" in
  9021. 1|true|yes|on) return 0 ;;
  9022. esac
  9023. [ -n "${LOKI_TARGET_DIR:-}" ] || return 1
  9024. [ -n "${LOKI_WORKSPACE_ROOTS:-}" ] || return 1
  9025. LOKI_AUTO_GIT_INIT=0 _loki_workspace_is_engine_owned
  9026. }
  9027. # Host-guard hook timeout, seconds. Mirrors hookTimeoutSeconds in
  9028. # loki-ts/src/runner/providers.ts (same env var, default 30, positive int <= 99999).
  9029. _loki_hook_timeout() {
  9030. case "${LOKI_HOOK_TIMEOUT:-}" in
  9031. [1-9] | [1-9][0-9] | [1-9][0-9][0-9] | [1-9][0-9][0-9][0-9] | [1-9][0-9][0-9][0-9][0-9]) printf '%s\n' "$LOKI_HOOK_TIMEOUT" ;;
  9032. *) printf '%s\n' 30 ;;
  9033. esac
  9034. }
  9035. _loki_prepare_host_guard() {
  9036. _loki_host_guard_required || return 0
  9037. if [ "${PROVIDER_NAME:-claude}" != "claude" ]; then
  9038. log_error "Host command guard is required, but provider '${PROVIDER_NAME:-unknown}' has no enforced PreToolUse hook. Refusing to start."
  9039. return 1
  9040. fi
  9041. if ! command -v python3 >/dev/null 2>&1; then
  9042. log_error "Host command guard requires python3 for deterministic command inspection. Refusing to start."
  9043. return 1
  9044. fi
  9045. local hook_path="${PROJECT_DIR}/autonomy/hooks/validate-bash.sh"
  9046. if [ ! -r "$hook_path" ]; then
  9047. log_error "Host command guard hook is missing or unreadable: $hook_path"
  9048. return 1
  9049. fi
  9050. if ! type loki_claude_flag_supported >/dev/null 2>&1 \
  9051. || ! loki_claude_flag_supported "--settings"; then
  9052. log_error "Host command guard requires a Claude CLI with --settings support. Refusing to start."
  9053. return 1
  9054. fi
  9055. local settings_json
  9056. settings_json=$(_LOKI_HOST_GUARD_HOOK="$hook_path" _LOKI_HOOK_TIMEOUT="$(_loki_hook_timeout)" python3 -c '
  9057. import json, os, shlex
  9058. command = "bash " + shlex.quote(os.environ["_LOKI_HOST_GUARD_HOOK"])
  9059. print(json.dumps({"hooks": {"PreToolUse": [{
  9060. "matcher": "Bash",
  9061. "hooks": [{"type": "command", "command": command, "timeout": int(os.environ["_LOKI_HOOK_TIMEOUT"])}],
  9062. }]}}))
  9063. ' 2>/dev/null) || settings_json=""
  9064. if [ -z "$settings_json" ]; then
  9065. log_error "Host command guard settings could not be generated. Refusing to start."
  9066. return 1
  9067. fi
  9068. export LOKI_HOST_GUARD=1
  9069. export LOKI_HOST_GUARD_SETTINGS_JSON="$settings_json"
  9070. return 0
  9071. }
  9072. # Plan #16 Option A-1: establish git in an engine-owned build workspace so the
  9073. # review/verify gate (run_code_review) and branch-isolation chain can actually
  9074. # run. Without git history, run_code_review's diff resolves empty and the gate
  9075. # SKIPS (silently reporting PASS) -- so a build that was never reviewed could
  9076. # earn a VERIFIED receipt. This makes the gate RUN; it does NOT force a green
  9077. # (a build whose review/tests fail still gets an honest verdict).
  9078. #
  9079. # Constraints honored:
  9080. # - Engine-owned workspaces ONLY (see _loki_workspace_is_engine_owned). A
  9081. # user's own folder is never silently git-init'd.
  9082. # - Already a git repo (user's repo OR the engine source tree) -> NO-OP. Only
  9083. # a non-git workspace is initialized.
  9084. # - One INITIAL commit (not a bare init): a zero-commit unborn HEAD breaks the
  9085. # start-SHA capture (git rev-parse HEAD) and re-trips the HEAD~1 skip. The
  9086. # commit uses --allow-empty because a greenfield workspace at build start
  9087. # holds only .loki/ (the spec lands in .loki/specs/), which .loki/.gitignore
  9088. # excludes -> nothing to stage -> a bare commit would fail and leave an
  9089. # unborn HEAD. --allow-empty guarantees a real HEAD either way.
  9090. # - Neutral repo-local identity (loki-build) so the initial commit AND the
  9091. # later commit_session_changes commit never inherit a developer's global git
  9092. # identity. Repo-local sticks on a freshly-init'd workspace (no revert hook).
  9093. # - Secrets never committed: brownfield files are staged through the same
  9094. # secret-scan guard (_commit_path_looks_secret / _commit_scan_secret_file)
  9095. # used by commit_session_changes; any offender unstages the whole set and
  9096. # the initial commit falls back to --allow-empty (HEAD still established).
  9097. maybe_git_init_engine_workspace() {
  9098. local ws="${TARGET_DIR:-.}"
  9099. if ! command -v git >/dev/null 2>&1; then
  9100. loki_is_supervised_simple_web && log_error "Git is required for supervised review baselines"
  9101. loki_is_supervised_simple_web && return 1
  9102. return 0
  9103. fi
  9104. if [ ! -d "$ws" ]; then
  9105. loki_is_supervised_simple_web && return 1
  9106. return 0
  9107. fi
  9108. # Already a git repo (user repo or engine source tree): do nothing.
  9109. if git -C "$ws" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  9110. if loki_is_supervised_simple_web \
  9111. && ! git -C "$ws" rev-parse --verify HEAD >/dev/null 2>&1; then
  9112. log_error "Supervised workspace has no Git baseline commit"
  9113. return 1
  9114. fi
  9115. return 0
  9116. fi
  9117. if ! _loki_workspace_is_engine_owned; then
  9118. loki_is_supervised_simple_web && log_error "Supervised workspace is not eligible for Git baseline initialization"
  9119. loki_is_supervised_simple_web && return 1
  9120. return 0
  9121. fi
  9122. log_info "Engine-owned workspace is not a git repo; initializing for review/verify gates"
  9123. if ! git -C "$ws" init -q >/dev/null 2>&1; then
  9124. if loki_is_supervised_simple_web; then
  9125. log_error "git init failed in supervised engine workspace"
  9126. return 1
  9127. fi
  9128. log_warn "git init failed in engine workspace; review gate will skip (non-fatal)"
  9129. return 0
  9130. fi
  9131. # Neutral repo-local identity for the initial AND session-end commits.
  9132. git -C "$ws" config user.name "loki-build" >/dev/null 2>&1 || true
  9133. git -C "$ws" config user.email "loki-build@autonomi.dev" >/dev/null 2>&1 || true
  9134. # Self-ignore .loki/ runtime state so no commit ever stages it (mirrors
  9135. # setup_agent_branch). Idempotent.
  9136. mkdir -p "$ws/.loki" 2>/dev/null || true
  9137. [ -f "$ws/.loki/.gitignore" ] || printf '*\n' > "$ws/.loki/.gitignore" 2>/dev/null || true
  9138. # Stage everything except .loki/ and an obvious-secret-path denylist (same
  9139. # first-cut excludes commit_session_changes uses). The scan loop below is the
  9140. # real guarantee for nested/weak secrets.
  9141. git -C "$ws" add -A \
  9142. ':!.loki' ':!.loki/' \
  9143. ':!.env' ':!.env.*' ':!*.env' \
  9144. ':!*.key' ':!*.pem' ':!*.p12' ':!*.keystore' \
  9145. ':!id_rsa*' ':!*.token' ':!credentials*' >/dev/null 2>&1 || true
  9146. # Secret-scan staged files. ANY offender -> unstage all (safe default: never
  9147. # commit a possible secret). The --allow-empty commit below still runs so a
  9148. # real HEAD is established regardless.
  9149. local _offenders=""
  9150. local _f _staged_paths
  9151. _staged_paths=$(mktemp "$ws/.loki/.baseline-staged.XXXXXX") || {
  9152. log_error "Initial Git baseline could not allocate a build-local scan file"
  9153. return 1
  9154. }
  9155. if ! git -C "$ws" diff --cached --name-only -z > "$_staged_paths" 2>/dev/null; then
  9156. rm -f "$_staged_paths"
  9157. log_error "Initial Git baseline could not enumerate staged files"
  9158. return 1
  9159. fi
  9160. while IFS= read -r -d '' _f; do
  9161. [ -f "$ws/$_f" ] || continue
  9162. if _commit_path_looks_secret "$ws/$_f" || _commit_scan_secret_file "$ws/$_f"; then
  9163. _offenders="${_offenders}${_offenders:+, }${_f}"
  9164. fi
  9165. done < "$_staged_paths"
  9166. rm -f "$_staged_paths"
  9167. if [ -n "$_offenders" ]; then
  9168. git -C "$ws" reset >/dev/null 2>&1 || true
  9169. log_warn "Initial commit: possible secret in ${_offenders}; left unstaged (baseline commit will be empty)"
  9170. fi
  9171. # ONE initial commit. --allow-empty: a greenfield workspace stages nothing
  9172. # (only .loki/, which is ignored), so a bare commit would fail and leave an
  9173. # unborn HEAD -- the exact failure mode this whole block exists to avoid.
  9174. if git -C "$ws" commit --allow-empty -q -m "loki: initial build workspace baseline" >/dev/null 2>&1; then
  9175. log_info "Initialized git in engine workspace (initial baseline commit created)"
  9176. audit_log "WORKSPACE_GIT_INIT" "workspace=$ws"
  9177. else
  9178. if loki_is_supervised_simple_web; then
  9179. log_error "Initial supervised Git baseline commit failed"
  9180. return 1
  9181. fi
  9182. log_warn "Initial baseline commit failed; review gate may skip (non-fatal)"
  9183. fi
  9184. if loki_is_supervised_simple_web \
  9185. && ! git -C "$ws" rev-parse --verify HEAD >/dev/null 2>&1; then
  9186. log_error "Supervised Git baseline could not be verified"
  9187. return 1
  9188. fi
  9189. return 0
  9190. }
  9191. #===============================================================================
  9192. # Branch Protection for Agent Changes
  9193. #===============================================================================
  9194. setup_agent_branch() {
  9195. # Create an isolated feature branch for agent changes off the branch Loki
  9196. # was run from. This keeps the user's working branch clean and leaves work
  9197. # on a feature branch ready to PR.
  9198. # Controlled by LOKI_BRANCH_PROTECTION env var (default: true). Set it to
  9199. # "false" to opt out fully and work on the current branch (back-compat).
  9200. local branch_protection="${LOKI_BRANCH_PROTECTION:-true}"
  9201. # Set to 1 only once THIS run has a current snapshot (mint or resume
  9202. # union); _loki_record_session_created records nothing otherwise, since a
  9203. # leftover or stale snapshot would make it claim a user's file.
  9204. _LOKI_SNAPSHOT_THIS_RUN=0
  9205. if [ "$branch_protection" != "true" ]; then
  9206. log_info "Branch protection disabled (LOKI_BRANCH_PROTECTION=${branch_protection})"
  9207. return 0
  9208. fi
  9209. # Need git to do anything here.
  9210. command -v git >/dev/null 2>&1 || { log_warn "git not available - skipping branch protection"; return 0; }
  9211. # Ensure we are inside a git repository
  9212. if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  9213. log_warn "Not a git repository - skipping branch protection"
  9214. return 0
  9215. fi
  9216. # Self-ignore .loki/ so NO git add (ours or the user's own `git add -A`)
  9217. # can ever stage runtime state (checkpoints, semantic memory, etc.). This is
  9218. # robust regardless of the repo's own .gitignore and applies brownfield and
  9219. # greenfield. Idempotent: write only when missing. Never fatal.
  9220. mkdir -p .loki 2>/dev/null || true
  9221. [ -f .loki/.gitignore ] || printf '*\n' > .loki/.gitignore 2>/dev/null || true
  9222. # Capture the ref Loki was run from. Detached HEAD yields the literal "HEAD".
  9223. # symbolic-ref also names an unborn branch (fresh `git init`), where
  9224. # rev-parse printed "HEAD" and failed, recording "HEAD" twice as the base.
  9225. local cur=""
  9226. cur="$(git symbolic-ref --short -q HEAD 2>/dev/null || echo HEAD)"
  9227. # Detached HEAD: do NOT branch, do NOT fabricate a base (LOCK A2/A6).
  9228. if [ "$cur" = "HEAD" ]; then
  9229. log_info "Detached HEAD; staying on current commit, no feature branch created"
  9230. return 0
  9231. fi
  9232. # Already on a loki branch (session-* or delegate-*): idempotent reuse, do
  9233. # not nest a branch off a loki branch (LOCK A5/A7).
  9234. # Both reuse paths add what is untracked now to the pre-existing list (a
  9235. # file the user made between sessions is theirs), except the files an
  9236. # unfinished previous session created (_loki_resume_snapshot).
  9237. case "$cur" in
  9238. loki/*)
  9239. log_info "Already on loki branch ${cur}"
  9240. mkdir -p .loki/state 2>/dev/null || true
  9241. printf '%s\n' "$cur" > .loki/state/agent-branch.txt 2>/dev/null || true
  9242. _loki_resume_snapshot
  9243. return 0
  9244. ;;
  9245. esac
  9246. # Resume reuse: if a prior session recorded a checkout-able branch, reuse it
  9247. # instead of minting a new one (LOCK A5).
  9248. local recorded=""
  9249. if [ -s .loki/state/agent-branch.txt ]; then
  9250. recorded="$(cat .loki/state/agent-branch.txt 2>/dev/null || true)"
  9251. if [ -n "$recorded" ] && git rev-parse --verify "$recorded" >/dev/null 2>&1; then
  9252. # --no-overwrite-ignore: git's default checkout silently replaces a
  9253. # gitignored user file at a path the session branch tracks, and the
  9254. # file is gone when the user switches back. On conflict the resume
  9255. # is refused and a fresh session branch (with a fresh snapshot) is
  9256. # minted below.
  9257. local resume_err="" resume_files=""
  9258. if resume_err="$(git checkout --no-overwrite-ignore "$recorded" 2>&1 >/dev/null)"; then
  9259. log_info "Resuming on recorded agent branch: ${recorded}"
  9260. _loki_resume_snapshot
  9261. return 0
  9262. fi
  9263. # git lists the blocking paths as tab-indented lines.
  9264. resume_files="$(printf '%s\n' "$resume_err" | sed -n 's/^[[:space:]][[:space:]]*//p' | head -5 | paste -sd, - | sed 's/,/, /g')"
  9265. if [ -n "$resume_files" ]; then
  9266. log_warn "Recorded agent branch ${recorded} could not be checked out because it would overwrite your ignored file(s): ${resume_files} - creating a new one; the earlier commits stay on ${recorded}"
  9267. else
  9268. log_warn "Recorded agent branch ${recorded} could not be checked out - creating a new one; the earlier commits stay on ${recorded}"
  9269. fi
  9270. fi
  9271. fi
  9272. # Fresh run: mint and check out the feature branch, then persist its base
  9273. # (fresh-run-only, LOCK A2).
  9274. local timestamp
  9275. timestamp=$(date +%s)
  9276. local branch_name="loki/session-${timestamp}-$$"
  9277. mkdir -p .loki/state 2>/dev/null || true
  9278. log_info "Branch protection enabled - creating agent branch: $branch_name (base: $cur)"
  9279. # Record the user's own untracked and gitignored files so
  9280. # commit_session_changes and the receipt leave them alone.
  9281. local snap_ok=1 leftover="" gittool=""
  9282. if ! _loki_snapshot_or_fail_closed; then
  9283. snap_ok=0
  9284. rm -f .loki/state/preexisting-untracked.sha.z 2>/dev/null
  9285. # BACKLOG 88: the receipt (workspace_diff._preexisting_untracked) reads
  9286. # preexisting-untracked.z on its own, independent of
  9287. # _LOKI_SNAPSHOT_THIS_RUN below. Leaving it absent after a failed
  9288. # snapshot (old code: `rm -f` here too) makes the receipt treat EVERY
  9289. # currently-untracked file as this run's own work, even though nothing
  9290. # is actually committed -- a pre-existing file the user never touched
  9291. # gets named as the run's. `ls-files --others --exclude-standard` needs
  9292. # no --no-renames or --ignored=matching, so it still works on the git
  9293. # that just failed the richer `status` call above, giving the receipt a
  9294. # names-only fallback list. Ignored files are not covered by this
  9295. # fallback (old-git territory), so it is partial, not a full snapshot;
  9296. # the commit path is untouched (snap_ok stays 0, _LOKI_SNAPSHOT_THIS_RUN
  9297. # is never set below), so this still fails closed.
  9298. gittool="$(_loki_snapshot_git_tool)" && \
  9299. "$gittool" ls-files --others --exclude-standard -z > .loki/state/preexisting-untracked.z.tmp 2>/dev/null && \
  9300. mv .loki/state/preexisting-untracked.z.tmp .loki/state/preexisting-untracked.z \
  9301. || rm -f .loki/state/preexisting-untracked.z .loki/state/preexisting-untracked.z.tmp 2>/dev/null
  9302. log_warn "Could not record pre-existing untracked files; this session will commit nothing (review and commit manually)"
  9303. fi
  9304. # Create and checkout the feature branch
  9305. if ! git checkout -b "$branch_name" 2>/dev/null; then
  9306. log_error "Failed to create agent branch: $branch_name"
  9307. return 1
  9308. fi
  9309. # A new session branch starts with an empty session-created record. Files
  9310. # an unfinished earlier session left (reached here after a refused resume)
  9311. # were just snapshotted as pre-existing: kept on disk, never committed.
  9312. leftover="$(_loki_nul_names .loki/state/session-created.z)"
  9313. if [ -n "$leftover" ]; then
  9314. log_warn "Left uncommitted from an earlier unfinished session, now treated as your files: $leftover"
  9315. fi
  9316. rm -f .loki/state/session-created.z 2>/dev/null
  9317. # BACKLOG 130(c): seal the now-empty session-created.z record so this
  9318. # fresh mint's OWN in-process memory of "absent" is what every later
  9319. # record call and commit_session_changes verify against for the rest of
  9320. # this run -- not the file's on-disk content, which an agent process could
  9321. # otherwise repopulate with a planted path before this run ever trusts it.
  9322. _loki_session_created_seal # BACKLOG-130C-MINT-SEAL
  9323. # BACKLOG 68 rework: a fresh mint starts with no tracked-since anchor and no
  9324. # in-flight marker (never seed the anchor from session-start-sha -- an
  9325. # anchor==start would let a later resume's "anchor..HEAD" diff include this
  9326. # session's OWN commits, pruning them from the next session's union as if
  9327. # the user had tracked them between sessions).
  9328. rm -f .loki/state/tracked-since.sha .loki/state/turn-in-flight 2>/dev/null
  9329. if [ "$snap_ok" = 1 ]; then _LOKI_SNAPSHOT_THIS_RUN=1; fi
  9330. # Every mint (a refused resume included) records its own base and the
  9331. # commit it starts at (the empty tree when unborn); the resume paths above
  9332. # keep both. A base left by an earlier session can name a branch deleted
  9333. # since, and _loki_untrack_agent_committed_user_files then could not find
  9334. # the user files the agent commits.
  9335. printf '%s\n' "$cur" > .loki/state/base-branch.txt 2>/dev/null
  9336. { git rev-parse --verify -q HEAD || git hash-object -t tree /dev/null; } \
  9337. > .loki/state/session-start-sha 2>/dev/null
  9338. # Store the branch name for later use (PR creation, cleanup)
  9339. printf '%s\n' "$branch_name" > .loki/state/agent-branch.txt 2>/dev/null
  9340. log_info "Agent branch created: $branch_name"
  9341. audit_log "BRANCH_PROTECTION" "branch=$branch_name"
  9342. echo "$branch_name"
  9343. }
  9344. # Snapshot, or fail closed. Any snapshot failure (for example git older than
  9345. # 2.18, whose status lacks --no-renames / --ignored=matching) leaves a marker
  9346. # that makes commit_session_changes commit nothing: a missing snapshot must
  9347. # only ever mean an older session, never "sweep everything".
  9348. _loki_snapshot_or_fail_closed() {
  9349. if _loki_snapshot_preexisting "$@"; then
  9350. rm -f .loki/state/preexisting-untracked.failed 2>/dev/null
  9351. return 0
  9352. fi
  9353. mkdir -p .loki/state 2>/dev/null
  9354. : > .loki/state/preexisting-untracked.failed 2>/dev/null
  9355. return 1
  9356. }
  9357. # BACKLOG 70: preexisting-untracked.z and its .sha.z sibling live in
  9358. # .loki/state, which the agent process (and anything it spawns) can write. A
  9359. # co-located adversarial agent can delete .sha.z to blind
  9360. # workspace_diff._preexisting_untracked's mismatch check (silently suppressing
  9361. # every preexisting_modified disclosure), or append/edit .z entries to hide its
  9362. # own new files from the receipt or falsely disown them. Full prevention against
  9363. # a same-UID process is not achievable (it can read and rewrite anything this
  9364. # process can); the realistic bar, and the one BACKLOG 70 asks for, is
  9365. # DISCLOSURE: this process is the only one that can remember what it itself
  9366. # just wrote, so it seals a sha256 of both files in its own memory right after
  9367. # writing them, and re-derives + compares that digest at every later read site.
  9368. # A mismatch (or the sealed file now missing) is reported as tampering and
  9369. # treated as a failed snapshot (fail closed: no session commit), never patched
  9370. # over silently. Cleared whenever the files are deliberately removed (line
  9371. # ~9165) so that path is never reported as tampering.
  9372. _LOKI_SNAPSHOT_SEAL=""
  9373. # BACKLOG 130(c): .loki/state/session-created.z is NOT one of the two files
  9374. # _LOKI_SNAPSHOT_SEAL above covers. It records the paths THIS session created
  9375. # (unioned after every provider turn by _loki_record_session_created), and
  9376. # _loki_snapshot_preexisting's resume union (line ~9683) trusts its content
  9377. # directly: any path it lists is EXCLUDED from the new preexisting-untracked.z
  9378. # snapshot, i.e. treated as "the session's own, never the user's". An agent
  9379. # that plants a user's file path into session-created.z therefore gets that
  9380. # exact path permanently exempted from pre-existing-file protection on the
  9381. # next resume -- the file is swept into `git add -A` and the session commit as
  9382. # if the session had created it. A separate seal, sibling to
  9383. # _LOKI_SNAPSHOT_SEAL, closes this WITHIN one process's lifetime: sealed right
  9384. # after every legitimate write this process makes (a successful record, the
  9385. # fresh-mint delete, and this process's first look at the file on resume),
  9386. # verified before any LATER point in the SAME process trusts the content,
  9387. # fail-closed (delete the file, clear the seal) on a mismatch. Kept SEPARATE
  9388. # from _LOKI_SNAPSHOT_SEAL rather than folded into one combined digest: this
  9389. # file legitimately changes every provider turn, so a shared digest would
  9390. # falsely flag tampering on every normal turn, and the fresh-mint `rm` (line
  9391. # ~9241) runs after preexisting-untracked.z is sealed, which would falsely
  9392. # flag every fresh mint too.
  9393. # ACCEPTED GAP (disclosure, not prevention, same bar as BACKLOG 70's own): a
  9394. # seal lives only in this process's memory, so it cannot verify content a
  9395. # PRIOR process wrote before this one starts (setup_agent_branch's resume path
  9396. # seals its own first read of the file rather than verifying it, since no
  9397. # prior seal survives to check against) -- tampering already on disk before
  9398. # this process begins is trusted at that one read, same as
  9399. # _LOKI_SNAPSHOT_SEAL's own resume-time trust of preexisting-untracked.z's
  9400. # prior content. What this DOES close: an agent process co-located with THIS
  9401. # run can no longer plant a path into session-created.z between this run's own
  9402. # writes and its own later reads (the resume union's exclude use, the next
  9403. # record's merge, or the commit-time gate) without it being caught before that
  9404. # content is trusted.
  9405. _LOKI_SESSION_CREATED_SEAL=""
  9406. # BACKLOG 129 (follow-up to BACKLOG 70/BACKLOG 70-awk): the DIGEST computation
  9407. # above is hardened (fixed-root-path-first, no awk), but until this function
  9408. # was added the snapshot WRITER (_loki_untracked_merge) and READER
  9409. # (_loki_covered_paths), plus the hash-snapshot helper that produces .sha.z,
  9410. # still ran bare `python3 -E`. -E only blocks PYTHON* env vars; it does NOT
  9411. # stop user (or Homebrew) site-packages from loading, and Python execs every
  9412. # line starting "import " in every .pth file found there on every startup
  9413. # (confirmed empirically: a .pth planted in user site-packages fired and
  9414. # silently rewrote the writer's declared output file after the legitimate
  9415. # write completed). A same-UID agent that plants such a .pth can forge the
  9416. # CONTENT of the snapshot BEFORE the (correctly hardened) seal ever computes a
  9417. # digest over it -- the seal then faithfully approves the forgery. Resolved
  9418. # the same way as _loki_snapshot_hash_tool: fixed root-owned candidate paths
  9419. # first (a materially higher bar than a user-writable site-packages
  9420. # directory), each PROBED with a real run (not just `-x`, which a Command Line
  9421. # Tools-less Mac's /usr/bin shims would still pass while failing to execute
  9422. # and possibly popping an install dialog), then a PATH walk restricted to
  9423. # absolute entries as a last resort. -I -S (isolated mode, and -S also skips
  9424. # `import site` entirely) is used at every call site below: -I alone still
  9425. # loads site-packages (verified: `python3 -I` still lists Homebrew's
  9426. # site-packages on this host), so -S is required to close the .pth vector,
  9427. # not just narrow it. Accepted gap, same shape as _loki_snapshot_hash_tool's:
  9428. # replacing the interpreter binary AT ONE OF THE FIXED PATHS this resolves to
  9429. # first requires root; a host with none of the fixed candidates falls through
  9430. # to the PATH-walk, which still trusts whatever sits first on an absolute PATH
  9431. # entry under that name. A further, host-specific gap: on macOS these fixed
  9432. # paths are typically Command Line Tools / Xcode shims that dispatch into
  9433. # /Applications/Xcode.app -- root-owned on a normal install, but the fixed
  9434. # path is only as trustworthy as that dispatch target on any host where it is
  9435. # not.
  9436. _loki_snapshot_py_tool() {
  9437. local c
  9438. for c in /usr/bin/python3 /bin/python3; do
  9439. [ -x "$c" ] && [ ! -d "$c" ] && "$c" -I -S -c '' >/dev/null 2>&1 && { printf '%s\n' "$c"; return 0; }
  9440. done
  9441. local dir
  9442. local IFS=:
  9443. for dir in $PATH; do
  9444. case "$dir" in
  9445. /*) ;;
  9446. *) continue ;;
  9447. esac
  9448. if [ -x "$dir/python3" ] && [ ! -d "$dir/python3" ] \
  9449. && "$dir/python3" -I -S -c '' >/dev/null 2>&1; then
  9450. printf '%s\n' "$dir/python3"
  9451. return 0
  9452. fi
  9453. done
  9454. return 1
  9455. }
  9456. # BACKLOG 129: same rationale and pattern as _loki_snapshot_py_tool above, for
  9457. # `git`. Every call in this snapshot writer/reader chain
  9458. # (_loki_untracked_status, _loki_tracked_by_user_since_anchor,
  9459. # _loki_snapshot_preexisting's own `git rev-parse --show-toplevel`) used a bare
  9460. # `git` -- an unscoped PATH lookup a same-UID attacker can shadow with a fake
  9461. # binary placed in a user-writable directory ahead of the real one, exactly
  9462. # the class of bypass _loki_snapshot_hash_tool's own comment already documents
  9463. # for sha256sum/shasum. Reproduced empirically: a fake `git` ahead on PATH
  9464. # that filters one path out of `git status`'s output caused that path to be
  9465. # completely absent from the sealed snapshot. `_loki_untrack_agent_committed_
  9466. # user_files`'s git calls (feeding the READER path, _loki_covered_paths) and
  9467. # the commit-time `git reset`/`git commit`/`git update-index` calls in
  9468. # commit_session_changes are DELIBERATELY left unresolved here (out of scope
  9469. # for this fix): a fake git on PATH already defeats the commit gate directly
  9470. # at commit time regardless of what the snapshot says, so resolving only the
  9471. # reader's git feed would not change that attacker's outcome. Tracked as a
  9472. # follow-up (process-wide resolved git) rather than folded into this change.
  9473. _loki_snapshot_git_tool() {
  9474. local c
  9475. for c in /usr/bin/git /bin/git; do
  9476. [ -x "$c" ] && [ ! -d "$c" ] && "$c" --version >/dev/null 2>&1 && { printf '%s\n' "$c"; return 0; }
  9477. done
  9478. local dir
  9479. local IFS=:
  9480. for dir in $PATH; do
  9481. case "$dir" in
  9482. /*) ;;
  9483. *) continue ;;
  9484. esac
  9485. if [ -x "$dir/git" ] && [ ! -d "$dir/git" ] && "$dir/git" --version >/dev/null 2>&1; then
  9486. printf '%s\n' "$dir/git"
  9487. return 0
  9488. fi
  9489. done
  9490. return 1
  9491. }
  9492. # _loki_snapshot_hash_tool: resolve an absolute path to sha256sum or shasum.
  9493. # Fixed, root-owned candidate paths are tried FIRST, before any PATH walk: a
  9494. # same-UID agent that can write to a user-writable directory ahead of the real
  9495. # tool on PATH (Homebrew's prefix, e.g. /opt/homebrew/bin, is user-writable by
  9496. # default on macOS and typically precedes /usr/bin) could otherwise drop its
  9497. # own sha256sum/shasum there that reads the two snapshot files and echoes back
  9498. # whatever hash it wants -- the same class of bypass as the .pth hijack this
  9499. # whole function exists to close, just with a fake binary as the carrier
  9500. # instead of a fake module. Trying the standard system locations first closes
  9501. # that: they require root to write to, a materially higher bar than a
  9502. # user-writable site-packages directory or a user-writable PATH entry.
  9503. # A PATH walk (D7: never trust a bare command name -- restricted to absolute
  9504. # entries, matching autonomy/loki's loki_verify_tool_path) is kept only as a
  9505. # last resort for a host that keeps sha256sum/shasum somewhere nonstandard;
  9506. # document there that this fallback still trusts whatever is first on an
  9507. # absolute PATH entry, which a user-writable directory ahead of the system
  9508. # dirs can still shadow. Prints nothing (caller then falls back) when no tool
  9509. # is found anywhere.
  9510. _loki_snapshot_hash_tool() {
  9511. local c
  9512. for c in /usr/bin/sha256sum /bin/sha256sum /sbin/sha256sum \
  9513. /usr/bin/shasum /bin/shasum; do
  9514. [ -x "$c" ] && [ ! -d "$c" ] && { printf '%s\n' "$c"; return 0; }
  9515. done
  9516. # Last resort: PATH walk, absolute entries only. Still trusts whatever
  9517. # sits first on PATH under that name; see comment above.
  9518. local name dir
  9519. local IFS=:
  9520. for name in sha256sum shasum; do
  9521. for dir in $PATH; do
  9522. case "$dir" in
  9523. /*) ;;
  9524. *) continue ;;
  9525. esac
  9526. if [ -x "$dir/$name" ] && [ ! -d "$dir/$name" ]; then
  9527. printf '%s\n' "$dir/$name"
  9528. return 0
  9529. fi
  9530. done
  9531. done
  9532. return 1
  9533. }
  9534. # _loki_snapshot_digest: sha256 of preexisting-untracked.z + .sha.z content
  9535. # (each independently, "-" standing in for "file absent" so a delete is part
  9536. # of the digest, not indistinguishable from an empty file; "?" standing in for
  9537. # "file present but the hash tool failed to produce a real digest" -- a
  9538. # nonzero exit, empty output, or output that fails validation below -- so a
  9539. # transient read error never seals or compares as an empty string).
  9540. #
  9541. # HIGH-severity reviewer finding on the original python3 -E version: -E only
  9542. # ignores PYTHON* env vars, it does NOT stop Python from loading user (or, on
  9543. # macOS with Homebrew, system) site-packages, and Python executes every .pth
  9544. # file found there on every startup. A same-UID agent process can plant a .pth
  9545. # file there that hijacks this script's own print() to emit a cached, stale
  9546. # (pre-tamper) digest -- fully defeating the seal while `git`/file state show
  9547. # real tampering happened. Fixed by computing the digest with sha256sum/shasum
  9548. # instead: neither has a .pth-style module-loading path for that hijack to
  9549. # attach to. shasum is a Perl script on macOS (not a compiled binary), but its
  9550. # @INC search path is root-owned system Perl library directories, not a user
  9551. # site-packages directory, so the .pth carrier does not transfer to it.
  9552. #
  9553. # SECOND reviewer finding, on the shasum/sha256sum version that first closed
  9554. # the .pth hole: the digest was piped through a bare `awk '{print $1}')`
  9555. # resolved via an unscoped PATH lookup (`awk '{...}'` runs whatever `awk`
  9556. # comes first on PATH). A fake `awk` placed in a user-writable directory ahead
  9557. # of the real one (the same Homebrew-prefix-precedes-/usr/bin shape as the
  9558. # hash-tool bypass above) can intercept the pipe and print a cached, stale
  9559. # digest, defeating the seal exactly as before -- the fix reintroduced the
  9560. # same bypass class it was written to close, just moved one command over.
  9561. # Fixed by dropping awk entirely: the first whitespace-delimited field is
  9562. # taken with the shell builtin `${h%% *}` (parameter expansion, no external
  9563. # process, nothing on PATH to hijack), and the result is validated as exactly
  9564. # 64 lowercase hex characters before being trusted as a real sha256 digest --
  9565. # anything else (empty, wrong length, non-hex, a hand-rolled fake tool that
  9566. # does not bother to format like sha256sum/shasum) becomes "?". The hash
  9567. # tool's own exit status is also checked explicitly now: a piped
  9568. # `cmd | awk ...` hid a nonzero exit from the hash tool behind awk's exit
  9569. # status; capturing to a plain variable surfaces it.
  9570. #
  9571. # Resolved via _loki_snapshot_hash_tool above (fixed root-owned paths first,
  9572. # PATH walk restricted to absolute entries only as a last resort), not a bare
  9573. # `command -v`, so this also closes the sibling D7-pattern gap: an unscoped
  9574. # lookup could otherwise resolve to a same-named binary planted in a
  9575. # user-writable directory ahead of the real tool on PATH.
  9576. #
  9577. # Accepted gaps (disclosure, not prevention, per BACKLOG 70's own bar, same as
  9578. # documented at _LOKI_SNAPSHOT_SEAL above): a same-UID ptrace attack on this
  9579. # process or on the sha256sum/shasum child process itself; replacing the
  9580. # sha256sum/shasum binary AT ONE OF THE FIXED SYSTEM PATHS this resolves to
  9581. # first (requires root, a materially higher bar than a user-writable
  9582. # site-packages .pth or a user-writable PATH entry); on a host with none of
  9583. # the fixed candidates, the PATH-walk fallback inside _loki_snapshot_hash_tool
  9584. # still trusts whatever sits first on an absolute PATH entry under that name,
  9585. # which a user-writable directory ahead of the system dirs (e.g. Homebrew's
  9586. # prefix on macOS) can shadow -- not fully closed, only narrowed to hosts
  9587. # lacking the standard tool paths; the python3 fallback below, kept only for
  9588. # the extremely rare host with neither tool anywhere, is resolved via
  9589. # _loki_snapshot_py_tool and run with -I -S (BACKLOG 131(b) -- see the comment
  9590. # at that fallback for why -S, not just -I, is required to close the .pth
  9591. # class), so it carries the same fixed-root-path-first resolution and
  9592. # site-packages exclusion as the other hardened call sites in this file. It
  9593. # still inherits _loki_snapshot_py_tool's own accepted gaps (documented at
  9594. # that function): replacing the interpreter AT ONE OF ITS FIXED PATHS requires
  9595. # root, and its own PATH-walk fallback still trusts whatever sits first on an
  9596. # absolute PATH entry on a host lacking those fixed paths.
  9597. _loki_snapshot_digest() {
  9598. local tool
  9599. tool="$(_loki_snapshot_hash_tool)" || tool=""
  9600. if [ -n "$tool" ]; then
  9601. local out="" path h
  9602. for path in ".loki/state/preexisting-untracked.z" ".loki/state/preexisting-untracked.sha.z"; do
  9603. if [ -f "$path" ]; then
  9604. h=""
  9605. case "${tool##*/}" in
  9606. shasum) h="$("$tool" -a 256 -- "$path" 2>/dev/null)" || h="" ;;
  9607. *) h="$("$tool" -- "$path" 2>/dev/null)" || h="" ;;
  9608. esac
  9609. # No awk: take the first whitespace-delimited field with a
  9610. # shell builtin (nothing on PATH to hijack), then require
  9611. # exactly 64 lowercase hex characters. Anything else -- empty
  9612. # output, a nonzero exit already caught above, an unexpected
  9613. # format, a hand-rolled fake tool -- becomes "?" rather than
  9614. # being trusted as a real digest. "?" is distinct from "-"
  9615. # (file absent) and from any real hex digest, so a later
  9616. # successful read of the same file mismatches it and is still
  9617. # disclosed as tampering.
  9618. h="${h%% *}"
  9619. if [ "${#h}" -eq 64 ]; then
  9620. case "$h" in
  9621. *[!0123456789abcdef]*) h="?" ;;
  9622. esac
  9623. else
  9624. h="?"
  9625. fi
  9626. out="$out $h"
  9627. else
  9628. out="$out -"
  9629. fi
  9630. done
  9631. printf '%s\n' "${out# }"
  9632. return 0
  9633. fi
  9634. # Fallback: neither sha256sum nor shasum found anywhere checked above
  9635. # (extremely rare on macOS/Linux). Resolved via _loki_snapshot_py_tool
  9636. # (fixed root-owned path first) and run with -I -S, the same BACKLOG 129
  9637. # pattern used at _loki_untracked_merge / _loki_covered_paths: -I alone is
  9638. # NOT enough -- a reviewer reproduced, on a sibling function in this same
  9639. # tamper-detection system, that a .pth file planted in Homebrew python3's
  9640. # own (user-writable) site-packages directory still fires under `python3
  9641. # -I` alone, since -I implies -s (skip user site-packages) but does not
  9642. # skip the resolved interpreter's OWN site-packages. Only -S (skip ALL
  9643. # site-packages, including the interpreter's own) closes that. A resolve
  9644. # failure degrades this fallback to printing nothing, same as any other
  9645. # failure path here (caller treats empty as "could not seal"). Additional
  9646. # accepted gap specific to this fallback (not present on the tool-based
  9647. # path above, which distinguishes "-" from "?"): OSError covers BOTH
  9648. # "file does not exist" and "file exists but is unreadable" here, so an
  9649. # unreadable file on this path is indistinguishable from an absent one --
  9650. # both print "-". The tool-based path above tells them apart ("-" for
  9651. # absent, "?" for present-but-unreadable, caught by the seal-time
  9652. # fail-closed check). Narrowing this further (os.path.lexists to emit "?"
  9653. # for present-but-unreadable) is straightforward but left undone here: this
  9654. # fallback only runs on a host with neither sha256sum nor shasum anywhere,
  9655. # already the rare case this whole function treats as a residual gap.
  9656. local pytool=""
  9657. pytool="$(_loki_snapshot_py_tool)" || return 1
  9658. "$pytool" -I -S -c 'import sys, hashlib
  9659. sys.path[:] = [p for p in sys.path if p not in ("", ".")]
  9660. out = []
  9661. for path in sys.argv[1:]:
  9662. try:
  9663. with open(path, "rb") as fh:
  9664. out.append(hashlib.sha256(fh.read()).hexdigest())
  9665. except OSError:
  9666. out.append("-")
  9667. print(" ".join(out))' \
  9668. ".loki/state/preexisting-untracked.z" ".loki/state/preexisting-untracked.sha.z" 2>/dev/null
  9669. }
  9670. # _loki_session_created_digest: sha256 of session-created.z alone ("-" for
  9671. # absent, "?" for present-but-unreadable/unhashable, same sentinel contract as
  9672. # _loki_snapshot_digest above). Deliberately a thin sibling, not a parameterized
  9673. # reuse of _loki_snapshot_digest: that function's exact text (the `for path in
  9674. # ...` line and the python fallback's trailing argv line) is matched by
  9675. # tests/test-branch-lifecycle.sh's RED mutation harness, so leaving it
  9676. # untouched keeps that harness's anchors valid. Resolved via the SAME hardened
  9677. # tool resolvers (_loki_snapshot_hash_tool, _loki_snapshot_py_tool) as the
  9678. # sibling above -- no fresh/unhardened python3 or shasum call site.
  9679. _loki_session_created_digest() {
  9680. local tool
  9681. tool="$(_loki_snapshot_hash_tool)" || tool=""
  9682. local path=".loki/state/session-created.z"
  9683. # A FIFO or ANY symlink at $path (dangling or not -- [ -L ] rejects both,
  9684. # deliberately broader than just the dangling case: the only way this
  9685. # file is ever legitimately written is _loki_untracked_merge's own
  9686. # write-to-.tmp-then-`mv -f`, which always leaves a plain regular file at
  9687. # $path, never a symlink, so a symlink here is never this session's own
  9688. # legitimate output) is neither absent ([ -f ] is false for both) nor a
  9689. # hashable regular file -- falling through to the absent-file branch below
  9690. # would print the same "-" sentinel every fresh mint already seals as its
  9691. # normal starting state, silently trusting an agent-planted FIFO (whose
  9692. # blocked writer feeds content into the merge's later open() call) or a
  9693. # dangling symlink (whose target read fails and is treated as "-" too).
  9694. # Caught here, before either digest branch, with a sentinel ("?") that can
  9695. # never equal "-", a real hex digest, or empty.
  9696. if [ -L "$path" ] || { [ -e "$path" ] && [ ! -f "$path" ]; }; then printf '%s\n' "?"; return 0; fi # BACKLOG-130C-NONREG-CHECK
  9697. if [ -n "$tool" ]; then
  9698. local h=""
  9699. if [ -f "$path" ]; then
  9700. case "${tool##*/}" in
  9701. shasum) h="$("$tool" -a 256 -- "$path" 2>/dev/null)" || h="" ;;
  9702. *) h="$("$tool" -- "$path" 2>/dev/null)" || h="" ;;
  9703. esac
  9704. h="${h%% *}"
  9705. if [ "${#h}" -eq 64 ]; then
  9706. case "$h" in
  9707. *[!0123456789abcdef]*) h="?" ;;
  9708. esac
  9709. else
  9710. h="?"
  9711. fi
  9712. printf '%s\n' "$h"
  9713. else
  9714. printf '%s\n' "-"
  9715. fi
  9716. return 0
  9717. fi
  9718. # Fallback: neither sha256sum nor shasum found anywhere (same rare-host
  9719. # case _loki_snapshot_digest's own fallback documents). Resolved via
  9720. # _loki_snapshot_py_tool, run with -I -S (see that function's comment for
  9721. # why -S, not just -I, is required to close the .pth vector).
  9722. local pytool=""
  9723. pytool="$(_loki_snapshot_py_tool)" || return 1
  9724. "$pytool" -I -S -c 'import sys, hashlib
  9725. sys.path[:] = [p for p in sys.path if p not in ("", ".")]
  9726. try:
  9727. with open(sys.argv[1], "rb") as fh:
  9728. print(hashlib.sha256(fh.read()).hexdigest())
  9729. except OSError:
  9730. print("-")' \
  9731. "$path" 2>/dev/null
  9732. }
  9733. # _loki_session_created_seal: record this process's own memory of the current
  9734. # session-created.z content, right after every point that legitimately changes
  9735. # it (a successful union in _loki_record_session_created, the fresh-mint
  9736. # delete, a resume, and the post-commit/no-op delete in
  9737. # commit_session_changes). Same fail-closed shape as _loki_snapshot_seal: an
  9738. # empty or "?" digest at seal time is itself suspicious (a legitimate write or
  9739. # delete should never immediately be unreadable), so it is rejected rather
  9740. # than sealed, which would let seal and a later verify silently agree on the
  9741. # sentinel and disarm the guard for the rest of the session.
  9742. _loki_session_created_seal() {
  9743. local d
  9744. d="$(_loki_session_created_digest)"
  9745. case "$d" in
  9746. ""|*'?'*)
  9747. # Fail closed, not just "untrusted": an empty seal makes
  9748. # _loki_session_created_verify's `[ -n "$_LOKI_SESSION_CREATED_SEAL" ]
  9749. # || return 0` skip every later check for the rest of this
  9750. # process's life. Leaving the file on disk here would fail OPEN --
  9751. # unreadable-but-present content would then be silently trusted by
  9752. # every later read site with no seal ever able to catch it. Delete
  9753. # it instead, matching _loki_session_created_verify's own
  9754. # fail-closed action on a real mismatch -- and, like that function's
  9755. # own re-seal after its delete, set the seal to the digest's real
  9756. # "absent" sentinel ("-"), not empty: an EMPTY seal here re-arms
  9757. # nothing (the guard above short-circuits as "never sealed" on
  9758. # every later check for the rest of this process), so an agent that
  9759. # recreates the file with a planted path in this window would get
  9760. # it silently merged in as trusted on the next successful record.
  9761. # "-" is a real, non-empty seal value that the next verify's live
  9762. # digest of a freshly-recreated file cannot match, keeping the
  9763. # guard armed against exactly that recreation.
  9764. rm -f .loki/state/session-created.z 2>/dev/null
  9765. _LOKI_SESSION_CREATED_SEAL="-"
  9766. log_warn "Could not seal the session-created record (hash tool failed or produced no readable digest); discarding it (its files are treated as yours, left uncommitted)"
  9767. audit_log "SESSION_CREATED_SEAL_FAILED" "_loki_session_created_digest returned empty or an unreadable-file sentinel at seal time"
  9768. ;;
  9769. *)
  9770. _LOKI_SESSION_CREATED_SEAL="$d"
  9771. ;;
  9772. esac
  9773. }
  9774. # _loki_session_created_verify: compare the live session-created.z digest
  9775. # against the sealed one. No seal recorded (never sealed this process, or
  9776. # deliberately cleared) is not tampering: returns success, does nothing --
  9777. # EVERY caller that trusts session-created.z's content must therefore have
  9778. # sealed it first (mint, resume, and after each successful record all do). A
  9779. # seal that no longer matches IS tampering: logs and discloses it (same
  9780. # DISCLOSURE bar as BACKLOG 70, not prevention against a same-UID process), and
  9781. # fails closed by deleting the untrusted file outright -- its entries then
  9782. # become "the user's, not recorded as this session's", the safe direction,
  9783. # since a false claim of user ownership only leaves a file uncommitted on disk,
  9784. # never deletes it, while a false claim of session ownership can get a user's
  9785. # file swept into a commit and removed from disk on a later checkout of the
  9786. # base. Called BEFORE any later point in the SAME process treats the file's
  9787. # content as trustworthy (verify-before-trust, not verify-after-use):
  9788. # _loki_record_session_created verifies at its very top, before its own
  9789. # self-referential merge (base="$out"), and unconditionally -- not gated on
  9790. # _LOKI_SNAPSHOT_THIS_RUN, since an interrupt/cleanup call can still fire after
  9791. # that flag was cleared mid-session; commit_session_changes verifies at its
  9792. # very top, before every one of its several early returns (the untrack
  9793. # failure, the git 2.25 unstage failure, a secret-scan abort, a failed `git
  9794. # commit`) that would otherwise skip its own rm -f cleanup and leave a
  9795. # tampered file on disk for the next resume. _loki_resume_snapshot does NOT
  9796. # call this function: it is this process's FIRST read of a file the PREVIOUS
  9797. # (dead) process wrote, so there is no seal from that process to verify
  9798. # against here; it instead calls _loki_session_created_seal directly to
  9799. # establish THIS process's own baseline before anything (including
  9800. # _loki_snapshot_preexisting's resume-union exclude use, right after) reads
  9801. # the file. See the ACCEPTED GAP note at _LOKI_SESSION_CREATED_SEAL above.
  9802. _loki_session_created_verify() {
  9803. local live="" path=".loki/state/session-created.z"
  9804. [ -n "$_LOKI_SESSION_CREATED_SEAL" ] || return 0
  9805. live="$(_loki_session_created_digest)"
  9806. if [ "$live" = "$_LOKI_SESSION_CREATED_SEAL" ]; then
  9807. return 0
  9808. fi
  9809. log_warn "session-created.z changed after this session recorded it (possible tampering by an agent process); discarding the record (its files are treated as yours, left uncommitted)"
  9810. audit_log "SESSION_CREATED_TAMPERED" "session-created.z changed after being sealed: live=$live,sealed=$_LOKI_SESSION_CREATED_SEAL"
  9811. rm -f "$path" 2>/dev/null
  9812. _LOKI_SESSION_CREATED_SEAL=""
  9813. _loki_session_created_seal
  9814. return 1
  9815. }
  9816. # _loki_snapshot_seal: record this process's own memory of what it just wrote.
  9817. # Called only right after a successful _loki_snapshot_preexisting. Minor
  9818. # reviewer note: a legitimate snapshot write should never hit an unreadable-
  9819. # file condition immediately after writing it, so a "?" in the digest AT SEAL
  9820. # TIME (as opposed to at a later verify) is itself suspicious -- both seal and
  9821. # a later verify could then read "?", compare equal, and silently disarm the
  9822. # guard for the rest of the session (the same failure mode
  9823. # _loki_snapshot_verify's mismatch path exists to catch, just never triggered
  9824. # because both sides agree on the sentinel). Closed cheaply: reject an empty
  9825. # or "?"-containing digest at seal time the same way _loki_snapshot_verify
  9826. # rejects a mismatch -- log and disclose it, write the .failed marker, AND
  9827. # clear _LOKI_SNAPSHOT_THIS_RUN directly here (belt-and-braces: the actual
  9828. # gate that stops a commit). The call site right after `_loki_snapshot_seal`
  9829. # in _loki_snapshot_preexisting also checks `_LOKI_SNAPSHOT_SEAL` and returns
  9830. # non-zero on failure, which is what actually propagates to
  9831. # _loki_snapshot_or_fail_closed / _loki_resume_snapshot and keeps their
  9832. # existing callers from ever setting _LOKI_SNAPSHOT_THIS_RUN=1 in the first
  9833. # place (see the comment there) -- so the in-function clear here is redundant
  9834. # with that, not the sole mechanism, kept only as defense in depth in case a
  9835. # future caller invokes this function directly. The call site above is
  9836. # intentionally left as a bare `_loki_snapshot_seal` (no `|| return`):
  9837. # tests/test-branch-lifecycle.sh's RED harness matches that exact line to
  9838. # strip the guard; the propagation check right after it carries its own
  9839. # trailing-comment marker the RED harness also strips (see the comment at
  9840. # that check, in _loki_snapshot_preexisting, for the exact text -- not
  9841. # repeated here so a grep for it does not also match this sentence), so
  9842. # removing either half of the guard is enough to reproduce the pre-fix
  9843. # silent-degrade behavior in that test.
  9844. _loki_snapshot_seal() {
  9845. local d
  9846. d="$(_loki_snapshot_digest)"
  9847. case "$d" in
  9848. ""|*'?'*)
  9849. _LOKI_SNAPSHOT_SEAL=""
  9850. _LOKI_SNAPSHOT_THIS_RUN=0
  9851. log_warn "Could not seal the pre-existing-file snapshot (hash tool failed or produced no readable digest); treating this session as unsnapshotted (nothing will be committed)"
  9852. audit_log "SNAPSHOT_SEAL_FAILED" "_loki_snapshot_digest returned empty or an unreadable-file sentinel at seal time"
  9853. mkdir -p .loki/state 2>/dev/null
  9854. : > .loki/state/preexisting-untracked.failed 2>/dev/null
  9855. ;;
  9856. *)
  9857. _LOKI_SNAPSHOT_SEAL="$d"
  9858. ;;
  9859. esac
  9860. }
  9861. # _loki_snapshot_verify: compare the live files against the sealed digest.
  9862. # No seal recorded yet (never snapshotted this process, or deliberately
  9863. # cleared) is not tampering: returns success and does nothing. A seal that no
  9864. # longer matches IS tampering: logs it (disclosure is the bar BACKLOG 70 asks
  9865. # for; a same-UID process can rewrite anything else this one could check), and
  9866. # forces every fail-closed path a missing/failed snapshot already takes --
  9867. # clears the seal so a caller cannot re-check a stale value, clears
  9868. # _LOKI_SNAPSHOT_THIS_RUN so commit_session_changes and
  9869. # _loki_record_session_created both treat this as an unsnapshotted session, and
  9870. # (re)writes preexisting-untracked.failed so BACKLOG 90's existing message
  9871. # fires. Every consumer of the snapshot/hash files calls this first and treats
  9872. # non-zero as "no snapshot".
  9873. _loki_snapshot_verify() {
  9874. local live=""
  9875. [ -n "$_LOKI_SNAPSHOT_SEAL" ] || return 0
  9876. live="$(_loki_snapshot_digest)"
  9877. if [ "$live" = "$_LOKI_SNAPSHOT_SEAL" ]; then
  9878. return 0
  9879. fi
  9880. log_warn "Pre-existing-file snapshot or its hash file changed after this session recorded it (possible tampering by an agent process); treating this session as unsnapshotted (nothing will be committed)"
  9881. # Record both digests (before clearing the seal below) so an operator
  9882. # reading the audit log can tell a genuine content tamper apart from a
  9883. # transient hash-tool failure that just produced a different digest.
  9884. audit_log "SNAPSHOT_TAMPERED" "preexisting-untracked.z or .sha.z changed after being sealed: live=$live,sealed=$_LOKI_SNAPSHOT_SEAL"
  9885. _LOKI_SNAPSHOT_SEAL=""
  9886. _LOKI_SNAPSHOT_THIS_RUN=0
  9887. mkdir -p .loki/state 2>/dev/null
  9888. : > .loki/state/preexisting-untracked.failed 2>/dev/null
  9889. return 1
  9890. }
  9891. # _loki_snapshot_preexisting [union]
  9892. # Write .loki/state/preexisting-untracked.z: every path git does not track,
  9893. # untracked and gitignored, repo-wide, NUL-delimited, relative to the repo top.
  9894. # commit_session_changes unstages these and the receipt
  9895. # (autonomy/lib/workspace_diff.py) does not claim them. --ignored=matching lists
  9896. # a directory only when an ignore pattern matches it, so node_modules/ is one
  9897. # "dir/" entry covering its subtree, while logs/ holding only *.log files is
  9898. # listed file by file (a new logs/app.json is still the agent's). "union" keeps
  9899. # the paths already recorded, minus any that became tracked because the USER
  9900. # committed them between sessions (BACKLOG 68 rework). Distinguishing that from
  9901. # "the AGENT itself committed or staged this pre-existing file during THIS
  9902. # session, then got killed" is the entire point of the anchor below: only a
  9903. # path git ADDED strictly after the last point this run itself could reach is
  9904. # ever pruned. A path the agent tracked before that point is left IN the union
  9905. # so it still flows into _loki_untrack_agent_committed_user_files's disclosure
  9906. # (warn, remove from the branch tip, keep on disk) -- never silently swept in.
  9907. #
  9908. # The anchor is .loki/state/tracked-since.sha, a commit SHA written by
  9909. # _loki_record_session_created (after each provider turn) and by
  9910. # commit_session_changes (at session end): the newest point this run itself
  9911. # recorded. .loki/state/turn-in-flight marks a provider turn as currently
  9912. # running (set right before invocation, cleared by the same record call that
  9913. # advances the anchor); its presence means a turn may have committed something
  9914. # since the last anchor write, so pruning is skipped entirely rather than risk
  9915. # treating an agent's mid-turn commit as the user's. Any anchor-resolution
  9916. # failure (missing anchor, anchor not an ancestor of HEAD, merge-base failure)
  9917. # also skips pruning: the safe direction is the old behavior (false blame, file
  9918. # kept on disk), never silent adoption.
  9919. #
  9920. # The sibling .sha.z (content hash per file entry) lets the receipt list a
  9921. # pre-existing file the run changed; it is removed first, so a failed hash step
  9922. # means no detection, never stale hashes. Returns non-zero, leaving any earlier
  9923. # list in place, when git cannot list. Seals a digest of both files on success
  9924. # (BACKLOG 70) so later reads can detect tampering.
  9925. _loki_snapshot_preexisting() {
  9926. local snap=".loki/state/preexisting-untracked.z" top="" base="" exclude="" tracked="" gittool="" pytool=""
  9927. rm -f "${snap%.z}.sha.z" 2>/dev/null
  9928. gittool="$(_loki_snapshot_git_tool)" || return 1
  9929. top="$("$gittool" rev-parse --show-toplevel 2>/dev/null)" || return 1
  9930. mkdir -p .loki/state 2>/dev/null || return 1
  9931. # "union" never adds a path listed in session-created.z: those files were
  9932. # made by a session that did not finish (interrupt, pod loss), so the
  9933. # session that finishes the work commits them. If the user edits one of
  9934. # them between sessions it is still the session's file and is committed
  9935. # with the edit. Recorded entries only block additions; a path this run can
  9936. # prove the USER tracked between sessions is the one case they DO remove
  9937. # (see _loki_tracked_by_user_since_anchor below).
  9938. if [ "${1:-}" = union ]; then
  9939. base="$snap"
  9940. exclude=".loki/state/session-created.z"
  9941. tracked="$snap.tracked"
  9942. if ! _loki_tracked_by_user_since_anchor "$top" "$tracked"; then
  9943. rm -f "$tracked"
  9944. tracked=""
  9945. fi
  9946. fi
  9947. if ! _loki_untracked_status "$snap.status" \
  9948. || ! _loki_untracked_merge "$snap.status" "$base" "$exclude" exact "$snap" "$tracked"; then
  9949. rm -f "$tracked" "$snap.status" "$snap.tmp"
  9950. return 1
  9951. fi
  9952. rm -f "$tracked" "$snap.status"
  9953. # BACKLOG 129: write_snapshot_hashes (the only code path "hash-snapshot"
  9954. # reaches) does pure file I/O (open/os.path/hashlib), no sibling import
  9955. # from autonomy/lib and no subprocess/git call, so -I -S is safe here: it
  9956. # never needs the script's own directory on sys.path or anything from
  9957. # site-packages. This writes .sha.z, one of the two files the seal above
  9958. # digests, so it is in scope of the same forgery class as the merge/covered
  9959. # sites below. A failed resolve here degrades to the existing log_warn path
  9960. # (receipt loses preexisting_modified detection) rather than blocking the
  9961. # snapshot -- matching this call's pre-existing non-fatal-on-failure shape.
  9962. pytool="$(_loki_snapshot_py_tool)" && "$pytool" -I -S "$SCRIPT_DIR/lib/workspace_diff.py" hash-snapshot "$top" "$snap" >/dev/null 2>&1 \
  9963. || log_warn "Could not hash your pre-existing untracked files; the receipt cannot list the ones this run changes"
  9964. _loki_snapshot_seal
  9965. # A seal failure (empty digest or an unreadable-file "?", see the comment
  9966. # at _loki_snapshot_seal) leaves _LOKI_SNAPSHOT_SEAL empty; propagate that
  9967. # as a failure of this function too, so both callers'
  9968. # (_loki_snapshot_or_fail_closed and _loki_resume_snapshot) existing
  9969. # fail-closed handling applies, instead of silently reporting success on a
  9970. # session this process could not actually seal. This is the line that
  9971. # actually makes a seal failure fail closed (_loki_snapshot_seal's own
  9972. # in-function clear is defense in depth, not the mechanism -- see the
  9973. # comment there). This line is deliberately also stripped by
  9974. # tests/test-branch-lifecycle.sh's BACKLOG-70 RED mutation, by its trailing
  9975. # comment marker (kept off this sentence on purpose, so a grep for the
  9976. # marker text matches only the one real code line below, not this prose),
  9977. # same as the guard calls above it: without it, on a mutated copy that
  9978. # also strips `_loki_snapshot_seal`, _LOKI_SNAPSHOT_SEAL stays at its
  9979. # global initial value ("") and this check alone would still return 1 and
  9980. # fail closed, masking the RED reproduction the test relies on to prove
  9981. # the mutation of the ACTUAL guard calls is non-vacuous.
  9982. [ -n "$_LOKI_SNAPSHOT_SEAL" ] || return 1 # BACKLOG-70-SEAL-CHECK
  9983. return 0
  9984. }
  9985. # _loki_untracked_status <out>: the one enumeration behind both the snapshot
  9986. # and the session-created record, so the two always agree: raw
  9987. # `git status --porcelain -z` records for the whole repo, .loki excluded.
  9988. # Non-zero, writing nothing, when git cannot list.
  9989. # BACKLOG 129: resolved via _loki_snapshot_git_tool (fixed root-owned path
  9990. # first, PATH walk restricted to absolute entries as a last resort), not a
  9991. # bare `git`, which a same-UID attacker can shadow with a fake binary on a
  9992. # user-writable PATH entry ahead of the real one -- reproduced empirically: a
  9993. # fake git filtering one path out of its `status` output made that path
  9994. # entirely absent from the sealed snapshot this function ultimately feeds.
  9995. _loki_untracked_status() {
  9996. local top="" prefix="" gittool=""
  9997. local genv=() fk="" fn="" n=0
  9998. gittool="$(_loki_snapshot_git_tool)" || return 1
  9999. # Version floor: GIT_CONFIG_COUNT needs git >= 2.31 and GIT_NO_LAZY_FETCH
  10000. # (no hook-running lazy fetch) needs git >= 2.44. Unparseable also refuses.
  10001. local gver="" gmaj="" gmin=""
  10002. gver="$(LC_ALL=C "$gittool" --version 2>/dev/null)" || gver=""
  10003. gver="${gver#git version }"
  10004. gmaj="${gver%%.*}"
  10005. gmin="${gver#*.}"
  10006. gmin="${gmin%%.*}"
  10007. case "${gmaj}${gmin}" in
  10008. '' | *[!0-9]*) gmaj="" ;;
  10009. esac
  10010. if [ -z "$gmaj" ] || [ -z "$gmin" ] || [ "$gmaj" -lt 2 ] || { [ "$gmaj" -eq 2 ] && [ "$gmin" -lt 44 ]; }; then
  10011. echo "snapshot: git >= 2.44 required (got '${gver:-unknown}'); refusing to run status unprotected" >&2
  10012. return 1
  10013. fi
  10014. # Inherited GIT_DIR / GIT_WORK_TREE would repoint every call below.
  10015. top="$(env -u GIT_DIR -u GIT_WORK_TREE "$gittool" rev-parse --show-toplevel 2>/dev/null)" || return 1
  10016. prefix="$(env -u GIT_DIR -u GIT_WORK_TREE "$gittool" rev-parse --show-prefix 2>/dev/null)" || return 1
  10017. # BACKLOG 131c: a repo-local core.fsmonitor is a command git runs on
  10018. # status; the agent can write .git/config, so disable it (and the
  10019. # untracked cache it could have poisoned) for this one call.
  10020. # A filter driver named by attributes (clean/smudge/process) is also a
  10021. # command git runs when status compares a stat-dirty tracked file, so every
  10022. # configured driver is blanked: clean/smudge/process become empty (never
  10023. # `cat`, which resolves from PATH) and required is false.
  10024. # The overrides travel as GIT_CONFIG_KEY_n / GIT_CONFIG_VALUE_n, never as
  10025. # `-c key=value`: git splits -c at the first "=", so a driver named "x=y"
  10026. # would be missed. GIT_NO_LAZY_FETCH stops a partial-clone status from
  10027. # fetching a missing blob over an agent-written core.sshCommand. Inherited
  10028. # GIT_DIR, GIT_WORK_TREE, GIT_CONFIG_PARAMETERS and GIT_CONFIG_GLOBAL are
  10029. # dropped and GIT_CONFIG_COUNT is overwritten, never appended to. LC_ALL=C
  10030. # keeps the name lookup byte-exact for non-UTF-8 driver names.
  10031. genv=(env -u GIT_DIR -u GIT_WORK_TREE -u GIT_CONFIG_PARAMETERS -u GIT_CONFIG_GLOBAL
  10032. LC_ALL=C GIT_NO_LAZY_FETCH=1)
  10033. # The NUL-delimited listing goes to a file beside the output path, read by
  10034. # a loop in this shell (it appends to genv and n); removed right after the
  10035. # loop, before any later return path.
  10036. : > "${1}.filters" || return 1
  10037. "${genv[@]}" "$gittool" -C "$top" config -z --name-only --get-regexp \
  10038. '^filter\..*\.(clean|smudge|process)$' > "${1}.filters" 2>/dev/null || true
  10039. while IFS= read -r -d '' fk; do
  10040. fn="${fk%.*}"
  10041. genv+=("GIT_CONFIG_KEY_${n}=${fk}" "GIT_CONFIG_VALUE_${n}=")
  10042. n=$((n + 1))
  10043. genv+=("GIT_CONFIG_KEY_${n}=${fn}.required" "GIT_CONFIG_VALUE_${n}=false")
  10044. n=$((n + 1))
  10045. done < "${1}.filters"
  10046. rm -f "${1}.filters"
  10047. # Prove the overrides are visible with a per-call random nonce (a constant
  10048. # could be planted in .git/config by the agent), or refuse to run.
  10049. local nonce=""
  10050. nonce="$(od -An -tx8 -N8 /dev/urandom 2>/dev/null | tr -d ' \n')"
  10051. [ -n "$nonce" ] || return 1
  10052. genv+=("GIT_CONFIG_KEY_${n}=loki.snapshot.sentinel" "GIT_CONFIG_VALUE_${n}=${nonce}")
  10053. n=$((n + 1))
  10054. genv+=("GIT_CONFIG_COUNT=${n}")
  10055. if [ "$("${genv[@]}" "$gittool" -C "$top" config --get loki.snapshot.sentinel 2>/dev/null)" != "$nonce" ]; then
  10056. echo "snapshot: git does not honor GIT_CONFIG_COUNT; refusing to run status unprotected" >&2
  10057. return 1
  10058. fi
  10059. "${genv[@]}" "$gittool" -C "$top" -c core.fsmonitor=false -c core.untrackedCache=false \
  10060. --no-optional-locks status --porcelain -z --no-renames -uall \
  10061. --ignored=matching --ignore-submodules=all -- ":(exclude,literal)${prefix}.loki" \
  10062. > "$1" 2>/dev/null && return 0
  10063. rm -f "$1"
  10064. return 1
  10065. }
  10066. # _loki_untracked_merge <status> <base> <exclude> exact|cover <out> [tracked]
  10067. # Atomically write <out>: the NUL-delimited paths of <base> minus any path
  10068. # <tracked> lists (BACKLOG 68: entries the USER tracked between sessions, as
  10069. # resolved by _loki_tracked_by_user_since_anchor -- never the whole index),
  10070. # plus every untracked ("??") or ignored ("!!") path in <status> that <exclude>
  10071. # does not hold. With "cover", an <exclude> entry ending in "/" also holds
  10072. # every path below it (workspace_diff._covered). An empty or missing <base> or
  10073. # <exclude> is an empty list; any other read or write failure is non-zero and
  10074. # leaves <out> as it was. Sorted bytewise, like `LC_ALL=C sort -z -u`. One
  10075. # python process: bash 3.2 has no associative arrays, and a bash loop cost
  10076. # 300ms per 6,000 entries on /bin/bash. -I -S and no cwd on sys.path: the
  10077. # cwd is the agent's repo.
  10078. # BACKLOG 129: this is the snapshot WRITER -- its <out> becomes
  10079. # preexisting-untracked.z, one of the two files the seal above digests. A bare
  10080. # `python3 -E` here still loads user site-packages (-E only blocks PYTHON* env
  10081. # vars), so a same-UID .pth planted there can run arbitrary code during this
  10082. # call and rewrite <out> after the legitimate write, forging exactly the bytes
  10083. # the seal is about to trust -- reproduced empirically (a .pth that stripped
  10084. # an entry from the just-written output). Resolved via _loki_snapshot_py_tool
  10085. # (fixed root-owned path first) and run with -I -S: -I alone is not enough
  10086. # (verified: `python3 -I` alone still lists Homebrew's user-writable
  10087. # site-packages on this host), -S additionally skips `import site` so no .pth
  10088. # anywhere is ever processed. A resolve failure fails this function closed
  10089. # (return 1, same as any other read/write failure here) rather than silently
  10090. # falling back to a bare, unresolved `python3`.
  10091. _loki_untracked_merge() {
  10092. local pytool=""
  10093. pytool="$(_loki_snapshot_py_tool)" || return 1
  10094. "$pytool" -I -S -c 'import sys
  10095. sys.path[:] = [p for p in sys.path if p not in ("", ".")]
  10096. import os
  10097. status, base, exclude, mode, out = sys.argv[1:6]
  10098. tracked = sys.argv[6] if len(sys.argv) > 6 else ""
  10099. def entries(path, missing_ok=True):
  10100. try:
  10101. with open(path, "rb") as fh:
  10102. return [p for p in fh.read().split(b"\0") if p]
  10103. except FileNotFoundError:
  10104. if missing_ok:
  10105. return []
  10106. raise
  10107. held = set(entries(exclude)) if exclude else set()
  10108. def covered(path):
  10109. if path in held:
  10110. return True
  10111. cut = path.find(b"/") if mode == "cover" else -1
  10112. while cut != -1:
  10113. if path[:cut + 1] in held:
  10114. return True
  10115. cut = path.find(b"/", cut + 1)
  10116. return False
  10117. paths = set(entries(base)) if base else set()
  10118. if tracked:
  10119. # Exact match only: a "dir/" entry is never dropped even if a file under it
  10120. # is now tracked, so an ignored directory (node_modules/) never re-exposes
  10121. # the rest of its subtree (BACKLOG 66/78 territory).
  10122. paths -= set(entries(tracked))
  10123. for rec in entries(status, missing_ok=False):
  10124. # The session-created record ("cover" mode) never holds a directory entry:
  10125. # a whole-directory entry (logs/, out/) would stop the resume union from
  10126. # adopting a file the user later creates inside it, and a .gitignore
  10127. # rewrite would then sweep that file. Adopting such a directory as
  10128. # pre-existing fails safe (the agent files inside stay uncommitted).
  10129. if mode == "cover" and rec.endswith(b"/"):
  10130. continue
  10131. if rec[:3] in (b"?? ", b"!! ") and not covered(rec[3:]):
  10132. paths.add(rec[3:])
  10133. with open(out + ".tmp", "wb") as fh:
  10134. fh.write(b"".join(p + b"\0" for p in sorted(paths)))
  10135. os.replace(out + ".tmp", out)' "$@" 2>/dev/null
  10136. }
  10137. # _loki_tracked_by_user_since_anchor <top> <out> (BACKLOG 68 rework)
  10138. # Write <out>: the NUL-delimited paths git added to the index between the
  10139. # recorded "tracked-since" anchor and HEAD -- i.e. what a resume union may
  10140. # safely treat as "the USER tracked this between sessions" and prune. Returns
  10141. # non-zero (writing nothing) whenever that inference is not safe, which leaves
  10142. # the caller pruning nothing (the old, safe-by-default behavior):
  10143. # - no anchor recorded yet (an older session, or nothing survived a turn)
  10144. # - a provider turn is marked in-flight (.loki/state/turn-in-flight): a turn
  10145. # can commit or stage a file and then be SIGKILLed before the next record
  10146. # call advances the anchor, so anything since the LAST anchor is unproven
  10147. # - the anchor does not resolve, or is not an ancestor of HEAD (branch
  10148. # switched, history rewritten, anchor stale)
  10149. # --diff-filter=A, --no-renames: only additions, matching
  10150. # _loki_untrack_agent_committed_user_files's own diff so the two never
  10151. # disagree about which paths are "added since X".
  10152. # BACKLOG 129: <out> here is subtracted from the snapshot base inside
  10153. # _loki_untracked_merge (the "tracked" argument) before that WRITER seals its
  10154. # result -- a fake git that lists an extra path here can prune a real
  10155. # untracked path (e.g. a secret) out of the sealed snapshot, the same attack
  10156. # as a forged `git status`, just one function over. Resolved via
  10157. # _loki_snapshot_git_tool like the other git calls in this writer chain.
  10158. _loki_tracked_by_user_since_anchor() {
  10159. local top="$1" out="$2" anchor_file=".loki/state/tracked-since.sha" anchor="" gittool=""
  10160. [ -f .loki/state/turn-in-flight ] && return 1
  10161. [ -s "$anchor_file" ] || return 1
  10162. anchor="$(cat "$anchor_file" 2>/dev/null)"
  10163. [ -n "$anchor" ] || return 1
  10164. gittool="$(_loki_snapshot_git_tool)" || return 1
  10165. "$gittool" rev-parse --verify -q "$anchor" >/dev/null 2>&1 || return 1
  10166. "$gittool" merge-base --is-ancestor "$anchor" HEAD 2>/dev/null || return 1
  10167. "$gittool" -C "$top" diff --name-only -z --no-renames --diff-filter=A "$anchor" HEAD > "$out.tmp" 2>/dev/null \
  10168. || { rm -f "$out.tmp"; return 1; }
  10169. mv -f "$out.tmp" "$out"
  10170. return 0
  10171. }
  10172. # _loki_advance_tracked_since_anchor
  10173. # Move .loki/state/tracked-since.sha to the current HEAD and clear
  10174. # .loki/state/turn-in-flight, but ONLY once the anchor write is confirmed on
  10175. # disk. Called after every provider turn (_loki_record_session_created) and at
  10176. # normal session end (commit_session_changes), i.e. at every point this run
  10177. # can prove no provider turn is currently running -- PROVIDED the anchor write
  10178. # itself lands, since _loki_tracked_by_user_since_anchor trusts both signals
  10179. # together (anchor position + in-flight marker) to prove a commit happened
  10180. # strictly after the anchor with no turn running. Clearing the marker on a
  10181. # failed write would decouple them: a stale anchor stays on disk while the
  10182. # marker falsely reports "no turn was in flight", so a later resume could
  10183. # prove something it can't actually prove and prune an agent's own commit with
  10184. # no disclosure (this is BACKLOG 68's failure mode, reintroduced). A HEAD that
  10185. # does not resolve (unborn branch) is not a write failure -- there is nothing
  10186. # to anchor yet -- so the marker still clears; the anchor stays "not set" as
  10187. # before. Always returns 0 (both call sites are bare, unchecked calls): the
  10188. # in-flight marker, not the return code, is the safety signal on a failed
  10189. # write.
  10190. _loki_advance_tracked_since_anchor() {
  10191. local sha=""
  10192. sha="$(git rev-parse --verify -q HEAD 2>/dev/null)" || true
  10193. if [ -n "$sha" ]; then
  10194. if ! { printf '%s\n' "$sha" > .loki/state/tracked-since.sha.tmp 2>/dev/null \
  10195. && mv -f .loki/state/tracked-since.sha.tmp .loki/state/tracked-since.sha 2>/dev/null; }; then
  10196. # Anchor write failed (disk full, a stray .tmp collision, a
  10197. # concurrent writer). Deliberately leave turn-in-flight SET and
  10198. # return here, before the unconditional clear below: the safe
  10199. # direction is to keep the old fallback-to-no-pruning path active
  10200. # for one more check, never to clear a marker the anchor write did
  10201. # not earn.
  10202. rm -f .loki/state/tracked-since.sha.tmp 2>/dev/null
  10203. return 0
  10204. fi
  10205. fi
  10206. rm -f .loki/state/turn-in-flight 2>/dev/null
  10207. return 0
  10208. }
  10209. # _loki_record_session_created
  10210. # Union into .loki/state/session-created.z (NUL-delimited, repo-top-relative,
  10211. # atomic) the paths git does not track now that the pre-existing snapshot does
  10212. # not cover: the files this session created. The resume union never adds these
  10213. # to the snapshot, so a session that is interrupted (cleanup) or killed (pod
  10214. # loss: no trap runs, hence the call after every provider turn) still has its
  10215. # own files committed by the session that finishes. commit_session_changes
  10216. # clears the record after a normal commit; a new session branch starts empty.
  10217. # Records nothing unless this run took a current snapshot (a leftover or stale
  10218. # one would make it claim a user's file). On a failure the record is left as it
  10219. # was, never partial, and preexisting-untracked.failed is never touched; the
  10220. # next successful call recomputes everything, so only a failure on the final
  10221. # call before a kill loses paths (those are then treated as the user's: kept on
  10222. # disk, not committed).
  10223. _loki_record_session_created() {
  10224. local snap=".loki/state/preexisting-untracked.z" out=".loki/state/session-created.z"
  10225. # BACKLOG 130(c): verify session-created.z BEFORE this run's THIS_RUN gate
  10226. # or its own merge trust the file -- verify-before-trust, not verify-after-
  10227. # use. Runs unconditionally (not gated on THIS_RUN=1): an interrupt/cleanup
  10228. # call can still fire after THIS_RUN was cleared mid-session (e.g. by
  10229. # _loki_snapshot_verify below on an earlier turn), and that call must not
  10230. # skip this check just because the gate below is about to return early. On
  10231. # a mismatch the file is already deleted by the time this returns (fail
  10232. # closed inside _loki_session_created_verify itself, not via this return
  10233. # value alone -- every call site of this function ignores its return with
  10234. # `|| true`, same as _loki_snapshot_verify above); the merge below then
  10235. # recomputes cleanly from a clean slate (base="$out" is now absent), so a
  10236. # legitimate record right after a caught tamper still records this turn's
  10237. # real files instead of hard-failing the whole call.
  10238. # ACCEPTED GAP (verify-to-use window, disclosed here since ordering is the
  10239. # whole point of this fix): this verify call and the merge's own read of
  10240. # "$out" as `base` (inside _loki_untracked_merge, below) are not atomic --
  10241. # _loki_advance_tracked_since_anchor, a git rev-parse, and a whole-repo
  10242. # `git status` all run in between. A same-UID write landing in that window
  10243. # is merged, then sealed as trusted by the seal call after a successful
  10244. # merge. This is the SAME shape of gap _LOKI_SNAPSHOT_SEAL's own
  10245. # verify/merge pair already accepts for preexisting-untracked.z (see
  10246. # _loki_untracked_merge's own BACKLOG 129 comment). Closing it would mean
  10247. # re-verifying again immediately before _loki_untracked_merge's read, inside
  10248. # that function -- deliberately not done here: _loki_untracked_merge's exact
  10249. # text is what tests/test-branch-lifecycle.sh's RED mutation harness matches
  10250. # by name-anchor, and threading a second, narrower verify through its
  10251. # shared codepath (also called with mode=exact for the OTHER snapshot) risks
  10252. # that anchor for a gap this narrow. Disclosure, not full prevention, is the
  10253. # same bar BACKLOG 70 itself accepts.
  10254. _loki_session_created_verify || true
  10255. [ "${_LOKI_SNAPSHOT_THIS_RUN:-0}" = 1 ] || return 0
  10256. # BACKLOG 70: detect tampering with the sealed snapshot as early as
  10257. # possible (this runs after every provider turn, not just at commit time).
  10258. _loki_snapshot_verify || return 1
  10259. # BACKLOG 68 rework: this call is the point right after a provider turn
  10260. # ends, so it also advances the tracked-since anchor and clears the
  10261. # in-flight marker -- whatever the agent committed or staged during the
  10262. # turn that just finished is now provably NOT mid-turn, so a later resume's
  10263. # anchor..HEAD diff may safely be checked against it.
  10264. _loki_advance_tracked_since_anchor
  10265. if [ -f "$snap" ] && _loki_untracked_status "$out.status" \
  10266. && _loki_untracked_merge "$out.status" "$out" "$snap" cover "$out"; then
  10267. rm -f "$out.status"
  10268. # BACKLOG 130(c): seal this process's own memory of the record it just
  10269. # wrote, so the NEXT call to this function (or commit_session_changes)
  10270. # verifies against what THIS process actually wrote, not whatever an
  10271. # agent process may write to the file in between.
  10272. _loki_session_created_seal
  10273. return 0
  10274. fi
  10275. rm -f "$out.status" "$out.tmp" 2>/dev/null
  10276. log_warn "Could not record the files this session created; if the run stops before the next record, they are treated as yours and left uncommitted"
  10277. return 1
  10278. }
  10279. # _loki_resume_snapshot: the resume paths' union, naming the files carried over
  10280. # from an unfinished previous session (this session's commit includes them).
  10281. _loki_resume_snapshot() {
  10282. local carried=""
  10283. # BACKLOG 130(c): seal THIS process's own first look at session-created.z
  10284. # before anything below reads or trusts it (verify-before-trust). This is
  10285. # a resuming process picking up a file the PREVIOUS (now-dead) process
  10286. # wrote; there is no seal from that process to verify against here (a
  10287. # seal never survives past the process that set it) -- sealing now is what
  10288. # lets THIS process detect an agent tampering with the file for the rest
  10289. # of ITS OWN lifetime (every subsequent record call, and the commit-time
  10290. # check), the same disclosure bar BACKLOG 70 already accepts for the
  10291. # sibling snapshot: tampering already on disk before this process starts
  10292. # is an accepted gap, never silently trusted for
autonomy/sandbox.sh · Bundled script · 2,265 lines · 2 findings marked
  1. #!/usr/bin/env bash
  2. #===============================================================================
  3. # Loki Mode - Docker Sandbox Manager
  4. # Provides isolated container execution for enhanced security
  5. #
  6. # Usage:
  7. # ./autonomy/sandbox.sh start [OPTIONS] [PRD_PATH]
  8. # ./autonomy/sandbox.sh stop
  9. # ./autonomy/sandbox.sh status
  10. # ./autonomy/sandbox.sh shell
  11. #
  12. # Environment Variables:
  13. # LOKI_SANDBOX_IMAGE - Docker image to use (default: loki-mode:sandbox)
  14. # LOKI_SANDBOX_NETWORK - Network mode: bridge, none, host (default: bridge)
  15. # LOKI_SANDBOX_ALLOW_HOSTS - Opt-in egress allowlist (comma-separated hostnames);
  16. # blocks link-local and private ranges (default: unset)
  17. # LOKI_SANDBOX_CPUS - CPU limit (default: 2)
  18. # LOKI_SANDBOX_MEMORY - Memory limit (default: 4g)
  19. # LOKI_SANDBOX_READONLY - Mount project as read-only (default: false)
  20. #
  21. # Security Features:
  22. # - Seccomp profile restricts dangerous syscalls
  23. # - No new privileges flag prevents privilege escalation
  24. # - Dropped capabilities reduce attack surface
  25. # - Resource limits prevent DoS
  26. # - Optional read-only filesystem
  27. # - API keys mounted read-only
  28. #===============================================================================
  29. set -euo pipefail
  30. # Colors
  31. RED='\033[0;31m'
  32. GREEN='\033[0;32m'
  33. YELLOW='\033[1;33m'
  34. BLUE='\033[0;34m'
  35. CYAN='\033[0;36m'
  36. NC='\033[0m'
  37. BOLD='\033[1m'
  38. # Configuration
  39. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  40. SKILL_DIR="$(dirname "$SCRIPT_DIR")"
  41. PROJECT_DIR="${LOKI_PROJECT_DIR:-$(pwd)}"
  42. # Normalize PROJECT_DIR (remove trailing slash)
  43. PROJECT_DIR="${PROJECT_DIR%/}"
  44. # Container name includes path hash to avoid collisions between similarly-named projects
  45. # macOS uses md5 instead of md5sum
  46. PROJECT_HASH=$(echo "$PROJECT_DIR" | (md5sum 2>/dev/null || md5 -r 2>/dev/null || echo "$$") | cut -c1-8)
  47. CONTAINER_NAME="loki-sandbox-$(basename "$PROJECT_DIR" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g')-${PROJECT_HASH}"
  48. # Sandbox settings
  49. SANDBOX_IMAGE="${LOKI_SANDBOX_IMAGE:-loki-mode:sandbox}"
  50. SANDBOX_NETWORK="${LOKI_SANDBOX_NETWORK:-bridge}"
  51. SANDBOX_CPUS="${LOKI_SANDBOX_CPUS:-2}"
  52. SANDBOX_MEMORY="${LOKI_SANDBOX_MEMORY:-4g}"
  53. SANDBOX_READONLY="${LOKI_SANDBOX_READONLY:-false}"
  54. # API ports
  55. API_PORT="${LOKI_DASHBOARD_PORT:-57374}"
  56. DASHBOARD_PORT="${LOKI_DASHBOARD_PORT:-57374}"
  57. # Security: Prompt injection disabled by default for enterprise security
  58. PROMPT_INJECTION_ENABLED="${LOKI_PROMPT_INJECTION:-false}"
  59. #===============================================================================
  60. # Docker Credential Mount Presets
  61. #===============================================================================
  62. # Preset definitions: "host_path:container_path:mode"
  63. # Container runs as user 'loki' (UID 1000), so mount to /home/loki/
  64. # Uses functions instead of declare -A for bash 3.2 compatibility (macOS default)
  65. _get_mount_preset() {
  66. case "$1" in
  67. gh) echo "$HOME/.config/gh:/home/loki/.config/gh:ro" ;;
  68. git) echo "$HOME/.gitconfig:/home/loki/.gitconfig:ro" ;;
  69. ssh) echo "$HOME/.ssh/known_hosts:/home/loki/.ssh/known_hosts:ro" ;;
  70. aws) echo "$HOME/.aws:/home/loki/.aws:ro" ;;
  71. azure) echo "$HOME/.azure:/home/loki/.azure:ro" ;;
  72. kube) echo "$HOME/.kube:/home/loki/.kube:ro" ;;
  73. terraform) echo "$HOME/.terraform.d:/home/loki/.terraform.d:ro" ;;
  74. gcloud) echo "$HOME/.config/gcloud:/home/loki/.config/gcloud:ro" ;;
  75. npm) echo "$HOME/.npmrc:/home/loki/.npmrc:ro" ;;
  76. *) echo "" ;;
  77. esac
  78. }
  79. # Environment variables auto-passed per preset (comma-separated)
  80. _get_env_preset() {
  81. case "$1" in
  82. aws) echo "AWS_REGION,AWS_PROFILE,AWS_DEFAULT_REGION" ;;
  83. azure) echo "AZURE_SUBSCRIPTION_ID,AZURE_TENANT_ID" ;;
  84. gcloud) echo "GOOGLE_PROJECT,GOOGLE_REGION,GCLOUD_PROJECT" ;;
  85. terraform) echo "TF_VAR_*" ;;
  86. *) echo "" ;;
  87. esac
  88. }
  89. #===============================================================================
  90. # Utility Functions
  91. #===============================================================================
  92. log_info() {
  93. echo -e "${BLUE}[SANDBOX]${NC} $1"
  94. }
  95. log_success() {
  96. echo -e "${GREEN}[SANDBOX]${NC} $1"
  97. }
  98. log_warn() {
  99. echo -e "${YELLOW}[SANDBOX]${NC} $1"
  100. }
  101. log_error() {
  102. echo -e "${RED}[SANDBOX]${NC} $1" >&2
  103. }
  104. #===============================================================================
  105. # A5: ALLOWED_PATHS + BLOCKED_COMMANDS enforcement (sandbox-local)
  106. #
  107. # sandbox.sh runs as its own process (set -euo pipefail) and does NOT source
  108. # run.sh, so the run.sh helpers are out of scope. These are self-contained
  109. # mirrors that read the same env vars (LOKI_ALLOWED_PATHS / LOKI_BLOCKED_COMMANDS)
  110. # so the sandbox -- the real security boundary -- enforces them at the point
  111. # where run.sh/sandbox.sh actually controls the surface: which host paths get
  112. # bind-mounted, and the operator-supplied `loki sandbox run <cmd>` argv.
  113. #
  114. # HONEST SCOPE: this does NOT restrict provider-driven agent writes inside the
  115. # container (those are contained by cap-drop/seccomp/read-only mounts, not a
  116. # path allowlist). It is defense-in-depth on the surfaces run.sh owns.
  117. #===============================================================================
  118. # Return 0 if target is within LOKI_ALLOWED_PATHS (or the allowlist is unset/
  119. # empty -> no restriction). realpath -m collapses ../ traversal before the
  120. # prefix check, so allowed/../../etc cannot slip past.
  121. _sandbox_path_within_allowed() {
  122. local target="$1"
  123. local allow="${LOKI_ALLOWED_PATHS:-}"
  124. [ -z "$allow" ] && return 0
  125. [ -z "$target" ] && return 1
  126. local norm_target
  127. norm_target="$(realpath -m -- "$target" 2>/dev/null || true)"
  128. if [ -z "$norm_target" ]; then
  129. norm_target="$(LOKI_AP_T="$target" python3 -c 'import os; print(os.path.realpath(os.environ["LOKI_AP_T"]))' 2>/dev/null || true)"
  130. fi
  131. [ -z "$norm_target" ] && return 1
  132. local entry norm_entry
  133. IFS=',' read -ra _AP_ARRAY <<< "$allow"
  134. for entry in "${_AP_ARRAY[@]}"; do
  135. entry="${entry#"${entry%%[![:space:]]*}"}"
  136. entry="${entry%"${entry##*[![:space:]]}"}"
  137. [ -z "$entry" ] && continue
  138. # Tilde-expand allowlist entries so "~/proj" matches a resolved $HOME path.
  139. # SC2088 disabled intentionally: we are pattern-MATCHING a literal "~/" at
  140. # the start of a config-supplied string (no expansion wanted here), then
  141. # expanding it ourselves via $HOME. The directive sits in front of the whole
  142. # if-block (it cannot precede an elif branch -- SC1123).
  143. # shellcheck disable=SC2088
  144. if [[ "$entry" == "~/"* ]]; then
  145. entry="$HOME/${entry#\~/}"
  146. elif [[ "$entry" == "~" ]]; then
  147. entry="$HOME"
  148. fi
  149. norm_entry="$(realpath -m -- "$entry" 2>/dev/null || true)"
  150. if [ -z "$norm_entry" ]; then
  151. norm_entry="$(LOKI_AP_E="$entry" python3 -c 'import os; print(os.path.realpath(os.environ["LOKI_AP_E"]))' 2>/dev/null || true)"
  152. fi
  153. [ -z "$norm_entry" ] && continue
  154. if [ "$norm_target" = "$norm_entry" ] || [ "${norm_target#"${norm_entry}/"}" != "$norm_target" ]; then
  155. return 0
  156. fi
  157. done
  158. return 1
  159. }
  160. # Return 1 (block) if the command string contains a blocked pattern. Mirrors
  161. # run.sh check_command_allowed; default list matches run.sh's BLOCKED_COMMANDS.
  162. _sandbox_command_allowed() {
  163. local command="$1"
  164. # NOTE: the default list is assigned to a separate var first. It cannot be an
  165. # inline ${VAR:-default} default because the default value contains a literal
  166. # "}" (the fork-bomb ":(){ :|:& };:"), which would prematurely terminate the
  167. # ${...} parameter expansion and corrupt the list.
  168. local _default_blocked='rm -rf /,dd if=,mkfs,:(){ :|:& };:'
  169. local blocked_list="${LOKI_BLOCKED_COMMANDS:-$_default_blocked}"
  170. local blocked
  171. IFS=',' read -ra _BC_ARRAY <<< "$blocked_list"
  172. for blocked in "${_BC_ARRAY[@]}"; do
  173. [ -z "$blocked" ] && continue
  174. if [[ "$command" == *"$blocked"* ]]; then
  175. log_error "SECURITY: blocked dangerous command (matched '$blocked'): $command"
  176. return 1
  177. fi
  178. done
  179. return 0
  180. }
  181. # Check if a port is available
  182. check_port_available() {
  183. local port="$1"
  184. if command -v lsof &>/dev/null; then
  185. ! lsof -i ":$port" &>/dev/null
  186. elif command -v nc &>/dev/null; then
  187. ! nc -z localhost "$port" 2>/dev/null
  188. else
  189. # Assume available if we can't check
  190. return 0
  191. fi
  192. }
  193. # Validate project directory
  194. validate_project_dir() {
  195. if [[ ! -d "$PROJECT_DIR" ]]; then
  196. log_error "Project directory does not exist: $PROJECT_DIR"
  197. return 1
  198. fi
  199. if [[ ! -r "$PROJECT_DIR" ]]; then
  200. log_error "Project directory is not readable: $PROJECT_DIR"
  201. return 1
  202. fi
  203. if [[ "$SANDBOX_READONLY" != "true" ]] && [[ ! -w "$PROJECT_DIR" ]]; then
  204. log_warn "Project directory is not writable: $PROJECT_DIR"
  205. log_info "Consider using LOKI_SANDBOX_READONLY=true"
  206. fi
  207. return 0
  208. }
  209. # Warn about API keys based on provider
  210. warn_missing_api_keys() {
  211. local provider="$1"
  212. case "$provider" in
  213. claude)
  214. if [[ -z "${ANTHROPIC_API_KEY:-}" ]]; then
  215. log_warn "ANTHROPIC_API_KEY not set - Claude commands will fail inside container"
  216. fi
  217. ;;
  218. opencode)
  219. if [[ -z "${OPENROUTER_API_KEY:-}" ]]; then
  220. log_info "OPENROUTER_API_KEY not set - opencode needs a key for the provider in its model id (e.g. openrouter/<vendor>/<model>)"
  221. fi
  222. ;;
  223. esac
  224. }
  225. # Cleanup handler for signals
  226. cleanup_container() {
  227. log_warn "Interrupted - cleaning up container..."
  228. docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
  229. _sandbox_egress_down
  230. exit 130
  231. }
  232. check_docker() {
  233. if ! command -v docker &> /dev/null; then
  234. log_error "Docker not found. Install Docker to use sandbox mode."
  235. log_error " macOS: brew install --cask docker"
  236. log_error " Linux: curl -fsSL https://get.docker.com | sh"
  237. exit 1
  238. fi
  239. if ! docker info &> /dev/null; then
  240. log_error "Docker daemon not running. Start Docker Desktop or dockerd."
  241. exit 1
  242. fi
  243. }
  244. build_sandbox_image() {
  245. local dockerfile="$SKILL_DIR/docker/Dockerfile.sandbox"
  246. if [[ ! -f "$dockerfile" ]]; then
  247. log_error "Sandbox Dockerfile not found at $dockerfile"
  248. exit 1
  249. fi
  250. log_info "Building sandbox image..."
  251. docker build -t "$SANDBOX_IMAGE" -f "$dockerfile" "$SKILL_DIR"
  252. log_success "Sandbox image built: $SANDBOX_IMAGE"
  253. }
  254. ensure_image() {
  255. if ! docker image inspect "$SANDBOX_IMAGE" &> /dev/null; then
  256. log_warn "Sandbox image not found. Building..."
  257. build_sandbox_image
  258. fi
  259. }
  260. #===============================================================================
  261. # Git Worktree Sandbox (Fallback for non-Docker environments)
  262. #===============================================================================
  263. # Worktree sandbox settings
  264. WORKTREE_PREFIX="loki-sandbox"
  265. WORKTREE_BASE="${LOKI_WORKTREE_BASE:-${TMPDIR:-/tmp}}"
  266. WORKTREE_STATE_FILE="${PROJECT_DIR}/.loki/sandbox/worktree-state.json"
  267. # Check if Docker Desktop Sandbox is available (microVM-based isolation)
  268. is_docker_desktop_sandbox_available() {
  269. command -v docker &>/dev/null && docker sandbox version &>/dev/null 2>&1
  270. }
  271. # Check if Docker is available (non-fatal version)
  272. is_docker_available() {
  273. command -v docker &>/dev/null && docker info &>/dev/null 2>&1
  274. }
  275. # Check if git worktree is available
  276. is_git_available() {
  277. command -v git &>/dev/null && git rev-parse --git-dir &>/dev/null 2>&1
  278. }
  279. # Detect which sandbox mode to use
  280. # Priority: docker-desktop > docker > worktree
  281. detect_sandbox_mode() {
  282. local requested="${1:-auto}"
  283. case "$requested" in
  284. docker-desktop)
  285. if is_docker_desktop_sandbox_available; then
  286. echo "docker-desktop"
  287. else
  288. log_error "Docker Desktop Sandbox not available (requires Docker Desktop 4.58+)"
  289. return 1
  290. fi
  291. ;;
  292. docker)
  293. if is_docker_available; then
  294. echo "docker"
  295. else
  296. log_error "Docker requested but not available"
  297. return 1
  298. fi
  299. ;;
  300. worktree)
  301. if is_git_available; then
  302. echo "worktree"
  303. else
  304. log_error "Git not available for worktree sandbox"
  305. return 1
  306. fi
  307. ;;
  308. auto|*)
  309. if is_docker_desktop_sandbox_available; then
  310. echo "docker-desktop"
  311. elif is_docker_available; then
  312. echo "docker"
  313. elif is_git_available; then
  314. log_warn "Docker not available - using worktree sandbox (soft isolation)"
  315. echo "worktree"
  316. else
  317. log_error "Neither Docker nor Git available for sandbox mode"
  318. return 1
  319. fi
  320. ;;
  321. esac
  322. }
  323. # Create a worktree sandbox
  324. create_worktree_sandbox() {
  325. local prd_path="${1:-}"
  326. local provider="${LOKI_PROVIDER:-claude}"
  327. local timestamp
  328. timestamp=$(date +%Y%m%d-%H%M%S)
  329. local sandbox_name="${WORKTREE_PREFIX}-${timestamp}"
  330. local sandbox_branch="${WORKTREE_PREFIX}-${timestamp}"
  331. local sandbox_path="${WORKTREE_BASE}/${sandbox_name}"
  332. log_warn ""
  333. log_warn "=========================================="
  334. log_warn " WORKTREE SANDBOX - SOFT ISOLATION ONLY"
  335. log_warn "=========================================="
  336. log_warn ""
  337. log_warn "This provides workspace isolation but NOT:"
  338. log_warn " - Filesystem isolation (can access any file)"
  339. log_warn " - Network isolation (full network access)"
  340. log_warn " - Process isolation (no resource limits)"
  341. log_warn ""
  342. log_warn "For full isolation, install Docker."
  343. log_warn ""
  344. log_info "Creating worktree sandbox..."
  345. log_info " Location: $sandbox_path"
  346. log_info " Branch: $sandbox_branch"
  347. # Check for existing sandbox
  348. if [[ -f "$WORKTREE_STATE_FILE" ]]; then
  349. local existing_path
  350. existing_path=$(jq -r '.sandbox_path // empty' "$WORKTREE_STATE_FILE" 2>/dev/null)
  351. if [[ -n "$existing_path" ]] && [[ -d "$existing_path" ]]; then
  352. log_warn "Existing sandbox found: $existing_path"
  353. log_info "Use 'loki sandbox stop' to stop it first."
  354. return 1
  355. fi
  356. fi
  357. # Create branch for sandbox
  358. if ! git branch "$sandbox_branch" HEAD 2>/dev/null; then
  359. log_error "Failed to create sandbox branch. Are you in a git repository?"
  360. return 1
  361. fi
  362. # Create worktree
  363. if ! git worktree add "$sandbox_path" "$sandbox_branch" 2>/dev/null; then
  364. git branch -D "$sandbox_branch" 2>/dev/null
  365. log_error "Failed to create worktree at $sandbox_path"
  366. return 1
  367. fi
  368. # Set up sandbox environment
  369. mkdir -p "$sandbox_path/.loki/"{state,logs,signals,queue,memory}
  370. # Copy essential files
  371. for file in ".loki/CONTINUITY.md" ".loki/config.yaml" "SKILL.md" "CLAUDE.md"; do
  372. if [[ -f "$PROJECT_DIR/$file" ]]; then
  373. mkdir -p "$(dirname "$sandbox_path/$file")"
  374. cp "$PROJECT_DIR/$file" "$sandbox_path/$file" 2>/dev/null || true
  375. fi
  376. done
  377. # Create sandbox marker
  378. cat > "$sandbox_path/.loki/SANDBOX_MODE" << EOF
  379. ISOLATION_TYPE=worktree
  380. CREATED_AT=$(date -Iseconds)
  381. PARENT_DIR=$PROJECT_DIR
  382. EOF
  383. # Save state using python3 for safe JSON encoding (avoids shell injection
  384. # from paths containing quotes, backslashes, or other special characters)
  385. mkdir -p "$(dirname "$WORKTREE_STATE_FILE")"
  386. python3 -c "
  387. import json, sys, datetime
  388. data = {
  389. 'sandbox_path': sys.argv[1],
  390. 'sandbox_branch': sys.argv[2],
  391. 'created_at': datetime.datetime.now().astimezone().isoformat(),
  392. 'provider': sys.argv[3],
  393. 'prd_path': sys.argv[4],
  394. 'status': 'created',
  395. 'isolation_type': 'worktree'
  396. }
  397. with open(sys.argv[5], 'w') as f:
  398. json.dump(data, f, indent=4)
  399. f.write('\n')
  400. " "$sandbox_path" "$sandbox_branch" "$provider" "$prd_path" "$WORKTREE_STATE_FILE"
  401. log_success "Worktree sandbox created: $sandbox_path"
  402. return 0
  403. }
  404. # Start loki in worktree sandbox
  405. start_worktree_sandbox() {
  406. local prd_path="${1:-}"
  407. local provider="${LOKI_PROVIDER:-claude}"
  408. # Create sandbox if needed
  409. if ! [[ -f "$WORKTREE_STATE_FILE" ]]; then
  410. create_worktree_sandbox "$prd_path" || return 1
  411. fi
  412. local sandbox_path
  413. sandbox_path=$(jq -r '.sandbox_path' "$WORKTREE_STATE_FILE")
  414. if [[ ! -d "$sandbox_path" ]]; then
  415. log_error "Sandbox path does not exist: $sandbox_path"
  416. rm -f "$WORKTREE_STATE_FILE"
  417. return 1
  418. fi
  419. log_info "Starting Loki in worktree sandbox..."
  420. log_info " Path: $sandbox_path"
  421. log_info " Provider: $provider"
  422. # Build loki command
  423. local loki_cmd="$SKILL_DIR/autonomy/run.sh"
  424. if [[ -n "$prd_path" ]]; then
  425. if [[ -f "$sandbox_path/$prd_path" ]]; then
  426. loki_cmd="$loki_cmd $prd_path"
  427. elif [[ -f "$prd_path" ]]; then
  428. cp "$prd_path" "$sandbox_path/" 2>/dev/null || true
  429. loki_cmd="$loki_cmd $(basename "$prd_path")"
  430. fi
  431. fi
  432. loki_cmd="$loki_cmd --provider $provider"
  433. # Set environment
  434. export LOKI_SANDBOX_MODE=true
  435. export LOKI_SANDBOX_TYPE=worktree
  436. export LOKI_NOTIFICATIONS=false
  437. log_info ""
  438. log_info "Commands (in another terminal):"
  439. log_info " loki sandbox status - Check status"
  440. log_info " loki sandbox prompt 'msg' - Send prompt"
  441. log_info " loki sandbox stop - Stop sandbox"
  442. log_info ""
  443. # Run loki in sandbox directory
  444. cd "$sandbox_path" && $loki_cmd
  445. }
  446. # Stop worktree sandbox
  447. stop_worktree_sandbox() {
  448. if [[ ! -f "$WORKTREE_STATE_FILE" ]]; then
  449. log_warn "No active worktree sandbox found"
  450. return 0
  451. fi
  452. local sandbox_path
  453. sandbox_path=$(jq -r '.sandbox_path' "$WORKTREE_STATE_FILE")
  454. local sandbox_branch
  455. sandbox_branch=$(jq -r '.sandbox_branch' "$WORKTREE_STATE_FILE")
  456. log_info "Stopping worktree sandbox..."
  457. # Send stop signal
  458. if [[ -d "$sandbox_path" ]]; then
  459. touch "$sandbox_path/.loki/STOP" 2>/dev/null || true
  460. fi
  461. # Cleanup
  462. if [[ "${LOKI_SANDBOX_CLEANUP:-true}" == "true" ]]; then
  463. log_info "Cleaning up worktree..."
  464. if [[ -n "$sandbox_path" ]] && [[ -d "$sandbox_path" ]]; then
  465. git worktree remove "$sandbox_path" --force 2>/dev/null || rm -rf "$sandbox_path" 2>/dev/null
  466. fi
  467. if [[ -n "$sandbox_branch" ]]; then
  468. git branch -D "$sandbox_branch" 2>/dev/null || true
  469. fi
  470. git worktree prune 2>/dev/null || true
  471. rm -f "$WORKTREE_STATE_FILE"
  472. log_success "Worktree sandbox cleaned up"
  473. else
  474. log_info "Sandbox preserved at: $sandbox_path"
  475. log_info "Run 'loki sandbox cleanup' to remove."
  476. fi
  477. }
  478. # Worktree sandbox status
  479. worktree_sandbox_status() {
  480. if [[ ! -f "$WORKTREE_STATE_FILE" ]]; then
  481. log_info "No active worktree sandbox"
  482. return 0
  483. fi
  484. local sandbox_path
  485. sandbox_path=$(jq -r '.sandbox_path' "$WORKTREE_STATE_FILE")
  486. local sandbox_branch
  487. sandbox_branch=$(jq -r '.sandbox_branch' "$WORKTREE_STATE_FILE")
  488. local created_at
  489. created_at=$(jq -r '.created_at' "$WORKTREE_STATE_FILE")
  490. echo ""
  491. echo -e "${BOLD}Worktree Sandbox Status${NC}"
  492. echo "========================"
  493. echo ""
  494. echo -e " Path: ${CYAN}$sandbox_path${NC}"
  495. echo -e " Branch: $sandbox_branch"
  496. echo -e " Created: $created_at"
  497. if [[ -d "$sandbox_path" ]]; then
  498. local disk_usage
  499. disk_usage=$(du -sh "$sandbox_path" 2>/dev/null | cut -f1)
  500. echo -e " Disk: $disk_usage"
  501. if [[ -f "$sandbox_path/.loki/STOP" ]]; then
  502. echo -e " Status: ${YELLOW}Stopping${NC}"
  503. else
  504. echo -e " Status: ${GREEN}Active${NC}"
  505. fi
  506. else
  507. echo -e " Status: ${RED}Missing${NC}"
  508. fi
  509. echo ""
  510. echo -e "${YELLOW}[SOFT ISOLATION]${NC} - No filesystem/network/process isolation"
  511. echo ""
  512. }
  513. # Send prompt to worktree sandbox
  514. worktree_sandbox_prompt() {
  515. local prompt="$*"
  516. # Security check: prompt injection disabled by default
  517. if [[ "$PROMPT_INJECTION_ENABLED" != "true" ]]; then
  518. log_error "Prompt injection is disabled for security"
  519. log_info ""
  520. log_info "To enable, set LOKI_PROMPT_INJECTION=true"
  521. log_info " Example: LOKI_PROMPT_INJECTION=true loki sandbox prompt 'your message'"
  522. log_info ""
  523. log_warn "WARNING: Only enable in trusted environments"
  524. return 1
  525. fi
  526. if [[ -z "$prompt" ]]; then
  527. log_error "Usage: loki sandbox prompt <your message>"
  528. return 1
  529. fi
  530. if [[ ! -f "$WORKTREE_STATE_FILE" ]]; then
  531. log_error "No active worktree sandbox"
  532. return 1
  533. fi
  534. local sandbox_path
  535. sandbox_path=$(jq -r '.sandbox_path' "$WORKTREE_STATE_FILE")
  536. if [[ ! -d "$sandbox_path" ]]; then
  537. log_error "Sandbox path does not exist"
  538. return 1
  539. fi
  540. # Use printf to safely write prompt without interpretation
  541. printf '%s\n' "$prompt" > "$sandbox_path/.loki/HUMAN_INPUT.md"
  542. log_success "Prompt sent to worktree sandbox"
  543. log_info "Check $sandbox_path/.loki/logs/ for response"
  544. }
  545. # Cleanup orphaned worktrees
  546. cleanup_worktrees() {
  547. log_info "Scanning for orphaned sandbox worktrees..."
  548. local found=0
  549. while IFS= read -r line; do
  550. if [[ "$line" == *"$WORKTREE_PREFIX"* ]]; then
  551. log_info " Found: $line"
  552. found=$((found+1))
  553. fi
  554. done < <(git worktree list 2>/dev/null)
  555. if [[ $found -eq 0 ]]; then
  556. log_info "No sandbox worktrees found"
  557. return 0
  558. fi
  559. log_info "Pruning worktrees..."
  560. git worktree prune 2>/dev/null || true
  561. # Clean up orphaned branches
  562. local branches
  563. branches=$(git branch --list "${WORKTREE_PREFIX}*" 2>/dev/null)
  564. while IFS= read -r branch; do
  565. branch=$(echo "$branch" | tr -d '* ')
  566. if [[ -n "$branch" ]]; then
  567. log_info " Removing branch: $branch"
  568. git branch -D "$branch" 2>/dev/null || true
  569. fi
  570. done <<< "$branches"
  571. rm -f "$WORKTREE_STATE_FILE" 2>/dev/null
  572. log_success "Cleanup complete"
  573. }
  574. #===============================================================================
  575. # Docker Desktop Sandbox (microVM-based isolation via Docker Desktop 4.58+)
  576. #===============================================================================
  577. #
  578. # v8.1 DEPRECATION SPLIT (binary-hosting is legacy; isolation is NOT):
  579. # This path creates a microVM from Docker Desktop's "claude" sandbox TEMPLATE,
  580. # which bakes the `claude` CLI binary into the VM so the agent can run inside it.
  581. # With v8's SDK route, the judge/text sites (and, under LOKI_SDK_MODE, the whole
  582. # engine) run pure-HTTPS with only ANTHROPIC_API_KEY -- NO in-VM `claude` binary
  583. # is needed. So the *binary-hosting* purpose of this template is obsolete.
  584. #
  585. # What is NOT obsolete, and matters MORE with an in-process SDK: isolating
  586. # untrusted CODE EXECUTION (npm install / build / test / the generated app).
  587. # In-process SDK judging runs at HOST privilege, so wrapping the code-exec in a
  588. # container/VM is still the right posture. The forward path is the existing
  589. # `start_sandbox` container/worktree isolation (the automatic fallback at the
  590. # `docker sandbox create` failure below); a future release ships a lightweight
  591. # `bun + SDK` sandbox image with no bundled agent binary.
  592. #
  593. # This release: annotation + a one-shot deprecation notice on selection only. No
  594. # behavior change, no removal. See tests/test-sandbox-deprecation.sh.
  595. # Desktop sandbox name follows same pattern as container name
  596. DESKTOP_SANDBOX_NAME="$CONTAINER_NAME"
  597. # Emit a one-shot deprecation notice for the microVM binary-hosting path. Names
  598. # the binary-hosting only and states explicitly that ISOLATION is retained (so it
  599. # is never misread as "the sandbox is going away"). Best-effort + non-blocking:
  600. # telemetry must never abort a sandbox start. Mirrors the cli_command_deprecated
  601. # pattern (event type/source/action + a human log line). Opt out with
  602. # LOKI_SANDBOX_DEPRECATION_QUIET=1.
  603. _desktop_deprecation_notice() {
  604. [ "${LOKI_SANDBOX_DEPRECATION_QUIET:-0}" = "1" ] && return 0
  605. log_warn "DEPRECATED: the Docker Desktop microVM 'claude' template hosts the"
  606. log_warn " claude BINARY in-VM, which the v8 SDK route no longer needs."
  607. log_warn " Code-execution ISOLATION is RETAINED via the container/worktree"
  608. log_warn " sandbox (unchanged); only the binary-hosting template is legacy."
  609. # Best-effort telemetry (never abort the start on a telemetry miss).
  610. if [ -f "$SKILL_DIR/events/emit.sh" ]; then
  611. bash "$SKILL_DIR/events/emit.sh" sandbox cli sandbox_microvm_binary_deprecated \
  612. isolation_retained=true path=docker-desktop >/dev/null 2>&1 || true
  613. fi
  614. return 0
  615. }
  616. # Install provider CLI inside sandbox if not pre-installed
  617. _desktop_install_provider_cli() {
  618. local sandbox_name="$1" provider="$2"
  619. case "$provider" in
  620. opencode)
  621. if ! docker sandbox exec "$sandbox_name" which opencode &>/dev/null 2>&1; then
  622. log_info "Installing opencode in sandbox (one-time)..."
  623. docker sandbox exec "$sandbox_name" npm install -g opencode-ai 2>&1 | tail -1
  624. fi
  625. ;;
  626. # claude is pre-installed in the sandbox template
  627. esac
  628. }
  629. # Build environment variable args for docker sandbox exec
  630. # Uses printf %q to escape values and prevent shell expansion corruption
  631. # (API keys can contain $, !, and other special characters)
  632. _desktop_build_env_args() {
  633. DESKTOP_ENV_ARGS=()
  634. local _escaped
  635. if [[ -n "${ANTHROPIC_API_KEY:-}" ]]; then
  636. printf -v _escaped '%s' "$ANTHROPIC_API_KEY"
  637. DESKTOP_ENV_ARGS+=("-e" "ANTHROPIC_API_KEY=$_escaped")
  638. fi
  639. if [[ -n "${OPENAI_API_KEY:-}" ]]; then
  640. printf -v _escaped '%s' "$OPENAI_API_KEY"
  641. DESKTOP_ENV_ARGS+=("-e" "OPENAI_API_KEY=$_escaped")
  642. fi
  643. if [[ -n "${GOOGLE_API_KEY:-}" ]]; then
  644. printf -v _escaped '%s' "$GOOGLE_API_KEY"
  645. DESKTOP_ENV_ARGS+=("-e" "GOOGLE_API_KEY=$_escaped")
  646. fi
  647. if [[ -n "${GITHUB_TOKEN:-}" ]]; then
  648. printf -v _escaped '%s' "$GITHUB_TOKEN"
  649. DESKTOP_ENV_ARGS+=("-e" "GITHUB_TOKEN=$_escaped")
  650. fi
  651. if [[ -n "${GH_TOKEN:-}" ]]; then
  652. printf -v _escaped '%s' "$GH_TOKEN"
  653. DESKTOP_ENV_ARGS+=("-e" "GH_TOKEN=$_escaped")
  654. fi
  655. # Forward all LOKI_* env vars via --env-file to avoid shell expansion issues.
  656. # Security: LOKI_* values may include secrets, so the env-file is created with
  657. # mktemp (unpredictable name, no symlink race) and restricted to mode 600 before
  658. # anything is written into it. The desktop path consumes this file via a
  659. # foreground/blocking "docker sandbox exec" (see start_docker_desktop_sandbox),
  660. # so the file is no longer needed once the script exits -- an EXIT trap is the
  661. # correct cleanup point here. The trap value is baked in (double quotes) so the
  662. # local path is captured now rather than re-evaluated at exit when it is out of
  663. # scope. We append our removal so we do not clobber any existing INT/TERM trap.
  664. local _env_file
  665. _env_file="$(mktemp "${TMPDIR:-/tmp}/loki-sandbox-env.XXXXXX")"
  666. chmod 600 "$_env_file"
  667. local _has_loki_vars=false
  668. local var
  669. while IFS= read -r var; do
  670. if [[ -n "$var" ]]; then
  671. printf '%s=%s\n' "$var" "${!var}" >> "$_env_file"
  672. _has_loki_vars=true
  673. fi
  674. done < <(compgen -v LOKI_ 2>/dev/null || true)
  675. if [[ "$_has_loki_vars" == "true" ]] && [[ -f "$_env_file" ]]; then
  676. DESKTOP_ENV_ARGS+=("--env-file" "$_env_file")
  677. # shellcheck disable=SC2064
  678. trap "rm -f -- '$_env_file'" EXIT
  679. else
  680. # No LOKI_ vars were forwarded; the file is unused, remove it immediately.
  681. rm -f -- "$_env_file"
  682. fi
  683. }
  684. start_docker_desktop_sandbox() {
  685. local prd_path="${1:-}"
  686. local provider="${LOKI_PROVIDER:-claude}"
  687. validate_project_dir || return 1
  688. warn_missing_api_keys "$provider"
  689. # v8.1: one-shot deprecation notice for the microVM binary-hosting path
  690. # (isolation is retained; only the bundled-binary template is legacy).
  691. _desktop_deprecation_notice
  692. # Check if sandbox already exists
  693. local sandbox_exists=false
  694. if docker sandbox ls 2>/dev/null | grep -q "$DESKTOP_SANDBOX_NAME"; then
  695. sandbox_exists=true
  696. fi
  697. if [[ "$sandbox_exists" == "false" ]]; then
  698. log_info "Creating Docker Desktop Sandbox (microVM)..."
  699. log_info " Workspace: $PROJECT_DIR"
  700. log_info " Agent: claude (provider: $provider)"
  701. docker sandbox create --name "$DESKTOP_SANDBOX_NAME" claude "$PROJECT_DIR" || {
  702. log_error "Failed to create Docker Desktop Sandbox"
  703. log_info "Falling back to Docker container sandbox..."
  704. start_sandbox "$@"
  705. return $?
  706. }
  707. log_success "Sandbox created: $DESKTOP_SANDBOX_NAME"
  708. else
  709. log_info "Using existing sandbox: $DESKTOP_SANDBOX_NAME"
  710. fi
  711. # Install provider CLI if needed (opencode is not pre-installed)
  712. _desktop_install_provider_cli "$DESKTOP_SANDBOX_NAME" "$provider"
  713. # Build environment variable args
  714. _desktop_build_env_args
  715. # Build loki command - use SKILL_DIR (the loki-mode install directory) rather than
  716. # PROJECT_DIR, which is the user's project and may not contain autonomy/run.sh
  717. local loki_cmd="bash ${SKILL_DIR}/autonomy/run.sh"
  718. if [[ -n "$prd_path" ]]; then
  719. local abs_prd
  720. abs_prd=$(cd "$(dirname "$prd_path")" && pwd)/$(basename "$prd_path")
  721. loki_cmd="$loki_cmd $abs_prd"
  722. fi
  723. loki_cmd="LOKI_PROVIDER=$provider LOKI_SANDBOX_MODE=true LOKI_DASHBOARD=false $loki_cmd"
  724. log_info ""
  725. log_info "Starting Loki Mode in Docker Desktop Sandbox..."
  726. log_info " Isolation: microVM (hypervisor-based)"
  727. log_info " Provider: $provider"
  728. log_info " Sandbox: $DESKTOP_SANDBOX_NAME"
  729. log_info " Workspace: $PROJECT_DIR (synced at same path)"
  730. log_info ""
  731. log_info " Private Docker daemon available inside sandbox"
  732. log_info " Dashboard: run 'loki dashboard' on host (reads synced .loki/ files)"
  733. log_info ""
  734. log_info "Commands:"
  735. log_info " loki sandbox status - Check sandbox status"
  736. log_info " loki sandbox shell - Open shell in sandbox"
  737. log_info " loki sandbox stop - Stop sandbox"
  738. log_info ""
  739. # Execute loki inside sandbox (use -it only when stdin is a terminal)
  740. local tty_args="-i"
  741. if [ -t 0 ]; then
  742. tty_args="-it"
  743. fi
  744. docker sandbox exec $tty_args \
  745. -w "$PROJECT_DIR" \
  746. ${DESKTOP_ENV_ARGS[@]+"${DESKTOP_ENV_ARGS[@]}"} \
  747. "$DESKTOP_SANDBOX_NAME" \
  748. bash -c "$loki_cmd"
  749. }
  750. # shellcheck disable=SC2120
  751. stop_docker_desktop_sandbox() {
  752. local remove="${1:-false}"
  753. if ! docker sandbox ls 2>/dev/null | grep -q "$DESKTOP_SANDBOX_NAME"; then
  754. log_error "Sandbox not found: $DESKTOP_SANDBOX_NAME"
  755. return 1
  756. fi
  757. # Signal loki to stop gracefully
  758. docker sandbox exec "$DESKTOP_SANDBOX_NAME" \
  759. bash -c "touch ${PROJECT_DIR}/.loki/STOP 2>/dev/null" 2>/dev/null || true
  760. log_info "Stopping sandbox: $DESKTOP_SANDBOX_NAME"
  761. docker sandbox stop "$DESKTOP_SANDBOX_NAME" 2>/dev/null || true
  762. if [[ "$remove" == "true" ]]; then
  763. log_info "Removing sandbox: $DESKTOP_SANDBOX_NAME"
  764. docker sandbox rm "$DESKTOP_SANDBOX_NAME" 2>/dev/null || true
  765. fi
  766. log_success "Sandbox stopped"
  767. }
  768. docker_desktop_sandbox_status() {
  769. local found=false
  770. while IFS= read -r line; do
  771. if echo "$line" | grep -q "$DESKTOP_SANDBOX_NAME"; then
  772. found=true
  773. echo "$line"
  774. fi
  775. done < <(docker sandbox ls 2>/dev/null)
  776. if [[ "$found" == "false" ]]; then
  777. log_info "No Docker Desktop Sandbox found for this project"
  778. log_info "Expected name: $DESKTOP_SANDBOX_NAME"
  779. return 1
  780. fi
  781. # If sandbox is running, get loki status
  782. if docker sandbox ls 2>/dev/null | grep "$DESKTOP_SANDBOX_NAME" | grep -q "running"; then
  783. echo ""
  784. docker sandbox exec -w "$PROJECT_DIR" "$DESKTOP_SANDBOX_NAME" \
  785. bash -c "bash ${PROJECT_DIR}/autonomy/loki status" 2>/dev/null || true
  786. fi
  787. }
  788. docker_desktop_sandbox_shell() {
  789. if ! docker sandbox ls 2>/dev/null | grep "$DESKTOP_SANDBOX_NAME" | grep -q "running"; then
  790. log_error "Sandbox is not running: $DESKTOP_SANDBOX_NAME"
  791. log_info "Start it with: loki sandbox start"
  792. return 1
  793. fi
  794. log_info "Opening shell in Docker Desktop Sandbox..."
  795. docker sandbox exec -it -w "$PROJECT_DIR" "$DESKTOP_SANDBOX_NAME" bash
  796. }
  797. docker_desktop_sandbox_logs() {
  798. local lines="${1:-100}"
  799. if ! docker sandbox ls 2>/dev/null | grep -q "$DESKTOP_SANDBOX_NAME"; then
  800. log_error "Sandbox not found: $DESKTOP_SANDBOX_NAME"
  801. return 1
  802. fi
  803. docker sandbox exec -w "$PROJECT_DIR" "$DESKTOP_SANDBOX_NAME" \
  804. bash -c "cat .loki/logs/*.log 2>/dev/null | tail -${lines}" || \
  805. log_warn "No logs found in .loki/logs/"
  806. }
  807. docker_desktop_sandbox_prompt() {
  808. local message="${1:-}"
  809. if [[ -z "$message" ]]; then
  810. log_error "Usage: loki sandbox prompt <message>"
  811. return 1
  812. fi
  813. if ! docker sandbox ls 2>/dev/null | grep "$DESKTOP_SANDBOX_NAME" | grep -q "running"; then
  814. log_error "Sandbox is not running"
  815. return 1
  816. fi
  817. _desktop_build_env_args
  818. docker sandbox exec -w "$PROJECT_DIR" \
  819. ${DESKTOP_ENV_ARGS[@]+"${DESKTOP_ENV_ARGS[@]}"} \
  820. "$DESKTOP_SANDBOX_NAME" \
  821. bash -c "printf '%s\n' \"\$1\" > ${PROJECT_DIR}/.loki/HUMAN_INPUT.md" -- "$message"
  822. log_success "Prompt sent to sandbox"
  823. }
  824. docker_desktop_sandbox_run() {
  825. local cmd="${*}"
  826. if [[ -z "$cmd" ]]; then
  827. log_error "Usage: loki sandbox run <command>"
  828. return 1
  829. fi
  830. if ! docker sandbox ls 2>/dev/null | grep "$DESKTOP_SANDBOX_NAME" | grep -q "running"; then
  831. log_error "Sandbox is not running"
  832. return 1
  833. fi
  834. _desktop_build_env_args
  835. docker sandbox exec -w "$PROJECT_DIR" \
  836. ${DESKTOP_ENV_ARGS[@]+"${DESKTOP_ENV_ARGS[@]}"} \
  837. "$DESKTOP_SANDBOX_NAME" \
  838. bash -c "$cmd"
  839. }
  840. #===============================================================================
  841. # Docker Container Sandbox (standard Docker - fallback from Docker Desktop)
  842. #===============================================================================
  843. # Resolve Docker credential mount presets
  844. # Reads from: .loki/config/settings.json dockerMounts, LOKI_DOCKER_MOUNTS env var
  845. # Returns: string of -v and -e flags for docker run
  846. resolve_docker_mounts() {
  847. RESOLVED_MOUNTS=()
  848. # Read configured presets (JSON array of strings)
  849. local presets_json=""
  850. if [[ -f "${PROJECT_DIR}/.loki/config/settings.json" ]]; then
  851. presets_json=$(python3 -c "
  852. import json, sys
  853. try:
  854. with open(sys.argv[1] + '/.loki/config/settings.json') as f:
  855. print(json.dumps(json.load(f).get('dockerMounts', [])))
  856. except: print('[]')
  857. " "${PROJECT_DIR}" 2>/dev/null || echo "[]")
  858. fi
  859. # Override with env var if set
  860. if [[ -n "${LOKI_DOCKER_MOUNTS:-}" ]]; then
  861. presets_json="$LOKI_DOCKER_MOUNTS"
  862. fi
  863. # Parse and resolve
  864. if [[ "$presets_json" != "[]" ]] && [[ "$presets_json" != "" ]]; then
  865. local preset_names
  866. preset_names=$(python3 -c "
  867. import json, sys
  868. try:
  869. data = json.loads(sys.argv[1])
  870. if isinstance(data, list):
  871. for item in data:
  872. print(item)
  873. except: pass
  874. " "$presets_json" 2>/dev/null || echo "")
  875. local name
  876. while IFS= read -r name; do
  877. [[ -z "$name" ]] && continue
  878. local preset_value
  879. preset_value="$(_get_mount_preset "$name")"
  880. if [[ -z "$preset_value" ]]; then
  881. log_warn "Unknown Docker mount preset: $name"
  882. continue
  883. fi
  884. IFS=':' read -ra parts <<< "$preset_value"
  885. local host_path="${parts[0]}"
  886. local container_path="${parts[1]}"
  887. local mode="${parts[2]:-ro}"
  888. # Safe tilde expansion (no eval) - SC2088 disabled: tilde is intentionally
  889. # treated as a literal string here for safe expansion without eval
  890. # shellcheck disable=SC2088
  891. if [[ "$host_path" == "~/"* ]]; then
  892. host_path="$HOME/${host_path#\~/}"
  893. elif [[ "$host_path" == "~" ]]; then
  894. host_path="$HOME"
  895. fi
  896. # Only mount if host path exists
  897. if [[ -e "$host_path" ]]; then
  898. RESOLVED_MOUNTS+=("-v" "${host_path}:${container_path}:${mode}")
  899. log_info " Mount preset [$name]: $host_path -> $container_path ($mode)"
  900. fi
  901. # Add associated env vars
  902. local env_list
  903. env_list="$(_get_env_preset "$name")"
  904. if [[ -n "$env_list" ]]; then
  905. IFS=',' read -ra env_names <<< "$env_list"
  906. local env_name
  907. for env_name in "${env_names[@]}"; do
  908. if [[ "$env_name" == *"*" ]]; then
  909. # Wildcard: pass all matching env vars
  910. local prefix="${env_name%\*}"
  911. while IFS='=' read -r key val; do
  912. [[ "$key" == "$prefix"* ]] && [[ -n "$val" ]] && {
  913. RESOLVED_MOUNTS+=("-e" "$key")
  914. log_info " Env wildcard [$name]: passing $key"
  915. }
  916. done < <(env)
  917. elif [[ -n "${!env_name:-}" ]]; then
  918. RESOLVED_MOUNTS+=("-e" "$env_name")
  919. fi
  920. done
  921. fi
  922. done <<< "$preset_names"
  923. fi
  924. }
  925. #-------------------------------------------------------------------------------
  926. # Opt-in egress allowlist (LOKI_SANDBOX_ALLOW_HOSTS, comma-separated hostnames).
  927. # Unset: nothing below runs and the docker run argv is unchanged. Set: the sandbox
  928. # joins an --internal network (no route out) and reaches the world only through a
  929. # filtering proxy sidecar that permits the listed hosts and refuses any target
  930. # resolving to link-local, loopback or private ranges.
  931. #-------------------------------------------------------------------------------
  932. SANDBOX_ALLOW_HOSTS="${LOKI_SANDBOX_ALLOW_HOSTS:-}"
  933. EGRESS_PROXY_PORT=3128
  934. EGRESS_BLOCKED_CIDRS="169.254.0.0/16 fe80::/10 fec0::/10 100.64.0.0/10 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 127.0.0.0/8 ::1/128 fc00::/7"
  935. # Print the validated allowlist one host per line. Rejects IP literals and
  936. # anything that is not a plain hostname, so a literal metadata IP can never be
  937. # allowlisted. Returns 1 on an invalid entry or an empty list.
  938. _sandbox_egress_hosts() {
  939. local raw="${1:-$SANDBOX_ALLOW_HOSTS}" h count=0
  940. local -a parts
  941. IFS=',' read -ra parts <<< "$raw"
  942. for h in "${parts[@]}"; do
  943. h="${h//[[:space:]]/}"
  944. [[ -z "$h" ]] && continue
  945. if [[ ! "$h" =~ ^[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?)+$ ]] \
  946. || [[ "$h" =~ ^[0-9.]+$ ]]; then
  947. log_error "LOKI_SANDBOX_ALLOW_HOSTS: invalid host (hostnames only, no IPs): $h"
  948. return 1
  949. fi
  950. printf '%s\n' "$h" | tr '[:upper:]' '[:lower:]'
  951. count=$((count+1))
  952. done
  953. [[ $count -gt 0 ]]
  954. }
  955. # Filtering proxy (CONNECT and plain HTTP). Decision function is check_target so
  956. # tests can exercise it with an injected resolver; no network needed.
  957. _sandbox_egress_proxy_source() {
  958. cat <<'PYEOF'
  959. import ipaddress, os, socket, sys, threading
  960. ALLOW = {h.strip().lower() for h in os.environ.get("ALLOW_HOSTS", "").split(",") if h.strip()}
  961. BLOCKED = [ipaddress.ip_network(c) for c in os.environ.get("BLOCKED_CIDRS", "").split()]
  962. def ip_blocked(ip):
  963. a = ipaddress.ip_address(ip)
  964. if getattr(a, "ipv4_mapped", None):
  965. a = a.ipv4_mapped
  966. return (not a.is_global or getattr(a, "is_site_local", False) or a.is_private or a.is_link_local or a.is_loopback or a.is_unspecified
  967. or a.is_multicast or a.is_reserved or any(a in n for n in BLOCKED))
  968. def check_target(host, resolve=None, port=443):
  969. """Return (ok, reason). Allowed only if host is listed, port is 80 or 443 and every resolved IP is global."""
  970. if port not in (80, 443):
  971. return False, "port not allowed"
  972. host = host.strip().lower().rstrip(".")
  973. if host not in ALLOW:
  974. return False, "host not in allowlist"
  975. resolve = resolve or (lambda h: [i[4][0] for i in socket.getaddrinfo(h, None)])
  976. try:
  977. ips = resolve(host)
  978. except Exception:
  979. return False, "resolution failed"
  980. if not ips:
  981. return False, "no address"
  982. for ip in ips:
  983. if ip_blocked(ip):
  984. return False, "resolves to blocked range " + ip
  985. return True, ips[0]
  986. def pipe(a, b):
  987. try:
  988. while True:
  989. d = a.recv(65536)
  990. if not d:
  991. break
  992. b.sendall(d)
  993. except OSError:
  994. pass
  995. finally:
  996. for s in (a, b):
  997. try:
  998. s.shutdown(socket.SHUT_RDWR)
  999. except OSError:
  1000. pass
  1001. def handle(c):
  1002. try:
  1003. buf = b""
  1004. while b"\r\n\r\n" not in buf and len(buf) < 65536:
  1005. d = c.recv(4096)
  1006. if not d:
  1007. return
  1008. buf += d
  1009. head, _, rest = buf.partition(b"\r\n\r\n")
  1010. line = head.split(b"\r\n")[0].decode("latin1").split()
  1011. if len(line) < 3:
  1012. return
  1013. method, target = line[0], line[1]
  1014. if method.upper() == "CONNECT":
  1015. host, _, port = target.rpartition(":")
  1016. port = int(port or 443)
  1017. rest_out = b""
  1018. else:
  1019. u = target.split("://", 1)[-1].split("/", 1)[0]
  1020. host, _, port = u.partition(":")
  1021. port = int(port or 80)
  1022. rest_out = buf
  1023. ok, info = check_target(host, port=port)
  1024. if not ok:
  1025. c.sendall(b"HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n")
  1026. return
  1027. up = socket.create_connection((info, port), timeout=20)
  1028. if method.upper() == "CONNECT":
  1029. c.sendall(b"HTTP/1.1 200 Connection established\r\n\r\n")
  1030. else:
  1031. up.sendall(rest_out)
  1032. t = threading.Thread(target=pipe, args=(up, c), daemon=True)
  1033. t.start()
  1034. pipe(c, up)
  1035. except Exception:
  1036. pass
  1037. finally:
  1038. c.close()
  1039. def main():
  1040. s = socket.socket()
  1041. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1042. s.bind(("0.0.0.0", int(os.environ.get("PROXY_PORT", "3128"))))
  1043. s.listen(128)
  1044. while True:
  1045. conn, _ = s.accept()
  1046. threading.Thread(target=handle, args=(conn,), daemon=True).start()
  1047. if __name__ == "__main__":
  1048. main()
  1049. PYEOF
  1050. }
  1051. # Echo the docker run argv fragments the sandbox container needs when the
  1052. # allowlist is on: one token per line (network override first, then proxy env).
  1053. _sandbox_egress_container_args() {
  1054. local net="$1" proxy="http://${CONTAINER_NAME}-egress:${EGRESS_PROXY_PORT}"
  1055. printf '%s\n' "--network=$net"
  1056. local v
  1057. for v in HTTP_PROXY HTTPS_PROXY http_proxy https_proxy; do
  1058. printf '%s\n' "--env" "$v=$proxy"
  1059. done
  1060. printf '%s\n' "--env" "NO_PROXY=localhost,127.0.0.1"
  1061. }
  1062. # Echo the docker run argv for the proxy sidecar (test-visible, no side effects).
  1063. _sandbox_egress_proxy_args() {
  1064. local script="$1" hosts="$2"
  1065. printf '%s\n' run --detach --name "${CONTAINER_NAME}-egress" \
  1066. --network=bridge --cap-drop=ALL --security-opt=no-new-privileges:true \
  1067. --read-only --pids-limit=128 --memory=256m \
  1068. --env "ALLOW_HOSTS=$hosts" --env "BLOCKED_CIDRS=$EGRESS_BLOCKED_CIDRS" \
  1069. --env "PROXY_PORT=$EGRESS_PROXY_PORT" \
  1070. --volume "$script:/egress-proxy.py:ro" \
  1071. --entrypoint python3 "$SANDBOX_IMAGE" /egress-proxy.py
  1072. }
  1073. # Create the internal network and proxy sidecar. Echoes the internal network name.
  1074. _sandbox_egress_up() {
  1075. local hosts="$1" net="${CONTAINER_NAME}-internal" script
  1076. local dir="${LOKI_RUN_TMP:-}" rec troot
  1077. if [[ -z "$dir" ]]; then
  1078. # Resolve the temp root first (macOS TMPDIR ends in a slash), so the dir is T/loki-egress.X and matches at teardown.
  1079. # mktemp -d picks an unpredictable name with mode 700, so a planted symlink in a shared temp dir is never followed.
  1080. troot="$(cd "${TMPDIR:-/tmp}" 2>/dev/null && pwd -P)" || return 1
  1081. dir="$(mktemp -d "${troot}/loki-egress.XXXXXXXX")" || return 1
  1082. rec="$(_sandbox_egress_record)"
  1083. if ! { mkdir -p "$(dirname "$rec")" && chmod 700 "$(dirname "$rec")" && printf '%s\n' "$dir" > "$rec"; }; then rm -rf -- "$dir"; return 1; fi
  1084. fi
  1085. chmod 700 "$dir" || return 1
  1086. script="$dir/egress-proxy.py"
  1087. _sandbox_egress_proxy_source > "$script"
  1088. docker rm -f "${CONTAINER_NAME}-egress" >/dev/null 2>&1 || true
  1089. docker network rm "$net" >/dev/null 2>&1 || true
  1090. docker network create --internal "$net" >/dev/null || return 1
  1091. local -a pargs=()
  1092. while IFS= read -r line; do pargs+=("$line"); done < <(_sandbox_egress_proxy_args "$script" "$(echo "$hosts" | paste -sd, -)")
  1093. docker "${pargs[@]}" >/dev/null || return 1
  1094. docker network connect --alias "${CONTAINER_NAME}-egress" "$net" "${CONTAINER_NAME}-egress" >/dev/null || return 1
  1095. printf '%s\n' "$net"
  1096. }
  1097. # Host-side state file naming the fallback egress dir for this container. It lives outside PROJECT_DIR on purpose:
  1098. # the project is mounted rw at /workspace, so a record there could be forged by the agent.
  1099. _sandbox_egress_record() { printf '%s\n' "${LOKI_SANDBOX_STATE_DIR:-${HOME}/.loki/sandbox-state}/egress-${CONTAINER_NAME}.dir"; }
  1100. _sandbox_egress_down() {
  1101. local rec fb root
  1102. rec="$(_sandbox_egress_record)"
  1103. if [[ -f "$rec" && ! -L "$rec" ]]; then
  1104. IFS= read -r fb < "$rec" || fb=""
  1105. root="$(cd "${TMPDIR:-/tmp}" 2>/dev/null && pwd -P)" || root=""
  1106. # Remove only a real, own, unlinked loki-egress.XXXXXXXX directory that sits directly in the resolved temp root.
  1107. if [[ -n "$root" && "$(dirname -- "$fb")" == "$root" ]] \
  1108. && [[ "$(basename -- "$fb")" =~ ^loki-egress\.[A-Za-z0-9]{8}$ ]] \
  1109. && [[ -d "$fb" && ! -L "$fb" && -O "$fb" && "$(cd "$fb" 2>/dev/null && pwd -P)" == "$fb" ]]; then
  1110. rm -rf -- "$fb"
  1111. fi
  1112. rm -f -- "$rec"
  1113. fi
  1114. docker rm -f "${CONTAINER_NAME}-egress" >/dev/null 2>&1 || true
  1115. docker network rm "${CONTAINER_NAME}-internal" >/dev/null 2>&1 || true
  1116. }
  1117. start_sandbox() {
  1118. # Parse arguments: extract flags before positional args
  1119. local prd_path=""
  1120. local provider="${LOKI_PROVIDER:-claude}"
  1121. local no_mounts=false
  1122. local -a custom_mounts=()
  1123. while [[ $# -gt 0 ]]; do
  1124. case "$1" in
  1125. --mount)
  1126. if [[ -n "${2:-}" ]]; then
  1127. custom_mounts+=("$2")
  1128. shift 2
  1129. else
  1130. log_error "--mount requires HOST:CONTAINER:MODE argument"
  1131. return 1
  1132. fi
  1133. ;;
  1134. --no-mounts)
  1135. no_mounts=true
  1136. shift
  1137. ;;
  1138. --provider)
  1139. if [[ -n "${2:-}" ]]; then
  1140. provider="$2"
  1141. shift 2
  1142. else
  1143. shift
  1144. fi
  1145. ;;
  1146. *)
  1147. # First non-flag argument is the PRD path
  1148. if [[ -z "$prd_path" ]]; then
  1149. prd_path="$1"
  1150. fi
  1151. shift
  1152. ;;
  1153. esac
  1154. done
  1155. # Set up signal handler to cleanup on Ctrl+C
  1156. trap cleanup_container INT TERM
  1157. check_docker
  1158. validate_project_dir || return 1
  1159. ensure_image
  1160. # Check port availability (unified dashboard/API port)
  1161. if ! check_port_available "$DASHBOARD_PORT"; then
  1162. log_error "Port $DASHBOARD_PORT is already in use"
  1163. log_info "Set LOKI_DASHBOARD_PORT to use a different port"
  1164. return 1
  1165. fi
  1166. # Warn about missing API keys
  1167. warn_missing_api_keys "$provider"
  1168. # Warn about network=none implications
  1169. if [[ "$SANDBOX_NETWORK" == "none" ]]; then
  1170. log_warn "Network disabled (--network=none)"
  1171. log_warn " - Git remote operations will fail"
  1172. log_warn " - Package installations will fail"
  1173. log_warn " - API calls to AI providers will fail"
  1174. fi
  1175. # Check if already running
  1176. if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1177. log_warn "Sandbox already running: $CONTAINER_NAME"
  1178. log_info "Use 'loki sandbox status' to check or 'loki sandbox stop' to stop"
  1179. return 0
  1180. fi
  1181. # Clean up any stopped container with same name
  1182. docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
  1183. local network_arg="--network=$SANDBOX_NETWORK" egress_args=() egress_ok=1
  1184. if [[ -n "$SANDBOX_ALLOW_HOSTS" ]]; then
  1185. local egress_hosts egress_net
  1186. egress_hosts=$(_sandbox_egress_hosts) || return 1
  1187. if [[ "$SANDBOX_NETWORK" != "bridge" ]]; then
  1188. log_error "LOKI_SANDBOX_ALLOW_HOSTS requires LOKI_SANDBOX_NETWORK=bridge (got $SANDBOX_NETWORK)"
  1189. return 1
  1190. fi
  1191. egress_net=$(_sandbox_egress_up "$egress_hosts") || { _sandbox_egress_down; log_error "Failed to start egress proxy"; return 1; }
  1192. # Any return after this point (refusal, failed docker run) must tear the proxy and network down.
  1193. egress_ok=0
  1194. trap '[[ "${egress_ok:-1}" == 1 ]] || _sandbox_egress_down; trap - RETURN' RETURN
  1195. while IFS= read -r line; do egress_args+=("$line"); done < <(_sandbox_egress_container_args "$egress_net")
  1196. network_arg="${egress_args[0]}"
  1197. fi
  1198. log_info "Starting sandbox container..."
  1199. log_info " Image: $SANDBOX_IMAGE"
  1200. log_info " Project: $PROJECT_DIR"
  1201. log_info " Provider: $provider"
  1202. log_info " Network: $SANDBOX_NETWORK"
  1203. log_info " CPUs: $SANDBOX_CPUS"
  1204. log_info " Memory: $SANDBOX_MEMORY"
  1205. # Build docker run command
  1206. local docker_args=(
  1207. "run"
  1208. "--name" "$CONTAINER_NAME"
  1209. "--detach"
  1210. "--interactive"
  1211. "--tty"
  1212. # Resource limits
  1213. "--cpus=$SANDBOX_CPUS"
  1214. "--memory=$SANDBOX_MEMORY"
  1215. "--memory-swap=$SANDBOX_MEMORY" # Disable swap
  1216. "--pids-limit=256"
  1217. # Security hardening
  1218. "--security-opt=no-new-privileges:true"
  1219. "--cap-drop=ALL"
  1220. "--cap-add=CHOWN"
  1221. # SETUID/SETGID intentionally omitted: container runs as non-root (UID 1000)
  1222. # and should not be able to change UID/GID, which would undermine isolation
  1223. # Network
  1224. "$network_arg"
  1225. )
  1226. if [[ ${#egress_args[@]} -gt 1 ]]; then
  1227. docker_args+=("${egress_args[@]:1}")
  1228. log_info " Egress: allowlist only ($(echo "$egress_hosts" | paste -sd, -)); private and link-local ranges blocked"
  1229. fi
  1230. # Add seccomp profile if available
  1231. local seccomp_profile="$SKILL_DIR/autonomy/seccomp-sandbox.json"
  1232. if [[ -f "$seccomp_profile" ]]; then
  1233. docker_args+=("--security-opt" "seccomp=$seccomp_profile")
  1234. log_info " Seccomp: enabled"
  1235. fi
  1236. # A5+: workspace-mount allowlist enforcement (opt-in via LOKI_ALLOWED_PATHS).
  1237. #
  1238. # The workspace bind-mount below is where provider-driven agent file writes
  1239. # actually land (the agent writes inside /workspace, which is $PROJECT_DIR on
  1240. # the host). Unlike the custom --mount surface, this mount was previously
  1241. # always made writable regardless of LOKI_ALLOWED_PATHS, so an allowlist that
  1242. # did not include the project dir was silently ignored for the main write
  1243. # surface.
  1244. #
  1245. # When LOKI_ALLOWED_PATHS is set and the workspace itself is OUTSIDE the
  1246. # allowlist, fail closed: refuse to start rather than bind-mount it writable.
  1247. # This is genuinely enforceable because docker mount mode (rw vs ro) and which
  1248. # host paths get bound are decided HERE, before the container exists -- the
  1249. # kernel enforces the resulting mount, not a wrapper check. We refuse rather
  1250. # than auto-downgrade to :ro because a read-only workspace cannot be written
  1251. # by the agent at all and would silently produce a no-op build; refusing makes
  1252. # the misconfiguration visible. (Allowlisted extra paths are still mounted via
  1253. # the custom --mount surface above, which also enforces the allowlist.)
  1254. #
  1255. # When LOKI_ALLOWED_PATHS is empty (default), _sandbox_path_within_allowed
  1256. # returns 0 unconditionally, so this is byte-identical to prior behavior.
  1257. if ! _sandbox_path_within_allowed "$PROJECT_DIR"; then
  1258. log_error "Workspace is outside LOKI_ALLOWED_PATHS, refusing to mount it writable: $PROJECT_DIR"
  1259. log_error " Add the project directory to LOKI_ALLOWED_PATHS, or unset LOKI_ALLOWED_PATHS to disable path enforcement."
  1260. return 1
  1261. fi
  1262. # Mount project directory
  1263. if [[ "$SANDBOX_READONLY" == "true" ]]; then
  1264. docker_args+=("--volume" "$PROJECT_DIR:/workspace:ro")
  1265. # Need a writable .loki directory - copy existing state to a temp dir so we
  1266. # do not start with an empty volume (which would lose config/state).
  1267. # Security: created with mktemp -d (unpredictable name, no symlink/predictable
  1268. # race) and restricted to mode 700 since it holds a copy of the project's .loki
  1269. # state. Lifecycle: this directory is bind-mounted into a DETACHED container
  1270. # (see "--detach" in docker_args) that outlives this function, so an EXIT trap
  1271. # would delete it out from under the running container. Instead, its path is
  1272. # recorded on the container via a docker label and removed by stop_sandbox()
  1273. # right before the container is removed. The label survives across separate
  1274. # script invocations, which a $$-derived name would not.
  1275. local _loki_state_tmp
  1276. _loki_state_tmp="$(mktemp -d "${TMPDIR:-/tmp}/loki-sandbox-state.XXXXXX")"
  1277. chmod 700 "$_loki_state_tmp"
  1278. if [[ -d "$PROJECT_DIR/.loki" ]]; then
  1279. cp -a "$PROJECT_DIR/.loki/." "$_loki_state_tmp/" 2>/dev/null || true
  1280. fi
  1281. docker_args+=("--volume" "$_loki_state_tmp:/workspace/.loki:rw")
  1282. docker_args+=("--label" "loki.state_dir=$_loki_state_tmp")
  1283. else
  1284. docker_args+=("--volume" "$PROJECT_DIR:/workspace:rw")
  1285. fi
  1286. # Config self-protection: mount critical .loki/ paths as read-only
  1287. # Prevents agents from corrupting council state, config, or audit trail
  1288. if [[ -d "$PROJECT_DIR/.loki/council" ]]; then
  1289. docker_args+=("--volume" "$PROJECT_DIR/.loki/council:/workspace/.loki/council:ro")
  1290. fi
  1291. if [[ -f "$PROJECT_DIR/.loki/config.yaml" ]]; then
  1292. docker_args+=("--volume" "$PROJECT_DIR/.loki/config.yaml:/workspace/.loki/config.yaml:ro")
  1293. fi
  1294. # Mount git config (read-only) - mount to /home/loki since container runs as user loki
  1295. if [[ -f "$HOME/.gitconfig" ]]; then
  1296. docker_args+=("--volume" "$HOME/.gitconfig:/home/loki/.gitconfig:ro")
  1297. fi
  1298. # SSH agent forwarding (more secure than mounting .ssh directory)
  1299. # Only forward if SSH agent is available
  1300. if [[ -n "${SSH_AUTH_SOCK:-}" ]]; then
  1301. docker_args+=("--volume" "$SSH_AUTH_SOCK:/ssh-agent:ro")
  1302. docker_args+=("--env" "SSH_AUTH_SOCK=/ssh-agent")
  1303. elif [[ -d "$HOME/.ssh" ]]; then
  1304. # Fallback: mount only known_hosts and public keys (NOT private keys)
  1305. if [[ -f "$HOME/.ssh/known_hosts" ]]; then
  1306. docker_args+=("--volume" "$HOME/.ssh/known_hosts:/home/loki/.ssh/known_hosts:ro")
  1307. fi
  1308. log_warn "SSH agent not available. Git operations may require manual auth."
  1309. fi
  1310. # Mount GitHub CLI config (read-only) - mount to /home/loki since container runs as user loki
  1311. if [[ -d "$HOME/.config/gh" ]]; then
  1312. docker_args+=("--volume" "$HOME/.config/gh:/home/loki/.config/gh:ro")
  1313. fi
  1314. # Apply Docker credential mount presets (additive on top of defaults above)
  1315. if [[ "$no_mounts" != "true" ]] && [[ "${LOKI_NO_DOCKER_MOUNTS:-}" != "true" ]]; then
  1316. resolve_docker_mounts
  1317. if [[ ${#RESOLVED_MOUNTS[@]} -gt 0 ]]; then
  1318. docker_args+=("${RESOLVED_MOUNTS[@]}")
  1319. fi
  1320. fi
  1321. # Apply custom --mount arguments
  1322. local custom_mount
  1323. for custom_mount in "${custom_mounts[@]+"${custom_mounts[@]}"}"; do
  1324. if [[ -n "$custom_mount" ]]; then
  1325. IFS=':' read -ra mount_parts <<< "$custom_mount"
  1326. local c_host="${mount_parts[0]:-}"
  1327. local c_container="${mount_parts[1]:-}"
  1328. local c_mode="${mount_parts[2]:-ro}"
  1329. if [[ -n "$c_host" ]] && [[ -n "$c_container" ]]; then
  1330. # Safe tilde expansion (no eval) - SC2088 disabled: intentional literal match
  1331. # shellcheck disable=SC2088
  1332. if [[ "$c_host" == "~/"* ]]; then
  1333. c_host="$HOME/${c_host#\~/}"
  1334. elif [[ "$c_host" == "~" ]]; then
  1335. c_host="$HOME"
  1336. fi
  1337. if [[ -e "$c_host" ]]; then
  1338. # A5: when LOKI_ALLOWED_PATHS is set, reject a custom mount
  1339. # whose host path is outside the allowlist. This is a write
  1340. # surface run.sh/sandbox.sh genuinely controls (we decide what
  1341. # to bind into the container), so the allowlist is enforced
  1342. # here rather than only documented.
  1343. if ! _sandbox_path_within_allowed "$c_host"; then
  1344. log_error "Custom mount host path is outside LOKI_ALLOWED_PATHS, refusing to mount: $c_host"
  1345. return 1
  1346. fi
  1347. docker_args+=("--volume" "${c_host}:${c_container}:${c_mode}")
  1348. log_info " Custom mount: $c_host -> $c_container ($c_mode)"
  1349. else
  1350. log_warn "Custom mount source does not exist: $c_host"
  1351. fi
  1352. else
  1353. log_warn "Invalid mount format: $custom_mount (expected HOST:CONTAINER[:MODE])"
  1354. fi
  1355. fi
  1356. done
  1357. # Pass API keys as environment variables (more secure than mounting files)
  1358. if [[ -n "${ANTHROPIC_API_KEY:-}" ]]; then
  1359. docker_args+=("--env" "ANTHROPIC_API_KEY")
  1360. fi
  1361. if [[ -n "${OPENAI_API_KEY:-}" ]]; then
  1362. docker_args+=("--env" "OPENAI_API_KEY")
  1363. fi
  1364. if [[ -n "${GOOGLE_API_KEY:-}" ]]; then
  1365. docker_args+=("--env" "GOOGLE_API_KEY")
  1366. fi
  1367. if [[ -n "${GITHUB_TOKEN:-}" ]]; then
  1368. docker_args+=("--env" "GITHUB_TOKEN")
  1369. fi
  1370. if [[ -n "${GH_TOKEN:-}" ]]; then
  1371. docker_args+=("--env" "GH_TOKEN")
  1372. fi
  1373. # Loki configuration
  1374. docker_args+=(
  1375. "--env" "LOKI_PROVIDER=$provider"
  1376. "--env" "LOKI_SANDBOX_MODE=true"
  1377. "--env" "LOKI_NOTIFICATIONS=false"
  1378. "--env" "LOKI_DASHBOARD=true"
  1379. )
  1380. # Expose ports
  1381. docker_args+=(
  1382. "--publish" "$API_PORT:57374"
  1383. )
  1384. # Expose additional ports for testing (e.g., LOKI_EXTRA_PORTS="3000:3000,8080:8080")
  1385. if [[ -n "${LOKI_EXTRA_PORTS:-}" ]]; then
  1386. IFS=',' read -ra EXTRA_PORTS <<< "$LOKI_EXTRA_PORTS"
  1387. for port_mapping in "${EXTRA_PORTS[@]}"; do
  1388. docker_args+=("--publish" "$port_mapping")
  1389. done
  1390. fi
  1391. # Working directory
  1392. docker_args+=("--workdir" "/workspace")
  1393. # Override entrypoint to use bash (Dockerfile has loki as entrypoint)
  1394. docker_args+=("--entrypoint" "/bin/bash")
  1395. # Image and command
  1396. docker_args+=("$SANDBOX_IMAGE")
  1397. # Build loki command
  1398. local loki_cmd="loki start"
  1399. if [[ -n "$prd_path" ]]; then
  1400. # Convert to container path (handle paths with spaces)
  1401. # macOS realpath does not support --relative-to, so use Python fallback
  1402. local relative_prd
  1403. relative_prd=$(realpath --relative-to="$PROJECT_DIR" "$prd_path" 2>/dev/null \
  1404. || python3 -c "import os.path; print(os.path.relpath('$prd_path', '$PROJECT_DIR'))" 2>/dev/null \
  1405. || basename "$prd_path")
  1406. local container_prd="/workspace/${relative_prd}"
  1407. # Quote path to handle spaces
  1408. loki_cmd="$loki_cmd \"$container_prd\""
  1409. fi
  1410. loki_cmd="$loki_cmd --provider $provider"
  1411. docker_args+=("-c" "$loki_cmd")
  1412. # Run container
  1413. local container_id
  1414. container_id=$(docker "${docker_args[@]}") || { log_error "docker run failed"; return 1; }
  1415. egress_ok=1
  1416. log_success "Sandbox started: ${container_id:0:12}"
  1417. log_info ""
  1418. log_info "Access:"
  1419. log_info " Dashboard: http://localhost:$DASHBOARD_PORT"
  1420. log_info " API: http://localhost:$API_PORT"
  1421. if [[ -n "${LOKI_EXTRA_PORTS:-}" ]]; then
  1422. log_info " Extra: $LOKI_EXTRA_PORTS"
  1423. fi
  1424. log_info ""
  1425. log_info "Commands:"
  1426. log_info " loki sandbox logs - View logs"
  1427. log_info " loki sandbox shell - Open shell in container"
  1428. log_info " loki sandbox stop - Stop sandbox"
  1429. log_info ""
  1430. log_info "Testing (when ready):"
  1431. log_info " loki sandbox phase - Check SDLC phase & testing tips"
  1432. log_info " loki sandbox test - Run tests"
  1433. log_info " loki sandbox serve - Start dev server"
  1434. log_info " loki sandbox prompt 'msg' - Send real-time prompt"
  1435. }
  1436. stop_sandbox() {
  1437. check_docker
  1438. if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1439. log_info "Stopping sandbox: $CONTAINER_NAME"
  1440. # Read the read-only-mode temp state dir path recorded as a container label
  1441. # (see start_sandbox H5 handling). It is removed once the container is gone.
  1442. local _state_dir=""
  1443. _state_dir=$(docker inspect --format '{{ index .Config.Labels "loki.state_dir" }}' "$CONTAINER_NAME" 2>/dev/null || true)
  1444. # Try graceful stop first (touch STOP file)
  1445. docker exec "$CONTAINER_NAME" touch /workspace/.loki/STOP 2>/dev/null || true
  1446. # Wait for graceful shutdown (check every second for up to 10 seconds)
  1447. local waited=0
  1448. while [ $waited -lt 10 ]; do
  1449. if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1450. log_success "Sandbox stopped gracefully"
  1451. _sandbox_egress_down
  1452. # Remove the temp state dir now that the container has stopped.
  1453. if [[ -n "$_state_dir" ]] && [[ -d "$_state_dir" ]]; then
  1454. rm -rf -- "$_state_dir" 2>/dev/null || true
  1455. fi
  1456. return 0
  1457. fi
  1458. sleep 1
  1459. waited=$((waited+1))
  1460. done
  1461. # Force stop if still running
  1462. log_info "Force stopping container..."
  1463. docker stop --time 5 "$CONTAINER_NAME" 2>/dev/null || true
  1464. docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
  1465. # Remove the temp state dir now that the container is gone.
  1466. if [[ -n "$_state_dir" ]] && [[ -d "$_state_dir" ]]; then
  1467. rm -rf -- "$_state_dir" 2>/dev/null || true
  1468. fi
  1469. _sandbox_egress_down
  1470. log_success "Sandbox stopped"
  1471. else
  1472. _sandbox_egress_down
  1473. log_warn "No running sandbox found"
  1474. fi
  1475. }
  1476. # Send a prompt/directive to the running sandbox
  1477. sandbox_prompt() {
  1478. local prompt="$*"
  1479. # Security check: prompt injection disabled by default
  1480. if [[ "$PROMPT_INJECTION_ENABLED" != "true" ]]; then
  1481. log_error "Prompt injection is disabled for security"
  1482. log_info ""
  1483. log_info "To enable, set LOKI_PROMPT_INJECTION=true"
  1484. log_info " Example: LOKI_PROMPT_INJECTION=true loki sandbox prompt 'your message'"
  1485. log_info ""
  1486. log_warn "WARNING: Only enable in trusted environments"
  1487. return 1
  1488. fi
  1489. if [[ -z "$prompt" ]]; then
  1490. log_error "Usage: loki sandbox prompt <your message>"
  1491. log_info "Example: loki sandbox prompt 'start the dev server and show me the URL'"
  1492. return 1
  1493. fi
  1494. check_docker
  1495. if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1496. log_error "Sandbox is not running. Start it first with: loki sandbox start"
  1497. return 1
  1498. fi
  1499. log_info "Sending prompt to sandbox..."
  1500. # Write to HUMAN_INPUT.md inside the container
  1501. # Use heredoc to avoid command injection via single quotes in prompt
  1502. docker exec "$CONTAINER_NAME" bash -c 'cat > /workspace/.loki/HUMAN_INPUT.md' <<< "$prompt"
  1503. log_success "Prompt sent. Loki will process it in the next iteration."
  1504. log_info ""
  1505. log_info "Watch the response with: loki sandbox logs"
  1506. log_info "Or check status with: loki sandbox status"
  1507. }
  1508. # Run a command inside the sandbox and show output
  1509. sandbox_run() {
  1510. local cmd="$*"
  1511. if [[ -z "$cmd" ]]; then
  1512. log_error "Usage: loki sandbox run <command>"
  1513. log_info "Example: loki sandbox run 'npm run dev'"
  1514. return 1
  1515. fi
  1516. # A5: defense-in-depth on the operator-supplied command. The container is the
  1517. # real boundary, but an obviously-destructive command is blocked here before
  1518. # it is dispatched into the sandbox (honors LOKI_BLOCKED_COMMANDS).
  1519. if ! _sandbox_command_allowed "$cmd"; then
  1520. return 1
  1521. fi
  1522. check_docker
  1523. if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1524. log_error "Sandbox is not running"
  1525. return 1
  1526. fi
  1527. log_info "Running command in sandbox: $cmd"
  1528. # Trust contract: $cmd is the operator's CLI argv (`loki sandbox run <cmd>`),
  1529. # and the operator is the same person who is running the sandbox container
  1530. # on their own machine. Shell metacharacters are intentionally allowed so
  1531. # the operator can run pipelines, redirects, etc. inside the sandbox -- but
  1532. # the sandbox is the security boundary, not this shell-out. We:
  1533. # 1. set the working dir via `docker exec -w` instead of `cd && $cmd`,
  1534. # so the user command cannot break out of the cd via metacharacters
  1535. # that confuse the wrapper (e.g. unmatched quotes affecting cd);
  1536. # 2. pass `--` to bash so the user string cannot be re-interpreted as a
  1537. # bash flag if it begins with `-`;
  1538. # 3. pass the user string as a separate argv element to bash -lc so it
  1539. # is treated as the script body, not concatenated into a wrapper.
  1540. docker exec -it -w /workspace "$CONTAINER_NAME" bash -lc -- "$cmd"
  1541. }
  1542. # Start a dev server inside the sandbox
  1543. sandbox_serve() {
  1544. local port="${1:-3000}"
  1545. check_docker
  1546. if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1547. log_error "Sandbox is not running"
  1548. return 1
  1549. fi
  1550. log_info "Looking for dev server configuration..."
  1551. # Detect project type and start appropriate server
  1552. local serve_cmd=""
  1553. if docker exec "$CONTAINER_NAME" test -f /workspace/package.json; then
  1554. # Check for common dev server scripts
  1555. local has_dev
  1556. has_dev=$(docker exec "$CONTAINER_NAME" jq -r '.scripts.dev // empty' /workspace/package.json 2>/dev/null)
  1557. local has_start
  1558. has_start=$(docker exec "$CONTAINER_NAME" jq -r '.scripts.start // empty' /workspace/package.json 2>/dev/null)
  1559. if [[ -n "$has_dev" ]]; then
  1560. serve_cmd="npm run dev"
  1561. elif [[ -n "$has_start" ]]; then
  1562. serve_cmd="npm start"
  1563. fi
  1564. elif docker exec "$CONTAINER_NAME" test -f /workspace/requirements.txt; then
  1565. # Python project
  1566. if docker exec "$CONTAINER_NAME" test -f /workspace/manage.py; then
  1567. serve_cmd="python manage.py runserver 0.0.0.0:$port"
  1568. elif docker exec "$CONTAINER_NAME" test -f /workspace/app.py; then
  1569. serve_cmd="python app.py"
  1570. fi
  1571. fi
  1572. if [[ -z "$serve_cmd" ]]; then
  1573. log_warn "Could not auto-detect dev server"
  1574. log_info "Try: loki sandbox run 'your-dev-command'"
  1575. return 1
  1576. fi
  1577. log_success "Starting dev server: $serve_cmd"
  1578. log_info ""
  1579. # Warn if the dev server port was not published when the container started
  1580. local port_published
  1581. port_published=$(docker port "$CONTAINER_NAME" "$port" 2>/dev/null || true)
  1582. if [[ -z "$port_published" ]]; then
  1583. log_warn "Port $port is NOT published to the host."
  1584. log_warn "The dev server will run inside the container but will not be accessible from localhost."
  1585. log_warn "To fix, restart the sandbox with: LOKI_EXTRA_PORTS='$port:$port' loki sandbox start"
  1586. log_info ""
  1587. fi
  1588. log_info "Access the app at:"
  1589. log_info " http://localhost:$port"
  1590. log_info ""
  1591. log_info "Press Ctrl+C to stop the server (sandbox continues running)"
  1592. docker exec -it "$CONTAINER_NAME" bash -c "cd /workspace && $serve_cmd"
  1593. }
  1594. # Run tests inside the sandbox
  1595. sandbox_test() {
  1596. local test_type="${1:-all}"
  1597. check_docker
  1598. if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1599. log_error "Sandbox is not running"
  1600. return 1
  1601. fi
  1602. log_info "Running tests in sandbox..."
  1603. # Detect project type and test command
  1604. local test_cmd=""
  1605. if docker exec "$CONTAINER_NAME" test -f /workspace/package.json; then
  1606. case "$test_type" in
  1607. unit)
  1608. test_cmd=$(docker exec "$CONTAINER_NAME" jq -r '.scripts["test:unit"] // .scripts.test // "npm test"' /workspace/package.json 2>/dev/null)
  1609. ;;
  1610. integration)
  1611. test_cmd=$(docker exec "$CONTAINER_NAME" jq -r '.scripts["test:integration"] // empty' /workspace/package.json 2>/dev/null)
  1612. [[ -z "$test_cmd" ]] && test_cmd="npm run test:integration"
  1613. ;;
  1614. e2e)
  1615. if docker exec "$CONTAINER_NAME" test -d /workspace/node_modules/.bin/playwright; then
  1616. test_cmd="npx playwright test"
  1617. elif docker exec "$CONTAINER_NAME" test -d /workspace/node_modules/.bin/cypress; then
  1618. test_cmd="npx cypress run"
  1619. else
  1620. test_cmd=$(docker exec "$CONTAINER_NAME" jq -r '.scripts["test:e2e"] // empty' /workspace/package.json 2>/dev/null)
  1621. fi
  1622. ;;
  1623. all|*)
  1624. test_cmd=$(docker exec "$CONTAINER_NAME" jq -r '.scripts.test // "npm test"' /workspace/package.json 2>/dev/null)
  1625. ;;
  1626. esac
  1627. elif docker exec "$CONTAINER_NAME" test -f /workspace/requirements.txt; then
  1628. case "$test_type" in
  1629. unit)
  1630. test_cmd="pytest tests/unit/ -v"
  1631. ;;
  1632. integration)
  1633. test_cmd="pytest tests/integration/ -v"
  1634. ;;
  1635. e2e)
  1636. test_cmd="pytest tests/e2e/ -v"
  1637. ;;
  1638. all|*)
  1639. test_cmd="pytest -v"
  1640. ;;
  1641. esac
  1642. elif docker exec "$CONTAINER_NAME" test -f /workspace/Cargo.toml; then
  1643. test_cmd="cargo test"
  1644. elif docker exec "$CONTAINER_NAME" test -f /workspace/go.mod; then
  1645. test_cmd="go test ./..."
  1646. fi
  1647. if [[ -z "$test_cmd" ]] || [[ "$test_cmd" == "null" ]]; then
  1648. log_warn "Could not auto-detect test command"
  1649. log_info "Supported test types: unit, integration, e2e, all"
  1650. log_info "Or run: loki sandbox run 'your-test-command'"
  1651. return 1
  1652. fi
  1653. log_success "Running: $test_cmd"
  1654. docker exec -it "$CONTAINER_NAME" bash -c "cd /workspace && $test_cmd"
  1655. }
  1656. # Check SDLC phase and suggest testing
  1657. sandbox_phase() {
  1658. check_docker
  1659. if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1660. log_error "Sandbox is not running"
  1661. return 1
  1662. fi
  1663. # Get current phase from orchestrator state
  1664. local phase
  1665. phase=$(docker exec "$CONTAINER_NAME" bash -c \
  1666. "python3 -c \"import json; print(json.load(open('/workspace/.loki/state/orchestrator.json')).get('currentPhase', 'UNKNOWN'))\" 2>/dev/null" \
  1667. || echo "UNKNOWN")
  1668. echo ""
  1669. echo -e "${BOLD}Current SDLC Phase: ${CYAN}$phase${NC}"
  1670. echo ""
  1671. case "$phase" in
  1672. BOOTSTRAP|DISCOVERY|ARCHITECTURE)
  1673. log_info "Phase $phase - No testing required yet"
  1674. log_info "Testing begins in DEVELOPMENT phase"
  1675. ;;
  1676. INFRASTRUCTURE)
  1677. log_info "Phase $phase - Infrastructure testing"
  1678. echo " Recommended:"
  1679. echo " loki sandbox run 'docker-compose up -d'"
  1680. echo " loki sandbox run 'terraform plan'"
  1681. ;;
  1682. DEVELOPMENT)
  1683. log_info "Phase $phase - Development testing recommended"
  1684. echo ""
  1685. echo " Run dev server:"
  1686. echo " loki sandbox serve"
  1687. echo ""
  1688. echo " Run unit tests:"
  1689. echo " loki sandbox test unit"
  1690. echo ""
  1691. echo " Manual testing:"
  1692. echo " loki sandbox shell"
  1693. ;;
  1694. QA)
  1695. log_info "Phase $phase - Full testing required"
  1696. echo ""
  1697. echo " Run all tests:"
  1698. echo " loki sandbox test all"
  1699. echo ""
  1700. echo " Run specific test suites:"
  1701. echo " loki sandbox test unit"
  1702. echo " loki sandbox test integration"
  1703. echo " loki sandbox test e2e"
  1704. echo ""
  1705. echo " Interactive testing:"
  1706. echo " loki sandbox serve"
  1707. echo " loki sandbox shell"
  1708. echo ""
  1709. echo " To report issues during testing:"
  1710. echo " loki sandbox prompt 'Found bug: describe the bug here'"
  1711. ;;
  1712. DEPLOYMENT)
  1713. log_info "Phase $phase - Smoke testing"
  1714. echo ""
  1715. echo " Run smoke tests:"
  1716. echo " loki sandbox run 'npm run test:smoke'"
  1717. echo ""
  1718. echo " Check deployment status:"
  1719. echo " loki sandbox run 'curl -s http://localhost:3000/health'"
  1720. ;;
  1721. GROWTH|*)
  1722. log_info "Phase $phase - Continuous testing"
  1723. echo ""
  1724. echo " Run regression tests:"
  1725. echo " loki sandbox test all"
  1726. echo ""
  1727. echo " Performance testing:"
  1728. echo " loki sandbox run 'npx k6 run tests/load.js'"
  1729. ;;
  1730. esac
  1731. echo ""
  1732. }
  1733. # Expose additional ports (for development testing)
  1734. sandbox_expose() {
  1735. local port="${1:-}"
  1736. if [[ -z "$port" ]]; then
  1737. log_error "Usage: loki sandbox expose <port>"
  1738. log_info "Example: loki sandbox expose 8080"
  1739. return 1
  1740. fi
  1741. check_docker
  1742. if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1743. log_error "Sandbox is not running"
  1744. log_info "Note: Ports must be exposed when starting the sandbox"
  1745. log_info "Set LOKI_EXTRA_PORTS='$port:$port' before running 'loki sandbox start'"
  1746. return 1
  1747. fi
  1748. log_warn "Cannot expose ports on running container"
  1749. log_info ""
  1750. log_info "To expose additional ports, restart sandbox with:"
  1751. echo ""
  1752. echo " LOKI_EXTRA_PORTS='$port:$port' loki sandbox start"
  1753. echo ""
  1754. log_info "Or access via sandbox shell:"
  1755. echo " loki sandbox shell"
  1756. echo " # Then run your server inside the container"
  1757. }
  1758. sandbox_status() {
  1759. check_docker
  1760. if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1761. log_success "Sandbox is running: $CONTAINER_NAME"
  1762. echo ""
  1763. docker ps --filter "name=$CONTAINER_NAME" --format "table {{.ID}}\t{{.Status}}\t{{.Ports}}"
  1764. echo ""
  1765. # Try to get loki status
  1766. log_info "Loki Status:"
  1767. docker exec "$CONTAINER_NAME" loki status 2>/dev/null || log_warn "Could not get loki status"
  1768. else
  1769. log_warn "Sandbox is not running"
  1770. # Check for stopped container
  1771. if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1772. log_info "Stopped container exists. Use 'loki sandbox start' to restart."
  1773. fi
  1774. fi
  1775. }
  1776. sandbox_logs() {
  1777. local lines="${1:-100}"
  1778. check_docker
  1779. if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1780. docker logs --tail "$lines" -f "$CONTAINER_NAME"
  1781. else
  1782. log_error "Sandbox is not running"
  1783. exit 1
  1784. fi
  1785. }
  1786. sandbox_shell() {
  1787. check_docker
  1788. if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  1789. log_info "Opening shell in sandbox..."
  1790. docker exec -it "$CONTAINER_NAME" bash
  1791. else
  1792. log_error "Sandbox is not running"
  1793. exit 1
  1794. fi
  1795. }
  1796. sandbox_build() {
  1797. check_docker
  1798. build_sandbox_image
  1799. }
  1800. show_help() {
  1801. echo -e "${BOLD}Loki Mode Sandbox${NC}"
  1802. echo ""
  1803. echo "Usage: loki sandbox <command> [options]"
  1804. echo ""
  1805. echo "Commands:"
  1806. echo " start [PRD] Start sandbox with optional PRD"
  1807. echo " stop Stop running sandbox"
  1808. echo " status Check sandbox status"
  1809. echo " logs [N] View last N log lines (default: 100)"
  1810. echo " shell Open bash shell in sandbox"
  1811. echo " build Build/rebuild sandbox image"
  1812. echo " cleanup Remove orphaned sandbox worktrees/branches"
  1813. echo ""
  1814. echo "Interactive Commands (while sandbox is running):"
  1815. echo " prompt <msg> Send a prompt/directive to Loki in real-time"
  1816. echo " run <cmd> Run a command inside the sandbox"
  1817. echo " serve [port] Auto-detect and start dev server (default port: 3000)"
  1818. echo ""
  1819. echo "Testing Commands:"
  1820. echo " test [type] Run tests (type: unit, integration, e2e, all)"
  1821. echo " phase Check SDLC phase and get testing suggestions"
  1822. echo " expose <port> Show how to expose additional ports"
  1823. echo ""
  1824. echo "Mode Options:"
  1825. echo " --docker-desktop Force Docker Desktop Sandbox (microVM isolation)"
  1826. echo " --docker Force Docker container sandbox (seccomp isolation)"
  1827. echo " --worktree Force git worktree sandbox (soft isolation)"
  1828. echo " --auto Auto-detect best mode (default)"
  1829. echo ""
  1830. echo "Sandbox Modes (priority order):"
  1831. echo " Docker Desktop - microVM isolation via 'docker sandbox' (Docker Desktop 4.58+)"
  1832. echo " (default) Hypervisor-based VM, private Docker daemon, pre-installed CLIs"
  1833. echo " Docker Container - Container isolation with seccomp, dropped capabilities,"
  1834. echo " (fallback) resource limits, network control (Dockerfile.sandbox)"
  1835. echo " Worktree - Git worktree isolation (last resort if no Docker)"
  1836. echo " (last resort) Warning: No filesystem/network/process isolation"
  1837. echo ""
  1838. echo "Environment Variables:"
  1839. echo " LOKI_SANDBOX_IMAGE Docker image (default: loki-mode:sandbox)"
  1840. echo " LOKI_SANDBOX_NETWORK Network mode: bridge, none, host (default: bridge)"
  1841. echo " LOKI_SANDBOX_ALLOW_HOSTS Egress allowlist, comma-separated hostnames (default: unset)"
  1842. echo " LOKI_SANDBOX_CPUS CPU limit (default: 2)"
  1843. echo " LOKI_SANDBOX_MEMORY Memory limit (default: 4g)"
  1844. echo " LOKI_SANDBOX_READONLY Mount project read-only (default: false)"
  1845. echo " LOKI_SANDBOX_CLEANUP Auto-cleanup worktree on stop (default: true)"
  1846. echo " LOKI_EXTRA_PORTS Expose extra ports (e.g., '3000:3000,8080:8080')"
  1847. echo " LOKI_PROMPT_INJECTION Enable real-time prompts (default: false)"
  1848. echo ""
  1849. echo "Security Features (Docker mode):"
  1850. echo " - Seccomp profile restricts syscalls"
  1851. echo " - No new privileges flag"
  1852. echo " - Dropped capabilities"
  1853. echo " - Resource limits (CPU, memory, PIDs)"
  1854. echo " - API keys passed as env vars (not mounted)"
  1855. echo " - Prompt injection DISABLED by default (enterprise security)"
  1856. echo ""
  1857. echo "Examples:"
  1858. echo " loki sandbox start # Start (auto-detect: Desktop > Docker > Worktree)"
  1859. echo " loki sandbox start --docker-desktop ./prd.md # Force Docker Desktop microVM"
  1860. echo " loki sandbox start --docker ./prd.md # Force Docker container mode"
  1861. echo " loki sandbox start --worktree # Force worktree mode"
  1862. echo " loki sandbox prompt 'start the app and show URL' # Send prompt"
  1863. echo " loki sandbox serve # Start dev server"
  1864. echo " loki sandbox test # Run all tests"
  1865. echo " loki sandbox test unit # Run unit tests only"
  1866. echo " loki sandbox phase # Check phase, get testing tips"
  1867. echo " loki sandbox run 'npm test' # Run custom command"
  1868. echo " loki sandbox cleanup # Remove old worktrees"
  1869. }
  1870. #===============================================================================
  1871. # Main
  1872. #===============================================================================
  1873. main() {
  1874. local command="${1:-help}"
  1875. shift || true
  1876. # Parse mode option
  1877. local mode="auto"
  1878. local args=()
  1879. while [[ $# -gt 0 ]]; do
  1880. case "$1" in
  1881. --docker-desktop) mode="docker-desktop"; shift ;;
  1882. --docker) mode="docker"; shift ;;
  1883. --worktree) mode="worktree"; shift ;;
  1884. --auto) mode="auto"; shift ;;
  1885. *) args+=("$1"); shift ;;
  1886. esac
  1887. done
  1888. # Detect sandbox mode for commands that need it
  1889. # For stop/status/etc, read persisted mode first so we stop the correct sandbox type
  1890. local sandbox_mode=""
  1891. local _mode_file="${PROJECT_DIR}/.loki/sandbox/mode"
  1892. case "$command" in
  1893. start)
  1894. sandbox_mode=$(detect_sandbox_mode "$mode") || exit 1
  1895. # Persist the detected mode so stop/status use the same mode
  1896. mkdir -p "$(dirname "$_mode_file")"
  1897. echo "$sandbox_mode" > "$_mode_file"
  1898. ;;
  1899. stop|status|prompt|shell|logs|run|serve|test|phase)
  1900. # Read persisted mode if no explicit mode override was given
  1901. if [[ "$mode" == "auto" ]] && [[ -f "$_mode_file" ]]; then
  1902. sandbox_mode=$(cat "$_mode_file" 2>/dev/null || true)
  1903. fi
  1904. # Fall back to detection if no persisted mode
  1905. if [[ -z "$sandbox_mode" ]]; then
  1906. sandbox_mode=$(detect_sandbox_mode "$mode") || exit 1
  1907. fi
  1908. ;;
  1909. esac
  1910. case "$command" in
  1911. start)
  1912. if [[ "$sandbox_mode" == "docker-desktop" ]]; then
  1913. start_docker_desktop_sandbox ${args[@]+"${args[@]}"}
  1914. elif [[ "$sandbox_mode" == "docker" ]]; then
  1915. start_sandbox ${args[@]+"${args[@]}"}
  1916. else
  1917. start_worktree_sandbox ${args[@]+"${args[@]}"}
  1918. fi
  1919. ;;
  1920. stop)
  1921. if [[ "$sandbox_mode" == "docker-desktop" ]]; then
  1922. stop_docker_desktop_sandbox
  1923. elif [[ "$sandbox_mode" == "docker" ]]; then
  1924. stop_sandbox
  1925. else
  1926. stop_worktree_sandbox
  1927. fi
  1928. ;;
  1929. status)
  1930. if [[ "$sandbox_mode" == "docker-desktop" ]]; then
  1931. docker_desktop_sandbox_status
  1932. elif [[ "$sandbox_mode" == "docker" ]]; then
  1933. sandbox_status
  1934. else
  1935. worktree_sandbox_status
  1936. fi
  1937. ;;
  1938. logs)
  1939. if [[ "$sandbox_mode" == "docker-desktop" ]]; then
  1940. docker_desktop_sandbox_logs ${args[@]+"${args[@]}"}
  1941. else
  1942. sandbox_logs ${args[@]+"${args[@]}"}
  1943. fi
  1944. ;;
  1945. shell)
  1946. if [[ "$sandbox_mode" == "docker-desktop" ]]; then
  1947. docker_desktop_sandbox_shell
  1948. else
  1949. sandbox_shell
  1950. fi
  1951. ;;
  1952. build)
  1953. sandbox_build
  1954. ;;
  1955. prompt)
  1956. if [[ "$sandbox_mode" == "docker-desktop" ]]; then
  1957. docker_desktop_sandbox_prompt ${args[@]+"${args[@]}"}
  1958. elif [[ "$sandbox_mode" == "docker" ]]; then
  1959. sandbox_prompt ${args[@]+"${args[@]}"}
  1960. else
  1961. worktree_sandbox_prompt ${args[@]+"${args[@]}"}
  1962. fi
  1963. ;;
  1964. run)
  1965. if [[ "$sandbox_mode" == "docker-desktop" ]]; then
  1966. docker_desktop_sandbox_run ${args[@]+"${args[@]}"}
  1967. else
  1968. sandbox_run ${args[@]+"${args[@]}"}
  1969. fi
  1970. ;;
  1971. cleanup)
  1972. cleanup_worktrees
  1973. ;;
  1974. serve)
  1975. if [[ "$sandbox_mode" == "docker-desktop" ]]; then
  1976. docker_desktop_sandbox_run "npm start 2>/dev/null || python3 -m http.server ${args[0]:-3000}"
  1977. else
  1978. sandbox_serve ${args[@]+"${args[@]}"}
  1979. fi
  1980. ;;
  1981. test)
  1982. if [[ "$sandbox_mode" == "docker-desktop" ]]; then
  1983. docker_desktop_sandbox_run "npm test 2>/dev/null || python3 -m pytest 2>/dev/null || echo 'No test runner found'"
  1984. else
  1985. sandbox_test ${args[@]+"${args[@]}"}
  1986. fi
  1987. ;;
  1988. phase)
  1989. if [[ "$sandbox_mode" == "docker-desktop" ]]; then
  1990. docker_desktop_sandbox_run "bash ${PROJECT_DIR}/autonomy/loki status"
  1991. else
  1992. sandbox_phase
  1993. fi
  1994. ;;
  1995. expose)
  1996. sandbox_expose ${args[@]+"${args[@]}"}
  1997. ;;
  1998. help|--help|-h)
  1999. show_help
  2000. ;;
  2001. *)
  2002. log_error "Unknown command: $command"
  2003. show_help
  2004. exit 1
  2005. ;;
  2006. esac
  2007. }
  2008. main "$@"

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.