Skip to content
Back to search

claude-code-templates

SkillWorks with Claude CodeGitHub

CLI tool for configuring and monitoring Claude Code

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/davila7/claude-code-templates 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/davila7/claude-code-templates'

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/davila7-claude-code-templates/badge.svg)](/listings/davila7-claude-code-templates)

HTML

<a href="/listings/davila7-claude-code-templates"><img src="/api/listings/davila7-claude-code-templates/badge.svg" alt="findsafeskills safety rating" /></a>

Install

i

Install instructions from the README

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

# Install a complete development stack
npx claude-code-templates@latest --agent development-team/frontend-developer --command testing/generate-tests --mcp development/github-integration --yes

# Browse and install interactively
npx claude-code-templates@latest

# Install specific components
npx claude-code-templates@latest --agent development-tools/code-reviewer --yes
npx claude-code-templates@latest --command performance/optimize-bundle --yes
npx claude-code-templates@latest --setting performance/mcp-timeouts --yes
npx claude-code-templates@latest --hook git/pre-commit-validation --yes
npx claude-code-templates@latest --mcp database/postgresql-integration --yes

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.

README.md · README · 233 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. <p align="center">
  2. <a href="https://www.aitmpl.com">
  3. <picture>
  4. <source media="(prefers-color-scheme: dark)" srcset="docs/images/readme/logo-dark.svg" />
  5. <source media="(prefers-color-scheme: light)" srcset="docs/images/readme/logo-light.svg" />
  6. <img src="docs/images/readme/logo-light.svg" alt="Claude Code Templates logo" width="80" height="80" />
  7. </picture>
  8. </a>
  9. </p>
  10. <h1 align="center">Claude Code Templates</h1>
  11. <p align="center"><sub>SUPPORTED BY</sub></p>
  12. <p align="center">
  13. <a href="https://www.nvidia.com/en-us/startups/"><picture>
  14. <source media="(prefers-color-scheme: dark)" srcset="docs/images/readme/supporters/nvidia-dark.svg" />
  15. <img src="docs/images/readme/supporters/nvidia-light.svg" alt="NVIDIA Inception" height="28" />
  16. </picture></a>
  17. &nbsp;&nbsp;&nbsp;
  18. <a href="https://claude.com/contact-sales/claude-for-oss"><picture>
  19. <source media="(prefers-color-scheme: dark)" srcset="docs/images/readme/supporters/claude-dark.svg" />
  20. <img src="docs/images/readme/supporters/claude-light.svg" alt="Claude for Open Source" height="28" />
  21. </picture></a>
  22. &nbsp;&nbsp;&nbsp;
  23. <a href="https://vercel.com/oss"><picture>
  24. <source media="(prefers-color-scheme: dark)" srcset="docs/images/readme/supporters/vercel-dark.svg" />
  25. <img src="docs/images/readme/supporters/vercel-light.svg" alt="Vercel OSS" height="28" />
  26. </picture></a>
  27. &nbsp;&nbsp;&nbsp;
  28. <a href="https://get.neon.com/4eCjZDz"><picture>
  29. <source media="(prefers-color-scheme: dark)" srcset="docs/images/readme/supporters/neon-dark.svg" />
  30. <img src="docs/images/readme/supporters/neon-light.svg" alt="Neon Open Source" height="28" />
  31. </picture></a>
  32. </p>
  33. <p align="center">
  34. An open catalog of components for Claude Code, installable with one command.
  35. </p>
  36. <p align="center">
  37. <a href="https://www.npmjs.com/package/claude-code-templates"><img src="https://img.shields.io/npm/v/claude-code-templates.svg?color=d57455" alt="npm version" /></a>
  38. <a href="https://www.npmjs.com/package/claude-code-templates"><img src="https://img.shields.io/npm/dt/claude-code-templates.svg?color=d57455" alt="npm downloads" /></a>
  39. <a href="https://github.com/davila7/claude-code-templates/stargazers"><img src="https://img.shields.io/github/stars/davila7/claude-code-templates?style=flat&label=stars&color=d57455" alt="GitHub stars" /></a>
  40. <a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-d57455.svg" alt="MIT license" /></a>
  41. </p>
  42. <p align="center">
  43. <a href="https://www.aitmpl.com"><b>Browse the catalog</b></a>
  44. &nbsp;·&nbsp;
  45. <a href="https://docs.aitmpl.com">Documentation</a>
  46. &nbsp;·&nbsp;
  47. <a href="CONTRIBUTING.md">Contributing</a>
  48. &nbsp;·&nbsp;
  49. <a href="https://github.com/davila7/claude-code-templates/discussions">Discussions</a>
  50. &nbsp;·&nbsp;
  51. <a href="CHANGELOG.md">Changelog</a>
  52. </p>
  53. <p align="center"><sub>SPONSORED BY</sub></p>
  54. <p align="center">
  55. <a href="https://www.swiftproxy.net/">
  56. <picture>
  57. <source media="(prefers-color-scheme: dark)" srcset="docs/sponsors/swiftproxy-dark.png" />
  58. <source media="(prefers-color-scheme: light)" srcset="docs/sponsors/swiftproxy-light.png" />
  59. <img src="docs/sponsors/swiftproxy-light.png" alt="Swiftproxy" width="280" />
  60. </picture>
  61. </a>
  62. </p>
  63. <p align="center">
  64. <b>Swiftproxy proudly supports this project.</b><br />
  65. Connect your AI-powered workflows to reliable residential proxies for web data collection, browser automation, and AI-driven research.<br />
  66. 90M+ residential IPs across 220+ locations, rotating and static residential proxies, with HTTP(S) and SOCKS5 support.<br /><br />
  67. <a href="https://www.swiftproxy.net/"><img src="https://img.shields.io/badge/Get%2010%25%20off-code%20PROXY90-2563eb?style=for-the-badge" alt="Get 10% off with code PROXY90" /></a>
  68. </p>
  69. <picture>
  70. <source media="(prefers-color-scheme: dark)" srcset="docs/images/readme/aitmpl-catalog-dark.png" />
  71. <source media="(prefers-color-scheme: light)" srcset="docs/images/readme/aitmpl-catalog-light.png" />
  72. <img src="docs/images/readme/aitmpl-catalog-dark.png" alt="The aitmpl.com component catalog" width="1600" />
  73. </picture>
  74. ## About
  75. Claude Code is configured through small files: skills, subagents, slash commands, hooks, MCP servers and settings. Claude Code Templates collects more than 1,980 of them in one catalog at [aitmpl.com](https://www.aitmpl.com), where you can search, preview every file a component writes, and combine several components into a single install command. The CLI writes each file to the right place and merges existing configuration instead of overwriting it.
  76. Every pull request is checked by Claude Code's plugin validator and scanned by [NVIDIA SkillSpector](https://github.com/NVIDIA/skillspector).
  77. ## Quickstart
  78. Runs with `npx`, no global install needed.
  79. ```bash
  80. # Browse the catalog and install interactively
  81. npx claude-code-templates@latest
  82. # Install one component
  83. npx claude-code-templates@latest --skill creative-design/frontend-design
  84. # Install a stack in one command
  85. npx claude-code-templates@latest \
  86. --agent development-team/frontend-developer \
  87. --command testing/generate-tests \
  88. --mcp integration/github-integration
  89. ```
  90. Add `--yes` to skip the confirmation prompt. Every component page on [aitmpl.com](https://www.aitmpl.com) shows its exact install command. To find one from the terminal: `npx claude-code-templates@latest --search "code review"`.
  91. <details>
  92. <summary>One example per component type</summary>
  93. ```bash
  94. npx claude-code-templates@latest --skill creative-design/frontend-design
  95. npx claude-code-templates@latest --agent development-tools/code-reviewer
  96. npx claude-code-templates@latest --command performance/optimize-bundle-size
  97. npx claude-code-templates@latest --mcp database/postgresql-integration
  98. npx claude-code-templates@latest --setting mcp/mcp-timeouts
  99. npx claude-code-templates@latest --hook git/conventional-commits
  100. npx claude-code-templates@latest --mod security/secret-redactor # Claude Code 2.1.287+
  101. npx claude-code-templates@latest --loop engineering/docs-sweep-loop # also installs the components the loop uses
  102. npx claude-code-templates@latest --plugin productivity/champion-kit # via the aitmpl marketplace, Claude Code 2.1.288+
  103. ```
  104. </details>
  105. ## What's in the catalog
  106. | Type | Count | What it is | Examples |
  107. |------|------:|------------|----------|
  108. | **Skills** | 918 | Reusable capabilities with progressive disclosure | PDF processing, frontend design, code review |
  109. | **Agents** | 422 | Subagents specialized in one domain | Security auditor, frontend developer, database architect |
  110. | **Commands** | 288 | Custom slash commands | `/generate-tests`, `/optimize-bundle-size`, `/security-audit` |
  111. | **MCPs** | 119 | External service integrations | GitHub, PostgreSQL, Stripe, Supabase, Sentry |
  112. | **Settings** | 80 | Claude Code configuration | Timeouts, permissions, status lines |
  113. | **Hooks** | 63 | Automation triggers | Conventional commits, blocking direct pushes |
  114. | **Mods** | 50 | Plugins that change Claude Code's own interface and behavior | Secret redaction, protected paths, terminal games |
  115. | **Loops** | 18 | Autonomous workflows with a goal, an interval and a stop condition | Docs sweep, build-test-fix, adversarial review |
  116. | **Templates** | 14 | Complete project configurations | Language and framework setups |
  117. | **Sandboxes** | 11 | Launchers that run Claude Code in an isolated environment (reference only, see [`cli-tool/components/sandbox/`](cli-tool/components/sandbox/)) | E2B, Cloudflare, Docker |
  118. | **Plugins** | 1 | Whole Claude Code plugins, installed from the `aitmpl` marketplace | Champion Kit |
  119. Live counts: [`dashboard/public/counts.json`](dashboard/public/counts.json).
  120. ## Local tools
  121. The CLI also ships three local tools. The two dashboards listen on `127.0.0.1` only, because they serve your conversation history.
  122. | Command | What it does |
  123. |---------|--------------|
  124. | `npx claude-code-templates@latest --analytics` | Real-time session monitoring and usage metrics at `localhost:3333` |
  125. | `npx claude-code-templates@latest --chats` | Read and search your conversations in a mobile-friendly UI at `localhost:9876` |
  126. | `npx claude-code-templates@latest --health-check` | Diagnose your Claude Code installation and project setup |
  127. To open the conversation browser from another device, forward the port over SSH: `ssh -L 9876:localhost:9876 <host>`.
  128. <details>
  129. <summary>Flags removed in 1.31.0</summary>
  130. `--plugins`, `--skills-manager`, `--command-stats`, `--hook-stats`, `--mcp-stats`, `--agents`, `--chats-mobile`, `--teams`, `--2025`, `--studio`, `--sandbox`, `--tunnel`, `--create-agent`, `--list-agents`, `--update-agent`, `--remove-agent` and `--clone-session` were removed. Running one prints what replaced it. To see what is installed in each scope, use the [stack viewer mod](https://www.aitmpl.com/component/mods/ui/stack-viewer) (`--mod ui/stack-viewer`, then `/stack` in Claude Code). Details in the [CHANGELOG](CHANGELOG.md).
  131. </details>
  132. ## Documentation
  133. Guides, the CLI reference and the catalog API live at **[docs.aitmpl.com](https://docs.aitmpl.com)**.
  134. ## Contributing
  135. Contributions are welcome. Read the [contributing guide](CONTRIBUTING.md) to add a skill, agent, command, MCP, setting, hook or mod, and the [Code of Conduct](CODE_OF_CONDUCT.md) before you open a pull request.
  136. ## Sponsors and programs
  137. ### Sponsors
  138. <p align="center">
  139. <a href="https://z.ai/subscribe?ic=8JVLJQFSKB&utm_source=github&utm_medium=badge&utm_campaign=readme"><img src="https://img.shields.io/badge/Z.AI-sponsor-2563eb?style=for-the-badge" alt="Z.AI" /></a>
  140. &nbsp;
  141. <a href="https://get.brightdata.com/lcqorc6nzp9w"><img src="https://img.shields.io/badge/Bright%20Data-sponsor-3d7ffc?style=for-the-badge" alt="Bright Data" /></a>
  142. </p>
  143. ### Open source programs
  144. <p align="center">
  145. <a href="https://claude.com/contact-sales/claude-for-oss"><img alt="Claude for Open Source" src="docs/claude-oss-badge.svg" height="40" /></a>
  146. &nbsp;&nbsp;
  147. <a href="https://vercel.com/oss"><img alt="Vercel OSS Program" src="https://vercel.com/oss/program-badge.svg" height="40" /></a>
  148. &nbsp;&nbsp;
  149. <a href="https://get.neon.com/4eCjZDz"><img alt="Neon Open Source Program" src="https://img.shields.io/badge/Neon-Open%20Source%20Program-00E599?style=for-the-badge" height="40" /></a>
  150. </p>
  151. <p align="center">
  152. <a href="https://trendshift.io/repositories/15113"><img src="https://trendshift.io/api/badge/repositories/15113" alt="davila7%2Fclaude-code-templates | Trendshift" width="200" height="40" /></a>
  153. </p>
  154. ### Support the project
  155. <p align="center">
  156. <a href="https://github.com/sponsors/davila7"><img src="https://img.shields.io/badge/Sponsor%20on%20GitHub-%E2%9D%A4-EA4AAA?style=for-the-badge&logo=githubsponsors&logoColor=white" alt="Sponsor on GitHub" /></a>
  157. &nbsp;
  158. <a href="https://buymeacoffee.com/daniavila"><img src="https://img.shields.io/badge/Buy%20me%20a%20coffee-FFDD00?style=for-the-badge&logo=buymeacoffee&logoColor=black" alt="Buy me a coffee" /></a>
  159. </p>
  160. Everyone who supports the project is listed at [aitmpl.com/supporters](https://www.aitmpl.com/supporters).
  161. ## Attribution
  162. The catalog includes components from these sources. Each keeps its original license and attribution.
  163. | Source | License | Contributes |
  164. |--------|---------|-------------|
  165. | [K-Dense-AI/claude-scientific-skills](https://github.com/K-Dense-AI/claude-scientific-skills) (K-Dense Inc.) | MIT | 139 scientific skills for biology, chemistry, medicine and computational research |
  166. | [anthropics/skills](https://github.com/anthropics/skills) | See repo | 21 official Anthropic skills |
  167. | [anthropics/claude-code](https://github.com/anthropics/claude-code) | See repo | 10 skills from development guides and examples |
  168. | [obra/superpowers](https://github.com/obra/superpowers) (Jesse Obra) | MIT | 14 workflow skills |
  169. | [alirezarezvani/claude-skills](https://github.com/alirezarezvani/claude-skills) (Alireza Rezvani) | MIT | 36 professional role skills |
  170. | [wshobson/agents](https://github.com/wshobson/agents) | MIT | 48 agents |
  171. | [hesreallyhim/awesome-claude-code](https://github.com/hesreallyhim/awesome-claude-code) | CC0 1.0 | 21 commands |
  172. | [mehdi-lamrani/awesome-claude-skills](https://github.com/mehdi-lamrani/awesome-claude-skills) | Apache 2.0 | Community skills |
  173. | NerdyChefsAI Skills | MIT | Specialized enterprise skills |
  174. | move-code-quality-skill | MIT | |
  175. | cocoindex-claude | Apache 2.0 | |
  176. ## Star history
  177. [![Stargazers over time](docs/star-history.svg)](https://www.aitmpl.com/component/skill/git/star-history-chart)
  178. ## License
  179. [MIT](LICENSE)
cli-tool/components/mods/security/jev-auto-mode/hooks/rules.ts · Bundled script · 799 lines · 2 findings marked
  1. /**
  2. * jev-auto-mode — the JSON policy and its matcher.
  3. *
  4. * No `$` and no I/O here: this module parses and validates the config files,
  5. * merges the user and project layers, and decides which rules an action hits.
  6. * The hooks module reads the files and asks; the tests drive this directly.
  7. *
  8. * An "action" is anything Claude is about to do that the mod governs:
  9. * tool a tool call (Bash, Write, WebFetch, mcp__server__tool, Skill...)
  10. * command a slash command the person typed (/deploy, a skill run as /name)
  11. * agent a subagent about to be spawned
  12. * skill a skill's prompt about to be expanded (typed, Skill tool, or
  13. * preloaded into a subagent)
  14. */
  15. export type Decision = 'allow' | 'ask' | 'deny'
  16. /** What happens to an action no rule matched. */
  17. export type Fallback = Decision | 'passthrough' | 'jev'
  18. export type ActionKind = 'tool' | 'command' | 'agent' | 'skill'
  19. export type Scope = 'all' | 'main' | 'subagents'
  20. export type Rule = {
  21. /** Shown in the log and in the reason the model reads; defaults to rule #n. */
  22. id?: string
  23. decision: Decision
  24. /** Why, in a sentence: what the model reads on a deny and the person on an ask. */
  25. reason?: string
  26. /** Tool name globs: `Bash`, `mcp__github__*`, `Write`. */
  27. tool?: string | string[]
  28. /** MCP server globs: `github` matches every `mcp__github__*` tool. */
  29. mcpServer?: string | string[]
  30. /** Bash command globs, matched against each part of a compound command. */
  31. bash?: string | string[]
  32. /** Regexes over each part of a compound Bash command. */
  33. bashRegex?: string | string[]
  34. /** Path globs over file_path / path / notebook_path (`**` crosses `/`). */
  35. path?: string | string[]
  36. /** Host globs over a WebFetch/WebSearch url or domain: `*.example.com`. */
  37. domain?: string | string[]
  38. /** Skill name globs: the Skill tool, a typed /skill, a preloaded skill. */
  39. skill?: string | string[]
  40. /** Slash command name globs (without the slash). */
  41. command?: string | string[]
  42. /** Subagent type globs: `general-purpose`, `Explore`, `*`. */
  43. agent?: string | string[]
  44. /** Regexes over the action's input as JSON: the catch-all matcher. */
  45. inputRegex?: string | string[]
  46. /** Which loops the rule applies in (default all). */
  47. scope?: Scope
  48. }
  49. export type Hazard = 'destructive' | 'exfiltration' | 'security_weakening' | 'out_of_scope'
  50. export type JevConfig = {
  51. /** Tool globs the judge is asked about when no rule decided; the rest fall to `default`. */
  52. tools: string[]
  53. /** What each hazard does once it crosses `threshold`. */
  54. hazards: Record<Hazard, Decision>
  55. /** At or above this probability a hazard triggers its decision. */
  56. threshold: number
  57. /** At or above this (but under `threshold`) a hazard asks. */
  58. askThreshold: number
  59. /** A severity (0-3) at or above this turns an ask into a deny. */
  60. severityDeny: number
  61. /** What a judgement that failed or timed out decides. */
  62. onError: Fallback
  63. timeoutMs: number
  64. }
  65. export type Config = {
  66. /** enforce acts; audit only logs what it would have done. */
  67. mode: 'enforce' | 'audit'
  68. /** What an action no rule matched gets: `jev` asks the judge. */
  69. default: Fallback
  70. /** Who settles an `ask`: the mod's own dialog, or the engine's permission mode. */
  71. askWith: 'mod' | 'engine'
  72. /** What an `ask` becomes when there is no one to ask (`claude -p`). */
  73. headless: 'deny' | 'allow'
  74. /** Allow rules a project file may add (only honoured from the user file). */
  75. trustProjectAllow: boolean
  76. /**
  77. * What a shell command whose program is only known at run time gets
  78. * (`$X -rf /`, `eval …`, `source …`): no rule can read it, so at least this.
  79. */
  80. opaqueShell: Decision
  81. rules: Rule[]
  82. jev: JevConfig
  83. }
  84. export const CONFIG_NAME = 'jev-auto-mode.json'
  85. export const DEFAULT_JEV: JevConfig = {
  86. tools: ['Bash', 'Write', 'Edit', 'MultiEdit', 'NotebookEdit', 'WebFetch', 'mcp__*'],
  87. hazards: {
  88. destructive: 'ask',
  89. exfiltration: 'deny',
  90. security_weakening: 'ask',
  91. out_of_scope: 'ask',
  92. },
  93. threshold: 0.7,
  94. askThreshold: 0.4,
  95. severityDeny: 3,
  96. onError: 'ask',
  97. timeoutMs: 2500,
  98. }
  99. export const DEFAULT_CONFIG: Config = {
  100. mode: 'enforce',
  101. default: 'passthrough',
  102. askWith: 'mod',
  103. headless: 'deny',
  104. trustProjectAllow: false,
  105. opaqueShell: 'ask',
  106. rules: [],
  107. jev: DEFAULT_JEV,
  108. }
  109. const DECISIONS: readonly string[] = ['allow', 'ask', 'deny']
  110. const FALLBACKS: readonly string[] = [...DECISIONS, 'passthrough', 'jev']
  111. const MATCHERS = ['tool', 'mcpServer', 'bash', 'bashRegex', 'path', 'domain', 'skill', 'command', 'agent', 'inputRegex'] as const
  112. const HAZARDS: readonly Hazard[] = ['destructive', 'exfiltration', 'security_weakening', 'out_of_scope']
  113. export type Parsed = { config: Partial<Omit<Config, 'jev'>> & { jev?: Partial<JevConfig> }; errors: string[] }
  114. const isObject = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v)
  115. const strings = (v: unknown): v is string | string[] =>
  116. typeof v === 'string' || (Array.isArray(v) && v.every(x => typeof x === 'string'))
  117. /**
  118. * Reads one config file's text. Every problem is reported and the offending
  119. * piece dropped, never the whole file: a typo in one rule must not silently
  120. * switch the policy off.
  121. */
  122. export function parseConfig(text: string, source: string): Parsed {
  123. const errors: string[] = []
  124. let raw: unknown
  125. try {
  126. raw = JSON.parse(text)
  127. } catch (err) {
  128. return { config: {}, errors: [`${source}: not valid JSON (${String(err)})`] }
  129. }
  130. if (!isObject(raw)) return { config: {}, errors: [`${source}: the top level must be an object`] }
  131. const config: Parsed['config'] = {}
  132. const pick = <T extends string>(key: string, allowed: readonly string[]): T | undefined => {
  133. if (raw[key] === undefined) return undefined
  134. if (typeof raw[key] === 'string' && allowed.includes(raw[key] as string)) return raw[key] as T
  135. errors.push(`${source}: "${key}" must be one of ${allowed.join(', ')}`)
  136. return undefined
  137. }
  138. config.mode = pick('mode', ['enforce', 'audit'])
  139. config.default = pick('default', FALLBACKS)
  140. config.askWith = pick('askWith', ['mod', 'engine'])
  141. config.headless = pick('headless', ['deny', 'allow'])
  142. config.opaqueShell = pick('opaqueShell', DECISIONS)
  143. if (raw.trustProjectAllow !== undefined) {
  144. if (typeof raw.trustProjectAllow === 'boolean') config.trustProjectAllow = raw.trustProjectAllow
  145. else errors.push(`${source}: "trustProjectAllow" must be true or false`)
  146. }
  147. if (raw.rules !== undefined) {
  148. if (!Array.isArray(raw.rules)) errors.push(`${source}: "rules" must be an array`)
  149. else {
  150. config.rules = []
  151. raw.rules.forEach((r, i) => {
  152. const rule = parseRule(r, `${source} rule #${i + 1}`, errors)
  153. if (rule) config.rules!.push({ ...rule, id: rule.id ?? `${source}#${i + 1}` })
  154. })
  155. }
  156. }
  157. if (raw.jev !== undefined) {
  158. if (!isObject(raw.jev)) errors.push(`${source}: "jev" must be an object`)
  159. else config.jev = parseJev(raw.jev, source, errors)
  160. }
  161. return { config, errors }
  162. }
  163. /**
  164. * A regex whose matching can blow up (a quantified group that itself holds a
  165. * quantifier, as in `(a+)+` or `(\\w*)*`): matching is synchronous, so one such
  166. * pattern could stall every tool call. Refused at load time.
  167. */
  168. export function riskyRegex(source: string): boolean {
  169. return /\((?:[^()\\]|\\.)*[+*}](?:[^()\\]|\\.)*\)[+*{]/.test(source)
  170. }
  171. /** Text longer than this is not regex-matched: the action is asked about instead. */
  172. export const MAX_MATCH_CHARS = 20_000
  173. function parseRule(r: unknown, where: string, errors: string[]): Rule | null {
  174. if (!isObject(r)) {
  175. errors.push(`${where}: must be an object`)
  176. return null
  177. }
  178. if (typeof r.decision !== 'string' || !DECISIONS.includes(r.decision)) {
  179. errors.push(`${where}: "decision" must be allow, ask or deny`)
  180. return null
  181. }
  182. const rule: Rule = { decision: r.decision as Decision }
  183. if (typeof r.id === 'string') rule.id = r.id
  184. if (typeof r.reason === 'string') rule.reason = r.reason
  185. if (r.scope !== undefined) {
  186. if (r.scope === 'all' || r.scope === 'main' || r.scope === 'subagents') rule.scope = r.scope
  187. else errors.push(`${where}: "scope" must be all, main or subagents`)
  188. }
  189. let matchers = 0
  190. for (const key of MATCHERS) {
  191. if (r[key] === undefined) continue
  192. if (!strings(r[key])) {
  193. errors.push(`${where}: "${key}" must be a string or a list of strings`)
  194. return null
  195. }
  196. if (key === 'bashRegex' || key === 'inputRegex') {
  197. for (const source of list(r[key] as string | string[])) {
  198. try {
  199. new RegExp(source)
  200. } catch {
  201. errors.push(`${where}: "${key}" has an invalid regex: ${source}`)
  202. return null
  203. }
  204. if (riskyRegex(source)) {
  205. errors.push(`${where}: "${key}" nests quantifiers (${source}), which can stall matching; rewrite it without a repeated group that itself repeats`)
  206. return null
  207. }
  208. }
  209. }
  210. ;(rule as Record<string, unknown>)[key] = r[key]
  211. matchers += 1
  212. }
  213. // A rule with no matcher would hit everything: almost always a typo'd key.
  214. if (matchers === 0) {
  215. errors.push(`${where}: names no matcher (${MATCHERS.join(', ')}); a rule that matches everything must say tool: "*"`)
  216. return null
  217. }
  218. const unknown = Object.keys(r).filter(k => !['id', 'decision', 'reason', 'scope', ...MATCHERS].includes(k))
  219. if (unknown.length) errors.push(`${where}: unknown key${unknown.length > 1 ? 's' : ''} ${unknown.join(', ')} ignored`)
  220. return rule
  221. }
  222. function parseJev(j: Record<string, unknown>, source: string, errors: string[]): Partial<JevConfig> {
  223. const out: Partial<JevConfig> = {}
  224. if (j.tools !== undefined) {
  225. if (strings(j.tools)) out.tools = list(j.tools)
  226. else errors.push(`${source}: "jev.tools" must be a list of tool globs`)
  227. }
  228. if (j.hazards !== undefined) {
  229. if (!isObject(j.hazards)) errors.push(`${source}: "jev.hazards" must be an object`)
  230. else {
  231. const hazards: Partial<Record<Hazard, Decision>> = {}
  232. for (const [name, value] of Object.entries(j.hazards)) {
  233. if (!HAZARDS.includes(name as Hazard)) errors.push(`${source}: unknown hazard "${name}" (${HAZARDS.join(', ')})`)
  234. else if (typeof value !== 'string' || !DECISIONS.includes(value)) errors.push(`${source}: hazard "${name}" must be allow, ask or deny`)
  235. else hazards[name as Hazard] = value as Decision
  236. }
  237. out.hazards = hazards as Record<Hazard, Decision>
  238. }
  239. }
  240. for (const key of ['threshold', 'askThreshold'] as const) {
  241. if (j[key] === undefined) continue
  242. if (typeof j[key] === 'number' && (j[key] as number) >= 0 && (j[key] as number) <= 1) out[key] = j[key] as number
  243. else errors.push(`${source}: "jev.${key}" must be a number from 0 to 1`)
  244. }
  245. if (j.severityDeny !== undefined) {
  246. if (typeof j.severityDeny === 'number') out.severityDeny = j.severityDeny
  247. else errors.push(`${source}: "jev.severityDeny" must be a number from 0 to 3`)
  248. }
  249. if (j.timeoutMs !== undefined) {
  250. if (typeof j.timeoutMs === 'number' && j.timeoutMs > 0) out.timeoutMs = j.timeoutMs
  251. else errors.push(`${source}: "jev.timeoutMs" must be a positive number`)
  252. }
  253. if (j.onError !== undefined) {
  254. if (typeof j.onError === 'string' && FALLBACKS.includes(j.onError) && j.onError !== 'jev') out.onError = j.onError as Fallback
  255. else errors.push(`${source}: "jev.onError" must be allow, ask, deny or passthrough`)
  256. }
  257. return out
  258. }
  259. /**
  260. * Folds the layers into one policy. The user file (yours, outside any
  261. * repository) may do anything. The project file ships with the repository, so
  262. * a checkout could otherwise open everything up: it adds deny and ask rules,
  263. * and may tighten the settings, but its allow rules only count when the user
  264. * file says `trustProjectAllow: true`.
  265. */
  266. export function mergeConfigs(user: Parsed['config'], project: Parsed['config']): { config: Config; notes: string[] } {
  267. const notes: string[] = []
  268. const trust = user.trustProjectAllow === true
  269. const projectRules = (project.rules ?? []).filter(r => {
  270. if (r.decision !== 'allow' || trust) return true
  271. notes.push(`project rule ${r.id} (allow) ignored: set trustProjectAllow in your user file to honour project allow rules`)
  272. return false
  273. })
  274. // A setting the project names may only move toward caution.
  275. const stricter = <T>(order: readonly T[], a: T | undefined, b: T | undefined, fallback: T): T => {
  276. const base = a ?? fallback
  277. if (b === undefined || trust) return b ?? base
  278. return order.indexOf(b) > order.indexOf(base) ? b : base
  279. }
  280. const config: Config = {
  281. mode: stricter(['audit', 'enforce'] as const, user.mode, project.mode, DEFAULT_CONFIG.mode),
  282. default: stricter(['allow', 'passthrough', 'jev', 'ask', 'deny'] as const, user.default, project.default, DEFAULT_CONFIG.default),
  283. // the mod's own dialog (with its headless deny) is the stricter of the two
  284. askWith: stricter(['engine', 'mod'] as const, user.askWith, project.askWith, DEFAULT_CONFIG.askWith),
  285. headless: stricter(['allow', 'deny'] as const, user.headless, project.headless, DEFAULT_CONFIG.headless),
  286. opaqueShell: stricter(['allow', 'ask', 'deny'] as const, user.opaqueShell, project.opaqueShell, DEFAULT_CONFIG.opaqueShell),
  287. trustProjectAllow: trust,
  288. rules: [...(user.rules ?? []), ...projectRules],
  289. jev: {
  290. ...DEFAULT_JEV,
  291. ...user.jev,
  292. ...(project.jev ? tightenJev(user.jev ?? {}, project.jev, trust) : {}),
  293. hazards: { ...DEFAULT_JEV.hazards, ...user.jev?.hazards, ...tightenHazards(user.jev?.hazards ?? {}, project.jev?.hazards ?? {}, trust) },
  294. },
  295. }
  296. return { config, notes }
  297. }
  298. const RANK: Record<Decision, number> = { allow: 0, ask: 1, deny: 2 }
  299. function tightenHazards(
  300. user: Partial<Record<Hazard, Decision>>,
  301. project: Partial<Record<Hazard, Decision>>,
  302. trust: boolean,
  303. ): Partial<Record<Hazard, Decision>> {
  304. const out: Partial<Record<Hazard, Decision>> = {}
  305. for (const [name, value] of Object.entries(project) as [Hazard, Decision][]) {
  306. const base = user[name] ?? DEFAULT_JEV.hazards[name]
  307. out[name] = trust || RANK[value] > RANK[base] ? value : base
  308. }
  309. return out
  310. }
  311. function tightenJev(user: Partial<JevConfig>, project: Partial<JevConfig>, trust: boolean): Partial<JevConfig> {
  312. if (trust) {
  313. const { hazards: _h, ...rest } = project
  314. return rest
  315. }
  316. const out: Partial<JevConfig> = {}
  317. // more tools judged, lower thresholds: both are more cautious
  318. if (project.tools) out.tools = [...new Set([...(user.tools ?? DEFAULT_JEV.tools), ...project.tools])]
  319. if (project.threshold !== undefined) out.threshold = Math.min(project.threshold, user.threshold ?? DEFAULT_JEV.threshold)
  320. if (project.askThreshold !== undefined)
  321. out.askThreshold = Math.min(project.askThreshold, user.askThreshold ?? DEFAULT_JEV.askThreshold)
  322. if (project.severityDeny !== undefined)
  323. out.severityDeny = Math.min(project.severityDeny, user.severityDeny ?? DEFAULT_JEV.severityDeny)
  324. return out
  325. }
  326. // ---------------------------------------------------------------------------
  327. // Matching
  328. export function list(v: string | string[] | undefined): string[] {
  329. return v === undefined ? [] : Array.isArray(v) ? v : [v]
  330. }
  331. const cache = new Map<string, RegExp>()
  332. /**
  333. * A glob as a regex. In `path` mode `*` stays inside one segment and `**`
  334. * crosses them; elsewhere `*` matches anything, spaces included, so
  335. * `git push*--force*` reads the way it is written. `?` is one character.
  336. */
  337. export function globToRegex(glob: string, mode: 'path' | 'text' = 'text'): RegExp {
  338. const key = `${mode}:${glob}`
  339. const hit = cache.get(key)
  340. if (hit) return hit
  341. let out = ''
  342. for (let i = 0; i < glob.length; i++) {
  343. const c = glob[i]!
  344. if (c === '*') {
  345. if (mode === 'path' && glob[i + 1] === '*') {
  346. // `**/` also matches no directory at all
  347. if (glob[i + 2] === '/') {
  348. out += '(?:.*/)?'
  349. i += 2
  350. } else {
  351. out += '.*'
  352. i += 1
  353. }
  354. } else out += mode === 'path' ? '[^/]*' : '.*'
  355. } else if (c === '?') out += mode === 'path' ? '[^/]' : '.'
  356. else out += c.replace(/[.+^${}()|[\]\\]/g, '\\$&')
  357. }
  358. const re = new RegExp(`^${out}$`, mode === 'text' ? 's' : '')
  359. cache.set(key, re)
  360. return re
  361. }
  362. export const globMatch = (glob: string, value: string, mode: 'path' | 'text' = 'text') => globToRegex(glob, mode).test(value)
  363. /**
  364. * The simple commands of a Bash line: split on `&&`, `||`, `;`, `|`, `&` and
  365. * newlines outside quotes, with `$( … )` and backtick bodies checked as parts
  366. * of their own. A deny rule hits a line when it hits any part; an allow rule
  367. * only when it covers every part, so `ls && rm -rf ~` is not allowed by an
    • Note: Quoted as data in code: Runs `rm -rf` against a root or home-directory path
    • Line 400 on GitHub
  368. * allow on `ls*`.
  369. */
  370. /** `[wrappers and VAR=x …] [/path/]bash|sh|zsh [flags] -c '<script>'`, the script captured. */
  371. const SHELL_C =
  372. /^(?:(?:sudo|env|nohup|time|command|builtin|exec|nice|stdbuf|timeout|xargs|doas)(?:\s+(?:-\S+|\d\S*))*\s+|[A-Za-z_][A-Za-z0-9_]*=\S*\s+)*(?:\S*\/)?(?:ba|z|da|k|fi)?sh\s+(?:-[a-zA-Z]+\s+)*-[a-zA-Z]*c[a-zA-Z]*\s+(['"])([\s\S]*)\1/
  373. export function bashParts(command: string): string[] {
  374. const parts: string[] = []
  375. const nested: string[] = []
  376. let current = ''
  377. let quote: '"' | "'" | null = null
  378. for (let i = 0; i < command.length; i++) {
  379. const c = command[i]!
  380. // `$( … )` and backticks run inside double quotes too (not inside single quotes)
  381. if (quote !== "'" && c === '$' && command[i + 1] === '(') {
  382. let depth = 1
  383. let j = i + 2
  384. for (; j < command.length && depth > 0; j++) {
  385. if (command[j] === '(') depth += 1
  386. else if (command[j] === ')') depth -= 1
  387. }
  388. nested.push(command.slice(i + 2, j - 1))
  389. current += command.slice(i, j)
  390. i = j - 1
  391. continue
  392. }
  393. if (quote !== "'" && c === '`') {
  394. const end = command.indexOf('`', i + 1)
  395. nested.push(end === -1 ? command.slice(i + 1) : command.slice(i + 1, end))
  396. current += end === -1 ? command.slice(i) : command.slice(i, end + 1)
  397. i = end === -1 ? command.length : end
  398. continue
  399. }
  400. if (quote) {
  401. if (c === quote) quote = null
  402. else if (c === '\\' && quote === '"') {
  403. current += c + (command[i + 1] ?? '')
  404. i += 1
  405. continue
  406. }
  407. current += c
  408. continue
  409. }
  410. if (c === "'" || c === '"') {
  411. quote = c
  412. current += c
  413. continue
  414. }
  415. if (c === '\\') {
  416. current += c + (command[i + 1] ?? '')
  417. i += 1
  418. continue
  419. }
  420. const two = command.slice(i, i + 2)
  421. if (two === '&&' || two === '||') {
  422. parts.push(current)
  423. current = ''
  424. i += 1
  425. continue
  426. }
  427. if (c === ';' || c === '|' || c === '\n' || (c === '&' && command[i + 1] !== '>' && command[i - 1] !== '>')) {
  428. parts.push(current)
  429. current = ''
  430. continue
  431. }
  432. current += c
  433. }
  434. parts.push(current)
  435. // `bash -c '…'` / `sh -c "…"`: the quoted script is a command line of its own
  436. for (const part of parts) {
  437. const m = SHELL_C.exec(part.trim())
  438. if (m) nested.push(m[2]!)
  439. }
  440. return [...parts, ...nested.flatMap(bashParts)].map(p => p.trim().replace(/\s+/g, ' ')).filter(Boolean)
  441. }
  442. /**
  443. * A part as the shell will run it, for matching: `$IFS` read as a space,
  444. * backslash escapes and quotes dropped, so `r'm' -rf` and `rm${IFS}-rf` read
  445. * as `rm -rf`.
  446. */
  447. export function normalizePart(part: string): string {
  448. return part
  449. .replace(/\$\{IFS[^}]*\}|\$IFS\b/g, ' ')
  450. .replace(/\\(.)/g, '$1')
  451. .replace(/['"]/g, '')
  452. .replace(/\s+/g, ' ')
  453. .trim()
  454. }
  455. const WRAPPERS = new Set(['sudo', 'env', 'nohup', 'time', 'command', 'builtin', 'exec', 'nice', 'stdbuf', 'timeout', 'xargs'])
  456. /** The part without leading `VAR=value` assignments and wrappers (`sudo`, `env`, `nohup` …). */
  457. export function strippedPart(part: string): string {
  458. const words = normalizePart(part).split(' ')
  459. let i = 0
  460. while (i < words.length) {
  461. const w = words[i]!
  462. if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(w) || WRAPPERS.has(w)) i += 1
  463. else if (i > 0 && WRAPPERS.has(words[i - 1]!) && /^-/.test(w)) i += 1
  464. else break
  465. }
  466. return words.slice(i).join(' ')
  467. }
  468. /** `NAME=value` assignments made anywhere in a command line, for reading `$NAME` back. */
  469. export function assignments(command: string): Record<string, string> {
  470. const vars: Record<string, string> = {}
  471. for (const part of bashParts(command)) {
  472. for (const word of normalizePart(part).split(' ')) {
  473. const m = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(word)
  474. if (m) vars[m[1]!] = m[2]!
  475. else break
  476. }
  477. }
  478. return vars
  479. }
  480. /** `$NAME` / `${NAME}` replaced by what the same command line assigned it. */
  481. export function substitute(part: string, vars: Record<string, string>): string {
  482. return part.replace(/\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?/g, (whole, name: string) => vars[name] ?? whole)
  483. }
  484. /** Every reading of a part a rule should see: as written, normalized, unwrapped, and with its variables read back. */
  485. export function readings(part: string, vars: Record<string, string> = {}): string[] {
  486. const resolved = substitute(part, vars)
  487. return [...new Set([part, normalizePart(part), strippedPart(part), normalizePart(resolved), strippedPart(resolved)].filter(Boolean))]
  488. }
  489. /**
  490. * Whether the program a part runs is only known at run time (`$X -rf /`,
  491. * `eval "$cmd"`, `source ./x`): no rule can read what it will do.
  492. */
  493. export function opaquePart(part: string, vars: Record<string, string> = {}): boolean {
  494. const word = strippedPart(substitute(part, vars)).split(' ')[0] ?? ''
  495. return /^[$`]/.test(word) || word === 'eval' || word === 'source' || word === '.'
  496. }
  497. /** One governed action, with what each matcher reads already pulled out. */
  498. export type Action = {
  499. kind: ActionKind
  500. /** The tool name; for command/agent/skill, `/name`, `Agent`, `Skill`. */
  501. tool: string
  502. input: Record<string, unknown>
  503. /** Command name (without slash) for kind command. */
  504. command?: string
  505. /** Skill name for the Skill tool, a /skill, or a preloaded skill. */
  506. skill?: string
  507. /** Subagent type for kind agent (or the Agent tool's subagent_type). */
  508. agent?: string
  509. /** The loop it runs in: undefined on main. */
  510. agentId?: string
  511. }
  512. const str = (v: unknown) => (typeof v === 'string' ? v : undefined)
  513. /** A tool call as an Action, with the fields the matchers need. */
  514. export function toolAction(tool: string, input: Record<string, unknown>, agentId?: string): Action {
  515. return {
  516. kind: 'tool',
  517. tool,
  518. input,
  519. agentId,
  520. skill: tool === 'Skill' ? str(input.skill) ?? str(input.command) : undefined,
  521. agent: tool === 'Agent' || tool === 'Task' ? str(input.subagent_type) ?? 'general-purpose' : undefined,
  522. }
  523. }
  524. /**
  525. * The path-like arguments of a Bash command: every word after the program in
  526. * every part (read as the shell runs it), `--opt=value` values and redirect
  527. * targets included, so a `path` rule sees `cat ~/.aws/credentials` too.
  528. */
  529. export function bashPathArgs(command: string): string[] {
  530. const out: string[] = []
  531. const vars = assignments(command)
  532. for (const part of bashParts(command)) {
  533. const words = strippedPart(substitute(part, vars)).split(' ').slice(1)
  534. for (let w of words) {
  535. w = w.replace(/^[0-9]*[<>]+&?/, '')
  536. if (w.startsWith('-')) {
  537. const eq = w.indexOf('=')
  538. if (eq === -1) continue
  539. w = w.slice(eq + 1)
  540. }
  541. w = w.replace(/^\.\//, '').replace(/[;,)]+$/, '')
  542. if (w && !/^[0-9]+$/.test(w)) out.push(w)
  543. }
  544. }
  545. return out
  546. }
  547. export function paths(a: Action, root: string, home: string | undefined): string[] {
  548. const command = a.tool === 'Bash' ? str(a.input.command) : undefined
  549. const raw = [
  550. ...[a.input.file_path, a.input.path, a.input.notebook_path].map(str).filter((p): p is string => !!p),
  551. ...(command !== undefined ? bashPathArgs(command) : []),
  552. ]
  553. const out = new Set<string>()
  554. for (let p of raw) {
  555. if (home && (p === '~' || p.startsWith('~/'))) p = home + p.slice(1)
  556. out.add(p)
  557. const r = root.replace(/\/+$/, '')
  558. if (p.startsWith(`${r}/`)) out.add(p.slice(r.length + 1))
  559. else if (!p.startsWith('/')) out.add(`${r}/${p}`)
  560. }
  561. return [...out]
  562. }
  563. export function hostOf(a: Action): string | undefined {
  564. const url = str(a.input.url)
  565. if (url) {
  566. // the authority, less any `user:pass@` (https://allowed.com@evil.com goes to evil.com) and port
  567. const m = /^[a-z][a-z0-9+.-]*:\/\/([^/?#]*)/i.exec(url.trim())
  568. if (m) {
  569. const host = m[1]!.slice(m[1]!.lastIndexOf('@') + 1).replace(/:\d*$/, '').replace(/^\[|\]$/g, '')
  570. return host.toLowerCase().replace(/\.$/, '')
  571. }
  572. }
  573. return str(a.input.domain)?.toLowerCase()
  574. }
  575. function expandHome(glob: string, home: string | undefined): string {
  576. return home && (glob === '~' || glob.startsWith('~/')) ? home + glob.slice(1) : glob
  577. }
  578. export type MatchContext = { root: string; home?: string }
  579. /**
  580. * Whether a rule applies to an action. Every matcher the rule names must hit
  581. * (they AND together); within one matcher any listed glob may hit (OR). A
  582. * matcher that cannot apply to this kind of action (a `bash` glob on a
  583. * Write) makes the rule miss rather than match vacuously.
  584. */
  585. export function ruleMatches(rule: Rule, a: Action, ctx: MatchContext): boolean {
  586. if (rule.scope === 'main' && a.agentId) return false
  587. if (rule.scope === 'subagents' && !a.agentId) return false
  588. if (rule.tool !== undefined && !list(rule.tool).some(g => globMatch(g, a.tool))) return false
  589. if (rule.mcpServer !== undefined) {
  590. const m = /^mcp__(.+?)__/.exec(a.tool)
  591. if (!m || !list(rule.mcpServer).some(g => globMatch(g, m[1]!))) return false
  592. }
  593. if (rule.bash !== undefined || rule.bashRegex !== undefined) {
  594. const command = a.tool === 'Bash' ? str(a.input.command) : undefined
  595. if (command === undefined) return false
  596. const parts = bashParts(command.slice(0, MAX_MATCH_CHARS))
  597. const vars = assignments(command.slice(0, MAX_MATCH_CHARS))
  598. const one = (text: string) =>
  599. list(rule.bash).some(g => globMatch(g, text)) || list(rule.bashRegex).some(r => new RegExp(r).test(text))
  600. // deny/ask read every spelling of a part (written, unquoted, unwrapped);
  601. // an allow must hold for the part as the shell will run it
  602. const hits = (part: string) => (rule.decision === 'allow' ? one(normalizePart(part)) : readings(part, vars).some(one))
  603. // deny/ask: any part is enough; allow: every part must be covered
  604. if (rule.decision === 'allow' ? !parts.every(hits) : !parts.some(hits)) return false
  605. }
  606. if (rule.path !== undefined) {
  607. const hit = (p: string) => list(rule.path).some(g => globMatch(expandHome(g, ctx.home), p, 'path'))
  608. const command = a.tool === 'Bash' ? str(a.input.command) : undefined
  609. if (command !== undefined && rule.decision === 'allow') {
  610. // an allow must hold for every argument, or `rm -rf / src/a` would ride on `src/**`
    • Note: Quoted as data in code: Runs `rm -rf` against a root or home-directory path
    • Line 661 on GitHub
  611. const args = bashPathArgs(command)
  612. if (!args.length || !args.every(arg => paths(toolAction('Read', { file_path: arg }), ctx.root, ctx.home).some(hit))) return false
  613. } else {
  614. const ps = paths(a, ctx.root, ctx.home)
  615. if (!ps.length || !ps.some(hit)) return false
  616. }
  617. }
  618. if (rule.domain !== undefined) {
  619. const host = hostOf(a)
  620. if (!host || !list(rule.domain).some(g => globMatch(g.toLowerCase(), host) || (g.startsWith('*.') && host === g.slice(2).toLowerCase())))
  621. return false
  622. }
  623. if (rule.skill !== undefined && !(a.skill && list(rule.skill).some(g => globMatch(g, a.skill!)))) return false
  624. if (rule.command !== undefined && !(a.kind === 'command' && a.command && list(rule.command).some(g => globMatch(g, a.command!))))
  625. return false
  626. if (rule.agent !== undefined && !(a.agent && list(rule.agent).some(g => globMatch(g, a.agent!)))) return false
  627. if (rule.inputRegex !== undefined) {
  628. const json = JSON.stringify(a.input).slice(0, MAX_MATCH_CHARS)
  629. if (!list(rule.inputRegex).some(r => new RegExp(r).test(json))) return false
  630. }
  631. return true
  632. }
  633. export type Verdict =
  634. | { source: 'rule'; decision: Decision; rule: Rule; reason: string }
  635. | { source: 'fallback'; fallback: Fallback }
  636. /** deny beats ask beats allow, whatever order the rules were written in. */
  637. export function evaluate(config: Config, a: Action, ctx: MatchContext): Verdict {
  638. let best: Rule | undefined
  639. for (const rule of config.rules) {
  640. if (!ruleMatches(rule, a, ctx)) continue
  641. if (!best || RANK[rule.decision] > RANK[best.decision]) best = rule
  642. if (best.decision === 'deny') break
  643. }
  644. // An input too long to match safely (regexes are synchronous) is asked about, never waved through.
  645. if ((!best || RANK[best.decision] < RANK.ask) && JSON.stringify(a.input).length > MAX_MATCH_CHARS) {
  646. best = { id: 'too-long', decision: 'ask', reason: `jev-auto-mode: this input is over ${MAX_MATCH_CHARS} characters, too long to check against the rules` }
  647. }
  648. // A program only known at run time slips past every bash matcher: give it at least opaqueShell.
  649. const command = a.tool === 'Bash' ? str(a.input.command) : undefined
  650. if (command !== undefined && (!best || RANK[best.decision] < RANK[config.opaqueShell]) && bashParts(command).some(part => opaquePart(part, assignments(command)))) {
  651. best = {
  652. id: 'opaque-shell',
  653. decision: config.opaqueShell,
  654. reason: `jev-auto-mode: this command runs a program only known at run time (a variable, eval or source), which no rule can check`,
  655. }
  656. }
  657. if (best) {
  658. return {
  659. source: 'rule',
  660. decision: best.decision,
  661. rule: best,
  662. reason: best.reason ?? `rule ${best.id} (${best.decision})`,
  663. }
  664. }
  665. return { source: 'fallback', fallback: config.default }
  666. }
  667. /** Whether the judge is asked about this action when no rule decided. */
  668. export function judged(config: Config, a: Action): boolean {
  669. return config.default === 'jev' && (a.kind !== 'tool' || config.jev.tools.some(g => globMatch(g, a.tool)))
  670. }
  671. // ---------------------------------------------------------------------------
  672. // Self-protection: Claude may not rewrite the policy that governs it.
  673. /** Tools that only read, and tools whose input is prose (a prompt, a question), not an operation. */
  674. const HARMLESS_TOOLS = new Set(['Read', 'Glob', 'Grep', 'LS', 'Agent', 'Task', 'TodoWrite', 'AskUserQuestion', 'WebSearch', 'Skill', 'ToolSearch'])
  675. const READ_ONLY_COMMAND = /^(cat|less|more|head|tail|grep|rg|jq|ls|stat|wc|diff|file|git (diff|log|show|status|blame))(\s|$)/
  676. /** Options by which a "read" command writes a file: `git show --output=x`, `less -o x`, `sed -i`, `sort -o x` … */
  677. const WRITE_OPTION = /\s(-[a-zA-Z]*[oOiw][a-zA-Z]*|--(output|out|log-file|in-place)[\w-]*)(=|\s|$)/
  678. /**
  679. * A reason to refuse an action that would change this mod's policy or the mod
  680. * itself, or undefined. It runs before any rule, so no rule (and no project
  681. * file) can switch it off. It errs toward refusing: any tool but a reader
  682. * whose input names the policy file or the mod's directory is refused, and so
  683. * is any shell part naming them that is not a plain read (`cat`, `grep`, …).
  684. */
  685. export function selfProtection(a: Action, pluginRoot: string, ctx: MatchContext, policyFiles: readonly string[] = []): string | undefined {
  686. const root = pluginRoot.replace(/\/+$/, '')
  687. // the active policy files by every spelling a command might use: absolute, ~/…, relative to the project, bare name
  688. const needles = new Set<string>([CONFIG_NAME])
  689. for (const f of policyFiles) {
  690. if (!f) continue
  691. needles.add(f)
  692. const name = f.slice(f.lastIndexOf('/') + 1)
  693. if (name) needles.add(name)
  694. if (ctx.home && f.startsWith(`${ctx.home}/`)) needles.add(`~${f.slice(ctx.home.length)}`)
  695. const r = ctx.root.replace(/\/+$/, '')
  696. if (f.startsWith(`${r}/`)) needles.add(f.slice(r.length + 1))
  697. }
  698. const mentions = (text: string) =>
  699. [...needles].some(n => text.includes(n)) || (root !== '' && text.includes(root)) || /jev-auto-mode\/(hooks|\.claude-plugin|examples)\b/.test(text)
  700. if (a.kind !== 'tool' || HARMLESS_TOOLS.has(a.tool)) return undefined
  701. if (a.tool === 'Bash') {
  702. const command = str(a.input.command) ?? ''
  703. const touches = bashParts(command).some(part => {
  704. const plain = normalizePart(part)
  705. if (!mentions(plain)) return false
  706. return !(READ_ONLY_COMMAND.test(strippedPart(part)) && !/[>]/.test(plain) && !WRITE_OPTION.test(plain))
  707. })
  708. return touches ? `jev-auto-mode: shell commands that touch ${CONFIG_NAME} or the mod beyond reading them are refused; edit it yourself` : undefined
  709. }
  710. // path-like values only (no whitespace): a file's content that merely mentions the name is fine
  711. const values: string[] = []
  712. const walk = (v: unknown): void => {
  713. if (typeof v === 'string') {
  714. if (!/\s/.test(v.trim())) values.push(v.trim())
  715. } else if (Array.isArray(v)) v.forEach(walk)
  716. else if (v && typeof v === 'object') Object.values(v).forEach(walk)
  717. }
  718. walk(a.input)
  719. if (values.some(mentions) || paths(a, ctx.root, ctx.home).some(p => mentions(p))) {
  720. return `jev-auto-mode: ${CONFIG_NAME} and the mod's own files can only be edited by the person, not by Claude`
  721. }
  722. return undefined
  723. }
  724. /** One line for the log: what the action is. */
  725. export function describeAction(a: Action): string {
  726. if (a.kind === 'command') return `/${a.command}${a.input.args ? ` ${a.input.args}` : ''}`
  727. if (a.kind === 'agent') return `Agent(${a.agent})`
  728. if (a.kind === 'skill') return `skill ${a.skill}`
  729. const detail =
  730. str(a.input.command) ??
  731. str(a.input.file_path) ??
  732. str(a.input.notebook_path) ??
  733. str(a.input.path) ??
  734. str(a.input.url) ??
  735. str(a.input.skill) ??
  736. str(a.input.pattern) ??
  737. str(a.input.description) ??
  738. ''
  739. const one = detail.replace(/\s+/g, ' ').trim()
  740. return one ? `${a.tool}(${one.length > 80 ? `${one.slice(0, 79)}…` : one})` : a.tool
  741. }
cli-tool/components/skills/ai-research/loki-mode/autonomy/run.sh · Bundled script · 1,991 lines · 3 findings marked
  1. #!/bin/bash
  2. #===============================================================================
  3. # Loki Mode - Autonomous Runner
  4. # Single script that handles prerequisites, setup, and autonomous execution
  5. #
  6. # Usage:
  7. # ./autonomy/run.sh [PRD_PATH]
  8. # ./autonomy/run.sh ./docs/requirements.md
  9. # ./autonomy/run.sh # Interactive mode
  10. #
  11. # Environment Variables:
  12. # LOKI_MAX_RETRIES - Max retry attempts (default: 50)
  13. # LOKI_BASE_WAIT - Base wait time in seconds (default: 60)
  14. # LOKI_MAX_WAIT - Max wait time in seconds (default: 3600)
  15. # LOKI_SKIP_PREREQS - Skip prerequisite checks (default: false)
  16. # LOKI_DASHBOARD - Enable web dashboard (default: true)
  17. # LOKI_DASHBOARD_PORT - Dashboard port (default: 57374)
  18. #
  19. # Resource Monitoring (prevents system overload):
  20. # LOKI_RESOURCE_CHECK_INTERVAL - Check resources every N seconds (default: 300 = 5min)
  21. # LOKI_RESOURCE_CPU_THRESHOLD - CPU % threshold to warn (default: 80)
  22. # LOKI_RESOURCE_MEM_THRESHOLD - Memory % threshold to warn (default: 80)
  23. #
  24. # Security & Autonomy Controls (Enterprise):
  25. # LOKI_STAGED_AUTONOMY - Require approval before execution (default: false)
  26. # LOKI_AUDIT_LOG - Enable audit logging (default: false)
  27. # LOKI_MAX_PARALLEL_AGENTS - Limit concurrent agent spawning (default: 10)
  28. # LOKI_SANDBOX_MODE - Run in sandboxed container (default: false, requires Docker)
  29. # LOKI_ALLOWED_PATHS - Comma-separated paths agents can modify (default: all)
  30. # 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 30 on GitHub
  31. #
  32. # SDLC Phase Controls (all enabled by default, set to 'false' to skip):
  33. # LOKI_PHASE_UNIT_TESTS - Run unit tests (default: true)
  34. # LOKI_PHASE_API_TESTS - Functional API testing (default: true)
  35. # LOKI_PHASE_E2E_TESTS - E2E/UI testing with Playwright (default: true)
  36. # LOKI_PHASE_SECURITY - Security scanning OWASP/auth (default: true)
  37. # LOKI_PHASE_INTEGRATION - Integration tests SAML/OIDC/SSO (default: true)
  38. # LOKI_PHASE_CODE_REVIEW - 3-reviewer parallel code review (default: true)
  39. # LOKI_PHASE_WEB_RESEARCH - Competitor/feature gap research (default: true)
  40. # LOKI_PHASE_PERFORMANCE - Load/performance testing (default: true)
  41. # LOKI_PHASE_ACCESSIBILITY - WCAG compliance testing (default: true)
  42. # LOKI_PHASE_REGRESSION - Regression testing (default: true)
  43. # LOKI_PHASE_UAT - UAT simulation (default: true)
  44. #
  45. # Autonomous Loop Controls (Ralph Wiggum Mode):
  46. # LOKI_COMPLETION_PROMISE - EXPLICIT stop condition text (default: none - runs forever)
  47. # Example: "ALL TESTS PASSING 100%"
  48. # Only stops when Claude outputs this EXACT text
  49. # LOKI_MAX_ITERATIONS - Max loop iterations before exit (default: 1000)
  50. # LOKI_PERPETUAL_MODE - Ignore ALL completion signals (default: false)
  51. # Set to 'true' for truly infinite operation
  52. #===============================================================================
  53. set -uo pipefail
  54. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  55. PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  56. #===============================================================================
  57. # Self-Copy Protection
  58. # Bash reads scripts incrementally, so editing a running script corrupts execution.
  59. # Solution: Copy ourselves to /tmp and run from there. The original can be safely edited.
  60. #===============================================================================
  61. if [[ -z "${LOKI_RUNNING_FROM_TEMP:-}" ]]; then
  62. TEMP_SCRIPT="/tmp/loki-run-$$.sh"
  63. cp "${BASH_SOURCE[0]}" "$TEMP_SCRIPT"
  64. chmod +x "$TEMP_SCRIPT"
  65. export LOKI_RUNNING_FROM_TEMP=1
  66. export LOKI_ORIGINAL_SCRIPT_DIR="$SCRIPT_DIR"
  67. export LOKI_ORIGINAL_PROJECT_DIR="$PROJECT_DIR"
  68. exec "$TEMP_SCRIPT" "$@"
  69. fi
  70. # Restore original paths when running from temp
  71. SCRIPT_DIR="${LOKI_ORIGINAL_SCRIPT_DIR:-$SCRIPT_DIR}"
  72. PROJECT_DIR="${LOKI_ORIGINAL_PROJECT_DIR:-$PROJECT_DIR}"
  73. # Clean up temp script on exit
  74. trap 'rm -f "${BASH_SOURCE[0]}" 2>/dev/null' EXIT
  75. # Configuration
  76. MAX_RETRIES=${LOKI_MAX_RETRIES:-50}
  77. BASE_WAIT=${LOKI_BASE_WAIT:-60}
  78. MAX_WAIT=${LOKI_MAX_WAIT:-3600}
  79. SKIP_PREREQS=${LOKI_SKIP_PREREQS:-false}
  80. ENABLE_DASHBOARD=${LOKI_DASHBOARD:-true}
  81. DASHBOARD_PORT=${LOKI_DASHBOARD_PORT:-57374}
  82. RESOURCE_CHECK_INTERVAL=${LOKI_RESOURCE_CHECK_INTERVAL:-300} # Check every 5 minutes
  83. RESOURCE_CPU_THRESHOLD=${LOKI_RESOURCE_CPU_THRESHOLD:-80} # CPU % threshold
  84. RESOURCE_MEM_THRESHOLD=${LOKI_RESOURCE_MEM_THRESHOLD:-80} # Memory % threshold
  85. # Security & Autonomy Controls
  86. STAGED_AUTONOMY=${LOKI_STAGED_AUTONOMY:-false} # Require plan approval
  87. AUDIT_LOG_ENABLED=${LOKI_AUDIT_LOG:-false} # Enable audit logging
  88. MAX_PARALLEL_AGENTS=${LOKI_MAX_PARALLEL_AGENTS:-10} # Limit concurrent agents
  89. SANDBOX_MODE=${LOKI_SANDBOX_MODE:-false} # Docker sandbox mode
  90. ALLOWED_PATHS=${LOKI_ALLOWED_PATHS:-""} # Empty = all paths allowed
  91. BLOCKED_COMMANDS=${LOKI_BLOCKED_COMMANDS:-"rm -rf /,dd if=,mkfs,:(){ :|:& };:"}
    • Note: Quoted as data in code: Matches a classic shell fork-bomb pattern
    • Line 98 on GitHub
  92. STATUS_MONITOR_PID=""
  93. DASHBOARD_PID=""
  94. RESOURCE_MONITOR_PID=""
  95. # SDLC Phase Controls (all enabled by default)
  96. PHASE_UNIT_TESTS=${LOKI_PHASE_UNIT_TESTS:-true}
  97. PHASE_API_TESTS=${LOKI_PHASE_API_TESTS:-true}
  98. PHASE_E2E_TESTS=${LOKI_PHASE_E2E_TESTS:-true}
  99. PHASE_SECURITY=${LOKI_PHASE_SECURITY:-true}
  100. PHASE_INTEGRATION=${LOKI_PHASE_INTEGRATION:-true}
  101. PHASE_CODE_REVIEW=${LOKI_PHASE_CODE_REVIEW:-true}
  102. PHASE_WEB_RESEARCH=${LOKI_PHASE_WEB_RESEARCH:-true}
  103. PHASE_PERFORMANCE=${LOKI_PHASE_PERFORMANCE:-true}
  104. PHASE_ACCESSIBILITY=${LOKI_PHASE_ACCESSIBILITY:-true}
  105. PHASE_REGRESSION=${LOKI_PHASE_REGRESSION:-true}
  106. PHASE_UAT=${LOKI_PHASE_UAT:-true}
  107. # Autonomous Loop Controls (Ralph Wiggum Mode)
  108. # Default: No auto-completion - runs until max iterations or explicit promise
  109. COMPLETION_PROMISE=${LOKI_COMPLETION_PROMISE:-""}
  110. MAX_ITERATIONS=${LOKI_MAX_ITERATIONS:-1000}
  111. ITERATION_COUNT=0
  112. # Perpetual mode: never stop unless max iterations (ignores all completion signals)
  113. PERPETUAL_MODE=${LOKI_PERPETUAL_MODE:-false}
  114. # Colors
  115. RED='\033[0;31m'
  116. GREEN='\033[0;32m'
  117. YELLOW='\033[1;33m'
  118. BLUE='\033[0;34m'
  119. CYAN='\033[0;36m'
  120. BOLD='\033[1m'
  121. NC='\033[0m'
  122. #===============================================================================
  123. # Logging Functions
  124. #===============================================================================
  125. log_header() {
  126. echo ""
  127. echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}"
  128. echo -e "${BLUE}║${NC} ${BOLD}$1${NC}"
  129. echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}"
  130. }
  131. log_info() { echo -e "${GREEN}[INFO]${NC} $*"; }
  132. log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
  133. log_warning() { log_warn "$@"; } # Alias for backwards compatibility
  134. log_error() { echo -e "${RED}[ERROR]${NC} $*"; }
  135. log_step() { echo -e "${CYAN}[STEP]${NC} $*"; }
  136. #===============================================================================
  137. # Prerequisites Check
  138. #===============================================================================
  139. check_prerequisites() {
  140. log_header "Checking Prerequisites"
  141. local missing=()
  142. # Check Claude Code CLI
  143. log_step "Checking Claude Code CLI..."
  144. if command -v claude &> /dev/null; then
  145. local version=$(claude --version 2>/dev/null | head -1 || echo "unknown")
  146. log_info "Claude Code CLI: $version"
  147. else
  148. missing+=("claude")
  149. log_error "Claude Code CLI not found"
  150. log_info "Install: https://claude.ai/code or npm install -g @anthropic-ai/claude-code"
  151. fi
  152. # Check Python 3
  153. log_step "Checking Python 3..."
  154. if command -v python3 &> /dev/null; then
  155. local py_version=$(python3 --version 2>&1)
  156. log_info "Python: $py_version"
  157. else
  158. missing+=("python3")
  159. log_error "Python 3 not found"
  160. fi
  161. # Check Git
  162. log_step "Checking Git..."
  163. if command -v git &> /dev/null; then
  164. local git_version=$(git --version)
  165. log_info "Git: $git_version"
  166. else
  167. missing+=("git")
  168. log_error "Git not found"
  169. fi
  170. # Check Node.js (optional but recommended)
  171. log_step "Checking Node.js (optional)..."
  172. if command -v node &> /dev/null; then
  173. local node_version=$(node --version)
  174. log_info "Node.js: $node_version"
  175. else
  176. log_warn "Node.js not found (optional, needed for some builds)"
  177. fi
  178. # Check npm (optional)
  179. if command -v npm &> /dev/null; then
  180. local npm_version=$(npm --version)
  181. log_info "npm: $npm_version"
  182. fi
  183. # Check curl (for web fetches)
  184. log_step "Checking curl..."
  185. if command -v curl &> /dev/null; then
  186. log_info "curl: available"
  187. else
  188. missing+=("curl")
  189. log_error "curl not found"
  190. fi
  191. # Check jq (optional but helpful)
  192. log_step "Checking jq (optional)..."
  193. if command -v jq &> /dev/null; then
  194. log_info "jq: available"
  195. else
  196. log_warn "jq not found (optional, for JSON parsing)"
  197. fi
  198. # Summary
  199. echo ""
  200. if [ ${#missing[@]} -gt 0 ]; then
  201. log_error "Missing required tools: ${missing[*]}"
  202. log_info "Please install the missing tools and try again."
  203. return 1
  204. else
  205. log_info "All required prerequisites are installed!"
  206. return 0
  207. fi
  208. }
  209. #===============================================================================
  210. # Skill Installation Check
  211. #===============================================================================
  212. check_skill_installed() {
  213. log_header "Checking Loki Mode Skill"
  214. local skill_locations=(
  215. "$HOME/.claude/skills/loki-mode/SKILL.md"
  216. ".claude/skills/loki-mode/SKILL.md"
  217. "$PROJECT_DIR/SKILL.md"
  218. )
  219. for loc in "${skill_locations[@]}"; do
  220. if [ -f "$loc" ]; then
  221. log_info "Skill found: $loc"
  222. return 0
  223. fi
  224. done
  225. log_warn "Loki Mode skill not found in standard locations"
  226. log_info "The skill will be used from: $PROJECT_DIR/SKILL.md"
  227. if [ -f "$PROJECT_DIR/SKILL.md" ]; then
  228. log_info "Using skill from project directory"
  229. return 0
  230. else
  231. log_error "SKILL.md not found!"
  232. return 1
  233. fi
  234. }
  235. #===============================================================================
  236. # Initialize Loki Directory
  237. #===============================================================================
  238. init_loki_dir() {
  239. log_header "Initializing Loki Mode Directory"
  240. mkdir -p .loki/{state,queue,messages,logs,config,prompts,artifacts,scripts}
  241. mkdir -p .loki/queue
  242. mkdir -p .loki/state/checkpoints
  243. mkdir -p .loki/artifacts/{releases,reports,backups}
  244. mkdir -p .loki/memory/{ledgers,handoffs,learnings,episodic,semantic,skills}
  245. mkdir -p .loki/metrics/{efficiency,rewards}
  246. mkdir -p .loki/rules
  247. mkdir -p .loki/signals
  248. # Initialize queue files if they don't exist
  249. for queue in pending in-progress completed failed dead-letter; do
  250. if [ ! -f ".loki/queue/${queue}.json" ]; then
  251. echo "[]" > ".loki/queue/${queue}.json"
  252. fi
  253. done
  254. # Initialize orchestrator state if it doesn't exist
  255. if [ ! -f ".loki/state/orchestrator.json" ]; then
  256. cat > ".loki/state/orchestrator.json" << EOF
  257. {
  258. "version": "$(cat "$PROJECT_DIR/VERSION" 2>/dev/null || echo "2.2.0")",
  259. "currentPhase": "BOOTSTRAP",
  260. "startedAt": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  261. "agents": {},
  262. "metrics": {
  263. "tasksCompleted": 0,
  264. "tasksFailed": 0,
  265. "retries": 0
  266. }
  267. }
  268. EOF
  269. fi
  270. log_info "Loki directory initialized: .loki/"
  271. }
  272. #===============================================================================
  273. # Task Status Monitor
  274. #===============================================================================
  275. update_status_file() {
  276. # Create a human-readable status file
  277. local status_file=".loki/STATUS.txt"
  278. # Get current phase
  279. local current_phase="UNKNOWN"
  280. if [ -f ".loki/state/orchestrator.json" ]; then
  281. current_phase=$(python3 -c "import json; print(json.load(open('.loki/state/orchestrator.json')).get('currentPhase', 'UNKNOWN'))" 2>/dev/null || echo "UNKNOWN")
  282. fi
  283. # Count tasks in each queue
  284. local pending=0 in_progress=0 completed=0 failed=0
  285. [ -f ".loki/queue/pending.json" ] && pending=$(python3 -c "import json; print(len(json.load(open('.loki/queue/pending.json'))))" 2>/dev/null || echo "0")
  286. [ -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")
  287. [ -f ".loki/queue/completed.json" ] && completed=$(python3 -c "import json; print(len(json.load(open('.loki/queue/completed.json'))))" 2>/dev/null || echo "0")
  288. [ -f ".loki/queue/failed.json" ] && failed=$(python3 -c "import json; print(len(json.load(open('.loki/queue/failed.json'))))" 2>/dev/null || echo "0")
  289. cat > "$status_file" << EOF
  290. ╔════════════════════════════════════════════════════════════════╗
  291. ║ LOKI MODE STATUS ║
  292. ╚════════════════════════════════════════════════════════════════╝
  293. Updated: $(date)
  294. Phase: $current_phase
  295. Tasks:
  296. ├─ Pending: $pending
  297. ├─ In Progress: $in_progress
  298. ├─ Completed: $completed
  299. └─ Failed: $failed
  300. Monitor: watch -n 2 cat .loki/STATUS.txt
  301. EOF
  302. }
  303. start_status_monitor() {
  304. log_step "Starting status monitor..."
  305. # Initial update
  306. update_status_file
  307. update_agents_state
  308. # Background update loop
  309. (
  310. while true; do
  311. update_status_file
  312. update_agents_state
  313. sleep 5
  314. done
  315. ) &
  316. STATUS_MONITOR_PID=$!
  317. log_info "Status monitor started"
  318. log_info "Monitor progress: ${CYAN}watch -n 2 cat .loki/STATUS.txt${NC}"
  319. }
  320. stop_status_monitor() {
  321. if [ -n "$STATUS_MONITOR_PID" ]; then
  322. kill "$STATUS_MONITOR_PID" 2>/dev/null || true
  323. wait "$STATUS_MONITOR_PID" 2>/dev/null || true
  324. fi
  325. stop_resource_monitor
  326. }
  327. #===============================================================================
  328. # Web Dashboard
  329. #===============================================================================
  330. generate_dashboard() {
  331. # Generate HTML dashboard with Anthropic design language + Agent Monitoring
  332. cat > .loki/dashboard/index.html << 'DASHBOARD_HTML'
  333. <!DOCTYPE html>
  334. <html lang="en">
  335. <head>
  336. <meta charset="UTF-8">
  337. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  338. <title>Loki Mode Dashboard</title>
  339. <style>
  340. * { box-sizing: border-box; margin: 0; padding: 0; }
  341. body {
  342. font-family: 'Söhne', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  343. background: #FAF9F6;
  344. color: #1A1A1A;
  345. padding: 24px;
  346. min-height: 100vh;
  347. }
  348. .header {
  349. text-align: center;
  350. padding: 32px 20px;
  351. margin-bottom: 32px;
  352. }
  353. .header h1 {
  354. color: #D97757;
  355. font-size: 28px;
  356. font-weight: 600;
  357. letter-spacing: -0.5px;
  358. margin-bottom: 8px;
  359. }
  360. .header .subtitle {
  361. color: #666;
  362. font-size: 14px;
  363. font-weight: 400;
  364. }
  365. .header .phase {
  366. display: inline-block;
  367. margin-top: 16px;
  368. padding: 8px 16px;
  369. background: #FFF;
  370. border: 1px solid #E5E3DE;
  371. border-radius: 20px;
  372. font-size: 13px;
  373. color: #1A1A1A;
  374. font-weight: 500;
  375. }
  376. .stats {
  377. display: flex;
  378. justify-content: center;
  379. gap: 16px;
  380. margin-bottom: 40px;
  381. flex-wrap: wrap;
  382. }
  383. .stat {
  384. background: #FFF;
  385. border: 1px solid #E5E3DE;
  386. border-radius: 12px;
  387. padding: 20px 32px;
  388. text-align: center;
  389. min-width: 140px;
  390. transition: box-shadow 0.2s ease;
  391. }
  392. .stat:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.06); }
  393. .stat .number { font-size: 36px; font-weight: 600; margin-bottom: 4px; }
  394. .stat .label { font-size: 12px; color: #888; text-transform: uppercase; letter-spacing: 0.5px; }
  395. .stat.pending .number { color: #D97757; }
  396. .stat.progress .number { color: #5B8DEF; }
  397. .stat.completed .number { color: #2E9E6E; }
  398. .stat.failed .number { color: #D44F4F; }
  399. .stat.agents .number { color: #9B6DD6; }
  400. .section-header {
  401. text-align: center;
  402. font-size: 16px;
  403. font-weight: 600;
  404. color: #666;
  405. margin: 40px 0 20px 0;
  406. text-transform: uppercase;
  407. letter-spacing: 1px;
  408. }
  409. .agents-grid {
  410. display: grid;
  411. grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  412. gap: 16px;
  413. max-width: 1400px;
  414. margin: 0 auto 40px auto;
  415. }
  416. .agent-card {
  417. background: #FFF;
  418. border: 1px solid #E5E3DE;
  419. border-radius: 12px;
  420. padding: 16px;
  421. transition: box-shadow 0.2s ease, border-color 0.2s ease;
  422. }
  423. .agent-card:hover {
  424. box-shadow: 0 4px 12px rgba(0,0,0,0.06);
  425. border-color: #9B6DD6;
  426. }
  427. .agent-card .agent-header {
  428. display: flex;
  429. justify-content: space-between;
  430. align-items: flex-start;
  431. margin-bottom: 12px;
  432. }
  433. .agent-card .agent-id {
  434. font-size: 11px;
  435. color: #999;
  436. font-family: monospace;
  437. }
  438. .agent-card .model-badge {
  439. padding: 4px 10px;
  440. border-radius: 6px;
  441. font-size: 10px;
  442. font-weight: 600;
  443. text-transform: uppercase;
  444. letter-spacing: 0.5px;
  445. }
  446. .agent-card .model-badge.sonnet {
  447. background: #E8F0FD;
  448. color: #5B8DEF;
  449. }
  450. .agent-card .model-badge.haiku {
  451. background: #FFF4E6;
  452. color: #F59E0B;
  453. }
  454. .agent-card .model-badge.opus {
  455. background: #F3E8FF;
  456. color: #9B6DD6;
  457. }
  458. .agent-card .agent-type {
  459. font-size: 14px;
  460. font-weight: 600;
  461. color: #1A1A1A;
  462. margin-bottom: 8px;
  463. }
  464. .agent-card .agent-status {
  465. display: inline-block;
  466. padding: 3px 8px;
  467. border-radius: 4px;
  468. font-size: 10px;
  469. font-weight: 500;
  470. margin-bottom: 12px;
  471. }
  472. .agent-card .agent-status.active {
  473. background: #E6F5EE;
  474. color: #2E9E6E;
  475. }
  476. .agent-card .agent-status.completed {
  477. background: #F0EFEA;
  478. color: #666;
  479. }
  480. .agent-card .agent-work {
  481. font-size: 12px;
  482. color: #666;
  483. line-height: 1.5;
  484. margin-bottom: 8px;
  485. }
  486. .agent-card .agent-meta {
  487. display: flex;
  488. gap: 12px;
  489. font-size: 11px;
  490. color: #999;
  491. margin-top: 8px;
  492. padding-top: 8px;
  493. border-top: 1px solid #F0EFEA;
  494. }
  495. .agent-card .agent-meta span {
  496. display: flex;
  497. align-items: center;
  498. gap: 4px;
  499. }
  500. .columns {
  501. display: flex;
  502. gap: 20px;
  503. overflow-x: auto;
  504. padding-bottom: 24px;
  505. max-width: 1400px;
  506. margin: 0 auto;
  507. }
  508. .column {
  509. flex: 1;
  510. min-width: 300px;
  511. max-width: 350px;
  512. background: #FFF;
  513. border: 1px solid #E5E3DE;
  514. border-radius: 12px;
  515. padding: 20px;
  516. }
  517. .column h2 {
  518. font-size: 13px;
  519. font-weight: 600;
  520. color: #666;
  521. margin-bottom: 16px;
  522. display: flex;
  523. align-items: center;
  524. gap: 10px;
  525. text-transform: uppercase;
  526. letter-spacing: 0.5px;
  527. }
  528. .column h2 .count {
  529. background: #F0EFEA;
  530. padding: 3px 10px;
  531. border-radius: 12px;
  532. font-size: 11px;
  533. color: #1A1A1A;
  534. }
  535. .column.pending h2 .count { background: #FCEEE8; color: #D97757; }
  536. .column.progress h2 .count { background: #E8F0FD; color: #5B8DEF; }
  537. .column.completed h2 .count { background: #E6F5EE; color: #2E9E6E; }
  538. .column.failed h2 .count { background: #FCE8E8; color: #D44F4F; }
  539. .task {
  540. background: #FAF9F6;
  541. border: 1px solid #E5E3DE;
  542. border-radius: 8px;
  543. padding: 14px;
  544. margin-bottom: 12px;
  545. transition: border-color 0.2s ease;
  546. }
  547. .task:hover { border-color: #D97757; }
  548. .task .id { font-size: 10px; color: #999; margin-bottom: 6px; font-family: monospace; }
  549. .task .type {
  550. display: inline-block;
  551. background: #FCEEE8;
  552. color: #D97757;
  553. padding: 3px 10px;
  554. border-radius: 4px;
  555. font-size: 11px;
  556. font-weight: 500;
  557. margin-bottom: 8px;
  558. }
  559. .task .title { font-size: 13px; color: #1A1A1A; line-height: 1.5; }
  560. .task .error {
  561. font-size: 11px;
  562. color: #D44F4F;
  563. margin-top: 10px;
  564. padding: 10px;
  565. background: #FCE8E8;
  566. border-radius: 6px;
  567. font-family: monospace;
  568. }
  569. .refresh {
  570. position: fixed;
  571. bottom: 24px;
  572. right: 24px;
  573. background: #D97757;
  574. color: white;
  575. border: none;
  576. padding: 12px 24px;
  577. border-radius: 8px;
  578. cursor: pointer;
  579. font-size: 14px;
  580. font-weight: 500;
  581. transition: background 0.2s ease;
  582. box-shadow: 0 4px 12px rgba(217, 119, 87, 0.3);
  583. }
  584. .refresh:hover { background: #C56747; }
  585. .updated {
  586. text-align: center;
  587. color: #999;
  588. font-size: 12px;
  589. margin-top: 24px;
  590. }
  591. .empty {
  592. color: #999;
  593. font-size: 13px;
  594. text-align: center;
  595. padding: 24px;
  596. font-style: italic;
  597. }
  598. .powered-by {
  599. text-align: center;
  600. margin-top: 40px;
  601. padding-top: 24px;
  602. border-top: 1px solid #E5E3DE;
  603. color: #999;
  604. font-size: 12px;
  605. }
  606. .powered-by span { color: #D97757; font-weight: 500; }
  607. </style>
  608. </head>
  609. <body>
  610. <div class="header">
  611. <h1>LOKI MODE</h1>
  612. <div class="subtitle">Autonomous Multi-Agent Startup System</div>
  613. <div class="phase" id="phase">Loading...</div>
  614. </div>
  615. <div class="stats">
  616. <div class="stat agents"><div class="number" id="agents-count">-</div><div class="label">Active Agents</div></div>
  617. <div class="stat pending"><div class="number" id="pending-count">-</div><div class="label">Pending</div></div>
  618. <div class="stat progress"><div class="number" id="progress-count">-</div><div class="label">In Progress</div></div>
  619. <div class="stat completed"><div class="number" id="completed-count">-</div><div class="label">Completed</div></div>
  620. <div class="stat failed"><div class="number" id="failed-count">-</div><div class="label">Failed</div></div>
  621. </div>
  622. <div class="section-header">Active Agents</div>
  623. <div class="agents-grid" id="agents-grid"></div>
  624. <div class="section-header">Task Queue</div>
  625. <div class="columns">
  626. <div class="column pending"><h2>Pending <span class="count" id="pending-badge">0</span></h2><div id="pending-tasks"></div></div>
  627. <div class="column progress"><h2>In Progress <span class="count" id="progress-badge">0</span></h2><div id="progress-tasks"></div></div>
  628. <div class="column completed"><h2>Completed <span class="count" id="completed-badge">0</span></h2><div id="completed-tasks"></div></div>
  629. <div class="column failed"><h2>Failed <span class="count" id="failed-badge">0</span></h2><div id="failed-tasks"></div></div>
  630. </div>
  631. <div class="updated" id="updated">Last updated: -</div>
  632. <div class="powered-by">Powered by <span>Claude</span></div>
  633. <button class="refresh" onclick="loadData()">Refresh</button>
  634. <script>
  635. async function loadJSON(path) {
  636. try {
  637. const res = await fetch(path + '?t=' + Date.now());
  638. if (!res.ok) return [];
  639. const text = await res.text();
  640. if (!text.trim()) return [];
  641. const data = JSON.parse(text);
  642. return Array.isArray(data) ? data : (data.tasks || data.agents || []);
  643. } catch { return []; }
  644. }
  645. function getModelClass(model) {
  646. if (!model) return 'sonnet';
  647. const m = model.toLowerCase();
  648. if (m.includes('haiku')) return 'haiku';
  649. if (m.includes('opus')) return 'opus';
  650. return 'sonnet';
  651. }
  652. function formatDuration(isoDate) {
  653. if (!isoDate) return 'Unknown';
  654. const start = new Date(isoDate);
  655. const now = new Date();
  656. const seconds = Math.floor((now - start) / 1000);
  657. if (seconds < 60) return seconds + 's';
  658. if (seconds < 3600) return Math.floor(seconds / 60) + 'm';
  659. return Math.floor(seconds / 3600) + 'h ' + Math.floor((seconds % 3600) / 60) + 'm';
  660. }
  661. function renderAgent(agent) {
  662. const modelClass = getModelClass(agent.model);
  663. const modelName = agent.model || 'Sonnet 4.5';
  664. const agentType = agent.agent_type || 'general-purpose';
  665. const status = agent.status === 'completed' ? 'completed' : 'active';
  666. const currentTask = agent.current_task || (agent.tasks_completed && agent.tasks_completed.length > 0
  667. ? 'Completed: ' + agent.tasks_completed.join(', ')
  668. : 'Initializing...');
  669. const duration = formatDuration(agent.spawned_at);
  670. const tasksCount = agent.tasks_completed ? agent.tasks_completed.length : 0;
  671. return `
  672. <div class="agent-card">
  673. <div class="agent-header">
  674. <div class="agent-id">${agent.agent_id || 'Unknown'}</div>
  675. <div class="model-badge ${modelClass}">${modelName}</div>
  676. </div>
  677. <div class="agent-type">${agentType}</div>
  678. <div class="agent-status ${status}">${status}</div>
  679. <div class="agent-work">${currentTask}</div>
  680. <div class="agent-meta">
  681. <span>⏱ ${duration}</span>
  682. <span>✓ ${tasksCount} tasks</span>
  683. </div>
  684. </div>
  685. `;
  686. }
  687. function renderTask(task) {
  688. const payload = task.payload || {};
  689. const title = payload.description || payload.action || task.type || 'Task';
  690. const error = task.lastError ? `<div class="error">${task.lastError}</div>` : '';
  691. return `<div class="task"><div class="id">${task.id}</div><span class="type">${task.type || 'general'}</span><div class="title">${title}</div>${error}</div>`;
  692. }
  693. async function loadData() {
  694. const [pending, progress, completed, failed, agents] = await Promise.all([
  695. loadJSON('../queue/pending.json'),
  696. loadJSON('../queue/in-progress.json'),
  697. loadJSON('../queue/completed.json'),
  698. loadJSON('../queue/failed.json'),
  699. loadJSON('../state/agents.json')
  700. ]);
  701. // Agent stats
  702. document.getElementById('agents-count').textContent = agents.length;
  703. document.getElementById('agents-grid').innerHTML = agents.length
  704. ? agents.map(renderAgent).join('')
  705. : '<div class="empty">No active agents</div>';
  706. // Task stats
  707. document.getElementById('pending-count').textContent = pending.length;
  708. document.getElementById('progress-count').textContent = progress.length;
  709. document.getElementById('completed-count').textContent = completed.length;
  710. document.getElementById('failed-count').textContent = failed.length;
  711. document.getElementById('pending-badge').textContent = pending.length;
  712. document.getElementById('progress-badge').textContent = progress.length;
  713. document.getElementById('completed-badge').textContent = completed.length;
  714. document.getElementById('failed-badge').textContent = failed.length;
  715. document.getElementById('pending-tasks').innerHTML = pending.length ? pending.map(renderTask).join('') : '<div class="empty">No pending tasks</div>';
  716. document.getElementById('progress-tasks').innerHTML = progress.length ? progress.map(renderTask).join('') : '<div class="empty">No tasks in progress</div>';
  717. document.getElementById('completed-tasks').innerHTML = completed.length ? completed.slice(-10).reverse().map(renderTask).join('') : '<div class="empty">No completed tasks</div>';
  718. document.getElementById('failed-tasks').innerHTML = failed.length ? failed.map(renderTask).join('') : '<div class="empty">No failed tasks</div>';
  719. try {
  720. const state = await fetch('../state/orchestrator.json?t=' + Date.now()).then(r => r.json());
  721. document.getElementById('phase').textContent = 'Phase: ' + (state.currentPhase || 'UNKNOWN');
  722. } catch { document.getElementById('phase').textContent = 'Phase: UNKNOWN'; }
  723. document.getElementById('updated').textContent = 'Last updated: ' + new Date().toLocaleTimeString();
  724. }
  725. loadData();
  726. setInterval(loadData, 3000);
  727. </script>
  728. </body>
  729. </html>
  730. DASHBOARD_HTML
  731. }
  732. update_agents_state() {
  733. # Aggregate agent information from .agent/sub-agents/*.json into .loki/state/agents.json
  734. local agents_dir=".agent/sub-agents"
  735. local output_file=".loki/state/agents.json"
  736. # Initialize empty array if no agents directory
  737. if [ ! -d "$agents_dir" ]; then
  738. echo "[]" > "$output_file"
  739. return
  740. fi
  741. # Find all agent JSON files and aggregate them
  742. local agents_json="["
  743. local first=true
  744. for agent_file in "$agents_dir"/*.json; do
  745. # Skip if no JSON files exist
  746. [ -e "$agent_file" ] || continue
  747. # Read agent JSON
  748. local agent_data=$(cat "$agent_file" 2>/dev/null)
  749. if [ -n "$agent_data" ]; then
  750. # Add comma separator for all but first entry
  751. if [ "$first" = true ]; then
  752. first=false
  753. else
  754. agents_json="${agents_json},"
  755. fi
  756. agents_json="${agents_json}${agent_data}"
  757. fi
  758. done
  759. agents_json="${agents_json}]"
  760. # Write aggregated data
  761. echo "$agents_json" > "$output_file"
  762. }
  763. #===============================================================================
  764. # Resource Monitoring
  765. #===============================================================================
  766. check_system_resources() {
  767. # Check CPU and memory usage and write status to .loki/state/resources.json
  768. local output_file=".loki/state/resources.json"
  769. # Get CPU usage (average across all cores)
  770. local cpu_usage=0
  771. if [[ "$OSTYPE" == "darwin"* ]]; then
  772. # macOS: get CPU idle from top header, calculate usage = 100 - idle
  773. 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)}')
  774. cpu_usage=$((100 - ${idle:-0}))
  775. elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
  776. # Linux: use top or mpstat
  777. cpu_usage=$(top -bn2 | grep "Cpu(s)" | tail -1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print int(100 - $1)}')
  778. else
  779. cpu_usage=0
  780. fi
  781. # Get memory usage
  782. local mem_usage=0
  783. if [[ "$OSTYPE" == "darwin"* ]]; then
  784. # macOS: use vm_stat
  785. local page_size=$(pagesize)
  786. local vm_stat=$(vm_stat)
  787. local pages_free=$(echo "$vm_stat" | awk '/Pages free/ {print $3}' | tr -d '.')
  788. local pages_active=$(echo "$vm_stat" | awk '/Pages active/ {print $3}' | tr -d '.')
  789. local pages_inactive=$(echo "$vm_stat" | awk '/Pages inactive/ {print $3}' | tr -d '.')
  790. local pages_speculative=$(echo "$vm_stat" | awk '/Pages speculative/ {print $3}' | tr -d '.')
  791. local pages_wired=$(echo "$vm_stat" | awk '/Pages wired down/ {print $4}' | tr -d '.')
  792. local total_pages=$((pages_free + pages_active + pages_inactive + pages_speculative + pages_wired))
  793. local used_pages=$((pages_active + pages_wired))
  794. mem_usage=$((used_pages * 100 / total_pages))
  795. elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
  796. # Linux: use free
  797. mem_usage=$(free | grep Mem | awk '{print int($3/$2 * 100)}')
  798. else
  799. mem_usage=0
  800. fi
  801. # Determine status
  802. local cpu_status="ok"
  803. local mem_status="ok"
  804. local overall_status="ok"
  805. local warning_message=""
  806. if [ "$cpu_usage" -ge "$RESOURCE_CPU_THRESHOLD" ]; then
  807. cpu_status="high"
  808. overall_status="warning"
  809. warning_message="CPU usage is ${cpu_usage}% (threshold: ${RESOURCE_CPU_THRESHOLD}%). Consider reducing parallel agent count or pausing non-critical tasks."
  810. fi
  811. if [ "$mem_usage" -ge "$RESOURCE_MEM_THRESHOLD" ]; then
  812. mem_status="high"
  813. overall_status="warning"
  814. if [ -n "$warning_message" ]; then
  815. warning_message="${warning_message} Memory usage is ${mem_usage}% (threshold: ${RESOURCE_MEM_THRESHOLD}%)."
  816. else
  817. warning_message="Memory usage is ${mem_usage}% (threshold: ${RESOURCE_MEM_THRESHOLD}%). Consider reducing parallel agent count or cleaning up resources."
  818. fi
  819. fi
  820. # Write JSON status
  821. cat > "$output_file" << EOF
  822. {
  823. "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  824. "cpu": {
  825. "usage_percent": $cpu_usage,
  826. "threshold_percent": $RESOURCE_CPU_THRESHOLD,
  827. "status": "$cpu_status"
  828. },
  829. "memory": {
  830. "usage_percent": $mem_usage,
  831. "threshold_percent": $RESOURCE_MEM_THRESHOLD,
  832. "status": "$mem_status"
  833. },
  834. "overall_status": "$overall_status",
  835. "warning_message": "$warning_message"
  836. }
  837. EOF
  838. # Log warning if resources are high
  839. if [ "$overall_status" = "warning" ]; then
  840. log_warn "RESOURCE WARNING: $warning_message"
  841. fi
  842. }
  843. start_resource_monitor() {
  844. log_step "Starting resource monitor (checks every ${RESOURCE_CHECK_INTERVAL}s)..."
  845. # Initial check
  846. check_system_resources
  847. # Background monitoring loop
  848. (
  849. while true; do
  850. sleep "$RESOURCE_CHECK_INTERVAL"
  851. check_system_resources
  852. done
  853. ) &
  854. RESOURCE_MONITOR_PID=$!
  855. log_info "Resource monitor started (CPU threshold: ${RESOURCE_CPU_THRESHOLD}%, Memory threshold: ${RESOURCE_MEM_THRESHOLD}%)"
  856. log_info "Check status: ${CYAN}cat .loki/state/resources.json${NC}"
  857. }
  858. stop_resource_monitor() {
  859. if [ -n "$RESOURCE_MONITOR_PID" ]; then
  860. kill "$RESOURCE_MONITOR_PID" 2>/dev/null || true
  861. wait "$RESOURCE_MONITOR_PID" 2>/dev/null || true
  862. fi
  863. }
  864. #===============================================================================
  865. # Audit Logging (Enterprise Security)
  866. #===============================================================================
  867. audit_log() {
  868. # Log security-relevant events for enterprise compliance
  869. local event_type="$1"
  870. local event_data="$2"
  871. local audit_file=".loki/logs/audit-$(date +%Y%m%d).jsonl"
  872. if [ "$AUDIT_LOG_ENABLED" != "true" ]; then
  873. return
  874. fi
  875. mkdir -p .loki/logs
  876. local log_entry=$(cat << EOF
  877. {"timestamp":"$(date -u +%Y-%m-%dT%H:%M:%SZ)","event":"$event_type","data":"$event_data","user":"$(whoami)","pid":$$}
  878. EOF
  879. )
  880. echo "$log_entry" >> "$audit_file"
  881. }
  882. check_staged_autonomy() {
  883. # In staged autonomy mode, write plan and wait for approval
  884. local plan_file="$1"
  885. if [ "$STAGED_AUTONOMY" != "true" ]; then
  886. return 0
  887. fi
  888. log_info "STAGED AUTONOMY: Waiting for plan approval..."
  889. log_info "Review plan at: $plan_file"
  890. log_info "Create .loki/signals/PLAN_APPROVED to continue"
  891. audit_log "STAGED_AUTONOMY_WAIT" "plan=$plan_file"
  892. # Wait for approval signal
  893. while [ ! -f ".loki/signals/PLAN_APPROVED" ]; do
  894. sleep 5
  895. done
  896. rm -f ".loki/signals/PLAN_APPROVED"
  897. audit_log "STAGED_AUTONOMY_APPROVED" "plan=$plan_file"
  898. log_success "Plan approved, continuing execution..."
  899. }
  900. check_command_allowed() {
  901. # Check if a command is in the blocked list
  902. local command="$1"
  903. IFS=',' read -ra BLOCKED_ARRAY <<< "$BLOCKED_COMMANDS"
  904. for blocked in "${BLOCKED_ARRAY[@]}"; do
  905. if [[ "$command" == *"$blocked"* ]]; then
  906. audit_log "BLOCKED_COMMAND" "command=$command,pattern=$blocked"
  907. log_error "SECURITY: Blocked dangerous command: $command"
  908. return 1
  909. fi
  910. done
  911. return 0
  912. }
  913. #===============================================================================
  914. # Cross-Project Learnings Database
  915. #===============================================================================
  916. init_learnings_db() {
  917. # Initialize the cross-project learnings database
  918. local learnings_dir="${HOME}/.loki/learnings"
  919. mkdir -p "$learnings_dir"
  920. # Create database files if they don't exist
  921. if [ ! -f "$learnings_dir/patterns.jsonl" ]; then
  922. echo '{"version":"1.0","created":"'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"}' > "$learnings_dir/patterns.jsonl"
  923. fi
  924. if [ ! -f "$learnings_dir/mistakes.jsonl" ]; then
  925. echo '{"version":"1.0","created":"'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"}' > "$learnings_dir/mistakes.jsonl"
  926. fi
  927. if [ ! -f "$learnings_dir/successes.jsonl" ]; then
  928. echo '{"version":"1.0","created":"'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"}' > "$learnings_dir/successes.jsonl"
  929. fi
  930. log_info "Learnings database initialized at: $learnings_dir"
  931. }
  932. save_learning() {
  933. # Save a learning to the cross-project database
  934. local learning_type="$1" # pattern, mistake, success
  935. local category="$2"
  936. local description="$3"
  937. local project="${4:-$(basename "$(pwd)")}"
  938. local learnings_dir="${HOME}/.loki/learnings"
  939. local target_file="$learnings_dir/${learning_type}s.jsonl"
  940. if [ ! -d "$learnings_dir" ]; then
  941. init_learnings_db
  942. fi
  943. local learning_entry=$(cat << EOF
  944. {"timestamp":"$(date -u +%Y-%m-%dT%H:%M:%SZ)","project":"$project","category":"$category","description":"$description"}
  945. EOF
  946. )
  947. echo "$learning_entry" >> "$target_file"
  948. log_info "Saved $learning_type: $category"
  949. }
  950. get_relevant_learnings() {
  951. # Get learnings relevant to the current context
  952. local context="$1"
  953. local learnings_dir="${HOME}/.loki/learnings"
  954. local output_file=".loki/state/relevant-learnings.json"
  955. if [ ! -d "$learnings_dir" ]; then
  956. echo '{"patterns":[],"mistakes":[],"successes":[]}' > "$output_file"
  957. return
  958. fi
  959. # Simple grep-based relevance (can be enhanced with embeddings)
  960. # Pass context via environment variable to avoid quote escaping issues
  961. export LOKI_CONTEXT="$context"
  962. python3 << 'LEARNINGS_SCRIPT'
  963. import json
  964. import os
  965. learnings_dir = os.path.expanduser("~/.loki/learnings")
  966. context = os.environ.get("LOKI_CONTEXT", "").lower()
  967. def load_jsonl(filepath):
  968. entries = []
  969. try:
  970. with open(filepath, 'r') as f:
  971. for line in f:
  972. try:
  973. entry = json.loads(line)
  974. if 'description' in entry:
  975. entries.append(entry)
  976. except:
  977. continue
  978. except:
  979. pass
  980. return entries
  981. def filter_relevant(entries, context, limit=5):
  982. scored = []
  983. for e in entries:
  984. desc = e.get('description', '').lower()
  985. cat = e.get('category', '').lower()
  986. score = sum(1 for word in context.split() if word in desc or word in cat)
  987. if score > 0:
  988. scored.append((score, e))
  989. scored.sort(reverse=True, key=lambda x: x[0])
  990. return [e for _, e in scored[:limit]]
  991. patterns = load_jsonl(f"{learnings_dir}/patterns.jsonl")
  992. mistakes = load_jsonl(f"{learnings_dir}/mistakes.jsonl")
  993. successes = load_jsonl(f"{learnings_dir}/successes.jsonl")
  994. result = {
  995. "patterns": filter_relevant(patterns, context),
  996. "mistakes": filter_relevant(mistakes, context),
  997. "successes": filter_relevant(successes, context)
  998. }
  999. with open(".loki/state/relevant-learnings.json", 'w') as f:
  1000. json.dump(result, f, indent=2)
  1001. LEARNINGS_SCRIPT
  1002. log_info "Loaded relevant learnings to: $output_file"
  1003. }
  1004. extract_learnings_from_session() {
  1005. # Extract learnings from completed session
  1006. local continuity_file=".loki/CONTINUITY.md"
  1007. if [ ! -f "$continuity_file" ]; then
  1008. return
  1009. fi
  1010. log_info "Extracting learnings from session..."
  1011. # Parse CONTINUITY.md for Mistakes & Learnings section
  1012. python3 << EXTRACT_SCRIPT
  1013. import re
  1014. import json
  1015. import os
  1016. from datetime import datetime, timezone
  1017. continuity_file = ".loki/CONTINUITY.md"
  1018. learnings_dir = os.path.expanduser("~/.loki/learnings")
  1019. if not os.path.exists(continuity_file):
  1020. exit(0)
  1021. with open(continuity_file, 'r') as f:
  1022. content = f.read()
  1023. # Find Mistakes & Learnings section
  1024. mistakes_match = re.search(r'## Mistakes & Learnings\n(.*?)(?=\n## |\Z)', content, re.DOTALL)
  1025. if mistakes_match:
  1026. mistakes_text = mistakes_match.group(1)
  1027. # Extract bullet points
  1028. bullets = re.findall(r'[-*]\s+(.+)', mistakes_text)
  1029. for bullet in bullets:
  1030. entry = {
  1031. "timestamp": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
  1032. "project": os.path.basename(os.getcwd()),
  1033. "category": "session",
  1034. "description": bullet.strip()
  1035. }
  1036. with open(f"{learnings_dir}/mistakes.jsonl", 'a') as f:
  1037. f.write(json.dumps(entry) + "\n")
  1038. print(f"Extracted: {bullet[:50]}...")
  1039. print("Learning extraction complete")
  1040. EXTRACT_SCRIPT
  1041. }
  1042. start_dashboard() {
  1043. log_header "Starting Loki Dashboard"
  1044. # Create dashboard directory
  1045. mkdir -p .loki/dashboard
  1046. # Generate HTML
  1047. generate_dashboard
  1048. # Kill any existing process on the dashboard port
  1049. if lsof -i :$DASHBOARD_PORT &>/dev/null; then
  1050. log_step "Killing existing process on port $DASHBOARD_PORT..."
  1051. lsof -ti :$DASHBOARD_PORT | xargs kill -9 2>/dev/null || true
  1052. sleep 1
  1053. fi
  1054. # Start Python HTTP server from .loki/ root so it can serve queue/ and state/
  1055. log_step "Starting dashboard server..."
  1056. (
  1057. cd .loki
  1058. python3 -m http.server $DASHBOARD_PORT --bind 127.0.0.1 2>&1 | while read line; do
  1059. echo "[dashboard] $line" >> logs/dashboard.log
  1060. done
  1061. ) &
  1062. DASHBOARD_PID=$!
  1063. sleep 1
  1064. if kill -0 $DASHBOARD_PID 2>/dev/null; then
  1065. log_info "Dashboard started (PID: $DASHBOARD_PID)"
  1066. log_info "Dashboard: ${CYAN}http://127.0.0.1:$DASHBOARD_PORT/dashboard/index.html${NC}"
  1067. # Open in browser (macOS)
  1068. if [[ "$OSTYPE" == "darwin"* ]]; then
  1069. open "http://127.0.0.1:$DASHBOARD_PORT/dashboard/index.html" 2>/dev/null || true
  1070. fi
  1071. return 0
  1072. else
  1073. log_warn "Dashboard failed to start"
  1074. DASHBOARD_PID=""
  1075. return 1
  1076. fi
  1077. }
  1078. stop_dashboard() {
  1079. if [ -n "$DASHBOARD_PID" ]; then
  1080. kill "$DASHBOARD_PID" 2>/dev/null || true
  1081. wait "$DASHBOARD_PID" 2>/dev/null || true
  1082. fi
  1083. }
  1084. #===============================================================================
  1085. # Calculate Exponential Backoff
  1086. #===============================================================================
  1087. calculate_wait() {
  1088. local retry="$1"
  1089. local wait_time=$((BASE_WAIT * (2 ** retry)))
  1090. # Add jitter (0-30 seconds)
  1091. local jitter=$((RANDOM % 30))
  1092. wait_time=$((wait_time + jitter))
  1093. # Cap at max wait
  1094. if [ $wait_time -gt $MAX_WAIT ]; then
  1095. wait_time=$MAX_WAIT
  1096. fi
  1097. echo $wait_time
  1098. }
  1099. #===============================================================================
  1100. # Rate Limit Detection
  1101. #===============================================================================
  1102. # Detect rate limit from log and calculate wait time until reset
  1103. # Returns: seconds to wait, or 0 if no rate limit detected
  1104. detect_rate_limit() {
  1105. local log_file="$1"
  1106. # Look for rate limit message like "resets 4am" or "resets 10pm"
  1107. local reset_time=$(grep -o "resets [0-9]\+[ap]m" "$log_file" 2>/dev/null | tail -1 | grep -o "[0-9]\+[ap]m")
  1108. if [ -z "$reset_time" ]; then
  1109. echo 0
  1110. return
  1111. fi
  1112. # Parse the reset time
  1113. local hour=$(echo "$reset_time" | grep -o "[0-9]\+")
  1114. local ampm=$(echo "$reset_time" | grep -o "[ap]m")
  1115. # Convert to 24-hour format
  1116. if [ "$ampm" = "pm" ] && [ "$hour" -ne 12 ]; then
  1117. hour=$((hour + 12))
  1118. elif [ "$ampm" = "am" ] && [ "$hour" -eq 12 ]; then
  1119. hour=0
  1120. fi
  1121. # Get current time
  1122. local current_hour=$(date +%H)
  1123. local current_min=$(date +%M)
  1124. local current_sec=$(date +%S)
  1125. # Calculate seconds until reset
  1126. local current_secs=$((current_hour * 3600 + current_min * 60 + current_sec))
  1127. local reset_secs=$((hour * 3600))
  1128. local wait_secs=$((reset_secs - current_secs))
  1129. # If reset time is in the past, it means tomorrow
  1130. if [ $wait_secs -le 0 ]; then
  1131. wait_secs=$((wait_secs + 86400)) # Add 24 hours
  1132. fi
  1133. # Add 2 minute buffer to ensure limit is actually reset
  1134. wait_secs=$((wait_secs + 120))
  1135. echo $wait_secs
  1136. }
  1137. # Format seconds into human-readable time
  1138. format_duration() {
  1139. local secs="$1"
  1140. local hours=$((secs / 3600))
  1141. local mins=$(((secs % 3600) / 60))
  1142. if [ $hours -gt 0 ]; then
  1143. echo "${hours}h ${mins}m"
  1144. else
  1145. echo "${mins}m"
  1146. fi
  1147. }
  1148. #===============================================================================
  1149. # Check Completion
  1150. #===============================================================================
  1151. is_completed() {
  1152. # Check orchestrator state
  1153. if [ -f ".loki/state/orchestrator.json" ]; then
  1154. if command -v python3 &> /dev/null; then
  1155. local phase=$(python3 -c "import json; print(json.load(open('.loki/state/orchestrator.json')).get('currentPhase', ''))" 2>/dev/null || echo "")
  1156. # Accept various completion states
  1157. if [ "$phase" = "COMPLETED" ] || [ "$phase" = "complete" ] || [ "$phase" = "finalized" ] || [ "$phase" = "growth-loop" ]; then
  1158. return 0
  1159. fi
  1160. fi
  1161. fi
  1162. # Check for completion marker
  1163. if [ -f ".loki/COMPLETED" ]; then
  1164. return 0
  1165. fi
  1166. return 1
  1167. }
  1168. # Check if completion promise is fulfilled in log output
  1169. check_completion_promise() {
  1170. local log_file="$1"
  1171. # Check for the completion promise phrase in recent log output
  1172. if grep -q "COMPLETION PROMISE FULFILLED" "$log_file" 2>/dev/null; then
  1173. return 0
  1174. fi
  1175. # Check for custom completion promise text
  1176. if [ -n "$COMPLETION_PROMISE" ] && grep -qF "$COMPLETION_PROMISE" "$log_file" 2>/dev/null; then
  1177. return 0
  1178. fi
  1179. return 1
  1180. }
  1181. # Check if max iterations reached
  1182. check_max_iterations() {
  1183. if [ $ITERATION_COUNT -ge $MAX_ITERATIONS ]; then
  1184. log_warn "Max iterations ($MAX_ITERATIONS) reached. Stopping."
  1185. return 0
  1186. fi
  1187. return 1
  1188. }
  1189. # Check if context clear was requested by agent
  1190. check_context_clear_signal() {
  1191. if [ -f ".loki/signals/CONTEXT_CLEAR_REQUESTED" ]; then
  1192. log_info "Context clear signal detected from agent"
  1193. rm -f ".loki/signals/CONTEXT_CLEAR_REQUESTED"
  1194. return 0
  1195. fi
  1196. return 1
  1197. }
  1198. # Load latest ledger content for context injection
  1199. load_ledger_context() {
  1200. local ledger_content=""
  1201. # Find most recent ledger
  1202. local latest_ledger=$(ls -t .loki/memory/ledgers/LEDGER-*.md 2>/dev/null | head -1)
  1203. if [ -n "$latest_ledger" ] && [ -f "$latest_ledger" ]; then
  1204. ledger_content=$(cat "$latest_ledger" | head -100)
  1205. echo "$ledger_content"
  1206. fi
  1207. }
  1208. # Load recent handoffs for context
  1209. load_handoff_context() {
  1210. local handoff_content=""
  1211. # Find most recent handoff (last 24 hours)
  1212. local recent_handoff=$(find .loki/memory/handoffs -name "*.md" -mtime -1 2>/dev/null | head -1)
  1213. if [ -n "$recent_handoff" ] && [ -f "$recent_handoff" ]; then
  1214. handoff_content=$(cat "$recent_handoff" | head -80)
  1215. echo "$handoff_content"
  1216. fi
  1217. }
  1218. # Load relevant learnings
  1219. load_learnings_context() {
  1220. local learnings=""
  1221. # Get recent learnings (last 7 days)
  1222. for learning in $(find .loki/memory/learnings -name "*.md" -mtime -7 2>/dev/null | head -5); do
  1223. learnings+="$(head -30 "$learning")\n---\n"
  1224. done
  1225. echo -e "$learnings"
  1226. }
  1227. #===============================================================================
  1228. # Save/Load Wrapper State
  1229. #===============================================================================
  1230. save_state() {
  1231. local retry_count="$1"
  1232. local status="$2"
  1233. local exit_code="$3"
  1234. cat > ".loki/autonomy-state.json" << EOF
  1235. {
  1236. "retryCount": $retry_count,
  1237. "status": "$status",
  1238. "lastExitCode": $exit_code,
  1239. "lastRun": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  1240. "prdPath": "${PRD_PATH:-}",
  1241. "pid": $$,
  1242. "maxRetries": $MAX_RETRIES,
  1243. "baseWait": $BASE_WAIT
  1244. }
  1245. EOF
  1246. }
  1247. load_state() {
  1248. if [ -f ".loki/autonomy-state.json" ]; then
  1249. if command -v python3 &> /dev/null; then
  1250. RETRY_COUNT=$(python3 -c "import json; print(json.load(open('.loki/autonomy-state.json')).get('retryCount', 0))" 2>/dev/null || echo "0")
  1251. else
  1252. RETRY_COUNT=0
  1253. fi
  1254. else
  1255. RETRY_COUNT=0
  1256. fi
  1257. }
  1258. #===============================================================================
  1259. # Build Resume Prompt
  1260. #===============================================================================
  1261. build_prompt() {
  1262. local retry="$1"
  1263. local prd="$2"
  1264. local iteration="$3"
  1265. # Build SDLC phases configuration
  1266. local phases=""
  1267. [ "$PHASE_UNIT_TESTS" = "true" ] && phases="${phases}UNIT_TESTS,"
  1268. [ "$PHASE_API_TESTS" = "true" ] && phases="${phases}API_TESTS,"
  1269. [ "$PHASE_E2E_TESTS" = "true" ] && phases="${phases}E2E_TESTS,"
  1270. [ "$PHASE_SECURITY" = "true" ] && phases="${phases}SECURITY,"
  1271. [ "$PHASE_INTEGRATION" = "true" ] && phases="${phases}INTEGRATION,"
  1272. [ "$PHASE_CODE_REVIEW" = "true" ] && phases="${phases}CODE_REVIEW,"
  1273. [ "$PHASE_WEB_RESEARCH" = "true" ] && phases="${phases}WEB_RESEARCH,"
  1274. [ "$PHASE_PERFORMANCE" = "true" ] && phases="${phases}PERFORMANCE,"
  1275. [ "$PHASE_ACCESSIBILITY" = "true" ] && phases="${phases}ACCESSIBILITY,"
  1276. [ "$PHASE_REGRESSION" = "true" ] && phases="${phases}REGRESSION,"
  1277. [ "$PHASE_UAT" = "true" ] && phases="${phases}UAT,"
  1278. phases="${phases%,}" # Remove trailing comma
  1279. # Ralph Wiggum Mode - Reason-Act-Reflect-VERIFY cycle with self-verification loop (Boris Cherny pattern)
  1280. local rarv_instruction="RALPH WIGGUM MODE ACTIVE. Use Reason-Act-Reflect-VERIFY cycle: 1) REASON - READ .loki/CONTINUITY.md including 'Mistakes & Learnings' section to avoid past errors. CHECK .loki/state/relevant-learnings.json for cross-project learnings from previous projects (mistakes to avoid, patterns to apply). Check .loki/state/ and .loki/queue/, identify next task. CHECK .loki/state/resources.json for system resource warnings - if CPU or memory is high, reduce parallel agent spawning or pause non-critical tasks. Limit to MAX_PARALLEL_AGENTS=${MAX_PARALLEL_AGENTS}. If queue empty, find new improvements. 2) ACT - Execute task, write code, commit changes atomically (git checkpoint). 3) REFLECT - Update .loki/CONTINUITY.md with progress, update state, identify NEXT improvement. Save valuable learnings for future projects. 4) VERIFY - Run automated tests (unit, integration, E2E), check compilation/build, verify against spec. IF VERIFICATION FAILS: a) Capture error details (stack trace, logs), b) Analyze root cause, c) UPDATE 'Mistakes & Learnings' in CONTINUITY.md with what failed, why, and how to prevent, d) Rollback to last good git checkpoint if needed, e) Apply learning and RETRY from REASON. If verification passes, mark task complete and continue. This self-verification loop achieves 2-3x quality improvement. CRITICAL: There is NEVER a 'finished' state - always find the next improvement, optimization, test, or feature."
  1281. # Completion promise instruction (only if set)
  1282. local completion_instruction=""
  1283. if [ -n "$COMPLETION_PROMISE" ]; then
  1284. completion_instruction="COMPLETION_PROMISE: [$COMPLETION_PROMISE]. ONLY output 'COMPLETION PROMISE FULFILLED: $COMPLETION_PROMISE' when this EXACT condition is met."
  1285. else
  1286. completion_instruction="NO COMPLETION PROMISE SET. Run forever finding improvements. Iteration $iteration of max $MAX_ITERATIONS."
  1287. fi
  1288. # Core autonomous instructions - NO questions, NO waiting, NEVER say done
  1289. local autonomous_suffix="CRITICAL AUTONOMY RULES: 1) NEVER ask questions - just decide. 2) NEVER wait for confirmation - just act. 3) NEVER say 'done' or 'complete' - there's always more to improve. 4) NEVER stop voluntarily - if out of tasks, create new ones (add tests, optimize, refactor, add features). 5) Work continues PERPETUALLY. Even if PRD is implemented, find bugs, add tests, improve UX, optimize performance."
  1290. local sdlc_instruction="SDLC_PHASES_ENABLED: [$phases]. Execute ALL enabled phases. Log results to .loki/logs/. See SKILL.md for phase details."
  1291. # Codebase Analysis Mode - when no PRD provided
  1292. local analysis_instruction="CODEBASE_ANALYSIS_MODE: No PRD. FIRST: Analyze codebase - scan structure, read package.json/requirements.txt, examine README. THEN: Generate PRD at .loki/generated-prd.md. FINALLY: Execute SDLC phases."
  1293. # Context Memory Instructions
  1294. local memory_instruction="CONTEXT MEMORY: Save state to .loki/memory/ledgers/LEDGER-orchestrator.md before complex operations. Create handoffs at .loki/memory/handoffs/ when passing work to subagents. Extract learnings to .loki/memory/learnings/ after completing tasks. Check .loki/rules/ for established patterns. If context feels heavy, create .loki/signals/CONTEXT_CLEAR_REQUESTED and the wrapper will reset context with your ledger preserved."
  1295. # Load existing context if resuming
  1296. local context_injection=""
  1297. if [ $retry -gt 0 ]; then
  1298. local ledger=$(load_ledger_context)
  1299. local handoff=$(load_handoff_context)
  1300. if [ -n "$ledger" ]; then
  1301. context_injection="PREVIOUS_LEDGER_STATE: $ledger"
  1302. fi
  1303. if [ -n "$handoff" ]; then
  1304. context_injection="$context_injection RECENT_HANDOFF: $handoff"
  1305. fi
  1306. fi
  1307. if [ $retry -eq 0 ]; then
  1308. if [ -n "$prd" ]; then
  1309. echo "Loki Mode with PRD at $prd. $rarv_instruction $memory_instruction $completion_instruction $sdlc_instruction $autonomous_suffix"
  1310. else
  1311. echo "Loki Mode. $analysis_instruction $rarv_instruction $memory_instruction $completion_instruction $sdlc_instruction $autonomous_suffix"
  1312. fi
  1313. else
  1314. if [ -n "$prd" ]; then
  1315. echo "Loki Mode - Resume iteration #$iteration (retry #$retry). PRD: $prd. $context_injection $rarv_instruction $memory_instruction $completion_instruction $sdlc_instruction $autonomous_suffix"
  1316. else
  1317. echo "Loki Mode - Resume iteration #$iteration (retry #$retry). $context_injection Use .loki/generated-prd.md if exists. $rarv_instruction $memory_instruction $completion_instruction $sdlc_instruction $autonomous_suffix"
  1318. fi
  1319. fi
  1320. }
  1321. #===============================================================================
  1322. # Main Autonomous Loop
  1323. #===============================================================================
  1324. run_autonomous() {
  1325. local prd_path="$1"
  1326. log_header "Starting Autonomous Execution"
  1327. # Auto-detect PRD if not provided
  1328. if [ -z "$prd_path" ]; then
  1329. log_step "No PRD provided, searching for existing PRD files..."
  1330. local found_prd=""
  1331. # Search common PRD file patterns
  1332. for pattern in "PRD.md" "prd.md" "REQUIREMENTS.md" "requirements.md" "SPEC.md" "spec.md" \
  1333. "docs/PRD.md" "docs/prd.md" "docs/REQUIREMENTS.md" "docs/requirements.md" \
  1334. "docs/SPEC.md" "docs/spec.md" ".github/PRD.md" "PROJECT.md" "project.md"; do
  1335. if [ -f "$pattern" ]; then
  1336. found_prd="$pattern"
  1337. break
  1338. fi
  1339. done
  1340. if [ -n "$found_prd" ]; then
  1341. log_info "Found existing PRD: $found_prd"
  1342. prd_path="$found_prd"
  1343. elif [ -f ".loki/generated-prd.md" ]; then
  1344. log_info "Using previously generated PRD: .loki/generated-prd.md"
  1345. prd_path=".loki/generated-prd.md"
  1346. else
  1347. log_info "No PRD found - will analyze codebase and generate one"
  1348. fi
  1349. fi
  1350. log_info "PRD: ${prd_path:-Codebase Analysis Mode}"
  1351. log_info "Max retries: $MAX_RETRIES"
  1352. log_info "Max iterations: $MAX_ITERATIONS"
  1353. log_info "Completion promise: $COMPLETION_PROMISE"
  1354. log_info "Base wait: ${BASE_WAIT}s"
  1355. log_info "Max wait: ${MAX_WAIT}s"
  1356. echo ""
  1357. load_state
  1358. local retry=$RETRY_COUNT
  1359. # Check max iterations before starting
  1360. if check_max_iterations; then
  1361. log_error "Max iterations already reached. Reset with: rm .loki/autonomy-state.json"
  1362. return 1
  1363. fi
  1364. while [ $retry -lt $MAX_RETRIES ]; do
  1365. # Increment iteration count
  1366. ((ITERATION_COUNT++))
  1367. # Check max iterations
  1368. if check_max_iterations; then
  1369. save_state $retry "max_iterations_reached" 0
  1370. return 0
  1371. fi
  1372. local prompt=$(build_prompt $retry "$prd_path" $ITERATION_COUNT)
  1373. echo ""
  1374. log_header "Attempt $((retry + 1)) of $MAX_RETRIES"
  1375. log_info "Prompt: $prompt"
  1376. echo ""
  1377. save_state $retry "running" 0
  1378. # Run Claude Code with live output
  1379. local start_time=$(date +%s)
  1380. local log_file=".loki/logs/autonomy-$(date +%Y%m%d).log"
  1381. echo ""
  1382. echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
  1383. echo -e "${CYAN} CLAUDE CODE OUTPUT (live)${NC}"
  1384. echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
  1385. echo ""
  1386. # Log start time
  1387. echo "=== Session started at $(date) ===" >> "$log_file"
  1388. echo "=== Prompt: $prompt ===" >> "$log_file"
  1389. set +e
  1390. # Run Claude with stream-json for real-time output
  1391. # Parse JSON stream, display formatted output, and track agents
  1392. claude --dangerously-skip-permissions -p "$prompt" \
  1393. --output-format stream-json --verbose 2>&1 | \
  1394. tee -a "$log_file" | \
  1395. python3 -u -c '
  1396. import sys
  1397. import json
  1398. import os
  1399. from datetime import datetime, timezone
  1400. # ANSI colors
  1401. CYAN = "\033[0;36m"
  1402. GREEN = "\033[0;32m"
  1403. YELLOW = "\033[1;33m"
  1404. MAGENTA = "\033[0;35m"
  1405. DIM = "\033[2m"
  1406. NC = "\033[0m"
  1407. # Agent tracking
  1408. AGENTS_FILE = ".loki/state/agents.json"
  1409. QUEUE_IN_PROGRESS = ".loki/queue/in-progress.json"
  1410. active_agents = {} # tool_id -> agent_info
  1411. orchestrator_id = "orchestrator-main"
  1412. session_start = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
  1413. def init_orchestrator():
  1414. """Initialize the main orchestrator agent (always visible)."""
  1415. active_agents[orchestrator_id] = {
  1416. "agent_id": orchestrator_id,
  1417. "tool_id": orchestrator_id,
  1418. "agent_type": "orchestrator",
  1419. "model": "sonnet",
  1420. "current_task": "Initializing...",
  1421. "status": "active",
  1422. "spawned_at": session_start,
  1423. "tasks_completed": [],
  1424. "tool_count": 0
  1425. }
  1426. save_agents()
  1427. def update_orchestrator_task(tool_name, description=""):
  1428. """Update orchestrator current task based on tool usage."""
  1429. if orchestrator_id in active_agents:
  1430. active_agents[orchestrator_id]["tool_count"] = active_agents[orchestrator_id].get("tool_count", 0) + 1
  1431. if description:
  1432. active_agents[orchestrator_id]["current_task"] = f"{tool_name}: {description[:80]}"
  1433. else:
  1434. active_agents[orchestrator_id]["current_task"] = f"Using {tool_name}..."
  1435. save_agents()
  1436. def load_agents():
  1437. """Load existing agents from file."""
  1438. try:
  1439. if os.path.exists(AGENTS_FILE):
  1440. with open(AGENTS_FILE, "r") as f:
  1441. data = json.load(f)
  1442. return {a.get("tool_id", a.get("agent_id")): a for a in data if isinstance(a, dict)}
  1443. except:
  1444. pass
  1445. return {}
  1446. def save_agents():
  1447. """Save agents to file for dashboard."""
  1448. try:
  1449. os.makedirs(os.path.dirname(AGENTS_FILE), exist_ok=True)
  1450. agents_list = list(active_agents.values())
  1451. with open(AGENTS_FILE, "w") as f:
  1452. json.dump(agents_list, f, indent=2)
  1453. except Exception as e:
  1454. print(f"{YELLOW}[Agent save error: {e}]{NC}", file=sys.stderr)
  1455. def save_in_progress(tasks):
  1456. """Save in-progress tasks to queue file."""
  1457. try:
  1458. os.makedirs(os.path.dirname(QUEUE_IN_PROGRESS), exist_ok=True)
  1459. with open(QUEUE_IN_PROGRESS, "w") as f:
  1460. json.dump(tasks, f, indent=2)
  1461. except:
  1462. pass
  1463. def process_stream():
  1464. global active_agents
  1465. active_agents = load_agents()
  1466. # Always show the main orchestrator
  1467. init_orchestrator()
  1468. print(f"{MAGENTA}[Orchestrator Active]{NC} Main agent started", flush=True)
  1469. for line in sys.stdin:
  1470. line = line.strip()
  1471. if not line:
  1472. continue
  1473. try:
  1474. data = json.loads(line)
  1475. msg_type = data.get("type", "")
  1476. if msg_type == "assistant":
  1477. # Extract and print assistant text
  1478. message = data.get("message", {})
  1479. content = message.get("content", [])
  1480. for item in content:
  1481. if item.get("type") == "text":
  1482. text = item.get("text", "")
  1483. if text:
  1484. print(text, end="", flush=True)
  1485. elif item.get("type") == "tool_use":
  1486. tool = item.get("name", "unknown")
  1487. tool_id = item.get("id", "")
  1488. tool_input = item.get("input", {})
  1489. # Extract description based on tool type
  1490. tool_desc = ""
  1491. if tool == "Read":
  1492. tool_desc = tool_input.get("file_path", "")
  1493. elif tool == "Edit" or tool == "Write":
  1494. tool_desc = tool_input.get("file_path", "")
  1495. elif tool == "Bash":
  1496. tool_desc = tool_input.get("description", tool_input.get("command", "")[:60])
  1497. elif tool == "Grep":
  1498. tool_desc = f"pattern: {tool_input.get('pattern', '')}"
  1499. elif tool == "Glob":
  1500. tool_desc = tool_input.get("pattern", "")
  1501. # Update orchestrator with current tool activity
  1502. update_orchestrator_task(tool, tool_desc)
  1503. # Track Task tool calls (agent spawning)
  1504. if tool == "Task":
  1505. agent_type = tool_input.get("subagent_type", "general-purpose")
  1506. description = tool_input.get("description", "")
  1507. model = tool_input.get("model", "sonnet")
  1508. agent_info = {
  1509. "agent_id": f"agent-{tool_id[:8]}",
  1510. "tool_id": tool_id,
  1511. "agent_type": agent_type,
  1512. "model": model,
  1513. "current_task": description,
  1514. "status": "active",
  1515. "spawned_at": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
  1516. "tasks_completed": []
  1517. }
  1518. active_agents[tool_id] = agent_info
  1519. save_agents()
  1520. print(f"\n{MAGENTA}[Agent Spawned: {agent_type}]{NC} {description}", flush=True)
  1521. # Track TodoWrite for task updates
  1522. elif tool == "TodoWrite":
  1523. todos = tool_input.get("todos", [])
  1524. in_progress = [t for t in todos if t.get("status") == "in_progress"]
  1525. save_in_progress([{"id": f"todo-{i}", "type": "todo", "payload": {"action": t.get("content", "")}} for i, t in enumerate(in_progress)])
  1526. print(f"\n{CYAN}[Tool: {tool}]{NC} {len(todos)} items", flush=True)
  1527. else:
  1528. print(f"\n{CYAN}[Tool: {tool}]{NC}", flush=True)
  1529. elif msg_type == "user":
  1530. # Tool results - check for agent completion
  1531. content = data.get("message", {}).get("content", [])
  1532. for item in content:
  1533. if item.get("type") == "tool_result":
  1534. tool_id = item.get("tool_use_id", "")
  1535. # Mark agent as completed if it was a Task
  1536. if tool_id in active_agents:
  1537. active_agents[tool_id]["status"] = "completed"
  1538. active_agents[tool_id]["completed_at"] = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
  1539. save_agents()
  1540. print(f"{DIM}[Agent Complete]{NC} ", end="", flush=True)
  1541. else:
  1542. print(f"{DIM}[Result]{NC} ", end="", flush=True)
  1543. elif msg_type == "result":
  1544. # Session complete - mark all agents as completed
  1545. completed_at = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
  1546. for agent_id in active_agents:
  1547. if active_agents[agent_id].get("status") == "active":
  1548. active_agents[agent_id]["status"] = "completed"
  1549. active_agents[agent_id]["completed_at"] = completed_at
  1550. active_agents[agent_id]["current_task"] = "Session complete"
  1551. # Add session stats to orchestrator
  1552. if orchestrator_id in active_agents:
  1553. tool_count = active_agents[orchestrator_id].get("tool_count", 0)
  1554. active_agents[orchestrator_id]["tasks_completed"].append(f"{tool_count} tools used")
  1555. save_agents()
  1556. print(f"\n{GREEN}[Session complete]{NC}", flush=True)
  1557. is_error = data.get("is_error", False)
  1558. sys.exit(1 if is_error else 0)
  1559. except json.JSONDecodeError:
  1560. # Not JSON, print as-is
  1561. print(line, flush=True)
  1562. except Exception as e:
  1563. print(f"{YELLOW}[Parse error: {e}]{NC}", file=sys.stderr)
  1564. if __name__ == "__main__":
  1565. try:
  1566. process_stream()
  1567. except KeyboardInterrupt:
  1568. sys.exit(130)
  1569. except BrokenPipeError:
  1570. sys.exit(0)
  1571. '
  1572. local exit_code=${PIPESTATUS[0]}
  1573. set -e
  1574. echo ""
  1575. echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
  1576. echo ""
  1577. # Log end time
  1578. echo "=== Session ended at $(date) with exit code $exit_code ===" >> "$log_file"
  1579. local end_time=$(date +%s)
  1580. local duration=$((end_time - start_time))
  1581. log_info "Claude exited with code $exit_code after ${duration}s"
  1582. save_state $retry "exited" $exit_code
  1583. # Check for success - ONLY stop on explicit completion promise
  1584. # There's never a "complete" product - always improvements, bugs, features
  1585. if [ $exit_code -eq 0 ]; then
  1586. # Perpetual mode: NEVER stop, always continue
  1587. if [ "$PERPETUAL_MODE" = "true" ]; then
  1588. log_info "Perpetual mode: Ignoring exit, continuing immediately..."
  1589. ((retry++))
  1590. continue # Immediately start next iteration, no wait
  1591. fi
  1592. # Only stop if EXPLICIT completion promise text was output
  1593. if [ -n "$COMPLETION_PROMISE" ] && check_completion_promise "$log_file"; then
  1594. echo ""
  1595. log_header "COMPLETION PROMISE FULFILLED: $COMPLETION_PROMISE"
  1596. log_info "Explicit completion promise detected in output."
  1597. save_state $retry "completion_promise_fulfilled" 0
  1598. return 0
  1599. fi
  1600. # Warn if Claude says it's "done" but no explicit promise
  1601. if is_completed; then
  1602. log_warn "Claude claims completion, but no explicit promise fulfilled."
  1603. log_warn "Projects are never truly complete - there are always improvements!"
  1604. fi
  1605. # SUCCESS exit - continue IMMEDIATELY to next iteration (no wait!)
  1606. log_info "Iteration complete. Continuing to next iteration..."
  1607. ((retry++))
  1608. continue # Immediately start next iteration, no exponential backoff
  1609. fi
  1610. # Only apply retry logic for ERRORS (non-zero exit code)
  1611. # Handle retry - check for rate limit first
  1612. local rate_limit_wait=$(detect_rate_limit "$log_file")
  1613. local wait_time
  1614. if [ $rate_limit_wait -gt 0 ]; then
  1615. wait_time=$rate_limit_wait
  1616. local human_time=$(format_duration $wait_time)
  1617. log_warn "Rate limit detected! Waiting until reset (~$human_time)..."
  1618. log_info "Rate limit resets at approximately $(date -v+${wait_time}S '+%I:%M %p' 2>/dev/null || date -d "+${wait_time} seconds" '+%I:%M %p' 2>/dev/null || echo 'soon')"
  1619. else
  1620. wait_time=$(calculate_wait $retry)
  1621. log_warn "Will retry in ${wait_time}s..."
  1622. fi
  1623. log_info "Press Ctrl+C to cancel"
  1624. # Countdown with progress
  1625. local remaining=$wait_time
  1626. local interval=10
  1627. # Use longer interval for long waits
  1628. if [ $wait_time -gt 1800 ]; then
  1629. interval=60
  1630. fi
  1631. while [ $remaining -gt 0 ]; do
  1632. local human_remaining=$(format_duration $remaining)
  1633. printf "\r${YELLOW}Resuming in ${human_remaining}...${NC} "
  1634. sleep $interval
  1635. remaining=$((remaining - interval))
  1636. done
  1637. echo ""
  1638. ((retry++))
  1639. done
  1640. log_error "Max retries ($MAX_RETRIES) exceeded"
  1641. save_state $retry "failed" 1
  1642. return 1
  1643. }
  1644. #===============================================================================
  1645. # Cleanup Handler
  1646. #===============================================================================
  1647. cleanup() {
  1648. echo ""
  1649. log_warn "Received interrupt signal"
  1650. stop_dashboard
  1651. stop_status_monitor
  1652. save_state ${RETRY_COUNT:-0} "interrupted" 130
  1653. log_info "State saved. Run again to resume."
  1654. exit 130
  1655. }
  1656. #===============================================================================
  1657. # Main Entry Point
  1658. #===============================================================================
  1659. main() {
  1660. trap cleanup INT TERM
  1661. echo ""
  1662. echo -e "${BOLD}${BLUE}"
  1663. echo " ██╗ ██████╗ ██╗ ██╗██╗ ███╗ ███╗ ██████╗ ██████╗ ███████╗"
  1664. echo " ██║ ██╔═══██╗██║ ██╔╝██║ ████╗ ████║██╔═══██╗██╔══██╗██╔════╝"
  1665. echo " ██║ ██║ ██║█████╔╝ ██║ ██╔████╔██║██║ ██║██║ ██║█████╗ "
  1666. echo " ██║ ██║ ██║██╔═██╗ ██║ ██║╚██╔╝██║██║ ██║██║ ██║██╔══╝ "
  1667. echo " ███████╗╚██████╔╝██║ ██╗██║ ██║ ╚═╝ ██║╚██████╔╝██████╔╝███████╗"
  1668. echo " ╚══════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝"
  1669. echo -e "${NC}"
  1670. echo -e " ${CYAN}Autonomous Multi-Agent Startup System${NC}"
  1671. echo -e " ${CYAN}Version: $(cat "$PROJECT_DIR/VERSION" 2>/dev/null || echo "2.x.x")${NC}"
  1672. echo ""
  1673. # Parse arguments
  1674. PRD_PATH="${1:-}"
  1675. # Validate PRD if provided
  1676. if [ -n "$PRD_PATH" ] && [ ! -f "$PRD_PATH" ]; then
  1677. log_error "PRD file not found: $PRD_PATH"
  1678. exit 1
  1679. fi
  1680. # Check prerequisites (unless skipped)
  1681. if [ "$SKIP_PREREQS" != "true" ]; then
  1682. if ! check_prerequisites; then
  1683. exit 1
  1684. fi
  1685. else
  1686. log_warn "Skipping prerequisite checks (LOKI_SKIP_PREREQS=true)"
  1687. fi
  1688. # Check skill installation
  1689. if ! check_skill_installed; then
  1690. exit 1
  1691. fi
  1692. # Initialize .loki directory
  1693. init_loki_dir
  1694. # Start web dashboard (if enabled)
  1695. if [ "$ENABLE_DASHBOARD" = "true" ]; then
  1696. start_dashboard
  1697. else
  1698. log_info "Dashboard disabled (LOKI_DASHBOARD=false)"
  1699. fi
  1700. # Start status monitor (background updates to .loki/STATUS.txt)
  1701. start_status_monitor
  1702. # Start resource monitor (background CPU/memory checks)
  1703. start_resource_monitor
  1704. # Initialize cross-project learnings database
  1705. init_learnings_db
  1706. # Load relevant learnings for this project context
  1707. if [ -n "$PRD_PATH" ] && [ -f "$PRD_PATH" ]; then
  1708. get_relevant_learnings "$(cat "$PRD_PATH" | head -100)"
  1709. else
  1710. get_relevant_learnings "general development"
  1711. fi
  1712. # Log session start for audit
  1713. audit_log "SESSION_START" "prd=$PRD_PATH,dashboard=$ENABLE_DASHBOARD,staged_autonomy=$STAGED_AUTONOMY"
  1714. # Run autonomous loop
  1715. local result=0
  1716. run_autonomous "$PRD_PATH" || result=$?
  1717. # Extract and save learnings from this session
  1718. extract_learnings_from_session
  1719. # Log session end for audit
  1720. audit_log "SESSION_END" "result=$result,prd=$PRD_PATH"
  1721. # Cleanup
  1722. stop_dashboard
  1723. stop_status_monitor
  1724. exit $result
  1725. }
  1726. # Run main
  1727. main "$@"

Ratings and comments

Sign in to rate this skill.

Sign in

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