mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-04-30 03:00:41 -04:00
Compare commits
6 Commits
fix/copilo
...
abhi/add-i
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35d27ece4a | ||
|
|
3e6be0c749 | ||
|
|
3969a1a29a | ||
|
|
e286fb6990 | ||
|
|
afe2257609 | ||
|
|
516097469e |
@@ -1,709 +0,0 @@
|
||||
---
|
||||
name: orchestrate
|
||||
description: "Meta-agent supervisor that manages a fleet of Claude Code agents running in tmux windows. Auto-discovers spare worktrees, spawns agents, monitors state, kicks idle agents, approves safe confirmations, and recycles worktrees when done. TRIGGER when user asks to supervise agents, run parallel tasks, manage worktrees, check agent status, or orchestrate parallel work."
|
||||
user-invocable: true
|
||||
argument-hint: "any free text — e.g. 'start 3 agents on X Y Z', 'show status', 'add task: implement feature A', 'stop', 'how many are free?'"
|
||||
metadata:
|
||||
author: autogpt-team
|
||||
version: "6.0.0"
|
||||
---
|
||||
|
||||
# Orchestrate — Agent Fleet Supervisor
|
||||
|
||||
One tmux session, N windows — each window is one agent working in its own worktree. Speak naturally; Claude maps your intent to the right scripts.
|
||||
|
||||
## Scripts
|
||||
|
||||
```bash
|
||||
SKILLS_DIR=$(git rev-parse --show-toplevel)/.claude/skills/orchestrate/scripts
|
||||
STATE_FILE=~/.claude/orchestrator-state.json
|
||||
```
|
||||
|
||||
| Script | Purpose |
|
||||
|---|---|
|
||||
| `find-spare.sh [REPO_ROOT]` | List free worktrees — one `PATH BRANCH` per line |
|
||||
| `spawn-agent.sh SESSION PATH SPARE NEW_BRANCH OBJECTIVE [PR_NUMBER] [STEPS...]` | Create window + checkout branch + launch claude + send task. **Stdout: `SESSION:WIN` only** |
|
||||
| `recycle-agent.sh WINDOW PATH SPARE_BRANCH` | Kill window + restore spare branch |
|
||||
| `run-loop.sh` | **Mechanical babysitter** — idle restart + dialog approval + recycle on ORCHESTRATOR:DONE + supervisor health check + all-done notification |
|
||||
| `verify-complete.sh WINDOW` | Verify PR is done: checkpoints ✓ + 0 unresolved threads + CI green + no fresh CHANGES_REQUESTED. Repo auto-derived from state file `.repo` or git remote. |
|
||||
| `notify.sh MESSAGE` | Send notification via Discord webhook (env `DISCORD_WEBHOOK_URL` or state `.discord_webhook`), macOS notification center, and stdout |
|
||||
| `capacity.sh [REPO_ROOT]` | Print available + in-use worktrees |
|
||||
| `status.sh` | Print fleet status + live pane commands |
|
||||
| `poll-cycle.sh` | One monitoring cycle — classifies panes, tracks checkpoints, returns JSON action array |
|
||||
| `classify-pane.sh WINDOW` | Classify one pane state |
|
||||
|
||||
## Supervision model
|
||||
|
||||
```
|
||||
Orchestrating Claude (this Claude session — IS the supervisor)
|
||||
└── Reads pane output, checks CI, intervenes with targeted guidance
|
||||
run-loop.sh (separate tmux window, every 30s)
|
||||
└── Mechanical only: idle restart, dialog approval, recycle on ORCHESTRATOR:DONE
|
||||
```
|
||||
|
||||
**You (the orchestrating Claude)** are the supervisor. After spawning agents, stay in this conversation and actively monitor: poll each agent's pane every 2-3 minutes, check CI, nudge stalled agents, and verify completions. Do not spawn a separate supervisor Claude window — it loses context, is hard to observe, and compounds context compression problems.
|
||||
|
||||
**run-loop.sh** is the mechanical layer — zero tokens, handles things that need no judgment: restart crashed agents, press Enter on dialogs, recycle completed worktrees (only after `verify-complete.sh` passes).
|
||||
|
||||
## Checkpoint protocol
|
||||
|
||||
Agents output checkpoints as they complete each required step:
|
||||
|
||||
```
|
||||
CHECKPOINT:<step-name>
|
||||
```
|
||||
|
||||
Required steps are passed as args to `spawn-agent.sh` (e.g. `pr-address pr-test`). `run-loop.sh` will not recycle a window until all required checkpoints are found in the pane output. If `verify-complete.sh` fails, the agent is re-briefed automatically.
|
||||
|
||||
## Worktree lifecycle
|
||||
|
||||
```text
|
||||
spare/N branch → spawn-agent.sh (--session-id UUID) → window + feat/branch + claude running
|
||||
↓
|
||||
CHECKPOINT:<step> (as steps complete)
|
||||
↓
|
||||
ORCHESTRATOR:DONE
|
||||
↓
|
||||
verify-complete.sh: checkpoints ✓ + 0 threads + CI green + no fresh CHANGES_REQUESTED
|
||||
↓
|
||||
state → "done", notify, window KEPT OPEN
|
||||
↓
|
||||
user/orchestrator explicitly requests recycle
|
||||
↓
|
||||
recycle-agent.sh → spare/N (free again)
|
||||
```
|
||||
|
||||
**Windows are never auto-killed.** The worktree stays on its branch, the session stays alive. The agent is done working but the window, git state, and Claude session are all preserved until you choose to recycle.
|
||||
|
||||
**To resume a done or crashed session:**
|
||||
```bash
|
||||
# Resume by stored session ID (preferred — exact session, full context)
|
||||
claude --resume SESSION_ID --permission-mode bypassPermissions
|
||||
|
||||
# Or resume most recent session in that worktree directory
|
||||
cd /path/to/worktree && claude --continue --permission-mode bypassPermissions
|
||||
```
|
||||
|
||||
**To manually recycle when ready:**
|
||||
```bash
|
||||
bash ~/.claude/orchestrator/scripts/recycle-agent.sh SESSION:WIN WORKTREE_PATH spare/N
|
||||
# Then update state:
|
||||
jq --arg w "SESSION:WIN" '.agents |= map(if .window == $w then .state = "recycled" else . end)' \
|
||||
~/.claude/orchestrator-state.json > /tmp/orch.tmp && mv /tmp/orch.tmp ~/.claude/orchestrator-state.json
|
||||
```
|
||||
|
||||
## State file (`~/.claude/orchestrator-state.json`)
|
||||
|
||||
Never committed to git. You maintain this file directly using `jq` + atomic writes (`.tmp` → `mv`).
|
||||
|
||||
```json
|
||||
{
|
||||
"active": true,
|
||||
"tmux_session": "autogpt1",
|
||||
"idle_threshold_seconds": 300,
|
||||
"loop_window": "autogpt1:5",
|
||||
"repo": "Significant-Gravitas/AutoGPT",
|
||||
"discord_webhook": "https://discord.com/api/webhooks/...",
|
||||
"last_poll_at": 0,
|
||||
"agents": [
|
||||
{
|
||||
"window": "autogpt1:3",
|
||||
"worktree": "AutoGPT6",
|
||||
"worktree_path": "/path/to/AutoGPT6",
|
||||
"spare_branch": "spare/6",
|
||||
"branch": "feat/my-feature",
|
||||
"objective": "Implement X and open a PR",
|
||||
"pr_number": "12345",
|
||||
"session_id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
"steps": ["pr-address", "pr-test"],
|
||||
"checkpoints": ["pr-address"],
|
||||
"state": "running",
|
||||
"last_output_hash": "",
|
||||
"last_seen_at": 0,
|
||||
"spawned_at": 0,
|
||||
"idle_since": 0,
|
||||
"revision_count": 0,
|
||||
"last_rebriefed_at": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Top-level optional fields:
|
||||
- `repo` — GitHub `owner/repo` for CI/thread checks. Auto-derived from git remote if omitted.
|
||||
- `discord_webhook` — Discord webhook URL for completion notifications. Also reads `DISCORD_WEBHOOK_URL` env var.
|
||||
|
||||
Per-agent fields:
|
||||
- `session_id` — UUID passed to `claude --session-id` at spawn; use with `claude --resume UUID` to restore exact session context after a crash or window close.
|
||||
- `last_rebriefed_at` — Unix timestamp of last re-brief; enforces 5-min cooldown to prevent spam.
|
||||
|
||||
Agent states: `running` | `idle` | `stuck` | `waiting_approval` | `complete` | `done` | `escalated`
|
||||
|
||||
`done` means verified complete — window is still open, session still alive, worktree still on task branch. Not recycled yet.
|
||||
|
||||
## Serial /pr-test rule
|
||||
|
||||
`/pr-test` and `/pr-test --fix` run local Docker + integration tests that use shared ports, a shared database, and shared build caches. **Running two `/pr-test` jobs simultaneously will cause port conflicts and database corruption.**
|
||||
|
||||
**Rule: only one `/pr-test` runs at a time. The orchestrator serializes them.**
|
||||
|
||||
You (the orchestrating Claude) own the test queue:
|
||||
1. Agents do `pr-review` and `pr-address` in parallel — that's safe (they only push code and reply to GitHub).
|
||||
2. When a PR needs local testing, add it to your mental queue — don't give agents a `pr-test` step.
|
||||
3. Run `/pr-test https://github.com/OWNER/REPO/pull/PR_NUMBER --fix` yourself, sequentially.
|
||||
4. Feed results back to the relevant agent via `tmux send-keys`:
|
||||
```bash
|
||||
tmux send-keys -t SESSION:WIN "Local tests for PR #N: <paste failure output or 'all passed'>. Fix any failures and push, then output ORCHESTRATOR:DONE."
|
||||
sleep 0.3
|
||||
tmux send-keys -t SESSION:WIN Enter
|
||||
```
|
||||
5. Wait for CI to confirm green before marking the agent done.
|
||||
|
||||
If multiple PRs need testing at the same time, pick the one furthest along (fewest pending CI checks) and test it first. Only start the next test after the previous one completes.
|
||||
|
||||
## Session restore (tested and confirmed)
|
||||
|
||||
Agent sessions are saved to disk. To restore a closed or crashed session:
|
||||
|
||||
```bash
|
||||
# If session_id is in state (preferred):
|
||||
NEW_WIN=$(tmux new-window -t SESSION -n WORKTREE_NAME -P -F '#{window_index}')
|
||||
tmux send-keys -t "SESSION:${NEW_WIN}" "cd /path/to/worktree && claude --resume SESSION_ID --permission-mode bypassPermissions" Enter
|
||||
|
||||
# If no session_id (use --continue for most recent session in that directory):
|
||||
tmux send-keys -t "SESSION:${NEW_WIN}" "cd /path/to/worktree && claude --continue --permission-mode bypassPermissions" Enter
|
||||
```
|
||||
|
||||
`--continue` restores the full conversation history including all tool calls, file edits, and context. The agent resumes exactly where it left off. After restoring, update the window address in the state file:
|
||||
|
||||
```bash
|
||||
jq --arg old "SESSION:OLD_WIN" --arg new "SESSION:NEW_WIN" \
|
||||
'(.agents[] | select(.window == $old)).window = $new' \
|
||||
~/.claude/orchestrator-state.json > /tmp/orch.tmp && mv /tmp/orch.tmp ~/.claude/orchestrator-state.json
|
||||
```
|
||||
|
||||
## Intent → action mapping
|
||||
|
||||
Match the user's message to one of these intents:
|
||||
|
||||
| The user says something like… | What to do |
|
||||
|---|---|
|
||||
| "status", "what's running", "show agents" | Run `status.sh` + `capacity.sh`, show output |
|
||||
| "how many free", "capacity", "available worktrees" | Run `capacity.sh`, show output |
|
||||
| "start N agents on X, Y, Z" or "run these tasks: …" | See **Spawning agents** below |
|
||||
| "add task: …", "add one more agent for …" | See **Adding an agent** below |
|
||||
| "stop", "shut down", "pause the fleet" | See **Stopping** below |
|
||||
| "poll", "check now", "run a cycle" | Run `poll-cycle.sh`, process actions |
|
||||
| "recycle window X", "free up autogpt3" | Run `recycle-agent.sh` directly |
|
||||
|
||||
When the intent is ambiguous, show capacity first and ask what tasks to run.
|
||||
|
||||
## Spawning agents
|
||||
|
||||
### 1. Resolve tmux session
|
||||
|
||||
```bash
|
||||
tmux list-sessions -F "#{session_name}: #{session_windows} windows" 2>/dev/null
|
||||
```
|
||||
|
||||
Use an existing session. **Never create a tmux session from within Claude** — it becomes a child of Claude's process and dies when the session ends. If no session exists, tell the user to run `tmux new-session -d -s autogpt1` in their terminal first, then re-invoke `/orchestrate`.
|
||||
|
||||
### 2. Show available capacity
|
||||
|
||||
```bash
|
||||
bash $SKILLS_DIR/capacity.sh $(git rev-parse --show-toplevel)
|
||||
```
|
||||
|
||||
### 3. Collect tasks from the user
|
||||
|
||||
For each task, gather:
|
||||
- **objective** — what to do (e.g. "implement feature X and open a PR")
|
||||
- **branch name** — e.g. `feat/my-feature` (derive from objective if not given)
|
||||
- **pr_number** — GitHub PR number if working on an existing PR (for verification)
|
||||
- **steps** — required checkpoint names in order (e.g. `pr-address pr-test`) — derive from objective
|
||||
|
||||
Ask for `idle_threshold_seconds` only if the user mentions it (default: 300).
|
||||
|
||||
Never ask the user to specify a worktree — auto-assign from `find-spare.sh`.
|
||||
|
||||
### 4. Spawn one agent per task
|
||||
|
||||
```bash
|
||||
# Get ordered list of spare worktrees
|
||||
SPARE_LIST=$(bash $SKILLS_DIR/find-spare.sh $(git rev-parse --show-toplevel))
|
||||
|
||||
# For each task, take the next spare line:
|
||||
WORKTREE_PATH=$(echo "$SPARE_LINE" | awk '{print $1}')
|
||||
SPARE_BRANCH=$(echo "$SPARE_LINE" | awk '{print $2}')
|
||||
|
||||
# With PR number and required steps:
|
||||
WINDOW=$(bash $SKILLS_DIR/spawn-agent.sh "$SESSION" "$WORKTREE_PATH" "$SPARE_BRANCH" "$NEW_BRANCH" "$OBJECTIVE" "$PR_NUMBER" "pr-address" "pr-test")
|
||||
|
||||
# Without PR (new work):
|
||||
WINDOW=$(bash $SKILLS_DIR/spawn-agent.sh "$SESSION" "$WORKTREE_PATH" "$SPARE_BRANCH" "$NEW_BRANCH" "$OBJECTIVE")
|
||||
```
|
||||
|
||||
Build an agent record and append it to the state file. If the state file doesn't exist yet, initialize it:
|
||||
|
||||
```bash
|
||||
# Derive repo from git remote (used by verify-complete.sh + supervisor)
|
||||
REPO=$(git remote get-url origin 2>/dev/null | sed 's|.*github\.com[:/]||; s|\.git$||' || echo "")
|
||||
|
||||
jq -n \
|
||||
--arg session "$SESSION" \
|
||||
--arg repo "$REPO" \
|
||||
--argjson threshold 300 \
|
||||
'{active:true, tmux_session:$session, idle_threshold_seconds:$threshold,
|
||||
repo:$repo, loop_window:null, supervisor_window:null, last_poll_at:0, agents:[]}' \
|
||||
> ~/.claude/orchestrator-state.json
|
||||
```
|
||||
|
||||
Optionally add a Discord webhook for completion notifications:
|
||||
```bash
|
||||
jq --arg hook "$DISCORD_WEBHOOK_URL" '.discord_webhook = $hook' ~/.claude/orchestrator-state.json \
|
||||
> /tmp/orch.tmp && mv /tmp/orch.tmp ~/.claude/orchestrator-state.json
|
||||
```
|
||||
|
||||
`spawn-agent.sh` writes the initial agent record (window, worktree_path, branch, objective, state, etc.) to the state file automatically — **do not append the record again after calling it.** The record already exists and `pr_number`/`steps` are patched in by the script itself.
|
||||
|
||||
### 5. Start the mechanical babysitter
|
||||
|
||||
```bash
|
||||
LOOP_WIN=$(tmux new-window -t "$SESSION" -n "orchestrator" -P -F '#{window_index}')
|
||||
LOOP_WINDOW="${SESSION}:${LOOP_WIN}"
|
||||
tmux send-keys -t "$LOOP_WINDOW" "bash $SKILLS_DIR/run-loop.sh" Enter
|
||||
|
||||
jq --arg w "$LOOP_WINDOW" '.loop_window = $w' ~/.claude/orchestrator-state.json \
|
||||
> /tmp/orch.tmp && mv /tmp/orch.tmp ~/.claude/orchestrator-state.json
|
||||
```
|
||||
|
||||
### 6. Begin supervising directly in this conversation
|
||||
|
||||
You are the supervisor. After spawning, immediately start your first poll loop (see **Supervisor duties** below) and continue every 2-3 minutes. Do NOT spawn a separate supervisor Claude window.
|
||||
|
||||
## Adding an agent
|
||||
|
||||
Find the next spare worktree, then spawn and append to state — same as steps 2–4 above but for a single task. If no spare worktrees are available, tell the user.
|
||||
|
||||
## Supervisor duties (YOUR job, every 2-3 min in this conversation)
|
||||
|
||||
You are the supervisor. Run this poll loop directly in your Claude session — not in a separate window.
|
||||
|
||||
### Poll loop mechanism
|
||||
|
||||
You are reactive — you only act when a tool completes or the user sends a message. To create a self-sustaining poll loop without user involvement:
|
||||
|
||||
1. Start each poll with `run_in_background: true` + a sleep before the work:
|
||||
```bash
|
||||
sleep 120 && tmux capture-pane -t autogpt1:0 -p -S -200 | tail -40
|
||||
# + similar for each active window
|
||||
```
|
||||
2. When the background job notifies you, read the pane output and take action.
|
||||
3. Immediately schedule the next background poll — this keeps the loop alive.
|
||||
4. Stop scheduling when all agents are done/escalated.
|
||||
|
||||
**Never tell the user "I'll poll every 2-3 minutes"** — that does nothing without a trigger. Start the background job instead.
|
||||
|
||||
### Each poll: what to check
|
||||
|
||||
```bash
|
||||
# 1. Read state
|
||||
cat ~/.claude/orchestrator-state.json | jq '.agents[] | {window, worktree, branch, state, pr_number, checkpoints}'
|
||||
|
||||
# 2. For each running/stuck/idle agent, capture pane
|
||||
tmux capture-pane -t SESSION:WIN -p -S -200 | tail -60
|
||||
```
|
||||
|
||||
For each agent, decide:
|
||||
|
||||
| What you see | Action |
|
||||
|---|---|
|
||||
| Spinner / tools running | Do nothing — agent is working |
|
||||
| Idle `❯` prompt, no `ORCHESTRATOR:DONE` | Stalled — send specific nudge with objective from state |
|
||||
| Stuck in error loop | Send targeted fix with exact error + solution |
|
||||
| Waiting for input / question | Answer and unblock via `tmux send-keys` |
|
||||
| CI red | `gh pr checks PR_NUMBER --repo REPO` → tell agent exactly what's failing |
|
||||
| GitHub abuse rate limit error | Nudge: "Wait 60 seconds then continue posting replies with sleep 3 between each" |
|
||||
| Context compacted / agent lost | Send recovery: `cat ~/.claude/orchestrator-state.json | jq '.agents[] | select(.window=="WIN")'` + `gh pr view PR_NUMBER --json title,body` |
|
||||
| `ORCHESTRATOR:DONE` in output | Query GraphQL for actual unresolved count. If >0, re-brief. If 0, run `verify-complete.sh` |
|
||||
|
||||
**Poll all windows from state, not from memory.** Before each poll, run:
|
||||
```bash
|
||||
jq -r '.agents[] | select(.state | test("running|idle|stuck|waiting_approval|pending_evaluation")) | .window' ~/.claude/orchestrator-state.json
|
||||
```
|
||||
and capture every window listed. If you manually added a window outside spawn-agent.sh, ensure it's in the state file first.
|
||||
|
||||
### RUNNING count includes waiting_approval agents
|
||||
|
||||
The `RUNNING` count from run-loop.sh includes agents in `waiting_approval` state (they match the regex `running|stuck|waiting_approval|idle`). This means a fleet that is only `waiting_approval` still shows RUNNING > 0 in the log — it does **not** mean agents are actively working.
|
||||
|
||||
When you see `RUNNING > 0` in the run-loop log but suspect agents are actually blocked, check state directly:
|
||||
```bash
|
||||
jq '.agents[] | {window, state, worktree}' ~/.claude/orchestrator-state.json
|
||||
```
|
||||
A count of `running=1 waiting=1` in the log actually means one agent is waiting for approval — the orchestrator should check and approve, not wait.
|
||||
|
||||
### State file staleness recovery
|
||||
|
||||
The state file is written by scripts but can drift from reality when windows are closed, sessions expire, or the orchestrator restarts across conversations.
|
||||
|
||||
**Signs of stale state:**
|
||||
- `loop_window` points to a window that no longer exists in the tmux session
|
||||
- An agent's `state` is `running` but tmux window is closed or shows a shell prompt (not claude)
|
||||
- `last_seen_at` is hours old but state still says `running`
|
||||
|
||||
**Recovery steps:**
|
||||
|
||||
1. **Verify actual tmux windows:**
|
||||
```bash
|
||||
tmux list-windows -t SESSION -F '#{window_index}: #{window_name} (#{pane_current_command})'
|
||||
```
|
||||
|
||||
2. **Cross-reference with state file:**
|
||||
```bash
|
||||
jq -r '.agents[] | "\(.window) \(.state) \(.worktree)"' ~/.claude/orchestrator-state.json
|
||||
```
|
||||
|
||||
3. **Fix stale entries:**
|
||||
```bash
|
||||
# Agent window closed — mark idle so run-loop.sh will restart it
|
||||
jq --arg w "SESSION:WIN" '(.agents[] | select(.window==$w)).state = "idle"' \
|
||||
~/.claude/orchestrator-state.json > /tmp/orch.tmp && mv /tmp/orch.tmp ~/.claude/orchestrator-state.json
|
||||
|
||||
# loop_window gone — kill the stale reference, then restart run-loop.sh
|
||||
jq '.loop_window = null' ~/.claude/orchestrator-state.json > /tmp/orch.tmp && mv /tmp/orch.tmp ~/.claude/orchestrator-state.json
|
||||
LOOP_WIN=$(tmux new-window -t "$SESSION" -n "orchestrator" -P -F '#{window_index}')
|
||||
LOOP_WINDOW="${SESSION}:${LOOP_WIN}"
|
||||
tmux send-keys -t "$LOOP_WINDOW" "bash $SKILLS_DIR/run-loop.sh" Enter
|
||||
jq --arg w "$LOOP_WINDOW" '.loop_window = $w' ~/.claude/orchestrator-state.json \
|
||||
> /tmp/orch.tmp && mv /tmp/orch.tmp ~/.claude/orchestrator-state.json
|
||||
```
|
||||
|
||||
4. **After any state repair, re-run `status.sh` to confirm coherence before resuming supervision.**
|
||||
|
||||
### Strict ORCHESTRATOR:DONE gate
|
||||
|
||||
`verify-complete.sh` handles the main checks automatically (checkpoints, threads, CI green, spawned_at, and CHANGES_REQUESTED). Run it:
|
||||
|
||||
**CHANGES_REQUESTED staleness rule**: a `CHANGES_REQUESTED` review only blocks if it was submitted *after* the latest commit. If the latest commit postdates the review, the review is considered stale (feedback already addressed) and does not block. This avoids false negatives when a bot reviewer hasn't re-reviewed after the agent's fixing commits.
|
||||
|
||||
```bash
|
||||
SKILLS_DIR=~/.claude/orchestrator/scripts
|
||||
bash $SKILLS_DIR/verify-complete.sh SESSION:WIN
|
||||
```
|
||||
|
||||
If it passes → run-loop.sh will recycle the window automatically. No manual action needed.
|
||||
If it fails → re-brief the agent with the failure reason. Never manually mark state `done` to bypass this.
|
||||
|
||||
### Re-brief a stalled agent
|
||||
|
||||
**Before sending any nudge, verify the pane is at an idle ❯ prompt.** Sending text into a still-processing pane produces stuck `[Pasted text +N lines]` that the agent never sees.
|
||||
|
||||
Check:
|
||||
```bash
|
||||
tmux capture-pane -t SESSION:WIN -p 2>/dev/null | tail -5
|
||||
```
|
||||
If the last line shows a spinner (✳✽✢✶·), `Running…`, or no `❯` — wait 10–15s and check again before sending.
|
||||
|
||||
```bash
|
||||
OBJ=$(jq -r --arg w SESSION:WIN '.agents[] | select(.window==$w) | .objective' ~/.claude/orchestrator-state.json)
|
||||
PR=$(jq -r --arg w SESSION:WIN '.agents[] | select(.window==$w) | .pr_number' ~/.claude/orchestrator-state.json)
|
||||
tmux send-keys -t SESSION:WIN "You appear stalled. Your objective: $OBJ. Check: gh pr view $PR --json title,body,headRefName to reorient."
|
||||
sleep 0.3
|
||||
tmux send-keys -t SESSION:WIN Enter
|
||||
```
|
||||
|
||||
If `image_path` is set on the agent record, include: "Re-read context at IMAGE_PATH with the Read tool."
|
||||
|
||||
## Self-recovery protocol (agents)
|
||||
|
||||
spawn-agent.sh automatically includes this instruction in every objective:
|
||||
|
||||
> If your context compacts and you lose track of what to do, run:
|
||||
> `cat ~/.claude/orchestrator-state.json | jq '.agents[] | select(.window=="SESSION:WIN")'`
|
||||
> and `gh pr view PR_NUMBER --json title,body,headRefName` to reorient.
|
||||
> Output each completed step as `CHECKPOINT:<step-name>` on its own line.
|
||||
|
||||
## Passing images and screenshots to agents
|
||||
|
||||
`tmux send-keys` is text-only — you cannot paste a raw image into a pane. To give an agent visual context (screenshots, diagrams, mockups):
|
||||
|
||||
1. **Save the image to a temp file** with a stable path:
|
||||
```bash
|
||||
# If the user drags in a screenshot or you receive a file path:
|
||||
IMAGE_PATH="/tmp/orchestrator-context-$(date +%s).png"
|
||||
cp "$USER_PROVIDED_PATH" "$IMAGE_PATH"
|
||||
```
|
||||
|
||||
2. **Reference the path in the objective string**:
|
||||
```bash
|
||||
OBJECTIVE="Implement the layout shown in /tmp/orchestrator-context-1234567890.png. Read that image first with the Read tool to understand the design."
|
||||
```
|
||||
|
||||
3. The agent uses its `Read` tool to view the image at startup — Claude Code agents are multimodal and can read image files directly.
|
||||
|
||||
**Rule**: always use `/tmp/orchestrator-context-<timestamp>.png` as the naming convention so the supervisor knows what to look for if it needs to re-brief an agent with the same image.
|
||||
|
||||
---
|
||||
|
||||
## Orchestrator final evaluation (YOU decide, not the script)
|
||||
|
||||
`verify-complete.sh` is a gate — it blocks premature marking. But it cannot tell you if the work is actually good. That is YOUR job.
|
||||
|
||||
When run-loop marks an agent `pending_evaluation` and you're notified, do all of these before marking done:
|
||||
|
||||
### 1. Run /pr-test (required, serialized, use TodoWrite to queue)
|
||||
|
||||
`/pr-test` is the only reliable confirmation that the objective is actually met. Run it yourself, not the agent.
|
||||
|
||||
**When multiple PRs reach `pending_evaluation` at the same time, use TodoWrite to queue them:**
|
||||
```
|
||||
- [ ] /pr-test https://github.com/Significant-Gravitas/AutoGPT/pull/NNNN — <feature description>
|
||||
- [ ] /pr-test https://github.com/Significant-Gravitas/AutoGPT/pull/MMMM — <feature description>
|
||||
```
|
||||
Run one at a time. Check off as you go.
|
||||
|
||||
```
|
||||
/pr-test https://github.com/Significant-Gravitas/AutoGPT/pull/PR_NUMBER
|
||||
```
|
||||
|
||||
**/pr-test can be lazy** — if it gives vague output, re-run with full context:
|
||||
|
||||
```
|
||||
/pr-test https://github.com/OWNER/REPO/pull/PR_NUMBER
|
||||
Context: This PR implements <objective from state file>. Key files: <list>.
|
||||
Please verify: <specific behaviors to check>.
|
||||
```
|
||||
|
||||
Only one `/pr-test` at a time — they share ports and DB.
|
||||
|
||||
### /pr-test result evaluation
|
||||
|
||||
**PARTIAL on any headline feature scenario is an immediate blocker.** Do not approve, do not mark done, do not let the agent output `ORCHESTRATOR:DONE`.
|
||||
|
||||
| `/pr-test` result | Action |
|
||||
|---|---|
|
||||
| All headline scenarios **PASS** | Proceed to evaluation step 2 |
|
||||
| Any headline scenario **PARTIAL** | Re-brief the agent immediately — see below |
|
||||
| Any headline scenario **FAIL** | Re-brief the agent immediately |
|
||||
|
||||
**What PARTIAL means**: the feature is only partly working. Example: the Apply button never appeared, or the AI returned no action blocks. The agent addressed part of the objective but not all of it.
|
||||
|
||||
**When any headline scenario is PARTIAL or FAIL:**
|
||||
|
||||
1. Do NOT mark the agent done or accept `ORCHESTRATOR:DONE`
|
||||
2. Re-brief the agent with the specific scenario that failed and what was missing:
|
||||
```bash
|
||||
tmux send-keys -t SESSION:WIN "PARTIAL result on /pr-test — S5 (Apply button) never appeared. The AI must output JSON action blocks for the Apply button to render. Fix this before re-running /pr-test."
|
||||
sleep 0.3
|
||||
tmux send-keys -t SESSION:WIN Enter
|
||||
```
|
||||
3. Set state back to `running`:
|
||||
```bash
|
||||
jq --arg w "SESSION:WIN" '(.agents[] | select(.window == $w)).state = "running"' \
|
||||
~/.claude/orchestrator-state.json > /tmp/orch.tmp && mv /tmp/orch.tmp ~/.claude/orchestrator-state.json
|
||||
```
|
||||
4. Wait for new `ORCHESTRATOR:DONE`, then re-run `/pr-test` from scratch
|
||||
|
||||
**Rule: only ALL-PASS qualifies for approval.** A mix of PASS + PARTIAL is a failure.
|
||||
|
||||
> **Why this matters**: A PR was once wrongly approved with S5 PARTIAL — the AI never output JSON action blocks so the Apply button never appeared. The fix was already in the agent's reach but slipped through because PARTIAL was not treated as blocking.
|
||||
|
||||
### 2. Do your own evaluation
|
||||
|
||||
1. **Read the PR diff and objective** — does the code actually implement what was asked? Is anything obviously missing or half-done?
|
||||
2. **Read the resolved threads** — were comments addressed with real fixes, or just dismissed/resolved without changes?
|
||||
3. **Check CI run names** — any suspicious retries that shouldn't have passed?
|
||||
4. **Check the PR description** — title, summary, test plan complete?
|
||||
|
||||
### 3. Decide
|
||||
|
||||
- `/pr-test` all scenarios PASS + evaluation looks good → mark `done` in state, tell the user the PR is ready, ask if window should be closed
|
||||
- `/pr-test` any scenario PARTIAL or FAIL → re-brief the agent with the specific failing scenario, set state back to `running` (see `/pr-test result evaluation` above)
|
||||
- Evaluation finds gaps even with all PASS → re-brief the agent with specific gaps, set state back to `running`
|
||||
|
||||
**Never mark done based purely on script output.** You hold the full objective context; the script does not.
|
||||
|
||||
```bash
|
||||
# Mark done after your positive evaluation:
|
||||
jq --arg w "SESSION:WIN" '(.agents[] | select(.window == $w)).state = "done"' \
|
||||
~/.claude/orchestrator-state.json > /tmp/orch.tmp && mv /tmp/orch.tmp ~/.claude/orchestrator-state.json
|
||||
```
|
||||
|
||||
## When to stop the fleet
|
||||
|
||||
Stop the fleet (`active = false`) when **all** of the following are true:
|
||||
|
||||
| Check | How to verify |
|
||||
|---|---|
|
||||
| All agents are `done` or `escalated` | `jq '[.agents[] | select(.state | test("running\|stuck\|idle\|waiting_approval"))] | length' ~/.claude/orchestrator-state.json` == 0 |
|
||||
| All PRs have 0 unresolved review threads | GraphQL `isResolved` check per PR |
|
||||
| All PRs have green CI **on a run triggered after the agent's last push** | `gh run list --branch BRANCH --limit 1` timestamp > `spawned_at` in state |
|
||||
| No fresh CHANGES_REQUESTED (after latest commit) | `verify-complete.sh` checks this — stale pre-commit reviews are ignored |
|
||||
| No agents are `escalated` without human review | If any are escalated, surface to user first |
|
||||
|
||||
**Do NOT stop just because agents output `ORCHESTRATOR:DONE`.** That is a signal to verify, not a signal to stop.
|
||||
|
||||
**Do stop** if the user explicitly says "stop", "shut down", or "kill everything", even with agents still running.
|
||||
|
||||
```bash
|
||||
# Graceful stop
|
||||
jq '.active = false' ~/.claude/orchestrator-state.json > /tmp/orch.tmp \
|
||||
&& mv /tmp/orch.tmp ~/.claude/orchestrator-state.json
|
||||
|
||||
LOOP_WINDOW=$(jq -r '.loop_window // ""' ~/.claude/orchestrator-state.json)
|
||||
[ -n "$LOOP_WINDOW" ] && tmux kill-window -t "$LOOP_WINDOW" 2>/dev/null || true
|
||||
```
|
||||
|
||||
Does **not** recycle running worktrees — agents may still be mid-task. Run `capacity.sh` to see what's still in progress.
|
||||
|
||||
## tmux send-keys pattern
|
||||
|
||||
**Always split long messages into text + Enter as two separate calls with a sleep between them.** If sent as one call (`"text" Enter`), Enter can fire before the full string is buffered into Claude's input — leaving the message stuck as `[Pasted text +N lines]` unsent.
|
||||
|
||||
```bash
|
||||
# CORRECT — text then Enter separately
|
||||
tmux send-keys -t "$WINDOW" "your long message here"
|
||||
sleep 0.3
|
||||
tmux send-keys -t "$WINDOW" Enter
|
||||
|
||||
# WRONG — Enter may fire before text is buffered
|
||||
tmux send-keys -t "$WINDOW" "your long message here" Enter
|
||||
```
|
||||
|
||||
Short single-character sends (`y`, `Down`, empty Enter for dialog approval) are safe to combine since they have no buffering lag.
|
||||
|
||||
---
|
||||
|
||||
## Protected worktrees
|
||||
|
||||
Some worktrees must **never** be used as spare worktrees for agent tasks because they host files critical to the orchestrator itself:
|
||||
|
||||
| Worktree | Protected branch | Why |
|
||||
|---|---|---|
|
||||
| `AutoGPT1` | `dx/orchestrate-skill` | Hosts the orchestrate skill scripts. `recycle-agent.sh` would check out `spare/1`, wiping `.claude/skills/` and breaking all subsequent `spawn-agent.sh` calls. |
|
||||
|
||||
**Rule**: when selecting spare worktrees via `find-spare.sh`, skip any worktree whose CURRENT branch matches a protected branch. If you accidentally spawn an agent in a protected worktree, do not let `recycle-agent.sh` run on it — manually restore the branch after the agent finishes.
|
||||
|
||||
When `dx/orchestrate-skill` is merged into `dev`, `AutoGPT1` becomes a normal spare again.
|
||||
|
||||
---
|
||||
|
||||
## Thread resolution integrity (critical)
|
||||
|
||||
**Agents MUST NOT resolve review threads via GraphQL unless a real code fix has been committed and pushed first.**
|
||||
|
||||
This is the most common failure mode: agents call `resolveReviewThread` to make unresolved counts drop without actually fixing anything. This produces a false "done" signal that gets past verify-complete.sh.
|
||||
|
||||
**The only valid resolution sequence:**
|
||||
1. Read the thread and understand what it's asking
|
||||
2. Make the actual code change
|
||||
3. `git commit` and `git push`
|
||||
4. Reply to the thread with the commit SHA (e.g. "Fixed in `abc1234`")
|
||||
5. THEN call `resolveReviewThread`
|
||||
|
||||
**The supervisor must verify actual thread counts via GraphQL** — never trust an agent's claim of "0 unresolved." After any agent's ORCHESTRATOR:DONE, always run:
|
||||
|
||||
```bash
|
||||
# Step 1: get total count
|
||||
TOTAL=$(gh api graphql -f query='{ repository(owner: "OWNER", name: "REPO") { pullRequest(number: PR) { reviewThreads { totalCount } } } }' \
|
||||
| jq '.data.repository.pullRequest.reviewThreads.totalCount')
|
||||
echo "Total threads: $TOTAL"
|
||||
|
||||
# Step 2: paginate all pages and count unresolved
|
||||
CURSOR=""; UNRESOLVED=0
|
||||
while true; do
|
||||
AFTER=${CURSOR:+", after: \"$CURSOR\""}
|
||||
PAGE=$(gh api graphql -f query="{ repository(owner: \"OWNER\", name: \"REPO\") { pullRequest(number: PR) { reviewThreads(first: 100${AFTER}) { pageInfo { hasNextPage endCursor } nodes { isResolved } } } } }")
|
||||
UNRESOLVED=$(( UNRESOLVED + $(echo "$PAGE" | jq '[.data.repository.pullRequest.reviewThreads.nodes[] | select(.isResolved==false)] | length') ))
|
||||
HAS_NEXT=$(echo "$PAGE" | jq -r '.data.repository.pullRequest.reviewThreads.pageInfo.hasNextPage')
|
||||
CURSOR=$(echo "$PAGE" | jq -r '.data.repository.pullRequest.reviewThreads.pageInfo.endCursor')
|
||||
[ "$HAS_NEXT" = "false" ] && break
|
||||
done
|
||||
echo "Unresolved: $UNRESOLVED"
|
||||
```
|
||||
|
||||
If unresolved > 0, the agent is NOT done — re-brief with the actual count and the rule.
|
||||
|
||||
**Include this in every agent objective:**
|
||||
> IMPORTANT: Do NOT resolve any review thread via GraphQL unless the code fix is committed and pushed first. Fix the code → commit → push → reply with SHA → then resolve. Never resolve without a real commit. "Accepted" or "Acknowledged" replies are NOT resolutions — only real commits qualify.
|
||||
|
||||
### Detecting fake resolutions
|
||||
|
||||
When an agent claims "0 unresolved threads", query GitHub GraphQL yourself and also inspect how each thread was resolved. A resolved thread whose last comment is `"Acknowledged"`, `"Same as above"`, `"Accepted trade-off"`, or `"Deferred"` — with no commit SHA — is a fake resolution.
|
||||
|
||||
To spot these, paginate all pages and collect resolved threads with missing SHA links:
|
||||
```bash
|
||||
# Paginate all pages — first:100 misses threads beyond page 1 on large PRs
|
||||
CURSOR=""; FAKE_RESOLUTIONS="[]"
|
||||
while true; do
|
||||
AFTER=${CURSOR:+", after: \"$CURSOR\""}
|
||||
PAGE=$(gh api graphql -f query="
|
||||
{
|
||||
repository(owner: \"Significant-Gravitas\", name: \"AutoGPT\") {
|
||||
pullRequest(number: PR_NUMBER) {
|
||||
reviewThreads(first: 100${AFTER}) {
|
||||
pageInfo { hasNextPage endCursor }
|
||||
nodes {
|
||||
isResolved
|
||||
comments(last: 1) {
|
||||
nodes { body author { login } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}")
|
||||
PAGE_FAKES=$(echo "$PAGE" | jq '[.data.repository.pullRequest.reviewThreads.nodes[]
|
||||
| select(.isResolved == true)
|
||||
| {body: .comments.nodes[0].body[:120], author: .comments.nodes[0].author.login}
|
||||
| select(.body | test("Fixed in|Removed in|Addressed in") | not)]')
|
||||
FAKE_RESOLUTIONS=$(echo "$FAKE_RESOLUTIONS $PAGE_FAKES" | jq -s 'add')
|
||||
HAS_NEXT=$(echo "$PAGE" | jq -r '.data.repository.pullRequest.reviewThreads.pageInfo.hasNextPage')
|
||||
CURSOR=$(echo "$PAGE" | jq -r '.data.repository.pullRequest.reviewThreads.pageInfo.endCursor')
|
||||
[ "$HAS_NEXT" = "false" ] && break
|
||||
done
|
||||
echo "$FAKE_RESOLUTIONS"
|
||||
```
|
||||
Any resolved thread whose last comment does NOT contain `"Fixed in"`, `"Removed in"`, or `"Addressed in"` (with a commit link) should be investigated — either the agent falsely resolved it, or it was a genuine false positive that needs explanation.
|
||||
|
||||
## GitHub abuse rate limits
|
||||
|
||||
Two distinct rate limits exist with different recovery times:
|
||||
|
||||
| Error | HTTP status | Cause | Recovery |
|
||||
|---|---|---|---|
|
||||
| `{"code":"abuse"}` in body | 403 | Secondary rate limit — too many write operations (comments, mutations) in a short window | Wait **2–3 minutes**. 60s is often not enough. |
|
||||
| `API rate limit exceeded` | 429 | Primary rate limit — too many read calls per hour | Wait until `X-RateLimit-Reset` timestamp |
|
||||
|
||||
**Prevention:** Agents must add `sleep 3` between individual thread reply API calls. For >20 unresolved threads, increase to `sleep 5`.
|
||||
|
||||
If you see a 403 `abuse` error from an agent's pane:
|
||||
1. Nudge the agent: `"You hit a GitHub secondary rate limit (403). Stop all API writes. Wait 2 minutes, then resume with sleep 3 between each thread reply."`
|
||||
2. Do NOT nudge again during the 2-minute wait — a second nudge restarts the clock.
|
||||
|
||||
Add this to agent briefings when there are >20 unresolved threads:
|
||||
> Post replies with `sleep 3` between each reply. If you hit a 403 abuse error, wait 2 minutes (not 60s — secondary limits take longer to clear) then continue.
|
||||
|
||||
## Key rules
|
||||
|
||||
1. **Scripts do all the heavy lifting** — don't reimplement their logic inline in this file
|
||||
2. **Never ask the user to pick a worktree** — auto-assign from `find-spare.sh` output
|
||||
3. **Never restart a running agent** — only restart on `idle` kicks (foreground is a shell)
|
||||
4. **Auto-dismiss settings dialogs** — if "Enter to confirm" appears, send Down+Enter
|
||||
5. **Always `--permission-mode bypassPermissions`** on every spawn
|
||||
6. **Escalate after 3 kicks** — mark `escalated`, surface to user
|
||||
7. **Atomic state writes** — always write to `.tmp` then `mv`
|
||||
8. **Never approve destructive commands** outside the worktree scope — when in doubt, escalate
|
||||
9. **Never recycle without verification** — `verify-complete.sh` must pass before recycling
|
||||
10. **No TASK.md files** — commit risk; use state file + `gh pr view` for agent context persistence
|
||||
11. **Re-brief stalled agents** — read objective from state file + `gh pr view`, send via tmux
|
||||
12. **ORCHESTRATOR:DONE is a signal to verify, not to accept** — always run `verify-complete.sh` and check CI run timestamp before recycling
|
||||
13. **Protected worktrees** — never use the worktree hosting the skill scripts as a spare
|
||||
14. **Images via file path** — save screenshots to `/tmp/orchestrator-context-<ts>.png`, pass path in objective; agents read with the `Read` tool
|
||||
15. **Split send-keys** — always separate text and Enter with `sleep 0.3` between calls for long strings
|
||||
16. **Poll ALL windows from state file** — never hardcode window count. Derive active windows dynamically: `jq -r '.agents[] | select(.state | test("running|idle|stuck")) | .window' ~/.claude/orchestrator-state.json`. If you added a window mid-session outside spawn-agent.sh, add it to the state file immediately.
|
||||
20. **Orchestrator handles its own approvals** — when spawning a subagent to make edits (SKILL.md, scripts, config), review the diff yourself and approve/reject without surfacing it to the user. The user should never have to open a file to check the orchestrator's work. Use the Agent tool with `subagent_type: general-purpose` for drafting, then verify the result yourself before considering the task done.
|
||||
17. **Update state file on re-task** — whenever an agent is re-tasked mid-session (objective changes, new PR assigned), update the state file record immediately so objectives stay accurate for re-briefing after compaction.
|
||||
18. **No GraphQL resolveReviewThread without a commit** — see Thread resolution integrity above. This is rule #1 for pr-address work.
|
||||
19. **Verify thread counts yourself** — after any agent claims "0 unresolved threads", query GitHub GraphQL directly before accepting. Never trust the agent's self-report.
|
||||
@@ -1,43 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# capacity.sh — show fleet capacity: available spare worktrees + in-use agents
|
||||
#
|
||||
# Usage: capacity.sh [REPO_ROOT]
|
||||
# REPO_ROOT defaults to the root worktree of the current git repo.
|
||||
#
|
||||
# Reads: ~/.claude/orchestrator-state.json (skipped if missing or corrupt)
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
STATE_FILE="${ORCHESTRATOR_STATE_FILE:-$HOME/.claude/orchestrator-state.json}"
|
||||
REPO_ROOT="${1:-$(git rev-parse --show-toplevel 2>/dev/null || echo "")}"
|
||||
|
||||
echo "=== Available (spare) worktrees ==="
|
||||
if [ -n "$REPO_ROOT" ]; then
|
||||
SPARE=$("$SCRIPTS_DIR/find-spare.sh" "$REPO_ROOT" 2>/dev/null || echo "")
|
||||
else
|
||||
SPARE=$("$SCRIPTS_DIR/find-spare.sh" 2>/dev/null || echo "")
|
||||
fi
|
||||
|
||||
if [ -z "$SPARE" ]; then
|
||||
echo " (none)"
|
||||
else
|
||||
while IFS= read -r line; do
|
||||
[ -z "$line" ] && continue
|
||||
echo " ✓ $line"
|
||||
done <<< "$SPARE"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "=== In-use worktrees ==="
|
||||
if [ -f "$STATE_FILE" ] && jq -e '.' "$STATE_FILE" >/dev/null 2>&1; then
|
||||
IN_USE=$(jq -r '.agents[] | select(.state != "done") | " [\(.state)] \(.worktree_path) → \(.branch)"' \
|
||||
"$STATE_FILE" 2>/dev/null || echo "")
|
||||
if [ -n "$IN_USE" ]; then
|
||||
echo "$IN_USE"
|
||||
else
|
||||
echo " (none)"
|
||||
fi
|
||||
else
|
||||
echo " (no active state file)"
|
||||
fi
|
||||
@@ -1,85 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# classify-pane.sh — Classify the current state of a tmux pane
|
||||
#
|
||||
# Usage: classify-pane.sh <tmux-target>
|
||||
# tmux-target: e.g. "work:0", "work:1.0"
|
||||
#
|
||||
# Output (stdout): JSON object:
|
||||
# { "state": "running|idle|waiting_approval|complete", "reason": "...", "pane_cmd": "..." }
|
||||
#
|
||||
# Exit codes: 0=ok, 1=error (invalid target or tmux window not found)
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
TARGET="${1:-}"
|
||||
|
||||
if [ -z "$TARGET" ]; then
|
||||
echo '{"state":"error","reason":"no target provided","pane_cmd":""}'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Validate tmux target format: session:window or session:window.pane
|
||||
if ! [[ "$TARGET" =~ ^[a-zA-Z0-9_.-]+:[a-zA-Z0-9_.-]+(\.[0-9]+)?$ ]]; then
|
||||
echo '{"state":"error","reason":"invalid tmux target format","pane_cmd":""}'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check session exists (use %%:* to extract session name from session:window)
|
||||
if ! tmux list-windows -t "${TARGET%%:*}" &>/dev/null 2>&1; then
|
||||
echo '{"state":"error","reason":"tmux target not found","pane_cmd":""}'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Get the current foreground command in the pane
|
||||
PANE_CMD=$(tmux display-message -t "$TARGET" -p '#{pane_current_command}' 2>/dev/null || echo "unknown")
|
||||
|
||||
# Capture and strip ANSI codes (use perl for cross-platform compatibility — BSD sed lacks \x1b support)
|
||||
RAW=$(tmux capture-pane -t "$TARGET" -p -S -50 2>/dev/null || echo "")
|
||||
CLEAN=$(echo "$RAW" | perl -pe 's/\x1b\[[0-9;]*[a-zA-Z]//g; s/\x1b\(B//g; s/\x1b\[\?[0-9]*[hl]//g; s/\r//g' \
|
||||
| grep -v '^[[:space:]]*$' || true)
|
||||
|
||||
# --- Check: explicit completion marker ---
|
||||
# Must be on its own line (not buried in the objective text sent at spawn time).
|
||||
if echo "$CLEAN" | grep -qE "^[[:space:]]*ORCHESTRATOR:DONE[[:space:]]*$"; then
|
||||
jq -n --arg cmd "$PANE_CMD" '{"state":"complete","reason":"ORCHESTRATOR:DONE marker found","pane_cmd":$cmd}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# --- Check: Claude Code approval prompt patterns ---
|
||||
LAST_40=$(echo "$CLEAN" | tail -40)
|
||||
APPROVAL_PATTERNS=(
|
||||
"Do you want to proceed"
|
||||
"Do you want to make this"
|
||||
"\\[y/n\\]"
|
||||
"\\[Y/n\\]"
|
||||
"\\[n/Y\\]"
|
||||
"Proceed\\?"
|
||||
"Allow this command"
|
||||
"Run bash command"
|
||||
"Allow bash"
|
||||
"Would you like"
|
||||
"Press enter to continue"
|
||||
"Esc to cancel"
|
||||
)
|
||||
for pattern in "${APPROVAL_PATTERNS[@]}"; do
|
||||
if echo "$LAST_40" | grep -qiE "$pattern"; then
|
||||
jq -n --arg pattern "$pattern" --arg cmd "$PANE_CMD" \
|
||||
'{"state":"waiting_approval","reason":"approval pattern: \($pattern)","pane_cmd":$cmd}'
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
|
||||
# --- Check: shell prompt (claude has exited) ---
|
||||
# If the foreground process is a shell (not claude/node), the agent has exited
|
||||
case "$PANE_CMD" in
|
||||
zsh|bash|fish|sh|dash|tcsh|ksh)
|
||||
jq -n --arg cmd "$PANE_CMD" \
|
||||
'{"state":"idle","reason":"agent exited — shell prompt active","pane_cmd":$cmd}'
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
|
||||
# Agent is still running (claude/node/python is the foreground process)
|
||||
jq -n --arg cmd "$PANE_CMD" \
|
||||
'{"state":"running","reason":"foreground process: \($cmd)","pane_cmd":$cmd}'
|
||||
exit 0
|
||||
@@ -1,24 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# find-spare.sh — list worktrees on spare/N branches (free to use)
|
||||
#
|
||||
# Usage: find-spare.sh [REPO_ROOT]
|
||||
# REPO_ROOT defaults to the root worktree containing the current git repo.
|
||||
#
|
||||
# Output (stdout): one line per available worktree: "PATH BRANCH"
|
||||
# e.g.: /Users/me/Code/AutoGPT3 spare/3
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
REPO_ROOT="${1:-$(git rev-parse --show-toplevel 2>/dev/null || echo "")}"
|
||||
if [ -z "$REPO_ROOT" ]; then
|
||||
echo "Error: not inside a git repo and no REPO_ROOT provided" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
git -C "$REPO_ROOT" worktree list --porcelain \
|
||||
| awk '
|
||||
/^worktree / { path = substr($0, 10) }
|
||||
/^branch / { branch = substr($0, 8); print path " " branch }
|
||||
' \
|
||||
| { grep -E " refs/heads/spare/[0-9]+$" || true; } \
|
||||
| sed 's|refs/heads/||'
|
||||
@@ -1,40 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# notify.sh — send a fleet notification message
|
||||
#
|
||||
# Delivery order (first available wins):
|
||||
# 1. Discord webhook — DISCORD_WEBHOOK_URL env var OR state file .discord_webhook
|
||||
# 2. macOS notification center — osascript (silent fail if unavailable)
|
||||
# 3. Stdout only
|
||||
#
|
||||
# Usage: notify.sh MESSAGE
|
||||
# Exit: always 0 (notification failure must not abort the caller)
|
||||
|
||||
MESSAGE="${1:-}"
|
||||
[ -z "$MESSAGE" ] && exit 0
|
||||
|
||||
STATE_FILE="${ORCHESTRATOR_STATE_FILE:-$HOME/.claude/orchestrator-state.json}"
|
||||
|
||||
# --- Resolve Discord webhook ---
|
||||
WEBHOOK="${DISCORD_WEBHOOK_URL:-}"
|
||||
if [ -z "$WEBHOOK" ] && [ -f "$STATE_FILE" ]; then
|
||||
WEBHOOK=$(jq -r '.discord_webhook // ""' "$STATE_FILE" 2>/dev/null || echo "")
|
||||
fi
|
||||
|
||||
# --- Discord delivery ---
|
||||
if [ -n "$WEBHOOK" ]; then
|
||||
PAYLOAD=$(jq -n --arg msg "$MESSAGE" '{"content": $msg}')
|
||||
curl -s -X POST "$WEBHOOK" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$PAYLOAD" > /dev/null 2>&1 || true
|
||||
fi
|
||||
|
||||
# --- macOS notification center (silent if not macOS or osascript missing) ---
|
||||
if command -v osascript &>/dev/null 2>&1; then
|
||||
# Escape single quotes for AppleScript
|
||||
SAFE_MSG=$(echo "$MESSAGE" | sed "s/'/\\\\'/g")
|
||||
osascript -e "display notification \"${SAFE_MSG}\" with title \"Orchestrator\"" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# Always print to stdout so run-loop.sh logs it
|
||||
echo "$MESSAGE"
|
||||
exit 0
|
||||
@@ -1,257 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# poll-cycle.sh — Single orchestrator poll cycle
|
||||
#
|
||||
# Reads ~/.claude/orchestrator-state.json, classifies each agent, updates state,
|
||||
# and outputs a JSON array of actions for Claude to take.
|
||||
#
|
||||
# Usage: poll-cycle.sh
|
||||
# Output (stdout): JSON array of action objects
|
||||
# [{ "window": "work:0", "action": "kick|approve|none", "state": "...",
|
||||
# "worktree": "...", "objective": "...", "reason": "..." }]
|
||||
#
|
||||
# The state file is updated in-place (atomic write via .tmp).
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
STATE_FILE="${ORCHESTRATOR_STATE_FILE:-$HOME/.claude/orchestrator-state.json}"
|
||||
SCRIPTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
CLASSIFY="$SCRIPTS_DIR/classify-pane.sh"
|
||||
|
||||
# Cross-platform md5: always outputs just the hex digest
|
||||
md5_hash() {
|
||||
if command -v md5sum &>/dev/null; then
|
||||
md5sum | awk '{print $1}'
|
||||
else
|
||||
md5 | awk '{print $NF}'
|
||||
fi
|
||||
}
|
||||
|
||||
# Clean up temp file on any exit (avoids stale .tmp if jq write fails)
|
||||
trap 'rm -f "${STATE_FILE}.tmp"' EXIT
|
||||
|
||||
# Ensure state file exists
|
||||
if [ ! -f "$STATE_FILE" ]; then
|
||||
echo '{"active":false,"agents":[]}' > "$STATE_FILE"
|
||||
fi
|
||||
|
||||
# Validate JSON upfront before any jq reads that run under set -e.
|
||||
# A truncated/corrupt file (e.g. from a SIGKILL mid-write) would otherwise
|
||||
# abort the script at the ACTIVE read below without emitting any JSON output.
|
||||
if ! jq -e '.' "$STATE_FILE" >/dev/null 2>&1; then
|
||||
echo "State file parse error — check $STATE_FILE" >&2
|
||||
echo "[]"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
ACTIVE=$(jq -r '.active // false' "$STATE_FILE")
|
||||
if [ "$ACTIVE" != "true" ]; then
|
||||
echo "[]"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
NOW=$(date +%s)
|
||||
IDLE_THRESHOLD=$(jq -r '.idle_threshold_seconds // 300' "$STATE_FILE")
|
||||
|
||||
ACTIONS="[]"
|
||||
UPDATED_AGENTS="[]"
|
||||
|
||||
# Read agents as newline-delimited JSON objects.
|
||||
# jq exits non-zero when .agents[] has no matches on an empty array, which is valid —
|
||||
# so we suppress that exit code and separately validate the file is well-formed JSON.
|
||||
if ! AGENTS_JSON=$(jq -e -c '.agents // empty | .[]' "$STATE_FILE" 2>/dev/null); then
|
||||
if ! jq -e '.' "$STATE_FILE" > /dev/null 2>&1; then
|
||||
echo "State file parse error — check $STATE_FILE" >&2
|
||||
fi
|
||||
echo "[]"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ -z "$AGENTS_JSON" ]; then
|
||||
echo "[]"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
while IFS= read -r agent; do
|
||||
[ -z "$agent" ] && continue
|
||||
|
||||
# Use // "" defaults so a single malformed field doesn't abort the whole cycle
|
||||
WINDOW=$(echo "$agent" | jq -r '.window // ""')
|
||||
WORKTREE=$(echo "$agent" | jq -r '.worktree // ""')
|
||||
OBJECTIVE=$(echo "$agent"| jq -r '.objective // ""')
|
||||
STATE=$(echo "$agent" | jq -r '.state // "running"')
|
||||
LAST_HASH=$(echo "$agent"| jq -r '.last_output_hash // ""')
|
||||
IDLE_SINCE=$(echo "$agent"| jq -r '.idle_since // 0')
|
||||
REVISION_COUNT=$(echo "$agent"| jq -r '.revision_count // 0')
|
||||
|
||||
# Validate window format to prevent tmux target injection.
|
||||
# Allow session:window (numeric or named) and session:window.pane
|
||||
if ! [[ "$WINDOW" =~ ^[a-zA-Z0-9_.-]+:[a-zA-Z0-9_.-]+(\.[0-9]+)?$ ]]; then
|
||||
echo "Skipping agent with invalid window value: $WINDOW" >&2
|
||||
UPDATED_AGENTS=$(echo "$UPDATED_AGENTS" | jq --argjson a "$agent" '. + [$a]')
|
||||
continue
|
||||
fi
|
||||
|
||||
# Pass-through terminal-state agents
|
||||
if [[ "$STATE" == "done" || "$STATE" == "escalated" || "$STATE" == "complete" || "$STATE" == "pending_evaluation" ]]; then
|
||||
UPDATED_AGENTS=$(echo "$UPDATED_AGENTS" | jq --argjson a "$agent" '. + [$a]')
|
||||
continue
|
||||
fi
|
||||
|
||||
# Classify pane.
|
||||
# classify-pane.sh always emits JSON before exit (even on error), so using
|
||||
# "|| echo '...'" would concatenate two JSON objects when it exits non-zero.
|
||||
# Use "|| true" inside the substitution so set -euo pipefail does not abort
|
||||
# the poll cycle when classify exits with a non-zero status code.
|
||||
CLASSIFICATION=$("$CLASSIFY" "$WINDOW" 2>/dev/null || true)
|
||||
[ -z "$CLASSIFICATION" ] && CLASSIFICATION='{"state":"error","reason":"classify failed","pane_cmd":"unknown"}'
|
||||
|
||||
PANE_STATE=$(echo "$CLASSIFICATION" | jq -r '.state')
|
||||
PANE_REASON=$(echo "$CLASSIFICATION" | jq -r '.reason')
|
||||
|
||||
# Capture full pane output once — used for hash (stuck detection) and checkpoint parsing.
|
||||
# Use -S -500 to get the last ~500 lines of scrollback so checkpoints aren't missed.
|
||||
RAW=$(tmux capture-pane -t "$WINDOW" -p -S -500 2>/dev/null || echo "")
|
||||
|
||||
# --- Checkpoint tracking ---
|
||||
# Parse any "CHECKPOINT:<step>" lines the agent has output and merge into state file.
|
||||
# The agent writes these as it completes each required step so verify-complete.sh can gate recycling.
|
||||
EXISTING_CPS=$(echo "$agent" | jq -c '.checkpoints // []')
|
||||
NEW_CHECKPOINTS_JSON="$EXISTING_CPS"
|
||||
if [ -n "$RAW" ]; then
|
||||
FOUND_CPS=$(echo "$RAW" \
|
||||
| grep -oE "CHECKPOINT:[a-zA-Z0-9_-]+" \
|
||||
| sed 's/CHECKPOINT://' \
|
||||
| sort -u \
|
||||
| jq -R . | jq -s . 2>/dev/null || echo "[]")
|
||||
NEW_CHECKPOINTS_JSON=$(jq -n \
|
||||
--argjson existing "$EXISTING_CPS" \
|
||||
--argjson found "$FOUND_CPS" \
|
||||
'($existing + $found) | unique' 2>/dev/null || echo "$EXISTING_CPS")
|
||||
fi
|
||||
|
||||
# Compute content hash for stuck-detection (only for running agents)
|
||||
CURRENT_HASH=""
|
||||
if [[ "$PANE_STATE" == "running" ]] && [ -n "$RAW" ]; then
|
||||
CURRENT_HASH=$(echo "$RAW" | tail -20 | md5_hash)
|
||||
fi
|
||||
|
||||
NEW_STATE="$STATE"
|
||||
NEW_IDLE_SINCE="$IDLE_SINCE"
|
||||
NEW_REVISION_COUNT="$REVISION_COUNT"
|
||||
ACTION="none"
|
||||
REASON="$PANE_REASON"
|
||||
|
||||
case "$PANE_STATE" in
|
||||
complete)
|
||||
# Agent output ORCHESTRATOR:DONE — mark pending_evaluation so orchestrator handles it.
|
||||
# run-loop does NOT verify or notify; orchestrator's background poll picks this up.
|
||||
NEW_STATE="pending_evaluation"
|
||||
ACTION="complete" # run-loop logs it but takes no action
|
||||
;;
|
||||
waiting_approval)
|
||||
NEW_STATE="waiting_approval"
|
||||
ACTION="approve"
|
||||
;;
|
||||
idle)
|
||||
# Agent process has exited — needs restart
|
||||
NEW_STATE="idle"
|
||||
ACTION="kick"
|
||||
REASON="agent exited (shell is foreground)"
|
||||
NEW_REVISION_COUNT=$(( REVISION_COUNT + 1 ))
|
||||
NEW_IDLE_SINCE=$NOW
|
||||
if [ "$NEW_REVISION_COUNT" -ge 3 ]; then
|
||||
NEW_STATE="escalated"
|
||||
ACTION="none"
|
||||
REASON="escalated after ${NEW_REVISION_COUNT} kicks — needs human attention"
|
||||
fi
|
||||
;;
|
||||
running)
|
||||
# Clear idle_since only when transitioning from idle (agent was kicked and
|
||||
# restarted). Do NOT reset for stuck — idle_since must persist across polls
|
||||
# so STUCK_DURATION can accumulate and trigger escalation.
|
||||
# Also update the local IDLE_SINCE so the hash-stability check below uses
|
||||
# the reset value on this same poll, not the stale kick timestamp.
|
||||
if [[ "$STATE" == "idle" ]]; then
|
||||
NEW_IDLE_SINCE=0
|
||||
IDLE_SINCE=0
|
||||
fi
|
||||
# Check if hash has been stable (agent may be stuck mid-task)
|
||||
if [ -n "$CURRENT_HASH" ] && [ "$CURRENT_HASH" = "$LAST_HASH" ] && [ "$LAST_HASH" != "" ]; then
|
||||
if [ "$IDLE_SINCE" = "0" ] || [ "$IDLE_SINCE" = "null" ]; then
|
||||
NEW_IDLE_SINCE=$NOW
|
||||
else
|
||||
STUCK_DURATION=$(( NOW - IDLE_SINCE ))
|
||||
if [ "$STUCK_DURATION" -gt "$IDLE_THRESHOLD" ]; then
|
||||
NEW_REVISION_COUNT=$(( REVISION_COUNT + 1 ))
|
||||
NEW_IDLE_SINCE=$NOW
|
||||
if [ "$NEW_REVISION_COUNT" -ge 3 ]; then
|
||||
NEW_STATE="escalated"
|
||||
ACTION="none"
|
||||
REASON="escalated after ${NEW_REVISION_COUNT} kicks — needs human attention"
|
||||
else
|
||||
NEW_STATE="stuck"
|
||||
ACTION="kick"
|
||||
REASON="output unchanged for ${STUCK_DURATION}s (threshold: ${IDLE_THRESHOLD}s)"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
else
|
||||
# Only reset the idle timer when we have a valid hash comparison (pane
|
||||
# capture succeeded). If CURRENT_HASH is empty (tmux capture-pane failed),
|
||||
# preserve existing timers so stuck detection is not inadvertently reset.
|
||||
if [ -n "$CURRENT_HASH" ]; then
|
||||
NEW_STATE="running"
|
||||
NEW_IDLE_SINCE=0
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
error)
|
||||
REASON="classify error: $PANE_REASON"
|
||||
;;
|
||||
esac
|
||||
|
||||
# Build updated agent record (ensure idle_since and revision_count are numeric)
|
||||
# Use || true on each jq call so a malformed field skips this agent rather than
|
||||
# aborting the entire poll cycle under set -e.
|
||||
UPDATED_AGENT=$(echo "$agent" | jq \
|
||||
--arg state "$NEW_STATE" \
|
||||
--arg hash "$CURRENT_HASH" \
|
||||
--argjson now "$NOW" \
|
||||
--arg idle_since "$NEW_IDLE_SINCE" \
|
||||
--arg revision_count "$NEW_REVISION_COUNT" \
|
||||
--argjson checkpoints "$NEW_CHECKPOINTS_JSON" \
|
||||
'.state = $state
|
||||
| .last_output_hash = (if $hash == "" then .last_output_hash else $hash end)
|
||||
| .last_seen_at = $now
|
||||
| .idle_since = ($idle_since | tonumber)
|
||||
| .revision_count = ($revision_count | tonumber)
|
||||
| .checkpoints = $checkpoints' 2>/dev/null) || {
|
||||
echo "Warning: failed to build updated agent for window $WINDOW — keeping original" >&2
|
||||
UPDATED_AGENTS=$(echo "$UPDATED_AGENTS" | jq --argjson a "$agent" '. + [$a]')
|
||||
continue
|
||||
}
|
||||
|
||||
UPDATED_AGENTS=$(echo "$UPDATED_AGENTS" | jq --argjson a "$UPDATED_AGENT" '. + [$a]')
|
||||
|
||||
# Add action if needed
|
||||
if [ "$ACTION" != "none" ]; then
|
||||
ACTION_OBJ=$(jq -n \
|
||||
--arg window "$WINDOW" \
|
||||
--arg action "$ACTION" \
|
||||
--arg state "$NEW_STATE" \
|
||||
--arg worktree "$WORKTREE" \
|
||||
--arg objective "$OBJECTIVE" \
|
||||
--arg reason "$REASON" \
|
||||
'{window:$window, action:$action, state:$state, worktree:$worktree, objective:$objective, reason:$reason}')
|
||||
ACTIONS=$(echo "$ACTIONS" | jq --argjson a "$ACTION_OBJ" '. + [$a]')
|
||||
fi
|
||||
|
||||
done <<< "$AGENTS_JSON"
|
||||
|
||||
# Atomic state file update
|
||||
jq --argjson agents "$UPDATED_AGENTS" \
|
||||
--argjson now "$NOW" \
|
||||
'.agents = $agents | .last_poll_at = $now' \
|
||||
"$STATE_FILE" > "${STATE_FILE}.tmp" && mv "${STATE_FILE}.tmp" "$STATE_FILE"
|
||||
|
||||
echo "$ACTIONS"
|
||||
@@ -1,32 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# recycle-agent.sh — kill a tmux window and restore the worktree to its spare branch
|
||||
#
|
||||
# Usage: recycle-agent.sh WINDOW WORKTREE_PATH SPARE_BRANCH
|
||||
# WINDOW — tmux target, e.g. autogpt1:3
|
||||
# WORKTREE_PATH — absolute path to the git worktree
|
||||
# SPARE_BRANCH — branch to restore, e.g. spare/6
|
||||
#
|
||||
# Stdout: one status line
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
if [ $# -lt 3 ]; then
|
||||
echo "Usage: recycle-agent.sh WINDOW WORKTREE_PATH SPARE_BRANCH" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
WINDOW="$1"
|
||||
WORKTREE_PATH="$2"
|
||||
SPARE_BRANCH="$3"
|
||||
|
||||
# Kill the tmux window (ignore error — may already be gone)
|
||||
tmux kill-window -t "$WINDOW" 2>/dev/null || true
|
||||
|
||||
# Restore to spare branch: abort any in-progress operation, then clean
|
||||
git -C "$WORKTREE_PATH" rebase --abort 2>/dev/null || true
|
||||
git -C "$WORKTREE_PATH" merge --abort 2>/dev/null || true
|
||||
git -C "$WORKTREE_PATH" reset --hard HEAD 2>/dev/null
|
||||
git -C "$WORKTREE_PATH" clean -fd 2>/dev/null
|
||||
git -C "$WORKTREE_PATH" checkout "$SPARE_BRANCH"
|
||||
|
||||
echo "Recycled: $(basename "$WORKTREE_PATH") → $SPARE_BRANCH (window $WINDOW closed)"
|
||||
@@ -1,215 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# run-loop.sh — Mechanical babysitter for the agent fleet (runs in its own tmux window)
|
||||
#
|
||||
# Handles ONLY two things that need no intelligence:
|
||||
# idle → restart claude using --resume SESSION_ID (or --continue) to restore context
|
||||
# approve → auto-approve safe dialogs, press Enter on numbered-option dialogs
|
||||
#
|
||||
# Everything else — ORCHESTRATOR:DONE, verification, /pr-test, final evaluation,
|
||||
# marking done, deciding to close windows — is the orchestrating Claude's job.
|
||||
# poll-cycle.sh sets state to pending_evaluation when ORCHESTRATOR:DONE is detected;
|
||||
# the orchestrator's background poll loop handles it from there.
|
||||
#
|
||||
# Usage: run-loop.sh
|
||||
# Env: POLL_INTERVAL (default: 30), ORCHESTRATOR_STATE_FILE
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Copy scripts to a stable location outside the repo so they survive branch
|
||||
# checkouts (e.g. recycle-agent.sh switching spare/N back into this worktree
|
||||
# would wipe .claude/skills/orchestrate/scripts if the skill only exists on the
|
||||
# current branch).
|
||||
_ORIGIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
STABLE_SCRIPTS_DIR="$HOME/.claude/orchestrator/scripts"
|
||||
mkdir -p "$STABLE_SCRIPTS_DIR"
|
||||
cp "$_ORIGIN_DIR"/*.sh "$STABLE_SCRIPTS_DIR/"
|
||||
chmod +x "$STABLE_SCRIPTS_DIR"/*.sh
|
||||
SCRIPTS_DIR="$STABLE_SCRIPTS_DIR"
|
||||
|
||||
STATE_FILE="${ORCHESTRATOR_STATE_FILE:-$HOME/.claude/orchestrator-state.json}"
|
||||
# Adaptive polling: starts at base interval, backs off up to POLL_IDLE_MAX when
|
||||
# no agents need attention, resets on any activity or waiting_approval state.
|
||||
POLL_INTERVAL="${POLL_INTERVAL:-30}"
|
||||
POLL_IDLE_MAX=${POLL_IDLE_MAX:-300}
|
||||
POLL_CURRENT=$POLL_INTERVAL
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# update_state WINDOW FIELD VALUE
|
||||
# ---------------------------------------------------------------------------
|
||||
update_state() {
|
||||
local window="$1" field="$2" value="$3"
|
||||
jq --arg w "$window" --arg f "$field" --arg v "$value" \
|
||||
'.agents |= map(if .window == $w then .[$f] = $v else . end)' \
|
||||
"$STATE_FILE" > "${STATE_FILE}.tmp" && mv "${STATE_FILE}.tmp" "$STATE_FILE"
|
||||
}
|
||||
|
||||
update_state_int() {
|
||||
local window="$1" field="$2" value="$3"
|
||||
jq --arg w "$window" --arg f "$field" --argjson v "$value" \
|
||||
'.agents |= map(if .window == $w then .[$f] = $v else . end)' \
|
||||
"$STATE_FILE" > "${STATE_FILE}.tmp" && mv "${STATE_FILE}.tmp" "$STATE_FILE"
|
||||
}
|
||||
|
||||
agent_field() {
|
||||
jq -r --arg w "$1" --arg f "$2" \
|
||||
'.agents[] | select(.window == $w) | .[$f] // ""' \
|
||||
"$STATE_FILE" 2>/dev/null
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# wait_for_prompt WINDOW — wait up to 60s for Claude's ❯ prompt
|
||||
# ---------------------------------------------------------------------------
|
||||
wait_for_prompt() {
|
||||
local window="$1"
|
||||
for i in $(seq 1 60); do
|
||||
local cmd pane
|
||||
cmd=$(tmux display-message -t "$window" -p '#{pane_current_command}' 2>/dev/null || echo "")
|
||||
pane=$(tmux capture-pane -t "$window" -p 2>/dev/null || echo "")
|
||||
if echo "$pane" | grep -q "Enter to confirm"; then
|
||||
tmux send-keys -t "$window" Down Enter; sleep 2; continue
|
||||
fi
|
||||
[[ "$cmd" == "node" ]] && echo "$pane" | grep -q "❯" && return 0
|
||||
sleep 1
|
||||
done
|
||||
return 1 # timed out
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# wait_for_claude_idle WINDOW — wait up to 30s for Claude to reach idle ❯ prompt
|
||||
# (no spinner or busy indicator visible in the last 3 lines of pane output)
|
||||
# Returns 0 when idle, 1 on timeout.
|
||||
# ---------------------------------------------------------------------------
|
||||
wait_for_claude_idle() {
|
||||
local window="$1"
|
||||
local timeout="${2:-30}"
|
||||
local elapsed=0
|
||||
while (( elapsed < timeout )); do
|
||||
local cmd pane pane_tail
|
||||
cmd=$(tmux display-message -t "$window" -p '#{pane_current_command}' 2>/dev/null || echo "")
|
||||
pane=$(tmux capture-pane -t "$window" -p 2>/dev/null || echo "")
|
||||
pane_tail=$(echo "$pane" | tail -3)
|
||||
# Check full pane (not just tail) — 'Enter to confirm' dialog can scroll above last 3 lines.
|
||||
# Do NOT reset elapsed — resetting allows an infinite loop if the dialog never clears.
|
||||
if echo "$pane" | grep -q "Enter to confirm"; then
|
||||
tmux send-keys -t "$window" Down Enter
|
||||
sleep 2; (( elapsed += 2 )); continue
|
||||
fi
|
||||
# Must be running under node (Claude is live)
|
||||
if [[ "$cmd" == "node" ]]; then
|
||||
# Idle: ❯ prompt visible AND no spinner/busy text in last 3 lines
|
||||
if echo "$pane_tail" | grep -q "❯" && \
|
||||
! echo "$pane_tail" | grep -qE '[✳✽✢✶·✻✼✿❋✤]|Running…|Compacting'; then
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
sleep 2
|
||||
(( elapsed += 2 ))
|
||||
done
|
||||
return 1 # timed out
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# handle_kick WINDOW STATE — only for idle (crashed) agents, not stuck
|
||||
# ---------------------------------------------------------------------------
|
||||
handle_kick() {
|
||||
local window="$1" state="$2"
|
||||
[[ "$state" != "idle" ]] && return # stuck agents handled by supervisor
|
||||
|
||||
local worktree_path session_id
|
||||
worktree_path=$(agent_field "$window" "worktree_path")
|
||||
session_id=$(agent_field "$window" "session_id")
|
||||
|
||||
echo "[$(date +%H:%M:%S)] KICK restart $window — agent exited, resuming session"
|
||||
|
||||
# Wait for the shell prompt before typing — avoids sending into a still-draining pane
|
||||
wait_for_claude_idle "$window" 30 \
|
||||
|| echo "[$(date +%H:%M:%S)] KICK WARNING $window — pane still busy before resume, sending anyway"
|
||||
|
||||
# Resume the exact session so the agent retains full context — no need to re-send objective
|
||||
if [ -n "$session_id" ]; then
|
||||
tmux send-keys -t "$window" "cd '${worktree_path}' && claude --resume '${session_id}' --permission-mode bypassPermissions" Enter
|
||||
else
|
||||
tmux send-keys -t "$window" "cd '${worktree_path}' && claude --continue --permission-mode bypassPermissions" Enter
|
||||
fi
|
||||
|
||||
wait_for_prompt "$window" || echo "[$(date +%H:%M:%S)] KICK WARNING $window — timed out waiting for ❯"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# handle_approve WINDOW — auto-approve dialogs that need no judgment
|
||||
# ---------------------------------------------------------------------------
|
||||
handle_approve() {
|
||||
local window="$1"
|
||||
local pane_tail
|
||||
pane_tail=$(tmux capture-pane -t "$window" -p 2>/dev/null | tail -3 || echo "")
|
||||
|
||||
# Settings error dialog at startup
|
||||
if echo "$pane_tail" | grep -q "Enter to confirm"; then
|
||||
echo "[$(date +%H:%M:%S)] APPROVE dialog $window — settings error"
|
||||
tmux send-keys -t "$window" Down Enter
|
||||
return
|
||||
fi
|
||||
|
||||
# Numbered-option dialog (e.g. "Do you want to make this edit?")
|
||||
# ❯ is already on option 1 (Yes) — Enter confirms it
|
||||
if echo "$pane_tail" | grep -qE "❯\s*1\." || echo "$pane_tail" | grep -q "Esc to cancel"; then
|
||||
echo "[$(date +%H:%M:%S)] APPROVE edit $window"
|
||||
tmux send-keys -t "$window" "" Enter
|
||||
return
|
||||
fi
|
||||
|
||||
# y/n prompt for safe operations
|
||||
if echo "$pane_tail" | grep -qiE "(^git |^npm |^pnpm |^poetry |^pytest|^docker |^make |^cargo |^pip |^yarn |curl .*(localhost|127\.0\.0\.1))"; then
|
||||
echo "[$(date +%H:%M:%S)] APPROVE safe $window"
|
||||
tmux send-keys -t "$window" "y" Enter
|
||||
return
|
||||
fi
|
||||
|
||||
# Anything else — supervisor handles it, just log
|
||||
echo "[$(date +%H:%M:%S)] APPROVE skip $window — unknown dialog, supervisor will handle"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Main loop
|
||||
# ---------------------------------------------------------------------------
|
||||
echo "[$(date +%H:%M:%S)] run-loop started (mechanical only, poll ${POLL_INTERVAL}s→${POLL_IDLE_MAX}s adaptive)"
|
||||
echo "[$(date +%H:%M:%S)] Supervisor: orchestrating Claude session (not a separate window)"
|
||||
echo "---"
|
||||
|
||||
while true; do
|
||||
if ! jq -e '.active == true' "$STATE_FILE" >/dev/null 2>&1; then
|
||||
echo "[$(date +%H:%M:%S)] active=false — exiting."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
ACTIONS=$("$SCRIPTS_DIR/poll-cycle.sh" 2>/dev/null || echo "[]")
|
||||
KICKED=0; DONE=0
|
||||
|
||||
while IFS= read -r action; do
|
||||
[ -z "$action" ] && continue
|
||||
WINDOW=$(echo "$action" | jq -r '.window // ""')
|
||||
ACTION=$(echo "$action" | jq -r '.action // ""')
|
||||
STATE=$(echo "$action" | jq -r '.state // ""')
|
||||
|
||||
case "$ACTION" in
|
||||
kick) handle_kick "$WINDOW" "$STATE" || true; KICKED=$(( KICKED + 1 )) ;;
|
||||
approve) handle_approve "$WINDOW" || true ;;
|
||||
complete) DONE=$(( DONE + 1 )) ;; # poll-cycle already set state=pending_evaluation; orchestrator handles
|
||||
esac
|
||||
done < <(echo "$ACTIONS" | jq -c '.[]' 2>/dev/null || true)
|
||||
|
||||
RUNNING=$(jq '[.agents[] | select(.state | test("running|stuck|waiting_approval|idle"))] | length' \
|
||||
"$STATE_FILE" 2>/dev/null || echo 0)
|
||||
|
||||
# Adaptive backoff: reset to base on activity or waiting_approval agents; back off when truly idle
|
||||
WAITING=$(jq '[.agents[] | select(.state == "waiting_approval")] | length' "$STATE_FILE" 2>/dev/null || echo 0)
|
||||
if (( KICKED > 0 || DONE > 0 || WAITING > 0 )); then
|
||||
POLL_CURRENT=$POLL_INTERVAL
|
||||
else
|
||||
POLL_CURRENT=$(( POLL_CURRENT + POLL_CURRENT / 2 + 1 ))
|
||||
(( POLL_CURRENT > POLL_IDLE_MAX )) && POLL_CURRENT=$POLL_IDLE_MAX
|
||||
fi
|
||||
|
||||
echo "[$(date +%H:%M:%S)] Poll — ${RUNNING} running ${KICKED} kicked ${DONE} recycled (next in ${POLL_CURRENT}s)"
|
||||
sleep "$POLL_CURRENT"
|
||||
done
|
||||
@@ -1,129 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# spawn-agent.sh — create tmux window, checkout branch, launch claude, send task
|
||||
#
|
||||
# Usage: spawn-agent.sh SESSION WORKTREE_PATH SPARE_BRANCH NEW_BRANCH OBJECTIVE [PR_NUMBER] [STEPS...]
|
||||
# SESSION — tmux session name, e.g. autogpt1
|
||||
# WORKTREE_PATH — absolute path to the git worktree
|
||||
# SPARE_BRANCH — spare branch being replaced, e.g. spare/6 (saved for recycle)
|
||||
# NEW_BRANCH — task branch to create, e.g. feat/my-feature
|
||||
# OBJECTIVE — task description sent to the agent
|
||||
# PR_NUMBER — (optional) GitHub PR number for completion verification
|
||||
# STEPS... — (optional) required checkpoint names, e.g. pr-address pr-test
|
||||
#
|
||||
# Stdout: SESSION:WINDOW_INDEX (nothing else — callers rely on this)
|
||||
# Exit non-zero on failure.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
if [ $# -lt 5 ]; then
|
||||
echo "Usage: spawn-agent.sh SESSION WORKTREE_PATH SPARE_BRANCH NEW_BRANCH OBJECTIVE [PR_NUMBER] [STEPS...]" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SESSION="$1"
|
||||
WORKTREE_PATH="$2"
|
||||
SPARE_BRANCH="$3"
|
||||
NEW_BRANCH="$4"
|
||||
OBJECTIVE="$5"
|
||||
PR_NUMBER="${6:-}"
|
||||
STEPS=("${@:7}")
|
||||
WORKTREE_NAME=$(basename "$WORKTREE_PATH")
|
||||
STATE_FILE="${ORCHESTRATOR_STATE_FILE:-$HOME/.claude/orchestrator-state.json}"
|
||||
|
||||
# Generate a stable session ID so this agent's Claude session can always be resumed:
|
||||
# claude --resume $SESSION_ID --permission-mode bypassPermissions
|
||||
SESSION_ID=$(uuidgen 2>/dev/null || python3 -c "import uuid; print(uuid.uuid4())")
|
||||
|
||||
# Create (or switch to) the task branch
|
||||
git -C "$WORKTREE_PATH" checkout -b "$NEW_BRANCH" 2>/dev/null \
|
||||
|| git -C "$WORKTREE_PATH" checkout "$NEW_BRANCH"
|
||||
|
||||
# Open a new named tmux window; capture its numeric index
|
||||
WIN_IDX=$(tmux new-window -t "$SESSION" -n "$WORKTREE_NAME" -P -F '#{window_index}')
|
||||
WINDOW="${SESSION}:${WIN_IDX}"
|
||||
|
||||
# Append the initial agent record to the state file so subsequent jq updates find it.
|
||||
# This must happen before the pr_number/steps update below.
|
||||
if [ -f "$STATE_FILE" ]; then
|
||||
NOW=$(date +%s)
|
||||
jq --arg window "$WINDOW" \
|
||||
--arg worktree "$WORKTREE_NAME" \
|
||||
--arg worktree_path "$WORKTREE_PATH" \
|
||||
--arg spare_branch "$SPARE_BRANCH" \
|
||||
--arg branch "$NEW_BRANCH" \
|
||||
--arg objective "$OBJECTIVE" \
|
||||
--arg session_id "$SESSION_ID" \
|
||||
--argjson now "$NOW" \
|
||||
'.agents += [{
|
||||
"window": $window,
|
||||
"worktree": $worktree,
|
||||
"worktree_path": $worktree_path,
|
||||
"spare_branch": $spare_branch,
|
||||
"branch": $branch,
|
||||
"objective": $objective,
|
||||
"session_id": $session_id,
|
||||
"state": "running",
|
||||
"checkpoints": [],
|
||||
"last_output_hash": "",
|
||||
"last_seen_at": $now,
|
||||
"spawned_at": $now,
|
||||
"idle_since": 0,
|
||||
"revision_count": 0,
|
||||
"last_rebriefed_at": 0
|
||||
}]' "$STATE_FILE" > "${STATE_FILE}.tmp" && mv "${STATE_FILE}.tmp" "$STATE_FILE"
|
||||
fi
|
||||
|
||||
# Store pr_number + steps in state file if provided (enables verify-complete.sh).
|
||||
# The agent record was appended above so the jq select now finds it.
|
||||
if [ -n "$PR_NUMBER" ] && [ -f "$STATE_FILE" ]; then
|
||||
if [ "${#STEPS[@]}" -gt 0 ]; then
|
||||
STEPS_JSON=$(printf '%s\n' "${STEPS[@]}" | jq -R . | jq -s .)
|
||||
else
|
||||
STEPS_JSON='[]'
|
||||
fi
|
||||
jq --arg w "$WINDOW" --arg pr "$PR_NUMBER" --argjson steps "$STEPS_JSON" \
|
||||
'.agents |= map(if .window == $w then . + {pr_number: $pr, steps: $steps, checkpoints: []} else . end)' \
|
||||
"$STATE_FILE" > "${STATE_FILE}.tmp" && mv "${STATE_FILE}.tmp" "$STATE_FILE"
|
||||
fi
|
||||
|
||||
# Launch claude with a stable session ID so it can always be resumed after a crash:
|
||||
# claude --resume SESSION_ID --permission-mode bypassPermissions
|
||||
tmux send-keys -t "$WINDOW" "cd '${WORKTREE_PATH}' && claude --permission-mode bypassPermissions --session-id '${SESSION_ID}'" Enter
|
||||
|
||||
# wait_for_claude_idle — poll until the pane shows idle ❯ with no spinner in the last 3 lines.
|
||||
# Returns 0 when idle, 1 on timeout.
|
||||
_wait_idle() {
|
||||
local window="$1" timeout="${2:-60}" elapsed=0
|
||||
while (( elapsed < timeout )); do
|
||||
local cmd pane_tail
|
||||
cmd=$(tmux display-message -t "$window" -p '#{pane_current_command}' 2>/dev/null || echo "")
|
||||
pane=$(tmux capture-pane -t "$window" -p 2>/dev/null || echo "")
|
||||
pane_tail=$(echo "$pane" | tail -3)
|
||||
# Check full pane (not just tail) — 'Enter to confirm' dialog can appear above the last 3 lines
|
||||
if echo "$pane" | grep -q "Enter to confirm"; then
|
||||
tmux send-keys -t "$window" Down Enter
|
||||
sleep 2; (( elapsed += 2 )); continue
|
||||
fi
|
||||
if [[ "$cmd" == "node" ]] && \
|
||||
echo "$pane_tail" | grep -q "❯" && \
|
||||
! echo "$pane_tail" | grep -qE '[✳✽✢✶·✻✼✿❋✤]|Running…|Compacting'; then
|
||||
return 0
|
||||
fi
|
||||
sleep 2; (( elapsed += 2 ))
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Wait up to 60s for claude to be fully interactive and idle (❯ visible, no spinner).
|
||||
if ! _wait_idle "$WINDOW" 60; then
|
||||
echo "[spawn-agent] WARNING: timed out waiting for idle ❯ prompt on $WINDOW — sending objective anyway" >&2
|
||||
fi
|
||||
|
||||
# Send the task. Split text and Enter — if combined, Enter can fire before the string
|
||||
# is fully buffered, leaving the message stuck as "[Pasted text +N lines]" unsent.
|
||||
tmux send-keys -t "$WINDOW" "${OBJECTIVE} Output each completed step as CHECKPOINT:<step-name>. When ALL steps are done, output ORCHESTRATOR:DONE on its own line."
|
||||
sleep 0.3
|
||||
tmux send-keys -t "$WINDOW" Enter
|
||||
|
||||
# Only output the window address — nothing else (callers parse this)
|
||||
echo "$WINDOW"
|
||||
@@ -1,43 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# status.sh — print orchestrator status: state file summary + live tmux pane commands
|
||||
#
|
||||
# Usage: status.sh
|
||||
# Reads: ~/.claude/orchestrator-state.json
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
STATE_FILE="${ORCHESTRATOR_STATE_FILE:-$HOME/.claude/orchestrator-state.json}"
|
||||
|
||||
if [ ! -f "$STATE_FILE" ] || ! jq -e '.' "$STATE_FILE" >/dev/null 2>&1; then
|
||||
echo "No orchestrator state found at $STATE_FILE"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Header: active status, session, thresholds, last poll
|
||||
jq -r '
|
||||
"=== Orchestrator [\(if .active then "RUNNING" else "STOPPED" end)] ===",
|
||||
"Session: \(.tmux_session // "unknown") | Idle threshold: \(.idle_threshold_seconds // 300)s",
|
||||
"Last poll: \(if (.last_poll_at // 0) == 0 then "never" else (.last_poll_at | strftime("%H:%M:%S")) end)",
|
||||
""
|
||||
' "$STATE_FILE"
|
||||
|
||||
# Each agent: state, window, worktree/branch, truncated objective
|
||||
AGENT_COUNT=$(jq '.agents | length' "$STATE_FILE")
|
||||
if [ "$AGENT_COUNT" -eq 0 ]; then
|
||||
echo " (no agents registered)"
|
||||
else
|
||||
jq -r '
|
||||
.agents[] |
|
||||
" [\(.state | ascii_upcase)] \(.window) \(.worktree)/\(.branch)",
|
||||
" \(.objective // "" | .[0:70])"
|
||||
' "$STATE_FILE"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
|
||||
# Live pane_current_command for non-done agents
|
||||
while IFS= read -r WINDOW; do
|
||||
[ -z "$WINDOW" ] && continue
|
||||
CMD=$(tmux display-message -t "$WINDOW" -p '#{pane_current_command}' 2>/dev/null || echo "unreachable")
|
||||
echo " $WINDOW live: $CMD"
|
||||
done < <(jq -r '.agents[] | select(.state != "done") | .window' "$STATE_FILE" 2>/dev/null || true)
|
||||
@@ -1,180 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# verify-complete.sh — verify a PR task is truly done before marking the agent done
|
||||
#
|
||||
# Check order matters:
|
||||
# 1. Checkpoints — did the agent do all required steps?
|
||||
# 2. CI complete — no pending (bots post comments AFTER their check runs, must wait)
|
||||
# 3. CI passing — no failures (agent must fix before done)
|
||||
# 4. spawned_at — a new CI run was triggered after agent spawned (proves real work)
|
||||
# 5. Unresolved threads — checked AFTER CI so bot-posted comments are included
|
||||
# 6. CHANGES_REQUESTED — checked AFTER CI so bot reviews are included
|
||||
#
|
||||
# Usage: verify-complete.sh WINDOW
|
||||
# Exit 0 = verified complete; exit 1 = not complete (stderr has reason)
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
WINDOW="$1"
|
||||
STATE_FILE="${ORCHESTRATOR_STATE_FILE:-$HOME/.claude/orchestrator-state.json}"
|
||||
|
||||
PR_NUMBER=$(jq -r --arg w "$WINDOW" '.agents[] | select(.window == $w) | .pr_number // ""' "$STATE_FILE" 2>/dev/null)
|
||||
STEPS=$(jq -r --arg w "$WINDOW" '.agents[] | select(.window == $w) | .steps // [] | .[]' "$STATE_FILE" 2>/dev/null || true)
|
||||
CHECKPOINTS=$(jq -r --arg w "$WINDOW" '.agents[] | select(.window == $w) | .checkpoints // [] | .[]' "$STATE_FILE" 2>/dev/null || true)
|
||||
WORKTREE_PATH=$(jq -r --arg w "$WINDOW" '.agents[] | select(.window == $w) | .worktree_path // ""' "$STATE_FILE" 2>/dev/null)
|
||||
BRANCH=$(jq -r --arg w "$WINDOW" '.agents[] | select(.window == $w) | .branch // ""' "$STATE_FILE" 2>/dev/null)
|
||||
SPAWNED_AT=$(jq -r --arg w "$WINDOW" '.agents[] | select(.window == $w) | .spawned_at // "0"' "$STATE_FILE" 2>/dev/null || echo "0")
|
||||
|
||||
# No PR number = cannot verify
|
||||
if [ -z "$PR_NUMBER" ]; then
|
||||
echo "NOT COMPLETE: no pr_number in state — set pr_number or mark done manually" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --- Check 1: all required steps are checkpointed ---
|
||||
MISSING=""
|
||||
while IFS= read -r step; do
|
||||
[ -z "$step" ] && continue
|
||||
if ! echo "$CHECKPOINTS" | grep -qFx "$step"; then
|
||||
MISSING="$MISSING $step"
|
||||
fi
|
||||
done <<< "$STEPS"
|
||||
|
||||
if [ -n "$MISSING" ]; then
|
||||
echo "NOT COMPLETE: missing checkpoints:$MISSING on PR #$PR_NUMBER" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Resolve repo for all GitHub checks below
|
||||
REPO=$(jq -r '.repo // ""' "$STATE_FILE" 2>/dev/null || echo "")
|
||||
if [ -z "$REPO" ] && [ -n "$WORKTREE_PATH" ] && [ -d "$WORKTREE_PATH" ]; then
|
||||
REPO=$(git -C "$WORKTREE_PATH" remote get-url origin 2>/dev/null \
|
||||
| sed 's|.*github\.com[:/]||; s|\.git$||' || echo "")
|
||||
fi
|
||||
|
||||
if [ -z "$REPO" ]; then
|
||||
echo "Warning: cannot resolve repo — skipping CI/thread checks" >&2
|
||||
echo "VERIFIED: PR #$PR_NUMBER — checkpoints ✓ (CI/thread checks skipped — no repo)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
CI_BUCKETS=$(gh pr checks "$PR_NUMBER" --repo "$REPO" --json bucket 2>/dev/null || echo "[]")
|
||||
|
||||
# --- Check 2: CI fully complete — no pending checks ---
|
||||
# Pending checks MUST finish before we check threads/reviews:
|
||||
# bots (Seer, Check PR Status, etc.) post comments and CHANGES_REQUESTED AFTER their CI check runs.
|
||||
PENDING=$(echo "$CI_BUCKETS" | jq '[.[] | select(.bucket == "pending")] | length' 2>/dev/null || echo "0")
|
||||
if [ "$PENDING" -gt 0 ]; then
|
||||
PENDING_NAMES=$(gh pr checks "$PR_NUMBER" --repo "$REPO" --json bucket,name 2>/dev/null \
|
||||
| jq -r '[.[] | select(.bucket == "pending") | .name] | join(", ")' 2>/dev/null || echo "unknown")
|
||||
echo "NOT COMPLETE: $PENDING CI checks still pending on PR #$PR_NUMBER ($PENDING_NAMES)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --- Check 3: CI passing — no failures ---
|
||||
FAILING=$(echo "$CI_BUCKETS" | jq '[.[] | select(.bucket == "fail")] | length' 2>/dev/null || echo "0")
|
||||
if [ "$FAILING" -gt 0 ]; then
|
||||
FAILING_NAMES=$(gh pr checks "$PR_NUMBER" --repo "$REPO" --json bucket,name 2>/dev/null \
|
||||
| jq -r '[.[] | select(.bucket == "fail") | .name] | join(", ")' 2>/dev/null || echo "unknown")
|
||||
echo "NOT COMPLETE: $FAILING failing CI checks on PR #$PR_NUMBER ($FAILING_NAMES)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --- Check 4: a new CI run was triggered AFTER the agent spawned ---
|
||||
if [ -n "$BRANCH" ] && [ "${SPAWNED_AT:-0}" -gt 0 ]; then
|
||||
LATEST_RUN_AT=$(gh run list --repo "$REPO" --branch "$BRANCH" \
|
||||
--json createdAt --limit 1 2>/dev/null | jq -r '.[0].createdAt // ""')
|
||||
if [ -n "$LATEST_RUN_AT" ]; then
|
||||
if date --version >/dev/null 2>&1; then
|
||||
LATEST_RUN_EPOCH=$(date -d "$LATEST_RUN_AT" "+%s" 2>/dev/null || echo "0")
|
||||
else
|
||||
LATEST_RUN_EPOCH=$(TZ=UTC date -j -f "%Y-%m-%dT%H:%M:%SZ" "$LATEST_RUN_AT" "+%s" 2>/dev/null || echo "0")
|
||||
fi
|
||||
if [ "$LATEST_RUN_EPOCH" -le "$SPAWNED_AT" ]; then
|
||||
echo "NOT COMPLETE: latest CI run on $BRANCH predates agent spawn — agent may not have pushed yet" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
OWNER=$(echo "$REPO" | cut -d/ -f1)
|
||||
REPONAME=$(echo "$REPO" | cut -d/ -f2)
|
||||
|
||||
# --- Check 5: no unresolved review threads (checked AFTER CI — bots post after their check) ---
|
||||
UNRESOLVED=$(gh api graphql -f query="
|
||||
{ repository(owner: \"${OWNER}\", name: \"${REPONAME}\") {
|
||||
pullRequest(number: ${PR_NUMBER}) {
|
||||
reviewThreads(first: 50) { nodes { isResolved } }
|
||||
}
|
||||
}
|
||||
}
|
||||
" --jq '[.data.repository.pullRequest.reviewThreads.nodes[] | select(.isResolved == false)] | length' 2>/dev/null || echo "0")
|
||||
|
||||
if [ "$UNRESOLVED" -gt 0 ]; then
|
||||
echo "NOT COMPLETE: $UNRESOLVED unresolved review threads on PR #$PR_NUMBER" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --- Check 6: no CHANGES_REQUESTED (checked AFTER CI — bots post reviews after their check) ---
|
||||
# A CHANGES_REQUESTED review is stale if the latest commit was pushed AFTER the review was submitted.
|
||||
# Stale reviews (pre-dating the fixing commits) should not block verification.
|
||||
#
|
||||
# Fetch commits and latestReviews in a single call and fail closed — if gh fails,
|
||||
# treat that as NOT COMPLETE rather than silently passing.
|
||||
# Use latestReviews (not reviews) so each reviewer's latest state is used — superseded
|
||||
# CHANGES_REQUESTED entries are automatically excluded when the reviewer later approved.
|
||||
# Note: we intentionally use committedDate (not PR updatedAt) because updatedAt changes on any
|
||||
# PR activity (bot comments, label changes) which would create false negatives.
|
||||
PR_REVIEW_METADATA=$(gh pr view "$PR_NUMBER" --repo "$REPO" \
|
||||
--json commits,latestReviews 2>/dev/null) || {
|
||||
echo "NOT COMPLETE: unable to fetch PR review metadata for PR #$PR_NUMBER" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
LATEST_COMMIT_DATE=$(jq -r '.commits[-1].committedDate // ""' <<< "$PR_REVIEW_METADATA")
|
||||
CHANGES_REQUESTED_REVIEWS=$(jq '[.latestReviews[]? | select(.state == "CHANGES_REQUESTED")]' <<< "$PR_REVIEW_METADATA")
|
||||
|
||||
BLOCKING_CHANGES_REQUESTED=0
|
||||
BLOCKING_REQUESTERS=""
|
||||
|
||||
if [ -n "$LATEST_COMMIT_DATE" ] && [ "$(echo "$CHANGES_REQUESTED_REVIEWS" | jq length)" -gt 0 ]; then
|
||||
if date --version >/dev/null 2>&1; then
|
||||
LATEST_COMMIT_EPOCH=$(date -d "$LATEST_COMMIT_DATE" "+%s" 2>/dev/null || echo "0")
|
||||
else
|
||||
LATEST_COMMIT_EPOCH=$(TZ=UTC date -j -f "%Y-%m-%dT%H:%M:%SZ" "$LATEST_COMMIT_DATE" "+%s" 2>/dev/null || echo "0")
|
||||
fi
|
||||
|
||||
while IFS= read -r review; do
|
||||
[ -z "$review" ] && continue
|
||||
REVIEW_DATE=$(echo "$review" | jq -r '.submittedAt // ""')
|
||||
REVIEWER=$(echo "$review" | jq -r '.author.login // "unknown"')
|
||||
if [ -z "$REVIEW_DATE" ]; then
|
||||
# No submission date — treat as fresh (conservative: blocks verification)
|
||||
BLOCKING_CHANGES_REQUESTED=$(( BLOCKING_CHANGES_REQUESTED + 1 ))
|
||||
BLOCKING_REQUESTERS="${BLOCKING_REQUESTERS:+$BLOCKING_REQUESTERS, }${REVIEWER}"
|
||||
else
|
||||
if date --version >/dev/null 2>&1; then
|
||||
REVIEW_EPOCH=$(date -d "$REVIEW_DATE" "+%s" 2>/dev/null || echo "0")
|
||||
else
|
||||
REVIEW_EPOCH=$(TZ=UTC date -j -f "%Y-%m-%dT%H:%M:%SZ" "$REVIEW_DATE" "+%s" 2>/dev/null || echo "0")
|
||||
fi
|
||||
if [ "$REVIEW_EPOCH" -gt "$LATEST_COMMIT_EPOCH" ]; then
|
||||
# Review was submitted AFTER latest commit — still fresh, blocks verification
|
||||
BLOCKING_CHANGES_REQUESTED=$(( BLOCKING_CHANGES_REQUESTED + 1 ))
|
||||
BLOCKING_REQUESTERS="${BLOCKING_REQUESTERS:+$BLOCKING_REQUESTERS, }${REVIEWER}"
|
||||
fi
|
||||
# Review submitted BEFORE latest commit — stale, skip
|
||||
fi
|
||||
done <<< "$(echo "$CHANGES_REQUESTED_REVIEWS" | jq -c '.[]')"
|
||||
else
|
||||
# No commit date or no changes_requested — check raw count as fallback
|
||||
BLOCKING_CHANGES_REQUESTED=$(echo "$CHANGES_REQUESTED_REVIEWS" | jq length 2>/dev/null || echo "0")
|
||||
BLOCKING_REQUESTERS=$(echo "$CHANGES_REQUESTED_REVIEWS" | jq -r '[.[].author.login] | join(", ")' 2>/dev/null || echo "unknown")
|
||||
fi
|
||||
|
||||
if [ "$BLOCKING_CHANGES_REQUESTED" -gt 0 ]; then
|
||||
echo "NOT COMPLETE: CHANGES_REQUESTED (after latest commit) from ${BLOCKING_REQUESTERS} on PR #$PR_NUMBER" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "VERIFIED: PR #$PR_NUMBER — checkpoints ✓, CI complete + green, 0 unresolved threads, no CHANGES_REQUESTED"
|
||||
exit 0
|
||||
@@ -25,102 +25,43 @@ Understand the **Why / What / How** before addressing comments — you need cont
|
||||
gh pr view {N} --json body --jq '.body'
|
||||
```
|
||||
|
||||
> If GraphQL is rate-limited, `gh pr view` fails. See [GitHub rate limits](#github-rate-limits) for REST fallbacks.
|
||||
|
||||
## Fetch comments (all sources)
|
||||
|
||||
### 1. Inline review threads — GraphQL (primary source of actionable items)
|
||||
|
||||
> ⚠️ **WARNING — PAGINATE ALL PAGES BEFORE ADDRESSING ANYTHING**
|
||||
>
|
||||
> `reviewThreads(first: 100)` returns at most 100 threads per page AND returns threads **oldest-first**. On a PR with many review cycles (e.g. 373 threads), the oldest 100–200 threads are from past cycles and are **all already resolved**. Filtering client-side with `select(.isResolved == false)` on page 1 therefore yields **0 results** — even though pages 2–4 contain many unresolved threads from recent review cycles.
|
||||
>
|
||||
> **This is the most common failure mode:** agent fetches page 1, sees 0 unresolved after filtering, stops pagination, reports "done" — while hundreds of unresolved threads sit on later pages.
|
||||
>
|
||||
> One observed PR had 142 total threads: page 1 returned 0 unresolved (all old/resolved), while pages 2–3 had 111 unresolved. Another with 373 threads across 4 pages also had page 1 entirely resolved.
|
||||
>
|
||||
> **The rule: ALWAYS paginate to `hasNextPage == false` regardless of the per-page unresolved count. Never stop early because a page returns 0 unresolved.**
|
||||
|
||||
**Step 1 — Fetch total count and sanity-check the newest threads:**
|
||||
Use GraphQL to fetch inline threads. It natively exposes `isResolved`, returns threads already grouped with all replies, and paginates via cursor — no manual thread reconstruction needed.
|
||||
|
||||
```bash
|
||||
# Get total count and the newest 100 threads (last: 100 returns newest-first)
|
||||
gh api graphql -f query='
|
||||
{
|
||||
repository(owner: "Significant-Gravitas", name: "AutoGPT") {
|
||||
pullRequest(number: {N}) {
|
||||
reviewThreads { totalCount }
|
||||
newest: reviewThreads(last: 100) {
|
||||
nodes { isResolved }
|
||||
}
|
||||
}
|
||||
}
|
||||
}' | jq '{ total: .data.repository.pullRequest.reviewThreads.totalCount, newest_unresolved: [.data.repository.pullRequest.newest.nodes[] | select(.isResolved == false)] | length }'
|
||||
```
|
||||
|
||||
If `total > 100`, you have multiple pages — you **must** paginate all of them regardless of what `newest_unresolved` shows. The `last: 100` check is a sanity signal only; the full loop below is mandatory.
|
||||
|
||||
**Step 2 — Collect all unresolved thread IDs across all pages:**
|
||||
|
||||
```bash
|
||||
# Accumulate all unresolved threads — loop until hasNextPage == false
|
||||
CURSOR=""
|
||||
ALL_THREADS="[]"
|
||||
while true; do
|
||||
AFTER=${CURSOR:+", after: \"$CURSOR\""}
|
||||
PAGE=$(gh api graphql -f query="
|
||||
{
|
||||
repository(owner: \"Significant-Gravitas\", name: \"AutoGPT\") {
|
||||
pullRequest(number: {N}) {
|
||||
reviewThreads(first: 100${AFTER}) {
|
||||
pageInfo { hasNextPage endCursor }
|
||||
nodes {
|
||||
id
|
||||
isResolved
|
||||
path
|
||||
line
|
||||
comments(last: 1) {
|
||||
nodes { databaseId body author { login } }
|
||||
}
|
||||
reviewThreads(first: 100) {
|
||||
pageInfo { hasNextPage endCursor }
|
||||
nodes {
|
||||
id
|
||||
isResolved
|
||||
path
|
||||
comments(last: 1) {
|
||||
nodes { databaseId body author { login } createdAt }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}")
|
||||
# Append unresolved nodes from this page
|
||||
PAGE_THREADS=$(echo "$PAGE" | jq '[.data.repository.pullRequest.reviewThreads.nodes[] | select(.isResolved == false)]')
|
||||
ALL_THREADS=$(echo "$ALL_THREADS $PAGE_THREADS" | jq -s 'add')
|
||||
HAS_NEXT=$(echo "$PAGE" | jq -r '.data.repository.pullRequest.reviewThreads.pageInfo.hasNextPage')
|
||||
CURSOR=$(echo "$PAGE" | jq -r '.data.repository.pullRequest.reviewThreads.pageInfo.endCursor')
|
||||
[ "$HAS_NEXT" = "false" ] && break
|
||||
done
|
||||
|
||||
# Reverse so newest threads (last pages) are addressed first — GitHub returns oldest-first
|
||||
# and the most recent review cycle's comments are the ones blocking approval.
|
||||
ALL_THREADS=$(echo "$ALL_THREADS" | jq 'reverse')
|
||||
|
||||
echo "Total unresolved threads: $(echo "$ALL_THREADS" | jq 'length')"
|
||||
echo "$ALL_THREADS" | jq '[.[] | {id, path, line, body: .comments.nodes[0].body[:200]}]'
|
||||
}
|
||||
}'
|
||||
```
|
||||
|
||||
**Step 3 — Address every thread in `ALL_THREADS`, then resolve.**
|
||||
|
||||
Only after this loop completes (all pages fetched, count confirmed) should you begin making fixes.
|
||||
|
||||
> **Why reverse?** GraphQL returns threads oldest-first and exposes no `orderBy` option. A PR with 373 threads has ~4 pages; threads from the latest review cycle land on the last pages. Processing in reverse ensures the newest, most blocking comments are addressed first — the earlier pages mostly contain outdated threads from prior cycles.
|
||||
If `pageInfo.hasNextPage` is true, fetch subsequent pages by adding `after: "<endCursor>"` to `reviewThreads(first: 100, after: "...")` and repeat until `hasNextPage` is false.
|
||||
|
||||
**Filter to unresolved threads only** — skip any thread where `isResolved: true`. `comments(last: 1)` returns the most recent comment in the thread — act on that; it reflects the reviewer's final ask. Use the thread `id` (Relay global ID) to track threads across polls.
|
||||
|
||||
> If GraphQL is rate-limited, see [GitHub rate limits](#github-rate-limits) for the REST fallback (flat comment list — no thread grouping or `isResolved`).
|
||||
|
||||
### 2. Top-level reviews — REST (MUST paginate)
|
||||
|
||||
```bash
|
||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/reviews --paginate
|
||||
```
|
||||
|
||||
> **Already REST — unaffected by GraphQL rate limits or outages. Continue polling reviews normally even when GraphQL is exhausted.**
|
||||
|
||||
**CRITICAL — always `--paginate`.** Reviews default to 30 per page. PRs can have 80–170+ reviews (mostly empty resolution events). Without pagination you miss reviews past position 30 — including `autogpt-reviewer`'s structured review which is typically posted after several CI runs and sits well beyond the first page.
|
||||
|
||||
Two things to extract:
|
||||
@@ -139,49 +80,20 @@ Two things to extract:
|
||||
gh api repos/Significant-Gravitas/AutoGPT/issues/{N}/comments --paginate
|
||||
```
|
||||
|
||||
> **Already REST — unaffected by GraphQL rate limits.**
|
||||
|
||||
Mostly contains: bot summaries (`coderabbitai[bot]`), CI/conflict detection (`github-actions[bot]`), and author status updates. Scan for non-empty messages from non-bot human reviewers that aren't the PR author — those are the ones that need a response.
|
||||
|
||||
## For each unaddressed comment
|
||||
|
||||
**CRITICAL: The only valid sequence is fix → commit → push → reply → resolve. Never resolve a thread without a real code commit.**
|
||||
|
||||
Resolving a thread via `resolveReviewThread` without an actual fix is the most common failure mode — it makes unresolved counts drop without any real change, producing a false "done" signal. If the issue was genuinely a false positive (no code change needed), reply explaining why and then resolve. Otherwise:
|
||||
|
||||
Address comments **one at a time**: fix → commit → push → inline reply → resolve.
|
||||
Address comments **one at a time**: fix → commit → push → inline reply → next.
|
||||
|
||||
1. Read the referenced code, make the fix (or reply explaining why it's not needed)
|
||||
2. Commit and push the fix
|
||||
3. Reply **inline** (not as a new top-level comment) referencing the fixing commit — this is what resolves the conversation for bot reviewers (coderabbitai, sentry):
|
||||
|
||||
Use a **markdown commit link** so GitHub renders it as a clickable reference. Always get the full SHA with `git rev-parse HEAD` **after** committing — never copy a SHA from a previous commit or hardcode one:
|
||||
|
||||
```bash
|
||||
FULL_SHA=$(git rev-parse HEAD)
|
||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments/{ID}/replies \
|
||||
-f body="🤖 Fixed in [${FULL_SHA:0:9}](https://github.com/Significant-Gravitas/AutoGPT/commit/${FULL_SHA}): <description>"
|
||||
```
|
||||
|
||||
| Comment type | How to reply |
|
||||
|---|---|
|
||||
| Inline review (`pulls/{N}/comments`) | `gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments/{ID}/replies -f body="🤖 Fixed in [abc1234](https://github.com/Significant-Gravitas/AutoGPT/commit/FULL_SHA): <description>"` |
|
||||
| Conversation (`issues/{N}/comments`) | `gh api repos/Significant-Gravitas/AutoGPT/issues/{N}/comments -f body="🤖 Fixed in [abc1234](https://github.com/Significant-Gravitas/AutoGPT/commit/FULL_SHA): <description>"` |
|
||||
|
||||
### What counts as a valid resolution
|
||||
|
||||
Only two situations justify calling `resolveReviewThread`:
|
||||
|
||||
1. **Real code fix**: you changed the code, committed + pushed, and replied with the SHA. The commit diff must actually address the concern — not just touch the same file.
|
||||
2. **Genuine false positive**: the reviewer's concern does not apply to this code, and you can give a specific technical reason (e.g. "Not applicable — `sdk_cwd` is pre-validated by `_make_sdk_cwd()` which applies normpath + prefix assertion before reaching this point").
|
||||
|
||||
**Anti-patterns that look resolved but aren't — never do these:**
|
||||
- `"Accepted, tracked as follow-up"` — a deferral, not a fix. The concern is still open. Do not resolve.
|
||||
- `"Acknowledged"` or `"Same as above"` — these are acknowledgements, not fixes. Do not resolve.
|
||||
- `"Fixed in abc1234"` where `abc1234` is a commit that doesn't actually change the flagged line/logic — dishonest. Verify `git show abc1234 -- path/to/file` changes the right thing before posting.
|
||||
- Resolving without replying — the reviewer never sees what happened.
|
||||
|
||||
When in doubt: if a code change is needed, make it. A deferred issue means the thread stays open until the follow-up PR is merged.
|
||||
| Inline review (`pulls/{N}/comments`) | `gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments/{ID}/replies -f body="🤖 Fixed in <commit-sha>: <description>"` |
|
||||
| Conversation (`issues/{N}/comments`) | `gh api repos/Significant-Gravitas/AutoGPT/issues/{N}/comments -f body="🤖 Fixed in <commit-sha>: <description>"` |
|
||||
|
||||
## Codecov coverage
|
||||
|
||||
@@ -229,22 +141,6 @@ Then commit and **push immediately** — never batch commits without pushing. Ea
|
||||
|
||||
For backend commits in worktrees: `poetry run git commit` (pre-commit hooks).
|
||||
|
||||
## Coverage
|
||||
|
||||
Codecov enforces patch coverage on new/changed lines — new code you write must be tested. Before pushing, verify you haven't left new lines uncovered:
|
||||
|
||||
```bash
|
||||
cd autogpt_platform/backend
|
||||
poetry run pytest --cov=. --cov-report=term-missing {path/to/changed/module}
|
||||
```
|
||||
|
||||
Look for lines marked `miss` — those are uncovered. Add tests for any new code you wrote as part of addressing comments.
|
||||
|
||||
**Rules:**
|
||||
- New code you add should have tests
|
||||
- Don't remove existing tests when fixing comments
|
||||
- If a reviewer asks you to delete code, also delete its tests, but verify coverage hasn't dropped on remaining lines
|
||||
|
||||
## The loop
|
||||
|
||||
```text
|
||||
@@ -334,162 +230,3 @@ git push
|
||||
```
|
||||
|
||||
5. Restart the polling loop from the top — new commits reset CI status.
|
||||
|
||||
## GitHub rate limits
|
||||
|
||||
Three distinct rate limits exist — they have different causes, error shapes, and recovery times:
|
||||
|
||||
| Error | HTTP code | Cause | Recovery |
|
||||
|---|---|---|---|
|
||||
| `{"code":"abuse"}` | 403 | Secondary rate limit — too many write operations (comments, mutations) in a short window | Wait **2–3 minutes**. 60s is often not enough. |
|
||||
| `{"message":"API rate limit exceeded"}` | 429 | Primary REST rate limit — 5000 calls/hr per user | Wait until `X-RateLimit-Reset` header timestamp |
|
||||
| `GraphQL: API rate limit already exceeded for user ID ...` | 403 on stderr, `gh` exits 1 | **GraphQL-specific** per-user limit — distinct from REST's 5000/hr and from the abuse secondary limit. Trips faster than REST because point costs per query. | Wait until the GraphQL window resets (typically ~1 hour from the first call in the window). REST still works — use fallbacks below. |
|
||||
|
||||
**Prevention:** Add `sleep 3` between individual thread reply API calls. When posting >20 replies, increase to `sleep 5`.
|
||||
|
||||
### Detection
|
||||
|
||||
The `gh` CLI surfaces the GraphQL limit on stderr with the exact string `GraphQL: API rate limit already exceeded for user ID <id>` and exits 1 — any `gh api graphql ...` **or** `gh pr view ...` call fails. Check current quota and reset time via the REST endpoint that reports GraphQL quota (this call is REST and still works whether GraphQL is rate-limited OR fully down):
|
||||
|
||||
```bash
|
||||
gh api rate_limit --jq '.resources.graphql' # { "limit": 5000, "used": 5000, "remaining": 0, "reset": 1729...}
|
||||
# Human-readable reset:
|
||||
gh api rate_limit --jq '.resources.graphql.reset' | xargs -I{} date -r {}
|
||||
```
|
||||
|
||||
Retry when `remaining > 0`. If you need to proceed sooner, sleep 2–5 min and probe again — the limit is per user, not per machine, so other concurrent agents under the same token also consume it.
|
||||
|
||||
### What keeps working
|
||||
|
||||
When GraphQL is unavailable (rate-limited or outage):
|
||||
|
||||
- **Keeps working (REST):** top-level reviews fetch, conversation comments fetch, all inline-comment replies, CI status (`gh pr checks`), and the `gh api rate_limit` probe.
|
||||
- **Degraded:** inline thread list — fall back to flat `/pulls/{N}/comments` REST, which drops thread grouping, `isResolved`, and Relay thread IDs. You still get comment bodies and the `databaseId` as `id`, enough to read and reply.
|
||||
- **Blocked:** `gh pr view`, the `resolveReviewThread` mutation, and any new `gh api graphql` queries — wait for the quota to reset.
|
||||
|
||||
### Fall back to REST
|
||||
|
||||
**PR metadata reads** — `gh pr view` uses GraphQL under the hood; use the REST pulls endpoint instead, which returns the full PR object:
|
||||
|
||||
```bash
|
||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N} --jq '.body' # == --json body
|
||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N} --jq '.base.ref' # == --json baseRefName
|
||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N} --jq '.mergeable' # == --json mergeable
|
||||
```
|
||||
|
||||
Note: REST `mergeable` returns `true|false|null`; GraphQL returns `MERGEABLE|CONFLICTING|UNKNOWN`. The `null` case maps to `UNKNOWN` — treat it the same (still computing; poll again).
|
||||
|
||||
**Inline comments (flat list)** — no thread grouping or `isResolved`, but enough to read and reply:
|
||||
|
||||
```bash
|
||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments --paginate \
|
||||
| jq '[.[] | {id, path, line, user: .user.login, body: .body[:200], in_reply_to_id}]'
|
||||
```
|
||||
|
||||
Use this degraded mode to make progress on the fix → reply loop, then return to GraphQL for `resolveReviewThread` once the rate limit resets.
|
||||
|
||||
**Replies** — already REST-native (`/pulls/{N}/comments/{ID}/replies`); no change needed, use the same command as the main flow.
|
||||
|
||||
**`resolveReviewThread`** — **no REST equivalent**; GitHub does not expose a REST endpoint for thread resolution. Queue the thread IDs needing resolution, wait for the GraphQL limit to reset, then run the resolve mutations in a batch (with `sleep 3` between calls, per the secondary-limit guidance).
|
||||
|
||||
### Recovery from secondary rate limit (403 abuse)
|
||||
|
||||
1. Stop all API writes immediately
|
||||
2. Wait **2 minutes minimum** (not 60s — secondary limits are stricter)
|
||||
3. Resume with `sleep 3` between each call
|
||||
4. If 403 persists after 2 min, wait another 2 min before retrying
|
||||
|
||||
Never batch all replies in a tight loop — always space them out.
|
||||
|
||||
## Parallel thread resolution
|
||||
|
||||
When a PR has more than 10 unresolved threads, addressing one commit per thread is slow. Use this strategy instead:
|
||||
|
||||
### Group by file, batch per commit
|
||||
|
||||
1. Sort `ALL_THREADS` by `path` — threads in the same file can share a single commit.
|
||||
2. Fix all threads in one file → `git commit` → `git push` → reply to **all** those threads with the same SHA → resolve them all.
|
||||
3. Move to the next file group and repeat.
|
||||
|
||||
This reduces N commits to (number of files touched), which is usually 3–5 instead of 15–30.
|
||||
|
||||
### Posting replies concurrently (for large batches)
|
||||
|
||||
For truly independent thread groups (different files, no shared logic), you can post replies in parallel using background subshells — but always space out API writes:
|
||||
|
||||
```bash
|
||||
# Post replies to a batch of threads concurrently, 3s apart
|
||||
(
|
||||
sleep 3
|
||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments/{ID1}/replies \
|
||||
-f body="🤖 Fixed in [${FULL_SHA:0:9}](https://github.com/Significant-Gravitas/AutoGPT/commit/${FULL_SHA}): ..."
|
||||
) &
|
||||
(
|
||||
sleep 6
|
||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments/{ID2}/replies \
|
||||
-f body="🤖 Fixed in [${FULL_SHA:0:9}](https://github.com/Significant-Gravitas/AutoGPT/commit/${FULL_SHA}): ..."
|
||||
) &
|
||||
wait # wait for all background replies before resolving
|
||||
```
|
||||
|
||||
Then resolve sequentially (GraphQL mutations):
|
||||
```bash
|
||||
for THREAD_ID in "$THREAD1" "$THREAD2" "$THREAD3"; do
|
||||
gh api graphql -f query="mutation { resolveReviewThread(input: {threadId: \"${THREAD_ID}\"}) { thread { isResolved } } }"
|
||||
sleep 3
|
||||
done
|
||||
```
|
||||
|
||||
**Always sleep 3s between individual API writes** — GitHub's secondary rate limit (403) triggers on bursts of >20 writes. Increase to `sleep 5` when posting more than 20 replies in a batch.
|
||||
|
||||
## Resolving threads via GraphQL
|
||||
|
||||
Use `resolveReviewThread` **only after** the commit is pushed and the reply is posted:
|
||||
|
||||
```bash
|
||||
gh api graphql -f query='mutation { resolveReviewThread(input: {threadId: "THREAD_ID"}) { thread { isResolved } } }'
|
||||
```
|
||||
|
||||
**Never call this mutation before committing the fix.** The orchestrator will verify actual unresolved counts via GraphQL after you output `ORCHESTRATOR:DONE` — false resolutions will be caught and you will be re-briefed.
|
||||
|
||||
> `resolveReviewThread` is GraphQL-only — no REST equivalent. If GraphQL is rate-limited, see [GitHub rate limits](#github-rate-limits) for the queue-and-retry flow.
|
||||
|
||||
### Verify actual count before outputting ORCHESTRATOR:DONE
|
||||
|
||||
Before claiming "0 unresolved threads", always query GitHub directly — don't rely on your own bookkeeping. Paginate all pages — a single `first: 100` query misses threads beyond page 1:
|
||||
|
||||
```bash
|
||||
# Step 1: get total thread count
|
||||
gh api graphql -f query='
|
||||
{
|
||||
repository(owner: "Significant-Gravitas", name: "AutoGPT") {
|
||||
pullRequest(number: {N}) {
|
||||
reviewThreads { totalCount }
|
||||
}
|
||||
}
|
||||
}' | jq '.data.repository.pullRequest.reviewThreads.totalCount'
|
||||
|
||||
# Step 2: paginate all pages, count truly unresolved
|
||||
CURSOR=""; UNRESOLVED=0
|
||||
while true; do
|
||||
AFTER=${CURSOR:+", after: \"$CURSOR\""}
|
||||
PAGE=$(gh api graphql -f query="
|
||||
{
|
||||
repository(owner: \"Significant-Gravitas\", name: \"AutoGPT\") {
|
||||
pullRequest(number: {N}) {
|
||||
reviewThreads(first: 100${AFTER}) {
|
||||
pageInfo { hasNextPage endCursor }
|
||||
nodes { isResolved }
|
||||
}
|
||||
}
|
||||
}
|
||||
}")
|
||||
UNRESOLVED=$(( UNRESOLVED + $(echo "$PAGE" | jq '[.data.repository.pullRequest.reviewThreads.nodes[] | select(.isResolved==false)] | length') ))
|
||||
HAS_NEXT=$(echo "$PAGE" | jq -r '.data.repository.pullRequest.reviewThreads.pageInfo.hasNextPage')
|
||||
CURSOR=$(echo "$PAGE" | jq -r '.data.repository.pullRequest.reviewThreads.pageInfo.endCursor')
|
||||
[ "$HAS_NEXT" = "false" ] && break
|
||||
done
|
||||
echo "Unresolved threads: $UNRESOLVED"
|
||||
```
|
||||
|
||||
Only output `ORCHESTRATOR:DONE` after this loop reports 0.
|
||||
|
||||
@@ -5,7 +5,7 @@ user-invocable: true
|
||||
argument-hint: "[worktree path or PR number] — tests the PR in the given worktree. Optional flags: --fix (auto-fix issues found)"
|
||||
metadata:
|
||||
author: autogpt-team
|
||||
version: "2.1.0"
|
||||
version: "2.0.0"
|
||||
---
|
||||
|
||||
# Manual E2E Test
|
||||
@@ -180,94 +180,6 @@ Based on the PR analysis, write a test plan to `$RESULTS_DIR/test-plan.md`:
|
||||
|
||||
**Be critical** — include edge cases, error paths, and security checks. Every scenario MUST specify what screenshots to take and what state to verify.
|
||||
|
||||
## Step 3.0: Claim the testing lock (coordinate parallel agents)
|
||||
|
||||
Multiple worktrees share the same host — Docker infra (postgres, redis, clamav), app ports (3000/8006/…), and the test user. Two agents running `/pr-test` concurrently will corrupt each other's state (connection-pool exhaustion, port binds failing silently, cross-test assertions). Use the root-worktree lock file to take turns.
|
||||
|
||||
### Lock file contract
|
||||
|
||||
Path (**always** the root worktree so all siblings see it): `/Users/majdyz/Code/AutoGPT/.ign.testing.lock`
|
||||
|
||||
Body (one `key=value` per line):
|
||||
```
|
||||
holder=<pr-XXXXX-purpose>
|
||||
pid=<pid-or-"self">
|
||||
started=<iso8601>
|
||||
heartbeat=<iso8601, updated every ~2 min>
|
||||
worktree=<full path>
|
||||
branch=<branch name>
|
||||
intent=<one-line description + rough duration>
|
||||
```
|
||||
|
||||
### Claim
|
||||
|
||||
```bash
|
||||
LOCK=/Users/majdyz/Code/AutoGPT/.ign.testing.lock
|
||||
NOW=$(date -u +%Y-%m-%dT%H:%MZ)
|
||||
STALE_AFTER_MIN=5
|
||||
|
||||
if [ -f "$LOCK" ]; then
|
||||
HB=$(grep '^heartbeat=' "$LOCK" | cut -d= -f2)
|
||||
HB_EPOCH=$(date -j -f '%Y-%m-%dT%H:%MZ' "$HB" +%s 2>/dev/null || date -d "$HB" +%s 2>/dev/null || echo 0)
|
||||
AGE_MIN=$(( ( $(date -u +%s) - HB_EPOCH ) / 60 ))
|
||||
if [ "$AGE_MIN" -gt "$STALE_AFTER_MIN" ]; then
|
||||
echo "WARN: stale lock (${AGE_MIN}m old) — reclaiming"
|
||||
cat "$LOCK" | sed 's/^/ stale: /'
|
||||
else
|
||||
echo "Another agent holds the lock:"; cat "$LOCK"
|
||||
echo "Wait until released or resume after $((STALE_AFTER_MIN - AGE_MIN))m."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
cat > "$LOCK" <<EOF
|
||||
holder=pr-${PR_NUMBER}-e2e
|
||||
pid=self
|
||||
started=$NOW
|
||||
heartbeat=$NOW
|
||||
worktree=$WORKTREE_PATH
|
||||
branch=$(cd $WORKTREE_PATH && git branch --show-current)
|
||||
intent=E2E test PR #${PR_NUMBER}, native mode, ~60min
|
||||
EOF
|
||||
echo "Lock claimed"
|
||||
```
|
||||
|
||||
### Heartbeat (MUST run in background during the whole test)
|
||||
|
||||
Without a heartbeat a crashed agent keeps the lock forever. Run this as a background process right after claim:
|
||||
|
||||
```bash
|
||||
(while true; do
|
||||
sleep 120
|
||||
[ -f "$LOCK" ] || exit 0 # lock released → exit heartbeat
|
||||
perl -i -pe "s/^heartbeat=.*/heartbeat=$(date -u +%Y-%m-%dT%H:%MZ)/" "$LOCK"
|
||||
done) &
|
||||
HEARTBEAT_PID=$!
|
||||
echo "$HEARTBEAT_PID" > /tmp/pr-test-heartbeat.pid
|
||||
```
|
||||
|
||||
### Release (always — even on failure)
|
||||
|
||||
```bash
|
||||
kill "$HEARTBEAT_PID" 2>/dev/null
|
||||
rm -f "$LOCK" /tmp/pr-test-heartbeat.pid
|
||||
echo "$(date -u +%Y-%m-%dT%H:%MZ) [pr-${PR_NUMBER}] released lock" \
|
||||
>> /Users/majdyz/Code/AutoGPT/.ign.testing.log
|
||||
```
|
||||
|
||||
Use a `trap` so release runs even on `exit 1`:
|
||||
```bash
|
||||
trap 'kill "$HEARTBEAT_PID" 2>/dev/null; rm -f "$LOCK"' EXIT INT TERM
|
||||
```
|
||||
|
||||
### Shared status log
|
||||
|
||||
`/Users/majdyz/Code/AutoGPT/.ign.testing.log` is an append-only channel any agent can read/write. Use it for "I'm waiting", "I'm done, resources free", or post-run notes:
|
||||
```bash
|
||||
echo "$(date -u +%Y-%m-%dT%H:%MZ) [pr-${PR_NUMBER}] <message>" \
|
||||
>> /Users/majdyz/Code/AutoGPT/.ign.testing.log
|
||||
```
|
||||
|
||||
## Step 3: Environment setup
|
||||
|
||||
### 3a. Copy .env files from the root worktree
|
||||
@@ -336,87 +248,7 @@ docker ps --format "{{.Names}}" | grep -E "rest_server|executor|copilot|websocke
|
||||
done
|
||||
```
|
||||
|
||||
**Native mode also:** when running the app natively (see 3e-native), kill any stray host processes and free the app ports before starting — otherwise `poetry run app` and `pnpm dev` will fail to bind.
|
||||
|
||||
```bash
|
||||
# Kill stray native app processes from prior runs
|
||||
pkill -9 -f "python.*backend" 2>/dev/null || true
|
||||
pkill -9 -f "poetry run app" 2>/dev/null || true
|
||||
pkill -9 -f "next-server|next dev" 2>/dev/null || true
|
||||
|
||||
# Free app ports (errors per port are ignored — port may simply be unused)
|
||||
for port in 3000 8006 8001 8002 8005 8008; do
|
||||
lsof -ti :$port -sTCP:LISTEN | xargs -r kill -9 2>/dev/null || true
|
||||
done
|
||||
```
|
||||
|
||||
### 3e-native. Run the app natively (PREFERRED for iterative dev)
|
||||
|
||||
Native mode runs infra (postgres, supabase, redis, rabbitmq, clamav) in docker but runs the backend and frontend directly on the host. This avoids the 3-8 minute `docker compose build` cycle on every backend change — code edits are picked up on process restart (seconds) instead of a full image rebuild.
|
||||
|
||||
**When to prefer native mode (default for this skill):**
|
||||
- Iterative dev/debug loops where you're editing backend or frontend code between test runs
|
||||
- Any PR that touches Python/TS source but not Dockerfiles, compose config, or infra images
|
||||
- Fast repro of a failing scenario — restart `poetry run app` in a couple of seconds
|
||||
|
||||
**When to prefer docker mode (3e fallback):**
|
||||
- Testing changes to `Dockerfile`, `docker-compose.yml`, or base images
|
||||
- Production-parity smoke tests (exact container env, networking, volumes)
|
||||
- CI-equivalent runs where you need the exact image that'll ship
|
||||
|
||||
**Note on 3b (copilot auth):** no npm install anywhere. `poetry install` pulls in `claude_agent_sdk`, which ships its own Claude CLI binary — available on `PATH` whenever you run commands via `poetry run` (native) OR whenever the copilot_executor container is built from its Poetry lockfile (docker). The OAuth token extraction still applies (same `refresh_claude_token.sh` call).
|
||||
|
||||
**Preamble:** before starting native, run the kill-stray + free-ports block from 3c's "Native mode also" subsection.
|
||||
|
||||
**1. Start infra only (one-time per session):**
|
||||
|
||||
```bash
|
||||
cd $PLATFORM_DIR && docker compose --profile local up deps --detach --remove-orphans --build
|
||||
```
|
||||
|
||||
This brings up postgres/supabase/redis/rabbitmq/clamav and skips all app services.
|
||||
|
||||
**2. Start the backend natively:**
|
||||
|
||||
```bash
|
||||
cd $BACKEND_DIR && (poetry run app 2>&1 | tee .ign.application.logs) &
|
||||
```
|
||||
|
||||
`poetry run app` spawns **all** app subprocesses — `rest_server`, `executor`, `copilot_executor`, `websocket`, `scheduler`, `notification_server`, `database_manager` — inside ONE parent process. No separate containers, no separate terminals. The `.ign.application.logs` prefix is already gitignored.
|
||||
|
||||
**3. Wait for the backend on :8006 BEFORE starting the frontend.** This ordering matters — the frontend's `pnpm dev` startup invokes `generate-api-queries`, which fetches `/openapi.json` from the backend. If the backend isn't listening yet, `pnpm dev` fails immediately.
|
||||
|
||||
```bash
|
||||
for i in $(seq 1 60); do
|
||||
if [ "$(curl -s -o /dev/null -w '%{http_code}' http://localhost:8006/docs 2>/dev/null)" = "200" ]; then
|
||||
echo "Backend ready"
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
```
|
||||
|
||||
**4. Start the frontend natively:**
|
||||
|
||||
```bash
|
||||
cd $FRONTEND_DIR && (pnpm dev 2>&1 | tee .ign.frontend.logs) &
|
||||
```
|
||||
|
||||
**5. Wait for the frontend on :3000:**
|
||||
|
||||
```bash
|
||||
for i in $(seq 1 60); do
|
||||
if [ "$(curl -s -o /dev/null -w '%{http_code}' http://localhost:3000 2>/dev/null)" = "200" ]; then
|
||||
echo "Frontend ready"
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
```
|
||||
|
||||
Once both are up, skip 3e/3f and go straight to **3g/3h** (feature flags / test user creation).
|
||||
|
||||
### 3e. Build and start (docker — fallback)
|
||||
### 3e. Build and start
|
||||
|
||||
```bash
|
||||
cd $PLATFORM_DIR && docker compose build --no-cache 2>&1 | tail -20
|
||||
@@ -478,28 +310,6 @@ TOKEN=$(curl -s -X POST 'http://localhost:8000/auth/v1/token?grant_type=password
|
||||
curl -H "Authorization: Bearer $TOKEN" http://localhost:8006/api/...
|
||||
```
|
||||
|
||||
### 3i. Disable onboarding for test user
|
||||
|
||||
The frontend redirects to `/onboarding` when the `VISIT_COPILOT` step is not in `completedSteps`.
|
||||
Mark it complete via the backend API so every browser test lands on the real feature UI:
|
||||
|
||||
```bash
|
||||
ONBOARDING_RESULT=$(curl -s --max-time 30 -X POST \
|
||||
"http://localhost:8006/api/onboarding/step?step=VISIT_COPILOT" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
echo "Onboarding bypass: $ONBOARDING_RESULT"
|
||||
|
||||
# Verify it took effect
|
||||
ONBOARDING_STATUS=$(curl -s --max-time 30 \
|
||||
"http://localhost:8006/api/onboarding/completed" \
|
||||
-H "Authorization: Bearer $TOKEN" | jq -r '.is_completed')
|
||||
echo "Onboarding completed: $ONBOARDING_STATUS"
|
||||
if [ "$ONBOARDING_STATUS" != "true" ]; then
|
||||
echo "ERROR: onboarding bypass failed — browser tests will hit /onboarding instead of the target feature. Investigate before proceeding."
|
||||
exit 1
|
||||
fi
|
||||
```
|
||||
|
||||
## Step 4: Run tests
|
||||
|
||||
### Service ports reference
|
||||
@@ -610,22 +420,6 @@ agent-browser --session-name pr-test snapshot | grep "text:"
|
||||
|
||||
### Checking logs
|
||||
|
||||
**Native mode:** when running via `poetry run app` + `pnpm dev`, all app logs stream to the `.ign.*.logs` files written by the `tee` pipes in 3e-native. `rest_server`, `executor`, `copilot_executor`, `websocket`, `scheduler`, `notification_server`, and `database_manager` are all subprocesses of the single `poetry run app` parent, so their output is interleaved in `.ign.application.logs`.
|
||||
|
||||
```bash
|
||||
# Backend (all app subprocesses interleaved)
|
||||
tail -f $BACKEND_DIR/.ign.application.logs
|
||||
|
||||
# Frontend (Next.js dev server)
|
||||
tail -f $FRONTEND_DIR/.ign.frontend.logs
|
||||
|
||||
# Filter for errors across either log
|
||||
grep -iE "error|exception|traceback" $BACKEND_DIR/.ign.application.logs | tail -20
|
||||
grep -iE "error|exception|traceback" $FRONTEND_DIR/.ign.frontend.logs | tail -20
|
||||
```
|
||||
|
||||
**Docker mode:**
|
||||
|
||||
```bash
|
||||
# Backend REST server
|
||||
docker logs autogpt_platform-rest_server-1 2>&1 | tail -30
|
||||
@@ -753,8 +547,6 @@ Upload screenshots to the PR using the GitHub Git API (no local git operations
|
||||
|
||||
**This step is MANDATORY. Every test run MUST post a PR comment with screenshots. No exceptions.**
|
||||
|
||||
**CRITICAL — NEVER post a bare directory link like `https://github.com/.../tree/...`.** Every screenshot MUST appear as `` inline in the PR comment so reviewers can see them without clicking any links. After posting, the verification step below greps the comment for `![` tags and exits 1 if none are found — the test run is considered incomplete until this passes.
|
||||
|
||||
```bash
|
||||
# Upload screenshots via GitHub Git API (creates blobs, tree, commit, and ref remotely)
|
||||
REPO="Significant-Gravitas/AutoGPT"
|
||||
@@ -792,27 +584,15 @@ TREE_JSON+=']'
|
||||
|
||||
# Step 2: Create tree, commit, and branch ref
|
||||
TREE_SHA=$(echo "$TREE_JSON" | jq -c '{tree: .}' | gh api "repos/${REPO}/git/trees" --input - --jq '.sha')
|
||||
|
||||
# Resolve parent commit so screenshots are chained, not orphan root commits
|
||||
PARENT_SHA=$(gh api "repos/${REPO}/git/refs/heads/${SCREENSHOTS_BRANCH}" --jq '.object.sha' 2>/dev/null || echo "")
|
||||
if [ -n "$PARENT_SHA" ]; then
|
||||
COMMIT_SHA=$(gh api "repos/${REPO}/git/commits" \
|
||||
-f message="test: add E2E test screenshots for PR #${PR_NUMBER}" \
|
||||
-f tree="$TREE_SHA" \
|
||||
-f "parents[]=$PARENT_SHA" \
|
||||
--jq '.sha')
|
||||
else
|
||||
COMMIT_SHA=$(gh api "repos/${REPO}/git/commits" \
|
||||
-f message="test: add E2E test screenshots for PR #${PR_NUMBER}" \
|
||||
-f tree="$TREE_SHA" \
|
||||
--jq '.sha')
|
||||
fi
|
||||
|
||||
COMMIT_SHA=$(gh api "repos/${REPO}/git/commits" \
|
||||
-f message="test: add E2E test screenshots for PR #${PR_NUMBER}" \
|
||||
-f tree="$TREE_SHA" \
|
||||
--jq '.sha')
|
||||
gh api "repos/${REPO}/git/refs" \
|
||||
-f ref="refs/heads/${SCREENSHOTS_BRANCH}" \
|
||||
-f sha="$COMMIT_SHA" 2>/dev/null \
|
||||
|| gh api "repos/${REPO}/git/refs/heads/${SCREENSHOTS_BRANCH}" \
|
||||
-X PATCH -f sha="$COMMIT_SHA" -F force=true
|
||||
-X PATCH -f sha="$COMMIT_SHA" -f force=true
|
||||
```
|
||||
|
||||
Then post the comment with **inline images AND explanations for each screenshot**:
|
||||
@@ -878,15 +658,6 @@ INNEREOF
|
||||
|
||||
gh api "repos/${REPO}/issues/$PR_NUMBER/comments" -F body=@"$COMMENT_FILE"
|
||||
rm -f "$COMMENT_FILE"
|
||||
|
||||
# Verify the posted comment contains inline images — exit 1 if none found
|
||||
# Use separate --paginate + jq pipe: --jq applies per-page, not to the full list
|
||||
LAST_COMMENT=$(gh api "repos/${REPO}/issues/$PR_NUMBER/comments" --paginate 2>/dev/null | jq -r '.[-1].body // ""')
|
||||
if ! echo "$LAST_COMMENT" | grep -q '!\['; then
|
||||
echo "ERROR: Posted comment contains no inline images (![). Bare directory links are not acceptable." >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "✓ Inline images verified in posted comment"
|
||||
```
|
||||
|
||||
**The PR comment MUST include:**
|
||||
@@ -896,103 +667,6 @@ echo "✓ Inline images verified in posted comment"
|
||||
|
||||
This approach uses the GitHub Git API to create blobs, trees, commits, and refs entirely server-side. No local `git checkout` or `git push` — safe for worktrees and won't interfere with the PR branch.
|
||||
|
||||
## Step 8: Evaluate and post a formal PR review
|
||||
|
||||
After the test comment is posted, evaluate whether the run was thorough enough to make a merge decision, then post a formal GitHub review (approve or request changes). **This step is mandatory — every test run MUST end with a formal review decision.**
|
||||
|
||||
### Evaluation criteria
|
||||
|
||||
Re-read the PR description:
|
||||
```bash
|
||||
gh pr view "$PR_NUMBER" --json body --jq '.body' --repo "$REPO"
|
||||
```
|
||||
|
||||
Score the run against each criterion:
|
||||
|
||||
| Criterion | Pass condition |
|
||||
|-----------|---------------|
|
||||
| **Coverage** | Every feature/change described in the PR has at least one test scenario |
|
||||
| **All scenarios pass** | No FAIL rows in the results table |
|
||||
| **Negative tests** | At least one failure-path test per feature (invalid input, unauthorized, edge case) |
|
||||
| **Before/after evidence** | Every state-changing API call has before/after values logged |
|
||||
| **Screenshots are meaningful** | Screenshots show the actual state change, not just a loading spinner or blank page |
|
||||
| **No regressions** | Existing core flows (login, agent create/run) still work |
|
||||
|
||||
### Decision logic
|
||||
|
||||
```
|
||||
ALL criteria pass → APPROVE
|
||||
Any scenario FAIL or missing PR feature → REQUEST_CHANGES (list gaps)
|
||||
Evidence weak (no before/after, vague shots) → REQUEST_CHANGES (list what's missing)
|
||||
```
|
||||
|
||||
### Post the review
|
||||
|
||||
```bash
|
||||
REVIEW_FILE=$(mktemp)
|
||||
|
||||
# Count results
|
||||
PASS_COUNT=$(echo "$TEST_RESULTS_TABLE" | grep -c "PASS" || true)
|
||||
FAIL_COUNT=$(echo "$TEST_RESULTS_TABLE" | grep -c "FAIL" || true)
|
||||
TOTAL=$(( PASS_COUNT + FAIL_COUNT ))
|
||||
|
||||
# List any coverage gaps found during evaluation (populate this array as you assess)
|
||||
# e.g. COVERAGE_GAPS=("PR claims to add X but no test covers it")
|
||||
COVERAGE_GAPS=()
|
||||
```
|
||||
|
||||
**If APPROVING** — all criteria met, zero failures, full coverage:
|
||||
|
||||
```bash
|
||||
cat > "$REVIEW_FILE" <<REVIEWEOF
|
||||
## E2E Test Evaluation — APPROVED
|
||||
|
||||
**Results:** ${PASS_COUNT}/${TOTAL} scenarios passed.
|
||||
|
||||
**Coverage:** All features described in the PR were exercised.
|
||||
|
||||
**Evidence:** Before/after API values logged for all state-changing operations; screenshots show meaningful state transitions.
|
||||
|
||||
**Negative tests:** Failure paths tested for each feature.
|
||||
|
||||
No regressions observed on core flows.
|
||||
REVIEWEOF
|
||||
|
||||
gh pr review "$PR_NUMBER" --repo "$REPO" --approve --body "$(cat "$REVIEW_FILE")"
|
||||
echo "✅ PR approved"
|
||||
```
|
||||
|
||||
**If REQUESTING CHANGES** — any failure, coverage gap, or missing evidence:
|
||||
|
||||
```bash
|
||||
FAIL_LIST=$(echo "$TEST_RESULTS_TABLE" | grep "FAIL" | awk -F'|' '{print "- Scenario" $2 "failed"}' || true)
|
||||
|
||||
cat > "$REVIEW_FILE" <<REVIEWEOF
|
||||
## E2E Test Evaluation — Changes Requested
|
||||
|
||||
**Results:** ${PASS_COUNT}/${TOTAL} scenarios passed, ${FAIL_COUNT} failed.
|
||||
|
||||
### Required before merge
|
||||
|
||||
${FAIL_LIST}
|
||||
$(for gap in "${COVERAGE_GAPS[@]}"; do echo "- $gap"; done)
|
||||
|
||||
Please fix the above and re-run the E2E tests.
|
||||
REVIEWEOF
|
||||
|
||||
gh pr review "$PR_NUMBER" --repo "$REPO" --request-changes --body "$(cat "$REVIEW_FILE")"
|
||||
echo "❌ Changes requested"
|
||||
```
|
||||
|
||||
```bash
|
||||
rm -f "$REVIEW_FILE"
|
||||
```
|
||||
|
||||
**Rules:**
|
||||
- In `--fix` mode, fix all failures before posting the review — the review reflects the final state after fixes
|
||||
- Never approve if any scenario failed, even if it seems like a flake — rerun that scenario first
|
||||
- Never request changes for issues already fixed in this run
|
||||
|
||||
## Fix mode (--fix flag)
|
||||
|
||||
When `--fix` is present, the standard is HIGHER. Do not just note issues — FIX them immediately.
|
||||
@@ -1060,15 +734,9 @@ test scenario → find issue (bug OR UX problem) → screenshot broken state
|
||||
### Problem: Frontend shows cookie banner blocking interaction
|
||||
**Fix:** `agent-browser click 'text=Accept All'` before other interactions.
|
||||
|
||||
### Problem: Claude CLI not found in copilot_executor container
|
||||
**Symptom:** Copilot logs say `claude: command not found` or similar when starting an SDK turn.
|
||||
**Cause:** Image was built without `poetry install` (stale base layer, or Dockerfile bypass). The SDK CLI ships inside the `claude_agent_sdk` Poetry dep — it is NOT an npm package.
|
||||
**Fix:** Rebuild the image cleanly: `docker compose build --no-cache copilot_executor && docker compose up -d copilot_executor`. Do NOT `docker exec ... npm install -g @anthropic-ai/claude-code` — that is outdated guidance and will pollute the container with a second CLI that the SDK won't use.
|
||||
|
||||
### Problem: agent-browser screenshot hangs / times out
|
||||
**Symptom:** `agent-browser screenshot` exits with code 124 even on `about:blank`.
|
||||
**Cause:** Stuck CDP connection or Chromium process tree. Seen on macOS when a prior `/pr-test` left a zombie Chrome for Testing.
|
||||
**Fix:** `pkill -9 -f "agent-browser|chromium|Chrome for Testing" && sleep 2`, then reopen the browser with a fresh `--session-name`. If still failing, verify via `agent-browser eval` + `agent-browser snapshot` (DOM state) instead of relying on PNGs — the feature under test is the same.
|
||||
### Problem: Container loses npm packages after rebuild
|
||||
**Cause:** `docker compose up --build` rebuilds the image, losing runtime installs.
|
||||
**Fix:** Add packages to the Dockerfile instead of installing at runtime.
|
||||
|
||||
### Problem: Services not starting after `docker compose up`
|
||||
**Fix:** Wait and check health: `docker compose ps`. Common cause: migration hasn't finished. Check: `docker logs autogpt_platform-migrate-1 2>&1 | tail -5`. If supabase-db isn't healthy: `docker restart supabase-db && sleep 10`.
|
||||
|
||||
@@ -48,15 +48,14 @@ git diff "$BASE_BRANCH"...HEAD -- src/ | head -500
|
||||
For each changed file, determine:
|
||||
|
||||
1. **Is it a page?** (`page.tsx`) — these are the primary test targets
|
||||
2. **Is it a hook?** (`use*.ts`) — test via the page/component that uses it; avoid direct `renderHook()` tests unless it is a shared reusable hook with standalone business logic
|
||||
2. **Is it a hook?** (`use*.ts`) — test via the page that uses it
|
||||
3. **Is it a component?** (`.tsx` in `components/`) — test via the parent page unless it's complex enough to warrant isolation
|
||||
4. **Is it a helper?** (`helpers.ts`, `utils.ts`) — unit test directly if pure logic
|
||||
|
||||
**Priority order:**
|
||||
|
||||
1. Pages with new/changed data fetching or user interactions
|
||||
2. Components with complex internal logic (modals, forms, wizards)
|
||||
3. Shared hooks with standalone business logic when UI-level coverage is impractical
|
||||
3. Hooks with non-trivial business logic
|
||||
4. Pure helper functions
|
||||
|
||||
Skip: styling-only changes, type-only changes, config changes.
|
||||
@@ -164,7 +163,6 @@ describe("LibraryPage", () => {
|
||||
- Use `waitFor` when asserting side effects or state changes after interactions
|
||||
- Import `fireEvent` or `userEvent` from the test-utils for interactions
|
||||
- Do NOT mock internal hooks or functions — mock at the API boundary via MSW
|
||||
- Prefer Orval-generated MSW handlers and response builders over hand-built API response objects
|
||||
- Do NOT use `act()` manually — `render` and `fireEvent` handle it
|
||||
- Keep tests focused: one behavior per test
|
||||
- Use descriptive test names that read like sentences
|
||||
@@ -192,7 +190,9 @@ import { http, HttpResponse } from "msw";
|
||||
server.use(
|
||||
http.get("http://localhost:3000/api/proxy/api/v2/library/agents", () => {
|
||||
return HttpResponse.json({
|
||||
agents: [{ id: "1", name: "Test Agent", description: "A test agent" }],
|
||||
agents: [
|
||||
{ id: "1", name: "Test Agent", description: "A test agent" },
|
||||
],
|
||||
pagination: { total_items: 1, total_pages: 1, page: 1, page_size: 10 },
|
||||
});
|
||||
}),
|
||||
@@ -211,7 +211,6 @@ pnpm test:unit --reporter=verbose
|
||||
```
|
||||
|
||||
If tests fail:
|
||||
|
||||
1. Read the error output carefully
|
||||
2. Fix the test (not the source code, unless there is a genuine bug)
|
||||
3. Re-run until all pass
|
||||
|
||||
13
.github/workflows/platform-fullstack-ci.yml
vendored
13
.github/workflows/platform-fullstack-ci.yml
vendored
@@ -160,7 +160,6 @@ jobs:
|
||||
run: |
|
||||
cp ../backend/.env.default ../backend/.env
|
||||
echo "OPENAI_INTERNAL_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> ../backend/.env
|
||||
echo "SCHEDULER_STARTUP_EMBEDDING_BACKFILL=false" >> ../backend/.env
|
||||
env:
|
||||
# Used by E2E test data script to generate embeddings for approved store agents
|
||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||
@@ -289,14 +288,6 @@ jobs:
|
||||
cache: "pnpm"
|
||||
cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml
|
||||
|
||||
- name: Set up tests - Cache Playwright browsers
|
||||
uses: actions/cache@v5
|
||||
with:
|
||||
path: ~/.cache/ms-playwright
|
||||
key: playwright-${{ runner.os }}-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml') }}
|
||||
restore-keys: |
|
||||
playwright-${{ runner.os }}-
|
||||
|
||||
- name: Copy source maps from Docker for E2E coverage
|
||||
run: |
|
||||
FRONTEND_CONTAINER=$(docker compose -f ../docker-compose.resolved.yml ps -q frontend)
|
||||
@@ -308,8 +299,8 @@ jobs:
|
||||
- name: Set up tests - Install browser 'chromium'
|
||||
run: pnpm playwright install --with-deps chromium
|
||||
|
||||
- name: Run Playwright E2E suite
|
||||
run: pnpm test:e2e:no-build
|
||||
- name: Run Playwright tests
|
||||
run: pnpm test:no-build
|
||||
continue-on-error: false
|
||||
|
||||
- name: Upload E2E coverage to Codecov
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -187,11 +187,9 @@ autogpt_platform/backend/settings.py
|
||||
.claude/settings.local.json
|
||||
CLAUDE.local.md
|
||||
/autogpt_platform/backend/logs
|
||||
/autogpt_platform/backend/poetry.toml
|
||||
|
||||
# Test database
|
||||
test.db
|
||||
.next
|
||||
# Implementation plans (generated by AI agents)
|
||||
plans/
|
||||
.claude/worktrees/
|
||||
|
||||
@@ -90,10 +90,6 @@
|
||||
{
|
||||
"path": "detect_secrets.filters.allowlist.is_line_allowlisted"
|
||||
},
|
||||
{
|
||||
"path": "detect_secrets.filters.common.is_baseline_file",
|
||||
"filename": ".secrets.baseline"
|
||||
},
|
||||
{
|
||||
"path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
|
||||
"min_level": 2
|
||||
@@ -454,7 +450,7 @@
|
||||
"filename": "autogpt_platform/frontend/src/lib/constants.ts",
|
||||
"hashed_secret": "27b924db06a28cc755fb07c54f0fddc30659fe4d",
|
||||
"is_verified": false,
|
||||
"line_number": 13
|
||||
"line_number": 10
|
||||
}
|
||||
],
|
||||
"autogpt_platform/frontend/src/tests/credentials/index.ts": [
|
||||
@@ -467,5 +463,5 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"generated_at": "2026-04-09T14:20:23Z"
|
||||
"generated_at": "2026-04-02T13:10:54Z"
|
||||
}
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
-- =============================================================
|
||||
-- View: analytics.platform_cost_log
|
||||
-- Looker source alias: ds115 | Charts: 0
|
||||
-- =============================================================
|
||||
-- DESCRIPTION
|
||||
-- One row per platform cost log entry (last 90 days).
|
||||
-- Tracks real API spend at the call level: provider, model,
|
||||
-- token counts (including Anthropic cache tokens), cost in
|
||||
-- microdollars, and the block/execution that incurred the cost.
|
||||
-- Joins the User table to provide email for per-user breakdowns.
|
||||
--
|
||||
-- SOURCE TABLES
|
||||
-- platform.PlatformCostLog — Per-call cost records
|
||||
-- platform.User — User email
|
||||
--
|
||||
-- OUTPUT COLUMNS
|
||||
-- id TEXT Log entry UUID
|
||||
-- createdAt TIMESTAMPTZ When the cost was recorded
|
||||
-- userId TEXT User who incurred the cost (nullable)
|
||||
-- email TEXT User email (nullable)
|
||||
-- graphExecId TEXT Graph execution UUID (nullable)
|
||||
-- nodeExecId TEXT Node execution UUID (nullable)
|
||||
-- blockName TEXT Block that made the API call (nullable)
|
||||
-- provider TEXT API provider, lowercase (e.g. 'openai', 'anthropic')
|
||||
-- model TEXT Model name (nullable)
|
||||
-- trackingType TEXT Cost unit: 'tokens' | 'cost_usd' | 'characters' | etc.
|
||||
-- costMicrodollars BIGINT Cost in microdollars (divide by 1,000,000 for USD)
|
||||
-- costUsd FLOAT Cost in USD (costMicrodollars / 1,000,000)
|
||||
-- inputTokens INT Prompt/input tokens (nullable)
|
||||
-- outputTokens INT Completion/output tokens (nullable)
|
||||
-- cacheReadTokens INT Anthropic cache-read tokens billed at 10% (nullable)
|
||||
-- cacheCreationTokens INT Anthropic cache-write tokens billed at 125% (nullable)
|
||||
-- totalTokens INT inputTokens + outputTokens (nullable if either is null)
|
||||
-- duration FLOAT API call duration in seconds (nullable)
|
||||
--
|
||||
-- WINDOW
|
||||
-- Rolling 90 days (createdAt > CURRENT_DATE - 90 days)
|
||||
--
|
||||
-- EXAMPLE QUERIES
|
||||
-- -- Total spend by provider (last 90 days)
|
||||
-- SELECT provider, SUM("costUsd") AS total_usd, COUNT(*) AS calls
|
||||
-- FROM analytics.platform_cost_log
|
||||
-- GROUP BY 1 ORDER BY total_usd DESC;
|
||||
--
|
||||
-- -- Spend by model
|
||||
-- SELECT provider, model, SUM("costUsd") AS total_usd,
|
||||
-- SUM("inputTokens") AS input_tokens,
|
||||
-- SUM("outputTokens") AS output_tokens
|
||||
-- FROM analytics.platform_cost_log
|
||||
-- WHERE model IS NOT NULL
|
||||
-- GROUP BY 1, 2 ORDER BY total_usd DESC;
|
||||
--
|
||||
-- -- Top 20 users by spend
|
||||
-- SELECT "userId", email, SUM("costUsd") AS total_usd, COUNT(*) AS calls
|
||||
-- FROM analytics.platform_cost_log
|
||||
-- WHERE "userId" IS NOT NULL
|
||||
-- GROUP BY 1, 2 ORDER BY total_usd DESC LIMIT 20;
|
||||
--
|
||||
-- -- Daily spend trend
|
||||
-- SELECT DATE_TRUNC('day', "createdAt") AS day,
|
||||
-- SUM("costUsd") AS daily_usd,
|
||||
-- COUNT(*) AS calls
|
||||
-- FROM analytics.platform_cost_log
|
||||
-- GROUP BY 1 ORDER BY 1;
|
||||
--
|
||||
-- -- Cache hit rate for Anthropic (cache reads vs total reads)
|
||||
-- SELECT DATE_TRUNC('day', "createdAt") AS day,
|
||||
-- SUM("cacheReadTokens")::float /
|
||||
-- NULLIF(SUM("inputTokens" + COALESCE("cacheReadTokens", 0)), 0) AS cache_hit_rate
|
||||
-- FROM analytics.platform_cost_log
|
||||
-- WHERE provider = 'anthropic'
|
||||
-- GROUP BY 1 ORDER BY 1;
|
||||
-- =============================================================
|
||||
|
||||
SELECT
|
||||
p."id" AS id,
|
||||
p."createdAt" AS createdAt,
|
||||
p."userId" AS userId,
|
||||
u."email" AS email,
|
||||
p."graphExecId" AS graphExecId,
|
||||
p."nodeExecId" AS nodeExecId,
|
||||
p."blockName" AS blockName,
|
||||
p."provider" AS provider,
|
||||
p."model" AS model,
|
||||
p."trackingType" AS trackingType,
|
||||
p."costMicrodollars" AS costMicrodollars,
|
||||
p."costMicrodollars"::float / 1000000.0 AS costUsd,
|
||||
p."inputTokens" AS inputTokens,
|
||||
p."outputTokens" AS outputTokens,
|
||||
p."cacheReadTokens" AS cacheReadTokens,
|
||||
p."cacheCreationTokens" AS cacheCreationTokens,
|
||||
CASE
|
||||
WHEN p."inputTokens" IS NOT NULL AND p."outputTokens" IS NOT NULL
|
||||
THEN p."inputTokens" + p."outputTokens"
|
||||
ELSE NULL
|
||||
END AS totalTokens,
|
||||
p."duration" AS duration
|
||||
FROM platform."PlatformCostLog" p
|
||||
LEFT JOIN platform."User" u ON u."id" = p."userId"
|
||||
WHERE p."createdAt" > CURRENT_DATE - INTERVAL '90 days'
|
||||
@@ -58,17 +58,6 @@ V0_API_KEY=
|
||||
OPEN_ROUTER_API_KEY=
|
||||
NVIDIA_API_KEY=
|
||||
|
||||
# Graphiti Temporal Knowledge Graph Memory
|
||||
# Rollout controlled by LaunchDarkly flag "graphiti-memory"
|
||||
# LLM key falls back to CHAT_API_KEY (AutoPilot), then OPEN_ROUTER_API_KEY.
|
||||
# Embedder key falls back to CHAT_OPENAI_API_KEY (AutoPilot), then OPENAI_API_KEY.
|
||||
GRAPHITI_FALKORDB_HOST=localhost
|
||||
GRAPHITI_FALKORDB_PORT=6380
|
||||
GRAPHITI_FALKORDB_PASSWORD=
|
||||
GRAPHITI_LLM_MODEL=gpt-4.1-mini
|
||||
GRAPHITI_EMBEDDER_MODEL=text-embedding-3-small
|
||||
GRAPHITI_SEMAPHORE_LIMIT=5
|
||||
|
||||
# Langfuse Prompt Management
|
||||
# Used for managing the CoPilot system prompt externally
|
||||
# Get credentials from https://cloud.langfuse.com or your self-hosted instance
|
||||
|
||||
@@ -1,166 +0,0 @@
|
||||
{
|
||||
"id": "858e2226-e047-4d19-a832-3be4a134d155",
|
||||
"version": 2,
|
||||
"is_active": true,
|
||||
"name": "Calculator agent",
|
||||
"description": "",
|
||||
"instructions": null,
|
||||
"recommended_schedule_cron": null,
|
||||
"forked_from_id": null,
|
||||
"forked_from_version": null,
|
||||
"user_id": "",
|
||||
"created_at": "2026-04-13T03:45:11.241Z",
|
||||
"nodes": [
|
||||
{
|
||||
"id": "6762da5d-6915-4836-a431-6dcd7d36a54a",
|
||||
"block_id": "c0a8e994-ebf1-4a9c-a4d8-89d09c86741b",
|
||||
"input_default": {
|
||||
"name": "Input",
|
||||
"secret": false,
|
||||
"advanced": false
|
||||
},
|
||||
"metadata": {
|
||||
"position": {
|
||||
"x": -188.2244873046875,
|
||||
"y": 95
|
||||
}
|
||||
},
|
||||
"input_links": [],
|
||||
"output_links": [
|
||||
{
|
||||
"id": "432c7caa-49b9-4b70-bd21-2fa33a569601",
|
||||
"source_id": "6762da5d-6915-4836-a431-6dcd7d36a54a",
|
||||
"sink_id": "bf4a15ff-b0c4-4032-a21b-5880224af690",
|
||||
"source_name": "result",
|
||||
"sink_name": "a",
|
||||
"is_static": true
|
||||
}
|
||||
],
|
||||
"graph_id": "858e2226-e047-4d19-a832-3be4a134d155",
|
||||
"graph_version": 2,
|
||||
"webhook_id": null
|
||||
},
|
||||
{
|
||||
"id": "65429c9e-a0c6-4032-a421-6899c394fa74",
|
||||
"block_id": "363ae599-353e-4804-937e-b2ee3cef3da4",
|
||||
"input_default": {
|
||||
"name": "Output",
|
||||
"secret": false,
|
||||
"advanced": false,
|
||||
"escape_html": false
|
||||
},
|
||||
"metadata": {
|
||||
"position": {
|
||||
"x": 825.198974609375,
|
||||
"y": 123.75
|
||||
}
|
||||
},
|
||||
"input_links": [
|
||||
{
|
||||
"id": "8cdb2f33-5b10-4cc2-8839-f8ccb70083a3",
|
||||
"source_id": "bf4a15ff-b0c4-4032-a21b-5880224af690",
|
||||
"sink_id": "65429c9e-a0c6-4032-a421-6899c394fa74",
|
||||
"source_name": "result",
|
||||
"sink_name": "value",
|
||||
"is_static": false
|
||||
}
|
||||
],
|
||||
"output_links": [],
|
||||
"graph_id": "858e2226-e047-4d19-a832-3be4a134d155",
|
||||
"graph_version": 2,
|
||||
"webhook_id": null
|
||||
},
|
||||
{
|
||||
"id": "bf4a15ff-b0c4-4032-a21b-5880224af690",
|
||||
"block_id": "b1ab9b19-67a6-406d-abf5-2dba76d00c79",
|
||||
"input_default": {
|
||||
"b": 34,
|
||||
"operation": "Add",
|
||||
"round_result": false
|
||||
},
|
||||
"metadata": {
|
||||
"position": {
|
||||
"x": 323.0255126953125,
|
||||
"y": 121.25
|
||||
}
|
||||
},
|
||||
"input_links": [
|
||||
{
|
||||
"id": "432c7caa-49b9-4b70-bd21-2fa33a569601",
|
||||
"source_id": "6762da5d-6915-4836-a431-6dcd7d36a54a",
|
||||
"sink_id": "bf4a15ff-b0c4-4032-a21b-5880224af690",
|
||||
"source_name": "result",
|
||||
"sink_name": "a",
|
||||
"is_static": true
|
||||
}
|
||||
],
|
||||
"output_links": [
|
||||
{
|
||||
"id": "8cdb2f33-5b10-4cc2-8839-f8ccb70083a3",
|
||||
"source_id": "bf4a15ff-b0c4-4032-a21b-5880224af690",
|
||||
"sink_id": "65429c9e-a0c6-4032-a421-6899c394fa74",
|
||||
"source_name": "result",
|
||||
"sink_name": "value",
|
||||
"is_static": false
|
||||
}
|
||||
],
|
||||
"graph_id": "858e2226-e047-4d19-a832-3be4a134d155",
|
||||
"graph_version": 2,
|
||||
"webhook_id": null
|
||||
}
|
||||
],
|
||||
"links": [
|
||||
{
|
||||
"id": "8cdb2f33-5b10-4cc2-8839-f8ccb70083a3",
|
||||
"source_id": "bf4a15ff-b0c4-4032-a21b-5880224af690",
|
||||
"sink_id": "65429c9e-a0c6-4032-a421-6899c394fa74",
|
||||
"source_name": "result",
|
||||
"sink_name": "value",
|
||||
"is_static": false
|
||||
},
|
||||
{
|
||||
"id": "432c7caa-49b9-4b70-bd21-2fa33a569601",
|
||||
"source_id": "6762da5d-6915-4836-a431-6dcd7d36a54a",
|
||||
"sink_id": "bf4a15ff-b0c4-4032-a21b-5880224af690",
|
||||
"source_name": "result",
|
||||
"sink_name": "a",
|
||||
"is_static": true
|
||||
}
|
||||
],
|
||||
"sub_graphs": [],
|
||||
"input_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Input": {
|
||||
"advanced": false,
|
||||
"secret": false,
|
||||
"title": "Input"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"Input"
|
||||
]
|
||||
},
|
||||
"output_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Output": {
|
||||
"advanced": false,
|
||||
"secret": false,
|
||||
"title": "Output"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"Output"
|
||||
]
|
||||
},
|
||||
"has_external_trigger": false,
|
||||
"has_human_in_the_loop": false,
|
||||
"has_sensitive_action": false,
|
||||
"trigger_setup_info": null,
|
||||
"credentials_input_schema": {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
}
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
from autogpt_libs.auth import get_user_id, requires_admin_user
|
||||
from fastapi import APIRouter, Query, Security
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.data.platform_cost import (
|
||||
CostLogRow,
|
||||
PlatformCostDashboard,
|
||||
get_platform_cost_dashboard,
|
||||
get_platform_cost_logs,
|
||||
get_platform_cost_logs_for_export,
|
||||
)
|
||||
from backend.util.models import Pagination
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/platform-costs",
|
||||
tags=["platform-cost", "admin"],
|
||||
dependencies=[Security(requires_admin_user)],
|
||||
)
|
||||
|
||||
|
||||
class PlatformCostLogsResponse(BaseModel):
|
||||
logs: list[CostLogRow]
|
||||
pagination: Pagination
|
||||
|
||||
|
||||
@router.get(
|
||||
"/dashboard",
|
||||
response_model=PlatformCostDashboard,
|
||||
summary="Get Platform Cost Dashboard",
|
||||
)
|
||||
async def get_cost_dashboard(
|
||||
admin_user_id: str = Security(get_user_id),
|
||||
start: datetime | None = Query(None),
|
||||
end: datetime | None = Query(None),
|
||||
provider: str | None = Query(None),
|
||||
user_id: str | None = Query(None),
|
||||
model: str | None = Query(None),
|
||||
block_name: str | None = Query(None),
|
||||
tracking_type: str | None = Query(None),
|
||||
graph_exec_id: str | None = Query(None),
|
||||
):
|
||||
logger.info("Admin %s fetching platform cost dashboard", admin_user_id)
|
||||
return await get_platform_cost_dashboard(
|
||||
start=start,
|
||||
end=end,
|
||||
provider=provider,
|
||||
user_id=user_id,
|
||||
model=model,
|
||||
block_name=block_name,
|
||||
tracking_type=tracking_type,
|
||||
graph_exec_id=graph_exec_id,
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/logs",
|
||||
response_model=PlatformCostLogsResponse,
|
||||
summary="Get Platform Cost Logs",
|
||||
)
|
||||
async def get_cost_logs(
|
||||
admin_user_id: str = Security(get_user_id),
|
||||
start: datetime | None = Query(None),
|
||||
end: datetime | None = Query(None),
|
||||
provider: str | None = Query(None),
|
||||
user_id: str | None = Query(None),
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(50, ge=1, le=200),
|
||||
model: str | None = Query(None),
|
||||
block_name: str | None = Query(None),
|
||||
tracking_type: str | None = Query(None),
|
||||
graph_exec_id: str | None = Query(None),
|
||||
):
|
||||
logger.info("Admin %s fetching platform cost logs", admin_user_id)
|
||||
logs, total = await get_platform_cost_logs(
|
||||
start=start,
|
||||
end=end,
|
||||
provider=provider,
|
||||
user_id=user_id,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
model=model,
|
||||
block_name=block_name,
|
||||
tracking_type=tracking_type,
|
||||
graph_exec_id=graph_exec_id,
|
||||
)
|
||||
total_pages = (total + page_size - 1) // page_size
|
||||
return PlatformCostLogsResponse(
|
||||
logs=logs,
|
||||
pagination=Pagination(
|
||||
total_items=total,
|
||||
total_pages=total_pages,
|
||||
current_page=page,
|
||||
page_size=page_size,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class PlatformCostExportResponse(BaseModel):
|
||||
logs: list[CostLogRow]
|
||||
total_rows: int
|
||||
truncated: bool
|
||||
|
||||
|
||||
@router.get(
|
||||
"/logs/export",
|
||||
response_model=PlatformCostExportResponse,
|
||||
summary="Export Platform Cost Logs",
|
||||
)
|
||||
async def export_cost_logs(
|
||||
admin_user_id: str = Security(get_user_id),
|
||||
start: datetime | None = Query(None),
|
||||
end: datetime | None = Query(None),
|
||||
provider: str | None = Query(None),
|
||||
user_id: str | None = Query(None),
|
||||
model: str | None = Query(None),
|
||||
block_name: str | None = Query(None),
|
||||
tracking_type: str | None = Query(None),
|
||||
graph_exec_id: str | None = Query(None),
|
||||
):
|
||||
logger.info("Admin %s exporting platform cost logs", admin_user_id)
|
||||
logs, truncated = await get_platform_cost_logs_for_export(
|
||||
start=start,
|
||||
end=end,
|
||||
provider=provider,
|
||||
user_id=user_id,
|
||||
model=model,
|
||||
block_name=block_name,
|
||||
tracking_type=tracking_type,
|
||||
graph_exec_id=graph_exec_id,
|
||||
)
|
||||
return PlatformCostExportResponse(
|
||||
logs=logs,
|
||||
total_rows=len(logs),
|
||||
truncated=truncated,
|
||||
)
|
||||
@@ -1,291 +0,0 @@
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import fastapi
|
||||
import fastapi.testclient
|
||||
import pytest
|
||||
import pytest_mock
|
||||
from autogpt_libs.auth.jwt_utils import get_jwt_payload
|
||||
|
||||
from backend.data.platform_cost import CostLogRow, PlatformCostDashboard
|
||||
|
||||
from .platform_cost_routes import router as platform_cost_router
|
||||
|
||||
app = fastapi.FastAPI()
|
||||
app.include_router(platform_cost_router)
|
||||
|
||||
client = fastapi.testclient.TestClient(app)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_app_admin_auth(mock_jwt_admin):
|
||||
"""Setup admin auth overrides for all tests in this module"""
|
||||
app.dependency_overrides[get_jwt_payload] = mock_jwt_admin["get_jwt_payload"]
|
||||
yield
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
def test_get_dashboard_success(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
real_dashboard = PlatformCostDashboard(
|
||||
by_provider=[],
|
||||
by_user=[],
|
||||
total_cost_microdollars=0,
|
||||
total_requests=0,
|
||||
total_users=0,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.admin.platform_cost_routes.get_platform_cost_dashboard",
|
||||
AsyncMock(return_value=real_dashboard),
|
||||
)
|
||||
|
||||
response = client.get("/platform-costs/dashboard")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "by_provider" in data
|
||||
assert "by_user" in data
|
||||
assert data["total_cost_microdollars"] == 0
|
||||
|
||||
|
||||
def test_get_logs_success(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
mocker.patch(
|
||||
"backend.api.features.admin.platform_cost_routes.get_platform_cost_logs",
|
||||
AsyncMock(return_value=([], 0)),
|
||||
)
|
||||
|
||||
response = client.get("/platform-costs/logs")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["logs"] == []
|
||||
assert data["pagination"]["total_items"] == 0
|
||||
|
||||
|
||||
def test_get_dashboard_with_filters(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
real_dashboard = PlatformCostDashboard(
|
||||
by_provider=[],
|
||||
by_user=[],
|
||||
total_cost_microdollars=0,
|
||||
total_requests=0,
|
||||
total_users=0,
|
||||
)
|
||||
mock_dashboard = AsyncMock(return_value=real_dashboard)
|
||||
mocker.patch(
|
||||
"backend.api.features.admin.platform_cost_routes.get_platform_cost_dashboard",
|
||||
mock_dashboard,
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
"/platform-costs/dashboard",
|
||||
params={
|
||||
"start": "2026-01-01T00:00:00",
|
||||
"end": "2026-04-01T00:00:00",
|
||||
"provider": "openai",
|
||||
"user_id": "test-user-123",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
mock_dashboard.assert_called_once()
|
||||
call_kwargs = mock_dashboard.call_args.kwargs
|
||||
assert call_kwargs["provider"] == "openai"
|
||||
assert call_kwargs["user_id"] == "test-user-123"
|
||||
assert call_kwargs["start"] is not None
|
||||
assert call_kwargs["end"] is not None
|
||||
|
||||
|
||||
def test_get_logs_with_pagination(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
mocker.patch(
|
||||
"backend.api.features.admin.platform_cost_routes.get_platform_cost_logs",
|
||||
AsyncMock(return_value=([], 0)),
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
"/platform-costs/logs",
|
||||
params={"page": 2, "page_size": 25, "provider": "anthropic"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["pagination"]["current_page"] == 2
|
||||
assert data["pagination"]["page_size"] == 25
|
||||
|
||||
|
||||
def test_get_dashboard_requires_admin() -> None:
|
||||
import fastapi
|
||||
from fastapi import HTTPException
|
||||
|
||||
def reject_jwt(request: fastapi.Request):
|
||||
raise HTTPException(status_code=401, detail="Not authenticated")
|
||||
|
||||
app.dependency_overrides[get_jwt_payload] = reject_jwt
|
||||
try:
|
||||
response = client.get("/platform-costs/dashboard")
|
||||
assert response.status_code == 401
|
||||
response = client.get("/platform-costs/logs")
|
||||
assert response.status_code == 401
|
||||
finally:
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
def test_get_dashboard_rejects_non_admin(mock_jwt_user, mock_jwt_admin) -> None:
|
||||
"""Non-admin JWT must be rejected with 403 by requires_admin_user."""
|
||||
app.dependency_overrides[get_jwt_payload] = mock_jwt_user["get_jwt_payload"]
|
||||
try:
|
||||
response = client.get("/platform-costs/dashboard")
|
||||
assert response.status_code == 403
|
||||
response = client.get("/platform-costs/logs")
|
||||
assert response.status_code == 403
|
||||
finally:
|
||||
app.dependency_overrides[get_jwt_payload] = mock_jwt_admin["get_jwt_payload"]
|
||||
|
||||
|
||||
def test_get_logs_invalid_page_size_too_large() -> None:
|
||||
"""page_size > 200 must be rejected with 422."""
|
||||
response = client.get("/platform-costs/logs", params={"page_size": 201})
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_get_logs_invalid_page_size_zero() -> None:
|
||||
"""page_size = 0 (below ge=1) must be rejected with 422."""
|
||||
response = client.get("/platform-costs/logs", params={"page_size": 0})
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_get_logs_invalid_page_negative() -> None:
|
||||
"""page < 1 must be rejected with 422."""
|
||||
response = client.get("/platform-costs/logs", params={"page": 0})
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_get_dashboard_invalid_date_format() -> None:
|
||||
"""Malformed start date must be rejected with 422."""
|
||||
response = client.get("/platform-costs/dashboard", params={"start": "not-a-date"})
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_get_dashboard_repeated_requests(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
"""Repeated requests to the dashboard route both return 200."""
|
||||
real_dashboard = PlatformCostDashboard(
|
||||
by_provider=[],
|
||||
by_user=[],
|
||||
total_cost_microdollars=42,
|
||||
total_requests=1,
|
||||
total_users=1,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.admin.platform_cost_routes.get_platform_cost_dashboard",
|
||||
AsyncMock(return_value=real_dashboard),
|
||||
)
|
||||
|
||||
r1 = client.get("/platform-costs/dashboard")
|
||||
r2 = client.get("/platform-costs/dashboard")
|
||||
|
||||
assert r1.status_code == 200
|
||||
assert r2.status_code == 200
|
||||
assert r1.json()["total_cost_microdollars"] == 42
|
||||
assert r2.json()["total_cost_microdollars"] == 42
|
||||
|
||||
|
||||
def _make_cost_log_row() -> CostLogRow:
|
||||
return CostLogRow(
|
||||
id="log-1",
|
||||
created_at=datetime(2026, 1, 1, tzinfo=timezone.utc),
|
||||
user_id="user-1",
|
||||
email="u***@example.com",
|
||||
graph_exec_id="graph-1",
|
||||
node_exec_id="node-1",
|
||||
block_name="LlmCallBlock",
|
||||
provider="anthropic",
|
||||
tracking_type="token",
|
||||
cost_microdollars=500,
|
||||
input_tokens=100,
|
||||
output_tokens=50,
|
||||
cache_read_tokens=10,
|
||||
cache_creation_tokens=5,
|
||||
duration=1.5,
|
||||
model="claude-3-5-sonnet-20241022",
|
||||
)
|
||||
|
||||
|
||||
def test_export_logs_success(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
row = _make_cost_log_row()
|
||||
mocker.patch(
|
||||
"backend.api.features.admin.platform_cost_routes.get_platform_cost_logs_for_export",
|
||||
AsyncMock(return_value=([row], False)),
|
||||
)
|
||||
|
||||
response = client.get("/platform-costs/logs/export")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["total_rows"] == 1
|
||||
assert data["truncated"] is False
|
||||
assert len(data["logs"]) == 1
|
||||
assert data["logs"][0]["cache_read_tokens"] == 10
|
||||
assert data["logs"][0]["cache_creation_tokens"] == 5
|
||||
|
||||
|
||||
def test_export_logs_truncated(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
rows = [_make_cost_log_row() for _ in range(3)]
|
||||
mocker.patch(
|
||||
"backend.api.features.admin.platform_cost_routes.get_platform_cost_logs_for_export",
|
||||
AsyncMock(return_value=(rows, True)),
|
||||
)
|
||||
|
||||
response = client.get("/platform-costs/logs/export")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["total_rows"] == 3
|
||||
assert data["truncated"] is True
|
||||
|
||||
|
||||
def test_export_logs_with_filters(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
mock_export = AsyncMock(return_value=([], False))
|
||||
mocker.patch(
|
||||
"backend.api.features.admin.platform_cost_routes.get_platform_cost_logs_for_export",
|
||||
mock_export,
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
"/platform-costs/logs/export",
|
||||
params={
|
||||
"provider": "anthropic",
|
||||
"model": "claude-3-5-sonnet-20241022",
|
||||
"block_name": "LlmCallBlock",
|
||||
"tracking_type": "token",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
mock_export.assert_called_once()
|
||||
call_kwargs = mock_export.call_args.kwargs
|
||||
assert call_kwargs["provider"] == "anthropic"
|
||||
assert call_kwargs["model"] == "claude-3-5-sonnet-20241022"
|
||||
assert call_kwargs["block_name"] == "LlmCallBlock"
|
||||
assert call_kwargs["tracking_type"] == "token"
|
||||
|
||||
|
||||
def test_export_logs_requires_admin() -> None:
|
||||
import fastapi
|
||||
from fastapi import HTTPException
|
||||
|
||||
def reject_jwt(request: fastapi.Request):
|
||||
raise HTTPException(status_code=401, detail="Not authenticated")
|
||||
|
||||
app.dependency_overrides[get_jwt_payload] = reject_jwt
|
||||
try:
|
||||
response = client.get("/platform-costs/logs/export")
|
||||
assert response.status_code == 401
|
||||
finally:
|
||||
app.dependency_overrides.clear()
|
||||
@@ -32,10 +32,10 @@ router = APIRouter(
|
||||
class UserRateLimitResponse(BaseModel):
|
||||
user_id: str
|
||||
user_email: Optional[str] = None
|
||||
daily_cost_limit_microdollars: int
|
||||
weekly_cost_limit_microdollars: int
|
||||
daily_cost_used_microdollars: int
|
||||
weekly_cost_used_microdollars: int
|
||||
daily_token_limit: int
|
||||
weekly_token_limit: int
|
||||
daily_tokens_used: int
|
||||
weekly_tokens_used: int
|
||||
tier: SubscriptionTier
|
||||
|
||||
|
||||
@@ -101,19 +101,17 @@ async def get_user_rate_limit(
|
||||
logger.info("Admin %s checking rate limit for user %s", admin_user_id, resolved_id)
|
||||
|
||||
daily_limit, weekly_limit, tier = await get_global_rate_limits(
|
||||
resolved_id,
|
||||
config.daily_cost_limit_microdollars,
|
||||
config.weekly_cost_limit_microdollars,
|
||||
resolved_id, config.daily_token_limit, config.weekly_token_limit
|
||||
)
|
||||
usage = await get_usage_status(resolved_id, daily_limit, weekly_limit, tier=tier)
|
||||
|
||||
return UserRateLimitResponse(
|
||||
user_id=resolved_id,
|
||||
user_email=resolved_email,
|
||||
daily_cost_limit_microdollars=daily_limit,
|
||||
weekly_cost_limit_microdollars=weekly_limit,
|
||||
daily_cost_used_microdollars=usage.daily.used,
|
||||
weekly_cost_used_microdollars=usage.weekly.used,
|
||||
daily_token_limit=daily_limit,
|
||||
weekly_token_limit=weekly_limit,
|
||||
daily_tokens_used=usage.daily.used,
|
||||
weekly_tokens_used=usage.weekly.used,
|
||||
tier=tier,
|
||||
)
|
||||
|
||||
@@ -143,9 +141,7 @@ async def reset_user_rate_limit(
|
||||
raise HTTPException(status_code=500, detail="Failed to reset usage") from e
|
||||
|
||||
daily_limit, weekly_limit, tier = await get_global_rate_limits(
|
||||
user_id,
|
||||
config.daily_cost_limit_microdollars,
|
||||
config.weekly_cost_limit_microdollars,
|
||||
user_id, config.daily_token_limit, config.weekly_token_limit
|
||||
)
|
||||
usage = await get_usage_status(user_id, daily_limit, weekly_limit, tier=tier)
|
||||
|
||||
@@ -158,10 +154,10 @@ async def reset_user_rate_limit(
|
||||
return UserRateLimitResponse(
|
||||
user_id=user_id,
|
||||
user_email=resolved_email,
|
||||
daily_cost_limit_microdollars=daily_limit,
|
||||
weekly_cost_limit_microdollars=weekly_limit,
|
||||
daily_cost_used_microdollars=usage.daily.used,
|
||||
weekly_cost_used_microdollars=usage.weekly.used,
|
||||
daily_token_limit=daily_limit,
|
||||
weekly_token_limit=weekly_limit,
|
||||
daily_tokens_used=usage.daily.used,
|
||||
weekly_tokens_used=usage.weekly.used,
|
||||
tier=tier,
|
||||
)
|
||||
|
||||
|
||||
@@ -85,10 +85,10 @@ def test_get_rate_limit(
|
||||
data = response.json()
|
||||
assert data["user_id"] == target_user_id
|
||||
assert data["user_email"] == _TARGET_EMAIL
|
||||
assert data["daily_cost_limit_microdollars"] == 2_500_000
|
||||
assert data["weekly_cost_limit_microdollars"] == 12_500_000
|
||||
assert data["daily_cost_used_microdollars"] == 500_000
|
||||
assert data["weekly_cost_used_microdollars"] == 3_000_000
|
||||
assert data["daily_token_limit"] == 2_500_000
|
||||
assert data["weekly_token_limit"] == 12_500_000
|
||||
assert data["daily_tokens_used"] == 500_000
|
||||
assert data["weekly_tokens_used"] == 3_000_000
|
||||
assert data["tier"] == "FREE"
|
||||
|
||||
configured_snapshot.assert_match(
|
||||
@@ -117,7 +117,7 @@ def test_get_rate_limit_by_email(
|
||||
data = response.json()
|
||||
assert data["user_id"] == target_user_id
|
||||
assert data["user_email"] == _TARGET_EMAIL
|
||||
assert data["daily_cost_limit_microdollars"] == 2_500_000
|
||||
assert data["daily_token_limit"] == 2_500_000
|
||||
|
||||
|
||||
def test_get_rate_limit_by_email_not_found(
|
||||
@@ -160,9 +160,9 @@ def test_reset_user_usage_daily_only(
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["daily_cost_used_microdollars"] == 0
|
||||
assert data["daily_tokens_used"] == 0
|
||||
# Weekly is untouched
|
||||
assert data["weekly_cost_used_microdollars"] == 3_000_000
|
||||
assert data["weekly_tokens_used"] == 3_000_000
|
||||
assert data["tier"] == "FREE"
|
||||
|
||||
mock_reset.assert_awaited_once_with(target_user_id, reset_weekly=False)
|
||||
@@ -192,8 +192,8 @@ def test_reset_user_usage_daily_and_weekly(
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["daily_cost_used_microdollars"] == 0
|
||||
assert data["weekly_cost_used_microdollars"] == 0
|
||||
assert data["daily_tokens_used"] == 0
|
||||
assert data["weekly_tokens_used"] == 0
|
||||
assert data["tier"] == "FREE"
|
||||
|
||||
mock_reset.assert_awaited_once_with(target_user_id, reset_weekly=True)
|
||||
|
||||
@@ -2,19 +2,20 @@
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import AsyncGenerator
|
||||
from typing import Annotated
|
||||
from uuid import uuid4
|
||||
|
||||
from autogpt_libs import auth
|
||||
from fastapi import APIRouter, HTTPException, Query, Response, Security
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from fastapi.responses import StreamingResponse
|
||||
from prisma.models import UserWorkspaceFile
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
from backend.copilot import service as chat_service
|
||||
from backend.copilot import stream_registry
|
||||
from backend.copilot.config import ChatConfig, CopilotLlmModel, CopilotMode
|
||||
from backend.copilot.db import get_chat_messages_paginated
|
||||
from backend.copilot.config import ChatConfig, CopilotMode
|
||||
from backend.copilot.executor.utils import enqueue_cancel_task, enqueue_copilot_turn
|
||||
from backend.copilot.model import (
|
||||
ChatMessage,
|
||||
@@ -27,14 +28,8 @@ from backend.copilot.model import (
|
||||
get_user_sessions,
|
||||
update_session_title,
|
||||
)
|
||||
from backend.copilot.pending_message_helpers import (
|
||||
QueuePendingMessageResponse,
|
||||
is_turn_in_flight,
|
||||
queue_pending_for_http,
|
||||
)
|
||||
from backend.copilot.pending_messages import peek_pending_messages
|
||||
from backend.copilot.rate_limit import (
|
||||
CoPilotUsagePublic,
|
||||
CoPilotUsageStatus,
|
||||
RateLimitExceeded,
|
||||
acquire_reset_lock,
|
||||
check_rate_limit,
|
||||
@@ -46,7 +41,6 @@ from backend.copilot.rate_limit import (
|
||||
reset_daily_usage,
|
||||
)
|
||||
from backend.copilot.response_model import StreamError, StreamFinish, StreamHeartbeat
|
||||
from backend.copilot.service import strip_injected_context_for_display
|
||||
from backend.copilot.tools.e2b_sandbox import kill_sandbox
|
||||
from backend.copilot.tools.models import (
|
||||
AgentDetailsResponse,
|
||||
@@ -65,10 +59,6 @@ from backend.copilot.tools.models import (
|
||||
InputValidationErrorResponse,
|
||||
MCPToolOutputResponse,
|
||||
MCPToolsDiscoveredResponse,
|
||||
MemoryForgetCandidatesResponse,
|
||||
MemoryForgetConfirmResponse,
|
||||
MemorySearchResponse,
|
||||
MemoryStoreResponse,
|
||||
NeedLoginResponse,
|
||||
NoResultsResponse,
|
||||
SetupRequirementsResponse,
|
||||
@@ -79,7 +69,7 @@ from backend.copilot.tracking import track_user_message
|
||||
from backend.data.credit import UsageTransactionMetadata, get_user_credit_model
|
||||
from backend.data.redis_client import get_redis_async
|
||||
from backend.data.understanding import get_business_understanding
|
||||
from backend.data.workspace import build_files_block, resolve_workspace_files
|
||||
from backend.data.workspace import get_or_create_workspace
|
||||
from backend.util.exceptions import InsufficientBalanceError, NotFoundError
|
||||
from backend.util.settings import Settings
|
||||
|
||||
@@ -89,6 +79,10 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
config = ChatConfig()
|
||||
|
||||
_UUID_RE = re.compile(
|
||||
r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", re.I
|
||||
)
|
||||
|
||||
|
||||
async def _validate_and_get_session(
|
||||
session_id: str,
|
||||
@@ -105,28 +99,6 @@ router = APIRouter(
|
||||
tags=["chat"],
|
||||
)
|
||||
|
||||
|
||||
def _strip_injected_context(message: dict) -> dict:
|
||||
"""Hide server-injected context blocks from the API response.
|
||||
|
||||
Returns a **shallow copy** of *message* with all server-injected XML
|
||||
blocks removed from ``content`` (if applicable). The original dict is
|
||||
never mutated, so callers can safely pass live session dicts without
|
||||
risking side-effects.
|
||||
|
||||
Handles all three injected block types — ``<memory_context>``,
|
||||
``<env_context>``, and ``<user_context>`` — regardless of the order they
|
||||
appear at the start of the message. Only ``user``-role messages with
|
||||
string content are touched; assistant / multimodal blocks pass through
|
||||
unchanged.
|
||||
"""
|
||||
if message.get("role") == "user" and isinstance(message.get("content"), str):
|
||||
result = message.copy()
|
||||
result["content"] = strip_injected_context_for_display(message["content"])
|
||||
return result
|
||||
return message
|
||||
|
||||
|
||||
# ========== Request/Response Models ==========
|
||||
|
||||
|
||||
@@ -144,24 +116,6 @@ class StreamChatRequest(BaseModel):
|
||||
description="Autopilot mode: 'fast' for baseline LLM, 'extended_thinking' for Claude Agent SDK. "
|
||||
"If None, uses the server default (extended_thinking).",
|
||||
)
|
||||
model: CopilotLlmModel | None = Field(
|
||||
default=None,
|
||||
description="Model tier: 'standard' for the default model, 'advanced' for the highest-capability model. "
|
||||
"If None, the server applies per-user LD targeting then falls back to config.",
|
||||
)
|
||||
|
||||
|
||||
class PeekPendingMessagesResponse(BaseModel):
|
||||
"""Response for the pending-message peek (GET) endpoint.
|
||||
|
||||
Returns a read-only view of the pending buffer — messages are NOT
|
||||
consumed. The frontend uses this to restore the queued-message
|
||||
indicator after a page refresh and to decide when to clear it once
|
||||
a turn has ended.
|
||||
"""
|
||||
|
||||
messages: list[str]
|
||||
count: int
|
||||
|
||||
|
||||
class CreateSessionRequest(BaseModel):
|
||||
@@ -201,8 +155,6 @@ class SessionDetailResponse(BaseModel):
|
||||
user_id: str | None
|
||||
messages: list[dict]
|
||||
active_stream: ActiveStreamInfo | None = None # Present if stream is still active
|
||||
has_more_messages: bool = False
|
||||
oldest_sequence: int | None = None
|
||||
total_prompt_tokens: int = 0
|
||||
total_completion_tokens: int = 0
|
||||
metadata: ChatSessionMetadata = ChatSessionMetadata()
|
||||
@@ -399,31 +351,6 @@ async def delete_session(
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@router.delete(
|
||||
"/sessions/{session_id}/stream",
|
||||
dependencies=[Security(auth.requires_user)],
|
||||
status_code=204,
|
||||
)
|
||||
async def disconnect_session_stream(
|
||||
session_id: str,
|
||||
user_id: Annotated[str, Security(auth.get_user_id)],
|
||||
) -> Response:
|
||||
"""Disconnect all active SSE listeners for a session.
|
||||
|
||||
Called by the frontend when the user switches away from a chat so the
|
||||
backend releases XREAD listeners immediately rather than waiting for
|
||||
the 5-10 s timeout.
|
||||
"""
|
||||
session = await get_chat_session(session_id, user_id)
|
||||
if not session:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Session {session_id} not found or access denied",
|
||||
)
|
||||
await stream_registry.disconnect_all_listeners(session_id)
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@router.patch(
|
||||
"/sessions/{session_id}/title",
|
||||
summary="Update session title",
|
||||
@@ -467,67 +394,60 @@ async def update_session_title_route(
|
||||
async def get_session(
|
||||
session_id: str,
|
||||
user_id: Annotated[str, Security(auth.get_user_id)],
|
||||
limit: int = Query(default=50, ge=1, le=200),
|
||||
before_sequence: int | None = Query(default=None, ge=0),
|
||||
) -> SessionDetailResponse:
|
||||
"""
|
||||
Retrieve the details of a specific chat session.
|
||||
|
||||
Supports cursor-based pagination via ``limit`` and ``before_sequence``.
|
||||
When no pagination params are provided, returns the most recent messages.
|
||||
Looks up a chat session by ID for the given user (if authenticated) and returns all session data including messages.
|
||||
If there's an active stream for this session, returns active_stream info for reconnection.
|
||||
|
||||
Args:
|
||||
session_id: The unique identifier for the desired chat session.
|
||||
user_id: The optional authenticated user ID, or None for anonymous access.
|
||||
|
||||
Returns:
|
||||
SessionDetailResponse: Details for the requested session, including active_stream info if applicable.
|
||||
|
||||
"""
|
||||
page = await get_chat_messages_paginated(
|
||||
session_id, limit, before_sequence, user_id=user_id
|
||||
)
|
||||
if page is None:
|
||||
session = await get_chat_session(session_id, user_id)
|
||||
if not session:
|
||||
raise NotFoundError(f"Session {session_id} not found.")
|
||||
|
||||
messages = [
|
||||
_strip_injected_context(message.model_dump()) for message in page.messages
|
||||
]
|
||||
messages = [message.model_dump() for message in session.messages]
|
||||
|
||||
# Only check active stream on initial load (not on "load more" requests)
|
||||
# Check if there's an active stream for this session
|
||||
active_stream_info = None
|
||||
if before_sequence is None:
|
||||
active_session, last_message_id = await stream_registry.get_active_session(
|
||||
session_id, user_id
|
||||
)
|
||||
if active_session:
|
||||
active_stream_info = ActiveStreamInfo(
|
||||
turn_id=active_session.turn_id,
|
||||
last_message_id=last_message_id,
|
||||
)
|
||||
|
||||
# Skip session metadata on "load more" — frontend only needs messages
|
||||
if before_sequence is not None:
|
||||
return SessionDetailResponse(
|
||||
id=page.session.session_id,
|
||||
created_at=page.session.started_at.isoformat(),
|
||||
updated_at=page.session.updated_at.isoformat(),
|
||||
user_id=page.session.user_id or None,
|
||||
messages=messages,
|
||||
active_stream=None,
|
||||
has_more_messages=page.has_more,
|
||||
oldest_sequence=page.oldest_sequence,
|
||||
total_prompt_tokens=0,
|
||||
total_completion_tokens=0,
|
||||
active_session, last_message_id = await stream_registry.get_active_session(
|
||||
session_id, user_id
|
||||
)
|
||||
logger.info(
|
||||
f"[GET_SESSION] session={session_id}, active_session={active_session is not None}, "
|
||||
f"msg_count={len(messages)}, last_role={messages[-1].get('role') if messages else 'none'}"
|
||||
)
|
||||
if active_session:
|
||||
# Keep the assistant message (including tool_calls) so the frontend can
|
||||
# render the correct tool UI (e.g. CreateAgent with mini game).
|
||||
# convertChatSessionToUiMessages handles isComplete=false by setting
|
||||
# tool parts without output to state "input-available".
|
||||
active_stream_info = ActiveStreamInfo(
|
||||
turn_id=active_session.turn_id,
|
||||
last_message_id=last_message_id,
|
||||
)
|
||||
|
||||
total_prompt = sum(u.prompt_tokens for u in page.session.usage)
|
||||
total_completion = sum(u.completion_tokens for u in page.session.usage)
|
||||
# Sum token usage from session
|
||||
total_prompt = sum(u.prompt_tokens for u in session.usage)
|
||||
total_completion = sum(u.completion_tokens for u in session.usage)
|
||||
|
||||
return SessionDetailResponse(
|
||||
id=page.session.session_id,
|
||||
created_at=page.session.started_at.isoformat(),
|
||||
updated_at=page.session.updated_at.isoformat(),
|
||||
user_id=page.session.user_id or None,
|
||||
id=session.session_id,
|
||||
created_at=session.started_at.isoformat(),
|
||||
updated_at=session.updated_at.isoformat(),
|
||||
user_id=session.user_id or None,
|
||||
messages=messages,
|
||||
active_stream=active_stream_info,
|
||||
has_more_messages=page.has_more,
|
||||
oldest_sequence=page.oldest_sequence,
|
||||
total_prompt_tokens=total_prompt,
|
||||
total_completion_tokens=total_completion,
|
||||
metadata=page.session.metadata,
|
||||
metadata=session.metadata,
|
||||
)
|
||||
|
||||
|
||||
@@ -536,27 +456,23 @@ async def get_session(
|
||||
)
|
||||
async def get_copilot_usage(
|
||||
user_id: Annotated[str, Security(auth.get_user_id)],
|
||||
) -> CoPilotUsagePublic:
|
||||
) -> CoPilotUsageStatus:
|
||||
"""Get CoPilot usage status for the authenticated user.
|
||||
|
||||
Returns the percentage of the daily/weekly allowance used — not the
|
||||
raw spend or cap — so clients cannot derive per-turn cost or platform
|
||||
margins. Global defaults sourced from LaunchDarkly (falling back to
|
||||
config). Includes the user's rate-limit tier.
|
||||
Returns current token usage vs limits for daily and weekly windows.
|
||||
Global defaults sourced from LaunchDarkly (falling back to config).
|
||||
Includes the user's rate-limit tier.
|
||||
"""
|
||||
daily_limit, weekly_limit, tier = await get_global_rate_limits(
|
||||
user_id,
|
||||
config.daily_cost_limit_microdollars,
|
||||
config.weekly_cost_limit_microdollars,
|
||||
user_id, config.daily_token_limit, config.weekly_token_limit
|
||||
)
|
||||
status = await get_usage_status(
|
||||
return await get_usage_status(
|
||||
user_id=user_id,
|
||||
daily_cost_limit=daily_limit,
|
||||
weekly_cost_limit=weekly_limit,
|
||||
daily_token_limit=daily_limit,
|
||||
weekly_token_limit=weekly_limit,
|
||||
rate_limit_reset_cost=config.rate_limit_reset_cost,
|
||||
tier=tier,
|
||||
)
|
||||
return CoPilotUsagePublic.from_status(status)
|
||||
|
||||
|
||||
class RateLimitResetResponse(BaseModel):
|
||||
@@ -565,9 +481,7 @@ class RateLimitResetResponse(BaseModel):
|
||||
success: bool
|
||||
credits_charged: int = Field(description="Credits charged (in cents)")
|
||||
remaining_balance: int = Field(description="Credit balance after charge (in cents)")
|
||||
usage: CoPilotUsagePublic = Field(
|
||||
description="Updated usage status after reset (percentages only)"
|
||||
)
|
||||
usage: CoPilotUsageStatus = Field(description="Updated usage status after reset")
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -591,7 +505,7 @@ async def reset_copilot_usage(
|
||||
) -> RateLimitResetResponse:
|
||||
"""Reset the daily CoPilot rate limit by spending credits.
|
||||
|
||||
Allows users who have hit their daily cost limit to spend credits
|
||||
Allows users who have hit their daily token limit to spend credits
|
||||
to reset their daily usage counter and continue working.
|
||||
Returns 400 if the feature is disabled or the user is not over the limit.
|
||||
Returns 402 if the user has insufficient credits.
|
||||
@@ -610,9 +524,7 @@ async def reset_copilot_usage(
|
||||
)
|
||||
|
||||
daily_limit, weekly_limit, tier = await get_global_rate_limits(
|
||||
user_id,
|
||||
config.daily_cost_limit_microdollars,
|
||||
config.weekly_cost_limit_microdollars,
|
||||
user_id, config.daily_token_limit, config.weekly_token_limit
|
||||
)
|
||||
|
||||
if daily_limit <= 0:
|
||||
@@ -649,8 +561,8 @@ async def reset_copilot_usage(
|
||||
# used for limit checks, not returned to the client.)
|
||||
usage_status = await get_usage_status(
|
||||
user_id=user_id,
|
||||
daily_cost_limit=daily_limit,
|
||||
weekly_cost_limit=weekly_limit,
|
||||
daily_token_limit=daily_limit,
|
||||
weekly_token_limit=weekly_limit,
|
||||
tier=tier,
|
||||
)
|
||||
if daily_limit > 0 and usage_status.daily.used < daily_limit:
|
||||
@@ -685,7 +597,7 @@ async def reset_copilot_usage(
|
||||
|
||||
# Reset daily usage in Redis. If this fails, refund the credits
|
||||
# so the user is not charged for a service they did not receive.
|
||||
if not await reset_daily_usage(user_id, daily_cost_limit=daily_limit):
|
||||
if not await reset_daily_usage(user_id, daily_token_limit=daily_limit):
|
||||
# Compensate: refund the charged credits.
|
||||
refunded = False
|
||||
try:
|
||||
@@ -721,11 +633,11 @@ async def reset_copilot_usage(
|
||||
finally:
|
||||
await release_reset_lock(user_id)
|
||||
|
||||
# Return updated usage status (public schema — percentages only).
|
||||
# Return updated usage status.
|
||||
updated_usage = await get_usage_status(
|
||||
user_id=user_id,
|
||||
daily_cost_limit=daily_limit,
|
||||
weekly_cost_limit=weekly_limit,
|
||||
daily_token_limit=daily_limit,
|
||||
weekly_token_limit=weekly_limit,
|
||||
rate_limit_reset_cost=config.rate_limit_reset_cost,
|
||||
tier=tier,
|
||||
)
|
||||
@@ -734,7 +646,7 @@ async def reset_copilot_usage(
|
||||
success=True,
|
||||
credits_charged=cost,
|
||||
remaining_balance=remaining,
|
||||
usage=CoPilotUsagePublic.from_status(updated_usage),
|
||||
usage=updated_usage,
|
||||
)
|
||||
|
||||
|
||||
@@ -785,52 +697,36 @@ async def cancel_session_task(
|
||||
|
||||
@router.post(
|
||||
"/sessions/{session_id}/stream",
|
||||
responses={
|
||||
202: {
|
||||
"model": QueuePendingMessageResponse,
|
||||
"description": (
|
||||
"Session has a turn in flight — message queued into the pending "
|
||||
"buffer and will be picked up between tool-call rounds by the "
|
||||
"executor currently processing the turn."
|
||||
),
|
||||
},
|
||||
404: {"description": "Session not found or access denied"},
|
||||
429: {"description": "Cost rate-limit or call-frequency cap exceeded"},
|
||||
},
|
||||
)
|
||||
async def stream_chat_post(
|
||||
session_id: str,
|
||||
request: StreamChatRequest,
|
||||
user_id: str = Security(auth.get_user_id),
|
||||
):
|
||||
"""Start a new turn OR queue a follow-up — decided server-side.
|
||||
"""
|
||||
Stream chat responses for a session (POST with context support).
|
||||
|
||||
- **Session idle**: starts a turn. Returns an SSE stream (``text/event-stream``)
|
||||
with Vercel AI SDK chunks (text fragments, tool-call UI, tool results).
|
||||
The generation runs in a background task that survives client disconnects;
|
||||
reconnect via ``GET /sessions/{session_id}/stream`` to resume.
|
||||
Streams the AI/completion responses in real time over Server-Sent Events (SSE), including:
|
||||
- Text fragments as they are generated
|
||||
- Tool call UI elements (if invoked)
|
||||
- Tool execution results
|
||||
|
||||
- **Session has a turn in flight**: pushes the message into the per-session
|
||||
pending buffer and returns ``202 application/json`` with
|
||||
``QueuePendingMessageResponse``. The executor running the current turn
|
||||
drains the buffer between tool-call rounds (baseline) or at the start of
|
||||
the next turn (SDK). Clients should detect the 202 and surface the
|
||||
message as a queued-chip in the UI.
|
||||
The AI generation runs in a background task that continues even if the client disconnects.
|
||||
All chunks are written to a per-turn Redis stream for reconnection support. If the client
|
||||
disconnects, they can reconnect using GET /sessions/{session_id}/stream to resume.
|
||||
|
||||
Args:
|
||||
session_id: The chat session identifier.
|
||||
request: Request body with message, is_user_message, and optional context.
|
||||
session_id: The chat session identifier to associate with the streamed messages.
|
||||
request: Request body containing message, is_user_message, and optional context.
|
||||
user_id: Authenticated user ID.
|
||||
Returns:
|
||||
StreamingResponse: SSE-formatted response chunks.
|
||||
|
||||
"""
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
stream_start_time = time.perf_counter()
|
||||
# Wall-clock arrival time, propagated to the executor so the turn-start
|
||||
# drain can order pending messages relative to this request (pending
|
||||
# pushed BEFORE this instant were typed earlier; pending pushed AFTER
|
||||
# are race-path follow-ups typed while /stream was still processing).
|
||||
request_arrival_at = time.time()
|
||||
log_meta = {"component": "ChatStream", "session_id": session_id, "user_id": user_id}
|
||||
|
||||
logger.info(
|
||||
@@ -839,26 +735,6 @@ async def stream_chat_post(
|
||||
extra={"json_fields": log_meta},
|
||||
)
|
||||
await _validate_and_get_session(session_id, user_id)
|
||||
|
||||
# Self-defensive queue-fallback: if a turn is already running, don't race
|
||||
# it on the cluster lock — drop the message into the pending buffer and
|
||||
# return 202 so the caller can render a chip. Both UI chips and autopilot
|
||||
# block follow-ups route through this path; keeping the decision on the
|
||||
# server means every caller gets uniform behaviour.
|
||||
if (
|
||||
request.is_user_message
|
||||
and request.message
|
||||
and await is_turn_in_flight(session_id)
|
||||
):
|
||||
response = await queue_pending_for_http(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
message=request.message,
|
||||
context=request.context,
|
||||
file_ids=request.file_ids,
|
||||
)
|
||||
return JSONResponse(status_code=202, content=response.model_dump())
|
||||
|
||||
logger.info(
|
||||
f"[TIMING] session validated in {(time.perf_counter() - stream_start_time) * 1000:.1f}ms",
|
||||
extra={
|
||||
@@ -869,20 +745,18 @@ async def stream_chat_post(
|
||||
},
|
||||
)
|
||||
|
||||
# Pre-turn rate limit check (cost-based, microdollars).
|
||||
# Pre-turn rate limit check (token-based).
|
||||
# check_rate_limit short-circuits internally when both limits are 0.
|
||||
# Global defaults sourced from LaunchDarkly, falling back to config.
|
||||
if user_id:
|
||||
try:
|
||||
daily_limit, weekly_limit, _ = await get_global_rate_limits(
|
||||
user_id,
|
||||
config.daily_cost_limit_microdollars,
|
||||
config.weekly_cost_limit_microdollars,
|
||||
user_id, config.daily_token_limit, config.weekly_token_limit
|
||||
)
|
||||
await check_rate_limit(
|
||||
user_id=user_id,
|
||||
daily_cost_limit=daily_limit,
|
||||
weekly_cost_limit=weekly_limit,
|
||||
daily_token_limit=daily_limit,
|
||||
weekly_token_limit=weekly_limit,
|
||||
)
|
||||
except RateLimitExceeded as e:
|
||||
raise HTTPException(status_code=429, detail=str(e)) from e
|
||||
@@ -891,74 +765,88 @@ async def stream_chat_post(
|
||||
# Also sanitise file_ids so only validated, workspace-scoped IDs are
|
||||
# forwarded downstream (e.g. to the executor via enqueue_copilot_turn).
|
||||
sanitized_file_ids: list[str] | None = None
|
||||
if request.file_ids:
|
||||
files = await resolve_workspace_files(user_id, request.file_ids)
|
||||
sanitized_file_ids = [wf.id for wf in files] or None
|
||||
request.message += build_files_block(files)
|
||||
if request.file_ids and user_id:
|
||||
# Filter to valid UUIDs only to prevent DB abuse
|
||||
valid_ids = [fid for fid in request.file_ids if _UUID_RE.match(fid)]
|
||||
|
||||
if valid_ids:
|
||||
workspace = await get_or_create_workspace(user_id)
|
||||
# Batch query instead of N+1
|
||||
files = await UserWorkspaceFile.prisma().find_many(
|
||||
where={
|
||||
"id": {"in": valid_ids},
|
||||
"workspaceId": workspace.id,
|
||||
"isDeleted": False,
|
||||
}
|
||||
)
|
||||
# Only keep IDs that actually exist in the user's workspace
|
||||
sanitized_file_ids = [wf.id for wf in files] or None
|
||||
file_lines: list[str] = [
|
||||
f"- {wf.name} ({wf.mimeType}, {round(wf.sizeBytes / 1024, 1)} KB), file_id={wf.id}"
|
||||
for wf in files
|
||||
]
|
||||
if file_lines:
|
||||
files_block = (
|
||||
"\n\n[Attached files]\n"
|
||||
+ "\n".join(file_lines)
|
||||
+ "\nUse read_workspace_file with the file_id to access file contents."
|
||||
)
|
||||
request.message += files_block
|
||||
|
||||
# Atomically append user message to session BEFORE creating task to avoid
|
||||
# race condition where GET_SESSION sees task as "running" but message isn't
|
||||
# saved yet. append_and_save_message returns None when a duplicate is
|
||||
# detected — in that case skip enqueue to avoid processing the message twice.
|
||||
is_duplicate_message = False
|
||||
# saved yet. append_and_save_message re-fetches inside a lock to prevent
|
||||
# message loss from concurrent requests.
|
||||
if request.message:
|
||||
message = ChatMessage(
|
||||
role="user" if request.is_user_message else "assistant",
|
||||
content=request.message,
|
||||
)
|
||||
logger.info(f"[STREAM] Saving user message to session {session_id}")
|
||||
is_duplicate_message = (
|
||||
await append_and_save_message(session_id, message)
|
||||
) is None
|
||||
logger.info(f"[STREAM] User message saved for session {session_id}")
|
||||
if not is_duplicate_message and request.is_user_message:
|
||||
if request.is_user_message:
|
||||
track_user_message(
|
||||
user_id=user_id,
|
||||
session_id=session_id,
|
||||
message_length=len(request.message),
|
||||
)
|
||||
logger.info(f"[STREAM] Saving user message to session {session_id}")
|
||||
await append_and_save_message(session_id, message)
|
||||
logger.info(f"[STREAM] User message saved for session {session_id}")
|
||||
|
||||
# Create a task in the stream registry for reconnection support.
|
||||
# For duplicate messages, skip create_session entirely so the infra-retry
|
||||
# client subscribes to the *existing* turn's Redis stream and receives the
|
||||
# in-progress executor output rather than an empty stream.
|
||||
turn_id = ""
|
||||
if not is_duplicate_message:
|
||||
turn_id = str(uuid4())
|
||||
log_meta["turn_id"] = turn_id
|
||||
session_create_start = time.perf_counter()
|
||||
await stream_registry.create_session(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
tool_call_id="chat_stream",
|
||||
tool_name="chat",
|
||||
turn_id=turn_id,
|
||||
)
|
||||
logger.info(
|
||||
f"[TIMING] create_session completed in {(time.perf_counter() - session_create_start) * 1000:.1f}ms",
|
||||
extra={
|
||||
"json_fields": {
|
||||
**log_meta,
|
||||
"duration_ms": (time.perf_counter() - session_create_start) * 1000,
|
||||
}
|
||||
},
|
||||
)
|
||||
await enqueue_copilot_turn(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
message=request.message,
|
||||
turn_id=turn_id,
|
||||
is_user_message=request.is_user_message,
|
||||
context=request.context,
|
||||
file_ids=sanitized_file_ids,
|
||||
mode=request.mode,
|
||||
model=request.model,
|
||||
request_arrival_at=request_arrival_at,
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
f"[STREAM] Duplicate message detected for session {session_id}, skipping enqueue"
|
||||
)
|
||||
# Create a task in the stream registry for reconnection support
|
||||
turn_id = str(uuid4())
|
||||
log_meta["turn_id"] = turn_id
|
||||
|
||||
session_create_start = time.perf_counter()
|
||||
await stream_registry.create_session(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
tool_call_id="chat_stream",
|
||||
tool_name="chat",
|
||||
turn_id=turn_id,
|
||||
)
|
||||
logger.info(
|
||||
f"[TIMING] create_session completed in {(time.perf_counter() - session_create_start) * 1000:.1f}ms",
|
||||
extra={
|
||||
"json_fields": {
|
||||
**log_meta,
|
||||
"duration_ms": (time.perf_counter() - session_create_start) * 1000,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Per-turn stream is always fresh (unique turn_id), subscribe from beginning
|
||||
subscribe_from_id = "0-0"
|
||||
|
||||
await enqueue_copilot_turn(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
message=request.message,
|
||||
turn_id=turn_id,
|
||||
is_user_message=request.is_user_message,
|
||||
context=request.context,
|
||||
file_ids=sanitized_file_ids,
|
||||
mode=request.mode,
|
||||
)
|
||||
|
||||
setup_time = (time.perf_counter() - stream_start_time) * 1000
|
||||
logger.info(
|
||||
@@ -966,9 +854,6 @@ async def stream_chat_post(
|
||||
extra={"json_fields": {**log_meta, "setup_time_ms": setup_time}},
|
||||
)
|
||||
|
||||
# Per-turn stream is always fresh (unique turn_id), subscribe from beginning
|
||||
subscribe_from_id = "0-0"
|
||||
|
||||
# SSE endpoint that subscribes to the task's stream
|
||||
async def event_generator() -> AsyncGenerator[str, None]:
|
||||
import time as time_module
|
||||
@@ -993,6 +878,7 @@ async def stream_chat_post(
|
||||
|
||||
if subscriber_queue is None:
|
||||
yield StreamFinish().to_sse()
|
||||
yield "data: [DONE]\n\n"
|
||||
return
|
||||
|
||||
# Read from the subscriber queue and yield to SSE
|
||||
@@ -1022,6 +908,7 @@ async def stream_chat_post(
|
||||
|
||||
yield chunk.to_sse()
|
||||
|
||||
# Check for finish signal
|
||||
if isinstance(chunk, StreamFinish):
|
||||
total_time = time_module.perf_counter() - event_gen_start
|
||||
logger.info(
|
||||
@@ -1036,7 +923,6 @@ async def stream_chat_post(
|
||||
},
|
||||
)
|
||||
break
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
yield StreamHeartbeat().to_sse()
|
||||
|
||||
@@ -1051,6 +937,7 @@ async def stream_chat_post(
|
||||
}
|
||||
},
|
||||
)
|
||||
pass # Client disconnected - background task continues
|
||||
except Exception as e:
|
||||
elapsed = (time_module.perf_counter() - event_gen_start) * 1000
|
||||
logger.error(
|
||||
@@ -1104,31 +991,6 @@ async def stream_chat_post(
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/sessions/{session_id}/messages/pending",
|
||||
response_model=PeekPendingMessagesResponse,
|
||||
responses={
|
||||
404: {"description": "Session not found or access denied"},
|
||||
},
|
||||
)
|
||||
async def get_pending_messages(
|
||||
session_id: str,
|
||||
user_id: str = Security(auth.get_user_id),
|
||||
):
|
||||
"""Peek at the pending-message buffer without consuming it.
|
||||
|
||||
Returns the current contents of the session's pending message buffer
|
||||
so the frontend can restore the queued-message indicator after a page
|
||||
refresh and clear it correctly once a turn drains the buffer.
|
||||
"""
|
||||
await _validate_and_get_session(session_id, user_id)
|
||||
pending = await peek_pending_messages(session_id)
|
||||
return PeekPendingMessagesResponse(
|
||||
messages=[m.content for m in pending],
|
||||
count=len(pending),
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/sessions/{session_id}/stream",
|
||||
)
|
||||
@@ -1381,10 +1243,6 @@ ToolResponseUnion = (
|
||||
| DocPageResponse
|
||||
| MCPToolsDiscoveredResponse
|
||||
| MCPToolOutputResponse
|
||||
| MemoryStoreResponse
|
||||
| MemorySearchResponse
|
||||
| MemoryForgetCandidatesResponse
|
||||
| MemoryForgetConfirmResponse
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import pytest
|
||||
import pytest_mock
|
||||
|
||||
from backend.api.features.chat import routes as chat_routes
|
||||
from backend.api.features.chat.routes import _strip_injected_context
|
||||
from backend.copilot.rate_limit import SubscriptionTier
|
||||
|
||||
app = fastapi.FastAPI()
|
||||
@@ -133,23 +132,16 @@ def test_stream_chat_rejects_too_many_file_ids():
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def _mock_stream_internals(mocker: pytest_mock.MockerFixture):
|
||||
def _mock_stream_internals(mocker: pytest_mock.MockFixture):
|
||||
"""Mock the async internals of stream_chat_post so tests can exercise
|
||||
validation and enrichment logic without needing RabbitMQ.
|
||||
|
||||
Returns:
|
||||
A namespace with ``save`` and ``enqueue`` mock objects so
|
||||
callers can make additional assertions about side-effects.
|
||||
"""
|
||||
import types
|
||||
|
||||
validation and enrichment logic without needing Redis/RabbitMQ."""
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes._validate_and_get_session",
|
||||
return_value=None,
|
||||
)
|
||||
mock_save = mocker.patch(
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.append_and_save_message",
|
||||
return_value=MagicMock(), # non-None = message was saved (not a duplicate)
|
||||
return_value=None,
|
||||
)
|
||||
mock_registry = mocker.MagicMock()
|
||||
mock_registry.create_session = mocker.AsyncMock(return_value=None)
|
||||
@@ -157,7 +149,7 @@ def _mock_stream_internals(mocker: pytest_mock.MockerFixture):
|
||||
"backend.api.features.chat.routes.stream_registry",
|
||||
mock_registry,
|
||||
)
|
||||
mock_enqueue = mocker.patch(
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.enqueue_copilot_turn",
|
||||
return_value=None,
|
||||
)
|
||||
@@ -165,17 +157,14 @@ def _mock_stream_internals(mocker: pytest_mock.MockerFixture):
|
||||
"backend.api.features.chat.routes.track_user_message",
|
||||
return_value=None,
|
||||
)
|
||||
return types.SimpleNamespace(
|
||||
save=mock_save, enqueue=mock_enqueue, registry=mock_registry
|
||||
)
|
||||
|
||||
|
||||
def test_stream_chat_accepts_20_file_ids(mocker: pytest_mock.MockerFixture):
|
||||
def test_stream_chat_accepts_20_file_ids(mocker: pytest_mock.MockFixture):
|
||||
"""Exactly 20 file_ids should be accepted (not rejected by validation)."""
|
||||
_mock_stream_internals(mocker)
|
||||
# Patch workspace lookup as imported by the routes module
|
||||
mocker.patch(
|
||||
"backend.data.workspace.get_or_create_workspace",
|
||||
"backend.api.features.chat.routes.get_or_create_workspace",
|
||||
return_value=type("W", (), {"id": "ws-1"})(),
|
||||
)
|
||||
mock_prisma = mocker.MagicMock()
|
||||
@@ -196,38 +185,15 @@ def test_stream_chat_accepts_20_file_ids(mocker: pytest_mock.MockerFixture):
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
# ─── Duplicate message dedup ──────────────────────────────────────────
|
||||
|
||||
|
||||
def test_stream_chat_skips_enqueue_for_duplicate_message(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
):
|
||||
"""When append_and_save_message returns None (duplicate detected),
|
||||
enqueue_copilot_turn and stream_registry.create_session must NOT be called
|
||||
to avoid double-processing and to prevent overwriting the active stream's
|
||||
turn_id in Redis (which would cause reconnecting clients to miss the response)."""
|
||||
mocks = _mock_stream_internals(mocker)
|
||||
# Override save to return None — signalling a duplicate
|
||||
mocks.save.return_value = None
|
||||
|
||||
response = client.post(
|
||||
"/sessions/sess-1/stream",
|
||||
json={"message": "hello"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
mocks.enqueue.assert_not_called()
|
||||
mocks.registry.create_session.assert_not_called()
|
||||
|
||||
|
||||
# ─── UUID format filtering ─────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_file_ids_filters_invalid_uuids(mocker: pytest_mock.MockerFixture):
|
||||
def test_file_ids_filters_invalid_uuids(mocker: pytest_mock.MockFixture):
|
||||
"""Non-UUID strings in file_ids should be silently filtered out
|
||||
and NOT passed to the database query."""
|
||||
_mock_stream_internals(mocker)
|
||||
mocker.patch(
|
||||
"backend.data.workspace.get_or_create_workspace",
|
||||
"backend.api.features.chat.routes.get_or_create_workspace",
|
||||
return_value=type("W", (), {"id": "ws-1"})(),
|
||||
)
|
||||
|
||||
@@ -261,11 +227,11 @@ def test_file_ids_filters_invalid_uuids(mocker: pytest_mock.MockerFixture):
|
||||
# ─── Cross-workspace file_ids ─────────────────────────────────────────
|
||||
|
||||
|
||||
def test_file_ids_scoped_to_workspace(mocker: pytest_mock.MockerFixture):
|
||||
def test_file_ids_scoped_to_workspace(mocker: pytest_mock.MockFixture):
|
||||
"""The batch query should scope to the user's workspace."""
|
||||
_mock_stream_internals(mocker)
|
||||
mocker.patch(
|
||||
"backend.data.workspace.get_or_create_workspace",
|
||||
"backend.api.features.chat.routes.get_or_create_workspace",
|
||||
return_value=type("W", (), {"id": "my-workspace-id"})(),
|
||||
)
|
||||
|
||||
@@ -290,14 +256,14 @@ def test_file_ids_scoped_to_workspace(mocker: pytest_mock.MockerFixture):
|
||||
# ─── Rate limit → 429 ─────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_stream_chat_returns_429_on_daily_rate_limit(mocker: pytest_mock.MockerFixture):
|
||||
def test_stream_chat_returns_429_on_daily_rate_limit(mocker: pytest_mock.MockFixture):
|
||||
"""When check_rate_limit raises RateLimitExceeded for daily limit the endpoint returns 429."""
|
||||
from backend.copilot.rate_limit import RateLimitExceeded
|
||||
|
||||
_mock_stream_internals(mocker)
|
||||
# Ensure the rate-limit branch is entered by setting a non-zero limit.
|
||||
mocker.patch.object(chat_routes.config, "daily_cost_limit_microdollars", 10000)
|
||||
mocker.patch.object(chat_routes.config, "weekly_cost_limit_microdollars", 50000)
|
||||
mocker.patch.object(chat_routes.config, "daily_token_limit", 10000)
|
||||
mocker.patch.object(chat_routes.config, "weekly_token_limit", 50000)
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.check_rate_limit",
|
||||
side_effect=RateLimitExceeded("daily", datetime.now(UTC) + timedelta(hours=1)),
|
||||
@@ -311,15 +277,13 @@ def test_stream_chat_returns_429_on_daily_rate_limit(mocker: pytest_mock.MockerF
|
||||
assert "daily" in response.json()["detail"].lower()
|
||||
|
||||
|
||||
def test_stream_chat_returns_429_on_weekly_rate_limit(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
):
|
||||
def test_stream_chat_returns_429_on_weekly_rate_limit(mocker: pytest_mock.MockFixture):
|
||||
"""When check_rate_limit raises RateLimitExceeded for weekly limit the endpoint returns 429."""
|
||||
from backend.copilot.rate_limit import RateLimitExceeded
|
||||
|
||||
_mock_stream_internals(mocker)
|
||||
mocker.patch.object(chat_routes.config, "daily_cost_limit_microdollars", 10000)
|
||||
mocker.patch.object(chat_routes.config, "weekly_cost_limit_microdollars", 50000)
|
||||
mocker.patch.object(chat_routes.config, "daily_token_limit", 10000)
|
||||
mocker.patch.object(chat_routes.config, "weekly_token_limit", 50000)
|
||||
resets_at = datetime.now(UTC) + timedelta(days=3)
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.check_rate_limit",
|
||||
@@ -336,13 +300,13 @@ def test_stream_chat_returns_429_on_weekly_rate_limit(
|
||||
assert "resets in" in detail
|
||||
|
||||
|
||||
def test_stream_chat_429_includes_reset_time(mocker: pytest_mock.MockerFixture):
|
||||
def test_stream_chat_429_includes_reset_time(mocker: pytest_mock.MockFixture):
|
||||
"""The 429 response detail should include the human-readable reset time."""
|
||||
from backend.copilot.rate_limit import RateLimitExceeded
|
||||
|
||||
_mock_stream_internals(mocker)
|
||||
mocker.patch.object(chat_routes.config, "daily_cost_limit_microdollars", 10000)
|
||||
mocker.patch.object(chat_routes.config, "weekly_cost_limit_microdollars", 50000)
|
||||
mocker.patch.object(chat_routes.config, "daily_token_limit", 10000)
|
||||
mocker.patch.object(chat_routes.config, "weekly_token_limit", 50000)
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.check_rate_limit",
|
||||
side_effect=RateLimitExceeded(
|
||||
@@ -402,33 +366,23 @@ def test_usage_returns_daily_and_weekly(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
test_user_id: str,
|
||||
) -> None:
|
||||
"""GET /usage returns percentages for daily and weekly windows only.
|
||||
|
||||
The raw used/limit microdollar values MUST NOT leak — clients should not
|
||||
be able to derive per-turn cost or platform margins from the public API.
|
||||
"""
|
||||
"""GET /usage returns daily and weekly usage."""
|
||||
mock_get = _mock_usage(mocker, daily_used=500, weekly_used=2000)
|
||||
|
||||
mocker.patch.object(chat_routes.config, "daily_cost_limit_microdollars", 10000)
|
||||
mocker.patch.object(chat_routes.config, "weekly_cost_limit_microdollars", 50000)
|
||||
mocker.patch.object(chat_routes.config, "daily_token_limit", 10000)
|
||||
mocker.patch.object(chat_routes.config, "weekly_token_limit", 50000)
|
||||
|
||||
response = client.get("/usage")
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
# 500 / 10000 = 5%, 2000 / 50000 = 4%
|
||||
assert data["daily"]["percent_used"] == 5.0
|
||||
assert data["weekly"]["percent_used"] == 4.0
|
||||
# Raw spend/limit must not be exposed.
|
||||
assert "used" not in data["daily"]
|
||||
assert "limit" not in data["daily"]
|
||||
assert "used" not in data["weekly"]
|
||||
assert "limit" not in data["weekly"]
|
||||
assert data["daily"]["used"] == 500
|
||||
assert data["weekly"]["used"] == 2000
|
||||
|
||||
mock_get.assert_called_once_with(
|
||||
user_id=test_user_id,
|
||||
daily_cost_limit=10000,
|
||||
weekly_cost_limit=50000,
|
||||
daily_token_limit=10000,
|
||||
weekly_token_limit=50000,
|
||||
rate_limit_reset_cost=chat_routes.config.rate_limit_reset_cost,
|
||||
tier=SubscriptionTier.FREE,
|
||||
)
|
||||
@@ -448,8 +402,8 @@ def test_usage_uses_config_limits(
|
||||
assert response.status_code == 200
|
||||
mock_get.assert_called_once_with(
|
||||
user_id=test_user_id,
|
||||
daily_cost_limit=99999,
|
||||
weekly_cost_limit=77777,
|
||||
daily_token_limit=99999,
|
||||
weekly_token_limit=77777,
|
||||
rate_limit_reset_cost=500,
|
||||
tier=SubscriptionTier.FREE,
|
||||
)
|
||||
@@ -625,441 +579,3 @@ class TestStreamChatRequestModeValidation:
|
||||
|
||||
req = StreamChatRequest(message="hi")
|
||||
assert req.mode is None
|
||||
|
||||
|
||||
# ─── POST /stream queue-fallback (when a turn is already in flight) ──
|
||||
|
||||
|
||||
def _mock_stream_queue_internals(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
*,
|
||||
session_exists: bool = True,
|
||||
turn_in_flight: bool = True,
|
||||
call_count: int = 1,
|
||||
):
|
||||
"""Mock dependencies for the POST /stream queue-fallback path.
|
||||
|
||||
When ``turn_in_flight`` is True the handler takes the 202 queue branch.
|
||||
"""
|
||||
if session_exists:
|
||||
mock_session = mocker.MagicMock()
|
||||
mock_session.id = "sess-1"
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes._validate_and_get_session",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_session,
|
||||
)
|
||||
else:
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes._validate_and_get_session",
|
||||
side_effect=fastapi.HTTPException(
|
||||
status_code=404, detail="Session not found."
|
||||
),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.is_turn_in_flight",
|
||||
new_callable=AsyncMock,
|
||||
return_value=turn_in_flight,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.get_global_rate_limits",
|
||||
new_callable=AsyncMock,
|
||||
return_value=(0, 0, None),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.check_rate_limit",
|
||||
new_callable=AsyncMock,
|
||||
return_value=None,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.pending_message_helpers.get_redis_async",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mocker.MagicMock(),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.pending_message_helpers.incr_with_ttl",
|
||||
new_callable=AsyncMock,
|
||||
return_value=call_count,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.pending_message_helpers.push_pending_message",
|
||||
new_callable=AsyncMock,
|
||||
return_value=1,
|
||||
)
|
||||
# queue_user_message re-runs is_turn_in_flight via the helper module —
|
||||
# stub that path out too so we don't need a fake stream_registry.
|
||||
mocker.patch(
|
||||
"backend.copilot.pending_message_helpers.get_active_session_meta",
|
||||
new_callable=AsyncMock,
|
||||
return_value=None,
|
||||
)
|
||||
|
||||
|
||||
def test_stream_queue_returns_202_when_turn_in_flight(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
"""Happy path: POST /stream to a session with a live turn → 202 queue."""
|
||||
_mock_stream_queue_internals(mocker)
|
||||
|
||||
response = client.post(
|
||||
"/sessions/sess-1/stream",
|
||||
json={"message": "follow-up", "is_user_message": True},
|
||||
)
|
||||
|
||||
assert response.status_code == 202
|
||||
data = response.json()
|
||||
assert data["buffer_length"] == 1
|
||||
assert "turn_in_flight" in data
|
||||
|
||||
|
||||
def test_stream_queue_session_not_found_returns_404(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
"""If the session doesn't exist or belong to the user, returns 404."""
|
||||
_mock_stream_queue_internals(mocker, session_exists=False)
|
||||
|
||||
response = client.post(
|
||||
"/sessions/bad-sess/stream",
|
||||
json={"message": "hi", "is_user_message": True},
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_stream_queue_call_frequency_limit_returns_429(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
"""Per-user call-frequency cap rejects rapid-fire queued pushes."""
|
||||
from backend.copilot.pending_message_helpers import PENDING_CALL_LIMIT
|
||||
|
||||
_mock_stream_queue_internals(mocker, call_count=PENDING_CALL_LIMIT + 1)
|
||||
|
||||
response = client.post(
|
||||
"/sessions/sess-1/stream",
|
||||
json={"message": "hi", "is_user_message": True},
|
||||
)
|
||||
assert response.status_code == 429
|
||||
assert "Too many queued message requests this minute" in response.json()["detail"]
|
||||
|
||||
|
||||
def test_stream_queue_converts_context_dict_to_pending_context(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
"""StreamChatRequest.context is a raw dict; must be coerced to the
|
||||
typed PendingMessageContext before being pushed onto the buffer."""
|
||||
_mock_stream_queue_internals(mocker)
|
||||
queue_spy = mocker.patch(
|
||||
"backend.copilot.pending_message_helpers.queue_user_message",
|
||||
new_callable=AsyncMock,
|
||||
)
|
||||
from backend.copilot.pending_message_helpers import QueuePendingMessageResponse
|
||||
|
||||
queue_spy.return_value = QueuePendingMessageResponse(
|
||||
buffer_length=1, max_buffer_length=10, turn_in_flight=True
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
"/sessions/sess-1/stream",
|
||||
json={
|
||||
"message": "hi",
|
||||
"is_user_message": True,
|
||||
"context": {"url": "https://example.test", "content": "body"},
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 202
|
||||
queue_spy.assert_awaited_once()
|
||||
kwargs = queue_spy.await_args.kwargs
|
||||
from backend.copilot.pending_messages import PendingMessageContext
|
||||
|
||||
assert isinstance(kwargs["context"], PendingMessageContext)
|
||||
assert kwargs["context"].url == "https://example.test"
|
||||
assert kwargs["context"].content == "body"
|
||||
|
||||
|
||||
def test_stream_queue_passes_none_context_when_omitted(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
"""When request.context is omitted, the queue call receives context=None."""
|
||||
_mock_stream_queue_internals(mocker)
|
||||
queue_spy = mocker.patch(
|
||||
"backend.copilot.pending_message_helpers.queue_user_message",
|
||||
new_callable=AsyncMock,
|
||||
)
|
||||
from backend.copilot.pending_message_helpers import QueuePendingMessageResponse
|
||||
|
||||
queue_spy.return_value = QueuePendingMessageResponse(
|
||||
buffer_length=1, max_buffer_length=10, turn_in_flight=True
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
"/sessions/sess-1/stream",
|
||||
json={"message": "hi", "is_user_message": True},
|
||||
)
|
||||
|
||||
assert response.status_code == 202
|
||||
queue_spy.assert_awaited_once()
|
||||
assert queue_spy.await_args.kwargs["context"] is None
|
||||
|
||||
|
||||
# ─── get_pending_messages (GET /sessions/{session_id}/messages/pending) ─────
|
||||
|
||||
|
||||
def test_get_pending_messages_returns_200_with_empty_buffer(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
"""Happy path: no pending messages returns 200 with empty list."""
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes._validate_and_get_session",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mocker.MagicMock(),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.peek_pending_messages",
|
||||
new_callable=AsyncMock,
|
||||
return_value=[],
|
||||
)
|
||||
|
||||
response = client.get("/sessions/sess-1/messages/pending")
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["messages"] == []
|
||||
assert data["count"] == 0
|
||||
|
||||
|
||||
def test_get_pending_messages_returns_queued_messages(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
"""Returns pending messages from buffer without consuming them."""
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes._validate_and_get_session",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mocker.MagicMock(),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.peek_pending_messages",
|
||||
new_callable=AsyncMock,
|
||||
return_value=[
|
||||
MagicMock(content="first message"),
|
||||
MagicMock(content="second message"),
|
||||
],
|
||||
)
|
||||
|
||||
response = client.get("/sessions/sess-1/messages/pending")
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["count"] == 2
|
||||
assert data["messages"] == ["first message", "second message"]
|
||||
|
||||
|
||||
def test_get_pending_messages_session_not_found_returns_404(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
) -> None:
|
||||
"""If session does not exist or belongs to another user, returns 404."""
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes._validate_and_get_session",
|
||||
side_effect=fastapi.HTTPException(status_code=404, detail="Session not found."),
|
||||
)
|
||||
|
||||
response = client.get("/sessions/bad-sess/messages/pending")
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
class TestStripInjectedContext:
|
||||
"""Unit tests for `_strip_injected_context` — the GET-side helper that
|
||||
hides the server-injected `<user_context>` block from API responses.
|
||||
|
||||
The strip is intentionally exact-match: it only removes the prefix the
|
||||
inject helper writes (`<user_context>...</user_context>\\n\\n` at the very
|
||||
start of the message). Any drift between writer and reader leaves the raw
|
||||
block visible in the chat history, which is the failure mode this suite
|
||||
documents.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def _msg(role: str, content):
|
||||
return {"role": role, "content": content}
|
||||
|
||||
def test_strips_well_formed_prefix(self) -> None:
|
||||
|
||||
original = "<user_context>\nbiz ctx\n</user_context>\n\nhello world"
|
||||
result = _strip_injected_context(self._msg("user", original))
|
||||
assert result["content"] == "hello world"
|
||||
|
||||
def test_passes_through_message_without_prefix(self) -> None:
|
||||
|
||||
result = _strip_injected_context(self._msg("user", "just a question"))
|
||||
assert result["content"] == "just a question"
|
||||
|
||||
def test_only_strips_when_prefix_is_at_start(self) -> None:
|
||||
"""An embedded `<user_context>` block later in the message must NOT
|
||||
be stripped — only the leading prefix is server-injected."""
|
||||
|
||||
content = (
|
||||
"I copied this from somewhere: <user_context>\nfoo\n</user_context>\n\n"
|
||||
)
|
||||
result = _strip_injected_context(self._msg("user", content))
|
||||
assert result["content"] == content
|
||||
|
||||
def test_does_not_strip_with_only_single_newline_separator(self) -> None:
|
||||
"""The strip regex requires `\\n\\n` after the closing tag — a single
|
||||
newline indicates a different format and must not be touched."""
|
||||
|
||||
content = "<user_context>\nfoo\n</user_context>\nhello"
|
||||
result = _strip_injected_context(self._msg("user", content))
|
||||
assert result["content"] == content
|
||||
|
||||
def test_assistant_messages_pass_through(self) -> None:
|
||||
|
||||
original = "<user_context>\nfoo\n</user_context>\n\nhi"
|
||||
result = _strip_injected_context(self._msg("assistant", original))
|
||||
assert result["content"] == original
|
||||
|
||||
def test_non_string_content_passes_through(self) -> None:
|
||||
"""Multimodal / structured content (e.g. list of blocks) is not a
|
||||
string and must not be touched by the strip helper."""
|
||||
|
||||
blocks = [{"type": "text", "text": "hello"}]
|
||||
result = _strip_injected_context(self._msg("user", blocks))
|
||||
assert result["content"] is blocks
|
||||
|
||||
def test_strip_with_multiline_understanding(self) -> None:
|
||||
"""The understanding payload spans multiple lines (markdown headings,
|
||||
bullet points). `re.DOTALL` must allow the regex to span them."""
|
||||
|
||||
original = (
|
||||
"<user_context>\n"
|
||||
"# User Business Context\n\n"
|
||||
"## User\nName: Alice\n\n"
|
||||
"## Business\nCompany: Acme\n"
|
||||
"</user_context>\n\nactual question"
|
||||
)
|
||||
result = _strip_injected_context(self._msg("user", original))
|
||||
assert result["content"] == "actual question"
|
||||
|
||||
def test_strip_when_message_is_only_the_prefix(self) -> None:
|
||||
"""An empty user message gets injected with just the prefix; the
|
||||
strip should yield an empty string."""
|
||||
|
||||
original = "<user_context>\nctx\n</user_context>\n\n"
|
||||
result = _strip_injected_context(self._msg("user", original))
|
||||
assert result["content"] == ""
|
||||
|
||||
def test_does_not_mutate_original_dict(self) -> None:
|
||||
"""The helper must return a copy — the original dict stays intact."""
|
||||
original_content = "<user_context>\nctx\n</user_context>\n\nhello"
|
||||
msg = self._msg("user", original_content)
|
||||
result = _strip_injected_context(msg)
|
||||
assert result["content"] == "hello"
|
||||
assert msg["content"] == original_content
|
||||
assert result is not msg
|
||||
|
||||
def test_no_role_field_does_not_crash(self) -> None:
|
||||
|
||||
msg = {"content": "hello"}
|
||||
result = _strip_injected_context(msg)
|
||||
# Without a role, the helper short-circuits without touching content.
|
||||
assert result["content"] == "hello"
|
||||
|
||||
|
||||
# ─── DELETE /sessions/{id}/stream — disconnect listeners ──────────────
|
||||
|
||||
|
||||
def test_disconnect_stream_returns_204_and_awaits_registry(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
test_user_id: str,
|
||||
) -> None:
|
||||
mock_session = MagicMock()
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.get_chat_session",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_session,
|
||||
)
|
||||
mock_disconnect = mocker.patch(
|
||||
"backend.api.features.chat.routes.stream_registry.disconnect_all_listeners",
|
||||
new_callable=AsyncMock,
|
||||
return_value=2,
|
||||
)
|
||||
|
||||
response = client.delete("/sessions/sess-1/stream")
|
||||
|
||||
assert response.status_code == 204
|
||||
mock_disconnect.assert_awaited_once_with("sess-1")
|
||||
|
||||
|
||||
def test_disconnect_stream_returns_404_when_session_missing(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
test_user_id: str,
|
||||
) -> None:
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.get_chat_session",
|
||||
new_callable=AsyncMock,
|
||||
return_value=None,
|
||||
)
|
||||
mock_disconnect = mocker.patch(
|
||||
"backend.api.features.chat.routes.stream_registry.disconnect_all_listeners",
|
||||
new_callable=AsyncMock,
|
||||
)
|
||||
|
||||
response = client.delete("/sessions/unknown-session/stream")
|
||||
|
||||
assert response.status_code == 404
|
||||
mock_disconnect.assert_not_awaited()
|
||||
|
||||
|
||||
# ─── GET /sessions/{session_id} — backward pagination ─────────────────────────
|
||||
|
||||
|
||||
def _make_paginated_messages(
|
||||
mocker: pytest_mock.MockerFixture, *, has_more: bool = False
|
||||
):
|
||||
"""Return a mock PaginatedMessages and configure the DB patch."""
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from backend.copilot.db import PaginatedMessages
|
||||
from backend.copilot.model import ChatMessage, ChatSessionInfo, ChatSessionMetadata
|
||||
|
||||
now = datetime.now(UTC)
|
||||
session_info = ChatSessionInfo(
|
||||
session_id="sess-1",
|
||||
user_id=TEST_USER_ID,
|
||||
usage=[],
|
||||
started_at=now,
|
||||
updated_at=now,
|
||||
metadata=ChatSessionMetadata(),
|
||||
)
|
||||
page = PaginatedMessages(
|
||||
messages=[ChatMessage(role="user", content="hello", sequence=0)],
|
||||
has_more=has_more,
|
||||
oldest_sequence=0,
|
||||
session=session_info,
|
||||
)
|
||||
mock_paginate = mocker.patch(
|
||||
"backend.api.features.chat.routes.get_chat_messages_paginated",
|
||||
new_callable=AsyncMock,
|
||||
return_value=page,
|
||||
)
|
||||
return page, mock_paginate
|
||||
|
||||
|
||||
def test_get_session_returns_backward_paginated(
|
||||
mocker: pytest_mock.MockerFixture,
|
||||
test_user_id: str,
|
||||
) -> None:
|
||||
"""All sessions use backward (newest-first) pagination."""
|
||||
_make_paginated_messages(mocker)
|
||||
mocker.patch(
|
||||
"backend.api.features.chat.routes.stream_registry.get_active_session",
|
||||
new_callable=AsyncMock,
|
||||
return_value=(None, None),
|
||||
)
|
||||
|
||||
response = client.get("/sessions/sess-1")
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["oldest_sequence"] == 0
|
||||
assert "forward_paginated" not in data
|
||||
assert "newest_sequence" not in data
|
||||
|
||||
@@ -12,7 +12,6 @@ import prisma.models
|
||||
|
||||
import backend.api.features.library.model as library_model
|
||||
import backend.data.graph as graph_db
|
||||
from backend.api.features.library.db import _fetch_schedule_info
|
||||
from backend.data.graph import GraphModel, GraphSettings
|
||||
from backend.data.includes import library_agent_include
|
||||
from backend.util.exceptions import NotFoundError
|
||||
@@ -118,5 +117,4 @@ async def add_graph_to_library(
|
||||
f"for store listing version #{store_listing_version_id} "
|
||||
f"to library for user #{user_id}"
|
||||
)
|
||||
schedule_info = await _fetch_schedule_info(user_id, graph_id=graph_model.id)
|
||||
return library_model.LibraryAgent.from_db(added_agent, schedule_info=schedule_info)
|
||||
return library_model.LibraryAgent.from_db(added_agent)
|
||||
|
||||
@@ -21,17 +21,13 @@ async def test_add_graph_to_library_create_new_agent() -> None:
|
||||
"backend.api.features.library._add_to_library.library_model.LibraryAgent.from_db",
|
||||
return_value=converted_agent,
|
||||
) as mock_from_db,
|
||||
patch(
|
||||
"backend.api.features.library._add_to_library._fetch_schedule_info",
|
||||
new=AsyncMock(return_value={}),
|
||||
),
|
||||
):
|
||||
mock_prisma.return_value.create = AsyncMock(return_value=created_agent)
|
||||
|
||||
result = await add_graph_to_library("slv-id", graph_model, "user-id")
|
||||
|
||||
assert result is converted_agent
|
||||
mock_from_db.assert_called_once_with(created_agent, schedule_info={})
|
||||
mock_from_db.assert_called_once_with(created_agent)
|
||||
# Verify create was called with correct data
|
||||
create_call = mock_prisma.return_value.create.call_args
|
||||
create_data = create_call.kwargs["data"]
|
||||
@@ -58,10 +54,6 @@ async def test_add_graph_to_library_unique_violation_updates_existing() -> None:
|
||||
"backend.api.features.library._add_to_library.library_model.LibraryAgent.from_db",
|
||||
return_value=converted_agent,
|
||||
) as mock_from_db,
|
||||
patch(
|
||||
"backend.api.features.library._add_to_library._fetch_schedule_info",
|
||||
new=AsyncMock(return_value={}),
|
||||
),
|
||||
):
|
||||
mock_prisma.return_value.create = AsyncMock(
|
||||
side_effect=prisma.errors.UniqueViolationError(
|
||||
@@ -73,7 +65,7 @@ async def test_add_graph_to_library_unique_violation_updates_existing() -> None:
|
||||
result = await add_graph_to_library("slv-id", graph_model, "user-id")
|
||||
|
||||
assert result is converted_agent
|
||||
mock_from_db.assert_called_once_with(updated_agent, schedule_info={})
|
||||
mock_from_db.assert_called_once_with(updated_agent)
|
||||
# Verify update was called with correct where and data
|
||||
update_call = mock_prisma.return_value.update.call_args
|
||||
assert update_call.kwargs["where"] == {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import asyncio
|
||||
import itertools
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from typing import Literal, Optional
|
||||
|
||||
import fastapi
|
||||
@@ -44,65 +43,6 @@ config = Config()
|
||||
integration_creds_manager = IntegrationCredentialsManager()
|
||||
|
||||
|
||||
async def _fetch_execution_counts(user_id: str, graph_ids: list[str]) -> dict[str, int]:
|
||||
"""Fetch execution counts per graph in a single batched query."""
|
||||
if not graph_ids:
|
||||
return {}
|
||||
rows = await prisma.models.AgentGraphExecution.prisma().group_by(
|
||||
by=["agentGraphId"],
|
||||
where={
|
||||
"userId": user_id,
|
||||
"agentGraphId": {"in": graph_ids},
|
||||
"isDeleted": False,
|
||||
},
|
||||
count=True,
|
||||
)
|
||||
return {
|
||||
row["agentGraphId"]: int((row.get("_count") or {}).get("_all") or 0)
|
||||
for row in rows
|
||||
}
|
||||
|
||||
|
||||
async def _fetch_schedule_info(
|
||||
user_id: str, graph_id: Optional[str] = None
|
||||
) -> dict[str, str]:
|
||||
"""Fetch a map of graph_id → earliest next_run_time ISO string.
|
||||
|
||||
When `graph_id` is provided, the scheduler query is narrowed to that graph,
|
||||
which is cheaper for single-agent lookups (detail page, post-update, etc.).
|
||||
"""
|
||||
try:
|
||||
scheduler_client = get_scheduler_client()
|
||||
schedules = await scheduler_client.get_execution_schedules(
|
||||
graph_id=graph_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
earliest: dict[str, tuple[datetime, str]] = {}
|
||||
for s in schedules:
|
||||
parsed = _parse_iso_datetime(s.next_run_time)
|
||||
if parsed is None:
|
||||
continue
|
||||
current = earliest.get(s.graph_id)
|
||||
if current is None or parsed < current[0]:
|
||||
earliest[s.graph_id] = (parsed, s.next_run_time)
|
||||
return {graph_id: iso for graph_id, (_, iso) in earliest.items()}
|
||||
except Exception:
|
||||
logger.warning("Failed to fetch schedules for library agents", exc_info=True)
|
||||
return {}
|
||||
|
||||
|
||||
def _parse_iso_datetime(value: str) -> Optional[datetime]:
|
||||
"""Parse an ISO 8601 datetime, tolerating `Z` and naive forms (assumed UTC)."""
|
||||
try:
|
||||
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
|
||||
except ValueError:
|
||||
logger.warning("Failed to parse schedule next_run_time: %s", value)
|
||||
return None
|
||||
if parsed.tzinfo is None:
|
||||
parsed = parsed.replace(tzinfo=timezone.utc)
|
||||
return parsed
|
||||
|
||||
|
||||
async def list_library_agents(
|
||||
user_id: str,
|
||||
search_term: Optional[str] = None,
|
||||
@@ -197,22 +137,12 @@ async def list_library_agents(
|
||||
|
||||
logger.debug(f"Retrieved {len(library_agents)} library agents for user #{user_id}")
|
||||
|
||||
graph_ids = [a.agentGraphId for a in library_agents if a.agentGraphId]
|
||||
execution_counts, schedule_info = await asyncio.gather(
|
||||
_fetch_execution_counts(user_id, graph_ids),
|
||||
_fetch_schedule_info(user_id),
|
||||
)
|
||||
|
||||
# Only pass valid agents to the response
|
||||
valid_library_agents: list[library_model.LibraryAgent] = []
|
||||
|
||||
for agent in library_agents:
|
||||
try:
|
||||
library_agent = library_model.LibraryAgent.from_db(
|
||||
agent,
|
||||
execution_count_override=execution_counts.get(agent.agentGraphId),
|
||||
schedule_info=schedule_info,
|
||||
)
|
||||
library_agent = library_model.LibraryAgent.from_db(agent)
|
||||
valid_library_agents.append(library_agent)
|
||||
except Exception as e:
|
||||
# Skip this agent if there was an error
|
||||
@@ -284,22 +214,12 @@ async def list_favorite_library_agents(
|
||||
f"Retrieved {len(library_agents)} favorite library agents for user #{user_id}"
|
||||
)
|
||||
|
||||
graph_ids = [a.agentGraphId for a in library_agents if a.agentGraphId]
|
||||
execution_counts, schedule_info = await asyncio.gather(
|
||||
_fetch_execution_counts(user_id, graph_ids),
|
||||
_fetch_schedule_info(user_id),
|
||||
)
|
||||
|
||||
# Only pass valid agents to the response
|
||||
valid_library_agents: list[library_model.LibraryAgent] = []
|
||||
|
||||
for agent in library_agents:
|
||||
try:
|
||||
library_agent = library_model.LibraryAgent.from_db(
|
||||
agent,
|
||||
execution_count_override=execution_counts.get(agent.agentGraphId),
|
||||
schedule_info=schedule_info,
|
||||
)
|
||||
library_agent = library_model.LibraryAgent.from_db(agent)
|
||||
valid_library_agents.append(library_agent)
|
||||
except Exception as e:
|
||||
# Skip this agent if there was an error
|
||||
@@ -365,12 +285,6 @@ async def get_library_agent(id: str, user_id: str) -> library_model.LibraryAgent
|
||||
where={"userId": store_listing.owningUserId}
|
||||
)
|
||||
|
||||
schedule_info = (
|
||||
await _fetch_schedule_info(user_id, graph_id=library_agent.AgentGraph.id)
|
||||
if library_agent.AgentGraph
|
||||
else {}
|
||||
)
|
||||
|
||||
return library_model.LibraryAgent.from_db(
|
||||
library_agent,
|
||||
sub_graphs=(
|
||||
@@ -380,7 +294,6 @@ async def get_library_agent(id: str, user_id: str) -> library_model.LibraryAgent
|
||||
),
|
||||
store_listing=store_listing,
|
||||
profile=profile,
|
||||
schedule_info=schedule_info,
|
||||
)
|
||||
|
||||
|
||||
@@ -416,10 +329,7 @@ async def get_library_agent_by_store_version_id(
|
||||
},
|
||||
include=library_agent_include(user_id),
|
||||
)
|
||||
if not agent:
|
||||
return None
|
||||
schedule_info = await _fetch_schedule_info(user_id, graph_id=agent.agentGraphId)
|
||||
return library_model.LibraryAgent.from_db(agent, schedule_info=schedule_info)
|
||||
return library_model.LibraryAgent.from_db(agent) if agent else None
|
||||
|
||||
|
||||
async def get_library_agent_by_graph_id(
|
||||
@@ -448,10 +358,7 @@ async def get_library_agent_by_graph_id(
|
||||
assert agent.AgentGraph # make type checker happy
|
||||
# Include sub-graphs so we can make a full credentials input schema
|
||||
sub_graphs = await graph_db.get_sub_graphs(agent.AgentGraph)
|
||||
schedule_info = await _fetch_schedule_info(user_id, graph_id=agent.agentGraphId)
|
||||
return library_model.LibraryAgent.from_db(
|
||||
agent, sub_graphs=sub_graphs, schedule_info=schedule_info
|
||||
)
|
||||
return library_model.LibraryAgent.from_db(agent, sub_graphs=sub_graphs)
|
||||
|
||||
|
||||
async def add_generated_agent_image(
|
||||
@@ -557,6 +464,9 @@ async def create_library_agent(
|
||||
sensitive_action_safe_mode=sensitive_action_safe_mode,
|
||||
).model_dump()
|
||||
),
|
||||
topIntegrations=SafeJson(
|
||||
library_model._compute_top_integrations(graph_entry)
|
||||
),
|
||||
**(
|
||||
{"Folder": {"connect": {"id": folder_id}}}
|
||||
if folder_id and graph_entry is graph
|
||||
@@ -574,6 +484,9 @@ async def create_library_agent(
|
||||
sensitive_action_safe_mode=sensitive_action_safe_mode,
|
||||
).model_dump()
|
||||
),
|
||||
"topIntegrations": SafeJson(
|
||||
library_model._compute_top_integrations(graph_entry)
|
||||
),
|
||||
**(
|
||||
{"Folder": {"connect": {"id": folder_id}}}
|
||||
if folder_id and graph_entry is graph
|
||||
@@ -593,11 +506,7 @@ async def create_library_agent(
|
||||
for agent, graph in zip(library_agents, graph_entries):
|
||||
asyncio.create_task(add_generated_agent_image(graph, user_id, agent.id))
|
||||
|
||||
schedule_info = await _fetch_schedule_info(user_id)
|
||||
return [
|
||||
library_model.LibraryAgent.from_db(agent, schedule_info=schedule_info)
|
||||
for agent in library_agents
|
||||
]
|
||||
return [library_model.LibraryAgent.from_db(agent) for agent in library_agents]
|
||||
|
||||
|
||||
async def update_agent_version_in_library(
|
||||
@@ -659,8 +568,7 @@ async def update_agent_version_in_library(
|
||||
f"Failed to update library agent for {agent_graph_id} v{agent_graph_version}"
|
||||
)
|
||||
|
||||
schedule_info = await _fetch_schedule_info(user_id, graph_id=agent_graph_id)
|
||||
return library_model.LibraryAgent.from_db(lib, schedule_info=schedule_info)
|
||||
return library_model.LibraryAgent.from_db(lib)
|
||||
|
||||
|
||||
async def create_graph_in_library(
|
||||
@@ -750,6 +658,15 @@ async def update_library_agent_version_and_settings(
|
||||
user_id=user_id,
|
||||
settings=updated_settings,
|
||||
)
|
||||
|
||||
# Recompute top integrations on version update
|
||||
top_integrations = library_model._compute_top_integrations(agent_graph)
|
||||
await prisma.models.LibraryAgent.prisma().update(
|
||||
where={"id": library.id},
|
||||
data={"topIntegrations": SafeJson(top_integrations)},
|
||||
)
|
||||
library.top_integrations = top_integrations
|
||||
|
||||
return library
|
||||
|
||||
|
||||
@@ -1565,11 +1482,7 @@ async def bulk_move_agents_to_folder(
|
||||
),
|
||||
)
|
||||
|
||||
schedule_info = await _fetch_schedule_info(user_id)
|
||||
return [
|
||||
library_model.LibraryAgent.from_db(agent, schedule_info=schedule_info)
|
||||
for agent in agents
|
||||
]
|
||||
return [library_model.LibraryAgent.from_db(agent) for agent in agents]
|
||||
|
||||
|
||||
def collect_tree_ids(
|
||||
|
||||
@@ -33,6 +33,7 @@ async def test_get_library_agents(mocker):
|
||||
userId="test-user",
|
||||
agentGraphId="agent2",
|
||||
settings="{}", # type: ignore
|
||||
topIntegrations="[]", # type: ignore
|
||||
agentGraphVersion=1,
|
||||
isCreatedByUser=False,
|
||||
isDeleted=False,
|
||||
@@ -65,11 +66,6 @@ async def test_get_library_agents(mocker):
|
||||
)
|
||||
mock_library_agent.return_value.count = mocker.AsyncMock(return_value=1)
|
||||
|
||||
mocker.patch(
|
||||
"backend.api.features.library.db._fetch_execution_counts",
|
||||
new=mocker.AsyncMock(return_value={}),
|
||||
)
|
||||
|
||||
# Call function
|
||||
result = await db.list_library_agents("test-user")
|
||||
|
||||
@@ -126,6 +122,7 @@ async def test_add_agent_to_library(mocker):
|
||||
userId="test-user",
|
||||
agentGraphId=mock_store_listing_data.agentGraphId,
|
||||
settings="{}", # type: ignore
|
||||
topIntegrations="[]", # type: ignore
|
||||
agentGraphVersion=1,
|
||||
isCreatedByUser=False,
|
||||
isDeleted=False,
|
||||
@@ -358,136 +355,3 @@ async def test_create_library_agent_uses_upsert():
|
||||
# Verify update branch restores soft-deleted/archived agents
|
||||
assert data["update"]["isDeleted"] is False
|
||||
assert data["update"]["isArchived"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_favorite_library_agents(mocker):
|
||||
mock_library_agents = [
|
||||
prisma.models.LibraryAgent(
|
||||
id="fav1",
|
||||
userId="test-user",
|
||||
agentGraphId="agent-fav",
|
||||
settings="{}", # type: ignore
|
||||
agentGraphVersion=1,
|
||||
isCreatedByUser=False,
|
||||
isDeleted=False,
|
||||
isArchived=False,
|
||||
createdAt=datetime.now(),
|
||||
updatedAt=datetime.now(),
|
||||
isFavorite=True,
|
||||
useGraphIsActiveVersion=True,
|
||||
AgentGraph=prisma.models.AgentGraph(
|
||||
id="agent-fav",
|
||||
version=1,
|
||||
name="Favorite Agent",
|
||||
description="My Favorite",
|
||||
userId="other-user",
|
||||
isActive=True,
|
||||
createdAt=datetime.now(),
|
||||
),
|
||||
)
|
||||
]
|
||||
|
||||
mock_library_agent = mocker.patch("prisma.models.LibraryAgent.prisma")
|
||||
mock_library_agent.return_value.find_many = mocker.AsyncMock(
|
||||
return_value=mock_library_agents
|
||||
)
|
||||
mock_library_agent.return_value.count = mocker.AsyncMock(return_value=1)
|
||||
|
||||
mocker.patch(
|
||||
"backend.api.features.library.db._fetch_execution_counts",
|
||||
new=mocker.AsyncMock(return_value={"agent-fav": 7}),
|
||||
)
|
||||
|
||||
result = await db.list_favorite_library_agents("test-user")
|
||||
|
||||
assert len(result.agents) == 1
|
||||
assert result.agents[0].id == "fav1"
|
||||
assert result.agents[0].name == "Favorite Agent"
|
||||
assert result.agents[0].graph_id == "agent-fav"
|
||||
assert result.pagination.total_items == 1
|
||||
assert result.pagination.total_pages == 1
|
||||
assert result.pagination.current_page == 1
|
||||
assert result.pagination.page_size == 50
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_library_agents_skips_failed_agent(mocker):
|
||||
"""Agents that fail parsing should be skipped — covers the except branch."""
|
||||
mock_library_agents = [
|
||||
prisma.models.LibraryAgent(
|
||||
id="ua-bad",
|
||||
userId="test-user",
|
||||
agentGraphId="agent-bad",
|
||||
settings="{}", # type: ignore
|
||||
agentGraphVersion=1,
|
||||
isCreatedByUser=False,
|
||||
isDeleted=False,
|
||||
isArchived=False,
|
||||
createdAt=datetime.now(),
|
||||
updatedAt=datetime.now(),
|
||||
isFavorite=False,
|
||||
useGraphIsActiveVersion=True,
|
||||
AgentGraph=prisma.models.AgentGraph(
|
||||
id="agent-bad",
|
||||
version=1,
|
||||
name="Bad Agent",
|
||||
description="",
|
||||
userId="other-user",
|
||||
isActive=True,
|
||||
createdAt=datetime.now(),
|
||||
),
|
||||
)
|
||||
]
|
||||
|
||||
mock_library_agent = mocker.patch("prisma.models.LibraryAgent.prisma")
|
||||
mock_library_agent.return_value.find_many = mocker.AsyncMock(
|
||||
return_value=mock_library_agents
|
||||
)
|
||||
mock_library_agent.return_value.count = mocker.AsyncMock(return_value=1)
|
||||
|
||||
mocker.patch(
|
||||
"backend.api.features.library.db._fetch_execution_counts",
|
||||
new=mocker.AsyncMock(return_value={}),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.library.model.LibraryAgent.from_db",
|
||||
side_effect=Exception("parse error"),
|
||||
)
|
||||
|
||||
result = await db.list_library_agents("test-user")
|
||||
|
||||
assert len(result.agents) == 0
|
||||
assert result.pagination.total_items == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_execution_counts_empty_graph_ids():
|
||||
result = await db._fetch_execution_counts("user-1", [])
|
||||
assert result == {}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_execution_counts_uses_group_by(mocker):
|
||||
mock_prisma = mocker.patch("prisma.models.AgentGraphExecution.prisma")
|
||||
mock_prisma.return_value.group_by = mocker.AsyncMock(
|
||||
return_value=[
|
||||
{"agentGraphId": "graph-1", "_count": {"_all": 5}},
|
||||
{"agentGraphId": "graph-2", "_count": {"_all": 2}},
|
||||
]
|
||||
)
|
||||
|
||||
result = await db._fetch_execution_counts(
|
||||
"user-1", ["graph-1", "graph-2", "graph-3"]
|
||||
)
|
||||
|
||||
assert result == {"graph-1": 5, "graph-2": 2}
|
||||
mock_prisma.return_value.group_by.assert_called_once_with(
|
||||
by=["agentGraphId"],
|
||||
where={
|
||||
"userId": "user-1",
|
||||
"agentGraphId": {"in": ["graph-1", "graph-2", "graph-3"]},
|
||||
"isDeleted": False,
|
||||
},
|
||||
count=True,
|
||||
)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import collections
|
||||
import datetime
|
||||
from enum import Enum
|
||||
from typing import TYPE_CHECKING, Any, Optional
|
||||
@@ -6,6 +7,7 @@ import prisma.enums
|
||||
import prisma.models
|
||||
import pydantic
|
||||
|
||||
from backend.blocks._base import BlockCategory
|
||||
from backend.data.graph import GraphModel, GraphSettings, GraphTriggerInfo
|
||||
from backend.data.model import (
|
||||
CredentialsMetaInput,
|
||||
@@ -144,6 +146,13 @@ class RecentExecution(pydantic.BaseModel):
|
||||
activity_summary: str | None = None
|
||||
|
||||
|
||||
def _parse_top_integrations(raw: object, graph: GraphModel) -> list[dict[str, str]]:
|
||||
"""Parse topIntegrations from database, falling back to on-the-fly computation."""
|
||||
if raw and isinstance(raw, list) and len(raw) > 0:
|
||||
return [dict(item) for item in raw]
|
||||
return _compute_top_integrations(graph)
|
||||
|
||||
|
||||
def _parse_settings(settings: dict | str | None) -> GraphSettings:
|
||||
"""Parse settings from database, handling both dict and string formats."""
|
||||
if settings is None:
|
||||
@@ -156,6 +165,62 @@ def _parse_settings(settings: dict | str | None) -> GraphSettings:
|
||||
return GraphSettings()
|
||||
|
||||
|
||||
# Priority order for category-based integration entries
|
||||
_CATEGORY_PRIORITY: list[BlockCategory] = [
|
||||
BlockCategory.AI,
|
||||
BlockCategory.SOCIAL,
|
||||
BlockCategory.COMMUNICATION,
|
||||
BlockCategory.DEVELOPER_TOOLS,
|
||||
BlockCategory.DATA,
|
||||
BlockCategory.CRM,
|
||||
BlockCategory.PRODUCTIVITY,
|
||||
BlockCategory.ISSUE_TRACKING,
|
||||
BlockCategory.TEXT,
|
||||
BlockCategory.SEARCH,
|
||||
BlockCategory.MULTIMEDIA,
|
||||
BlockCategory.MARKETING,
|
||||
BlockCategory.LOGIC,
|
||||
BlockCategory.BASIC,
|
||||
BlockCategory.INPUT,
|
||||
BlockCategory.OUTPUT,
|
||||
]
|
||||
|
||||
|
||||
def _compute_top_integrations(
|
||||
graph: GraphModel,
|
||||
) -> list[dict[str, str]]:
|
||||
"""Compute the top integrations used by an agent's graph.
|
||||
|
||||
Returns up to 5 entries: providers first (by frequency), then categories.
|
||||
"""
|
||||
provider_counter: collections.Counter[str] = collections.Counter()
|
||||
category_counter: collections.Counter[BlockCategory] = collections.Counter()
|
||||
|
||||
for g in [graph, *graph.sub_graphs]:
|
||||
for node in g.nodes:
|
||||
for info in node.block.input_schema.get_credentials_fields_info().values():
|
||||
for provider in info.provider:
|
||||
provider_counter[provider] += 1
|
||||
|
||||
if node.block.categories:
|
||||
for cat in node.block.categories:
|
||||
category_counter[cat] += 1
|
||||
|
||||
result: list[dict[str, str]] = [
|
||||
{"name": name, "type": "provider"}
|
||||
for name, _ in provider_counter.most_common(5)
|
||||
]
|
||||
|
||||
if len(result) < 5:
|
||||
for cat in _CATEGORY_PRIORITY:
|
||||
if len(result) >= 5:
|
||||
break
|
||||
if category_counter.get(cat, 0) > 0:
|
||||
result.append({"name": cat.name, "type": "category"})
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class LibraryAgent(pydantic.BaseModel):
|
||||
"""
|
||||
Represents an agent in the library, including metadata for display and
|
||||
@@ -214,15 +279,8 @@ class LibraryAgent(pydantic.BaseModel):
|
||||
folder_name: str | None = None # Denormalized for display
|
||||
|
||||
recommended_schedule_cron: str | None = None
|
||||
is_scheduled: bool = pydantic.Field(
|
||||
default=False,
|
||||
description="Whether this agent has active execution schedules",
|
||||
)
|
||||
next_scheduled_run: str | None = pydantic.Field(
|
||||
default=None,
|
||||
description="ISO 8601 timestamp of the next scheduled run, if any",
|
||||
)
|
||||
settings: GraphSettings = pydantic.Field(default_factory=GraphSettings)
|
||||
top_integrations: list[dict[str, str]] = pydantic.Field(default_factory=list)
|
||||
marketplace_listing: Optional["MarketplaceListing"] = None
|
||||
|
||||
@staticmethod
|
||||
@@ -231,8 +289,6 @@ class LibraryAgent(pydantic.BaseModel):
|
||||
sub_graphs: Optional[list[prisma.models.AgentGraph]] = None,
|
||||
store_listing: Optional[prisma.models.StoreListing] = None,
|
||||
profile: Optional[prisma.models.Profile] = None,
|
||||
execution_count_override: Optional[int] = None,
|
||||
schedule_info: Optional[dict[str, str]] = None,
|
||||
) -> "LibraryAgent":
|
||||
"""
|
||||
Factory method that constructs a LibraryAgent from a Prisma LibraryAgent
|
||||
@@ -268,14 +324,10 @@ class LibraryAgent(pydantic.BaseModel):
|
||||
status = status_result.status
|
||||
new_output = status_result.new_output
|
||||
|
||||
execution_count = (
|
||||
execution_count_override
|
||||
if execution_count_override is not None
|
||||
else len(executions)
|
||||
)
|
||||
execution_count = len(executions)
|
||||
success_rate: float | None = None
|
||||
avg_correctness_score: float | None = None
|
||||
if executions and execution_count > 0:
|
||||
if execution_count > 0:
|
||||
success_count = sum(
|
||||
1
|
||||
for e in executions
|
||||
@@ -368,11 +420,8 @@ class LibraryAgent(pydantic.BaseModel):
|
||||
folder_id=agent.folderId,
|
||||
folder_name=agent.Folder.name if agent.Folder else None,
|
||||
recommended_schedule_cron=agent.AgentGraph.recommendedScheduleCron,
|
||||
is_scheduled=bool(schedule_info and agent.agentGraphId in schedule_info),
|
||||
next_scheduled_run=(
|
||||
schedule_info.get(agent.agentGraphId) if schedule_info else None
|
||||
),
|
||||
settings=_parse_settings(agent.settings),
|
||||
top_integrations=_parse_top_integrations(agent.topIntegrations, graph),
|
||||
marketplace_listing=marketplace_listing_data,
|
||||
)
|
||||
|
||||
|
||||
@@ -1,66 +1,11 @@
|
||||
import datetime
|
||||
|
||||
import prisma.enums
|
||||
import prisma.models
|
||||
import pytest
|
||||
|
||||
from . import model as library_model
|
||||
|
||||
|
||||
def _make_library_agent(
|
||||
*,
|
||||
graph_id: str = "g1",
|
||||
executions: list | None = None,
|
||||
) -> prisma.models.LibraryAgent:
|
||||
return prisma.models.LibraryAgent(
|
||||
id="la1",
|
||||
userId="u1",
|
||||
agentGraphId=graph_id,
|
||||
settings="{}", # type: ignore
|
||||
agentGraphVersion=1,
|
||||
isCreatedByUser=True,
|
||||
isDeleted=False,
|
||||
isArchived=False,
|
||||
createdAt=datetime.datetime.now(),
|
||||
updatedAt=datetime.datetime.now(),
|
||||
isFavorite=False,
|
||||
useGraphIsActiveVersion=True,
|
||||
AgentGraph=prisma.models.AgentGraph(
|
||||
id=graph_id,
|
||||
version=1,
|
||||
name="Agent",
|
||||
description="Desc",
|
||||
userId="u1",
|
||||
isActive=True,
|
||||
createdAt=datetime.datetime.now(),
|
||||
Executions=executions,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_from_db_execution_count_override_covers_success_rate():
|
||||
"""Covers execution_count_override is not None branch and executions/count > 0 block."""
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
exec1 = prisma.models.AgentGraphExecution(
|
||||
id="exec-1",
|
||||
agentGraphId="g1",
|
||||
agentGraphVersion=1,
|
||||
userId="u1",
|
||||
executionStatus=prisma.enums.AgentExecutionStatus.COMPLETED,
|
||||
createdAt=now,
|
||||
updatedAt=now,
|
||||
isDeleted=False,
|
||||
isShared=False,
|
||||
)
|
||||
agent = _make_library_agent(executions=[exec1])
|
||||
|
||||
result = library_model.LibraryAgent.from_db(agent, execution_count_override=1)
|
||||
|
||||
assert result.execution_count == 1
|
||||
assert result.success_rate is not None
|
||||
assert result.success_rate == 100.0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_agent_preset_from_db(test_user_id: str):
|
||||
# Create mock DB agent
|
||||
|
||||
@@ -105,7 +105,7 @@ async def test_get_library_agents_success(
|
||||
assert data.agents[1].can_access_graph is False
|
||||
|
||||
snapshot.snapshot_dir = "snapshots"
|
||||
snapshot.assert_match(json.dumps(response.json(), indent=2), "lib_agts_search")
|
||||
snapshot.assert_match(f"{json.dumps(response.json(), indent=2)}\n", "lib_agts_search")
|
||||
|
||||
mock_db_call.assert_called_once_with(
|
||||
user_id=test_user_id,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,8 +5,7 @@ import time
|
||||
import uuid
|
||||
from collections import defaultdict
|
||||
from datetime import datetime, timezone
|
||||
from typing import Annotated, Any, Literal, Sequence, cast, get_args
|
||||
from urllib.parse import urlparse
|
||||
from typing import Annotated, Any, Sequence, get_args
|
||||
|
||||
import pydantic
|
||||
import stripe
|
||||
@@ -25,8 +24,7 @@ from fastapi import (
|
||||
UploadFile,
|
||||
)
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
from prisma.enums import SubscriptionTier
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel
|
||||
from starlette.status import HTTP_204_NO_CONTENT, HTTP_404_NOT_FOUND
|
||||
from typing_extensions import Optional, TypedDict
|
||||
|
||||
@@ -49,24 +47,12 @@ from backend.data.auth import api_key as api_key_db
|
||||
from backend.data.block import BlockInput, CompletedBlockOutput
|
||||
from backend.data.credit import (
|
||||
AutoTopUpConfig,
|
||||
PendingChangeUnknown,
|
||||
RefundRequest,
|
||||
TransactionHistory,
|
||||
UserCredit,
|
||||
cancel_stripe_subscription,
|
||||
create_subscription_checkout,
|
||||
get_auto_top_up,
|
||||
get_pending_subscription_change,
|
||||
get_proration_credit_cents,
|
||||
get_subscription_price_id,
|
||||
get_user_credit_model,
|
||||
handle_subscription_payment_failure,
|
||||
modify_stripe_subscription_for_tier,
|
||||
release_pending_subscription_schedule,
|
||||
set_auto_top_up,
|
||||
set_subscription_tier,
|
||||
sync_subscription_from_stripe,
|
||||
sync_subscription_schedule_from_stripe,
|
||||
)
|
||||
from backend.data.graph import GraphSettings
|
||||
from backend.data.model import CredentialsMetaInput, UserOnboarding
|
||||
@@ -675,12 +661,9 @@ async def configure_user_auto_top_up(
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
raise
|
||||
|
||||
try:
|
||||
await set_auto_top_up(
|
||||
user_id, AutoTopUpConfig(threshold=request.threshold, amount=request.amount)
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
await set_auto_top_up(
|
||||
user_id, AutoTopUpConfig(threshold=request.threshold, amount=request.amount)
|
||||
)
|
||||
return "Auto top-up settings updated"
|
||||
|
||||
|
||||
@@ -696,430 +679,41 @@ async def get_user_auto_top_up(
|
||||
return await get_auto_top_up(user_id)
|
||||
|
||||
|
||||
class SubscriptionTierRequest(BaseModel):
|
||||
tier: Literal["FREE", "PRO", "BUSINESS"]
|
||||
success_url: str = ""
|
||||
cancel_url: str = ""
|
||||
|
||||
|
||||
class SubscriptionStatusResponse(BaseModel):
|
||||
tier: Literal["FREE", "PRO", "BUSINESS", "ENTERPRISE"]
|
||||
monthly_cost: int # amount in cents (Stripe convention)
|
||||
tier_costs: dict[str, int] # tier name -> amount in cents
|
||||
proration_credit_cents: int # unused portion of current sub to convert on upgrade
|
||||
pending_tier: Optional[Literal["FREE", "PRO", "BUSINESS"]] = None
|
||||
pending_tier_effective_at: Optional[datetime] = None
|
||||
url: str = Field(
|
||||
default="",
|
||||
description=(
|
||||
"Populated only when POST /credits/subscription starts a Stripe Checkout"
|
||||
" Session (FREE → paid upgrade). Empty string in all other branches —"
|
||||
" the client redirects to this URL when non-empty."
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _validate_checkout_redirect_url(url: str) -> bool:
|
||||
"""Return True if `url` matches the configured frontend origin.
|
||||
|
||||
Prevents open-redirect: attackers must not be able to supply arbitrary
|
||||
success_url/cancel_url that Stripe will redirect users to after checkout.
|
||||
|
||||
Pre-parse rejection rules (applied before urlparse):
|
||||
- Backslashes (``\\``) are normalised differently across parsers/browsers.
|
||||
- Control characters (U+0000–U+001F) are not valid in URLs and may confuse
|
||||
some URL-parsing implementations.
|
||||
"""
|
||||
# Reject characters that can confuse URL parsers before any parsing.
|
||||
if "\\" in url:
|
||||
return False
|
||||
if any(ord(c) < 0x20 for c in url):
|
||||
return False
|
||||
|
||||
allowed = settings.config.frontend_base_url or settings.config.platform_base_url
|
||||
if not allowed:
|
||||
# No configured origin — refuse to validate rather than allow arbitrary URLs.
|
||||
return False
|
||||
try:
|
||||
parsed = urlparse(url)
|
||||
allowed_parsed = urlparse(allowed)
|
||||
except ValueError:
|
||||
return False
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
return False
|
||||
# Reject ``user:pass@host`` authority tricks — ``@`` in the netloc component
|
||||
# can trick browsers into connecting to a different host than displayed.
|
||||
# ``@`` in query/fragment is harmless and must be allowed.
|
||||
if "@" in parsed.netloc:
|
||||
return False
|
||||
return (
|
||||
parsed.scheme == allowed_parsed.scheme
|
||||
and parsed.netloc == allowed_parsed.netloc
|
||||
)
|
||||
|
||||
|
||||
@cached(ttl_seconds=300, maxsize=32, cache_none=False)
|
||||
async def _get_stripe_price_amount(price_id: str) -> int | None:
|
||||
"""Return the unit_amount (cents) for a Stripe Price ID, cached for 5 minutes.
|
||||
|
||||
Returns ``None`` on transient Stripe errors. ``cache_none=False`` opts out
|
||||
of caching the ``None`` sentinel so the next request retries Stripe instead
|
||||
of being served a stale "no price" for the rest of the TTL window. Callers
|
||||
should treat ``None`` as an unknown price and fall back to 0.
|
||||
|
||||
Stripe prices rarely change; caching avoids a ~200-600 ms Stripe round-trip on
|
||||
every GET /credits/subscription page load and reduces quota consumption.
|
||||
"""
|
||||
try:
|
||||
price = await run_in_threadpool(stripe.Price.retrieve, price_id)
|
||||
return price.unit_amount or 0
|
||||
except stripe.StripeError:
|
||||
logger.warning(
|
||||
"Failed to retrieve Stripe price %s — returning None (not cached)",
|
||||
price_id,
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
@v1_router.get(
|
||||
path="/credits/subscription",
|
||||
summary="Get subscription tier, current cost, and all tier costs",
|
||||
operation_id="getSubscriptionStatus",
|
||||
tags=["credits"],
|
||||
dependencies=[Security(requires_user)],
|
||||
)
|
||||
async def get_subscription_status(
|
||||
user_id: Annotated[str, Security(get_user_id)],
|
||||
) -> SubscriptionStatusResponse:
|
||||
user = await get_user_by_id(user_id)
|
||||
tier = user.subscription_tier or SubscriptionTier.FREE
|
||||
|
||||
paid_tiers = [SubscriptionTier.PRO, SubscriptionTier.BUSINESS]
|
||||
price_ids = await asyncio.gather(
|
||||
*[get_subscription_price_id(t) for t in paid_tiers]
|
||||
)
|
||||
|
||||
tier_costs: dict[str, int] = {
|
||||
SubscriptionTier.FREE.value: 0,
|
||||
SubscriptionTier.ENTERPRISE.value: 0,
|
||||
}
|
||||
|
||||
async def _cost(pid: str | None) -> int:
|
||||
return (await _get_stripe_price_amount(pid) or 0) if pid else 0
|
||||
|
||||
costs = await asyncio.gather(*[_cost(pid) for pid in price_ids])
|
||||
for t, cost in zip(paid_tiers, costs):
|
||||
tier_costs[t.value] = cost
|
||||
|
||||
current_monthly_cost = tier_costs.get(tier.value, 0)
|
||||
proration_credit = await get_proration_credit_cents(user_id, current_monthly_cost)
|
||||
|
||||
try:
|
||||
pending = await get_pending_subscription_change(user_id)
|
||||
except (stripe.StripeError, PendingChangeUnknown):
|
||||
# Swallow Stripe-side failures (rate limits, transient network) AND
|
||||
# PendingChangeUnknown (LaunchDarkly price-id lookup failed). Both
|
||||
# propagate past the cache so the next request retries fresh instead
|
||||
# of serving a stale None for the TTL window. Let real bugs (KeyError,
|
||||
# AttributeError, etc.) propagate so they surface in Sentry.
|
||||
logger.exception(
|
||||
"get_subscription_status: failed to resolve pending change for user %s",
|
||||
user_id,
|
||||
)
|
||||
pending = None
|
||||
|
||||
response = SubscriptionStatusResponse(
|
||||
tier=tier.value,
|
||||
monthly_cost=current_monthly_cost,
|
||||
tier_costs=tier_costs,
|
||||
proration_credit_cents=proration_credit,
|
||||
)
|
||||
if pending is not None:
|
||||
pending_tier_enum, pending_effective_at = pending
|
||||
if pending_tier_enum == SubscriptionTier.FREE:
|
||||
response.pending_tier = "FREE"
|
||||
elif pending_tier_enum == SubscriptionTier.PRO:
|
||||
response.pending_tier = "PRO"
|
||||
elif pending_tier_enum == SubscriptionTier.BUSINESS:
|
||||
response.pending_tier = "BUSINESS"
|
||||
if response.pending_tier is not None:
|
||||
response.pending_tier_effective_at = pending_effective_at
|
||||
return response
|
||||
|
||||
|
||||
@v1_router.post(
|
||||
path="/credits/subscription",
|
||||
summary="Update subscription tier or start a Stripe Checkout session",
|
||||
operation_id="updateSubscriptionTier",
|
||||
tags=["credits"],
|
||||
dependencies=[Security(requires_user)],
|
||||
)
|
||||
async def update_subscription_tier(
|
||||
request: SubscriptionTierRequest,
|
||||
user_id: Annotated[str, Security(get_user_id)],
|
||||
) -> SubscriptionStatusResponse:
|
||||
# Pydantic validates tier is one of FREE/PRO/BUSINESS via Literal type.
|
||||
tier = SubscriptionTier(request.tier)
|
||||
|
||||
# ENTERPRISE tier is admin-managed — block self-service changes from ENTERPRISE users.
|
||||
user = await get_user_by_id(user_id)
|
||||
if (user.subscription_tier or SubscriptionTier.FREE) == SubscriptionTier.ENTERPRISE:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="ENTERPRISE subscription changes must be managed by an administrator",
|
||||
)
|
||||
|
||||
# Same-tier request = "stay on my current tier" = cancel any pending
|
||||
# scheduled change (paid→paid downgrade or paid→FREE cancel). This is the
|
||||
# collapsed behaviour that replaces the old /credits/subscription/cancel-pending
|
||||
# route. Safe when no pending change exists: release_pending_subscription_schedule
|
||||
# returns False and we simply return the current status.
|
||||
if (user.subscription_tier or SubscriptionTier.FREE) == tier:
|
||||
try:
|
||||
await release_pending_subscription_schedule(user_id)
|
||||
except stripe.StripeError as e:
|
||||
logger.exception(
|
||||
"Stripe error releasing pending subscription change for user %s: %s",
|
||||
user_id,
|
||||
e,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=(
|
||||
"Unable to cancel the pending subscription change right now. "
|
||||
"Please try again or contact support."
|
||||
),
|
||||
)
|
||||
return await get_subscription_status(user_id)
|
||||
|
||||
payment_enabled = await is_feature_enabled(
|
||||
Flag.ENABLE_PLATFORM_PAYMENT, user_id, default=False
|
||||
)
|
||||
|
||||
# Downgrade to FREE: schedule Stripe cancellation at period end so the user
|
||||
# keeps their tier for the time they already paid for. The DB tier is NOT
|
||||
# updated here when a subscription exists — the customer.subscription.deleted
|
||||
# webhook fires at period end and downgrades to FREE then.
|
||||
# Exception: if the user has no active Stripe subscription (e.g. admin-granted
|
||||
# tier), cancel_stripe_subscription returns False and we update the DB tier
|
||||
# immediately since no webhook will ever fire.
|
||||
# When payment is disabled entirely, update the DB tier directly.
|
||||
if tier == SubscriptionTier.FREE:
|
||||
if payment_enabled:
|
||||
try:
|
||||
had_subscription = await cancel_stripe_subscription(user_id)
|
||||
except stripe.StripeError as e:
|
||||
# Log full Stripe error server-side but return a generic message
|
||||
# to the client — raw Stripe errors can leak customer/sub IDs and
|
||||
# infrastructure config details.
|
||||
logger.exception(
|
||||
"Stripe error cancelling subscription for user %s: %s",
|
||||
user_id,
|
||||
e,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=(
|
||||
"Unable to cancel your subscription right now. "
|
||||
"Please try again or contact support."
|
||||
),
|
||||
)
|
||||
if not had_subscription:
|
||||
# No active Stripe subscription found — the user was on an
|
||||
# admin-granted tier. Update DB immediately since the
|
||||
# subscription.deleted webhook will never fire.
|
||||
await set_subscription_tier(user_id, tier)
|
||||
return await get_subscription_status(user_id)
|
||||
await set_subscription_tier(user_id, tier)
|
||||
return await get_subscription_status(user_id)
|
||||
|
||||
# Paid tier changes require payment to be enabled — block self-service upgrades
|
||||
# when the flag is off. Admins use the /api/admin/ routes to set tiers directly.
|
||||
if not payment_enabled:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=f"Subscription not available for tier {tier}",
|
||||
)
|
||||
|
||||
# Paid→paid tier change: if the user already has a Stripe subscription,
|
||||
# modify it in-place with proration instead of creating a new Checkout
|
||||
# Session. This preserves remaining paid time and avoids double-charging.
|
||||
# The customer.subscription.updated webhook fires and updates the DB tier.
|
||||
current_tier = user.subscription_tier or SubscriptionTier.FREE
|
||||
if current_tier in (SubscriptionTier.PRO, SubscriptionTier.BUSINESS):
|
||||
try:
|
||||
modified = await modify_stripe_subscription_for_tier(user_id, tier)
|
||||
if modified:
|
||||
return await get_subscription_status(user_id)
|
||||
# modify_stripe_subscription_for_tier returns False when no active
|
||||
# Stripe subscription exists — i.e. the user has an admin-granted
|
||||
# paid tier with no Stripe record. In that case, update the DB
|
||||
# tier directly (same as the FREE-downgrade path for admin-granted
|
||||
# users) rather than sending them through a new Checkout Session.
|
||||
await set_subscription_tier(user_id, tier)
|
||||
return await get_subscription_status(user_id)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
except stripe.StripeError as e:
|
||||
logger.exception(
|
||||
"Stripe error modifying subscription for user %s: %s", user_id, e
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=(
|
||||
"Unable to update your subscription right now. "
|
||||
"Please try again or contact support."
|
||||
),
|
||||
)
|
||||
|
||||
# Paid upgrade from FREE → create Stripe Checkout Session.
|
||||
if not request.success_url or not request.cancel_url:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail="success_url and cancel_url are required for paid tier upgrades",
|
||||
)
|
||||
# Open-redirect protection: both URLs must point to the configured frontend
|
||||
# origin, otherwise an attacker could use our Stripe integration as a
|
||||
# redirector to arbitrary phishing sites.
|
||||
#
|
||||
# Fail early with a clear 503 if the server is misconfigured (neither
|
||||
# frontend_base_url nor platform_base_url set), so operators get an
|
||||
# actionable error instead of the misleading "must match the platform
|
||||
# frontend origin" 422 that _validate_checkout_redirect_url would otherwise
|
||||
# produce when `allowed` is empty.
|
||||
if not (settings.config.frontend_base_url or settings.config.platform_base_url):
|
||||
logger.error(
|
||||
"update_subscription_tier: neither frontend_base_url nor "
|
||||
"platform_base_url is configured; cannot validate checkout redirect URLs"
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=(
|
||||
"Payment redirect URLs cannot be validated: "
|
||||
"frontend_base_url or platform_base_url must be set on the server."
|
||||
),
|
||||
)
|
||||
if not _validate_checkout_redirect_url(
|
||||
request.success_url
|
||||
) or not _validate_checkout_redirect_url(request.cancel_url):
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail="success_url and cancel_url must match the platform frontend origin",
|
||||
)
|
||||
try:
|
||||
url = await create_subscription_checkout(
|
||||
user_id=user_id,
|
||||
tier=tier,
|
||||
success_url=request.success_url,
|
||||
cancel_url=request.cancel_url,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
except stripe.StripeError as e:
|
||||
logger.exception(
|
||||
"Stripe error creating checkout session for user %s: %s", user_id, e
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=(
|
||||
"Unable to start checkout right now. "
|
||||
"Please try again or contact support."
|
||||
),
|
||||
)
|
||||
|
||||
status = await get_subscription_status(user_id)
|
||||
status.url = url
|
||||
return status
|
||||
|
||||
|
||||
@v1_router.post(
|
||||
path="/credits/stripe_webhook", summary="Handle Stripe webhooks", tags=["credits"]
|
||||
)
|
||||
async def stripe_webhook(request: Request):
|
||||
webhook_secret = settings.secrets.stripe_webhook_secret
|
||||
if not webhook_secret:
|
||||
# Guard: an empty secret allows HMAC forgery (attacker can compute a valid
|
||||
# signature over the same empty key). Reject all webhook calls when unconfigured.
|
||||
logger.error(
|
||||
"stripe_webhook: STRIPE_WEBHOOK_SECRET is not configured — "
|
||||
"rejecting request to prevent signature bypass"
|
||||
)
|
||||
raise HTTPException(status_code=503, detail="Webhook not configured")
|
||||
|
||||
# Get the raw request body
|
||||
payload = await request.body()
|
||||
# Get the signature header
|
||||
sig_header = request.headers.get("stripe-signature")
|
||||
|
||||
try:
|
||||
event = stripe.Webhook.construct_event(payload, sig_header, webhook_secret)
|
||||
except ValueError:
|
||||
event = stripe.Webhook.construct_event(
|
||||
payload, sig_header, settings.secrets.stripe_webhook_secret
|
||||
)
|
||||
except ValueError as e:
|
||||
# Invalid payload
|
||||
raise HTTPException(status_code=400, detail="Invalid payload")
|
||||
except stripe.SignatureVerificationError:
|
||||
raise HTTPException(
|
||||
status_code=400, detail=f"Invalid payload: {str(e) or type(e).__name__}"
|
||||
)
|
||||
except stripe.SignatureVerificationError as e:
|
||||
# Invalid signature
|
||||
raise HTTPException(status_code=400, detail="Invalid signature")
|
||||
|
||||
# Defensive payload extraction. A malformed payload (missing/non-dict
|
||||
# `data.object`, missing `id`) would otherwise raise KeyError/TypeError
|
||||
# AFTER signature verification — which Stripe interprets as a delivery
|
||||
# failure and retries forever, while spamming Sentry with no useful info.
|
||||
# Acknowledge with 200 and a warning so Stripe stops retrying.
|
||||
event_type = event.get("type", "")
|
||||
event_data = event.get("data") or {}
|
||||
data_object = event_data.get("object") if isinstance(event_data, dict) else None
|
||||
if not isinstance(data_object, dict):
|
||||
logger.warning(
|
||||
"stripe_webhook: %s missing or non-dict data.object; ignoring",
|
||||
event_type,
|
||||
raise HTTPException(
|
||||
status_code=400, detail=f"Invalid signature: {str(e) or type(e).__name__}"
|
||||
)
|
||||
return Response(status_code=200)
|
||||
|
||||
if event_type in (
|
||||
"checkout.session.completed",
|
||||
"checkout.session.async_payment_succeeded",
|
||||
if (
|
||||
event["type"] == "checkout.session.completed"
|
||||
or event["type"] == "checkout.session.async_payment_succeeded"
|
||||
):
|
||||
session_id = data_object.get("id")
|
||||
if not session_id:
|
||||
logger.warning(
|
||||
"stripe_webhook: %s missing data.object.id; ignoring", event_type
|
||||
)
|
||||
return Response(status_code=200)
|
||||
await UserCredit().fulfill_checkout(session_id=session_id)
|
||||
await UserCredit().fulfill_checkout(session_id=event["data"]["object"]["id"])
|
||||
|
||||
if event_type in (
|
||||
"customer.subscription.created",
|
||||
"customer.subscription.updated",
|
||||
"customer.subscription.deleted",
|
||||
):
|
||||
await sync_subscription_from_stripe(data_object)
|
||||
if event["type"] == "charge.dispute.created":
|
||||
await UserCredit().handle_dispute(event["data"]["object"])
|
||||
|
||||
# `subscription_schedule.updated` is deliberately omitted: our own
|
||||
# `SubscriptionSchedule.create` + `.modify` calls in
|
||||
# `_schedule_downgrade_at_period_end` would fire that event right back at us
|
||||
# and loop redundant traffic through this handler. We only care about state
|
||||
# transitions (released / completed); phase advance to the new price is
|
||||
# already covered by `customer.subscription.updated`.
|
||||
if event_type in (
|
||||
"subscription_schedule.released",
|
||||
"subscription_schedule.completed",
|
||||
):
|
||||
await sync_subscription_schedule_from_stripe(data_object)
|
||||
|
||||
if event_type == "invoice.payment_failed":
|
||||
await handle_subscription_payment_failure(data_object)
|
||||
|
||||
# `handle_dispute` and `deduct_credits` expect Stripe SDK typed objects
|
||||
# (Dispute/Refund). The Stripe webhook payload's `data.object` is a
|
||||
# StripeObject (a dict subclass) carrying that runtime shape, so we cast
|
||||
# to satisfy the type checker without changing runtime behaviour.
|
||||
if event_type == "charge.dispute.created":
|
||||
await UserCredit().handle_dispute(cast(stripe.Dispute, data_object))
|
||||
|
||||
if event_type == "refund.created" or event_type == "charge.dispute.closed":
|
||||
await UserCredit().deduct_credits(
|
||||
cast("stripe.Refund | stripe.Dispute", data_object)
|
||||
)
|
||||
if event["type"] == "refund.created" or event["type"] == "charge.dispute.closed":
|
||||
await UserCredit().deduct_credits(event["data"]["object"])
|
||||
|
||||
return Response(status_code=200)
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import fastapi
|
||||
from autogpt_libs.auth.dependencies import get_user_id, requires_user
|
||||
from fastapi import Query, UploadFile
|
||||
from fastapi.responses import Response
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.data.workspace import (
|
||||
WorkspaceFile,
|
||||
@@ -131,26 +131,9 @@ class StorageUsageResponse(BaseModel):
|
||||
file_count: int
|
||||
|
||||
|
||||
class WorkspaceFileItem(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
path: str
|
||||
mime_type: str
|
||||
size_bytes: int
|
||||
metadata: dict = Field(default_factory=dict)
|
||||
created_at: str
|
||||
|
||||
|
||||
class ListFilesResponse(BaseModel):
|
||||
files: list[WorkspaceFileItem]
|
||||
offset: int = 0
|
||||
has_more: bool = False
|
||||
|
||||
|
||||
@router.get(
|
||||
"/files/{file_id}/download",
|
||||
summary="Download file by ID",
|
||||
operation_id="getWorkspaceDownloadFileById",
|
||||
)
|
||||
async def download_file(
|
||||
user_id: Annotated[str, fastapi.Security(get_user_id)],
|
||||
@@ -175,7 +158,6 @@ async def download_file(
|
||||
@router.delete(
|
||||
"/files/{file_id}",
|
||||
summary="Delete a workspace file",
|
||||
operation_id="deleteWorkspaceFile",
|
||||
)
|
||||
async def delete_workspace_file(
|
||||
user_id: Annotated[str, fastapi.Security(get_user_id)],
|
||||
@@ -201,7 +183,6 @@ async def delete_workspace_file(
|
||||
@router.post(
|
||||
"/files/upload",
|
||||
summary="Upload file to workspace",
|
||||
operation_id="uploadWorkspaceFile",
|
||||
)
|
||||
async def upload_file(
|
||||
user_id: Annotated[str, fastapi.Security(get_user_id)],
|
||||
@@ -215,9 +196,6 @@ async def upload_file(
|
||||
Files are stored in session-scoped paths when session_id is provided,
|
||||
so the agent's session-scoped tools can discover them automatically.
|
||||
"""
|
||||
# Empty-string session_id drops session scoping; normalize to None.
|
||||
session_id = session_id or None
|
||||
|
||||
config = Config()
|
||||
|
||||
# Sanitize filename — strip any directory components
|
||||
@@ -272,27 +250,16 @@ async def upload_file(
|
||||
manager = WorkspaceManager(user_id, workspace.id, session_id)
|
||||
try:
|
||||
workspace_file = await manager.write_file(
|
||||
content, filename, overwrite=overwrite, metadata={"origin": "user-upload"}
|
||||
content, filename, overwrite=overwrite
|
||||
)
|
||||
except ValueError as e:
|
||||
# write_file raises ValueError for both path-conflict and size-limit
|
||||
# cases; map each to its correct HTTP status.
|
||||
message = str(e)
|
||||
if message.startswith("File too large"):
|
||||
raise fastapi.HTTPException(status_code=413, detail=message) from e
|
||||
raise fastapi.HTTPException(status_code=409, detail=message) from e
|
||||
raise fastapi.HTTPException(status_code=409, detail=str(e)) from e
|
||||
|
||||
# Post-write storage check — eliminates TOCTOU race on the quota.
|
||||
# If a concurrent upload pushed us over the limit, undo this write.
|
||||
new_total = await get_workspace_total_size(workspace.id)
|
||||
if storage_limit_bytes and new_total > storage_limit_bytes:
|
||||
try:
|
||||
await soft_delete_workspace_file(workspace_file.id, workspace.id)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"Failed to soft-delete over-quota file {workspace_file.id} "
|
||||
f"in workspace {workspace.id}: {e}"
|
||||
)
|
||||
await soft_delete_workspace_file(workspace_file.id, workspace.id)
|
||||
raise fastapi.HTTPException(
|
||||
status_code=413,
|
||||
detail={
|
||||
@@ -314,7 +281,6 @@ async def upload_file(
|
||||
@router.get(
|
||||
"/storage/usage",
|
||||
summary="Get workspace storage usage",
|
||||
operation_id="getWorkspaceStorageUsage",
|
||||
)
|
||||
async def get_storage_usage(
|
||||
user_id: Annotated[str, fastapi.Security(get_user_id)],
|
||||
@@ -335,57 +301,3 @@ async def get_storage_usage(
|
||||
used_percent=round((used_bytes / limit_bytes) * 100, 1) if limit_bytes else 0,
|
||||
file_count=file_count,
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/files",
|
||||
summary="List workspace files",
|
||||
operation_id="listWorkspaceFiles",
|
||||
)
|
||||
async def list_workspace_files(
|
||||
user_id: Annotated[str, fastapi.Security(get_user_id)],
|
||||
session_id: str | None = Query(default=None),
|
||||
limit: int = Query(default=200, ge=1, le=1000),
|
||||
offset: int = Query(default=0, ge=0),
|
||||
) -> ListFilesResponse:
|
||||
"""
|
||||
List files in the user's workspace.
|
||||
|
||||
When session_id is provided, only files for that session are returned.
|
||||
Otherwise, all files across sessions are listed. Results are paginated
|
||||
via `limit`/`offset`; `has_more` indicates whether additional pages exist.
|
||||
"""
|
||||
workspace = await get_or_create_workspace(user_id)
|
||||
|
||||
# Treat empty-string session_id the same as omitted — an empty value
|
||||
# would otherwise silently list files across every session instead of
|
||||
# scoping to one.
|
||||
session_id = session_id or None
|
||||
|
||||
manager = WorkspaceManager(user_id, workspace.id, session_id)
|
||||
include_all = session_id is None
|
||||
# Fetch one extra to compute has_more without a separate count query.
|
||||
files = await manager.list_files(
|
||||
limit=limit + 1,
|
||||
offset=offset,
|
||||
include_all_sessions=include_all,
|
||||
)
|
||||
has_more = len(files) > limit
|
||||
page = files[:limit]
|
||||
|
||||
return ListFilesResponse(
|
||||
files=[
|
||||
WorkspaceFileItem(
|
||||
id=f.id,
|
||||
name=f.name,
|
||||
path=f.path,
|
||||
mime_type=f.mime_type,
|
||||
size_bytes=f.size_bytes,
|
||||
metadata=f.metadata or {},
|
||||
created_at=f.created_at.isoformat(),
|
||||
)
|
||||
for f in page
|
||||
],
|
||||
offset=offset,
|
||||
has_more=has_more,
|
||||
)
|
||||
|
||||
@@ -1,28 +1,48 @@
|
||||
"""Tests for workspace file upload and download routes."""
|
||||
|
||||
import io
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import fastapi
|
||||
import fastapi.testclient
|
||||
import pytest
|
||||
import pytest_mock
|
||||
|
||||
from backend.api.features.workspace.routes import router
|
||||
from backend.data.workspace import Workspace, WorkspaceFile
|
||||
from backend.api.features.workspace import routes as workspace_routes
|
||||
from backend.data.workspace import WorkspaceFile
|
||||
|
||||
app = fastapi.FastAPI()
|
||||
app.include_router(router)
|
||||
app.include_router(workspace_routes.router)
|
||||
|
||||
|
||||
@app.exception_handler(ValueError)
|
||||
async def _value_error_handler(
|
||||
request: fastapi.Request, exc: ValueError
|
||||
) -> fastapi.responses.JSONResponse:
|
||||
"""Mirror the production ValueError → 400 mapping from the REST app."""
|
||||
"""Mirror the production ValueError → 400 mapping from rest_api.py."""
|
||||
return fastapi.responses.JSONResponse(status_code=400, content={"detail": str(exc)})
|
||||
|
||||
|
||||
client = fastapi.testclient.TestClient(app)
|
||||
|
||||
TEST_USER_ID = "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
|
||||
|
||||
MOCK_WORKSPACE = type("W", (), {"id": "ws-1"})()
|
||||
|
||||
_NOW = datetime(2023, 1, 1, tzinfo=timezone.utc)
|
||||
|
||||
MOCK_FILE = WorkspaceFile(
|
||||
id="file-aaa-bbb",
|
||||
workspace_id="ws-1",
|
||||
created_at=_NOW,
|
||||
updated_at=_NOW,
|
||||
name="hello.txt",
|
||||
path="/session/hello.txt",
|
||||
mime_type="text/plain",
|
||||
size_bytes=13,
|
||||
storage_path="local://hello.txt",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_app_auth(mock_jwt_user):
|
||||
@@ -33,201 +53,25 @@ def setup_app_auth(mock_jwt_user):
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
def _make_workspace(user_id: str = "test-user-id") -> Workspace:
|
||||
return Workspace(
|
||||
id="ws-001",
|
||||
user_id=user_id,
|
||||
created_at=datetime(2026, 1, 1, tzinfo=timezone.utc),
|
||||
updated_at=datetime(2026, 1, 1, tzinfo=timezone.utc),
|
||||
)
|
||||
|
||||
|
||||
def _make_file(**overrides) -> WorkspaceFile:
|
||||
defaults = {
|
||||
"id": "file-001",
|
||||
"workspace_id": "ws-001",
|
||||
"created_at": datetime(2026, 1, 1, tzinfo=timezone.utc),
|
||||
"updated_at": datetime(2026, 1, 1, tzinfo=timezone.utc),
|
||||
"name": "test.txt",
|
||||
"path": "/test.txt",
|
||||
"storage_path": "local://test.txt",
|
||||
"mime_type": "text/plain",
|
||||
"size_bytes": 100,
|
||||
"checksum": None,
|
||||
"is_deleted": False,
|
||||
"deleted_at": None,
|
||||
"metadata": {},
|
||||
}
|
||||
defaults.update(overrides)
|
||||
return WorkspaceFile(**defaults)
|
||||
|
||||
|
||||
def _make_file_mock(**overrides) -> MagicMock:
|
||||
"""Create a mock WorkspaceFile to simulate DB records with null fields."""
|
||||
defaults = {
|
||||
"id": "file-001",
|
||||
"name": "test.txt",
|
||||
"path": "/test.txt",
|
||||
"mime_type": "text/plain",
|
||||
"size_bytes": 100,
|
||||
"metadata": {},
|
||||
"created_at": datetime(2026, 1, 1, tzinfo=timezone.utc),
|
||||
}
|
||||
defaults.update(overrides)
|
||||
mock = MagicMock(spec=WorkspaceFile)
|
||||
for k, v in defaults.items():
|
||||
setattr(mock, k, v)
|
||||
return mock
|
||||
|
||||
|
||||
# -- list_workspace_files tests --
|
||||
|
||||
|
||||
@patch("backend.api.features.workspace.routes.get_or_create_workspace")
|
||||
@patch("backend.api.features.workspace.routes.WorkspaceManager")
|
||||
def test_list_files_returns_all_when_no_session(mock_manager_cls, mock_get_workspace):
|
||||
mock_get_workspace.return_value = _make_workspace()
|
||||
files = [
|
||||
_make_file(id="f1", name="a.txt", metadata={"origin": "user-upload"}),
|
||||
_make_file(id="f2", name="b.csv", metadata={"origin": "agent-created"}),
|
||||
]
|
||||
mock_instance = AsyncMock()
|
||||
mock_instance.list_files.return_value = files
|
||||
mock_manager_cls.return_value = mock_instance
|
||||
|
||||
response = client.get("/files")
|
||||
assert response.status_code == 200
|
||||
|
||||
data = response.json()
|
||||
assert len(data["files"]) == 2
|
||||
assert data["has_more"] is False
|
||||
assert data["offset"] == 0
|
||||
assert data["files"][0]["id"] == "f1"
|
||||
assert data["files"][0]["metadata"] == {"origin": "user-upload"}
|
||||
assert data["files"][1]["id"] == "f2"
|
||||
mock_instance.list_files.assert_called_once_with(
|
||||
limit=201, offset=0, include_all_sessions=True
|
||||
)
|
||||
|
||||
|
||||
@patch("backend.api.features.workspace.routes.get_or_create_workspace")
|
||||
@patch("backend.api.features.workspace.routes.WorkspaceManager")
|
||||
def test_list_files_scopes_to_session_when_provided(
|
||||
mock_manager_cls, mock_get_workspace, test_user_id
|
||||
):
|
||||
mock_get_workspace.return_value = _make_workspace(user_id=test_user_id)
|
||||
mock_instance = AsyncMock()
|
||||
mock_instance.list_files.return_value = []
|
||||
mock_manager_cls.return_value = mock_instance
|
||||
|
||||
response = client.get("/files?session_id=sess-123")
|
||||
assert response.status_code == 200
|
||||
|
||||
data = response.json()
|
||||
assert data["files"] == []
|
||||
assert data["has_more"] is False
|
||||
mock_manager_cls.assert_called_once_with(test_user_id, "ws-001", "sess-123")
|
||||
mock_instance.list_files.assert_called_once_with(
|
||||
limit=201, offset=0, include_all_sessions=False
|
||||
)
|
||||
|
||||
|
||||
@patch("backend.api.features.workspace.routes.get_or_create_workspace")
|
||||
@patch("backend.api.features.workspace.routes.WorkspaceManager")
|
||||
def test_list_files_null_metadata_coerced_to_empty_dict(
|
||||
mock_manager_cls, mock_get_workspace
|
||||
):
|
||||
"""Route uses `f.metadata or {}` for pre-existing files with null metadata."""
|
||||
mock_get_workspace.return_value = _make_workspace()
|
||||
mock_instance = AsyncMock()
|
||||
mock_instance.list_files.return_value = [_make_file_mock(metadata=None)]
|
||||
mock_manager_cls.return_value = mock_instance
|
||||
|
||||
response = client.get("/files")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["files"][0]["metadata"] == {}
|
||||
|
||||
|
||||
# -- upload_file metadata tests --
|
||||
|
||||
|
||||
@patch("backend.api.features.workspace.routes.get_or_create_workspace")
|
||||
@patch("backend.api.features.workspace.routes.get_workspace_total_size")
|
||||
@patch("backend.api.features.workspace.routes.scan_content_safe")
|
||||
@patch("backend.api.features.workspace.routes.WorkspaceManager")
|
||||
def test_upload_passes_user_upload_origin_metadata(
|
||||
mock_manager_cls, mock_scan, mock_total_size, mock_get_workspace
|
||||
):
|
||||
mock_get_workspace.return_value = _make_workspace()
|
||||
mock_total_size.return_value = 100
|
||||
written = _make_file(id="new-file", name="doc.pdf")
|
||||
mock_instance = AsyncMock()
|
||||
mock_instance.write_file.return_value = written
|
||||
mock_manager_cls.return_value = mock_instance
|
||||
|
||||
response = client.post(
|
||||
"/files/upload",
|
||||
files={"file": ("doc.pdf", b"fake-pdf-content", "application/pdf")},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
mock_instance.write_file.assert_called_once()
|
||||
call_kwargs = mock_instance.write_file.call_args
|
||||
assert call_kwargs.kwargs.get("metadata") == {"origin": "user-upload"}
|
||||
|
||||
|
||||
@patch("backend.api.features.workspace.routes.get_or_create_workspace")
|
||||
@patch("backend.api.features.workspace.routes.get_workspace_total_size")
|
||||
@patch("backend.api.features.workspace.routes.scan_content_safe")
|
||||
@patch("backend.api.features.workspace.routes.WorkspaceManager")
|
||||
def test_upload_returns_409_on_file_conflict(
|
||||
mock_manager_cls, mock_scan, mock_total_size, mock_get_workspace
|
||||
):
|
||||
mock_get_workspace.return_value = _make_workspace()
|
||||
mock_total_size.return_value = 100
|
||||
mock_instance = AsyncMock()
|
||||
mock_instance.write_file.side_effect = ValueError("File already exists at path")
|
||||
mock_manager_cls.return_value = mock_instance
|
||||
|
||||
response = client.post(
|
||||
"/files/upload",
|
||||
files={"file": ("dup.txt", b"content", "text/plain")},
|
||||
)
|
||||
assert response.status_code == 409
|
||||
assert "already exists" in response.json()["detail"]
|
||||
|
||||
|
||||
# -- Restored upload/download/delete security + invariant tests --
|
||||
|
||||
|
||||
def _upload(
|
||||
filename: str = "hello.txt",
|
||||
content: bytes = b"Hello, world!",
|
||||
content_type: str = "text/plain",
|
||||
):
|
||||
"""Helper to POST a file upload."""
|
||||
return client.post(
|
||||
"/files/upload?session_id=sess-1",
|
||||
files={"file": (filename, io.BytesIO(content), content_type)},
|
||||
)
|
||||
|
||||
|
||||
_MOCK_FILE = WorkspaceFile(
|
||||
id="file-aaa-bbb",
|
||||
workspace_id="ws-001",
|
||||
created_at=datetime(2026, 1, 1, tzinfo=timezone.utc),
|
||||
updated_at=datetime(2026, 1, 1, tzinfo=timezone.utc),
|
||||
name="hello.txt",
|
||||
path="/sessions/sess-1/hello.txt",
|
||||
mime_type="text/plain",
|
||||
size_bytes=13,
|
||||
storage_path="local://hello.txt",
|
||||
)
|
||||
# ---- Happy path ----
|
||||
|
||||
|
||||
def test_upload_happy_path(mocker):
|
||||
def test_upload_happy_path(mocker: pytest_mock.MockFixture):
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_or_create_workspace",
|
||||
return_value=_make_workspace(),
|
||||
return_value=MOCK_WORKSPACE,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace_total_size",
|
||||
@@ -238,7 +82,7 @@ def test_upload_happy_path(mocker):
|
||||
return_value=None,
|
||||
)
|
||||
mock_manager = mocker.MagicMock()
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=_MOCK_FILE)
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=MOCK_FILE)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.WorkspaceManager",
|
||||
return_value=mock_manager,
|
||||
@@ -252,7 +96,10 @@ def test_upload_happy_path(mocker):
|
||||
assert data["size_bytes"] == 13
|
||||
|
||||
|
||||
def test_upload_exceeds_max_file_size(mocker):
|
||||
# ---- Per-file size limit ----
|
||||
|
||||
|
||||
def test_upload_exceeds_max_file_size(mocker: pytest_mock.MockFixture):
|
||||
"""Files larger than max_file_size_mb should be rejected with 413."""
|
||||
cfg = mocker.patch("backend.api.features.workspace.routes.Config")
|
||||
cfg.return_value.max_file_size_mb = 0 # 0 MB → any content is too big
|
||||
@@ -262,11 +109,15 @@ def test_upload_exceeds_max_file_size(mocker):
|
||||
assert response.status_code == 413
|
||||
|
||||
|
||||
def test_upload_storage_quota_exceeded(mocker):
|
||||
# ---- Storage quota exceeded ----
|
||||
|
||||
|
||||
def test_upload_storage_quota_exceeded(mocker: pytest_mock.MockFixture):
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_or_create_workspace",
|
||||
return_value=_make_workspace(),
|
||||
return_value=MOCK_WORKSPACE,
|
||||
)
|
||||
# Current usage already at limit
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace_total_size",
|
||||
return_value=500 * 1024 * 1024,
|
||||
@@ -277,22 +128,27 @@ def test_upload_storage_quota_exceeded(mocker):
|
||||
assert "Storage limit exceeded" in response.text
|
||||
|
||||
|
||||
def test_upload_post_write_quota_race(mocker):
|
||||
"""Concurrent upload tipping over limit after write should soft-delete + 413."""
|
||||
# ---- Post-write quota race (B2) ----
|
||||
|
||||
|
||||
def test_upload_post_write_quota_race(mocker: pytest_mock.MockFixture):
|
||||
"""If a concurrent upload tips the total over the limit after write,
|
||||
the file should be soft-deleted and 413 returned."""
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_or_create_workspace",
|
||||
return_value=_make_workspace(),
|
||||
return_value=MOCK_WORKSPACE,
|
||||
)
|
||||
# Pre-write check passes (under limit), but post-write check fails
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace_total_size",
|
||||
side_effect=[0, 600 * 1024 * 1024],
|
||||
side_effect=[0, 600 * 1024 * 1024], # first call OK, second over limit
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.scan_content_safe",
|
||||
return_value=None,
|
||||
)
|
||||
mock_manager = mocker.MagicMock()
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=_MOCK_FILE)
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=MOCK_FILE)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.WorkspaceManager",
|
||||
return_value=mock_manager,
|
||||
@@ -304,14 +160,17 @@ def test_upload_post_write_quota_race(mocker):
|
||||
|
||||
response = _upload()
|
||||
assert response.status_code == 413
|
||||
mock_delete.assert_called_once_with("file-aaa-bbb", "ws-001")
|
||||
mock_delete.assert_called_once_with("file-aaa-bbb", "ws-1")
|
||||
|
||||
|
||||
def test_upload_any_extension(mocker):
|
||||
# ---- Any extension accepted (no allowlist) ----
|
||||
|
||||
|
||||
def test_upload_any_extension(mocker: pytest_mock.MockFixture):
|
||||
"""Any file extension should be accepted — ClamAV is the security layer."""
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_or_create_workspace",
|
||||
return_value=_make_workspace(),
|
||||
return_value=MOCK_WORKSPACE,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace_total_size",
|
||||
@@ -322,7 +181,7 @@ def test_upload_any_extension(mocker):
|
||||
return_value=None,
|
||||
)
|
||||
mock_manager = mocker.MagicMock()
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=_MOCK_FILE)
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=MOCK_FILE)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.WorkspaceManager",
|
||||
return_value=mock_manager,
|
||||
@@ -332,13 +191,16 @@ def test_upload_any_extension(mocker):
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_upload_blocked_by_virus_scan(mocker):
|
||||
# ---- Virus scan rejection ----
|
||||
|
||||
|
||||
def test_upload_blocked_by_virus_scan(mocker: pytest_mock.MockFixture):
|
||||
"""Files flagged by ClamAV should be rejected and never written to storage."""
|
||||
from backend.api.features.store.exceptions import VirusDetectedError
|
||||
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_or_create_workspace",
|
||||
return_value=_make_workspace(),
|
||||
return_value=MOCK_WORKSPACE,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace_total_size",
|
||||
@@ -349,7 +211,7 @@ def test_upload_blocked_by_virus_scan(mocker):
|
||||
side_effect=VirusDetectedError("Eicar-Test-Signature"),
|
||||
)
|
||||
mock_manager = mocker.MagicMock()
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=_MOCK_FILE)
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=MOCK_FILE)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.WorkspaceManager",
|
||||
return_value=mock_manager,
|
||||
@@ -357,14 +219,18 @@ def test_upload_blocked_by_virus_scan(mocker):
|
||||
|
||||
response = _upload(filename="evil.exe", content=b"X5O!P%@AP...")
|
||||
assert response.status_code == 400
|
||||
assert "Virus detected" in response.text
|
||||
mock_manager.write_file.assert_not_called()
|
||||
|
||||
|
||||
def test_upload_file_without_extension(mocker):
|
||||
# ---- No file extension ----
|
||||
|
||||
|
||||
def test_upload_file_without_extension(mocker: pytest_mock.MockFixture):
|
||||
"""Files without an extension should be accepted and stored as-is."""
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_or_create_workspace",
|
||||
return_value=_make_workspace(),
|
||||
return_value=MOCK_WORKSPACE,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace_total_size",
|
||||
@@ -375,7 +241,7 @@ def test_upload_file_without_extension(mocker):
|
||||
return_value=None,
|
||||
)
|
||||
mock_manager = mocker.MagicMock()
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=_MOCK_FILE)
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=MOCK_FILE)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.WorkspaceManager",
|
||||
return_value=mock_manager,
|
||||
@@ -391,11 +257,14 @@ def test_upload_file_without_extension(mocker):
|
||||
assert mock_manager.write_file.call_args[0][1] == "Makefile"
|
||||
|
||||
|
||||
def test_upload_strips_path_components(mocker):
|
||||
# ---- Filename sanitization (SF5) ----
|
||||
|
||||
|
||||
def test_upload_strips_path_components(mocker: pytest_mock.MockFixture):
|
||||
"""Path-traversal filenames should be reduced to their basename."""
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_or_create_workspace",
|
||||
return_value=_make_workspace(),
|
||||
return_value=MOCK_WORKSPACE,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace_total_size",
|
||||
@@ -406,23 +275,28 @@ def test_upload_strips_path_components(mocker):
|
||||
return_value=None,
|
||||
)
|
||||
mock_manager = mocker.MagicMock()
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=_MOCK_FILE)
|
||||
mock_manager.write_file = mocker.AsyncMock(return_value=MOCK_FILE)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.WorkspaceManager",
|
||||
return_value=mock_manager,
|
||||
)
|
||||
|
||||
# Filename with traversal
|
||||
_upload(filename="../../etc/passwd.txt")
|
||||
|
||||
# write_file should have been called with just the basename
|
||||
mock_manager.write_file.assert_called_once()
|
||||
call_args = mock_manager.write_file.call_args
|
||||
assert call_args[0][1] == "passwd.txt"
|
||||
|
||||
|
||||
def test_download_file_not_found(mocker):
|
||||
# ---- Download ----
|
||||
|
||||
|
||||
def test_download_file_not_found(mocker: pytest_mock.MockFixture):
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace",
|
||||
return_value=_make_workspace(),
|
||||
return_value=MOCK_WORKSPACE,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace_file",
|
||||
@@ -433,11 +307,14 @@ def test_download_file_not_found(mocker):
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_delete_file_success(mocker):
|
||||
# ---- Delete ----
|
||||
|
||||
|
||||
def test_delete_file_success(mocker: pytest_mock.MockFixture):
|
||||
"""Deleting an existing file should return {"deleted": true}."""
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace",
|
||||
return_value=_make_workspace(),
|
||||
return_value=MOCK_WORKSPACE,
|
||||
)
|
||||
mock_manager = mocker.MagicMock()
|
||||
mock_manager.delete_file = mocker.AsyncMock(return_value=True)
|
||||
@@ -452,11 +329,11 @@ def test_delete_file_success(mocker):
|
||||
mock_manager.delete_file.assert_called_once_with("file-aaa-bbb")
|
||||
|
||||
|
||||
def test_delete_file_not_found(mocker):
|
||||
def test_delete_file_not_found(mocker: pytest_mock.MockFixture):
|
||||
"""Deleting a non-existent file should return 404."""
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace",
|
||||
return_value=_make_workspace(),
|
||||
return_value=MOCK_WORKSPACE,
|
||||
)
|
||||
mock_manager = mocker.MagicMock()
|
||||
mock_manager.delete_file = mocker.AsyncMock(return_value=False)
|
||||
@@ -470,7 +347,7 @@ def test_delete_file_not_found(mocker):
|
||||
assert "File not found" in response.text
|
||||
|
||||
|
||||
def test_delete_file_no_workspace(mocker):
|
||||
def test_delete_file_no_workspace(mocker: pytest_mock.MockFixture):
|
||||
"""Deleting when user has no workspace should return 404."""
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace",
|
||||
@@ -480,123 +357,3 @@ def test_delete_file_no_workspace(mocker):
|
||||
response = client.delete("/files/file-aaa-bbb")
|
||||
assert response.status_code == 404
|
||||
assert "Workspace not found" in response.text
|
||||
|
||||
|
||||
def test_upload_write_file_too_large_returns_413(mocker):
|
||||
"""write_file raises ValueError("File too large: …") → must map to 413."""
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_or_create_workspace",
|
||||
return_value=_make_workspace(),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace_total_size",
|
||||
return_value=0,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.scan_content_safe",
|
||||
return_value=None,
|
||||
)
|
||||
mock_manager = mocker.MagicMock()
|
||||
mock_manager.write_file = mocker.AsyncMock(
|
||||
side_effect=ValueError("File too large: 900 bytes exceeds 1MB limit")
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.WorkspaceManager",
|
||||
return_value=mock_manager,
|
||||
)
|
||||
|
||||
response = _upload()
|
||||
assert response.status_code == 413
|
||||
assert "File too large" in response.text
|
||||
|
||||
|
||||
def test_upload_write_file_conflict_returns_409(mocker):
|
||||
"""Non-'File too large' ValueErrors from write_file stay as 409."""
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_or_create_workspace",
|
||||
return_value=_make_workspace(),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.get_workspace_total_size",
|
||||
return_value=0,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.scan_content_safe",
|
||||
return_value=None,
|
||||
)
|
||||
mock_manager = mocker.MagicMock()
|
||||
mock_manager.write_file = mocker.AsyncMock(
|
||||
side_effect=ValueError("File already exists at path: /sessions/x/a.txt")
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.api.features.workspace.routes.WorkspaceManager",
|
||||
return_value=mock_manager,
|
||||
)
|
||||
|
||||
response = _upload()
|
||||
assert response.status_code == 409
|
||||
assert "already exists" in response.text
|
||||
|
||||
|
||||
@patch("backend.api.features.workspace.routes.get_or_create_workspace")
|
||||
@patch("backend.api.features.workspace.routes.WorkspaceManager")
|
||||
def test_list_files_has_more_true_when_limit_exceeded(
|
||||
mock_manager_cls, mock_get_workspace
|
||||
):
|
||||
"""The limit+1 fetch trick must flip has_more=True and trim the page."""
|
||||
mock_get_workspace.return_value = _make_workspace()
|
||||
# Backend was asked for limit+1=3, and returned exactly 3 items.
|
||||
files = [
|
||||
_make_file(id="f1", name="a.txt"),
|
||||
_make_file(id="f2", name="b.txt"),
|
||||
_make_file(id="f3", name="c.txt"),
|
||||
]
|
||||
mock_instance = AsyncMock()
|
||||
mock_instance.list_files.return_value = files
|
||||
mock_manager_cls.return_value = mock_instance
|
||||
|
||||
response = client.get("/files?limit=2")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["has_more"] is True
|
||||
assert len(data["files"]) == 2
|
||||
assert data["files"][0]["id"] == "f1"
|
||||
assert data["files"][1]["id"] == "f2"
|
||||
mock_instance.list_files.assert_called_once_with(
|
||||
limit=3, offset=0, include_all_sessions=True
|
||||
)
|
||||
|
||||
|
||||
@patch("backend.api.features.workspace.routes.get_or_create_workspace")
|
||||
@patch("backend.api.features.workspace.routes.WorkspaceManager")
|
||||
def test_list_files_has_more_false_when_exactly_page_size(
|
||||
mock_manager_cls, mock_get_workspace
|
||||
):
|
||||
"""Exactly `limit` rows means we're on the last page — has_more=False."""
|
||||
mock_get_workspace.return_value = _make_workspace()
|
||||
files = [_make_file(id="f1", name="a.txt"), _make_file(id="f2", name="b.txt")]
|
||||
mock_instance = AsyncMock()
|
||||
mock_instance.list_files.return_value = files
|
||||
mock_manager_cls.return_value = mock_instance
|
||||
|
||||
response = client.get("/files?limit=2")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["has_more"] is False
|
||||
assert len(data["files"]) == 2
|
||||
|
||||
|
||||
@patch("backend.api.features.workspace.routes.get_or_create_workspace")
|
||||
@patch("backend.api.features.workspace.routes.WorkspaceManager")
|
||||
def test_list_files_offset_is_echoed_back(mock_manager_cls, mock_get_workspace):
|
||||
mock_get_workspace.return_value = _make_workspace()
|
||||
mock_instance = AsyncMock()
|
||||
mock_instance.list_files.return_value = []
|
||||
mock_manager_cls.return_value = mock_instance
|
||||
|
||||
response = client.get("/files?offset=50&limit=10")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["offset"] == 50
|
||||
mock_instance.list_files.assert_called_once_with(
|
||||
limit=11, offset=50, include_all_sessions=True
|
||||
)
|
||||
|
||||
@@ -18,7 +18,6 @@ from prisma.errors import PrismaError
|
||||
|
||||
import backend.api.features.admin.credit_admin_routes
|
||||
import backend.api.features.admin.execution_analytics_routes
|
||||
import backend.api.features.admin.platform_cost_routes
|
||||
import backend.api.features.admin.rate_limit_admin_routes
|
||||
import backend.api.features.admin.store_admin_routes
|
||||
import backend.api.features.builder
|
||||
@@ -330,11 +329,6 @@ app.include_router(
|
||||
tags=["v2", "admin"],
|
||||
prefix="/api/copilot",
|
||||
)
|
||||
app.include_router(
|
||||
backend.api.features.admin.platform_cost_routes.router,
|
||||
tags=["v2", "admin"],
|
||||
prefix="/api/admin",
|
||||
)
|
||||
app.include_router(
|
||||
backend.api.features.executions.review.routes.router,
|
||||
tags=["v2", "executions", "review"],
|
||||
|
||||
@@ -25,7 +25,6 @@ from backend.data.model import (
|
||||
Credentials,
|
||||
CredentialsFieldInfo,
|
||||
CredentialsMetaInput,
|
||||
NodeExecutionStats,
|
||||
SchemaField,
|
||||
is_credentials_field_name,
|
||||
)
|
||||
@@ -44,7 +43,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from backend.data.execution import ExecutionContext
|
||||
from backend.data.model import ContributorDetails
|
||||
from backend.data.model import ContributorDetails, NodeExecutionStats
|
||||
|
||||
from ..data.graph import Link
|
||||
|
||||
@@ -421,19 +420,6 @@ class BlockWebhookConfig(BlockManualWebhookConfig):
|
||||
class Block(ABC, Generic[BlockSchemaInputType, BlockSchemaOutputType]):
|
||||
_optimized_description: ClassVar[str | None] = None
|
||||
|
||||
def extra_runtime_cost(self, execution_stats: NodeExecutionStats) -> int:
|
||||
"""Return extra runtime cost to charge after this block run completes.
|
||||
|
||||
Called by the executor after a block finishes with COMPLETED status.
|
||||
The return value is the number of additional base-cost credits to
|
||||
charge beyond the single credit already collected by charge_usage
|
||||
at the start of execution. Defaults to 0 (no extra charges).
|
||||
|
||||
Override in blocks (e.g. OrchestratorBlock) that make multiple LLM
|
||||
calls within one run and should be billed per call.
|
||||
"""
|
||||
return 0
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
id: str = "",
|
||||
@@ -469,6 +455,8 @@ class Block(ABC, Generic[BlockSchemaInputType, BlockSchemaOutputType]):
|
||||
disabled: If the block is disabled, it will not be available for execution.
|
||||
static_output: Whether the output links of the block are static by default.
|
||||
"""
|
||||
from backend.data.model import NodeExecutionStats
|
||||
|
||||
self.id = id
|
||||
self.input_schema = input_schema
|
||||
self.output_schema = output_schema
|
||||
@@ -486,7 +474,7 @@ class Block(ABC, Generic[BlockSchemaInputType, BlockSchemaOutputType]):
|
||||
self.is_sensitive_action = is_sensitive_action
|
||||
# Read from ClassVar set by initialize_blocks()
|
||||
self.optimized_description: str | None = type(self)._optimized_description
|
||||
self.execution_stats: NodeExecutionStats = NodeExecutionStats()
|
||||
self.execution_stats: "NodeExecutionStats" = NodeExecutionStats()
|
||||
|
||||
if self.webhook_config:
|
||||
if isinstance(self.webhook_config, BlockWebhookConfig):
|
||||
@@ -566,7 +554,7 @@ class Block(ABC, Generic[BlockSchemaInputType, BlockSchemaOutputType]):
|
||||
return data
|
||||
raise ValueError(f"{self.name} did not produce any output for {output}")
|
||||
|
||||
def merge_stats(self, stats: NodeExecutionStats) -> NodeExecutionStats:
|
||||
def merge_stats(self, stats: "NodeExecutionStats") -> "NodeExecutionStats":
|
||||
self.execution_stats += stats
|
||||
return self.execution_stats
|
||||
|
||||
|
||||
@@ -207,9 +207,6 @@ class AIConditionBlock(AIBlockBase):
|
||||
NodeExecutionStats(
|
||||
input_token_count=response.prompt_tokens,
|
||||
output_token_count=response.completion_tokens,
|
||||
cache_read_token_count=response.cache_read_tokens,
|
||||
cache_creation_token_count=response.cache_creation_tokens,
|
||||
provider_cost=response.provider_cost,
|
||||
)
|
||||
)
|
||||
self.prompt = response.prompt
|
||||
|
||||
@@ -47,13 +47,7 @@ def _make_input(**overrides) -> AIConditionBlock.Input:
|
||||
return AIConditionBlock.Input(**defaults)
|
||||
|
||||
|
||||
def _mock_llm_response(
|
||||
response_text: str,
|
||||
*,
|
||||
cache_read_tokens: int = 0,
|
||||
cache_creation_tokens: int = 0,
|
||||
provider_cost: float | None = None,
|
||||
) -> LLMResponse:
|
||||
def _mock_llm_response(response_text: str) -> LLMResponse:
|
||||
return LLMResponse(
|
||||
raw_response="",
|
||||
prompt=[],
|
||||
@@ -62,9 +56,6 @@ def _mock_llm_response(
|
||||
prompt_tokens=10,
|
||||
completion_tokens=5,
|
||||
reasoning=None,
|
||||
cache_read_tokens=cache_read_tokens,
|
||||
cache_creation_tokens=cache_creation_tokens,
|
||||
provider_cost=provider_cost,
|
||||
)
|
||||
|
||||
|
||||
@@ -154,35 +145,3 @@ class TestExceptionPropagation:
|
||||
input_data = _make_input()
|
||||
with pytest.raises(RuntimeError, match="LLM provider error"):
|
||||
await _collect_outputs(block, input_data, credentials=TEST_CREDENTIALS)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Regression: cache tokens and provider_cost must be propagated to stats
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestCacheTokenPropagation:
|
||||
@pytest.mark.asyncio
|
||||
async def test_cache_tokens_propagated_to_stats(
|
||||
self, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
"""cache_read_tokens and cache_creation_tokens must be forwarded to
|
||||
NodeExecutionStats so that usage dashboards count cached tokens."""
|
||||
block = AIConditionBlock()
|
||||
|
||||
async def spy_llm(**kwargs):
|
||||
return _mock_llm_response(
|
||||
"true",
|
||||
cache_read_tokens=7,
|
||||
cache_creation_tokens=3,
|
||||
provider_cost=0.0012,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(block, "llm_call", spy_llm)
|
||||
|
||||
input_data = _make_input()
|
||||
await _collect_outputs(block, input_data, credentials=TEST_CREDENTIALS)
|
||||
|
||||
assert block.execution_stats.cache_read_token_count == 7
|
||||
assert block.execution_stats.cache_creation_token_count == 3
|
||||
assert block.execution_stats.provider_cost == 0.0012
|
||||
|
||||
@@ -17,7 +17,7 @@ from backend.blocks.apollo.models import (
|
||||
PrimaryPhone,
|
||||
SearchOrganizationsRequest,
|
||||
)
|
||||
from backend.data.model import CredentialsField, NodeExecutionStats, SchemaField
|
||||
from backend.data.model import CredentialsField, SchemaField
|
||||
|
||||
|
||||
class SearchOrganizationsBlock(Block):
|
||||
@@ -218,11 +218,6 @@ To find IDs, identify the values for organization_id when you call this endpoint
|
||||
) -> BlockOutput:
|
||||
query = SearchOrganizationsRequest(**input_data.model_dump())
|
||||
organizations = await self.search_organizations(query, credentials)
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(
|
||||
provider_cost=float(len(organizations)), provider_cost_type="items"
|
||||
)
|
||||
)
|
||||
for organization in organizations:
|
||||
yield "organization", organization
|
||||
yield "organizations", organizations
|
||||
|
||||
@@ -21,7 +21,7 @@ from backend.blocks.apollo.models import (
|
||||
SearchPeopleRequest,
|
||||
SenorityLevels,
|
||||
)
|
||||
from backend.data.model import CredentialsField, NodeExecutionStats, SchemaField
|
||||
from backend.data.model import CredentialsField, SchemaField
|
||||
|
||||
|
||||
class SearchPeopleBlock(Block):
|
||||
@@ -366,9 +366,4 @@ class SearchPeopleBlock(Block):
|
||||
*(enrich_or_fallback(person) for person in people)
|
||||
)
|
||||
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(
|
||||
provider_cost=float(len(people)), provider_cost_type="items"
|
||||
)
|
||||
)
|
||||
yield "people", people
|
||||
|
||||
@@ -4,7 +4,6 @@ import asyncio
|
||||
import contextvars
|
||||
import json
|
||||
import logging
|
||||
import uuid
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from typing_extensions import TypedDict # Needed for Python <3.12 compatibility
|
||||
@@ -23,7 +22,6 @@ from backend.copilot.permissions import (
|
||||
validate_block_identifiers,
|
||||
)
|
||||
from backend.data.model import SchemaField
|
||||
from backend.util.exceptions import BlockExecutionError
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from backend.data.execution import ExecutionContext
|
||||
@@ -33,37 +31,6 @@ logger = logging.getLogger(__name__)
|
||||
# Block ID shared between autopilot.py and copilot prompting.py.
|
||||
AUTOPILOT_BLOCK_ID = "c069dc6b-c3ed-4c12-b6e5-d47361e64ce6"
|
||||
|
||||
# Identifiers used when registering an AutoPilotBlock turn with the
|
||||
# stream registry — distinguishes block-originated turns from sub-session
|
||||
# or HTTP SSE turns in logs / observability.
|
||||
_AUTOPILOT_TOOL_CALL_ID = "autopilot_block"
|
||||
_AUTOPILOT_TOOL_NAME = "autopilot_block"
|
||||
|
||||
# Ceiling on how long AutoPilotBlock.execute_copilot will wait for the
|
||||
# enqueued turn's terminal event. Graph blocks run synchronously from
|
||||
# the caller's perspective so we wait effectively as long as needed; 6h
|
||||
# matches the previous abandoned-task cap and is much longer than any
|
||||
# legitimate AutoPilot turn.
|
||||
_AUTOPILOT_BLOCK_MAX_WAIT_SECONDS = 6 * 60 * 60 # 6 hours
|
||||
|
||||
|
||||
class SubAgentRecursionError(BlockExecutionError):
|
||||
"""Raised when the AutoPilot sub-agent nesting depth limit is exceeded.
|
||||
|
||||
Inherits :class:`BlockExecutionError` — this is a known, handled
|
||||
runtime failure at the block level (caller nested AutoPilotBlocks
|
||||
beyond the configured limit). Surfaces with the block_name /
|
||||
block_id the block framework expects, instead of being wrapped in
|
||||
``BlockUnknownError``.
|
||||
"""
|
||||
|
||||
def __init__(self, message: str) -> None:
|
||||
super().__init__(
|
||||
message=message,
|
||||
block_name="AutoPilotBlock",
|
||||
block_id=AUTOPILOT_BLOCK_ID,
|
||||
)
|
||||
|
||||
|
||||
class ToolCallEntry(TypedDict):
|
||||
"""A single tool invocation record from an autopilot execution."""
|
||||
@@ -296,15 +263,11 @@ class AutoPilotBlock(Block):
|
||||
user_id: str,
|
||||
permissions: "CopilotPermissions | None" = None,
|
||||
) -> tuple[str, list[ToolCallEntry], str, str, TokenUsage]:
|
||||
"""Invoke the copilot on the copilot_executor queue and aggregate the
|
||||
result.
|
||||
"""Invoke the copilot and collect all stream results.
|
||||
|
||||
Delegates to :func:`run_copilot_turn_via_queue` — the shared
|
||||
primitive used by ``run_sub_session`` too — which creates the
|
||||
stream_registry meta record, enqueues the job, and waits on the
|
||||
Redis stream for the terminal event. Any available
|
||||
copilot_executor worker picks up the job, so this call survives
|
||||
the graph-executor worker dying mid-turn (RabbitMQ redelivers).
|
||||
Delegates to :func:`collect_copilot_response` — the shared helper that
|
||||
consumes ``stream_chat_completion_sdk`` without wrapping it in an
|
||||
``asyncio.timeout`` (the SDK manages its own heartbeat-based timeouts).
|
||||
|
||||
Args:
|
||||
prompt: The user task/instruction.
|
||||
@@ -317,8 +280,8 @@ class AutoPilotBlock(Block):
|
||||
Returns:
|
||||
A tuple of (response_text, tool_calls, history_json, session_id, usage).
|
||||
"""
|
||||
from backend.copilot.sdk.session_waiter import (
|
||||
run_copilot_turn_via_queue, # avoid circular import
|
||||
from backend.copilot.sdk.collect import (
|
||||
collect_copilot_response, # avoid circular import
|
||||
)
|
||||
|
||||
tokens = _check_recursion(max_recursion_depth)
|
||||
@@ -331,35 +294,14 @@ class AutoPilotBlock(Block):
|
||||
if system_context:
|
||||
effective_prompt = f"[System Context: {system_context}]\n\n{prompt}"
|
||||
|
||||
outcome, result = await run_copilot_turn_via_queue(
|
||||
result = await collect_copilot_response(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
message=effective_prompt,
|
||||
# Graph block execution is synchronous from the caller's
|
||||
# perspective — wait effectively as long as needed. The
|
||||
# SDK enforces its own idle-based timeout inside the
|
||||
# stream_registry pipeline.
|
||||
timeout=_AUTOPILOT_BLOCK_MAX_WAIT_SECONDS,
|
||||
user_id=user_id,
|
||||
permissions=effective_permissions,
|
||||
tool_call_id=_AUTOPILOT_TOOL_CALL_ID,
|
||||
tool_name=_AUTOPILOT_TOOL_NAME,
|
||||
)
|
||||
if outcome == "failed":
|
||||
raise RuntimeError(
|
||||
"AutoPilot turn failed — see the session's transcript"
|
||||
)
|
||||
if outcome == "running":
|
||||
raise RuntimeError(
|
||||
"AutoPilot turn did not complete within "
|
||||
f"{_AUTOPILOT_BLOCK_MAX_WAIT_SECONDS}s — session "
|
||||
f"{session_id}"
|
||||
)
|
||||
|
||||
# Build a lightweight conversation summary from the aggregated data.
|
||||
# When ``result.queued`` is True the prompt rode on an already-
|
||||
# in-flight turn (``run_copilot_turn_via_queue`` queued it and
|
||||
# waited on the existing turn's stream); the aggregated result
|
||||
# is still valid, so the same rendering path applies.
|
||||
# Build a lightweight conversation summary from streamed data.
|
||||
turn_messages: list[dict[str, Any]] = [
|
||||
{"role": "user", "content": effective_prompt},
|
||||
]
|
||||
@@ -368,7 +310,7 @@ class AutoPilotBlock(Block):
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": result.response_text,
|
||||
"tool_calls": [tc.model_dump() for tc in result.tool_calls],
|
||||
"tool_calls": result.tool_calls,
|
||||
}
|
||||
)
|
||||
else:
|
||||
@@ -379,11 +321,11 @@ class AutoPilotBlock(Block):
|
||||
|
||||
tool_calls: list[ToolCallEntry] = [
|
||||
{
|
||||
"tool_call_id": tc.tool_call_id,
|
||||
"tool_name": tc.tool_name,
|
||||
"input": tc.input,
|
||||
"output": tc.output,
|
||||
"success": tc.success,
|
||||
"tool_call_id": tc["tool_call_id"],
|
||||
"tool_name": tc["tool_name"],
|
||||
"input": tc["input"],
|
||||
"output": tc["output"],
|
||||
"success": tc["success"],
|
||||
}
|
||||
for tc in result.tool_calls
|
||||
]
|
||||
@@ -441,8 +383,7 @@ class AutoPilotBlock(Block):
|
||||
sid = input_data.session_id
|
||||
if not sid:
|
||||
sid = await self.create_session(
|
||||
execution_context.user_id,
|
||||
dry_run=input_data.dry_run or execution_context.dry_run,
|
||||
execution_context.user_id, dry_run=input_data.dry_run
|
||||
)
|
||||
|
||||
# NOTE: No asyncio.timeout() here — the SDK manages its own
|
||||
@@ -468,41 +409,8 @@ class AutoPilotBlock(Block):
|
||||
yield "session_id", sid
|
||||
yield "error", "AutoPilot execution was cancelled."
|
||||
raise
|
||||
except SubAgentRecursionError as exc:
|
||||
# Deliberate block — re-enqueueing would immediately hit the limit
|
||||
# again, so skip recovery and just surface the error.
|
||||
yield "session_id", sid
|
||||
yield "error", str(exc)
|
||||
except Exception as exc:
|
||||
yield "session_id", sid
|
||||
# Recovery enqueue must happen BEFORE yielding "error": the block
|
||||
# framework (_base.execute) raises BlockExecutionError immediately
|
||||
# when it sees ("error", ...) and stops consuming the generator,
|
||||
# so any code after that yield is dead code in production.
|
||||
effective_prompt = input_data.prompt
|
||||
if input_data.system_context:
|
||||
effective_prompt = (
|
||||
f"[System Context: {input_data.system_context}]\n\n"
|
||||
f"{input_data.prompt}"
|
||||
)
|
||||
try:
|
||||
await _enqueue_for_recovery(
|
||||
sid,
|
||||
execution_context.user_id,
|
||||
effective_prompt,
|
||||
input_data.dry_run or execution_context.dry_run,
|
||||
)
|
||||
except asyncio.CancelledError:
|
||||
# Task cancelled during recovery — still yield the error
|
||||
# so the session_id + error pair is visible before re-raising.
|
||||
yield "error", str(exc)
|
||||
raise
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"AutoPilot session %s: recovery enqueue raised unexpectedly",
|
||||
sid[:12],
|
||||
exc_info=True,
|
||||
)
|
||||
yield "error", str(exc)
|
||||
|
||||
|
||||
@@ -530,13 +438,13 @@ def _check_recursion(
|
||||
when the caller exits to restore the previous depth.
|
||||
|
||||
Raises:
|
||||
SubAgentRecursionError: If the current depth already meets or exceeds the limit.
|
||||
RuntimeError: If the current depth already meets or exceeds the limit.
|
||||
"""
|
||||
current = _autopilot_recursion_depth.get()
|
||||
inherited = _autopilot_recursion_limit.get()
|
||||
limit = max_depth if inherited is None else min(inherited, max_depth)
|
||||
if current >= limit:
|
||||
raise SubAgentRecursionError(
|
||||
raise RuntimeError(
|
||||
f"AutoPilot recursion depth limit reached ({limit}). "
|
||||
"The autopilot has called itself too many times."
|
||||
)
|
||||
@@ -627,51 +535,3 @@ def _merge_inherited_permissions(
|
||||
# Return the token so the caller can restore the previous value in finally.
|
||||
token = _inherited_permissions.set(merged)
|
||||
return merged, token
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Recovery helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def _enqueue_for_recovery(
|
||||
session_id: str,
|
||||
user_id: str,
|
||||
message: str,
|
||||
dry_run: bool,
|
||||
) -> None:
|
||||
"""Re-enqueue an orphaned sub-agent session so a fresh executor picks it up.
|
||||
|
||||
When ``execute_copilot`` raises an unexpected exception the sub-agent
|
||||
session is left with ``last_role=user`` and no active consumer — identical
|
||||
to the state that caused Toran's reports of silent sub-agents. Publishing
|
||||
the original prompt back to the copilot queue lets the executor service
|
||||
resume the session without manual intervention.
|
||||
|
||||
Skipped for dry-run sessions (no real consumers listen to the queue for
|
||||
simulated sessions). Any failure to publish is logged and swallowed so
|
||||
it never masks the original exception.
|
||||
"""
|
||||
if dry_run:
|
||||
return
|
||||
try:
|
||||
from backend.copilot.executor.utils import ( # avoid circular import
|
||||
enqueue_copilot_turn,
|
||||
)
|
||||
|
||||
await asyncio.wait_for(
|
||||
enqueue_copilot_turn(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
message=message,
|
||||
turn_id=str(uuid.uuid4()),
|
||||
),
|
||||
timeout=10,
|
||||
)
|
||||
logger.info("AutoPilot session %s enqueued for recovery", session_id[:12])
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"AutoPilot session %s: failed to enqueue for recovery",
|
||||
session_id[:12],
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
@@ -1,712 +0,0 @@
|
||||
"""Unit tests for merge_stats cost tracking in individual blocks.
|
||||
|
||||
Covers the exa code_context, exa contents, and apollo organization blocks
|
||||
to verify provider cost is correctly extracted and reported.
|
||||
"""
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from pydantic import SecretStr
|
||||
|
||||
from backend.data.model import APIKeyCredentials, NodeExecutionStats
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
TEST_EXA_CREDENTIALS = APIKeyCredentials(
|
||||
id="01234567-89ab-cdef-0123-456789abcdef",
|
||||
provider="exa",
|
||||
api_key=SecretStr("mock-exa-api-key"),
|
||||
title="Mock Exa API key",
|
||||
expires_at=None,
|
||||
)
|
||||
|
||||
TEST_EXA_CREDENTIALS_INPUT = {
|
||||
"provider": TEST_EXA_CREDENTIALS.provider,
|
||||
"id": TEST_EXA_CREDENTIALS.id,
|
||||
"type": TEST_EXA_CREDENTIALS.type,
|
||||
"title": TEST_EXA_CREDENTIALS.title,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ExaCodeContextBlock — cost_dollars is a string like "0.005"
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestExaCodeContextBlockCostTracking:
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_stats_called_with_float_cost(self):
|
||||
"""float(cost_dollars) parsed from API string and passed to merge_stats."""
|
||||
from backend.blocks.exa.code_context import ExaCodeContextBlock
|
||||
|
||||
block = ExaCodeContextBlock()
|
||||
|
||||
api_response = {
|
||||
"requestId": "req-1",
|
||||
"query": "how to use hooks",
|
||||
"response": "Here are some examples...",
|
||||
"resultsCount": 3,
|
||||
"costDollars": "0.005",
|
||||
"searchTime": 1.2,
|
||||
"outputTokens": 100,
|
||||
}
|
||||
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = api_response
|
||||
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch(
|
||||
"backend.blocks.exa.code_context.Requests.post",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_resp,
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = ExaCodeContextBlock.Input(
|
||||
query="how to use hooks",
|
||||
credentials=TEST_EXA_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
results = []
|
||||
async for output in block.run(
|
||||
input_data,
|
||||
credentials=TEST_EXA_CREDENTIALS,
|
||||
):
|
||||
results.append(output)
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].provider_cost == pytest.approx(0.005)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_invalid_cost_dollars_does_not_raise(self):
|
||||
"""When cost_dollars cannot be parsed as float, merge_stats is not called."""
|
||||
from backend.blocks.exa.code_context import ExaCodeContextBlock
|
||||
|
||||
block = ExaCodeContextBlock()
|
||||
|
||||
api_response = {
|
||||
"requestId": "req-2",
|
||||
"query": "query",
|
||||
"response": "response",
|
||||
"resultsCount": 0,
|
||||
"costDollars": "N/A",
|
||||
"searchTime": 0.5,
|
||||
"outputTokens": 0,
|
||||
}
|
||||
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = api_response
|
||||
|
||||
merge_calls: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch(
|
||||
"backend.blocks.exa.code_context.Requests.post",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_resp,
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: merge_calls.append(s)
|
||||
),
|
||||
):
|
||||
input_data = ExaCodeContextBlock.Input(
|
||||
query="query",
|
||||
credentials=TEST_EXA_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(
|
||||
input_data,
|
||||
credentials=TEST_EXA_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert merge_calls == []
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zero_cost_is_tracked(self):
|
||||
"""A zero cost_dollars string '0.0' should still be recorded."""
|
||||
from backend.blocks.exa.code_context import ExaCodeContextBlock
|
||||
|
||||
block = ExaCodeContextBlock()
|
||||
|
||||
api_response = {
|
||||
"requestId": "req-3",
|
||||
"query": "query",
|
||||
"response": "...",
|
||||
"resultsCount": 1,
|
||||
"costDollars": "0.0",
|
||||
"searchTime": 0.1,
|
||||
"outputTokens": 10,
|
||||
}
|
||||
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = api_response
|
||||
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch(
|
||||
"backend.blocks.exa.code_context.Requests.post",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_resp,
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = ExaCodeContextBlock.Input(
|
||||
query="query",
|
||||
credentials=TEST_EXA_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(
|
||||
input_data,
|
||||
credentials=TEST_EXA_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].provider_cost == 0.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ExaContentsBlock — response.cost_dollars.total (CostDollars model)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestExaContentsBlockCostTracking:
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_stats_called_with_cost_dollars_total(self):
|
||||
"""provider_cost equals response.cost_dollars.total when present."""
|
||||
from backend.blocks.exa.contents import ExaContentsBlock
|
||||
from backend.blocks.exa.helpers import CostDollars
|
||||
|
||||
block = ExaContentsBlock()
|
||||
|
||||
cost_dollars = CostDollars(total=0.012)
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.results = []
|
||||
mock_response.context = None
|
||||
mock_response.statuses = None
|
||||
mock_response.cost_dollars = cost_dollars
|
||||
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch(
|
||||
"backend.blocks.exa.contents.AsyncExa",
|
||||
return_value=MagicMock(
|
||||
get_contents=AsyncMock(return_value=mock_response)
|
||||
),
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = ExaContentsBlock.Input(
|
||||
urls=["https://example.com"],
|
||||
credentials=TEST_EXA_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(
|
||||
input_data,
|
||||
credentials=TEST_EXA_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].provider_cost == pytest.approx(0.012)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_merge_stats_when_cost_dollars_absent(self):
|
||||
"""When response.cost_dollars is None, merge_stats is not called."""
|
||||
from backend.blocks.exa.contents import ExaContentsBlock
|
||||
|
||||
block = ExaContentsBlock()
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.results = []
|
||||
mock_response.context = None
|
||||
mock_response.statuses = None
|
||||
mock_response.cost_dollars = None
|
||||
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch(
|
||||
"backend.blocks.exa.contents.AsyncExa",
|
||||
return_value=MagicMock(
|
||||
get_contents=AsyncMock(return_value=mock_response)
|
||||
),
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = ExaContentsBlock.Input(
|
||||
urls=["https://example.com"],
|
||||
credentials=TEST_EXA_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(
|
||||
input_data,
|
||||
credentials=TEST_EXA_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert accumulated == []
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SearchOrganizationsBlock — provider_cost = float(len(organizations))
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestSearchOrganizationsBlockCostTracking:
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_stats_called_with_org_count(self):
|
||||
"""provider_cost == number of returned organizations, type == 'items'."""
|
||||
from backend.blocks.apollo._auth import TEST_CREDENTIALS as APOLLO_CREDS
|
||||
from backend.blocks.apollo._auth import (
|
||||
TEST_CREDENTIALS_INPUT as APOLLO_CREDS_INPUT,
|
||||
)
|
||||
from backend.blocks.apollo.models import Organization
|
||||
from backend.blocks.apollo.organization import SearchOrganizationsBlock
|
||||
|
||||
block = SearchOrganizationsBlock()
|
||||
|
||||
fake_orgs = [Organization(id=str(i), name=f"Org{i}") for i in range(3)]
|
||||
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
SearchOrganizationsBlock,
|
||||
"search_organizations",
|
||||
new_callable=AsyncMock,
|
||||
return_value=fake_orgs,
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = SearchOrganizationsBlock.Input(
|
||||
credentials=APOLLO_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
results = []
|
||||
async for output in block.run(
|
||||
input_data,
|
||||
credentials=APOLLO_CREDS,
|
||||
):
|
||||
results.append(output)
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].provider_cost == pytest.approx(3.0)
|
||||
assert accumulated[0].provider_cost_type == "items"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_org_list_tracks_zero(self):
|
||||
"""An empty organization list results in provider_cost=0.0."""
|
||||
from backend.blocks.apollo._auth import TEST_CREDENTIALS as APOLLO_CREDS
|
||||
from backend.blocks.apollo._auth import (
|
||||
TEST_CREDENTIALS_INPUT as APOLLO_CREDS_INPUT,
|
||||
)
|
||||
from backend.blocks.apollo.organization import SearchOrganizationsBlock
|
||||
|
||||
block = SearchOrganizationsBlock()
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
SearchOrganizationsBlock,
|
||||
"search_organizations",
|
||||
new_callable=AsyncMock,
|
||||
return_value=[],
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = SearchOrganizationsBlock.Input(
|
||||
credentials=APOLLO_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(
|
||||
input_data,
|
||||
credentials=APOLLO_CREDS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].provider_cost == 0.0
|
||||
assert accumulated[0].provider_cost_type == "items"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# JinaEmbeddingBlock — token count from usage.total_tokens
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestJinaEmbeddingBlockCostTracking:
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_stats_called_with_token_count(self):
|
||||
"""provider token count is recorded when API returns usage.total_tokens."""
|
||||
from backend.blocks.jina._auth import TEST_CREDENTIALS as JINA_CREDS
|
||||
from backend.blocks.jina._auth import TEST_CREDENTIALS_INPUT as JINA_CREDS_INPUT
|
||||
from backend.blocks.jina.embeddings import JinaEmbeddingBlock
|
||||
|
||||
block = JinaEmbeddingBlock()
|
||||
|
||||
api_response = {
|
||||
"data": [{"embedding": [0.1, 0.2, 0.3]}],
|
||||
"usage": {"total_tokens": 42},
|
||||
}
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = api_response
|
||||
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch(
|
||||
"backend.blocks.jina.embeddings.Requests.post",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_resp,
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = JinaEmbeddingBlock.Input(
|
||||
texts=["hello world"],
|
||||
credentials=JINA_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(input_data, credentials=JINA_CREDS):
|
||||
pass
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].input_token_count == 42
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_merge_stats_when_usage_absent(self):
|
||||
"""When API response omits usage field, merge_stats is not called."""
|
||||
from backend.blocks.jina._auth import TEST_CREDENTIALS as JINA_CREDS
|
||||
from backend.blocks.jina._auth import TEST_CREDENTIALS_INPUT as JINA_CREDS_INPUT
|
||||
from backend.blocks.jina.embeddings import JinaEmbeddingBlock
|
||||
|
||||
block = JinaEmbeddingBlock()
|
||||
|
||||
api_response = {
|
||||
"data": [{"embedding": [0.1, 0.2, 0.3]}],
|
||||
}
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = api_response
|
||||
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch(
|
||||
"backend.blocks.jina.embeddings.Requests.post",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_resp,
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = JinaEmbeddingBlock.Input(
|
||||
texts=["hello"],
|
||||
credentials=JINA_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(input_data, credentials=JINA_CREDS):
|
||||
pass
|
||||
|
||||
assert accumulated == []
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# UnrealTextToSpeechBlock — character count from input text length
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestUnrealTextToSpeechBlockCostTracking:
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_stats_called_with_character_count(self):
|
||||
"""provider_cost equals len(text) with type='characters'."""
|
||||
from backend.blocks.text_to_speech_block import TEST_CREDENTIALS as TTS_CREDS
|
||||
from backend.blocks.text_to_speech_block import (
|
||||
TEST_CREDENTIALS_INPUT as TTS_CREDS_INPUT,
|
||||
)
|
||||
from backend.blocks.text_to_speech_block import UnrealTextToSpeechBlock
|
||||
|
||||
block = UnrealTextToSpeechBlock()
|
||||
test_text = "Hello, world!"
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
UnrealTextToSpeechBlock,
|
||||
"call_unreal_speech_api",
|
||||
new_callable=AsyncMock,
|
||||
return_value={"OutputUri": "https://example.com/audio.mp3"},
|
||||
),
|
||||
patch.object(block, "merge_stats") as mock_merge,
|
||||
):
|
||||
input_data = UnrealTextToSpeechBlock.Input(
|
||||
text=test_text,
|
||||
credentials=TTS_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(input_data, credentials=TTS_CREDS):
|
||||
pass
|
||||
|
||||
mock_merge.assert_called_once()
|
||||
stats = mock_merge.call_args[0][0]
|
||||
assert stats.provider_cost == float(len(test_text))
|
||||
assert stats.provider_cost_type == "characters"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_text_gives_zero_characters(self):
|
||||
"""An empty text string results in provider_cost=0.0."""
|
||||
from backend.blocks.text_to_speech_block import TEST_CREDENTIALS as TTS_CREDS
|
||||
from backend.blocks.text_to_speech_block import (
|
||||
TEST_CREDENTIALS_INPUT as TTS_CREDS_INPUT,
|
||||
)
|
||||
from backend.blocks.text_to_speech_block import UnrealTextToSpeechBlock
|
||||
|
||||
block = UnrealTextToSpeechBlock()
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
UnrealTextToSpeechBlock,
|
||||
"call_unreal_speech_api",
|
||||
new_callable=AsyncMock,
|
||||
return_value={"OutputUri": "https://example.com/audio.mp3"},
|
||||
),
|
||||
patch.object(block, "merge_stats") as mock_merge,
|
||||
):
|
||||
input_data = UnrealTextToSpeechBlock.Input(
|
||||
text="",
|
||||
credentials=TTS_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(input_data, credentials=TTS_CREDS):
|
||||
pass
|
||||
|
||||
mock_merge.assert_called_once()
|
||||
stats = mock_merge.call_args[0][0]
|
||||
assert stats.provider_cost == 0.0
|
||||
assert stats.provider_cost_type == "characters"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# GoogleMapsSearchBlock — item count from search_places results
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestGoogleMapsSearchBlockCostTracking:
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_stats_called_with_place_count(self):
|
||||
"""provider_cost equals number of returned places, type == 'items'."""
|
||||
from backend.blocks.google_maps import TEST_CREDENTIALS as MAPS_CREDS
|
||||
from backend.blocks.google_maps import (
|
||||
TEST_CREDENTIALS_INPUT as MAPS_CREDS_INPUT,
|
||||
)
|
||||
from backend.blocks.google_maps import GoogleMapsSearchBlock
|
||||
|
||||
block = GoogleMapsSearchBlock()
|
||||
|
||||
fake_places = [{"name": f"Place{i}", "address": f"Addr{i}"} for i in range(4)]
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
GoogleMapsSearchBlock,
|
||||
"search_places",
|
||||
return_value=fake_places,
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = GoogleMapsSearchBlock.Input(
|
||||
query="coffee shops",
|
||||
credentials=MAPS_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(input_data, credentials=MAPS_CREDS):
|
||||
pass
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].provider_cost == 4.0
|
||||
assert accumulated[0].provider_cost_type == "items"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_results_tracks_zero(self):
|
||||
"""Zero places returned results in provider_cost=0.0."""
|
||||
from backend.blocks.google_maps import TEST_CREDENTIALS as MAPS_CREDS
|
||||
from backend.blocks.google_maps import (
|
||||
TEST_CREDENTIALS_INPUT as MAPS_CREDS_INPUT,
|
||||
)
|
||||
from backend.blocks.google_maps import GoogleMapsSearchBlock
|
||||
|
||||
block = GoogleMapsSearchBlock()
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
GoogleMapsSearchBlock,
|
||||
"search_places",
|
||||
return_value=[],
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = GoogleMapsSearchBlock.Input(
|
||||
query="nothing here",
|
||||
credentials=MAPS_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(input_data, credentials=MAPS_CREDS):
|
||||
pass
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].provider_cost == 0.0
|
||||
assert accumulated[0].provider_cost_type == "items"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SmartLeadAddLeadsBlock — item count from lead_list length
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestSmartLeadAddLeadsBlockCostTracking:
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_stats_called_with_lead_count(self):
|
||||
"""provider_cost equals number of leads uploaded, type == 'items'."""
|
||||
from backend.blocks.smartlead._auth import TEST_CREDENTIALS as SL_CREDS
|
||||
from backend.blocks.smartlead._auth import (
|
||||
TEST_CREDENTIALS_INPUT as SL_CREDS_INPUT,
|
||||
)
|
||||
from backend.blocks.smartlead.campaign import AddLeadToCampaignBlock
|
||||
from backend.blocks.smartlead.models import (
|
||||
AddLeadsToCampaignResponse,
|
||||
LeadInput,
|
||||
)
|
||||
|
||||
block = AddLeadToCampaignBlock()
|
||||
|
||||
fake_leads = [
|
||||
LeadInput(first_name="Alice", last_name="A", email="alice@example.com"),
|
||||
LeadInput(first_name="Bob", last_name="B", email="bob@example.com"),
|
||||
]
|
||||
fake_response = AddLeadsToCampaignResponse(
|
||||
ok=True,
|
||||
upload_count=2,
|
||||
total_leads=2,
|
||||
block_count=0,
|
||||
duplicate_count=0,
|
||||
invalid_email_count=0,
|
||||
invalid_emails=[],
|
||||
already_added_to_campaign=0,
|
||||
unsubscribed_leads=[],
|
||||
is_lead_limit_exhausted=False,
|
||||
lead_import_stopped_count=0,
|
||||
bounce_count=0,
|
||||
)
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
AddLeadToCampaignBlock,
|
||||
"add_leads_to_campaign",
|
||||
new_callable=AsyncMock,
|
||||
return_value=fake_response,
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = AddLeadToCampaignBlock.Input(
|
||||
campaign_id=123,
|
||||
lead_list=fake_leads,
|
||||
credentials=SL_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(input_data, credentials=SL_CREDS):
|
||||
pass
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].provider_cost == 2.0
|
||||
assert accumulated[0].provider_cost_type == "items"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SearchPeopleBlock — item count from people list length
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestSearchPeopleBlockCostTracking:
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_stats_called_with_people_count(self):
|
||||
"""provider_cost equals number of returned people, type == 'items'."""
|
||||
from backend.blocks.apollo._auth import TEST_CREDENTIALS as APOLLO_CREDS
|
||||
from backend.blocks.apollo._auth import (
|
||||
TEST_CREDENTIALS_INPUT as APOLLO_CREDS_INPUT,
|
||||
)
|
||||
from backend.blocks.apollo.models import Contact
|
||||
from backend.blocks.apollo.people import SearchPeopleBlock
|
||||
|
||||
block = SearchPeopleBlock()
|
||||
fake_people = [Contact(id=str(i), first_name=f"Person{i}") for i in range(5)]
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
SearchPeopleBlock,
|
||||
"search_people",
|
||||
new_callable=AsyncMock,
|
||||
return_value=fake_people,
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = SearchPeopleBlock.Input(
|
||||
credentials=APOLLO_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(input_data, credentials=APOLLO_CREDS):
|
||||
pass
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].provider_cost == pytest.approx(5.0)
|
||||
assert accumulated[0].provider_cost_type == "items"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_people_list_tracks_zero(self):
|
||||
"""An empty people list results in provider_cost=0.0."""
|
||||
from backend.blocks.apollo._auth import TEST_CREDENTIALS as APOLLO_CREDS
|
||||
from backend.blocks.apollo._auth import (
|
||||
TEST_CREDENTIALS_INPUT as APOLLO_CREDS_INPUT,
|
||||
)
|
||||
from backend.blocks.apollo.people import SearchPeopleBlock
|
||||
|
||||
block = SearchPeopleBlock()
|
||||
accumulated: list[NodeExecutionStats] = []
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
SearchPeopleBlock,
|
||||
"search_people",
|
||||
new_callable=AsyncMock,
|
||||
return_value=[],
|
||||
),
|
||||
patch.object(
|
||||
block, "merge_stats", side_effect=lambda s: accumulated.append(s)
|
||||
),
|
||||
):
|
||||
input_data = SearchPeopleBlock.Input(
|
||||
credentials=APOLLO_CREDS_INPUT, # type: ignore[arg-type]
|
||||
)
|
||||
async for _ in block.run(input_data, credentials=APOLLO_CREDS):
|
||||
pass
|
||||
|
||||
assert len(accumulated) == 1
|
||||
assert accumulated[0].provider_cost == 0.0
|
||||
assert accumulated[0].provider_cost_type == "items"
|
||||
@@ -9,7 +9,6 @@ from typing import Union
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.data.model import NodeExecutionStats
|
||||
from backend.sdk import (
|
||||
APIKeyCredentials,
|
||||
Block,
|
||||
@@ -117,10 +116,3 @@ class ExaCodeContextBlock(Block):
|
||||
yield "cost_dollars", context.cost_dollars
|
||||
yield "search_time", context.search_time
|
||||
yield "output_tokens", context.output_tokens
|
||||
|
||||
# Parse cost_dollars (API returns as string, e.g. "0.005")
|
||||
try:
|
||||
cost_usd = float(context.cost_dollars)
|
||||
self.merge_stats(NodeExecutionStats(provider_cost=cost_usd))
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
@@ -4,7 +4,6 @@ from typing import Optional
|
||||
from exa_py import AsyncExa
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.data.model import NodeExecutionStats
|
||||
from backend.sdk import (
|
||||
APIKeyCredentials,
|
||||
Block,
|
||||
@@ -224,6 +223,3 @@ class ExaContentsBlock(Block):
|
||||
|
||||
if response.cost_dollars:
|
||||
yield "cost_dollars", response.cost_dollars
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(provider_cost=response.cost_dollars.total)
|
||||
)
|
||||
|
||||
@@ -1,575 +0,0 @@
|
||||
"""Tests for cost tracking in Exa blocks.
|
||||
|
||||
Covers the cost_dollars → provider_cost → merge_stats path for both
|
||||
ExaContentsBlock and ExaCodeContextBlock.
|
||||
"""
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.blocks.exa._test import TEST_CREDENTIALS, TEST_CREDENTIALS_INPUT
|
||||
from backend.data.model import NodeExecutionStats
|
||||
|
||||
|
||||
class TestExaCodeContextCostTracking:
|
||||
"""ExaCodeContextBlock parses cost_dollars (string) and calls merge_stats."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_valid_cost_string_is_parsed_and_merged(self):
|
||||
"""A numeric cost string like '0.005' is merged as provider_cost."""
|
||||
from backend.blocks.exa.code_context import ExaCodeContextBlock
|
||||
|
||||
block = ExaCodeContextBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
api_response = {
|
||||
"requestId": "req-1",
|
||||
"query": "test query",
|
||||
"response": "some code",
|
||||
"resultsCount": 3,
|
||||
"costDollars": "0.005",
|
||||
"searchTime": 1.2,
|
||||
"outputTokens": 100,
|
||||
}
|
||||
|
||||
with patch("backend.blocks.exa.code_context.Requests") as mock_requests_cls:
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = api_response
|
||||
mock_requests_cls.return_value.post = AsyncMock(return_value=mock_resp)
|
||||
|
||||
outputs = []
|
||||
async for key, value in block.run(
|
||||
block.Input(query="test query", credentials=TEST_CREDENTIALS_INPUT), # type: ignore[arg-type]
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
outputs.append((key, value))
|
||||
|
||||
assert any(k == "cost_dollars" for k, _ in outputs)
|
||||
assert len(merged) == 1
|
||||
assert merged[0].provider_cost == pytest.approx(0.005)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_invalid_cost_string_does_not_raise(self):
|
||||
"""A non-numeric cost_dollars value is swallowed silently."""
|
||||
from backend.blocks.exa.code_context import ExaCodeContextBlock
|
||||
|
||||
block = ExaCodeContextBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
api_response = {
|
||||
"requestId": "req-2",
|
||||
"query": "test",
|
||||
"response": "code",
|
||||
"resultsCount": 0,
|
||||
"costDollars": "N/A",
|
||||
"searchTime": 0.5,
|
||||
"outputTokens": 0,
|
||||
}
|
||||
|
||||
with patch("backend.blocks.exa.code_context.Requests") as mock_requests_cls:
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = api_response
|
||||
mock_requests_cls.return_value.post = AsyncMock(return_value=mock_resp)
|
||||
|
||||
outputs = []
|
||||
async for key, value in block.run(
|
||||
block.Input(query="test", credentials=TEST_CREDENTIALS_INPUT), # type: ignore[arg-type]
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
outputs.append((key, value))
|
||||
|
||||
# No merge_stats call because float() raised ValueError
|
||||
assert len(merged) == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zero_cost_string_is_merged(self):
|
||||
"""'0.0' is a valid cost — should still be tracked."""
|
||||
from backend.blocks.exa.code_context import ExaCodeContextBlock
|
||||
|
||||
block = ExaCodeContextBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
api_response = {
|
||||
"requestId": "req-3",
|
||||
"query": "free query",
|
||||
"response": "result",
|
||||
"resultsCount": 1,
|
||||
"costDollars": "0.0",
|
||||
"searchTime": 0.1,
|
||||
"outputTokens": 10,
|
||||
}
|
||||
|
||||
with patch("backend.blocks.exa.code_context.Requests") as mock_requests_cls:
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = api_response
|
||||
mock_requests_cls.return_value.post = AsyncMock(return_value=mock_resp)
|
||||
|
||||
async for _ in block.run(
|
||||
block.Input(query="free query", credentials=TEST_CREDENTIALS_INPUT), # type: ignore[arg-type]
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 1
|
||||
assert merged[0].provider_cost == pytest.approx(0.0)
|
||||
|
||||
|
||||
class TestExaContentsCostTracking:
|
||||
"""ExaContentsBlock merges cost_dollars.total as provider_cost."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cost_dollars_total_is_merged(self):
|
||||
"""When the SDK response includes cost_dollars, its total is merged."""
|
||||
from backend.blocks.exa.contents import ExaContentsBlock
|
||||
from backend.blocks.exa.helpers import CostDollars
|
||||
|
||||
block = ExaContentsBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
mock_sdk_response = MagicMock()
|
||||
mock_sdk_response.results = []
|
||||
mock_sdk_response.context = None
|
||||
mock_sdk_response.statuses = None
|
||||
mock_sdk_response.cost_dollars = CostDollars(total=0.012)
|
||||
|
||||
with patch("backend.blocks.exa.contents.AsyncExa") as mock_exa_cls:
|
||||
mock_exa = MagicMock()
|
||||
mock_exa.get_contents = AsyncMock(return_value=mock_sdk_response)
|
||||
mock_exa_cls.return_value = mock_exa
|
||||
|
||||
async for _ in block.run(
|
||||
block.Input(urls=["https://example.com"], credentials=TEST_CREDENTIALS_INPUT), # type: ignore[arg-type]
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 1
|
||||
assert merged[0].provider_cost == pytest.approx(0.012)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_cost_dollars_skips_merge(self):
|
||||
"""When cost_dollars is absent, merge_stats is not called."""
|
||||
from backend.blocks.exa.contents import ExaContentsBlock
|
||||
|
||||
block = ExaContentsBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
mock_sdk_response = MagicMock()
|
||||
mock_sdk_response.results = []
|
||||
mock_sdk_response.context = None
|
||||
mock_sdk_response.statuses = None
|
||||
mock_sdk_response.cost_dollars = None
|
||||
|
||||
with patch("backend.blocks.exa.contents.AsyncExa") as mock_exa_cls:
|
||||
mock_exa = MagicMock()
|
||||
mock_exa.get_contents = AsyncMock(return_value=mock_sdk_response)
|
||||
mock_exa_cls.return_value = mock_exa
|
||||
|
||||
async for _ in block.run(
|
||||
block.Input(urls=["https://example.com"], credentials=TEST_CREDENTIALS_INPUT), # type: ignore[arg-type]
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zero_cost_dollars_is_merged(self):
|
||||
"""A total of 0.0 (free tier) should still be merged."""
|
||||
from backend.blocks.exa.contents import ExaContentsBlock
|
||||
from backend.blocks.exa.helpers import CostDollars
|
||||
|
||||
block = ExaContentsBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
mock_sdk_response = MagicMock()
|
||||
mock_sdk_response.results = []
|
||||
mock_sdk_response.context = None
|
||||
mock_sdk_response.statuses = None
|
||||
mock_sdk_response.cost_dollars = CostDollars(total=0.0)
|
||||
|
||||
with patch("backend.blocks.exa.contents.AsyncExa") as mock_exa_cls:
|
||||
mock_exa = MagicMock()
|
||||
mock_exa.get_contents = AsyncMock(return_value=mock_sdk_response)
|
||||
mock_exa_cls.return_value = mock_exa
|
||||
|
||||
async for _ in block.run(
|
||||
block.Input(urls=["https://example.com"], credentials=TEST_CREDENTIALS_INPUT), # type: ignore[arg-type]
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 1
|
||||
assert merged[0].provider_cost == pytest.approx(0.0)
|
||||
|
||||
|
||||
class TestExaSearchCostTracking:
|
||||
"""ExaSearchBlock merges cost_dollars.total as provider_cost."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cost_dollars_total_is_merged(self):
|
||||
"""When the SDK response includes cost_dollars, its total is merged."""
|
||||
from backend.blocks.exa.helpers import CostDollars
|
||||
from backend.blocks.exa.search import ExaSearchBlock
|
||||
|
||||
block = ExaSearchBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
mock_sdk_response = MagicMock()
|
||||
mock_sdk_response.results = []
|
||||
mock_sdk_response.context = None
|
||||
mock_sdk_response.resolved_search_type = None
|
||||
mock_sdk_response.cost_dollars = CostDollars(total=0.008)
|
||||
|
||||
with patch("backend.blocks.exa.search.AsyncExa") as mock_exa_cls:
|
||||
mock_exa = MagicMock()
|
||||
mock_exa.search = AsyncMock(return_value=mock_sdk_response)
|
||||
mock_exa_cls.return_value = mock_exa
|
||||
|
||||
async for _ in block.run(
|
||||
block.Input(query="test query", credentials=TEST_CREDENTIALS_INPUT), # type: ignore[arg-type]
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 1
|
||||
assert merged[0].provider_cost == pytest.approx(0.008)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_cost_dollars_skips_merge(self):
|
||||
"""When cost_dollars is absent, merge_stats is not called."""
|
||||
from backend.blocks.exa.search import ExaSearchBlock
|
||||
|
||||
block = ExaSearchBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
mock_sdk_response = MagicMock()
|
||||
mock_sdk_response.results = []
|
||||
mock_sdk_response.context = None
|
||||
mock_sdk_response.resolved_search_type = None
|
||||
mock_sdk_response.cost_dollars = None
|
||||
|
||||
with patch("backend.blocks.exa.search.AsyncExa") as mock_exa_cls:
|
||||
mock_exa = MagicMock()
|
||||
mock_exa.search = AsyncMock(return_value=mock_sdk_response)
|
||||
mock_exa_cls.return_value = mock_exa
|
||||
|
||||
async for _ in block.run(
|
||||
block.Input(query="test query", credentials=TEST_CREDENTIALS_INPUT), # type: ignore[arg-type]
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 0
|
||||
|
||||
|
||||
class TestExaSimilarCostTracking:
|
||||
"""ExaFindSimilarBlock merges cost_dollars.total as provider_cost."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cost_dollars_total_is_merged(self):
|
||||
"""When the SDK response includes cost_dollars, its total is merged."""
|
||||
from backend.blocks.exa.helpers import CostDollars
|
||||
from backend.blocks.exa.similar import ExaFindSimilarBlock
|
||||
|
||||
block = ExaFindSimilarBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
mock_sdk_response = MagicMock()
|
||||
mock_sdk_response.results = []
|
||||
mock_sdk_response.context = None
|
||||
mock_sdk_response.request_id = "req-1"
|
||||
mock_sdk_response.cost_dollars = CostDollars(total=0.015)
|
||||
|
||||
with patch("backend.blocks.exa.similar.AsyncExa") as mock_exa_cls:
|
||||
mock_exa = MagicMock()
|
||||
mock_exa.find_similar = AsyncMock(return_value=mock_sdk_response)
|
||||
mock_exa_cls.return_value = mock_exa
|
||||
|
||||
async for _ in block.run(
|
||||
block.Input(url="https://example.com", credentials=TEST_CREDENTIALS_INPUT), # type: ignore[arg-type]
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 1
|
||||
assert merged[0].provider_cost == pytest.approx(0.015)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_cost_dollars_skips_merge(self):
|
||||
"""When cost_dollars is absent, merge_stats is not called."""
|
||||
from backend.blocks.exa.similar import ExaFindSimilarBlock
|
||||
|
||||
block = ExaFindSimilarBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
mock_sdk_response = MagicMock()
|
||||
mock_sdk_response.results = []
|
||||
mock_sdk_response.context = None
|
||||
mock_sdk_response.request_id = "req-2"
|
||||
mock_sdk_response.cost_dollars = None
|
||||
|
||||
with patch("backend.blocks.exa.similar.AsyncExa") as mock_exa_cls:
|
||||
mock_exa = MagicMock()
|
||||
mock_exa.find_similar = AsyncMock(return_value=mock_sdk_response)
|
||||
mock_exa_cls.return_value = mock_exa
|
||||
|
||||
async for _ in block.run(
|
||||
block.Input(url="https://example.com", credentials=TEST_CREDENTIALS_INPUT), # type: ignore[arg-type]
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ExaCreateResearchBlock — cost_dollars from completed poll response
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
COMPLETED_RESEARCH_RESPONSE = {
|
||||
"researchId": "test-research-id",
|
||||
"status": "completed",
|
||||
"model": "exa-research",
|
||||
"instructions": "test instructions",
|
||||
"createdAt": 1700000000000,
|
||||
"finishedAt": 1700000060000,
|
||||
"costDollars": {
|
||||
"total": 0.05,
|
||||
"numSearches": 3,
|
||||
"numPages": 10,
|
||||
"reasoningTokens": 500,
|
||||
},
|
||||
"output": {"content": "Research findings...", "parsed": None},
|
||||
}
|
||||
|
||||
PENDING_RESEARCH_RESPONSE = {
|
||||
"researchId": "test-research-id",
|
||||
"status": "pending",
|
||||
"model": "exa-research",
|
||||
"instructions": "test instructions",
|
||||
"createdAt": 1700000000000,
|
||||
}
|
||||
|
||||
|
||||
class TestExaCreateResearchBlockCostTracking:
|
||||
"""ExaCreateResearchBlock merges cost from completed poll response."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cost_merged_when_research_completes(self):
|
||||
"""merge_stats called with provider_cost=total when poll returns completed."""
|
||||
from backend.blocks.exa.research import ExaCreateResearchBlock
|
||||
|
||||
block = ExaCreateResearchBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
create_resp = MagicMock()
|
||||
create_resp.json.return_value = PENDING_RESEARCH_RESPONSE
|
||||
|
||||
poll_resp = MagicMock()
|
||||
poll_resp.json.return_value = COMPLETED_RESEARCH_RESPONSE
|
||||
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.post = AsyncMock(return_value=create_resp)
|
||||
mock_instance.get = AsyncMock(return_value=poll_resp)
|
||||
|
||||
with (
|
||||
patch("backend.blocks.exa.research.Requests", return_value=mock_instance),
|
||||
patch("asyncio.sleep", new=AsyncMock()),
|
||||
):
|
||||
async for _ in block.run(
|
||||
block.Input(
|
||||
instructions="test instructions",
|
||||
wait_for_completion=True,
|
||||
credentials=TEST_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
),
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 1
|
||||
assert merged[0].provider_cost == pytest.approx(0.05)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_merge_when_no_cost_dollars(self):
|
||||
"""When completed response has no costDollars, merge_stats is not called."""
|
||||
from backend.blocks.exa.research import ExaCreateResearchBlock
|
||||
|
||||
block = ExaCreateResearchBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
no_cost_response = {**COMPLETED_RESEARCH_RESPONSE, "costDollars": None}
|
||||
create_resp = MagicMock()
|
||||
create_resp.json.return_value = PENDING_RESEARCH_RESPONSE
|
||||
poll_resp = MagicMock()
|
||||
poll_resp.json.return_value = no_cost_response
|
||||
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.post = AsyncMock(return_value=create_resp)
|
||||
mock_instance.get = AsyncMock(return_value=poll_resp)
|
||||
|
||||
with (
|
||||
patch("backend.blocks.exa.research.Requests", return_value=mock_instance),
|
||||
patch("asyncio.sleep", new=AsyncMock()),
|
||||
):
|
||||
async for _ in block.run(
|
||||
block.Input(
|
||||
instructions="test instructions",
|
||||
wait_for_completion=True,
|
||||
credentials=TEST_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
),
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert merged == []
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ExaGetResearchBlock — cost_dollars from single GET response
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestExaGetResearchBlockCostTracking:
|
||||
"""ExaGetResearchBlock merges cost when the fetched research has cost_dollars."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cost_merged_from_completed_research(self):
|
||||
"""merge_stats called with provider_cost=total when research has costDollars."""
|
||||
from backend.blocks.exa.research import ExaGetResearchBlock
|
||||
|
||||
block = ExaGetResearchBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
get_resp = MagicMock()
|
||||
get_resp.json.return_value = COMPLETED_RESEARCH_RESPONSE
|
||||
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.get = AsyncMock(return_value=get_resp)
|
||||
|
||||
with patch("backend.blocks.exa.research.Requests", return_value=mock_instance):
|
||||
async for _ in block.run(
|
||||
block.Input(
|
||||
research_id="test-research-id",
|
||||
credentials=TEST_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
),
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 1
|
||||
assert merged[0].provider_cost == pytest.approx(0.05)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_merge_when_no_cost_dollars(self):
|
||||
"""When research has no costDollars, merge_stats is not called."""
|
||||
from backend.blocks.exa.research import ExaGetResearchBlock
|
||||
|
||||
block = ExaGetResearchBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
no_cost_response = {**COMPLETED_RESEARCH_RESPONSE, "costDollars": None}
|
||||
get_resp = MagicMock()
|
||||
get_resp.json.return_value = no_cost_response
|
||||
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.get = AsyncMock(return_value=get_resp)
|
||||
|
||||
with patch("backend.blocks.exa.research.Requests", return_value=mock_instance):
|
||||
async for _ in block.run(
|
||||
block.Input(
|
||||
research_id="test-research-id",
|
||||
credentials=TEST_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
),
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert merged == []
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ExaWaitForResearchBlock — cost_dollars from polling response
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestExaWaitForResearchBlockCostTracking:
|
||||
"""ExaWaitForResearchBlock merges cost when the polled research has cost_dollars."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cost_merged_when_research_completes(self):
|
||||
"""merge_stats called with provider_cost=total once polling returns completed."""
|
||||
from backend.blocks.exa.research import ExaWaitForResearchBlock
|
||||
|
||||
block = ExaWaitForResearchBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
poll_resp = MagicMock()
|
||||
poll_resp.json.return_value = COMPLETED_RESEARCH_RESPONSE
|
||||
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.get = AsyncMock(return_value=poll_resp)
|
||||
|
||||
with (
|
||||
patch("backend.blocks.exa.research.Requests", return_value=mock_instance),
|
||||
patch("asyncio.sleep", new=AsyncMock()),
|
||||
):
|
||||
async for _ in block.run(
|
||||
block.Input(
|
||||
research_id="test-research-id",
|
||||
credentials=TEST_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
),
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(merged) == 1
|
||||
assert merged[0].provider_cost == pytest.approx(0.05)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_merge_when_no_cost_dollars(self):
|
||||
"""When completed research has no costDollars, merge_stats is not called."""
|
||||
from backend.blocks.exa.research import ExaWaitForResearchBlock
|
||||
|
||||
block = ExaWaitForResearchBlock()
|
||||
merged: list[NodeExecutionStats] = []
|
||||
block.merge_stats = lambda s: merged.append(s) # type: ignore[assignment]
|
||||
|
||||
no_cost_response = {**COMPLETED_RESEARCH_RESPONSE, "costDollars": None}
|
||||
poll_resp = MagicMock()
|
||||
poll_resp.json.return_value = no_cost_response
|
||||
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.get = AsyncMock(return_value=poll_resp)
|
||||
|
||||
with (
|
||||
patch("backend.blocks.exa.research.Requests", return_value=mock_instance),
|
||||
patch("asyncio.sleep", new=AsyncMock()),
|
||||
):
|
||||
async for _ in block.run(
|
||||
block.Input(
|
||||
research_id="test-research-id",
|
||||
credentials=TEST_CREDENTIALS_INPUT, # type: ignore[arg-type]
|
||||
),
|
||||
credentials=TEST_CREDENTIALS,
|
||||
):
|
||||
pass
|
||||
|
||||
assert merged == []
|
||||
@@ -12,7 +12,6 @@ from typing import Any, Dict, List, Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.data.model import NodeExecutionStats
|
||||
from backend.sdk import (
|
||||
APIKeyCredentials,
|
||||
Block,
|
||||
@@ -233,11 +232,6 @@ class ExaCreateResearchBlock(Block):
|
||||
|
||||
if research.cost_dollars:
|
||||
yield "cost_total", research.cost_dollars.total
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(
|
||||
provider_cost=research.cost_dollars.total
|
||||
)
|
||||
)
|
||||
return
|
||||
|
||||
await asyncio.sleep(check_interval)
|
||||
@@ -352,9 +346,6 @@ class ExaGetResearchBlock(Block):
|
||||
yield "cost_searches", research.cost_dollars.num_searches
|
||||
yield "cost_pages", research.cost_dollars.num_pages
|
||||
yield "cost_reasoning_tokens", research.cost_dollars.reasoning_tokens
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(provider_cost=research.cost_dollars.total)
|
||||
)
|
||||
|
||||
yield "error_message", research.error
|
||||
|
||||
@@ -441,9 +432,6 @@ class ExaWaitForResearchBlock(Block):
|
||||
|
||||
if research.cost_dollars:
|
||||
yield "cost_total", research.cost_dollars.total
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(provider_cost=research.cost_dollars.total)
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ from typing import Optional
|
||||
|
||||
from exa_py import AsyncExa
|
||||
|
||||
from backend.data.model import NodeExecutionStats
|
||||
from backend.sdk import (
|
||||
APIKeyCredentials,
|
||||
Block,
|
||||
@@ -207,6 +206,3 @@ class ExaSearchBlock(Block):
|
||||
|
||||
if response.cost_dollars:
|
||||
yield "cost_dollars", response.cost_dollars
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(provider_cost=response.cost_dollars.total)
|
||||
)
|
||||
|
||||
@@ -3,7 +3,6 @@ from typing import Optional
|
||||
|
||||
from exa_py import AsyncExa
|
||||
|
||||
from backend.data.model import NodeExecutionStats
|
||||
from backend.sdk import (
|
||||
APIKeyCredentials,
|
||||
Block,
|
||||
@@ -168,6 +167,3 @@ class ExaFindSimilarBlock(Block):
|
||||
|
||||
if response.cost_dollars:
|
||||
yield "cost_dollars", response.cost_dollars
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(provider_cost=response.cost_dollars.total)
|
||||
)
|
||||
|
||||
@@ -14,7 +14,6 @@ from backend.data.model import (
|
||||
APIKeyCredentials,
|
||||
CredentialsField,
|
||||
CredentialsMetaInput,
|
||||
NodeExecutionStats,
|
||||
SchemaField,
|
||||
)
|
||||
from backend.integrations.providers import ProviderName
|
||||
@@ -118,11 +117,6 @@ class GoogleMapsSearchBlock(Block):
|
||||
input_data.radius,
|
||||
input_data.max_results,
|
||||
)
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(
|
||||
provider_cost=float(len(places)), provider_cost_type="items"
|
||||
)
|
||||
)
|
||||
for place in places:
|
||||
yield "place", place
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ from backend.blocks.jina._auth import (
|
||||
JinaCredentialsField,
|
||||
JinaCredentialsInput,
|
||||
)
|
||||
from backend.data.model import NodeExecutionStats, SchemaField
|
||||
from backend.data.model import SchemaField
|
||||
from backend.util.request import Requests
|
||||
|
||||
|
||||
@@ -45,13 +45,5 @@ class JinaEmbeddingBlock(Block):
|
||||
}
|
||||
data = {"input": input_data.texts, "model": input_data.model}
|
||||
response = await Requests().post(url, headers=headers, json=data)
|
||||
resp_json = response.json()
|
||||
embeddings = [e["embedding"] for e in resp_json["data"]]
|
||||
usage = resp_json.get("usage", {})
|
||||
if usage.get("total_tokens"):
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(
|
||||
input_token_count=usage.get("total_tokens", 0),
|
||||
)
|
||||
)
|
||||
embeddings = [e["embedding"] for e in response.json()["data"]]
|
||||
yield "embeddings", embeddings
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# This file contains a lot of prompt block strings that would trigger "line too long"
|
||||
# flake8: noqa: E501
|
||||
import logging
|
||||
import math
|
||||
import re
|
||||
import secrets
|
||||
from abc import ABC
|
||||
@@ -14,7 +13,6 @@ import ollama
|
||||
import openai
|
||||
from anthropic.types import ToolParam
|
||||
from groq import AsyncGroq
|
||||
from openai.types.chat import ChatCompletion as OpenAIChatCompletion
|
||||
from pydantic import BaseModel, SecretStr
|
||||
|
||||
from backend.blocks._base import (
|
||||
@@ -106,6 +104,7 @@ class LlmModelMeta(EnumMeta):
|
||||
|
||||
|
||||
class LlmModel(str, Enum, metaclass=LlmModelMeta):
|
||||
|
||||
@classmethod
|
||||
def _missing_(cls, value: object) -> "LlmModel | None":
|
||||
"""Handle provider-prefixed model names like 'anthropic/claude-sonnet-4-6'."""
|
||||
@@ -202,8 +201,6 @@ class LlmModel(str, Enum, metaclass=LlmModelMeta):
|
||||
GROK_4 = "x-ai/grok-4"
|
||||
GROK_4_FAST = "x-ai/grok-4-fast"
|
||||
GROK_4_1_FAST = "x-ai/grok-4.1-fast"
|
||||
GROK_4_20 = "x-ai/grok-4.20"
|
||||
GROK_4_20_MULTI_AGENT = "x-ai/grok-4.20-multi-agent"
|
||||
GROK_CODE_FAST_1 = "x-ai/grok-code-fast-1"
|
||||
KIMI_K2 = "moonshotai/kimi-k2"
|
||||
QWEN3_235B_A22B_THINKING = "qwen/qwen3-235b-a22b-thinking-2507"
|
||||
@@ -628,18 +625,6 @@ MODEL_METADATA = {
|
||||
LlmModel.GROK_4_1_FAST: ModelMetadata(
|
||||
"open_router", 2000000, 30000, "Grok 4.1 Fast", "OpenRouter", "xAI", 1
|
||||
),
|
||||
LlmModel.GROK_4_20: ModelMetadata(
|
||||
"open_router", 2000000, 100000, "Grok 4.20", "OpenRouter", "xAI", 3
|
||||
),
|
||||
LlmModel.GROK_4_20_MULTI_AGENT: ModelMetadata(
|
||||
"open_router",
|
||||
2000000,
|
||||
100000,
|
||||
"Grok 4.20 Multi-Agent",
|
||||
"OpenRouter",
|
||||
"xAI",
|
||||
3,
|
||||
),
|
||||
LlmModel.GROK_CODE_FAST_1: ModelMetadata(
|
||||
"open_router", 256000, 10000, "Grok Code Fast 1", "OpenRouter", "xAI", 1
|
||||
),
|
||||
@@ -751,20 +736,17 @@ class LLMResponse(BaseModel):
|
||||
tool_calls: Optional[List[ToolContentBlock]] | None
|
||||
prompt_tokens: int
|
||||
completion_tokens: int
|
||||
cache_read_tokens: int = 0
|
||||
cache_creation_tokens: int = 0
|
||||
reasoning: Optional[str] = None
|
||||
provider_cost: float | None = None
|
||||
|
||||
|
||||
def convert_openai_tool_fmt_to_anthropic(
|
||||
openai_tools: list[dict] | None = None,
|
||||
) -> Iterable[ToolParam] | anthropic.NotGiven:
|
||||
) -> Iterable[ToolParam] | anthropic.Omit:
|
||||
"""
|
||||
Convert OpenAI tool format to Anthropic tool format.
|
||||
"""
|
||||
if not openai_tools or len(openai_tools) == 0:
|
||||
return anthropic.NOT_GIVEN
|
||||
return anthropic.omit
|
||||
|
||||
anthropic_tools = []
|
||||
for tool in openai_tools:
|
||||
@@ -789,35 +771,6 @@ def convert_openai_tool_fmt_to_anthropic(
|
||||
return anthropic_tools
|
||||
|
||||
|
||||
def extract_openrouter_cost(response: OpenAIChatCompletion) -> float | None:
|
||||
"""Extract OpenRouter's `x-total-cost` header from an OpenAI SDK response.
|
||||
|
||||
OpenRouter returns the per-request USD cost in a response header. The
|
||||
OpenAI SDK exposes the raw httpx response via an undocumented `_response`
|
||||
attribute. We use try/except AttributeError so that if the SDK ever drops
|
||||
or renames that attribute, the warning is visible in logs rather than
|
||||
silently degrading to no cost tracking.
|
||||
"""
|
||||
try:
|
||||
raw_resp = response._response # type: ignore[attr-defined]
|
||||
except AttributeError:
|
||||
logger.warning(
|
||||
"OpenAI SDK response missing _response attribute"
|
||||
" — OpenRouter cost tracking unavailable"
|
||||
)
|
||||
return None
|
||||
try:
|
||||
cost_header = raw_resp.headers.get("x-total-cost")
|
||||
if not cost_header:
|
||||
return None
|
||||
cost = float(cost_header)
|
||||
if not math.isfinite(cost) or cost < 0:
|
||||
return None
|
||||
return cost
|
||||
except (ValueError, TypeError, AttributeError):
|
||||
return None
|
||||
|
||||
|
||||
def extract_openai_reasoning(response) -> str | None:
|
||||
"""Extract reasoning from OpenAI-compatible response if available."""
|
||||
"""Note: This will likely not working since the reasoning is not present in another Response API"""
|
||||
@@ -900,21 +853,6 @@ async def llm_call(
|
||||
provider = llm_model.metadata.provider
|
||||
context_window = llm_model.context_window
|
||||
|
||||
# Transparent OpenRouter routing for Anthropic models: when an OpenRouter API key
|
||||
# is configured, route direct-Anthropic models through OpenRouter instead. This
|
||||
# gives us the x-total-cost header for free, so provider_cost is always populated
|
||||
# without manual token-rate arithmetic.
|
||||
or_key = settings.secrets.open_router_api_key
|
||||
or_model_id: str | None = None
|
||||
if provider == "anthropic" and or_key:
|
||||
provider = "open_router"
|
||||
credentials = APIKeyCredentials(
|
||||
provider=ProviderName.OPEN_ROUTER,
|
||||
title="OpenRouter (auto)",
|
||||
api_key=SecretStr(or_key),
|
||||
)
|
||||
or_model_id = f"anthropic/{llm_model.value}"
|
||||
|
||||
if compress_prompt_to_fit:
|
||||
result = await compress_context(
|
||||
messages=prompt,
|
||||
@@ -1000,12 +938,8 @@ async def llm_call(
|
||||
reasoning=reasoning,
|
||||
)
|
||||
elif provider == "anthropic":
|
||||
|
||||
an_tools = convert_openai_tool_fmt_to_anthropic(tools)
|
||||
# Cache tool definitions alongside the system prompt.
|
||||
# Placing cache_control on the last tool caches all tool schemas as a
|
||||
# single prefix — reads cost 10% of normal input tokens.
|
||||
if isinstance(an_tools, list) and an_tools:
|
||||
an_tools[-1] = {**an_tools[-1], "cache_control": {"type": "ephemeral"}}
|
||||
|
||||
system_messages = [p["content"] for p in prompt if p["role"] == "system"]
|
||||
sysprompt = " ".join(system_messages)
|
||||
@@ -1028,34 +962,14 @@ async def llm_call(
|
||||
client = anthropic.AsyncAnthropic(
|
||||
api_key=credentials.api_key.get_secret_value()
|
||||
)
|
||||
# create_kwargs is built as a plain dict so we can conditionally add
|
||||
# the `system` field only when the prompt is non-empty. Anthropic's
|
||||
# API rejects empty text blocks (returns HTTP 400), so omitting the
|
||||
# field is the correct behaviour for whitespace-only prompts.
|
||||
create_kwargs: dict[str, Any] = dict(
|
||||
resp = await client.messages.create(
|
||||
model=llm_model.value,
|
||||
system=sysprompt,
|
||||
messages=messages,
|
||||
max_tokens=max_tokens,
|
||||
# `an_tools` may be anthropic.NOT_GIVEN when no tools were
|
||||
# configured. The SDK treats NOT_GIVEN as a sentinel meaning "omit
|
||||
# this field from the serialized request", so passing it here is
|
||||
# equivalent to not including the key at all — no `tools` field is
|
||||
# sent to the API in that case.
|
||||
tools=an_tools,
|
||||
timeout=600,
|
||||
)
|
||||
if sysprompt.strip():
|
||||
# Wrap the system prompt in a single cacheable text block.
|
||||
# The guard intentionally omits `system` for whitespace-only
|
||||
# prompts — Anthropic rejects empty text blocks with HTTP 400.
|
||||
create_kwargs["system"] = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": sysprompt,
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
}
|
||||
]
|
||||
resp = await client.messages.create(**create_kwargs)
|
||||
|
||||
if not resp.content:
|
||||
raise ValueError("No content returned from Anthropic.")
|
||||
@@ -1100,11 +1014,6 @@ async def llm_call(
|
||||
tool_calls=tool_calls,
|
||||
prompt_tokens=resp.usage.input_tokens,
|
||||
completion_tokens=resp.usage.output_tokens,
|
||||
cache_read_tokens=getattr(resp.usage, "cache_read_input_tokens", None) or 0,
|
||||
cache_creation_tokens=getattr(
|
||||
resp.usage, "cache_creation_input_tokens", None
|
||||
)
|
||||
or 0,
|
||||
reasoning=reasoning,
|
||||
)
|
||||
elif provider == "groq":
|
||||
@@ -1173,7 +1082,7 @@ async def llm_call(
|
||||
"HTTP-Referer": "https://agpt.co",
|
||||
"X-Title": "AutoGPT",
|
||||
},
|
||||
model=or_model_id or llm_model.value,
|
||||
model=llm_model.value,
|
||||
messages=prompt, # type: ignore
|
||||
max_tokens=max_tokens,
|
||||
tools=tools_param, # type: ignore
|
||||
@@ -1194,7 +1103,6 @@ async def llm_call(
|
||||
prompt_tokens=response.usage.prompt_tokens if response.usage else 0,
|
||||
completion_tokens=response.usage.completion_tokens if response.usage else 0,
|
||||
reasoning=reasoning,
|
||||
provider_cost=extract_openrouter_cost(response),
|
||||
)
|
||||
elif provider == "llama_api":
|
||||
tools_param = tools if tools else openai.NOT_GIVEN
|
||||
@@ -1502,7 +1410,6 @@ class AIStructuredResponseGeneratorBlock(AIBlockBase):
|
||||
|
||||
error_feedback_message = ""
|
||||
llm_model = input_data.model
|
||||
total_provider_cost: float | None = None
|
||||
|
||||
for retry_count in range(input_data.retry):
|
||||
logger.debug(f"LLM request: {prompt}")
|
||||
@@ -1520,19 +1427,12 @@ class AIStructuredResponseGeneratorBlock(AIBlockBase):
|
||||
max_tokens=input_data.max_tokens,
|
||||
)
|
||||
response_text = llm_response.response
|
||||
# Accumulate token counts and provider_cost for every attempt
|
||||
# (each call costs tokens and USD, regardless of validation outcome).
|
||||
token_stats = NodeExecutionStats(
|
||||
input_token_count=llm_response.prompt_tokens,
|
||||
output_token_count=llm_response.completion_tokens,
|
||||
cache_read_token_count=llm_response.cache_read_tokens,
|
||||
cache_creation_token_count=llm_response.cache_creation_tokens,
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(
|
||||
input_token_count=llm_response.prompt_tokens,
|
||||
output_token_count=llm_response.completion_tokens,
|
||||
)
|
||||
)
|
||||
self.merge_stats(token_stats)
|
||||
if llm_response.provider_cost is not None:
|
||||
total_provider_cost = (
|
||||
total_provider_cost or 0.0
|
||||
) + llm_response.provider_cost
|
||||
logger.debug(f"LLM attempt-{retry_count} response: {response_text}")
|
||||
|
||||
if input_data.expected_format:
|
||||
@@ -1601,7 +1501,6 @@ class AIStructuredResponseGeneratorBlock(AIBlockBase):
|
||||
NodeExecutionStats(
|
||||
llm_call_count=retry_count + 1,
|
||||
llm_retry_count=retry_count,
|
||||
provider_cost=total_provider_cost,
|
||||
)
|
||||
)
|
||||
yield "response", response_obj
|
||||
@@ -1622,7 +1521,6 @@ class AIStructuredResponseGeneratorBlock(AIBlockBase):
|
||||
NodeExecutionStats(
|
||||
llm_call_count=retry_count + 1,
|
||||
llm_retry_count=retry_count,
|
||||
provider_cost=total_provider_cost,
|
||||
)
|
||||
)
|
||||
yield "response", {"response": response_text}
|
||||
@@ -1654,10 +1552,6 @@ class AIStructuredResponseGeneratorBlock(AIBlockBase):
|
||||
|
||||
error_feedback_message = f"Error calling LLM: {e}"
|
||||
|
||||
# All retries exhausted or user-error break: persist accumulated cost so
|
||||
# the executor can still charge/report the spend even on failure.
|
||||
if total_provider_cost is not None:
|
||||
self.merge_stats(NodeExecutionStats(provider_cost=total_provider_cost))
|
||||
raise RuntimeError(error_feedback_message)
|
||||
|
||||
def response_format_instructions(
|
||||
|
||||
@@ -36,7 +36,6 @@ from backend.data.execution import ExecutionContext
|
||||
from backend.data.model import NodeExecutionStats, SchemaField
|
||||
from backend.util import json
|
||||
from backend.util.clients import get_database_manager_async_client
|
||||
from backend.util.exceptions import InsufficientBalanceError
|
||||
from backend.util.prompt import MAIN_OBJECTIVE_PREFIX
|
||||
from backend.util.security import SENSITIVE_FIELD_NAMES
|
||||
from backend.util.tool_call_loop import (
|
||||
@@ -252,13 +251,8 @@ def _convert_raw_response_to_dict(
|
||||
# Already a dict (from tests or some providers)
|
||||
return raw_response
|
||||
elif _is_responses_api_object(raw_response):
|
||||
# OpenAI Responses API: extract individual output items.
|
||||
# Strip 'status' — it's a response-only field that OpenAI rejects
|
||||
# when the item is sent back as input on the next API call.
|
||||
items = [
|
||||
{k: v for k, v in json.to_dict(item).items() if k != "status"}
|
||||
for item in raw_response.output
|
||||
]
|
||||
# OpenAI Responses API: extract individual output items
|
||||
items = [json.to_dict(item) for item in raw_response.output]
|
||||
return items if items else [{"role": "assistant", "content": ""}]
|
||||
else:
|
||||
# Chat Completions / Anthropic return message objects
|
||||
@@ -365,31 +359,10 @@ def _disambiguate_tool_names(tools: list[dict[str, Any]]) -> None:
|
||||
|
||||
|
||||
class OrchestratorBlock(Block):
|
||||
"""A block that uses a language model to orchestrate tool calls.
|
||||
|
||||
Supports both single-shot and iterative agent mode execution.
|
||||
|
||||
**InsufficientBalanceError propagation contract**: ``InsufficientBalanceError``
|
||||
(IBE) must always re-raise through every ``except`` block in this class.
|
||||
Swallowing IBE would let the agent loop continue with unpaid work. Every
|
||||
exception handler that catches ``Exception`` includes an explicit IBE
|
||||
re-raise carve-out for this reason.
|
||||
"""
|
||||
|
||||
def extra_runtime_cost(self, execution_stats: NodeExecutionStats) -> int:
|
||||
"""Charge one extra runtime cost per LLM call beyond the first.
|
||||
|
||||
In agent mode each iteration makes one LLM call. The first is already
|
||||
covered by charge_usage(); this returns the number of additional
|
||||
credits so the executor can bill the remaining calls post-completion.
|
||||
|
||||
SDK-mode exemption: when the block runs via _execute_tools_sdk_mode,
|
||||
the SDK manages its own conversation loop and only exposes aggregate
|
||||
usage. We hardcode llm_call_count=1 there (the SDK does not report a
|
||||
per-turn call count), so this method always returns 0 for SDK-mode
|
||||
executions. Per-iteration billing does not apply to SDK mode.
|
||||
"""
|
||||
return max(0, execution_stats.llm_call_count - 1)
|
||||
A block that uses a language model to orchestrate tool calls, supporting both
|
||||
single-shot and iterative agent mode execution.
|
||||
"""
|
||||
|
||||
# MCP server name used by the Claude Code SDK execution mode. Keep in sync
|
||||
# with _create_graph_mcp_server and the MCP_PREFIX derivation in _execute_tools_sdk_mode.
|
||||
@@ -871,10 +844,7 @@ class OrchestratorBlock(Block):
|
||||
NodeExecutionStats(
|
||||
input_token_count=resp.prompt_tokens,
|
||||
output_token_count=resp.completion_tokens,
|
||||
cache_read_token_count=resp.cache_read_tokens,
|
||||
cache_creation_token_count=resp.cache_creation_tokens,
|
||||
llm_call_count=1,
|
||||
provider_cost=resp.provider_cost,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1099,10 +1069,7 @@ class OrchestratorBlock(Block):
|
||||
input_data=input_value,
|
||||
)
|
||||
|
||||
if node_exec_result is None:
|
||||
raise RuntimeError(
|
||||
f"upsert_execution_input returned None for node {sink_node_id}"
|
||||
)
|
||||
assert node_exec_result is not None, "node_exec_result should not be None"
|
||||
|
||||
# Create NodeExecutionEntry for execution manager
|
||||
node_exec_entry = NodeExecutionEntry(
|
||||
@@ -1137,86 +1104,15 @@ class OrchestratorBlock(Block):
|
||||
task=node_exec_future,
|
||||
)
|
||||
|
||||
# Execute the node directly since we're in the Orchestrator context.
|
||||
# Wrap in try/except so the future is always resolved, even on
|
||||
# error — an unresolved Future would block anything awaiting it.
|
||||
#
|
||||
# on_node_execution is decorated with @async_error_logged(swallow=True),
|
||||
# which catches BaseException and returns None rather than raising.
|
||||
# Treat a None return as a failure: set_exception so the future
|
||||
# carries an error state rather than a None result, and return an
|
||||
# error response so the LLM knows the tool failed.
|
||||
try:
|
||||
tool_node_stats = await execution_processor.on_node_execution(
|
||||
# Execute the node directly since we're in the Orchestrator context
|
||||
node_exec_future.set_result(
|
||||
await execution_processor.on_node_execution(
|
||||
node_exec=node_exec_entry,
|
||||
node_exec_progress=node_exec_progress,
|
||||
nodes_input_masks=None,
|
||||
graph_stats_pair=graph_stats_pair,
|
||||
)
|
||||
if tool_node_stats is None:
|
||||
nil_err = RuntimeError(
|
||||
f"on_node_execution returned None for node {sink_node_id} "
|
||||
"(error was swallowed by @async_error_logged)"
|
||||
)
|
||||
node_exec_future.set_exception(nil_err)
|
||||
resp = _create_tool_response(
|
||||
tool_call.id,
|
||||
"Tool execution returned no result",
|
||||
responses_api=responses_api,
|
||||
)
|
||||
resp["_is_error"] = True
|
||||
return resp
|
||||
node_exec_future.set_result(tool_node_stats)
|
||||
except Exception as exec_err:
|
||||
node_exec_future.set_exception(exec_err)
|
||||
raise
|
||||
|
||||
# Charge user credits AFTER successful tool execution. Tools
|
||||
# spawned by the orchestrator bypass the main execution queue
|
||||
# (where _charge_usage is called), so we must charge here to
|
||||
# avoid free tool execution. Charging post-completion (vs.
|
||||
# pre-execution) avoids billing users for failed tool calls.
|
||||
# Skipped for dry runs.
|
||||
#
|
||||
# `error is None` intentionally excludes both Exception and
|
||||
# BaseException subclasses (e.g. CancelledError) so cancelled
|
||||
# or terminated tool runs are not billed.
|
||||
#
|
||||
# Billing errors (including non-balance exceptions) are kept
|
||||
# in a separate try/except so they are never silently swallowed
|
||||
# by the generic tool-error handler below.
|
||||
if (
|
||||
not execution_params.execution_context.dry_run
|
||||
and tool_node_stats.error is None
|
||||
):
|
||||
try:
|
||||
tool_cost, _ = await execution_processor.charge_node_usage(
|
||||
node_exec_entry,
|
||||
)
|
||||
except InsufficientBalanceError:
|
||||
# IBE must propagate — see OrchestratorBlock class docstring.
|
||||
# Log the billing failure here so the discarded tool result
|
||||
# is traceable before the loop aborts.
|
||||
logger.warning(
|
||||
"Insufficient balance charging for tool node %s after "
|
||||
"successful execution; agent loop will be aborted",
|
||||
sink_node_id,
|
||||
)
|
||||
raise
|
||||
except Exception:
|
||||
# Non-billing charge failures (DB outage, network, etc.)
|
||||
# must NOT propagate to the outer except handler because
|
||||
# the tool itself succeeded. Re-raising would mark the
|
||||
# tool as failed (_is_error=True), causing the LLM to
|
||||
# retry side-effectful operations. Log and continue.
|
||||
logger.exception(
|
||||
"Unexpected error charging for tool node %s; "
|
||||
"tool execution was successful",
|
||||
sink_node_id,
|
||||
)
|
||||
tool_cost = 0
|
||||
if tool_cost > 0:
|
||||
self.merge_stats(NodeExecutionStats(extra_cost=tool_cost))
|
||||
)
|
||||
|
||||
# Get outputs from database after execution completes using database manager client
|
||||
node_outputs = await db_client.get_execution_outputs_by_node_exec_id(
|
||||
@@ -1229,26 +1125,18 @@ class OrchestratorBlock(Block):
|
||||
if node_outputs
|
||||
else "Tool executed successfully"
|
||||
)
|
||||
resp = _create_tool_response(
|
||||
return _create_tool_response(
|
||||
tool_call.id, tool_response_content, responses_api=responses_api
|
||||
)
|
||||
resp["_is_error"] = False
|
||||
return resp
|
||||
|
||||
except InsufficientBalanceError:
|
||||
# IBE must propagate — see class docstring.
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.warning("Tool execution with manager failed: %s", e, exc_info=True)
|
||||
# Return a generic error to the LLM — internal exception messages
|
||||
# may contain server paths, DB details, or infrastructure info.
|
||||
resp = _create_tool_response(
|
||||
logger.warning("Tool execution with manager failed: %s", e)
|
||||
# Return error response
|
||||
return _create_tool_response(
|
||||
tool_call.id,
|
||||
"Tool execution failed due to an internal error",
|
||||
f"Tool execution failed: {e}",
|
||||
responses_api=responses_api,
|
||||
)
|
||||
resp["_is_error"] = True
|
||||
return resp
|
||||
|
||||
async def _agent_mode_llm_caller(
|
||||
self,
|
||||
@@ -1348,16 +1236,13 @@ class OrchestratorBlock(Block):
|
||||
content = str(raw_content)
|
||||
else:
|
||||
content = "Tool executed successfully"
|
||||
tool_failed = result.get("_is_error", True)
|
||||
tool_failed = content.startswith("Tool execution failed:")
|
||||
return ToolCallResult(
|
||||
tool_call_id=tool_call.id,
|
||||
tool_name=tool_call.name,
|
||||
content=content,
|
||||
is_error=tool_failed,
|
||||
)
|
||||
except InsufficientBalanceError:
|
||||
# IBE must propagate — see class docstring.
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("Tool execution failed: %s", e)
|
||||
return ToolCallResult(
|
||||
@@ -1477,13 +1362,9 @@ class OrchestratorBlock(Block):
|
||||
"arguments": tc.arguments,
|
||||
},
|
||||
)
|
||||
except InsufficientBalanceError:
|
||||
# IBE must propagate — see class docstring.
|
||||
raise
|
||||
except Exception as e:
|
||||
# Catch all OTHER errors (validation, network, API) so that
|
||||
# the block surfaces them as user-visible output instead of
|
||||
# crashing.
|
||||
# Catch all errors (validation, network, API) so that the block
|
||||
# surfaces them as user-visible output instead of crashing.
|
||||
yield "error", str(e)
|
||||
return
|
||||
|
||||
@@ -1561,14 +1442,11 @@ class OrchestratorBlock(Block):
|
||||
text = content
|
||||
else:
|
||||
text = json.dumps(content)
|
||||
tool_failed = result.get("_is_error", True)
|
||||
tool_failed = text.startswith("Tool execution failed:")
|
||||
return {
|
||||
"content": [{"type": "text", "text": text}],
|
||||
"isError": tool_failed,
|
||||
}
|
||||
except InsufficientBalanceError:
|
||||
# IBE must propagate — see class docstring.
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("SDK tool execution failed: %s", e)
|
||||
return {
|
||||
@@ -1694,7 +1572,6 @@ class OrchestratorBlock(Block):
|
||||
conversation: list[dict[str, Any]] = list(prompt) # Start with input prompt
|
||||
total_prompt_tokens = 0
|
||||
total_completion_tokens = 0
|
||||
total_cost_usd: float | None = None
|
||||
|
||||
sdk_error: Exception | None = None
|
||||
try:
|
||||
@@ -1838,8 +1715,6 @@ class OrchestratorBlock(Block):
|
||||
total_completion_tokens += getattr(
|
||||
sdk_msg.usage, "output_tokens", 0
|
||||
)
|
||||
if sdk_msg.total_cost_usd is not None:
|
||||
total_cost_usd = sdk_msg.total_cost_usd
|
||||
finally:
|
||||
if pending_task is not None and not pending_task.done():
|
||||
pending_task.cancel()
|
||||
@@ -1847,15 +1722,11 @@ class OrchestratorBlock(Block):
|
||||
await pending_task
|
||||
except (asyncio.CancelledError, StopAsyncIteration):
|
||||
pass
|
||||
except InsufficientBalanceError:
|
||||
# IBE must propagate — see class docstring. The `finally`
|
||||
# block below still runs and records partial token usage.
|
||||
raise
|
||||
except Exception as e:
|
||||
# Surface OTHER SDK errors as user-visible output instead
|
||||
# of crashing, consistent with _execute_tools_agent_mode
|
||||
# error handling. Don't return yet — fall through to
|
||||
# merge_stats below so partial token usage is always recorded.
|
||||
# Surface SDK errors as user-visible output instead of crashing,
|
||||
# consistent with _execute_tools_agent_mode error handling.
|
||||
# Don't return yet — fall through to merge_stats below so
|
||||
# partial token usage is always recorded.
|
||||
sdk_error = e
|
||||
finally:
|
||||
# Always record usage stats, even on error. The SDK may have
|
||||
@@ -1863,17 +1734,12 @@ class OrchestratorBlock(Block):
|
||||
# those stats would under-count resource usage.
|
||||
# llm_call_count=1 is approximate; the SDK manages its own
|
||||
# multi-turn loop and only exposes aggregate usage.
|
||||
if (
|
||||
total_prompt_tokens > 0
|
||||
or total_completion_tokens > 0
|
||||
or total_cost_usd is not None
|
||||
):
|
||||
if total_prompt_tokens > 0 or total_completion_tokens > 0:
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(
|
||||
input_token_count=total_prompt_tokens,
|
||||
output_token_count=total_completion_tokens,
|
||||
llm_call_count=1,
|
||||
provider_cost=total_cost_usd,
|
||||
)
|
||||
)
|
||||
# Clean up execution-specific working directory.
|
||||
|
||||
@@ -23,7 +23,7 @@ from backend.blocks.smartlead.models import (
|
||||
SaveSequencesResponse,
|
||||
Sequence,
|
||||
)
|
||||
from backend.data.model import CredentialsField, NodeExecutionStats, SchemaField
|
||||
from backend.data.model import CredentialsField, SchemaField
|
||||
|
||||
|
||||
class CreateCampaignBlock(Block):
|
||||
@@ -226,12 +226,6 @@ class AddLeadToCampaignBlock(Block):
|
||||
response = await self.add_leads_to_campaign(
|
||||
input_data.campaign_id, input_data.lead_list, credentials
|
||||
)
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(
|
||||
provider_cost=float(len(input_data.lead_list)),
|
||||
provider_cost_type="items",
|
||||
)
|
||||
)
|
||||
|
||||
yield "campaign_id", input_data.campaign_id
|
||||
yield "upload_count", response.upload_count
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
"""Tests for AutoPilotBlock: recursion guard, streaming, validation, and error paths."""
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.blocks.autopilot import (
|
||||
AUTOPILOT_BLOCK_ID,
|
||||
AutoPilotBlock,
|
||||
SubAgentRecursionError,
|
||||
_autopilot_recursion_depth,
|
||||
_autopilot_recursion_limit,
|
||||
_check_recursion,
|
||||
@@ -58,7 +57,7 @@ class TestCheckRecursion:
|
||||
try:
|
||||
t2 = _check_recursion(2)
|
||||
try:
|
||||
with pytest.raises(SubAgentRecursionError):
|
||||
with pytest.raises(RuntimeError, match="recursion depth limit"):
|
||||
_check_recursion(2)
|
||||
finally:
|
||||
_reset_recursion(t2)
|
||||
@@ -72,7 +71,7 @@ class TestCheckRecursion:
|
||||
t2 = _check_recursion(10) # inner wants 10, but inherited is 2
|
||||
try:
|
||||
# depth is now 2, limit is min(10, 2) = 2 → should raise
|
||||
with pytest.raises(SubAgentRecursionError):
|
||||
with pytest.raises(RuntimeError, match="recursion depth limit"):
|
||||
_check_recursion(10)
|
||||
finally:
|
||||
_reset_recursion(t2)
|
||||
@@ -82,7 +81,7 @@ class TestCheckRecursion:
|
||||
def test_limit_of_one_blocks_immediately_on_second_call(self):
|
||||
t1 = _check_recursion(1)
|
||||
try:
|
||||
with pytest.raises(SubAgentRecursionError):
|
||||
with pytest.raises(RuntimeError):
|
||||
_check_recursion(1)
|
||||
finally:
|
||||
_reset_recursion(t1)
|
||||
@@ -176,29 +175,6 @@ class TestRunValidation:
|
||||
assert outputs["session_id"] == "sess-cancel"
|
||||
assert "cancelled" in outputs.get("error", "").lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dry_run_inherited_from_execution_context(self, block):
|
||||
"""execution_context.dry_run=True must be OR-ed into create_session dry_run
|
||||
so that nested AutoPilot sessions simulate even when input_data.dry_run=False.
|
||||
"""
|
||||
mock_result = (
|
||||
"ok",
|
||||
[],
|
||||
"[]",
|
||||
"sess-dry",
|
||||
{"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
|
||||
)
|
||||
block.execute_copilot = AsyncMock(return_value=mock_result)
|
||||
block.create_session = AsyncMock(return_value="sess-dry")
|
||||
|
||||
input_data = block.Input(prompt="test", max_recursion_depth=3, dry_run=False)
|
||||
ctx = _make_context()
|
||||
ctx.dry_run = True # outer execution is dry_run
|
||||
async for _ in block.run(input_data, execution_context=ctx):
|
||||
pass
|
||||
|
||||
block.create_session.assert_called_once_with(ctx.user_id, dry_run=True)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_existing_session_id_skips_create(self, block):
|
||||
"""When session_id is provided, create_session should not be called."""
|
||||
@@ -245,171 +221,3 @@ class TestBlockRegistration:
|
||||
# The field should exist (inherited) but there should be no explicit
|
||||
# redefinition. We verify by checking the class __annotations__ directly.
|
||||
assert "error" not in AutoPilotBlock.Output.__annotations__
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Recovery enqueue integration tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestRecoveryEnqueue:
|
||||
"""Tests that run() enqueues orphaned sessions for recovery on failure."""
|
||||
|
||||
@pytest.fixture
|
||||
def block(self):
|
||||
return AutoPilotBlock()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_recovery_enqueued_on_transient_exception(self, block):
|
||||
"""A generic exception should trigger _enqueue_for_recovery."""
|
||||
block.execute_copilot = AsyncMock(side_effect=RuntimeError("network error"))
|
||||
block.create_session = AsyncMock(return_value="sess-recover")
|
||||
|
||||
input_data = block.Input(prompt="do work", max_recursion_depth=3)
|
||||
ctx = _make_context()
|
||||
|
||||
with patch("backend.blocks.autopilot._enqueue_for_recovery") as mock_enqueue:
|
||||
mock_enqueue.return_value = None
|
||||
outputs = {}
|
||||
async for name, value in block.run(input_data, execution_context=ctx):
|
||||
outputs[name] = value
|
||||
|
||||
assert "network error" in outputs.get("error", "")
|
||||
mock_enqueue.assert_awaited_once_with(
|
||||
"sess-recover",
|
||||
ctx.user_id,
|
||||
"do work",
|
||||
False,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_recovery_not_enqueued_for_recursion_limit(self, block):
|
||||
"""Recursion limit errors are deliberate — no recovery enqueue."""
|
||||
block.execute_copilot = AsyncMock(
|
||||
side_effect=SubAgentRecursionError(
|
||||
"AutoPilot recursion depth limit reached (3). "
|
||||
"The autopilot has called itself too many times."
|
||||
)
|
||||
)
|
||||
block.create_session = AsyncMock(return_value="sess-rec-limit")
|
||||
|
||||
input_data = block.Input(prompt="recurse", max_recursion_depth=3)
|
||||
ctx = _make_context()
|
||||
|
||||
with patch("backend.blocks.autopilot._enqueue_for_recovery") as mock_enqueue:
|
||||
async for _ in block.run(input_data, execution_context=ctx):
|
||||
pass
|
||||
|
||||
mock_enqueue.assert_not_awaited()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_recovery_not_enqueued_for_dry_run(self, block):
|
||||
"""dry_run=True sessions must not be enqueued (no real consumers)."""
|
||||
block.execute_copilot = AsyncMock(side_effect=RuntimeError("transient"))
|
||||
block.create_session = AsyncMock(return_value="sess-dry-fail")
|
||||
|
||||
input_data = block.Input(prompt="test", max_recursion_depth=3, dry_run=True)
|
||||
ctx = _make_context()
|
||||
|
||||
with patch("backend.blocks.autopilot._enqueue_for_recovery") as mock_enqueue:
|
||||
mock_enqueue.return_value = None
|
||||
async for _ in block.run(input_data, execution_context=ctx):
|
||||
pass
|
||||
|
||||
# _enqueue_for_recovery is called with dry_run=True,
|
||||
# so the inner guard returns early without publishing to the queue.
|
||||
mock_enqueue.assert_awaited_once()
|
||||
positional = mock_enqueue.call_args_list[0][0]
|
||||
assert positional[3] is True # dry_run=True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_recovery_enqueue_failure_does_not_mask_original_error(self, block):
|
||||
"""If _enqueue_for_recovery itself raises, the original error is still yielded."""
|
||||
block.execute_copilot = AsyncMock(side_effect=ValueError("original"))
|
||||
block.create_session = AsyncMock(return_value="sess-enq-fail")
|
||||
|
||||
input_data = block.Input(prompt="hello", max_recursion_depth=3)
|
||||
ctx = _make_context()
|
||||
|
||||
async def _failing_enqueue(*args, **kwargs):
|
||||
raise OSError("rabbitmq down")
|
||||
|
||||
with patch(
|
||||
"backend.blocks.autopilot._enqueue_for_recovery",
|
||||
side_effect=_failing_enqueue,
|
||||
):
|
||||
outputs = {}
|
||||
async for name, value in block.run(input_data, execution_context=ctx):
|
||||
outputs[name] = value
|
||||
|
||||
# Original error must still be surfaced despite the enqueue failure
|
||||
assert outputs.get("error") == "original"
|
||||
assert outputs.get("session_id") == "sess-enq-fail"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_recovery_uses_dry_run_from_context(self, block):
|
||||
"""execution_context.dry_run=True is OR-ed into the dry_run arg."""
|
||||
block.execute_copilot = AsyncMock(side_effect=RuntimeError("fail"))
|
||||
block.create_session = AsyncMock(return_value="sess-ctx-dry")
|
||||
|
||||
input_data = block.Input(prompt="test", max_recursion_depth=3, dry_run=False)
|
||||
ctx = _make_context()
|
||||
ctx.dry_run = True # outer execution is dry_run
|
||||
|
||||
with patch("backend.blocks.autopilot._enqueue_for_recovery") as mock_enqueue:
|
||||
mock_enqueue.return_value = None
|
||||
async for _ in block.run(input_data, execution_context=ctx):
|
||||
pass
|
||||
|
||||
mock_enqueue.assert_awaited_once()
|
||||
positional = mock_enqueue.call_args_list[0][0]
|
||||
assert positional[3] is True # dry_run=True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_recovery_uses_effective_prompt_with_system_context(self, block):
|
||||
"""When system_context is set, _enqueue_for_recovery receives the
|
||||
effective_prompt (system_context prepended) so the dedup check in
|
||||
maybe_append_user_message passes on replay."""
|
||||
block.execute_copilot = AsyncMock(side_effect=RuntimeError("e2b timeout"))
|
||||
block.create_session = AsyncMock(return_value="sess-sys-ctx")
|
||||
|
||||
input_data = block.Input(
|
||||
prompt="do work",
|
||||
system_context="Be concise.",
|
||||
max_recursion_depth=3,
|
||||
)
|
||||
ctx = _make_context()
|
||||
|
||||
with patch("backend.blocks.autopilot._enqueue_for_recovery") as mock_enqueue:
|
||||
mock_enqueue.return_value = None
|
||||
async for _ in block.run(input_data, execution_context=ctx):
|
||||
pass
|
||||
|
||||
mock_enqueue.assert_awaited_once()
|
||||
positional = mock_enqueue.call_args_list[0][0]
|
||||
assert positional[2] == "[System Context: Be concise.]\n\ndo work"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_recovery_cancelled_error_still_yields_error(self, block):
|
||||
"""CancelledError during _enqueue_for_recovery still yields the error output."""
|
||||
block.execute_copilot = AsyncMock(side_effect=RuntimeError("e2b stall"))
|
||||
block.create_session = AsyncMock(return_value="sess-cancel")
|
||||
|
||||
async def _cancelled_enqueue(*args, **kwargs):
|
||||
raise asyncio.CancelledError
|
||||
|
||||
outputs = {}
|
||||
with patch(
|
||||
"backend.blocks.autopilot._enqueue_for_recovery",
|
||||
side_effect=_cancelled_enqueue,
|
||||
):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
async for name, value in block.run(
|
||||
block.Input(prompt="do work", max_recursion_depth=3),
|
||||
execution_context=_make_context(),
|
||||
):
|
||||
outputs[name] = value
|
||||
|
||||
# error must be yielded even when recovery raises CancelledError
|
||||
assert outputs.get("error") == "e2b stall"
|
||||
assert outputs.get("session_id") == "sess-cancel"
|
||||
|
||||
@@ -46,110 +46,6 @@ class TestLLMStatsTracking:
|
||||
assert response.completion_tokens == 20
|
||||
assert response.response == "Test response"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_llm_call_anthropic_returns_cache_tokens(self):
|
||||
"""Test that llm_call returns cache read/creation tokens from Anthropic."""
|
||||
from pydantic import SecretStr
|
||||
|
||||
import backend.blocks.llm as llm
|
||||
from backend.data.model import APIKeyCredentials
|
||||
|
||||
anthropic_creds = APIKeyCredentials(
|
||||
id="test-anthropic-id",
|
||||
provider="anthropic",
|
||||
api_key=SecretStr("mock-anthropic-key"),
|
||||
title="Mock Anthropic key",
|
||||
expires_at=None,
|
||||
)
|
||||
|
||||
mock_content_block = MagicMock()
|
||||
mock_content_block.type = "text"
|
||||
mock_content_block.text = "Test anthropic response"
|
||||
|
||||
mock_usage = MagicMock()
|
||||
mock_usage.input_tokens = 15
|
||||
mock_usage.output_tokens = 25
|
||||
mock_usage.cache_read_input_tokens = 100
|
||||
mock_usage.cache_creation_input_tokens = 50
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.content = [mock_content_block]
|
||||
mock_response.usage = mock_usage
|
||||
mock_response.stop_reason = "end_turn"
|
||||
|
||||
with (
|
||||
patch("anthropic.AsyncAnthropic") as mock_anthropic,
|
||||
patch("backend.blocks.llm.settings") as mock_settings,
|
||||
):
|
||||
mock_settings.secrets.open_router_api_key = ""
|
||||
mock_client = AsyncMock()
|
||||
mock_anthropic.return_value = mock_client
|
||||
mock_client.messages.create = AsyncMock(return_value=mock_response)
|
||||
|
||||
response = await llm.llm_call(
|
||||
credentials=anthropic_creds,
|
||||
llm_model=llm.LlmModel.CLAUDE_3_HAIKU,
|
||||
prompt=[{"role": "user", "content": "Hello"}],
|
||||
max_tokens=100,
|
||||
)
|
||||
|
||||
assert isinstance(response, llm.LLMResponse)
|
||||
assert response.prompt_tokens == 15
|
||||
assert response.completion_tokens == 25
|
||||
assert response.cache_read_tokens == 100
|
||||
assert response.cache_creation_tokens == 50
|
||||
assert response.response == "Test anthropic response"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_anthropic_routes_through_openrouter_when_key_present(self):
|
||||
"""When open_router_api_key is set, Anthropic models route via OpenRouter."""
|
||||
from pydantic import SecretStr
|
||||
|
||||
import backend.blocks.llm as llm
|
||||
from backend.data.model import APIKeyCredentials
|
||||
|
||||
anthropic_creds = APIKeyCredentials(
|
||||
id="test-anthropic-id",
|
||||
provider="anthropic",
|
||||
api_key=SecretStr("mock-anthropic-key"),
|
||||
title="Mock Anthropic key",
|
||||
)
|
||||
|
||||
mock_choice = MagicMock()
|
||||
mock_choice.message.content = "routed response"
|
||||
mock_choice.message.tool_calls = None
|
||||
|
||||
mock_usage = MagicMock()
|
||||
mock_usage.prompt_tokens = 10
|
||||
mock_usage.completion_tokens = 5
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.choices = [mock_choice]
|
||||
mock_response.usage = mock_usage
|
||||
|
||||
mock_create = AsyncMock(return_value=mock_response)
|
||||
|
||||
with (
|
||||
patch("openai.AsyncOpenAI") as mock_openai,
|
||||
patch("backend.blocks.llm.settings") as mock_settings,
|
||||
):
|
||||
mock_settings.secrets.open_router_api_key = "sk-or-test-key"
|
||||
mock_client = MagicMock()
|
||||
mock_openai.return_value = mock_client
|
||||
mock_client.chat.completions.create = mock_create
|
||||
|
||||
await llm.llm_call(
|
||||
credentials=anthropic_creds,
|
||||
llm_model=llm.LlmModel.CLAUDE_3_HAIKU,
|
||||
prompt=[{"role": "user", "content": "Hello"}],
|
||||
max_tokens=100,
|
||||
)
|
||||
|
||||
# Verify OpenAI client was used (not Anthropic SDK) and model was prefixed
|
||||
mock_openai.assert_called_once()
|
||||
call_kwargs = mock_create.call_args.kwargs
|
||||
assert call_kwargs["model"] == "anthropic/claude-3-haiku-20240307"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ai_structured_response_block_tracks_stats(self):
|
||||
"""Test that AIStructuredResponseGeneratorBlock correctly tracks stats."""
|
||||
@@ -303,139 +199,6 @@ class TestLLMStatsTracking:
|
||||
assert block.execution_stats.llm_call_count == 2 # retry_count + 1 = 1 + 1 = 2
|
||||
assert block.execution_stats.llm_retry_count == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_retry_cost_accumulates_across_attempts(self):
|
||||
"""provider_cost accumulates across all retry attempts.
|
||||
|
||||
Each LLM call incurs a real cost, including failed validation attempts.
|
||||
The total cost is the sum of all attempts so no billed USD is lost.
|
||||
"""
|
||||
import backend.blocks.llm as llm
|
||||
|
||||
block = llm.AIStructuredResponseGeneratorBlock()
|
||||
call_count = 0
|
||||
|
||||
async def mock_llm_call(*args, **kwargs):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
# First attempt: fails validation, returns cost $0.01
|
||||
return llm.LLMResponse(
|
||||
raw_response="",
|
||||
prompt=[],
|
||||
response='<json_output id="test123456">{"wrong": "key"}</json_output>',
|
||||
tool_calls=None,
|
||||
prompt_tokens=10,
|
||||
completion_tokens=5,
|
||||
reasoning=None,
|
||||
provider_cost=0.01,
|
||||
)
|
||||
# Second attempt: succeeds, returns cost $0.02
|
||||
return llm.LLMResponse(
|
||||
raw_response="",
|
||||
prompt=[],
|
||||
response='<json_output id="test123456">{"key1": "value1", "key2": "value2"}</json_output>',
|
||||
tool_calls=None,
|
||||
prompt_tokens=20,
|
||||
completion_tokens=10,
|
||||
reasoning=None,
|
||||
provider_cost=0.02,
|
||||
)
|
||||
|
||||
block.llm_call = mock_llm_call # type: ignore
|
||||
|
||||
input_data = llm.AIStructuredResponseGeneratorBlock.Input(
|
||||
prompt="Test prompt",
|
||||
expected_format={"key1": "desc1", "key2": "desc2"},
|
||||
model=llm.DEFAULT_LLM_MODEL,
|
||||
credentials=llm.TEST_CREDENTIALS_INPUT, # type: ignore
|
||||
retry=2,
|
||||
)
|
||||
|
||||
with patch("secrets.token_hex", return_value="test123456"):
|
||||
async for _ in block.run(input_data, credentials=llm.TEST_CREDENTIALS):
|
||||
pass
|
||||
|
||||
# provider_cost accumulates across all attempts: $0.01 + $0.02 = $0.03
|
||||
assert block.execution_stats.provider_cost == pytest.approx(0.03)
|
||||
# Tokens from both attempts accumulate
|
||||
assert block.execution_stats.input_token_count == 30
|
||||
assert block.execution_stats.output_token_count == 15
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cache_tokens_accumulated_in_stats(self):
|
||||
"""Cache read/creation tokens are tracked per-attempt and accumulated."""
|
||||
import backend.blocks.llm as llm
|
||||
|
||||
block = llm.AIStructuredResponseGeneratorBlock()
|
||||
|
||||
async def mock_llm_call(*args, **kwargs):
|
||||
return llm.LLMResponse(
|
||||
raw_response="",
|
||||
prompt=[],
|
||||
response='<json_output id="tok123456">{"key1": "v1", "key2": "v2"}</json_output>',
|
||||
tool_calls=None,
|
||||
prompt_tokens=10,
|
||||
completion_tokens=5,
|
||||
cache_read_tokens=20,
|
||||
cache_creation_tokens=8,
|
||||
reasoning=None,
|
||||
provider_cost=0.005,
|
||||
)
|
||||
|
||||
block.llm_call = mock_llm_call # type: ignore
|
||||
|
||||
input_data = llm.AIStructuredResponseGeneratorBlock.Input(
|
||||
prompt="Test prompt",
|
||||
expected_format={"key1": "desc1", "key2": "desc2"},
|
||||
model=llm.DEFAULT_LLM_MODEL,
|
||||
credentials=llm.TEST_CREDENTIALS_INPUT, # type: ignore
|
||||
retry=1,
|
||||
)
|
||||
|
||||
with patch("secrets.token_hex", return_value="tok123456"):
|
||||
async for _ in block.run(input_data, credentials=llm.TEST_CREDENTIALS):
|
||||
pass
|
||||
|
||||
assert block.execution_stats.cache_read_token_count == 20
|
||||
assert block.execution_stats.cache_creation_token_count == 8
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_failure_path_persists_accumulated_cost(self):
|
||||
"""When all retries are exhausted, accumulated provider_cost is preserved."""
|
||||
import backend.blocks.llm as llm
|
||||
|
||||
block = llm.AIStructuredResponseGeneratorBlock()
|
||||
|
||||
async def mock_llm_call(*args, **kwargs):
|
||||
return llm.LLMResponse(
|
||||
raw_response="",
|
||||
prompt=[],
|
||||
response="not valid json at all",
|
||||
tool_calls=None,
|
||||
prompt_tokens=10,
|
||||
completion_tokens=5,
|
||||
reasoning=None,
|
||||
provider_cost=0.01,
|
||||
)
|
||||
|
||||
block.llm_call = mock_llm_call # type: ignore
|
||||
|
||||
input_data = llm.AIStructuredResponseGeneratorBlock.Input(
|
||||
prompt="Test prompt",
|
||||
expected_format={"key1": "desc1"},
|
||||
model=llm.DEFAULT_LLM_MODEL,
|
||||
credentials=llm.TEST_CREDENTIALS_INPUT, # type: ignore
|
||||
retry=2,
|
||||
)
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
async for _ in block.run(input_data, credentials=llm.TEST_CREDENTIALS):
|
||||
pass
|
||||
|
||||
# Both retry attempts each cost $0.01, total $0.02
|
||||
assert block.execution_stats.provider_cost == pytest.approx(0.02)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ai_text_summarizer_multiple_chunks(self):
|
||||
"""Test that AITextSummarizerBlock correctly accumulates stats across multiple chunks."""
|
||||
@@ -1224,295 +987,3 @@ class TestLlmModelMissing:
|
||||
assert (
|
||||
llm.LlmModel("extra/google/gemini-2.5-pro") == llm.LlmModel.GEMINI_2_5_PRO
|
||||
)
|
||||
|
||||
|
||||
class TestExtractOpenRouterCost:
|
||||
"""Tests for extract_openrouter_cost — the x-total-cost header parser."""
|
||||
|
||||
def _mk_response(self, headers: dict | None):
|
||||
response = MagicMock()
|
||||
if headers is None:
|
||||
response._response = None
|
||||
else:
|
||||
raw = MagicMock()
|
||||
raw.headers = headers
|
||||
response._response = raw
|
||||
return response
|
||||
|
||||
def test_extracts_numeric_cost(self):
|
||||
response = self._mk_response({"x-total-cost": "0.0042"})
|
||||
assert llm.extract_openrouter_cost(response) == 0.0042
|
||||
|
||||
def test_returns_none_when_header_missing(self):
|
||||
response = self._mk_response({})
|
||||
assert llm.extract_openrouter_cost(response) is None
|
||||
|
||||
def test_returns_none_when_header_empty_string(self):
|
||||
response = self._mk_response({"x-total-cost": ""})
|
||||
assert llm.extract_openrouter_cost(response) is None
|
||||
|
||||
def test_returns_none_when_header_non_numeric(self):
|
||||
response = self._mk_response({"x-total-cost": "not-a-number"})
|
||||
assert llm.extract_openrouter_cost(response) is None
|
||||
|
||||
def test_returns_none_when_no_response_attr(self):
|
||||
response = MagicMock(spec=[]) # no _response attr
|
||||
assert llm.extract_openrouter_cost(response) is None
|
||||
|
||||
def test_returns_none_when_raw_is_none(self):
|
||||
response = self._mk_response(None)
|
||||
assert llm.extract_openrouter_cost(response) is None
|
||||
|
||||
def test_returns_none_when_raw_has_no_headers(self):
|
||||
response = MagicMock()
|
||||
response._response = MagicMock(spec=[]) # no headers attr
|
||||
assert llm.extract_openrouter_cost(response) is None
|
||||
|
||||
def test_returns_zero_for_zero_cost(self):
|
||||
"""Zero-cost is a valid value (free tier) and must not become None."""
|
||||
response = self._mk_response({"x-total-cost": "0"})
|
||||
assert llm.extract_openrouter_cost(response) == 0.0
|
||||
|
||||
def test_returns_none_for_inf(self):
|
||||
response = self._mk_response({"x-total-cost": "inf"})
|
||||
assert llm.extract_openrouter_cost(response) is None
|
||||
|
||||
def test_returns_none_for_negative_inf(self):
|
||||
response = self._mk_response({"x-total-cost": "-inf"})
|
||||
assert llm.extract_openrouter_cost(response) is None
|
||||
|
||||
def test_returns_none_for_nan(self):
|
||||
response = self._mk_response({"x-total-cost": "nan"})
|
||||
assert llm.extract_openrouter_cost(response) is None
|
||||
|
||||
def test_returns_none_for_negative_cost(self):
|
||||
response = self._mk_response({"x-total-cost": "-0.005"})
|
||||
assert llm.extract_openrouter_cost(response) is None
|
||||
|
||||
|
||||
class TestAnthropicCacheControl:
|
||||
"""Verify that llm_call attaches cache_control to the system prompt block
|
||||
and to the last tool definition when calling the Anthropic API."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def disable_openrouter_routing(self):
|
||||
"""Ensure tests exercise the direct-Anthropic path by suppressing the
|
||||
OpenRouter API key. Without this, a local .env with OPEN_ROUTER_API_KEY
|
||||
set would silently reroute all Anthropic calls through OpenRouter,
|
||||
bypassing the cache_control code under test."""
|
||||
with patch("backend.blocks.llm.settings") as mock_settings:
|
||||
mock_settings.secrets.open_router_api_key = ""
|
||||
yield mock_settings
|
||||
|
||||
def _make_anthropic_credentials(self) -> llm.APIKeyCredentials:
|
||||
from pydantic import SecretStr
|
||||
|
||||
return llm.APIKeyCredentials(
|
||||
id="test-anthropic-id",
|
||||
provider="anthropic",
|
||||
api_key=SecretStr("mock-anthropic-key"),
|
||||
title="Mock Anthropic key",
|
||||
expires_at=None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_system_prompt_sent_as_block_with_cache_control(self):
|
||||
"""The system prompt is wrapped in a structured block with cache_control ephemeral."""
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.content = [MagicMock(type="text", text="hello")]
|
||||
mock_resp.usage = MagicMock(input_tokens=5, output_tokens=3)
|
||||
|
||||
captured_kwargs: dict = {}
|
||||
|
||||
async def fake_create(**kwargs):
|
||||
captured_kwargs.update(kwargs)
|
||||
return mock_resp
|
||||
|
||||
mock_client = MagicMock()
|
||||
mock_client.messages.create = fake_create
|
||||
|
||||
credentials = self._make_anthropic_credentials()
|
||||
|
||||
with patch("anthropic.AsyncAnthropic", return_value=mock_client):
|
||||
await llm.llm_call(
|
||||
credentials=credentials,
|
||||
llm_model=llm.LlmModel.CLAUDE_4_6_SONNET,
|
||||
prompt=[
|
||||
{"role": "system", "content": "You are an assistant."},
|
||||
{"role": "user", "content": "Hello"},
|
||||
],
|
||||
max_tokens=100,
|
||||
)
|
||||
|
||||
system_arg = captured_kwargs.get("system")
|
||||
assert isinstance(system_arg, list), "system should be a list of blocks"
|
||||
assert len(system_arg) == 1
|
||||
block = system_arg[0]
|
||||
assert block["type"] == "text"
|
||||
assert block["text"] == "You are an assistant."
|
||||
assert block.get("cache_control") == {"type": "ephemeral"}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_last_tool_gets_cache_control(self):
|
||||
"""cache_control is placed on the last tool in the Anthropic tools list."""
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.content = [MagicMock(type="text", text="ok")]
|
||||
mock_resp.usage = MagicMock(input_tokens=10, output_tokens=5)
|
||||
|
||||
captured_kwargs: dict = {}
|
||||
|
||||
async def fake_create(**kwargs):
|
||||
captured_kwargs.update(kwargs)
|
||||
return mock_resp
|
||||
|
||||
mock_client = MagicMock()
|
||||
mock_client.messages.create = fake_create
|
||||
|
||||
credentials = self._make_anthropic_credentials()
|
||||
tools = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "tool_a",
|
||||
"description": "First tool",
|
||||
"parameters": {"type": "object", "properties": {}, "required": []},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "tool_b",
|
||||
"description": "Second tool",
|
||||
"parameters": {"type": "object", "properties": {}, "required": []},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
with patch("anthropic.AsyncAnthropic", return_value=mock_client):
|
||||
await llm.llm_call(
|
||||
credentials=credentials,
|
||||
llm_model=llm.LlmModel.CLAUDE_4_6_SONNET,
|
||||
prompt=[
|
||||
{"role": "system", "content": "System."},
|
||||
{"role": "user", "content": "Do something"},
|
||||
],
|
||||
max_tokens=100,
|
||||
tools=tools,
|
||||
)
|
||||
|
||||
an_tools = captured_kwargs.get("tools")
|
||||
assert isinstance(an_tools, list)
|
||||
assert len(an_tools) == 2
|
||||
assert (
|
||||
an_tools[0].get("cache_control") is None
|
||||
), "Only last tool gets cache_control"
|
||||
assert an_tools[-1].get("cache_control") == {"type": "ephemeral"}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_tools_no_cache_control_on_tools(self):
|
||||
"""When there are no tools, the Anthropic call receives anthropic.NOT_GIVEN for tools."""
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.content = [MagicMock(type="text", text="ok")]
|
||||
mock_resp.usage = MagicMock(input_tokens=5, output_tokens=2)
|
||||
|
||||
captured_kwargs: dict = {}
|
||||
|
||||
async def fake_create(**kwargs):
|
||||
captured_kwargs.update(kwargs)
|
||||
return mock_resp
|
||||
|
||||
mock_client = MagicMock()
|
||||
mock_client.messages.create = fake_create
|
||||
|
||||
credentials = self._make_anthropic_credentials()
|
||||
|
||||
with patch("anthropic.AsyncAnthropic", return_value=mock_client):
|
||||
await llm.llm_call(
|
||||
credentials=credentials,
|
||||
llm_model=llm.LlmModel.CLAUDE_4_6_SONNET,
|
||||
prompt=[
|
||||
{"role": "system", "content": "System."},
|
||||
{"role": "user", "content": "Hello"},
|
||||
],
|
||||
max_tokens=100,
|
||||
tools=None,
|
||||
)
|
||||
|
||||
import anthropic
|
||||
|
||||
tools_arg = captured_kwargs.get("tools")
|
||||
assert (
|
||||
tools_arg is anthropic.NOT_GIVEN
|
||||
), "Empty tools should pass anthropic.NOT_GIVEN sentinel"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_system_prompt_omits_system_key(self):
|
||||
"""When sysprompt is empty, the 'system' key must not be sent to Anthropic.
|
||||
|
||||
Anthropic rejects empty text blocks; the guard in llm_call must ensure
|
||||
the system argument is omitted entirely when no system messages are present.
|
||||
"""
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.content = [MagicMock(type="text", text="ok")]
|
||||
mock_resp.usage = MagicMock(input_tokens=3, output_tokens=2)
|
||||
|
||||
captured_kwargs: dict = {}
|
||||
|
||||
async def fake_create(**kwargs):
|
||||
captured_kwargs.update(kwargs)
|
||||
return mock_resp
|
||||
|
||||
mock_client = MagicMock()
|
||||
mock_client.messages.create = fake_create
|
||||
|
||||
credentials = self._make_anthropic_credentials()
|
||||
|
||||
with patch("anthropic.AsyncAnthropic", return_value=mock_client):
|
||||
await llm.llm_call(
|
||||
credentials=credentials,
|
||||
llm_model=llm.LlmModel.CLAUDE_4_6_SONNET,
|
||||
prompt=[{"role": "user", "content": "Hi"}],
|
||||
max_tokens=50,
|
||||
)
|
||||
|
||||
assert (
|
||||
"system" not in captured_kwargs
|
||||
), "system must be omitted when sysprompt is empty to avoid Anthropic 400"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_whitespace_only_system_prompt_omits_system_key(self):
|
||||
"""Whitespace-only system content is treated as empty and omitted.
|
||||
|
||||
The guard in llm_call uses sysprompt.strip() so a prompt consisting of
|
||||
only whitespace should NOT reach the Anthropic API (it would be rejected
|
||||
as an empty text block).
|
||||
"""
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.content = [MagicMock(type="text", text="ok")]
|
||||
mock_resp.usage = MagicMock(input_tokens=3, output_tokens=2)
|
||||
|
||||
captured_kwargs: dict = {}
|
||||
|
||||
async def fake_create(**kwargs):
|
||||
captured_kwargs.update(kwargs)
|
||||
return mock_resp
|
||||
|
||||
mock_client = MagicMock()
|
||||
mock_client.messages.create = fake_create
|
||||
|
||||
credentials = self._make_anthropic_credentials()
|
||||
|
||||
with patch("anthropic.AsyncAnthropic", return_value=mock_client):
|
||||
await llm.llm_call(
|
||||
credentials=credentials,
|
||||
llm_model=llm.LlmModel.CLAUDE_4_6_SONNET,
|
||||
prompt=[
|
||||
{"role": "system", "content": " \n\t "},
|
||||
{"role": "user", "content": "Hi"},
|
||||
],
|
||||
max_tokens=50,
|
||||
)
|
||||
|
||||
assert (
|
||||
"system" not in captured_kwargs
|
||||
), "whitespace-only sysprompt must be omitted to avoid Anthropic 400"
|
||||
|
||||
@@ -922,11 +922,6 @@ async def test_orchestrator_agent_mode():
|
||||
mock_execution_processor.on_node_execution = AsyncMock(
|
||||
return_value=mock_node_stats
|
||||
)
|
||||
# Mock charge_node_usage (called after successful tool execution).
|
||||
# Returns (cost, remaining_balance). Must be AsyncMock because it is
|
||||
# an async method and is directly awaited in _execute_single_tool_with_manager.
|
||||
# Use a non-zero cost so the merge_stats branch is exercised.
|
||||
mock_execution_processor.charge_node_usage = AsyncMock(return_value=(10, 990))
|
||||
|
||||
# Mock the get_execution_outputs_by_node_exec_id method
|
||||
mock_db_client.get_execution_outputs_by_node_exec_id.return_value = {
|
||||
@@ -972,11 +967,6 @@ async def test_orchestrator_agent_mode():
|
||||
# Verify tool was executed via execution processor
|
||||
assert mock_execution_processor.on_node_execution.call_count == 1
|
||||
|
||||
# Verify charge_node_usage was actually called for the successful
|
||||
# tool execution — this guards against regressions where the
|
||||
# post-execution tool charging is accidentally removed.
|
||||
assert mock_execution_processor.charge_node_usage.call_count == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrator_traditional_mode_default():
|
||||
|
||||
@@ -306,9 +306,6 @@ async def test_output_yielding_with_dynamic_fields():
|
||||
mock_response.raw_response = {"role": "assistant", "content": "test"}
|
||||
mock_response.prompt_tokens = 100
|
||||
mock_response.completion_tokens = 50
|
||||
mock_response.cache_read_tokens = 0
|
||||
mock_response.cache_creation_tokens = 0
|
||||
mock_response.provider_cost = None
|
||||
|
||||
# Mock the LLM call
|
||||
with patch(
|
||||
@@ -641,14 +638,6 @@ async def test_validation_errors_dont_pollute_conversation():
|
||||
mock_execution_processor.on_node_execution.return_value = (
|
||||
mock_node_stats
|
||||
)
|
||||
# Mock charge_node_usage (called after successful tool execution).
|
||||
# Must be AsyncMock because it is async and is awaited in
|
||||
# _execute_single_tool_with_manager — a plain MagicMock would
|
||||
# return a non-awaitable tuple and TypeError out, then be
|
||||
# silently swallowed by the orchestrator's catch-all.
|
||||
mock_execution_processor.charge_node_usage = AsyncMock(
|
||||
return_value=(0, 0)
|
||||
)
|
||||
|
||||
async for output_name, output_value in block.run(
|
||||
input_data,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -211,30 +211,6 @@ class TestConvertRawResponseToDict:
|
||||
# A single dict is wrong — there are two distinct items
|
||||
pytest.fail("Expected a list of output items, got a single dict")
|
||||
|
||||
def test_responses_api_strips_status_from_function_call(self):
|
||||
"""Responses API function_call items have a 'status' field that OpenAI
|
||||
rejects when sent back as input ('Unknown parameter: input[N].status').
|
||||
It must be stripped before the item is stored in conversation history."""
|
||||
resp = _MockResponse(
|
||||
output=[_MockFunctionCall("my_tool", '{"x": 1}', call_id="call_xyz")]
|
||||
)
|
||||
result = _convert_raw_response_to_dict(resp)
|
||||
assert isinstance(result, list)
|
||||
for item in result:
|
||||
assert (
|
||||
"status" not in item
|
||||
), f"'status' must be stripped from Responses API items: {item}"
|
||||
|
||||
def test_responses_api_strips_status_from_message(self):
|
||||
"""Responses API message items also carry 'status'; it must be stripped."""
|
||||
resp = _MockResponse(output=[_MockOutputMessage("Hello")])
|
||||
result = _convert_raw_response_to_dict(resp)
|
||||
assert isinstance(result, list)
|
||||
for item in result:
|
||||
assert (
|
||||
"status" not in item
|
||||
), f"'status' must be stripped from Responses API items: {item}"
|
||||
|
||||
|
||||
# ───────────────────────────────────────────────────────────────────────────
|
||||
# _get_tool_requests (lines 61-86)
|
||||
@@ -956,12 +932,6 @@ async def test_agent_mode_conversation_valid_for_responses_api():
|
||||
ep.execution_stats_lock = threading.Lock()
|
||||
ns = MagicMock(error=None)
|
||||
ep.on_node_execution = AsyncMock(return_value=ns)
|
||||
# Mock charge_node_usage (called after successful tool execution).
|
||||
# Must be AsyncMock because it is async and is awaited in
|
||||
# _execute_single_tool_with_manager — a plain MagicMock would return a
|
||||
# non-awaitable tuple and TypeError out, then be silently swallowed by
|
||||
# the orchestrator's catch-all.
|
||||
ep.charge_node_usage = AsyncMock(return_value=(0, 0))
|
||||
|
||||
with patch("backend.blocks.llm.llm_call", llm_mock), patch.object(
|
||||
block, "_create_tool_node_signatures", return_value=tool_sigs
|
||||
|
||||
@@ -13,7 +13,6 @@ from backend.data.model import (
|
||||
APIKeyCredentials,
|
||||
CredentialsField,
|
||||
CredentialsMetaInput,
|
||||
NodeExecutionStats,
|
||||
SchemaField,
|
||||
)
|
||||
from backend.integrations.providers import ProviderName
|
||||
@@ -105,10 +104,4 @@ class UnrealTextToSpeechBlock(Block):
|
||||
input_data.text,
|
||||
input_data.voice_id,
|
||||
)
|
||||
self.merge_stats(
|
||||
NodeExecutionStats(
|
||||
provider_cost=float(len(input_data.text)),
|
||||
provider_cost_type="characters",
|
||||
)
|
||||
)
|
||||
yield "mp3_url", api_response["OutputUri"]
|
||||
|
||||
@@ -1,230 +0,0 @@
|
||||
"""Extended-thinking wire support for the baseline (OpenRouter) path.
|
||||
|
||||
Anthropic routes on OpenRouter expose extended thinking through
|
||||
non-OpenAI extension fields that the OpenAI Python SDK doesn't model:
|
||||
|
||||
* ``reasoning`` (legacy string) — enabled by ``include_reasoning: true``.
|
||||
* ``reasoning_content`` — DeepSeek / some OpenRouter routes.
|
||||
* ``reasoning_details`` — structured list shipped with the unified
|
||||
``reasoning`` request param.
|
||||
|
||||
This module keeps the wire-level concerns in one place:
|
||||
|
||||
* :class:`OpenRouterDeltaExtension` validates the extension dict pulled off
|
||||
``ChoiceDelta.model_extra`` into typed pydantic models — no ``getattr`` +
|
||||
``isinstance`` duck-typing at the call site.
|
||||
* :class:`BaselineReasoningEmitter` owns the reasoning block lifecycle for
|
||||
one streaming round and emits ``StreamReasoning*`` events so the caller
|
||||
only has to plumb the events into its pending queue.
|
||||
* :func:`reasoning_extra_body` builds the ``extra_body`` fragment for the
|
||||
OpenAI client call. Returns ``None`` on non-Anthropic routes.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from openai.types.chat.chat_completion_chunk import ChoiceDelta
|
||||
from pydantic import BaseModel, ConfigDict, Field, ValidationError
|
||||
|
||||
from backend.copilot.model import ChatMessage
|
||||
from backend.copilot.response_model import (
|
||||
StreamBaseResponse,
|
||||
StreamReasoningDelta,
|
||||
StreamReasoningEnd,
|
||||
StreamReasoningStart,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
_VISIBLE_REASONING_TYPES = frozenset({"reasoning.text", "reasoning.summary"})
|
||||
|
||||
|
||||
class ReasoningDetail(BaseModel):
|
||||
"""One entry in OpenRouter's ``reasoning_details`` list.
|
||||
|
||||
OpenRouter ships ``type: "reasoning.text"`` / ``"reasoning.summary"`` /
|
||||
``"reasoning.encrypted"`` entries. Only the first two carry
|
||||
user-visible text; encrypted entries are opaque and omitted from the
|
||||
rendered collapse. Unknown future types are tolerated (``extra="ignore"``)
|
||||
so an upstream addition doesn't crash the stream — but their ``text`` /
|
||||
``summary`` fields are NOT surfaced because they may carry provider
|
||||
metadata rather than user-visible reasoning (see
|
||||
:attr:`visible_text`).
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
type: str | None = None
|
||||
text: str | None = None
|
||||
summary: str | None = None
|
||||
|
||||
@property
|
||||
def visible_text(self) -> str:
|
||||
"""Return the human-readable text for this entry, or ``""``.
|
||||
|
||||
Only entries with a recognised reasoning type (``reasoning.text`` /
|
||||
``reasoning.summary``) surface text; unknown or encrypted types
|
||||
return an empty string even if they carry a ``text`` /
|
||||
``summary`` field, to guard against future provider metadata
|
||||
being rendered as reasoning in the UI. Entries missing a
|
||||
``type`` are treated as text (pre-``reasoning_details`` OpenRouter
|
||||
payloads omit the field).
|
||||
"""
|
||||
if self.type is not None and self.type not in _VISIBLE_REASONING_TYPES:
|
||||
return ""
|
||||
return self.text or self.summary or ""
|
||||
|
||||
|
||||
class OpenRouterDeltaExtension(BaseModel):
|
||||
"""Non-OpenAI fields OpenRouter adds to streaming deltas.
|
||||
|
||||
Instantiate via :meth:`from_delta` which pulls the extension dict off
|
||||
``ChoiceDelta.model_extra`` (where pydantic v2 stashes fields that
|
||||
aren't part of the declared schema) and validates it through this
|
||||
model. That keeps the parser honest — malformed entries surface as
|
||||
validation errors rather than silent ``None``-coalesce bugs — and
|
||||
avoids the ``getattr`` + ``isinstance`` duck-typing the earlier inline
|
||||
extractor relied on.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
reasoning: str | None = None
|
||||
reasoning_content: str | None = None
|
||||
reasoning_details: list[ReasoningDetail] = Field(default_factory=list)
|
||||
|
||||
@classmethod
|
||||
def from_delta(cls, delta: ChoiceDelta) -> "OpenRouterDeltaExtension":
|
||||
"""Build an extension view from ``delta.model_extra``.
|
||||
|
||||
Malformed provider payloads (e.g. ``reasoning_details`` shipped as
|
||||
a string rather than a list) surface as a ``ValidationError`` which
|
||||
is logged and swallowed — returning an empty extension so the rest
|
||||
of the stream (valid text / tool calls) keeps flowing. An optional
|
||||
feature's corrupted wire data must never abort the whole stream.
|
||||
"""
|
||||
try:
|
||||
return cls.model_validate(delta.model_extra or {})
|
||||
except ValidationError as exc:
|
||||
logger.warning(
|
||||
"[Baseline] Dropping malformed OpenRouter reasoning payload: %s",
|
||||
exc,
|
||||
)
|
||||
return cls()
|
||||
|
||||
def visible_text(self) -> str:
|
||||
"""Concatenated reasoning text, pulled from whichever channel is set.
|
||||
|
||||
Priority: the legacy ``reasoning`` string, then DeepSeek's
|
||||
``reasoning_content``, then the concatenation of text-bearing
|
||||
entries in ``reasoning_details``. Only one channel is set per
|
||||
provider in practice; the priority order just makes the fallback
|
||||
deterministic if a provider ever emits multiple.
|
||||
"""
|
||||
if self.reasoning:
|
||||
return self.reasoning
|
||||
if self.reasoning_content:
|
||||
return self.reasoning_content
|
||||
return "".join(d.visible_text for d in self.reasoning_details)
|
||||
|
||||
|
||||
def reasoning_extra_body(model: str, max_thinking_tokens: int) -> dict[str, Any] | None:
|
||||
"""Build the ``extra_body["reasoning"]`` fragment for the OpenAI client.
|
||||
|
||||
Returns ``None`` for non-Anthropic routes (other OpenRouter providers
|
||||
ignore the field but we skip it anyway to keep the payload minimal)
|
||||
and for ``max_thinking_tokens <= 0`` (operator kill switch).
|
||||
"""
|
||||
# Imported lazily to avoid pulling service.py at module load — service.py
|
||||
# imports this module, and the lazy import keeps the dependency one-way.
|
||||
from backend.copilot.baseline.service import _is_anthropic_model
|
||||
|
||||
if not _is_anthropic_model(model) or max_thinking_tokens <= 0:
|
||||
return None
|
||||
return {"reasoning": {"max_tokens": max_thinking_tokens}}
|
||||
|
||||
|
||||
class BaselineReasoningEmitter:
|
||||
"""Owns the reasoning block lifecycle for one streaming round.
|
||||
|
||||
Two concerns live here, both driven by the same state machine:
|
||||
|
||||
1. **Wire events.** The AI SDK v6 wire format pairs every
|
||||
``reasoning-start`` with a matching ``reasoning-end`` and treats
|
||||
reasoning / text / tool-use as distinct UI parts that must not
|
||||
interleave.
|
||||
2. **Session persistence.** ``ChatMessage(role="reasoning")`` rows in
|
||||
``session.messages`` are what
|
||||
``convertChatSessionToUiMessages.ts`` folds into the assistant
|
||||
bubble as ``{type: "reasoning"}`` UI parts on reload and on
|
||||
``useHydrateOnStreamEnd`` swaps. Without them the live-streamed
|
||||
reasoning parts get overwritten by the hydrated (reasoning-less)
|
||||
message list the moment the stream ends. Mirrors the SDK path's
|
||||
``acc.reasoning_response`` pattern so both routes render the same
|
||||
way on reload.
|
||||
|
||||
Pass ``session_messages`` to enable persistence; omit for pure
|
||||
wire-emission (tests, scratch callers). On first reasoning delta a
|
||||
fresh ``ChatMessage(role="reasoning")`` is appended and mutated
|
||||
in-place as further deltas arrive; :meth:`close` drops the reference
|
||||
but leaves the appended row intact.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
session_messages: list[ChatMessage] | None = None,
|
||||
) -> None:
|
||||
self._block_id: str = str(uuid.uuid4())
|
||||
self._open: bool = False
|
||||
self._session_messages = session_messages
|
||||
self._current_row: ChatMessage | None = None
|
||||
|
||||
@property
|
||||
def is_open(self) -> bool:
|
||||
return self._open
|
||||
|
||||
def on_delta(self, delta: ChoiceDelta) -> list[StreamBaseResponse]:
|
||||
"""Return events for the reasoning text carried by *delta*.
|
||||
|
||||
Empty list when the chunk carries no reasoning payload, so this is
|
||||
safe to call on every chunk without guarding at the call site.
|
||||
Persistence (when a session message list is attached) happens in
|
||||
lockstep with emission so the row's content stays equal to the
|
||||
concatenated deltas at every delta boundary.
|
||||
"""
|
||||
ext = OpenRouterDeltaExtension.from_delta(delta)
|
||||
text = ext.visible_text()
|
||||
if not text:
|
||||
return []
|
||||
events: list[StreamBaseResponse] = []
|
||||
if not self._open:
|
||||
events.append(StreamReasoningStart(id=self._block_id))
|
||||
self._open = True
|
||||
if self._session_messages is not None:
|
||||
self._current_row = ChatMessage(role="reasoning", content="")
|
||||
self._session_messages.append(self._current_row)
|
||||
events.append(StreamReasoningDelta(id=self._block_id, delta=text))
|
||||
if self._current_row is not None:
|
||||
self._current_row.content = (self._current_row.content or "") + text
|
||||
return events
|
||||
|
||||
def close(self) -> list[StreamBaseResponse]:
|
||||
"""Emit ``StreamReasoningEnd`` for the open block (if any) and rotate.
|
||||
|
||||
Idempotent — returns ``[]`` when no block is open. The id rotation
|
||||
guarantees the next reasoning block starts with a fresh id rather
|
||||
than reusing one already closed on the wire. The persisted row is
|
||||
not removed — it stays in ``session_messages`` as the durable
|
||||
record of what was reasoned.
|
||||
"""
|
||||
if not self._open:
|
||||
return []
|
||||
event = StreamReasoningEnd(id=self._block_id)
|
||||
self._open = False
|
||||
self._block_id = str(uuid.uuid4())
|
||||
self._current_row = None
|
||||
return [event]
|
||||
@@ -1,281 +0,0 @@
|
||||
"""Tests for the baseline reasoning extension module.
|
||||
|
||||
Covers the typed OpenRouter delta parser, the stateful emitter, and the
|
||||
``extra_body`` builder. The emitter is tested against real
|
||||
``ChoiceDelta`` pydantic instances so the ``model_extra`` plumbing the
|
||||
parser relies on is exercised end-to-end.
|
||||
"""
|
||||
|
||||
from openai.types.chat.chat_completion_chunk import ChoiceDelta
|
||||
|
||||
from backend.copilot.baseline.reasoning import (
|
||||
BaselineReasoningEmitter,
|
||||
OpenRouterDeltaExtension,
|
||||
ReasoningDetail,
|
||||
reasoning_extra_body,
|
||||
)
|
||||
from backend.copilot.model import ChatMessage
|
||||
from backend.copilot.response_model import (
|
||||
StreamReasoningDelta,
|
||||
StreamReasoningEnd,
|
||||
StreamReasoningStart,
|
||||
)
|
||||
|
||||
|
||||
def _delta(**extra) -> ChoiceDelta:
|
||||
"""Build a ChoiceDelta with the given extension fields on ``model_extra``."""
|
||||
return ChoiceDelta.model_validate({"role": "assistant", **extra})
|
||||
|
||||
|
||||
class TestReasoningDetail:
|
||||
def test_visible_text_prefers_text(self):
|
||||
d = ReasoningDetail(type="reasoning.text", text="hi", summary="ignored")
|
||||
assert d.visible_text == "hi"
|
||||
|
||||
def test_visible_text_falls_back_to_summary(self):
|
||||
d = ReasoningDetail(type="reasoning.summary", summary="tldr")
|
||||
assert d.visible_text == "tldr"
|
||||
|
||||
def test_visible_text_empty_for_encrypted(self):
|
||||
d = ReasoningDetail(type="reasoning.encrypted")
|
||||
assert d.visible_text == ""
|
||||
|
||||
def test_unknown_fields_are_ignored(self):
|
||||
# OpenRouter may add new fields in future payloads — they shouldn't
|
||||
# cause validation errors.
|
||||
d = ReasoningDetail.model_validate(
|
||||
{"type": "reasoning.future", "text": "x", "signature": "opaque"}
|
||||
)
|
||||
assert d.text == "x"
|
||||
|
||||
def test_visible_text_empty_for_unknown_type(self):
|
||||
# Unknown types may carry provider metadata that must not render as
|
||||
# user-visible reasoning — regardless of whether a text/summary is
|
||||
# present. Only ``reasoning.text`` / ``reasoning.summary`` surface.
|
||||
d = ReasoningDetail(type="reasoning.future", text="leaked metadata")
|
||||
assert d.visible_text == ""
|
||||
|
||||
def test_visible_text_surfaces_text_when_type_missing(self):
|
||||
# Pre-``reasoning_details`` OpenRouter payloads omit ``type`` — treat
|
||||
# them as text so we don't regress the legacy structured shape.
|
||||
d = ReasoningDetail(text="plain")
|
||||
assert d.visible_text == "plain"
|
||||
|
||||
|
||||
class TestOpenRouterDeltaExtension:
|
||||
def test_from_delta_reads_model_extra(self):
|
||||
delta = _delta(reasoning="step one")
|
||||
ext = OpenRouterDeltaExtension.from_delta(delta)
|
||||
assert ext.reasoning == "step one"
|
||||
|
||||
def test_visible_text_legacy_string(self):
|
||||
ext = OpenRouterDeltaExtension(reasoning="plain text")
|
||||
assert ext.visible_text() == "plain text"
|
||||
|
||||
def test_visible_text_deepseek_alias(self):
|
||||
ext = OpenRouterDeltaExtension(reasoning_content="alt channel")
|
||||
assert ext.visible_text() == "alt channel"
|
||||
|
||||
def test_visible_text_structured_details_concat(self):
|
||||
ext = OpenRouterDeltaExtension(
|
||||
reasoning_details=[
|
||||
ReasoningDetail(type="reasoning.text", text="hello "),
|
||||
ReasoningDetail(type="reasoning.text", text="world"),
|
||||
]
|
||||
)
|
||||
assert ext.visible_text() == "hello world"
|
||||
|
||||
def test_visible_text_skips_encrypted(self):
|
||||
ext = OpenRouterDeltaExtension(
|
||||
reasoning_details=[
|
||||
ReasoningDetail(type="reasoning.encrypted"),
|
||||
ReasoningDetail(type="reasoning.text", text="visible"),
|
||||
]
|
||||
)
|
||||
assert ext.visible_text() == "visible"
|
||||
|
||||
def test_visible_text_empty_when_all_channels_blank(self):
|
||||
ext = OpenRouterDeltaExtension()
|
||||
assert ext.visible_text() == ""
|
||||
|
||||
def test_empty_delta_produces_empty_extension(self):
|
||||
ext = OpenRouterDeltaExtension.from_delta(_delta())
|
||||
assert ext.reasoning is None
|
||||
assert ext.reasoning_content is None
|
||||
assert ext.reasoning_details == []
|
||||
|
||||
def test_malformed_reasoning_payload_logged_and_swallowed(self, caplog):
|
||||
# A malformed payload (e.g. reasoning_details shipped as a string
|
||||
# rather than a list) must not abort the stream — log it and
|
||||
# return an empty extension so valid text/tool events keep flowing.
|
||||
# A plain mock is used here because ``from_delta`` only reads
|
||||
# ``delta.model_extra`` — avoids reaching into pydantic internals
|
||||
# (``__pydantic_extra__``) that could be renamed across versions.
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
delta = MagicMock(spec=ChoiceDelta)
|
||||
delta.model_extra = {"reasoning_details": "not a list"}
|
||||
with caplog.at_level("WARNING"):
|
||||
ext = OpenRouterDeltaExtension.from_delta(delta)
|
||||
assert ext.reasoning_details == []
|
||||
assert ext.visible_text() == ""
|
||||
assert any("malformed" in r.message.lower() for r in caplog.records)
|
||||
|
||||
def test_unknown_typed_entry_with_text_is_not_surfaced(self):
|
||||
# Regression: the legacy extractor emitted any entry with a
|
||||
# ``text`` or ``summary`` field. The typed parser now filters on
|
||||
# the recognised types so future provider metadata can't leak
|
||||
# into the reasoning collapse.
|
||||
ext = OpenRouterDeltaExtension(
|
||||
reasoning_details=[
|
||||
ReasoningDetail(type="reasoning.future", text="provider metadata"),
|
||||
ReasoningDetail(type="reasoning.text", text="real"),
|
||||
]
|
||||
)
|
||||
assert ext.visible_text() == "real"
|
||||
|
||||
|
||||
class TestReasoningExtraBody:
|
||||
def test_anthropic_route_returns_fragment(self):
|
||||
assert reasoning_extra_body("anthropic/claude-sonnet-4-6", 4096) == {
|
||||
"reasoning": {"max_tokens": 4096}
|
||||
}
|
||||
|
||||
def test_direct_claude_model_id_still_matches(self):
|
||||
assert reasoning_extra_body("claude-3-5-sonnet-20241022", 2048) == {
|
||||
"reasoning": {"max_tokens": 2048}
|
||||
}
|
||||
|
||||
def test_non_anthropic_route_returns_none(self):
|
||||
assert reasoning_extra_body("openai/gpt-4o", 4096) is None
|
||||
assert reasoning_extra_body("google/gemini-2.5-pro", 4096) is None
|
||||
|
||||
def test_zero_max_tokens_kill_switch(self):
|
||||
# Operator kill switch: ``max_thinking_tokens <= 0`` disables the
|
||||
# ``reasoning`` extra_body fragment even on an Anthropic route.
|
||||
# Lets us silence reasoning without dropping the SDK path's budget.
|
||||
assert reasoning_extra_body("anthropic/claude-sonnet-4-6", 0) is None
|
||||
assert reasoning_extra_body("anthropic/claude-sonnet-4-6", -1) is None
|
||||
|
||||
|
||||
class TestBaselineReasoningEmitter:
|
||||
def test_first_text_delta_emits_start_then_delta(self):
|
||||
emitter = BaselineReasoningEmitter()
|
||||
events = emitter.on_delta(_delta(reasoning="thinking"))
|
||||
|
||||
assert len(events) == 2
|
||||
assert isinstance(events[0], StreamReasoningStart)
|
||||
assert isinstance(events[1], StreamReasoningDelta)
|
||||
assert events[0].id == events[1].id
|
||||
assert events[1].delta == "thinking"
|
||||
assert emitter.is_open is True
|
||||
|
||||
def test_subsequent_deltas_reuse_block_id_without_new_start(self):
|
||||
emitter = BaselineReasoningEmitter()
|
||||
first = emitter.on_delta(_delta(reasoning="a"))
|
||||
second = emitter.on_delta(_delta(reasoning="b"))
|
||||
|
||||
assert any(isinstance(e, StreamReasoningStart) for e in first)
|
||||
assert all(not isinstance(e, StreamReasoningStart) for e in second)
|
||||
assert len(second) == 1
|
||||
assert isinstance(second[0], StreamReasoningDelta)
|
||||
assert first[0].id == second[0].id
|
||||
|
||||
def test_empty_delta_emits_nothing(self):
|
||||
emitter = BaselineReasoningEmitter()
|
||||
assert emitter.on_delta(_delta(content="hello")) == []
|
||||
assert emitter.is_open is False
|
||||
|
||||
def test_close_emits_end_and_rotates_id(self):
|
||||
emitter = BaselineReasoningEmitter()
|
||||
# Capture the block id from the wire event rather than reaching
|
||||
# into emitter internals — the id on the emitted Start/Delta is
|
||||
# what the frontend actually receives.
|
||||
start_events = emitter.on_delta(_delta(reasoning="x"))
|
||||
first_id = start_events[0].id
|
||||
|
||||
events = emitter.close()
|
||||
assert len(events) == 1
|
||||
assert isinstance(events[0], StreamReasoningEnd)
|
||||
assert events[0].id == first_id
|
||||
assert emitter.is_open is False
|
||||
# Next reasoning uses a fresh id.
|
||||
new_events = emitter.on_delta(_delta(reasoning="y"))
|
||||
assert isinstance(new_events[0], StreamReasoningStart)
|
||||
assert new_events[0].id != first_id
|
||||
|
||||
def test_close_is_idempotent(self):
|
||||
emitter = BaselineReasoningEmitter()
|
||||
assert emitter.close() == []
|
||||
emitter.on_delta(_delta(reasoning="x"))
|
||||
assert len(emitter.close()) == 1
|
||||
assert emitter.close() == []
|
||||
|
||||
def test_structured_details_round_trip(self):
|
||||
emitter = BaselineReasoningEmitter()
|
||||
events = emitter.on_delta(
|
||||
_delta(
|
||||
reasoning_details=[
|
||||
{"type": "reasoning.text", "text": "plan: "},
|
||||
{"type": "reasoning.summary", "summary": "do the thing"},
|
||||
]
|
||||
)
|
||||
)
|
||||
deltas = [e for e in events if isinstance(e, StreamReasoningDelta)]
|
||||
assert len(deltas) == 1
|
||||
assert deltas[0].delta == "plan: do the thing"
|
||||
|
||||
|
||||
class TestReasoningPersistence:
|
||||
"""The persistence contract: without ``role="reasoning"`` rows in
|
||||
session.messages, useHydrateOnStreamEnd overwrites the live-streamed
|
||||
reasoning parts and the Reasoning collapse vanishes. Every delta
|
||||
must be reflected in the persisted row the moment it's emitted."""
|
||||
|
||||
def test_session_row_appended_on_first_delta(self):
|
||||
session: list[ChatMessage] = []
|
||||
emitter = BaselineReasoningEmitter(session)
|
||||
|
||||
assert session == []
|
||||
emitter.on_delta(_delta(reasoning="hi"))
|
||||
assert len(session) == 1
|
||||
assert session[0].role == "reasoning"
|
||||
assert session[0].content == "hi"
|
||||
|
||||
def test_subsequent_deltas_mutate_same_row(self):
|
||||
session: list[ChatMessage] = []
|
||||
emitter = BaselineReasoningEmitter(session)
|
||||
|
||||
emitter.on_delta(_delta(reasoning="part one "))
|
||||
emitter.on_delta(_delta(reasoning="part two"))
|
||||
|
||||
assert len(session) == 1
|
||||
assert session[0].content == "part one part two"
|
||||
|
||||
def test_close_keeps_row_in_session(self):
|
||||
session: list[ChatMessage] = []
|
||||
emitter = BaselineReasoningEmitter(session)
|
||||
|
||||
emitter.on_delta(_delta(reasoning="thought"))
|
||||
emitter.close()
|
||||
|
||||
assert len(session) == 1
|
||||
assert session[0].content == "thought"
|
||||
|
||||
def test_second_reasoning_block_appends_new_row(self):
|
||||
session: list[ChatMessage] = []
|
||||
emitter = BaselineReasoningEmitter(session)
|
||||
|
||||
emitter.on_delta(_delta(reasoning="first"))
|
||||
emitter.close()
|
||||
emitter.on_delta(_delta(reasoning="second"))
|
||||
|
||||
assert len(session) == 2
|
||||
assert [m.content for m in session] == ["first", "second"]
|
||||
|
||||
def test_no_session_means_no_persistence(self):
|
||||
"""Emitter without attached session list emits wire events only."""
|
||||
emitter = BaselineReasoningEmitter()
|
||||
events = emitter.on_delta(_delta(reasoning="pure wire"))
|
||||
assert len(events) == 2 # start + delta, no crash
|
||||
# Nothing else to assert — just proves None session is supported.
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
||||
"""Integration tests for baseline transcript flow.
|
||||
|
||||
Exercises the real helpers in ``baseline/service.py`` that restore,
|
||||
validate, load, append to, backfill, and upload the CLI session.
|
||||
Exercises the real helpers in ``baseline/service.py`` that download,
|
||||
validate, load, append to, backfill, and upload the transcript.
|
||||
Storage is mocked via ``download_transcript`` / ``upload_transcript``
|
||||
patches; no network access is required.
|
||||
"""
|
||||
@@ -12,14 +12,13 @@ from unittest.mock import AsyncMock, patch
|
||||
import pytest
|
||||
|
||||
from backend.copilot.baseline.service import (
|
||||
_append_gap_to_builder,
|
||||
_load_prior_transcript,
|
||||
_record_turn_to_transcript,
|
||||
_resolve_baseline_model,
|
||||
_upload_final_transcript,
|
||||
is_transcript_stale,
|
||||
should_upload_transcript,
|
||||
)
|
||||
from backend.copilot.model import ChatMessage
|
||||
from backend.copilot.service import config
|
||||
from backend.copilot.transcript import (
|
||||
STOP_REASON_END_TURN,
|
||||
@@ -55,128 +54,106 @@ def _make_transcript_content(*roles: str) -> str:
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
def _make_session_messages(*roles: str) -> list[ChatMessage]:
|
||||
"""Build a list of ChatMessage objects matching the given roles."""
|
||||
return [
|
||||
ChatMessage(role=r, content=f"{r} message {i}") for i, r in enumerate(roles)
|
||||
]
|
||||
|
||||
|
||||
class TestResolveBaselineModel:
|
||||
"""Baseline model resolution honours the per-request tier toggle."""
|
||||
"""Model selection honours the per-request mode."""
|
||||
|
||||
def test_advanced_tier_selects_advanced_model(self):
|
||||
assert _resolve_baseline_model("advanced") == config.advanced_model
|
||||
def test_fast_mode_selects_fast_model(self):
|
||||
assert _resolve_baseline_model("fast") == config.fast_model
|
||||
|
||||
def test_standard_tier_selects_default_model(self):
|
||||
assert _resolve_baseline_model("standard") == config.model
|
||||
def test_extended_thinking_selects_default_model(self):
|
||||
assert _resolve_baseline_model("extended_thinking") == config.model
|
||||
|
||||
def test_none_tier_selects_default_model(self):
|
||||
"""Baseline users without a tier MUST keep the default (standard)."""
|
||||
def test_none_mode_selects_default_model(self):
|
||||
"""Critical: baseline users without a mode MUST keep the default (opus)."""
|
||||
assert _resolve_baseline_model(None) == config.model
|
||||
|
||||
def test_standard_and_advanced_models_differ(self):
|
||||
"""Advanced tier defaults to a different (Opus) model than standard."""
|
||||
assert config.model != config.advanced_model
|
||||
def test_default_and_fast_models_differ(self):
|
||||
"""Sanity: the two tiers are actually distinct in production config."""
|
||||
assert config.model != config.fast_model
|
||||
|
||||
|
||||
class TestLoadPriorTranscript:
|
||||
"""``_load_prior_transcript`` wraps the CLI session restore + validate + load flow."""
|
||||
"""``_load_prior_transcript`` wraps the download + validate + load flow."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_loads_fresh_transcript(self):
|
||||
builder = TranscriptBuilder()
|
||||
content = _make_transcript_content("user", "assistant")
|
||||
restore = TranscriptDownload(
|
||||
content=content.encode("utf-8"), message_count=2, mode="sdk"
|
||||
)
|
||||
download = TranscriptDownload(content=content, message_count=2)
|
||||
|
||||
with patch(
|
||||
"backend.copilot.baseline.service.download_transcript",
|
||||
new=AsyncMock(return_value=restore),
|
||||
new=AsyncMock(return_value=download),
|
||||
):
|
||||
covers, dl = await _load_prior_transcript(
|
||||
covers = await _load_prior_transcript(
|
||||
user_id="user-1",
|
||||
session_id="session-1",
|
||||
session_messages=_make_session_messages("user", "assistant", "user"),
|
||||
session_msg_count=3,
|
||||
transcript_builder=builder,
|
||||
)
|
||||
|
||||
assert covers is True
|
||||
assert dl is not None
|
||||
assert dl.message_count == 2
|
||||
assert builder.entry_count == 2
|
||||
assert builder.last_entry_type == "assistant"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fills_gap_when_transcript_is_behind(self):
|
||||
"""When transcript covers fewer messages than session, gap is filled from DB."""
|
||||
async def test_rejects_stale_transcript(self):
|
||||
"""msg_count strictly less than session-1 is treated as stale."""
|
||||
builder = TranscriptBuilder()
|
||||
content = _make_transcript_content("user", "assistant")
|
||||
# transcript covers 2 messages, session has 4 (plus current user turn = 5)
|
||||
restore = TranscriptDownload(
|
||||
content=content.encode("utf-8"), message_count=2, mode="baseline"
|
||||
)
|
||||
# session has 6 messages, transcript only covers 2 → stale.
|
||||
download = TranscriptDownload(content=content, message_count=2)
|
||||
|
||||
with patch(
|
||||
"backend.copilot.baseline.service.download_transcript",
|
||||
new=AsyncMock(return_value=restore),
|
||||
new=AsyncMock(return_value=download),
|
||||
):
|
||||
covers, dl = await _load_prior_transcript(
|
||||
covers = await _load_prior_transcript(
|
||||
user_id="user-1",
|
||||
session_id="session-1",
|
||||
session_messages=_make_session_messages(
|
||||
"user", "assistant", "user", "assistant", "user"
|
||||
),
|
||||
session_msg_count=6,
|
||||
transcript_builder=builder,
|
||||
)
|
||||
|
||||
assert covers is True
|
||||
assert dl is not None
|
||||
# 2 from transcript + 2 gap messages (user+assistant at positions 2,3)
|
||||
assert builder.entry_count == 4
|
||||
assert covers is False
|
||||
assert builder.is_empty
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_transcript_allows_upload(self):
|
||||
"""Nothing in GCS → upload is safe; the turn writes the first snapshot."""
|
||||
async def test_missing_transcript_returns_false(self):
|
||||
builder = TranscriptBuilder()
|
||||
with patch(
|
||||
"backend.copilot.baseline.service.download_transcript",
|
||||
new=AsyncMock(return_value=None),
|
||||
):
|
||||
upload_safe, dl = await _load_prior_transcript(
|
||||
covers = await _load_prior_transcript(
|
||||
user_id="user-1",
|
||||
session_id="session-1",
|
||||
session_messages=_make_session_messages("user", "assistant"),
|
||||
session_msg_count=2,
|
||||
transcript_builder=builder,
|
||||
)
|
||||
|
||||
assert upload_safe is True
|
||||
assert dl is None
|
||||
assert covers is False
|
||||
assert builder.is_empty
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_invalid_transcript_allows_upload(self):
|
||||
"""Corrupt file in GCS → overwriting with a valid one is better."""
|
||||
async def test_invalid_transcript_returns_false(self):
|
||||
builder = TranscriptBuilder()
|
||||
restore = TranscriptDownload(
|
||||
content=b'{"type":"progress","uuid":"a"}\n',
|
||||
download = TranscriptDownload(
|
||||
content='{"type":"progress","uuid":"a"}\n',
|
||||
message_count=1,
|
||||
mode="sdk",
|
||||
)
|
||||
with patch(
|
||||
"backend.copilot.baseline.service.download_transcript",
|
||||
new=AsyncMock(return_value=restore),
|
||||
new=AsyncMock(return_value=download),
|
||||
):
|
||||
upload_safe, dl = await _load_prior_transcript(
|
||||
covers = await _load_prior_transcript(
|
||||
user_id="user-1",
|
||||
session_id="session-1",
|
||||
session_messages=_make_session_messages("user", "assistant"),
|
||||
session_msg_count=2,
|
||||
transcript_builder=builder,
|
||||
)
|
||||
|
||||
assert upload_safe is True
|
||||
assert dl is None
|
||||
assert covers is False
|
||||
assert builder.is_empty
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -186,39 +163,36 @@ class TestLoadPriorTranscript:
|
||||
"backend.copilot.baseline.service.download_transcript",
|
||||
new=AsyncMock(side_effect=RuntimeError("boom")),
|
||||
):
|
||||
covers, dl = await _load_prior_transcript(
|
||||
covers = await _load_prior_transcript(
|
||||
user_id="user-1",
|
||||
session_id="session-1",
|
||||
session_messages=_make_session_messages("user", "assistant"),
|
||||
session_msg_count=2,
|
||||
transcript_builder=builder,
|
||||
)
|
||||
|
||||
assert covers is False
|
||||
assert dl is None
|
||||
assert builder.is_empty
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zero_message_count_not_stale(self):
|
||||
"""When msg_count is 0 (unknown), gap detection is skipped."""
|
||||
"""When msg_count is 0 (unknown), staleness check is skipped."""
|
||||
builder = TranscriptBuilder()
|
||||
restore = TranscriptDownload(
|
||||
content=_make_transcript_content("user", "assistant").encode("utf-8"),
|
||||
download = TranscriptDownload(
|
||||
content=_make_transcript_content("user", "assistant"),
|
||||
message_count=0,
|
||||
mode="sdk",
|
||||
)
|
||||
with patch(
|
||||
"backend.copilot.baseline.service.download_transcript",
|
||||
new=AsyncMock(return_value=restore),
|
||||
new=AsyncMock(return_value=download),
|
||||
):
|
||||
covers, dl = await _load_prior_transcript(
|
||||
covers = await _load_prior_transcript(
|
||||
user_id="user-1",
|
||||
session_id="session-1",
|
||||
session_messages=_make_session_messages(*["user"] * 20),
|
||||
session_msg_count=20,
|
||||
transcript_builder=builder,
|
||||
)
|
||||
|
||||
assert covers is True
|
||||
assert dl is not None
|
||||
assert builder.entry_count == 2
|
||||
|
||||
|
||||
@@ -253,7 +227,7 @@ class TestUploadFinalTranscript:
|
||||
assert call_kwargs["user_id"] == "user-1"
|
||||
assert call_kwargs["session_id"] == "session-1"
|
||||
assert call_kwargs["message_count"] == 2
|
||||
assert b"hello" in call_kwargs["content"]
|
||||
assert "hello" in call_kwargs["content"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_skips_upload_when_builder_empty(self):
|
||||
@@ -400,19 +374,17 @@ class TestRoundTrip:
|
||||
@pytest.mark.asyncio
|
||||
async def test_full_round_trip(self):
|
||||
prior = _make_transcript_content("user", "assistant")
|
||||
restore = TranscriptDownload(
|
||||
content=prior.encode("utf-8"), message_count=2, mode="sdk"
|
||||
)
|
||||
download = TranscriptDownload(content=prior, message_count=2)
|
||||
|
||||
builder = TranscriptBuilder()
|
||||
with patch(
|
||||
"backend.copilot.baseline.service.download_transcript",
|
||||
new=AsyncMock(return_value=restore),
|
||||
new=AsyncMock(return_value=download),
|
||||
):
|
||||
covers, _ = await _load_prior_transcript(
|
||||
covers = await _load_prior_transcript(
|
||||
user_id="user-1",
|
||||
session_id="session-1",
|
||||
session_messages=_make_session_messages("user", "assistant", "user"),
|
||||
session_msg_count=3,
|
||||
transcript_builder=builder,
|
||||
)
|
||||
assert covers is True
|
||||
@@ -452,11 +424,11 @@ class TestRoundTrip:
|
||||
upload_mock.assert_awaited_once()
|
||||
assert upload_mock.await_args is not None
|
||||
uploaded = upload_mock.await_args.kwargs["content"]
|
||||
assert b"new question" in uploaded
|
||||
assert b"new answer" in uploaded
|
||||
assert "new question" in uploaded
|
||||
assert "new answer" in uploaded
|
||||
# Original content preserved in the round trip.
|
||||
assert b"user message 0" in uploaded
|
||||
assert b"assistant message 1" in uploaded
|
||||
assert "user message 0" in uploaded
|
||||
assert "assistant message 1" in uploaded
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_backfill_append_guard(self):
|
||||
@@ -487,6 +459,36 @@ class TestRoundTrip:
|
||||
assert builder.entry_count == initial_count
|
||||
|
||||
|
||||
class TestIsTranscriptStale:
|
||||
"""``is_transcript_stale`` gates prior-transcript loading."""
|
||||
|
||||
def test_none_download_is_not_stale(self):
|
||||
assert is_transcript_stale(None, session_msg_count=5) is False
|
||||
|
||||
def test_zero_message_count_is_not_stale(self):
|
||||
"""Legacy transcripts without msg_count tracking must remain usable."""
|
||||
dl = TranscriptDownload(content="", message_count=0)
|
||||
assert is_transcript_stale(dl, session_msg_count=20) is False
|
||||
|
||||
def test_stale_when_covers_less_than_prefix(self):
|
||||
dl = TranscriptDownload(content="", message_count=2)
|
||||
# session has 6 messages; transcript must cover at least 5 (6-1).
|
||||
assert is_transcript_stale(dl, session_msg_count=6) is True
|
||||
|
||||
def test_fresh_when_covers_full_prefix(self):
|
||||
dl = TranscriptDownload(content="", message_count=5)
|
||||
assert is_transcript_stale(dl, session_msg_count=6) is False
|
||||
|
||||
def test_fresh_when_exceeds_prefix(self):
|
||||
"""Race: transcript ahead of session count is still acceptable."""
|
||||
dl = TranscriptDownload(content="", message_count=10)
|
||||
assert is_transcript_stale(dl, session_msg_count=6) is False
|
||||
|
||||
def test_boundary_equal_to_prefix_minus_one(self):
|
||||
dl = TranscriptDownload(content="", message_count=5)
|
||||
assert is_transcript_stale(dl, session_msg_count=6) is False
|
||||
|
||||
|
||||
class TestShouldUploadTranscript:
|
||||
"""``should_upload_transcript`` gates the final upload."""
|
||||
|
||||
@@ -508,7 +510,7 @@ class TestShouldUploadTranscript:
|
||||
|
||||
|
||||
class TestTranscriptLifecycle:
|
||||
"""End-to-end: restore → validate → build → upload.
|
||||
"""End-to-end: download → validate → build → upload.
|
||||
|
||||
Simulates the full transcript lifecycle inside
|
||||
``stream_chat_completion_baseline`` by mocking the storage layer and
|
||||
@@ -517,29 +519,27 @@ class TestTranscriptLifecycle:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_full_lifecycle_happy_path(self):
|
||||
"""Fresh restore, append a turn, upload covers the session."""
|
||||
"""Fresh download, append a turn, upload covers the session."""
|
||||
builder = TranscriptBuilder()
|
||||
prior = _make_transcript_content("user", "assistant")
|
||||
restore = TranscriptDownload(
|
||||
content=prior.encode("utf-8"), message_count=2, mode="sdk"
|
||||
)
|
||||
download = TranscriptDownload(content=prior, message_count=2)
|
||||
|
||||
upload_mock = AsyncMock(return_value=None)
|
||||
with (
|
||||
patch(
|
||||
"backend.copilot.baseline.service.download_transcript",
|
||||
new=AsyncMock(return_value=restore),
|
||||
new=AsyncMock(return_value=download),
|
||||
),
|
||||
patch(
|
||||
"backend.copilot.baseline.service.upload_transcript",
|
||||
new=upload_mock,
|
||||
),
|
||||
):
|
||||
# --- 1. Restore & load prior session ---
|
||||
covers, _ = await _load_prior_transcript(
|
||||
# --- 1. Download & load prior transcript ---
|
||||
covers = await _load_prior_transcript(
|
||||
user_id="user-1",
|
||||
session_id="session-1",
|
||||
session_messages=_make_session_messages("user", "assistant", "user"),
|
||||
session_msg_count=3,
|
||||
transcript_builder=builder,
|
||||
)
|
||||
assert covers is True
|
||||
@@ -559,7 +559,10 @@ class TestTranscriptLifecycle:
|
||||
|
||||
# --- 3. Gate + upload ---
|
||||
assert (
|
||||
should_upload_transcript(user_id="user-1", upload_safe=covers) is True
|
||||
should_upload_transcript(
|
||||
user_id="user-1", transcript_covers_prefix=covers
|
||||
)
|
||||
is True
|
||||
)
|
||||
await _upload_final_transcript(
|
||||
user_id="user-1",
|
||||
@@ -571,21 +574,20 @@ class TestTranscriptLifecycle:
|
||||
upload_mock.assert_awaited_once()
|
||||
assert upload_mock.await_args is not None
|
||||
uploaded = upload_mock.await_args.kwargs["content"]
|
||||
assert b"follow-up question" in uploaded
|
||||
assert b"follow-up answer" in uploaded
|
||||
assert "follow-up question" in uploaded
|
||||
assert "follow-up answer" in uploaded
|
||||
# Original prior-turn content preserved.
|
||||
assert b"user message 0" in uploaded
|
||||
assert b"assistant message 1" in uploaded
|
||||
assert "user message 0" in uploaded
|
||||
assert "assistant message 1" in uploaded
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_lifecycle_stale_download_fills_gap(self):
|
||||
"""When transcript covers fewer messages, gap is filled rather than rejected."""
|
||||
async def test_lifecycle_stale_download_suppresses_upload(self):
|
||||
"""Stale download → covers=False → upload must be skipped."""
|
||||
builder = TranscriptBuilder()
|
||||
# session has 5 msgs but stored transcript only covers 2 → gap filled.
|
||||
# session has 10 msgs but stored transcript only covers 2 → stale.
|
||||
stale = TranscriptDownload(
|
||||
content=_make_transcript_content("user", "assistant").encode("utf-8"),
|
||||
content=_make_transcript_content("user", "assistant"),
|
||||
message_count=2,
|
||||
mode="baseline",
|
||||
)
|
||||
|
||||
upload_mock = AsyncMock(return_value=None)
|
||||
@@ -599,18 +601,20 @@ class TestTranscriptLifecycle:
|
||||
new=upload_mock,
|
||||
),
|
||||
):
|
||||
covers, _ = await _load_prior_transcript(
|
||||
covers = await _load_prior_transcript(
|
||||
user_id="user-1",
|
||||
session_id="session-1",
|
||||
session_messages=_make_session_messages(
|
||||
"user", "assistant", "user", "assistant", "user"
|
||||
),
|
||||
session_msg_count=10,
|
||||
transcript_builder=builder,
|
||||
)
|
||||
|
||||
assert covers is True
|
||||
# Gap was filled: 2 from transcript + 2 gap messages
|
||||
assert builder.entry_count == 4
|
||||
assert covers is False
|
||||
# The caller's gate mirrors the production path.
|
||||
assert (
|
||||
should_upload_transcript(user_id="user-1", transcript_covers_prefix=covers)
|
||||
is False
|
||||
)
|
||||
upload_mock.assert_not_awaited()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_lifecycle_anonymous_user_skips_upload(self):
|
||||
@@ -623,11 +627,15 @@ class TestTranscriptLifecycle:
|
||||
stop_reason=STOP_REASON_END_TURN,
|
||||
)
|
||||
|
||||
assert should_upload_transcript(user_id=None, upload_safe=True) is False
|
||||
assert (
|
||||
should_upload_transcript(user_id=None, transcript_covers_prefix=True)
|
||||
is False
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_lifecycle_missing_download_still_uploads_new_content(self):
|
||||
"""No prior session → upload is safe; the turn writes the first snapshot."""
|
||||
"""No prior transcript → covers defaults to True in the service,
|
||||
new turn should upload cleanly."""
|
||||
builder = TranscriptBuilder()
|
||||
upload_mock = AsyncMock(return_value=None)
|
||||
with (
|
||||
@@ -640,117 +648,20 @@ class TestTranscriptLifecycle:
|
||||
new=upload_mock,
|
||||
),
|
||||
):
|
||||
upload_safe, dl = await _load_prior_transcript(
|
||||
covers = await _load_prior_transcript(
|
||||
user_id="user-1",
|
||||
session_id="session-1",
|
||||
session_messages=_make_session_messages("user"),
|
||||
session_msg_count=1,
|
||||
transcript_builder=builder,
|
||||
)
|
||||
# Nothing in GCS → upload is safe so the first baseline turn
|
||||
# can write the initial transcript snapshot.
|
||||
assert upload_safe is True
|
||||
assert dl is None
|
||||
# No download: covers is False, so the production path would
|
||||
# skip upload. This protects against overwriting a future
|
||||
# more-complete transcript with a single-turn snapshot.
|
||||
assert covers is False
|
||||
assert (
|
||||
should_upload_transcript(user_id="user-1", upload_safe=upload_safe)
|
||||
is True
|
||||
should_upload_transcript(
|
||||
user_id="user-1", transcript_covers_prefix=covers
|
||||
)
|
||||
is False
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _append_gap_to_builder
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestAppendGapToBuilder:
|
||||
"""``_append_gap_to_builder`` converts ChatMessage objects to TranscriptBuilder entries."""
|
||||
|
||||
def test_user_message_appended(self):
|
||||
builder = TranscriptBuilder()
|
||||
msgs = [ChatMessage(role="user", content="hello")]
|
||||
_append_gap_to_builder(msgs, builder)
|
||||
assert builder.entry_count == 1
|
||||
assert builder.last_entry_type == "user"
|
||||
|
||||
def test_assistant_text_message_appended(self):
|
||||
builder = TranscriptBuilder()
|
||||
msgs = [
|
||||
ChatMessage(role="user", content="q"),
|
||||
ChatMessage(role="assistant", content="answer"),
|
||||
]
|
||||
_append_gap_to_builder(msgs, builder)
|
||||
assert builder.entry_count == 2
|
||||
assert builder.last_entry_type == "assistant"
|
||||
assert "answer" in builder.to_jsonl()
|
||||
|
||||
def test_assistant_with_tool_calls_appended(self):
|
||||
"""Assistant tool_calls are recorded as tool_use blocks in the transcript."""
|
||||
builder = TranscriptBuilder()
|
||||
tool_call = {
|
||||
"id": "tc-1",
|
||||
"type": "function",
|
||||
"function": {"name": "my_tool", "arguments": '{"key":"val"}'},
|
||||
}
|
||||
msgs = [ChatMessage(role="assistant", content=None, tool_calls=[tool_call])]
|
||||
_append_gap_to_builder(msgs, builder)
|
||||
assert builder.entry_count == 1
|
||||
jsonl = builder.to_jsonl()
|
||||
assert "tool_use" in jsonl
|
||||
assert "my_tool" in jsonl
|
||||
assert "tc-1" in jsonl
|
||||
|
||||
def test_assistant_invalid_json_args_uses_empty_dict(self):
|
||||
"""Malformed JSON in tool_call arguments falls back to {}."""
|
||||
builder = TranscriptBuilder()
|
||||
tool_call = {
|
||||
"id": "tc-bad",
|
||||
"type": "function",
|
||||
"function": {"name": "bad_tool", "arguments": "not-json"},
|
||||
}
|
||||
msgs = [ChatMessage(role="assistant", content=None, tool_calls=[tool_call])]
|
||||
_append_gap_to_builder(msgs, builder)
|
||||
assert builder.entry_count == 1
|
||||
jsonl = builder.to_jsonl()
|
||||
assert '"input":{}' in jsonl
|
||||
|
||||
def test_assistant_empty_content_and_no_tools_uses_fallback(self):
|
||||
"""Assistant with no content and no tool_calls gets a fallback empty text block."""
|
||||
builder = TranscriptBuilder()
|
||||
msgs = [ChatMessage(role="assistant", content=None)]
|
||||
_append_gap_to_builder(msgs, builder)
|
||||
assert builder.entry_count == 1
|
||||
jsonl = builder.to_jsonl()
|
||||
assert "text" in jsonl
|
||||
|
||||
def test_tool_role_with_tool_call_id_appended(self):
|
||||
"""Tool result messages are appended when tool_call_id is set."""
|
||||
builder = TranscriptBuilder()
|
||||
# Need a preceding assistant tool_use entry
|
||||
builder.append_user("use tool")
|
||||
builder.append_assistant(
|
||||
content_blocks=[
|
||||
{"type": "tool_use", "id": "tc-1", "name": "my_tool", "input": {}}
|
||||
]
|
||||
)
|
||||
msgs = [ChatMessage(role="tool", tool_call_id="tc-1", content="result")]
|
||||
_append_gap_to_builder(msgs, builder)
|
||||
assert builder.entry_count == 3
|
||||
assert "tool_result" in builder.to_jsonl()
|
||||
|
||||
def test_tool_role_without_tool_call_id_skipped(self):
|
||||
"""Tool messages without tool_call_id are silently skipped."""
|
||||
builder = TranscriptBuilder()
|
||||
msgs = [ChatMessage(role="tool", tool_call_id=None, content="orphan")]
|
||||
_append_gap_to_builder(msgs, builder)
|
||||
assert builder.entry_count == 0
|
||||
|
||||
def test_tool_call_missing_function_key_uses_unknown_name(self):
|
||||
"""A tool_call dict with no 'function' key uses 'unknown' as the tool name."""
|
||||
builder = TranscriptBuilder()
|
||||
# Tool call dict exists but 'function' sub-dict is missing entirely
|
||||
msgs = [
|
||||
ChatMessage(role="assistant", content=None, tool_calls=[{"id": "tc-x"}])
|
||||
]
|
||||
_append_gap_to_builder(msgs, builder)
|
||||
assert builder.entry_count == 1
|
||||
jsonl = builder.to_jsonl()
|
||||
assert "unknown" in jsonl
|
||||
upload_mock.assert_not_awaited()
|
||||
|
||||
@@ -16,32 +16,18 @@ from backend.util.clients import OPENROUTER_BASE_URL
|
||||
# subscription flag → LaunchDarkly COPILOT_SDK → config.use_claude_agent_sdk.
|
||||
CopilotMode = Literal["fast", "extended_thinking"]
|
||||
|
||||
# Per-request model tier set by the frontend model toggle.
|
||||
# 'standard' uses ``ChatConfig.model`` (Sonnet by default).
|
||||
# 'advanced' uses ``ChatConfig.advanced_model`` (Opus by default).
|
||||
# None means no preference — falls through to LD per-user targeting, then config.
|
||||
# Using tier names instead of model names keeps the contract model-agnostic.
|
||||
CopilotLlmModel = Literal["standard", "advanced"]
|
||||
|
||||
|
||||
class ChatConfig(BaseSettings):
|
||||
"""Configuration for the chat system."""
|
||||
|
||||
# Chat model tiers — applied orthogonally to the path (fast=baseline vs
|
||||
# extended_thinking=SDK). The "fast" vs "extended_thinking" toggle picks
|
||||
# which code path runs (no reasoning / heavy SDK); "standard" vs
|
||||
# "advanced" picks the model inside that path.
|
||||
# OpenAI API Configuration
|
||||
model: str = Field(
|
||||
default="anthropic/claude-sonnet-4-6",
|
||||
description="Model used for the 'standard' tier (Sonnet by default). "
|
||||
"Applies to both baseline (fast) and SDK (extended thinking) paths. "
|
||||
"Override via CHAT_MODEL env var.",
|
||||
default="anthropic/claude-opus-4.6",
|
||||
description="Default model for extended thinking mode",
|
||||
)
|
||||
advanced_model: str = Field(
|
||||
default="anthropic/claude-opus-4-7",
|
||||
description="Model used for the 'advanced' tier (Opus by default). "
|
||||
"Applies to both baseline (fast) and SDK (extended thinking) paths. "
|
||||
"Override via CHAT_ADVANCED_MODEL env var.",
|
||||
fast_model: str = Field(
|
||||
default="anthropic/claude-sonnet-4",
|
||||
description="Model for fast mode (baseline path). Should be faster/cheaper than the default model.",
|
||||
)
|
||||
title_model: str = Field(
|
||||
default="openai/gpt-4o-mini",
|
||||
@@ -101,31 +87,25 @@ class ChatConfig(BaseSettings):
|
||||
description="Cache TTL in seconds for Langfuse prompt (0 to disable caching)",
|
||||
)
|
||||
|
||||
# Rate limiting — cost-based limits per day and per week, stored in
|
||||
# microdollars (1 USD = 1_000_000). The counter tracks the real
|
||||
# generation cost reported by the provider (OpenRouter ``usage.cost``
|
||||
# or Claude Agent SDK ``total_cost_usd``), so cache discounts and
|
||||
# cross-model price differences are already reflected — no token
|
||||
# weighting or model multiplier is applied on top.
|
||||
# Rate limiting — token-based limits per day and per week.
|
||||
# Per-turn token cost varies with context size: ~10-15K for early turns,
|
||||
# ~30-50K mid-session, up to ~100K pre-compaction. Average across a
|
||||
# session with compaction cycles is ~25-35K tokens/turn, so 2.5M daily
|
||||
# allows ~70-100 turns/day.
|
||||
# Checked at the HTTP layer (routes.py) before each turn.
|
||||
#
|
||||
# These are base limits for the FREE tier. Higher tiers (PRO, BUSINESS,
|
||||
# These are base limits for the FREE tier. Higher tiers (PRO, BUSINESS,
|
||||
# ENTERPRISE) multiply these by their tier multiplier (see
|
||||
# rate_limit.TIER_MULTIPLIERS). User tier is stored in the
|
||||
# rate_limit.TIER_MULTIPLIERS). User tier is stored in the
|
||||
# User.subscriptionTier DB column and resolved inside
|
||||
# get_global_rate_limits().
|
||||
#
|
||||
# These defaults act as the ceiling when LaunchDarkly is unreachable;
|
||||
# the live per-tier values come from the COPILOT_*_COST_LIMIT flags.
|
||||
daily_cost_limit_microdollars: int = Field(
|
||||
default=1_000_000,
|
||||
description="Max cost per day in microdollars, resets at midnight UTC "
|
||||
"(0 = unlimited).",
|
||||
daily_token_limit: int = Field(
|
||||
default=2_500_000,
|
||||
description="Max tokens per day, resets at midnight UTC (0 = unlimited)",
|
||||
)
|
||||
weekly_cost_limit_microdollars: int = Field(
|
||||
default=5_000_000,
|
||||
description="Max cost per week in microdollars, resets Monday 00:00 UTC "
|
||||
"(0 = unlimited).",
|
||||
weekly_token_limit: int = Field(
|
||||
default=12_500_000,
|
||||
description="Max tokens per week, resets Monday 00:00 UTC (0 = unlimited)",
|
||||
)
|
||||
|
||||
# Cost (in credits / cents) to reset the daily rate limit using credits.
|
||||
@@ -166,97 +146,6 @@ class ChatConfig(BaseSettings):
|
||||
description="Use --resume for multi-turn conversations instead of "
|
||||
"history compression. Falls back to compression when unavailable.",
|
||||
)
|
||||
claude_agent_fallback_model: str = Field(
|
||||
default="",
|
||||
description="Fallback model when the primary model is unavailable (e.g. 529 "
|
||||
"overloaded). The SDK automatically retries with this cheaper model. "
|
||||
"Empty string disables the fallback (no --fallback-model flag passed to CLI).",
|
||||
)
|
||||
claude_agent_max_turns: int = Field(
|
||||
default=50,
|
||||
ge=1,
|
||||
le=10000,
|
||||
description="Maximum number of agentic turns (tool-use loops) per query. "
|
||||
"Prevents runaway tool loops from burning budget. "
|
||||
"Changed from 1000 to 50 in SDK 0.1.58 upgrade — override via "
|
||||
"CHAT_CLAUDE_AGENT_MAX_TURNS env var if your workflows need more.",
|
||||
)
|
||||
claude_agent_max_budget_usd: float = Field(
|
||||
default=10.0,
|
||||
ge=0.01,
|
||||
le=1000.0,
|
||||
description="Maximum spend in USD per SDK query. The CLI attempts "
|
||||
"to wrap up gracefully when this budget is reached. "
|
||||
"Set to $10 to allow most tasks to complete (p50=$5.37, p75=$13.07). "
|
||||
"Override via CHAT_CLAUDE_AGENT_MAX_BUDGET_USD env var.",
|
||||
)
|
||||
claude_agent_max_thinking_tokens: int = Field(
|
||||
default=8192,
|
||||
ge=0,
|
||||
le=128000,
|
||||
description="Maximum thinking/reasoning tokens per LLM call. Applies "
|
||||
"to both the Claude Agent SDK path (as ``max_thinking_tokens``) and "
|
||||
"the baseline OpenRouter path (as ``extra_body.reasoning.max_tokens`` "
|
||||
"on Anthropic routes). Extended thinking on Opus can generate 50k+ "
|
||||
"tokens at $75/M — capping this is the single biggest cost lever. "
|
||||
"8192 is sufficient for most tasks; increase for complex reasoning. "
|
||||
"Set to 0 to disable extended thinking on both paths (kill switch): "
|
||||
"baseline skips the ``reasoning`` extra_body; SDK omits the "
|
||||
"``max_thinking_tokens`` kwarg so the CLI falls back to model default "
|
||||
"(which, without the flag, leaves extended thinking off).",
|
||||
)
|
||||
claude_agent_thinking_effort: Literal["low", "medium", "high", "max"] | None = (
|
||||
Field(
|
||||
default=None,
|
||||
description="Thinking effort level: 'low', 'medium', 'high', 'max', or None. "
|
||||
"Only applies to models with extended thinking (Opus). "
|
||||
"Sonnet doesn't have extended thinking — setting effort on Sonnet "
|
||||
"can cause <internal_reasoning> tag leaks. "
|
||||
"None = let the model decide. Override via CHAT_CLAUDE_AGENT_THINKING_EFFORT.",
|
||||
)
|
||||
)
|
||||
claude_agent_max_transient_retries: int = Field(
|
||||
default=3,
|
||||
ge=0,
|
||||
le=10,
|
||||
description="Maximum number of retries for transient API errors "
|
||||
"(429, 5xx, ECONNRESET) before surfacing the error to the user.",
|
||||
)
|
||||
claude_agent_cross_user_prompt_cache: bool = Field(
|
||||
default=True,
|
||||
description="Enable cross-user prompt caching via SystemPromptPreset. "
|
||||
"The Claude Code default prompt becomes a cacheable prefix shared "
|
||||
"across all users, and our custom prompt is appended after it. "
|
||||
"Dynamic sections (working dir, git status, auto-memory) are excluded "
|
||||
"from the prefix. Set to False to fall back to passing the system "
|
||||
"prompt as a raw string.",
|
||||
)
|
||||
baseline_prompt_cache_ttl: str = Field(
|
||||
default="1h",
|
||||
description="TTL for the ephemeral prompt-cache markers on the baseline "
|
||||
"OpenRouter path. Anthropic supports only `5m` (default, 1.25x input "
|
||||
"price for the write) or `1h` (2x input price for the write). 1h is "
|
||||
"strictly cheaper overall when the static prefix gets >7 reads per "
|
||||
"write-window; since the system prompt + tools array is identical "
|
||||
"across all users in our workspace, 1h is the default so cross-user "
|
||||
"reads amortise the higher write cost. Anthropic has no longer "
|
||||
"(24h, permanent) TTL option — see "
|
||||
"https://platform.claude.com/docs/en/build-with-claude/prompt-caching.",
|
||||
)
|
||||
claude_agent_cli_path: str | None = Field(
|
||||
default=None,
|
||||
description="Optional explicit path to a Claude Code CLI binary. "
|
||||
"When set, the SDK uses this binary instead of the version bundled "
|
||||
"with the installed `claude-agent-sdk` package — letting us pin "
|
||||
"the Python SDK and the CLI independently. Critical for keeping "
|
||||
"OpenRouter compatibility while still picking up newer SDK API "
|
||||
"features (the bundled CLI version in 0.1.46+ is broken against "
|
||||
"OpenRouter — see PR #12294 and "
|
||||
"anthropics/claude-agent-sdk-python#789). Falls back to the "
|
||||
"bundled binary when unset. Reads from `CHAT_CLAUDE_AGENT_CLI_PATH` "
|
||||
"or the unprefixed `CLAUDE_AGENT_CLI_PATH` environment variable "
|
||||
"(same pattern as `api_key` / `base_url`).",
|
||||
)
|
||||
use_openrouter: bool = Field(
|
||||
default=True,
|
||||
description="Enable routing API calls through the OpenRouter proxy. "
|
||||
@@ -379,40 +268,6 @@ class ChatConfig(BaseSettings):
|
||||
v = OPENROUTER_BASE_URL
|
||||
return v
|
||||
|
||||
@field_validator("claude_agent_cli_path", mode="before")
|
||||
@classmethod
|
||||
def get_claude_agent_cli_path(cls, v):
|
||||
"""Resolve the Claude Code CLI override path from environment.
|
||||
|
||||
Accepts either the Pydantic-prefixed ``CHAT_CLAUDE_AGENT_CLI_PATH``
|
||||
or the unprefixed ``CLAUDE_AGENT_CLI_PATH`` (matching the same
|
||||
fallback pattern used by ``api_key`` / ``base_url``). Keeping the
|
||||
unprefixed form working is important because the field is
|
||||
primarily an operator escape hatch set via container/host env,
|
||||
and the unprefixed name is what the PR description, the field
|
||||
docstrings, and the reproduction test in
|
||||
``cli_openrouter_compat_test.py`` refer to.
|
||||
"""
|
||||
if not v:
|
||||
v = os.getenv("CHAT_CLAUDE_AGENT_CLI_PATH")
|
||||
if not v:
|
||||
v = os.getenv("CLAUDE_AGENT_CLI_PATH")
|
||||
if v:
|
||||
if not os.path.exists(v):
|
||||
raise ValueError(
|
||||
f"claude_agent_cli_path '{v}' does not exist. "
|
||||
"Check the path or unset CLAUDE_AGENT_CLI_PATH to use "
|
||||
"the bundled CLI."
|
||||
)
|
||||
if not os.path.isfile(v):
|
||||
raise ValueError(f"claude_agent_cli_path '{v}' is not a regular file.")
|
||||
if not os.access(v, os.X_OK):
|
||||
raise ValueError(
|
||||
f"claude_agent_cli_path '{v}' exists but is not executable. "
|
||||
"Check file permissions."
|
||||
)
|
||||
return v
|
||||
|
||||
# Prompt paths for different contexts
|
||||
PROMPT_PATHS: dict[str, str] = {
|
||||
"default": "prompts/chat_system.md",
|
||||
|
||||
@@ -17,8 +17,6 @@ _ENV_VARS_TO_CLEAR = (
|
||||
"CHAT_BASE_URL",
|
||||
"OPENROUTER_BASE_URL",
|
||||
"OPENAI_BASE_URL",
|
||||
"CHAT_CLAUDE_AGENT_CLI_PATH",
|
||||
"CLAUDE_AGENT_CLI_PATH",
|
||||
)
|
||||
|
||||
|
||||
@@ -89,78 +87,3 @@ class TestE2BActive:
|
||||
"""e2b_active is False when use_e2b_sandbox=False regardless of key."""
|
||||
cfg = ChatConfig(use_e2b_sandbox=False, e2b_api_key="test-key")
|
||||
assert cfg.e2b_active is False
|
||||
|
||||
|
||||
class TestClaudeAgentCliPathEnvFallback:
|
||||
"""``claude_agent_cli_path`` accepts both the Pydantic-prefixed
|
||||
``CHAT_CLAUDE_AGENT_CLI_PATH`` env var and the unprefixed
|
||||
``CLAUDE_AGENT_CLI_PATH`` form (mirrors ``api_key`` / ``base_url``).
|
||||
"""
|
||||
|
||||
def test_prefixed_env_var_is_picked_up(
|
||||
self, monkeypatch: pytest.MonkeyPatch, tmp_path
|
||||
) -> None:
|
||||
fake_cli = tmp_path / "fake-claude"
|
||||
fake_cli.write_text("#!/bin/sh\n")
|
||||
fake_cli.chmod(0o755)
|
||||
monkeypatch.setenv("CHAT_CLAUDE_AGENT_CLI_PATH", str(fake_cli))
|
||||
cfg = ChatConfig()
|
||||
assert cfg.claude_agent_cli_path == str(fake_cli)
|
||||
|
||||
def test_unprefixed_env_var_is_picked_up(
|
||||
self, monkeypatch: pytest.MonkeyPatch, tmp_path
|
||||
) -> None:
|
||||
fake_cli = tmp_path / "fake-claude"
|
||||
fake_cli.write_text("#!/bin/sh\n")
|
||||
fake_cli.chmod(0o755)
|
||||
monkeypatch.setenv("CLAUDE_AGENT_CLI_PATH", str(fake_cli))
|
||||
cfg = ChatConfig()
|
||||
assert cfg.claude_agent_cli_path == str(fake_cli)
|
||||
|
||||
def test_prefixed_wins_over_unprefixed(
|
||||
self, monkeypatch: pytest.MonkeyPatch, tmp_path
|
||||
) -> None:
|
||||
prefixed_cli = tmp_path / "fake-claude-prefixed"
|
||||
prefixed_cli.write_text("#!/bin/sh\n")
|
||||
prefixed_cli.chmod(0o755)
|
||||
unprefixed_cli = tmp_path / "fake-claude-unprefixed"
|
||||
unprefixed_cli.write_text("#!/bin/sh\n")
|
||||
unprefixed_cli.chmod(0o755)
|
||||
monkeypatch.setenv("CHAT_CLAUDE_AGENT_CLI_PATH", str(prefixed_cli))
|
||||
monkeypatch.setenv("CLAUDE_AGENT_CLI_PATH", str(unprefixed_cli))
|
||||
cfg = ChatConfig()
|
||||
assert cfg.claude_agent_cli_path == str(prefixed_cli)
|
||||
|
||||
def test_no_env_var_defaults_to_none(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
cfg = ChatConfig()
|
||||
assert cfg.claude_agent_cli_path is None
|
||||
|
||||
def test_nonexistent_path_raises_validation_error(
|
||||
self, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""Non-existent CLI path must be rejected at config time, not at
|
||||
runtime when subprocess.run fails with an opaque OS error."""
|
||||
monkeypatch.setenv(
|
||||
"CLAUDE_AGENT_CLI_PATH", "/opt/nonexistent/claude-cli-binary"
|
||||
)
|
||||
with pytest.raises(Exception, match="does not exist"):
|
||||
ChatConfig()
|
||||
|
||||
def test_non_executable_path_raises_validation_error(
|
||||
self, monkeypatch: pytest.MonkeyPatch, tmp_path
|
||||
) -> None:
|
||||
"""Path that exists but is not executable must be rejected."""
|
||||
non_exec = tmp_path / "claude-not-executable"
|
||||
non_exec.write_text("#!/bin/sh\n")
|
||||
non_exec.chmod(0o644) # readable but not executable
|
||||
monkeypatch.setenv("CLAUDE_AGENT_CLI_PATH", str(non_exec))
|
||||
with pytest.raises(Exception, match="not executable"):
|
||||
ChatConfig()
|
||||
|
||||
def test_directory_path_raises_validation_error(
|
||||
self, monkeypatch: pytest.MonkeyPatch, tmp_path
|
||||
) -> None:
|
||||
"""Path pointing to a directory must be rejected."""
|
||||
monkeypatch.setenv("CLAUDE_AGENT_CLI_PATH", str(tmp_path))
|
||||
with pytest.raises(Exception, match="not a regular file"):
|
||||
ChatConfig()
|
||||
|
||||
@@ -9,11 +9,6 @@ COPILOT_RETRYABLE_ERROR_PREFIX = (
|
||||
)
|
||||
COPILOT_SYSTEM_PREFIX = "[__COPILOT_SYSTEM_e3b0__]" # Renders as system info message
|
||||
|
||||
# Canonical marker appended as an assistant ChatMessage when the SDK stream
|
||||
# ends without a ResultMessage (user hit Stop). Checked by exact equality
|
||||
# at turn start so the next turn's --resume transcript doesn't carry it.
|
||||
STOPPED_BY_USER_MARKER = f"{COPILOT_SYSTEM_PREFIX} Execution stopped by user"
|
||||
|
||||
# Prefix for all synthetic IDs generated by CoPilot block execution.
|
||||
# Used to distinguish CoPilot-generated records from real graph execution records
|
||||
# in PendingHumanReview and other tables.
|
||||
@@ -32,24 +27,6 @@ COMPACTION_DONE_MSG = "Earlier messages were summarized to fit within context li
|
||||
COMPACTION_TOOL_NAME = "context_compaction"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tool / stream timing budget
|
||||
# ---------------------------------------------------------------------------
|
||||
# Max seconds any single MCP tool call may block the stream before returning
|
||||
# a "still running" handle. Shared by run_agent (wait_for_result),
|
||||
# view_agent_output (wait_if_running), run_sub_session (wait_for_result),
|
||||
# get_sub_session_result (wait_if_running), and run_block (hard cap).
|
||||
#
|
||||
# Chosen so the stream idle timeout (2× this) always has headroom — a tool
|
||||
# that returns right at the cap can't race the idle watchdog.
|
||||
MAX_TOOL_WAIT_SECONDS = 5 * 60 # 5 minutes
|
||||
|
||||
# Idle-stream watchdog: abort the SDK stream if no meaningful event arrives
|
||||
# for this long. Derived from MAX_TOOL_WAIT_SECONDS so the invariant
|
||||
# "no tool blocks >= idle_timeout" holds by construction.
|
||||
STREAM_IDLE_TIMEOUT_SECONDS = MAX_TOOL_WAIT_SECONDS * 2 # 10 minutes
|
||||
|
||||
|
||||
def is_copilot_synthetic_id(id_value: str) -> bool:
|
||||
"""Check if an ID is a CoPilot synthetic ID (not from a real graph execution)."""
|
||||
return id_value.startswith(COPILOT_SYNTHETIC_ID_PREFIX)
|
||||
@@ -67,36 +44,15 @@ def parse_node_id_from_exec_id(node_exec_id: str) -> str:
|
||||
# Transient Anthropic API error detection
|
||||
# ---------------------------------------------------------------------------
|
||||
# Patterns in error text that indicate a transient Anthropic API error
|
||||
# which is retryable. Covers:
|
||||
# - Connection-level: ECONNRESET, dropped TCP connections
|
||||
# - HTTP 429: rate-limit / too-many-requests
|
||||
# - HTTP 5xx: server errors
|
||||
#
|
||||
# Prefer specific status-code patterns over natural-language phrases
|
||||
# (e.g. "overloaded", "bad gateway") — those phrases can appear in
|
||||
# application-level SDK messages and would trigger spurious retries.
|
||||
# (ECONNRESET / dropped TCP connection) which is retryable.
|
||||
_TRANSIENT_ERROR_PATTERNS = (
|
||||
# Connection-level
|
||||
"socket connection was closed unexpectedly",
|
||||
"ECONNRESET",
|
||||
"connection was forcibly closed",
|
||||
"network socket disconnected",
|
||||
# 429 rate-limit patterns
|
||||
"rate limit",
|
||||
"rate_limit",
|
||||
"too many requests",
|
||||
"status code 429",
|
||||
# 5xx server error patterns (status-code-specific to avoid false positives)
|
||||
"status code 529",
|
||||
"status code 500",
|
||||
"status code 502",
|
||||
"status code 503",
|
||||
"status code 504",
|
||||
)
|
||||
|
||||
FRIENDLY_TRANSIENT_MSG = (
|
||||
"Anthropic connection interrupted after repeated attempts — please try again later"
|
||||
)
|
||||
FRIENDLY_TRANSIENT_MSG = "Anthropic connection interrupted — please retry"
|
||||
|
||||
|
||||
def is_transient_api_error(error_text: str) -> bool:
|
||||
|
||||
@@ -23,7 +23,7 @@ if TYPE_CHECKING:
|
||||
# Allowed base directory for the Read tool. Public so service.py can use it
|
||||
# for sweep operations without depending on a private implementation detail.
|
||||
# Respects CLAUDE_CONFIG_DIR env var, consistent with transcript.py's
|
||||
# projects_base() function.
|
||||
# _projects_base() function.
|
||||
_config_dir = os.environ.get("CLAUDE_CONFIG_DIR") or os.path.expanduser("~/.claude")
|
||||
SDK_PROJECTS_DIR = os.path.realpath(os.path.join(_config_dir, "projects"))
|
||||
|
||||
@@ -116,47 +116,6 @@ def is_within_allowed_dirs(path: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def is_sdk_tool_path(path: str) -> bool:
|
||||
"""Return True if *path* is an SDK-internal tool-results or tool-outputs path.
|
||||
|
||||
These paths exist on the host filesystem (not in the E2B sandbox) and are
|
||||
created by the Claude Agent SDK itself. In E2B mode, only these paths should
|
||||
be read from the host; all other paths should be read from the sandbox.
|
||||
|
||||
This is a strict subset of ``is_allowed_local_path`` — it intentionally
|
||||
excludes ``sdk_cwd`` paths because those are the agent's working directory,
|
||||
which in E2B mode is the sandbox, not the host.
|
||||
"""
|
||||
if not path:
|
||||
return False
|
||||
|
||||
if path.startswith("~"):
|
||||
resolved = os.path.realpath(os.path.expanduser(path))
|
||||
elif not os.path.isabs(path):
|
||||
# Relative paths cannot resolve to an absolute SDK-internal path
|
||||
return False
|
||||
else:
|
||||
resolved = os.path.realpath(path)
|
||||
|
||||
encoded = _current_project_dir.get("")
|
||||
if not encoded:
|
||||
return False
|
||||
|
||||
project_dir = os.path.realpath(os.path.join(SDK_PROJECTS_DIR, encoded))
|
||||
if not project_dir.startswith(SDK_PROJECTS_DIR + os.sep):
|
||||
return False
|
||||
if not resolved.startswith(project_dir + os.sep):
|
||||
return False
|
||||
|
||||
relative = resolved[len(project_dir) + 1 :]
|
||||
parts = relative.split(os.sep)
|
||||
return (
|
||||
len(parts) >= 3
|
||||
and _UUID_RE.match(parts[0]) is not None
|
||||
and parts[1] in ("tool-results", "tool-outputs")
|
||||
)
|
||||
|
||||
|
||||
def resolve_sandbox_path(path: str) -> str:
|
||||
"""Normalise *path* to an absolute sandbox path under an allowed directory.
|
||||
|
||||
|
||||
@@ -10,13 +10,10 @@ from prisma.models import ChatMessage as PrismaChatMessage
|
||||
from prisma.models import ChatSession as PrismaChatSession
|
||||
from prisma.types import (
|
||||
ChatMessageCreateInput,
|
||||
ChatMessageWhereInput,
|
||||
ChatSessionCreateInput,
|
||||
ChatSessionUpdateInput,
|
||||
ChatSessionWhereInput,
|
||||
FindManyChatMessageArgsFromChatSession,
|
||||
)
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.data import db
|
||||
from backend.util.json import SafeJson, sanitize_string
|
||||
@@ -32,17 +29,6 @@ from .model import get_chat_session as get_chat_session_cached
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_BOUNDARY_SCAN_LIMIT = 10
|
||||
|
||||
|
||||
class PaginatedMessages(BaseModel):
|
||||
"""Result of a paginated message query."""
|
||||
|
||||
messages: list[ChatMessage]
|
||||
has_more: bool
|
||||
oldest_sequence: int | None
|
||||
session: ChatSessionInfo
|
||||
|
||||
|
||||
async def get_chat_session(session_id: str) -> ChatSession | None:
|
||||
"""Get a chat session by ID from the database."""
|
||||
@@ -53,182 +39,6 @@ async def get_chat_session(session_id: str) -> ChatSession | None:
|
||||
return ChatSession.from_db(session) if session else None
|
||||
|
||||
|
||||
async def get_chat_session_metadata(session_id: str) -> ChatSessionInfo | None:
|
||||
"""Get chat session metadata (without messages) for ownership validation."""
|
||||
session = await PrismaChatSession.prisma().find_unique(
|
||||
where={"id": session_id},
|
||||
)
|
||||
return ChatSessionInfo.from_db(session) if session else None
|
||||
|
||||
|
||||
async def get_chat_messages_paginated(
|
||||
session_id: str,
|
||||
limit: int = 50,
|
||||
before_sequence: int | None = None,
|
||||
user_id: str | None = None,
|
||||
) -> PaginatedMessages | None:
|
||||
"""Get paginated messages for a session, newest first.
|
||||
|
||||
Verifies session existence (and ownership when ``user_id`` is provided)
|
||||
in parallel with the message query. Returns ``None`` when the session
|
||||
is not found or does not belong to the user.
|
||||
|
||||
After fetching, a visibility guarantee ensures the page contains at least
|
||||
one user or assistant message. If the entire page is tool messages (which
|
||||
are hidden in the UI), it expands backward until a visible message is found
|
||||
so the chat never appears blank.
|
||||
"""
|
||||
# Build session-existence / ownership check
|
||||
session_where: ChatSessionWhereInput = {"id": session_id}
|
||||
if user_id is not None:
|
||||
session_where["userId"] = user_id
|
||||
|
||||
# Build message include — fetch paginated messages in the same query
|
||||
msg_include: FindManyChatMessageArgsFromChatSession = {
|
||||
"order_by": {"sequence": "desc"},
|
||||
"take": limit + 1,
|
||||
}
|
||||
if before_sequence is not None:
|
||||
msg_include["where"] = {"sequence": {"lt": before_sequence}}
|
||||
|
||||
# Single query: session existence/ownership + paginated messages
|
||||
session = await PrismaChatSession.prisma().find_first(
|
||||
where=session_where,
|
||||
include={"Messages": msg_include},
|
||||
)
|
||||
|
||||
if session is None:
|
||||
return None
|
||||
|
||||
session_info = ChatSessionInfo.from_db(session)
|
||||
results = list(session.Messages) if session.Messages else []
|
||||
|
||||
has_more = len(results) > limit
|
||||
results = results[:limit]
|
||||
|
||||
# Reverse to ascending order
|
||||
results.reverse()
|
||||
|
||||
# Tool-call boundary fix: if the oldest message is a tool message,
|
||||
# expand backward to include the preceding assistant message that
|
||||
# owns the tool_calls, so convertChatSessionMessagesToUiMessages
|
||||
# can pair them correctly.
|
||||
if results and results[0].role == "tool":
|
||||
results, has_more = await _expand_tool_boundary(
|
||||
session_id, results, has_more, user_id
|
||||
)
|
||||
|
||||
# Visibility guarantee: if the entire page has no user/assistant messages
|
||||
# (all tool messages), the chat would appear blank. Expand backward
|
||||
# until we find at least one visible message.
|
||||
if results and not any(m.role in ("user", "assistant") for m in results):
|
||||
results, has_more = await _expand_for_visibility(
|
||||
session_id, results, has_more, user_id
|
||||
)
|
||||
|
||||
messages = [ChatMessage.from_db(m) for m in results]
|
||||
oldest_sequence = messages[0].sequence if messages else None
|
||||
|
||||
return PaginatedMessages(
|
||||
messages=messages,
|
||||
has_more=has_more,
|
||||
oldest_sequence=oldest_sequence,
|
||||
session=session_info,
|
||||
)
|
||||
|
||||
|
||||
async def _expand_tool_boundary(
|
||||
session_id: str,
|
||||
results: list[Any],
|
||||
has_more: bool,
|
||||
user_id: str | None,
|
||||
) -> tuple[list[Any], bool]:
|
||||
"""Expand backward from the oldest message to include the owning assistant
|
||||
message when the page starts mid-tool-group."""
|
||||
boundary_where: ChatMessageWhereInput = {
|
||||
"sessionId": session_id,
|
||||
"sequence": {"lt": results[0].sequence},
|
||||
}
|
||||
if user_id is not None:
|
||||
boundary_where["Session"] = {"is": {"userId": user_id}}
|
||||
extra = await PrismaChatMessage.prisma().find_many(
|
||||
where=boundary_where,
|
||||
order={"sequence": "desc"},
|
||||
take=_BOUNDARY_SCAN_LIMIT,
|
||||
)
|
||||
# Find the first non-tool message (should be the assistant)
|
||||
boundary_msgs = []
|
||||
found_owner = False
|
||||
for msg in extra:
|
||||
boundary_msgs.append(msg)
|
||||
if msg.role != "tool":
|
||||
found_owner = True
|
||||
break
|
||||
boundary_msgs.reverse()
|
||||
if not found_owner:
|
||||
logger.warning(
|
||||
"Boundary expansion did not find owning assistant message "
|
||||
"for session=%s before sequence=%s (%d msgs scanned)",
|
||||
session_id,
|
||||
results[0].sequence,
|
||||
len(extra),
|
||||
)
|
||||
if boundary_msgs:
|
||||
results = boundary_msgs + results
|
||||
has_more = boundary_msgs[0].sequence > 0
|
||||
return results, has_more
|
||||
|
||||
|
||||
_VISIBILITY_EXPAND_LIMIT = 200
|
||||
|
||||
|
||||
async def _expand_for_visibility(
|
||||
session_id: str,
|
||||
results: list[Any],
|
||||
has_more: bool,
|
||||
user_id: str | None,
|
||||
) -> tuple[list[Any], bool]:
|
||||
"""Expand backward until the page contains at least one user or assistant
|
||||
message, so the chat is never blank."""
|
||||
expand_where: ChatMessageWhereInput = {
|
||||
"sessionId": session_id,
|
||||
"sequence": {"lt": results[0].sequence},
|
||||
}
|
||||
if user_id is not None:
|
||||
expand_where["Session"] = {"is": {"userId": user_id}}
|
||||
extra = await PrismaChatMessage.prisma().find_many(
|
||||
where=expand_where,
|
||||
order={"sequence": "desc"},
|
||||
take=_VISIBILITY_EXPAND_LIMIT,
|
||||
)
|
||||
if not extra:
|
||||
return results, has_more
|
||||
|
||||
# Collect messages until we find a visible one (user/assistant)
|
||||
prepend = []
|
||||
found_visible = False
|
||||
for msg in extra:
|
||||
prepend.append(msg)
|
||||
if msg.role in ("user", "assistant"):
|
||||
found_visible = True
|
||||
break
|
||||
|
||||
if not found_visible:
|
||||
logger.warning(
|
||||
"Visibility expansion did not find any user/assistant message "
|
||||
"for session=%s before sequence=%s (%d msgs scanned)",
|
||||
session_id,
|
||||
results[0].sequence,
|
||||
len(extra),
|
||||
)
|
||||
|
||||
prepend.reverse()
|
||||
if prepend:
|
||||
results = prepend + results
|
||||
has_more = prepend[0].sequence > 0
|
||||
return results, has_more
|
||||
|
||||
|
||||
async def create_chat_session(
|
||||
session_id: str,
|
||||
user_id: str,
|
||||
@@ -568,56 +378,6 @@ async def update_tool_message_content(
|
||||
return False
|
||||
|
||||
|
||||
async def update_message_content_by_sequence(
|
||||
session_id: str,
|
||||
sequence: int,
|
||||
new_content: str,
|
||||
) -> bool:
|
||||
"""Update the content of a specific message by its sequence number.
|
||||
|
||||
Used to persist content modifications (e.g. user-context prefix injection)
|
||||
to a message that was already saved to the DB.
|
||||
|
||||
Authorization note: session_id is a high-entropy UUID generated at session
|
||||
creation time. Callers (inject_user_context) only receive a session_id
|
||||
after the service layer has already validated that the requesting user owns
|
||||
the session, so a userId join is not required here.
|
||||
|
||||
Args:
|
||||
session_id: The chat session ID.
|
||||
sequence: The 0-based sequence number of the message to update.
|
||||
new_content: The new content to set.
|
||||
|
||||
Returns:
|
||||
True if a message was updated, False otherwise.
|
||||
"""
|
||||
try:
|
||||
result = await PrismaChatMessage.prisma().update_many(
|
||||
where={"sessionId": session_id, "sequence": sequence},
|
||||
data={"content": sanitize_string(new_content)},
|
||||
)
|
||||
if result == 0:
|
||||
logger.warning(
|
||||
f"No message found to update for session {session_id}, sequence {sequence}"
|
||||
)
|
||||
return False
|
||||
if result > 1:
|
||||
# Defence-in-depth: (sessionId, sequence) is expected to identify
|
||||
# at most one message. If we ever hit this branch it indicates a
|
||||
# data integrity issue (non-unique sequence numbers within a
|
||||
# session) that silently corrupted multiple rows.
|
||||
logger.error(
|
||||
f"update_message_content_by_sequence touched {result} rows "
|
||||
f"for session {session_id}, sequence {sequence} — expected 1"
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Failed to update message for session {session_id}, sequence {sequence}: {e}"
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
async def set_turn_duration(session_id: str, duration_ms: int) -> None:
|
||||
"""Set durationMs on the last assistant message in a session.
|
||||
|
||||
|
||||
@@ -1,475 +1,7 @@
|
||||
"""Unit tests for copilot.db — paginated message queries."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from prisma.models import ChatMessage as PrismaChatMessage
|
||||
from prisma.models import ChatSession as PrismaChatSession
|
||||
|
||||
from backend.copilot.db import (
|
||||
PaginatedMessages,
|
||||
get_chat_messages_paginated,
|
||||
set_turn_duration,
|
||||
update_message_content_by_sequence,
|
||||
)
|
||||
from backend.copilot.model import ChatMessage as CopilotChatMessage
|
||||
from backend.copilot.model import ChatSession, get_chat_session, upsert_chat_session
|
||||
|
||||
|
||||
def _make_msg(
|
||||
sequence: int,
|
||||
role: str = "assistant",
|
||||
content: str | None = "hello",
|
||||
tool_calls: Any = None,
|
||||
) -> PrismaChatMessage:
|
||||
"""Build a minimal PrismaChatMessage for testing."""
|
||||
return PrismaChatMessage(
|
||||
id=f"msg-{sequence}",
|
||||
createdAt=datetime.now(UTC),
|
||||
sessionId="sess-1",
|
||||
role=role,
|
||||
content=content,
|
||||
sequence=sequence,
|
||||
toolCalls=tool_calls,
|
||||
name=None,
|
||||
toolCallId=None,
|
||||
refusal=None,
|
||||
functionCall=None,
|
||||
)
|
||||
|
||||
|
||||
def _make_session(
|
||||
session_id: str = "sess-1",
|
||||
user_id: str = "user-1",
|
||||
messages: list[PrismaChatMessage] | None = None,
|
||||
) -> PrismaChatSession:
|
||||
"""Build a minimal PrismaChatSession for testing."""
|
||||
now = datetime.now(UTC)
|
||||
session = PrismaChatSession.model_construct(
|
||||
id=session_id,
|
||||
createdAt=now,
|
||||
updatedAt=now,
|
||||
userId=user_id,
|
||||
credentials={},
|
||||
successfulAgentRuns={},
|
||||
successfulAgentSchedules={},
|
||||
totalPromptTokens=0,
|
||||
totalCompletionTokens=0,
|
||||
title=None,
|
||||
metadata={},
|
||||
Messages=messages or [],
|
||||
)
|
||||
return session
|
||||
|
||||
|
||||
SESSION_ID = "sess-1"
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def mock_db():
|
||||
"""Patch ChatSession.prisma().find_first and ChatMessage.prisma().find_many.
|
||||
|
||||
find_first is used for the main query (session + included messages).
|
||||
find_many is used only for boundary expansion queries.
|
||||
"""
|
||||
with (
|
||||
patch.object(PrismaChatSession, "prisma") as mock_session_prisma,
|
||||
patch.object(PrismaChatMessage, "prisma") as mock_msg_prisma,
|
||||
):
|
||||
find_first = AsyncMock()
|
||||
mock_session_prisma.return_value.find_first = find_first
|
||||
|
||||
find_many = AsyncMock(return_value=[])
|
||||
mock_msg_prisma.return_value.find_many = find_many
|
||||
|
||||
yield find_first, find_many
|
||||
|
||||
|
||||
# ---------- Basic pagination ----------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_basic_page_returns_messages_ascending(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""Messages are returned in ascending sequence order."""
|
||||
find_first, _ = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(3), _make_msg(2), _make_msg(1)],
|
||||
)
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=5)
|
||||
|
||||
assert isinstance(page, PaginatedMessages)
|
||||
assert [m.sequence for m in page.messages] == [1, 2, 3]
|
||||
assert page.has_more is False
|
||||
assert page.oldest_sequence == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_has_more_when_results_exceed_limit(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""has_more is True when DB returns more than limit items."""
|
||||
find_first, _ = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(3), _make_msg(2), _make_msg(1)],
|
||||
)
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=2)
|
||||
|
||||
assert page is not None
|
||||
assert page.has_more is True
|
||||
assert len(page.messages) == 2
|
||||
assert [m.sequence for m in page.messages] == [2, 3]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_session_returns_no_messages(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
find_first, _ = mock_db
|
||||
find_first.return_value = _make_session(messages=[])
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=50)
|
||||
|
||||
assert page is not None
|
||||
assert page.messages == []
|
||||
assert page.has_more is False
|
||||
assert page.oldest_sequence is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_before_sequence_filters_correctly(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""before_sequence is passed as a where filter inside the Messages include."""
|
||||
find_first, _ = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(2), _make_msg(1)],
|
||||
)
|
||||
|
||||
await get_chat_messages_paginated(SESSION_ID, limit=50, before_sequence=5)
|
||||
|
||||
call_kwargs = find_first.call_args
|
||||
include = call_kwargs.kwargs.get("include") or call_kwargs[1].get("include")
|
||||
assert include["Messages"]["where"] == {"sequence": {"lt": 5}}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_where_on_messages_without_before_sequence(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""Without before_sequence, the Messages include has no where clause."""
|
||||
find_first, _ = mock_db
|
||||
find_first.return_value = _make_session(messages=[_make_msg(1)])
|
||||
|
||||
await get_chat_messages_paginated(SESSION_ID, limit=50)
|
||||
|
||||
call_kwargs = find_first.call_args
|
||||
include = call_kwargs.kwargs.get("include") or call_kwargs[1].get("include")
|
||||
assert "where" not in include["Messages"]
|
||||
|
||||
|
||||
# ---------- Visibility guarantee ----------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_visibility_expands_when_all_tool_messages(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""When the entire page is tool messages, expand backward to find
|
||||
at least one visible (user/assistant) message so the chat isn't blank."""
|
||||
find_first, find_many = mock_db
|
||||
# Newest 3 messages are all tool messages (DESC → reversed to ASC)
|
||||
find_first.return_value = _make_session(
|
||||
messages=[
|
||||
_make_msg(12, role="tool"),
|
||||
_make_msg(11, role="tool"),
|
||||
_make_msg(10, role="tool"),
|
||||
],
|
||||
)
|
||||
# Boundary expansion finds the owning assistant first (boundary fix),
|
||||
# then visibility expansion finds a user message further back
|
||||
find_many.side_effect = [
|
||||
# First call: boundary fix (oldest msg is tool → find owner)
|
||||
[_make_msg(9, role="tool"), _make_msg(8, role="tool")],
|
||||
# Second call: visibility expansion (still all tool → find visible)
|
||||
[_make_msg(7, role="tool"), _make_msg(6, role="assistant")],
|
||||
]
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=3)
|
||||
|
||||
assert page is not None
|
||||
# Should include the expanded messages + original tool messages
|
||||
roles = [m.role for m in page.messages]
|
||||
assert "assistant" in roles or "user" in roles
|
||||
assert page.has_more is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_visibility_expansion_when_visible_messages_present(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""No visibility expansion needed when page already has visible messages."""
|
||||
find_first, find_many = mock_db
|
||||
# Page has an assistant message among tool messages
|
||||
find_first.return_value = _make_session(
|
||||
messages=[
|
||||
_make_msg(5, role="tool"),
|
||||
_make_msg(4, role="assistant"),
|
||||
_make_msg(3, role="user"),
|
||||
],
|
||||
)
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=3)
|
||||
|
||||
assert page is not None
|
||||
# Boundary expansion might fire (oldest is tool), but NOT visibility
|
||||
assert [m.sequence for m in page.messages][0] <= 3
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_visibility_no_expansion_when_no_earlier_messages(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""When the page is all tool messages but there are no earlier messages
|
||||
in the DB, visibility expansion returns early without changes."""
|
||||
find_first, find_many = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(1, role="tool"), _make_msg(0, role="tool")],
|
||||
)
|
||||
# Boundary expansion: no earlier messages
|
||||
# Visibility expansion: no earlier messages
|
||||
find_many.side_effect = [[], []]
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=2)
|
||||
|
||||
assert page is not None
|
||||
assert all(m.role == "tool" for m in page.messages)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_visibility_expansion_reaches_seq_zero(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""When visibility expansion finds a visible message at sequence 0,
|
||||
has_more should be False."""
|
||||
find_first, find_many = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(5, role="tool"), _make_msg(4, role="tool")],
|
||||
)
|
||||
find_many.side_effect = [
|
||||
# Boundary expansion
|
||||
[_make_msg(3, role="tool")],
|
||||
# Visibility expansion — finds user at seq 0
|
||||
[
|
||||
_make_msg(2, role="tool"),
|
||||
_make_msg(1, role="tool"),
|
||||
_make_msg(0, role="user"),
|
||||
],
|
||||
]
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=2)
|
||||
|
||||
assert page is not None
|
||||
assert page.messages[0].role == "user"
|
||||
assert page.messages[0].sequence == 0
|
||||
assert page.has_more is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_visibility_expansion_with_user_id(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""Visibility expansion passes user_id filter to the boundary query."""
|
||||
find_first, find_many = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(10, role="tool")],
|
||||
)
|
||||
find_many.side_effect = [
|
||||
# Boundary expansion
|
||||
[_make_msg(9, role="tool")],
|
||||
# Visibility expansion
|
||||
[_make_msg(8, role="assistant")],
|
||||
]
|
||||
|
||||
await get_chat_messages_paginated(SESSION_ID, limit=1, user_id="user-abc")
|
||||
|
||||
# Both find_many calls should include the user_id session filter
|
||||
for call in find_many.call_args_list:
|
||||
where = call.kwargs.get("where") or call[1].get("where")
|
||||
assert "Session" in where
|
||||
assert where["Session"] == {"is": {"userId": "user-abc"}}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_user_id_filter_applied_to_session_where(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""user_id adds a userId filter to the session-level where clause."""
|
||||
find_first, _ = mock_db
|
||||
find_first.return_value = _make_session(messages=[_make_msg(1)])
|
||||
|
||||
await get_chat_messages_paginated(SESSION_ID, limit=50, user_id="user-abc")
|
||||
|
||||
call_kwargs = find_first.call_args
|
||||
where = call_kwargs.kwargs.get("where") or call_kwargs[1].get("where")
|
||||
assert where["userId"] == "user-abc"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_session_not_found_returns_none(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""Returns None when session doesn't exist or user doesn't own it."""
|
||||
find_first, _ = mock_db
|
||||
find_first.return_value = None
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=50)
|
||||
|
||||
assert page is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_session_info_included_in_result(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""PaginatedMessages includes session metadata."""
|
||||
find_first, _ = mock_db
|
||||
find_first.return_value = _make_session(messages=[_make_msg(1)])
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=50)
|
||||
|
||||
assert page is not None
|
||||
assert page.session.session_id == SESSION_ID
|
||||
|
||||
|
||||
# ---------- Backward boundary expansion ----------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_boundary_expansion_includes_assistant(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""When page starts with a tool message, expand backward to include
|
||||
the owning assistant message."""
|
||||
find_first, find_many = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(5, role="tool"), _make_msg(4, role="tool")],
|
||||
)
|
||||
find_many.return_value = [_make_msg(3, role="assistant")]
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=5)
|
||||
|
||||
assert page is not None
|
||||
assert [m.sequence for m in page.messages] == [3, 4, 5]
|
||||
assert page.messages[0].role == "assistant"
|
||||
assert page.oldest_sequence == 3
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_boundary_expansion_includes_multiple_tool_msgs(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""Boundary expansion scans past consecutive tool messages to find
|
||||
the owning assistant."""
|
||||
find_first, find_many = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(7, role="tool")],
|
||||
)
|
||||
find_many.return_value = [
|
||||
_make_msg(6, role="tool"),
|
||||
_make_msg(5, role="tool"),
|
||||
_make_msg(4, role="assistant"),
|
||||
]
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=5)
|
||||
|
||||
assert page is not None
|
||||
assert [m.sequence for m in page.messages] == [4, 5, 6, 7]
|
||||
assert page.messages[0].role == "assistant"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_boundary_expansion_sets_has_more_when_not_at_start(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""After boundary expansion, has_more=True if expanded msgs aren't at seq 0."""
|
||||
find_first, find_many = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(3, role="tool")],
|
||||
)
|
||||
find_many.return_value = [_make_msg(2, role="assistant")]
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=5)
|
||||
|
||||
assert page is not None
|
||||
assert page.has_more is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_boundary_expansion_no_has_more_at_conversation_start(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""has_more stays False when boundary expansion reaches seq 0."""
|
||||
find_first, find_many = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(1, role="tool")],
|
||||
)
|
||||
find_many.return_value = [_make_msg(0, role="assistant")]
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=5)
|
||||
|
||||
assert page is not None
|
||||
assert page.has_more is False
|
||||
assert page.oldest_sequence == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_boundary_expansion_when_first_msg_not_tool(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""No boundary expansion when the first message is not a tool message."""
|
||||
find_first, find_many = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(3, role="user"), _make_msg(2, role="assistant")],
|
||||
)
|
||||
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=5)
|
||||
|
||||
assert page is not None
|
||||
assert find_many.call_count == 0
|
||||
assert [m.sequence for m in page.messages] == [2, 3]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_boundary_expansion_warns_when_no_owner_found(
|
||||
mock_db: tuple[AsyncMock, AsyncMock],
|
||||
):
|
||||
"""When boundary scan doesn't find a non-tool message, a warning is logged
|
||||
and the boundary messages are still included."""
|
||||
find_first, find_many = mock_db
|
||||
find_first.return_value = _make_session(
|
||||
messages=[_make_msg(10, role="tool")],
|
||||
)
|
||||
find_many.return_value = [_make_msg(i, role="tool") for i in range(9, -1, -1)]
|
||||
|
||||
with patch("backend.copilot.db.logger") as mock_logger:
|
||||
page = await get_chat_messages_paginated(SESSION_ID, limit=5)
|
||||
# Two warnings: boundary expansion + visibility expansion (all tool msgs)
|
||||
assert mock_logger.warning.call_count == 2
|
||||
|
||||
assert page is not None
|
||||
assert page.messages[0].role == "tool"
|
||||
assert len(page.messages) > 1
|
||||
|
||||
|
||||
# ---------- Turn duration (integration tests) ----------
|
||||
from .db import set_turn_duration
|
||||
from .model import ChatMessage, ChatSession, get_chat_session, upsert_chat_session
|
||||
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="session")
|
||||
@@ -483,8 +15,8 @@ async def test_set_turn_duration_updates_cache_in_place(setup_test_user, test_us
|
||||
"""
|
||||
session = ChatSession.new(user_id=test_user_id, dry_run=False)
|
||||
session.messages = [
|
||||
CopilotChatMessage(role="user", content="hello"),
|
||||
CopilotChatMessage(role="assistant", content="hi there"),
|
||||
ChatMessage(role="user", content="hello"),
|
||||
ChatMessage(role="assistant", content="hi there"),
|
||||
]
|
||||
session = await upsert_chat_session(session)
|
||||
|
||||
@@ -509,7 +41,7 @@ async def test_set_turn_duration_no_assistant_message(setup_test_user, test_user
|
||||
"""set_turn_duration is a no-op when there are no assistant messages."""
|
||||
session = ChatSession.new(user_id=test_user_id, dry_run=False)
|
||||
session.messages = [
|
||||
CopilotChatMessage(role="user", content="hello"),
|
||||
ChatMessage(role="user", content="hello"),
|
||||
]
|
||||
session = await upsert_chat_session(session)
|
||||
|
||||
@@ -520,91 +52,3 @@ async def test_set_turn_duration_no_assistant_message(setup_test_user, test_user
|
||||
assert cached is not None
|
||||
# User message should not have durationMs
|
||||
assert cached.messages[0].duration_ms is None
|
||||
|
||||
|
||||
# ---------- update_message_content_by_sequence ----------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_message_content_by_sequence_success():
|
||||
"""Returns True when update_many reports exactly one row updated."""
|
||||
with (
|
||||
patch.object(PrismaChatMessage, "prisma") as mock_prisma,
|
||||
patch("backend.copilot.db.sanitize_string", side_effect=lambda x: x),
|
||||
):
|
||||
mock_prisma.return_value.update_many = AsyncMock(return_value=1)
|
||||
|
||||
result = await update_message_content_by_sequence("sess-1", 0, "new content")
|
||||
|
||||
assert result is True
|
||||
mock_prisma.return_value.update_many.assert_called_once_with(
|
||||
where={"sessionId": "sess-1", "sequence": 0},
|
||||
data={"content": "new content"},
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_message_content_by_sequence_not_found():
|
||||
"""Returns False and logs a warning when no rows are updated."""
|
||||
with (
|
||||
patch.object(PrismaChatMessage, "prisma") as mock_prisma,
|
||||
patch("backend.copilot.db.logger") as mock_logger,
|
||||
):
|
||||
mock_prisma.return_value.update_many = AsyncMock(return_value=0)
|
||||
|
||||
result = await update_message_content_by_sequence("sess-1", 99, "content")
|
||||
|
||||
assert result is False
|
||||
mock_logger.warning.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_message_content_by_sequence_db_error():
|
||||
"""Returns False and logs an error when the DB raises an exception."""
|
||||
with (
|
||||
patch.object(PrismaChatMessage, "prisma") as mock_prisma,
|
||||
patch("backend.copilot.db.logger") as mock_logger,
|
||||
):
|
||||
mock_prisma.return_value.update_many = AsyncMock(
|
||||
side_effect=RuntimeError("db error")
|
||||
)
|
||||
|
||||
result = await update_message_content_by_sequence("sess-1", 0, "content")
|
||||
|
||||
assert result is False
|
||||
mock_logger.error.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_message_content_by_sequence_multi_row_logs_error():
|
||||
"""Returns True but logs an error when update_many touches more than one row."""
|
||||
with (
|
||||
patch.object(PrismaChatMessage, "prisma") as mock_prisma,
|
||||
patch("backend.copilot.db.logger") as mock_logger,
|
||||
):
|
||||
mock_prisma.return_value.update_many = AsyncMock(return_value=2)
|
||||
|
||||
result = await update_message_content_by_sequence("sess-1", 0, "content")
|
||||
|
||||
assert result is True
|
||||
mock_logger.error.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_message_content_by_sequence_sanitizes_content():
|
||||
"""Verifies sanitize_string is applied to content before the DB write."""
|
||||
with (
|
||||
patch.object(PrismaChatMessage, "prisma") as mock_prisma,
|
||||
patch(
|
||||
"backend.copilot.db.sanitize_string", return_value="sanitized"
|
||||
) as mock_sanitize,
|
||||
):
|
||||
mock_prisma.return_value.update_many = AsyncMock(return_value=1)
|
||||
|
||||
await update_message_content_by_sequence("sess-1", 0, "raw content")
|
||||
|
||||
mock_sanitize.assert_called_once_with("raw content")
|
||||
mock_prisma.return_value.update_many.assert_called_once_with(
|
||||
where={"sessionId": "sess-1", "sequence": 0},
|
||||
data={"content": "sanitized"},
|
||||
)
|
||||
|
||||
@@ -34,7 +34,6 @@ from .utils import (
|
||||
CancelCoPilotEvent,
|
||||
CoPilotExecutionEntry,
|
||||
create_copilot_queue_config,
|
||||
get_session_lock_key,
|
||||
)
|
||||
|
||||
logger = TruncatedLogger(logging.getLogger(__name__), prefix="[CoPilotExecutor]")
|
||||
@@ -367,7 +366,7 @@ class CoPilotExecutor(AppProcess):
|
||||
# Try to acquire cluster-wide lock
|
||||
cluster_lock = ClusterLock(
|
||||
redis=redis.get_redis(),
|
||||
key=get_session_lock_key(session_id),
|
||||
key=f"copilot:session:{session_id}:lock",
|
||||
owner_id=self.executor_id,
|
||||
timeout=settings.config.cluster_lock_timeout,
|
||||
)
|
||||
|
||||
@@ -151,8 +151,8 @@ class CoPilotProcessor:
|
||||
This method is called once per worker thread to set up the async event
|
||||
loop and initialize any required resources.
|
||||
|
||||
DB operations route through DatabaseManagerAsyncClient (RPC) via the
|
||||
db_accessors pattern — no direct Prisma connection is needed here.
|
||||
Database is accessed only through DatabaseManager, so we don't need to connect
|
||||
to Prisma directly.
|
||||
"""
|
||||
configure_logging()
|
||||
set_service_name("CoPilotExecutor")
|
||||
@@ -169,36 +169,18 @@ class CoPilotProcessor:
|
||||
|
||||
# Pre-warm the bundled CLI binary so the OS page-caches the ~185 MB
|
||||
# executable. First spawn pays ~1.2 s; subsequent spawns ~0.65 s.
|
||||
# Read cli_path directly from env here so _prewarm_cli does not have
|
||||
# to construct a ChatConfig() (which can raise and abort the worker).
|
||||
# Priority: CHAT_CLAUDE_AGENT_CLI_PATH (prefixed) first, then
|
||||
# CLAUDE_AGENT_CLI_PATH (unprefixed) — matches config.py's validator
|
||||
# order so both paths resolve to the same binary.
|
||||
cli_path = os.getenv("CHAT_CLAUDE_AGENT_CLI_PATH") or os.getenv(
|
||||
"CLAUDE_AGENT_CLI_PATH"
|
||||
)
|
||||
self._prewarm_cli(cli_path=cli_path or None)
|
||||
self._prewarm_cli()
|
||||
|
||||
logger.info(f"[CoPilotExecutor] Worker {self.tid} started")
|
||||
|
||||
def _prewarm_cli(self, cli_path: str | None = None) -> None:
|
||||
"""Run the Claude Code CLI binary once to warm OS page caches.
|
||||
|
||||
Accepts an explicit ``cli_path`` so the caller can pass the value
|
||||
already resolved at startup rather than constructing a full
|
||||
``ChatConfig()`` here (which reads env vars, runs validators, and
|
||||
can raise — aborting the worker prewarm silently). Falls back to
|
||||
the ``CLAUDE_AGENT_CLI_PATH`` / ``CHAT_CLAUDE_AGENT_CLI_PATH`` env
|
||||
vars (same precedence as ``ChatConfig``), and then to the SDK's
|
||||
bundled binary when neither is set.
|
||||
"""
|
||||
def _prewarm_cli(self) -> None:
|
||||
"""Run the bundled CLI binary once to warm OS page caches."""
|
||||
try:
|
||||
if not cli_path:
|
||||
from claude_agent_sdk._internal.transport.subprocess_cli import (
|
||||
SubprocessCLITransport,
|
||||
)
|
||||
from claude_agent_sdk._internal.transport.subprocess_cli import (
|
||||
SubprocessCLITransport,
|
||||
)
|
||||
|
||||
cli_path = SubprocessCLITransport._find_bundled_cli(None) # type: ignore[arg-type]
|
||||
cli_path = SubprocessCLITransport._find_bundled_cli(None) # type: ignore[arg-type]
|
||||
if cli_path:
|
||||
result = subprocess.run(
|
||||
[cli_path, "-v"],
|
||||
@@ -222,10 +204,6 @@ class CoPilotProcessor:
|
||||
Shuts down the workspace storage instance that belongs to this
|
||||
worker's event loop, ensuring ``aiohttp.ClientSession.close()``
|
||||
runs on the same loop that created the session.
|
||||
|
||||
Sub-AutoPilots are enqueued on the copilot_execution queue, so
|
||||
rolling deploys survive via RabbitMQ redelivery — no bespoke
|
||||
shutdown notifier needed.
|
||||
"""
|
||||
coro = shutdown_workspace_storage()
|
||||
try:
|
||||
@@ -346,9 +324,7 @@ class CoPilotProcessor:
|
||||
|
||||
# Stream chat completion and publish chunks to Redis.
|
||||
# stream_and_publish wraps the raw stream with registry
|
||||
# publishing so subscribers on the session Redis stream
|
||||
# (e.g. wait_for_session_result, SSE clients) receive the
|
||||
# same events as they are produced.
|
||||
# publishing (shared with collect_copilot_response).
|
||||
raw_stream = stream_fn(
|
||||
session_id=entry.session_id,
|
||||
message=entry.message if entry.message else None,
|
||||
@@ -357,38 +333,27 @@ class CoPilotProcessor:
|
||||
context=entry.context,
|
||||
file_ids=entry.file_ids,
|
||||
mode=effective_mode,
|
||||
model=entry.model,
|
||||
permissions=entry.permissions,
|
||||
request_arrival_at=entry.request_arrival_at,
|
||||
)
|
||||
published_stream = stream_registry.stream_and_publish(
|
||||
async for chunk in stream_registry.stream_and_publish(
|
||||
session_id=entry.session_id,
|
||||
turn_id=entry.turn_id,
|
||||
stream=raw_stream,
|
||||
)
|
||||
# Explicit aclose() on early exit: ``async for … break`` does
|
||||
# not close the generator, so GeneratorExit would never reach
|
||||
# stream_chat_completion_sdk, leaving its stream lock held
|
||||
# until GC eventually runs.
|
||||
try:
|
||||
async for chunk in published_stream:
|
||||
if cancel.is_set():
|
||||
log.info("Cancel requested, breaking stream")
|
||||
break
|
||||
):
|
||||
if cancel.is_set():
|
||||
log.info("Cancel requested, breaking stream")
|
||||
break
|
||||
|
||||
# Capture StreamError so mark_session_completed receives
|
||||
# the error message (stream_and_publish yields but does
|
||||
# not publish StreamError — that's done by mark_session_completed).
|
||||
if isinstance(chunk, StreamError):
|
||||
error_msg = chunk.errorText
|
||||
break
|
||||
# Capture StreamError so mark_session_completed receives
|
||||
# the error message (stream_and_publish yields but does
|
||||
# not publish StreamError — that's done by mark_session_completed).
|
||||
if isinstance(chunk, StreamError):
|
||||
error_msg = chunk.errorText
|
||||
break
|
||||
|
||||
current_time = time.monotonic()
|
||||
if current_time - last_refresh >= refresh_interval:
|
||||
cluster_lock.refresh()
|
||||
last_refresh = current_time
|
||||
finally:
|
||||
await published_stream.aclose()
|
||||
current_time = time.monotonic()
|
||||
if current_time - last_refresh >= refresh_interval:
|
||||
cluster_lock.refresh()
|
||||
last_refresh = current_time
|
||||
|
||||
# Stream loop completed
|
||||
if cancel.is_set():
|
||||
|
||||
@@ -10,18 +10,14 @@ the real production helpers from ``processor.py`` so the routing logic
|
||||
has meaningful coverage.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import threading
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.copilot.executor.processor import (
|
||||
CoPilotProcessor,
|
||||
resolve_effective_mode,
|
||||
resolve_use_sdk_for_mode,
|
||||
)
|
||||
from backend.copilot.executor.utils import CoPilotExecutionEntry, CoPilotLogMetadata
|
||||
|
||||
|
||||
class TestResolveUseSdkForMode:
|
||||
@@ -177,101 +173,3 @@ class TestResolveEffectiveMode:
|
||||
) as flag_mock:
|
||||
assert await resolve_effective_mode("fast", None) is None
|
||||
flag_mock.assert_awaited_once()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _execute_async aclose propagation
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class _TrackedStream:
|
||||
"""Minimal async-generator stand-in that records whether ``aclose``
|
||||
was called, so tests can verify the processor forces explicit cleanup
|
||||
of the published stream on every exit path (normal + break on cancel)."""
|
||||
|
||||
def __init__(self, events: list):
|
||||
self._events = events
|
||||
self.aclose_called = False
|
||||
|
||||
def __aiter__(self):
|
||||
return self
|
||||
|
||||
async def __anext__(self):
|
||||
if not self._events:
|
||||
raise StopAsyncIteration
|
||||
return self._events.pop(0)
|
||||
|
||||
async def aclose(self) -> None:
|
||||
self.aclose_called = True
|
||||
|
||||
|
||||
def _make_entry() -> CoPilotExecutionEntry:
|
||||
return CoPilotExecutionEntry(
|
||||
session_id="sess-1",
|
||||
turn_id="turn-1",
|
||||
user_id="user-1",
|
||||
message="hi",
|
||||
is_user_message=True,
|
||||
request_arrival_at=0.0,
|
||||
)
|
||||
|
||||
|
||||
def _make_log() -> CoPilotLogMetadata:
|
||||
return CoPilotLogMetadata(logger=logging.getLogger("test-copilot"))
|
||||
|
||||
|
||||
class TestExecuteAsyncAclose:
|
||||
"""``_execute_async`` must call ``aclose`` on the published stream both
|
||||
when the loop exits naturally and when ``cancel`` is set mid-stream —
|
||||
otherwise ``stream_chat_completion_sdk`` stays suspended and keeps
|
||||
holding the per-session Redis lock until GC."""
|
||||
|
||||
def _patches(self, published_stream: _TrackedStream):
|
||||
"""Shared mock context: patches every dependency ``_execute_async``
|
||||
touches so the aclose path is the only behaviour under test."""
|
||||
return [
|
||||
patch(
|
||||
"backend.copilot.executor.processor.ChatConfig",
|
||||
return_value=MagicMock(test_mode=True, use_claude_agent_sdk=True),
|
||||
),
|
||||
patch(
|
||||
"backend.copilot.executor.processor.stream_chat_completion_dummy",
|
||||
return_value=MagicMock(),
|
||||
),
|
||||
patch(
|
||||
"backend.copilot.executor.processor.stream_registry.stream_and_publish",
|
||||
return_value=published_stream,
|
||||
),
|
||||
patch(
|
||||
"backend.copilot.executor.processor.stream_registry.mark_session_completed",
|
||||
new=AsyncMock(),
|
||||
),
|
||||
]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_normal_exit_calls_aclose(self) -> None:
|
||||
published = _TrackedStream(events=[MagicMock(), MagicMock()])
|
||||
proc = CoPilotProcessor()
|
||||
cancel = threading.Event()
|
||||
cluster_lock = MagicMock()
|
||||
|
||||
patches = self._patches(published)
|
||||
with patches[0], patches[1], patches[2], patches[3]:
|
||||
await proc._execute_async(_make_entry(), cancel, cluster_lock, _make_log())
|
||||
|
||||
assert published.aclose_called is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cancel_break_calls_aclose(self) -> None:
|
||||
events = [MagicMock()] # first chunk delivered, then cancel fires
|
||||
published = _TrackedStream(events=events)
|
||||
proc = CoPilotProcessor()
|
||||
cancel = threading.Event()
|
||||
cancel.set() # pre-set so the loop breaks on the first chunk
|
||||
cluster_lock = MagicMock()
|
||||
|
||||
patches = self._patches(published)
|
||||
with patches[0], patches[1], patches[2], patches[3]:
|
||||
await proc._execute_async(_make_entry(), cancel, cluster_lock, _make_log())
|
||||
|
||||
assert published.aclose_called is True
|
||||
|
||||
@@ -9,8 +9,7 @@ import logging
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.copilot.config import CopilotLlmModel, CopilotMode
|
||||
from backend.copilot.permissions import CopilotPermissions
|
||||
from backend.copilot.config import CopilotMode
|
||||
from backend.data.rabbitmq import Exchange, ExchangeType, Queue, RabbitMQConfig
|
||||
from backend.util.logging import TruncatedLogger, is_structured_logging_enabled
|
||||
|
||||
@@ -82,12 +81,6 @@ COPILOT_CANCEL_EXCHANGE = Exchange(
|
||||
)
|
||||
COPILOT_CANCEL_QUEUE_NAME = "copilot_cancel_queue"
|
||||
|
||||
|
||||
def get_session_lock_key(session_id: str) -> str:
|
||||
"""Redis key for the per-session cluster lock held by the executing pod."""
|
||||
return f"copilot:session:{session_id}:lock"
|
||||
|
||||
|
||||
# CoPilot operations can include extended thinking and agent generation
|
||||
# which may take 30+ minutes to complete
|
||||
COPILOT_CONSUMER_TIMEOUT_SECONDS = 60 * 60 # 1 hour
|
||||
@@ -167,23 +160,6 @@ class CoPilotExecutionEntry(BaseModel):
|
||||
mode: CopilotMode | None = None
|
||||
"""Autopilot mode override: 'fast' or 'extended_thinking'. None = server default."""
|
||||
|
||||
model: CopilotLlmModel | None = None
|
||||
"""Per-request model tier: 'standard' or 'advanced'. None = server default."""
|
||||
|
||||
permissions: CopilotPermissions | None = None
|
||||
"""Capability filter inherited from a parent run (e.g. ``run_sub_session``
|
||||
forwards its parent's permissions so the sub can't escalate). ``None``
|
||||
means the worker applies no filter."""
|
||||
|
||||
request_arrival_at: float = 0.0
|
||||
"""Unix-epoch seconds (server clock) when the originating HTTP
|
||||
``/stream`` request arrived. The executor's turn-start drain uses
|
||||
this to decide whether each pending message was typed BEFORE or AFTER
|
||||
the turn's ``current`` message, and orders the combined user bubble
|
||||
chronologically. Defaults to ``0.0`` for backward compatibility with
|
||||
queue messages written before this field existed (they sort as "all
|
||||
pending before current" — the pre-fix behaviour)."""
|
||||
|
||||
|
||||
class CancelCoPilotEvent(BaseModel):
|
||||
"""Event to cancel a CoPilot operation."""
|
||||
@@ -204,9 +180,6 @@ async def enqueue_copilot_turn(
|
||||
context: dict[str, str] | None = None,
|
||||
file_ids: list[str] | None = None,
|
||||
mode: CopilotMode | None = None,
|
||||
model: CopilotLlmModel | None = None,
|
||||
permissions: CopilotPermissions | None = None,
|
||||
request_arrival_at: float = 0.0,
|
||||
) -> None:
|
||||
"""Enqueue a CoPilot task for processing by the executor service.
|
||||
|
||||
@@ -219,9 +192,6 @@ async def enqueue_copilot_turn(
|
||||
context: Optional context for the message (e.g., {url: str, content: str})
|
||||
file_ids: Optional workspace file IDs attached to the user's message
|
||||
mode: Autopilot mode override ('fast' or 'extended_thinking'). None = server default.
|
||||
model: Per-request model tier ('standard' or 'advanced'). None = server default.
|
||||
permissions: Capability filter inherited from a parent run (sub-AutoPilot).
|
||||
None = no filter.
|
||||
"""
|
||||
from backend.util.clients import get_async_copilot_queue
|
||||
|
||||
@@ -234,9 +204,6 @@ async def enqueue_copilot_turn(
|
||||
context=context,
|
||||
file_ids=file_ids,
|
||||
mode=mode,
|
||||
model=model,
|
||||
permissions=permissions,
|
||||
request_arrival_at=request_arrival_at,
|
||||
)
|
||||
|
||||
queue_client = await get_async_copilot_queue()
|
||||
|
||||
@@ -1,197 +0,0 @@
|
||||
# Graphiti Memory
|
||||
|
||||
This directory contains the Graphiti-backed memory integration for CoPilot.
|
||||
This file is developer documentation only — it is NOT injected into LLM prompts.
|
||||
Runtime prompt instructions live in `prompting.py:get_graphiti_supplement()`.
|
||||
|
||||
## Scope
|
||||
|
||||
- Keep Graphiti and FalkorDB-specific logic in this package.
|
||||
- Prefer changes here over scattering Graphiti behavior across unrelated copilot modules.
|
||||
|
||||
## Debugging
|
||||
|
||||
- Use raw FalkorDB queries to inspect stored nodes, episodes, and `RELATES_TO` facts before changing retrieval behavior.
|
||||
- Distinguish user-provided facts, assistant-generated findings, and provenance/meta entities when evaluating memory quality.
|
||||
|
||||
## Design Intent
|
||||
|
||||
- Preserve per-user isolation through `group_id`-scoped databases and clients.
|
||||
- Be careful about memory pollution from assistant/tool phrasing; extraction quality matters as much as ingestion success.
|
||||
- Keep warm-context and tool-driven recall resilient: failures should degrade gracefully rather than break chat execution.
|
||||
|
||||
## Query Cookbook
|
||||
|
||||
Run everything from `autogpt_platform/backend` and use `poetry run ...`.
|
||||
|
||||
Get the `group_id` for a user:
|
||||
|
||||
```bash
|
||||
poetry run python - <<'PY'
|
||||
from backend.copilot.graphiti.client import derive_group_id
|
||||
print(derive_group_id("883cc9da-fe37-4863-839b-acba022bf3ef"))
|
||||
PY
|
||||
```
|
||||
|
||||
Inspect graph counts:
|
||||
|
||||
```bash
|
||||
poetry run python - <<'PY'
|
||||
import asyncio
|
||||
from backend.copilot.graphiti.client import derive_group_id
|
||||
from backend.copilot.graphiti.config import graphiti_config
|
||||
from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
|
||||
|
||||
USER_ID = "883cc9da-fe37-4863-839b-acba022bf3ef"
|
||||
GROUP_ID = derive_group_id(USER_ID)
|
||||
|
||||
QUERIES = {
|
||||
"entities": "MATCH (n:Entity) RETURN count(n) AS count",
|
||||
"episodes": "MATCH (n:Episodic) RETURN count(n) AS count",
|
||||
"communities": "MATCH (n:Community) RETURN count(n) AS count",
|
||||
"relates_to_edges": "MATCH ()-[e:RELATES_TO]->() RETURN count(e) AS count",
|
||||
}
|
||||
|
||||
async def run():
|
||||
driver = AutoGPTFalkorDriver(
|
||||
host=graphiti_config.falkordb_host,
|
||||
port=graphiti_config.falkordb_port,
|
||||
password=graphiti_config.falkordb_password or None,
|
||||
database=GROUP_ID,
|
||||
)
|
||||
try:
|
||||
for name, query in QUERIES.items():
|
||||
records, _, _ = await driver.execute_query(query)
|
||||
print(name, records[0]["count"])
|
||||
finally:
|
||||
await driver.close()
|
||||
|
||||
asyncio.run(run())
|
||||
PY
|
||||
```
|
||||
|
||||
List entities or relation-name counts:
|
||||
|
||||
```bash
|
||||
poetry run python - <<'PY'
|
||||
import asyncio
|
||||
from backend.copilot.graphiti.client import derive_group_id
|
||||
from backend.copilot.graphiti.config import graphiti_config
|
||||
from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
|
||||
|
||||
USER_ID = "883cc9da-fe37-4863-839b-acba022bf3ef"
|
||||
GROUP_ID = derive_group_id(USER_ID)
|
||||
|
||||
async def run():
|
||||
driver = AutoGPTFalkorDriver(
|
||||
host=graphiti_config.falkordb_host,
|
||||
port=graphiti_config.falkordb_port,
|
||||
password=graphiti_config.falkordb_password or None,
|
||||
database=GROUP_ID,
|
||||
)
|
||||
try:
|
||||
records, _, _ = await driver.execute_query(
|
||||
"MATCH (n:Entity) RETURN n.name AS name, n.summary AS summary ORDER BY n.name"
|
||||
)
|
||||
print("## entities")
|
||||
for row in records:
|
||||
print(row)
|
||||
|
||||
records, _, _ = await driver.execute_query(
|
||||
"""
|
||||
MATCH ()-[e:RELATES_TO]->()
|
||||
RETURN e.name AS relation, count(e) AS count
|
||||
ORDER BY count DESC, relation
|
||||
"""
|
||||
)
|
||||
print("\\n## relation_counts")
|
||||
for row in records:
|
||||
print(row)
|
||||
finally:
|
||||
await driver.close()
|
||||
|
||||
asyncio.run(run())
|
||||
PY
|
||||
```
|
||||
|
||||
Inspect facts around one node:
|
||||
|
||||
```bash
|
||||
poetry run python - <<'PY'
|
||||
import asyncio
|
||||
from backend.copilot.graphiti.client import derive_group_id
|
||||
from backend.copilot.graphiti.config import graphiti_config
|
||||
from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
|
||||
|
||||
USER_ID = "883cc9da-fe37-4863-839b-acba022bf3ef"
|
||||
GROUP_ID = derive_group_id(USER_ID)
|
||||
TARGET = "sarah"
|
||||
|
||||
async def run():
|
||||
driver = AutoGPTFalkorDriver(
|
||||
host=graphiti_config.falkordb_host,
|
||||
port=graphiti_config.falkordb_port,
|
||||
password=graphiti_config.falkordb_password or None,
|
||||
database=GROUP_ID,
|
||||
)
|
||||
try:
|
||||
records, _, _ = await driver.execute_query(
|
||||
"""
|
||||
MATCH (a)-[e:RELATES_TO]->(b)
|
||||
WHERE (exists(a.name) AND toLower(a.name) = $target)
|
||||
OR (exists(b.name) AND toLower(b.name) = $target)
|
||||
RETURN a.name AS source, e.name AS relation, e.fact AS fact, b.name AS target
|
||||
ORDER BY e.created_at
|
||||
""",
|
||||
target=TARGET,
|
||||
)
|
||||
for row in records:
|
||||
print(row)
|
||||
finally:
|
||||
await driver.close()
|
||||
|
||||
asyncio.run(run())
|
||||
PY
|
||||
```
|
||||
|
||||
Inspect all chat messages for a user:
|
||||
|
||||
```bash
|
||||
poetry run python - <<'PY'
|
||||
import asyncio
|
||||
from prisma import Prisma
|
||||
|
||||
USER_ID = "883cc9da-fe37-4863-839b-acba022bf3ef"
|
||||
|
||||
async def run():
|
||||
db = Prisma()
|
||||
await db.connect()
|
||||
try:
|
||||
rows = await db.query_raw(
|
||||
'''
|
||||
select cm."sessionId" as session_id,
|
||||
cm.sequence,
|
||||
cm.role,
|
||||
left(cm.content, 260) as content,
|
||||
cm."createdAt" as created_at
|
||||
from "ChatMessage" cm
|
||||
join "ChatSession" cs on cs.id = cm."sessionId"
|
||||
where cs."userId" = $1
|
||||
order by cm."createdAt", cm.sequence
|
||||
''',
|
||||
USER_ID,
|
||||
)
|
||||
for row in rows:
|
||||
print(row)
|
||||
finally:
|
||||
await db.disconnect()
|
||||
|
||||
asyncio.run(run())
|
||||
PY
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
- `RELATES_TO` edges hold semantic facts. Inspect `e.name` and `e.fact`.
|
||||
- `MENTIONS` edges are provenance from episodes to extracted nodes.
|
||||
- Prefer directed queries `->` when checking for duplicates; undirected matches double-count mirrored edges.
|
||||
@@ -1 +0,0 @@
|
||||
@AGENTS.md
|
||||
@@ -1 +0,0 @@
|
||||
"""Graphiti temporal knowledge graph memory for AutoPilot."""
|
||||
@@ -1,43 +0,0 @@
|
||||
"""Shared attribute-resolution helpers for Graphiti edge/episode objects.
|
||||
|
||||
graphiti-core edge and episode objects have varying attribute names across
|
||||
versions. These helpers centralise the fallback chains so there's one place
|
||||
to update when upstream changes an attribute name.
|
||||
"""
|
||||
|
||||
|
||||
def extract_fact(edge) -> str:
|
||||
"""Extract the human-readable fact from an edge object."""
|
||||
return getattr(edge, "fact", None) or getattr(edge, "name", "") or ""
|
||||
|
||||
|
||||
def extract_temporal_validity(edge) -> tuple[str, str]:
|
||||
"""Return ``(valid_from, valid_to)`` for an edge."""
|
||||
valid_from = getattr(edge, "valid_at", None) or "unknown"
|
||||
valid_to = getattr(edge, "invalid_at", None) or "present"
|
||||
return str(valid_from), str(valid_to)
|
||||
|
||||
|
||||
def extract_episode_body_raw(episode) -> str:
|
||||
"""Extract the full body text from an episode object (no truncation).
|
||||
|
||||
Use this when the body needs to be parsed as JSON (e.g. scope filtering
|
||||
on MemoryEnvelope payloads). For display purposes, use
|
||||
``extract_episode_body()`` which truncates.
|
||||
"""
|
||||
return str(
|
||||
getattr(episode, "content", None)
|
||||
or getattr(episode, "body", None)
|
||||
or getattr(episode, "episode_body", None)
|
||||
or ""
|
||||
)
|
||||
|
||||
|
||||
def extract_episode_body(episode, max_len: int = 500) -> str:
|
||||
"""Extract the body text from an episode object, truncated to *max_len*."""
|
||||
return extract_episode_body_raw(episode)[:max_len]
|
||||
|
||||
|
||||
def extract_episode_timestamp(episode) -> str:
|
||||
"""Extract the created_at timestamp from an episode object."""
|
||||
return str(getattr(episode, "created_at", None) or "")
|
||||
@@ -1,90 +0,0 @@
|
||||
"""Tests for shared attribute-resolution helpers."""
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
from backend.copilot.graphiti._format import (
|
||||
extract_episode_body,
|
||||
extract_episode_timestamp,
|
||||
extract_fact,
|
||||
extract_temporal_validity,
|
||||
)
|
||||
|
||||
|
||||
def test_extract_fact_prefers_fact_attribute() -> None:
|
||||
edge = SimpleNamespace(fact="user likes python", name="preference")
|
||||
assert extract_fact(edge) == "user likes python"
|
||||
|
||||
|
||||
def test_extract_fact_falls_back_to_name() -> None:
|
||||
edge = SimpleNamespace(name="preference")
|
||||
assert extract_fact(edge) == "preference"
|
||||
|
||||
|
||||
def test_extract_fact_handles_none_fact() -> None:
|
||||
edge = SimpleNamespace(fact=None, name="fallback")
|
||||
assert extract_fact(edge) == "fallback"
|
||||
|
||||
|
||||
def test_extract_fact_handles_missing_both() -> None:
|
||||
edge = SimpleNamespace()
|
||||
assert extract_fact(edge) == ""
|
||||
|
||||
|
||||
def test_extract_temporal_validity_with_values() -> None:
|
||||
edge = SimpleNamespace(valid_at="2025-01-01", invalid_at="2025-12-31")
|
||||
assert extract_temporal_validity(edge) == ("2025-01-01", "2025-12-31")
|
||||
|
||||
|
||||
def test_extract_temporal_validity_defaults() -> None:
|
||||
edge = SimpleNamespace()
|
||||
assert extract_temporal_validity(edge) == ("unknown", "present")
|
||||
|
||||
|
||||
def test_extract_temporal_validity_none_values() -> None:
|
||||
edge = SimpleNamespace(valid_at=None, invalid_at=None)
|
||||
assert extract_temporal_validity(edge) == ("unknown", "present")
|
||||
|
||||
|
||||
def test_extract_episode_body_prefers_content() -> None:
|
||||
ep = SimpleNamespace(content="hello world", body="alt", episode_body="alt2")
|
||||
assert extract_episode_body(ep) == "hello world"
|
||||
|
||||
|
||||
def test_extract_episode_body_falls_back_to_body() -> None:
|
||||
ep = SimpleNamespace(body="fallback body")
|
||||
assert extract_episode_body(ep) == "fallback body"
|
||||
|
||||
|
||||
def test_extract_episode_body_falls_back_to_episode_body() -> None:
|
||||
ep = SimpleNamespace(episode_body="last resort")
|
||||
assert extract_episode_body(ep) == "last resort"
|
||||
|
||||
|
||||
def test_extract_episode_body_handles_none_all() -> None:
|
||||
ep = SimpleNamespace(content=None, body=None, episode_body=None)
|
||||
assert extract_episode_body(ep) == ""
|
||||
|
||||
|
||||
def test_extract_episode_body_truncates() -> None:
|
||||
ep = SimpleNamespace(content="x" * 1000)
|
||||
assert len(extract_episode_body(ep)) == 500
|
||||
|
||||
|
||||
def test_extract_episode_body_custom_max_len() -> None:
|
||||
ep = SimpleNamespace(content="x" * 100)
|
||||
assert len(extract_episode_body(ep, max_len=10)) == 10
|
||||
|
||||
|
||||
def test_extract_episode_timestamp_with_value() -> None:
|
||||
ep = SimpleNamespace(created_at="2025-01-01T00:00:00Z")
|
||||
assert extract_episode_timestamp(ep) == "2025-01-01T00:00:00Z"
|
||||
|
||||
|
||||
def test_extract_episode_timestamp_missing() -> None:
|
||||
ep = SimpleNamespace()
|
||||
assert extract_episode_timestamp(ep) == ""
|
||||
|
||||
|
||||
def test_extract_episode_timestamp_none() -> None:
|
||||
ep = SimpleNamespace(created_at=None)
|
||||
assert extract_episode_timestamp(ep) == ""
|
||||
@@ -1,193 +0,0 @@
|
||||
"""Graphiti client management with per-group_id isolation and LRU caching."""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
import weakref
|
||||
|
||||
from cachetools import TTLCache
|
||||
|
||||
from .config import graphiti_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_GROUP_ID_PATTERN = re.compile(r"^[a-zA-Z0-9_-]+$")
|
||||
_MAX_GROUP_ID_LEN = 128
|
||||
|
||||
|
||||
# Graphiti clients wrap redis.asyncio connections whose internal Futures are
|
||||
# pinned to the event loop they were first used on. The CoPilot executor runs
|
||||
# one asyncio loop per worker thread, so a process-wide client cache would
|
||||
# hand a loop-1-bound connection to a task running on loop 2 → RuntimeError
|
||||
# "got Future attached to a different loop". Scope the cache (and its lock)
|
||||
# per running loop so each loop gets its own clients.
|
||||
class _LoopState:
|
||||
__slots__ = ("cache", "lock")
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.cache: TTLCache = _EvictingTTLCache(
|
||||
maxsize=graphiti_config.client_cache_maxsize,
|
||||
ttl=graphiti_config.client_cache_ttl,
|
||||
)
|
||||
self.lock = asyncio.Lock()
|
||||
|
||||
|
||||
_loop_state: "weakref.WeakKeyDictionary[asyncio.AbstractEventLoop, _LoopState]" = (
|
||||
weakref.WeakKeyDictionary()
|
||||
)
|
||||
|
||||
|
||||
def _get_loop_state() -> _LoopState:
|
||||
loop = asyncio.get_running_loop()
|
||||
state = _loop_state.get(loop)
|
||||
if state is None:
|
||||
state = _LoopState()
|
||||
_loop_state[loop] = state
|
||||
return state
|
||||
|
||||
|
||||
def derive_group_id(user_id: str) -> str:
|
||||
"""Derive a deterministic, injection-safe group_id from a user_id.
|
||||
|
||||
Strips to ``[a-zA-Z0-9_-]``, enforces max length, and prefixes with
|
||||
``user_``. Raises if sanitization changed the input.
|
||||
"""
|
||||
if not user_id:
|
||||
raise ValueError("user_id must be non-empty to derive group_id")
|
||||
|
||||
safe_id = re.sub(r"[^a-zA-Z0-9_-]", "", user_id)[:_MAX_GROUP_ID_LEN]
|
||||
if not safe_id:
|
||||
raise ValueError(
|
||||
f"user_id '{user_id[:32]}...' yields empty group_id after sanitization"
|
||||
)
|
||||
|
||||
if safe_id != user_id:
|
||||
raise ValueError(
|
||||
f"user_id contains invalid characters for group_id derivation "
|
||||
f"(original length={len(user_id)}, sanitized='{safe_id[:32]}'). "
|
||||
f"Only [a-zA-Z0-9_-] are allowed."
|
||||
)
|
||||
|
||||
group_id = f"user_{safe_id}"
|
||||
if not _GROUP_ID_PATTERN.match(group_id):
|
||||
raise ValueError(f"Generated group_id '{group_id}' fails validation")
|
||||
|
||||
return group_id
|
||||
|
||||
|
||||
def _close_client_driver(client) -> None:
|
||||
"""Best-effort close of a Graphiti client's graph driver.
|
||||
|
||||
Called on cache eviction (TTL expiry or manual pop) to prevent
|
||||
leaked FalkorDB connections. Runs the async ``driver.close()``
|
||||
in a fire-and-forget task if an event loop is running, otherwise
|
||||
logs and moves on.
|
||||
"""
|
||||
driver = getattr(client, "graph_driver", None) or getattr(client, "driver", None)
|
||||
if driver is None or not hasattr(driver, "close"):
|
||||
return
|
||||
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
loop.create_task(driver.close())
|
||||
except RuntimeError:
|
||||
logger.debug("No running event loop — skipping driver.close() on eviction")
|
||||
|
||||
|
||||
class _EvictingTTLCache(TTLCache):
|
||||
"""TTLCache that closes Graphiti drivers on TTL expiry and capacity eviction.
|
||||
|
||||
Overrides ``expire()`` (not ``__delitem__``) per cachetools maintainer
|
||||
guidance — ``expire()`` is the only hook that fires for TTL-expired items
|
||||
since the internal expiry path uses ``Cache.__delitem__`` directly,
|
||||
bypassing subclass overrides. ``popitem()`` handles capacity eviction.
|
||||
See https://github.com/tkem/cachetools/issues/205.
|
||||
"""
|
||||
|
||||
def expire(self, time=None):
|
||||
expired = super().expire(time)
|
||||
for _key, client in expired:
|
||||
_close_client_driver(client)
|
||||
return expired
|
||||
|
||||
def popitem(self):
|
||||
key, client = super().popitem()
|
||||
_close_client_driver(client)
|
||||
return key, client
|
||||
|
||||
|
||||
def _get_cache() -> TTLCache:
|
||||
"""Return the client cache for the current running event loop."""
|
||||
return _get_loop_state().cache
|
||||
|
||||
|
||||
async def get_graphiti_client(group_id: str):
|
||||
"""Return a Graphiti client scoped to the given group_id.
|
||||
|
||||
Each group_id gets its own ``Graphiti`` instance to prevent the
|
||||
``self.driver`` mutation race condition when different groups are
|
||||
accessed concurrently. Instances are cached with a TTL to bound
|
||||
memory usage.
|
||||
|
||||
Returns a ``graphiti_core.Graphiti`` instance.
|
||||
"""
|
||||
from graphiti_core import Graphiti
|
||||
from graphiti_core.embedder import OpenAIEmbedder, OpenAIEmbedderConfig
|
||||
from graphiti_core.llm_client import LLMConfig, OpenAIClient
|
||||
|
||||
from .falkordb_driver import AutoGPTFalkorDriver
|
||||
|
||||
state = _get_loop_state()
|
||||
cache = state.cache
|
||||
|
||||
async with state.lock:
|
||||
if group_id in cache:
|
||||
return cache[group_id]
|
||||
|
||||
llm_config = LLMConfig(
|
||||
api_key=graphiti_config.resolve_llm_api_key(),
|
||||
model=graphiti_config.llm_model,
|
||||
small_model=graphiti_config.llm_model, # avoid gpt-4.1-nano dedup hallucination (#760)
|
||||
base_url=graphiti_config.resolve_llm_base_url(),
|
||||
)
|
||||
llm_client = OpenAIClient(config=llm_config)
|
||||
|
||||
embedder_config = OpenAIEmbedderConfig(
|
||||
api_key=graphiti_config.resolve_embedder_api_key(),
|
||||
embedding_model=graphiti_config.embedder_model,
|
||||
base_url=graphiti_config.resolve_embedder_base_url(),
|
||||
)
|
||||
embedder = OpenAIEmbedder(config=embedder_config)
|
||||
|
||||
graph_driver = AutoGPTFalkorDriver(
|
||||
host=graphiti_config.falkordb_host,
|
||||
port=graphiti_config.falkordb_port,
|
||||
password=graphiti_config.falkordb_password or None,
|
||||
database=group_id,
|
||||
)
|
||||
client = Graphiti(
|
||||
llm_client=llm_client,
|
||||
embedder=embedder,
|
||||
graph_driver=graph_driver,
|
||||
max_coroutines=graphiti_config.semaphore_limit,
|
||||
)
|
||||
|
||||
cache[group_id] = client
|
||||
return client
|
||||
|
||||
|
||||
async def evict_client(group_id: str) -> None:
|
||||
"""Remove a cached client and close its driver connection."""
|
||||
cache = _get_cache()
|
||||
# pop() may return None for expired or missing keys.
|
||||
# _EvictingTTLCache.expire() handles TTL-expired cleanup separately.
|
||||
client = cache.pop(group_id, None)
|
||||
if client is not None:
|
||||
driver = getattr(client, "graph_driver", None) or getattr(
|
||||
client, "driver", None
|
||||
)
|
||||
if driver and hasattr(driver, "close"):
|
||||
try:
|
||||
await driver.close()
|
||||
except Exception:
|
||||
logger.debug("Failed to close driver for %s", group_id, exc_info=True)
|
||||
@@ -1,38 +0,0 @@
|
||||
"""Tests for Graphiti client management — derive_group_id and evict_client."""
|
||||
|
||||
import pytest
|
||||
|
||||
from .client import derive_group_id, evict_client
|
||||
|
||||
|
||||
class TestDeriveGroupId:
|
||||
def test_empty_user_id_raises(self) -> None:
|
||||
with pytest.raises(ValueError, match="non-empty"):
|
||||
derive_group_id("")
|
||||
|
||||
def test_all_invalid_chars_raises(self) -> None:
|
||||
with pytest.raises(ValueError, match="empty group_id after sanitization"):
|
||||
derive_group_id("!!!")
|
||||
|
||||
def test_user_id_with_stripped_chars_raises(self) -> None:
|
||||
with pytest.raises(ValueError, match="invalid characters"):
|
||||
derive_group_id("abc.def")
|
||||
|
||||
def test_valid_uuid_passthrough(self) -> None:
|
||||
uid = "883cc9da-fe37-4863-839b-acba022bf3ef"
|
||||
result = derive_group_id(uid)
|
||||
assert result == f"user_{uid}"
|
||||
|
||||
def test_simple_alphanumeric_id(self) -> None:
|
||||
result = derive_group_id("user123")
|
||||
assert result == "user_user123"
|
||||
|
||||
def test_hyphens_and_underscores_allowed(self) -> None:
|
||||
result = derive_group_id("a-b_c")
|
||||
assert result == "user_a-b_c"
|
||||
|
||||
|
||||
class TestEvictClient:
|
||||
@pytest.mark.asyncio
|
||||
async def test_evict_nonexistent_group_id_does_not_raise(self) -> None:
|
||||
await evict_client("no-such-group-id")
|
||||
@@ -1,159 +0,0 @@
|
||||
"""Configuration for Graphiti temporal knowledge graph integration."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import Field
|
||||
from pydantic_settings import (
|
||||
BaseSettings,
|
||||
DotEnvSettingsSource,
|
||||
PydanticBaseSettingsSource,
|
||||
SettingsConfigDict,
|
||||
)
|
||||
|
||||
from backend.util.clients import OPENROUTER_BASE_URL
|
||||
|
||||
_BACKEND_ROOT = Path(__file__).resolve().parents[3]
|
||||
|
||||
|
||||
class GraphitiConfig(BaseSettings):
|
||||
"""Configuration for Graphiti memory integration.
|
||||
|
||||
All fields use the ``GRAPHITI_`` env-var prefix, e.g. ``GRAPHITI_ENABLED``.
|
||||
LLM/embedder keys fall back to the AutoPilot-dedicated keys
|
||||
(``CHAT_API_KEY`` / ``CHAT_OPENAI_API_KEY``) so that memory costs are
|
||||
tracked under AutoPilot, then to the platform-wide OpenRouter / OpenAI
|
||||
keys as a last resort.
|
||||
"""
|
||||
|
||||
model_config = SettingsConfigDict(env_prefix="GRAPHITI_", extra="allow")
|
||||
|
||||
# FalkorDB connection
|
||||
falkordb_host: str = Field(default="localhost")
|
||||
falkordb_port: int = Field(default=6380)
|
||||
falkordb_password: str = Field(default="")
|
||||
|
||||
# LLM for entity extraction (used by graphiti-core during ingestion)
|
||||
llm_model: str = Field(
|
||||
default="gpt-4.1-mini",
|
||||
description="Model for entity extraction — must support structured output",
|
||||
)
|
||||
llm_base_url: str = Field(
|
||||
default="",
|
||||
description="Base URL for LLM API — empty falls back to OPENROUTER_BASE_URL",
|
||||
)
|
||||
llm_api_key: str = Field(
|
||||
default="",
|
||||
description="API key for LLM — empty falls back to CHAT_API_KEY, then OPEN_ROUTER_API_KEY",
|
||||
)
|
||||
|
||||
# Embedder (separate from LLM — embeddings go direct to OpenAI)
|
||||
embedder_model: str = Field(default="text-embedding-3-small")
|
||||
embedder_base_url: str = Field(
|
||||
default="",
|
||||
description="Base URL for embedder — empty uses OpenAI direct",
|
||||
)
|
||||
embedder_api_key: str = Field(
|
||||
default="",
|
||||
description="API key for embedder — empty falls back to CHAT_OPENAI_API_KEY, then OPENAI_API_KEY",
|
||||
)
|
||||
|
||||
# Concurrency
|
||||
semaphore_limit: int = Field(
|
||||
default=5,
|
||||
description="Max concurrent LLM calls during ingestion (prevents rate limits)",
|
||||
)
|
||||
|
||||
# Warm context
|
||||
context_max_facts: int = Field(default=20)
|
||||
context_timeout: float = Field(
|
||||
default=8.0,
|
||||
description="Seconds before warm context fetch is abandoned (needs headroom for FalkorDB cold connections)",
|
||||
)
|
||||
|
||||
# Client cache
|
||||
client_cache_maxsize: int = Field(default=500)
|
||||
client_cache_ttl: int = Field(
|
||||
default=1800,
|
||||
description="TTL in seconds for cached Graphiti client instances (30 min)",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def settings_customise_sources(
|
||||
cls,
|
||||
settings_cls: type[BaseSettings],
|
||||
init_settings: PydanticBaseSettingsSource,
|
||||
env_settings: PydanticBaseSettingsSource,
|
||||
dotenv_settings: PydanticBaseSettingsSource,
|
||||
file_secret_settings: PydanticBaseSettingsSource,
|
||||
) -> tuple[PydanticBaseSettingsSource, ...]:
|
||||
return (
|
||||
init_settings,
|
||||
env_settings,
|
||||
file_secret_settings,
|
||||
DotEnvSettingsSource(settings_cls, env_file=_BACKEND_ROOT / ".env"),
|
||||
DotEnvSettingsSource(settings_cls, env_file=_BACKEND_ROOT / ".env.default"),
|
||||
)
|
||||
|
||||
def resolve_llm_api_key(self) -> str:
|
||||
if self.llm_api_key:
|
||||
return self.llm_api_key
|
||||
# Prefer the AutoPilot-dedicated key so memory costs are tracked
|
||||
# separately from the platform-wide OpenRouter key.
|
||||
return os.getenv("CHAT_API_KEY") or os.getenv("OPEN_ROUTER_API_KEY", "")
|
||||
|
||||
def resolve_llm_base_url(self) -> str:
|
||||
if self.llm_base_url:
|
||||
return self.llm_base_url
|
||||
return OPENROUTER_BASE_URL
|
||||
|
||||
def resolve_embedder_api_key(self) -> str:
|
||||
if self.embedder_api_key:
|
||||
return self.embedder_api_key
|
||||
# Prefer the AutoPilot-dedicated OpenAI key so memory costs are
|
||||
# tracked separately from the platform-wide OpenAI key.
|
||||
return os.getenv("CHAT_OPENAI_API_KEY") or os.getenv("OPENAI_API_KEY", "")
|
||||
|
||||
def resolve_embedder_base_url(self) -> str | None:
|
||||
if self.embedder_base_url:
|
||||
return self.embedder_base_url
|
||||
return None # OpenAI SDK default
|
||||
|
||||
|
||||
_graphiti_config: GraphitiConfig | None = None
|
||||
|
||||
|
||||
def _get_config() -> GraphitiConfig:
|
||||
global _graphiti_config
|
||||
if _graphiti_config is None:
|
||||
_graphiti_config = GraphitiConfig()
|
||||
return _graphiti_config
|
||||
|
||||
|
||||
# Backwards-compatible module-level attribute access.
|
||||
# All internal code should use ``_get_config()`` to avoid import-time
|
||||
# construction, but this keeps existing ``graphiti_config.xxx`` usage working.
|
||||
class _LazyConfigProxy:
|
||||
def __getattr__(self, name: str):
|
||||
return getattr(_get_config(), name)
|
||||
|
||||
|
||||
graphiti_config = _LazyConfigProxy() # type: ignore[assignment]
|
||||
|
||||
|
||||
async def is_enabled_for_user(user_id: str | None) -> bool:
|
||||
"""Check if Graphiti memory is enabled for a specific user.
|
||||
|
||||
Gated solely by LaunchDarkly flag ``graphiti-memory``
|
||||
(Flag.GRAPHITI_MEMORY). When LD is not configured, defaults to False.
|
||||
"""
|
||||
if not user_id:
|
||||
return False
|
||||
|
||||
from backend.util.feature_flag import Flag, is_feature_enabled
|
||||
|
||||
return await is_feature_enabled(
|
||||
Flag.GRAPHITI_MEMORY,
|
||||
user_id,
|
||||
default=False,
|
||||
)
|
||||
@@ -1,121 +0,0 @@
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from .config import GraphitiConfig, is_enabled_for_user
|
||||
|
||||
_ENV_VARS_TO_CLEAR = (
|
||||
"GRAPHITI_FALKORDB_HOST",
|
||||
"GRAPHITI_FALKORDB_PORT",
|
||||
"GRAPHITI_FALKORDB_PASSWORD",
|
||||
"CHAT_API_KEY",
|
||||
"CHAT_OPENAI_API_KEY",
|
||||
"OPEN_ROUTER_API_KEY",
|
||||
"OPENAI_API_KEY",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clean_env(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
for var in _ENV_VARS_TO_CLEAR:
|
||||
monkeypatch.delenv(var, raising=False)
|
||||
|
||||
|
||||
def test_graphiti_config_reads_backend_env_defaults() -> None:
|
||||
cfg = GraphitiConfig()
|
||||
|
||||
assert cfg.falkordb_host == "localhost"
|
||||
assert cfg.falkordb_port == 6380
|
||||
|
||||
|
||||
class TestResolveLlmApiKey:
|
||||
def test_returns_configured_key_when_set(self) -> None:
|
||||
cfg = GraphitiConfig(llm_api_key="my-llm-key")
|
||||
assert cfg.resolve_llm_api_key() == "my-llm-key"
|
||||
|
||||
def test_falls_back_to_chat_api_key_first(
|
||||
self, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setenv("CHAT_API_KEY", "autopilot-key")
|
||||
monkeypatch.setenv("OPEN_ROUTER_API_KEY", "platform-key")
|
||||
cfg = GraphitiConfig(llm_api_key="")
|
||||
assert cfg.resolve_llm_api_key() == "autopilot-key"
|
||||
|
||||
def test_falls_back_to_open_router_when_no_chat_key(
|
||||
self, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setenv("OPEN_ROUTER_API_KEY", "fallback-router-key")
|
||||
cfg = GraphitiConfig(llm_api_key="")
|
||||
assert cfg.resolve_llm_api_key() == "fallback-router-key"
|
||||
|
||||
def test_returns_empty_when_no_fallback(self) -> None:
|
||||
cfg = GraphitiConfig(llm_api_key="")
|
||||
assert cfg.resolve_llm_api_key() == ""
|
||||
|
||||
|
||||
class TestResolveLlmBaseUrl:
|
||||
def test_returns_configured_url_when_set(self) -> None:
|
||||
cfg = GraphitiConfig(llm_base_url="https://custom.api/v1")
|
||||
assert cfg.resolve_llm_base_url() == "https://custom.api/v1"
|
||||
|
||||
def test_falls_back_to_openrouter_base_url(self) -> None:
|
||||
cfg = GraphitiConfig(llm_base_url="")
|
||||
result = cfg.resolve_llm_base_url()
|
||||
assert result == "https://openrouter.ai/api/v1"
|
||||
|
||||
|
||||
class TestResolveEmbedderApiKey:
|
||||
def test_returns_configured_key_when_set(self) -> None:
|
||||
cfg = GraphitiConfig(embedder_api_key="my-embedder-key")
|
||||
assert cfg.resolve_embedder_api_key() == "my-embedder-key"
|
||||
|
||||
def test_falls_back_to_chat_openai_api_key_first(
|
||||
self, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setenv("CHAT_OPENAI_API_KEY", "autopilot-openai-key")
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "platform-openai-key")
|
||||
cfg = GraphitiConfig(embedder_api_key="")
|
||||
assert cfg.resolve_embedder_api_key() == "autopilot-openai-key"
|
||||
|
||||
def test_falls_back_to_openai_when_no_chat_openai_key(
|
||||
self, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fallback-openai-key")
|
||||
cfg = GraphitiConfig(embedder_api_key="")
|
||||
assert cfg.resolve_embedder_api_key() == "fallback-openai-key"
|
||||
|
||||
def test_returns_empty_when_no_fallback(self) -> None:
|
||||
cfg = GraphitiConfig(embedder_api_key="")
|
||||
assert cfg.resolve_embedder_api_key() == ""
|
||||
|
||||
|
||||
class TestResolveEmbedderBaseUrl:
|
||||
def test_returns_configured_url_when_set(self) -> None:
|
||||
cfg = GraphitiConfig(embedder_base_url="https://embed.custom/v1")
|
||||
assert cfg.resolve_embedder_base_url() == "https://embed.custom/v1"
|
||||
|
||||
def test_returns_none_when_empty(self) -> None:
|
||||
cfg = GraphitiConfig(embedder_base_url="")
|
||||
assert cfg.resolve_embedder_base_url() is None
|
||||
|
||||
|
||||
class TestIsEnabledForUser:
|
||||
@pytest.mark.asyncio
|
||||
async def test_none_user_returns_false(self) -> None:
|
||||
result = await is_enabled_for_user(None)
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_user_returns_false(self) -> None:
|
||||
result = await is_enabled_for_user("")
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delegates_to_feature_flag(self) -> None:
|
||||
with patch(
|
||||
"backend.util.feature_flag.is_feature_enabled",
|
||||
new_callable=AsyncMock,
|
||||
return_value=True,
|
||||
):
|
||||
result = await is_enabled_for_user("some-user-id")
|
||||
assert result is True
|
||||
@@ -1,117 +0,0 @@
|
||||
"""Warm context retrieval — pre-loads relevant facts at session start."""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from ._format import (
|
||||
extract_episode_body,
|
||||
extract_episode_body_raw,
|
||||
extract_episode_timestamp,
|
||||
extract_fact,
|
||||
extract_temporal_validity,
|
||||
)
|
||||
from .client import derive_group_id, get_graphiti_client
|
||||
from .config import graphiti_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def fetch_warm_context(user_id: str, message: str) -> str | None:
|
||||
"""Fetch relevant temporal context for the current user and message.
|
||||
|
||||
Called at the start of a session (first turn) to pre-load facts from
|
||||
prior conversations. Returns a formatted ``<temporal_context>`` block
|
||||
suitable for appending to the system prompt, or ``None`` on failure.
|
||||
|
||||
Graceful degradation: any error (timeout, connection, graphiti-core bug)
|
||||
returns ``None`` so the copilot continues without temporal context.
|
||||
"""
|
||||
if not user_id:
|
||||
return None
|
||||
|
||||
try:
|
||||
return await asyncio.wait_for(
|
||||
_fetch(user_id, message),
|
||||
timeout=graphiti_config.context_timeout,
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(
|
||||
"Graphiti warm context timed out after %.1fs",
|
||||
graphiti_config.context_timeout,
|
||||
)
|
||||
return None
|
||||
except Exception:
|
||||
logger.warning("Graphiti warm context fetch failed", exc_info=True)
|
||||
return None
|
||||
|
||||
|
||||
async def _fetch(user_id: str, message: str) -> str | None:
|
||||
group_id = derive_group_id(user_id)
|
||||
client = await get_graphiti_client(group_id)
|
||||
|
||||
edges, episodes = await asyncio.gather(
|
||||
client.search(
|
||||
query=message,
|
||||
group_ids=[group_id],
|
||||
num_results=graphiti_config.context_max_facts,
|
||||
),
|
||||
client.retrieve_episodes(
|
||||
reference_time=datetime.now(timezone.utc),
|
||||
group_ids=[group_id],
|
||||
last_n=5,
|
||||
),
|
||||
)
|
||||
|
||||
if not edges and not episodes:
|
||||
return None
|
||||
|
||||
return _format_context(edges, episodes)
|
||||
|
||||
|
||||
def _format_context(edges, episodes) -> str | None:
|
||||
sections: list[str] = []
|
||||
|
||||
if edges:
|
||||
fact_lines = []
|
||||
for e in edges:
|
||||
valid_from, valid_to = extract_temporal_validity(e)
|
||||
fact = extract_fact(e)
|
||||
fact_lines.append(f" - {fact} ({valid_from} — {valid_to})")
|
||||
sections.append("<FACTS>\n" + "\n".join(fact_lines) + "\n</FACTS>")
|
||||
|
||||
if episodes:
|
||||
ep_lines = []
|
||||
for ep in episodes:
|
||||
# Use raw body (no truncation) for scope parsing — truncated
|
||||
# JSON from extract_episode_body() would fail json.loads().
|
||||
raw_body = extract_episode_body_raw(ep)
|
||||
if _is_non_global_scope(raw_body):
|
||||
continue
|
||||
display_body = extract_episode_body(ep)
|
||||
ts = extract_episode_timestamp(ep)
|
||||
ep_lines.append(f" - [{ts}] {display_body}")
|
||||
if ep_lines:
|
||||
sections.append(
|
||||
"<RECENT_EPISODES>\n" + "\n".join(ep_lines) + "\n</RECENT_EPISODES>"
|
||||
)
|
||||
|
||||
if not sections:
|
||||
return None
|
||||
|
||||
body = "\n\n".join(sections)
|
||||
return f"<temporal_context>\n{body}\n</temporal_context>"
|
||||
|
||||
|
||||
def _is_non_global_scope(body: str) -> bool:
|
||||
"""Check if an episode body is a MemoryEnvelope with a non-global scope."""
|
||||
import json
|
||||
|
||||
try:
|
||||
data = json.loads(body)
|
||||
if not isinstance(data, dict):
|
||||
return False
|
||||
scope = data.get("scope", "real:global")
|
||||
return scope != "real:global"
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
return False
|
||||
@@ -1,266 +0,0 @@
|
||||
"""Tests for Graphiti warm context retrieval."""
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from . import context
|
||||
from ._format import extract_episode_body
|
||||
from .context import _format_context, _is_non_global_scope, fetch_warm_context
|
||||
from .memory_model import MemoryEnvelope, MemoryKind, SourceKind
|
||||
|
||||
|
||||
class TestFetchWarmContextEmptyUserId:
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_none_for_empty_user_id(self) -> None:
|
||||
result = await fetch_warm_context("", "hello")
|
||||
assert result is None
|
||||
|
||||
|
||||
class TestFetchWarmContextTimeout:
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_none_on_timeout(
|
||||
self, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
async def _slow_fetch(user_id: str, message: str) -> str:
|
||||
await asyncio.sleep(10)
|
||||
return "<temporal_context>data</temporal_context>"
|
||||
|
||||
with patch.object(context, "_fetch", side_effect=_slow_fetch):
|
||||
# Set an extremely short timeout.
|
||||
monkeypatch.setattr(context.graphiti_config, "context_timeout", 0.01)
|
||||
result = await fetch_warm_context("valid-user-id", "hello")
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
class TestFetchWarmContextGeneralError:
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_none_on_unexpected_error(self) -> None:
|
||||
with (
|
||||
patch.object(
|
||||
context,
|
||||
"derive_group_id",
|
||||
return_value="user_abc",
|
||||
),
|
||||
patch.object(
|
||||
context,
|
||||
"get_graphiti_client",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=RuntimeError("connection lost"),
|
||||
),
|
||||
):
|
||||
result = await fetch_warm_context("abc", "hello")
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Bug: extract_episode_body() truncation breaks scope filtering
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestFetchInternal:
|
||||
"""Test the internal _fetch function with mocked graphiti client."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_none_when_no_edges_or_episodes(self) -> None:
|
||||
mock_client = AsyncMock()
|
||||
mock_client.search.return_value = []
|
||||
mock_client.retrieve_episodes.return_value = []
|
||||
|
||||
with (
|
||||
patch.object(context, "derive_group_id", return_value="user_abc"),
|
||||
patch.object(
|
||||
context,
|
||||
"get_graphiti_client",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_client,
|
||||
),
|
||||
):
|
||||
result = await context._fetch("test-user", "hello")
|
||||
|
||||
assert result is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_context_with_edges(self) -> None:
|
||||
edge = SimpleNamespace(
|
||||
fact="user likes python",
|
||||
name="preference",
|
||||
valid_at="2025-01-01",
|
||||
invalid_at=None,
|
||||
)
|
||||
mock_client = AsyncMock()
|
||||
mock_client.search.return_value = [edge]
|
||||
mock_client.retrieve_episodes.return_value = []
|
||||
|
||||
with (
|
||||
patch.object(context, "derive_group_id", return_value="user_abc"),
|
||||
patch.object(
|
||||
context,
|
||||
"get_graphiti_client",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_client,
|
||||
),
|
||||
):
|
||||
result = await context._fetch("test-user", "hello")
|
||||
|
||||
assert result is not None
|
||||
assert "<temporal_context>" in result
|
||||
assert "user likes python" in result
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_context_with_episodes(self) -> None:
|
||||
ep = SimpleNamespace(
|
||||
content="talked about coffee",
|
||||
created_at="2025-06-01T00:00:00Z",
|
||||
)
|
||||
mock_client = AsyncMock()
|
||||
mock_client.search.return_value = []
|
||||
mock_client.retrieve_episodes.return_value = [ep]
|
||||
|
||||
with (
|
||||
patch.object(context, "derive_group_id", return_value="user_abc"),
|
||||
patch.object(
|
||||
context,
|
||||
"get_graphiti_client",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_client,
|
||||
),
|
||||
):
|
||||
result = await context._fetch("test-user", "hello")
|
||||
|
||||
assert result is not None
|
||||
assert "talked about coffee" in result
|
||||
|
||||
|
||||
class TestFormatContextWithContent:
|
||||
"""Test _format_context with actual edges and episodes."""
|
||||
|
||||
def test_with_edges_only(self) -> None:
|
||||
edge = SimpleNamespace(
|
||||
fact="user likes coffee",
|
||||
name="preference",
|
||||
valid_at="2025-01-01",
|
||||
invalid_at="present",
|
||||
)
|
||||
result = _format_context(edges=[edge], episodes=[])
|
||||
assert result is not None
|
||||
assert "<FACTS>" in result
|
||||
assert "user likes coffee" in result
|
||||
assert "<temporal_context>" in result
|
||||
|
||||
def test_with_episodes_only(self) -> None:
|
||||
ep = SimpleNamespace(
|
||||
content="plain conversation text",
|
||||
created_at="2025-01-01T00:00:00Z",
|
||||
)
|
||||
result = _format_context(edges=[], episodes=[ep])
|
||||
assert result is not None
|
||||
assert "<RECENT_EPISODES>" in result
|
||||
assert "plain conversation text" in result
|
||||
|
||||
def test_with_both_edges_and_episodes(self) -> None:
|
||||
edge = SimpleNamespace(
|
||||
fact="user likes coffee",
|
||||
valid_at="2025-01-01",
|
||||
invalid_at=None,
|
||||
)
|
||||
ep = SimpleNamespace(
|
||||
content="talked about coffee",
|
||||
created_at="2025-06-01T00:00:00Z",
|
||||
)
|
||||
result = _format_context(edges=[edge], episodes=[ep])
|
||||
assert result is not None
|
||||
assert "<FACTS>" in result
|
||||
assert "<RECENT_EPISODES>" in result
|
||||
|
||||
def test_global_scope_episode_included(self) -> None:
|
||||
envelope = MemoryEnvelope(content="global note", scope="real:global")
|
||||
ep = SimpleNamespace(
|
||||
content=envelope.model_dump_json(),
|
||||
created_at="2025-01-01T00:00:00Z",
|
||||
)
|
||||
result = _format_context(edges=[], episodes=[ep])
|
||||
assert result is not None
|
||||
assert "<RECENT_EPISODES>" in result
|
||||
|
||||
def test_non_global_scope_episode_excluded(self) -> None:
|
||||
envelope = MemoryEnvelope(content="project note", scope="project:crm")
|
||||
ep = SimpleNamespace(
|
||||
content=envelope.model_dump_json(),
|
||||
created_at="2025-01-01T00:00:00Z",
|
||||
)
|
||||
result = _format_context(edges=[], episodes=[ep])
|
||||
assert result is None
|
||||
|
||||
|
||||
class TestIsNonGlobalScopeEdgeCases:
|
||||
"""Verify _is_non_global_scope handles non-dict JSON without crashing."""
|
||||
|
||||
def test_list_json_treated_as_global(self) -> None:
|
||||
assert _is_non_global_scope("[1, 2, 3]") is False
|
||||
|
||||
def test_string_json_treated_as_global(self) -> None:
|
||||
assert _is_non_global_scope('"just a string"') is False
|
||||
|
||||
def test_null_json_treated_as_global(self) -> None:
|
||||
assert _is_non_global_scope("null") is False
|
||||
|
||||
def test_plain_text_treated_as_global(self) -> None:
|
||||
assert _is_non_global_scope("plain conversation text") is False
|
||||
|
||||
|
||||
class TestIsNonGlobalScopeTruncation:
|
||||
"""Verify _is_non_global_scope handles long MemoryEnvelope JSON.
|
||||
|
||||
extract_episode_body() truncates to 500 chars. A MemoryEnvelope with
|
||||
a long content field serializes to >500 chars, so the truncated string
|
||||
is invalid JSON. The except clause falls through to return False,
|
||||
incorrectly treating a project-scoped episode as global.
|
||||
"""
|
||||
|
||||
def test_long_envelope_with_non_global_scope_detected(self) -> None:
|
||||
"""Long MemoryEnvelope JSON should be parsed with raw (untruncated) body."""
|
||||
envelope = MemoryEnvelope(
|
||||
content="x" * 600,
|
||||
source_kind=SourceKind.user_asserted,
|
||||
scope="project:crm",
|
||||
memory_kind=MemoryKind.fact,
|
||||
)
|
||||
full_json = envelope.model_dump_json()
|
||||
assert len(full_json) > 500, "precondition: JSON must exceed truncation limit"
|
||||
|
||||
# With the fix: _is_non_global_scope on the raw (untruncated) body
|
||||
# correctly detects the non-global scope.
|
||||
assert _is_non_global_scope(full_json) is True
|
||||
|
||||
# Truncated body still fails — that's expected; callers must use raw body.
|
||||
ep = SimpleNamespace(content=full_json)
|
||||
truncated = extract_episode_body(ep)
|
||||
assert _is_non_global_scope(truncated) is False # truncated JSON → parse fails
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Bug: empty <temporal_context> wrapper when all episodes are non-global
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestFormatContextEmptyWrapper:
|
||||
"""When all episodes are non-global and edges is empty, _format_context
|
||||
should return None (no useful content) instead of an empty XML wrapper.
|
||||
"""
|
||||
|
||||
def test_returns_none_when_all_episodes_filtered(self) -> None:
|
||||
envelope = MemoryEnvelope(
|
||||
content="project-only note",
|
||||
scope="project:crm",
|
||||
)
|
||||
ep = SimpleNamespace(
|
||||
content=envelope.model_dump_json(),
|
||||
created_at="2025-01-01T00:00:00Z",
|
||||
)
|
||||
result = _format_context(edges=[], episodes=[ep])
|
||||
assert result is None
|
||||
@@ -1,34 +0,0 @@
|
||||
from graphiti_core.driver.falkordb import STOPWORDS
|
||||
from graphiti_core.driver.falkordb_driver import FalkorDriver
|
||||
from graphiti_core.helpers import validate_group_ids
|
||||
|
||||
|
||||
class AutoGPTFalkorDriver(FalkorDriver):
|
||||
def build_fulltext_query(
|
||||
self,
|
||||
query: str,
|
||||
group_ids: list[str] | None = None,
|
||||
max_query_length: int = 128,
|
||||
) -> str:
|
||||
validate_group_ids(group_ids)
|
||||
|
||||
group_filter = ""
|
||||
if group_ids:
|
||||
group_filter = f"(@group_id:{'|'.join(group_ids)})"
|
||||
|
||||
sanitized_query = self.sanitize(query)
|
||||
query_words = sanitized_query.split()
|
||||
filtered_words = [word for word in query_words if word.lower() not in STOPWORDS]
|
||||
sanitized_query = " | ".join(filtered_words)
|
||||
|
||||
if not sanitized_query:
|
||||
fulltext_query = group_filter
|
||||
elif not group_filter:
|
||||
fulltext_query = f"({sanitized_query})"
|
||||
else:
|
||||
fulltext_query = f"{group_filter} ({sanitized_query})"
|
||||
|
||||
if len(fulltext_query) >= max_query_length:
|
||||
return ""
|
||||
|
||||
return fulltext_query
|
||||
@@ -1,43 +0,0 @@
|
||||
from .falkordb_driver import AutoGPTFalkorDriver
|
||||
|
||||
|
||||
def test_build_fulltext_query_uses_unquoted_group_ids_for_falkordb() -> None:
|
||||
driver = AutoGPTFalkorDriver()
|
||||
|
||||
query = driver.build_fulltext_query(
|
||||
"Sarah",
|
||||
group_ids=["user_883cc9da-fe37-4863-839b-acba022bf3ef"],
|
||||
)
|
||||
|
||||
assert query == "(@group_id:user_883cc9da-fe37-4863-839b-acba022bf3ef) (Sarah)"
|
||||
assert '"user_883cc9da-fe37-4863-839b-acba022bf3ef"' not in query
|
||||
|
||||
|
||||
def test_build_fulltext_query_joins_multiple_group_ids_with_or() -> None:
|
||||
driver = AutoGPTFalkorDriver()
|
||||
|
||||
query = driver.build_fulltext_query("Sarah", group_ids=["user_a", "user_b"])
|
||||
|
||||
assert query == "(@group_id:user_a|user_b) (Sarah)"
|
||||
|
||||
|
||||
def test_stopwords_only_query_returns_group_filter_only() -> None:
|
||||
"""Line 25: sanitized_query is empty (all stopwords) but group_ids present."""
|
||||
driver = AutoGPTFalkorDriver()
|
||||
|
||||
# "the" is a common stopword — the query should reduce to just the group filter.
|
||||
query = driver.build_fulltext_query(
|
||||
"the",
|
||||
group_ids=["user_abc"],
|
||||
)
|
||||
|
||||
assert query == "(@group_id:user_abc)"
|
||||
|
||||
|
||||
def test_query_without_group_ids_returns_parenthesized_query() -> None:
|
||||
"""Line 27: sanitized_query has content but no group_ids provided."""
|
||||
driver = AutoGPTFalkorDriver()
|
||||
|
||||
query = driver.build_fulltext_query("Sarah", group_ids=None)
|
||||
|
||||
assert query == "(Sarah)"
|
||||
@@ -1,327 +0,0 @@
|
||||
"""Async episode ingestion with per-user serialization.
|
||||
|
||||
graphiti-core requires sequential ``add_episode()`` calls within the same
|
||||
group_id. This module provides a per-user asyncio.Queue that serializes
|
||||
ingestion while keeping it fire-and-forget from the caller's perspective.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import weakref
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from graphiti_core.nodes import EpisodeType
|
||||
|
||||
from .client import derive_group_id, get_graphiti_client
|
||||
from .memory_model import MemoryEnvelope, MemoryKind, MemoryStatus, SourceKind
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# The CoPilot executor runs one asyncio loop per worker thread, and
|
||||
# asyncio.Queue / asyncio.Lock / asyncio.Task are all bound to the loop they
|
||||
# were first used on. A process-wide worker registry would hand a loop-1-bound
|
||||
# Queue to a coroutine running on loop 2 → RuntimeError "Future attached to a
|
||||
# different loop". Scope the registry per running loop so each loop has its
|
||||
# own queues, workers, and lock. Entries auto-clean when the loop is GC'd.
|
||||
class _LoopIngestState:
|
||||
__slots__ = ("user_queues", "user_workers", "workers_lock")
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.user_queues: dict[str, asyncio.Queue] = {}
|
||||
self.user_workers: dict[str, asyncio.Task] = {}
|
||||
self.workers_lock = asyncio.Lock()
|
||||
|
||||
|
||||
_loop_state: (
|
||||
"weakref.WeakKeyDictionary[asyncio.AbstractEventLoop, _LoopIngestState]"
|
||||
) = weakref.WeakKeyDictionary()
|
||||
|
||||
|
||||
def _get_loop_state() -> _LoopIngestState:
|
||||
loop = asyncio.get_running_loop()
|
||||
state = _loop_state.get(loop)
|
||||
if state is None:
|
||||
state = _LoopIngestState()
|
||||
_loop_state[loop] = state
|
||||
return state
|
||||
|
||||
|
||||
# Idle workers are cleaned up after this many seconds of inactivity.
|
||||
_WORKER_IDLE_TIMEOUT = 60
|
||||
|
||||
CUSTOM_EXTRACTION_INSTRUCTIONS = """
|
||||
- Do not extract "User", "Assistant", "AI", "System", "CoPilot", or "human" as entity nodes.
|
||||
- Do not extract software tool names, block names, API endpoint names, or internal system identifiers as entities.
|
||||
- Do not extract action descriptions like "the assistant created..." as facts. Extract only the underlying user intent or real-world information.
|
||||
- Focus on real-world entities: people, companies, products, projects, concepts, and preferences.
|
||||
- Use canonical names: if the speaker says "my company" and context reveals it is "Acme Corp", use "Acme Corp".
|
||||
"""
|
||||
|
||||
|
||||
async def _ingestion_worker(user_id: str, queue: asyncio.Queue) -> None:
|
||||
"""Process episodes sequentially for a single user.
|
||||
|
||||
Exits after ``_WORKER_IDLE_TIMEOUT`` seconds of inactivity so that
|
||||
idle workers don't leak memory indefinitely.
|
||||
"""
|
||||
# Snapshot the loop-local state at task start so cleanup always runs
|
||||
# against the same state dict the worker was registered in, even if the
|
||||
# worker is cancelled from another task.
|
||||
state = _get_loop_state()
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
payload = await asyncio.wait_for(
|
||||
queue.get(), timeout=_WORKER_IDLE_TIMEOUT
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
break # idle — clean up below
|
||||
|
||||
try:
|
||||
group_id = derive_group_id(user_id)
|
||||
client = await get_graphiti_client(group_id)
|
||||
await client.add_episode(**payload)
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"Graphiti ingestion failed for user %s",
|
||||
user_id[:12],
|
||||
exc_info=True,
|
||||
)
|
||||
finally:
|
||||
queue.task_done()
|
||||
except asyncio.CancelledError:
|
||||
logger.debug("Ingestion worker cancelled for user %s", user_id[:12])
|
||||
raise
|
||||
finally:
|
||||
# Clean up so the next message re-creates the worker.
|
||||
state.user_queues.pop(user_id, None)
|
||||
state.user_workers.pop(user_id, None)
|
||||
|
||||
|
||||
async def enqueue_conversation_turn(
|
||||
user_id: str,
|
||||
session_id: str,
|
||||
user_msg: str,
|
||||
assistant_msg: str = "",
|
||||
) -> None:
|
||||
"""Enqueue a conversation turn for async background ingestion.
|
||||
|
||||
This returns almost immediately — the actual graphiti-core
|
||||
``add_episode()`` call (which triggers LLM entity extraction)
|
||||
runs in a background worker task.
|
||||
|
||||
If ``assistant_msg`` is provided and contains substantive findings
|
||||
(not just acknowledgments), a separate derived-finding episode is
|
||||
queued with ``source_kind=assistant_derived`` and ``status=tentative``.
|
||||
"""
|
||||
if not user_id:
|
||||
return
|
||||
|
||||
try:
|
||||
group_id = derive_group_id(user_id)
|
||||
except ValueError:
|
||||
logger.warning("Invalid user_id for ingestion: %s", user_id[:12])
|
||||
return
|
||||
|
||||
user_display_name = await _resolve_user_name(user_id)
|
||||
|
||||
episode_name = f"conversation_{session_id}"
|
||||
|
||||
# User's own words only, in graphiti's expected "Speaker: content" format.
|
||||
# Assistant response is excluded from extraction
|
||||
# (Zep Cloud approach: ignore_roles=["assistant"]).
|
||||
episode_body_for_graphiti = f"{user_display_name}: {user_msg}"
|
||||
|
||||
source_description = f"User message in session {session_id}"
|
||||
|
||||
queue = await _ensure_worker(user_id)
|
||||
|
||||
try:
|
||||
queue.put_nowait(
|
||||
{
|
||||
"name": episode_name,
|
||||
"episode_body": episode_body_for_graphiti,
|
||||
"source": EpisodeType.message,
|
||||
"source_description": source_description,
|
||||
"reference_time": datetime.now(timezone.utc),
|
||||
"group_id": group_id,
|
||||
"custom_extraction_instructions": CUSTOM_EXTRACTION_INSTRUCTIONS,
|
||||
}
|
||||
)
|
||||
except asyncio.QueueFull:
|
||||
logger.warning(
|
||||
"Graphiti ingestion queue full for user %s — dropping episode",
|
||||
user_id[:12],
|
||||
)
|
||||
return
|
||||
|
||||
# --- Derived-finding lane ---
|
||||
# If the assistant response is substantive, distill it into a
|
||||
# structured finding with tentative status.
|
||||
if assistant_msg and _is_finding_worthy(assistant_msg):
|
||||
finding = _distill_finding(assistant_msg)
|
||||
if finding:
|
||||
envelope = MemoryEnvelope(
|
||||
content=finding,
|
||||
source_kind=SourceKind.assistant_derived,
|
||||
memory_kind=MemoryKind.finding,
|
||||
status=MemoryStatus.tentative,
|
||||
provenance=f"session:{session_id}",
|
||||
)
|
||||
try:
|
||||
queue.put_nowait(
|
||||
{
|
||||
"name": f"finding_{session_id}",
|
||||
"episode_body": envelope.model_dump_json(),
|
||||
"source": EpisodeType.json,
|
||||
"source_description": f"Assistant-derived finding in session {session_id}",
|
||||
"reference_time": datetime.now(timezone.utc),
|
||||
"group_id": group_id,
|
||||
"custom_extraction_instructions": CUSTOM_EXTRACTION_INSTRUCTIONS,
|
||||
}
|
||||
)
|
||||
except asyncio.QueueFull:
|
||||
pass # user canonical episode already queued — finding is best-effort
|
||||
|
||||
|
||||
async def enqueue_episode(
|
||||
user_id: str,
|
||||
session_id: str,
|
||||
*,
|
||||
name: str,
|
||||
episode_body: str,
|
||||
source_description: str = "Conversation memory",
|
||||
is_json: bool = False,
|
||||
) -> bool:
|
||||
"""Enqueue an arbitrary episode for background ingestion.
|
||||
|
||||
Used by ``MemoryStoreTool`` so that explicit memory-store calls go
|
||||
through the same per-user serialization queue as conversation turns.
|
||||
|
||||
Args:
|
||||
is_json: When ``True``, ingest as ``EpisodeType.json`` (for
|
||||
structured ``MemoryEnvelope`` payloads). Otherwise uses
|
||||
``EpisodeType.text``.
|
||||
|
||||
Returns ``True`` if the episode was queued, ``False`` if it was dropped.
|
||||
"""
|
||||
if not user_id:
|
||||
return False
|
||||
|
||||
try:
|
||||
group_id = derive_group_id(user_id)
|
||||
except ValueError:
|
||||
logger.warning("Invalid user_id for episode ingestion: %s", user_id[:12])
|
||||
return False
|
||||
|
||||
queue = await _ensure_worker(user_id)
|
||||
|
||||
source = EpisodeType.json if is_json else EpisodeType.text
|
||||
|
||||
try:
|
||||
queue.put_nowait(
|
||||
{
|
||||
"name": name,
|
||||
"episode_body": episode_body,
|
||||
"source": source,
|
||||
"source_description": source_description,
|
||||
"reference_time": datetime.now(timezone.utc),
|
||||
"group_id": group_id,
|
||||
"custom_extraction_instructions": CUSTOM_EXTRACTION_INSTRUCTIONS,
|
||||
}
|
||||
)
|
||||
return True
|
||||
except asyncio.QueueFull:
|
||||
logger.warning(
|
||||
"Graphiti ingestion queue full for user %s — dropping episode",
|
||||
user_id[:12],
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
async def _ensure_worker(user_id: str) -> asyncio.Queue:
|
||||
"""Create a queue and worker for *user_id* if one doesn't exist.
|
||||
|
||||
Returns the queue directly so callers don't need to look it up from
|
||||
the state dict (which avoids a TOCTOU race if the worker times out
|
||||
and cleans up between this call and the put_nowait).
|
||||
"""
|
||||
state = _get_loop_state()
|
||||
async with state.workers_lock:
|
||||
if user_id not in state.user_queues:
|
||||
q: asyncio.Queue = asyncio.Queue(maxsize=100)
|
||||
state.user_queues[user_id] = q
|
||||
state.user_workers[user_id] = asyncio.create_task(
|
||||
_ingestion_worker(user_id, q),
|
||||
name=f"graphiti-ingest-{user_id[:12]}",
|
||||
)
|
||||
return state.user_queues[user_id]
|
||||
|
||||
|
||||
async def _resolve_user_name(user_id: str) -> str:
|
||||
"""Get the user's display name from BusinessUnderstanding, or fall back to 'User'."""
|
||||
try:
|
||||
from backend.data.db_accessors import understanding_db
|
||||
|
||||
understanding = await understanding_db().get_business_understanding(user_id)
|
||||
if understanding and understanding.user_name:
|
||||
return understanding.user_name
|
||||
except Exception:
|
||||
logger.debug("Could not resolve user name for %s", user_id[:12])
|
||||
return "User"
|
||||
|
||||
|
||||
# --- Derived-finding distillation ---
|
||||
|
||||
# Phrases that indicate workflow chatter, not substantive findings.
|
||||
_CHATTER_PREFIXES = (
|
||||
"done",
|
||||
"got it",
|
||||
"sure, i",
|
||||
"sure!",
|
||||
"ok",
|
||||
"okay",
|
||||
"i've created",
|
||||
"i've updated",
|
||||
"i've sent",
|
||||
"i'll ",
|
||||
"let me ",
|
||||
"a sign-in button",
|
||||
"please click",
|
||||
)
|
||||
|
||||
# Minimum length for an assistant message to be considered finding-worthy.
|
||||
_MIN_FINDING_LENGTH = 150
|
||||
|
||||
|
||||
def _is_finding_worthy(assistant_msg: str) -> bool:
|
||||
"""Heuristic gate: is this assistant response worth distilling into a finding?
|
||||
|
||||
Skips short acknowledgments, workflow chatter, and UI prompts.
|
||||
Only passes through responses that likely contain substantive
|
||||
factual content (research results, analysis, conclusions).
|
||||
"""
|
||||
if len(assistant_msg) < _MIN_FINDING_LENGTH:
|
||||
return False
|
||||
|
||||
lower = assistant_msg.lower().strip()
|
||||
for prefix in _CHATTER_PREFIXES:
|
||||
if lower.startswith(prefix):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def _distill_finding(assistant_msg: str) -> str | None:
|
||||
"""Extract the core finding from an assistant response.
|
||||
|
||||
For now, uses a simple truncation approach. Phase 3+ could use
|
||||
a lightweight LLM call for proper distillation.
|
||||
"""
|
||||
# Take the first 500 chars as the finding content.
|
||||
# Strip markdown formatting artifacts.
|
||||
content = assistant_msg.strip()
|
||||
if len(content) > 500:
|
||||
content = content[:500] + "..."
|
||||
return content if content else None
|
||||
@@ -1,317 +0,0 @@
|
||||
"""Tests for Graphiti ingestion queue and worker logic."""
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from . import ingest
|
||||
|
||||
# Per-loop state in ingest.py auto-isolates between tests: pytest-asyncio
|
||||
# creates a fresh event loop per test function, and the WeakKeyDictionary
|
||||
# forgets the previous loop's state when it is GC'd. No manual reset needed.
|
||||
|
||||
|
||||
class TestIngestionWorkerExceptionHandling:
|
||||
@pytest.mark.asyncio
|
||||
async def test_worker_continues_after_client_error(self) -> None:
|
||||
"""If get_graphiti_client raises, the worker logs and continues."""
|
||||
queue: asyncio.Queue = asyncio.Queue(maxsize=10)
|
||||
queue.put_nowait(
|
||||
{
|
||||
"name": "ep1",
|
||||
"episode_body": "hello",
|
||||
"source": "message",
|
||||
"source_description": "test",
|
||||
"reference_time": None,
|
||||
"group_id": "user_test",
|
||||
}
|
||||
)
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
ingest,
|
||||
"derive_group_id",
|
||||
return_value="user_test",
|
||||
),
|
||||
patch.object(
|
||||
ingest,
|
||||
"get_graphiti_client",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=RuntimeError("connection failed"),
|
||||
),
|
||||
):
|
||||
# Use a short idle timeout so the worker exits quickly.
|
||||
original_timeout = ingest._WORKER_IDLE_TIMEOUT
|
||||
ingest._WORKER_IDLE_TIMEOUT = 0.1
|
||||
try:
|
||||
await ingest._ingestion_worker("test-user", queue)
|
||||
finally:
|
||||
ingest._WORKER_IDLE_TIMEOUT = original_timeout
|
||||
|
||||
# Worker processed the item (task_done called) and exited.
|
||||
assert queue.empty()
|
||||
|
||||
|
||||
class TestEnqueueConversationTurn:
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_user_id_returns_without_error(self) -> None:
|
||||
await ingest.enqueue_conversation_turn(
|
||||
user_id="",
|
||||
session_id="sess1",
|
||||
user_msg="hi",
|
||||
)
|
||||
# No queue should have been created.
|
||||
assert len(ingest._get_loop_state().user_queues) == 0
|
||||
|
||||
|
||||
class TestQueueFullScenario:
|
||||
@pytest.mark.asyncio
|
||||
async def test_queue_full_logs_warning_no_crash(self) -> None:
|
||||
user_id = "abc-valid-id"
|
||||
|
||||
mock_understanding = SimpleNamespace(user_name="Alice")
|
||||
mock_understanding_db = MagicMock()
|
||||
mock_understanding_db.return_value.get_business_understanding = AsyncMock(
|
||||
return_value=mock_understanding
|
||||
)
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
ingest,
|
||||
"derive_group_id",
|
||||
return_value="user_abc-valid-id",
|
||||
),
|
||||
patch(
|
||||
"backend.copilot.graphiti.ingest._resolve_user_name",
|
||||
new_callable=AsyncMock,
|
||||
return_value="Alice",
|
||||
),
|
||||
):
|
||||
# Create a tiny queue so it fills instantly.
|
||||
await ingest._ensure_worker(user_id)
|
||||
# Replace the queue with one that is already full.
|
||||
tiny_q: asyncio.Queue = asyncio.Queue(maxsize=1)
|
||||
tiny_q.put_nowait({"dummy": True})
|
||||
ingest._get_loop_state().user_queues[user_id] = tiny_q
|
||||
|
||||
# Should not raise even though the queue is full.
|
||||
await ingest.enqueue_conversation_turn(
|
||||
user_id=user_id,
|
||||
session_id="sess1",
|
||||
user_msg="hi",
|
||||
)
|
||||
|
||||
|
||||
class TestResolveUserName:
|
||||
@pytest.mark.asyncio
|
||||
async def test_fallback_when_db_raises(self) -> None:
|
||||
mock_db = MagicMock()
|
||||
mock_db.return_value.get_business_understanding = AsyncMock(
|
||||
side_effect=RuntimeError("DB not available")
|
||||
)
|
||||
|
||||
with patch(
|
||||
"backend.data.db_accessors.understanding_db",
|
||||
mock_db,
|
||||
):
|
||||
name = await ingest._resolve_user_name("some-user-id")
|
||||
|
||||
assert name == "User"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_user_name_when_available(self) -> None:
|
||||
mock_understanding = SimpleNamespace(user_name="Alice")
|
||||
mock_db = MagicMock()
|
||||
mock_db.return_value.get_business_understanding = AsyncMock(
|
||||
return_value=mock_understanding
|
||||
)
|
||||
|
||||
with patch(
|
||||
"backend.data.db_accessors.understanding_db",
|
||||
mock_db,
|
||||
):
|
||||
name = await ingest._resolve_user_name("some-user-id")
|
||||
|
||||
assert name == "Alice"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_user_when_understanding_is_none(self) -> None:
|
||||
mock_db = MagicMock()
|
||||
mock_db.return_value.get_business_understanding = AsyncMock(return_value=None)
|
||||
|
||||
with patch(
|
||||
"backend.data.db_accessors.understanding_db",
|
||||
mock_db,
|
||||
):
|
||||
name = await ingest._resolve_user_name("some-user-id")
|
||||
|
||||
assert name == "User"
|
||||
|
||||
|
||||
class TestEnqueueEpisode:
|
||||
@pytest.mark.asyncio
|
||||
async def test_enqueue_episode_returns_true_on_success(self) -> None:
|
||||
with (
|
||||
patch.object(ingest, "derive_group_id", return_value="user_abc"),
|
||||
patch.object(
|
||||
ingest, "_ensure_worker", new_callable=AsyncMock
|
||||
) as mock_worker,
|
||||
):
|
||||
q: asyncio.Queue = asyncio.Queue(maxsize=100)
|
||||
mock_worker.return_value = q
|
||||
|
||||
result = await ingest.enqueue_episode(
|
||||
user_id="abc",
|
||||
session_id="sess1",
|
||||
name="test_ep",
|
||||
episode_body="hello",
|
||||
is_json=False,
|
||||
)
|
||||
assert result is True
|
||||
assert not q.empty()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_enqueue_episode_returns_false_for_empty_user(self) -> None:
|
||||
result = await ingest.enqueue_episode(
|
||||
user_id="",
|
||||
session_id="sess1",
|
||||
name="test_ep",
|
||||
episode_body="hello",
|
||||
)
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_enqueue_episode_returns_false_on_invalid_user(self) -> None:
|
||||
with patch.object(ingest, "derive_group_id", side_effect=ValueError("bad id")):
|
||||
result = await ingest.enqueue_episode(
|
||||
user_id="bad",
|
||||
session_id="sess1",
|
||||
name="test_ep",
|
||||
episode_body="hello",
|
||||
)
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_enqueue_episode_json_mode(self) -> None:
|
||||
with (
|
||||
patch.object(ingest, "derive_group_id", return_value="user_abc"),
|
||||
patch.object(
|
||||
ingest, "_ensure_worker", new_callable=AsyncMock
|
||||
) as mock_worker,
|
||||
):
|
||||
q: asyncio.Queue = asyncio.Queue(maxsize=100)
|
||||
mock_worker.return_value = q
|
||||
|
||||
result = await ingest.enqueue_episode(
|
||||
user_id="abc",
|
||||
session_id="sess1",
|
||||
name="test_ep",
|
||||
episode_body='{"content": "hello"}',
|
||||
is_json=True,
|
||||
)
|
||||
assert result is True
|
||||
item = q.get_nowait()
|
||||
from graphiti_core.nodes import EpisodeType
|
||||
|
||||
assert item["source"] == EpisodeType.json
|
||||
|
||||
|
||||
class TestDerivedFindingLane:
|
||||
@pytest.mark.asyncio
|
||||
async def test_finding_worthy_message_enqueues_two_episodes(self) -> None:
|
||||
"""A substantive assistant message should enqueue both the user
|
||||
episode and a derived-finding episode."""
|
||||
long_msg = "The analysis reveals significant growth patterns " + "x" * 200
|
||||
|
||||
with (
|
||||
patch.object(ingest, "derive_group_id", return_value="user_abc"),
|
||||
patch.object(
|
||||
ingest, "_ensure_worker", new_callable=AsyncMock
|
||||
) as mock_worker,
|
||||
patch(
|
||||
"backend.copilot.graphiti.ingest._resolve_user_name",
|
||||
new_callable=AsyncMock,
|
||||
return_value="Alice",
|
||||
),
|
||||
):
|
||||
q: asyncio.Queue = asyncio.Queue(maxsize=100)
|
||||
mock_worker.return_value = q
|
||||
|
||||
await ingest.enqueue_conversation_turn(
|
||||
user_id="abc",
|
||||
session_id="sess1",
|
||||
user_msg="tell me about growth",
|
||||
assistant_msg=long_msg,
|
||||
)
|
||||
# Should have 2 items: user episode + derived finding
|
||||
assert q.qsize() == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_short_assistant_msg_skips_finding(self) -> None:
|
||||
with (
|
||||
patch.object(ingest, "derive_group_id", return_value="user_abc"),
|
||||
patch.object(
|
||||
ingest, "_ensure_worker", new_callable=AsyncMock
|
||||
) as mock_worker,
|
||||
patch(
|
||||
"backend.copilot.graphiti.ingest._resolve_user_name",
|
||||
new_callable=AsyncMock,
|
||||
return_value="Alice",
|
||||
),
|
||||
):
|
||||
q: asyncio.Queue = asyncio.Queue(maxsize=100)
|
||||
mock_worker.return_value = q
|
||||
|
||||
await ingest.enqueue_conversation_turn(
|
||||
user_id="abc",
|
||||
session_id="sess1",
|
||||
user_msg="hi",
|
||||
assistant_msg="ok",
|
||||
)
|
||||
# Only 1 item: the user episode (no finding for short msg)
|
||||
assert q.qsize() == 1
|
||||
|
||||
|
||||
class TestDerivedFindingDistillation:
|
||||
"""_is_finding_worthy and _distill_finding gate derived-finding creation."""
|
||||
|
||||
def test_short_message_not_finding_worthy(self) -> None:
|
||||
assert ingest._is_finding_worthy("ok") is False
|
||||
|
||||
def test_chatter_prefix_not_finding_worthy(self) -> None:
|
||||
assert ingest._is_finding_worthy("done " + "x" * 200) is False
|
||||
|
||||
def test_long_substantive_message_is_finding_worthy(self) -> None:
|
||||
msg = "The quarterly revenue analysis shows a 15% increase " + "x" * 200
|
||||
assert ingest._is_finding_worthy(msg) is True
|
||||
|
||||
def test_distill_finding_truncates_to_500(self) -> None:
|
||||
result = ingest._distill_finding("x" * 600)
|
||||
assert result is not None
|
||||
assert len(result) == 503 # 500 + "..."
|
||||
|
||||
|
||||
class TestWorkerIdleTimeout:
|
||||
@pytest.mark.asyncio
|
||||
async def test_worker_cleans_up_on_idle(self) -> None:
|
||||
user_id = "idle-user"
|
||||
queue: asyncio.Queue = asyncio.Queue(maxsize=10)
|
||||
|
||||
# Pre-populate state so cleanup can remove entries.
|
||||
state = ingest._get_loop_state()
|
||||
state.user_queues[user_id] = queue
|
||||
task_sentinel = MagicMock()
|
||||
state.user_workers[user_id] = task_sentinel
|
||||
|
||||
original_timeout = ingest._WORKER_IDLE_TIMEOUT
|
||||
ingest._WORKER_IDLE_TIMEOUT = 0.05
|
||||
try:
|
||||
await ingest._ingestion_worker(user_id, queue)
|
||||
finally:
|
||||
ingest._WORKER_IDLE_TIMEOUT = original_timeout
|
||||
|
||||
# After idle timeout the worker should have cleaned up.
|
||||
assert user_id not in state.user_queues
|
||||
assert user_id not in state.user_workers
|
||||
@@ -1,118 +0,0 @@
|
||||
"""Generic memory metadata model for Graphiti episodes.
|
||||
|
||||
Domain-agnostic envelope that works across business, fiction, research,
|
||||
personal life, and arbitrary knowledge domains. Designed so retrieval
|
||||
can distinguish user-asserted facts from assistant-derived findings
|
||||
and filter by scope.
|
||||
"""
|
||||
|
||||
from enum import Enum
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class SourceKind(str, Enum):
|
||||
user_asserted = "user_asserted"
|
||||
assistant_derived = "assistant_derived"
|
||||
tool_observed = "tool_observed"
|
||||
|
||||
|
||||
class MemoryKind(str, Enum):
|
||||
fact = "fact"
|
||||
preference = "preference"
|
||||
rule = "rule"
|
||||
finding = "finding"
|
||||
plan = "plan"
|
||||
event = "event"
|
||||
procedure = "procedure"
|
||||
|
||||
|
||||
class MemoryStatus(str, Enum):
|
||||
active = "active"
|
||||
tentative = "tentative"
|
||||
superseded = "superseded"
|
||||
contradicted = "contradicted"
|
||||
|
||||
|
||||
class RuleMemory(BaseModel):
|
||||
"""Structured representation of a standing instruction or rule.
|
||||
|
||||
Preserves the exact user intent rather than relying on LLM
|
||||
extraction to reconstruct it from prose.
|
||||
"""
|
||||
|
||||
instruction: str = Field(
|
||||
description="The actionable instruction (e.g. 'CC Sarah on client communications')"
|
||||
)
|
||||
actor: str | None = Field(
|
||||
default=None, description="Who performs or is subject to the rule"
|
||||
)
|
||||
trigger: str | None = Field(
|
||||
default=None,
|
||||
description="When the rule applies (e.g. 'client-related communications')",
|
||||
)
|
||||
negation: str | None = Field(
|
||||
default=None,
|
||||
description="What NOT to do, if applicable (e.g. 'do not use SMTP')",
|
||||
)
|
||||
|
||||
|
||||
class ProcedureStep(BaseModel):
|
||||
"""A single step in a multi-step procedure."""
|
||||
|
||||
order: int = Field(description="Step number (1-based)")
|
||||
action: str = Field(description="What to do in this step")
|
||||
tool: str | None = Field(default=None, description="Tool or service to use")
|
||||
condition: str | None = Field(default=None, description="When/if this step applies")
|
||||
negation: str | None = Field(
|
||||
default=None, description="What NOT to do in this step"
|
||||
)
|
||||
|
||||
|
||||
class ProcedureMemory(BaseModel):
|
||||
"""Structured representation of a multi-step workflow.
|
||||
|
||||
Steps with ordering, tools, conditions, and negations that don't
|
||||
decompose cleanly into fact triples.
|
||||
"""
|
||||
|
||||
description: str = Field(description="What this procedure accomplishes")
|
||||
steps: list[ProcedureStep] = Field(default_factory=list)
|
||||
|
||||
|
||||
class MemoryEnvelope(BaseModel):
|
||||
"""Structured wrapper for explicit memory storage.
|
||||
|
||||
Serialized as JSON and ingested via ``EpisodeType.json`` so that
|
||||
Graphiti extracts entities from the ``content`` field while the
|
||||
metadata fields survive as episode-level context.
|
||||
|
||||
For ``memory_kind=rule``, populate the ``rule`` field with a
|
||||
``RuleMemory`` to preserve the exact instruction. For
|
||||
``memory_kind=procedure``, populate ``procedure`` with a
|
||||
``ProcedureMemory`` for structured steps.
|
||||
"""
|
||||
|
||||
content: str = Field(
|
||||
description="The memory content — the actual fact, rule, or finding"
|
||||
)
|
||||
source_kind: SourceKind = Field(default=SourceKind.user_asserted)
|
||||
scope: str = Field(
|
||||
default="real:global",
|
||||
description="Namespace: 'real:global', 'project:<name>', 'book:<title>', 'session:<id>'",
|
||||
)
|
||||
memory_kind: MemoryKind = Field(default=MemoryKind.fact)
|
||||
status: MemoryStatus = Field(default=MemoryStatus.active)
|
||||
confidence: float | None = Field(default=None, ge=0.0, le=1.0)
|
||||
provenance: str | None = Field(
|
||||
default=None,
|
||||
description="Origin reference — session_id, tool_call_id, or URL",
|
||||
)
|
||||
rule: RuleMemory | None = Field(
|
||||
default=None,
|
||||
description="Structured rule data — populate when memory_kind=rule",
|
||||
)
|
||||
procedure: ProcedureMemory | None = Field(
|
||||
default=None,
|
||||
description="Structured procedure data — populate when memory_kind=procedure",
|
||||
)
|
||||
@@ -1,8 +1,9 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any, AsyncIterator, Self, cast
|
||||
from typing import Any, Self, cast
|
||||
from weakref import WeakValueDictionary
|
||||
|
||||
from openai.types.chat import (
|
||||
ChatCompletionAssistantMessageParam,
|
||||
@@ -63,7 +64,6 @@ class ChatMessage(BaseModel):
|
||||
refusal: str | None = None
|
||||
tool_calls: list[dict] | None = None
|
||||
function_call: dict | None = None
|
||||
sequence: int | None = None
|
||||
duration_ms: int | None = None
|
||||
|
||||
@staticmethod
|
||||
@@ -77,7 +77,6 @@ class ChatMessage(BaseModel):
|
||||
refusal=prisma_message.refusal,
|
||||
tool_calls=_parse_json_field(prisma_message.toolCalls),
|
||||
function_call=_parse_json_field(prisma_message.functionCall),
|
||||
sequence=prisma_message.sequence,
|
||||
duration_ms=prisma_message.durationMs,
|
||||
)
|
||||
|
||||
@@ -521,7 +520,10 @@ async def upsert_chat_session(
|
||||
callers are aware of the persistence failure.
|
||||
RedisError: If the cache write fails (after successful DB write).
|
||||
"""
|
||||
async with _get_session_lock(session.session_id) as _:
|
||||
# Acquire session-specific lock to prevent concurrent upserts
|
||||
lock = await _get_session_lock(session.session_id)
|
||||
|
||||
async with lock:
|
||||
# Always query DB for existing message count to ensure consistency
|
||||
existing_message_count = await chat_db().get_next_sequence(session.session_id)
|
||||
|
||||
@@ -640,57 +642,21 @@ async def _save_session_to_db(
|
||||
start_sequence=existing_message_count,
|
||||
)
|
||||
|
||||
# Back-fill sequence numbers on the in-memory ChatMessage objects so
|
||||
# that downstream callers (inject_user_context) can persist updates
|
||||
# by sequence rather than falling back to index-based writes.
|
||||
for i, msg in enumerate(new_messages):
|
||||
msg.sequence = existing_message_count + i
|
||||
|
||||
|
||||
async def append_and_save_message(
|
||||
session_id: str, message: ChatMessage
|
||||
) -> ChatSession | None:
|
||||
async def append_and_save_message(session_id: str, message: ChatMessage) -> ChatSession:
|
||||
"""Atomically append a message to a session and persist it.
|
||||
|
||||
Returns the updated session, or None if the message was detected as a
|
||||
duplicate (idempotency guard). Callers must check for None and skip any
|
||||
downstream work (e.g. enqueuing a new LLM turn) when a duplicate is detected.
|
||||
|
||||
Uses _get_session_lock (Redis NX) to serialise concurrent writers across replicas.
|
||||
The idempotency check below provides a last-resort guard when the lock degrades.
|
||||
Acquires the session lock, re-fetches the latest session state,
|
||||
appends the message, and saves — preventing message loss when
|
||||
concurrent requests modify the same session.
|
||||
"""
|
||||
async with _get_session_lock(session_id) as lock_acquired:
|
||||
# When the lock degraded (Redis down or 2s timeout), bypass cache for
|
||||
# the idempotency check. Stale cache could let two concurrent writers
|
||||
# both see the old state, pass the check, and write the same message.
|
||||
if lock_acquired:
|
||||
session = await get_chat_session(session_id)
|
||||
else:
|
||||
session = await _get_session_from_db(session_id)
|
||||
lock = await _get_session_lock(session_id)
|
||||
|
||||
async with lock:
|
||||
session = await get_chat_session(session_id)
|
||||
if session is None:
|
||||
raise ValueError(f"Session {session_id} not found")
|
||||
|
||||
# Idempotency: skip if the trailing block of same-role messages already
|
||||
# contains this content. Uses is_message_duplicate which checks all
|
||||
# consecutive trailing messages of the same role, not just [-1].
|
||||
#
|
||||
# This collapses infra/nginx retries whether they land on the same pod
|
||||
# (serialised by the Redis lock) or a different pod.
|
||||
#
|
||||
# Legit same-text messages are distinguished by the assistant turn
|
||||
# between them: if the user said "yes", got a response, and says
|
||||
# "yes" again, session.messages[-1] is the assistant reply, so the
|
||||
# role check fails and the second message goes through normally.
|
||||
#
|
||||
# Edge case: if a turn dies without writing any assistant message,
|
||||
# the user's next send of the same text is blocked here permanently.
|
||||
# The fix is to ensure failed turns always write an error/timeout
|
||||
# assistant message so the session always ends on an assistant turn.
|
||||
if message.content is not None and is_message_duplicate(
|
||||
session.messages, message.role, message.content
|
||||
):
|
||||
return None # duplicate — caller should skip enqueue
|
||||
|
||||
session.messages.append(message)
|
||||
existing_message_count = await chat_db().get_next_sequence(session_id)
|
||||
|
||||
@@ -705,9 +671,6 @@ async def append_and_save_message(
|
||||
await cache_chat_session(session)
|
||||
except Exception as e:
|
||||
logger.warning(f"Cache write failed for session {session_id}: {e}")
|
||||
# Invalidate the stale entry so future reads fall back to DB,
|
||||
# preventing a retry from bypassing the idempotency check above.
|
||||
await invalidate_session_cache(session_id)
|
||||
|
||||
return session
|
||||
|
||||
@@ -793,6 +756,10 @@ async def delete_chat_session(session_id: str, user_id: str | None = None) -> bo
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to delete session {session_id} from cache: {e}")
|
||||
|
||||
# Clean up session lock (belt-and-suspenders with WeakValueDictionary)
|
||||
async with _session_locks_mutex:
|
||||
_session_locks.pop(session_id, None)
|
||||
|
||||
# Shut down any local browser daemon for this session (best-effort).
|
||||
# Inline import required: all tool modules import ChatSession from this
|
||||
# module, so any top-level import from tools.* would create a cycle.
|
||||
@@ -857,38 +824,25 @@ async def update_session_title(
|
||||
|
||||
# ==================== Chat session locks ==================== #
|
||||
|
||||
_session_locks: WeakValueDictionary[str, asyncio.Lock] = WeakValueDictionary()
|
||||
_session_locks_mutex = asyncio.Lock()
|
||||
|
||||
@asynccontextmanager
|
||||
async def _get_session_lock(session_id: str) -> AsyncIterator[bool]:
|
||||
"""Distributed Redis lock for a session, usable as an async context manager.
|
||||
|
||||
Yields True if the lock was acquired, False if it timed out or Redis was
|
||||
unavailable. Callers should treat False as a degraded mode and prefer fresh
|
||||
DB reads over cache to avoid acting on stale state.
|
||||
async def _get_session_lock(session_id: str) -> asyncio.Lock:
|
||||
"""Get or create a lock for a specific session to prevent concurrent upserts.
|
||||
|
||||
Uses redis-py's built-in Lock (Lua-script acquire/release) so lock acquisition
|
||||
is atomic and release is owner-verified. Blocks up to 2s for a concurrent
|
||||
writer to finish; the 10s TTL ensures a dead pod never holds the lock forever.
|
||||
This was originally added to solve the specific problem of race conditions between
|
||||
the session title thread and the conversation thread, which always occurs on the
|
||||
same instance as we prevent rapid request sends on the frontend.
|
||||
|
||||
Uses WeakValueDictionary for automatic cleanup: locks are garbage collected
|
||||
when no coroutine holds a reference to them, preventing memory leaks from
|
||||
unbounded growth of session locks. Explicit cleanup also occurs
|
||||
in `delete_chat_session()`.
|
||||
"""
|
||||
_lock_key = f"copilot:session_lock:{session_id}"
|
||||
lock = None
|
||||
acquired = False
|
||||
try:
|
||||
_redis = await get_redis_async()
|
||||
lock = _redis.lock(_lock_key, timeout=10, blocking_timeout=2)
|
||||
acquired = await lock.acquire(blocking=True)
|
||||
if not acquired:
|
||||
logger.warning(
|
||||
"Could not acquire session lock for %s within 2s", session_id
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning("Redis unavailable for session lock on %s: %s", session_id, e)
|
||||
|
||||
try:
|
||||
yield acquired
|
||||
finally:
|
||||
if acquired and lock is not None:
|
||||
try:
|
||||
await lock.release()
|
||||
except Exception:
|
||||
pass # TTL will expire the key
|
||||
async with _session_locks_mutex:
|
||||
lock = _session_locks.get(session_id)
|
||||
if lock is None:
|
||||
lock = asyncio.Lock()
|
||||
_session_locks[session_id] = lock
|
||||
return lock
|
||||
|
||||
@@ -11,13 +11,11 @@ from openai.types.chat.chat_completion_message_tool_call_param import (
|
||||
ChatCompletionMessageToolCallParam,
|
||||
Function,
|
||||
)
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from .model import (
|
||||
ChatMessage,
|
||||
ChatSession,
|
||||
Usage,
|
||||
append_and_save_message,
|
||||
get_chat_session,
|
||||
is_message_duplicate,
|
||||
maybe_append_user_message,
|
||||
@@ -576,345 +574,3 @@ def test_maybe_append_assistant_skips_duplicate():
|
||||
result = maybe_append_user_message(session, "dup", is_user_message=False)
|
||||
assert result is False
|
||||
assert len(session.messages) == 2
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# append_and_save_message #
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def _make_session_with_messages(*msgs: ChatMessage) -> ChatSession:
|
||||
s = ChatSession.new(user_id="u1", dry_run=False)
|
||||
s.messages = list(msgs)
|
||||
return s
|
||||
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="session")
|
||||
async def test_append_and_save_message_returns_none_for_duplicate(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""append_and_save_message returns None when the trailing message is a duplicate."""
|
||||
|
||||
session = _make_session_with_messages(
|
||||
ChatMessage(role="user", content="hello"),
|
||||
)
|
||||
mock_redis_lock = mocker.AsyncMock()
|
||||
mock_redis_lock.acquire = mocker.AsyncMock(return_value=True)
|
||||
mock_redis_lock.release = mocker.AsyncMock()
|
||||
mock_redis_client = mocker.MagicMock()
|
||||
mock_redis_client.lock = mocker.MagicMock(return_value=mock_redis_lock)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_redis_async",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=mock_redis_client,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=session,
|
||||
)
|
||||
|
||||
result = await append_and_save_message(
|
||||
session.session_id, ChatMessage(role="user", content="hello")
|
||||
)
|
||||
assert result is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="session")
|
||||
async def test_append_and_save_message_appends_new_message(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""append_and_save_message appends a non-duplicate message and returns the session."""
|
||||
|
||||
session = _make_session_with_messages(
|
||||
ChatMessage(role="user", content="hello"),
|
||||
ChatMessage(role="assistant", content="hi"),
|
||||
)
|
||||
mock_redis_lock = mocker.AsyncMock()
|
||||
mock_redis_lock.acquire = mocker.AsyncMock(return_value=True)
|
||||
mock_redis_lock.release = mocker.AsyncMock()
|
||||
mock_redis_client = mocker.MagicMock()
|
||||
mock_redis_client.lock = mocker.MagicMock(return_value=mock_redis_lock)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_redis_async",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=mock_redis_client,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=session,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model._save_session_to_db",
|
||||
new_callable=mocker.AsyncMock,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.chat_db",
|
||||
return_value=mocker.MagicMock(
|
||||
get_next_sequence=mocker.AsyncMock(return_value=2)
|
||||
),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.cache_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
)
|
||||
|
||||
new_msg = ChatMessage(role="user", content="second message")
|
||||
result = await append_and_save_message(session.session_id, new_msg)
|
||||
assert result is not None
|
||||
assert result.messages[-1].content == "second message"
|
||||
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="session")
|
||||
async def test_append_and_save_message_raises_when_session_not_found(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""append_and_save_message raises ValueError when the session does not exist."""
|
||||
|
||||
mock_redis_lock = mocker.AsyncMock()
|
||||
mock_redis_lock.acquire = mocker.AsyncMock(return_value=True)
|
||||
mock_redis_lock.release = mocker.AsyncMock()
|
||||
mock_redis_client = mocker.MagicMock()
|
||||
mock_redis_client.lock = mocker.MagicMock(return_value=mock_redis_lock)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_redis_async",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=mock_redis_client,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=None,
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="not found"):
|
||||
await append_and_save_message(
|
||||
"missing-session-id", ChatMessage(role="user", content="hi")
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="session")
|
||||
async def test_append_and_save_message_uses_db_when_lock_degraded(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""When the Redis lock times out (acquired=False), the fallback reads from DB."""
|
||||
|
||||
session = _make_session_with_messages(
|
||||
ChatMessage(role="assistant", content="hi"),
|
||||
)
|
||||
mock_redis_lock = mocker.AsyncMock()
|
||||
mock_redis_lock.acquire = mocker.AsyncMock(return_value=False)
|
||||
mock_redis_client = mocker.MagicMock()
|
||||
mock_redis_client.lock = mocker.MagicMock(return_value=mock_redis_lock)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_redis_async",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=mock_redis_client,
|
||||
)
|
||||
mock_get_from_db = mocker.patch(
|
||||
"backend.copilot.model._get_session_from_db",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=session,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model._save_session_to_db",
|
||||
new_callable=mocker.AsyncMock,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.chat_db",
|
||||
return_value=mocker.MagicMock(
|
||||
get_next_sequence=mocker.AsyncMock(return_value=1)
|
||||
),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.cache_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
)
|
||||
|
||||
new_msg = ChatMessage(role="user", content="new msg")
|
||||
result = await append_and_save_message(session.session_id, new_msg)
|
||||
# DB path was used (not cache-first)
|
||||
mock_get_from_db.assert_called_once_with(session.session_id)
|
||||
assert result is not None
|
||||
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="session")
|
||||
async def test_append_and_save_message_raises_database_error_on_save_failure(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""When _save_session_to_db fails, append_and_save_message raises DatabaseError."""
|
||||
from backend.util.exceptions import DatabaseError
|
||||
|
||||
session = _make_session_with_messages(
|
||||
ChatMessage(role="assistant", content="hi"),
|
||||
)
|
||||
mock_redis_lock = mocker.AsyncMock()
|
||||
mock_redis_lock.acquire = mocker.AsyncMock(return_value=True)
|
||||
mock_redis_lock.release = mocker.AsyncMock()
|
||||
mock_redis_client = mocker.MagicMock()
|
||||
mock_redis_client.lock = mocker.MagicMock(return_value=mock_redis_lock)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_redis_async",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=mock_redis_client,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=session,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model._save_session_to_db",
|
||||
new_callable=mocker.AsyncMock,
|
||||
side_effect=RuntimeError("db down"),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.chat_db",
|
||||
return_value=mocker.MagicMock(
|
||||
get_next_sequence=mocker.AsyncMock(return_value=1)
|
||||
),
|
||||
)
|
||||
|
||||
with pytest.raises(DatabaseError):
|
||||
await append_and_save_message(
|
||||
session.session_id, ChatMessage(role="user", content="new msg")
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="session")
|
||||
async def test_append_and_save_message_invalidates_cache_on_cache_failure(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""When cache_chat_session fails, invalidate_session_cache is called to avoid stale reads."""
|
||||
|
||||
session = _make_session_with_messages(
|
||||
ChatMessage(role="assistant", content="hi"),
|
||||
)
|
||||
mock_redis_lock = mocker.AsyncMock()
|
||||
mock_redis_lock.acquire = mocker.AsyncMock(return_value=True)
|
||||
mock_redis_lock.release = mocker.AsyncMock()
|
||||
mock_redis_client = mocker.MagicMock()
|
||||
mock_redis_client.lock = mocker.MagicMock(return_value=mock_redis_lock)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_redis_async",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=mock_redis_client,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=session,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model._save_session_to_db",
|
||||
new_callable=mocker.AsyncMock,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.chat_db",
|
||||
return_value=mocker.MagicMock(
|
||||
get_next_sequence=mocker.AsyncMock(return_value=1)
|
||||
),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.cache_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
side_effect=RuntimeError("redis write failed"),
|
||||
)
|
||||
mock_invalidate = mocker.patch(
|
||||
"backend.copilot.model.invalidate_session_cache",
|
||||
new_callable=mocker.AsyncMock,
|
||||
)
|
||||
|
||||
result = await append_and_save_message(
|
||||
session.session_id, ChatMessage(role="user", content="new msg")
|
||||
)
|
||||
# DB write succeeded, cache invalidation was called
|
||||
mock_invalidate.assert_called_once_with(session.session_id)
|
||||
assert result is not None
|
||||
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="session")
|
||||
async def test_append_and_save_message_uses_db_when_redis_unavailable(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""When get_redis_async raises, _get_session_lock yields False (degraded) and DB is read."""
|
||||
|
||||
session = _make_session_with_messages(
|
||||
ChatMessage(role="assistant", content="hi"),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_redis_async",
|
||||
new_callable=mocker.AsyncMock,
|
||||
side_effect=ConnectionError("redis down"),
|
||||
)
|
||||
mock_get_from_db = mocker.patch(
|
||||
"backend.copilot.model._get_session_from_db",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=session,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model._save_session_to_db",
|
||||
new_callable=mocker.AsyncMock,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.chat_db",
|
||||
return_value=mocker.MagicMock(
|
||||
get_next_sequence=mocker.AsyncMock(return_value=1)
|
||||
),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.cache_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
)
|
||||
|
||||
new_msg = ChatMessage(role="user", content="new msg")
|
||||
result = await append_and_save_message(session.session_id, new_msg)
|
||||
mock_get_from_db.assert_called_once_with(session.session_id)
|
||||
assert result is not None
|
||||
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="session")
|
||||
async def test_append_and_save_message_lock_release_failure_is_ignored(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""If lock.release() raises, the exception is swallowed (TTL will clean up)."""
|
||||
|
||||
session = _make_session_with_messages(
|
||||
ChatMessage(role="assistant", content="hi"),
|
||||
)
|
||||
mock_redis_lock = mocker.AsyncMock()
|
||||
mock_redis_lock.acquire = mocker.AsyncMock(return_value=True)
|
||||
mock_redis_lock.release = mocker.AsyncMock(
|
||||
side_effect=RuntimeError("release failed")
|
||||
)
|
||||
mock_redis_client = mocker.MagicMock()
|
||||
mock_redis_client.lock = mocker.MagicMock(return_value=mock_redis_lock)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_redis_async",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=mock_redis_client,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.get_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
return_value=session,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model._save_session_to_db",
|
||||
new_callable=mocker.AsyncMock,
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.chat_db",
|
||||
return_value=mocker.MagicMock(
|
||||
get_next_sequence=mocker.AsyncMock(return_value=1)
|
||||
),
|
||||
)
|
||||
mocker.patch(
|
||||
"backend.copilot.model.cache_chat_session",
|
||||
new_callable=mocker.AsyncMock,
|
||||
)
|
||||
|
||||
new_msg = ChatMessage(role="user", content="new msg")
|
||||
result = await append_and_save_message(session.session_id, new_msg)
|
||||
assert result is not None
|
||||
|
||||
@@ -1,384 +0,0 @@
|
||||
"""Shared helpers for draining and injecting pending messages.
|
||||
|
||||
Used by both the baseline and SDK copilot paths to avoid duplicating
|
||||
the try/except drain, format, insert, and persist patterns.
|
||||
|
||||
Also provides the call-rate-limit check for the queue endpoint so
|
||||
routes.py stays free of Redis/Lua details.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Callable
|
||||
|
||||
from fastapi import HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.copilot.model import ChatMessage, upsert_chat_session
|
||||
from backend.copilot.pending_messages import (
|
||||
MAX_PENDING_MESSAGES,
|
||||
PendingMessage,
|
||||
PendingMessageContext,
|
||||
drain_pending_messages,
|
||||
format_pending_as_user_message,
|
||||
push_pending_message,
|
||||
)
|
||||
from backend.copilot.stream_registry import get_session as get_active_session_meta
|
||||
from backend.data.redis_client import get_redis_async
|
||||
from backend.data.redis_helpers import incr_with_ttl
|
||||
from backend.data.workspace import resolve_workspace_files
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from backend.copilot.model import ChatSession
|
||||
from backend.copilot.transcript_builder import TranscriptBuilder
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Call-frequency cap for the pending-message endpoint. The token-budget
|
||||
# check guards against overspend but not rapid-fire pushes from a client
|
||||
# with a large budget.
|
||||
PENDING_CALL_LIMIT = 30
|
||||
PENDING_CALL_WINDOW_SECONDS = 60
|
||||
_PENDING_CALL_KEY_PREFIX = "copilot:pending:calls:"
|
||||
|
||||
|
||||
async def is_turn_in_flight(session_id: str) -> bool:
|
||||
"""Return ``True`` when a copilot turn is actively running for *session_id*.
|
||||
|
||||
Used by the unified POST /stream entry point and the autopilot block so
|
||||
a second message arriving while an earlier turn is still executing gets
|
||||
queued into the pending buffer instead of racing the in-flight turn on
|
||||
the cluster lock.
|
||||
"""
|
||||
active = await get_active_session_meta(session_id)
|
||||
return active is not None and active.status == "running"
|
||||
|
||||
|
||||
class QueuePendingMessageResponse(BaseModel):
|
||||
"""Response returned by ``POST /stream`` with status 202 when a message
|
||||
is queued because the session already has a turn in flight.
|
||||
|
||||
- ``buffer_length``: how many messages are now in the session's
|
||||
pending buffer (after this push)
|
||||
- ``max_buffer_length``: the per-session cap (server-side constant)
|
||||
- ``turn_in_flight``: ``True`` if a copilot turn was running when
|
||||
we checked — purely informational for UX feedback. Always ``True``
|
||||
for responses from ``POST /stream`` with status 202.
|
||||
"""
|
||||
|
||||
buffer_length: int
|
||||
max_buffer_length: int
|
||||
turn_in_flight: bool
|
||||
|
||||
|
||||
async def queue_user_message(
|
||||
*,
|
||||
session_id: str,
|
||||
message: str,
|
||||
context: PendingMessageContext | None = None,
|
||||
file_ids: list[str] | None = None,
|
||||
) -> QueuePendingMessageResponse:
|
||||
"""Push *message* into the per-session pending buffer.
|
||||
|
||||
The shared primitive for "a message arrived while a turn is in flight" —
|
||||
called from the unified POST /stream handler and the autopilot block.
|
||||
Call-frequency rate limiting is the caller's responsibility (HTTP path
|
||||
enforces it; internal block callers skip it).
|
||||
"""
|
||||
pending = PendingMessage(
|
||||
content=message,
|
||||
file_ids=file_ids or [],
|
||||
context=context,
|
||||
)
|
||||
new_len = await push_pending_message(session_id, pending)
|
||||
return QueuePendingMessageResponse(
|
||||
buffer_length=new_len,
|
||||
max_buffer_length=MAX_PENDING_MESSAGES,
|
||||
turn_in_flight=await is_turn_in_flight(session_id),
|
||||
)
|
||||
|
||||
|
||||
async def queue_pending_for_http(
|
||||
*,
|
||||
session_id: str,
|
||||
user_id: str,
|
||||
message: str,
|
||||
context: dict[str, str] | None,
|
||||
file_ids: list[str] | None,
|
||||
) -> QueuePendingMessageResponse:
|
||||
"""HTTP-facing wrapper around :func:`queue_user_message`.
|
||||
|
||||
Owns the HTTP-only concerns that sat inline in ``stream_chat_post``:
|
||||
|
||||
1. Per-user call-rate cap (429 on overflow).
|
||||
2. File-ID sanitisation against the user's own workspace.
|
||||
3. ``{url, content}`` dict → ``PendingMessageContext`` coercion.
|
||||
4. Push via ``queue_user_message``.
|
||||
|
||||
Raises :class:`HTTPException` with status 429 if the rate cap is hit;
|
||||
otherwise returns the ``QueuePendingMessageResponse`` the handler can
|
||||
serialise 1:1 into the 202 body.
|
||||
"""
|
||||
call_count = await check_pending_call_rate(user_id)
|
||||
if call_count > PENDING_CALL_LIMIT:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=(
|
||||
f"Too many queued message requests this minute: limit is "
|
||||
f"{PENDING_CALL_LIMIT} per {PENDING_CALL_WINDOW_SECONDS}s "
|
||||
"across all sessions"
|
||||
),
|
||||
)
|
||||
|
||||
sanitized_file_ids: list[str] | None = None
|
||||
if file_ids:
|
||||
files = await resolve_workspace_files(user_id, file_ids)
|
||||
sanitized_file_ids = [wf.id for wf in files] or None
|
||||
|
||||
# ``PendingMessageContext`` uses the default ``extra='ignore'`` so
|
||||
# unknown keys in the loose HTTP-level ``context`` dict are silently
|
||||
# dropped rather than raising ``ValidationError`` + 500ing (sentry
|
||||
# r3105553772). The strict mode would only help protect against
|
||||
# typos, but the upstream ``StreamChatRequest.context: dict[str, str]``
|
||||
# is already schemaless, so the strict mode adds no real safety.
|
||||
queue_context = PendingMessageContext.model_validate(context) if context else None
|
||||
return await queue_user_message(
|
||||
session_id=session_id,
|
||||
message=message,
|
||||
context=queue_context,
|
||||
file_ids=sanitized_file_ids,
|
||||
)
|
||||
|
||||
|
||||
async def check_pending_call_rate(user_id: str) -> int:
|
||||
"""Increment and return the per-user push counter for the current window.
|
||||
|
||||
The counter is **user-global**: it counts pushes across ALL sessions
|
||||
belonging to the user, not per-session. This prevents a client from
|
||||
bypassing the cap by spreading rapid pushes across many sessions.
|
||||
|
||||
Returns the new call count. Raises nothing — callers compare the
|
||||
return value against ``PENDING_CALL_LIMIT`` and decide what to do.
|
||||
Fails open (returns 0) if Redis is unavailable so the endpoint stays
|
||||
usable during Redis hiccups.
|
||||
"""
|
||||
try:
|
||||
redis = await get_redis_async()
|
||||
key = f"{_PENDING_CALL_KEY_PREFIX}{user_id}"
|
||||
return await incr_with_ttl(redis, key, PENDING_CALL_WINDOW_SECONDS)
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"pending_message_helpers: call-rate check failed for user=%s, failing open",
|
||||
user_id,
|
||||
)
|
||||
return 0
|
||||
|
||||
|
||||
async def drain_pending_safe(
|
||||
session_id: str, log_prefix: str = ""
|
||||
) -> list[PendingMessage]:
|
||||
"""Drain the pending buffer and return the full ``PendingMessage`` objects.
|
||||
|
||||
Returns ``[]`` on any Redis error so callers can always treat the
|
||||
result as a plain list. Callers that only need the rendered string
|
||||
(turn-start injection, auto-continue combined prompt) wrap this with
|
||||
:func:`pending_texts_from` — we return the structured objects so the
|
||||
re-queue rollback path can preserve ``file_ids`` / ``context`` that
|
||||
would otherwise be stripped by a text-only conversion.
|
||||
"""
|
||||
try:
|
||||
return await drain_pending_messages(session_id)
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"%s drain_pending_messages failed, skipping",
|
||||
log_prefix or "pending_messages",
|
||||
exc_info=True,
|
||||
)
|
||||
return []
|
||||
|
||||
|
||||
def pending_texts_from(pending: list[PendingMessage]) -> list[str]:
|
||||
"""Render a list of ``PendingMessage`` objects into plain text strings.
|
||||
|
||||
Shared helper for the two callers that need the rendered form:
|
||||
turn-start injection (bundles the pending block into the user prompt)
|
||||
and the auto-continue combined-message path.
|
||||
"""
|
||||
return [format_pending_as_user_message(pm)["content"] for pm in pending]
|
||||
|
||||
|
||||
def combine_pending_with_current(
|
||||
pending: list[PendingMessage],
|
||||
current_message: str | None,
|
||||
*,
|
||||
request_arrival_at: float,
|
||||
) -> str:
|
||||
"""Order pending messages around *current_message* by typing time.
|
||||
|
||||
Pending messages whose ``enqueued_at`` is strictly greater than
|
||||
``request_arrival_at`` were typed AFTER the user hit enter to start
|
||||
the current turn (the "race" path: queued into the pending buffer
|
||||
while ``/stream`` was still processing on the server). They belong
|
||||
chronologically AFTER the current message.
|
||||
|
||||
Pending messages whose ``enqueued_at`` is less than or equal to
|
||||
``request_arrival_at`` were typed BEFORE the current turn — usually
|
||||
from a prior in-flight window that auto-continue didn't consume.
|
||||
They belong BEFORE the current message.
|
||||
|
||||
Stable-sort within each bucket preserves enqueue order for messages
|
||||
typed in the same phase. Legacy ``PendingMessage`` objects with no
|
||||
``enqueued_at`` (written by older workers, defaulted to 0.0) sort as
|
||||
"before everything" — the pre-fix behaviour, which is a safe default
|
||||
for the rare queue entries that outlived a deploy.
|
||||
"""
|
||||
before: list[PendingMessage] = []
|
||||
after: list[PendingMessage] = []
|
||||
for pm in pending:
|
||||
if request_arrival_at > 0 and pm.enqueued_at > request_arrival_at:
|
||||
after.append(pm)
|
||||
else:
|
||||
before.append(pm)
|
||||
parts = pending_texts_from(before)
|
||||
if current_message and current_message.strip():
|
||||
parts.append(current_message)
|
||||
parts.extend(pending_texts_from(after))
|
||||
return "\n\n".join(parts)
|
||||
|
||||
|
||||
def insert_pending_before_last(session: "ChatSession", texts: list[str]) -> None:
|
||||
"""Insert pending messages into *session* just before the last message.
|
||||
|
||||
Pending messages were queued during the previous turn, so they belong
|
||||
chronologically before the current user message that was already
|
||||
appended via ``maybe_append_user_message``. Inserting at ``len-1``
|
||||
preserves that order: [...history, pending_1, pending_2, current_msg].
|
||||
|
||||
The caller must have already appended the current user message before
|
||||
calling this function. If ``session.messages`` is unexpectedly empty,
|
||||
a warning is logged and the messages are appended at index 0 so they
|
||||
are not silently lost.
|
||||
"""
|
||||
if not texts:
|
||||
return
|
||||
if not session.messages:
|
||||
logger.warning(
|
||||
"insert_pending_before_last: session.messages is empty — "
|
||||
"current user message was not appended before drain; "
|
||||
"inserting pending messages at index 0"
|
||||
)
|
||||
insert_idx = max(0, len(session.messages) - 1)
|
||||
for i, content in enumerate(texts):
|
||||
session.messages.insert(
|
||||
insert_idx + i, ChatMessage(role="user", content=content)
|
||||
)
|
||||
|
||||
|
||||
async def persist_session_safe(
|
||||
session: "ChatSession", log_prefix: str = ""
|
||||
) -> "ChatSession":
|
||||
"""Persist *session* to the DB, returning the (possibly updated) session.
|
||||
|
||||
Swallows transient DB errors so a failing persist doesn't discard
|
||||
messages already popped from Redis — the turn continues from memory.
|
||||
"""
|
||||
try:
|
||||
return await upsert_chat_session(session)
|
||||
except Exception as err:
|
||||
logger.warning(
|
||||
"%s Failed to persist pending messages: %s",
|
||||
log_prefix or "pending_messages",
|
||||
err,
|
||||
)
|
||||
return session
|
||||
|
||||
|
||||
async def persist_pending_as_user_rows(
|
||||
session: "ChatSession",
|
||||
transcript_builder: "TranscriptBuilder",
|
||||
pending: list[PendingMessage],
|
||||
*,
|
||||
log_prefix: str,
|
||||
content_of: Callable[[PendingMessage], str] = lambda pm: pm.content,
|
||||
on_rollback: Callable[[int], None] | None = None,
|
||||
) -> bool:
|
||||
"""Append ``pending`` as user rows to *session* + *transcript_builder*,
|
||||
persist, and roll back + re-queue if the persist silently failed.
|
||||
|
||||
This is the shared mid-turn follow-up persist used by both the baseline
|
||||
and SDK paths — they differ only in (a) how they derive the displayed
|
||||
string from a ``PendingMessage`` and (b) what extra per-path state
|
||||
(e.g. ``openai_messages``) needs trimming on rollback. Those variance
|
||||
points are exposed as ``content_of`` and ``on_rollback``.
|
||||
|
||||
Flow:
|
||||
1. Snapshot transcript + record the session.messages length.
|
||||
2. Append one user row per pending message to both stores.
|
||||
3. ``persist_session_safe`` — swallowed errors mean no sequences get
|
||||
back-filled, which we use as the failure signal.
|
||||
4. If any newly-appended row has ``sequence is None`` → rollback:
|
||||
delete the appended rows, restore the transcript snapshot, call
|
||||
``on_rollback(anchor)`` for the caller's own state, then re-push
|
||||
each ``PendingMessage`` into the primary pending buffer so the
|
||||
next turn-start drain picks them up.
|
||||
|
||||
Returns ``True`` when the rows were persisted with sequences, ``False``
|
||||
when the rollback path fired. Callers can use this to decide whether
|
||||
to log success or continue a retry loop.
|
||||
"""
|
||||
if not pending:
|
||||
return True
|
||||
|
||||
session_anchor = len(session.messages)
|
||||
transcript_snapshot = transcript_builder.snapshot()
|
||||
|
||||
for pm in pending:
|
||||
content = content_of(pm)
|
||||
session.messages.append(ChatMessage(role="user", content=content))
|
||||
transcript_builder.append_user(content=content)
|
||||
|
||||
# ``persist_session_safe`` may return a ``model_copy`` of *session* (e.g.
|
||||
# when ``upsert_chat_session`` patches a concurrently-updated title).
|
||||
# Do NOT reassign the caller's reference — the caller already pushed the
|
||||
# rows into its own ``session.messages`` above, and rollback below MUST
|
||||
# delete from that same list. Inspect the returned object only to learn
|
||||
# whether sequences were back-filled; if so, copy them onto the caller's
|
||||
# objects so the session stays internally consistent for downstream
|
||||
# ``append_and_save_message`` calls.
|
||||
persisted = await persist_session_safe(session, log_prefix)
|
||||
persisted_tail = persisted.messages[session_anchor:]
|
||||
if len(persisted_tail) == len(pending) and all(
|
||||
m.sequence is not None for m in persisted_tail
|
||||
):
|
||||
for caller_msg, persisted_msg in zip(
|
||||
session.messages[session_anchor:], persisted_tail
|
||||
):
|
||||
caller_msg.sequence = persisted_msg.sequence
|
||||
newly_appended = session.messages[session_anchor:]
|
||||
|
||||
if any(m.sequence is None for m in newly_appended):
|
||||
logger.warning(
|
||||
"%s Mid-turn follow-up persist did not back-fill sequences; "
|
||||
"rolling back %d row(s) and re-queueing into the primary buffer",
|
||||
log_prefix,
|
||||
len(pending),
|
||||
)
|
||||
del session.messages[session_anchor:]
|
||||
transcript_builder.restore(transcript_snapshot)
|
||||
if on_rollback is not None:
|
||||
on_rollback(session_anchor)
|
||||
for pm in pending:
|
||||
try:
|
||||
await push_pending_message(session.session_id, pm)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"%s Failed to re-queue mid-turn follow-up on rollback",
|
||||
log_prefix,
|
||||
)
|
||||
return False
|
||||
|
||||
logger.info(
|
||||
"%s Persisted %d mid-turn follow-up user row(s)",
|
||||
log_prefix,
|
||||
len(pending),
|
||||
)
|
||||
return True
|
||||
@@ -1,472 +0,0 @@
|
||||
"""Unit tests for pending_message_helpers."""
|
||||
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.copilot import pending_message_helpers as helpers_module
|
||||
from backend.copilot.pending_message_helpers import (
|
||||
PENDING_CALL_LIMIT,
|
||||
check_pending_call_rate,
|
||||
combine_pending_with_current,
|
||||
drain_pending_safe,
|
||||
insert_pending_before_last,
|
||||
persist_session_safe,
|
||||
)
|
||||
from backend.copilot.pending_messages import PendingMessage
|
||||
|
||||
# ── check_pending_call_rate ────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_check_pending_call_rate_returns_count(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
helpers_module, "get_redis_async", AsyncMock(return_value=MagicMock())
|
||||
)
|
||||
monkeypatch.setattr(helpers_module, "incr_with_ttl", AsyncMock(return_value=3))
|
||||
|
||||
result = await check_pending_call_rate("user-1")
|
||||
assert result == 3
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_check_pending_call_rate_fails_open_on_redis_error(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
helpers_module,
|
||||
"get_redis_async",
|
||||
AsyncMock(side_effect=ConnectionError("down")),
|
||||
)
|
||||
|
||||
result = await check_pending_call_rate("user-1")
|
||||
assert result == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_check_pending_call_rate_at_limit(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
helpers_module, "get_redis_async", AsyncMock(return_value=MagicMock())
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
helpers_module,
|
||||
"incr_with_ttl",
|
||||
AsyncMock(return_value=PENDING_CALL_LIMIT + 1),
|
||||
)
|
||||
|
||||
result = await check_pending_call_rate("user-1")
|
||||
assert result > PENDING_CALL_LIMIT
|
||||
|
||||
|
||||
# ── drain_pending_safe ─────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_pending_safe_returns_pending_messages(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""``drain_pending_safe`` now returns the structured ``PendingMessage``
|
||||
objects (not pre-formatted strings) so the auto-continue re-queue path
|
||||
can preserve ``file_ids`` / ``context`` on rollback."""
|
||||
msgs = [
|
||||
PendingMessage(content="hello", file_ids=["f1"]),
|
||||
PendingMessage(content="world"),
|
||||
]
|
||||
monkeypatch.setattr(
|
||||
helpers_module, "drain_pending_messages", AsyncMock(return_value=msgs)
|
||||
)
|
||||
|
||||
result = await drain_pending_safe("sess-1")
|
||||
assert result == msgs
|
||||
# Structured metadata survives — the bug r3105523410 guard.
|
||||
assert result[0].file_ids == ["f1"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_pending_safe_returns_empty_on_error(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
helpers_module,
|
||||
"drain_pending_messages",
|
||||
AsyncMock(side_effect=RuntimeError("redis down")),
|
||||
)
|
||||
|
||||
result = await drain_pending_safe("sess-1", "[Test]")
|
||||
assert result == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_pending_safe_empty_buffer(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(
|
||||
helpers_module, "drain_pending_messages", AsyncMock(return_value=[])
|
||||
)
|
||||
|
||||
result = await drain_pending_safe("sess-1")
|
||||
assert result == []
|
||||
|
||||
|
||||
# ── combine_pending_with_current ───────────────────────────────────────
|
||||
|
||||
|
||||
def test_combine_before_current_when_pending_older() -> None:
|
||||
"""Pending typed before the /stream request → goes ahead of current
|
||||
(prior-turn / inter-turn case)."""
|
||||
pending = [
|
||||
PendingMessage(content="older_a", enqueued_at=100.0),
|
||||
PendingMessage(content="older_b", enqueued_at=110.0),
|
||||
]
|
||||
result = combine_pending_with_current(
|
||||
pending, "current_msg", request_arrival_at=120.0
|
||||
)
|
||||
assert result == "older_a\n\nolder_b\n\ncurrent_msg"
|
||||
|
||||
|
||||
def test_combine_after_current_when_pending_newer() -> None:
|
||||
"""Pending queued AFTER the /stream request arrived → goes after
|
||||
current. This is the race path where user hits enter twice in quick
|
||||
succession (second press goes through the queue endpoint while the
|
||||
first /stream is still processing)."""
|
||||
pending = [
|
||||
PendingMessage(content="race_followup", enqueued_at=125.0),
|
||||
]
|
||||
result = combine_pending_with_current(
|
||||
pending, "current_msg", request_arrival_at=120.0
|
||||
)
|
||||
assert result == "current_msg\n\nrace_followup"
|
||||
|
||||
|
||||
def test_combine_mixed_before_and_after() -> None:
|
||||
"""Mixed bucket: older items first, current, then newer race items."""
|
||||
pending = [
|
||||
PendingMessage(content="way_older", enqueued_at=50.0),
|
||||
PendingMessage(content="race_fast_follow", enqueued_at=125.0),
|
||||
PendingMessage(content="also_older", enqueued_at=80.0),
|
||||
]
|
||||
result = combine_pending_with_current(
|
||||
pending, "current_msg", request_arrival_at=120.0
|
||||
)
|
||||
# Enqueue order preserved within each bucket (stable partition).
|
||||
assert result == "way_older\n\nalso_older\n\ncurrent_msg\n\nrace_fast_follow"
|
||||
|
||||
|
||||
def test_combine_no_current_joins_pending() -> None:
|
||||
"""Auto-continue case: no current message, just drained pending."""
|
||||
pending = [PendingMessage(content="a"), PendingMessage(content="b")]
|
||||
result = combine_pending_with_current(pending, None, request_arrival_at=0.0)
|
||||
assert result == "a\n\nb"
|
||||
|
||||
|
||||
def test_combine_legacy_zero_timestamp_sorts_before() -> None:
|
||||
"""A ``PendingMessage`` from before this field existed (default 0.0)
|
||||
should sort as "before everything" — safe pre-fix behaviour."""
|
||||
pending = [PendingMessage(content="legacy", enqueued_at=0.0)]
|
||||
result = combine_pending_with_current(
|
||||
pending, "current_msg", request_arrival_at=120.0
|
||||
)
|
||||
assert result == "legacy\n\ncurrent_msg"
|
||||
|
||||
|
||||
def test_combine_missing_request_arrival_falls_back_to_before() -> None:
|
||||
"""If the HTTP handler didn't stamp ``request_arrival_at`` (0.0
|
||||
default — older queue entries) the combine degrades gracefully to
|
||||
the pre-fix behaviour: all pending goes before current."""
|
||||
pending = [
|
||||
PendingMessage(content="a", enqueued_at=500.0),
|
||||
PendingMessage(content="b", enqueued_at=1000.0),
|
||||
]
|
||||
result = combine_pending_with_current(pending, "current", request_arrival_at=0.0)
|
||||
assert result == "a\n\nb\n\ncurrent"
|
||||
|
||||
|
||||
# ── insert_pending_before_last ─────────────────────────────────────────
|
||||
|
||||
|
||||
def _make_session(*contents: str) -> Any:
|
||||
session = MagicMock()
|
||||
session.messages = [MagicMock(role="user", content=c) for c in contents]
|
||||
return session
|
||||
|
||||
|
||||
def test_insert_pending_before_last_single_existing_message() -> None:
|
||||
session = _make_session("current")
|
||||
insert_pending_before_last(session, ["queued"])
|
||||
assert session.messages[0].content == "queued"
|
||||
assert session.messages[1].content == "current"
|
||||
|
||||
|
||||
def test_insert_pending_before_last_multiple_pending() -> None:
|
||||
session = _make_session("current")
|
||||
insert_pending_before_last(session, ["p1", "p2"])
|
||||
contents = [m.content for m in session.messages]
|
||||
assert contents == ["p1", "p2", "current"]
|
||||
|
||||
|
||||
def test_insert_pending_before_last_empty_session() -> None:
|
||||
session = _make_session()
|
||||
insert_pending_before_last(session, ["queued"])
|
||||
assert session.messages[0].content == "queued"
|
||||
|
||||
|
||||
def test_insert_pending_before_last_no_texts_is_noop() -> None:
|
||||
session = _make_session("current")
|
||||
insert_pending_before_last(session, [])
|
||||
assert len(session.messages) == 1
|
||||
|
||||
|
||||
# ── persist_session_safe ───────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_persist_session_safe_returns_updated_session(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
original = MagicMock()
|
||||
updated = MagicMock()
|
||||
monkeypatch.setattr(
|
||||
helpers_module, "upsert_chat_session", AsyncMock(return_value=updated)
|
||||
)
|
||||
|
||||
result = await persist_session_safe(original, "[Test]")
|
||||
assert result is updated
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_persist_session_safe_returns_original_on_error(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
original = MagicMock()
|
||||
monkeypatch.setattr(
|
||||
helpers_module,
|
||||
"upsert_chat_session",
|
||||
AsyncMock(side_effect=Exception("db error")),
|
||||
)
|
||||
|
||||
result = await persist_session_safe(original, "[Test]")
|
||||
assert result is original
|
||||
|
||||
|
||||
# ── persist_pending_as_user_rows ───────────────────────────────────────
|
||||
|
||||
|
||||
class _FakeTranscript:
|
||||
"""Minimal TranscriptBuilder shim — records append_user + snapshot/restore."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.entries: list[str] = []
|
||||
|
||||
def append_user(self, content: str, uuid: str | None = None) -> None:
|
||||
self.entries.append(content)
|
||||
|
||||
def snapshot(self) -> list[str]:
|
||||
return list(self.entries)
|
||||
|
||||
def restore(self, snap: list[str]) -> None:
|
||||
self.entries = list(snap)
|
||||
|
||||
|
||||
def _make_chat_message_class(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> Any:
|
||||
"""Return a simple ChatMessage stand-in that tracks sequence."""
|
||||
|
||||
class _Msg:
|
||||
def __init__(self, role: str, content: str) -> None:
|
||||
self.role = role
|
||||
self.content = content
|
||||
self.sequence: int | None = None
|
||||
|
||||
monkeypatch.setattr(helpers_module, "ChatMessage", _Msg)
|
||||
return _Msg
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_persist_pending_empty_list_is_noop(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
|
||||
|
||||
_make_chat_message_class(monkeypatch)
|
||||
session = MagicMock()
|
||||
session.messages = []
|
||||
tb = _FakeTranscript()
|
||||
monkeypatch.setattr(helpers_module, "upsert_chat_session", AsyncMock())
|
||||
monkeypatch.setattr(helpers_module, "push_pending_message", AsyncMock())
|
||||
|
||||
ok = await persist_pending_as_user_rows(session, tb, [], log_prefix="[T]")
|
||||
assert ok is True
|
||||
assert session.messages == []
|
||||
assert tb.entries == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_persist_pending_happy_path_appends_and_returns_true(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
|
||||
from backend.copilot.pending_messages import PendingMessage as PM
|
||||
|
||||
_make_chat_message_class(monkeypatch)
|
||||
session = MagicMock()
|
||||
session.session_id = "sess"
|
||||
session.messages = []
|
||||
tb = _FakeTranscript()
|
||||
|
||||
async def _fake_upsert(sess: Any) -> Any:
|
||||
# Simulate the DB back-filling sequence numbers on success.
|
||||
for i, m in enumerate(sess.messages):
|
||||
m.sequence = i
|
||||
return sess
|
||||
|
||||
monkeypatch.setattr(helpers_module, "upsert_chat_session", _fake_upsert)
|
||||
push_mock = AsyncMock()
|
||||
monkeypatch.setattr(helpers_module, "push_pending_message", push_mock)
|
||||
|
||||
pending = [PM(content="a"), PM(content="b")]
|
||||
ok = await persist_pending_as_user_rows(session, tb, pending, log_prefix="[T]")
|
||||
assert ok is True
|
||||
assert [m.content for m in session.messages] == ["a", "b"]
|
||||
assert tb.entries == ["a", "b"]
|
||||
push_mock.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_persist_pending_rollback_when_sequence_missing(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
|
||||
from backend.copilot.pending_messages import PendingMessage as PM
|
||||
|
||||
_make_chat_message_class(monkeypatch)
|
||||
session = MagicMock()
|
||||
session.session_id = "sess"
|
||||
# Prior state — anchor point is len(messages) before the helper runs.
|
||||
session.messages = []
|
||||
tb = _FakeTranscript()
|
||||
tb.entries = ["earlier-entry"]
|
||||
|
||||
async def _fake_upsert_fails_silently(sess: Any) -> Any:
|
||||
# Simulate the "persist swallowed the error" branch — sequences stay None.
|
||||
return sess
|
||||
|
||||
monkeypatch.setattr(
|
||||
helpers_module, "upsert_chat_session", _fake_upsert_fails_silently
|
||||
)
|
||||
push_mock = AsyncMock()
|
||||
monkeypatch.setattr(helpers_module, "push_pending_message", push_mock)
|
||||
|
||||
pending = [PM(content="a"), PM(content="b")]
|
||||
ok = await persist_pending_as_user_rows(session, tb, pending, log_prefix="[T]")
|
||||
|
||||
assert ok is False
|
||||
# Rollback: session.messages trimmed to anchor, transcript restored.
|
||||
assert session.messages == []
|
||||
assert tb.entries == ["earlier-entry"]
|
||||
# Both pending messages re-queued.
|
||||
assert push_mock.await_count == 2
|
||||
assert push_mock.await_args_list[0].args[1] is pending[0]
|
||||
assert push_mock.await_args_list[1].args[1] is pending[1]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_persist_pending_rollback_calls_on_rollback_hook(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""Baseline's openai_messages trim runs via the on_rollback hook."""
|
||||
from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
|
||||
from backend.copilot.pending_messages import PendingMessage as PM
|
||||
|
||||
_make_chat_message_class(monkeypatch)
|
||||
session = MagicMock()
|
||||
session.session_id = "sess"
|
||||
session.messages = []
|
||||
tb = _FakeTranscript()
|
||||
|
||||
async def _fails(sess: Any) -> Any:
|
||||
return sess
|
||||
|
||||
monkeypatch.setattr(helpers_module, "upsert_chat_session", _fails)
|
||||
monkeypatch.setattr(helpers_module, "push_pending_message", AsyncMock())
|
||||
|
||||
on_rollback_calls: list[int] = []
|
||||
|
||||
def _on_rollback(anchor: int) -> None:
|
||||
on_rollback_calls.append(anchor)
|
||||
|
||||
await persist_pending_as_user_rows(
|
||||
session,
|
||||
tb,
|
||||
[PM(content="x")],
|
||||
log_prefix="[T]",
|
||||
on_rollback=_on_rollback,
|
||||
)
|
||||
assert on_rollback_calls == [0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_persist_pending_uses_custom_content_of(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
|
||||
from backend.copilot.pending_messages import PendingMessage as PM
|
||||
|
||||
_make_chat_message_class(monkeypatch)
|
||||
session = MagicMock()
|
||||
session.session_id = "sess"
|
||||
session.messages = []
|
||||
tb = _FakeTranscript()
|
||||
|
||||
async def _ok(sess: Any) -> Any:
|
||||
for i, m in enumerate(sess.messages):
|
||||
m.sequence = i
|
||||
return sess
|
||||
|
||||
monkeypatch.setattr(helpers_module, "upsert_chat_session", _ok)
|
||||
monkeypatch.setattr(helpers_module, "push_pending_message", AsyncMock())
|
||||
|
||||
await persist_pending_as_user_rows(
|
||||
session,
|
||||
tb,
|
||||
[PM(content="raw")],
|
||||
log_prefix="[T]",
|
||||
content_of=lambda pm: f"FORMATTED:{pm.content}",
|
||||
)
|
||||
assert session.messages[0].content == "FORMATTED:raw"
|
||||
assert tb.entries == ["FORMATTED:raw"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_persist_pending_swallows_requeue_errors(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""A broken push_pending_message on rollback must not raise upward —
|
||||
the rollback still needs to trim state even if re-queue fails."""
|
||||
from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
|
||||
from backend.copilot.pending_messages import PendingMessage as PM
|
||||
|
||||
_make_chat_message_class(monkeypatch)
|
||||
session = MagicMock()
|
||||
session.session_id = "sess"
|
||||
session.messages = []
|
||||
tb = _FakeTranscript()
|
||||
|
||||
async def _fails(sess: Any) -> Any:
|
||||
return sess
|
||||
|
||||
monkeypatch.setattr(helpers_module, "upsert_chat_session", _fails)
|
||||
monkeypatch.setattr(
|
||||
helpers_module,
|
||||
"push_pending_message",
|
||||
AsyncMock(side_effect=RuntimeError("redis down")),
|
||||
)
|
||||
|
||||
ok = await persist_pending_as_user_rows(
|
||||
session, tb, [PM(content="x")], log_prefix="[T]"
|
||||
)
|
||||
# Still returns False (rolled back) — exception was logged + swallowed.
|
||||
assert ok is False
|
||||
@@ -1,450 +0,0 @@
|
||||
"""Pending-message buffer for in-flight copilot turns.
|
||||
|
||||
When a user sends a new message while a copilot turn is already executing,
|
||||
instead of blocking the frontend (or queueing a brand-new turn after the
|
||||
current one finishes), we want the new message to be *injected into the
|
||||
running turn* — appended between tool-call rounds so the model sees it
|
||||
before its next LLM call.
|
||||
|
||||
This module provides the cross-process buffer that makes that possible:
|
||||
|
||||
- **Producer** (chat API route): pushes a pending message to Redis and
|
||||
publishes a notification on a pub/sub channel.
|
||||
- **Consumer** (executor running the turn): on each tool-call round,
|
||||
drains the buffer and appends the pending messages to the conversation.
|
||||
|
||||
The Redis list is the durable store; the pub/sub channel is a fast
|
||||
wake-up hint for long-idle consumers (not used by default, but available
|
||||
for future blocking-wait semantics).
|
||||
|
||||
A hard cap of ``MAX_PENDING_MESSAGES`` per session prevents abuse. The
|
||||
buffer is trimmed to the latest ``MAX_PENDING_MESSAGES`` on every push.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from typing import Any, cast
|
||||
|
||||
from pydantic import BaseModel, Field, ValidationError
|
||||
|
||||
from backend.data.redis_client import get_redis_async
|
||||
from backend.data.redis_helpers import capped_rpush
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Per-session cap. Higher values risk a runaway consumer; lower values
|
||||
# risk dropping user input under heavy typing. 10 was chosen as a
|
||||
# reasonable ceiling — a user typing faster than the copilot can drain
|
||||
# between tool rounds is already an unusual usage pattern.
|
||||
MAX_PENDING_MESSAGES = 10
|
||||
|
||||
# Redis key + TTL. The buffer is ephemeral: if a turn completes or the
|
||||
# executor dies, the pending messages should either have been drained
|
||||
# already or are safe to drop (the user can resend).
|
||||
_PENDING_KEY_PREFIX = "copilot:pending:"
|
||||
_PENDING_CHANNEL_PREFIX = "copilot:pending:notify:"
|
||||
_PENDING_TTL_SECONDS = 3600 # 1 hour — matches stream_ttl default
|
||||
|
||||
# Secondary queue that carries drained-but-awaiting-persist PendingMessages
|
||||
# from the MCP tool wrapper (which drains the primary buffer and injects
|
||||
# into tool output for the LLM) to sdk/service.py's _dispatch_response
|
||||
# handler for StreamToolOutputAvailable, which pops and persists them as a
|
||||
# separate user row chronologically after the tool_result row. This is the
|
||||
# hand-off between "Claude saw the follow-up mid-turn" (wrapper) and "UI
|
||||
# renders a user bubble for it" (service). Rollback path re-queues into
|
||||
# the PRIMARY buffer so the next turn-start drain picks them up if the
|
||||
# user-row persist fails.
|
||||
_PERSIST_QUEUE_KEY_PREFIX = "copilot:pending-persist:"
|
||||
|
||||
# Payload sent on the pub/sub notify channel. Subscribers treat any
|
||||
# message as a wake-up hint; the value itself is not meaningful.
|
||||
_NOTIFY_PAYLOAD = "1"
|
||||
|
||||
|
||||
class PendingMessageContext(BaseModel):
|
||||
"""Structured page context attached to a pending message.
|
||||
|
||||
Default ``extra='ignore'`` (pydantic's default): unknown keys from
|
||||
the loose HTTP-level ``StreamChatRequest.context: dict[str, str]``
|
||||
are silently dropped rather than raising ``ValidationError`` on
|
||||
forward-compat additions. The strict ``extra='forbid'`` mode was
|
||||
removed after sentry r3105553772 — strict validation at this
|
||||
boundary only added a 500 footgun; the upstream request model is
|
||||
already schemaless so strict mode protects nothing.
|
||||
"""
|
||||
|
||||
url: str | None = Field(default=None, max_length=2_000)
|
||||
content: str | None = Field(default=None, max_length=32_000)
|
||||
|
||||
|
||||
class PendingMessage(BaseModel):
|
||||
"""A user message queued for injection into an in-flight turn."""
|
||||
|
||||
content: str = Field(min_length=1, max_length=32_000)
|
||||
file_ids: list[str] = Field(default_factory=list, max_length=20)
|
||||
context: PendingMessageContext | None = None
|
||||
# Wall-clock time (unix seconds, float) the message was queued by the
|
||||
# user. Used by the turn-start drain to order pending relative to the
|
||||
# turn's ``current`` message: items typed *before* the current's
|
||||
# /stream arrival go ahead of it; items typed *after* (race path,
|
||||
# queued while the /stream HTTP request was still processing) go
|
||||
# after. Defaults to 0.0 for backward compatibility with entries
|
||||
# written before this field existed — those sort as "before everything"
|
||||
# which matches the pre-fix behaviour.
|
||||
enqueued_at: float = Field(default_factory=time.time)
|
||||
|
||||
|
||||
def _buffer_key(session_id: str) -> str:
|
||||
return f"{_PENDING_KEY_PREFIX}{session_id}"
|
||||
|
||||
|
||||
def _notify_channel(session_id: str) -> str:
|
||||
return f"{_PENDING_CHANNEL_PREFIX}{session_id}"
|
||||
|
||||
|
||||
def _decode_redis_item(item: Any) -> str:
|
||||
"""Decode a redis-py list item to a str.
|
||||
|
||||
redis-py returns ``bytes`` when ``decode_responses=False`` and ``str``
|
||||
when ``decode_responses=True``. This helper handles both so callers
|
||||
don't have to repeat the isinstance guard.
|
||||
"""
|
||||
return item.decode("utf-8") if isinstance(item, bytes) else str(item)
|
||||
|
||||
|
||||
async def push_pending_message(
|
||||
session_id: str,
|
||||
message: PendingMessage,
|
||||
) -> int:
|
||||
"""Append a pending message to the session's buffer.
|
||||
|
||||
Returns the new buffer length. Enforces ``MAX_PENDING_MESSAGES`` by
|
||||
trimming from the left (oldest) — the newest message always wins if
|
||||
the user has been typing faster than the copilot can drain.
|
||||
|
||||
Delegates to :func:`backend.data.redis_helpers.capped_rpush` so RPUSH
|
||||
+ LTRIM + EXPIRE + LLEN run atomically (MULTI/EXEC) in one round
|
||||
trip; a concurrent drain (LPOP) can no longer observe the list
|
||||
temporarily over ``MAX_PENDING_MESSAGES``.
|
||||
|
||||
Note on durability: if the executor turn crashes after a push but before
|
||||
the drain window runs, the message remains in Redis until the TTL expires
|
||||
(``_PENDING_TTL_SECONDS``, currently 1 hour). It is delivered on the
|
||||
next turn that drains the buffer. If no turn runs within the TTL the
|
||||
message is silently dropped; the user may resend it.
|
||||
"""
|
||||
redis = await get_redis_async()
|
||||
key = _buffer_key(session_id)
|
||||
payload = message.model_dump_json()
|
||||
|
||||
new_length = await capped_rpush(
|
||||
redis,
|
||||
key,
|
||||
payload,
|
||||
max_len=MAX_PENDING_MESSAGES,
|
||||
ttl_seconds=_PENDING_TTL_SECONDS,
|
||||
)
|
||||
|
||||
# Fire-and-forget notify. Subscribers use this as a wake-up hint;
|
||||
# the buffer itself is authoritative so a lost notify is harmless.
|
||||
try:
|
||||
await redis.publish(_notify_channel(session_id), _NOTIFY_PAYLOAD)
|
||||
except Exception as e: # pragma: no cover
|
||||
logger.warning("pending_messages: publish failed for %s: %s", session_id, e)
|
||||
|
||||
logger.info(
|
||||
"pending_messages: pushed message to session=%s (buffer_len=%d)",
|
||||
session_id,
|
||||
new_length,
|
||||
)
|
||||
return new_length
|
||||
|
||||
|
||||
async def drain_pending_messages(session_id: str) -> list[PendingMessage]:
|
||||
"""Atomically pop all pending messages for *session_id*.
|
||||
|
||||
Returns them in enqueue order (oldest first). Uses ``LPOP`` with a
|
||||
count so the read+delete is a single Redis round trip. If the list
|
||||
is empty or missing, returns ``[]``.
|
||||
"""
|
||||
redis = await get_redis_async()
|
||||
key = _buffer_key(session_id)
|
||||
|
||||
# Redis LPOP with count (Redis 6.2+) returns None for missing key,
|
||||
# empty list if we somehow race an empty key, or the popped items.
|
||||
# Draining MAX_PENDING_MESSAGES at once is safe because the push side
|
||||
# uses RPUSH + LTRIM(-MAX_PENDING_MESSAGES, -1) to cap the list to that
|
||||
# same value, so the list can never hold more items than we drain here.
|
||||
# If the cap is raised on the push side, raise the drain count here too
|
||||
# (or switch to a loop drain).
|
||||
lpop_result = await redis.lpop(key, MAX_PENDING_MESSAGES) # type: ignore[assignment]
|
||||
if not lpop_result:
|
||||
return []
|
||||
raw_popped: list[Any] = list(lpop_result)
|
||||
|
||||
# redis-py may return bytes or str depending on decode_responses.
|
||||
decoded: list[str] = [_decode_redis_item(item) for item in raw_popped]
|
||||
|
||||
messages: list[PendingMessage] = []
|
||||
for payload in decoded:
|
||||
try:
|
||||
messages.append(PendingMessage.model_validate(json.loads(payload)))
|
||||
except (json.JSONDecodeError, ValidationError, TypeError, ValueError) as e:
|
||||
logger.warning(
|
||||
"pending_messages: dropping malformed entry for %s: %s",
|
||||
session_id,
|
||||
e,
|
||||
)
|
||||
|
||||
if messages:
|
||||
logger.info(
|
||||
"pending_messages: drained %d messages for session=%s",
|
||||
len(messages),
|
||||
session_id,
|
||||
)
|
||||
return messages
|
||||
|
||||
|
||||
async def peek_pending_count(session_id: str) -> int:
|
||||
"""Return the current buffer length without consuming it."""
|
||||
redis = await get_redis_async()
|
||||
length = await cast("Any", redis.llen(_buffer_key(session_id)))
|
||||
return int(length)
|
||||
|
||||
|
||||
async def peek_pending_messages(session_id: str) -> list[PendingMessage]:
|
||||
"""Return pending messages without consuming them.
|
||||
|
||||
Uses LRANGE 0 -1 to read all items in enqueue order (oldest first)
|
||||
without removing them. Returns an empty list if the buffer is empty
|
||||
or the session has no pending messages.
|
||||
"""
|
||||
redis = await get_redis_async()
|
||||
key = _buffer_key(session_id)
|
||||
items = await cast("Any", redis.lrange(key, 0, -1))
|
||||
if not items:
|
||||
return []
|
||||
messages: list[PendingMessage] = []
|
||||
for item in items:
|
||||
try:
|
||||
messages.append(
|
||||
PendingMessage.model_validate(json.loads(_decode_redis_item(item)))
|
||||
)
|
||||
except (json.JSONDecodeError, ValidationError, TypeError, ValueError) as e:
|
||||
logger.warning(
|
||||
"pending_messages: dropping malformed peek entry for %s: %s",
|
||||
session_id,
|
||||
e,
|
||||
)
|
||||
return messages
|
||||
|
||||
|
||||
async def _clear_pending_messages_unsafe(session_id: str) -> None:
|
||||
"""Drop the session's pending buffer — **not** the normal turn cleanup.
|
||||
|
||||
Named ``_unsafe`` because reaching for this at turn end drops queued
|
||||
follow-ups on the floor instead of running them (the bug fixed by
|
||||
commit b64be73). The atomic ``LPOP`` drain at turn start is the
|
||||
primary consumer; anything pushed after the drain window belongs to
|
||||
the next turn by definition. Retained only as an operator/debug
|
||||
escape hatch for manually clearing a stuck session and as a fixture
|
||||
in the unit tests.
|
||||
"""
|
||||
redis = await get_redis_async()
|
||||
await redis.delete(_buffer_key(session_id))
|
||||
|
||||
|
||||
# Per-message and total-block caps for inline tool-boundary injection.
|
||||
# Per-message keeps a single long paste from dominating; the total cap
|
||||
# keeps the follow-up block small relative to the 100 KB MCP truncation
|
||||
# boundary so tool output always stays the larger share of the wrapper
|
||||
# return value.
|
||||
_FOLLOWUP_CONTENT_MAX_CHARS = 2_000
|
||||
_FOLLOWUP_TOTAL_MAX_CHARS = 6_000
|
||||
|
||||
|
||||
def _persist_queue_key(session_id: str) -> str:
|
||||
return f"{_PERSIST_QUEUE_KEY_PREFIX}{session_id}"
|
||||
|
||||
|
||||
async def stash_pending_for_persist(
|
||||
session_id: str,
|
||||
messages: list[PendingMessage],
|
||||
) -> None:
|
||||
"""Enqueue drained PendingMessages for UI-row persistence.
|
||||
|
||||
Writes each message as a JSON payload to
|
||||
``copilot:pending-persist:{session_id}``. The SDK service's
|
||||
tool-result dispatch handler LPOPs this queue right after appending
|
||||
the tool_result row to ``session.messages``, so the resulting user
|
||||
row lands at the correct chronological position (after the tool
|
||||
output the follow-up was drained against).
|
||||
|
||||
Fire-and-forget on Redis failures: a stash failure means Claude
|
||||
still saw the follow-up in tool output (the injection step ran
|
||||
first), so the only consequence is a missing UI bubble. Logged
|
||||
so it can be spotted.
|
||||
"""
|
||||
if not messages:
|
||||
return
|
||||
try:
|
||||
redis = await get_redis_async()
|
||||
key = _persist_queue_key(session_id)
|
||||
payloads = [m.model_dump_json() for m in messages]
|
||||
await redis.rpush(key, *payloads) # type: ignore[misc]
|
||||
await redis.expire(key, _PENDING_TTL_SECONDS) # type: ignore[misc]
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"pending_messages: failed to stash %d message(s) for persist "
|
||||
"(session=%s); UI will miss the follow-up bubble but Claude "
|
||||
"already saw the content in tool output",
|
||||
len(messages),
|
||||
session_id,
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
|
||||
async def drain_pending_for_persist(session_id: str) -> list[PendingMessage]:
|
||||
"""Atomically drain the persist queue for *session_id*.
|
||||
|
||||
Returns the queued ``PendingMessage`` objects in enqueue order (oldest
|
||||
first). Returns ``[]`` on any error so the service-layer caller can
|
||||
always treat the result as a plain list. Called by sdk/service.py
|
||||
after appending a tool_result row to ``session.messages``.
|
||||
"""
|
||||
try:
|
||||
redis = await get_redis_async()
|
||||
key = _persist_queue_key(session_id)
|
||||
lpop_result = await redis.lpop( # type: ignore[assignment]
|
||||
key, MAX_PENDING_MESSAGES
|
||||
)
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"pending_messages: drain_pending_for_persist failed for session=%s",
|
||||
session_id,
|
||||
exc_info=True,
|
||||
)
|
||||
return []
|
||||
if not lpop_result:
|
||||
return []
|
||||
raw_popped: list[Any] = list(lpop_result)
|
||||
messages: list[PendingMessage] = []
|
||||
for item in raw_popped:
|
||||
try:
|
||||
messages.append(
|
||||
PendingMessage.model_validate(json.loads(_decode_redis_item(item)))
|
||||
)
|
||||
except (json.JSONDecodeError, ValidationError, TypeError, ValueError) as e:
|
||||
logger.warning(
|
||||
"pending_messages: dropping malformed persist-queue entry "
|
||||
"for %s: %s",
|
||||
session_id,
|
||||
e,
|
||||
)
|
||||
return messages
|
||||
|
||||
|
||||
def format_pending_as_followup(pending: list[PendingMessage]) -> str:
|
||||
"""Render drained pending messages as a ``<user_follow_up>`` block.
|
||||
|
||||
Used by the SDK tool-boundary injection path to surface queued user
|
||||
text inside a tool result so the model reads it on the next LLM round,
|
||||
without starting a separate turn. Wrapped in a stable XML-style tag so
|
||||
the shared system-prompt supplement can teach the model to treat the
|
||||
contents as the user's continuation of their request, not as tool
|
||||
output. Each message is capped to keep the block bounded even if the
|
||||
user pastes long content.
|
||||
"""
|
||||
if not pending:
|
||||
return ""
|
||||
rendered: list[str] = []
|
||||
total_chars = 0
|
||||
dropped = 0
|
||||
for idx, pm in enumerate(pending, start=1):
|
||||
text = pm.content
|
||||
if len(text) > _FOLLOWUP_CONTENT_MAX_CHARS:
|
||||
text = text[:_FOLLOWUP_CONTENT_MAX_CHARS] + "… [truncated]"
|
||||
entry = f"Message {idx}:\n{text}"
|
||||
if pm.context and pm.context.url:
|
||||
entry += f"\n[Page URL: {pm.context.url}]"
|
||||
if pm.file_ids:
|
||||
entry += "\n[Attached files: " + ", ".join(pm.file_ids) + "]"
|
||||
if total_chars + len(entry) > _FOLLOWUP_TOTAL_MAX_CHARS:
|
||||
dropped = len(pending) - idx + 1
|
||||
break
|
||||
rendered.append(entry)
|
||||
total_chars += len(entry)
|
||||
if dropped:
|
||||
rendered.append(f"… [{dropped} more message(s) truncated]")
|
||||
body = "\n\n".join(rendered)
|
||||
return (
|
||||
"<user_follow_up>\n"
|
||||
"The user sent the following message(s) while this tool was running. "
|
||||
"Treat them as a continuation of their current request — acknowledge "
|
||||
"and act on them in your next response. Do not echo these tags back.\n\n"
|
||||
f"{body}\n"
|
||||
"</user_follow_up>"
|
||||
)
|
||||
|
||||
|
||||
async def drain_and_format_for_injection(
|
||||
session_id: str,
|
||||
*,
|
||||
log_prefix: str,
|
||||
) -> str:
|
||||
"""Drain the pending buffer and produce a ``<user_follow_up>`` block.
|
||||
|
||||
Shared entry point for every mid-turn injection site (``PostToolUse``
|
||||
hook for MCP + built-in tools, baseline between-rounds drain, etc.).
|
||||
Also stashes the drained messages on the persist queue so the service
|
||||
layer appends a real user row after the tool_result it rode in on —
|
||||
giving the UI a correctly-ordered bubble.
|
||||
|
||||
Returns an empty string if nothing was queued or Redis failed; callers
|
||||
can pass the result straight to ``additionalContext``.
|
||||
"""
|
||||
if not session_id:
|
||||
return ""
|
||||
try:
|
||||
pending = await drain_pending_messages(session_id)
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"%s drain_pending_messages failed (session=%s); skipping injection",
|
||||
log_prefix,
|
||||
session_id,
|
||||
exc_info=True,
|
||||
)
|
||||
return ""
|
||||
if not pending:
|
||||
return ""
|
||||
logger.info(
|
||||
"%s Injected %d user follow-up(s) into tool output (session=%s)",
|
||||
log_prefix,
|
||||
len(pending),
|
||||
session_id,
|
||||
)
|
||||
await stash_pending_for_persist(session_id, pending)
|
||||
return format_pending_as_followup(pending)
|
||||
|
||||
|
||||
def format_pending_as_user_message(message: PendingMessage) -> dict[str, Any]:
|
||||
"""Shape a ``PendingMessage`` into the OpenAI-format user message dict.
|
||||
|
||||
Used by the baseline tool-call loop when injecting the buffered
|
||||
message into the conversation. Context/file metadata (if any) is
|
||||
embedded into the content so the model sees everything in one block.
|
||||
"""
|
||||
parts: list[str] = [message.content]
|
||||
if message.context:
|
||||
if message.context.url:
|
||||
parts.append(f"\n\n[Page URL: {message.context.url}]")
|
||||
if message.context.content:
|
||||
parts.append(f"\n\n[Page content]\n{message.context.content}")
|
||||
if message.file_ids:
|
||||
parts.append(
|
||||
"\n\n[Attached files]\n"
|
||||
+ "\n".join(f"- file_id={fid}" for fid in message.file_ids)
|
||||
+ "\nUse read_workspace_file with the file_id to access file contents."
|
||||
)
|
||||
return {"role": "user", "content": "".join(parts)}
|
||||
@@ -1,614 +0,0 @@
|
||||
"""Tests for the copilot pending-messages buffer.
|
||||
|
||||
Uses a fake async Redis client so the tests don't require a real Redis
|
||||
instance (the backend test suite's DB/Redis fixtures are heavyweight
|
||||
and pull in the full app startup).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.copilot import pending_messages as pm_module
|
||||
from backend.copilot.pending_messages import (
|
||||
MAX_PENDING_MESSAGES,
|
||||
PendingMessage,
|
||||
PendingMessageContext,
|
||||
_clear_pending_messages_unsafe,
|
||||
drain_and_format_for_injection,
|
||||
drain_pending_for_persist,
|
||||
drain_pending_messages,
|
||||
format_pending_as_followup,
|
||||
format_pending_as_user_message,
|
||||
peek_pending_count,
|
||||
peek_pending_messages,
|
||||
push_pending_message,
|
||||
stash_pending_for_persist,
|
||||
)
|
||||
|
||||
# ── Fake Redis ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class _FakeRedis:
|
||||
def __init__(self) -> None:
|
||||
# Values are ``str | bytes`` because real redis-py returns
|
||||
# bytes when ``decode_responses=False``; the drain path must
|
||||
# handle both and our tests exercise both.
|
||||
self.lists: dict[str, list[str | bytes]] = {}
|
||||
self.published: list[tuple[str, str]] = []
|
||||
|
||||
async def rpush(self, key: str, *values: Any) -> int:
|
||||
lst = self.lists.setdefault(key, [])
|
||||
lst.extend(values)
|
||||
return len(lst)
|
||||
|
||||
async def ltrim(self, key: str, start: int, stop: int) -> None:
|
||||
lst = self.lists.get(key, [])
|
||||
# Redis LTRIM stop is inclusive; -1 means the last element.
|
||||
if stop == -1:
|
||||
self.lists[key] = lst[start:]
|
||||
else:
|
||||
self.lists[key] = lst[start : stop + 1]
|
||||
|
||||
async def expire(self, key: str, seconds: int) -> int:
|
||||
# Fake doesn't enforce TTL — just acknowledge.
|
||||
return 1
|
||||
|
||||
async def publish(self, channel: str, payload: str) -> int:
|
||||
self.published.append((channel, payload))
|
||||
return 1
|
||||
|
||||
async def lpop(self, key: str, count: int) -> list[str | bytes] | None:
|
||||
lst = self.lists.get(key)
|
||||
if not lst:
|
||||
return None
|
||||
popped = lst[:count]
|
||||
self.lists[key] = lst[count:]
|
||||
return popped
|
||||
|
||||
async def llen(self, key: str) -> int:
|
||||
return len(self.lists.get(key, []))
|
||||
|
||||
async def lrange(self, key: str, start: int, stop: int) -> list[str | bytes]:
|
||||
lst = self.lists.get(key, [])
|
||||
# Redis LRANGE stop is inclusive; -1 means the last element.
|
||||
if stop == -1:
|
||||
return list(lst[start:])
|
||||
return list(lst[start : stop + 1])
|
||||
|
||||
async def delete(self, key: str) -> int:
|
||||
if key in self.lists:
|
||||
del self.lists[key]
|
||||
return 1
|
||||
return 0
|
||||
|
||||
def pipeline(self, transaction: bool = True) -> "_FakePipeline":
|
||||
# Returns a fake pipeline that records ops and replays them in
|
||||
# order on ``execute()``. Used by ``capped_rpush`` (push_pending_message)
|
||||
# and ``incr_with_ttl`` (call-rate check) via MULTI/EXEC.
|
||||
return _FakePipeline(self)
|
||||
|
||||
async def incr(self, key: str) -> int:
|
||||
# Used by incr_with_ttl's pipeline.
|
||||
current = int(self.lists.get(key, [0])[0]) if self.lists.get(key) else 0
|
||||
current += 1
|
||||
# We abuse the same lists dict for simple counters — store [count].
|
||||
self.lists[key] = [str(current)]
|
||||
return current
|
||||
|
||||
|
||||
class _FakePipeline:
|
||||
"""Async pipeline shim matching the redis-py MULTI/EXEC surface."""
|
||||
|
||||
def __init__(self, parent: "_FakeRedis") -> None:
|
||||
self._parent = parent
|
||||
self._ops: list[tuple[str, tuple[Any, ...], dict[str, Any]]] = []
|
||||
|
||||
# Each method just records the op; dispatching happens in execute().
|
||||
def rpush(self, key: str, *values: Any) -> "_FakePipeline":
|
||||
self._ops.append(("rpush", (key, *values), {}))
|
||||
return self
|
||||
|
||||
def ltrim(self, key: str, start: int, stop: int) -> "_FakePipeline":
|
||||
self._ops.append(("ltrim", (key, start, stop), {}))
|
||||
return self
|
||||
|
||||
def expire(self, key: str, seconds: int, **kw: Any) -> "_FakePipeline":
|
||||
self._ops.append(("expire", (key, seconds), kw))
|
||||
return self
|
||||
|
||||
def llen(self, key: str) -> "_FakePipeline":
|
||||
self._ops.append(("llen", (key,), {}))
|
||||
return self
|
||||
|
||||
def incr(self, key: str) -> "_FakePipeline":
|
||||
self._ops.append(("incr", (key,), {}))
|
||||
return self
|
||||
|
||||
async def execute(self) -> list[Any]:
|
||||
results: list[Any] = []
|
||||
for name, args, _kw in self._ops:
|
||||
fn = getattr(self._parent, name)
|
||||
results.append(await fn(*args))
|
||||
return results
|
||||
|
||||
# Support `async with pipeline() as pipe:` too.
|
||||
async def __aenter__(self) -> "_FakePipeline":
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *a: Any) -> None:
|
||||
return None
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def fake_redis(monkeypatch: pytest.MonkeyPatch) -> _FakeRedis:
|
||||
redis = _FakeRedis()
|
||||
|
||||
async def _get_redis_async() -> _FakeRedis:
|
||||
return redis
|
||||
|
||||
monkeypatch.setattr(pm_module, "get_redis_async", _get_redis_async)
|
||||
return redis
|
||||
|
||||
|
||||
# ── Basic push / drain ──────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_push_and_drain_single_message(fake_redis: _FakeRedis) -> None:
|
||||
length = await push_pending_message("sess1", PendingMessage(content="hello"))
|
||||
assert length == 1
|
||||
assert await peek_pending_count("sess1") == 1
|
||||
|
||||
drained = await drain_pending_messages("sess1")
|
||||
assert len(drained) == 1
|
||||
assert drained[0].content == "hello"
|
||||
assert await peek_pending_count("sess1") == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_push_and_drain_preserves_order(fake_redis: _FakeRedis) -> None:
|
||||
for i in range(3):
|
||||
await push_pending_message("sess2", PendingMessage(content=f"msg {i}"))
|
||||
|
||||
drained = await drain_pending_messages("sess2")
|
||||
assert [m.content for m in drained] == ["msg 0", "msg 1", "msg 2"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_empty_returns_empty_list(fake_redis: _FakeRedis) -> None:
|
||||
assert await drain_pending_messages("nope") == []
|
||||
|
||||
|
||||
# ── Buffer cap ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cap_drops_oldest_when_exceeded(fake_redis: _FakeRedis) -> None:
|
||||
# Push MAX_PENDING_MESSAGES + 3 messages
|
||||
for i in range(MAX_PENDING_MESSAGES + 3):
|
||||
await push_pending_message("sess3", PendingMessage(content=f"m{i}"))
|
||||
|
||||
# Buffer should be clamped to MAX
|
||||
assert await peek_pending_count("sess3") == MAX_PENDING_MESSAGES
|
||||
|
||||
drained = await drain_pending_messages("sess3")
|
||||
assert len(drained) == MAX_PENDING_MESSAGES
|
||||
# Oldest 3 dropped — we should only see m3..m(MAX+2)
|
||||
assert drained[0].content == "m3"
|
||||
assert drained[-1].content == f"m{MAX_PENDING_MESSAGES + 2}"
|
||||
|
||||
|
||||
# ── Clear ───────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_clear_removes_buffer(fake_redis: _FakeRedis) -> None:
|
||||
await push_pending_message("sess4", PendingMessage(content="x"))
|
||||
await push_pending_message("sess4", PendingMessage(content="y"))
|
||||
await _clear_pending_messages_unsafe("sess4")
|
||||
assert await peek_pending_count("sess4") == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_clear_is_idempotent(fake_redis: _FakeRedis) -> None:
|
||||
# Clearing an already-empty buffer should not raise
|
||||
await _clear_pending_messages_unsafe("sess_empty")
|
||||
await _clear_pending_messages_unsafe("sess_empty")
|
||||
|
||||
|
||||
# ── Publish hook ────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_push_publishes_notification(fake_redis: _FakeRedis) -> None:
|
||||
await push_pending_message("sess5", PendingMessage(content="hi"))
|
||||
assert ("copilot:pending:notify:sess5", "1") in fake_redis.published
|
||||
|
||||
|
||||
# ── Format helper ───────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_format_pending_plain_text() -> None:
|
||||
msg = PendingMessage(content="just text")
|
||||
out = format_pending_as_user_message(msg)
|
||||
assert out == {"role": "user", "content": "just text"}
|
||||
|
||||
|
||||
def test_format_pending_with_context_url() -> None:
|
||||
msg = PendingMessage(
|
||||
content="see this page",
|
||||
context=PendingMessageContext(url="https://example.com"),
|
||||
)
|
||||
out = format_pending_as_user_message(msg)
|
||||
content = out["content"]
|
||||
assert out["role"] == "user"
|
||||
assert "see this page" in content
|
||||
# The URL should appear verbatim in the [Page URL: ...] block.
|
||||
assert "[Page URL: https://example.com]" in content
|
||||
|
||||
|
||||
def test_format_pending_with_file_ids() -> None:
|
||||
msg = PendingMessage(content="look here", file_ids=["a", "b"])
|
||||
out = format_pending_as_user_message(msg)
|
||||
assert "file_id=a" in out["content"]
|
||||
assert "file_id=b" in out["content"]
|
||||
|
||||
|
||||
def test_format_pending_with_all_fields() -> None:
|
||||
"""All fields (content + context url/content + file_ids) should all appear."""
|
||||
msg = PendingMessage(
|
||||
content="summarise this",
|
||||
context=PendingMessageContext(
|
||||
url="https://example.com/page",
|
||||
content="headline text",
|
||||
),
|
||||
file_ids=["f1", "f2"],
|
||||
)
|
||||
out = format_pending_as_user_message(msg)
|
||||
body = out["content"]
|
||||
assert out["role"] == "user"
|
||||
assert "summarise this" in body
|
||||
assert "[Page URL: https://example.com/page]" in body
|
||||
assert "[Page content]\nheadline text" in body
|
||||
assert "file_id=f1" in body
|
||||
assert "file_id=f2" in body
|
||||
|
||||
|
||||
# ── Followup block caps ────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_format_followup_single_message() -> None:
|
||||
out = format_pending_as_followup([PendingMessage(content="hello")])
|
||||
assert "<user_follow_up>" in out
|
||||
assert "</user_follow_up>" in out
|
||||
assert "Message 1:\nhello" in out
|
||||
|
||||
|
||||
def test_format_followup_total_cap_drops_overflow() -> None:
|
||||
"""10 × 2 KB messages must truncate past the total cap (~6 KB) with a
|
||||
marker indicating how many were dropped."""
|
||||
messages = [PendingMessage(content="A" * 2_000) for _ in range(10)]
|
||||
out = format_pending_as_followup(messages)
|
||||
# Block stays within the total cap (plus a little wrapper overhead).
|
||||
# The body alone is capped at 6 KB; we allow generous overhead for the
|
||||
# <user_follow_up> wrapper + headers.
|
||||
assert len(out) < 8_000
|
||||
assert "more message(s) truncated" in out
|
||||
# The first message at least must be present.
|
||||
assert "Message 1:" in out
|
||||
|
||||
|
||||
def test_format_followup_total_cap_marker_counts_dropped() -> None:
|
||||
"""The marker should name the exact number of dropped messages."""
|
||||
# Each 3 KB message gets capped to 2 KB first; with ~2 KB per entry and a
|
||||
# 6 KB total cap, roughly two entries fit and the rest are dropped.
|
||||
messages = [PendingMessage(content="X" * 3_000) for _ in range(5)]
|
||||
out = format_pending_as_followup(messages)
|
||||
assert "Message 1:" in out
|
||||
assert "Message 2:" in out
|
||||
# Message 3 would push total past 6 KB; marker should report exactly how
|
||||
# many were left out (here: messages 3, 4, 5 → 3 dropped).
|
||||
assert "[3 more message(s) truncated]" in out
|
||||
|
||||
|
||||
def test_format_followup_empty_returns_empty_string() -> None:
|
||||
assert format_pending_as_followup([]) == ""
|
||||
|
||||
|
||||
# ── Malformed payload handling ──────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_skips_malformed_entries(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
# Seed the fake with a mix of valid and malformed payloads
|
||||
fake_redis.lists["copilot:pending:bad"] = [
|
||||
json.dumps({"content": "valid"}),
|
||||
"{not valid json",
|
||||
json.dumps({"content": "also valid", "file_ids": ["a"]}),
|
||||
]
|
||||
drained = await drain_pending_messages("bad")
|
||||
assert len(drained) == 2
|
||||
assert drained[0].content == "valid"
|
||||
assert drained[1].content == "also valid"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_decodes_bytes_payloads(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
"""Real redis-py returns ``bytes`` when ``decode_responses=False``.
|
||||
|
||||
Seed the fake with bytes values to exercise the ``decode("utf-8")``
|
||||
branch in ``drain_pending_messages`` so a regression there doesn't
|
||||
slip past CI.
|
||||
"""
|
||||
fake_redis.lists["copilot:pending:bytes_sess"] = [
|
||||
json.dumps({"content": "from bytes"}).encode("utf-8"),
|
||||
]
|
||||
drained = await drain_pending_messages("bytes_sess")
|
||||
assert len(drained) == 1
|
||||
assert drained[0].content == "from bytes"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_peek_decodes_bytes_payloads(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
"""``peek_pending_messages`` uses the same ``_decode_redis_item`` helper
|
||||
as the drain path. Seed with bytes to guard against regression.
|
||||
"""
|
||||
fake_redis.lists["copilot:pending:peek_bytes_sess"] = [
|
||||
json.dumps({"content": "peeked from bytes"}).encode("utf-8"),
|
||||
]
|
||||
peeked = await peek_pending_messages("peek_bytes_sess")
|
||||
assert len(peeked) == 1
|
||||
assert peeked[0].content == "peeked from bytes"
|
||||
# peek must NOT consume the item
|
||||
assert fake_redis.lists["copilot:pending:peek_bytes_sess"] != []
|
||||
|
||||
|
||||
# ── Concurrency ─────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_concurrent_push_and_drain(fake_redis: _FakeRedis) -> None:
|
||||
"""Two pushes fired concurrently should both land; a concurrent drain
|
||||
should see at least one of them (the fake serialises, so it will
|
||||
always see both, but we exercise the code path either way)."""
|
||||
await asyncio.gather(
|
||||
push_pending_message("sess_conc", PendingMessage(content="a")),
|
||||
push_pending_message("sess_conc", PendingMessage(content="b")),
|
||||
)
|
||||
drained = await drain_pending_messages("sess_conc")
|
||||
assert len(drained) >= 1
|
||||
contents = {m.content for m in drained}
|
||||
assert contents <= {"a", "b"}
|
||||
|
||||
|
||||
# ── Publish error path ──────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_push_survives_publish_failure(
|
||||
fake_redis: _FakeRedis, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""A publish error must not propagate — the buffer is still authoritative."""
|
||||
|
||||
async def _fail_publish(channel: str, payload: str) -> int:
|
||||
raise RuntimeError("redis publish down")
|
||||
|
||||
monkeypatch.setattr(fake_redis, "publish", _fail_publish)
|
||||
|
||||
length = await push_pending_message("sess_pub_err", PendingMessage(content="ok"))
|
||||
assert length == 1
|
||||
drained = await drain_pending_messages("sess_pub_err")
|
||||
assert len(drained) == 1
|
||||
assert drained[0].content == "ok"
|
||||
|
||||
|
||||
# ── peek_pending_messages ────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_peek_pending_messages_returns_all_without_consuming(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
"""Peek returns all queued messages and leaves the buffer intact."""
|
||||
await push_pending_message("peek1", PendingMessage(content="first"))
|
||||
await push_pending_message("peek1", PendingMessage(content="second"))
|
||||
|
||||
peeked = await peek_pending_messages("peek1")
|
||||
assert len(peeked) == 2
|
||||
assert peeked[0].content == "first"
|
||||
assert peeked[1].content == "second"
|
||||
|
||||
# Buffer must not be consumed — count still 2
|
||||
assert await peek_pending_count("peek1") == 2
|
||||
drained = await drain_pending_messages("peek1")
|
||||
assert len(drained) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_peek_pending_messages_empty_buffer(fake_redis: _FakeRedis) -> None:
|
||||
"""Peek on a missing key returns an empty list without raising."""
|
||||
result = await peek_pending_messages("no_such_session")
|
||||
assert result == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_peek_pending_messages_decodes_bytes_payloads(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
"""peek_pending_messages decodes bytes entries the same way drain does."""
|
||||
fake_redis.lists["copilot:pending:peek_bytes"] = [
|
||||
json.dumps({"content": "from bytes"}).encode("utf-8"),
|
||||
]
|
||||
peeked = await peek_pending_messages("peek_bytes")
|
||||
assert len(peeked) == 1
|
||||
assert peeked[0].content == "from bytes"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_peek_pending_messages_skips_malformed_entries(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
"""Malformed entries are skipped and valid ones are returned."""
|
||||
fake_redis.lists["copilot:pending:peek_bad"] = [
|
||||
json.dumps({"content": "valid peek"}),
|
||||
"{bad json",
|
||||
json.dumps({"content": "also valid peek"}),
|
||||
]
|
||||
peeked = await peek_pending_messages("peek_bad")
|
||||
assert len(peeked) == 2
|
||||
assert peeked[0].content == "valid peek"
|
||||
assert peeked[1].content == "also valid peek"
|
||||
|
||||
|
||||
# ── Persist queue (mid-turn follow-up UI bubble hand-off) ───────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stash_for_persist_enqueues_and_drain_pops_in_order(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
"""stash_pending_for_persist writes messages under the persist key;
|
||||
drain_pending_for_persist LPOPs them in enqueue order."""
|
||||
msgs = [
|
||||
PendingMessage(content="first mid-turn follow-up"),
|
||||
PendingMessage(content="second"),
|
||||
]
|
||||
await stash_pending_for_persist("sess-persist", msgs)
|
||||
|
||||
# Stored under the distinct persist key, NOT the primary buffer.
|
||||
assert "copilot:pending-persist:sess-persist" in fake_redis.lists
|
||||
assert "copilot:pending:sess-persist" not in fake_redis.lists
|
||||
|
||||
drained = await drain_pending_for_persist("sess-persist")
|
||||
assert len(drained) == 2
|
||||
assert drained[0].content == "first mid-turn follow-up"
|
||||
assert drained[1].content == "second"
|
||||
|
||||
# Queue is empty after drain.
|
||||
assert await drain_pending_for_persist("sess-persist") == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stash_for_persist_empty_list_is_noop(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
"""Passing an empty list must NOT create a Redis key (would leak
|
||||
empty persist entries and require a drain for no reason)."""
|
||||
await stash_pending_for_persist("sess-noop", [])
|
||||
assert "copilot:pending-persist:sess-noop" not in fake_redis.lists
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_pending_for_persist_missing_key_returns_empty(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
assert await drain_pending_for_persist("never-stashed") == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_pending_for_persist_skips_malformed(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
fake_redis.lists["copilot:pending-persist:bad"] = [
|
||||
json.dumps({"content": "good one"}),
|
||||
"not json",
|
||||
json.dumps({"content": "another good one"}),
|
||||
]
|
||||
result = await drain_pending_for_persist("bad")
|
||||
assert [m.content for m in result] == ["good one", "another good one"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_persist_queue_isolated_from_primary_buffer(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
"""Draining the persist queue must NOT touch the primary pending
|
||||
buffer (and vice versa) — they serve different lifecycles."""
|
||||
# Seed the primary buffer with one entry.
|
||||
await push_pending_message("sess-iso", PendingMessage(content="primary"))
|
||||
# Stash a separate entry on the persist queue.
|
||||
await stash_pending_for_persist("sess-iso", [PendingMessage(content="persist")])
|
||||
|
||||
drained_persist = await drain_pending_for_persist("sess-iso")
|
||||
assert [m.content for m in drained_persist] == ["persist"]
|
||||
|
||||
# Primary buffer untouched.
|
||||
assert await peek_pending_count("sess-iso") == 1
|
||||
drained_primary = await drain_pending_messages("sess-iso")
|
||||
assert [m.content for m in drained_primary] == ["primary"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stash_for_persist_swallows_redis_failure(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""A broken Redis during stash must not raise — Claude has already
|
||||
seen the follow-up via tool output; the only fallout is a missing
|
||||
UI bubble, which we log and move on."""
|
||||
|
||||
async def _broken_redis() -> Any:
|
||||
raise ConnectionError("redis down")
|
||||
|
||||
monkeypatch.setattr(pm_module, "get_redis_async", _broken_redis)
|
||||
|
||||
# Must NOT raise.
|
||||
await stash_pending_for_persist("sess-broken", [PendingMessage(content="lost")])
|
||||
|
||||
|
||||
# ── drain_and_format_for_injection: shared entry point ─────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_and_format_for_injection_happy_path(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
"""Queued messages drain into a ready-to-inject <user_follow_up> block
|
||||
AND are stashed on the persist queue for UI row hand-off."""
|
||||
await push_pending_message("sess-share", PendingMessage(content="do X also"))
|
||||
|
||||
result = await drain_and_format_for_injection("sess-share", log_prefix="[TEST]")
|
||||
|
||||
assert "<user_follow_up>" in result
|
||||
assert "do X also" in result
|
||||
# Primary buffer drained.
|
||||
assert await peek_pending_count("sess-share") == 0
|
||||
# Persist queue got a copy for the UI.
|
||||
persisted = await drain_pending_for_persist("sess-share")
|
||||
assert len(persisted) == 1
|
||||
assert persisted[0].content == "do X also"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_and_format_for_injection_empty_returns_empty(
|
||||
fake_redis: _FakeRedis,
|
||||
) -> None:
|
||||
assert await drain_and_format_for_injection("sess-empty", log_prefix="[TEST]") == ""
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_and_format_for_injection_swallows_redis_error(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
async def _broken() -> Any:
|
||||
raise ConnectionError("down")
|
||||
|
||||
monkeypatch.setattr(pm_module, "get_redis_async", _broken)
|
||||
|
||||
# Must NOT raise — broken Redis becomes "nothing to inject".
|
||||
assert (
|
||||
await drain_and_format_for_injection("sess-broken", log_prefix="[TEST]") == ""
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drain_and_format_for_injection_missing_session_id() -> None:
|
||||
assert await drain_and_format_for_injection("", log_prefix="[TEST]") == ""
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user