Skip to content

docs: add session handover for 2026-05-31#89

Closed
B67687 wants to merge 6 commits into
mainfrom
docs/handover-2026-05-31
Closed

docs: add session handover for 2026-05-31#89
B67687 wants to merge 6 commits into
mainfrom
docs/handover-2026-05-31

Conversation

@B67687
Copy link
Copy Markdown
Owner

@B67687 B67687 commented May 31, 2026

Comprehensive handover: daemon running on MiniPC, 4 AI commits, GitHub push setup instructions, cloud LLM consideration, next steps.

Summary by Sourcery

Document the current agent-seed and agentic-workflows architecture and decisions, introduce ADR-based architecture records and budget policies, and add a detailed session handover for 2026-05-31 covering the MiniPC daemon setup and next steps.

Enhancements:

  • Refine agent-seed architecture documentation to emphasize decisions, model choice, quantization strategy, and safety layers.
  • Clarify AGENTS.md workflow to integrate ADR checks, omp-native memory operations, and architecture documentation responsibilities.

Documentation:

  • Add living architecture snapshot and multiple ADR documents describing tier system, model routing, and pro-model budget policy.
  • Document a detailed Ubuntu installation guide for the Minisforum AI Pro-370 MiniPC.
  • Add a session handover document for 2026-05-31 capturing system state, MiniPC deployment status, and planned next steps.

Chores:

  • Extend red-team document with model and quantization research plus updated risk table formatting.

Summary by CodeRabbit

  • New Features

    • Added Architecture Decision Records system to track and document architecture choices
    • Implemented cost governance with budget policies and automatic model fallback for cost control
    • Introduced two-tier agent architecture with strategic/implementation role separation
  • Documentation

    • Added living architecture snapshot and operating procedures for agent workflows
    • Created hardware setup and installation guides for deployment
    • Documented agent safety layers, health checks, and rollback mechanisms
    • Added session handover documentation for agent context preservation

Copilot AI review requested due to automatic review settings May 31, 2026 14:47
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 31, 2026

Reviewer's Guide

Documents the current agent-seed deployment on the MiniPC, introduces an architect/worker two-tier agent architecture and ADR system for agentic-workflows, and adds a dated session handover plus cost guard tooling for Pro models.

Sequence diagram for Pro model cost guard before architect delegation

sequenceDiagram
  participant Orchestrator
  participant ProCostGuard as pro_cost_guard_sh
  participant Opencode as opencode_stats
  participant Architect
  participant Pro as deepseek_v4_pro
  participant Flash as deepseek_v4_flash

  Orchestrator->>ProCostGuard: pro-cost-guard.sh
  ProCostGuard->>Opencode: opencode stats --days 1
  Opencode-->>ProCostGuard: current spend
  ProCostGuard-->>Orchestrator: {status, monthMax, message}

  alt status == PASS or WARN
    Orchestrator->>Architect: delegate @architect
    Architect->>Pro: architecture_request
    Pro-->>Architect: architecture_response
  else status == BLOCK
    Orchestrator->>Architect: delegate @architect (fallback)
    Architect->>Flash: architecture_request (reasoning_effort max)
    Flash-->>Architect: architecture_response
  end
Loading

File-Level Changes

Change Details Files
Refined agent-seed architecture doc to emphasize concrete runtime choices, model selection, safety mechanisms, and loop behavior, and aligned it with red-team analysis.
  • Clarified OS, kernel, and firmware rationale and split out non-Ubuntu choices.
  • Detailed Qwen 3.6-27B GGUF selection, quantization strategy, chat template, and token drift mitigations.
  • Reframed mini-SWE-agent vs alternatives and updated loop description with SCAN markers, summarization, schema validation, and health checks.
  • Summarized safety architecture as five implemented layers plus blocked commands and protected paths.
  • Updated key design decisions to emphasize model-agnostic design, minimal protocol, CPU-first runtime, llama.cpp vs Ollama, and observer daemon.
agent-seed-architecture.md
Tightened red-team doc formatting and expanded it with concrete model and quantization research plus architecture-quality tooling recommendations.
  • Fixed minor formatting in the risk table and elaborated on mitigation rows.
  • Added detailed GGUF provider comparison, quant selection guidance for 32GB, and alternative model trade-offs for Qwen 3.6-27B.
  • Documented license, chat template bug and fix, expected llama.cpp performance, and architecture-quality measurement tools (drift/archy/sentrux) and CI gates.
