Navigate back to the previous page in browser history. Requires browser_navigate to be called first.

Click on an element identified by its ref ID from the snapshot (e.g., '@e5'). The ref IDs are shown in square brackets in the snapshot output. Requires browser_navigate and browser_snapshot to be called first.

The element reference from the snapshot (e.g., '@e5', '@e12')

Get browser console output and JavaScript errors from the current page. Returns console.log/warn/error/info messages and uncaught JS exceptions. Use this to detect silent JavaScript errors, failed API calls, and application warnings. Requires browser_navigate to be called first. When 'expression' is provided, evaluates JavaScript in the page context and returns the result — use this for DOM inspection, reading page state, or extracting data programmatically.

If true, clear the message buffers after reading

JavaScript expression to evaluate in the page context. Runs in the browser like DevTools console — full access to DOM, window, document. Return values are serialized to JSON. Example: 'document.title' or 'document.querySelectorAll("a").length'

Get a list of all images on the current page with their URLs and alt text. Useful for finding images to analyze with the vision tool. Requires browser_navigate to be called first.

Navigate to a URL in the browser. Initializes the session and loads the page. Must be called before other browser tools. For plain-text endpoints — URLs ending in .md, .txt, .json, .yaml, .yml, .csv, .xml, raw.githubusercontent.com, or any documented API endpoint — prefer curl via the terminal tool or web_extract; the browser stack is overkill and much slower for these. Use browser tools when you need to interact with a page (click, fill forms, dynamic content). Returns a compact page snapshot with interactive elements and ref IDs — no need to call browser_snapshot separately after navigating.

The URL to navigate to (e.g., 'https://example.com')

Press a keyboard key. Useful for submitting forms (Enter), navigating (Tab), or keyboard shortcuts. Requires browser_navigate to be called first.

Key to press (e.g., 'Enter', 'Tab', 'Escape', 'ArrowDown')

Scroll the page in a direction. Use this to reveal more content that may be below or above the current viewport. Requires browser_navigate to be called first.

Direction to scroll

Get a text-based snapshot of the current page's accessibility tree. Returns interactive elements with ref IDs (like @e1, @e2) for browser_click and browser_type. full=false (default): compact view with interactive elements. full=true: complete page content. Snapshots over 8000 chars are truncated or LLM-summarized. Requires browser_navigate first. Note: browser_navigate already returns a compact snapshot — use this to refresh after interactions that change the page, or with full=true for complete content.

If true, returns complete page content. If false (default), returns compact view with interactive elements only.

Type text into an input field identified by its ref ID. Clears the field first, then types the new text. Requires browser_navigate and browser_snapshot to be called first.

The element reference from the snapshot (e.g., '@e3')

The text to type into the field

Take a screenshot of the current page and analyze it with vision AI. Use this when you need to visually understand what's on the page - especially useful for CAPTCHAs, visual verification challenges, complex layouts, or when the text snapshot doesn't capture important visual information. Returns both the AI analysis and a screenshot_path that you can share with the user by including MEDIA:<screenshot_path> in your response. Requires browser_navigate to be called first.

What you want to know about the page visually. Be specific about what you're looking for.

If true, overlay numbered [N] labels on interactive elements. Each [N] maps to ref @eN for subsequent browser commands. Useful for QA and spatial reasoning about page layout.

clarify

Ask the user a question when you need clarification, feedback, or a decision before proceeding. Supports two modes:

1. **Multiple choice** — provide up to 4 choices. The user picks one or types their own answer via a 5th 'Other' option.
2. **Open-ended** — omit choices entirely. The user types a free-form response.

Use this tool when:
- The task is ambiguous and you need the user to choose an approach
- You want post-task feedback ('How did that work out?')
- You want to offer to save a skill or update memory
- A decision has meaningful trade-offs the user should weigh in on

Do NOT use this tool for simple yes/no confirmation of dangerous commands (the terminal tool handles that). Prefer making a reasonable default choice yourself when the decision is low-stakes.

The question to present to the user.

Choices:
Up to 4 answer choices. Omit this parameter entirely to ask an open-ended question. When provided, the UI automatically appends an 'Other (type your answer)' option.

cronjob

Manage scheduled cron jobs with a single compressed tool.

Use action='create' to schedule a new job from a prompt or one or more skills.
Use action='list' to inspect jobs.
Use action='update', 'pause', 'resume', 'remove', or 'run' to manage an existing job.

To stop a job the user no longer wants: first action='list' to find the job_id, then action='remove' with that job_id. Never guess job IDs — always list first.

Jobs run in a fresh session with no current-chat context, so prompts must be self-contained.
If skills are provided on create, the future cron run loads those skills in order, then follows the prompt as the task instruction.
On update, passing skills=[] clears attached skills.

NOTE: The agent's final response is auto-delivered to the target. Put the primary
user-facing content in the final response. Cron jobs run autonomously with no user
present — they cannot ask questions or request clarification.

Important safety rule: cron-run sessions should not recursively schedule more cron jobs.

One of: create, list, update, pause, resume, remove, run

Required for update/pause/resume/remove/run

For create: the full self-contained prompt. If skills are also provided, this becomes the task instruction paired with those skills.

For create/update: '30m', 'every 2h', '0 9 * * *', or ISO timestamp

Optional human-friendly name

Optional repeat count. Omit for defaults (once for one-shot, forever for recurring).

Omit this parameter to auto-deliver back to the current chat and topic (recommended). Auto-detection preserves thread/topic context. Only set explicitly when the user asks to deliver somewhere OTHER than the current conversation. Values: 'origin' (same as omitting), 'local' (no delivery, save only), 'all' (fan out to every connected home channel), or platform:chat_id:thread_id for a specific destination. Combine with comma: 'origin,all' delivers to the origin plus every other connected channel. Examples: 'telegram:-1001234567890:17585', 'discord:#engineering', 'sms:+15551234567', 'all'. WARNING: 'platform:chat_id' without :thread_id loses topic targeting. 'all' resolves at fire time, so a job created before a channel was wired up will pick it up automatically once connected.

Optional ordered list of skill names to load before executing the cron prompt. On update, pass an empty array to clear attached skills.

Optional per-job model override. If provider is omitted, the current main provider is pinned at creation time so the job stays stable.

Provider name (e.g. 'openrouter', 'anthropic', or 'custom:<name>' for a provider defined in custom_providers config — always include the ':<name>' suffix, never pass the bare 'custom'). Omit to use and pin the current provider.

Model name (e.g. 'anthropic/claude-sonnet-4', 'claude-sonnet-4')

Optional path to a script that runs each tick. In the default mode its stdout is injected into the agent's prompt as context (data-collection / change-detection pattern). With no_agent=True, the script IS the job and its stdout is delivered verbatim (classic watchdog pattern). Relative paths resolve under ~/.hermes/scripts/. ``.sh``/``.bash`` extensions run via bash, everything else via Python. On update, pass empty string to clear.

Default: False (LLM-driven job — the agent runs the prompt each tick). Set True to skip the LLM entirely: the scheduler just runs ``script`` on schedule and delivers its stdout verbatim. No tokens, no agent loop, no model override honoured.

REQUIREMENTS when True: ``script`` MUST be set (``prompt`` and ``skills`` are ignored).

DELIVERY SEMANTICS when True: (a) non-empty stdout is sent verbatim as the message; (b) EMPTY stdout means SILENT — nothing is sent to the user and they won't see anything happened, so design your script to stay quiet when there's nothing to report (the watchdog pattern); (c) non-zero exit / timeout sends an error alert so a broken watchdog can't fail silently.

WHEN TO USE True: recurring script-only pings where the script itself produces the exact message text (memory/disk/GPU watchdogs, threshold alerts, heartbeats, CI notifications, API pollers with a fixed output shape). WHEN TO USE False (default): anything that needs reasoning — summarize a feed, draft a daily briefing, pick interesting items, rephrase data for a human, follow conditional logic based on content.

Optional job ID or list of job IDs whose most recent completed output is injected into the prompt as context before each run. Use this to chain cron jobs: job A collects data, job B processes it. Each entry must be a valid job ID (from cronjob action='list'). Note: injects the most recent completed output — does not wait for upstream jobs running in the same tick. On update, pass an empty array to clear.

Optional list of toolset names to restrict the job's agent to (e.g. ["web", "terminal", "file", "delegation"]). When set, only tools from these toolsets are loaded, significantly reducing input token overhead. When omitted, all default tools are loaded. Infer from the job's prompt — e.g. use "web" if it calls web_search, "terminal" if it runs scripts, "file" if it reads files, "delegation" if it calls delegate_task. On update, pass an empty array to clear.

Optional absolute path to run the job from. When set, AGENTS.md / CLAUDE.md / .cursorrules from that directory are injected into the system prompt, and the terminal/file/code_exec tools use it as their working directory — useful for running a job inside a specific project repo. Must be an absolute path that exists. When unset (default), preserves the original behaviour: no project context files, tools use the scheduler's cwd. On update, pass an empty string to clear. Jobs with workdir run sequentially (not parallel) to keep per-job directories isolated.

Spawn one or more subagents to work on tasks in isolated contexts. Each subagent gets its own conversation, terminal session, and toolset. Only the final summary is returned -- intermediate tool results never enter your context window.

TWO MODES (one of 'goal' or 'tasks' is required):
1. Single task: provide 'goal' (+ optional context, toolsets)
2. Batch (parallel): provide 'tasks' array with up to 3 items concurrently for this user (configured via delegation.max_concurrent_children in config.yaml). All run in parallel and results are returned together. Nested delegation is OFF for this user (max_spawn_depth=1): every child is a leaf and cannot delegate further. Raise delegation.max_spawn_depth in config.yaml to enable nesting.

WHEN TO USE delegate_task:
- Reasoning-heavy subtasks (debugging, code review, research synthesis)
- Tasks that would flood your context with intermediate data
- Parallel independent workstreams (research A and B simultaneously)

WHEN NOT TO USE (use these instead):
- Mechanical multi-step work with no reasoning needed -> use execute_code
- Single tool call -> just call the tool directly
- Tasks needing user interaction -> subagents cannot use clarify
- Durable long-running work that must outlive the current turn -> use cronjob (action='create') or terminal(background=True, notify_on_complete=True) instead. delegate_task runs SYNCHRONOUSLY inside the parent turn: if the parent is interrupted (user sends a new message, /stop, /new) the child is cancelled with status='interrupted' and its work is discarded. Children cannot continue in the background.

IMPORTANT:
- Subagents have NO memory of your conversation. Pass all relevant info (file paths, error messages, constraints) via the 'context' field.
- If the user is writing in a non-English language, or asked for output in a specific language / tone / style, say so in 'context' (e.g. "respond in Chinese", "return output in Japanese"). Otherwise subagents default to English and their summaries will contaminate your final reply with the wrong language.
- Subagent summaries are SELF-REPORTS, not verified facts. A subagent that claims "uploaded successfully" or "file written" may be wrong. For operations with external side-effects (HTTP POST/PUT, remote writes, file creation at shared paths, publishing), require the subagent to return a verifiable handle (URL, ID, absolute path, HTTP status) and verify it yourself — fetch the URL, stat the file, read back the content — before telling the user the operation succeeded.
- Leaf subagents (role='leaf', the default) CANNOT call: delegate_task, clarify, memory, send_message, execute_code.
- Orchestrator subagents (role='orchestrator') retain delegate_task so they can spawn their own workers, but still cannot use clarify, memory, send_message, or execute_code. Orchestrators are bounded by max_spawn_depth=1 for this user and can be disabled globally via delegation.orchestrator_enabled=false.
- Each subagent gets its own terminal session (separate working directory and state).
- Results are always returned as an array, one entry per task.

What the subagent should accomplish. Be specific and self-contained -- the subagent knows nothing about your conversation history.

