Commit Graph

297 Commits

Author SHA1 Message Date
Ryan Chen 9249f92db8 Fix YNAB mirror review findings
- Full resyncs mark mirror rows missing from the download as deleted;
  YNAB omits deleted entities from non-delta responses, so deletions
  made just before a full sync never reached the mirror.
- YNAB tools return errors as text instead of raising, since ToolNode
  re-raises and would fail the whole chat turn. ynab_sql catches all
  sqlglot errors (e.g. an unbalanced quote) and runs the SQL regenerated
  from the validated tree, which also drops comments that broke the
  LIMIT wrapper. Results keep duplicate column names.
- Sync fetches from YNAB before opening the DB transaction, discards the
  payload if the other container synced meanwhile, re-checks freshness
  under the lock so parallel tool calls share one sync, and backs off
  for 60s after a failure.
- The mirror is pinned to one budget; a changed YNAB_BUDGET_ID errors
  instead of mixing budgets, and an unset id stays on the budget the
  first sync resolved from "last-used".
- Change log: no entries for empty month rollover rows, and id fields
  are logged as names (category, payee, account). ynab_recent_changes
  accepts the names it prints ("assignment") and plurals.
- Outputs sized to the 4000-char tool result cap; ynab_sql reuses the
  shared admin message.
- YNAB API client moves back to utils/ynab_service.py, and the frontend
  tool status labels list the new tools.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:58:14 -04:00
Ryan Chen 8f02318542 Mirror YNAB budget into Postgres with delta sync and change log
YNAB has no event feed, so keep a local copy in ynab_* tables synced via
server_knowledge delta requests: full download on first run, deltas every
YNAB_SYNC_INTERVAL seconds and on demand before each tool call, full
resync daily. Each sync diffs against the mirror and appends real edits
(transactions, assignments, renames) to ynab_changes, the edit history
YNAB doesn't keep.

Agent YNAB tools now answer from the mirror: budget summary (spending
computed from transactions, since YNAB's month activity nets in credit
card payment categories), account balances, transaction search with
splits expanded, spending reports by category/payee/account/month,
category budget, and recent changes. Adds an admin-only read-only
ynab_sql tool guarded by a sqlglot allowlist (scope-resolved tables, no
dangerous functions), a READ ONLY transaction, a timeout, and a row cap.