agent-seed-red-team.md
Updated AGENTS runtime contract to integrate omp-native memory and commit concepts and to require architecture documentation usage.
  • Switched memory interaction from custom scripts to omp-native retain/recall/reflect and referenced the full historical session archive.
  • Required checking ADRs before architecture-significant changes and creating new ADRs when needed.
  • Updated commit guidance to use omp commit/commit tool and clarified automatic session-archive behavior.
  • Documented where to find architecture docs, ADRs, and principles for agents.
AGENTS.md
Introduced a comprehensive research note advocating a two-tier Architect+Worker agent pattern with supporting benchmarks, patterns, and next steps.
  • Summarized benchmark evidence for separating architecture reasoning from execution and recommended model routing per role.
  • Surveyed ADR formats, planner-executor-reviewer patterns, and architecture-quality measurement tools.
  • Outlined immediate, medium-term, and long-term steps to implement a two-tier architecture in this workspace.
research/architect-worker-two-tier.md
Added a detailed session handover document capturing the current MiniPC agent-seed deployment, daemon behavior, and next-step guidance.
  • Described current hardware, OS, runtime, and model configuration plus the minimal 40-line daemon architecture.
  • Recorded current deployment status: llama.cpp server, agent-seed daemon, and four AI commits, with SSH and logging commands for verification.
  • Documented GitHub push setup steps, known SSH/daemon issues, and prioritized next actions (daemon hardening, observation, cloud LLM option, safety layering).
  • Listed key context files to preserve and commands to verify experiment health from the laptop.
session-handover-2026-05-31.md
Captured a living architecture snapshot and formalized initial ADRs for tiering, model routing, and Pro model budget policy in agentic-workflows.
  • Created docs/architecture.md to describe system purpose, architecture diagram, tier system, model routing, governance, constraints, and known gaps.
  • Added ADR-0001 to adopt ADRs, ADR-0002 for tier system and model role routing, ADR-0003 to route architect/planner to DeepSeek V4 Pro with max reasoning, and ADR-0004 for a Pro model budget policy.
  • Documented a four-layer cost-defense strategy (pre-flight script, global cost guard, fallback chain, token monitor) and confirmation criteria in ADR-0004.
docs/architecture.md
docs/adr/0001-adopt-architecture-decision-records.md
docs/adr/0002-tier-system-and-model-routing.md
docs/adr/0003-route-architect-planner-to-deepseek-v4-pro.md
docs/adr/0004-pro-model-budget-policy.md
Introduced a Pro-model pre-flight cost guard script and an Ubuntu installation prompt for the Minisforum AI Pro-370.
  • Added scripts/tools/pro-cost-guard.sh to emit JSON status (PASS/WARN/BLOCK) based on opencode stats and cost-guard config, parameterized by PRO_MONTHLY_MAX.
  • Added ubuntu-install-prompt.md as a reusable prompt for guiding an agent through installing Ubuntu 26.04 LTS on the MiniPC, including BIOS, install, and post-install steps.
scripts/tools/pro-cost-guard.sh
ubuntu-install-prompt.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 31, 2026

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR establishes an ADR-based governance system for recording architecture decisions, implements two-tier model routing (strategic vs. tactical agents on different model tiers), defines a four-layer cost defense to control expensive model usage, documents the complete agent architecture with safety layers and daemon loop phases, and provides deployment guidance and session continuity procedures.

Changes

Architecture Governance, Model Routing, and Cost Controls