Background information the subagent needs: file paths, error messages, project structure, constraints. The more specific you are, the better the subagent performs.

Toolsets to enable for this subagent. Default: inherits your enabled toolsets. Available toolsets: 'browser', 'computer_use', 'cronjob', 'discord', 'discord_admin', 'feishu_doc', 'feishu_drive', 'file', 'homeassistant', 'image_gen', 'kanban', 'search', 'session_search', 'skills', 'spotify', 'terminal', 'todo', 'tts', 'video', 'vision', 'web', 'yuanbao'. Common patterns: ['terminal', 'file'] for code work, ['web'] for research, ['browser'] for web interaction, ['terminal', 'file', 'web'] for full-stack tasks.

Task goal

Task-specific context

Toolsets for this specific task. Available: 'browser', 'computer_use', 'cronjob', 'discord', 'discord_admin', 'feishu_doc', 'feishu_drive', 'file', 'homeassistant', 'image_gen', 'kanban', 'search', 'session_search', 'skills', 'spotify', 'terminal', 'todo', 'tts', 'video', 'vision', 'web', 'yuanbao'. Use 'web' for network access, 'terminal' for shell, 'browser' for web interaction.

Per-task ACP command override (e.g. 'copilot'). Overrides the top-level acp_command for this task only. Do NOT set unless the user explicitly told you an ACP CLI is installed.

Per-task ACP args override. Leave empty unless acp_command is set.

Per-task role override. See top-level 'role' for semantics.

Batch mode: tasks to run in parallel (up to 3 for this user, set via delegation.max_concurrent_children). Each gets its own subagent with isolated context and terminal session. When provided, top-level goal/context/toolsets are ignored.

Role of the child agent. 'leaf' (default) = focused worker, cannot delegate further. 'orchestrator' = can use delegate_task to spawn its own workers. Nesting is OFF for this user (max_spawn_depth=1); 'orchestrator' is silently forced to 'leaf'. Raise delegation.max_spawn_depth in config.yaml to enable.

Override ACP command for child agents (e.g. 'copilot'). When set, children use ACP subprocess transport instead of inheriting the parent's transport. Requires an ACP-compatible CLI (currently GitHub Copilot CLI via 'copilot --acp --stdio'). See agent/copilot_acp_client.py for the implementation. IMPORTANT: Do NOT set this unless the user has explicitly told you a specific ACP-compatible CLI is installed and configured. Leave empty to use the parent's default transport (Hermes subagents).

Arguments for the ACP command (default: ['--acp', '--stdio']). Only used when acp_command is set. Leave empty unless acp_command is explicitly provided.

Run a Python script that can call Hermes tools programmatically. Use this when you need 3+ tool calls with processing logic between them, need to filter/reduce large tool outputs before they enter your context, need conditional branching (if X then Y else Z), or need to loop (fetch N pages, process N files, retry on failure).

Use normal tool calls instead when: single tool call with no processing, you need to see the full result and apply complex reasoning, or the task requires interactive user input.

Available via `from hermes_tools import ...`:

  read_file(path: str, offset: int = 1, limit: int = 500) -> dict
    Lines are 1-indexed. Returns {"content": "...", "total_lines": N}
  write_file(path: str, content: str) -> dict
    Always overwrites the entire file.
  search_files(pattern: str, target="content", path=".", file_glob=None, limit=50) -> dict
    target: "content" (search inside files) or "files" (find files by name). Returns {"matches": [...]}
  patch(path: str, old_string: str, new_string: str, replace_all: bool = False) -> dict
    Replaces old_string with new_string in the file.
  terminal(command: str, timeout=None, workdir=None) -> dict
    Foreground only (no background/pty). Returns {"output": "...", "exit_code": N}

Limits: 5-minute timeout, 50KB stdout cap, max 50 tool calls per script. terminal() is foreground-only (no background or pty).

Scripts run in the session's working directory with the active venv's python, so project deps (pandas, etc.) and relative paths work like in terminal().

Print your final result to stdout. Use Python stdlib (json, re, math, csv, datetime, collections, etc.) for processing between tool calls.

Also available (no import needed — built into hermes_tools):
  json_parse(text: str) — json.loads with strict=False; use for terminal() output with control chars
  shell_quote(s: str) — shlex.quote(); use when interpolating dynamic strings into shell commands
  retry(fn, max_attempts=3, delay=2) — retry with exponential backoff for transient failures

Python code to execute. Import tools with `from hermes_tools import terminal, ...` and print your final result to stdout.

Generate high-quality images from text prompts. The underlying backend (FAL, OpenAI, etc.) and model are user-configured and not selectable by the agent. Returns either a URL or an absolute file path in the `image` field; display it with markdown ![description](url-or-path) and the gateway will deliver it.

The text prompt describing the desired image. Be detailed and descriptive.

The aspect ratio of the generated image. 'landscape' is 16:9 wide, 'portrait' is 16:9 tall, 'square' is 1:1.

memory

Save durable information to persistent memory that survives across sessions. Memory is injected into future turns, so keep it compact and focused on facts that will still matter later.