Replaces utils/ynab_service.py and drops the unused ynab SDK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:32:38 -04:00
ryan 3e2a237eb6 Merge pull request 'Expose Homebox set_item edit tool to admin agent' (#54) from feat/homebox-set-item into main 2026-10-12 17:02:27 -04:00
Ryan Chen b1e210de5d Expose Homebox set_item edit tool to admin agent
Allowlist homebox-mcp's general item editor so the agent can rename items
and edit description, notes, quantity, purchase info, flags and custom
fields. Only passed args change; the rest of the item is preserved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:02:17 -04:00
ryan cdd067fb8e Merge pull request 'Add admin-only BookStack MCP connector' (#53) from feat/bookstack-mcp into main 2026-10-12 16:54:35 -04:00
Ryan Chen 0b8fe7b2df Add admin-only BookStack MCP connector
Load an allowlisted subset of bookstack-mcp tools (search/read plus
create/update books, chapters, pages and comments; no deletes) over stdio
and add them to the admin agent alongside Homebox. Extract the shared
allowlist + is_admin wrapping into utils/mcp_tools.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 16:54:01 -04:00
ryan e03c10cde5 Merge pull request 'Add admin-only Homebox MCP connector' (#52) from feat/homebox-mcp into main
Reviewed-on: #52
2026-10-12 16:45:32 -04:00
Ryan Chen 9858e06b73 Add admin-only Homebox MCP connector
Load an allowlisted subset of homebox-mcp tools (read + safe writes, no
deletes) over stdio and compile them into a separate admin agent, so
non-admins never see them. Each tool also re-checks is_admin per call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 11:01:19 -04:00
Ryan Chen ccc5126ae0 Sample least-recently-fetched friends instead of random
Replaces random.sample with a staleness-ordered pick: friends never
fetched sort first (epoch timestamp), then oldest fetched_at. Ensures
full following list coverage over successive nightly runs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-18 07:15:35 -04:00
Ryan Chen 4d29bb3ca4 Pin llama-server reasoning_effort to low to fix Jinja template error
LangChain was sending reasoning_effort=thinking which Qwen3's chat
template rejects (only xhigh/medium/low are valid). low disables
thinking mode, which is appropriate for agentic tool use and saves tokens.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-18 07:09:11 -04:00
Ryan Chen 64b20754a1 Trim all tool docstrings to reduce token count per request
Removed verbose "Use this tool when the user asks about" bullet lists
from all 31 tools. Each tool now has a 1-2 line description plus compact
arg docs. Saves ~3-5k tokens per request on a 32k context budget.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-18 07:04:26 -04:00
Ryan Chen 6fb9bade08 Trim conversation history to last 12 messages to prevent context overflow
The LangGraph checkpointer replays full history on every request. Without
trimming, long conversations compound past the 32k llama-server slot limit.
12 messages covers ~4-6 turns including tool call/result pairs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-18 06:56:12 -04:00
Ryan Chen 70475865cc Sample up to 3 top picks per friend for friend restaurant queries
Previously ordered by friend_beli_user_id first, returning 15 results
from whoever sorts first. Now fetches top-ranked rows globally and
caps at 3 per friend, giving a diverse cross-friend sample.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 22:49:34 -04:00
Ryan Chen 98b4b46b11 Add friends_only filter to beli_search_restaurants tool
Replaces include_friends with friends_only so the LLM can return
exclusively friend-sourced rows when user asks what their friends like,
rather than mixing in own rankings.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 22:44:28 -04:00
Ryan Chen 834c2c5bfe Drop beli result cap to 15 to stay under 32k context
30 still overflowed when combined with system prompt and conversation history.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 22:39:31 -04:00
Ryan Chen b7ac24f2ff Cap beli_search_restaurants results at 30 to prevent context overflow
50k+ token requests were blowing past the 32k llama-server context window
when returning the full ranked list.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 22:36:25 -04:00
Ryan Chen 69bf5edefe Add manual friend refresh endpoint and fix category filter guidance
POST /api/beli/profiles/<id>/refresh-friends triggers friend ranking
sampling on demand without waiting for the nightly cron. Also tightens
the beli_search_restaurants docstring so the LLM passes category=restaurant
when the user asks for restaurants, preventing bars/bakeries from appearing.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 22:29:52 -04:00
Ryan Chen 4c0d161f10 Fix Beli update_or_create ambiguity and friend sampling scope
- Add friend_beli_user_id="" to own ranking lookups to prevent
  MultipleObjectsReturned when friend rows exist for same restaurant
- Move friend sampling out of refresh_profile into _refresh_after_delay
  so manual refreshes stay fast; nightly cron still samples friends

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 22:20:08 -04:00
Ryan Chen c24a44618f Demote friend ranking 405s to debug — private profiles are expected
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 22:13:02 -04:00
Ryan Chen c183a13db2 Add migration for beli_category column
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 22:09:46 -04:00
Ryan Chen c682d2b320 Add beli_category (RES/DES/BAR/BAK) to restaurant records
Store Beli's category code on each ranking so the agent can filter by type
(restaurant/bar/cafe/bakery). Category label shown in tool output.

Requires migration for beli_category column + updated unique constraint.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 22:09:09 -04:00
Ryan Chen aa61ec54b9 Add migration for Beli friend ranking columns
Adds friend_beli_user_id (default '') and friend_name to beli_restaurants,
updates unique constraint to include friend_beli_user_id.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 20:55:10 -04:00
Ryan Chen b79229a5b0 Add random friend ranking refresh to Beli integration
Each nightly profile refresh now also picks FRIEND_SAMPLE_SIZE (5) random
followed users and fetches their rankings, building up a picture of friends'
lists over time. Rows are tagged with friend_beli_user_id/friend_name so the
agent tool can surface attribution ("via Alex").

Requires migration to add friend_beli_user_id/friend_name columns and update
unique_together on beli_restaurants.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 20:52:14 -04:00
Ryan Chen 7d1a89475a Fix Beli API client against actual spec
- Category codes: RES/DES/BAR/BAK (was full English words)
- score field: use 'value' from Score model (was 'score', which doesn't exist)
- rank_index: pass array position (no rank field in response)
- fetch_bookmarks: no query params (user param caused 405)
- notes: correctly None (stored separately, not in ranking results)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 20:23:52 -04:00
Ryan Chen 69b8697389 Fix Beli category codes: use RES/DES/BAR/BAK not full words
The API uses 3-letter codes, not full English words. Remove debug logging.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 20:21:14 -04:00
Ryan Chen 0367dac3cd Log full JWT claims on auth and refresh to find correct user UUID
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 20:15:04 -04:00
Ryan Chen 828fc7d16a Log full Beli ranking response for debugging
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 20:12:25 -04:00
Ryan Chen fcadec8bf6 Log raw response keys from Beli ranking endpoint
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 20:09:56 -04:00
Ryan Chen 2674a49574 Add debug logging to Beli refresh to inspect API response shape
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 20:06:13 -04:00
Ryan Chen 4c55168d52 Make Beli bookmark fetch non-fatal, pass user_id param
Bookmarks returning 400 was aborting the whole refresh and discarding
successfully-fetched ranking data. Now bookmarks log a warning and
continue. Also pass user= to the bookmark endpoint in case it's required.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 20:02:13 -04:00
Ryan Chen 863ebe8e20 Fix EncryptedTextField crashing on plaintext values during model creation
Tortoise calls to_python_value() when setting field values at create() time,
not only when loading from DB. Add _is_fernet_token() to distinguish encrypted
DB values (≥73 decoded bytes, version 0x80) from plaintext being set — the
latter passes through untouched while wrong-key errors on real tokens still raise.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 19:54:20 -04:00
Ryan Chen b6b866c9fc manage_beli: use uv run and username instead of email
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 19:43:15 -04:00
Ryan Chen f709528def Add manage_beli.py management script
CLI to add/list/refresh/remove Beli profiles without a frontend.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 19:42:05 -04:00
Ryan Chen d23273590f Add Beli restaurant recommendation integration
Multi-tenant system to fetch and store restaurant lists from Beli accounts.
Each user can connect up to 3 Beli profiles; a nightly Cronicle job (1–5 AM,
randomised) refreshes the 3 stalest profiles using the reverse-engineered Beli
API (mirrored at git.torrtle.co/ryan/beli-api). Rankings (been) and bookmarks
(want-to-try) are stored in Postgres and queryable via a new LangChain agent
tool. Beli passwords and refresh tokens are stored Fernet-encrypted.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 19:36:16 -04:00
Ryan Chen 53bbea42aa Use fresh UUID thread_id per agent run in scheduler and cron
Reusing the message/job ID as thread_id causes corrupted checkpoint state
when a previous run failed mid-tool-call. A fresh UUID each time guarantees
a clean conversation context.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 08:43:57 -04:00
Ryan Chen d806416f93 Round up scheduled_at to next minute for Cronicle one-shot timing
If scheduled_at has sub-minute seconds (e.g. retry = now+30s), the
Cronicle event would fire at the start of that minute before the message
is due and find nothing. Round up so it always fires after the due time.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 08:42:31 -04:00
Ryan Chen c6419f8931 Increase Cronicle trigger timeout to 5 min to allow LLM agent to complete
The shell script's Node.js socket timeout was 10s, too short for agent
runs with use_agent=true. Bump to 300s (5min) and set Cronicle job
timeout to 360s to match.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 08:38:58 -04:00
Ryan Chen 790a7059ce Per-message Cronicle events with recurrence-aware timing
Instead of an every-minute poller, each scheduled message gets its own
Cronicle event created at schedule time:
- One-time: year/month/day/hour/minute one-shot
- Daily/weekly/monthly: recurring timing matching the recurrence setting

Retry (PUT with new scheduled_at on failed messages) also creates a new
Cronicle event and resets status to pending. Recurring messages reuse
their existing Cronicle event across occurrences.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 08:32:22 -04:00
Ryan Chen 171410cf26 Fix agent invocation missing thread_id in scheduler and cron trigger
The checkpointer requires a thread_id in configurable. Use stable IDs
derived from the message/job ID so each has its own checkpoint thread.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 08:26:20 -04:00
Ryan Chen 504753eef9 Use Node.js script for Cronicle trigger to avoid shell dependency
Switches from curl (shell not available in Cronicle container) to a Node.js
inline script using the built-in http module. Tries raggr-green first, falls
back to raggr-blue automatically — no manual update needed on color swap.

Delete the existing broken Cronicle event before redeploying.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 08:21:09 -04:00
Ryan Chen 15a03dba91 Fix Cronicle event: use HTTP plugin (no shell needed) and every-minute timing
- Switch shellplug → urlplug to avoid spawn errors in shell-less containers
- Explicitly set timing to all 60 minutes (Cronicle {} defaults to hourly :00)
- Restore RAGGR_INTERNAL_URL env var for the HTTP plugin target URL

Delete the existing broken Cronicle event before redeploying so it gets recreated.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 08:19:04 -04:00
Ryan Chen c9c75b6be3 Fix Cronicle event creation: fetch real category ID instead of hardcoded c_general
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 08:13:15 -04:00
Ryan Chen 0e80e8b9df Try both raggr-green and raggr-blue in Cronicle scheduled messages script
Shell script falls back to the other color if the primary is down,
so the trigger works regardless of which container is currently active.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 08:10:44 -04:00
Ryan Chen 6e72d5f95a Auto-register scheduled messages event in Cronicle on startup
On startup raggr calls the Cronicle API (CRONICLE_URL + CRONICLE_API_KEY)
and creates a 'Scheduled Messages Trigger' shell event if it doesn't exist,
replacing the need to configure it manually in the Cronicle UI.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 08:09:27 -04:00
Ryan Chen 35b0e74a41 Migrate scheduled messages from internal poll loop to Cronicle trigger
Replaces the asyncio background loop (15s polling) with a POST
/api/scheduled-messages/trigger endpoint authenticated by X-Cron-Secret,
matching the existing cron jobs blueprint pattern.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 07:55:31 -04:00
Ryan Chen 2542fa2342 Fix: capture main_agent before SSE generator to avoid lost app context
Quart exits the request/app context before iterating the SSE response
body, so current_app inside event_generator() raised "Not within an app
context". Capture the reference before the generator is defined. Also
add return after the exception error yield to prevent falling through to
a second "No response generated" event on the same stream.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-17 07:43:52 -04:00
Ryan Chen e36bf35b2a Add ynab_category_allocations tool and fix category spending cap
Add get_category_allocations() to YNABService that fetches all categories
grouped by category group (using the categories API for group structure),
showing assigned and available amounts for every non-hidden category.

Add the corresponding ynab_category_allocations agent tool so the LLM
can answer questions about per-category budgets even when a category has
no spending activity this month.

Also remove the [:10] cap in ynab_category_spending so all spending
categories are returned — previously categories ranked below 10th by
activity (e.g. Coffee slush fund) were silently dropped.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-16 23:19:17 -04:00
Ryan Chen 19aa2b17d7 Fix _build_prompt receiving state dict instead of messages list
LangGraph passes the full agent state dict {"messages": [...]} to the
prompt callable, not just the messages list. Iterating a dict yields
its keys, so "messages" (the string) was getting appended to the LLM
input — causing the agent to respond as if the literal word "messages"
was the user's query.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-16 23:10:22 -04:00
Ryan Chen 43e0de24d6 Restore dynamic system prompt via RunnableLambda prompt parameter
Uses prompt=_build_prompt (a RunnableLambda) so LangGraph passes the
runtime config through, allowing per-request system_prompt injection.
Restores user memory loading for web and iMessage plain-text enforcement.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-16 23:04:43 -04:00
Ryan Chen 2a81ea8f76 Fix: migrate all remaining main_agent imports to current_app
email, scheduled_messages scheduler, and cron blueprints still imported
main_agent directly from agents. All now use current_app.main_agent so
they read the initialized value set by build_agent() in the lifespan.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-16 22:59:13 -04:00