Layer / File(s) Summary
ADR System Adoption and Governance Framework
docs/adr/0001-adopt-architecture-decision-records.md, docs/adr/0002-tier-system-and-model-routing.md, research/architect-worker-two-tier.md, AGENTS.md
ADR adoption establishes formal decision records using Nygard template with immutability rules; governance tier system (Light/Standard/Full) with escalation triggers; research synthesis justifying separation of architecture/strategic planning from execution; AGENTS.md updated to check docs/adr/ before architecture-significant changes and point to architectural memory.
Model Routing Strategy: Architect/Planner Escalation
docs/adr/0003-route-architect-planner-to-deepseek-v4-pro.md, research synthesis
ADR-0003 routes Architect/Planner roles to expensive deepseek-v4-pro with max reasoning effort; Orchestrator/Worker remain on flash for speed/cost; introduces reviewer diversity via slow role pointing to kimi-k2.5/minimax-m2.5; records consequences and confirmation checklist for two-week evaluation window.
Cost Defense Policy and Budget Pre-Flight
docs/adr/0004-pro-model-budget-policy.md, scripts/tools/pro-cost-guard.sh
Four-layer cost defense: pre-flight spend check via pro-cost-guard.sh (reads monthly ceiling, returns PASS/WARN/BLOCK JSON); global cost-guard config enforces higher ceiling in block mode; model fallback chain (Pro→Flash, Kimi→Flash); daily/weekly/monthly token monitoring; tunable parameters like PRO_MONTHLY_MAX; explicit escalation behavior when Pro is blocked.
Living Architecture Reference and Constraints
docs/architecture.md
Living snapshot documenting system purpose, orchestrator/subagent/support-systems architecture, tiering/escalation rules, model routing table with cost/effort rationale, governance structure and directory pointers, repository layout, operating constraints, DeepSeek Pro budget policy with fallback chain, known gaps, and references to ADRs/research/related docs.

Agent Architecture, Safety Layers, and Operational Workflows

Layer / File(s) Summary
Agent Architecture: Hardware, Runtime, Loop, and Safety
agent-seed-architecture.md
Complete rewrite specifying Ubuntu 26.04 LTS with kernel parameters; local llama.cpp server (not Ollama) with CPU AVX-512 primary, iGPU/Vulkan fallback contingent on driver stability, daily cron restart; Qwen 3.6-27B with GGUF sourcing, quantization trade-offs, chat-template fix for tool-calls; daemon.py loop with explicit phases (survey→SCAN marker→agent run→git commit→schema validation→health check/rollback→sleep); five safety layers (step timeout/budget, git checkpointing, filesystem quota/logrotate, JSON/schema validation, post-modification health checks with rollback); 19 blocked command patterns and 4 protected paths enforced at Python level.
Safety Gaps and Adversarial Review
agent-seed-red-team.md
Restructures "Critical gaps remain" section into markdown table format; documents context degradation, token drift, coordination failures, observability blind spots, and deployment risk gaps with severity and mitigation rows.
Agent Operating Contract: Archive Queries and Commit Workflow
AGENTS.md
Directs session archive queries via @recall/reflect (Hindsight memory) and full search in ~/.sessions/ (replacing session-archive.sh query); adds Architecture Documentation section pointing to docs/adr/, docs/architecture.md, constitution.md; updates commit steps to use omp commit after each verified slice and omp commit --context "summary" for meaningful changes (replacing bash checkpoint-commit.sh).

Deployment, Session Continuity, and Operational Setup

Layer / File(s) Summary
Session Handover and Deployment State
session-handover-2026-05-31.md
Captures MiniPC deployment status, SSH setup/GitHub push-token guidance, daemon/llama.cpp runtime details, agentic-workflows repo state (branch/commits/merged PRs), key session learnings, prioritized next steps (daemon reliability, observation, cloud acceleration, safety layers), cloud LLM considerations, verification commands for daemon/logs, and file preservation checklist for session continuity.
Ubuntu 26.04 Installation Prompt for MiniPC
ubuntu-install-prompt.md
Complete agent guidance prompt with eight installation steps: ISO acquisition/checksum verification, bootable USB creation, BIOS configuration, Ubuntu server install (SSH, no desktop), GRUB kernel parameter edits, post-boot apt/package/SSH setup, Tailscale remote access installation/auth, and SSH connectivity verification.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • B67687/agentic-workflows#88: Overlaps substantially with this PR in rewrites to agent-seed-architecture.md (loop/safety sections) and agent-seed-red-team.md (critical gaps table restructuring).
  • B67687/agentic-workflows#17: Both PRs modify AGENTS.md operating contract to change session archive query and context loading behavior during agent startup.
  • B67687/agentic-workflows#86: Shares modifications to AGENTS.md session archive querying and commit/checkpoint workflow rules.

Poem