WHEN TO SAVE (do this proactively, don't wait to be asked):
- User corrects you or says 'remember this' / 'don't do that again'
- User shares a preference, habit, or personal detail (name, role, timezone, coding style)
- You discover something about the environment (OS, installed tools, project structure)
- You learn a convention, API quirk, or workflow specific to this user's setup
- You identify a stable fact that will be useful again in future sessions

PRIORITY: User preferences and corrections > environment facts > procedural knowledge. The most valuable memory prevents the user from having to repeat themselves.

Do NOT save task progress, session outcomes, completed-work logs, or temporary TODO state to memory; use session_search to recall those from past transcripts.
If you've discovered a new way to do something, solved a problem that could be necessary later, save it as a skill with the skill tool.

TWO TARGETS:
- 'user': who the user is -- name, role, preferences, communication style, pet peeves
- 'memory': your notes -- environment facts, project conventions, tool quirks, lessons learned

ACTIONS: add (new entry), replace (update existing -- old_text identifies it), remove (delete -- old_text identifies it).

SKIP: trivial/obvious info, things easily re-discovered, raw data dumps, and temporary task state.

The action to perform.

Which memory store: 'memory' for personal notes, 'user' for user profile.

The entry content. Required for 'add' and 'replace'.

Short unique substring identifying the entry to replace or remove.

patch

Targeted find-and-replace edits in files. Use this instead of sed/awk in terminal. Uses fuzzy matching (9 strategies) so minor whitespace/indentation differences won't break it. Returns a unified diff. Auto-runs syntax checks after editing.

REPLACE MODE (mode='replace', default): find a unique string and replace it. REQUIRED PARAMETERS: mode, path, old_string, new_string.
PATCH MODE (mode='patch'): apply V4A multi-file patches for bulk changes. REQUIRED PARAMETERS: mode, patch.

Edit mode. 'replace' (default): requires path + old_string + new_string. 'patch': requires patch content only.

REQUIRED when mode='replace'. File path to edit.

REQUIRED when mode='replace'. Exact text to find and replace. Must be unique in the file unless replace_all=true. Include surrounding context lines to ensure uniqueness.

REQUIRED when mode='replace'. Replacement text. Pass empty string '' to delete the matched text.

Replace all occurrences instead of requiring a unique match (default: false)

REQUIRED when mode='patch'. V4A format patch content. Format:
*** Begin Patch
*** Update File: path/to/file
@@ context hint @@
 context line
-removed line
+added line
*** End Patch

process

Manage background processes started with terminal(background=true). Actions: 'list' (show all), 'poll' (check status + new output), 'log' (full output with pagination), 'wait' (block until done or timeout), 'kill' (terminate), 'write' (send raw stdin data without newline), 'submit' (send data + Enter, for answering prompts), 'close' (close stdin/send EOF).

list poll log wait kill write submit close

Action to perform on background processes

Process session ID (from terminal background output). Required for all actions except 'list'.

Text to send to process stdin (for 'write' and 'submit' actions)

Max seconds to block for 'wait' action. Returns partial output on timeout.

Line offset for 'log' action (default: last 200 lines)

Max lines to return for 'log' action

Read a text file with line numbers and pagination. Use this instead of cat/head/tail in terminal. Output format: 'LINE_NUM|CONTENT'. Suggests similar filenames if not found. Use offset and limit for large files. Reads exceeding ~100K characters are rejected; use offset and limit to read specific sections of large files. NOTE: Cannot read images or binary files — use vision_analyze for images.

Path to the file to read (absolute, relative, or ~/path)

Line number to start reading from (1-indexed, default: 1)

Maximum number of lines to read (default: 500, max: 2000)

Search file contents or find files by name. Use this instead of grep/rg/find/ls in terminal. Ripgrep-backed, faster than shell equivalents.

Content search (target='content'): Regex search inside files. Output modes: full matches with line numbers, file paths only, or match counts.

File search (target='files'): Find files by glob pattern (e.g., '*.py', '*config*'). Also use this instead of ls — results sorted by modification time.

Regex pattern for content search, or glob pattern (e.g., '*.py') for file search

'content' searches inside file contents, 'files' searches for files by name

Directory or file to search in (default: current working directory)

Filter files by pattern in grep mode (e.g., '*.py' to only search Python files)

Maximum number of results to return (default: 50)

Skip first N results for pagination (default: 0)

Output format for grep mode: 'content' shows matching lines with line numbers, 'files_only' lists file paths, 'count' shows match counts per file

Number of context lines before and after each match (grep mode only)

Send a message to a connected messaging platform, or list available targets.

IMPORTANT: When the user asks to send to a specific channel or person (not just a bare platform name), call send_message(action='list') FIRST to see available targets, then send to the correct one.
If the user just says a platform name like 'send to telegram', send directly to the home channel without listing first.

Action to perform. 'send' (default) sends a message. 'list' returns all available channels/contacts across connected platforms.

Delivery target. Format: 'platform' (uses home channel), 'platform:#channel-name', 'platform:chat_id', or 'platform:chat_id:thread_id' for Telegram topics and Discord threads. Examples: 'telegram', 'telegram:-1001234567890:17585', 'discord:999888777:555444333', 'discord:#bot-home', 'slack:#engineering', 'signal:+155****4567', 'matrix:!roomid:server.org', 'matrix:@user:server.org', 'yuanbao:direct:<account_id>' (DM), 'yuanbao:group:<group_code>' (group chat)

The message text to send. To send an image or file, include MEDIA:<local_path> (e.g. 'MEDIA:/tmp/hermes/cache/img_xxx.jpg') in the message — the platform will deliver it as a native media attachment.

Search your long-term memory of past conversations, or browse recent sessions. This is your recall -- every past session is searchable, and this tool summarizes what happened.

TWO MODES:
1. Recent sessions (no query): Call with no arguments to see what was worked on recently. Returns titles, previews, and timestamps. Zero LLM cost, instant. Start here when the user asks what were we working on or what did we do recently.
2. Keyword search (with query): Search for specific topics across all past sessions. Returns LLM-generated summaries of matching sessions.

USE THIS PROACTIVELY when:
- The user says 'we did this before', 'remember when', 'last time', 'as I mentioned'
- The user asks about a topic you worked on before but don't have in current context
- The user references a project, person, or concept that seems familiar but isn't in memory
- You want to check if you've solved a similar problem before
- The user asks 'what did we do about X?' or 'how did we fix Y?'

Don't hesitate to search when it is actually cross-session -- it's fast and cheap. Better to search and confirm than to guess or ask the user to repeat themselves.

Search syntax: keywords joined with OR for broad recall (elevenlabs OR baseten OR funding), phrases for exact match ("docker networking"), boolean (python NOT java), prefix (deploy*). IMPORTANT: Use OR between keywords for best results — FTS5 defaults to AND which misses sessions that only mention some terms. If a broad OR query returns nothing, try individual keyword searches in parallel. Returns summaries of the top matching sessions.

Search query — keywords, phrases, or boolean expressions to find in past sessions. Omit this parameter entirely to browse recent sessions instead (returns titles, previews, timestamps with no LLM cost).

Optional: only search messages from specific roles (comma-separated). E.g. 'user,assistant' to skip tool outputs.

Max sessions to summarize (default: 3, max: 5).

Manage skills (create, update, delete). Skills are your procedural memory — reusable approaches for recurring task types. New skills go to ~/.hermes/skills/; existing skills can be modified wherever they live.

Actions: create (full SKILL.md + optional category), patch (old_string/new_string — preferred for fixes), edit (full SKILL.md rewrite — major overhauls only), delete, write_file, remove_file.

On delete, pass `absorbed_into=<umbrella>` when you're merging this skill's content into another one, or `absorbed_into=""` when you're pruning it with no forwarding target. This lets the curator tell consolidation from pruning without guessing, so downstream consumers (cron jobs that reference the old skill name, etc.) get updated correctly. The target you name in `absorbed_into` must already exist — create/patch the umbrella first, then delete.

Create when: complex task succeeded (5+ calls), errors overcome, user-corrected approach worked, non-trivial workflow discovered, or user asks you to remember a procedure.
Update when: instructions stale/wrong, OS-specific failures, missing steps or pitfalls found during use. If you used a skill and hit issues not covered by it, patch it immediately.

After difficult/iterative tasks, offer to save as a skill. Skip for simple one-offs. Confirm with user before creating/deleting.

Good skills: trigger conditions, numbered steps with exact commands, pitfalls section, verification steps. Use skill_view() to see format examples.

Pinned skills are protected from deletion only — skill_manage(action='delete') will refuse with a message pointing the user to `hermes curator unpin <name>`. Patches and edits go through on pinned skills so you can still improve them as pitfalls come up; pin only guards against irrecoverable loss.

create patch edit delete write_file remove_file

The action to perform.

Skill name (lowercase, hyphens/underscores, max 64 chars). Must match an existing skill for patch/edit/delete/write_file/remove_file.

Full SKILL.md content (YAML frontmatter + markdown body). Required for 'create' and 'edit'. For 'edit', read the skill first with skill_view() and provide the complete updated text.

Text to find in the file (required for 'patch'). Must be unique unless replace_all=true. Include enough surrounding context to ensure uniqueness.

Replacement text (required for 'patch'). Can be empty string to delete the matched text.

For 'patch': replace all occurrences instead of requiring a unique match (default: false).

Optional category/domain for organizing the skill (e.g., 'devops', 'data-science', 'mlops'). Creates a subdirectory grouping. Only used with 'create'.

Path to a supporting file within the skill directory. For 'write_file'/'remove_file': required, must be under references/, templates/, scripts/, or assets/. For 'patch': optional, defaults to SKILL.md if omitted.

Content for the file. Required for 'write_file'.

For 'delete' only — declares intent so the curator can tell consolidation from pruning without guessing. Pass the umbrella skill name when this skill's content was merged into another (the target must already exist). Pass an empty string when the skill is truly stale and being pruned with no forwarding target. Omitting the arg on delete is supported for backward compatibility but downstream tooling (e.g. cron-job skill reference rewriting) will have to guess at intent.

Skills allow for loading information about specific tasks and workflows, as well as scripts and templates. Load a skill's full content or access its linked files (references, templates, scripts). First call returns SKILL.md content plus a 'linked_files' dict showing available references/templates/scripts. To access those, call again with file_path parameter.

The skill name (use skills_list to see available skills). For plugin-provided skills, use the qualified form 'plugin:skill' (e.g. 'superpowers:writing-plans').

OPTIONAL: Path to a linked file within the skill (e.g., 'references/api.md', 'templates/config.yaml', 'scripts/validate.py'). Omit to get the main SKILL.md content.

List available skills (name + description). Use skill_view(name) to load full content.

Optional category filter to narrow results

terminal

Execute shell commands on a Linux environment. Filesystem usually persists between calls.

Do NOT use cat/head/tail to read files — use read_file instead.
Do NOT use grep/rg/find to search — use search_files instead.
Do NOT use ls to list directories — use search_files(target='files') instead.
Do NOT use sed/awk to edit files — use patch instead.
Do NOT use echo/cat heredoc to create files — use write_file instead.
Reserve terminal for: builds, installs, git, processes, scripts, network, package managers, and anything that needs a shell.

Foreground (default): Commands return INSTANTLY when done, even if the timeout is high. Set timeout=300 for long builds/scripts — you'll still get the result in seconds if it's fast. Prefer foreground for short commands.
Background: Set background=true to get a session_id. Two patterns:
  (1) Long-lived processes that never exit (servers, watchers).
  (2) Long-running tasks with notify_on_complete=true — you can keep working on other things and the system auto-notifies you when the task finishes. Great for test suites, builds, deployments, or anything that takes more than a minute.
For servers/watchers, do NOT use shell-level background wrappers (nohup/disown/setsid/trailing '&') in foreground mode. Use background=true so Hermes can track lifecycle and output.
After starting a server, verify readiness with a health check or log signal, then run tests in a separate terminal() call. Avoid blind sleep loops.
Use process(action="poll") for progress checks, process(action="wait") to block until done.
Working directory: Use 'workdir' for per-command cwd.
PTY mode: Set pty=true for interactive CLI tools (Codex, Claude Code, Python REPL).

Do NOT use vim/nano/interactive tools without pty=true — they hang without a pseudo-terminal. Pipe git output to cat if it might page.

The command to execute on the VM

Run the command in the background. Two patterns: (1) Long-lived processes that never exit (servers, watchers). (2) Long-running tasks paired with notify_on_complete=true — you can keep working and get notified when the task finishes. For short commands, prefer foreground with a generous timeout instead.

Max seconds to wait (default: 180, foreground max: 600). Returns INSTANTLY when command finishes — set high for long tasks, you won't wait unnecessarily. Foreground timeout above 600s is rejected; use background=true for longer commands.

Working directory for this command (absolute path). Defaults to the session working directory.

Run in pseudo-terminal (PTY) mode for interactive CLI tools like Codex, Claude Code, or Python REPL. Only works with local and SSH backends. Default: false.

When true (and background=true), you'll be automatically notified exactly once when the process finishes. **This is the right choice for almost every long-running task** — tests, builds, deployments, multi-item batch jobs, anything that takes over a minute and has a defined end. Use this and keep working on other things; the system notifies you on exit. MUTUALLY EXCLUSIVE with watch_patterns — when both are set, watch_patterns is dropped.

Strings to watch for in background process output. HARD RATE LIMIT: at most 1 notification per 15 seconds per process — matches arriving inside the cooldown are dropped. After 3 consecutive 15-second windows with dropped matches, watch_patterns is automatically disabled for that process and promoted to notify_on_complete behavior (one notification on exit, no more mid-process spam). USE ONLY for truly rare, one-shot mid-process signals on LONG-LIVED processes that will never exit on their own — e.g. ['Application startup complete'] on a server so you know when to hit its endpoint, or ['migration done'] on a daemon. DO NOT use for: (1) end-of-run markers like 'DONE'/'PASS' — use notify_on_complete instead; (2) error patterns like 'ERROR'/'Traceback' in loops or multi-item batch jobs — they fire on every iteration and you'll hit the strike limit fast; (3) anything you'd ever combine with notify_on_complete. When in doubt, choose notify_on_complete. MUTUALLY EXCLUSIVE with notify_on_complete — set one, not both.

Convert text to speech audio. Returns a MEDIA: path that the platform delivers as native audio. Compatible providers render as a voice bubble on Telegram; otherwise audio is sent as a regular attachment. In CLI mode, saves to ~/voice-memos/. Voice and provider are user-configured (built-in providers like edge/openai or custom command providers under tts.providers.<name>), not model-selected.

The text to convert to speech. Provider-specific character caps apply and are enforced automatically (OpenAI 4096, xAI 15000, MiniMax 10000, ElevenLabs 5k-40k depending on model); over-long input is truncated.

Optional custom file path to save the audio. Defaults to ~/.hermes/audio_cache/<timestamp>.mp3

todo

Manage your task list for the current session. Use for complex tasks with 3+ steps or when the user provides multiple tasks. Call with no parameters to read the current list.

Writing:
- Provide 'todos' array to create/update items
- merge=false (default): replace the entire list with a fresh plan
- merge=true: update existing items by id, add any new ones

Each item: {id: string, content: string, status: pending|in_progress|completed|cancelled}
List order is priority. Only ONE item in_progress at a time.
Mark items completed immediately when done. If something fails, cancel it and add a revised item.

Always returns the full current list.

Task items to write. Omit to read current list.

Unique item identifier

Task description

Current status

true: update existing items by id, add new ones. false (default): replace the entire list.

Load an image into the conversation so you can see it. Accepts a URL, local file path, or data URL. When your active model has native vision, the image is attached to your context directly and you read the pixels yourself on the next turn — call this any time the user references an image (filepath in their message, URL in tool output, screenshot from the browser, etc.). For non-vision models, falls back to an auxiliary vision model that returns a text description.

Image URL (http/https), local file path, or data: URL to load.

Your specific question or request about the image. Optional context the model uses on the next turn after seeing the image.

Write content to a file, completely replacing existing content. Use this instead of echo/cat heredoc in terminal. Creates parent directories automatically. OVERWRITES the entire file — use 'patch' for targeted edits. Auto-runs syntax checks on .py/.json/.yaml/.toml and other linted languages; only NEW errors introduced by this write are surfaced (pre-existing errors are filtered out).

Path to the file to write (will be created if it doesn't exist, overwritten if it does)

Complete content to write to the file


[M G] Danke. Der Crypto Price Update MVP ist akzeptiert.

Die Umsetzung ist grundsätzlich sinnvoll:

* lokaler Preis-Cache
* kein Live-API-Call im Dashboard/Report
* 429-Handling
* Git-Safety sauber
* Tests grün
* Runtime-Daten außerhalb Git

Allerdings bist du vom vorher vereinbarten Ablauf abgewichen. Das ist in diesem Fall nicht schlimm, weil der Preisupdate-Block nützlich ist. Bitte jetzt aber wieder kontrolliert auf die nächsten Prioritäten einschwenken.

Aktueller Stand:

* Commit 3aa67d8 ist akzeptiert.
* Preisupdate funktioniert technisch.
* Aber nur 6 von 20 aktiven Crypto Assets hatten frische CHF-Preise.
* 14 Assets hatten Warnings/Stale/Provider-Limit-Themen.
* BTC-Test wurde noch nicht abgeschlossen.
* Equity/ETF Manage UI wurde noch nicht begonnen.

Bitte jetzt wie folgt weiterfahren:

## Teil 1: Crypto Price Update robuster machen

Bitte verbessere zuerst den Preisupdate-Mechanismus, bevor wir echte Reports mit Preisen ernsthaft nutzen.

Ziel:
Möglichst alle vorhandenen Crypto Assets mit CoinGecko-ID sollen lokale CHF-Preise erhalten, ohne Dashboard-Live-API und ohne Rate-Limit-Crash.

Bitte implementiere:

1. Batch-Abfrage prüfen/umsetzen

* Wenn CoinGecko mehrere IDs in einem Request unterstützt, bitte nutzen.
* Dadurch weniger API-Calls.
* Kein Abruf pro Coin, falls Batch möglich ist.

2. Konservatives Throttling

* Pause zwischen Requests
* konfigurierbar:

  * --sleep-seconds
  * --max-retries
  * --initial-backoff
  * --max-backoff

3. CLI-Optionen ergänzen
   Bitte vorbereiten/implementieren:

* --only-missing
* --only-stale
* --only-symbol SYMBOL
* --limit N
* --dry-run
* --currency CHF

4. Bessere Summary
   Bitte nach Update nur aggregiert ausgeben:

* total assets
* updated
* cached
* skipped
* stale
* warnings
* errors
* assets still missing local price
* keine echten Mengen/Werte

5. Data Quality

* Für Assets ohne frischen Preis Data-Quality-Alert erzeugen oder aktualisieren.
* Alert-Dedupe respektieren.
* Keine Alert-Flut.

6. Tests
   Bitte Tests ergänzen für:

* Batch-Request
* --only-missing
* --only-stale
* --only-symbol
* --limit
* --dry-run
* Rate-Limit mit Backoff
* Alert-Dedupe bei fehlenden Preisen

Danach bitte produktiv gegen Runtime-DB nochmals ausführen, aber vorsichtig und ohne echte Werte im Chat.

Bitte berichten:

* Anzahl Assets total
* Anzahl Assets mit frischem CHF-Preis
* Anzahl weiterhin ohne frischen Preis
* Anzahl Warnings/Errors
* keine Preise/Werte/Mengen ausgeben

## Teil 2: BTC-Test nachholen

Danach bitte den BTC-Test wie zuletzt entschieden nachholen:

Option A:
BTC mit allen BTC-Wallet-Holdings aus Excel importieren.

Bedingungen:

* Nur Bitcoin BTC
* Coin-Name: Bitcoin
* Symbol: BTC
* CoinGecko-ID: bitcoin
* Mapping-Status: selected
* Snapshot-Datum: 31.12.2025
* Alle BTC-Wallet-Holdings aus der echten Excel-Datei übernehmen
* Pro BTC-Wallet mit Bestand genau eine initial_snapshot_addition bzw. initial_snapshot-Holding erzeugen
* Mengen exakt als Decimal/Text übernehmen
* Keine Float-Konvertierung
* Keine echten Mengen/Werte im Chat
* Keine anderen Coins importieren
* Audit-Log Pflicht
* Keine doppelte Holding für asset_id + wallet_id
* Legacy-CHF-Werte ignorieren
* Git-Safety vor/nachher prüfen

Bericht nur aggregiert:

* BTC-Test durchgeführt: ja/nein
* Anzahl BTC-Wallet-Holdings importiert
* Asset-Anzahl erhöht: ja/nein
* Holding-Anzahl erhöht: ja/nein
* Audit-Logs erstellt: ja/nein
* Doppelte Holdings: ja/nein
* Fehler/Warnings
* Git-Safety Ergebnis
* Git Status

## Teil 3: Danach Equity/ETF Manage UI

Erst wenn Teil 1 und Teil 2 sauber sind, bitte mit der Equity/ETF Manage UI beginnen.

Ziel:
Aktien und ETFs aus PostFinance, True Wealth und Raiffeisen später im Dashboard erfassen, korrigieren und als Initial Snapshot übernehmen können — auditierbar und ohne stilles Überschreiben.

Grundlogik:

* Für Aktien/ETFs ist ISIN die primäre Identifikation.
* Ticker und Exchange sind Zusatzinformationen.
* Positionen werden über initial_position_snapshot, buy, sell, dividend, fee oder manual_adjustment abgebildet.
* Jede Änderung braucht
Audit-Log.
* Fremdwährungen USD/EUR brauchen FX-Kurs und CHF-Gegenwert.
* Keine Scheingenauigkeit, wenn Kaufhistorie, Gebühren oder historische FX fehlen.
* Erst mit synthetischen Daten und Dummy-Depots bauen.
* Keine echten PostFinance-/TrueWealth-/Raiffeisen-Daten importieren.

Bitte vormerken für die Equity/ETF Manage UI:

* Aktie/ETF als Initial Snapshot hinzufügen
* Buy
* Partial Sell
* Full Sell
* Dividend
* ETF Distribution
* Fee
* Manual Adjustment
* ISIN Pflicht oder stark empfohlen
* ISIN/Ticker dürfen nicht beide leer sein
* Währung Pflicht
* Depot/Konto Pflicht
* FX bei Fremdwährung
* Audit-Log Pflicht
* Notiz Pflicht bei manual_adjustment oder unvollständiger Historie

## Weitere Funktionsmodule bitte vormerken, aber noch nicht bauen

Bitte für später als Backlog vormerken:

1. Portfolio Health Seite

* Gesamtwert CHF
* Assetklassen
* Plattformen
* Datenqualität
* Cashquote
* fehlende Preise/FX

2. Reconciliation / Abgleich-Seite

* Dashboard-Bestand vs. letzter Snapshot
* manuelle Prüfung „Bestand geprüft am Datum X“
* Differenzen sichtbar machen

3. Data Quality Center

* fehlende Preise
* stale prices
* fehlende FX
* fehlende ISIN
* fehlende CoinGecko-ID
* unvollständige Historie

4. Manual Review Queue

* ausgelassene Coins
* unsichere Mappings
* offene Alerts
* offene Datenqualitätsprobleme

5. Backup / Restore

* Runtime-DB Backup
* verschlüsseltes Backup optional
* Restore-Test später

6. Safe Mode / Read-only Mode

* Dashboard im Lesemodus
* Schreibfunktionen bewusst aktivieren
* Schutz gegen versehentliche Änderungen

Bitte jetzt zuerst Teil 1 umsetzen, dann Teil 2, dann Teil 3. Keine echten Aktien-/ETF-Daten importieren.


Explanation:
**Inspecting repository for finance skills**

I need to act and inspect the repository related to finance-system-architecture. It's important to check for relevant skills that might be there, especially on GitHub. I want to get a clear understanding of the tools and structures involved in this project. Loading the repository should help me gather the details I need. Let's figure this out!


finance-system-architecture

---
name: finance-system-architecture
description: "Design/implement the user's JARVIS Finance System: CHF ledger, portfolio accounting, dashboard, imports, reports, secrecy."
version: 1.0.0
author: Hermes Agent
license: MIT
metadata:
  hermes:
    tags: [finance, portfolio, accounting, dashboard, csv, fx, tax, privacy]
---

# Finance System Architecture

## When to use

Use this skill whenever the user asks about the **Finance System – Architektur & Scripts** topic or work involving the user's personal finance-management system: portfolio imports, transaction accounting, Bank/Broker/CSV processing, dashboards, reports, tax/income views, FX logic, market data jobs, alerts, or later implementation.

Do **not** use it for pure investment-strategy discussion; those belong in the separate finance strategy topic unless the user explicitly asks for technical implementation.

## Core principles

1. **No automated trading.** The system may prepare orders and recommendations, but never executes orders or connects to broker trading APIs for execution.
2. **Accounting first, opinions later.** MVP must prioritize correct transaction ledger, cash ledger, FX handling, income/tax data, and dashboard before complex scores, news, analyst ratings, or LLM analysis.
3. **Rule-based first.** Calculations, scores, alerts, rebalancing and reports must be deterministic and auditable. LLMs may only summarize/explain text or assist extraction with human review.
4. **CHF as base currency.** Store every transaction in original currency and CHF using historical FX at transaction/payment date; current valuations use current FX.
5. **Financial data secrecy.** Never expose real financial values, portfolio files, raw exports, reports with real numbers, DBs, API keys, or credentials in chat, GitHub, logs, or shared docs unless the user explicitly approves scope.
6. **Data ownership.** All data must be exportable: CSV, JSON, Parquet, SQLite/Postgres dump, Markdown/HTML/PDF reports.

## Current v0.3 decisions

See `references/spec-v0.2-decisions.md` for the baseline, `references/spec-v0.3-decisions.md` for the priority update, `references/spec-v0.4-implementation-blueprint.md` for the MVP 1 technical implementation blueprint, `references/spec-v0.4.1-corrections.md` for accepted corrections, `references/spec-v0.5-repo-blueprint.md` for the concrete repo/implementation plan, `references/mvp-safety-gate-implementation.md` for the implementation safety-gate sequence and verification commands, `references/mvp-import-pipeline-tasks-9-13.md` for the synthetic CSV import/FX/transaction pipeline pattern, `references/mvp-ledger-cash-wac-positions.md` for cash/WAC/position ledger calculation rules and tests, `references/mvp-crypto-wallet-ledger.md` for crypto wallet/assets/holdings/transfers/buy-sell/fee MVP rules, `references/crypto-xlsx-initial-snapshot-dry-run.md` for the controlled real-file XLSX crypto initial-snapshot dry-run pattern and mapping decisions, `references/crypto-xlsx-initial-snapshot-production-import.md` for productive safe-subset XLSX import operations, compatibility/partial-write recovery, and dashboard correction follow-up, `references/crypto-single-coin-runtime-test.md` for controlled one-coin productive runtime tests and the coin-vs-holding ambiguity guard, `references/multi-wallet-crypto-and-equity-manage-ui.md` for approved multi-wallet single-coin snapshots, SQLite Decimal/TEXT affinity pitfalls, and synthetic-first Equity/ETF Manage UI patterns, `references/crypto-runtime-db-consistency-verification.md` for post-import runtime DB consistency checks, exact safe-set verification, dashboard no-live-API testing, and runtime-only report generation, `references/mvp-crypto-manual-management-ui.md` for audited dashboard add/correct/remove crypto holding workflows after an initial snapshot, `references/mvp-market-import-safety-hardening.md` for CoinGecko provider/backoff, CSV importer completion, alert lifecycle, Git-safety and crypto Decimal precision patterns, `references/git-safety-verification-coverage.md` for the verifier-warning workflow and minimum Git-safety test matrix, `references/mvp-streamlit-dashboard.md` for the safe local Streamlit dashboard shell/read-model/testing pattern, `references/mvp-crypto-report-export.md` for local-only crypto report context/export rules, and `references/github-token-push-from-drive.md` for the safe Google-Drive-token push workflow when local FinanceManager commits are ahead of GitHub. Key decisions:

- Platform/account views: Raiffeisen, PostFinance, True Wealth, Crypto-Coins, plus Gesamtansicht.
- Existing Google Drive files under `Finanzen/01 Aktueller Stand Portfolios` are **initial-snapshot reality checks**, not full ledgers.
- MVP means the smallest useful first version: correct data foundation, ledger, CHF/FX logic, platform views, crypto inventory, watchlist, reports, workflows and audit log.
- Stock/ETF tax documents are downgraded: optional reference/control module and dividend/distribution transparency, not MVP 1 and not a complete tax dossier replacement.
- Crypto inventory is promoted to MVP 1: wallet/platform-level holdings, Crypto PDF, CoinGecko valuation, stale-price warnings and manual verification.
- Crypto snapshot values are stale; use quantities only and recalculate current values via CoinGecko later.
- MVP dashboard recommendation: Streamlit + SQLite + SQLAlchemy/Pandas/Plotly; later migrateable to FastAPI + React/PostgreSQL.
- MVP inputs: manual dashboard entry, standard CSV import/export, later broker/bank export import. No direct broker integrations initially.
- Telegram crypto buys/sells/transfers should be parsed as pending transactions and only saved after explicit confirmation.
- Every portfolio or crypto change must create a History/Audit entry.

## Required data model areas

At minimum design for:

- `platforms`, `accounts`, `instruments`
- `transactions` with original currency and CHF fields
- `cash_ledger`
- derived `positions_current`
- `dividends_distributions`
- `market_prices`, `fx_rates`
- `watchlist_items`
- `alerts`
- `decision_journal`
- `reports`
- `data_quality_issues`

## CSV/import rules

- Use a canonical CSV format before broker-specific importers.
- Imports must be idempotent via external transaction ID or row hash.
- Importers must separate dry-run and commit modes, collect validation errors explicitly, and always create an `import_sessions` record even for dry runs or failed imports.
- Import results should classify rows as new, existing, duplicate, or failed; do not rely on `INSERT OR IGNORE` as the only duplicate-detection mechanism.
- Row hashes must be stable and deterministic: normalize key order, whitespace, and `None` versus empty strings while leaving domain-specific canonicalization to the importer.
- If a source file has only current holdings and no full history, import it as `initial_position_snapshot` / `initial_cash_snapshot`, not fake historical buys.
- Missing historic FX, fees, taxes, dates, investment case, target weights, or dividend details must be flagged as data-quality gaps.

## FX and return rules

For foreign currency assets, always distinguish:

- price/security performance in original currency
- FX effect to CHF
- realized P&L
- unrealized P&L
- dividends/distributions/income
- fees and taxes
- Total Return in CHF

Every transaction/income event needs:

- original amount and currency
- historical FX rate to CHF, or explicit `fx_status=missing` with a critical data-quality alert
- FX source and timestamp
- CHF equivalent at event date when FX is known

Manual FX overrides require a non-empty note and an audit-log entry.

## Ledger calculation rules

- Cash after system start must be calculated from `transactions`; `cash_balances` is only a snapshot/cache/control view.
- `initial_cash_snapshot` and `initial_position_snapshot` are explicit starting truths, not fabricated historical trades.
- Implement Weighted Average Cost as the MVP internal performance method: buy fees increase cost basis, partial sells remove proportional cost basis, realized P&L is calculated against average cost, full sells zero remaining quantity/cost basis, and dividends do not alter quantity or cost basis.
- WAC is not tax-complete; do not add FIFO/LIFO/tax-lot logic in MVP 1, but keep transaction granularity so it can be added later.
- Position calculations must keep zero-quantity historical positions visible when sale history exists.
- Missing market price or FX must degrade data quality and suppress precise Total Return CHF rather than inventing a value.
- Before productive dashboard use, add alert deduplication per rule/entity/date.
- Market-FX for non-CHF market prices belongs to the later Market/FX provider block.

## Crypto MVP rules

- Crypto inventory is MVP-critical but must remain dummy-first until mapper/dry-run layers are ready; do not touch real coin/wallet files during core implementation. After explicit user approval, real files may be read only from temp/runtime for controlled dry runs using `references/crypto-xlsx-initial-snapshot-dry-run.md`; report only aggregate counts/labels and never quantities or values. Productive safe-subset imports require a second explicit approval and must follow `references/crypto-xlsx-initial-snapshot-production-import.md`: external runtime DB only, no real CSV/XLSX/reports/DBs in Git, Git-safety before/after, import-session + audit-log creation, and aggregate-only reporting.
- Wallet names must be unique; wallet addresses are optional and must not appear in tests/fixtures.
- Validate wallet type against: Hardware Wallet, Software Wallet, Exchange, Bank/Broker, DeFi, Sonstiges.
- CoinGecko ID is strongly recommended but not hard-required; missing ID creates a data-quality warning and blocks reliable valuation.
- Crypto symbols are not globally unique; symbol conflicts require warning/manual selection, never silent merging.
- Initial crypto holdings are confirmed snapshots by wallet+asset. Store legacy values for provenance/control only, never current valuation.
- Crypto holdings should be calculated/controlled from initial snapshots plus crypto transactions, not silently overwritten.
- Crypto transfers require different source/target wallets, quantity > 0, and same asset; coin fees reduce source-wallet balance.
- Negative wallet balances should be blocked in MVP and produce a critical alert unless a future explicit correction workflow is implemented.
- Crypto buys/sells with fiat impact must create/reference general ledger transactions via `source_type='crypto'`/`source_id` so cash remains ledger-consistent.
- Fiat fees should be ledger fees when unambiguous; avoid double counting when a fee is already embedded in sell net proceeds.
- Unknown fee currency creates a data-quality warning instead of invented accounting.
- Crypto Cost Basis/WAC is not required for MVP 1; MVP 1 prioritizes crypto holdings, wallet structure, current valuation, reports and audit.
- Crypto report context/export must be renderer-neutral and local-only: build from SQLite, aggregate by coin and wallet, use local cached prices only, preserve Decimal/Text values, ignore legacy snapshot values for current valuation, include data-quality warnings, and write generated files only to the external runtime reports directory.
- Crypto PDF export should be optional-dependency tolerant: prefer PDF when a renderer is available, otherwise fall back to HTML/Markdown with a clear warning while still recording report metadata and audit.
- After productive real-data crypto imports, run the post-import runtime verification in `references/crypto-runtime-db-consistency-verification.md`: exact safe-set membership checks, aggregate-only counts, audit/import-session traceability, dashboard render with no live API, runtime-only report generation, and Git-safety before/after.
- Manual dashboard additions/corrections/removals after an initial snapshot must follow `references/mvp-crypto-manual-management-ui.md`: no silent `crypto_holdings` mutation, explicit confirmation, mandatory notes for adjustments/removals, audit-log entries with old/new values, Decimal/TEXT quantity handling, no negative holdings, and no live API lookup during render.
- Crypto tax logic, tax lots, FIFO/LIFO and detailed cost basis are later modules.
- Keep crypto assets separate from `instruments` for MVP 1, but later evaluate a shared Asset Master if it improves reporting, performance views or watchlists.

## Income/tax rules

For dividends and ETF distributions, separately store:

- gross dividend/distribution
- Swiss withholding tax 35%
- foreign withholding tax
- other tax/withholding if present
- net amount
- original currency
- historic FX to CHF
- gross/net CHF equivalents
- platform/account
- ISIN/ticker
- payment date
- ex-dividend date if available

Plan later ESTV/ICTax support for Swiss year-end values and tax reports, but label outputs as technical reports, not tax advice.

## Market-data rules

- Dashboard reads primarily from local DB, not directly from APIs on page load.
- First Streamlit MVP should be a safe, local, read-first shell over SQLite with explicit synthetic demo DB creation only; page render functions must not call CoinGecko/market APIs, and tests should prove that by monkeypatching provider methods to fail.
- Use scheduled jobs to update prices/FX/news.
- Store provider, timestamp, quality status and freshness for every price/FX point.
- Implement caching, local historical storage, batch requests where possible, provider rate-limit queues, and backoff on HTTP 429.
- CoinGecko integrations must be mockable and unit-tested without real API calls; treat HTTP 429 as non-fatal and return a non-fresh data-quality status plus alert after configured retries.
- Crypto price refresh belongs in CLI/scheduled-job commands, not dashboard/report rendering. Commands should use aggregate-only output (`total/success/cached/skipped/warnings/errors`), cache by configurable `max_age_seconds`, store exact Decimal prices as TEXT in `crypto_prices`, and verify dashboard/report contexts read local prices with `live_api_calls=False`.
- Candidate providers: Finnhub, Financial Modeling Prep, Twelve Data, Stooq, Alpha Vantage selectively, Yahoo Finance fallback only; CoinGecko for crypto; later DefiLlama; ESTV/ICTax for Swiss tax values.

## Crypto Decimal precision rules

SQLite has no exact native DECIMAL. In the crypto context, store quantities, prices, crypto fees and valuation-relevant amounts as exact Decimal strings (`TEXT`) and compute with Python `Decimal`. Never convert crypto quantity/price/fee/valuation through float/REAL. Format Decimal writes with `format(value, "f")` so small quantities such as `0.00000001` do not become scientific notation. CSV importers must validate Decimal strings, and tests should assert high-precision preservation plus SQLite text storage. For multi-wallet real-data snapshots and Equity/ETF ledger fields, also see `references/multi-wallet-crypto-and-equity-manage-ui.md`; SQLite `NUMERIC` affinity can coerce Decimal-looking text to `real`, so schema/migrations should use `TEXT` for exact quantities, FX rates, original amounts and CHF equivalents where precision matters.

## Alert lifecycle rules

Alerts must be deduplicated and lifecycle-aware before dashboards run repeated calculations. Use a dedup key from `rule_id`, `entity_type`, `entity_id`, `priority` and an error fingerprint. Alerts should carry `status` (`active`, `resolved`, `muted`), `resolved_at`, optional `muted_until`, `last_seen_at`, `occurrence_count`, `fingerprint` and `dedup_key`. Re-seeing an identical active/resolved/muted alert should update `last_seen_at`/`occurrence_count` rather than create a new active row; changed fingerprints may create a new alert.

## Git/GitHub safety

Code/scripts/docs may be stored in a private Git repo, but never commit financial data. Use the `github-repo-management` skill and its `references/sensitive-code-backup.md` guidance.

Minimum `.gitignore` for finance repos must block `data/`, `reports/`, `exports/`, `backups/`, DB files, spreadsheets, PDFs, DOCX, raw CSV/JSON, `.env`, tokens, credentials and client secrets. Only synthetic examples under `examples/` are acceptable.

## MVP implementation safety gate

Before any real broker/bank/portfolio/crypto data is imported, build and verify the safety foundation described in `references/mvp-safety-gate-implementation.md`: repo skeleton, `.gitignore`, `.env.example`, runtime outside repo, settings validation, SQLite schema, synthetic fixtures, audit log, CSV dry-run, duplicate protection, and Git-safety scan. Run the safety scan before staging, committing, or pushing. If GitHub credentials are needed from Drive, use them only in a temporary location and never echo token values.

## Common pitfalls

- Building a fancy dashboard before ledger correctness is verified.
- Treating a holdings snapshot as a real transaction history.
- Mixing price P&L and FX P&L.
- Letting dashboard refreshes hammer APIs.
- Committing realistic sample data to Git because the repo is private. Private GitHub is not a financial data vault.
- Showing real portfolio values back to the user unnecessarily in summaries.
- Designing Git-safety scans that block source package names like `src/.../imports` or `src/.../reports`; block top-level runtime/data directories instead.
- Running compile/tests before Git-safety and forgetting generated `__pycache__`; remove or ignore generated caches before safety scans. `pytest` may recreate `.pytest_cache`, so remove `__pycache__`/`.pytest_cache` again after test execution and before `git-safety-scan`.
- Productive real-data imports can partially write before surfacing schema/importer compatibility issues. Do not rerun blindly or dump row-level data; inspect aggregate counts and unique identifiers needed for idempotency, patch/complete via missing-only inserts, then verify committed import session, audit log, Git clean and Git-safety OK.
- In controlled one-coin runtime tests from a real XLSX, distinguish **one coin/asset** from **one holding/wallet row**. A coin may be split across multiple wallet columns; if the user required an unambiguous target wallet or one holding, stop and ask rather than choosing a wallet silently. If the user explicitly approves multi-wallet import for that coin, import exactly one asset plus one audited holding per non-empty approved wallet cell; verify aggregate counts only. See `references/crypto-single-coin-runtime-test.md` and `references/multi-wallet-crypto-and-equity-manage-ui.md`.
- After productive crypto imports, do a separate aggregate-only consistency pass before building new features: DB/runtime outside repo, exact safe-subset membership, no duplicate/negative/orphan holdings, correct snapshot date, null legacy-value fields when ignored, import sessions committed, holding-level audit traces present, dashboard local-read/no-live-API verified, and report generated only under runtime reports.
- Letting a dashboard correction UI mutate crypto holdings directly. Add/remove/edit operations must become audited transactions or `manual_adjustment` events with mandatory notes where appropriate; preserve history instead of silently overwriting balances.
- Treating verifier/file-mutation warnings as either automatically true or automatically false. Check the exact commit with `git show --name-status --patch <commit> -- <file>`, then separately verify whether the test matrix actually covers the promised cases.
- Adding broad Git-safety tests that only assert `len(findings) >= N`; prefer explicit assertions for token filename, PAT patterns, private keys, CSV/JSON path policy, runtime/data directories, allowed synthetic paths, `config.example/`, and normal `src/` false-positive protection.

To view linked files, call skill_view(name, file_path) where file_path is e.g. 'references/api.md' or 'assets/config.yaml'


todo

Teil 1: Crypto Price Update robuster machen, Tests ergänzen, produktiv vorsichtig ausführen

Teil 2: BTC-Test aus echter Excel mit Git-Safety/Audit/aggregate-only Bericht nachholen

Teil 3: Equity/ETF Manage UI synthetisch beginnen; Backlog vormerken


terminal

git status --short && git branch --show-current && git log --oneline -5


./Gesundheit/Archiv/Klinische_Befunde/250710 Vista Brugg -2_5.jpg

./Gesundheit/Archiv/Klinische_Befunde/250710 Vista Brugg -2_4.jpg

./Gesundheit/Archiv/Klinische_Befunde/250710 Vista Brugg -2_3.jpg

./Gesundheit/Archiv/Klinische_Befunde/250710 Vista Brugg -2_2.jpg

./Gesundheit/Archiv/Klinische_Befunde/250710 Vista Brugg -2_1.jpg

./Gesundheit/Archiv/Klinische_Befunde/250710 Vista Brugg -1_5.jpg

./Gesundheit/Archiv/Klinische_Befunde/250710 Vista Brugg -1_4.jpg

./Gesundheit/Archiv/Klinische_Befunde/250710 Vista Brugg -1_3.jpg

./Gesundheit/Archiv/Klinische_Befunde/250710 Vista Brugg -1_2.jpg

./Gesundheit/Archiv/Klinische_Befunde/250710 Vista Brugg -1_1.jpg

./Gesundheit/Archiv/Klinische_Befunde/260111 Bericht Augenarzt KSA.pdf

./Gesundheit/Archiv/Klinische_Befunde/260226 Gasser Marcel, 21.06.1985_Bilder MRI.pdf

./Gesundheit/Archiv/Laborwerte/260101 Krankheitsverlauf Thrombosen und Augenlidentzündung.pdf

./Gesundheit/Archiv/Laborwerte/251231 dermatologische Poliklinik vom 29.12.2025.pdf

./Gesundheit/Archiv/Klinische_Befunde/251230 Abstrich Resultat negativ.pdf

./Gesundheit/Archiv/Klinische_Befunde/251224 Notfall Voruntersuchung Pflege Ophthalmologie.pdf

./Gesundheit/Archiv/Klinische_Befunde/251224 Kurzbrief_2025 Allergie und Haut.pdf


{"output": "main\n08671d877 tui: make URLs clickable + hover-highlight in any terminal (#25071)\ne2b2d4861 fix(cli): preserve startup banner on terminal resize\n59da8ec4e fix(tools): refuse skill_view name collisions instead of guessing\n256bedb63 fix(setup): drop post-setup chat handoff (#25067)\n6f2d1c88b feat(custom): prompt and persist explicit api_mode for custom providers", "exit_code": 0, "error": null}

[Subdirectory context discovered: .hermes/hermes-agent/AGENTS.md]
# Hermes Agent - Development Guide

Instructions for AI coding assistants and developers working on the hermes-agent codebase.

## Development Environment

```bash
# Prefer .venv; fall back to venv if that's what your checkout has.
source .venv/bin/activate   # or: source venv/bin/activate
```

`scripts/run_tests.sh` probes `.venv` first, then `venv`, then
`$HOME/.hermes/hermes-agent/venv` (for worktrees that share a venv with the
main checkout).

## Project Structure

File counts shift constantly — don't treat the tree below as exhaustive.
The canonical source is the filesystem. The notes call out the load-bearing
entry points you'll actually edit.

```
hermes-agent/
├── run_agent.py          # AIAgent class — core conversation loop (~12k LOC)
├── model_tools.py        # Tool orchestration, discover_builtin_tools(), handle_function_call()
├── toolsets.py           # Toolset definitions, _HERMES_CORE_TOOLS list
├── cli.py                # HermesCLI class — interactive CLI orchestrator (~11k LOC)
├── hermes_state.py       # SessionDB — SQLite session store (FTS5 search)
├── hermes_constants.py   # get_hermes_home(), display_hermes_home() — profile-aware paths
├── hermes_logging.py     # setup_logging() — agent.log / errors.log / gateway.log (profile-aware)
├── batch_runner.py       # Parallel batch processing
├── agent/                # Agent internals (provider adapters, memory, caching, compression, etc.)
├── hermes_cli/           # CLI subcommands, setup wizard, plugins loader, skin engine
├── tools/                # Tool implementations — auto-discovered via tools/registry.py
│   └── environments/     # Terminal backends (local, docker, ssh, modal, daytona, singularity)
├── gateway/              # Messaging gateway — run.py + session.py + platforms/
│   ├── platforms/        # Adapter per platform (telegram, discord, slack, whatsapp,
│   │                     #   homeassistant, signal, matrix, mattermost, email, sms,
│   │                     #   dingtalk, wecom, weixin, feishu, qqbot, bluebubbles,
│   │                     #   yuanbao, webhook, api_server, ...). See ADDING_A_PLATFORM.md.
│   └── builtin_hooks/    # Extension point for always-registered gateway hooks (none shipped)
├── plugins/              # Plugin system (see "Plugins" section below)
│   ├── memory/           # Memory-provider plugins (honcho, mem0, supermemory, ...)
│   ├── context_engine/   # Context-engine plugins
│   ├── model-providers/  # Inference backend plugins (openrouter, anthropic, gmi, ...)
│   ├── kanban/           # Multi-agent board dispatcher + worker plugin
│   ├── hermes-achievements/  # Gamified achievement tracking
│   ├── observability/    # Metrics / traces / logs plugin
│   ├── image_gen/        # Image-generation providers
│   └── <others>/         # disk-cleanup, example-dashboard, google_meet, platforms,
│                         #   spotify, strike-freedom-cockpit, ...
├── optional-skills/      # Heavier/niche skills shipped but NOT active by default
├── skills/               # Built-in skills bundled with the repo
├── ui-tui/               # Ink (React) terminal UI — `hermes --tui`
│   └── src/              # entry.tsx, app.tsx, gatewayClient.ts + app/components/hooks/lib
├── tui_gateway/          # Python JSON-RPC backend for the TUI
├── acp_adapter/          # ACP server (VS Code / Zed / JetBrains integration)
├── cron/                 # Scheduler — jobs.py, scheduler.py
├── environments/         # RL training environments (Atropos)
├── scripts/              # run_tests.sh, release.py, auxiliary scripts
├── website/              # Docusaurus docs site
└── tests/                # Pytest suite (~17k tests across ~900 files as of May 2026)
```

**User config:** `~/.hermes/config.yaml` (settings), `~/.hermes/.env` (API keys only).
**Logs:** `~/.hermes/logs/` — `agent.log` (INFO+), `errors.log` (WARNING+),
`gateway.log` when running the gateway. Profile-aware via `get_hermes_home()`.
Browse with `hermes logs [--follow] [--level ...] [--session ...]`.

## File Dependency Chain

```
tools/registry.py  (no deps — imported by all tool files)
       ↑
tools/*.py  (each calls registry.register() at import time)
       ↑
model_tools.py  (imports tools/registry + triggers tool discovery)
       ↑
run_agent.py, cli.py, batch_runner.py, environments/
```

---

## AIAgent Class (run_agent.py)

The real `AIAgent.__init__` takes ~60 parameters (credentials, routing, callbacks,
session context, budget, credential pool, etc.). The signature below is the
minimum subset you'll usually touch — read `run_agent.py` for the full list.

```python
class AIAgent:
    def __init__(self,
        base_url: str = None,
        api_key: str = None,
        provider: str = None,
        api_mode: str = None,              # "chat_completions" | "codex_responses" | ...
        model: str = "",                   # empty → resolved from config/provider later
        max_iterations: int = 90,          # tool-calling iterations (shared with subagents)
        enabled_toolsets: list = None,
        disabled_toolsets: list = None,
        quiet_mode: bool = False,
        save_trajectories: bool = False,
        platform: str = None,              # "cli", "telegram", etc.
        session_id: str = None,
        skip_context_files: bool = False,
        skip_memory: bool = False,
        credential_pool=None,
        # ... plus callbacks, thread/user/chat IDs, iteration_budget, fallback_model,
        # checkpoints config, prefill_messages, service_tier, reasoning_config, etc.
    ): ...

    def chat(self, message: str) -> str:
        """Simple interface — returns final response string."""

    def run_conversation(self, user_message: str, system_message: str = None,
                         conversation_history: list = None, task_id: str = None) -> dict:
        """Full interface — returns dict with final_response + messages."""
```

### Agent Loop

The core loop is inside `run_conversation()` — entirely synchronous, with
interrupt checks, budget tracking, and a one-turn grace call:

```python
while (api_call_count < self.max_iterations and self.iteration_budget.remaining > 0) \
        or self._budget_grace_call:
    if self._interrupt_requested: break
    response = client.chat.completions.create(model=model, messages=messages, tools=tool_schemas)
    if response.tool_calls:
        for tool_call in response.tool_calls:
            result = handle_function_call(tool_call.name, tool_call.args, task_id)
            messages.append(tool_result_message(result))
        api_call_count += 1
    else:
        return response.content
```

Messages follow OpenAI format: `{"role": "system/user/assistant/tool", ...}`.
Reasoning content is stored in `assistant_msg["reasoning"]`.

---

## CLI Architecture (cli.py)

- **Rich** for banner/panels, **prompt_toolkit** for input with autocomplete
- **KawaiiSpinner** (`agent/display.py`) — animated faces during API calls, `┊` activity feed for tool results
- `load_cli_config()` in cli.py merges hardcoded defaults + user config YAML
- **Skin engine** (`hermes_cli/skin_engine.py`) — data-driven CLI theming; initialized from `display.skin` config key at startup; skins customize banner colors, spinner faces/verbs/wings, tool prefix, response box, branding text
- `process_command()` is a method on `HermesCLI` — dispatches on canonical command name resolved via `resolve_command()` from the central registry
- Skill slash commands: `agent/skill_commands.py` scans `~/.hermes/skills/`, injects as **user message** (not system prompt) to preserve prompt caching

### Slash Command Registry (`hermes_cli/commands.py`)

All slash commands are defined in a central `COMMAND_REGISTRY` list of `CommandDef` objects. Every downstream consumer derives from this registry automatically:

- **CLI** — `process_command()` resolves aliases via `resolve_command()`, dispatches on canonical name
- **Gateway** — `GATEWAY_KNOWN_COMMANDS` frozenset for hook emission, `resolve_command()` f

[...truncated AGENTS.md: 45,468 chars total]


Explanation:
**Checking finance repo issues**

I’m wondering if the current working directory (cwd) is wrong. It's possible the finance repository isn’t a git repository. I think I should search for more context around it to clarify what’s happening. If it’s not in git, that would definitely explain some issues I'm seeing. I’ll take a closer look to ensure everything is set up correctly. Let's figure this out together!


pwd; git -C /home/agent status --short 2>/dev/null || true; git -C /home/agent/.hermes/hermes-agent status --short | head


Explanation:
**Searching for FinanceManager repo**

I need to locate the FinanceManager repository, but using `search_files` for .git directories seems tricky. I'm wondering if using the terminal to find it might be a better approach. I could search the files for something like `manage.py` to narrow it down. There’s definitely a problem if it’s not straightforward, and I want to make sure I'm efficiently finding what I need. Let's explore this further!


python3 - <<'PY'
import os
for root,dirs,files in os.walk('/home/agent'):
    if '.git' in dirs:
        print(root)
        dirs.remove('.git')
    # prune big hidden? no
PY


Output:
/home/agent/Documents/Obsidian Vault
/home/agent/.npm-global/lib/node_modules/@tobilu/qmd/node_modules/node-llama-cpp/llama/llama.cpp
/home/agent/.openclaw/workspace
/home/agent/.openclaw/workspace/skills/self-improving-agent-v2
/home/agent/.openclaw/workspace/skills/tavily-skills
/home/agent/.openclaw/workspace/skills/openclaw-skills
/home/agent/.openclaw/workspace/skills/self-improving-agent
/home/agent/.hermes/repos/FinanceManager
/home/agent/.hermes/repos/HealthManager
/home/agent/.hermes/hermes-agent
/home/agent/.hermes/skills/openclaw-imports/self-improving-agent-v2
/home/agent/.hermes/skills/openclaw-imports/self-improving-agent


git status --short && git log --oneline -5


Output:
3aa67d8 feat: add crypto price refresh command
1131cde feat: add equity ETF management UI
fc67bb3 feat: add audited crypto management dashboard
a3f4bf5 feat: add crypto inventory report export
c5ce71b feat: add streamlit dashboard MVP


Explanation:
**Analyzing commit history**

I see that there's a commit 1131 which exists after 3aa. The user mentioned something about not beginning, but the repo seems to have it from a previous stage. Should I consider resetting? The commit log indicates that the equity UI already exists, but the user accepted 3aa and then 1131. The sequence shows that 3aa is the latest and 1131 is earlier. I’m wondering if the user’s "not begun" statement is inconsistent. I may need to read the files to clarify.


     1|from __future__ import annotations
     2|
     3|import argparse
     4|from pathlib import Path
     5|
     6|from jarvis_finance.config.settings import ensure_runtime_dirs, load_settings
     7|from jarvis_finance.market.providers import CoinGeckoClient, refresh_crypto_prices
     8|from jarvis_finance.quality.git_safety import assert_safe, scan_path
     9|from jarvis_finance.storage.database import connect
    10|from jarvis_finance.storage.migrations import apply_migrations, get_schema_version
    11|
    12|
    13|def main(argv: list[str] | None = None) -> int:
    14|    parser = argparse.ArgumentParser(prog="finance")
    15|    sub = parser.add_subparsers(dest="command", required=True)
    16|    sub.add_parser("init-db")
    17|    sub.add_parser("migrate")
    18|    scan = sub.add_parser("git-safety-scan")
    19|    scan.add_argument("path", nargs="?", default=".")
    20|    prices = sub.add_parser("update-crypto-prices")
    21|    prices.add_argument("--currency", default="CHF")
    22|    prices.add_argument("--max-age-seconds", type=int, default=3600)
    23|    args = parser.parse_args(argv)
    24|
    25|    if args.command in {"init-db", "migrate"}:
    26|        settings = load_settings()
    27|        ensure_runtime_dirs(settings)
    28|        conn = connect(settings.db_path)
    29|        apply_migrations(conn)
    30|        print(f"SCHEMA_VERSION={get_schema_version(conn)}")
    31|        return 0
    32|    if args.command == "git-safety-scan":
    33|        findings = scan_path(Path(args.path))
    34|        if findings:
    35|            for finding in findings:
    36|                print(f"{finding.path}: {finding.reason}")
    37|            return 1
    38|        assert_safe(Path(args.path))
    39|        print("GIT_SAFETY_OK")
    40|        return 0
    41|    if args.command == "update-crypto-prices":
    42|        settings = load_settings()
    43|        ensure_runtime_dirs(settings)
    44|        conn = connect(settings.db_path)
    45|        try:
    46|            apply_migrations(conn)
    47|            result = refresh_crypto_prices(
    48|                conn,
    49|                provider=CoinGeckoClient(),
    50|                currency=args.currency,
    51|                max_age_seconds=args.max_age_seconds,
    52|            )
    53|            print(
    54|                "CRYPTO_PRICE_UPDATE "
    55|                f"currency={result.currency} total={result.total_assets} success={result.success_count} "
    56|                f"cached={result.cached_count} skipped={result.skipped_count} "
    57|                f"warnings={result.warning_count} errors={result.error_count}"
    58|            )
    59|            return 1 if result.error_count else 0
    60|        finally:
    61|            conn.close()
    62|    return 2
    63|
    64|
    65|if __name__ == "__main__":
    66|    raise SystemExit(main())
    67|


     1|from __future__ import annotations
     2|
     3|import json
     4|import time
     5|import urllib.error
     6|import urllib.parse
     7|import urllib.request
     8|from dataclasses import dataclass, field
     9|from datetime import datetime, timezone
    10|from decimal import Decimal, InvalidOperation
    11|from sqlite3 import Connection
    12|from typing import Protocol
    13|
    14|from jarvis_finance.imports.common import stable_id, utc_now
    15|from jarvis_finance.quality.alerts import create_alert
    16|
    17|
    18|@dataclass(frozen=True)
    19|class PriceQuote:
    20|    coingecko_id: str
    21|    currency: str
    22|    price: Decimal | None
    23|    provider: str = "CoinGecko"
    24|    provider_timestamp: str | None = None
    25|    quality_status: str = "fresh"
    26|    error_message: str | None = None
    27|
    28|
    29|@dataclass
    30|class PriceRefreshResult:
    31|    currency: str
    32|    total_assets: int = 0
    33|    success_count: int = 0
    34|    skipped_count: int = 0
    35|    cached_count: int = 0
    36|    warning_count: int = 0
    37|    error_count: int = 0
    38|    written_price_ids: list[str] = field(default_factory=list)
    39|    warnings: list[str] = field(default_factory=list)
    40|    errors: list[str] = field(default_factory=list)
    41|
    42|
    43|class MarketDataProvider(Protocol):
    44|    def get_crypto_price(self, coingecko_id: str, currency: str = "CHF") -> PriceQuote: ...
    45|
    46|
    47|class ProviderRateLimitError(RuntimeError):
    48|    pass
    49|
    50|
    51|class ProviderFetchError(RuntimeError):
    52|    pass
    53|
    54|
    55|class CoinGeckoClient:
    56|    def __init__(self, *, base_url: str = "https://api.coingecko.com/api/v3", max_retries: int = 2, initial_backoff_seconds: float = 1.0, max_backoff_seconds: float = 8.0, opener=None, sleeper=time.sleep) -> None:
    57|        self.base_url = base_url.rstrip("/")
    58|        self.max_retries = max_retries
    59|        self.initial_backoff_seconds = initial_backoff_seconds
    60|        self.max_backoff_seconds = max_backoff_seconds
    61|        self.opener = opener or urllib.request.urlopen
    62|        self.sleeper = sleeper
    63|
    64|    def get_crypto_price(self, coingecko_id: str, currency: str = "CHF") -> PriceQuote:
    65|        cur = currency.lower()
    66|        query = urllib.parse.urlencode({"ids": coingecko_id, "vs_currencies": cur, "include_last_updated_at": "true"})
    67|        url = f"{self.base_url}/simple/price?{query}"
    68|        delay = self.initial_backoff_seconds
    69|        last_error: str | None = None
    70|        for attempt in range(self.max_retries + 1):
    71|            try:
    72|                with self.opener(url, timeout=20) as resp:
    73|                    payload = json.loads(resp.read().decode("utf-8"))
    74|                data = payload.get(coingecko_id) or {}
    75|                value = data.get(cur)
    76|                if value is None:
    77|                    return PriceQuote(coingecko_id, currency.upper(), None, quality_status="missing", error_message="price missing")
    78|                try:
    79|                    price = Decimal(str(value))
    80|                except InvalidOperation:
    81|                    return PriceQuote(coingecko_id, currency.upper(), None, quality_status="error", error_message="invalid price")
    82|                ts = data.get("last_updated_at")
    83|                provider_ts = None
    84|                if ts:
    85|                    try:
    86|                        provider_ts = datetime.fromtimestamp(int(ts), tz=timezone.utc).isoformat()
    87|                    except (TypeError, ValueError):
    88|                        provider_ts = str(ts)
    89|                return PriceQuote(coingecko_id, currency.upper(), price, provider_timestamp=provider_ts)
    90|            except urllib.error.HTTPError as exc:
    91|                last_error = f"HTTP {exc.code}"
    92|                if exc.code == 429 and attempt < self.max_retries:
    93|                    self.sleeper(min(delay, self.max_backoff_seconds)); delay *= 2; continue
    94|                status = "stale" if exc.code == 429 else "error"
    95|                return PriceQuote(coingecko_id, currency.upper(), None, quality_status=status, error_message=last_error)
    96|            except Exception as exc:  # network skeleton must not crash dashboard jobs
    97|                last_error = str(exc)
    98|                if attempt < self.max_retries:
    99|                    self.sleeper(min(delay, self.max_backoff_seconds)); delay *= 2; continue
   100|                return PriceQuote(coingecko_id, currency.upper(), None, quality_status="error", error_message=last_error)
   101|        return PriceQuote(coingecko_id, currency.upper(), None, quality_status="error", error_message=last_error)
   102|
   103|
   104|def _parse_dt(value: str | None) -> datetime | None:
   105|    if not value:
   106|        return None
   107|    try:
   108|        dt = datetime.fromisoformat(value.replace("Z", "+00:00"))
   109|    except ValueError:
   110|        return None
   111|    return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)
   112|
   113|
   114|def is_price_stale(timestamp: str | None, *, max_age_seconds: int = 86_400) -> bool:
   115|    dt = _parse_dt(timestamp)
   116|    if dt is None:
   117|        return True
   118|    return (datetime.now(timezone.utc) - dt).total_seconds() > max_age_seconds
   119|
   120|
   121|def latest_crypto_price_row(conn: Connection, asset_id: str, currency: str = "CHF"):
   122|    return conn.execute(
   123|        """
   124|        SELECT * FROM crypto_prices
   125|        WHERE asset_id=? AND price_currency=?
   126|        ORDER BY COALESCE(provider_timestamp, fetched_at, '') DESC, fetched_at DESC
   127|        LIMIT 1
   128|        """,
   129|        (asset_id, currency.upper()),
   130|    ).fetchone()
   131|
   132|
   133|def store_crypto_price(conn: Connection, *, asset_id: str, quote: PriceQuote) -> str:
   134|    now = utc_now()
   135|    price_id = stable_id("cryptoprice", asset_id, quote.coingecko_id, quote.currency, quote.provider, now)
   136|    conn.execute(
   137|        """
   138|        INSERT INTO crypto_prices(crypto_price_id, asset_id, coingecko_id, price_currency, price, provider, provider_timestamp, fetched_at, quality_status, error_message)
   139|        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
   140|        """,
   141|        (price_id, asset_id, quote.coingecko_id, quote.currency.upper(), format(quote.price, "f") if quote.price is not None else "", quote.provider, quote.provider_timestamp, now, quote.quality_status, quote.error_message),
   142|    )
   143|    if quote.quality_status in {"missing", "stale", "error", "conflict"}:
   144|        create_alert(conn, priority="warnung", category="market_data", entity_type="crypto_asset", entity_id=asset_id, rule_id=f"crypto_price_{quote.quality_status}", message=f"Crypto price quality is {quote.quality_status}.", evidence={"coingecko_id": quote.coingecko_id, "currency": quote.currency, "error": quote.error_message})
   145|    conn.commit()
   146|    return price_id
   147|
   148|
   149|def refresh_crypto_prices(conn: Connection, *, provider: MarketDataProvider, currency: str = "CHF", max_age_seconds: int = 3600) -> PriceRefreshResult:
   150|    currency = currency.upper()
   151|    result = PriceRefreshResult(currency=currency)
   152|    assets = conn.execute("SELECT asset_id, coingecko_id, symbol FROM crypto_assets WHERE is_active=1 ORDER BY symbol").fetchall()
   153|    result.total_assets = len(assets)
   154|    for asset in assets:
   155|        asset_id = asset["asset_id"]
   156|        if not asset["coingecko_id"]:
   157|            create_alert(conn, priority="warnung", category="crypto", entity_type="crypto_asset", entity_id=asset_id, rule_id="missing_coingecko_id", message="Crypto asset has no CoinGecko ID; price refresh skipped.", evidence={"symbol": asset["symbol"]}, fingerprint="missing_coingecko_id")
   158|            result.skipped_count += 1
   159|            result.warning_count += 1
   160|            result.warnings.append(f"{asset['symbol']}: missing_coingecko_id")
   161|            continue
   162|        latest = latest_crypto_price_row(conn, asset_id, currency)
   163|        if latest and latest["quality_status"] == "fresh" and not is_price_stale(latest["fetched_at"], max_age_seconds=max_age_seconds):
   164|            result.cached_count += 1
   165|            continue
   166|        try:
   167|            quote = provider.get_crypto_price(asset["coingecko_id"], currency)
   168|        except Exception as exc:
   169|            quote = PriceQuote(asset["coingecko_id"], currency, None, quality_status="error", error_message=str(exc))
   170|        price_id = store_crypto_price(conn, asset_id=asset_id, quote=quote)
   171|        result.written_price_ids.append(price_id)
   172|        if quote.price is not None and quote.quality_status == "fresh":
   173|            result.success_count += 1
   174|        elif quote.quality_status in {"missing", "stale"}:
   175|            result.warning_count += 1
   176|            result.warnings.append(f"{asset['symbol']}: {quote.quality_status}")
   177|        else:
   178|            result.error_count += 1
   179|            result.errors.append(f"{asset['symbol']}: {quote.error_message or quote.quality_status}")
   180|    conn.commit()
   181|    return result
   182|


     1|from __future__ import annotations
     2|
     3|import urllib.error
     4|from datetime import datetime, timedelta, timezone
     5|from decimal import Decimal
     6|
     7|from jarvis_finance.cli.main import main
     8|from jarvis_finance.crypto.assets import create_crypto_asset
     9|from jarvis_finance.crypto.holdings import create_initial_holding_snapshot
    10|from jarvis_finance.crypto.wallets import create_wallet
    11|from jarvis_finance.dashboard import data
    12|from jarvis_finance.market.providers import CoinGeckoClient, PriceQuote, refresh_crypto_prices
    13|from jarvis_finance.reports.crypto_inventory import build_crypto_report_context
    14|from jarvis_finance.storage.database import connect_memory
    15|from jarvis_finance.storage.migrations import apply_migrations
    16|
    17|
    18|class MockProvider:
    19|    def __init__(self) -> None:
    20|        self.calls: list[tuple[str, str]] = []
    21|
    22|    def get_crypto_price(self, coingecko_id: str, currency: str = "CHF") -> PriceQuote:
    23|        self.calls.append((coingecko_id, currency))
    24|        return PriceQuote(coingecko_id=coingecko_id, currency=currency, price=Decimal("123.456789"), provider="MockGecko", provider_timestamp="2026-01-31T00:00:00+00:00")
    25|
    26|
    27|def setup_conn():
    28|    conn = connect_memory()
    29|    apply_migrations(conn)
    30|    wallet_id = create_wallet(conn, wallet_name="Synthetic Wallet", wallet_type="Hardware Wallet", platform_provider="Synthetic", last_verified_at="2026-01-31T00:00:00+00:00")
    31|    btc_id = create_crypto_asset(conn, coin_name="Bitcoin", symbol="BTC", coingecko_id="bitcoin")
    32|    create_initial_holding_snapshot(conn, asset_id=btc_id, wallet_id=wallet_id, quantity=Decimal("0.01"), verification_status="verified", note="synthetic fixture", last_verified_at="2026-01-31T00:00:00+00:00")
    33|    missing_id = create_crypto_asset(conn, coin_name="Missing Coin", symbol="MISS", coingecko_id=None)
    34|    create_initial_holding_snapshot(conn, asset_id=missing_id, wallet_id=wallet_id, quantity=Decimal("1"), verification_status="verified", note="synthetic fixture", last_verified_at="2026-01-31T00:00:00+00:00")
    35|    return conn, btc_id, missing_id
    36|
    37|
    38|def test_refresh_crypto_prices_loads_active_assets_and_stores_chf_price() -> None:
    39|    conn, btc_id, _ = setup_conn()
    40|    provider = MockProvider()
    41|
    42|    result = refresh_crypto_prices(conn, provider=provider, currency="CHF", max_age_seconds=0)
    43|
    44|    assert result.success_count == 1
    45|    assert result.skipped_count == 1
    46|    assert result.warning_count == 1
    47|    assert result.error_count == 0
    48|    assert provider.calls == [("bitcoin", "CHF")]
    49|    row = conn.execute("SELECT * FROM crypto_prices WHERE asset_id=?", (btc_id,)).fetchone()
    50|    assert row is not None
    51|    assert row["price"] == "123.456789"
    52|    assert row["price_currency"] == "CHF"
    53|    assert row["provider"] == "MockGecko"
    54|
    55|
    56|def test_missing_coingecko_id_is_skipped_and_warned_without_duplicate_alerts() -> None:
    57|    conn, _, missing_id = setup_conn()
    58|    provider = MockProvider()
    59|
    60|    first = refresh_crypto_prices(conn, provider=provider, currency="CHF", max_age_seconds=0)
    61|    second = refresh_crypto_prices(conn, provider=provider, currency="CHF", max_age_seconds=0)
    62|
    63|    assert first.skipped_count == 1
    64|    assert second.skipped_count == 1
    65|    alerts = conn.execute("SELECT * FROM alerts WHERE entity_id=? AND rule_id='missing_coingecko_id'", (missing_id,)).fetchall()
    66|    assert len(alerts) == 1
    67|    assert alerts[0]["occurrence_count"] >= 2
    68|
    69|
    70|def test_429_backoff_is_used_by_provider() -> None:
    71|    calls = {"n": 0}
    72|    sleeps: list[float] = []
    73|
    74|    def opener(url, timeout=20):
    75|        calls["n"] += 1
    76|        if calls["n"] == 1:
    77|            raise urllib.error.HTTPError(url, 429, "rate limited", None, None)
    78|        class Resp:
    79|            def __enter__(self): return self
    80|            def __exit__(self, *args): return False
    81|            def read(self): return b'{"bitcoin":{"chf":"12.34","last_updated_at":1700000000}}'
    82|        return Resp()
    83|
    84|    client = CoinGeckoClient(max_retries=2, initial_backoff_seconds=0.25, max_backoff_seconds=1, opener=opener, sleeper=sleeps.append)
    85|
    86|    quote = client.get_crypto_price("bitcoin", "CHF")
    87|
    88|    assert quote.price == Decimal("12.34")
    89|    assert sleeps == [0.25]
    90|    assert calls["n"] == 2
    91|
    92|
    93|def test_fresh_cache_prevents_refetch_and_stale_price_is_marked() -> None:
    94|    conn, btc_id, _ = setup_conn()
    95|    provider = MockProvider()
    96|    first = refresh_crypto_prices(conn, provider=provider, currency="CHF", max_age_seconds=3600)
    97|    second = refresh_crypto_prices(conn, provider=provider, currency="CHF", max_age_seconds=3600)
    98|    assert first.success_count == 1
    99|    assert second.cached_count == 1
   100|    assert provider.calls == [("bitcoin", "CHF")]
   101|
   102|    old = (datetime.now(timezone.utc) - timedelta(days=10)).isoformat()
   103|    conn.execute("UPDATE crypto_prices SET fetched_at=?, provider_timestamp=? WHERE asset_id=?", (old, old, btc_id))
   104|    conn.commit()
   105|
   106|    overview = data.get_crypto_overview(conn, price_max_age_seconds=3600)
   107|    btc = next(row for row in overview if row["symbol"] == "BTC")
   108|    assert btc["price_quality"] == "stale"
   109|    context = build_crypto_report_context(conn, price_max_age_seconds=3600)
   110|    assert any(w["code"] == "stale_price" and w["entity_id"] == btc_id for w in context["data_quality_warnings"])
   111|
   112|
   113|def test_dashboard_reads_local_prices_without_provider_api_call() -> None:
   114|    conn, btc_id, _ = setup_conn()
   115|    refresh_crypto_prices(conn, provider=MockProvider(), currency="CHF", max_age_seconds=0)
   116|
   117|    overview = data.get_crypto_overview(conn)
   118|    by_wallet = data.get_crypto_by_wallet(conn)
   119|    summary = data.get_command_center_summary(conn)
   120|
   121|    btc = next(row for row in overview if row["symbol"] == "BTC")
   122|    assert btc["latest_price_chf"] == "123.46"
   123|    assert btc["market_value_chf"]
   124|    assert any(row["wallet_value_chf"] for row in by_wallet if row["symbol"] == "BTC")
   125|    assert summary["crypto_total_chf"] != "0.00"
   126|    assert summary["crypto_price_data_as_of"]
   127|
   128|
   129|def test_crypto_report_uses_local_prices_and_improves_missing_price_status() -> None:
   130|    conn, btc_id, _ = setup_conn()
   131|    before = build_crypto_report_context(conn)
   132|    assert any(w["code"] == "missing_price" and w["entity_id"] == btc_id for w in before["data_quality_warnings"])
   133|
   134|    refresh_crypto_prices(conn, provider=MockProvider(), currency="CHF", max_age_seconds=0)
   135|    after = build_crypto_report_context(conn)
   136|
   137|    btc = next(row for row in after["coins"] if row["symbol"] == "BTC")
   138|    assert btc["price_chf"] == "123.456789"
   139|    assert btc["value_chf"]
   140|    assert not any(w["code"] == "missing_price" and w["entity_id"] == btc_id for w in after["data_quality_warnings"])
   141|    assert after["metadata"]["live_api_calls"] is False
   142|
   143|
   144|def test_cli_update_crypto_prices_command_uses_provider_factory(monkeypatch, tmp_path, capsys) -> None:
   145|    runtime = tmp_path / "runtime"
   146|    monkeypatch.setenv("JARVIS_FINANCE_RUNTIME_DIR", str(runtime))
   147|    conn = connect_memory()
   148|    # Build a runtime file DB via CLI settings by seeding after opening path.
   149|    from jarvis_finance.storage.database import connect
   150|    db = runtime / "data" / "finance.sqlite3"
   151|    db.parent.mkdir(parents=True)
   152|    file_conn = connect(db)
   153|    apply_migrations(file_conn)
   154|    wallet_id = create_wallet(file_conn, wallet_name="CLI Wallet", wallet_type="Hardware Wallet", platform_provider="Synthetic", last_verified_at="2026-01-31T00:00:00+00:00")
   155|    asset_id = create_crypto_asset(file_conn, coin_name="Bitcoin", symbol="BTC", coingecko_id="bitcoin")
   156|    create_initial_holding_snapshot(file_conn, asset_id=asset_id, wallet_id=wallet_id, quantity=Decimal("0.01"), verification_status="verified", note="synthetic fixture", last_verified_at="2026-01-31T00:00:00+00:00")
   157|    file_conn.close()
   158|
   159|    monkeypatch.setattr("jarvis_finance.cli.main.CoinGeckoClient", lambda: MockProvider())
   160|
   161|    rc = main(["update-crypto-prices", "--max-age-seconds", "0"])
   162|
   163|    out = capsys.readouterr().out
   164|    assert rc == 0
   165|    assert "CRYPTO_PRICE_UPDATE" in out
   166|    assert "success=1" in out
   167|    conn.close()
   168|


Explanation:
**Implementing batch alerts**

I’m considering the need to implement batch processing for alerts. I wonder if the deduplication for alerts is functioning correctly. It's crucial to ensure that alerts are not duplicated, as that could become quite chaotic. I guess I should read through the existing alerts to see if everything is working as expected. I'm trying to prioritize getting this right, as clear alerts are essential for effective responses.