🐰 Four layers deep, the costs now reign,
ADRs guide each decision's chain,
The agent learns: commit with care,
While safety checks float in the air.
Ubuntu boots, the MiniPC hops—
Watch for drift, as reasoning stops. 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description outlines the scope (handover, daemon, commits, setup), but omits critical verification details, testing confirmation, and migration steps required by the template. Add verification section confirming bash validation on new scripts (pro-cost-guard.sh, ubuntu-install-prompt.md references), CI pass status, and migration requirements for daemon/cost-guard config setup.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The PR title 'docs: add session handover for 2026-05-31' is specific and directly reflects the primary new addition (session-handover-2026-05-31.md), though it understates the scope which also includes research, ADR documents, architecture documentation, and updates to existing operational files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch docs/handover-2026-05-31

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues, and left some high level feedback:

  • The pro-cost-guard.sh script only ever emits status values of pass/warn and never actually inspects current spend or enforces a ceiling, which doesn’t match the ADR’s description of PASS/WARN/BLOCK behavior—consider wiring it into opencode stats and emitting a true BLOCK state when projected or actual spend exceeds PRO_MONTHLY_MAX.
  • agent-seed architecture docs and the new session handover describe different daemon implementations (640-line safety-layered daemon vs a 40-line minimal loop); consider explicitly distinguishing “target architecture” vs “current deployment” in one place to avoid confusion about which design is authoritative.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `pro-cost-guard.sh` script only ever emits `status` values of `pass`/`warn` and never actually inspects current spend or enforces a ceiling, which doesn’t match the ADR’s description of PASS/WARN/BLOCK behavior—consider wiring it into `opencode stats` and emitting a true BLOCK state when projected or actual spend exceeds `PRO_MONTHLY_MAX`.
- agent-seed architecture docs and the new session handover describe different daemon implementations (640-line safety-layered daemon vs a 40-line minimal loop); consider explicitly distinguishing “target architecture” vs “current deployment” in one place to avoid confusion about which design is authoritative.

## Individual Comments

### Comment 1
<location path="scripts/tools/pro-cost-guard.sh" line_range="55-56" />
<code_context>
+  MESSAGE="${MESSAGE} Cannot verify current spend (opencode stats unavailable)."
+fi
+
+cat <<EOF
+{"status":"${MODE}","monthMax":${CEILING},"message":"${MESSAGE}"}
+EOF
+
</code_context>
<issue_to_address>
**issue (bug_risk):** MESSAGE is interpolated into JSON without escaping, which can produce invalid JSON if it ever contains double quotes or newlines.

This is currently safe only because MESSAGE happens to avoid double quotes/newlines. Any future content change (quotes, backslashes, newlines) will break the JSON. Please construct the JSON via a tool that handles escaping (e.g., jq or a small python -c json.dumps helper), or add a shell escape helper to safely encode MESSAGE before interpolation.
</issue_to_address>

### Comment 2
<location path="scripts/tools/pro-cost-guard.sh" line_range="21-26" />
<code_context>
+
+COST_GUARD="${HOME}/.config/opencode/cost-guard.config.json"
+# Read ceiling from env or cost-guard config
+if [ -n "${PRO_MONTHLY_MAX:-}" ]; then
+  CEILING="$PRO_MONTHLY_MAX"
+elif [ -f "$COST_GUARD" ]; then
+  CEILING=$(python3 -c "import json; print(json.load(open('$COST_GUARD')).get('maxCostUsd', 30))" 2>/dev/null || echo "30")
+else
+  CEILING="30"
+fi
+
</code_context>
<issue_to_address>
**issue (bug_risk):** monthMax is emitted as a bare number but CEILING is never validated, so non-numeric input will lead to invalid JSON.

Because CEILING is never validated and monthMax is emitted unquoted, any non-numeric value (e.g. `PRO_MONTHLY_MAX=30usd` or a bad `maxCostUsd` in the config) will produce invalid JSON and can break consumers. Please either validate CEILING as numeric (and fall back to a safe default) before emitting it bare, or emit it as a quoted JSON string so arbitrary values remain syntactically valid JSON.
</issue_to_address>

### Comment 3
<location path="docs/architecture.md" line_range="28-29" />
<code_context>
+├─────────────────────────────────────────────────────────────┤
+│                   Subagent Delegation Layer                  │
+├──────────┬──────────┬──────────┬──────────┬──────────┬──────┤
+│ Architect│  Worker  │  Review  │  Plan    │  Explore │ Scout│
+│ v4-flash │ free     │ free     │ free     │ free     │ free │
+│ reasn:h  │ none     │ none     │ none     │ none     │ none │
</code_context>
<issue_to_address>
**issue (typo):** Typo in architecture diagram: `reasn:h` should likely be `reason:h` or similar.

Please correct the `reasn:h` label in the diagram (e.g., to `reason:h` or `reason: h`) to keep the architecture snapshot clear and consistent.

```suggestion
│ v4-flash │ free     │ free     │ free     │ free     │ free │
│ reason:h │ none     │ none     │ none     │ none     │ none │
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +55 to +56
cat <<EOF
{"status":"${MODE}","monthMax":${CEILING},"message":"${MESSAGE}"}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): MESSAGE is interpolated into JSON without escaping, which can produce invalid JSON if it ever contains double quotes or newlines.

This is currently safe only because MESSAGE happens to avoid double quotes/newlines. Any future content change (quotes, backslashes, newlines) will break the JSON. Please construct the JSON via a tool that handles escaping (e.g., jq or a small python -c json.dumps helper), or add a shell escape helper to safely encode MESSAGE before interpolation.

Comment on lines +21 to +26
if [ -n "${PRO_MONTHLY_MAX:-}" ]; then
CEILING="$PRO_MONTHLY_MAX"
elif [ -f "$COST_GUARD" ]; then
CEILING=$(python3 -c "import json; print(json.load(open('$COST_GUARD')).get('maxCostUsd', 30))" 2>/dev/null || echo "30")
else
CEILING="30"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): monthMax is emitted as a bare number but CEILING is never validated, so non-numeric input will lead to invalid JSON.

Because CEILING is never validated and monthMax is emitted unquoted, any non-numeric value (e.g. PRO_MONTHLY_MAX=30usd or a bad maxCostUsd in the config) will produce invalid JSON and can break consumers. Please either validate CEILING as numeric (and fall back to a safe default) before emitting it bare, or emit it as a quoted JSON string so arbitrary values remain syntactically valid JSON.

Comment thread docs/architecture.md
Comment on lines +28 to +29
│ v4-flash │ free │ free │ free │ free │ free │
│ reasn:h │ none │ none │ none │ none │ none │
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (typo): Typo in architecture diagram: reasn:h should likely be reason:h or similar.

Please correct the reasn:h label in the diagram (e.g., to reason:h or reason: h) to keep the architecture snapshot clear and consistent.

Suggested change
│ v4-flash │ free │ free │ free │ free │ free │
reasn:h │ none │ none │ none │ none │ none │
│ v4-flash │ free │ free │ free │ free │ free │
reason:h │ none │ none │ none │ none │ none │

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request establishes a comprehensive architecture documentation framework, introducing a living architecture snapshot, research on a two-tier agent architecture, and several Architecture Decision Records (ADRs) covering the tier system, model routing, and budget policies. It also adds a pre-flight cost guard script (pro-cost-guard.sh) to prevent budget overruns when delegating to premium models. Feedback on the changes highlights critical issues in the cost guard script, including case mismatches in status outputs, missing block logic for budget checks, and unsafe shell expansion in Python commands, along with a robust code suggestion to address these flaws.

Comment on lines +20 to +59
# Read ceiling from env or cost-guard config
if [ -n "${PRO_MONTHLY_MAX:-}" ]; then
CEILING="$PRO_MONTHLY_MAX"
elif [ -f "$COST_GUARD" ]; then
CEILING=$(python3 -c "import json; print(json.load(open('$COST_GUARD')).get('maxCostUsd', 30))" 2>/dev/null || echo "30")
else
CEILING="30"
fi

MODE="pass"
MESSAGE=""

# Check cost-guard mode
if [ -f "$COST_GUARD" ]; then
GUARD_MODE=$(python3 -c "import json; print(json.load(open('$COST_GUARD')).get('mode','warn'))" 2>/dev/null || echo "unknown")
GUARD_MAX=$(python3 -c "import json; print(json.load(open('$COST_GUARD')).get('maxCostUsd',0))" 2>/dev/null || echo "0")

if [ "$GUARD_MODE" = "block" ]; then
MODE="pass"
MESSAGE="Cost guard active at \$${GUARD_MAX} block mode. Pro approved if within ceiling."
else
MODE="warn"
MESSAGE="Cost guard mode is '${GUARD_MODE}' --- set to 'block' for full protection."
fi
else
MODE="warn"
MESSAGE="No cost-guard.config.json found --- Pro spend has no hard ceiling."
fi

# Check if we can reach opencode stats (sanity check)
if ! opencode stats --days 1 >/dev/null 2>&1; then
MODE="warn"
MESSAGE="${MESSAGE} Cannot verify current spend (opencode stats unavailable)."
fi

cat <<EOF
{"status":"${MODE}","monthMax":${CEILING},"message":"${MESSAGE}"}
EOF

exit 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There are several critical issues in the current implementation of pro-cost-guard.sh:

  1. Case Mismatch in Status Values: The script outputs lowercase status values ("pass" and "warn"), but the documentation and ADR-0004 explicitly expect uppercase values ("PASS", "WARN", "BLOCK"). This could break integrations or bypass the fallback mechanism.
  2. Missing BLOCK Logic: The script never actually performs the budget check or spend comparison, meaning it will never return "BLOCK". This directly violates the confirmation criteria in ADR-0004 (e.g., PRO_MONTHLY_MAX=0 must return BLOCK).
  3. Unsafe Shell Expansion in Python Command: Passing $COST_GUARD directly inside the single-quoted Python command string can cause syntax errors or shell injection if the path (which depends on $HOME) contains spaces or single quotes. Passing it via sys.argv is much safer.

We can resolve these issues by parsing the spend from opencode stats and performing a robust comparison in Python.

# Read ceiling from env or cost-guard config
if [ -n "${PRO_MONTHLY_MAX:-}" ]; then
  CEILING="$PRO_MONTHLY_MAX"
elif [ -f "$COST_GUARD" ]; then
  CEILING=$(python3 -c "import sys, json; print(json.load(open(sys.argv[1])).get('maxCostUsd', 30))" "$COST_GUARD" 2>/dev/null || echo "30")
else
  CEILING="30"
fi

MODE="PASS"
MESSAGE=""

# Check cost-guard mode
if [ -f "$COST_GUARD" ]; then
  GUARD_MODE=$(python3 -c "import sys, json; print(json.load(open(sys.argv[1])).get('mode','warn'))" "$COST_GUARD" 2>/dev/null || echo "unknown")
  GUARD_MAX=$(python3 -c "import sys, json; print(json.load(open(sys.argv[1])).get('maxCostUsd',0))" "$COST_GUARD" 2>/dev/null || echo "0")

  if [ "$GUARD_MODE" = "block" ]; then
    MODE="PASS"
    MESSAGE="Cost guard active at \$${GUARD_MAX} block mode. Pro approved if within ceiling."
  else
    MODE="WARN"
    MESSAGE="Cost guard mode is '${GUARD_MODE}' --- set to 'block' for full protection."
  fi
else
  MODE="WARN"
  MESSAGE="No cost-guard.config.json found --- Pro spend has no hard ceiling."
fi

# Query spend from opencode stats
SPEND="0.0"
if opencode stats --days 1 >/dev/null 2>&1; then
  SPEND=$(opencode stats --days 1 2>/dev/null | python3 -c "import sys, re; match = re.search(sys.argv[1], sys.stdin.read()); print(match.group(1) if match else sys.argv[2])" "([0-9]+\.[0-9]+)" "0.0")
else
  MODE="WARN"
  MESSAGE="${MESSAGE} Cannot verify current spend (opencode stats unavailable)."
fi

# Perform budget check
if ! python3 -c "import sys; spend, ceiling = float(sys.argv[1]), float(sys.argv[2]); sys.exit(0 if (ceiling > 0 and spend <= ceiling) else 1)" "$SPEND" "$CEILING" 2>/dev/null; then
  MODE="BLOCK"
  MESSAGE="Pro spend limit exceeded or blocked (\$${SPEND} spend, \$${CEILING} limit)."
fi

cat <<EOF
{"status":"${MODE}","monthMax":${CEILING},"message":"${MESSAGE}"}
EOF

exit 0

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@B67687 B67687 closed this May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants