Compare commits
235 Commits
feat/netwo
...
8914391336
| Author | SHA1 | Date | |
|---|---|---|---|
| 8914391336 | |||
| e8ba74ed6d | |||
| aca915b047 | |||
| e910bcc67d | |||
| 5085bb47c2 | |||
| 029680ab2d | |||
| 2a2329ad19 | |||
| 641853a002 | |||
| c194ba4a43 | |||
| 8e9d22e0e0 | |||
| f65b65790c | |||
| b8627f4180 | |||
| 02d5c6c08c | |||
| 9bdd9959a8 | |||
| fff8338bf2 | |||
| bc15952857 | |||
| ba46fc5644 | |||
| 87bd0b7c56 | |||
| a283d0d3fd | |||
| 9f0bfd44f9 | |||
| 07d0db9579 | |||
| 7a5b853b3b | |||
| 52f6d0308f | |||
| d51e95cdfa | |||
| c0ed6d9840 | |||
| 8f818a7c71 | |||
| 68fcdc6122 | |||
| 11fe32f4c6 | |||
| 43eed720d9 | |||
| 1559feb759 | |||
| b56da2facd | |||
| 87bb20b8f0 | |||
| 1e3a6cfa01 | |||
| ede6eebf9a | |||
| 261814ae65 | |||
| 6656ebdead | |||
| 69f2a37591 | |||
| 7f714363dd | |||
| 1bd43abb8f | |||
| c203b70ef9 | |||
| f27e3b44b0 | |||
| fe632c9194 | |||
| e976fb4e87 | |||
| 0aefaa78eb | |||
| 49f88569da | |||
| 208ec996d5 | |||
| 8f7df2c0ef | |||
| f27f671fe6 | |||
| d6218f2e07 | |||
| 920a246d77 | |||
| b7f8e70be2 | |||
| 857d73e3d0 | |||
| 406ee0ef97 | |||
| 32fae2c693 | |||
| a45915fbbc | |||
| 06593a40d9 | |||
| 9737d90f1b | |||
| 1c904373f8 | |||
| 16060d2235 | |||
| 9330ce4782 | |||
| d68131a865 | |||
| 875bd924a9 | |||
| 49c6c8fd00 | |||
| a77e8ea578 | |||
| 90252bc98f | |||
| 036431aef8 | |||
| b3be1e0749 | |||
| b3506b5e73 | |||
| b14a16a1ab | |||
| 9c8ba296a8 | |||
| bee8690056 | |||
| e110fedfe4 | |||
| dab740ddf7 | |||
| 24972e8444 | |||
| d386d11af2 | |||
| 65a831bf9a | |||
| faf1d8dd12 | |||
| 0386fa1fd5 | |||
| 82db1c78e4 | |||
| f930787200 | |||
| 5bcb7aa7c3 | |||
| 04fbfe3b8f | |||
| f92cbefed9 | |||
| c9306e40c9 | |||
| 1c855563ee | |||
| d4fae87236 | |||
| f2fce27f0d | |||
| 93c974466a | |||
| 8012668975 | |||
| 563bb1aa6f | |||
| 1d2d548fc8 | |||
| 3ee0101c6d | |||
| 861d082ff7 | |||
| 75b59123e6 | |||
| fcd224429c | |||
| 196c003876 | |||
| f2b9476edb | |||
| 70c5da0c75 | |||
| de2bef3175 | |||
| 362c7b1d79 | |||
| ec104dc8de | |||
| a47ce07326 | |||
| 2a54127a54 | |||
| 8582d24236 | |||
| bdb238a274 | |||
| 075b0fc1d8 | |||
| 217747f46e | |||
| 7fa1d6a32f | |||
| ac67e48500 | |||
| cdd29b460e | |||
| 2cde6673b0 | |||
| c0112f8bee | |||
| 8988dbc885 | |||
| 4a8e3ae954 | |||
| cdd8bb05cc | |||
| 8879f96fbf | |||
| 8a242f5db9 | |||
| 4aaf57adb5 | |||
| ddae171a37 | |||
| d0ebdef9e8 | |||
| 50215b9110 | |||
| ce7c8ac3d5 | |||
| fa61376303 | |||
| 8fd2c1bac6 | |||
| 7ccf4c602b | |||
| 66e592096c | |||
| 625dba7548 | |||
| 19cfd71995 | |||
| 3b55697c77 | |||
| 851966966d | |||
| 66968e4c59 | |||
| b0622f5511 | |||
| f3c3ee5b57 | |||
| b49772f1a1 | |||
| 210d310fb2 | |||
| 92fadfb90a | |||
| 3f0a132058 | |||
| da93ae55c3 | |||
| 56fd440b16 | |||
| b3be66652e | |||
| 0fbc1e0a57 | |||
| 46291f30b9 | |||
| 995a0c1d2e | |||
| f6a24ea4e1 | |||
| 04ff2ea301 | |||
| 60851b400a | |||
| bea34229d6 | |||
| 294b309faa | |||
| fb7690485b | |||
| 6044d5a88b | |||
| 00cd8b7c55 | |||
| fded959b5e | |||
| 5f5b9e5b23 | |||
| b2ee1a2150 | |||
| 08909aa884 | |||
| 070d2383bc | |||
| d7b1fe6645 | |||
| a3f8bb3427 | |||
| f050afc2f7 | |||
| 849e1c16e2 | |||
| 5310cd3fff | |||
| d2689afa53 | |||
| 9d88c8456c | |||
| 506aac609d | |||
| 7fa81f69a6 | |||
| 6e0188d0b4 | |||
| 24ab1908a6 | |||
| e2cdfac1c3 | |||
| a5e9615666 | |||
| 66cca70588 | |||
| e714088a2b | |||
| ff0ec143e2 | |||
| 8d964e64e4 | |||
| 44634b1145 | |||
| 001438008b | |||
| c8b68ad26d | |||
| 2b3d52ad77 | |||
| 52b369680b | |||
| f0ccf313a4 | |||
| 0d9babb986 | |||
| 567985402f | |||
| 08a4c6600d | |||
| 29fa48e71b | |||
| 908a867986 | |||
| 346576a730 | |||
| b18072e24b | |||
| e0f44e2985 | |||
| adfbb39297 | |||
| 6bae205a8c | |||
| ee2b2c2399 | |||
| 37bc47b75b | |||
| c8bdd0014e | |||
| 2a2b770405 | |||
| d6d0e9f3c1 | |||
| ab4bf3b32f | |||
|
|
d3c93cd006 | ||
|
|
4037a5213e | ||
|
|
0ed5977fee | ||
|
|
c5b8229ef6 | ||
|
|
eba50e1f95 | ||
|
|
8eb814283d | ||
|
|
b433b232dc | ||
|
|
015df1fe5f | ||
|
|
cf9c258f9e | ||
|
|
c063952f12 | ||
|
|
36721eb5af | ||
|
|
3cd4084f78 | ||
|
|
ed763d1cea | ||
|
|
c37e216e0b | ||
|
|
91cc9a4170 | ||
|
|
2a4220b496 | ||
|
|
c8f571db39 | ||
|
|
7efa22454d | ||
|
|
05421fc65c | ||
|
|
dfcad531e2 | ||
|
|
684fb07e47 | ||
|
|
4a12c9b37d | ||
|
|
e41d7bd960 | ||
|
|
f2c3bd7a9b | ||
|
|
9786c6b1fb | ||
|
|
4529955f7d | ||
|
|
b7b0d41f92 | ||
|
|
a4512dcf90 | ||
|
|
764db79060 | ||
|
|
f90e2c956f | ||
|
|
bdaea68dd3 | ||
|
|
02c19a7580 | ||
|
|
a392d24101 | ||
|
|
b9c9bb548d | ||
|
|
662df2907d | ||
|
|
b9547e6ce1 | ||
|
|
760e0f77f8 | ||
|
|
a71f082e25 | ||
|
|
abd79bc763 | ||
|
|
af5ceea7f9 |
56
.ai/CURRENT_TASK.md
Normal file
56
.ai/CURRENT_TASK.md
Normal file
@@ -0,0 +1,56 @@
|
||||
# CURRENT_TASK.md
|
||||
|
||||
**Task:** Build **Escalation Mode** — the wedge for ResolutionFlow's GTM (first paying-customer push). When a junior tech escalates a FlowPilot session, the senior tech sees structured handoff context in seconds instead of running a 5-minute verbal "tell me what you tried" call.
|
||||
|
||||
**Status:** in-flight on `feat/escalation-metric-endpoint`. Branch is pushed; **draft PR #155** is open against `main` ([gitea.resolutionflow.com/chihlasm/resolutionflow/pulls/155](https://gitea.resolutionflow.com/chihlasm/resolutionflow/pulls/155)). Backend is **feature-complete and test-stabilized**. **Frontend live-arrival SSE subscription**, **magic-moment handoff-context screen**, and **bell-icon notification fix** all shipped. **`/escalate` and `/handoff` are now unified** through `HandoffManager` — every escalation creates a SessionHandoff, persists an AppNotification, fans out on the SSE bus, dispatches Slack/Teams via `notify()`, and emails per-user, regardless of which URL it entered through. **Next:** visual QA via `/qa`, then optional follow-ups (suggested-step chips, snapshot expansion, analytics page, Playwright e2e).
|
||||
|
||||
**Plan:** [`docs/plans/2026-04-27-escalation-mode-wedge-design.md`](../docs/plans/2026-04-27-escalation-mode-wedge-design.md). Reviewed by `/office-hours`, `/plan-eng-review`, `/plan-design-review`, `/codex review`. Eng + Design CLEARED. Codex's two-metric correction + claim role gate + per-channel notification model + SSE bus diagnostics all applied.
|
||||
|
||||
**Test plan artifact:** [`docs/plans/2026-04-27-escalation-mode-wedge-test-plan.md`](../docs/plans/2026-04-27-escalation-mode-wedge-test-plan.md) — primary input for `/qa` once feature-complete.
|
||||
|
||||
## Done on `feat/escalation-metric-endpoint` (8 commits, branched from `main` @ `c0ed6d9`)
|
||||
|
||||
| Commit | What it ships |
|
||||
|---|---|
|
||||
| `d51e95c` | Plan + test-plan artifacts |
|
||||
| `52f6d03` | `GET /analytics/flowpilot/escalations` — in-product time-to-first-action; account-scoped, engineer-or-admin gated |
|
||||
| `7a5b853` | Role-gate POST `/handoffs/{id}/claim` to engineer-or-admin |
|
||||
| `07d0db9` | `HandoffManager.dispatch_escalation_notifications` — emails engineer/admin teammates on intent=escalate; graceful-degradation regression |
|
||||
| `9f0bfd4` | `EscalationMetricCard` mounted above the queue list |
|
||||
| `a283d0d` | `.ai/` mid-flight refresh |
|
||||
| `87bd0b7` | **WIP** marker for the SSE backend slice (paused for Codex pass) |
|
||||
| `bc15952` | Codex: stabilize SSE backend tests — `Depends(..., scope="function")` releases auth DB deps before the long-lived stream body; SSE handshake test calls the generator directly; AI-assessment stub fixture; bus normalizes string vs UUID account_id |
|
||||
| `fff8338` | Doc-only: track escalation assessment latency follow-up |
|
||||
| `9bdd995` | Bound escalation assessment latency to `ESCALATION_AI_ASSESSMENT_TIMEOUT_SECONDS` (default 5s); handoff still creates if assessment times out |
|
||||
| `b8627f4` | Frontend SSE subscription in `EscalationQueue.tsx` — fetch-based `ReadableStream` reader; `handoff_created` triggers refetch + prepend with locked 200ms slide-in; exponential-backoff reconnect; tab-title flash when backgrounded; `prefers-reduced-motion` honored; ARIA live-region |
|
||||
| `f65b657` | Handoff state docs after frontend SSE slice lands |
|
||||
| `8e9d22e` | Magic-moment handoff-context screen on pickup — `HandoffContextScreen.tsx` (4 sections, graceful null AI assessment, focus management, prefers-reduced-motion); `FlowPilotSessionPage.tsx` integration (pre-claim handoff fetch, claim on Start here, toolbar re-open overlay) |
|
||||
| `c194ba4` | Handoff state docs after magic-moment screen lands |
|
||||
| `641853a` | Bell-icon notification opens the pickup flow — notification link template adds `?pickup=true`; GET `/ai-sessions/{id}` allows account-scoped read for `requesting_escalation` / `escalated` states |
|
||||
| `2a2329a` | Handoff state docs after bell-icon fix; record draft PR #155 |
|
||||
| `029680a` | Unify `/escalate` through `HandoffManager` — single canonical path for every escalation. `HandoffCreateRequest.target_user_id`, `create_handoff` does the legacy enriched-package work + sets `escalation_reason`, `finalize_escalation` runs documentation + PSA push + `notify()` pre-commit, `dispatch_escalation_notifications` keeps only fire-and-forget IO post-commit. `pickup_session` accepts either status for in-flight migration. `flowpilot_engine.escalate_session` no longer called from any endpoint |
|
||||
|
||||
**Test status:** full backend suite → `1103 passed in 259.63s` with `-n auto` after the unification. Frontend `tsc -b` clean. End-to-end smoke test against the running dev stack confirmed: SSE handshake delivers `ready` + `handoff_created` frames; `listHandoffs` returns the unclaimed handoff for a senior pre-claim; `claimHandoff` flips session status `escalated` → `active`; senior (non-owner, non-target) can `GET` an in-transit session detail; **a single legacy `/escalate` call now produces status='escalated', SessionDocumentation, SessionHandoff row, AppNotification with link `/pilot/{id}?pickup=true` for the team admin, and a PSA push attempt** — all from one funneled HandoffManager call. Branch pushed; draft PR #155 open.
|
||||
|
||||
## Remaining work on this branch
|
||||
|
||||
1. **Visual QA in a real browser** via `/qa` — slide-in animation, tab-title flash, magic-moment layout, dissolve, full junior-escalates → senior-receives → senior-claims demo path.
|
||||
2. **Suggested-step chips below the chat input** (Codex correction, design plan locks this) — surfaces `ai_assessment_data.suggested_steps[]` as clickable chips in `FlowPilotMessageBar` that prefill the input. Threading through `FlowPilotSession` → message bar.
|
||||
3. **Snapshot expansion in `HandoffManager._generate_snapshot`** — include the recent diagnostic steps / conversation tail so the magic-moment screen's "What's been tried" section can render the actual timeline pre-claim instead of "full timeline available after pickup".
|
||||
4. **Toolbar Context button on legacy-arrival sessions** — currently the button only appears when the senior arrived via the magic-moment flow this session. Lazy-fetching the handoff list on session-load (when status was-escalated) would make it work on revisits.
|
||||
5. **Owner-facing analytics page** at `/analytics/escalations` — period selector, conversion-rate, trend chart. ~0.5d. Optional for v1 demo.
|
||||
6. **Playwright e2e** for the magic-moment demo flow (junior escalates → senior receives via SSE → senior claims → opens session). Critical for the GTM Loom not to crash mid-recording.
|
||||
|
||||
## Two-metric framing — read this before quoting numbers to anyone
|
||||
|
||||
The in-product endpoint measures *post-claim time-to-first-action*. The "minutes recovered" sales claim is `manual_baseline − in_product_metric`. Manual baseline comes from the founder's stopwatch on the next 5 escalations (The Assignment in the design doc). Don't roll the in-product number alone into "minutes recovered" — that's the apples-to-oranges miscount Codex caught.
|
||||
|
||||
## Kill-switch
|
||||
|
||||
Week 8: if 0 of 3 pilots produce a verifiable hours-saved-per-week number above 1.0, revisit the wedge. The design doc names the alternative direction (deterministic-ops territory) for context, but data lands first.
|
||||
|
||||
## Previous task — closed out
|
||||
|
||||
**Task:** Land PR #153 — fix the `AssistantChatPage` prefill `currentChatRef` bug. **Status:** complete (2026-04-26). Merged as `68fcdc6` on `main`.
|
||||
|
||||
**Background CI item, not blocking:** promoting `CI / e2e (pull_request)` to required on `main`. Two consecutive green runs cleared the threshold. Ops-only.
|
||||
31
.ai/DECISIONS.md
Normal file
31
.ai/DECISIONS.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# DECISIONS.md
|
||||
|
||||
> Append-only architectural decision log. Newest entries at the top.
|
||||
> Entry format:
|
||||
>
|
||||
> ```
|
||||
> ## YYYY-MM-DD — <short title>
|
||||
> **Context:** why this came up
|
||||
> **Decision:** what we chose
|
||||
> **Rejected:** what we didn't choose and why
|
||||
> **Consequences:** what this means going forward
|
||||
> ```
|
||||
|
||||
---
|
||||
|
||||
## 2026-04-24 — Adopt dual-agent handoff system (`.ai/` + `CLAUDE.md` + `AGENTS.md`)
|
||||
|
||||
**Context:** Claude Code hits session and weekly usage limits. Work stalls when the primary agent is locked out. Needed a structured way for OpenAI Codex to resume where Claude left off without losing architectural truth or drifting across sessions.
|
||||
|
||||
**Decision:** Split the old CLAUDE.md into `.ai/PROJECT_CONTEXT.md` (stable repo truth), agent-specific root files (`CLAUDE.md`, `AGENTS.md`) with a shared protocol block, and a small handoff toolkit (`CURRENT_TASK.md`, `HANDOFF.md`, `TODO.md`, `DECISIONS.md`, `SESSION_LOG.md`, `README.md`). Previous CLAUDE.md snapshotted in commit `e110fed` before the migration.
|
||||
|
||||
**Rejected:**
|
||||
- Single symlinked CLAUDE.md/AGENTS.md — diverges silently, hides agent-specific tooling differences.
|
||||
- Putting GitNexus/gstack content in AGENTS.md — Codex doesn't have those tools; would mislead the resume agent.
|
||||
- Keeping the old CLAUDE.md as-is and adding AGENTS.md alongside it — duplicated truth, drift guaranteed.
|
||||
|
||||
**Consequences:**
|
||||
- First read for either agent: `.ai/PROJECT_CONTEXT.md` + `.ai/CURRENT_TASK.md` + `.ai/HANDOFF.md`.
|
||||
- Architectural changes in the repo require updating PROJECT_CONTEXT.md, not the root agent files.
|
||||
- Git trailers differ per agent (`Claude Opus 4.7` vs `Codex`) — preserved in each root file.
|
||||
- Legacy `SESSION-HANDOFF.md` deleted in the same commit; superseded by `.ai/HANDOFF.md`.
|
||||
63
.ai/HANDOFF.md
Normal file
63
.ai/HANDOFF.md
Normal file
@@ -0,0 +1,63 @@
|
||||
<!-- Keep under ~2K tokens. Old handoffs live in SESSION_LOG.md. Do not let this file accumulate history. -->
|
||||
|
||||
# HANDOFF.md
|
||||
|
||||
**Last updated:** 2026-04-27 22:30 EDT
|
||||
|
||||
**Active task:** **Escalation Mode** wedge build. See [`CURRENT_TASK.md`](CURRENT_TASK.md) for the full status; this file holds the resume point only.
|
||||
|
||||
**Branch:** `feat/escalation-metric-endpoint` — pushed (latest: `029680a`). **Draft PR #155** open against `main` ([gitea.resolutionflow.com/chihlasm/resolutionflow/pulls/155](https://gitea.resolutionflow.com/chihlasm/resolutionflow/pulls/155)). Wedge is feature-complete pending visual QA + the deferred follow-ups in `CURRENT_TASK.md`. **/escalate and /handoff are unified** — every escalation goes through `HandoffManager` and produces the full set of artifacts (handoff row, AppNotification, SSE bus event, Slack/Teams via `notify()`, per-user emails, documentation, PSA push) regardless of which URL it entered through.
|
||||
|
||||
## Status
|
||||
|
||||
Previous session shipped the two remaining frontend slices: live-arrival SSE subscription in `EscalationQueue.tsx`, and the magic-moment `HandoffContextScreen` for senior pickup.
|
||||
|
||||
What landed (commits added to the branch):
|
||||
|
||||
- `b8627f4` feat(escalations): subscribe EscalationQueue to live SSE arrivals — `streamEscalations` in `aiSessions.ts` (fetch-based `ReadableStream` parser; native `EventSource` can't send auth headers); `HandoffCreatedEvent` + `EscalationStreamHandlers` types; `EscalationQueue.tsx` rewrite with `AbortController`-managed subscription, exponential-backoff reconnect (1s → 30s cap, resets on `ready`), prepend-on-arrival with locked 200ms slide-in, tab-title `(N)` prefix while `document.hidden`, `prefers-reduced-motion` swap, ARIA live region.
|
||||
- `f65b657` docs(ai): handoff state after frontend SSE slice lands.
|
||||
- `8e9d22e` feat(escalations): magic-moment handoff-context screen on pickup — new `HandoffContextScreen.tsx` (4 sections; renders gracefully when `ai_assessment` is null per the 5s timeout from `9bdd995`; ARIA dialog + focus on primary CTA + Esc dismiss for re-open overlay; `prefers-reduced-motion` honored). `FlowPilotSessionPage.tsx` integration: on `?pickup=true`, fetch the handoff list first (account-scoped via RLS, no claim required), find the latest unclaimed escalate handoff, render the screen and skip `loadSession` (senior would 404 pre-claim). "Start here" calls `claimHandoff`, drops the pickup query, and dismisses — `loadSession` then fires because senior is now `escalated_to_id`. Toolbar "Context" button on active sessions re-opens the screen as a dismissible overlay (visible only when senior arrived via the magic-moment flow this session).
|
||||
- `c194ba4` docs(ai): handoff state after magic-moment screen lands.
|
||||
- `641853a` fix(escalations): bell-icon notification opens the pickup flow — `_build_notification_link` for `session.escalated` now ends with `?pickup=true` so notification clicks route through the senior-pickup flow. `GET /ai-sessions/{id}` now allows account-scoped read for `requesting_escalation` / `escalated` status (RLS already enforces tenant boundary; the owner-only guard was overly restrictive for explicitly-shared in-transit states). Without these two fixes the user observed bell-icon clicks "just clearing the notification" — the navigation was happening but landing on a 404 the senior couldn't escape from.
|
||||
- `2a2329a` docs(ai): handoff state after bell-icon fix; record draft PR #155.
|
||||
- `029680a` feat(escalations): unify `/escalate` through `HandoffManager` — single canonical path for every escalation. `HandoffCreateRequest.target_user_id` added (rejects self-targeting). `HandoffManager.create_handoff` for intent='escalate' now sets `session.escalation_reason` + `escalated_to_id`, builds the legacy AI-enhanced escalation_package via Sonnet (lazy-import from flowpilot_engine, graceful fallback), and merges handoff metadata into it; eager-loads `session.steps` + `session.user` to dodge async lazy-load greenlet errors. New `HandoffManager.finalize_escalation` runs `_generate_documentation` + `_push_to_psa` + `notify()` pre-commit so the AppNotification rows and PSA writes land atomically with the handoff. `dispatch_escalation_notifications` keeps only fire-and-forget IO (bus publish + per-user emails) post-commit. The `/escalate` endpoint is a thin shim: owner-only session lookup → `create_handoff(intent='escalate')` → `finalize_escalation` → commit → `dispatch_escalation_notifications` → return `SessionCloseResponse`. `flowpilot_engine.escalate_session` is no longer called by any endpoint. `pickup_session` accepts both `requesting_escalation` and `escalated` for in-flight migration. Escalation queue list + sidebar count match either status.
|
||||
|
||||
Verified:
|
||||
|
||||
- `tsc -b` exit 0 after each frontend commit.
|
||||
- Full backend test suite after unification: `1103 passed in 259.63s` with `-n auto`.
|
||||
- Live SSE handshake against the running dev stack: 200 + `text/event-stream`; `ready` frame on connect; `handoff_created` frame with full payload arrived after posting a handoff via the API. Wire format matches the parser exactly.
|
||||
- Live claim flow against the running dev stack: `listHandoffs` returns the unclaimed handoff for a senior pre-claim; `claimHandoff` flips session status from `escalated` → `active` and sets `escalated_to_id`; subsequent `GET /ai-sessions/{id}` succeeds.
|
||||
- Live access-policy verification: senior (non-owner, non-target) can now `GET` an in-transit escalated session detail.
|
||||
- Live unification verification: a single legacy `/escalate` call from a junior produced status='escalated', a `SessionDocumentation`, a `SessionHandoff` row, an attempted PSA push (`no_psa` since no ticket linked), AND an `AppNotification` row for the team admin with title "Session escalated by Jordan Tech" and link `/pilot/{session_id}?pickup=true`. The bell-icon click now lands the senior in the magic-moment flow with the actual handoff data.
|
||||
|
||||
Not yet verified (would need a real browser session): the slide-in animation visually plays, tab title actually updates, reduced-motion media-query path renders, AbortController cleanup on unmount, exponential backoff after a real network blip, the magic-moment screen layout/typography looks right, dissolve transition feels right. Wire contract + integration semantics are confirmed; visuals are next.
|
||||
|
||||
Smoke-test artifact: a single test handoff (`0f6149db…` on session `50ea20d4…`) was claimed during verification and is now an `active` session owned by the engineer test user. Harmless; useful as visual demo data.
|
||||
|
||||
## Resume point
|
||||
|
||||
1. **Visual QA via `/qa` against the dev stack.** End-to-end demo flow: junior escalates via EscalateModal → senior gets bell-icon notification → senior clicks the notification (now routes through `?pickup=true`) → magic-moment screen renders with the rich handoff data → Start here → FlowPilot session view loads. Also: open `/escalations` as senior with a second session escalating in the background, watch the slide-in + tab-title flash. The PR description has a checklist mirroring this.
|
||||
2. **Pick up the deferred follow-ups** in `CURRENT_TASK.md`. Highest-leverage: suggested-step chips below the chat input (Codex correction, locked design — needs threading through `FlowPilotSession` → `FlowPilotMessageBar`). Next: `HandoffManager._generate_snapshot` expansion to include the recent diagnostic timeline pre-claim — though this is lower-priority now that the unified path already merges the legacy enriched escalation_package into the dual-write, so the magic-moment screen has access to `steps_tried` / `remaining_hypotheses` / `suggested_next_steps` once it's wired to read them.
|
||||
3. Optional v1: owner-facing `/analytics/escalations` page; Playwright e2e for the GTM Loom demo path.
|
||||
4. Eventual cleanup: `flowpilot_engine.escalate_session` is no longer called by any endpoint and could be deleted; the legacy `SessionBriefing` render branch in `FlowPilotSessionPage.tsx` is effectively dead code for any new escalation (magic-moment takes over) but still useful for in-flight legacy `requesting_escalation` sessions during the transition window. Both can come out after pilots have run a couple of weeks on the unified path.
|
||||
|
||||
## Useful breadcrumbs
|
||||
|
||||
- SSE endpoint: [`backend/app/api/endpoints/session_handoffs.py`](../backend/app/api/endpoints/session_handoffs.py) — `stream_escalations`.
|
||||
- Pub/sub bus: [`backend/app/core/escalation_bus.py`](../backend/app/core/escalation_bus.py).
|
||||
- Frontend SSE consumer: [`frontend/src/api/aiSessions.ts`](../frontend/src/api/aiSessions.ts) → `streamEscalations`.
|
||||
- Live-arrival queue UI: [`frontend/src/components/flowpilot/EscalationQueue.tsx`](../frontend/src/components/flowpilot/EscalationQueue.tsx).
|
||||
- Magic-moment screen: [`frontend/src/components/flowpilot/HandoffContextScreen.tsx`](../frontend/src/components/flowpilot/HandoffContextScreen.tsx).
|
||||
- Pickup integration: [`frontend/src/pages/FlowPilotSessionPage.tsx`](../frontend/src/pages/FlowPilotSessionPage.tsx) — `magicState`, `handleStartHere`, `openHandoffContextOverlay`.
|
||||
- Notification dispatch: [`backend/app/services/handoff_manager.py`](../backend/app/services/handoff_manager.py) — `dispatch_escalation_notifications`.
|
||||
- Metric endpoint: [`backend/app/api/endpoints/flowpilot_analytics.py`](../backend/app/api/endpoints/flowpilot_analytics.py).
|
||||
|
||||
## Watch-outs
|
||||
|
||||
- Do not reintroduce `client.stream()`/ASGITransport tests for infinite SSE responses; test the generator directly.
|
||||
- The bus is acceptable for v1 pilot scale only because Railway is single-replica. Redis pub/sub is the obvious swap when horizontal scaling appears.
|
||||
- `streamEscalations` doesn't drive token refresh on a mid-stream 401 — the Axios interceptor only covers axios calls. Acceptable for v1.
|
||||
- The handoff snapshot today is sparse (`problem_summary, problem_domain, status, step_count, confidence_tier` plus optional branch info). The magic-moment screen's "What's been tried" section currently shows engineer notes + step-count affordance, not the actual step timeline. Snapshot expansion is the right fix.
|
||||
- `HandoffResponse.ai_assessment_data.confidence` is typed `number` on the frontend but the backend currently emits `'low' | 'medium' | 'high'` strings. The `ConfidenceBadge` component handles both shapes at runtime; the type definition is stale and should be widened to `number | 'low' | 'medium' | 'high'`.
|
||||
- The toolbar "Context" button is hidden on revisited active sessions where the senior didn't arrive via magic-moment this session — known scope cut. Lazy-fetching handoff list on session-load (when status was previously `escalated`) is the cleanup.
|
||||
254
.ai/PROJECT_CONTEXT.md
Normal file
254
.ai/PROJECT_CONTEXT.md
Normal file
@@ -0,0 +1,254 @@
|
||||
# PROJECT_CONTEXT.md — ResolutionFlow
|
||||
|
||||
> SaaS troubleshooting platform for MSPs. Stable architectural truth. Updated only when the repo's shape changes.
|
||||
|
||||
---
|
||||
|
||||
## Product & naming
|
||||
|
||||
Canonical product name is **ResolutionFlow**. `patherly` is the legacy internal name — still present in DB name (`patherly` on Railway, `resolutionflow` locally), some Railway service names, and historical paths. Treat as aliases, not canonical. Docker containers are `resolutionflow_*`.
|
||||
|
||||
**User terminology:** "Flows" (not Trees), "Projects" (not Procedures), "Solutions Library" (not Step Library). Maintenance flows hidden from pilot UI (backend retains them). DB column `tree_type` values unchanged.
|
||||
|
||||
---
|
||||
|
||||
## SaaS shape
|
||||
|
||||
Multi-tenant by account. Primary role hierarchy: `super_admin` > `owner` > `engineer` > `viewer` — driven by `is_super_admin` + `account_role`. Never `role=='admin'` — use `is_super_admin`. Separate team-scoped admin gate exists orthogonally to the role hierarchy: `is_team_admin=True` + valid `team_id`, enforced by `require_team_admin`. Backend deps in `app/api/deps.py`: `get_current_active_user`, `require_engineer_or_admin`, `require_admin`, `require_account_owner`, `require_team_admin`. Frontend: `usePermissions()` hook. Central logic in `backend/app/core/permissions.py` + `frontend/src/hooks/usePermissions.ts`.
|
||||
|
||||
---
|
||||
|
||||
## Status
|
||||
|
||||
Go-to-Market Validation (pre-PMF). Backend feature-complete (55+ endpoints, 100+ tests). Phase 0.5 FlowPilot telemetry baseline accruing. See [CURRENT-STATE.md](../CURRENT-STATE.md) for live status, [03-DEVELOPMENT-ROADMAP.md](../03-DEVELOPMENT-ROADMAP.md) for phases.
|
||||
|
||||
---
|
||||
|
||||
## Tech stack
|
||||
|
||||
- **Backend:** Python 3.11 + FastAPI, SQLAlchemy 2.0 async (asyncpg), Alembic, Pydantic v2, JWT (python-jose + bcrypt, JTI refresh rotation), APScheduler (in-process with FastAPI lifespan).
|
||||
- **Frontend:** React 19 + Vite + TypeScript, Tailwind v4 (CSS-only config in `index.css`), Zustand (immer + zundo), React Router v7, Axios (token-refresh interceptor), Lucide.
|
||||
- **DB:** PostgreSQL 16 (RLS enabled Phase 4, pgvector).
|
||||
|
||||
---
|
||||
|
||||
## Project structure
|
||||
|
||||
```
|
||||
resolutionflow/
|
||||
├── backend/
|
||||
│ ├── app/
|
||||
│ │ ├── main.py # FastAPI entry
|
||||
│ │ ├── api/endpoints/ # 50+ routers registered in api/router.py — auth/admin, trees/sessions, AI/chat, scripts, integrations, uploads, accounts, FlowPilot, etc.
|
||||
│ │ ├── api/deps.py # auth deps (incl. require_team_admin)
|
||||
│ │ ├── api/router.py # registration
|
||||
│ │ ├── core/ # config, database, permissions, security, audit, rate_limit
|
||||
│ │ ├── models/ # SQLAlchemy (incl. FlowProposal)
|
||||
│ │ ├── schemas/ # Pydantic
|
||||
│ │ ├── services/psa/ # PSA provider pattern (base, connectwise/, autotask/, halopsa/, cache, encryption, exceptions, registry, ticket_context, types)
|
||||
│ │ ├── services/knowledge_flywheel.py + _scheduler.py
|
||||
│ │ └── services/knowledge_gap_service.py
|
||||
│ ├── alembic/versions/ # 001-070 sequential, then hex hash
|
||||
│ ├── scripts/ # seed_data, seed_trees, seed_test_users
|
||||
│ └── tests/ # pytest integration
|
||||
├── frontend/
|
||||
│ ├── src/
|
||||
│ │ ├── api/ # Axios client + endpoint modules
|
||||
│ │ ├── components/ # common, layout, dashboard, tree-editor, session, procedural, procedural-editor, library, step-library, ui, flowpilot
|
||||
│ │ ├── hooks/ # usePermissions, useSessionTimer, useKeyboardShortcuts
|
||||
│ │ ├── pages/
|
||||
│ │ ├── store/ # Zustand (auth, treeEditor, proceduralEditor, userPreferences, scriptGeneratorStore)
|
||||
│ │ └── types/
|
||||
│ └── (Tailwind v4 CSS-only config in src/index.css)
|
||||
├── docs/plans/archive/ # pre-March 2026 plans
|
||||
├── docs/connectwise/ # CW API reference + best-practices guides
|
||||
├── docs/LESSONS-ARCHIVE.md # archived lessons (fixes in code)
|
||||
├── .ai/ # dual-agent handoff system (see .ai/README.md)
|
||||
├── CLAUDE.md · AGENTS.md · CURRENT-STATE.md · DESIGN-SYSTEM.md · DEV-ENV.md
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Dev commands
|
||||
|
||||
Full setup in [DEV-ENV.md](../DEV-ENV.md) (host-agnostic, with homelab Proxmox reference topology). Day-to-day:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.dev.yml up -d # start stack
|
||||
cd backend && source venv/bin/activate && uvicorn app.main:app --reload
|
||||
cd frontend && npm run dev
|
||||
pytest --override-ini="addopts=" # tests (first time: CREATE DATABASE resolutionflow_test)
|
||||
cd backend && alembic upgrade head # migrate
|
||||
cd backend && alembic revision -m "desc" # manual migration (preferred per Lesson 77)
|
||||
cd backend && alembic revision --autogenerate -m "desc" # picks up drift; review carefully
|
||||
cd frontend && npm run build # stricter than tsc --noEmit — final check
|
||||
cd frontend && npx tsc -b # TS-only check when dist/ has EACCES
|
||||
docker exec -it resolutionflow_postgres psql -U postgres -d resolutionflow
|
||||
python -m scripts.seed_trees # seed (from backend/)
|
||||
```
|
||||
|
||||
**Never pass `--rev-id`** to alembic — let it generate the hex hash.
|
||||
|
||||
---
|
||||
|
||||
## URLs & test users
|
||||
|
||||
**URLs:** Frontend <http://localhost:5173>, backend <http://localhost:8000>, API docs <http://localhost:8000/api/docs>.
|
||||
|
||||
**Test users** (all password `TestPass123!`): `admin@resolutionflow.example.com` (super_admin), `teamadmin@resolutionflow.example.com`, `engineer@resolutionflow.example.com`, `pro@resolutionflow.example.com`.
|
||||
|
||||
---
|
||||
|
||||
## CI
|
||||
|
||||
Gitea (`gitea.resolutionflow.com/chihlasm/resolutionflow/actions`). `gh` CLI works for issues/PRs on the GitHub mirror, but not CI runs.
|
||||
|
||||
---
|
||||
|
||||
## Deployment (Railway)
|
||||
|
||||
- **Prod:** `resolutionflow.com` (frontend), `api.resolutionflow.com` (backend).
|
||||
- Auto-deploy: Gitea push → GitHub mirror → Railway follows GitHub `main`.
|
||||
- PR environments auto-created; need manual domain generation + `VITE_API_URL` with `https://` prefix.
|
||||
- `ALLOW_RAILWAY_ORIGINS=true` for `*.up.railway.app` CORS.
|
||||
- Shared Variables (Railway project-level) auto-propagate to PR envs — use for secrets like `ANTHROPIC_API_KEY`.
|
||||
- Super admin utility: `backend/make_superadmin_simple.py list|<email>`.
|
||||
|
||||
---
|
||||
|
||||
## ConnectWise PSA
|
||||
|
||||
Reference: `docs/connectwise/` — start with `CONNECTWISE-API-REFERENCE.md`, then the `best-practices/` guides. Extracted OpenAPI spec in `connectwise-psa-resolutionflow-reference.json` (670 endpoints, v2025.16); full spec in `connectwise-psa-openapi-full.json`.
|
||||
|
||||
- **Auth:** API Key (Base64 `companyId+publicKey:privateKey`) + `clientId` header every request. `clientId` is server-side (`CW_CLIENT_ID` in `config.py`) — identifies ResolutionFlow, not per-tenant. Per-connection: `company_id`, `public_key`, `private_key`, `server_url`.
|
||||
- **Architecture:** `services/psa/` provider pattern — `PSAProvider` base, `ConnectWiseProvider` impl, `PsaProviderRegistry` for multi-PSA dispatch. Credentials encrypted at rest via `services/psa/encryption.py` (Fernet). Per-team credentials, never per-user. Endpoints in `api/endpoints/integrations.py`. In-memory TTL cache in `services/psa/cache.py`.
|
||||
- **Integration flows:** session docs → ticket notes (`POST /service/tickets/{id}/notes`, markdown supported); ticket context → FlowPilot; callbacks via `/system/callbacks` with HMAC verification.
|
||||
- **API rules:** pin version via Accept header `application/vnd.connectwise.com+json; version=2025.16`. Paginate ≤1000/page. Dynamic base URL via `/login/companyinfo/{companyId}`. Request minimal permissions (MY, not ALL).
|
||||
|
||||
---
|
||||
|
||||
## Coding standards
|
||||
|
||||
- **Python:** type hints everywhere, async/await for DB, Pydantic v2, `DateTime(timezone=True)` always.
|
||||
- **TypeScript:** interfaces for all data, `const` over `let`, functional components + hooks, shared logic in custom hooks.
|
||||
- **Git:** feature branch before committing (`git checkout -b feat/feature-name`). Commit format: `type: description` (feat/fix/refactor/docs/test/chore). Large features: commit per phase with `npm run build` validation. Push to Gitea — auto-mirrors to GitHub (`.gitea/workflows/mirror-to-github.yml`); never push GitHub directly. (Agent-specific `Co-Authored-By` trailers live in CLAUDE.md / AGENTS.md.)
|
||||
|
||||
**After shipping:** update [CURRENT-STATE.md](../CURRENT-STATE.md) + [03-DEVELOPMENT-ROADMAP.md](../03-DEVELOPMENT-ROADMAP.md), `gh issue close #N` for resolved issues, add lessons only for non-obvious traps (otherwise let the code speak).
|
||||
|
||||
---
|
||||
|
||||
## Common tasks
|
||||
|
||||
- **New endpoint:** `endpoints/` → `router.py` → `schemas/` → tests → frontend API client.
|
||||
- **New page:** `pages/` → route in `router.tsx` → nav in `AppLayout.tsx`.
|
||||
- **New public route:** top-level in `router.tsx` alongside `/login`, not inside `ProtectedRoute`.
|
||||
- **New frontend API module:** types in `types/` → export from `types/index.ts` → client in `api/` → export from `api/index.ts`.
|
||||
- **Schema change:** update model → `alembic revision -m "desc"` → review → `alembic upgrade head`.
|
||||
- **New `VITE_*` env var:** add as `ARG` + `ENV` in `frontend/Dockerfile` for Railway builds (Lesson 60 — Railway env vars are runtime-only, Vite bakes at build time).
|
||||
- **Account sub-page:** add route in `router.tsx` under `account` children + add link card in `AccountSettingsPage.tsx` — `AccountLayout` has NO sidebar nav.
|
||||
|
||||
---
|
||||
|
||||
## Design system
|
||||
|
||||
**Source of truth: [DESIGN-SYSTEM.md](../DESIGN-SYSTEM.md).** Read before any visual change.
|
||||
|
||||
- Flat high-contrast dark theme, Sentry/PostHog-inspired. **No** glass, backdrop blur, ambient orbs, gradient surfaces.
|
||||
- Accent **electric blue** (#60a5fa dark / #2563eb light) — ≤5% of UI, interactive elements only. Warning amber (#fbbf24), info cyan (#67e8f9), success green (#34d399), danger red (#f87171). Each with `-dim` at 10% opacity.
|
||||
- Backgrounds: `bg-sidebar` (#0e1016) → `bg-page` (#16181f) → `bg-card` (#1e2028) → `bg-elevated` (#2a2d38). Borders `border-default` / `border-hover`.
|
||||
- Text: `text-heading` → `text-primary` → `text-muted-foreground` → `text-muted`.
|
||||
- Fonts: IBM Plex Sans (body), Bricolage Grotesque (heading, 700 weight for logo), JetBrains Mono (code).
|
||||
- Logo: 30px gradient square (ember orange) + "ResolutionFlow" in Bricolage Grotesque. Assets in `brand-assets/`, `frontend/src/assets/brand/`, `frontend/public/icons/`.
|
||||
- Mockups: `docs/mockups/` (HTML).
|
||||
- **Deprecated — do not use:** glass-card, glass-stat, `bg-gradient-brand`, `backdrop-filter: blur()`, ambient orbs, purple gradients, ember orange as accent, cyan as accent (cyan is info only).
|
||||
|
||||
---
|
||||
|
||||
## Frontend patterns
|
||||
|
||||
- **Component basics:** `cn()` from `@/lib/utils`, Lucide icons, `Modal.tsx` for modals (mobile-responsive `items-end sm:items-center` + `max-w-full sm:max-w-lg`).
|
||||
- **Types:** Create in `types/`, export from `types/index.ts`, `import type { T } from '@/types'`.
|
||||
- **Routing:** `getTreeNavigatePath()` / `getTreeEditorPath()` from `@/lib/routing`. Tree editor is `/trees/new`. All dashboard session clicks → `/pilot/:id` regardless of `session_type`.
|
||||
- **Lazy routes:** `lazyWithRetry` from `@/lib/lazyWithRetry.ts`, not `React.lazy` (auto-reload on stale chunks).
|
||||
- **Public pages:** raw `fetch()` with full URL, NOT `apiClient` (which requires auth tokens).
|
||||
- **Toast:** `toast.warning()` not `toast.warn()`. Import from `@/lib/toast` — methods: `success`, `error`, `warning`, `info`.
|
||||
- **Assistant chat:** uses local React `useState`, not Zustand. All three send paths (`handleSend`, `sendPrefill`, `handleResumeNew`) must call `setShowTaskLane(true)` when response has actions/questions.
|
||||
- **Chat backend wiring:** `aiSessionsApi.sendChatMessage` → `/ai-sessions/{id}/chat` → `unified_chat_service.py`. NOT `assistant_chat_service.py` (removed except retention settings).
|
||||
- **FlowPilot:** Actions live in page header (Resolve/Escalate/Share Update + overflow). `useBlocker` for active-session nav guard. "Pause & Leave" auto-pauses.
|
||||
- **AI markers:** `[QUESTIONS]`, `[ACTIONS]`, `[FORK]`, `[DELTA]...[/DELTA]` (editor), `[TREE_UPDATE]` (troubleshooting builder), `[STEPS_UPDATE]` (procedural builder), `[METADATA]`. Parsed in `unified_chat_service.py`; conversation history stores stripped `display_content`. If markers disappear: check system-prompt final reminder + per-user-message `[SYSTEM: ...]` injection in `_call_anthropic_cached()`.
|
||||
- **Image uploads:** paste/attach → Railway S3 via `uploadsApi.upload()` → resized by `storage_service.resize_image_for_vision()` (Pillow, 1568px max, PNG→JPEG) → base64 → Claude multimodal blocks. Max 3/msg. Images NOT stored in history.
|
||||
- **Async select-load-apply:** guard with a ref (pattern in `AssistantChatPage` `currentChatRef`). Update synchronously on every selection change; after every `await`, bail out if `ref.current !== thisId`.
|
||||
- **Editor-Embedded Flow Assist:** `EditorAIPanel` (320px side panel) + `useEditorAI`. Ghost nodes via `_suggestion: true`. Route actions via `settings.get_model_for_action()`.
|
||||
- **Script Builder:** `/script-builder`, chat-style. Backend `ScriptBuilderSession`, `script_builder_service.py`, endpoints `/scripts/builder/`. FlowPilot handoff via `action_type: "open_script_builder"` + `sessionStorage`.
|
||||
- **Intake form field schema:** `variable_name` + `field_type` (NOT `name` / `type`).
|
||||
- **Node field priority** (copilot, summaries): `title` → `question` → `description` → `content` → `label`.
|
||||
- **Procedural sessions auto-start** on page load (no intake/Start screen). Troubleshooting flows DO have a start screen.
|
||||
|
||||
---
|
||||
|
||||
## Critical lessons
|
||||
|
||||
> Lessons 1-40 archived to [docs/LESSONS-ARCHIVE.md](../docs/LESSONS-ARCHIVE.md) — fixes baked into the codebase. **Grep the archive when an error message or symptom is unfamiliar, or after two failed attempts at resolving an issue.** Don't pre-load for routine work.
|
||||
|
||||
### Backend / data
|
||||
|
||||
- **APScheduler interval jobs always `max_instances=1`** — without it, overlapping runs reprocess records (TOCTOU).
|
||||
- **`get_db` rolls back on exception** — never remove the `await session.rollback()`, or one failed request poisons the connection with `InFailedSQLTransaction` cascading.
|
||||
- **Startup routines on tenant-isolated tables must use `_admin_session_factory()`, not `get_db()`.** Phase 4 RLS has no `app.current_account_id` set at startup. `get_service_account_id` is safe (reads cached `app.state`).
|
||||
- **Backfill migrations adding `account_id`:** grep ALL `ModelClass(` sites in service code to verify `account_id=` is passed. SQLAlchemy accepts `None` silently — Phase 4 RLS WITH CHECK surfaces the problem at runtime as `InsufficientPrivilegeError: new row violates row-level security policy`.
|
||||
- **`tree_shares.account_id = tree.account_id`**, never `current_user.account_id`. A super_admin sharing another tenant's tree must produce the share in the tree owner's tenant, or it becomes invisible post-RLS.
|
||||
- **Global tables (no `account_id`, never in RLS migrations):** `script_categories`, `platform_steps`, `template_trees`, `plan_feature_defaults`, `accounts`. Scan at class level — one `.py` file can hold multiple classes with different columns (e.g. `ScriptCategory` vs `ScriptTemplate`).
|
||||
- **`ai_sessions.status` is VARCHAR(30)** — fits `requesting_escalation` (23 chars). Migration `f0aad74ea51b` widened from 20.
|
||||
- **PostgreSQL `func.sum(case(...))` returns `Decimal` via asyncpg** — cast to `int()` before Pydantic `dict[str, Any]`.
|
||||
- **Enhancement / branch_addition proposals need `modified_flow_data` via "Edit & Publish"** — backend 400 on direct approve. Only `new_flow` supports direct approve.
|
||||
- **Adding email types:** static async method on `EmailService` in `core/email.py`. Fire-and-forget from endpoints (log errors, don't fail the request).
|
||||
|
||||
### AI / FlowPilot
|
||||
|
||||
- **Anthropic SDK `max_retries=1`** — default of 2 can take 3× the timeout.
|
||||
- **Model tier routing:** `settings.get_model_for_action(action_type)`. Always alias form (`claude-sonnet-4-6`).
|
||||
- **FlowPilot must ask GUI-vs-script before suggesting either** when both are viable — see `FLOWPILOT_SYSTEM_PROMPT` in `flowpilot_engine.py`.
|
||||
- **Telemetry events to grep:** `anthropic.cache` (prompt-cache hit/create), `mcp.turn` (per-turn MCP availability), `mcp.fallback` (MCP silent-retry fired).
|
||||
- **Don't put literal payloads in system prompts.** Bit us twice in one day: a worked `[QUESTIONS]` example with literal "Outlook + jsmith" content, and a full DNS troubleshooting tree, both caused Claude to recite that content on unrelated tickets — the symptom looked like task-lane state leaking across chats. The fix is structural: every output example in a system prompt uses `<placeholder>` syntax (`{"text": "<one short, specific question>"}`), never literal field values. Real-looking format examples live in few-shot messages (separate file, separate code path), not system prompts. Guardrail: `tests/test_prompt_anti_parrot.py` scans every `*_PROMPT`/`*_SCHEMA`/`*_PROTOCOL`/`*_FORMAT` constant in `app/services/` and `app/core/`; CI fails when a marker block contains a literal JSON value or when a known leaked token (jsmith, DC01, ADSync, Dnscache, etc.) appears anywhere in a prompt.
|
||||
|
||||
### Frontend / UI
|
||||
|
||||
- **Flex height chain:** every ancestor from `app-shell` grid to React Flow canvas needs `flex` + `flex-1` + `min-h-0` or `h-full`. Missing `flex` collapses to 0. Same rule for FlowPilot action bar and any tall scroller.
|
||||
- **React Flow CSS in Tailwind v4:** import in `index.css`, not component JS. Override dark theme via `--xy-*` CSS vars.
|
||||
- **`text-secondary` renders invisible on dark** — Tailwind v4 maps it to `--color-secondary` (a surface color). Use `text-muted-foreground` for readable secondary text. Avoid `text-muted` for body — labels only.
|
||||
- **`bg-accent` is electric blue — never for code/kbd.** Use `bg-white/[0.12] border border-white/[0.06]` for inline code, `bg-white/[0.08]` for kbd. Accent reserved for interactive elements.
|
||||
- **`landing.css` uses self-contained `--lp-*` vars** — never `var(--color-*)` theme tokens (they resolve incorrectly outside the app shell).
|
||||
- **Never `transition: all`** — list properties explicitly, or layout props animate and jank.
|
||||
- **Date range filter end dates:** `setHours(23, 59, 59, 999)` before sending, or the day's items are excluded. For string-based date inputs, append `T23:59:59.999Z`.
|
||||
- **TopBar search:** full bar `hidden sm:block`, icon button `sm:hidden` — both open CommandPalette.
|
||||
- **Hover pop-out cards:** scrim `pointer-events-none`, expanded card has its own click handler at `z-50`, dismiss via `onMouseLeave` on wrapper. Never put handlers on the scrim.
|
||||
- **`tsc -b` in Dockerfile is stricter than `tsc --noEmit`** — enforces `noUnusedLocals` / `noUnusedParameters` as hard errors. Check IDE yellow squiggles before pushing.
|
||||
- **Dashboard prefill auto-submits** via `useEffect` + `prefillHandledRef` guard — no double-enter.
|
||||
- **Global Axios 5xx interceptor fires before component `.catch()`** — fix optional-data endpoints at the source (return `[]` / `{}` on provider failure), not in the component.
|
||||
- **Playwright strict mode:** scope selectors to avoid sidebar/main ambiguity. Use `getByRole('heading', { name })` or `.animate-scale-in` locators, not bare `getByText()`.
|
||||
|
||||
### Env / infra
|
||||
|
||||
- **Node 20.19+ required** (Vite 7). `nvm use 20` or `PATH="$HOME/.nvm/versions/node/v20.19.0/bin:$PATH"`.
|
||||
- **Railway backend service is `patherly`, DB name `railway`.** Public Postgres proxy: `interchange.proxy.rlwy.net:45797`.
|
||||
- **Railway Object Storage bucket `resolutionflow-uploads`.** Env vars `STORAGE_*`. boto3 in `storage_service.py`. Dockerfile needs Pillow + `libjpeg-dev` / `zlib1g-dev`.
|
||||
- **PostHog:** `PostHogProvider` + `posthog.init()` in `main.tsx`. Helpers in `lib/analytics.ts`. Env: `VITE_PUBLIC_POSTHOG_KEY`, `VITE_PUBLIC_POSTHOG_HOST`. `identifyUser()` in `authStore.fetchUser()`, `resetAnalytics()` on logout.
|
||||
- **bun PATH on devserver01:** `BUN_INSTALL="$HOME/.bun"`, `PATH="$BUN_INSTALL/bin:$PATH"`. Playwright Chromium needs `libatk1.0-0 libatk-bridge2.0-0 libcups2 libxkbcommon0 libatspi2.0-0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libasound2`.
|
||||
- **Full-stack change:** trace schema → endpoint → API client → hook → store → UI. Don't assume one end proves the other.
|
||||
- **Dev env** — see [DEV-ENV.md](../DEV-ENV.md) for current topology, `REPO_ROOT` requirement when compose runs inside a container, Vite `allowedHosts`, linuxserver.io `group_add` + custom-cont-init.d workaround, `docker compose up` no-op-on-unchanged-hash gotcha.
|
||||
|
||||
---
|
||||
|
||||
## Quick reference
|
||||
|
||||
| What | Where |
|
||||
|---|---|
|
||||
| Detailed status | [CURRENT-STATE.md](../CURRENT-STATE.md) |
|
||||
| Roadmap | [03-DEVELOPMENT-ROADMAP.md](../03-DEVELOPMENT-ROADMAP.md) |
|
||||
| Design system | [DESIGN-SYSTEM.md](../DESIGN-SYSTEM.md) |
|
||||
| Dev env | [DEV-ENV.md](../DEV-ENV.md) |
|
||||
| Archived lessons | [docs/LESSONS-ARCHIVE.md](../docs/LESSONS-ARCHIVE.md) |
|
||||
| ConnectWise API | `docs/connectwise/` |
|
||||
| GitHub issues | `gh issue list --state open` |
|
||||
| Local API docs | <http://localhost:8000/api/docs> |
|
||||
| Handoff system | [.ai/README.md](README.md) |
|
||||
42
.ai/README.md
Normal file
42
.ai/README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# .ai/ — dual-agent handoff system
|
||||
|
||||
ResolutionFlow uses two coding agents: **Claude Code** (primary) and **OpenAI Codex** (resume when Claude hits session or weekly limits). This directory holds the shared state that lets either agent start a session with full context.
|
||||
|
||||
## Files
|
||||
|
||||
| File | Holds | Written when | Read when |
|
||||
|---|---|---|---|
|
||||
| [PROJECT_CONTEXT.md](PROJECT_CONTEXT.md) | Stable repo truth: stack, structure, SaaS shape, ConnectWise, coding standards, frontend patterns, critical lessons | Only when the repo's shape changes | Every session start |
|
||||
| [CURRENT_TASK.md](CURRENT_TASK.md) | The single active task: goal, DoD, assumptions, out-of-scope | On task start; status updates during work | Every session start |
|
||||
| [HANDOFF.md](HANDOFF.md) | Exact resume point: branch, where you left off, next steps, blockers | On session end / context-window limit | Every session start (most important) |
|
||||
| [TODO.md](TODO.md) | Backlog of work NOT currently active | When deferring or queueing work | Only when `CURRENT_TASK.md` is `complete` |
|
||||
| [DECISIONS.md](DECISIONS.md) | Append-only architectural decision log | When an architectural choice is made | Skim top entries each session |
|
||||
| [SESSION_LOG.md](SESSION_LOG.md) | Append-only chronological history | On session end | Only when broader context is needed |
|
||||
|
||||
Agent-specific tooling lives at the repo root:
|
||||
- [../CLAUDE.md](../CLAUDE.md) — Claude Code's tooling (GitNexus, gstack slash commands, Claude trailer)
|
||||
- [../AGENTS.md](../AGENTS.md) — OpenAI Codex's tooling (grep/rg fallbacks, Codex trailer)
|
||||
|
||||
Both root files contain an **identical shared-protocol block**. If you edit one, edit the other.
|
||||
|
||||
## The handoff ritual
|
||||
|
||||
At session end (limit hit, task complete, or user stop): update `HANDOFF.md` to reflect the new resume point, update `CURRENT_TASK.md` status if it changed, append to `DECISIONS.md` if you made an architectural call, append a session entry to `SESSION_LOG.md`, and WIP-commit any dirty working tree with `wip(handoff): <one-line>` unless told otherwise. Don't push.
|
||||
|
||||
## How to invoke a resume
|
||||
|
||||
Tell the agent:
|
||||
|
||||
> Read CLAUDE.md (or AGENTS.md) and follow its instructions.
|
||||
|
||||
The agent will read its root file, which directs it to `.ai/PROJECT_CONTEXT.md`, `.ai/CURRENT_TASK.md`, and `.ai/HANDOFF.md` before doing anything else.
|
||||
|
||||
## Recovery
|
||||
|
||||
The previous monolithic CLAUDE.md is recoverable via:
|
||||
|
||||
```bash
|
||||
git show pre-ai-handoff:CLAUDE.md
|
||||
```
|
||||
|
||||
(Tag `pre-ai-handoff` on commit `e110fed` — the snapshot taken before this migration.)
|
||||
152
.ai/SESSION_LOG.md
Normal file
152
.ai/SESSION_LOG.md
Normal file
@@ -0,0 +1,152 @@
|
||||
# SESSION_LOG.md
|
||||
|
||||
> Append-only chronological record. Newest entries at the top. Skim when broader context is needed.
|
||||
> Entry format:
|
||||
>
|
||||
> ```
|
||||
> ## YYYY-MM-DD HH:MM <timezone> — <agent> — <one-line summary>
|
||||
> - What was accomplished
|
||||
> - What was left for next session
|
||||
> - Files touched
|
||||
> ```
|
||||
|
||||
---
|
||||
|
||||
## 2026-04-27 22:30 EDT — Claude Code — Escalation Mode: unify /escalate through HandoffManager
|
||||
|
||||
- User pushed back on the dual-path proposal: "why would we want two different escalation methods? Should the new one just be the way we escalate regardless if we're using a PSA or not using a PSA?" Right answer. Unified everything through `HandoffManager`.
|
||||
- Backend changes (commit `029680a`):
|
||||
- `HandoffCreateRequest` gains optional `target_user_id`; rejects self-targeting.
|
||||
- `HandoffManager.create_handoff` for intent='escalate' now does what the legacy `flowpilot_engine.escalate_session` used to: sets `session.escalation_reason` and `escalated_to_id`, builds the legacy AI-enhanced `escalation_package` via Sonnet (`_build_escalation_package_enhanced` lazy-imported with graceful fallback), and merges handoff metadata (`intent`, `handoff_id`, `snapshot`, `engineer_notes`) into it. Eager-loads `session.steps` + `session.user` via `selectinload` to dodge async lazy-load `MissingGreenlet` errors.
|
||||
- New `HandoffManager.finalize_escalation`: generates `SessionDocumentation`, pushes to PSA, and runs `notify()` (bell-icon AppNotification + Slack/Teams external channels) — all pre-commit so persistent state lands atomically with the handoff. Pulls engineer name via a separate User query rather than relying on `session.user` lazy access.
|
||||
- `dispatch_escalation_notifications` keeps only the fire-and-forget IO (bus publish + per-user emails) post-commit. Found and fixed an in-flight bug: had originally put `notify()` inside dispatch (post-commit), which left `Notification` rows uncommitted — moved into `finalize_escalation` (pre-commit).
|
||||
- `/handoff` endpoint passes `target_user_id` through and calls `finalize_escalation` pre-commit.
|
||||
- `/escalate` is now a thin shim: owner-only session lookup → `create_handoff(intent='escalate')` → `finalize_escalation` → commit → `dispatch_escalation_notifications` → return `SessionCloseResponse`. `flowpilot_engine.escalate_session` is no longer called by any endpoint.
|
||||
- `pickup_session` accepts both `requesting_escalation` (legacy in-flight) and `escalated` (new canonical) so existing queue items migrate seamlessly.
|
||||
- Escalation queue list (`/escalation-queue`) and sidebar count match either status.
|
||||
- Frontend: `useFlowPilotSession` optimistic update flips status to `escalated` instead of `requesting_escalation` so the page state matches the unified backend response.
|
||||
- Verified end-to-end live against the running dev stack: a single legacy `/escalate` call from `engineer@` produced status=`escalated`, a `SessionHandoff` row (`ea9b375a…`, intent='escalate'), a `SessionDocumentation`, a PSA push attempt (`no_psa` since no ticket), AND an `AppNotification` for `teamadmin@` with title "Session escalated by Jordan Tech" and link `/pilot/{session_id}?pickup=true`. Backend test suite: `1103 passed in 259.63s` with `-n auto`. Frontend `tsc -b` clean.
|
||||
- The legacy `SessionBriefing` render branch in `FlowPilotSessionPage.tsx` is now effectively dead for any new escalation (magic-moment takes over via the handoff record), but stays in place during the transition for legacy in-flight `requesting_escalation` sessions. Slated for cleanup after pilots run a couple of weeks on the unified path. `flowpilot_engine.escalate_session` is similarly orphaned and can be deleted at the same time.
|
||||
- Files touched: `backend/app/api/endpoints/ai_sessions.py`, `backend/app/api/endpoints/session_handoffs.py`, `backend/app/api/endpoints/sidebar.py`, `backend/app/schemas/session_handoff.py`, `backend/app/services/flowpilot_engine.py`, `backend/app/services/handoff_manager.py`, `frontend/src/hooks/useFlowPilotSession.ts`.
|
||||
|
||||
## 2026-04-27 21:50 EDT — Claude Code — Escalation Mode: bell-icon notification fix; push + draft PR
|
||||
|
||||
- User ran a live escalation test via the EscalateModal (legacy `/escalate` path) and reported that clicking the bell-icon notification "just clears the notification instead of taking me to the session". Diagnosed: navigation IS happening, but the notification link template was `/pilot/{session_id}` without `?pickup=true`, so the senior landed on `FlowPilotSessionPage` with no pickup mode. `loadSession` then hit `GET /ai-sessions/{id}` which 404'd because the senior wasn't owner / `escalated_to_id` / picked-up handler. The user perceived the resulting error state as the action having done nothing.
|
||||
- Two-part backend fix shipped in `641853a`. (1) `_build_notification_link` for `session.escalated` now ends with `?pickup=true` so notification clicks route through the senior-pickup flow (handoff-based or legacy SessionBriefing). (2) `GET /ai-sessions/{id}` access policy: any account member can now read a session's detail when status is `requesting_escalation` or `escalated`. Tenant boundary enforced by RLS — the owner-only guard was overly restrictive for explicitly-shared in-transit states. After-pickup access (handler / `escalated_to_id`) checks still apply for active/resolved sessions.
|
||||
- Verified end-to-end live: re-login as senior engineer (non-owner, non-target) and `GET /ai-sessions/{escalated-session-id}` returns 200 with full detail. Backend regression with broader subset (`test_escalation_bus`, `test_handoff_manager`, `test_session_handoffs_api`, `test_flowpilot_analytics_escalations`, `test_sessions`, `test_session_sharing`) → 94 passed in 43.26s.
|
||||
- Pushed `feat/escalation-metric-endpoint` to Gitea. Opened **draft PR #155** against `main` via Gitea API ([gitea.resolutionflow.com/chihlasm/resolutionflow/pulls/155](https://gitea.resolutionflow.com/chihlasm/resolutionflow/pulls/155)). Title prefixed `WIP:` so Gitea marks it `draft: true`. PR body links the design + test-plan artifacts and mirrors the test plan as a checklist with visual QA + e2e demo flow as the unchecked items.
|
||||
- Open question for next session: EscalateModal still calls the legacy `/escalate` endpoint, not the new `/handoff` path. The wedge demo flow (junior escalates → magic-moment renders) is cleaner if EscalateModal goes through `/handoff`. Legacy path does PSA documentation push that the handoff path doesn't, so a parallel path (legacy escalate also creates a handoff record) is probably the right call rather than full migration.
|
||||
- Files touched: `backend/app/api/endpoints/ai_sessions.py`, `backend/app/services/notification_service.py`, `.ai/CURRENT_TASK.md`, `.ai/HANDOFF.md`, `.ai/SESSION_LOG.md`.
|
||||
|
||||
## 2026-04-27 21:30 EDT — Claude Code — Escalation Mode: magic-moment handoff-context screen on pickup
|
||||
|
||||
- Continued the same session that shipped the live-arrival SSE subscription. Added the magic-moment screen on top.
|
||||
- New `frontend/src/components/flowpilot/HandoffContextScreen.tsx`: presentational 4-section view (header with problem summary + domain + step count + escalated-time + priority badge; "What's been tried" with engineer notes + step-count affordance; "AI assessment" with likely_cause / suggested_steps / confidence badge; "Start here" CTA). Confidence badge accepts both numeric (0..1) and string ("low"/"medium"/"high") shapes — backend emits the latter, the frontend type says `number`, runtime handles both. Renders an explicit "assessment unavailable — model didn't respond in time" branch when `ai_assessment_data` is null (the 5s timeout from `9bdd995` fired). `prefers-reduced-motion` swaps `animate-slide-up` for `animate-fade-in`. ARIA `role=dialog` + `aria-modal=true` + focus on primary CTA on mount + Esc dismiss when used as a re-openable overlay.
|
||||
- Integration in `frontend/src/pages/FlowPilotSessionPage.tsx`: on `/pilot/:id?pickup=true`, fetch the handoff list via `handoffsApi.listHandoffs` (account-scoped via RLS, no claim required) and find the latest unclaimed escalate handoff. If found, render the screen and skip `loadSession` (the senior would 404 pre-claim because they aren't yet `escalated_to_id`). "Start here" calls `handoffsApi.claimHandoff`, drops the `?pickup=true` query, and dismisses the screen — the existing `loadSession` effect then fires because the senior is now `escalated_to_id`. New "Context" toolbar button on active sessions (visible only when the senior arrived via the magic-moment flow this session — handoff lookup on demand) re-opens the screen as a dismissible overlay.
|
||||
- Verified end-to-end against the running dev stack: `listHandoffs` returns the unclaimed handoff with full payload (engineer_notes, snapshot keys); `claimHandoff` flips session status from `escalated` → `active` and sets `escalated_to_id`; subsequent `GET /ai-sessions/{id}` succeeds. `tsc -b` exit 0. No backend changes; backend tests still `32 passed in 18.91s`.
|
||||
- Deferred to TODOs in `CURRENT_TASK.md`: suggested-step chips below the chat input (Codex correction; threads through to `FlowPilotMessageBar`); `HandoffManager._generate_snapshot` expansion to include the recent diagnostic timeline pre-claim (today's snapshot is just `problem_summary, problem_domain, status, step_count, confidence_tier`); toolbar "Context" button visibility on revisited active sessions; owner-facing `/analytics/escalations` page; Playwright e2e for the GTM Loom demo path.
|
||||
- Branch state: 3 new commits (`b8627f4` SSE subscription, `f65b657` handoff doc bump, `8e9d22e` magic-moment screen). Branch is unpushed — next session pushes + opens draft PR.
|
||||
- Files touched this slice: `frontend/src/components/flowpilot/HandoffContextScreen.tsx` (new), `frontend/src/components/flowpilot/index.ts`, `frontend/src/pages/FlowPilotSessionPage.tsx`, `.ai/CURRENT_TASK.md`, `.ai/HANDOFF.md`, `.ai/SESSION_LOG.md`.
|
||||
|
||||
## 2026-04-27 21:00 EDT — Claude Code — Escalation Mode: frontend SSE subscription in EscalationQueue
|
||||
|
||||
- Picked up `feat/escalation-metric-endpoint` after the Codex test-stabilization pass. Confirmed green starting state: focused backend subset `32 passed in 18.78s` with `-n auto`.
|
||||
- Implemented the live-arrival frontend slice. Added `streamEscalations(handlers, signal)` to `frontend/src/api/aiSessions.ts` — fetch-based `ReadableStream` reader (native `EventSource` can't send auth headers) that parses SSE frames (event/data/comment lines), buffers partial frames across chunks, ignores `: keepalive` heartbeats, dispatches `ready` and `handoff_created` events. Added `HandoffCreatedEvent` and `EscalationStreamHandlers` types in `frontend/src/types/ai-session.ts` mirroring the backend bus payload.
|
||||
- Rewrote `frontend/src/components/flowpilot/EscalationQueue.tsx`. SSE subscription with `AbortController` + exponential-backoff reconnect (1s → 30s cap, attempt counter resets on `ready`). On `handoff_created` the component refetches the queue, diffs against the previous IDs via a `sessionsRef`, prepends new arrivals (newest-first) above established cards (oldest-first preserved). New IDs are tagged for 800ms so the locked 200ms slide-in animation plays before cleanup. Tab-title flash: captures `document.title` at mount, prefixes `(N)` while `document.hidden`, clears on `focus` / `visibilitychange`, restores on unmount. `prefers-reduced-motion: reduce` swaps `animate-slide-in-bottom` for `animate-fade-in`. ARIA: `role="region"` + `aria-live="polite"` on the list, `aria-label="N escalations awaiting pickup"` on the heading; Pick Up button bumped to `py-2.5` to clear the 44px touch floor.
|
||||
- Verified end-to-end against the running dev stack. `tsc -b` exit 0. Vite HMR'd the new component without errors. Raw SSE handshake against `/api/v1/ai-sessions/escalations/stream` returned 200 with `text/event-stream; charset=utf-8` plus the locked headers (`cache-control: no-cache`, `x-accel-buffering: no`). Subscriber received the `ready` frame on connect; after posting a handoff via the API, the subscriber received the `handoff_created` frame with the full payload — wire format matches the parser exactly. Backend regression: same focused subset still `32 passed in 18.91s`.
|
||||
- Not yet verified (would need a real browser session): the slide-in animation visually plays, the tab title actually updates, the reduced-motion media-query path, AbortController cancellation on unmount, backoff after a real network blip. Wire contract is confirmed; these are visual/timing-dependent and follow from correct parser + state machine.
|
||||
- Smoke-test artifact: a single test handoff (`0f6149db…` on session `50ea20d4…`) is sitting in the engineer's queue from the verification step. Harmless; useful as visual demo data.
|
||||
- Left for next session: the magic-moment handoff-context screen — 4 sections (problem summary / what's been tried / AI assessment / Start here CTA), loads on Pick Up, dissolves into the regular FlowPilot session view. Must render gracefully when `ai_assessment` is `None` (per the 5s assessment timeout from Codex's earlier fix).
|
||||
- Files touched: `frontend/src/api/aiSessions.ts`, `frontend/src/types/ai-session.ts`, `frontend/src/components/flowpilot/EscalationQueue.tsx`, `.ai/CURRENT_TASK.md`, `.ai/HANDOFF.md`, `.ai/SESSION_LOG.md`.
|
||||
|
||||
## 2026-04-27 EDT — Claude Code — Escalation Mode wedge: design through SSE backend (8 commits)
|
||||
|
||||
- One long session that produced the entire planning artifact stack and most of the backend for the Escalation Mode wedge. Output of `/office-hours` (8 founder-signal session, top-tier YC archetype indicators), `/plan-eng-review` (scope reduced from "2-3 weeks greenfield" to "~6-9 days integration + metric + polish" once the existing handoff_manager surface was inventoried), `/plan-design-review` (6/10 → 9/10 with magic-moment screen, hero metric placement, and real-time arrival visual locked), and `/codex review` (12 findings, 6 applied — two-metric framing, notification routing, claim auth gate moved in-scope, unread-state fix, "Start here" CTA reframe, per-channel delivery model; 5 rejected including the full-scope reduction Codex pushed for).
|
||||
- Branched `feat/escalation-metric-endpoint` off `main` @ `c0ed6d9`. Stack at session end: `d51e95c` plan + test-plan artifacts; `52f6d03` `GET /analytics/flowpilot/escalations` endpoint with 9 tests including multi-tenant isolation; `7a5b853` claim-endpoint role gate; `07d0db9` email dispatch on escalate with graceful-degradation regression; `9f0bfd4` `EscalationMetricCard` mounted above the queue list; `a283d0d` mid-flight `.ai/` refresh; `87bd0b7` WIP commit for SSE pub/sub bus + endpoint + 7 bus unit tests + 1 dispatcher integration test + 2 endpoint tests; `ba46fc5` paused-for-Codex-review handoff. Codex picked up from `ba46fc5` and added `bc15952` / `fff8338` / `9bdd995` (test stabilization + assessment latency bound).
|
||||
- Pause was forced by a runaway local test loop: multiple stale `pytest` processes were left inside `resolutionflow_backend` after several aborted runs and contended on the same Postgres test schema. Codex diagnosed and fixed (see entry above).
|
||||
- Frontend: thin slice — added `getEscalationMetrics` to `flowpilotAnalyticsApi`, the `EscalationMetricCard` component (loading / error / zero-data states + avg + median + conversion-rate + the inline two-metric disclaimer), and mounted it above `EscalationQueue`. `tsc -b` clean.
|
||||
- Plan-stage UI decisions locked into the design doc and the codebase: dedicated 4-section magic-moment screen on Pick Up that dissolves into FlowPilot; queue stat-card + dedicated owner analytics page for the hero metric (in two places, not one); 200ms slide-in + tab-title flash on real-time arrival, no sound, respects `prefers-reduced-motion`; unread dot clears on open/claim/dismiss, NOT on hover (Codex correction). Claim role gate moved in-scope per Codex (not deferred to TODO).
|
||||
- Two TODOs added: peer-tech escalation (deferred to v2 once a pilot asks); mobile/responsive design (also v2; pre-PMF wedge demo targets desktop). Claim role gate's TODO entry was struck through in the same session because it shipped in `7a5b853`.
|
||||
- Plan and test-plan artifacts copied into `docs/plans/` under the `YYYY-MM-DD-name-design.md` / `-test-plan.md` convention so they live alongside the existing project plans, not just in `~/.gstack/projects/`.
|
||||
- Left for next session: frontend SSE subscription in `EscalationQueue.tsx` (fetch-based ReadableStream — native EventSource can't send auth headers; match `streamDocumentation` in `frontend/src/api/aiSessions.ts`), then the magic-moment handoff-context screen, then push + draft PR. Default Claude Code model is being switched from Opus 4.7 1M-context to Opus 4.7 (200k) for the next session — the resume docs are sized to be self-sufficient under the smaller window.
|
||||
- Files touched (committed): `docs/plans/2026-04-27-escalation-mode-wedge-design.md`, `docs/plans/2026-04-27-escalation-mode-wedge-test-plan.md`, `backend/app/api/endpoints/flowpilot_analytics.py`, `backend/app/schemas/flowpilot_analytics.py`, `backend/app/api/endpoints/session_handoffs.py`, `backend/app/services/handoff_manager.py`, `backend/app/core/escalation_bus.py` (new), `backend/tests/test_flowpilot_analytics_escalations.py` (new), `backend/tests/test_escalation_bus.py` (new), `backend/tests/test_handoff_manager.py`, `backend/tests/test_session_handoffs_api.py`, `frontend/src/types/flowpilot-analytics.ts`, `frontend/src/api/flowpilotAnalytics.ts`, `frontend/src/components/flowpilot/EscalationMetricCard.tsx` (new), `frontend/src/components/flowpilot/index.ts`, `frontend/src/pages/EscalationQueuePage.tsx`, `.ai/CURRENT_TASK.md`, `.ai/HANDOFF.md`, `.ai/TODO.md`.
|
||||
|
||||
## 2026-04-27 19:50 EDT — Codex — Stabilize Escalation Mode SSE backend tests
|
||||
|
||||
- Diagnosed slow backend tests on `feat/escalation-metric-endpoint`. Multiple stale pytest processes were still alive inside `resolutionflow_backend` and held `resolutionflow_test` transactions open, blocking later per-test schema resets on `DROP SCHEMA public CASCADE`.
|
||||
- Reproduced a deterministic hang in `test_escalations_stream_returns_sse_content_type`: HTTPX `ASGITransport` buffers the full response body before returning, so an infinite SSE response never yielded the initial chunk and kept the auth DB dependency transaction open.
|
||||
- Fixed `stream_escalations` to release auth dependencies before the long-lived stream body with `Depends(..., scope="function")`.
|
||||
- Reworked the SSE handshake test to call `stream_escalations()` directly and consume one generator yield, then close it; kept viewer role-gate coverage through the API client.
|
||||
- Stubbed `_generate_ai_assessment()` in handoff manager/API tests so escalation handoff tests no longer wait on the real AI path.
|
||||
- Normalized account IDs inside `EscalationBus` so string UUIDs and `UUID` objects hit the same subscriber bucket; added a regression test.
|
||||
- Verified focused backend subset: serial `31 passed in 46.95s`; xdist `31 passed in 17.80s`. Confirmed no lingering pytest processes or test DB sessions afterward.
|
||||
- Follow-up in the same session: fixed the product latency risk by adding `ESCALATION_AI_ASSESSMENT_TIMEOUT_SECONDS` (default 5s) around escalation AI assessment generation. If the optional assessment times out, handoff creation continues with no assessment. Added regression coverage; focused xdist subset now `32 passed in 17.77s`.
|
||||
- Left for next session: continue frontend SSE subscription in `EscalationQueue.tsx`, then the magic-moment handoff-context screen.
|
||||
- Files touched: `backend/app/api/endpoints/session_handoffs.py`, `backend/app/core/config.py`, `backend/app/core/escalation_bus.py`, `backend/app/services/handoff_manager.py`, `backend/tests/test_escalation_bus.py`, `backend/tests/test_handoff_manager.py`, `backend/tests/test_session_handoffs_api.py`, `.ai/HANDOFF.md`, `.ai/SESSION_LOG.md`, `.ai/TODO.md`.
|
||||
|
||||
## 2026-04-26 03:50 EDT — Claude Code — Ship AssistantChatPage prefill `currentChatRef` fix; close out PR #150
|
||||
|
||||
- User reported a troubleshooting-session bug: after answering a subset of task-lane questions and clicking *Send N of M Responses*, no AI response appeared. Traced to `AssistantChatPage`: the dashboard prefill effect set `activeChatId` after creating a new chat session but never updated `currentChatRef.current`. The `currentChatRef.current !== sentForChatId` guard in `handleSend` and `handleTaskSubmit` then bailed silently on every later request and discarded the AI's reply. The user message was already pushed to the chat before the await, so the user saw their answers but nothing else.
|
||||
- Fix: one-line addition mirroring `handleNewChat` and `handleResumeNew` — assign `currentChatRef.current = session.session_id` immediately after `setActiveChatId(session.session_id)` in the prefill effect. Branched off `origin/main` as `fix/tasklane-prefill-ref`; PR #153 opened on Gitea.
|
||||
- Authored a Playwright regression test `frontend/e2e/assistant-chat-prefill.spec.ts` that drives the real dashboard prefill flow against the real backend, stubs `/ai-sessions/*/chat` with `page.route` for deterministic turn-1/turn-2 responses, and asserts the second AI message renders. Confirmed the test fails on unfixed code at the exact assertion (`Got it — based on your answer…` never appears) and passes once the fix is restored.
|
||||
- Verified locally inside `mcr.microsoft.com/playwright:v1.58.2-noble` against the running dev stack: new spec passes, adjacent `flowpilot-chat` spec still passes, `tsc -b` clean. `resume.spec` and `history.spec` failures observed are pre-existing real-backend fixture collisions, unrelated to this change.
|
||||
- First CI run on PR #153 failed on infrastructure issues already addressed by PR #150: backend hit `Bind for 0.0.0.0:5432 failed: port is already allocated`, frontend hit `actions/upload-artifact@v4 not supported on GHES`. PR #150 was already merged (commit `87bb20b` on `main`). Rebased `fix/tasklane-prefill-ref` onto new `main` (force-push `1a8cb06` → `1559feb`), resolved a `.ai/TODO.md` conflict by keeping both backlog item sets, kicked off CI on the rebased SHA.
|
||||
- Confirmed `CI / backend (pull_request)` is now in branch protection's required-status-checks list (added during PR #150 close-out). `CI / e2e (pull_request)` left as not-required pending one more clean PR run as the threshold.
|
||||
- Recorded the broader silent-return concern in TODO backlog: the `currentChatRef.current !== sentForChatId` guard is applied across `handleSend`, `handleTaskSubmit`, `selectChat`, `refreshFacts`, `refreshActiveFix`, and `refreshPreview`. PR #153 fixes one symptom but the same pattern can mask other drift. Either log a Sentry breadcrumb on the mismatch path or distinguish "expected stale" (chat switch) from "unexpected stale" (ref never updated) so the latter alerts.
|
||||
- First CI run on the rebased SHA passed backend and frontend but failed e2e: the new prefill regression test couldn't render the task-lane question text. Diagnosed via the job log: `POST /api/v1/ai-sessions` calls `_require_ai_enabled()` and returns 503 when no provider key is set. The e2e CI job had neither `ANTHROPIC_API_KEY` nor `GOOGLE_AI_API_KEY` in env. Locally the dev backend has a real key, hence the local pass. The Playwright `page.route` stub on `/chat` was correct but never had a chance to fire because the upstream session-creation call was 503-ing.
|
||||
- Fix: added a stub `ANTHROPIC_API_KEY: ci-stub-key-not-used-by-tests` to the e2e job env in `.gitea/workflows/ci.yml`. The Playwright stub still intercepts the actual `/chat` call in the browser, so the backend never contacts Anthropic — the gate just needs to clear. Documented the convention in a workflow comment so future AI-touching e2e tests know what to expect. Pushed `11fe32f`; CI went all-green.
|
||||
- Merged PR #153 as `68fcdc6` on `main`. Local feature branch and remote both deleted via Gitea's `delete_branch_after_merge`.
|
||||
- Opened a small follow-up `chore/post-153-handoff` PR to refresh the now-stale `.ai/` files (this entry, plus `CURRENT_TASK.md` rolling forward to "no active task — pick from `TODO.md`" and `HANDOFF.md` updating to the post-merge home position). The `data-testid` audit at the top of `TODO.md` "Up next" or the `currentChatRef` silent-return audit added in this session's backlog are the natural next pickups.
|
||||
- Files touched: `frontend/src/pages/AssistantChatPage.tsx` (the one-line fix + comment), `frontend/e2e/assistant-chat-prefill.spec.ts` (new regression test), `.gitea/workflows/ci.yml` (stub `ANTHROPIC_API_KEY` for e2e), `.ai/TODO.md` (silent-return follow-up entry, plus conflict resolution preserving PR #150's backlog additions), `.ai/CURRENT_TASK.md`, `.ai/HANDOFF.md`, `.ai/SESSION_LOG.md` (this entry).
|
||||
|
||||
## 2026-04-25 16:41 EDT — Codex — Stabilize PR #150 e2e selectors
|
||||
|
||||
- Investigated the remaining PR #150 failure after backend and frontend CI were green. The e2e resume smoke test was not failing because of product behavior; it used `.bg-card` plus text filtering and matched the tree filter `<select>` before the intended session card.
|
||||
- Added stable test IDs to flow session, tree, and share cards, then updated affected e2e tests to target those cards instead of Tailwind class names.
|
||||
- Hardened the CI workflow by making Postgres healthchecks authenticate as `postgres` and baking `VITE_API_URL="${PLAYWRIGHT_API_ORIGIN}"` into the e2e frontend build.
|
||||
- Verified with `git diff --check`, frontend build in Docker, no remaining `.bg-card` e2e selectors, and focused Playwright runs in an Actions-like Ubuntu container: resume spec passed, then history/library/library-start/resume/shares passed (`6 passed`).
|
||||
- Left for next session: push this WIP commit to PR #150, watch CI, merge when all three jobs are green, then enable backend branch protection and consider the e2e gate after a reliable green run.
|
||||
- Files touched: `.gitea/workflows/ci.yml`, `frontend/e2e/history.spec.ts`, `frontend/e2e/library-start.spec.ts`, `frontend/e2e/library.spec.ts`, `frontend/e2e/resume.spec.ts`, `frontend/e2e/shares.spec.ts`, `frontend/src/components/library/TreeGridView.tsx`, `frontend/src/components/library/TreeListView.tsx`, `frontend/src/pages/MySharesPage.tsx`, `frontend/src/pages/SessionHistoryPage.tsx`, `.ai/HANDOFF.md`, `.ai/CURRENT_TASK.md`, `.ai/SESSION_LOG.md`.
|
||||
|
||||
## 2026-04-25 12:00 America/New_York — Claude Code — Mock final AI-provider test, cache CI deps, parallelize backend with pytest-xdist
|
||||
|
||||
- Diagnosed why CI was still red despite Codex's local 1076 passed: a single test (`test_record_decision_persists_and_bumps_state_version`) needed `ANTHROPIC_API_KEY` because the `decision: draft_template` path calls `TemplateExtractionService` → AI provider. Patched `_extract_template_parameters` with an `AsyncMock` so the test no longer depends on AI availability. Verified.
|
||||
- Pushed Codex's WIP commit `49f8856` to PR #150 (had been local-only per handoff protocol).
|
||||
- PR #150 (`fix/ci-workflow-config`) extended with cheap CI wins: `actions/cache@v3` for pip + npm in all three jobs; dropped `--cov-report=term-missing` (the custom display step parses JSON); added `--maxfail=10` so structural breakage exits fast.
|
||||
- PR #151 (`fix/ci-pytest-xdist`) opened, stacked on #150: pytest-xdist with per-worker DB isolation. `conftest.py` reads `PYTEST_XDIST_WORKER`, computes a per-worker DB URL like `…_gw0`, and synchronously CREATEs the DB on first import. The per-test `DROP SCHEMA public CASCADE` then operates on the worker's isolated DB. Verified locally: backend suite went from 22m 27s serial → 4m 28s parallel (8 workers), 1076 passed in both cases. ~5× speedup.
|
||||
- Decided NOT to do per-test transactional rollback (bigger refactor); captured for future TODO consideration.
|
||||
- Left for next session: watch CI on both PRs, merge in order (#150 first, #151 second), then enable `CI / backend (pull_request)` as a required status check on main.
|
||||
- Files touched: `backend/tests/test_session_suggested_fixes_api.py`, `backend/tests/conftest.py`, `backend/requirements-dev.txt`, `.gitea/workflows/ci.yml`, `.ai/HANDOFF.md`, `.ai/CURRENT_TASK.md`, `.ai/TODO.md`.
|
||||
|
||||
## 2026-04-25 06:12 EDT — Codex — Fix backend suite to green
|
||||
|
||||
- Fixed the real backend failures left after the CI-infra cleanup: tenant-scoped seed drift, missing production `account_id` writes, public route mounting for survey/share links, Script Builder library saves, resolution output async loading, AI search schema metadata, disabled-AI fixture leakage, and prompt marker guardrails.
|
||||
- Added backend CI/dev system packages required by WeasyPrint PDF export.
|
||||
- Stabilized the pytest harness for pytest-asyncio/asyncpg teardown ResourceWarnings under `filterwarnings = error`.
|
||||
- Verified `pytest --override-ini="addopts=" -q` inside `resolutionflow_backend`: `1076 passed, 35 deselected in 1347.41s`.
|
||||
- Left for next session: commit/push if needed, check and merge PR #150 when Gitea CI is green, add backend CI as a required branch-protection check, and rerun frontend lint if final DoD requires it.
|
||||
- Files touched: `.gitea/workflows/ci.yml`, `backend/Dockerfile.dev`, `backend/app/api/endpoints/folders.py`, `backend/app/api/endpoints/script_builder.py`, `backend/app/api/endpoints/shares.py`, `backend/app/api/router.py`, `backend/app/models/ai_session.py`, `backend/app/schemas/user.py`, `backend/app/services/assistant_chat_service.py`, `backend/app/services/resolution_output_generator.py`, `backend/app/services/script_builder_service.py`, `backend/pytest.ini`, `backend/tests/conftest.py`, and focused backend tests.
|
||||
|
||||
## 2026-04-25 02:00 America/New_York — Claude Code — Land FlowPilot + PSA, recover CI from 488 errors to ~4
|
||||
|
||||
- Started session by completing pending FlowPilot Phase 9 QA: ran `/qa` against the seeded fixtures, found and fixed four latent layout/state bugs (`ResolutionNotePreview` off-screen, `TemplateMatchPanel` deadlock when TaskLane closed, `EscalateInterceptDialog` clipped above viewport, `seed_test_users.py` `cancel_at_period_end` NOT NULL crash). Added a new fixture seeder `backend/scripts/seed_phase9_qa_fixtures.py` that pre-bakes the four backend states the AI orchestrator needs to emit, so future QA can exercise all 7 conditional Phase 9 components without depending on stochastic AI behavior.
|
||||
- Discovered PR #141 (PSA ticket management) and `feat/flowpilot-migration` had 5 overlapping files but only 2 real conflicts (`CLAUDE.md`, `AssistantChatPage.tsx`). Conflicts were both additive — concatenated rather than chose-a-side.
|
||||
- Merged PSA first (PR #141), then merged FlowPilot (PR #147), each through Gitea API. `tsc -b` clean and visual smoke-test confirmed PSA's Tickets sidebar coexists with Phase 9 ProposalBanner.
|
||||
- Discovered main had been merging through a broken CI gate for several merges. Initially recommended "stop the line, fix CI before shipping." After scoping the actual rot (~50% of tests red, ~600 errors on a clean run), reversed the recommendation: ship the queue first because FlowPilot itself carried significant test-infra repairs that would be duplicated work on a fresh recovery branch.
|
||||
- PR #148: two surgical fixes to main (network_diagrams JSONB `server_default` triple-quote bug, deprecated session-scoped `event_loop` fixture in conftest). +78 passing / -114 errors.
|
||||
- PR #149: frontend lint `20 errors → 0`, `requirements-dev.txt` pytest pin bumped to satisfy `pytest-asyncio==0.24.0`'s `pytest>=8.2`, and a one-line `from app import models as _models` in conftest that registers all ~60 models with `Base.metadata` before `create_all`. The conftest fix collapsed 484 of the remaining 488 backend errors. `1018 passed / 4 errors / 54 failed` after.
|
||||
- Enabled Gitea branch protection on `main`: PR-only merges, `CI / frontend (pull_request)` required, force-push blocked, no review required.
|
||||
- Discovered CI on the merge commit STILL showed red despite local pytest being mostly green. Root cause: workflow only set `DATABASE_URL`, but conftest reads only `DATABASE_TEST_URL` (per `dab740d`'s safety hardening). 638 connection-refused errors on every fixture setup. Plus `actions/upload-artifact@v4` not supported by Gitea Actions. PR #150 fixes both.
|
||||
- Left for next session: merge PR #150 once CI confirms green, add `CI / backend (pull_request)` to required status checks, then root-cause and fix the 54 real backend test failures (one sample seen — `test_user` fixture leaking across calls causing duplicate-email violations).
|
||||
- Files touched (committed): `backend/scripts/seed_test_users.py`, `backend/scripts/seed_phase9_qa_fixtures.py` (new), `backend/app/models/network_diagram.py`, `backend/tests/conftest.py`, `backend/requirements-dev.txt`, `frontend/src/components/pilot/ResolutionNotePreview.tsx`, `frontend/src/components/pilot/EscalateInterceptDialog.tsx`, `frontend/src/components/pilot/ScriptBuilderTab.tsx`, `frontend/src/pages/AssistantChatPage.tsx`, `frontend/src/pages/FlowPilotSessionPage.tsx`, `frontend/src/pages/TicketsPage.tsx`, `frontend/src/hooks/useFlowPilotSession.ts`, `frontend/src/hooks/useMediaQuery.ts`, `frontend/src/components/dashboard/TicketQueue.tsx`, `frontend/src/components/network/nodes/DeviceNode.tsx`, `frontend/src/components/network/nodes/GroupNode.tsx`, `frontend/src/components/routing/AssistantSessionRedirect.tsx` (new), `frontend/src/router.tsx`, `.gitea/workflows/ci.yml`, `.claude/settings.json` (new), `.claude/hooks/check-gstack.sh` (new), `.gitignore`, `CLAUDE.md`, `.gstack/qa-reports/phase9-*/` (QA artifacts).
|
||||
- Net merges to main: PR #141 (PSA), PR #147 (FlowPilot), PR #148 (CI fixes part 1), PR #149 (CI fixes part 2). PR #150 still open at session end.
|
||||
|
||||
## 2026-04-24 — Claude Code — Migrate to dual-agent handoff system
|
||||
|
||||
- Split CLAUDE.md into `.ai/PROJECT_CONTEXT.md` + shared-protocol root files (`CLAUDE.md`, `AGENTS.md`).
|
||||
- Seeded `CURRENT_TASK.md`, `HANDOFF.md`, `TODO.md`, `DECISIONS.md`, `SESSION_LOG.md`, `README.md`.
|
||||
- Deleted legacy `SESSION-HANDOFF.md` (superseded).
|
||||
- Left for next session: first real feature task should replace the seed `CURRENT_TASK.md` and update `HANDOFF.md` with real resume state.
|
||||
- Files touched: `.ai/*.md` (created), `CLAUDE.md` (rewritten), `AGENTS.md` (created), `SESSION-HANDOFF.md` (deleted).
|
||||
- Follow-up (same day): Codex review pass flagged stale SaaS-role claim and incomplete file-listings carried over from the pre-migration CLAUDE.md. Verified against `backend/app/core/permissions.py`, `frontend/src/hooks/usePermissions.ts`, `backend/app/api/deps.py`, `backend/app/api/router.py`, and `backend/app/services/psa/`. Corrected PROJECT_CONTEXT.md role hierarchy (`super_admin > owner > engineer > viewer`, not `team_admin`), added `require_account_owner` / `require_team_admin` to deps list, replaced stale endpoint comment with a summary pointing at `api/router.py`, added `exceptions.py` + `ticket_context.py` to the PSA file list. Also replaced seed-example content in `CURRENT_TASK.md` and `TODO.md` with clearer empty-state sentinels.
|
||||
- Branch cleanup (same day): committed pending test-isolation work as `b14a16a chore(tests): gate RLS tests behind RUN_RLS_TESTS flag`, new Phase 9 review doc as `b3506b5 docs(pilot): phase 9 review issues`, and `.remember/` gitignore entry as `b3be1e0 chore: ignore .remember/ skill runtime state`. Deleted `docs/landing-handoff/` (prepared for external design work, not meant to live in the repo). Working tree clean; 3 cleanup commits unpushed.
|
||||
23
.ai/TODO.md
Normal file
23
.ai/TODO.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# TODO.md
|
||||
|
||||
> Backlog of work NOT currently active. Read only when `CURRENT_TASK.md` status is `complete`.
|
||||
> Format: `- [ ] short description — optional link to issue/PR`
|
||||
|
||||
## Up next
|
||||
|
||||
- [ ] **Parallelize backend pytest with pytest-xdist.** ✅ landing as PR #151. Verified locally: backend suite 22 min → 4m 28s with `-n auto` on the 8-core homelab runner. Per-worker DB isolation via `PYTEST_XDIST_WORKER` in conftest.py.
|
||||
|
||||
## Backlog
|
||||
|
||||
- [ ] **Frontend lint warnings cleanup.** 23 `react-hooks/exhaustive-deps` warnings remain after PR #149 (mostly missing-deps in useEffect). Either fix them or audit them for known-safe ones and add eslint-disable comments. Not blocking CI today.
|
||||
- [ ] **Audit `filterwarnings` ignores added in `wip(handoff): restore backend suite to green`.** Codex added narrow `ResourceWarning` filters for unclosed socket/transport/event-loop noise from pytest-asyncio teardown. Worth periodically reviewing whether those are still needed (e.g. when bumping pytest-asyncio) — if a real warning appears in those forms it would be silenced.
|
||||
- [ ] **Add `data-testid` attributes to e2e-critical interactive elements.** PR #152 fixed five Playwright tests by chasing UI-text changes (`Sessions` → `Session History`, `Account Settings` → `Account Management`, `/assistant` → `/pilot`, "Flow Sessions" tab, Resume button on session cards). Each was a one-line selector update, but every UI churn re-breaks them. Adding stable `data-testid` attributes on the targeted elements (page heading wrappers, tab nav, primary action buttons) and switching tests to `getByTestId` would make these immune to copy/route renames. Scope it small — start with `SessionHistoryPage` heading, the AI/Flow Sessions tab buttons, the per-session `Resume` button, and the command-palette FlowPilot option.
|
||||
- [ ] **Per-test transactional rollback in `test_db` fixture.** Bigger engineering than xdist (which we already shipped). Instead of `DROP SCHEMA public CASCADE` per test, wrap each test in a savepoint and rollback at teardown. ~30-40% additional speedup on top of xdist for test-DB-heavy tests. Real refactor; only worth it if the suite gets significantly larger or runs more frequently.
|
||||
- [ ] **Consider `pytest-testmon` for PR-time test selection.** Tracks which tests touched which source files and only re-runs affected ones. Best for small PRs touching ~few files. Adds cache-invalidation complexity; only worth it if the suite stays painfully long even after xdist.
|
||||
- [ ] **AssistantChatPage `currentChatRef` guard is a silent return** — `handleSend`, `handleTaskSubmit`, `selectChat`, `refreshFacts`, `refreshActiveFix`, and `refreshPreview` all bail with `if (currentChatRef.current !== sentForChatId) return` when stale. This is by design for chat switching, but it also silently masked the prefill-ref bug fixed in PR #153 — the user just saw "no AI response" with no log, no toast, no Sentry event. Either (a) log a `console.warn`/Sentry breadcrumb on the mismatch path so future drift is visible, or (b) split "expected stale" (chat switch) from "unexpected stale" (ref never updated) so only the latter alerts. Pair with an audit of every `currentChatRef.current = ...` assignment vs every `setActiveChatId(...)` call to make sure they're paired everywhere.
|
||||
|
||||
- [ ] **Allow peer-tech to escalate a colleague's session.** Today `POST /ai-sessions/{session_id}/handoff` in [endpoints/session_handoffs.py:48](backend/app/api/endpoints/session_handoffs.py#L48) filters by `AISession.user_id == current_user.id`, so only the session owner can escalate. Real MSP shops have peer hand-offs: Junior A is on lunch, Junior B sees the session is stuck and should be able to escalate it. Auth tweak: switch from session-owner check to `require_engineer_or_admin` + same-account scope. Add a `handed_off_by` audit column (already exists on `SessionHandoff`) so the original-owner-vs-actual-escalator distinction is preserved. Surfaced from /plan-eng-review on the Escalation-Mode wedge plan; v1 wedge demo doesn't need this (solo-founder pilot), but capture for v2 once 3+ pilots are live and a peer-claim need surfaces.
|
||||
|
||||
- [ ] **Mobile/responsive design for EscalationQueue + handoff-context screen.** Pre-PMF wedge demo targets desktop only — MSP techs work on laptops/desktops in shop environments. Once 3+ paying customers exist and a tech requests mobile (likely on-call use case), spec the responsive behavior: stacked card layout below `sm:` breakpoint, full-bleed handoff-context overlay on mobile, swipe-to-claim gesture instead of Pick Up button. Surfaced from /plan-design-review on the Escalation-Mode wedge plan.
|
||||
|
||||
- [ ] **(MOVED IN-SCOPE for Escalation Mode v1, 2026-04-27)** ~~Add role gate to handoff claim endpoint.~~ Codex review correctly flagged this as wedge-relevant (the race-condition story depends on auth gating). Now part of the Escalation Mode v1 build, not a deferred TODO.
|
||||
20
.claude/hooks/check-gstack.sh
Executable file
20
.claude/hooks/check-gstack.sh
Executable file
@@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
# Block skill usage when gstack is not installed globally.
|
||||
|
||||
if [ ! -d "$HOME/.claude/skills/gstack/bin" ]; then
|
||||
cat >&2 <<'MSG'
|
||||
BLOCKED: gstack is not installed globally.
|
||||
|
||||
gstack is required for AI-assisted work in this repo.
|
||||
|
||||
Install it:
|
||||
git clone --depth 1 https://github.com/garrytan/gstack.git ~/.claude/skills/gstack
|
||||
cd ~/.claude/skills/gstack && ./setup --team
|
||||
|
||||
Then restart your AI coding tool.
|
||||
MSG
|
||||
echo '{"permissionDecision":"deny","message":"gstack is required but not installed. See stderr for install instructions."}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo '{}'
|
||||
15
.claude/settings.json
Normal file
15
.claude/settings.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"hooks": {
|
||||
"PreToolUse": [
|
||||
{
|
||||
"matcher": "Skill",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "\"$CLAUDE_PROJECT_DIR/.claude/hooks/check-gstack.sh\""
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
218
.gitea/workflows/ci.yml
Normal file
218
.gitea/workflows/ci.yml
Normal file
@@ -0,0 +1,218 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
backend:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: pgvector/pgvector:pg16
|
||||
env:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: resolutionflow_test
|
||||
# No host port mapping. Tests connect to `postgres:5432` (the service
|
||||
# container's docker-network DNS name), not `localhost:5432`. With
|
||||
# multiple Gitea runners on the same homelab box, host-port mapping
|
||||
# would race — two backend/e2e jobs both binding 0.0.0.0:5432 → the
|
||||
# second fails with "port is already allocated".
|
||||
options: >-
|
||||
--health-cmd "pg_isready -U postgres"
|
||||
--health-interval 10s
|
||||
--health-timeout 5s
|
||||
--health-retries 5
|
||||
|
||||
env:
|
||||
DATABASE_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/resolutionflow_test
|
||||
DATABASE_URL_SYNC: postgresql://postgres:postgres@postgres:5432/resolutionflow_test
|
||||
# conftest.py reads DATABASE_TEST_URL only (DATABASE_URL is intentionally
|
||||
# not consulted after the dab740d test-isolation hardening). The CI test
|
||||
# DB is the same postgres service, so point DATABASE_TEST_URL at it
|
||||
# explicitly — without this, conftest falls back to localhost:5432 and
|
||||
# all tests fail at fixture setup with "connection refused".
|
||||
DATABASE_TEST_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/resolutionflow_test
|
||||
SECRET_KEY: ci-test-secret-key-not-for-production
|
||||
DEBUG: "true"
|
||||
APP_NAME: ResolutionFlow
|
||||
TEST_DB_NAME: resolutionflow_test
|
||||
DB_APP_ROLE_PASSWORD: app_secret_ci
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Cache pip
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.cache/pip
|
||||
key: pip-${{ runner.os }}-${{ hashFiles('backend/requirements.txt', 'backend/requirements-dev.txt') }}
|
||||
restore-keys: |
|
||||
pip-${{ runner.os }}-
|
||||
|
||||
- name: Install system dependencies
|
||||
run: |
|
||||
apt-get update
|
||||
apt-get install -y libpango1.0-dev libcairo2-dev libgdk-pixbuf-2.0-dev libffi-dev libjpeg-dev zlib1g-dev
|
||||
|
||||
- name: Install dependencies
|
||||
run: pip install --break-system-packages -r backend/requirements.txt -r backend/requirements-dev.txt
|
||||
|
||||
- name: Run Alembic migrations
|
||||
run: cd backend && alembic upgrade head
|
||||
|
||||
- name: Check tenant filter enforcement
|
||||
run: cd backend && python scripts/check_tenant_filters.py
|
||||
|
||||
- name: Run tests with coverage
|
||||
# `-n auto` parallelizes across all runner cores via pytest-xdist.
|
||||
# conftest.py creates a per-worker DB (resolutionflow_test_gw0,
|
||||
# resolutionflow_test_gw1, …) so the per-test DROP SCHEMA doesn't
|
||||
# race across workers. Master/serial runs keep the base DB.
|
||||
# term-missing dropped — the custom "Display coverage summary" step
|
||||
# below parses coverage.json and prints the same info more concisely.
|
||||
# --maxfail=10 short-circuits on structural breakage so we don't burn
|
||||
# 25 minutes when a fixture explodes.
|
||||
run: cd backend && python -m pytest --override-ini="addopts=" -n auto --maxfail=10 --cov=app --cov-report=json:coverage.json --cov-fail-under=50
|
||||
|
||||
- name: Display coverage summary
|
||||
if: always()
|
||||
run: |
|
||||
cd backend
|
||||
python -c "
|
||||
import json
|
||||
with open('coverage.json') as f:
|
||||
data = json.load(f)
|
||||
total = data['totals']['percent_covered_display']
|
||||
print(f'Total coverage: {total}%')
|
||||
print()
|
||||
print('Module coverage:')
|
||||
for fname, fdata in sorted(data['files'].items()):
|
||||
pct = fdata['summary']['percent_covered_display']
|
||||
if float(pct) < 80:
|
||||
print(f' WARNING {fname}: {pct}%')
|
||||
else:
|
||||
print(f' OK {fname}: {pct}%')
|
||||
"
|
||||
|
||||
frontend:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Cache npm
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.npm
|
||||
key: npm-${{ runner.os }}-${{ hashFiles('frontend/package-lock.json') }}
|
||||
restore-keys: |
|
||||
npm-${{ runner.os }}-
|
||||
|
||||
- name: Install dependencies
|
||||
run: cd frontend && npm ci
|
||||
|
||||
- name: Lint
|
||||
run: cd frontend && npm run lint
|
||||
|
||||
- name: Test with coverage
|
||||
run: cd frontend && npm run test:coverage
|
||||
|
||||
- name: Build
|
||||
run: cd frontend && NODE_OPTIONS="--max-old-space-size=4096" npm run build
|
||||
|
||||
# Build artifact intentionally NOT uploaded. The e2e job below builds
|
||||
# its own frontend rather than downloading one from this job, so there
|
||||
# is no need for the cross-job artifact handoff (which previously broke
|
||||
# on actions/upload-artifact@v4 GHES support and forced a v3 pin).
|
||||
# Decoupling also lets e2e start immediately rather than waiting for
|
||||
# this job to finish — important on a multi-runner setup.
|
||||
|
||||
e2e:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: pgvector/pgvector:pg16
|
||||
env:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: resolutionflow_test
|
||||
# No host port mapping. Tests connect to `postgres:5432` (the service
|
||||
# container's docker-network DNS name), not `localhost:5432`. With
|
||||
# multiple Gitea runners on the same homelab box, host-port mapping
|
||||
# would race — two backend/e2e jobs both binding 0.0.0.0:5432 → the
|
||||
# second fails with "port is already allocated".
|
||||
options: >-
|
||||
--health-cmd "pg_isready -U postgres"
|
||||
--health-interval 10s
|
||||
--health-timeout 5s
|
||||
--health-retries 5
|
||||
|
||||
env:
|
||||
PLAYWRIGHT_DATABASE_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/resolutionflow_test
|
||||
PLAYWRIGHT_DATABASE_URL_SYNC: postgresql://postgres:postgres@postgres:5432/resolutionflow_test
|
||||
PLAYWRIGHT_API_ORIGIN: http://127.0.0.1:8000
|
||||
PLAYWRIGHT_BASE_URL: http://127.0.0.1:4173
|
||||
PLAYWRIGHT_SECRET_KEY: ci-playwright-secret-key
|
||||
PLAYWRIGHT_TEST_EMAIL: teamadmin@resolutionflow.example.com
|
||||
PLAYWRIGHT_TEST_PASSWORD: TestPass123!
|
||||
# AI-touching endpoints (POST /ai-sessions, /chat, /respond, etc.) are
|
||||
# gated by `_require_ai_enabled()`, which returns 503 when no provider
|
||||
# key is set. Tests that exercise those flows stub the AI calls in the
|
||||
# browser via `page.route`, so the backend never actually contacts
|
||||
# Anthropic — but the gate still has to pass. A stub value is enough.
|
||||
ANTHROPIC_API_KEY: ci-stub-key-not-used-by-tests
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Cache pip
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.cache/pip
|
||||
key: pip-${{ runner.os }}-${{ hashFiles('backend/requirements.txt', 'backend/requirements-dev.txt') }}
|
||||
restore-keys: |
|
||||
pip-${{ runner.os }}-
|
||||
|
||||
- name: Cache npm
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.npm
|
||||
key: npm-${{ runner.os }}-${{ hashFiles('frontend/package-lock.json') }}
|
||||
restore-keys: |
|
||||
npm-${{ runner.os }}-
|
||||
|
||||
- name: Install backend dependencies
|
||||
run: pip install --break-system-packages -r backend/requirements.txt -r backend/requirements-dev.txt
|
||||
|
||||
- name: Install frontend dependencies
|
||||
run: cd frontend && npm ci
|
||||
|
||||
- name: Build frontend
|
||||
# Building inline (instead of downloading an artifact from the
|
||||
# frontend job) drops the cross-job dependency, so e2e can start
|
||||
# immediately on a free runner. Adds ~1-2 min of build time, but
|
||||
# eliminates the artifact-upload mechanism entirely (no more
|
||||
# v3/v4 GHES headaches) and saves ~5 min of waiting.
|
||||
run: cd frontend && NODE_OPTIONS="--max-old-space-size=4096" VITE_API_URL="${PLAYWRIGHT_API_ORIGIN}" npm run build
|
||||
|
||||
- name: Install Playwright browser
|
||||
run: cd frontend && npx playwright install --with-deps chromium
|
||||
|
||||
- name: Run Playwright smoke tests
|
||||
run: cd frontend && npm run test:e2e
|
||||
|
||||
- name: Upload Playwright report
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: playwright-report
|
||||
path: |
|
||||
frontend/playwright-report
|
||||
frontend/test-results
|
||||
if-no-files-found: ignore
|
||||
19
.gitea/workflows/mirror-to-github.yml
Normal file
19
.gitea/workflows/mirror-to-github.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
name: Mirror to GitHub
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- '**'
|
||||
|
||||
jobs:
|
||||
mirror:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Push to GitHub
|
||||
run: |
|
||||
cd /tmp
|
||||
git clone --mirror https://gitea.resolutionflow.com/chihlasm/resolutionflow.git repo
|
||||
cd repo
|
||||
git remote add github https://x-access-token:${{ secrets.GH_MIRROR_TOKEN }}@github.com/${{ secrets.GH_MIRROR_REPO }}
|
||||
git push github --all --force
|
||||
git push github --tags --force
|
||||
43
.gitea/workflows/runner-probe.yml
Normal file
43
.gitea/workflows/runner-probe.yml
Normal file
@@ -0,0 +1,43 @@
|
||||
name: Runner Probe
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
probe:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Runner labels and OS
|
||||
run: |
|
||||
echo "=== OS ==="
|
||||
uname -a
|
||||
cat /etc/os-release 2>/dev/null || true
|
||||
|
||||
- name: Python versions
|
||||
run: |
|
||||
echo "=== Python ==="
|
||||
which python3 && python3 --version || echo "python3 not found"
|
||||
which python && python --version || echo "python not found"
|
||||
ls /usr/bin/python* 2>/dev/null || true
|
||||
|
||||
- name: Node versions
|
||||
run: |
|
||||
echo "=== Node ==="
|
||||
which node && node --version || echo "node not found"
|
||||
which npm && npm --version || echo "npm not found"
|
||||
ls /usr/bin/node* 2>/dev/null || true
|
||||
ls ~/.nvm/versions/node/ 2>/dev/null || echo "no nvm versions"
|
||||
|
||||
- name: Docker
|
||||
run: |
|
||||
echo "=== Docker ==="
|
||||
which docker && docker --version || echo "docker not found"
|
||||
docker info 2>/dev/null | grep -E "Server Version|Operating System" || true
|
||||
|
||||
- name: User and home
|
||||
run: |
|
||||
echo "=== User ==="
|
||||
whoami
|
||||
echo "HOME=$HOME"
|
||||
echo "PATH=$PATH"
|
||||
9
.gitignore
vendored
9
.gitignore
vendored
@@ -207,7 +207,11 @@ marimo/_lsp/
|
||||
__marimo__/
|
||||
|
||||
# Claude Code (local config, agents, settings)
|
||||
.claude/
|
||||
.claude/*
|
||||
!.claude/settings.json
|
||||
!.claude/hooks/
|
||||
.claude/hooks/*
|
||||
!.claude/hooks/check-gstack.sh
|
||||
.agents/
|
||||
|
||||
# Database dumps
|
||||
@@ -238,3 +242,6 @@ package-lock.json
|
||||
# graphify knowledge graph outputs
|
||||
graphify-out/
|
||||
.graphify_python
|
||||
|
||||
# remember skill runtime state (hook logs, PIDs)
|
||||
.remember/
|
||||
|
||||
61
AGENTS.md
Normal file
61
AGENTS.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# AGENTS.md — ResolutionFlow
|
||||
|
||||
You are OpenAI Codex, the resume agent for ResolutionFlow. Claude Code is the primary coding agent; you step in when Claude hits session or weekly limits.
|
||||
|
||||
The first thing to do every session: read [`.ai/PROJECT_CONTEXT.md`](.ai/PROJECT_CONTEXT.md), [`.ai/CURRENT_TASK.md`](.ai/CURRENT_TASK.md), and [`.ai/HANDOFF.md`](.ai/HANDOFF.md). The ritual is spelled out below.
|
||||
|
||||
> The protocol section below is byte-identical to the shared block in CLAUDE.md. If you edit one, edit the other.
|
||||
|
||||
## Shared protocol
|
||||
|
||||
### Startup ritual (every session)
|
||||
|
||||
1. Read `.ai/PROJECT_CONTEXT.md` — architectural truth for this repo.
|
||||
2. Read `.ai/CURRENT_TASK.md` — what we're actively working on.
|
||||
3. Read `.ai/HANDOFF.md` — exact resume point.
|
||||
4. Skim `.ai/DECISIONS.md` for recent entries relevant to the current task.
|
||||
5. Run `git log --oneline -15` and `git status`.
|
||||
6. Before taking action, state back in two sentences: the current goal and your proposed next action.
|
||||
|
||||
### Handoff ritual (session end — limit hit, task complete, or user stop)
|
||||
|
||||
1. Update `.ai/HANDOFF.md` to reflect new state. Keep it under ~2K tokens.
|
||||
2. If `CURRENT_TASK.md` status changed, update it.
|
||||
3. If you made an architectural decision, append to `.ai/DECISIONS.md`.
|
||||
4. Append a session entry to `.ai/SESSION_LOG.md`.
|
||||
5. If working tree is dirty, commit WIP with `wip(handoff): <one-line summary>`. Do not push unless explicitly asked.
|
||||
|
||||
### Writing rules for .ai/ files
|
||||
|
||||
- Use model-neutral voice in `HANDOFF.md`, `SESSION_LOG.md`, `DECISIONS.md` ("previous session did X", NOT "Claude did X" or "Codex did X"). Exception: `SESSION_LOG.md` entries include an `<agent>` field in the header.
|
||||
- Do not duplicate content between files. `CURRENT_TASK.md` holds the goal, `HANDOFF.md` holds the resume point, `TODO.md` holds the backlog. If unsure where something goes, check `.ai/README.md`.
|
||||
- Don't invent facts about the repo. If you're uncertain, write `TODO: confirm` and flag it.
|
||||
|
||||
### Project principle
|
||||
|
||||
Prefer correct architecture over minimal diff. Flag "simpler approach" tradeoffs for review before taking them.
|
||||
|
||||
## Codex-specific notes
|
||||
|
||||
### Tooling you do NOT have
|
||||
|
||||
- **No GitNexus tools.** Use `grep -r`, `rg`, `git grep`, or `find` for code search. For blast-radius reasoning, grep call sites manually and read the files.
|
||||
- **No gstack slash commands** (`/review`, `/ship`, `/qa`, `/browse`, `/investigate`, `/design-review`, `/plan-*`). Run the equivalent work directly: `pytest` for tests, `npm run build` for frontend validation, manual PR description for review flow.
|
||||
- **No `/codex` second-opinion command.** You are Codex.
|
||||
|
||||
### Git trailer
|
||||
|
||||
Every commit: `Co-Authored-By: Codex <noreply@openai.com>`
|
||||
|
||||
### Model selection
|
||||
|
||||
Handled on OpenAI's side. Do not attempt to set Anthropic model aliases for your own runtime. (The repo's application code still uses Anthropic aliases like `claude-sonnet-4-6` via `settings.get_model_for_action()` — that's runtime config for the product, not your agent.)
|
||||
|
||||
### Reviewing Claude's work
|
||||
|
||||
When you resume from a Claude session, assume some decisions may have been informed by GitNexus queries or gstack commands whose output isn't in the handoff. If a decision looks unverified from the `.ai/` files alone, either:
|
||||
|
||||
- re-verify with `grep`/`rg`/file reads, or
|
||||
- flag it in `HANDOFF.md` under "Open questions" so Michael or Claude can confirm on the next handoff.
|
||||
|
||||
Do not assume tooling output that isn't written down.
|
||||
26
CHANGELOG.md
26
CHANGELOG.md
@@ -2,6 +2,30 @@
|
||||
|
||||
All notable changes to ResolutionFlow are documented here.
|
||||
|
||||
## [0.1.0.0] - 2026-04-16
|
||||
|
||||
### Added
|
||||
- **PSA Ticket Management** — dedicated `/tickets` page with URL-param filter state (board, status, priority, company, assignment, closed), paginated ticket list, and slide-in detail panel
|
||||
- **TicketDetailPanel** — full ticket view with notes feed, configurations, related tickets, and resource manager; optimistic status updates via dropdown
|
||||
- **NewTicketModal** — two-tab ticket creation: "Quick Create (AI)" parses natural language into a pre-filled form via Claude, "Full Form" for manual entry; validates required fields before submitting to CW
|
||||
- **AiTicketParseForm** — natural language → structured ticket data using Claude; resolves board and assignee automatically, flags fields needing manual selection
|
||||
- **TicketResourceManager** — add/remove CW members as ticket resources with member search autocomplete
|
||||
- **Spin-off ticket creation from ResolutionAssist** — AI can detect when a new ticket should be created mid-session and surface the NewTicketModal pre-filled with session context
|
||||
- **TicketQueue improvements** — dashboard widget now detects member mapping, caps at 5 items, shows "View All" link to `/tickets`
|
||||
- **Board statuses endpoint** — `GET /integrations/boards/{board_id}/statuses` for direct status lookup without a ticket context
|
||||
- **Paginated ticket search** — `search_tickets` returns `{items, total, page, page_size}`; parallel CW count fetch for accurate totals
|
||||
- **Ticket service layer** — `ticket_service.py` wraps all PSA mutations (create, update status, list/add/remove resources)
|
||||
- **Priority lookup endpoint** — `GET /integrations/tickets/priorities` for form dropdowns
|
||||
- **PSA error surfacing** — `/tickets` page shows inline error banner with specific guidance when CW returns a permissions error (replaces silent empty state)
|
||||
|
||||
### Fixed
|
||||
- CW query injection: sanitize search `query` string to strip single quotes before interpolation into CW conditions
|
||||
- `company_id` filter now correctly applied to CW ticket search conditions (was silently ignored)
|
||||
- `linkedTicket` fetch in ResolutionAssist guarded with `currentChatRef` to prevent race condition on session switch
|
||||
- Members endpoint auth gate no longer rejects engineers without a PSA mapping
|
||||
- Board fallback: ticket list derives available boards from ticket data when the boards API returns empty (permissions)
|
||||
- Assignment search and "Load More" removed from resource manager in favor of direct member list
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
@@ -11,6 +35,7 @@ All notable changes to ResolutionFlow are documented here.
|
||||
- **Tenant Isolation Phase 0** — multi-tenant data isolation (#132) with app-layer filtering helpers (`tenant_filter()`, `get_tenant_context`), cross-tenant access audit (analytics, categories, AI sessions, trees), UUID endpoint isolation with 404 responses for unauthorized access, ownership checks on all sensitive operations, and CI grep gate for missing tenant filters
|
||||
- **Tenant Isolation Phase 2** — PostgreSQL Row Level Security (RLS) on 11 session-related tables (ai_sessions, session_steps, session_tags, etc.), account_id NOT NULL enforcement on all write paths, Alembic migrations with dual-env support (Railway native vars + explicit DATABASE_URL_SYNC), RLS test coverage with cross-account isolation verification, migration CI/CD integration
|
||||
- **Tenant Isolation Phase 3** — RLS on audit_logs and tree_shares tables, cross-tenant session access for public shares (via get_admin_db), complete account_id propagation across PSA integration write paths, final RLS policy enforcement
|
||||
- **Tenant Isolation Phase 4** (#136) — RLS enforcement on all 31 remaining tables (users, trees, teams, integrations, scripts, categories, templates, surveys, etc.), BYPASSRLS session pattern for auth deps and background jobs, admin session factory for startup routines (service accounts, seed data), global table exclusions (platform_steps, template_trees, script_categories, accounts), RLS tests with complete cross-tenant isolation verification, proper tree_shares ownership checks using tree owner's account_id
|
||||
- **Script Library default view** — "All Scripts" tab now displays all accessible scripts (team + library)
|
||||
- **Session documentation overhaul** — reformatted PSA resolution/escalation notes with cleaner headers, inline engineer responses, decimal hour display (0.25 hrs), follow-up recommendations, and improved "What We Know" section from evidence items
|
||||
- **Client communication improvements** — new `request_info` audience type for client-facing information requests, improved status update and email draft prompts with per-context guidance
|
||||
@@ -33,6 +58,7 @@ All notable changes to ResolutionFlow are documented here.
|
||||
- **Category tree counts** — cross-tenant row count leakage via tree_count field in GET `/categories/{id}`. Now scoped to requesting account.
|
||||
- **PSA retry ownership check** — retry-psa-push had no ownership validation (CRITICAL). Now validates user ownership before allowing retry.
|
||||
- **Task Lane save operation** — invalid task_lane_item UUIDs returned 403 revealing existence. Now returns 404 and uses query-level filtering.
|
||||
- **Phase 4 RLS enforcement** — fixed auth deps, user-mutation endpoints, background jobs, and lifespan routines to use BYPASSRLS sessions for reading/writing tenant-isolated tables; fixed seed scripts to use ADMIN_DATABASE_URL; bootstrap service account now initializes correctly with proper BYPASSRLS context
|
||||
- Dark text rendering on blue accent step-number badges across all flow types
|
||||
- Script Library tab ownership filter now preserved across category and search changes
|
||||
- Race conditions in script builder session creation and slug generation
|
||||
|
||||
650
CLAUDE.md
650
CLAUDE.md
@@ -1,628 +1,74 @@
|
||||
# CLAUDE.md - Patherly / ResolutionFlow Project Context
|
||||
# CLAUDE.md — ResolutionFlow
|
||||
|
||||
> **Last Updated:** April 6, 2026
|
||||
You are Claude Code, the primary coding agent for ResolutionFlow. OpenAI Codex is the resume agent when you hit session or weekly limits.
|
||||
|
||||
---
|
||||
The first thing to do every session: read [`.ai/PROJECT_CONTEXT.md`](.ai/PROJECT_CONTEXT.md), [`.ai/CURRENT_TASK.md`](.ai/CURRENT_TASK.md), and [`.ai/HANDOFF.md`](.ai/HANDOFF.md). The ritual is spelled out below.
|
||||
|
||||
## Project Overview
|
||||
> The protocol section below is byte-identical to the shared block in AGENTS.md. If you edit one, edit the other.
|
||||
|
||||
**Patherly** (user-facing brand: **ResolutionFlow**) is a **SaaS product for MSP professionals**. It provides troubleshooting decision trees that guide engineers through proven troubleshooting paths, capture decisions and notes, and generate professional ticket documentation.
|
||||
## Shared protocol
|
||||
|
||||
**Target Market:** MSP companies — IT service providers managing infrastructure and support for multiple clients.
|
||||
### Startup ritual (every session)
|
||||
|
||||
**SaaS Context:** Multi-tenant design — teams represent MSP companies, trees shared within teams, tiered access (super_admin, team_admin, engineer, viewer).
|
||||
1. Read `.ai/PROJECT_CONTEXT.md` — architectural truth for this repo.
|
||||
2. Read `.ai/CURRENT_TASK.md` — what we're actively working on.
|
||||
3. Read `.ai/HANDOFF.md` — exact resume point.
|
||||
4. Skim `.ai/DECISIONS.md` for recent entries relevant to the current task.
|
||||
5. Run `git log --oneline -15` and `git status`.
|
||||
6. Before taking action, state back in two sentences: the current goal and your proposed next action.
|
||||
|
||||
### Branding
|
||||
### Handoff ritual (session end — limit hit, task complete, or user stop)
|
||||
|
||||
| Context | Name Used |
|
||||
|---------|-----------|
|
||||
| Repository / directory / database | `patherly` (internal name) |
|
||||
| Docker containers | `resolutionflow_postgres`, `resolutionflow_frontend`, `resolutionflow_backend` |
|
||||
| Backend, frontend UI, production URLs | **ResolutionFlow** |
|
||||
1. Update `.ai/HANDOFF.md` to reflect new state. Keep it under ~2K tokens.
|
||||
2. If `CURRENT_TASK.md` status changed, update it.
|
||||
3. If you made an architectural decision, append to `.ai/DECISIONS.md`.
|
||||
4. Append a session entry to `.ai/SESSION_LOG.md`.
|
||||
5. If working tree is dirty, commit WIP with `wip(handoff): <one-line summary>`. Do not push unless explicitly asked.
|
||||
|
||||
- **Design system:** [DESIGN-SYSTEM.md](DESIGN-SYSTEM.md) — THE source of truth for all design decisions
|
||||
- **Design aesthetic:** Flat, high-contrast dark theme (Sentry/PostHog-inspired). No glass morphism, no gradients on surfaces, no ambient effects. Light mode planned.
|
||||
- **Accent color:** Electric blue (#60a5fa dark / #2563eb light). Used sparingly — ≤5% of the UI. Warning is amber (#fbbf24), info is cyan (#67e8f9).
|
||||
- **Fonts:** IBM Plex Sans (`font-sans`, body), Bricolage Grotesque (`font-heading`, headings), JetBrains Mono (`font-mono`, code) — loaded via Google Fonts
|
||||
- **Logo:** 30px gradient square (ember orange) + "ResolutionFlow" in Bricolage Grotesque 700
|
||||
- **Layout:** Icon rail sidebar (72px default) with hover flyout panels. Pinnable to full 260px sidebar. See [DESIGN-SYSTEM.md](DESIGN-SYSTEM.md)
|
||||
- **Brand assets:** `brand-assets/` (source SVGs), `frontend/src/assets/brand/` (app assets), `frontend/public/icons/` (favicon)
|
||||
- **Terminology:** User-facing label is "Flows" (not "Trees"). Procedural flows are called "Projects" in the UI. Step Library is called "Solutions Library" in the UI. Maintenance flows are hidden from UI for pilot (backend still supports them). `tree_type` column values unchanged in DB.
|
||||
- **Reference mockups:** `docs/mockups/` (HTML files, open in browser)
|
||||
### Writing rules for .ai/ files
|
||||
|
||||
**Component styling:** See Design System section below and [DESIGN-SYSTEM.md](DESIGN-SYSTEM.md). All colors via CSS variables. Use "Flows" not "Trees" in user-facing text; use "Projects" not "Procedures" for procedural flows.
|
||||
- Use model-neutral voice in `HANDOFF.md`, `SESSION_LOG.md`, `DECISIONS.md` ("previous session did X", NOT "Claude did X" or "Codex did X"). Exception: `SESSION_LOG.md` entries include an `<agent>` field in the header.
|
||||
- Do not duplicate content between files. `CURRENT_TASK.md` holds the goal, `HANDOFF.md` holds the resume point, `TODO.md` holds the backlog. If unsure where something goes, check `.ai/README.md`.
|
||||
- Don't invent facts about the repo. If you're uncertain, write `TODO: confirm` and flag it.
|
||||
|
||||
## Implementation Principles
|
||||
### Project principle
|
||||
|
||||
- Prefer correct architecture over minimal diff
|
||||
- If two approaches exist, implement the one that scales, not the one that's faster to write
|
||||
- Flag any "simpler approach" tradeoffs for product owner review before proceeding
|
||||
Prefer correct architecture over minimal diff. Flag "simpler approach" tradeoffs for review before taking them.
|
||||
|
||||
---
|
||||
## Claude-specific tooling
|
||||
|
||||
## Current State
|
||||
### GitNexus code intelligence
|
||||
|
||||
- **Phase:** Go-to-Market Validation (Pre-PMF)
|
||||
- **Backend:** Complete (55+ API endpoints, 100+ integration tests)
|
||||
- **Frontend:** Core features complete, Tree Editor functional
|
||||
- **Database:** PostgreSQL with Docker, 101 migrations
|
||||
- **Detailed status:** [CURRENT-STATE.md](CURRENT-STATE.md)
|
||||
Indexed as `resolutionflow`. Earns its cost on cross-cutting work only.
|
||||
|
||||
### What's In Progress
|
||||
| Tool | When |
|
||||
|---|---|
|
||||
| `gitnexus_query({query})` | Find code by concept when you don't know where to look |
|
||||
| `gitnexus_context({name})` | Callers/callees of a symbol before touching it |
|
||||
| `gitnexus_impact({target, direction})` | Blast radius before editing shared symbols |
|
||||
| `gitnexus_rename({symbol_name, new_name, dry_run: true})` | Safe multi-file rename |
|
||||
|
||||
- GTM validation: Shadow & Ship — founder dogfooding for 2 weeks, then 5 colleague pilot
|
||||
- Solutions Library spec written (`docs/plans/2026-03-23-solutions-library-design.md`), implementation post-pilot
|
||||
- Remaining open issues: #66 Templates + Import/Export, #60 Recurring Issue Detection, #58 Step Feedback Flag
|
||||
**Use for:** core shared symbols (`flowpilot_engine`, `unified_chat_service`, auth middleware, `get_db`, shared hooks), cross-file renames, unfamiliar bug traces, refactor safety. **Skip for:** new endpoints, isolated fixes, changes you can read in one file.
|
||||
|
||||
---
|
||||
Re-indexes automatically on commit (PostToolUse hook). Manual refresh if stale: `npx gitnexus analyze`.
|
||||
|
||||
## Tech Stack
|
||||
### gstack skills
|
||||
|
||||
### Backend
|
||||
Always use `/browse` for web, never `mcp__claude-in-chrome__*`.
|
||||
|
||||
- **Framework:** Python FastAPI
|
||||
- **Database:** PostgreSQL 16 (async via SQLAlchemy 2.0 + asyncpg)
|
||||
- **Migrations:** Alembic
|
||||
- **Auth:** JWT (python-jose) + bcrypt, refresh token rotation (JTI-based)
|
||||
- **Validation:** Pydantic v2
|
||||
- **Scheduling:** APScheduler 3.x (async, in-process with FastAPI lifespan) + croniter + pytz
|
||||
Available commands:
|
||||
|
||||
### Frontend
|
||||
- **Planning & review:** `/autoplan`, `/plan-eng-review`, `/plan-design-review`, `/plan-ceo-review`, `/plan-devex-review`, `/devex-review`, `/review`, `/cso`, `/office-hours`
|
||||
- **Design:** `/design-consultation`, `/design-shotgun`, `/design-html`, `/design-review`
|
||||
- **Browser & QA:** `/browse`, `/connect-chrome`, `/qa`, `/qa-only`, `/setup-browser-cookies`
|
||||
- **Ship & deploy:** `/ship`, `/land-and-deploy`, `/canary`, `/benchmark`, `/setup-deploy`, `/document-release`
|
||||
- **Debug & investigate:** `/investigate`, `/careful`, `/freeze`, `/guard`, `/unfreeze`
|
||||
- **Other:** `/codex` (OpenAI second opinion), `/setup-gbrain`, `/retro`, `/learn`, `/gstack-upgrade`
|
||||
|
||||
- **Framework:** React 19 + Vite + TypeScript
|
||||
- **Styling:** Tailwind CSS v4 (`@tailwindcss/vite` plugin, CSS-only config in `index.css`) — flat dark theme with ember orange accent (see [DESIGN-SYSTEM.md](DESIGN-SYSTEM.md))
|
||||
- **State:** Zustand (with immer + zundo for undo/redo)
|
||||
- **Routing:** React Router v7
|
||||
- **API Client:** Axios with token refresh interceptor
|
||||
- **Icons:** Lucide React
|
||||
### Git trailer
|
||||
|
||||
---
|
||||
Every commit: `Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>`
|
||||
|
||||
## Key Project Structure
|
||||
### Model aliases
|
||||
|
||||
```
|
||||
patherly/
|
||||
├── backend/
|
||||
│ ├── app/
|
||||
│ │ ├── main.py # FastAPI entry point
|
||||
│ │ ├── api/endpoints/ # Route handlers (auth, trees, sessions, admin, steps, survey, copilot, assistant_chat, integrations)
|
||||
│ │ │ ├── flow_proposals.py # Knowledge Flywheel review queue CRUD
|
||||
│ │ │ └── flowpilot_analytics.py # FlowPilot dashboard metrics
|
||||
│ │ ├── api/deps.py # Auth dependencies (includes require_team_admin)
|
||||
│ │ ├── api/router.py # Route registration
|
||||
│ │ ├── core/ # config, database, permissions, security, audit, rate_limit
|
||||
│ │ ├── models/ # SQLAlchemy models (includes FlowProposal)
|
||||
│ │ ├── schemas/ # Pydantic schemas
|
||||
│ │ ├── services/psa/ # PSA provider abstraction (base, connectwise/, autotask/, halopsa/, cache, encryption, registry, types)
|
||||
│ │ ├── services/knowledge_flywheel.py # AI session analysis → flow proposals
|
||||
│ │ ├── services/knowledge_flywheel_scheduler.py # APScheduler job for batch analysis
|
||||
│ │ └── services/knowledge_gap_service.py # Weak options & escalation signal detection
|
||||
│ ├── alembic/ # Database migrations (001-070 sequential, then hash IDs)
|
||||
│ ├── scripts/ # seed_data.py, seed_trees.py
|
||||
│ └── tests/ # pytest integration tests
|
||||
├── frontend/
|
||||
│ ├── src/
|
||||
│ │ ├── api/ # Axios client + endpoint modules
|
||||
│ │ ├── components/ # common, layout, dashboard, tree-editor, session, procedural, procedural-editor, library, step-library, ui, flowpilot
|
||||
│ │ ├── hooks/ # usePermissions, useSessionTimer, useKeyboardShortcuts
|
||||
│ │ ├── pages/ # All page components
|
||||
│ │ ├── store/ # Zustand stores (auth, treeEditor, proceduralEditor, userPreferences, scriptGeneratorStore)
|
||||
│ │ └── types/ # TypeScript interfaces
|
||||
│ └── (Tailwind v4: CSS-only config in src/index.css)
|
||||
├── docs/plans/archive/ # Archived design/impl docs (pre-March 2026)
|
||||
├── CLAUDE.md # This file
|
||||
├── CURRENT-STATE.md # Detailed feature status
|
||||
├── LESSONS-LEARNED.md # (Deprecated — consolidated into CLAUDE.md)
|
||||
└── docs/plans/ # Design docs & implementation plans
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Environment Variables
|
||||
|
||||
### Backend (`backend/.env`)
|
||||
|
||||
```bash
|
||||
APP_NAME=ResolutionFlow
|
||||
DEBUG=true
|
||||
DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/patherly
|
||||
DATABASE_URL_SYNC=postgresql://postgres:postgres@localhost:5432/patherly
|
||||
SECRET_KEY=<openssl rand -hex 32>
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES=5
|
||||
REFRESH_TOKEN_EXPIRE_DAYS=7
|
||||
REQUIRE_INVITE_CODE=true
|
||||
```
|
||||
|
||||
### Frontend (`frontend/.env.local` - optional)
|
||||
|
||||
```bash
|
||||
VITE_API_URL=http://localhost:8000
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ConnectWise PSA Integration
|
||||
|
||||
ResolutionFlow integrates with ConnectWise PSA (formerly Manage) as the primary PSA integration. All ConnectWise API reference materials live in `docs/connectwise/`.
|
||||
|
||||
### Best Practices Documentation
|
||||
|
||||
Official ConnectWise developer guides live in `docs/connectwise/best-practices/`. Read these BEFORE implementing any CW API integration code:
|
||||
|
||||
- `PSA-API-Requests.md` — HTTP methods, response codes, condition query syntax, PATCH format, URL encoding, partial responses, custom fields. READ FIRST.
|
||||
- `PSA-Callbacks.md` — Callback type/level matrix, retry behavior, URL parameter gotcha, HMAC signature verification.
|
||||
- `PSA-Pagination.md` — Navigable vs Forward-Only pagination, Link headers, while-loop pattern.
|
||||
- `PSA-Service-Tickets.md` — Ticket field philosophy, recommended field mappings.
|
||||
- `PSA-Versioning.md` — Pin API version via Accept header. Use `application/vnd.connectwise.com+json; version=2025.16`.
|
||||
- `PSA-Cloud-URL-Formatting.md` — Dynamic base URL construction via `/login/companyinfo/{companyId}`.
|
||||
- `Bundled-Requests.md` — Batch multiple API calls into one request via `/system/bundles`.
|
||||
- `PSA-Markdown.md` — Ticket notes support markdown. Format session documentation output accordingly.
|
||||
- `PSA-Company-Synchronization.md` — Filter companies by Status/Type for mapping UI.
|
||||
- `PSA-Data-Protection.md` — Security role model, request minimal permissions (MY not ALL).
|
||||
|
||||
### Reference Files (read in this order)
|
||||
|
||||
1. `docs/connectwise/CONNECTWISE-API-REFERENCE.md` — Read FIRST. Quick reference covering auth patterns, tiered endpoint map, key field mappings, and integration architecture flows.
|
||||
2. `docs/connectwise/connectwise-psa-resolutionflow-reference.json` — Extracted OpenAPI 3.0.1 spec (v2025.16) with only the 670 endpoints and 342 schemas relevant to ResolutionFlow. Use for exact field types, request/response shapes, and parameter details.
|
||||
3. `docs/connectwise/connectwise-psa-openapi-full.json` — Complete ConnectWise PSA OpenAPI spec (1838 endpoints, 842 schemas). Only consult if you need an endpoint outside the extracted subset.
|
||||
|
||||
### Integration Architecture
|
||||
|
||||
- **Session → Ticket Notes:** Post auto-generated session documentation to ConnectWise tickets as internal analysis notes via `POST /service/tickets/{id}/notes`
|
||||
- **Ticket Context → Session Runner:** Pull ticket details, company info, and attached configurations to give FlowPilot AI real-world context
|
||||
- **Callbacks:** Register webhooks via `/system/callbacks` for real-time ticket event notifications to suggest relevant Flows
|
||||
|
||||
### Key Implementation Rules
|
||||
|
||||
- Auth: API Key auth (Base64 of `companyId+publicKey:privateKey`) + `clientId` header on every request
|
||||
- `clientId` is server-side config (`CW_CLIENT_ID` in `config.py`) — identifies the ResolutionFlow app, NOT per-tenant. Per-connection credentials: `company_id`, `public_key`, `private_key`, `server_url`
|
||||
- All PSA integration code in `services/psa/` — provider pattern with `PSAProvider` abstract base class, `ConnectWiseProvider` implementation, `PsaProviderRegistry` for multi-PSA dispatch
|
||||
- PSA endpoints in `api/endpoints/integrations.py` — connection CRUD, ticket ops, member mapping
|
||||
- Credentials encrypted at rest via `services/psa/encryption.py` (Fernet)
|
||||
- Each MSP tenant provides their own CW credentials — ResolutionFlow stores these per-team, never per-user
|
||||
- Design for the Autotask integration following the same service layer pattern (future PSA)
|
||||
- In-memory TTL cache in `services/psa/cache.py` for board/status/priority lookups
|
||||
- Respect CW API: paginate with max 1000 per page, handle retries gracefully
|
||||
|
||||
---
|
||||
|
||||
## Development Commands
|
||||
|
||||
```powershell
|
||||
# Start PostgreSQL (run from VPS SSH — docker not available inside code-server, see Lesson 103)
|
||||
docker start resolutionflow_postgres
|
||||
|
||||
# Backend (from backend/)
|
||||
source venv/bin/activate # Linux/Mac
|
||||
# .\venv\Scripts\Activate # Windows
|
||||
uvicorn app.main:app --reload
|
||||
|
||||
# Frontend (from frontend/)
|
||||
npm run dev
|
||||
|
||||
# Run tests (from backend/)
|
||||
pytest --override-ini="addopts="
|
||||
|
||||
# First time only: create test database
|
||||
docker exec -it resolutionflow_postgres psql -U postgres -c "CREATE DATABASE resolutionflow_test;"
|
||||
|
||||
# Frontend build (IMPORTANT: stricter than tsc --noEmit — always use as final check)
|
||||
cd frontend && npm run build
|
||||
|
||||
# Database migrations
|
||||
cd backend && alembic upgrade head
|
||||
alembic revision --autogenerate -m "Description"
|
||||
# Sequential 3-digit IDs (001–070) were used historically. New migrations use Alembic's default hex hash IDs.
|
||||
# Do NOT pass --rev-id — let Alembic generate the hash automatically.
|
||||
|
||||
# Access PostgreSQL (run from VPS SSH — docker not available inside code-server, see Lesson 103)
|
||||
docker exec -it resolutionflow_postgres psql -U postgres -d resolutionflow
|
||||
|
||||
# Seed data
|
||||
cd backend && pip install httpx && python -m scripts.seed_trees
|
||||
|
||||
# CI/CD debugging
|
||||
gh run list --limit 5 # Recent CI runs
|
||||
gh run view <id> --log-failed # Failed job logs
|
||||
gh run view <id> --json jobs --jq '.jobs[] | {name: .name, conclusion: .conclusion}'
|
||||
# NEVER use `gh run watch` — it holds context open and burns tokens while waiting
|
||||
```
|
||||
|
||||
### URLs
|
||||
|
||||
- Frontend: <http://localhost:5173>
|
||||
- Backend API: <http://localhost:8000>
|
||||
- API Docs: <http://localhost:8000/api/docs>
|
||||
|
||||
### Test Users (seeded via `scripts/seed_test_users.py`)
|
||||
|
||||
- All share password: `TestPass123!`
|
||||
- `admin@resolutionflow.example.com` (super_admin), `teamadmin@resolutionflow.example.com` (team_admin), `engineer@resolutionflow.example.com` (engineer), `pro@resolutionflow.example.com` (solo pro)
|
||||
|
||||
---
|
||||
|
||||
## Critical Lessons Learned
|
||||
|
||||
> Lessons 1-40 archived to `docs/LESSONS-ARCHIVE.md` — fixes are baked into the codebase. Consult if you hit a regression.
|
||||
|
||||
### Active Lessons (41+)
|
||||
|
||||
**41. Assistant chat uses local React state, not Zustand:** `AssistantChatPage.tsx` uses `useState` for `chats`, `messages`, `input`, `loading`. No store.
|
||||
|
||||
**42. Public pages use raw `fetch()`, not `apiClient`:** Survey, shared sessions, and no-auth pages use `fetch()` with full URL. `apiClient` requires auth tokens.
|
||||
|
||||
**43. Adding new email types:** Add static async method to `EmailService` in `core/email.py`. Fire-and-forget from endpoints (log errors, don't fail).
|
||||
|
||||
**44. AI Chat Builder is flow-type-aware:** `ai_chat_service.py` dispatches by `flow_type`. Troubleshooting: `[TREE_UPDATE]` markers. Procedural: `[STEPS_UPDATE]` markers. Both support `[METADATA]`.
|
||||
|
||||
**45. Intake form field schema:** Uses `variable_name` and `field_type` (NOT `name` and `type`).
|
||||
|
||||
**46. `CreateFlowDropdown` uses `AIPromptDialog`:** Opens prompt modal, starts AI session, generates flow, navigates to editor with `{ state: { aiPanelOpen: true, sessionId } }`.
|
||||
|
||||
**47. Editor-Embedded Flow Assist:** `EditorAIPanel` (320px side panel) + `useEditorAI` hook. Ghost nodes use `_suggestion: true` flag. Actions route to model tiers via `settings.get_model_for_action()`. Delta responses use `[DELTA]...[/DELTA]` markers.
|
||||
|
||||
**48. Tree orphan validation uses dynamic root ID:** Orphan check compares against `state.treeStructure?.id` (NOT hardcoded `'root'`).
|
||||
|
||||
**49. Full-stack features — verify both ends:** Check the full data flow: schema → endpoint → API client → hook → store → UI.
|
||||
|
||||
**50. Anthropic SDK retry:** Set `max_retries=1` to fail fast. Default `max_retries=2` can take 3× timeout.
|
||||
|
||||
**51. AI model tier routing:** Use `settings.get_model_for_action(action_type)`. Model IDs: use alias form (`claude-sonnet-4-6`).
|
||||
|
||||
**52. Mobile scroll-to-top:** Use `ref.current.scrollIntoView()`, not `window.scrollTo()`. Trigger via `useEffect`.
|
||||
|
||||
**53. Flex height chain:** Every ancestor must be a flex container for `flex-1` to work. Missing `flex` class collapses React Flow to 0 height.
|
||||
|
||||
**54. React Flow CSS in Tailwind v4:** Import in `index.css`, not component JS. Override dark theme using `--xy-*` CSS custom properties.
|
||||
|
||||
**55. App shell height chain:** Every wrapper between `.main-content` and canvas needs `flex` + `flex-1` + `min-h-0` or `h-full`.
|
||||
|
||||
**56. Railway backend service name is `patherly`:** Production DB name is `railway`. Public Postgres proxy: `interchange.proxy.rlwy.net:45797`.
|
||||
|
||||
**57. Node field priority:** `title` → `question` → `description` → `content` → `label`. See `copilot_service.py`.
|
||||
|
||||
**58. `scriptGeneratorStore.generate()` optional param:** Always wrap: `onClick={() => generate()}`, never `onClick={generate}`.
|
||||
|
||||
**59. ConnectWise `clientId` is server-side config:** Set in `config.py` as `CW_CLIENT_ID`. Per-connection: `company_id`, `public_key`, `private_key`, `server_url`.
|
||||
|
||||
**60. Dockerfile build args for Vite env vars:** Any new `VITE_*` or `VITE_PUBLIC_*` env var must be added as `ARG` + `ENV` in `frontend/Dockerfile` for Railway deploys. Railway env vars are runtime-only unless explicitly passed through as Docker build args. Without this, `import.meta.env.VITE_*` resolves to `undefined` in production builds.
|
||||
|
||||
**61. Procedural sessions auto-start on page load:** `ProceduralNavigationPage` calls `startSession()` immediately in `loadTree()` — there is no intake form screen or "Start" button. Variables are filled inline during execution. Troubleshooting flows DO have a start screen with ticket/client fields. Don't write tests or UI that assume a Start button on procedural flows.
|
||||
|
||||
**62. Playwright strict mode — scope selectors to avoid ambiguity:** Step titles appear in both the sidebar checklist and main content heading. Use `getByRole('heading', { name })` for the main content, or scope with `page.locator('.animate-scale-in')` for command palette items. `getByText()` frequently matches multiple elements due to the sidebar + main content layout.
|
||||
|
||||
**63. Node 20 required for frontend builds:** Vite 7+ requires Node 20.19+. The system Node may be v18; use nvm: `export NVM_DIR="$HOME/.nvm" && source "$NVM_DIR/nvm.sh" && nvm use 20`. For direct binary access without nvm sourcing: `PATH="$HOME/.nvm/versions/node/v20.19.0/bin:$PATH"`.
|
||||
|
||||
**64. PostHog product analytics:** Initialized via `PostHogProvider` in `main.tsx` with explicit `posthog.init()` + `client` prop pattern. Event helpers in `lib/analytics.ts` — use `analytics.eventName(props)` to track. `identifyUser()` called in `authStore.fetchUser()`, `resetAnalytics()` on logout. Env vars: `VITE_PUBLIC_POSTHOG_KEY`, `VITE_PUBLIC_POSTHOG_HOST`. Autocapture enabled.
|
||||
|
||||
**65. Local Docker Compose uses `resolutionflow` database on port 5433:** Container name is `resolutionflow_postgres`, database is `resolutionflow` (not `patherly`), port mapped to `5433` (not `5432`). The `POSTGRES_PORT` env var controls this. Playwright config defaults must match: `postgresql+asyncpg://postgres:postgres@127.0.0.1:5433/resolutionflow`.
|
||||
|
||||
**66. Dev environment runs on Hostinger VPS (46.202.92.250), not localhost:** Code-server runs in Docker on a VPS (previously devserver01/192.168.0.9). Frontend/backend are accessed via `46.202.92.250`, not `localhost`. CORS must include the VPS IP in `CORS_ORIGINS` and `FRONTEND_URL`. Frontend `.env` must set `VITE_API_URL` to the VPS backend URL. See [DEV-ENV.md](DEV-ENV.md) for full setup, Docker config, networking, and known issues.
|
||||
|
||||
**67. Tree editor route is `/trees/new`:** NOT `/editor/new`. Check `router.tsx` line 156 for the canonical path. Use `getTreeEditorPath()` from `@/lib/routing` when navigating programmatically.
|
||||
|
||||
**68. APScheduler jobs need `max_instances=1`:** Without it, overlapping scheduler runs can process the same records twice (TOCTOU race). Always set `max_instances=1` on interval jobs in `main.py`.
|
||||
|
||||
**69. PostgreSQL `func.sum(case(...))` returns `Decimal` via asyncpg:** Cast to `int()` before storing in Pydantic `dict[str, Any]` fields, or JSON serialization may produce unexpected types.
|
||||
|
||||
**70. Toast library uses `toast.warning()` not `toast.warn()`:** Import from `@/lib/toast`. Methods: `success`, `error`, `warning`, `info`. See `frontend/src/lib/toast.ts`.
|
||||
|
||||
**71. Enhancement/branch_addition proposals cannot be directly approved:** Backend returns 400 — they require `modified_flow_data` via "Edit & Publish" flow. Only `new_flow` proposals support direct approve.
|
||||
|
||||
**72. `ai_sessions.status` column is `VARCHAR(30)`:** Must fit `requesting_escalation` (23 chars). If adding new status values, verify length. Migration `f0aad74ea51b` widened from 20→30.
|
||||
|
||||
**73. `get_db` rolls back on exception:** The dependency does `await session.rollback()` on error to prevent `InFailedSQLTransaction` cascade. Never remove this — without it, one failed request poisons subsequent requests on the same connection.
|
||||
|
||||
**74. FlowPilot action bar height chain:** The action bar (Resolve/Escalate/Pause) requires every ancestor from `app-shell` grid down to have proper flex constraints. Key fix: `ViewTransitionOutlet` wrapper needs `flex flex-col`. If action bar disappears, check height chain with DevTools `getBoundingClientRect()` walk.
|
||||
|
||||
**75. Dashboard prefill auto-submits:** `StartSessionInput` navigates to `/pilot` or `/assistant` with `{ state: { prefill } }`. `FlowPilotSessionPage` auto-submits via `useEffect` + `prefillHandledRef` guard — no double-enter. `AssistantChatPage` does the same pattern.
|
||||
|
||||
**76. Active session navigation guard:** `FlowPilotSessionPage` uses `useBlocker` (same as `TreeEditorPage`) to intercept navigation during active sessions. "Pause & Leave" auto-pauses before proceeding.
|
||||
|
||||
**77. Prefer manual Alembic migrations for targeted changes:** `alembic revision --autogenerate` picks up drift from all tables. For single-column fixes, use `alembic revision -m "desc"` and write `op.alter_column()` manually.
|
||||
|
||||
**78. Landing page subtitle is "AI-Powered Troubleshooting for MSPs":** Not "Decision Tree Platform". This tagline appears on login, register, and the HTML `<title>`. The old "Decision Tree Platform" was internal jargon misaligned with user-facing branding.
|
||||
|
||||
**79. Custom modals must be mobile-responsive:** Use `items-end sm:items-center` (bottom-sheet on mobile, centered on desktop) and `max-w-full sm:max-w-lg` (full-width on mobile). The shared `Modal.tsx` does this correctly — custom modal implementations must follow the same pattern. See `PrepareSessionModal.tsx` for the fix pattern.
|
||||
|
||||
**80. TopBar search collapses to icon on mobile:** Full search bar (`hidden sm:block`) shows on desktop; magnifying glass icon button (`sm:hidden`) shows on mobile (<640px). Both open the same CommandPalette. Don't add `w-full` search bar without the mobile icon fallback.
|
||||
|
||||
**81. Never use `transition: all` in landing.css:** Specify exact properties: `transition: background 0.3s, border-color 0.3s, box-shadow 0.3s, transform 0.3s, opacity 0.3s`. `transition: all` animates layout properties and causes jank.
|
||||
|
||||
**82. `bun` requires PATH setup on devserver01:** `export BUN_INSTALL="$HOME/.bun" && export PATH="$BUN_INSTALL/bin:$PATH"`. The gstack browse binary and Playwright need this. Chromium system deps: `libatk1.0-0 libatk-bridge2.0-0 libcups2 libxkbcommon0 libatspi2.0-0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libasound2`.
|
||||
|
||||
**83. ~~FlowPilot ActionBar fixed bottom~~ (Superseded by Lesson 93):** Actions moved to the page header. `FlowPilotActionBar` component exists but is no longer used in the main session flow. The only fixed-bottom element is the message input.
|
||||
|
||||
**84. AI session `abandoned` status is fully wired:** `POST /ai-sessions/{id}/abandon` sets status to `abandoned` with optional `reason` param. Frontend: `aiSessionsApi.abandonSession()`, `useFlowPilotSession().abandonSession()`, "Close" button in `FlowPilotActionBar`. Redirects to `/sessions` after closing.
|
||||
|
||||
**85. Date range filter end dates must use end-of-day:** `toDate.toISOString()` sends midnight (start of day), excluding items created later that day. Always set `toDate.setHours(23, 59, 59, 999)` before sending. For string-based date inputs (AI sessions), append `T23:59:59.999Z`. See `SessionHistoryPage.tsx`.
|
||||
|
||||
**86. Script Builder system:** AI-powered script generation at `/script-builder`. Chat-style interface generates PowerShell/Bash/Python scripts from natural language. Backend: `ScriptBuilderSession` model, `script_builder_service.py`, endpoints at `/scripts/builder/`. Frontend: `ScriptBuilderPage`, `ScriptCodeBlock`, `ScriptPreviewModal`, `SaveToLibraryDialog`. FlowPilot can hand off to Script Builder via `action_type: "open_script_builder"` with `sessionStorage` context passing.
|
||||
|
||||
**87. FlowPilot must ask GUI vs script preference:** When a task can be done via GUI or script (e.g., creating AD users), FlowPilot must ask the engineer which approach they prefer BEFORE suggesting either. Never assume the user wants a script. See `FLOWPILOT_SYSTEM_PROMPT` rules in `flowpilot_engine.py`.
|
||||
|
||||
**88. Charcoal palette — sidebar-darkest approach:** Sidebar `#0e1016`, page `#16181f`, cards `#1e2028`, borders `#2a2e3a`. This gives more contrast range than true-dark. All colors via CSS variables in `index.css` `@theme` block. Accent is electric blue (#60a5fa), not orange or cyan.
|
||||
|
||||
*(Lessons 89–91 were retracted.)*
|
||||
|
||||
**92. `tsc -b` in Dockerfile is stricter than `npx tsc --noEmit`:** The production build (`tsc -b && vite build`) enforces `noUnusedLocals` and `noUnusedParameters` as hard errors. After any refactor that moves logic between components or removes features, trace every import and destructured prop to remove orphans. IDE warnings (yellow squiggles) flag these — check them before pushing.
|
||||
|
||||
**93. FlowPilot actions live in the page header, not a bottom bar:** `FlowPilotSessionPage` renders Resolve/Escalate/Share Update in the header bar. Desktop: inline buttons + `⋯` overflow (Pause/Close). Mobile: single `⋯` menu. The bottom only has the message input. `FlowPilotActionBar` component still exists but is no longer used in the main session flow.
|
||||
|
||||
**94. Frontend chat uses unified_chat_service, not assistant_chat_service:** `AssistantChatPage` calls `/ai-sessions/{id}/chat` → `unified_chat_service.py`. The old `assistant_chat_service` endpoints were removed (only retention settings remain at `/assistant/retention`). When tracing chat features, start from `aiSessionsApi.sendChatMessage` → `ai_sessions.py` → `unified_chat_service.py`. Never wire chat features into `assistant_chat.py`.
|
||||
|
||||
**95. Image upload → AI vision pipeline:** Paste/attach images → upload to Railway S3 bucket via `uploadsApi.upload()` → send `upload_ids` with chat message → backend fetches from S3 via `storage_service.download_file()` → resized via `storage_service.resize_image_for_vision()` (Pillow, 1568px max, PNG→JPEG) → base64-encoded → sent as Claude multimodal content blocks. Max 3 images/message. Images are NOT stored in conversation history (text-only). Vision helpers live in `storage_service.py`.
|
||||
|
||||
**96. `bg-accent` is electric blue — never use for code/kbd elements:** In Tailwind v4, `bg-accent` maps to `--color-accent: #60a5fa` (dark) / `#2563eb` (light). Use `bg-code` for code blocks, `bg-white/[0.12] border border-white/[0.06]` for inline code/badges, `bg-white/[0.08]` for kbd shortcuts. Blue accent is reserved for interactive elements only (buttons, active nav, links). Ember orange (#f97316) is deprecated — do not use.
|
||||
|
||||
**97. Railway Object Storage (S3 bucket) is provisioned:** Bucket `resolutionflow-uploads` on Railway canvas. Variables: `STORAGE_ENDPOINT`, `STORAGE_ACCESS_KEY`, `STORAGE_SECRET_KEY`, `STORAGE_BUCKET_NAME`, `STORAGE_REGION` — mapped via variable references on the `patherly` backend service. Accessed via boto3 in `storage_service.py`. Pillow (`Pillow>=10.0.0`) + `libjpeg-dev`/`zlib1g-dev` in Dockerfile for image resize.
|
||||
|
||||
**98. `lazyWithRetry` for stale chunk errors:** All lazy-loaded routes use `lazyWithRetry` from `@/lib/lazyWithRetry.ts` instead of `React.lazy`. Auto-reloads the page on chunk load failures (stale deploys). Uses sessionStorage debounce (10s) to prevent loops. When adding new lazy routes, use `lazyWithRetry`, not `lazy`.
|
||||
|
||||
**99. Tailwind v4 `text-secondary` renders invisible on dark backgrounds:** `text-secondary` maps to `--color-secondary: #2e3140` (a dark surface color), NOT `--color-text-secondary`. For readable secondary text, use `text-muted-foreground` (`#848b9b`). Also avoid `text-muted` (`#4f5666`) for body text — it's for labels only. This applies to ALL new components.
|
||||
|
||||
**100. Hover pop-out card pattern:** For cards that expand on hover "in front of everything": use `pointer-events-none` on the scrim (`fixed inset-0 z-40 bg-black/30`), absolute-position the expanded card at `z-50` with its own `onClick` handler, and dismiss via `onMouseLeave` on the wrapper div. Never put interactive event handlers on the scrim — it blocks clicks on sibling elements.
|
||||
|
||||
**101. AI marker format compliance:** The AI assistant uses `[QUESTIONS]`, `[ACTIONS]`, and `[FORK]` markers in responses. Parsed by `unified_chat_service.py` (`_parse_*_marker` functions), returned as structured data in the API response. System prompt in `assistant_chat_service.py` has a final reminder section, and each user message gets an invisible `[SYSTEM: ...]` reminder appended in `_call_anthropic_cached()`. If markers stop appearing: check conversation history stores `display_content` (stripped), verify system prompt final reminder exists, check user message reminder injection is active.
|
||||
|
||||
**102. TaskLane activation must happen in ALL chat response paths:** `AssistantChatPage.tsx` has three code paths calling `sendChatMessage`: `handleSend` (regular messages), `sendPrefill` (dashboard handoff), `handleResumeNew` (resume from concluded session). ALL three must check `response.actions`/`response.questions` and call `setShowTaskLane(true)`. Missing this in any path causes TaskLane to not appear on first message.
|
||||
|
||||
**103. Docker not available in code-server container:** The dev environment runs code-server inside Docker on the VPS. The `docker` CLI is not available inside the code-server container. To query the database, use the VPS SSH session: `docker exec resolutionflow_postgres psql -U postgres -d resolutionflow -t -c "SQL"`. Python is also not available in the container.
|
||||
|
||||
**104. `landing.css` uses self-contained `--lp-*` color variables:** The landing page defines its own color palette at the top of `landing.css` (`--lp-bg`, `--lp-accent`, `--lp-text-*`, etc.). Never use `var(--color-*)` theme tokens in `landing.css` — they may resolve incorrectly outside the app shell context. Extend the `--lp-*` palette for any new landing page colors.
|
||||
|
||||
**105. `npm run build` fails with `EACCES: permission denied` on `dist/` in code-server:** This is a filesystem permission issue in the Docker environment, not a TypeScript error — the TS compilation completes successfully. Use `npx tsc -b` to verify TypeScript cleanly without needing to write to `dist/`.
|
||||
|
||||
**106. Guard async "select item → load data → apply state" flows with a ref:** When a component lets the user switch between items (chat sessions, flows, scripts) and loads data asynchronously on each switch, the load for item A can complete *after* the user has already switched to item B — overwriting B's state with A's stale data. Fix pattern: keep a `currentSelectionRef = useRef(initialId)` and update it synchronously whenever the selection changes (in every creation/switch path). After every `await`, bail out if `currentSelectionRef.current !== thisItemId`. See `AssistantChatPage.tsx` `selectChat` for the reference implementation (`currentChatRef`).
|
||||
|
||||
**107. Startup routines must use `_admin_session_factory()` after Phase 4 RLS:** Any code that runs at startup (lifespan, `ensure_service_account`, seed scripts) and touches tenant-isolated tables (`users`, etc.) must use `_admin_session_factory()` — not `get_db()`. Phase 4 enabled RLS on `users`; a tenant-scoped session has no `app.current_account_id` set at startup, so all queries return 0 rows or fail. `get_service_account_id` in `deps.py` is safe — it reads from `app.state` cached at startup, never hits the DB per-request.
|
||||
|
||||
**108. Tables with no `account_id` column (never add to RLS migrations):** `script_categories`, `platform_steps`, `template_trees`, `plan_feature_defaults`, `accounts` — global/platform tables documented with "No account_id. No RLS." in their model files. When writing RLS migrations, scan at the class level (check for `account_id: Mapped` within the class block), not the file level — multiple classes in one `.py` file can have different columns (e.g. `ScriptCategory` vs `ScriptTemplate` in `script_template.py`).
|
||||
|
||||
**109. `tree_shares.account_id` must equal `tree.account_id`, not the actor's account:** When creating a `TreeShare`, always use `account_id=tree.account_id` (tree owner's tenant). A super admin in tenant A sharing tenant B's tree must produce a share row in tenant B's RLS context — using `current_user.account_id` instead makes the share invisible to the tree owner after RLS is enforced.
|
||||
|
||||
## RBAC & Permissions
|
||||
|
||||
- **Role hierarchy:** super_admin > team_admin > engineer > viewer
|
||||
- **Team Admin:** `role='engineer'` + `is_team_admin=True` + valid `team_id`
|
||||
- **Backend deps:** `get_current_active_user(user, db)` (any active + auto-downgrades expired trials), `require_engineer_or_admin` (blocks viewers), `require_admin` (super admin only)
|
||||
- **Never use** `role == "admin"` — use `is_super_admin` instead
|
||||
- **Frontend:** `usePermissions()` hook for all permission checks
|
||||
- **Centralized:** `backend/app/core/permissions.py`, `frontend/src/hooks/usePermissions.ts`
|
||||
|
||||
---
|
||||
|
||||
## Design System
|
||||
|
||||
**Source of truth:** [DESIGN-SYSTEM.md](DESIGN-SYSTEM.md) — always read this before making visual or UI decisions.
|
||||
|
||||
- **Theme:** Flat, high-contrast dark theme (Sentry/PostHog-inspired). No glass morphism, no backdrop blur, no ambient orbs, no gradient backgrounds on surfaces. Light mode fully specified (v6).
|
||||
- **Backgrounds:** `bg-page` (`#16181f`), `bg-sidebar` (`#0e1016`), `bg-card` (`#1e2028`), `bg-elevated` (`#2a2d38`)
|
||||
- **Cards:** `bg-card` with 1px `border-default` (`#2a2e3a`), 8px radius. No shadows, no blur, no gradients. Hover: `border-hover` (`#3d4252`)
|
||||
- **Buttons:** Primary: solid `accent` (#60a5fa dark / #2563eb light), white text, 5px radius. Ghost: transparent + 1px border, hover `bg-elevated`
|
||||
- **Inputs:** `bg-input` (`#252830`) with 1px `border-default`, 5px radius. Focus: `border-color: accent` + `box-shadow: 0 0 0 2px accent-dim`
|
||||
- **Text:** `text-heading` (`#f0f2f5`) → `text-primary` (`#e2e5eb`) → `text-muted-foreground` (`#848b9b`) → `text-muted` (`#4f5666`). NEVER use `text-secondary` — in Tailwind v4 it maps to a surface color, not a text color.
|
||||
- **Borders:** `border-default` (`#2a2e3a`), `border-hover` (`#3d4252`)
|
||||
- **Functional colors:** `#34d399` (success), `#fbbf24` (warning/amber), `#f87171` (danger), `#67e8f9` (info/cyan) — each with `-dim` variant at 10% opacity
|
||||
- **Accent:** Electric blue `#60a5fa` (dark) / `#2563eb` (light) — used sparingly (≤5% of UI). `accent-dim` = `rgba(96,165,250,0.10)`, `accent-text` = `#93c5fd`
|
||||
- **Deprecated:** Do NOT use `glass-card`, `glass-stat`, `bg-gradient-brand`, `text-gradient-brand`, `backdrop-filter: blur()`, ambient orbs, purple gradients, ember orange (`#f97316`), or cyan (`#22d3ee`) as accent — cyan is now the info color only
|
||||
|
||||
---
|
||||
|
||||
## Frontend Patterns
|
||||
|
||||
- **Component guidelines:** Use `cn()` from `@/lib/utils`, Lucide icons (wrap in `<span>` for title), modals with fixed header/footer
|
||||
- **Type organization:** Create in `types/`, export from `types/index.ts`, import with `import type { T } from '@/types'`
|
||||
- **Scratchpad overlay:** `position: fixed`, `onOpenChange` callback for parent padding adjustment, `right-2` positioning
|
||||
- **Custom step flow:** `CustomStepModal` → `PostStepActionModal` → `ContinuationModal` → custom step view. Key state: `pendingStep`, `pendingContinuationNodeId`, `customBranchMode`, `branchOriginNodeId`. Use `findCustomStep()` not `findNode()` for custom step UUIDs.
|
||||
- **Session sharing:** `ShareSessionModal` manages share links, `SharedSessionPage` renders public/account views. Helper utils in `lib/sessionShare.ts`. Share URLs use `/shared/sessions/:token`.
|
||||
- **Procedural navigation:** `ProceduralNavigationPage` handles intake forms, step-by-step execution, and resume via `location.state.sessionId`. Uses `StepChecklist`, `StepDetail`, `ProgressBar`, `CompletionSummary` components.
|
||||
- **Routing helper:** Use `getTreeNavigatePath()` and `getTreeEditorPath()` from `@/lib/routing` for all tree/session navigation.
|
||||
- **Account section layout:** `AccountLayout` has NO sidebar nav. Account sub-pages (categories, target-lists) are reached via link cards on `AccountSettingsPage.tsx`. New account pages: add route in `router.tsx` under `account` children + add a link card in `AccountSettingsPage`.
|
||||
- **Dashboard cockpit:** `QuickStartPage` is the copilot-first launchpad. Greeting + "What are you troubleshooting?" + ChatGPT-style `StartSessionInput` (auto-growing textarea, paste images, drag-drop files, attach button, paste logs, suggestion chips). Below: `PendingEscalations`, `ActiveFlowPilotSessions`, `RecentFlowPilotSessions`. Collapsible "Dashboard" section for `PerformanceCards`, `KnowledgeBaseCards`, `TeamSummary`.
|
||||
- **Sidebar sections:** Amber "New Session" button → Home → RESOLVE (History) → KNOWLEDGE (Flows with Solutions Library sub-item, Scripts) → INSIGHTS (Data). Footer: Account, Pin/Unpin. No help/guides/feedback in sidebar — accessible via TopBar.
|
||||
|
||||
---
|
||||
|
||||
## Common Tasks
|
||||
|
||||
- **New endpoint:** Create in `endpoints/` → add to `router.py` → schema in `schemas/` → tests → frontend API client
|
||||
- **New page:** Create in `pages/` → add route in `router.tsx` → nav link in `AppLayout.tsx`
|
||||
- **New public route (no auth):** Add at top level in `router.tsx` alongside `/login`, `/register` — NOT inside the `ProtectedRoute`/`AppLayout` children.
|
||||
- **Schema change:** Update model → `alembic revision --autogenerate -m "desc" --rev-id=NNN` (NNN = next sequential number, e.g., 068 → 069) → review → `alembic upgrade head`
|
||||
- **New frontend API module:** Types in `types/` → export from `types/index.ts` → client in `api/` → export from `api/index.ts`
|
||||
|
||||
---
|
||||
|
||||
## Coding Standards
|
||||
|
||||
### Python
|
||||
|
||||
- Type hints everywhere, async/await for DB, Pydantic for validation, `DateTime(timezone=True)` always
|
||||
|
||||
### TypeScript
|
||||
|
||||
- Interfaces for all data, `const` over `let`, functional components + hooks, reusable logic in custom hooks
|
||||
|
||||
### Git
|
||||
|
||||
- Format: `type: description` (feat, fix, refactor, docs, test, chore)
|
||||
- Always include `Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>`
|
||||
- Always create feature branch BEFORE committing: `git checkout -b feat/feature-name`
|
||||
- Large features: commit per phase with `npm run build` validation
|
||||
|
||||
### After Completing Work
|
||||
|
||||
When a feature, fix, or significant piece of work is finished and merged/committed:
|
||||
|
||||
1. **Update `CURRENT-STATE.md`** — move completed items, update "In Progress" and "What's Next" sections
|
||||
2. **Update `03-DEVELOPMENT-ROADMAP.md`** — check off completed work, update phase status
|
||||
3. **Close related GitHub Issues** — use `gh issue close #N` for any issues resolved by the work
|
||||
4. **Update `CLAUDE.md`** if the work introduced new patterns, lessons learned, or changed project structure
|
||||
|
||||
---
|
||||
|
||||
## gstack (Browser & Workflow Skills)
|
||||
|
||||
**Web browsing:** Always use the `/browse` skill from gstack for all web browsing needs. Never use `mcp__claude-in-chrome__*` tools.
|
||||
|
||||
**Available skills:**
|
||||
|
||||
| Skill | Purpose |
|
||||
|-------|---------|
|
||||
| `/office-hours` | Brainstorm new ideas (YC-style office hours) |
|
||||
| `/plan-ceo-review` | CEO/founder-mode plan review (scope, ambition) |
|
||||
| `/plan-eng-review` | Engineering plan review (architecture, edge cases) |
|
||||
| `/plan-design-review` | Design plan review (UI/UX critique) |
|
||||
| `/design-consultation` | Create a design system / DESIGN.md |
|
||||
| `/review` | Pre-landing PR code review |
|
||||
| `/ship` | Ship workflow (tests, review, PR creation) |
|
||||
| `/browse` | Headless browser for QA testing and site dogfooding |
|
||||
| `/qa` | Systematic QA testing + auto-fix bugs found |
|
||||
| `/qa-only` | QA report only (no fixes) |
|
||||
| `/design-review` | Visual QA — find and fix design inconsistencies |
|
||||
| `/setup-browser-cookies` | Import cookies from real browser for authenticated testing |
|
||||
| `/retro` | Weekly engineering retrospective |
|
||||
| `/investigate` | Systematic debugging with root cause analysis |
|
||||
| `/document-release` | Post-ship documentation updates |
|
||||
| `/codex` | Second opinion via OpenAI Codex CLI |
|
||||
| `/careful` | Safety guardrails for destructive commands |
|
||||
| `/freeze` | Restrict edits to a specific directory |
|
||||
| `/guard` | Full safety mode (careful + freeze) |
|
||||
| `/unfreeze` | Remove edit restrictions |
|
||||
| `/gstack-upgrade` | Upgrade gstack to latest version |
|
||||
|
||||
---
|
||||
|
||||
## Deployment (Railway)
|
||||
|
||||
- **Production:** `resolutionflow.com` (frontend), `api.resolutionflow.com` (backend)
|
||||
- Auto-deploys on push to `main`
|
||||
- PR environments auto-created (need manual domain generation in Railway dashboard)
|
||||
- PR envs need `VITE_API_URL` set with `https://` prefix on frontend service
|
||||
- `ALLOW_RAILWAY_ORIGINS=true` enables CORS for `*.up.railway.app`
|
||||
- Shared Variables (project-level in Railway dashboard) auto-propagate to all environments including PR envs — use for secrets like `ANTHROPIC_API_KEY`
|
||||
- Super admin utility: `backend/make_superadmin_simple.py list|<email>`
|
||||
|
||||
---
|
||||
|
||||
## Future Roadmap
|
||||
|
||||
- **Phase 3:** PSA integrations (ConnectWise in progress), file attachments, client context, analytics
|
||||
- **Phase 4:** Additional PSA integrations (Autotask/Kaseya), PowerShell automation, enterprise SSO
|
||||
|
||||
---
|
||||
|
||||
## Quick Reference
|
||||
|
||||
| What | Where |
|
||||
|------|-------|
|
||||
| API Docs | <http://localhost:8000/api/docs> |
|
||||
| Detailed Status | [CURRENT-STATE.md](CURRENT-STATE.md) |
|
||||
| Development Roadmap | [03-DEVELOPMENT-ROADMAP.md](03-DEVELOPMENT-ROADMAP.md) |
|
||||
| GitHub Issues | `gh issue list --state open` |
|
||||
| Bugs & Fixes | CLAUDE.md → Critical Lessons Learned section |
|
||||
| Design System | [DESIGN-SYSTEM.md](DESIGN-SYSTEM.md) |
|
||||
| Dev Environment | [DEV-ENV.md](DEV-ENV.md) — 46.202.92.250 setup, Docker, CORS, networking |
|
||||
|
||||
<!-- gitnexus:start -->
|
||||
# GitNexus — Code Intelligence
|
||||
|
||||
This project is indexed by GitNexus as **resolutionflow** (16703 symbols, 35922 relationships, 300 execution flows). Use the GitNexus MCP tools to understand code, assess impact, and navigate safely.
|
||||
|
||||
> If any GitNexus tool warns the index is stale, run `npx gitnexus analyze` in terminal first.
|
||||
|
||||
## Always Do
|
||||
|
||||
- **MUST run impact analysis before editing any symbol.** Before modifying a function, class, or method, run `gitnexus_impact({target: "symbolName", direction: "upstream"})` and report the blast radius (direct callers, affected processes, risk level) to the user.
|
||||
- **MUST run `gitnexus_detect_changes()` before committing** to verify your changes only affect expected symbols and execution flows.
|
||||
- **MUST warn the user** if impact analysis returns HIGH or CRITICAL risk before proceeding with edits.
|
||||
- When exploring unfamiliar code, use `gitnexus_query({query: "concept"})` to find execution flows instead of grepping. It returns process-grouped results ranked by relevance.
|
||||
- When you need full context on a specific symbol — callers, callees, which execution flows it participates in — use `gitnexus_context({name: "symbolName"})`.
|
||||
|
||||
## When Debugging
|
||||
|
||||
1. `gitnexus_query({query: "<error or symptom>"})` — find execution flows related to the issue
|
||||
2. `gitnexus_context({name: "<suspect function>"})` — see all callers, callees, and process participation
|
||||
3. `READ gitnexus://repo/resolutionflow/process/{processName}` — trace the full execution flow step by step
|
||||
4. For regressions: `gitnexus_detect_changes({scope: "compare", base_ref: "main"})` — see what your branch changed
|
||||
|
||||
## When Refactoring
|
||||
|
||||
- **Renaming**: MUST use `gitnexus_rename({symbol_name: "old", new_name: "new", dry_run: true})` first. Review the preview — graph edits are safe, text_search edits need manual review. Then run with `dry_run: false`.
|
||||
- **Extracting/Splitting**: MUST run `gitnexus_context({name: "target"})` to see all incoming/outgoing refs, then `gitnexus_impact({target: "target", direction: "upstream"})` to find all external callers before moving code.
|
||||
- After any refactor: run `gitnexus_detect_changes({scope: "all"})` to verify only expected files changed.
|
||||
|
||||
## Never Do
|
||||
|
||||
- NEVER edit a function, class, or method without first running `gitnexus_impact` on it.
|
||||
- NEVER ignore HIGH or CRITICAL risk warnings from impact analysis.
|
||||
- NEVER rename symbols with find-and-replace — use `gitnexus_rename` which understands the call graph.
|
||||
- NEVER commit changes without running `gitnexus_detect_changes()` to check affected scope.
|
||||
|
||||
## Tools Quick Reference
|
||||
|
||||
| Tool | When to use | Command |
|
||||
|------|-------------|---------|
|
||||
| `query` | Find code by concept | `gitnexus_query({query: "auth validation"})` |
|
||||
| `context` | 360-degree view of one symbol | `gitnexus_context({name: "validateUser"})` |
|
||||
| `impact` | Blast radius before editing | `gitnexus_impact({target: "X", direction: "upstream"})` |
|
||||
| `detect_changes` | Pre-commit scope check | `gitnexus_detect_changes({scope: "staged"})` |
|
||||
| `rename` | Safe multi-file rename | `gitnexus_rename({symbol_name: "old", new_name: "new", dry_run: true})` |
|
||||
| `cypher` | Custom graph queries | `gitnexus_cypher({query: "MATCH ..."})` |
|
||||
|
||||
## Impact Risk Levels
|
||||
|
||||
| Depth | Meaning | Action |
|
||||
|-------|---------|--------|
|
||||
| d=1 | WILL BREAK — direct callers/importers | MUST update these |
|
||||
| d=2 | LIKELY AFFECTED — indirect deps | Should test |
|
||||
| d=3 | MAY NEED TESTING — transitive | Test if critical path |
|
||||
|
||||
## Resources
|
||||
|
||||
| Resource | Use for |
|
||||
|----------|---------|
|
||||
| `gitnexus://repo/resolutionflow/context` | Codebase overview, check index freshness |
|
||||
| `gitnexus://repo/resolutionflow/clusters` | All functional areas |
|
||||
| `gitnexus://repo/resolutionflow/processes` | All execution flows |
|
||||
| `gitnexus://repo/resolutionflow/process/{name}` | Step-by-step execution trace |
|
||||
|
||||
## Self-Check Before Finishing
|
||||
|
||||
Before completing any code modification task, verify:
|
||||
1. `gitnexus_impact` was run for all modified symbols
|
||||
2. No HIGH/CRITICAL risk warnings were ignored
|
||||
3. `gitnexus_detect_changes()` confirms changes match expected scope
|
||||
4. All d=1 (WILL BREAK) dependents were updated
|
||||
|
||||
## Keeping the Index Fresh
|
||||
|
||||
After committing code changes, the GitNexus index becomes stale. Re-run analyze to update it:
|
||||
|
||||
```bash
|
||||
npx gitnexus analyze
|
||||
```
|
||||
|
||||
If the index previously included embeddings, preserve them by adding `--embeddings`:
|
||||
|
||||
```bash
|
||||
npx gitnexus analyze --embeddings
|
||||
```
|
||||
|
||||
To check whether embeddings exist, inspect `.gitnexus/meta.json` — the `stats.embeddings` field shows the count (0 means no embeddings). **Running analyze without `--embeddings` will delete any previously generated embeddings.**
|
||||
|
||||
> Claude Code users: A PostToolUse hook handles this automatically after `git commit` and `git merge`.
|
||||
|
||||
## CLI
|
||||
|
||||
| Task | Read this skill file |
|
||||
|------|---------------------|
|
||||
| Understand architecture / "How does X work?" | `.claude/skills/gitnexus/gitnexus-exploring/SKILL.md` |
|
||||
| Blast radius / "What breaks if I change X?" | `.claude/skills/gitnexus/gitnexus-impact-analysis/SKILL.md` |
|
||||
| Trace bugs / "Why is X failing?" | `.claude/skills/gitnexus/gitnexus-debugging/SKILL.md` |
|
||||
| Rename / extract / split / refactor | `.claude/skills/gitnexus/gitnexus-refactoring/SKILL.md` |
|
||||
| Tools, resources, schema reference | `.claude/skills/gitnexus/gitnexus-guide/SKILL.md` |
|
||||
| Index, status, clean, wiki CLI commands | `.claude/skills/gitnexus/gitnexus-cli/SKILL.md` |
|
||||
|
||||
<!-- gitnexus:end -->
|
||||
Always use alias form (`claude-sonnet-4-6`, `claude-opus-4-6`, etc.) via `settings.get_model_for_action()`. Never hardcode a dated model ID.
|
||||
|
||||
783
DEV-ENV.md
783
DEV-ENV.md
@@ -1,262 +1,671 @@
|
||||
# ResolutionFlow Dev Environment Setup & Operations Guide
|
||||
# ResolutionFlow — Dev Environment Setup & Operations Guide
|
||||
|
||||
## Server Overview
|
||||
> **Scope:** Stand up a working ResolutionFlow dev environment from scratch on any Linux host (VPS, on-prem Proxmox LXC/VM, bare metal). Self-contained — do not read another doc to get the dev stack running.
|
||||
> **Last rewritten:** April 2026, post-Hostinger-VPS deprecation, ahead of Proxmox migration.
|
||||
> **Audience:** You (returning to the project), a teammate, or a fresh Claude Code session.
|
||||
|
||||
- **Provider:** Hostinger KVM VPS (srv1522117)
|
||||
- **IP Address:** 46.202.92.250
|
||||
- **OS:** Ubuntu 24.04 LTS
|
||||
- **CPU:** 2 vCPU cores
|
||||
- **RAM:** 8GB
|
||||
- **Disk:** 100GB NVMe SSD
|
||||
- **Swap:** 4GB (`/swapfile`, swappiness=10)
|
||||
If you're picking up mid-migration and need to know what code state is on the current branch, read `docs/FlowAssist_Migration/MIGRATION-HANDOFF.md` first.
|
||||
|
||||
## Architecture
|
||||
---
|
||||
|
||||
All services run as Docker containers on the host, managed via SSH or from the VS Code Server integrated terminal.
|
||||
## 1. What this project needs, regardless of host
|
||||
|
||||
```
|
||||
Host (root@srv1522117)
|
||||
├── Traefik → reverse proxy + auto SSL (Let's Encrypt)
|
||||
├── VS Code Server → browser IDE at https://code.resolutionflow.com
|
||||
└── ResolutionFlow Stack
|
||||
├── resolutionflow_frontend → Vite/React on port 5173
|
||||
├── resolutionflow_backend → FastAPI/Uvicorn on port 8000
|
||||
└── resolutionflow_postgres → PostgreSQL 16 + pgvector on port 5432
|
||||
```
|
||||
These are non-negotiable. If your host can't provide them, fix that before anything else.
|
||||
|
||||
## Access URLs
|
||||
| Component | Required version | Notes |
|
||||
|---|---|---|
|
||||
| **Linux** | any mainstream distro | Ubuntu 22.04+ / Debian 12+ tested; Alpine fine for containers |
|
||||
| **Python** | 3.11+ | Backend and migrations |
|
||||
| **Node.js** | 20.19+ | Vite 7 fails on older versions — CLAUDE.md Lesson 63 |
|
||||
| **PostgreSQL** | 16 | `gen_random_uuid()` + `jsonb` + RLS are all leaned on |
|
||||
| **Docker + Docker Compose** | recent | Only if you are running Postgres and/or backend as containers |
|
||||
| **Git** | recent | |
|
||||
|
||||
| Service | URL |
|
||||
Optional but recommended:
|
||||
|
||||
| Tool | Why |
|
||||
|---|---|
|
||||
| VS Code Server | https://code.resolutionflow.com |
|
||||
| Frontend (dev) | http://46.202.92.250:5173 |
|
||||
| Backend API | http://46.202.92.250:8000 |
|
||||
| API Docs | http://46.202.92.250:8000/docs |
|
||||
| **code-server** | Browser-based VS Code; how this project has historically been edited |
|
||||
| **`gh` CLI** | Mirror repo is on GitHub via Gitea; `gh` reads issues and PRs |
|
||||
| **bun** | Required for the gstack `/browse` + `/qa` skills (CLAUDE.md Lesson 82) |
|
||||
| **`npx gitnexus analyze`** | Code-graph for Phase 2+ work that touches `unified_chat_service` |
|
||||
| **Claude Code CLI** | If you want to run Claude Code locally on the host |
|
||||
|
||||
## Docker Layout
|
||||
---
|
||||
|
||||
## 2. Architectural shape
|
||||
|
||||
The project is three services plus your editor. Keep these facts in mind regardless of topology:
|
||||
|
||||
```
|
||||
/docker/
|
||||
├── traefik/
|
||||
│ ├── docker-compose.yml → Traefik reverse proxy
|
||||
│ └── .env → ACME_EMAIL for Let's Encrypt
|
||||
└── vscode/
|
||||
├── docker-compose.yml → VS Code Server
|
||||
└── .env → CODE_PASSWORD
|
||||
Your browser
|
||||
├─► code-server (editor, optional — usually port 8080 or behind TLS)
|
||||
├─► frontend (Vite) (dev server, port 5173)
|
||||
└─► backend (FastAPI) (dev server, port 8000)
|
||||
│
|
||||
└─► PostgreSQL (port 5432)
|
||||
```
|
||||
|
||||
Project lives inside the VS Code Server Docker volume:
|
||||
**The frontend calls the backend by URL at runtime.** The frontend does not proxy through the backend. Whatever URL your browser uses to reach the backend is what `VITE_API_URL` must be set to, **baked in at build time**. Changing `VITE_API_URL` requires rebuilding the frontend.
|
||||
|
||||
**The backend calls the database by URL at runtime.** The URL depends on where Postgres is relative to the backend — Docker service name if both are in the same compose network, `localhost` if Postgres is native on the same host, or a DNS name if they're in separate containers/VMs.
|
||||
|
||||
**CORS is configured explicitly.** The backend's `CORS_ORIGINS` list must include every origin your browser will use to reach the frontend. A missing origin shows up as failed preflight requests.
|
||||
|
||||
---
|
||||
|
||||
## 3. Topology choices — pick one before you start
|
||||
|
||||
The project is agnostic to topology, but each shape has different setup steps.
|
||||
|
||||
### Option A — all-in-one LXC/VM/host (simplest)
|
||||
|
||||
Postgres, backend, and frontend all run on one Linux host. code-server runs on the same host or a sibling. No Docker required. Best for a single-developer Proxmox LXC.
|
||||
|
||||
### Option B — Docker Compose on one host
|
||||
|
||||
Postgres, backend, and frontend run as Docker containers on one host. code-server runs outside the compose network (on the host or in another container). This is how the old Hostinger VPS was configured. Best if you want reproducible container images.
|
||||
|
||||
### Option C — split services across containers/VMs
|
||||
|
||||
Postgres in one container/VM, backend and frontend in another, code-server in a third. Most complex; requires explicit networking between them. Use only if you have a specific reason.
|
||||
|
||||
**Pick one and stick with it for the entire setup.** Mixing Options A and B halfway through is where setup runs off the rails.
|
||||
|
||||
---
|
||||
|
||||
## 4. Per-host configuration
|
||||
|
||||
These values are specific to your host. Fill them in once and reference them by name throughout the rest of the doc.
|
||||
|
||||
```
|
||||
/var/lib/docker/volumes/vscode_vscode-data/_data/resolutionflow/
|
||||
DEV_HOST = <hostname or IP your browser uses, e.g. dev.internal, 10.0.0.42>
|
||||
DEV_HOST_SCHEME = <http or https; http is fine for internal dev, https if behind a TLS proxy>
|
||||
FRONTEND_PORT = 5173
|
||||
BACKEND_PORT = 8000
|
||||
POSTGRES_PORT = 5433 # host-side port. 5433 is the recommended default on any shared host to avoid collision with a host-level Postgres. The container's internal port stays 5432.
|
||||
POSTGRES_DB_NAME = resolutionflow
|
||||
POSTGRES_USER = postgres
|
||||
POSTGRES_PASSWORD = <local-dev-password; anything, this is not prod>
|
||||
SECRET_KEY = <openssl rand -hex 32 — generate fresh per host, do not reuse>
|
||||
ANTHROPIC_API_KEY = <from https://console.anthropic.com>
|
||||
GOOGLE_AI_API_KEY = <optional, only if using Gemini as a fallback>
|
||||
```
|
||||
|
||||
## VS Code Server
|
||||
Store these somewhere you can copy from during setup. Do not commit them.
|
||||
|
||||
- **Container user:** `coder` (UID 1000)
|
||||
- **Home directory:** `/home/coder`
|
||||
- **Project location:** `/home/coder/resolutionflow`
|
||||
- **Host volume path:** `/var/lib/docker/volumes/vscode_vscode-data/_data`
|
||||
- **Access URL:** `https://code.resolutionflow.com`
|
||||
- **HTTPS:** Auto-provisioned via Traefik + Let's Encrypt
|
||||
> **Naming note:** the canonical database name is `resolutionflow`. If you see `patherly` in a config file, that's drift from an earlier rename and is being swept in a separate commit — use `resolutionflow`. CLAUDE.md tracks the live-code files that still reference `patherly`.
|
||||
|
||||
### Compose File Location
|
||||
`/docker/vscode/docker-compose.yml`
|
||||
---
|
||||
|
||||
## Traefik
|
||||
## 5. Setup procedure
|
||||
|
||||
Handles reverse proxying and automatic SSL for all services. HTTP automatically redirects to HTTPS.
|
||||
Run these in order. Stop at the first failure and investigate.
|
||||
|
||||
### Adding A New Service Behind Traefik
|
||||
|
||||
Add these labels to any new Docker service:
|
||||
|
||||
```yaml
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
- "traefik.http.routers.<n>.rule=Host(`subdomain.resolutionflow.com`)"
|
||||
- "traefik.http.routers.<n>.entrypoints=websecure"
|
||||
- "traefik.http.routers.<n>.tls.certresolver=letsencrypt"
|
||||
- "traefik.http.services.<n>.loadbalancer.server.port=<port>"
|
||||
```
|
||||
|
||||
Also create an A record in DNS pointing the subdomain to `46.202.92.250`.
|
||||
|
||||
## ResolutionFlow Dev Stack
|
||||
|
||||
### Important: No Docker Inside VS Code Container
|
||||
|
||||
The VS Code Server container does NOT have Docker. All `docker compose` commands must be run via SSH as root on the host.
|
||||
|
||||
### Environment Files
|
||||
|
||||
| File | Purpose |
|
||||
|---|---|
|
||||
| `.env` | Root — Docker Compose interpolation (`SECRET_KEY`, `ANTHROPIC_API_KEY`, `GOOGLE_AI_API_KEY`, `POSTGRES_PORT`) |
|
||||
| `backend/.env` | Backend source of truth — all FastAPI settings, API keys, DB URLs, CORS |
|
||||
| `frontend/.env` | Frontend — `VITE_API_URL` pointing to backend |
|
||||
|
||||
### Critical Remote Access Config
|
||||
|
||||
**`frontend/.env`:**
|
||||
```
|
||||
VITE_API_URL=http://46.202.92.250:8000
|
||||
```
|
||||
|
||||
**`backend/.env`:**
|
||||
```
|
||||
CORS_ORIGINS=["http://localhost:3000","http://localhost:5173","http://127.0.0.1:3000","http://127.0.0.1:5173","http://46.202.92.250:5173","http://46.202.92.250:3000","https://resolutionflow.com","https://www.resolutionflow.com"]
|
||||
FRONTEND_URL=http://46.202.92.250:5173
|
||||
DATABASE_URL=postgresql+asyncpg://postgres:postgres@db:5432/resolutionflow
|
||||
DATABASE_URL_SYNC=postgresql://postgres:postgres@db:5432/resolutionflow
|
||||
```
|
||||
|
||||
Note: `DATABASE_URL` uses `@db:5432` (Docker service name), not `@localhost`.
|
||||
|
||||
**`docker-compose.dev.yml`:**
|
||||
```yaml
|
||||
- VITE_API_URL=http://46.202.92.250:8000
|
||||
```
|
||||
|
||||
### Starting the Dev Environment
|
||||
|
||||
SSH into host as root:
|
||||
### 5.1 Install system dependencies
|
||||
|
||||
```bash
|
||||
cd /var/lib/docker/volumes/vscode_vscode-data/_data/resolutionflow
|
||||
docker compose -f docker-compose.dev.yml up -d
|
||||
# Ubuntu / Debian
|
||||
sudo apt update && sudo apt install -y \
|
||||
git curl build-essential \
|
||||
python3.11 python3.11-venv python3-pip \
|
||||
postgresql-client # not the server — only if running Postgres natively
|
||||
|
||||
# Node 20 via nvm (survives container rebuilds if stored in a volume)
|
||||
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
|
||||
export NVM_DIR="$HOME/.nvm" && source "$NVM_DIR/nvm.sh"
|
||||
nvm install 20
|
||||
nvm alias default 20
|
||||
```
|
||||
|
||||
### Running Migrations (Fresh Database)
|
||||
For Option B (Docker Compose), also:
|
||||
|
||||
```bash
|
||||
cd /var/lib/docker/volumes/vscode_vscode-data/_data/resolutionflow
|
||||
curl -fsSL https://get.docker.com | sh
|
||||
sudo usermod -aG docker $USER # log out and back in for this to take effect
|
||||
```
|
||||
|
||||
### 5.2 Clone the repo
|
||||
|
||||
```bash
|
||||
git clone https://gitea.resolutionflow.com/chihlasm/resolutionflow.git
|
||||
# or the GitHub mirror:
|
||||
# git clone https://github.com/chihlasm/resolutionflow.git
|
||||
cd resolutionflow
|
||||
|
||||
# Check out the working branch if you're continuing mid-migration.
|
||||
git fetch origin
|
||||
git checkout feat/flowpilot-migration
|
||||
```
|
||||
|
||||
### 5.3 Start PostgreSQL
|
||||
|
||||
**Option A (native Postgres on the host):**
|
||||
|
||||
```bash
|
||||
sudo apt install -y postgresql-16
|
||||
sudo -u postgres psql -c "CREATE DATABASE resolutionflow;"
|
||||
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
|
||||
# Adjust pg_hba.conf if you need non-local connections.
|
||||
```
|
||||
|
||||
**Option B (Postgres via Docker Compose):** The repo has a `docker-compose.dev.yml` at the root. Check its Postgres service for the container name, port mapping, and volume. The local compose defaults use container name `resolutionflow_postgres`, database `resolutionflow`, and host-side port `5433` (mapped to the container's internal `5432`) — see CLAUDE.md Lesson 65. The host-side `5433` is the recommended default on any shared host: it keeps the port free for a host-level Postgres if you ever need one. The compose file also defines explicit `command:` directives on both `backend` and `frontend` to force `--host 0.0.0.0`, and expects the caller to pass `REPO_ROOT` (see 5.4) for bind-mount resolution. Confirm what the compose file actually says on your branch before trusting these values.
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.dev.yml up -d db
|
||||
docker compose -f docker-compose.dev.yml logs db # wait for "ready to accept connections"
|
||||
```
|
||||
|
||||
**Verify:**
|
||||
|
||||
```bash
|
||||
# From the host (Option A) or the backend container/LXC (Option B):
|
||||
psql -h <db-host> -p <POSTGRES_PORT> -U postgres -d resolutionflow -c "SELECT now();"
|
||||
```
|
||||
|
||||
### 5.4 Write the `.env` files
|
||||
|
||||
The repo expects three env files. Create each one:
|
||||
|
||||
**`backend/.env`** — backend source of truth:
|
||||
|
||||
```bash
|
||||
APP_NAME=ResolutionFlow
|
||||
DEBUG=true
|
||||
|
||||
# DB URLs — `<db-host>` is `localhost` for Option A, the Docker service name
|
||||
# (e.g. `db`) for Option B, or the DB container/VM hostname for Option C.
|
||||
DATABASE_URL=postgresql+asyncpg://postgres:postgres@<db-host>:<POSTGRES_PORT>/resolutionflow
|
||||
DATABASE_URL_SYNC=postgresql://postgres:postgres@<db-host>:<POSTGRES_PORT>/resolutionflow
|
||||
|
||||
# Auth
|
||||
SECRET_KEY=<SECRET_KEY>
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES=5
|
||||
REFRESH_TOKEN_EXPIRE_DAYS=7
|
||||
REQUIRE_INVITE_CODE=true
|
||||
|
||||
# AI providers
|
||||
AI_PROVIDER=anthropic
|
||||
ANTHROPIC_API_KEY=<ANTHROPIC_API_KEY>
|
||||
GOOGLE_AI_API_KEY=<GOOGLE_AI_API_KEY or leave unset>
|
||||
|
||||
# FlowPilot MCP telemetry — leave on so the Phase 0.5 baseline data keeps accruing
|
||||
ENABLE_MCP_MICROSOFT_LEARN=true
|
||||
|
||||
# CORS + frontend URL
|
||||
FRONTEND_URL=<DEV_HOST_SCHEME>://<DEV_HOST>:<FRONTEND_PORT>
|
||||
CORS_ORIGINS=["http://localhost:5173","http://127.0.0.1:5173","<DEV_HOST_SCHEME>://<DEV_HOST>:<FRONTEND_PORT>"]
|
||||
```
|
||||
|
||||
**`frontend/.env.local`** — frontend build-time config:
|
||||
|
||||
```bash
|
||||
VITE_API_URL=<DEV_HOST_SCHEME>://<DEV_HOST>:<BACKEND_PORT>
|
||||
```
|
||||
|
||||
Optional PostHog (CLAUDE.md Lesson 64 — enables product analytics locally):
|
||||
|
||||
```bash
|
||||
VITE_PUBLIC_POSTHOG_KEY=<from PostHog project settings>
|
||||
VITE_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
|
||||
```
|
||||
|
||||
**Repo root `.env`** — only needed for Option B (Docker Compose interpolation):
|
||||
|
||||
```bash
|
||||
SECRET_KEY=<SECRET_KEY>
|
||||
ANTHROPIC_API_KEY=<ANTHROPIC_API_KEY>
|
||||
GOOGLE_AI_API_KEY=<GOOGLE_AI_API_KEY or leave unset>
|
||||
POSTGRES_PORT=<POSTGRES_PORT>
|
||||
# Absolute host-side path to the repo root. REQUIRED whenever docker-compose is
|
||||
# invoked from inside a container (e.g. a code-server container with the host
|
||||
# Docker socket mounted in). Without it, the bind mounts in
|
||||
# docker-compose.dev.yml (`${REPO_ROOT}/backend:/app`, `${REPO_ROOT}/frontend:/app`)
|
||||
# resolve against the CLI's CWD — a path the host daemon cannot see — and
|
||||
# Docker silently creates empty directories there instead of mounting the code.
|
||||
# If you run docker compose directly on the host shell, you can set this to `.`
|
||||
# or the absolute path of the repo; being explicit is safer either way.
|
||||
REPO_ROOT=/absolute/path/to/resolutionflow
|
||||
```
|
||||
|
||||
> **Never commit any `.env` file.** The `.gitignore` already covers this.
|
||||
|
||||
### 5.5 Run the backend setup
|
||||
|
||||
**Option A (native):**
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
python3.11 -m venv venv
|
||||
source venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
|
||||
# Migrate the DB to head.
|
||||
alembic upgrade head
|
||||
```
|
||||
|
||||
**Option B (Docker):**
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.dev.yml up -d backend
|
||||
docker compose -f docker-compose.dev.yml run --rm backend alembic upgrade head
|
||||
```
|
||||
|
||||
### Seeding Test Users
|
||||
**Expected alembic head** (as of `feat/flowpilot-migration`): `f07010f17b01`. If `alembic current` shows anything else after `upgrade head`, something has gone wrong — stop and investigate.
|
||||
|
||||
### 5.6 Seed test users
|
||||
|
||||
```bash
|
||||
# Option A
|
||||
cd backend && source venv/bin/activate
|
||||
python -m scripts.seed_test_users
|
||||
|
||||
# Option B
|
||||
docker exec resolutionflow_backend python -m scripts.seed_test_users
|
||||
```
|
||||
|
||||
Test accounts (password: `TestPass123!`):
|
||||
Test users (all share password `TestPass123!`):
|
||||
|
||||
| Email | Role | Plan |
|
||||
|---|---|---|
|
||||
| admin@resolutionflow.example.com | Owner | Team |
|
||||
| pro@resolutionflow.example.com | Owner | Pro |
|
||||
| teamadmin@resolutionflow.example.com | Owner | Team |
|
||||
| engineer@resolutionflow.example.com | Engineer | Shared |
|
||||
| Email | Role |
|
||||
|---|---|
|
||||
| `admin@resolutionflow.example.com` | super admin |
|
||||
| `teamadmin@resolutionflow.example.com` | team admin |
|
||||
| `engineer@resolutionflow.example.com` | engineer |
|
||||
| `pro@resolutionflow.example.com` | solo pro |
|
||||
|
||||
### Rebuilding After Config Changes
|
||||
### 5.7 Run the backend
|
||||
|
||||
**Option A:**
|
||||
|
||||
```bash
|
||||
cd backend && source venv/bin/activate
|
||||
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
|
||||
```
|
||||
|
||||
**Option B:** Already running from `docker compose up -d backend`. Tail logs:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.dev.yml logs -f backend
|
||||
```
|
||||
|
||||
**Verify:** `curl <DEV_HOST_SCHEME>://<DEV_HOST>:<BACKEND_PORT>/api/docs` — OpenAPI docs page loads.
|
||||
|
||||
### 5.8 Run the frontend
|
||||
|
||||
**Option A:**
|
||||
|
||||
```bash
|
||||
cd frontend
|
||||
npm install
|
||||
npm run dev -- --host 0.0.0.0 --port 5173
|
||||
```
|
||||
|
||||
**Option B:**
|
||||
|
||||
**Frontend** (Vite bakes env vars at build time — requires rebuild):
|
||||
```bash
|
||||
cd /var/lib/docker/volumes/vscode_vscode-data/_data/resolutionflow
|
||||
docker compose -f docker-compose.dev.yml up -d --build frontend
|
||||
```
|
||||
|
||||
**Backend** (restart only):
|
||||
**Verify:** Open `<DEV_HOST_SCHEME>://<DEV_HOST>:<FRONTEND_PORT>` in your browser. Log in with one of the test users. Navigate to `/pilot` — the FlowPilot session page should render.
|
||||
|
||||
---
|
||||
|
||||
## 6. Verification — proof the env actually works
|
||||
|
||||
Run these after setup. Every item has a concrete expected outcome.
|
||||
|
||||
### 6.1 Database schema is at the right version
|
||||
|
||||
```bash
|
||||
# Option A
|
||||
cd backend && source venv/bin/activate && alembic current
|
||||
# Option B
|
||||
docker compose -f docker-compose.dev.yml run --rm backend alembic current
|
||||
```
|
||||
|
||||
Expected: `f07010f17b01 (head)` on the `feat/flowpilot-migration` branch. On `main`, expected: `074 (head)`.
|
||||
|
||||
### 6.2 Alembic reversibility
|
||||
|
||||
```bash
|
||||
alembic downgrade -1 # should complete cleanly
|
||||
alembic upgrade head # should return to f07010f17b01
|
||||
```
|
||||
|
||||
If either step fails, the migration has a bug and Phase 2 cannot start.
|
||||
|
||||
### 6.3 Prompt-cache hit verification (the deferred Phase 0 TODO)
|
||||
|
||||
`backend/app/core/ai_provider.py` module docstring has a `TODO(phase0-verify)` note describing this. Procedure:
|
||||
|
||||
1. Confirm `AI_PROVIDER=anthropic` and `ANTHROPIC_API_KEY` is set in `backend/.env`.
|
||||
2. Start the backend with log level INFO or lower.
|
||||
3. In the UI, open `/pilot` and send a chat message. Wait a few seconds for the response.
|
||||
4. Send a second chat message in the same session, within 5 minutes of the first.
|
||||
5. In backend logs, grep for lines containing `anthropic.cache`:
|
||||
|
||||
```bash
|
||||
# Option A
|
||||
grep 'anthropic.cache' <log-path>
|
||||
# Option B
|
||||
docker compose -f docker-compose.dev.yml logs backend | grep 'anthropic.cache'
|
||||
```
|
||||
|
||||
6. Expected: two `anthropic.cache` log events. First has `cache_creation_input_tokens > 0`. Second has `cache_read_input_tokens > 0`.
|
||||
7. If the second shows zero reads, inspect the prompt prefix for silent invalidators (timestamps, unsorted JSON keys, varying tool list ordering). Fix before proceeding with any Phase 2 work.
|
||||
|
||||
### 6.4 Frontend build is TypeScript-clean
|
||||
|
||||
```bash
|
||||
cd frontend
|
||||
npx tsc -b # no errors
|
||||
npm run build # no errors
|
||||
```
|
||||
|
||||
CLAUDE.md Lesson 105 notes that `npm run build` may fail with an `EACCES` on `dist/` inside code-server — that is a Docker filesystem permission issue, not a real build error. Use `npx tsc -b` to verify TypeScript cleanliness in that case.
|
||||
|
||||
### 6.5 `/assistant` → `/pilot` redirect
|
||||
|
||||
Open `<DEV_HOST_SCHEME>://<DEV_HOST>:<FRONTEND_PORT>/assistant/<some-real-session-id>` in the browser. Expected: URL changes to `/pilot/<that-id>`; the FlowPilot session page renders. Bare `/assistant` redirects to bare `/pilot`.
|
||||
|
||||
### 6.6 Dispatcher de-branching
|
||||
|
||||
Navigate to the dashboard. Click a session in `ActiveFlowPilotSessions` or `RecentFlowPilotSessions`. Expected: routes to `/pilot/:id` regardless of the session's `session_type` value. (Check the browser URL bar.)
|
||||
|
||||
### 6.7 CORS
|
||||
|
||||
Open the browser DevTools Network tab, navigate to any backend-hitting page. Expected: no CORS errors. If you see "blocked by CORS policy," the missing origin needs adding to `backend/.env`'s `CORS_ORIGINS`.
|
||||
|
||||
---
|
||||
|
||||
## 7. Runbook
|
||||
|
||||
Day-to-day commands after setup is complete.
|
||||
|
||||
### Restart services
|
||||
|
||||
```bash
|
||||
# Option A
|
||||
# backend — Ctrl-C and re-run uvicorn
|
||||
# frontend — Ctrl-C and re-run npm run dev
|
||||
|
||||
# Option B
|
||||
docker compose -f docker-compose.dev.yml restart backend
|
||||
docker compose -f docker-compose.dev.yml up -d --build frontend # rebuild required if VITE_* changed
|
||||
docker compose -f docker-compose.dev.yml down && docker compose -f docker-compose.dev.yml up -d # full restart
|
||||
```
|
||||
|
||||
**Full restart:**
|
||||
```bash
|
||||
docker compose -f docker-compose.dev.yml down
|
||||
docker compose -f docker-compose.dev.yml up -d
|
||||
```
|
||||
|
||||
## Installed Tools (Inside VS Code Server Container)
|
||||
|
||||
Installed in `/home/coder` — persists via Docker volume:
|
||||
|
||||
- **nvm** — Node version manager
|
||||
- **Node.js 20.x** — via nvm, default alias set
|
||||
- **npm** — latest
|
||||
- **GitHub CLI (gh)** — authenticated via personal access token
|
||||
- **Claude Code CLI** — `@anthropic-ai/claude-code` (global npm)
|
||||
|
||||
### Permanent Tool Installs
|
||||
|
||||
Tools installed via `apt` inside the container do NOT survive container rebuilds. To add permanently, modify the VS Code Server Docker image and rebuild.
|
||||
|
||||
Temporary (session only):
|
||||
```bash
|
||||
sudo apt update && sudo apt install -y <tool>
|
||||
```
|
||||
|
||||
## SSH Access
|
||||
### Apply a new migration
|
||||
|
||||
```bash
|
||||
ssh root@46.202.92.250
|
||||
# Option A
|
||||
cd backend && source venv/bin/activate && alembic upgrade head
|
||||
# Option B
|
||||
docker compose -f docker-compose.dev.yml run --rm backend alembic upgrade head
|
||||
```
|
||||
|
||||
Key auth configured via `~/.ssh/authorized_keys` on host.
|
||||
### Create a new migration
|
||||
|
||||
## Useful Commands
|
||||
|
||||
### Check all running containers
|
||||
```bash
|
||||
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
|
||||
# Option A
|
||||
cd backend && source venv/bin/activate
|
||||
alembic revision -m "short description" # manual, preferred per CLAUDE.md Lesson 77
|
||||
# OR
|
||||
alembic revision --autogenerate -m "description" # pulls in drift; review carefully
|
||||
```
|
||||
|
||||
### View container logs
|
||||
Never pass `--rev-id` — let Alembic generate the hex hash.
|
||||
|
||||
### Inspect the database
|
||||
|
||||
```bash
|
||||
docker logs <container_name> --tail 30 -f
|
||||
# Option A (native Postgres)
|
||||
psql -h localhost -p 5432 -U postgres -d resolutionflow
|
||||
|
||||
# Option B (Docker)
|
||||
docker exec -it resolutionflow_postgres psql -U postgres -d resolutionflow
|
||||
```
|
||||
|
||||
### Restart VS Code Server
|
||||
### Run tests
|
||||
|
||||
```bash
|
||||
cd /docker/vscode && docker compose restart
|
||||
# Option A
|
||||
cd backend && source venv/bin/activate
|
||||
pytest --override-ini="addopts="
|
||||
|
||||
# Option B
|
||||
docker compose -f docker-compose.dev.yml run --rm backend pytest --override-ini="addopts="
|
||||
```
|
||||
|
||||
### Restart Traefik
|
||||
First time only, create the test database:
|
||||
|
||||
```bash
|
||||
cd /docker/traefik && docker compose restart
|
||||
# Option A
|
||||
sudo -u postgres psql -c "CREATE DATABASE resolutionflow_test;"
|
||||
|
||||
# Option B
|
||||
docker exec -it resolutionflow_postgres psql -U postgres -c "CREATE DATABASE resolutionflow_test;"
|
||||
```
|
||||
|
||||
### Restart dev stack
|
||||
### View backend logs
|
||||
|
||||
```bash
|
||||
cd /var/lib/docker/volumes/vscode_vscode-data/_data/resolutionflow
|
||||
docker compose -f docker-compose.dev.yml down
|
||||
docker compose -f docker-compose.dev.yml up -d
|
||||
# Option A: wherever you ran uvicorn
|
||||
# Option B
|
||||
docker compose -f docker-compose.dev.yml logs -f --tail=100 backend
|
||||
```
|
||||
|
||||
### Check swap
|
||||
Structured events to grep for:
|
||||
- `anthropic.cache` — prompt-cache hit/creation telemetry (Phase 0.1)
|
||||
- `mcp.turn` — per-turn MCP availability/invocation (Phase 0.5)
|
||||
- `mcp.fallback` — MCP silent-retry fallback fired (Phase 0.5)
|
||||
|
||||
---
|
||||
|
||||
## 8. Troubleshooting
|
||||
|
||||
### CORS errors in the browser
|
||||
|
||||
The backend did not accept the origin your browser used. Check `backend/.env`'s `CORS_ORIGINS` — it must include the exact scheme + host + port the browser sent. Restart the backend after editing.
|
||||
|
||||
### `VITE_API_URL` points at the wrong place
|
||||
|
||||
The frontend was built with a stale value. Rebuild the frontend. Option B: `docker compose up -d --build frontend`. Option A: restart `npm run dev`.
|
||||
|
||||
### `alembic upgrade head` fails with "target database is not up to date"
|
||||
|
||||
Your DB migration chain is out of sync with the code. On a dev box, the safe recovery is to drop the DB and re-migrate from scratch:
|
||||
|
||||
```bash
|
||||
free -h && swapon --show
|
||||
# Option A
|
||||
sudo -u postgres psql -c "DROP DATABASE resolutionflow;" -c "CREATE DATABASE resolutionflow;"
|
||||
cd backend && source venv/bin/activate && alembic upgrade head
|
||||
|
||||
# Option B
|
||||
docker exec resolutionflow_postgres psql -U postgres -c "DROP DATABASE resolutionflow;" -c "CREATE DATABASE resolutionflow;"
|
||||
docker compose -f docker-compose.dev.yml run --rm backend alembic upgrade head
|
||||
```
|
||||
|
||||
### Check disk
|
||||
Only do this on a dev box — it destroys all local data.
|
||||
|
||||
### `alembic heads` shows more than one head
|
||||
|
||||
Only on a local branch that has diverged from `origin/main`. Production `main` has a single head. If this happens on a fresh clone, one of your local migration files has the wrong `down_revision`. Inspect each file's `down_revision` and reconnect the chain.
|
||||
|
||||
### Frontend build fails with "EACCES: permission denied" on `dist/`
|
||||
|
||||
Filesystem permission issue inside the code-server container (CLAUDE.md Lesson 105). TypeScript compilation itself completes — use `npx tsc -b` to verify cleanliness without needing to write to `dist/`.
|
||||
|
||||
### Backend/frontend containers start but `/app` is empty (no code mounted)
|
||||
|
||||
Almost always a `REPO_ROOT` problem. `docker-compose.dev.yml` uses `${REPO_ROOT}/backend:/app` and `${REPO_ROOT}/frontend:/app` bind mounts. If `REPO_ROOT` is unset, or set to a path that doesn't exist *on the Docker host* (not inside the code-server container), Docker silently creates an empty directory at that path and mounts it — the containers come up but have no source code. Symptom: backend returns import errors, or frontend serves a default Vite page. Fix: set `REPO_ROOT` in the repo-root `.env` to the absolute host-side path to the repo, then `docker compose down && docker compose up -d`. See 5.4 for the full note. This matters specifically when `docker compose` is invoked from inside a container (e.g. code-server with the host Docker socket mounted) — the CLI's CWD is container-local but the daemon resolves paths against the host filesystem.
|
||||
|
||||
### Frontend shows "Blocked request. This host is not allowed" in the browser
|
||||
|
||||
Vite 5+ ships DNS-rebinding protection that rejects any `Host:` header not in `server.allowedHosts`. The browser's hostname must be in that list. Edit `frontend/vite.config.ts` — the `server.allowedHosts` array should include every hostname you reach the dev server from (e.g. `'docker-01'`, `'localhost'`, `.ts.net` as a wildcard for Tailscale MagicDNS). Restart the Vite dev server (for Option B: `docker compose restart frontend`). This is unrelated to CORS — Vite blocks the request before any app code runs.
|
||||
|
||||
### `docker` command not found inside code-server
|
||||
|
||||
If your code-server is itself inside a container, Docker is probably not exposed to it. CLAUDE.md Lesson 103 was written for this case on the old VPS. On Proxmox, the fix depends on topology — either SSH to the host to run Docker commands, or mount the host's Docker socket into the code-server container.
|
||||
|
||||
### Backend returns 500 with `InsufficientPrivilegeError: new row violates row-level security policy`
|
||||
|
||||
RLS is enabled on a table your code wrote to without the right `account_id`. CLAUDE.md Lessons 107, 108, 110 cover this family of bugs. The fix is always at the service layer: make sure every model creation passes `account_id=` explicitly, and that startup routines that touch tenant-isolated tables use `_admin_session_factory()` rather than `get_db()`.
|
||||
|
||||
### Anthropic cache reads are zero on the second turn
|
||||
|
||||
Something in the cached prefix is changing between turns. Inspect the system-block list and the first N history messages for timestamps, `datetime.now()`, unsorted dict keys in JSON prompts, or varying tool-list order. The `anthropic.cache` telemetry shows exactly how many tokens were read vs created — use it to narrow down the invalidator.
|
||||
|
||||
---
|
||||
|
||||
## 9. Security posture for dev environments
|
||||
|
||||
This doc is about dev, not production. But:
|
||||
|
||||
- Never commit `.env` files. The `.gitignore` covers this.
|
||||
- `SECRET_KEY` should be generated per-host, not reused across environments.
|
||||
- `ANTHROPIC_API_KEY` is billable — rotate if leaked into logs or chat.
|
||||
- Postgres on a dev host should not be exposed to the internet. Bind it to `127.0.0.1` or to a private network interface only.
|
||||
- If you expose the frontend or backend publicly (for teammates to test against), put it behind TLS with a real certificate. Do not let dev credentials travel over plain HTTP on the public internet.
|
||||
|
||||
---
|
||||
|
||||
## 10. What's not in this doc
|
||||
|
||||
- **Production deployment.** This is a dev-env doc. Production lives on Railway — see `CLAUDE.md`'s Deployment section.
|
||||
- **How to set up Traefik or any particular reverse proxy.** Whichever proxy you use is your choice; the dev stack just needs something that routes `<host>:5173` and `<host>:8000` to the right services. **Direct port exposure over a private network** (Tailscale, WireGuard, a VPN, or a LAN behind a firewall) is a fully supported option for dev and is what the homelab reference topology in Section 11 uses — no reverse proxy, no TLS, just `http://<host>:5173` and `http://<host>:8000` reachable only from the private network. That's a perfectly reasonable choice; it's just not the only one.
|
||||
- **How to configure code-server itself.** Install it however you prefer (native, Docker, LXC); point it at the repo, and the rest of this doc applies.
|
||||
- **Where to host the Proxmox instance.** Up to you.
|
||||
|
||||
If something in this doc turns out to be wrong on your host, fix the doc. This is a living document — the whole point of rewriting it from the Hostinger-specific version was to make it survive host changes.
|
||||
|
||||
---
|
||||
|
||||
## 11. Reference topology: homelab Proxmox + code-server (Option B)
|
||||
|
||||
This section documents the first concrete host instantiation since the April 2026 host-agnostic rewrite. It's a worked example, not the canonical topology — Section 3's Option A/B/C framing still stands. If your setup looks different, follow Sections 1–10 and ignore this appendix.
|
||||
|
||||
### 11.1 Host
|
||||
|
||||
- **Hypervisor:** Proxmox (homelab).
|
||||
- **VM:** `docker-01`, Debian 13, running Docker Engine + Docker Compose natively.
|
||||
- **Tailscale IP:** `100.64.78.44`. MagicDNS hostname: `docker-01` (and the full `.ts.net` FQDN).
|
||||
- **code-server:** runs on the same VM in its own container, with the host's Docker socket mounted in so it can drive `docker compose`. Its workspace bind-mounts the repo at `/opt/docker/code-server/workspace/resolutionflow`.
|
||||
|
||||
This is a concrete instance of Option B from Section 3: Postgres, backend, and frontend all run as containers from `docker-compose.dev.yml`; the editor lives outside that compose network.
|
||||
|
||||
### 11.2 Access pattern — direct port over Tailscale, no reverse proxy
|
||||
|
||||
The browser reaches the dev stack directly:
|
||||
|
||||
- Frontend: `http://docker-01:5173`
|
||||
- Backend: `http://docker-01:8000`
|
||||
- Backend API docs: `http://docker-01:8000/api/docs`
|
||||
|
||||
There is **no Caddy, no Traefik, no nginx, no TLS, no basic auth** in front of either service. The tailnet provides the wire encryption and access control — only devices on the tailnet can resolve `docker-01` or reach `100.64.78.44`, and Tailscale ACLs decide which of those devices are allowed to connect.
|
||||
|
||||
Why this choice:
|
||||
|
||||
- **Zero routing config to maintain.** There is no proxy rulebook to keep in sync with new services. Add a container, expose a port, you're done.
|
||||
- **Backend-to-backend services stay private.** Redis, Celery workers, the planned ConnectWise proxy, the MCP server — none of them need to be reachable from the browser, so none of them need proxy rules. They stay inside the `resolutionflow` Docker network and talk by service name. The proxy would only ever have carried frontend and backend traffic, so the proxy's value was small relative to its maintenance cost.
|
||||
- **Debuggability.** `curl http://docker-01:8000/api/docs` from any tailnet device works without auth headers, TLS handshakes, or DNS shenanigans.
|
||||
|
||||
Tradeoff: **this only works because every client device is on the tailnet.** If someone needed to test from a non-tailnet device, they'd either join the tailnet or we'd need to front the stack with a proxy. For the current single-developer setup, the tailnet-only assumption holds.
|
||||
|
||||
### 11.3 Per-host config values (as actually configured on `docker-01`)
|
||||
|
||||
Plugging these into Section 4's template:
|
||||
|
||||
```
|
||||
DEV_HOST = docker-01
|
||||
DEV_HOST_SCHEME = http
|
||||
FRONTEND_PORT = 5173
|
||||
BACKEND_PORT = 8000
|
||||
POSTGRES_PORT = 5433 # host-side; container-internal stays 5432
|
||||
POSTGRES_DB_NAME = resolutionflow
|
||||
POSTGRES_USER = postgres
|
||||
POSTGRES_PASSWORD = postgres # local-dev only
|
||||
SECRET_KEY = <generated per host; do not reuse>
|
||||
ANTHROPIC_API_KEY = <from console.anthropic.com>
|
||||
GOOGLE_AI_API_KEY = <unset; Anthropic is sole provider in dev>
|
||||
```
|
||||
|
||||
And the repo-root `.env` that `docker-compose.dev.yml` interpolates from:
|
||||
|
||||
```bash
|
||||
df -h
|
||||
SECRET_KEY=<redacted>
|
||||
ANTHROPIC_API_KEY=<redacted>
|
||||
POSTGRES_PORT=5433
|
||||
REPO_ROOT=/opt/docker/code-server/workspace/resolutionflow
|
||||
```
|
||||
|
||||
### Check memory + container usage
|
||||
### 11.4 Why `REPO_ROOT` is non-optional on this host
|
||||
|
||||
code-server runs inside a container. When you open a terminal in code-server and run `docker compose -f docker-compose.dev.yml up -d`, the Docker CLI talks to the *host* daemon via the mounted socket — but the CWD it reports (`/config/workspace/resolutionflow`) is a path that only exists inside the code-server container. The host daemon has never heard of it.
|
||||
|
||||
Relative bind mounts like `./backend:/app` therefore resolve against a path the host can't see, and Docker silently creates empty directories there rather than erroring out. The containers come up, but `/app` is empty.
|
||||
|
||||
`docker-compose.dev.yml` sidesteps this by using `${REPO_ROOT}/backend:/app` and `${REPO_ROOT}/frontend:/app`. `REPO_ROOT` must be set to the absolute path **on the host** (`/opt/docker/code-server/workspace/resolutionflow`), not the path inside the code-server container. Same contents, different mount point, different name.
|
||||
|
||||
If you ever run `docker compose` directly from a host shell (SSH'd into `docker-01`), set `REPO_ROOT` to `.` or the absolute host path. Being explicit is always safe; leaving it unset is the failure mode.
|
||||
|
||||
### 11.5 Vite `server.allowedHosts` — required for `docker-01` to resolve
|
||||
|
||||
Vite 5+ rejects any `Host:` header not in `server.allowedHosts` (DNS-rebinding protection). `frontend/vite.config.ts` has:
|
||||
|
||||
```ts
|
||||
server: {
|
||||
host: '0.0.0.0',
|
||||
allowedHosts: ['docker-01', '.ts.net', 'localhost'],
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
- `docker-01` — the MagicDNS short name the browser uses day-to-day.
|
||||
- `.ts.net` — wildcard for the full Tailscale MagicDNS FQDN, in case anyone uses it.
|
||||
- `localhost` — for the "am I serving anything at all" smoke-test from inside the container.
|
||||
|
||||
If you move this setup to a different host, add that host's hostname to `allowedHosts` or the browser will see "Blocked request. This host is not allowed." See Section 8's troubleshooting entry for the full symptom/fix.
|
||||
|
||||
### 11.6 CORS origins on this host
|
||||
|
||||
The `backend` service's `CORS_ORIGINS` environment variable is pinned in the compose file to:
|
||||
|
||||
```
|
||||
["http://localhost:5173","http://127.0.0.1:5173","http://docker-01:5173","http://100.64.78.44:5173"]
|
||||
```
|
||||
|
||||
The last two are what make browser calls from tailnet clients work — they cover both MagicDNS (`docker-01`) and the raw Tailscale IP. If you add a new hostname to reach the frontend from, also add the matching origin here and restart the backend.
|
||||
|
||||
### 11.7 Compose file shape (as of this writing)
|
||||
|
||||
`docker-compose.dev.yml` has been through a round of cleanup for this topology. Specifics worth knowing if you're comparing against older revisions of the file:
|
||||
|
||||
- **No Traefik labels.** They were removed — nothing in this topology uses Traefik.
|
||||
- **No Hostinger-VPS-era origins** in `CORS_ORIGINS`.
|
||||
- `Dockerfile.dev` for both `backend` and `frontend` is still the build source — this didn't change.
|
||||
- Explicit `command:` directives on both `backend` (`uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload`) and `frontend` (`npm run dev -- --host 0.0.0.0 --port 5173`) — this guarantees `--host 0.0.0.0` regardless of what's baked into the image, so the services listen on all interfaces and are reachable from outside the container.
|
||||
- `REPO_ROOT` is interpolated into both service volume mounts (see 11.4).
|
||||
|
||||
If you're adapting the file for a different host, the things most likely to need editing are `REPO_ROOT` (see 11.4), `CORS_ORIGINS` (see 11.6), `FRONTEND_URL`, `VITE_API_URL`, and `POSTGRES_PORT` if you want something other than `5433`.
|
||||
|
||||
### 11.8 End-to-end sanity check for this topology
|
||||
|
||||
From any device on the tailnet:
|
||||
|
||||
```bash
|
||||
free -h && docker stats --no-stream
|
||||
# Backend reachable
|
||||
curl -sSf http://docker-01:8000/api/docs >/dev/null && echo OK
|
||||
|
||||
# Frontend reachable
|
||||
curl -sSf http://docker-01:5173 >/dev/null && echo OK
|
||||
|
||||
# Alembic head matches the branch expectation
|
||||
docker exec resolutionflow_backend alembic current
|
||||
# expect f07010f17b01 on feat/flowpilot-migration, 074 on main
|
||||
|
||||
# Postgres is alive inside the compose network
|
||||
docker exec resolutionflow_postgres psql -U postgres -d resolutionflow -c "SELECT now();"
|
||||
```
|
||||
|
||||
## DNS Records (resolutionflow.com)
|
||||
|
||||
| Type | Name | Value | Purpose |
|
||||
|---|---|---|---|
|
||||
| A | code | 46.202.92.250 | VS Code Server |
|
||||
|
||||
## Security Notes
|
||||
|
||||
- UFW is inactive — Traefik and Docker manage port exposure
|
||||
- All public-facing services run through Traefik with valid HTTPS certs
|
||||
- PostgreSQL port 5432 is exposed on all interfaces — restrict if needed in production
|
||||
- Rotate API keys (Anthropic, Voyage) if ever exposed in logs or chat
|
||||
- Never commit `.env` files to Git
|
||||
|
||||
## VS Code Server Browser Tips
|
||||
|
||||
- **Command Palette:** `F1`
|
||||
- **Terminal:** Ctrl+`
|
||||
- **Rename file:** `F2`
|
||||
- **Go to definition:** `F12`
|
||||
- **Find references:** `Shift+F12`
|
||||
- **Context Menu:** `Alt + Right Click`
|
||||
All four passing = the dev environment is live end-to-end.
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
# Session Handoff — Design System v4 Migration
|
||||
|
||||
> **For the next Claude session:** Read this file completely, internalize the context, then delete it (`rm SESSION-HANDOFF.md`). This is a one-time context transfer.
|
||||
|
||||
---
|
||||
|
||||
## What Was Done This Session
|
||||
|
||||
### 1. FlowPilot Message Bar + AI Script Builder (MERGED to main)
|
||||
- PR #118 merged. Always-visible message bar in FlowPilot sessions, AI Script Builder at `/script-builder`, library reorg (My/Team Scripts tabs), FlowPilot-to-Script-Builder handoff, session abandon/close, unified session history.
|
||||
- Eng review completed: normalized `script_builder_messages` table, typed content helpers, 6 edge case tests.
|
||||
|
||||
### 2. Design System v4 Migration (PR #119, open, branch: `refactor/design-system-v4`)
|
||||
- Complete frontend redesign from glassmorphism to flat dark theme (Sentry/PostHog-inspired)
|
||||
- **CSS Foundation:** New color tokens in `index.css`, all via CSS custom properties. Light mode ready (just needs `.light` class values).
|
||||
- **Icon Rail Sidebar:** 72px rail with 5 grouped icons (Home, Work, Knowledge, Insights, Help). Full-height resizable drawer on hover. Pin-to-expand to 260px. Mobile hamburger overlay.
|
||||
- **Component Sweep:** ~200 files migrated. All hardcoded hex replaced with semantic Tailwind tokens (bg-card, text-foreground, border-border, etc.).
|
||||
- **Landing Page:** Flat surfaces, no glow, solid buttons.
|
||||
- **Interactive Shadows:** Dark-mode-aware — elevated surfaces + faint cyan accent glow (black shadows invisible on dark bg).
|
||||
- **Stat Cards:** 3px colored left borders.
|
||||
- **Tab Toggles:** Active state uses `tab-active-shadow` (elevated bg + faint glow).
|
||||
|
||||
### 3. GTM Strategy (from /office-hours)
|
||||
- Shadow & Ship approach: Michael uses ResolutionFlow on real tickets for 2 weeks, then hands logins to 5 MSP colleagues. Key metric: unprompted return.
|
||||
- Design doc at `~/.gstack/projects/patherly-patherly/`
|
||||
|
||||
---
|
||||
|
||||
## What Needs To Be Done Next
|
||||
|
||||
### Immediate (Design System v4 polish)
|
||||
1. **Home icon color fix:** The Home icon in the sidebar shouldn't have a cyan background when not active. Instead, the Home icon itself should always be cyan (brand accent), and only show the `bg-accent-dim` background when the route is actually `/`. Michael specifically requested this.
|
||||
2. **Visual QA pass:** Michael hasn't done a full page-by-page walkthrough yet. Expect feedback on individual pages once he does.
|
||||
3. **`font-label` cleanup:** ~10 files still reference `font-label` (deprecated alias for `font-mono`). Each needs inspection — some should be `font-mono`, others `font-sans text-xs`.
|
||||
4. **Inline `style` attributes:** ~29 instances still use hardcoded hex in inline styles (sidebar, drawer, badges). Should be converted to CSS variable references or Tailwind classes where possible.
|
||||
|
||||
### Before Merging PR #119
|
||||
- Run migrations: `docker exec resolutionflow_backend alembic upgrade head` (new tables from the Script Builder PR are on main now)
|
||||
- Full visual QA with backend running
|
||||
- Test mobile responsive (hamburger menu)
|
||||
- Test FlowPilot session with new message bar + action bar positioning
|
||||
|
||||
### Future
|
||||
- **Light mode toggle:** CSS variables are ready. Need to add `.light` class values in `index.css` + toggle in user settings/account page.
|
||||
- **Script Builder testing:** The AI Script Builder hasn't been tested end-to-end with the backend running yet.
|
||||
|
||||
---
|
||||
|
||||
## Key Files to Know
|
||||
|
||||
| File | What it does |
|
||||
|------|-------------|
|
||||
| `DESIGN-SYSTEM.md` | Single source of truth for all design decisions |
|
||||
| `frontend/src/index.css` | CSS tokens, component utilities, shadow patterns |
|
||||
| `frontend/src/components/layout/Sidebar.tsx` | Icon rail + drawer + pinned sidebar |
|
||||
| `frontend/src/components/layout/AppLayout.tsx` | CSS Grid shell |
|
||||
| `frontend/src/components/dashboard/StartSessionInput.tsx` | The Guided/Chat toggle |
|
||||
| `frontend/src/components/dashboard/PerformanceCards.tsx` | Stat cards with colored borders |
|
||||
|
||||
## Key Lessons From This Session
|
||||
|
||||
- The component sweep agents missed `editor-ai/`, `guides/`, `maintenance/`, `scripts/`, `settings/` directories and `text-brand-dark` references. Always do a final grep audit after sweeps.
|
||||
- `bg-[#hex]` hardcoding defeats the purpose of CSS variables. We had to do a second pass to replace 3,200+ hardcoded values with semantic tokens.
|
||||
- Black shadows (`rgba(0,0,0,...)`) are invisible on dark backgrounds. Use elevated surfaces + faint accent glow instead.
|
||||
- The sidebar flyout needed `position: fixed` to escape the CSS Grid cell clipping — `absolute` positioning was hidden behind the main content area.
|
||||
- Flyout hover timing: individual item `onMouseLeave` was killing the flyout before the mouse reached the drawer. Only the outer wrapper should handle `onMouseLeave`.
|
||||
|
||||
---
|
||||
|
||||
> **After reading this file:** Save relevant context to your session memory, then run `rm SESSION-HANDOFF.md` and `git add -A && git commit -m "chore: remove session handoff file"`.
|
||||
@@ -5,6 +5,12 @@ WORKDIR /app
|
||||
RUN apt-get update && apt-get install -y \
|
||||
gcc \
|
||||
libpq-dev \
|
||||
libpango1.0-dev \
|
||||
libcairo2-dev \
|
||||
libgdk-pixbuf-2.0-dev \
|
||||
libffi-dev \
|
||||
libjpeg-dev \
|
||||
zlib1g-dev \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY requirements.txt requirements-dev.txt ./
|
||||
@@ -12,4 +18,4 @@ RUN pip install --no-cache-dir -r requirements-dev.txt
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
CMD [ "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload" ]
|
||||
CMD [ "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload" ]
|
||||
|
||||
132
backend/alembic/versions/073_add_device_types_table.py
Normal file
132
backend/alembic/versions/073_add_device_types_table.py
Normal file
@@ -0,0 +1,132 @@
|
||||
"""Add account-scoped device_types table with platform seed data.
|
||||
|
||||
Revision ID: 073
|
||||
Revises: b3c7e9f2a1d8
|
||||
Create Date: 2026-04-12
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
import uuid
|
||||
|
||||
|
||||
revision = "073"
|
||||
down_revision = "b3c7e9f2a1d8"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
_PLATFORM_UUID = "00000000-0000-0000-0000-000000000001"
|
||||
_CURRENT_ACCOUNT = (
|
||||
"COALESCE("
|
||||
"NULLIF(current_setting('app.current_account_id', TRUE), ''), "
|
||||
"'00000000-0000-0000-0000-000000000000'"
|
||||
")::uuid"
|
||||
)
|
||||
|
||||
SYSTEM_DEVICE_TYPES = [
|
||||
("router", "Router", "network", 0),
|
||||
("switch", "Switch", "network", 1),
|
||||
("firewall", "Firewall", "network", 2),
|
||||
("access-point", "Access Point", "network", 3),
|
||||
("load-balancer", "Load Balancer", "network", 4),
|
||||
("server", "Server", "compute", 0),
|
||||
("workstation", "Workstation", "compute", 1),
|
||||
("vm", "Virtual Machine", "compute", 2),
|
||||
("container", "Container", "compute", 3),
|
||||
("nas", "NAS", "storage", 0),
|
||||
("san", "SAN", "storage", 1),
|
||||
("cloud-storage", "Cloud Storage", "storage", 2),
|
||||
("cloud", "Cloud", "cloud", 0),
|
||||
("aws", "AWS", "cloud", 1),
|
||||
("azure", "Azure", "cloud", 2),
|
||||
("gcp", "Google Cloud", "cloud", 3),
|
||||
("printer", "Printer", "endpoint", 0),
|
||||
("phone", "Phone", "endpoint", 1),
|
||||
("iot", "IoT Device", "endpoint", 2),
|
||||
("camera", "Camera", "endpoint", 3),
|
||||
("tablet", "Tablet", "endpoint", 4),
|
||||
("laptop", "Laptop", "endpoint", 5),
|
||||
("ups", "UPS", "infrastructure", 0),
|
||||
("pdu", "PDU", "infrastructure", 1),
|
||||
("rack", "Rack", "infrastructure", 2),
|
||||
("patch-panel", "Patch Panel", "infrastructure", 3),
|
||||
("nvr", "NVR", "security", 0),
|
||||
("badge-reader", "Badge Reader", "security", 1),
|
||||
]
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"device_types",
|
||||
sa.Column("id", UUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")),
|
||||
sa.Column("slug", sa.String(50), nullable=False),
|
||||
sa.Column("label", sa.String(100), nullable=False),
|
||||
sa.Column("category", sa.String(50), nullable=False),
|
||||
sa.Column("is_system", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
||||
sa.Column("account_id", UUID(as_uuid=True), sa.ForeignKey("accounts.id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("sort_order", sa.Integer(), nullable=False, server_default=sa.text("0")),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()")),
|
||||
)
|
||||
|
||||
op.create_unique_constraint("uq_device_types_slug_account", "device_types", ["slug", "account_id"])
|
||||
op.create_index("ix_device_types_account_id", "device_types", ["account_id"])
|
||||
|
||||
device_types_table = sa.table(
|
||||
"device_types",
|
||||
sa.column("id", UUID(as_uuid=True)),
|
||||
sa.column("slug", sa.String),
|
||||
sa.column("label", sa.String),
|
||||
sa.column("category", sa.String),
|
||||
sa.column("is_system", sa.Boolean),
|
||||
sa.column("account_id", UUID(as_uuid=True)),
|
||||
sa.column("sort_order", sa.Integer),
|
||||
)
|
||||
|
||||
op.bulk_insert(device_types_table, [
|
||||
{
|
||||
"id": uuid.uuid4(),
|
||||
"slug": slug,
|
||||
"label": label,
|
||||
"category": category,
|
||||
"is_system": True,
|
||||
"account_id": uuid.UUID(_PLATFORM_UUID),
|
||||
"sort_order": sort_order,
|
||||
}
|
||||
for slug, label, category, sort_order in SYSTEM_DEVICE_TYPES
|
||||
])
|
||||
|
||||
op.execute("ALTER TABLE device_types ENABLE ROW LEVEL SECURITY")
|
||||
op.execute("ALTER TABLE device_types FORCE ROW LEVEL SECURITY")
|
||||
op.execute(f"""
|
||||
CREATE POLICY device_types_select ON device_types
|
||||
FOR SELECT
|
||||
USING (
|
||||
account_id = {_CURRENT_ACCOUNT}
|
||||
OR account_id = '{_PLATFORM_UUID}'::uuid
|
||||
)
|
||||
""")
|
||||
op.execute(f"""
|
||||
CREATE POLICY device_types_insert ON device_types
|
||||
FOR INSERT
|
||||
WITH CHECK (account_id = {_CURRENT_ACCOUNT})
|
||||
""")
|
||||
op.execute(f"""
|
||||
CREATE POLICY device_types_update ON device_types
|
||||
FOR UPDATE
|
||||
USING (account_id = {_CURRENT_ACCOUNT})
|
||||
WITH CHECK (account_id = {_CURRENT_ACCOUNT})
|
||||
""")
|
||||
op.execute(f"""
|
||||
CREATE POLICY device_types_delete ON device_types
|
||||
FOR DELETE
|
||||
USING (account_id = {_CURRENT_ACCOUNT})
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("DROP POLICY IF EXISTS device_types_delete ON device_types")
|
||||
op.execute("DROP POLICY IF EXISTS device_types_update ON device_types")
|
||||
op.execute("DROP POLICY IF EXISTS device_types_insert ON device_types")
|
||||
op.execute("DROP POLICY IF EXISTS device_types_select ON device_types")
|
||||
op.execute("ALTER TABLE device_types DISABLE ROW LEVEL SECURITY")
|
||||
op.drop_table("device_types")
|
||||
57
backend/alembic/versions/074_add_network_diagrams_table.py
Normal file
57
backend/alembic/versions/074_add_network_diagrams_table.py
Normal file
@@ -0,0 +1,57 @@
|
||||
"""Add network_diagrams table.
|
||||
|
||||
Revision ID: 074
|
||||
Revises: 073
|
||||
Create Date: 2026-04-12
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
|
||||
|
||||
revision = "074"
|
||||
down_revision = "073"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
_CURRENT_ACCOUNT = (
|
||||
"COALESCE("
|
||||
"NULLIF(current_setting('app.current_account_id', TRUE), ''), "
|
||||
"'00000000-0000-0000-0000-000000000000'"
|
||||
")::uuid"
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"network_diagrams",
|
||||
sa.Column("id", UUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")),
|
||||
sa.Column("account_id", UUID(as_uuid=True), sa.ForeignKey("accounts.id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("name", sa.String(255), nullable=False),
|
||||
sa.Column("client_name", sa.String(255), nullable=True),
|
||||
sa.Column("asset_name", sa.String(255), nullable=True),
|
||||
sa.Column("description", sa.Text(), nullable=True),
|
||||
sa.Column("nodes", JSONB(), nullable=False, server_default=sa.text("'[]'::jsonb")),
|
||||
sa.Column("edges", JSONB(), nullable=False, server_default=sa.text("'[]'::jsonb")),
|
||||
sa.Column("thumbnail_url", sa.Text(), nullable=True),
|
||||
sa.Column("is_archived", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
||||
sa.Column("created_by", UUID(as_uuid=True), sa.ForeignKey("users.id"), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()")),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()")),
|
||||
)
|
||||
|
||||
op.create_index("ix_network_diagrams_account_id", "network_diagrams", ["account_id"])
|
||||
op.create_index("idx_network_diagrams_account_client", "network_diagrams", ["account_id", "client_name"])
|
||||
op.execute("ALTER TABLE network_diagrams ENABLE ROW LEVEL SECURITY")
|
||||
op.execute("ALTER TABLE network_diagrams FORCE ROW LEVEL SECURITY")
|
||||
op.execute(f"""
|
||||
CREATE POLICY tenant_isolation ON network_diagrams
|
||||
USING (account_id = {_CURRENT_ACCOUNT})
|
||||
WITH CHECK (account_id = {_CURRENT_ACCOUNT})
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("DROP POLICY IF EXISTS tenant_isolation ON network_diagrams")
|
||||
op.execute("ALTER TABLE network_diagrams DISABLE ROW LEVEL SECURITY")
|
||||
op.drop_table("network_diagrams")
|
||||
@@ -0,0 +1,74 @@
|
||||
"""add fix outcome tracking columns to session_suggested_fixes
|
||||
|
||||
Adds: status, applied_at, verified_at, partial_notes, failure_reason,
|
||||
ai_outcome_proposal.
|
||||
|
||||
status is the outcome dimension (did the fix work?), orthogonal to the
|
||||
existing user_decision column (which script-path the engineer took).
|
||||
|
||||
Revision ID: 6492ec8d2d5b
|
||||
Revises: f07010f17b01
|
||||
Create Date: 2026-04-23 18:32:38.609719
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '6492ec8d2d5b'
|
||||
down_revision: Union[str, None] = 'f07010f17b01'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column(
|
||||
"session_suggested_fixes",
|
||||
sa.Column("status", sa.String(length=20), nullable=False, server_default=sa.text("'proposed'")),
|
||||
)
|
||||
op.add_column(
|
||||
"session_suggested_fixes",
|
||||
sa.Column("applied_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"session_suggested_fixes",
|
||||
sa.Column("verified_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"session_suggested_fixes",
|
||||
sa.Column("partial_notes", sa.Text(), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"session_suggested_fixes",
|
||||
sa.Column("failure_reason", sa.Text(), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"session_suggested_fixes",
|
||||
sa.Column("ai_outcome_proposal", postgresql.JSONB(), nullable=True),
|
||||
)
|
||||
# Backfill before constraint creation so dismissed rows satisfy the new CHECK.
|
||||
op.execute(
|
||||
"UPDATE session_suggested_fixes "
|
||||
"SET status = 'dismissed' "
|
||||
"WHERE user_decision = 'dismissed'"
|
||||
)
|
||||
op.create_check_constraint(
|
||||
"ck_session_suggested_fixes_status",
|
||||
"session_suggested_fixes",
|
||||
"status IN ('proposed', 'applied_success', 'applied_failed', 'applied_partial', 'dismissed')",
|
||||
)
|
||||
op.alter_column("session_suggested_fixes", "status", server_default=None)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_constraint("ck_session_suggested_fixes_status", "session_suggested_fixes", type_="check")
|
||||
op.drop_column("session_suggested_fixes", "ai_outcome_proposal")
|
||||
op.drop_column("session_suggested_fixes", "failure_reason")
|
||||
op.drop_column("session_suggested_fixes", "partial_notes")
|
||||
op.drop_column("session_suggested_fixes", "verified_at")
|
||||
op.drop_column("session_suggested_fixes", "applied_at")
|
||||
op.drop_column("session_suggested_fixes", "status")
|
||||
@@ -0,0 +1,70 @@
|
||||
"""add origin discriminator + inline idempotency to script_builder_sessions
|
||||
|
||||
Adds:
|
||||
- origin VARCHAR(20) NOT NULL DEFAULT 'standalone' with CHECK enum
|
||||
- invariant: pilot_inline rows must have ai_session_id
|
||||
- partial unique index: one pilot_inline session per (user, pilot session)
|
||||
|
||||
Revision ID: 71efd2102f49
|
||||
Revises: 6492ec8d2d5b
|
||||
Create Date: 2026-04-24 04:22:10.819809
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '71efd2102f49'
|
||||
down_revision = '6492ec8d2d5b'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column(
|
||||
"script_builder_sessions",
|
||||
sa.Column(
|
||||
"origin",
|
||||
sa.String(length=20),
|
||||
nullable=False,
|
||||
server_default=sa.text("'standalone'"),
|
||||
),
|
||||
)
|
||||
op.create_check_constraint(
|
||||
"ck_script_builder_sessions_origin",
|
||||
"script_builder_sessions",
|
||||
"origin IN ('standalone', 'pilot_inline')",
|
||||
)
|
||||
op.create_check_constraint(
|
||||
"ck_script_builder_sessions_origin_ai_session",
|
||||
"script_builder_sessions",
|
||||
"origin <> 'pilot_inline' OR ai_session_id IS NOT NULL",
|
||||
)
|
||||
op.create_index(
|
||||
"ux_script_builder_sessions_pilot_inline",
|
||||
"script_builder_sessions",
|
||||
["user_id", "ai_session_id"],
|
||||
unique=True,
|
||||
postgresql_where=sa.text("origin = 'pilot_inline'"),
|
||||
)
|
||||
# Drop the server_default — app code owns the default via model default.
|
||||
op.alter_column("script_builder_sessions", "origin", server_default=None)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index(
|
||||
"ux_script_builder_sessions_pilot_inline",
|
||||
table_name="script_builder_sessions",
|
||||
)
|
||||
op.drop_constraint(
|
||||
"ck_script_builder_sessions_origin_ai_session",
|
||||
"script_builder_sessions",
|
||||
type_="check",
|
||||
)
|
||||
op.drop_constraint(
|
||||
"ck_script_builder_sessions_origin",
|
||||
"script_builder_sessions",
|
||||
type_="check",
|
||||
)
|
||||
op.drop_column("script_builder_sessions", "origin")
|
||||
404
backend/alembic/versions/f07010f17b01_flowpilot_phase1_schema.py
Normal file
404
backend/alembic/versions/f07010f17b01_flowpilot_phase1_schema.py
Normal file
@@ -0,0 +1,404 @@
|
||||
"""FlowPilot migration Phase 1 — schema for the unified session surface.
|
||||
|
||||
Revision ID: f07010f17b01
|
||||
Revises: 074
|
||||
Create Date: 2026-04-17
|
||||
|
||||
Creates the backing store for the FlowPilot unified session surface:
|
||||
|
||||
- `session_facts` — "What we know" facts, keyed to a session, with a polymorphic
|
||||
`source_ref` pointing at a task-lane item inside `ai_sessions.pending_task_lane`
|
||||
(no DB-level FK; integrity enforced at the service layer per the design doc).
|
||||
- `session_suggested_fixes` — AI-proposed resolution paths. Only one active
|
||||
(`superseded_at IS NULL`) per session at a time.
|
||||
- `draft_templates` — scripts pending post-resolve templatization
|
||||
(Option 2 in the three-option dialog).
|
||||
- `account_settings` — new per-account key/value settings table with a JSONB
|
||||
`preferences` grab-bag. Rows are created lazily on first write.
|
||||
- Column additions to `ai_sessions` — resolution/escalation markdown + external IDs,
|
||||
plus `state_version` (incremented by any write that invalidates the resolution
|
||||
note preview cache).
|
||||
- Column additions to `script_templates` — provenance fields for templates
|
||||
promoted from draft_templates.
|
||||
|
||||
All four new tenant-scoped tables have RLS enabled + forced with a
|
||||
`tenant_isolation` policy matching the repo pattern (USING + WITH CHECK on
|
||||
`account_id = app.current_account_id`). Downgrade is reversible: drops in the
|
||||
inverse order of creation.
|
||||
|
||||
Chained from `074` (add_network_diagrams_table) per the single-head state of
|
||||
production; the other local heads on feat/flowpilot-migration are branch
|
||||
artifacts not present in production.
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
|
||||
|
||||
revision = "f07010f17b01"
|
||||
down_revision = "074"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
_CURRENT_ACCOUNT = (
|
||||
"COALESCE("
|
||||
"NULLIF(current_setting('app.current_account_id', TRUE), ''), "
|
||||
"'00000000-0000-0000-0000-000000000000'"
|
||||
")::uuid"
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ── ai_sessions: resolution / escalation columns + state_version ───────
|
||||
op.add_column(
|
||||
"ai_sessions",
|
||||
sa.Column("resolution_note_markdown", sa.Text(), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"ai_sessions",
|
||||
sa.Column("resolution_note_posted_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"ai_sessions",
|
||||
sa.Column("resolution_note_external_id", sa.String(128), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"ai_sessions",
|
||||
sa.Column("escalation_package_markdown", sa.Text(), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"ai_sessions",
|
||||
sa.Column("escalation_package_posted_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"ai_sessions",
|
||||
sa.Column("escalation_package_external_id", sa.String(128), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"ai_sessions",
|
||||
sa.Column(
|
||||
"state_version",
|
||||
sa.Integer(),
|
||||
nullable=False,
|
||||
server_default=sa.text("0"),
|
||||
),
|
||||
)
|
||||
|
||||
# ── script_templates: provenance for post-resolve promotion ────────────
|
||||
op.add_column(
|
||||
"script_templates",
|
||||
sa.Column(
|
||||
"source_session_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("ai_sessions.id"),
|
||||
nullable=True,
|
||||
),
|
||||
)
|
||||
op.add_column(
|
||||
"script_templates",
|
||||
sa.Column(
|
||||
"source_user_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("users.id"),
|
||||
nullable=True,
|
||||
),
|
||||
)
|
||||
op.add_column(
|
||||
"script_templates",
|
||||
sa.Column("source_ticket_ref", sa.String(64), nullable=True),
|
||||
)
|
||||
|
||||
# ── session_facts ──────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"session_facts",
|
||||
sa.Column(
|
||||
"id",
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
server_default=sa.text("gen_random_uuid()"),
|
||||
),
|
||||
sa.Column(
|
||||
"session_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("ai_sessions.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
"account_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("accounts.id"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("text", sa.Text(), nullable=False),
|
||||
sa.Column("source_type", sa.String(32), nullable=False),
|
||||
# `source_ref` is a polymorphic pointer to a task-lane item inside
|
||||
# ai_sessions.pending_task_lane JSON, NOT a FK to any table.
|
||||
# Integrity enforced at the service layer per Section 4.2 of the
|
||||
# migration design doc.
|
||||
sa.Column("source_ref", UUID(as_uuid=True), nullable=True),
|
||||
sa.Column("source_summary", sa.Text(), nullable=True),
|
||||
sa.Column(
|
||||
"created_by",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("users.id"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()"),
|
||||
),
|
||||
sa.Column(
|
||||
"updated_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()"),
|
||||
),
|
||||
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.CheckConstraint(
|
||||
"source_type IN ('question', 'diagnostic_check', 'user_note', 'ai_synthesis')",
|
||||
name="ck_session_facts_source_type",
|
||||
),
|
||||
)
|
||||
# Active-facts-per-session; partial index excludes soft-deleted rows.
|
||||
op.create_index(
|
||||
"idx_session_facts_session",
|
||||
"session_facts",
|
||||
["session_id"],
|
||||
postgresql_where=sa.text("deleted_at IS NULL"),
|
||||
)
|
||||
op.create_index(
|
||||
"idx_session_facts_account",
|
||||
"session_facts",
|
||||
["account_id"],
|
||||
)
|
||||
op.execute("ALTER TABLE session_facts ENABLE ROW LEVEL SECURITY")
|
||||
op.execute("ALTER TABLE session_facts FORCE ROW LEVEL SECURITY")
|
||||
op.execute(f"""
|
||||
CREATE POLICY tenant_isolation ON session_facts
|
||||
USING (account_id = {_CURRENT_ACCOUNT})
|
||||
WITH CHECK (account_id = {_CURRENT_ACCOUNT})
|
||||
""")
|
||||
|
||||
# ── session_suggested_fixes ────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"session_suggested_fixes",
|
||||
sa.Column(
|
||||
"id",
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
server_default=sa.text("gen_random_uuid()"),
|
||||
),
|
||||
sa.Column(
|
||||
"session_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("ai_sessions.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
"account_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("accounts.id"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("title", sa.String(200), nullable=False),
|
||||
sa.Column("description", sa.Text(), nullable=False),
|
||||
sa.Column("confidence_pct", sa.Integer(), nullable=False),
|
||||
sa.Column(
|
||||
"script_template_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("script_templates.id"),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column("ai_drafted_script", sa.Text(), nullable=True),
|
||||
sa.Column("ai_drafted_parameters", JSONB(), nullable=True),
|
||||
sa.Column("user_decision", sa.String(32), nullable=True),
|
||||
sa.Column("superseded_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"confidence_pct BETWEEN 0 AND 100",
|
||||
name="ck_session_suggested_fixes_confidence_pct",
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"user_decision IS NULL OR user_decision IN ("
|
||||
"'one_off', 'draft_template', 'build_template', 'dismissed')",
|
||||
name="ck_session_suggested_fixes_user_decision",
|
||||
),
|
||||
)
|
||||
# Only-one-active-per-session is enforced by service-layer supersession;
|
||||
# this partial index serves the "find active fix" query.
|
||||
op.create_index(
|
||||
"idx_session_suggested_fixes_session_active",
|
||||
"session_suggested_fixes",
|
||||
["session_id"],
|
||||
postgresql_where=sa.text("superseded_at IS NULL"),
|
||||
)
|
||||
op.execute("ALTER TABLE session_suggested_fixes ENABLE ROW LEVEL SECURITY")
|
||||
op.execute("ALTER TABLE session_suggested_fixes FORCE ROW LEVEL SECURITY")
|
||||
op.execute(f"""
|
||||
CREATE POLICY tenant_isolation ON session_suggested_fixes
|
||||
USING (account_id = {_CURRENT_ACCOUNT})
|
||||
WITH CHECK (account_id = {_CURRENT_ACCOUNT})
|
||||
""")
|
||||
|
||||
# ── draft_templates ────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"draft_templates",
|
||||
sa.Column(
|
||||
"id",
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
server_default=sa.text("gen_random_uuid()"),
|
||||
),
|
||||
sa.Column(
|
||||
"account_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("accounts.id"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
"source_session_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("ai_sessions.id"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
"source_user_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("users.id"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("script_body", sa.Text(), nullable=False),
|
||||
sa.Column("proposed_parameters", JSONB(), nullable=False),
|
||||
sa.Column("proposed_name", sa.String(200), nullable=True),
|
||||
sa.Column(
|
||||
"proposed_category_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("script_categories.id"),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column(
|
||||
"status",
|
||||
sa.String(32),
|
||||
nullable=False,
|
||||
server_default=sa.text("'pending'"),
|
||||
),
|
||||
sa.Column("resolved_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column(
|
||||
"promoted_template_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("script_templates.id"),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"status IN ('pending', 'accepted', 'rejected')",
|
||||
name="ck_draft_templates_status",
|
||||
),
|
||||
)
|
||||
# Supports the Script Library "N scripts ready to review" badge.
|
||||
op.create_index(
|
||||
"idx_draft_templates_account_pending",
|
||||
"draft_templates",
|
||||
["account_id"],
|
||||
postgresql_where=sa.text("status = 'pending'"),
|
||||
)
|
||||
op.execute("ALTER TABLE draft_templates ENABLE ROW LEVEL SECURITY")
|
||||
op.execute("ALTER TABLE draft_templates FORCE ROW LEVEL SECURITY")
|
||||
op.execute(f"""
|
||||
CREATE POLICY tenant_isolation ON draft_templates
|
||||
USING (account_id = {_CURRENT_ACCOUNT})
|
||||
WITH CHECK (account_id = {_CURRENT_ACCOUNT})
|
||||
""")
|
||||
|
||||
# ── account_settings ───────────────────────────────────────────────────
|
||||
# One row per account, created lazily on first write. The `preferences`
|
||||
# JSONB is a grab-bag for simple settings (e.g. templatize_prompt_enabled).
|
||||
# Settings graduate to typed columns via future migrations when they meet
|
||||
# the promotion criteria in Section 4.6 of the design doc (hot path /
|
||||
# validation / joins).
|
||||
op.create_table(
|
||||
"account_settings",
|
||||
sa.Column(
|
||||
"account_id",
|
||||
UUID(as_uuid=True),
|
||||
sa.ForeignKey("accounts.id", ondelete="CASCADE"),
|
||||
primary_key=True,
|
||||
),
|
||||
sa.Column(
|
||||
"preferences",
|
||||
JSONB(),
|
||||
nullable=False,
|
||||
server_default=sa.text("'{}'::jsonb"),
|
||||
),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()"),
|
||||
),
|
||||
sa.Column(
|
||||
"updated_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()"),
|
||||
),
|
||||
)
|
||||
op.execute("ALTER TABLE account_settings ENABLE ROW LEVEL SECURITY")
|
||||
op.execute("ALTER TABLE account_settings FORCE ROW LEVEL SECURITY")
|
||||
op.execute(f"""
|
||||
CREATE POLICY tenant_isolation ON account_settings
|
||||
USING (account_id = {_CURRENT_ACCOUNT})
|
||||
WITH CHECK (account_id = {_CURRENT_ACCOUNT})
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# Drop in reverse order so FK dependencies unwind cleanly.
|
||||
op.execute("DROP POLICY IF EXISTS tenant_isolation ON account_settings")
|
||||
op.execute("ALTER TABLE account_settings DISABLE ROW LEVEL SECURITY")
|
||||
op.drop_table("account_settings")
|
||||
|
||||
op.execute("DROP POLICY IF EXISTS tenant_isolation ON draft_templates")
|
||||
op.execute("ALTER TABLE draft_templates DISABLE ROW LEVEL SECURITY")
|
||||
op.drop_index("idx_draft_templates_account_pending", table_name="draft_templates")
|
||||
op.drop_table("draft_templates")
|
||||
|
||||
op.execute("DROP POLICY IF EXISTS tenant_isolation ON session_suggested_fixes")
|
||||
op.execute("ALTER TABLE session_suggested_fixes DISABLE ROW LEVEL SECURITY")
|
||||
op.drop_index(
|
||||
"idx_session_suggested_fixes_session_active",
|
||||
table_name="session_suggested_fixes",
|
||||
)
|
||||
op.drop_table("session_suggested_fixes")
|
||||
|
||||
op.execute("DROP POLICY IF EXISTS tenant_isolation ON session_facts")
|
||||
op.execute("ALTER TABLE session_facts DISABLE ROW LEVEL SECURITY")
|
||||
op.drop_index("idx_session_facts_account", table_name="session_facts")
|
||||
op.drop_index("idx_session_facts_session", table_name="session_facts")
|
||||
op.drop_table("session_facts")
|
||||
|
||||
op.drop_column("script_templates", "source_ticket_ref")
|
||||
op.drop_column("script_templates", "source_user_id")
|
||||
op.drop_column("script_templates", "source_session_id")
|
||||
|
||||
op.drop_column("ai_sessions", "state_version")
|
||||
op.drop_column("ai_sessions", "escalation_package_external_id")
|
||||
op.drop_column("ai_sessions", "escalation_package_posted_at")
|
||||
op.drop_column("ai_sessions", "escalation_package_markdown")
|
||||
op.drop_column("ai_sessions", "resolution_note_external_id")
|
||||
op.drop_column("ai_sessions", "resolution_note_posted_at")
|
||||
op.drop_column("ai_sessions", "resolution_note_markdown")
|
||||
@@ -16,6 +16,7 @@ from app.models.refresh_token import RefreshToken
|
||||
from app.core.email import EmailService
|
||||
from app.models.account import Account
|
||||
from app.models.account_invite import AccountInvite
|
||||
from app.models.account_settings import AccountSettings
|
||||
from app.models.subscription import Subscription
|
||||
from app.models.user import User
|
||||
from app.schemas.account import AccountResponse, AccountUpdate, AccountInviteCreate, AccountInviteResponse, TransferOwnershipRequest
|
||||
@@ -559,3 +560,65 @@ async def get_sso_status(
|
||||
sso_enabled=account.sso_enabled,
|
||||
sso_provider=account.sso_provider,
|
||||
)
|
||||
|
||||
|
||||
# ─── Account Preferences (FlowPilot Phase 6) ──────────────────────────────────
|
||||
#
|
||||
# Preferences live in `account_settings.preferences` as a JSONB grab-bag
|
||||
# (per FLOWPILOT-MIGRATION.md Section 4.6). Rows are lazily created on first
|
||||
# write. Any engineer-role user can read + update preferences because the
|
||||
# keys stored here (templatize_prompt_enabled, cw_resolved_status_id, etc.)
|
||||
# are team-level toggles rather than account-owner-gated admin settings.
|
||||
|
||||
|
||||
class AccountPreferencesResponse(BaseModel):
|
||||
preferences: dict
|
||||
|
||||
|
||||
class AccountPreferencesUpdate(BaseModel):
|
||||
"""Merge-style update — each key in `preferences` overwrites that key in
|
||||
the stored JSONB, other keys are preserved. Omit the body entirely to
|
||||
no-op.
|
||||
"""
|
||||
preferences: dict
|
||||
|
||||
|
||||
@router.get("/me/preferences", response_model=AccountPreferencesResponse)
|
||||
async def get_my_preferences(
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
):
|
||||
"""Return the current account's preferences JSONB (empty dict if no row)."""
|
||||
result = await db.execute(
|
||||
select(AccountSettings.preferences).where(
|
||||
AccountSettings.account_id == current_user.account_id
|
||||
)
|
||||
)
|
||||
prefs = result.scalar_one_or_none() or {}
|
||||
return AccountPreferencesResponse(preferences=prefs)
|
||||
|
||||
|
||||
@router.patch("/me/preferences", response_model=AccountPreferencesResponse)
|
||||
async def update_my_preferences(
|
||||
data: AccountPreferencesUpdate,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
):
|
||||
"""Upsert preference keys. Existing keys not present in the payload are kept.
|
||||
|
||||
Example: posting `{"preferences": {"templatize_prompt_enabled": false}}`
|
||||
from the post-resolve "Don't ask me again for this team" checkbox sets
|
||||
just that key without clobbering any other preferences.
|
||||
"""
|
||||
for key, value in data.preferences.items():
|
||||
await AccountSettings.set_setting(db, current_user.account_id, key, value)
|
||||
await db.commit()
|
||||
|
||||
# Return the merged state so the client doesn't need a second GET.
|
||||
result = await db.execute(
|
||||
select(AccountSettings.preferences).where(
|
||||
AccountSettings.account_id == current_user.account_id
|
||||
)
|
||||
)
|
||||
prefs = result.scalar_one_or_none() or {}
|
||||
return AccountPreferencesResponse(preferences=prefs)
|
||||
|
||||
@@ -5,8 +5,8 @@ from typing import Annotated, Optional
|
||||
from uuid import UUID
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, func
|
||||
from sqlalchemy.orm import selectinload
|
||||
from sqlalchemy import select, func, or_
|
||||
from sqlalchemy.orm import selectinload, aliased
|
||||
|
||||
from app.core.admin_database import get_admin_db
|
||||
from app.core.audit import log_audit
|
||||
@@ -24,21 +24,44 @@ from app.models.invite_code import InviteCode
|
||||
from app.models.account_invite import AccountInvite
|
||||
from app.models.tree import Tree
|
||||
from app.schemas.user import UserResponse, RoleUpdate, AccountRoleUpdate
|
||||
from app.schemas.admin import MoveUserAccount, AdminUserCreate, AdminUserCreateResponse, AdminPasswordReset, AdminPasswordResetResponse, HardDeleteCheckResponse
|
||||
from app.schemas.admin import (
|
||||
MoveUserAccount,
|
||||
AdminUserCreate,
|
||||
AdminUserCreateResponse,
|
||||
AdminPasswordReset,
|
||||
AdminPasswordResetResponse,
|
||||
HardDeleteCheckResponse,
|
||||
AdminUserListItem,
|
||||
AdminUserListResponse,
|
||||
AdminAccountMember,
|
||||
AdminAccountListItem,
|
||||
AdminAccountListResponse,
|
||||
AdminAccountOwnerSummary,
|
||||
AdminAccountSubscriptionSummary,
|
||||
AdminAccountUsageSummary,
|
||||
AdminAccountDetailResponse,
|
||||
AdminAccountInviteSummary,
|
||||
AdminAccountCreate,
|
||||
AdminAccountUpdate,
|
||||
)
|
||||
from app.schemas.subscription import SubscriptionPlanUpdate, ExtendTrialRequest
|
||||
from app.schemas.user_detail import (
|
||||
UserDetailResponse, AccountSummary, SubscriptionSummary,
|
||||
SessionSummary, AuditLogSummary, InviteCodeUsedSummary,
|
||||
)
|
||||
from app.api.deps import require_admin
|
||||
from app.core.subscriptions import get_account_usage
|
||||
|
||||
router = APIRouter(prefix="/admin", tags=["admin"])
|
||||
|
||||
|
||||
@router.get("/users", response_model=list[UserResponse])
|
||||
@router.get("/users", response_model=AdminUserListResponse)
|
||||
async def list_users(
|
||||
db: Annotated[AsyncSession, Depends(get_admin_db)],
|
||||
current_user: Annotated[User, Depends(require_admin)],
|
||||
page: Optional[int] = Query(None, ge=1),
|
||||
size: Optional[int] = Query(None, ge=1, le=100),
|
||||
search: Optional[str] = Query(None, description="Search by user or account fields"),
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(100, ge=1, le=100),
|
||||
is_active: Optional[bool] = Query(None, description="Filter by active status"),
|
||||
@@ -46,23 +69,240 @@ async def list_users(
|
||||
account_id: Optional[UUID] = Query(None, description="Filter by account"),
|
||||
include_archived: bool = Query(False, description="Include archived (soft-deleted) users"),
|
||||
):
|
||||
"""List all users (super admin only)."""
|
||||
query = select(User)
|
||||
"""List users for super admin global people search."""
|
||||
resolved_limit = size or limit
|
||||
resolved_skip = skip
|
||||
current_page = 1
|
||||
|
||||
if page is not None:
|
||||
resolved_skip = (page - 1) * resolved_limit
|
||||
current_page = page
|
||||
elif resolved_limit > 0:
|
||||
current_page = (resolved_skip // resolved_limit) + 1
|
||||
|
||||
count_query = (
|
||||
select(func.count())
|
||||
.select_from(User)
|
||||
.outerjoin(Account, User.account_id == Account.id)
|
||||
)
|
||||
query = (
|
||||
select(
|
||||
User,
|
||||
Account.name.label("account_name"),
|
||||
Account.display_code.label("account_display_code"),
|
||||
)
|
||||
.outerjoin(Account, User.account_id == Account.id)
|
||||
)
|
||||
|
||||
if not include_archived:
|
||||
query = query.where(User.deleted_at.is_(None))
|
||||
count_query = count_query.where(User.deleted_at.is_(None))
|
||||
if is_active is not None:
|
||||
query = query.where(User.is_active == is_active)
|
||||
count_query = count_query.where(User.is_active == is_active)
|
||||
if role:
|
||||
query = query.where(User.role == role)
|
||||
count_query = count_query.where(User.role == role)
|
||||
if account_id:
|
||||
query = query.where(User.account_id == account_id)
|
||||
count_query = count_query.where(User.account_id == account_id)
|
||||
if search:
|
||||
search_term = f"%{search.strip()}%"
|
||||
search_filter = or_(
|
||||
User.name.ilike(search_term),
|
||||
User.email.ilike(search_term),
|
||||
Account.name.ilike(search_term),
|
||||
Account.display_code.ilike(search_term),
|
||||
)
|
||||
query = query.where(search_filter)
|
||||
count_query = count_query.where(search_filter)
|
||||
|
||||
query = query.order_by(User.created_at.desc()).offset(skip).limit(limit)
|
||||
total_result = await db.execute(count_query)
|
||||
total = total_result.scalar() or 0
|
||||
|
||||
query = query.order_by(User.created_at.desc()).offset(resolved_skip).limit(resolved_limit)
|
||||
result = await db.execute(query)
|
||||
users = result.scalars().all()
|
||||
return users
|
||||
rows = result.all()
|
||||
|
||||
items = [
|
||||
AdminUserListItem(
|
||||
id=user.id,
|
||||
email=user.email,
|
||||
name=user.name,
|
||||
role=user.role,
|
||||
is_super_admin=user.is_super_admin,
|
||||
is_active=user.is_active,
|
||||
account_id=user.account_id,
|
||||
account_role=user.account_role,
|
||||
account_name=account_name,
|
||||
account_display_code=account_display_code,
|
||||
created_at=user.created_at,
|
||||
last_login=user.last_login,
|
||||
deleted_at=user.deleted_at,
|
||||
)
|
||||
for user, account_name, account_display_code in rows
|
||||
]
|
||||
|
||||
return AdminUserListResponse(
|
||||
items=items,
|
||||
total=total,
|
||||
page=current_page,
|
||||
per_page=resolved_limit,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/accounts", response_model=AdminAccountListResponse)
|
||||
async def list_accounts(
|
||||
db: Annotated[AsyncSession, Depends(get_admin_db)],
|
||||
current_user: Annotated[User, Depends(require_admin)],
|
||||
page: int = Query(1, ge=1),
|
||||
size: int = Query(12, ge=1, le=100),
|
||||
search: Optional[str] = Query(None, description="Search by account, display code, or owner"),
|
||||
plan: Optional[str] = Query(None, description="Filter by subscription plan"),
|
||||
status: Optional[str] = Query(None, description="Filter by subscription status"),
|
||||
include_archived: bool = Query(False, description="Include archived users in account member lists"),
|
||||
):
|
||||
"""List accounts with embedded members for the admin panel."""
|
||||
owner_user = aliased(User)
|
||||
|
||||
count_query = (
|
||||
select(func.count(func.distinct(Account.id)))
|
||||
.select_from(Account)
|
||||
.outerjoin(owner_user, Account.owner_id == owner_user.id)
|
||||
.outerjoin(Subscription, Subscription.account_id == Account.id)
|
||||
)
|
||||
accounts_query = (
|
||||
select(
|
||||
Account,
|
||||
owner_user.id.label("owner_user_id"),
|
||||
owner_user.name.label("owner_name"),
|
||||
owner_user.email.label("owner_email"),
|
||||
Subscription.id.label("subscription_id"),
|
||||
Subscription.plan.label("subscription_plan"),
|
||||
Subscription.status.label("subscription_status"),
|
||||
Subscription.billing_interval.label("subscription_billing_interval"),
|
||||
Subscription.current_period_end.label("subscription_current_period_end"),
|
||||
Subscription.cancel_at_period_end.label("subscription_cancel_at_period_end"),
|
||||
)
|
||||
.outerjoin(owner_user, Account.owner_id == owner_user.id)
|
||||
.outerjoin(Subscription, Subscription.account_id == Account.id)
|
||||
)
|
||||
|
||||
if search:
|
||||
search_term = f"%{search.strip()}%"
|
||||
search_filter = or_(
|
||||
Account.name.ilike(search_term),
|
||||
Account.display_code.ilike(search_term),
|
||||
owner_user.name.ilike(search_term),
|
||||
owner_user.email.ilike(search_term),
|
||||
)
|
||||
count_query = count_query.where(search_filter)
|
||||
accounts_query = accounts_query.where(search_filter)
|
||||
if plan:
|
||||
count_query = count_query.where(Subscription.plan == plan)
|
||||
accounts_query = accounts_query.where(Subscription.plan == plan)
|
||||
if status:
|
||||
count_query = count_query.where(Subscription.status == status)
|
||||
accounts_query = accounts_query.where(Subscription.status == status)
|
||||
|
||||
total_result = await db.execute(count_query)
|
||||
total = total_result.scalar() or 0
|
||||
|
||||
accounts_result = await db.execute(
|
||||
accounts_query
|
||||
.order_by(Account.created_at.desc())
|
||||
.offset((page - 1) * size)
|
||||
.limit(size)
|
||||
)
|
||||
rows = accounts_result.all()
|
||||
accounts = [row.Account for row in rows]
|
||||
|
||||
account_ids = [account.id for account in accounts]
|
||||
members_by_account: dict[UUID, list[AdminAccountMember]] = {account_id: [] for account_id in account_ids}
|
||||
pending_invites_by_account: dict[UUID, int] = {account_id: 0 for account_id in account_ids}
|
||||
usage_by_account: dict[UUID, AdminAccountUsageSummary] = {}
|
||||
|
||||
if account_ids:
|
||||
members_query = select(User).where(User.account_id.in_(account_ids))
|
||||
if not include_archived:
|
||||
members_query = members_query.where(User.deleted_at.is_(None))
|
||||
members_query = members_query.order_by(User.created_at.asc())
|
||||
|
||||
members_result = await db.execute(members_query)
|
||||
for member in members_result.scalars().all():
|
||||
members_by_account.setdefault(member.account_id, []).append(
|
||||
AdminAccountMember(
|
||||
id=member.id,
|
||||
email=member.email,
|
||||
name=member.name,
|
||||
role=member.role,
|
||||
is_super_admin=member.is_super_admin,
|
||||
is_active=member.is_active,
|
||||
account_role=member.account_role,
|
||||
created_at=member.created_at,
|
||||
last_login=member.last_login,
|
||||
deleted_at=member.deleted_at,
|
||||
)
|
||||
)
|
||||
|
||||
pending_invites_result = await db.execute(
|
||||
select(AccountInvite.account_id, func.count(AccountInvite.id))
|
||||
.where(
|
||||
AccountInvite.account_id.in_(account_ids),
|
||||
AccountInvite.used_at.is_(None),
|
||||
)
|
||||
.group_by(AccountInvite.account_id)
|
||||
)
|
||||
pending_invites_by_account.update({row[0]: row[1] for row in pending_invites_result.all()})
|
||||
|
||||
for account_id in account_ids:
|
||||
usage = await get_account_usage(account_id, db)
|
||||
usage_by_account[account_id] = AdminAccountUsageSummary(
|
||||
tree_count=usage.get("tree_count", 0),
|
||||
session_count_this_month=usage.get("session_count_this_month", 0),
|
||||
)
|
||||
|
||||
items = [
|
||||
AdminAccountListItem(
|
||||
id=row.Account.id,
|
||||
name=row.Account.name,
|
||||
display_code=row.Account.display_code,
|
||||
created_at=row.Account.created_at,
|
||||
owner_id=row.Account.owner_id,
|
||||
owner=(
|
||||
AdminAccountOwnerSummary(
|
||||
id=row.owner_user_id,
|
||||
name=row.owner_name,
|
||||
email=row.owner_email,
|
||||
) if row.owner_user_id and row.owner_name and row.owner_email else None
|
||||
),
|
||||
subscription=(
|
||||
AdminAccountSubscriptionSummary(
|
||||
id=row.subscription_id,
|
||||
plan=row.subscription_plan,
|
||||
status=row.subscription_status,
|
||||
billing_interval=row.subscription_billing_interval,
|
||||
current_period_end=row.subscription_current_period_end,
|
||||
cancel_at_period_end=row.subscription_cancel_at_period_end or False,
|
||||
) if row.subscription_id and row.subscription_plan and row.subscription_status else None
|
||||
),
|
||||
usage=usage_by_account.get(row.Account.id, AdminAccountUsageSummary()),
|
||||
member_count=len(members_by_account.get(row.Account.id, [])),
|
||||
active_member_count=sum(1 for member in members_by_account.get(row.Account.id, []) if member.is_active),
|
||||
pending_invite_count=pending_invites_by_account.get(row.Account.id, 0),
|
||||
sso_enabled=row.Account.sso_enabled,
|
||||
branding_company_name=row.Account.branding_company_name,
|
||||
members=members_by_account.get(row.Account.id, []),
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
|
||||
return AdminAccountListResponse(
|
||||
items=items,
|
||||
total=total,
|
||||
page=page,
|
||||
per_page=size,
|
||||
)
|
||||
|
||||
|
||||
def _generate_display_code() -> str:
|
||||
@@ -71,6 +311,192 @@ def _generate_display_code() -> str:
|
||||
return ''.join(secrets.choice(chars) for _ in range(8))
|
||||
|
||||
|
||||
async def _generate_unique_display_code(db: AsyncSession) -> str:
|
||||
"""Generate a unique display code for a new account."""
|
||||
while True:
|
||||
display_code = _generate_display_code()
|
||||
existing = await db.execute(select(Account.id).where(Account.display_code == display_code))
|
||||
if existing.scalar_one_or_none() is None:
|
||||
return display_code
|
||||
|
||||
|
||||
async def _get_account_detail_payload(
|
||||
account_id: UUID,
|
||||
db: AsyncSession,
|
||||
include_archived: bool = False,
|
||||
) -> AdminAccountDetailResponse:
|
||||
owner_user = aliased(User)
|
||||
result = await db.execute(
|
||||
select(
|
||||
Account,
|
||||
owner_user.id.label("owner_user_id"),
|
||||
owner_user.name.label("owner_name"),
|
||||
owner_user.email.label("owner_email"),
|
||||
Subscription.id.label("subscription_id"),
|
||||
Subscription.plan.label("subscription_plan"),
|
||||
Subscription.status.label("subscription_status"),
|
||||
Subscription.billing_interval.label("subscription_billing_interval"),
|
||||
Subscription.current_period_end.label("subscription_current_period_end"),
|
||||
Subscription.cancel_at_period_end.label("subscription_cancel_at_period_end"),
|
||||
)
|
||||
.outerjoin(owner_user, Account.owner_id == owner_user.id)
|
||||
.outerjoin(Subscription, Subscription.account_id == Account.id)
|
||||
.where(Account.id == account_id)
|
||||
)
|
||||
row = result.one_or_none()
|
||||
if not row:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found")
|
||||
|
||||
members_query = select(User).where(User.account_id == account_id).order_by(User.created_at.asc())
|
||||
if not include_archived:
|
||||
members_query = members_query.where(User.deleted_at.is_(None))
|
||||
members_result = await db.execute(members_query)
|
||||
members = [
|
||||
AdminAccountMember(
|
||||
id=member.id,
|
||||
email=member.email,
|
||||
name=member.name,
|
||||
role=member.role,
|
||||
is_super_admin=member.is_super_admin,
|
||||
is_active=member.is_active,
|
||||
account_role=member.account_role,
|
||||
created_at=member.created_at,
|
||||
last_login=member.last_login,
|
||||
deleted_at=member.deleted_at,
|
||||
)
|
||||
for member in members_result.scalars().all()
|
||||
]
|
||||
|
||||
invites_result = await db.execute(
|
||||
select(AccountInvite)
|
||||
.where(AccountInvite.account_id == account_id)
|
||||
.order_by(AccountInvite.created_at.desc())
|
||||
)
|
||||
invites = [
|
||||
AdminAccountInviteSummary(
|
||||
id=invite.id,
|
||||
email=invite.email,
|
||||
role=invite.role,
|
||||
expires_at=invite.expires_at,
|
||||
created_at=invite.created_at,
|
||||
used_at=invite.used_at,
|
||||
)
|
||||
for invite in invites_result.scalars().all()
|
||||
if invite.used_at is None
|
||||
]
|
||||
|
||||
usage = await get_account_usage(account_id, db)
|
||||
|
||||
return AdminAccountDetailResponse(
|
||||
id=row.Account.id,
|
||||
name=row.Account.name,
|
||||
display_code=row.Account.display_code,
|
||||
created_at=row.Account.created_at,
|
||||
owner_id=row.Account.owner_id,
|
||||
owner=(
|
||||
AdminAccountOwnerSummary(
|
||||
id=row.owner_user_id,
|
||||
name=row.owner_name,
|
||||
email=row.owner_email,
|
||||
) if row.owner_user_id and row.owner_name and row.owner_email else None
|
||||
),
|
||||
subscription=(
|
||||
AdminAccountSubscriptionSummary(
|
||||
id=row.subscription_id,
|
||||
plan=row.subscription_plan,
|
||||
status=row.subscription_status,
|
||||
billing_interval=row.subscription_billing_interval,
|
||||
current_period_end=row.subscription_current_period_end,
|
||||
cancel_at_period_end=row.subscription_cancel_at_period_end or False,
|
||||
) if row.subscription_id and row.subscription_plan and row.subscription_status else None
|
||||
),
|
||||
usage=AdminAccountUsageSummary(
|
||||
tree_count=usage.get("tree_count", 0),
|
||||
session_count_this_month=usage.get("session_count_this_month", 0),
|
||||
),
|
||||
member_count=len(members),
|
||||
active_member_count=sum(1 for member in members if member.is_active),
|
||||
pending_invite_count=len(invites),
|
||||
sso_enabled=row.Account.sso_enabled,
|
||||
branding_company_name=row.Account.branding_company_name,
|
||||
members=members,
|
||||
invites=invites,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/accounts", response_model=AdminAccountDetailResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_account(
|
||||
data: AdminAccountCreate,
|
||||
db: Annotated[AsyncSession, Depends(get_admin_db)],
|
||||
current_user: Annotated[User, Depends(require_admin)],
|
||||
):
|
||||
"""Create a new account without requiring an initial user."""
|
||||
owner_id = None
|
||||
if data.owner_email:
|
||||
result = await db.execute(select(User).where(User.email == data.owner_email.strip()))
|
||||
owner = result.scalar_one_or_none()
|
||||
if not owner:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No user found with email '{data.owner_email}'")
|
||||
owner_id = owner.id
|
||||
|
||||
display_code = await _generate_unique_display_code(db)
|
||||
new_account = Account(
|
||||
name=data.name.strip(),
|
||||
display_code=display_code,
|
||||
owner_id=owner_id,
|
||||
)
|
||||
db.add(new_account)
|
||||
await db.flush()
|
||||
|
||||
new_subscription = Subscription(
|
||||
account_id=new_account.id,
|
||||
plan=data.plan,
|
||||
status="active",
|
||||
)
|
||||
db.add(new_subscription)
|
||||
|
||||
await log_audit(
|
||||
db, current_user.id, "account.create_admin", "account", new_account.id,
|
||||
{"name": new_account.name, "plan": data.plan, "owner_email": data.owner_email},
|
||||
)
|
||||
await db.commit()
|
||||
return await _get_account_detail_payload(new_account.id, db)
|
||||
|
||||
|
||||
@router.get("/accounts/{account_id}", response_model=AdminAccountDetailResponse)
|
||||
async def get_account_detail(
|
||||
account_id: UUID,
|
||||
db: Annotated[AsyncSession, Depends(get_admin_db)],
|
||||
current_user: Annotated[User, Depends(require_admin)],
|
||||
include_archived: bool = Query(False),
|
||||
):
|
||||
"""Get detailed account information for admin management."""
|
||||
return await _get_account_detail_payload(account_id, db, include_archived=include_archived)
|
||||
|
||||
|
||||
@router.put("/accounts/{account_id}", response_model=AdminAccountDetailResponse)
|
||||
async def update_account(
|
||||
account_id: UUID,
|
||||
data: AdminAccountUpdate,
|
||||
db: Annotated[AsyncSession, Depends(get_admin_db)],
|
||||
current_user: Annotated[User, Depends(require_admin)],
|
||||
):
|
||||
"""Update account settings from the admin panel."""
|
||||
result = await db.execute(select(Account).where(Account.id == account_id))
|
||||
account = result.scalar_one_or_none()
|
||||
if not account:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found")
|
||||
|
||||
old_name = account.name
|
||||
account.name = data.name.strip()
|
||||
await log_audit(
|
||||
db, current_user.id, "account.update_admin", "account", account.id,
|
||||
{"old_name": old_name, "new_name": account.name},
|
||||
)
|
||||
await db.commit()
|
||||
return await _get_account_detail_payload(account.id, db)
|
||||
|
||||
|
||||
@router.post("/users", response_model=AdminUserCreateResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_user(
|
||||
data: AdminUserCreate,
|
||||
@@ -516,6 +942,28 @@ async def _get_user_subscription(user_id: UUID, db: AsyncSession) -> tuple[User,
|
||||
return user, subscription
|
||||
|
||||
|
||||
async def _get_account_subscription(account_id: UUID, db: AsyncSession) -> tuple[Account, Subscription]:
|
||||
"""Helper to load account and its subscription."""
|
||||
account_result = await db.execute(select(Account).where(Account.id == account_id))
|
||||
account = account_result.scalar_one_or_none()
|
||||
if not account:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found")
|
||||
|
||||
sub_result = await db.execute(
|
||||
select(Subscription).where(Subscription.account_id == account.id)
|
||||
)
|
||||
subscription = sub_result.scalar_one_or_none()
|
||||
if not subscription:
|
||||
subscription = Subscription(
|
||||
account_id=account.id,
|
||||
plan="free",
|
||||
status="active",
|
||||
)
|
||||
db.add(subscription)
|
||||
await db.flush()
|
||||
return account, subscription
|
||||
|
||||
|
||||
@router.put("/users/{user_id}/subscription/plan")
|
||||
async def update_user_plan(
|
||||
user_id: UUID,
|
||||
@@ -535,6 +983,31 @@ async def update_user_plan(
|
||||
return {"plan": subscription.plan, "status": subscription.status}
|
||||
|
||||
|
||||
@router.put("/accounts/{account_id}/subscription/plan")
|
||||
async def update_account_plan(
|
||||
account_id: UUID,
|
||||
data: SubscriptionPlanUpdate,
|
||||
db: Annotated[AsyncSession, Depends(get_admin_db)],
|
||||
current_user: Annotated[User, Depends(require_admin)],
|
||||
):
|
||||
"""Change an account subscription plan (super admin only)."""
|
||||
if data.plan not in ("free", "pro", "team"):
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid plan")
|
||||
account, subscription = await _get_account_subscription(account_id, db)
|
||||
old_plan = subscription.plan
|
||||
subscription.plan = data.plan
|
||||
await log_audit(
|
||||
db,
|
||||
current_user.id,
|
||||
"subscription.plan_change",
|
||||
"subscription",
|
||||
subscription.id,
|
||||
{"old_plan": old_plan, "new_plan": data.plan, "account_id": str(account_id)},
|
||||
)
|
||||
await db.commit()
|
||||
return {"plan": subscription.plan, "status": subscription.status}
|
||||
|
||||
|
||||
@router.put("/users/{user_id}/subscription/extend-trial")
|
||||
async def extend_user_trial(
|
||||
user_id: UUID,
|
||||
@@ -565,6 +1038,43 @@ async def extend_user_trial(
|
||||
"current_period_end": subscription.current_period_end}
|
||||
|
||||
|
||||
@router.put("/accounts/{account_id}/subscription/extend-trial")
|
||||
async def extend_account_trial(
|
||||
account_id: UUID,
|
||||
data: ExtendTrialRequest,
|
||||
db: Annotated[AsyncSession, Depends(get_admin_db)],
|
||||
current_user: Annotated[User, Depends(require_admin)],
|
||||
):
|
||||
"""Extend or start a trial for an account subscription (super admin only)."""
|
||||
if data.days < 1 or data.days > 90:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Days must be 1-90")
|
||||
account, subscription = await _get_account_subscription(account_id, db)
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
if subscription.status == "trialing" and subscription.current_period_end:
|
||||
new_end = subscription.current_period_end + timedelta(days=data.days)
|
||||
else:
|
||||
subscription.status = "trialing"
|
||||
subscription.current_period_start = now
|
||||
new_end = now + timedelta(days=data.days)
|
||||
|
||||
subscription.current_period_end = new_end
|
||||
await log_audit(
|
||||
db,
|
||||
current_user.id,
|
||||
"subscription.extend_trial",
|
||||
"subscription",
|
||||
subscription.id,
|
||||
{"days": data.days, "new_end": new_end.isoformat(), "account_id": str(account.id)},
|
||||
)
|
||||
await db.commit()
|
||||
return {
|
||||
"plan": subscription.plan,
|
||||
"status": subscription.status,
|
||||
"current_period_end": subscription.current_period_end,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/users/{user_id}/password-reset", response_model=AdminPasswordResetResponse)
|
||||
async def admin_reset_password(
|
||||
user_id: UUID,
|
||||
|
||||
@@ -15,7 +15,7 @@ from datetime import datetime
|
||||
from typing import Annotated, Optional
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, status
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query, Request, status
|
||||
from sqlalchemy import or_, select, func, text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
@@ -452,6 +452,13 @@ async def resolve_session(
|
||||
|
||||
|
||||
# ── Escalate ──
|
||||
#
|
||||
# Thin shim over HandoffManager. The legacy `flowpilot_engine.escalate_session`
|
||||
# is no longer the source of truth — every escalation now creates a
|
||||
# SessionHandoff row, fans out via the SSE bus, dispatches AppNotification +
|
||||
# external channels via notify(), and emails per-user. EscalateModal and the
|
||||
# /handoff endpoint both funnel through here / through HandoffManager so the
|
||||
# senior-pickup magic-moment screen works regardless of entry point.
|
||||
|
||||
@router.post("/{session_id}/escalate", response_model=SessionCloseResponse)
|
||||
@limiter.limit("15/minute")
|
||||
@@ -459,25 +466,62 @@ async def escalate_session(
|
||||
request: Request,
|
||||
session_id: UUID,
|
||||
data: EscalateSessionRequest,
|
||||
background_tasks: BackgroundTasks,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
):
|
||||
"""Escalate a FlowPilot session to another engineer."""
|
||||
"""Escalate a FlowPilot session — unified through HandoffManager."""
|
||||
from app.services.handoff_manager import HandoffManager, enrich_escalation_async
|
||||
|
||||
# Owner-only — matches the original constraint on flowpilot_engine.escalate_session.
|
||||
session_result = await db.execute(
|
||||
select(AISession).where(
|
||||
AISession.id == session_id,
|
||||
AISession.user_id == current_user.id,
|
||||
)
|
||||
)
|
||||
session = session_result.scalar_one_or_none()
|
||||
if not session:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Session not found"
|
||||
)
|
||||
|
||||
manager = HandoffManager(db)
|
||||
try:
|
||||
result = await flowpilot_engine.escalate_session(
|
||||
handoff = await manager.create_handoff(
|
||||
session_id=session_id,
|
||||
request=data,
|
||||
intent="escalate",
|
||||
engineer_notes=data.escalation_reason,
|
||||
user_id=current_user.id,
|
||||
db=db,
|
||||
priority="normal",
|
||||
target_user_id=data.escalated_to_id,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
except PermissionError as e:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=str(e))
|
||||
|
||||
documentation, psa_result = await manager.finalize_escalation(
|
||||
handoff, session, current_user.id
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
return result
|
||||
|
||||
await manager.dispatch_escalation_notifications(handoff)
|
||||
|
||||
# AI enrichment (Sonnet assessment + enhanced escalation_package) runs
|
||||
# in the background so the escalating engineer doesn't wait on
|
||||
# 15-25s of model latency. Result lands on the handoff row when ready;
|
||||
# the senior's magic-moment screen reads it at pickup time.
|
||||
background_tasks.add_task(
|
||||
enrich_escalation_async, handoff.id, current_user.id
|
||||
)
|
||||
|
||||
return SessionCloseResponse(
|
||||
session_id=session.id,
|
||||
status=session.status,
|
||||
documentation=documentation,
|
||||
**psa_result,
|
||||
)
|
||||
|
||||
|
||||
# ── Pause ──
|
||||
@@ -644,7 +688,7 @@ async def get_escalation_queue(
|
||||
select(AISession)
|
||||
.where(
|
||||
scope_filter,
|
||||
AISession.status == "requesting_escalation",
|
||||
AISession.status.in_(("requesting_escalation", "escalated")),
|
||||
)
|
||||
.order_by(AISession.created_at.desc())
|
||||
)
|
||||
@@ -838,13 +882,25 @@ async def list_sessions(
|
||||
date_to: Optional[datetime] = Query(None),
|
||||
q: Optional[str] = Query(None, min_length=2, max_length=200),
|
||||
):
|
||||
"""List the current user's AI sessions (owned or picked up)."""
|
||||
"""List the current user's AI sessions (owned or picked up).
|
||||
|
||||
"Picked up" includes both the legacy escalation_package.picked_up_by
|
||||
marker (set by flowpilot_engine.pickup_session) AND the new
|
||||
escalated_to_id field (set by HandoffManager.claim_session for the
|
||||
unified handoff/escalate path). Without the escalated_to_id branch
|
||||
the senior tech wouldn't see a session they just claimed in their
|
||||
chat sidebar — the picked-up session lands as the active chat with
|
||||
no entry in the list, which is what the user reported as "4 versions
|
||||
of the session" (their unrelated owned sessions show up while the
|
||||
claimed one is invisible).
|
||||
"""
|
||||
user_id_str = str(current_user.id)
|
||||
query = (
|
||||
select(AISession)
|
||||
.where(
|
||||
or_(
|
||||
AISession.user_id == current_user.id,
|
||||
AISession.escalated_to_id == current_user.id,
|
||||
AISession.escalation_package["picked_up_by"].as_string() == user_id_str,
|
||||
)
|
||||
)
|
||||
@@ -901,10 +957,21 @@ async def get_session(
|
||||
if not session:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Session not found")
|
||||
|
||||
# Allow access if user is owner, escalation target, or picked-up handler
|
||||
# Allow access if user is owner, escalation target, or picked-up handler.
|
||||
# Sessions in transit (requesting_escalation / escalated) are also
|
||||
# readable by any account member — the whole point of escalation is that
|
||||
# other techs can see the context before claiming. Tenant boundary is
|
||||
# enforced by RLS on the underlying query, so account-scope is the right
|
||||
# ceiling for in-transit reads.
|
||||
pkg = session.escalation_package or {}
|
||||
is_handler = pkg.get("picked_up_by") == str(current_user.id)
|
||||
if session.user_id != current_user.id and session.escalated_to_id != current_user.id and not is_handler:
|
||||
is_in_transit = session.status in ("requesting_escalation", "escalated")
|
||||
if (
|
||||
session.user_id != current_user.id
|
||||
and session.escalated_to_id != current_user.id
|
||||
and not is_handler
|
||||
and not is_in_transit
|
||||
):
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Session not found")
|
||||
|
||||
return _build_session_detail(session)
|
||||
|
||||
120
backend/app/api/endpoints/device_types.py
Normal file
120
backend/app/api/endpoints/device_types.py
Normal file
@@ -0,0 +1,120 @@
|
||||
"""Device types API endpoints."""
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import select, or_
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.api.deps import get_current_active_user
|
||||
from app.models.user import User
|
||||
from app.models.device_type import DeviceType
|
||||
from app.schemas.device_type import (
|
||||
DeviceTypeCreate,
|
||||
DeviceTypeUpdate,
|
||||
DeviceTypeResponse,
|
||||
)
|
||||
from app.core.service_account import PLATFORM_ACCOUNT_ID
|
||||
|
||||
router = APIRouter(prefix="/device-types", tags=["device-types"])
|
||||
|
||||
|
||||
@router.get("/", response_model=list[DeviceTypeResponse])
|
||||
async def list_device_types(
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> list[DeviceTypeResponse]:
|
||||
stmt = (
|
||||
select(DeviceType)
|
||||
.where(
|
||||
or_(
|
||||
DeviceType.account_id == PLATFORM_ACCOUNT_ID,
|
||||
DeviceType.account_id == current_user.account_id,
|
||||
)
|
||||
)
|
||||
.order_by(DeviceType.category, DeviceType.sort_order, DeviceType.label)
|
||||
)
|
||||
result = await db.execute(stmt)
|
||||
rows = result.scalars().all()
|
||||
return [DeviceTypeResponse.model_validate(r) for r in rows]
|
||||
|
||||
|
||||
@router.post("/", response_model=DeviceTypeResponse, status_code=201)
|
||||
async def create_device_type(
|
||||
data: DeviceTypeCreate,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> DeviceTypeResponse:
|
||||
existing = await db.execute(
|
||||
select(DeviceType).where(
|
||||
DeviceType.slug == data.slug,
|
||||
DeviceType.account_id == current_user.account_id,
|
||||
)
|
||||
)
|
||||
if existing.scalar_one_or_none():
|
||||
raise HTTPException(status_code=409, detail=f"Device type '{data.slug}' already exists for your account")
|
||||
|
||||
system_existing = await db.execute(
|
||||
select(DeviceType).where(
|
||||
DeviceType.slug == data.slug,
|
||||
DeviceType.account_id == PLATFORM_ACCOUNT_ID,
|
||||
)
|
||||
)
|
||||
if system_existing.scalar_one_or_none():
|
||||
raise HTTPException(status_code=409, detail=f"Device type '{data.slug}' conflicts with a system type")
|
||||
|
||||
device_type = DeviceType(
|
||||
slug=data.slug,
|
||||
label=data.label,
|
||||
category=data.category,
|
||||
is_system=False,
|
||||
account_id=current_user.account_id,
|
||||
sort_order=data.sort_order,
|
||||
)
|
||||
db.add(device_type)
|
||||
await db.commit()
|
||||
await db.refresh(device_type)
|
||||
return DeviceTypeResponse.model_validate(device_type)
|
||||
|
||||
|
||||
@router.put("/{device_type_id}", response_model=DeviceTypeResponse)
|
||||
async def update_device_type(
|
||||
device_type_id: UUID,
|
||||
data: DeviceTypeUpdate,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> DeviceTypeResponse:
|
||||
device_type = await db.get(DeviceType, device_type_id)
|
||||
if not device_type:
|
||||
raise HTTPException(status_code=404, detail="Device type not found")
|
||||
if device_type.is_system:
|
||||
raise HTTPException(status_code=403, detail="Cannot modify system device types")
|
||||
if device_type.account_id != current_user.account_id:
|
||||
raise HTTPException(status_code=404, detail="Device type not found")
|
||||
|
||||
update_data = data.model_dump(exclude_unset=True)
|
||||
for field, value in update_data.items():
|
||||
setattr(device_type, field, value)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(device_type)
|
||||
return DeviceTypeResponse.model_validate(device_type)
|
||||
|
||||
|
||||
@router.delete("/{device_type_id}", status_code=204)
|
||||
async def delete_device_type(
|
||||
device_type_id: UUID,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> None:
|
||||
device_type = await db.get(DeviceType, device_type_id)
|
||||
if not device_type:
|
||||
raise HTTPException(status_code=404, detail="Device type not found")
|
||||
if device_type.is_system:
|
||||
raise HTTPException(status_code=403, detail="Cannot delete system device types")
|
||||
if device_type.account_id != current_user.account_id:
|
||||
raise HTTPException(status_code=404, detail="Device type not found")
|
||||
|
||||
await db.delete(device_type)
|
||||
await db.commit()
|
||||
221
backend/app/api/endpoints/draft_templates.py
Normal file
221
backend/app/api/endpoints/draft_templates.py
Normal file
@@ -0,0 +1,221 @@
|
||||
"""Draft template endpoints — Phase 6 post-resolve templatization flow.
|
||||
|
||||
Engineers who picked "Run now, templatize after resolve" on the three-option
|
||||
dialog (Phase 5) generate a `draft_templates` row at decision time. After
|
||||
the session resolves, the TemplatizePrompt component lets them either:
|
||||
- Accept → promotes the draft to a real `script_templates` row
|
||||
- Reject → marks the draft rejected, no library entry created
|
||||
|
||||
The Script Library sidebar uses the list endpoint to surface a
|
||||
"X drafts ready to review" badge for the account.
|
||||
|
||||
See FLOWPILOT-MIGRATION.md Section 5.3.
|
||||
"""
|
||||
import logging
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.deps import get_current_active_user, get_db, require_engineer_or_admin
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.draft_template import DraftTemplate
|
||||
from app.models.script_template import ScriptCategory, ScriptTemplate
|
||||
from app.models.user import User
|
||||
from app.schemas.draft_template import (
|
||||
DraftTemplateAcceptRequest,
|
||||
DraftTemplateAcceptResponse,
|
||||
DraftTemplateListResponse,
|
||||
DraftTemplateRejectResponse,
|
||||
DraftTemplateResponse,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/draft-templates", tags=["draft-templates"])
|
||||
|
||||
|
||||
def _slugify(name: str) -> str:
|
||||
"""Same slug rule as scripts.create_template — lowercase, kebab-case, ASCII."""
|
||||
return re.sub(r"[^a-z0-9]+", "-", name.lower()).strip("-")
|
||||
|
||||
|
||||
# ── List ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("", response_model=DraftTemplateListResponse)
|
||||
async def list_drafts(
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
pending_only: bool = True,
|
||||
) -> DraftTemplateListResponse:
|
||||
"""List drafts for the current user's account.
|
||||
|
||||
Defaults to pending-only — that's what the Script Library badge counts
|
||||
and what the post-resolve TemplatizePrompt iterates over. Pass
|
||||
`pending_only=false` to include accepted/rejected for an audit view.
|
||||
"""
|
||||
stmt = select(DraftTemplate).order_by(DraftTemplate.created_at.desc())
|
||||
if pending_only:
|
||||
stmt = stmt.where(DraftTemplate.status == "pending")
|
||||
result = await db.execute(stmt)
|
||||
drafts = list(result.scalars().all())
|
||||
return DraftTemplateListResponse(
|
||||
drafts=[DraftTemplateResponse.model_validate(d) for d in drafts]
|
||||
)
|
||||
|
||||
|
||||
# ── Get one ──────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/{draft_id}", response_model=DraftTemplateResponse)
|
||||
async def get_draft(
|
||||
draft_id: UUID,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> DraftTemplateResponse:
|
||||
draft = await _load_draft_or_404(db, draft_id)
|
||||
return DraftTemplateResponse.model_validate(draft)
|
||||
|
||||
|
||||
# ── Accept ───────────────────────────────────────────────────────────────
|
||||
|
||||
@router.post(
|
||||
"/{draft_id}/accept",
|
||||
response_model=DraftTemplateAcceptResponse,
|
||||
status_code=201,
|
||||
)
|
||||
async def accept_draft(
|
||||
draft_id: UUID,
|
||||
body: DraftTemplateAcceptRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> DraftTemplateAcceptResponse:
|
||||
"""Promote a draft to a real `script_templates` row.
|
||||
|
||||
Provenance fields (`source_session_id`, `source_user_id`,
|
||||
`source_ticket_ref`) are copied so the Script Library can render the
|
||||
"generated from CW #X · resolved by Y · used N times" chip.
|
||||
|
||||
On success: draft.status='accepted', draft.promoted_template_id set,
|
||||
draft.resolved_at set. The new template is owned by the engineer's team
|
||||
(matches scripts.create_template's behavior).
|
||||
|
||||
Returns 409 if the draft is already accepted/rejected.
|
||||
"""
|
||||
draft = await _load_draft_or_404(db, draft_id)
|
||||
if draft.status != "pending":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"Draft is already {draft.status}",
|
||||
)
|
||||
|
||||
# Validate the category exists and belongs to (or is global for) this account.
|
||||
cat_result = await db.execute(
|
||||
select(ScriptCategory).where(
|
||||
ScriptCategory.id == body.category_id,
|
||||
ScriptCategory.is_active == True, # noqa: E712
|
||||
)
|
||||
)
|
||||
if cat_result.scalar_one_or_none() is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="category_id does not reference an active script category",
|
||||
)
|
||||
|
||||
# Look up source-session ticket ref for the provenance chip. RLS makes
|
||||
# cross-account ai_session lookup impossible — the draft must belong to
|
||||
# the same account as the requesting user.
|
||||
source_session = (
|
||||
await db.execute(
|
||||
select(AISession).where(AISession.id == draft.source_session_id)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
source_ticket_ref = (
|
||||
f"CW #{source_session.psa_ticket_id}"
|
||||
if source_session and source_session.psa_ticket_id
|
||||
else None
|
||||
)
|
||||
|
||||
slug = _slugify(body.name)
|
||||
|
||||
template = ScriptTemplate(
|
||||
category_id=body.category_id,
|
||||
team_id=current_user.team_id,
|
||||
account_id=current_user.account_id,
|
||||
created_by=current_user.id,
|
||||
name=body.name,
|
||||
slug=slug,
|
||||
description=body.description,
|
||||
script_body=body.edited_body or draft.script_body,
|
||||
parameters_schema=body.parameters_schema,
|
||||
# FlowPilot provenance — drives the Script Library chip.
|
||||
source_session_id=draft.source_session_id,
|
||||
source_user_id=draft.source_user_id,
|
||||
source_ticket_ref=source_ticket_ref,
|
||||
)
|
||||
db.add(template)
|
||||
await db.flush() # populate template.id
|
||||
|
||||
draft.status = "accepted"
|
||||
draft.promoted_template_id = template.id
|
||||
draft.resolved_at = datetime.now(timezone.utc)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(template)
|
||||
|
||||
return DraftTemplateAcceptResponse(
|
||||
draft_id=draft.id,
|
||||
promoted_template_id=template.id,
|
||||
template_slug=template.slug,
|
||||
)
|
||||
|
||||
|
||||
# ── Reject ───────────────────────────────────────────────────────────────
|
||||
|
||||
@router.post("/{draft_id}/reject", response_model=DraftTemplateRejectResponse)
|
||||
async def reject_draft(
|
||||
draft_id: UUID,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> DraftTemplateRejectResponse:
|
||||
"""Mark a draft rejected.
|
||||
|
||||
No template is created. The row stays for audit (so a team admin can see
|
||||
the engineer reviewed and explicitly declined). Returns 409 on a draft
|
||||
that's already accepted/rejected.
|
||||
"""
|
||||
draft = await _load_draft_or_404(db, draft_id)
|
||||
if draft.status != "pending":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"Draft is already {draft.status}",
|
||||
)
|
||||
draft.status = "rejected"
|
||||
draft.resolved_at = datetime.now(timezone.utc)
|
||||
await db.commit()
|
||||
return DraftTemplateRejectResponse(draft_id=draft.id, status="rejected")
|
||||
|
||||
|
||||
# ── Helpers ─────────────────────────────────────────────────────────────
|
||||
|
||||
async def _load_draft_or_404(
|
||||
db: AsyncSession, draft_id: UUID
|
||||
) -> DraftTemplate:
|
||||
"""RLS-scoped draft load. 404 covers missing + cross-tenant."""
|
||||
result = await db.execute(
|
||||
select(DraftTemplate).where(DraftTemplate.id == draft_id)
|
||||
)
|
||||
draft = result.scalar_one_or_none()
|
||||
if draft is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Draft template not found",
|
||||
)
|
||||
return draft
|
||||
@@ -3,8 +3,10 @@
|
||||
Endpoints:
|
||||
GET /analytics/flowpilot?period=30d — Main dashboard data
|
||||
GET /analytics/flowpilot/knowledge-gaps — Knowledge gap report
|
||||
GET /analytics/flowpilot/escalations?period=30d — Escalation handoff metrics
|
||||
"""
|
||||
import logging
|
||||
import statistics
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Annotated, Optional
|
||||
|
||||
@@ -13,10 +15,17 @@ from sqlalchemy import select, func, case, cast, Date, extract
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.rate_limit import limiter
|
||||
from app.api.deps import get_current_active_user, get_db, require_team_admin
|
||||
from app.api.deps import (
|
||||
get_current_active_user,
|
||||
get_db,
|
||||
require_engineer_or_admin,
|
||||
require_team_admin,
|
||||
)
|
||||
from app.models.user import User
|
||||
from app.models.tree import Tree
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.ai_session_step import AISessionStep
|
||||
from app.models.session_handoff import SessionHandoff
|
||||
from app.models.flow_proposal import FlowProposal
|
||||
from app.models.psa_activity_log import PsaActivityLog
|
||||
from app.models.psa_post_log import PsaPostLog
|
||||
@@ -36,6 +45,7 @@ from app.schemas.flowpilot_analytics import (
|
||||
EnhancedPsaMetrics,
|
||||
PsaFunnel,
|
||||
PsaDailyTrend,
|
||||
EscalationMetrics,
|
||||
)
|
||||
from app.services.knowledge_gap_service import get_knowledge_gaps, KnowledgeGapReport
|
||||
|
||||
@@ -727,3 +737,104 @@ async def get_enhanced_psa_metrics(
|
||||
push_funnel=push_funnel,
|
||||
daily_trend=daily_trend,
|
||||
)
|
||||
|
||||
|
||||
# ─── Escalation Mode metrics (wedge stat for /escalations queue + analytics page)
|
||||
#
|
||||
# Pulls all (handoff.claimed_at, first_step_after_claim.created_at) pairs in the
|
||||
# window and aggregates avg/median/p95 of the delta in Python. Pilot scale
|
||||
# (~1k rows max per account per month) makes this cheaper and clearer than
|
||||
# Postgres percentile_cont gymnastics.
|
||||
#
|
||||
# IMPORTANT: this is the in-product metric only. The "minutes recovered"
|
||||
# sales claim requires manual baseline measurement (see The Assignment in
|
||||
# docs/plans/2026-04-27-escalation-mode-wedge-design.md).
|
||||
|
||||
|
||||
@router.get("/escalations", response_model=EscalationMetrics)
|
||||
@limiter.limit("30/minute")
|
||||
async def get_escalation_metrics(
|
||||
request: Request,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
period: str = Query("30d", pattern="^(7d|30d|90d)$"),
|
||||
) -> EscalationMetrics:
|
||||
"""Time-to-first-action after escalation claim, account-scoped.
|
||||
|
||||
Returns:
|
||||
n_handoffs_claimed: handoffs in window that were claimed by a senior.
|
||||
n_handoffs_with_action: subset where the senior took at least one
|
||||
action (an ai_session_step row created after claimed_at).
|
||||
avg/median/p95_seconds_to_first_action: aggregates of
|
||||
(first_step.created_at - claimed_at) in seconds.
|
||||
|
||||
Excludes handoffs where claimed_at IS NULL (never claimed) and handoffs
|
||||
where no ai_session_step was created after the claim. Both are
|
||||
counted — n_handoffs_claimed includes "no action yet" handoffs so the
|
||||
conversion rate is visible.
|
||||
"""
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="No account"
|
||||
)
|
||||
|
||||
account_id = current_user.account_id
|
||||
period_start = _get_period_start(period)
|
||||
|
||||
# First-action timestamp per handoff via correlated scalar subquery.
|
||||
first_action_subq = (
|
||||
select(func.min(AISessionStep.created_at))
|
||||
.where(
|
||||
AISessionStep.session_id == SessionHandoff.session_id,
|
||||
AISessionStep.created_at > SessionHandoff.claimed_at,
|
||||
)
|
||||
.correlate(SessionHandoff)
|
||||
.scalar_subquery()
|
||||
)
|
||||
|
||||
rows = (
|
||||
await db.execute(
|
||||
select(
|
||||
SessionHandoff.claimed_at,
|
||||
first_action_subq.label("first_action_at"),
|
||||
).where(
|
||||
SessionHandoff.account_id == account_id,
|
||||
SessionHandoff.claimed_at.isnot(None),
|
||||
SessionHandoff.claimed_at >= period_start,
|
||||
)
|
||||
)
|
||||
).all()
|
||||
|
||||
n_handoffs_claimed = len(rows)
|
||||
deltas: list[float] = []
|
||||
for claimed_at, first_action_at in rows:
|
||||
if first_action_at is None:
|
||||
continue
|
||||
delta_s = (first_action_at - claimed_at).total_seconds()
|
||||
# Floor at zero — clock drift between rows could in theory yield a
|
||||
# tiny negative if a step's created_at races claimed_at. Surface as
|
||||
# 0s rather than absurd negative deltas.
|
||||
if delta_s < 0:
|
||||
delta_s = 0.0
|
||||
deltas.append(delta_s)
|
||||
|
||||
n_handoffs_with_action = len(deltas)
|
||||
if n_handoffs_with_action == 0:
|
||||
return EscalationMetrics(
|
||||
period=period,
|
||||
n_handoffs_claimed=n_handoffs_claimed,
|
||||
n_handoffs_with_action=0,
|
||||
)
|
||||
|
||||
sorted_deltas = sorted(deltas)
|
||||
p95_idx = max(0, int(round(0.95 * (n_handoffs_with_action - 1))))
|
||||
|
||||
return EscalationMetrics(
|
||||
period=period,
|
||||
n_handoffs_claimed=n_handoffs_claimed,
|
||||
n_handoffs_with_action=n_handoffs_with_action,
|
||||
avg_seconds_to_first_action=round(statistics.fmean(deltas), 2),
|
||||
median_seconds_to_first_action=round(statistics.median(deltas), 2),
|
||||
p95_seconds_to_first_action=round(sorted_deltas[p95_idx], 2),
|
||||
)
|
||||
|
||||
@@ -194,6 +194,7 @@ async def create_folder(
|
||||
|
||||
new_folder = UserFolder(
|
||||
user_id=current_user.id,
|
||||
account_id=current_user.account_id,
|
||||
name=folder_data.name,
|
||||
color=folder_data.color,
|
||||
icon=folder_data.icon,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""PSA integration endpoints — connection CRUD and test."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
@@ -11,6 +12,8 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from sqlalchemy import delete
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from app.api.deps import get_current_active_user, require_account_owner, require_engineer_or_admin
|
||||
from app.core.database import get_db
|
||||
from app.models.psa_connection import PsaConnection
|
||||
@@ -27,8 +30,20 @@ from app.schemas.psa_connection import (
|
||||
PsaMemberMappingSaveRequest,
|
||||
PsaMemberResponse,
|
||||
AutoMatchResult,
|
||||
PSABoardResponse,
|
||||
)
|
||||
from app.core.config import settings
|
||||
from app.schemas.psa_tickets import (
|
||||
PSAResourceSchema,
|
||||
PSATicketCreatedSchema,
|
||||
PSATicketStatusUpdateSchema,
|
||||
TicketCreatePayloadSchema,
|
||||
PSAPrioritySchema,
|
||||
TicketListResponseSchema,
|
||||
AiParseRequestSchema,
|
||||
AiParseResponseSchema,
|
||||
)
|
||||
import app.services.ticket_service as ticket_svc
|
||||
from app.services.psa.encryption import (
|
||||
decrypt_credentials,
|
||||
encrypt_credentials,
|
||||
@@ -345,16 +360,12 @@ async def update_flowpilot_settings(
|
||||
# ── ticket / status / company endpoints ──────────────────────────
|
||||
|
||||
|
||||
@router.get("/tickets/search", response_model=list[PSATicketSearchResult])
|
||||
async def search_tickets(
|
||||
@router.get("/boards", response_model=list[PSABoardResponse])
|
||||
async def list_boards(
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
query: str = "",
|
||||
board_id: int | None = None,
|
||||
status_id: int | None = None,
|
||||
include_closed: bool = False,
|
||||
):
|
||||
"""Search ConnectWise tickets."""
|
||||
"""List PSA service boards."""
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(status_code=400, detail="User has no account")
|
||||
|
||||
@@ -363,25 +374,321 @@ async def search_tickets(
|
||||
|
||||
try:
|
||||
provider = await get_provider_for_account(current_user.account_id, db)
|
||||
tickets = await provider.search_tickets(
|
||||
query, board_id=board_id, status_id=status_id, include_closed=include_closed
|
||||
boards = await provider.list_boards()
|
||||
return [PSABoardResponse(id=b.id, name=b.name) for b in boards]
|
||||
except PSAError as e:
|
||||
# Boards are optional UI chrome — degrade gracefully rather than surfacing a toast
|
||||
logger.warning("list_boards failed: %s", e)
|
||||
return []
|
||||
|
||||
|
||||
@router.get("/tickets/search", response_model=TicketListResponseSchema)
|
||||
async def search_tickets(
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
query: str = "",
|
||||
board_id: int | None = None,
|
||||
status_id: int | None = None,
|
||||
status_name: str | None = None,
|
||||
include_closed: bool = False,
|
||||
assigned_to_me: bool = False,
|
||||
unassigned: bool = False,
|
||||
board_ids: str = "",
|
||||
priority: str | None = None,
|
||||
company_id: int | None = None,
|
||||
page: int = 1,
|
||||
page_size: int = 25,
|
||||
):
|
||||
"""Search ConnectWise tickets — returns paginated TicketListResponse."""
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(status_code=400, detail="User has no account")
|
||||
|
||||
from app.services.psa.registry import get_provider_for_account
|
||||
from app.services.psa.exceptions import PSAError
|
||||
|
||||
member_identifier: str | None = None
|
||||
if assigned_to_me:
|
||||
conn_result = await db.execute(
|
||||
select(PsaConnection).where(
|
||||
PsaConnection.account_id == current_user.account_id,
|
||||
PsaConnection.is_active.is_(True),
|
||||
)
|
||||
)
|
||||
return [
|
||||
conn = conn_result.scalar_one_or_none()
|
||||
if conn:
|
||||
mapping_result = await db.execute(
|
||||
select(PsaMemberMapping).where(
|
||||
PsaMemberMapping.psa_connection_id == conn.id,
|
||||
PsaMemberMapping.user_id == current_user.id,
|
||||
)
|
||||
)
|
||||
mapping = mapping_result.scalar_one_or_none()
|
||||
if not mapping:
|
||||
return {"items": [], "total": 0, "page": page, "page_size": page_size}
|
||||
try:
|
||||
_provider = await get_provider_for_account(current_user.account_id, db)
|
||||
cw_members = await _provider.list_members()
|
||||
matched = next((m for m in cw_members if m.id == mapping.external_member_id), None)
|
||||
if matched:
|
||||
member_identifier = matched.identifier
|
||||
else:
|
||||
return {"items": [], "total": 0, "page": page, "page_size": page_size}
|
||||
except PSAError:
|
||||
return {"items": [], "total": 0, "page": page, "page_size": page_size}
|
||||
|
||||
parsed_board_ids: list[int] = []
|
||||
if board_ids:
|
||||
try:
|
||||
parsed_board_ids = [int(bid.strip()) for bid in board_ids.split(",") if bid.strip()]
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=400, detail="board_ids must be comma-separated integers")
|
||||
|
||||
try:
|
||||
provider = await get_provider_for_account(current_user.account_id, db)
|
||||
result = await provider.search_tickets(
|
||||
query,
|
||||
board_id=board_id,
|
||||
status_id=status_id,
|
||||
status_name=status_name,
|
||||
include_closed=include_closed,
|
||||
member_identifier=member_identifier,
|
||||
unassigned=unassigned,
|
||||
board_ids=parsed_board_ids,
|
||||
company_id=company_id,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
)
|
||||
items = [
|
||||
PSATicketSearchResult(
|
||||
id=t.id,
|
||||
summary=t.summary,
|
||||
company_name=t.company_name,
|
||||
company_id=t.company_id,
|
||||
board_name=t.board_name,
|
||||
board_id=t.board_id,
|
||||
status_name=t.status_name,
|
||||
status_id=t.status_id,
|
||||
priority_name=t.priority_name,
|
||||
priority_id=t.priority_id,
|
||||
closed=t.closed,
|
||||
)
|
||||
for t in tickets
|
||||
for t in result.items
|
||||
]
|
||||
return {"items": items, "total": result.total, "page": result.page, "page_size": result.page_size}
|
||||
except PSAError as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/tickets", response_model=PSATicketCreatedSchema, status_code=201)
|
||||
async def create_ticket(
|
||||
data: TicketCreatePayloadSchema,
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
):
|
||||
"""Create a new PSA ticket."""
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(status_code=400, detail="User has no account")
|
||||
from app.services.psa.exceptions import PSAError
|
||||
from app.services.psa.types import TicketCreatePayload
|
||||
try:
|
||||
return await ticket_svc.create_ticket(
|
||||
current_user.account_id,
|
||||
TicketCreatePayload(**data.model_dump()),
|
||||
db,
|
||||
)
|
||||
except PSAError as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/tickets/ai-parse", response_model=AiParseResponseSchema)
|
||||
async def ai_parse_ticket(
|
||||
data: AiParseRequestSchema,
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
):
|
||||
"""Parse natural language into a ticket pre-fill payload using Claude."""
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(status_code=400, detail="User has no account")
|
||||
|
||||
from app.services.psa.registry import get_provider_for_account
|
||||
from app.services.psa.exceptions import PSAError
|
||||
import anthropic
|
||||
import json
|
||||
|
||||
# Fetch boards + members for context (both cached)
|
||||
boards = []
|
||||
members = []
|
||||
try:
|
||||
provider = await get_provider_for_account(current_user.account_id, db)
|
||||
boards = await provider.list_boards()
|
||||
members = await provider.list_members()
|
||||
except PSAError:
|
||||
pass
|
||||
|
||||
boards_list = [{"id": b.id, "name": b.name} for b in boards]
|
||||
members_list = [{"id": m.id, "name": m.name, "identifier": m.identifier} for m in members]
|
||||
|
||||
system_prompt = """You are a ticket triage assistant for an MSP help desk.
|
||||
Extract structured ticket information from the engineer's natural language description.
|
||||
Return ONLY valid JSON matching this exact schema — no other text:
|
||||
{
|
||||
"summary": "short one-line ticket title or null",
|
||||
"board_id": "integer matching one of the provided boards or null",
|
||||
"priority_name": "one of: Critical, High, Medium, Low, or null",
|
||||
"description": "expanded description or null",
|
||||
"assignee_identifier": "member identifier string from the provided members list or null",
|
||||
"warnings": ["list of strings explaining what could not be resolved"]
|
||||
}"""
|
||||
|
||||
user_msg = f"""Available boards: {json.dumps(boards_list)}
|
||||
Available members: {json.dumps(members_list[:50])}
|
||||
|
||||
Engineer's description: {data.prompt}"""
|
||||
|
||||
missing_fields: list[str] = []
|
||||
warnings: list[str] = []
|
||||
response_data = AiParseResponseSchema()
|
||||
|
||||
try:
|
||||
client = anthropic.AsyncAnthropic(
|
||||
api_key=settings.ANTHROPIC_API_KEY,
|
||||
max_retries=1,
|
||||
)
|
||||
msg = await client.messages.create(
|
||||
model=settings.get_model_for_action("default"),
|
||||
max_tokens=512,
|
||||
system=system_prompt,
|
||||
messages=[{"role": "user", "content": user_msg}],
|
||||
)
|
||||
raw = msg.content[0].text.strip()
|
||||
# Strip markdown fences if present
|
||||
if raw.startswith("```"):
|
||||
import re
|
||||
raw = re.sub(r'^```(?:json)?\s*', '', raw)
|
||||
raw = re.sub(r'\s*```$', '', raw.strip())
|
||||
parsed = json.loads(raw)
|
||||
|
||||
response_data.summary = parsed.get("summary")
|
||||
response_data.description = parsed.get("description")
|
||||
warnings = parsed.get("warnings", [])
|
||||
|
||||
# Resolve board_id
|
||||
if parsed.get("board_id"):
|
||||
board_match = next((b for b in boards if b.id == int(parsed["board_id"])), None)
|
||||
if board_match:
|
||||
response_data.board_id = board_match.id
|
||||
else:
|
||||
missing_fields.append("board_id")
|
||||
warnings.append(f"Board ID {parsed['board_id']} not found")
|
||||
else:
|
||||
missing_fields.append("board_id")
|
||||
|
||||
# Resolve assignee
|
||||
if parsed.get("assignee_identifier"):
|
||||
member = next((m for m in members if m.identifier == parsed["assignee_identifier"]), None)
|
||||
if member:
|
||||
response_data.assigned_member_id = int(member.id)
|
||||
else:
|
||||
warnings.append(f"Member '{parsed['assignee_identifier']}' not found")
|
||||
|
||||
# Priority/status/company always need manual selection
|
||||
missing_fields.extend(["status_id", "priority_id", "company_id"])
|
||||
|
||||
except Exception as e:
|
||||
logger.warning("AI parse failed: %s", e)
|
||||
missing_fields = ["summary", "board_id", "status_id", "priority_id", "company_id"]
|
||||
warnings = ["AI parsing failed — please fill in manually"]
|
||||
|
||||
response_data.missing_fields = missing_fields
|
||||
response_data.warnings = warnings
|
||||
return response_data
|
||||
|
||||
|
||||
@router.patch("/tickets/{ticket_id}/status", response_model=PSATicketStatusUpdateSchema)
|
||||
async def update_ticket_status_endpoint(
|
||||
ticket_id: int,
|
||||
status_id: int,
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
):
|
||||
"""Update a ticket's status."""
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(status_code=400, detail="User has no account")
|
||||
from app.services.psa.exceptions import PSAError
|
||||
try:
|
||||
return await ticket_svc.update_status(current_user.account_id, ticket_id, status_id, db)
|
||||
except PSAError as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/tickets/{ticket_id}/resources", response_model=list[PSAResourceSchema])
|
||||
async def list_ticket_resources(
|
||||
ticket_id: int,
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
):
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(status_code=400, detail="User has no account")
|
||||
from app.services.psa.exceptions import PSAError
|
||||
try:
|
||||
return await ticket_svc.list_resources(current_user.account_id, ticket_id, db)
|
||||
except PSAError as e:
|
||||
# Resources are optional display data — degrade gracefully rather than surfacing a toast
|
||||
logger.warning("list_resources(%s) failed: %s", ticket_id, e)
|
||||
return []
|
||||
|
||||
|
||||
@router.post("/tickets/{ticket_id}/resources", response_model=PSAResourceSchema, status_code=201)
|
||||
async def add_ticket_resource(
|
||||
ticket_id: int,
|
||||
member_id: int,
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
):
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(status_code=400, detail="User has no account")
|
||||
from app.services.psa.exceptions import PSAError
|
||||
try:
|
||||
return await ticket_svc.add_resource(current_user.account_id, ticket_id, member_id, db)
|
||||
except PSAError as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
|
||||
|
||||
@router.delete("/tickets/{ticket_id}/resources/{member_id}", status_code=204)
|
||||
async def remove_ticket_resource(
|
||||
ticket_id: int,
|
||||
member_id: int,
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
):
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(status_code=400, detail="User has no account")
|
||||
from app.services.psa.exceptions import PSAError
|
||||
try:
|
||||
await ticket_svc.remove_resource(current_user.account_id, ticket_id, member_id, db)
|
||||
except PSAError as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/priorities", response_model=list[PSAPrioritySchema])
|
||||
async def list_priorities(
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
):
|
||||
"""List PSA priority levels for ticket creation form."""
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(status_code=400, detail="User has no account")
|
||||
from app.services.psa.registry import get_provider_for_account
|
||||
from app.services.psa.exceptions import PSAError
|
||||
try:
|
||||
provider = await get_provider_for_account(current_user.account_id, db)
|
||||
raw = await provider.list_priorities()
|
||||
return [PSAPrioritySchema(id=p["id"], name=p["name"]) for p in raw if p.get("id")]
|
||||
except PSAError as e:
|
||||
logger.warning("list_priorities failed: %s", e)
|
||||
return []
|
||||
|
||||
|
||||
@router.get("/tickets/{ticket_id}/context")
|
||||
async def get_ticket_context(
|
||||
ticket_id: int,
|
||||
@@ -483,7 +790,30 @@ async def get_ticket_statuses(
|
||||
except PSANotFoundError:
|
||||
raise HTTPException(status_code=404, detail="Ticket not found")
|
||||
except PSAError as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
logger.warning("get_ticket_statuses(%s) failed: %s", ticket_id, e)
|
||||
return []
|
||||
|
||||
|
||||
@router.get("/boards/{board_id}/statuses", response_model=list[PSATicketStatusItem])
|
||||
async def get_board_statuses(
|
||||
board_id: int,
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
):
|
||||
"""Get available statuses for a service board directly (no ticket lookup required)."""
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(status_code=400, detail="User has no account")
|
||||
|
||||
from app.services.psa.registry import get_provider_for_account
|
||||
from app.services.psa.exceptions import PSAError
|
||||
|
||||
try:
|
||||
provider = await get_provider_for_account(current_user.account_id, db)
|
||||
statuses = await provider.get_ticket_statuses(board_id)
|
||||
return [PSATicketStatusItem(id=s.id, name=s.name, is_closed=s.is_closed) for s in statuses]
|
||||
except PSAError as e:
|
||||
logger.warning("get_board_statuses(%s) failed: %s", board_id, e)
|
||||
return []
|
||||
|
||||
|
||||
# ── member mapping endpoints ─────────────────────────────────────────
|
||||
@@ -491,7 +821,7 @@ async def get_ticket_statuses(
|
||||
|
||||
@router.get("/members", response_model=list[PsaMemberResponse])
|
||||
async def list_members(
|
||||
current_user: Annotated[User, Depends(require_account_owner)],
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
):
|
||||
"""List CW members (from CW API)."""
|
||||
@@ -509,7 +839,9 @@ async def list_members(
|
||||
for m in members
|
||||
]
|
||||
except PSAError as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
# Members are optional display data — degrade gracefully
|
||||
logger.warning("list_members failed: %s", e)
|
||||
return []
|
||||
|
||||
|
||||
@router.get("/member-mappings", response_model=list[PsaMemberMappingResponse])
|
||||
@@ -517,31 +849,37 @@ async def get_member_mappings(
|
||||
current_user: Annotated[User, Depends(require_account_owner)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
):
|
||||
"""Get all member mappings for the account."""
|
||||
"""Get all account users with their PSA member mappings (unmapped users included)."""
|
||||
conn = await _get_account_connection(current_user.account_id, db)
|
||||
if not conn:
|
||||
return []
|
||||
|
||||
result = await db.execute(
|
||||
# Fetch all active account users
|
||||
users_result = await db.execute(
|
||||
select(User).where(User.account_id == current_user.account_id, User.is_active.is_(True))
|
||||
)
|
||||
users = users_result.scalars().all()
|
||||
|
||||
# Fetch all existing mappings keyed by user_id for O(1) lookup
|
||||
mappings_result = await db.execute(
|
||||
select(PsaMemberMapping).where(PsaMemberMapping.psa_connection_id == conn.id)
|
||||
)
|
||||
mappings = result.scalars().all()
|
||||
mapping_by_user: dict[str, PsaMemberMapping] = {
|
||||
str(m.user_id): m for m in mappings_result.scalars().all()
|
||||
}
|
||||
|
||||
response = []
|
||||
for m in mappings:
|
||||
user_result = await db.execute(select(User).where(User.id == m.user_id))
|
||||
user = user_result.scalar_one_or_none()
|
||||
if user:
|
||||
response.append(PsaMemberMappingResponse(
|
||||
id=str(m.id),
|
||||
user_id=str(m.user_id),
|
||||
user_email=user.email,
|
||||
user_name=user.name,
|
||||
external_member_id=m.external_member_id,
|
||||
external_member_name=m.external_member_name,
|
||||
matched_by=m.matched_by,
|
||||
))
|
||||
return response
|
||||
return [
|
||||
PsaMemberMappingResponse(
|
||||
id=str(m.id) if (m := mapping_by_user.get(str(user.id))) else None,
|
||||
user_id=str(user.id),
|
||||
user_email=user.email,
|
||||
user_name=user.name,
|
||||
external_member_id=m.external_member_id if m else None,
|
||||
external_member_name=m.external_member_name if m else None,
|
||||
matched_by=m.matched_by if m else None,
|
||||
)
|
||||
for user in users
|
||||
]
|
||||
|
||||
|
||||
@router.post("/member-mappings", response_model=list[PsaMemberMappingResponse])
|
||||
@@ -564,6 +902,7 @@ async def save_member_mappings(
|
||||
for m in mappings:
|
||||
mapping = PsaMemberMapping(
|
||||
psa_connection_id=conn.id,
|
||||
account_id=current_user.account_id,
|
||||
user_id=UUID(m.user_id),
|
||||
external_member_id=m.external_member_id,
|
||||
external_member_name=m.external_member_name,
|
||||
@@ -624,6 +963,7 @@ async def auto_match_members(
|
||||
if not existing.scalar_one_or_none():
|
||||
mapping = PsaMemberMapping(
|
||||
psa_connection_id=conn.id,
|
||||
account_id=current_user.account_id,
|
||||
user_id=user.id,
|
||||
external_member_id=cw_member.id,
|
||||
external_member_name=cw_member.name,
|
||||
|
||||
362
backend/app/api/endpoints/network_diagrams.py
Normal file
362
backend/app/api/endpoints/network_diagrams.py
Normal file
@@ -0,0 +1,362 @@
|
||||
"""Network diagrams API endpoints."""
|
||||
import base64
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import select, or_
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.api.deps import get_current_active_user
|
||||
from app.models.user import User
|
||||
from app.models.device_type import DeviceType
|
||||
from app.models.network_diagram import NetworkDiagram
|
||||
from app.core.service_account import PLATFORM_ACCOUNT_ID
|
||||
from app.schemas.network_diagram import (
|
||||
NetworkDiagramCreate,
|
||||
NetworkDiagramUpdate,
|
||||
NetworkDiagramResponse,
|
||||
NetworkDiagramListItem,
|
||||
AIGenerateRequest,
|
||||
AIGenerateResponse,
|
||||
DiagramImportRequest,
|
||||
DiagramImportResponse,
|
||||
DiagramExportResponse,
|
||||
DiagramNode,
|
||||
DiagramEdge,
|
||||
)
|
||||
from app.services import network_diagram_ai_service, storage_service
|
||||
|
||||
# Maps system device-type slugs to their category — mirrors frontend deviceRegistry.ts
|
||||
_SLUG_CATEGORY: dict[str, str] = {
|
||||
"router": "network", "switch": "network", "access-point": "network", "load-balancer": "network",
|
||||
"firewall": "security", "badge-reader": "security",
|
||||
"server": "compute", "vm": "compute", "container": "compute",
|
||||
"nas": "storage", "san": "storage", "cloud-storage": "storage",
|
||||
"cloud": "cloud", "aws": "cloud", "azure": "cloud", "gcp": "cloud", "isp": "cloud",
|
||||
"workstation": "endpoint", "laptop": "endpoint", "tablet": "endpoint",
|
||||
"phone": "endpoint", "printer": "endpoint",
|
||||
"ups": "infrastructure", "pdu": "infrastructure", "rack": "infrastructure",
|
||||
"patch-panel": "infrastructure", "camera": "infrastructure",
|
||||
"nvr": "infrastructure", "iot": "infrastructure",
|
||||
}
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/network-diagrams", tags=["network-diagrams"])
|
||||
|
||||
|
||||
async def _get_diagram_or_404(
|
||||
diagram_id: UUID,
|
||||
account_id: UUID,
|
||||
db: AsyncSession,
|
||||
) -> NetworkDiagram:
|
||||
diagram = await db.get(NetworkDiagram, diagram_id)
|
||||
if not diagram or diagram.account_id != account_id or diagram.is_archived:
|
||||
raise HTTPException(status_code=404, detail="Diagram not found")
|
||||
return diagram
|
||||
|
||||
|
||||
def _diagram_to_response(diagram: NetworkDiagram) -> NetworkDiagramResponse:
|
||||
return NetworkDiagramResponse.model_validate(diagram)
|
||||
|
||||
|
||||
def _diagram_to_list_item(
|
||||
diagram: NetworkDiagram,
|
||||
custom_slug_category: dict[str, str] | None = None,
|
||||
) -> NetworkDiagramListItem:
|
||||
nodes = diagram.nodes if isinstance(diagram.nodes, list) else []
|
||||
slug_to_cat = {**_SLUG_CATEGORY, **(custom_slug_category or {})}
|
||||
|
||||
category_counts: dict[str, int] = {}
|
||||
for node in nodes:
|
||||
slug = node.get("type", "") if isinstance(node, dict) else ""
|
||||
cat = slug_to_cat.get(slug, "other")
|
||||
category_counts[cat] = category_counts.get(cat, 0) + 1
|
||||
|
||||
return NetworkDiagramListItem(
|
||||
id=diagram.id,
|
||||
name=diagram.name,
|
||||
client_name=diagram.client_name,
|
||||
description=diagram.description,
|
||||
node_count=len(nodes),
|
||||
category_counts=category_counts,
|
||||
thumbnail_url=diagram.thumbnail_url,
|
||||
created_by=diagram.created_by,
|
||||
created_at=diagram.created_at,
|
||||
updated_at=diagram.updated_at,
|
||||
)
|
||||
|
||||
|
||||
async def _get_available_slugs(account_id: UUID, db: AsyncSession) -> set[str]:
|
||||
stmt = select(DeviceType.slug).where(
|
||||
or_(
|
||||
DeviceType.account_id == PLATFORM_ACCOUNT_ID,
|
||||
DeviceType.account_id == account_id,
|
||||
)
|
||||
)
|
||||
result = await db.execute(stmt)
|
||||
return {row[0] for row in result.all()}
|
||||
|
||||
|
||||
@router.get("/clients", response_model=list[str])
|
||||
async def list_client_names(
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> list[str]:
|
||||
stmt = (
|
||||
select(NetworkDiagram.client_name)
|
||||
.where(
|
||||
NetworkDiagram.account_id == current_user.account_id,
|
||||
NetworkDiagram.is_archived.is_(False),
|
||||
NetworkDiagram.client_name.isnot(None),
|
||||
NetworkDiagram.client_name != "",
|
||||
)
|
||||
.distinct()
|
||||
.order_by(NetworkDiagram.client_name)
|
||||
)
|
||||
result = await db.execute(stmt)
|
||||
return [row[0] for row in result.all()]
|
||||
|
||||
|
||||
@router.get("/", response_model=list[NetworkDiagramListItem])
|
||||
async def list_diagrams(
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
client_name: str | None = Query(default=None),
|
||||
search: str | None = Query(default=None),
|
||||
) -> list[NetworkDiagramListItem]:
|
||||
stmt = (
|
||||
select(NetworkDiagram)
|
||||
.where(
|
||||
NetworkDiagram.account_id == current_user.account_id,
|
||||
NetworkDiagram.is_archived.is_(False),
|
||||
)
|
||||
.order_by(NetworkDiagram.updated_at.desc())
|
||||
)
|
||||
|
||||
if client_name:
|
||||
stmt = stmt.where(NetworkDiagram.client_name == client_name)
|
||||
|
||||
if search:
|
||||
escaped = search.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
||||
search_filter = f"%{escaped}%"
|
||||
stmt = stmt.where(
|
||||
or_(
|
||||
NetworkDiagram.name.ilike(search_filter),
|
||||
NetworkDiagram.client_name.ilike(search_filter),
|
||||
)
|
||||
)
|
||||
|
||||
# Single query for custom device types so category_counts is accurate
|
||||
dt_stmt = select(DeviceType.slug, DeviceType.category).where(
|
||||
DeviceType.is_system.is_(False),
|
||||
DeviceType.account_id == current_user.account_id,
|
||||
)
|
||||
dt_result = await db.execute(dt_stmt)
|
||||
custom_slug_category = {row[0]: row[1] for row in dt_result.all()}
|
||||
|
||||
result = await db.execute(stmt)
|
||||
rows = result.scalars().all()
|
||||
return [_diagram_to_list_item(r, custom_slug_category) for r in rows]
|
||||
|
||||
|
||||
@router.post("/", response_model=NetworkDiagramResponse, status_code=201)
|
||||
async def create_diagram(
|
||||
data: NetworkDiagramCreate,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> NetworkDiagramResponse:
|
||||
diagram = NetworkDiagram(
|
||||
account_id=current_user.account_id,
|
||||
name=data.name,
|
||||
client_name=data.client_name,
|
||||
asset_name=data.asset_name,
|
||||
description=data.description,
|
||||
nodes=[n.model_dump() for n in data.nodes],
|
||||
edges=[e.model_dump() for e in data.edges],
|
||||
created_by=current_user.id,
|
||||
)
|
||||
db.add(diagram)
|
||||
await db.commit()
|
||||
await db.refresh(diagram)
|
||||
return _diagram_to_response(diagram)
|
||||
|
||||
|
||||
@router.get("/{diagram_id}", response_model=NetworkDiagramResponse)
|
||||
async def get_diagram(
|
||||
diagram_id: UUID,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> NetworkDiagramResponse:
|
||||
diagram = await _get_diagram_or_404(diagram_id, current_user.account_id, db)
|
||||
return _diagram_to_response(diagram)
|
||||
|
||||
|
||||
@router.put("/{diagram_id}", response_model=NetworkDiagramResponse)
|
||||
async def update_diagram(
|
||||
diagram_id: UUID,
|
||||
data: NetworkDiagramUpdate,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> NetworkDiagramResponse:
|
||||
diagram = await _get_diagram_or_404(diagram_id, current_user.account_id, db)
|
||||
|
||||
update_data = data.model_dump(exclude_unset=True)
|
||||
if "nodes" in update_data and update_data["nodes"] is not None:
|
||||
update_data["nodes"] = [n.model_dump() if hasattr(n, "model_dump") else n for n in update_data["nodes"]]
|
||||
if "edges" in update_data and update_data["edges"] is not None:
|
||||
update_data["edges"] = [e.model_dump() if hasattr(e, "model_dump") else e for e in update_data["edges"]]
|
||||
|
||||
for field, value in update_data.items():
|
||||
setattr(diagram, field, value)
|
||||
|
||||
diagram.updated_at = datetime.now(timezone.utc)
|
||||
await db.commit()
|
||||
await db.refresh(diagram)
|
||||
return _diagram_to_response(diagram)
|
||||
|
||||
|
||||
@router.delete("/{diagram_id}", status_code=204)
|
||||
async def archive_diagram(
|
||||
diagram_id: UUID,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> None:
|
||||
diagram = await _get_diagram_or_404(diagram_id, current_user.account_id, db)
|
||||
diagram.is_archived = True
|
||||
diagram.updated_at = datetime.now(timezone.utc)
|
||||
await db.commit()
|
||||
|
||||
|
||||
@router.post("/{diagram_id}/duplicate", response_model=NetworkDiagramResponse, status_code=201)
|
||||
async def duplicate_diagram(
|
||||
diagram_id: UUID,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> NetworkDiagramResponse:
|
||||
source = await _get_diagram_or_404(diagram_id, current_user.account_id, db)
|
||||
copy = NetworkDiagram(
|
||||
account_id=current_user.account_id,
|
||||
name=f"Copy of {source.name}",
|
||||
client_name=source.client_name,
|
||||
asset_name=source.asset_name,
|
||||
description=source.description,
|
||||
nodes=source.nodes,
|
||||
edges=source.edges,
|
||||
created_by=current_user.id,
|
||||
)
|
||||
db.add(copy)
|
||||
await db.commit()
|
||||
await db.refresh(copy)
|
||||
return _diagram_to_response(copy)
|
||||
|
||||
|
||||
@router.get("/{diagram_id}/export", response_model=DiagramExportResponse)
|
||||
async def export_diagram(
|
||||
diagram_id: UUID,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> DiagramExportResponse:
|
||||
diagram = await _get_diagram_or_404(diagram_id, current_user.account_id, db)
|
||||
nodes = [DiagramNode(**n) for n in (diagram.nodes or [])]
|
||||
edges = [DiagramEdge(**e) for e in (diagram.edges or [])]
|
||||
return DiagramExportResponse(
|
||||
schemaVersion=1,
|
||||
name=diagram.name,
|
||||
client_name=diagram.client_name,
|
||||
description=diagram.description,
|
||||
nodes=nodes,
|
||||
edges=edges,
|
||||
exportedAt=datetime.now(timezone.utc).isoformat(),
|
||||
)
|
||||
|
||||
|
||||
@router.post("/import", response_model=DiagramImportResponse, status_code=201)
|
||||
async def import_diagram(
|
||||
data: DiagramImportRequest,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> DiagramImportResponse:
|
||||
available_slugs = await _get_available_slugs(current_user.account_id, db)
|
||||
|
||||
warnings: list[str] = []
|
||||
for node in data.nodes:
|
||||
if node.type not in available_slugs:
|
||||
warnings.append(f"Unknown device type '{node.type}' — will render with default icon")
|
||||
|
||||
diagram = NetworkDiagram(
|
||||
account_id=current_user.account_id,
|
||||
name=data.name,
|
||||
client_name=data.client_name,
|
||||
description=data.description,
|
||||
nodes=[n.model_dump() for n in data.nodes],
|
||||
edges=[e.model_dump() for e in data.edges],
|
||||
created_by=current_user.id,
|
||||
)
|
||||
db.add(diagram)
|
||||
await db.commit()
|
||||
await db.refresh(diagram)
|
||||
|
||||
return DiagramImportResponse(
|
||||
diagram=_diagram_to_response(diagram),
|
||||
warnings=warnings,
|
||||
)
|
||||
|
||||
|
||||
class ThumbnailUploadRequest(BaseModel):
|
||||
data_url: str # base64 PNG data URL: "data:image/png;base64,..."
|
||||
|
||||
|
||||
@router.post("/{diagram_id}/thumbnail", status_code=204)
|
||||
async def upload_thumbnail(
|
||||
diagram_id: UUID,
|
||||
body: ThumbnailUploadRequest,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> None:
|
||||
diagram = await _get_diagram_or_404(diagram_id, current_user.account_id, db)
|
||||
try:
|
||||
header, encoded = body.data_url.split(",", 1)
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=422, detail="Invalid data URL format")
|
||||
image_bytes = base64.b64decode(encoded)
|
||||
storage_key = await storage_service.upload_file(
|
||||
file_data=image_bytes,
|
||||
filename=f"thumbnail-{diagram_id}.png",
|
||||
content_type="image/png",
|
||||
account_id=str(current_user.account_id),
|
||||
)
|
||||
presigned_url = storage_service.get_presigned_url(storage_key)
|
||||
diagram.thumbnail_url = presigned_url
|
||||
await db.commit()
|
||||
|
||||
|
||||
@router.post("/ai-generate", response_model=AIGenerateResponse)
|
||||
async def ai_generate_diagram(
|
||||
data: AIGenerateRequest,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> AIGenerateResponse:
|
||||
available_slugs_set = await _get_available_slugs(current_user.account_id, db)
|
||||
available_slugs = list(available_slugs_set)
|
||||
|
||||
existing_node_ids: list[str] | None = None
|
||||
if data.mode == "merge" and data.existingBounds:
|
||||
existing_node_ids = []
|
||||
|
||||
try:
|
||||
return await network_diagram_ai_service.generate_diagram(
|
||||
request=data,
|
||||
available_slugs=available_slugs,
|
||||
existing_node_ids=existing_node_ids,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
except Exception:
|
||||
logger.exception("AI diagram generation failed")
|
||||
raise HTTPException(status_code=500, detail="Diagram generation failed")
|
||||
@@ -3,12 +3,14 @@ from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy import select, text
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.rate_limit import limiter
|
||||
from app.api.deps import get_current_active_user
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.user import User
|
||||
from app.models.script_builder_session import ScriptBuilderSession
|
||||
from app.schemas.script_builder import (
|
||||
@@ -67,15 +69,85 @@ async def create_session(
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
) -> ScriptBuilderSessionDetail:
|
||||
"""Start a new Script Builder session."""
|
||||
"""Start a new Script Builder session.
|
||||
|
||||
When origin='pilot_inline', behaves as get-or-create: the same row is
|
||||
returned on repeated calls with the same (user, ai_session_id) pair.
|
||||
Inline sessions are excluded from the session cap and the list endpoint.
|
||||
"""
|
||||
# Phase 9: inline origin validation + authorization
|
||||
if data.origin == "pilot_inline":
|
||||
if data.ai_session_id is None:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="ai_session_id is required when origin='pilot_inline'",
|
||||
)
|
||||
# Ownership check: the pilot session must belong to the current user.
|
||||
ai_session = await db.scalar(
|
||||
select(AISession).where(
|
||||
AISession.id == data.ai_session_id,
|
||||
AISession.user_id == current_user.id,
|
||||
)
|
||||
)
|
||||
if ai_session is None:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Session not found",
|
||||
)
|
||||
|
||||
# Idempotent get-or-create: if a pilot_inline row already exists for
|
||||
# this (user, ai_session_id) pair, return it without creating a duplicate.
|
||||
existing = await db.scalar(
|
||||
select(ScriptBuilderSession).where(
|
||||
ScriptBuilderSession.user_id == current_user.id,
|
||||
ScriptBuilderSession.ai_session_id == data.ai_session_id,
|
||||
ScriptBuilderSession.origin == "pilot_inline",
|
||||
)
|
||||
)
|
||||
if existing is not None:
|
||||
# Re-fetch with message_records loaded
|
||||
session = await script_builder_service.get_session(db, existing.id, current_user.id)
|
||||
return _session_to_detail(session)
|
||||
|
||||
# Create the inline session — wrap in IntegrityError catch for races.
|
||||
try:
|
||||
session = await script_builder_service.create_session(
|
||||
db=db,
|
||||
user_id=current_user.id,
|
||||
account_id=current_user.account_id,
|
||||
team_id=current_user.team_id,
|
||||
language=data.language,
|
||||
origin=data.origin,
|
||||
ai_session_id=data.ai_session_id,
|
||||
)
|
||||
await db.commit()
|
||||
except IntegrityError:
|
||||
await db.rollback()
|
||||
# Race: another request won the unique index — re-read the winner row.
|
||||
existing = await db.scalar(
|
||||
select(ScriptBuilderSession).where(
|
||||
ScriptBuilderSession.user_id == current_user.id,
|
||||
ScriptBuilderSession.ai_session_id == data.ai_session_id,
|
||||
ScriptBuilderSession.origin == "pilot_inline",
|
||||
)
|
||||
)
|
||||
if existing is None:
|
||||
raise
|
||||
session = existing
|
||||
|
||||
# Re-fetch with message_records loaded
|
||||
session = await script_builder_service.get_session(db, session.id, current_user.id)
|
||||
return _session_to_detail(session)
|
||||
|
||||
# ── Standalone session ──────────────────────────────────────────────────
|
||||
# Acquire per-user advisory lock so concurrent create requests are serialized.
|
||||
# Without this, two simultaneous requests both read count < limit and both
|
||||
# insert, exceeding MAX_SESSIONS_PER_USER.
|
||||
user_lock_key = hash(str(current_user.id)) % (2**62)
|
||||
await db.execute(text("SELECT pg_advisory_xact_lock(:key)"), {"key": user_lock_key})
|
||||
|
||||
# Enforce max concurrent sessions
|
||||
count = await script_builder_service.count_user_sessions(db, current_user.id)
|
||||
# Enforce max concurrent sessions (inline sessions excluded from cap)
|
||||
count = await script_builder_service.count_user_sessions(db, current_user.id, include_inline=False)
|
||||
if count >= MAX_SESSIONS_PER_USER:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
@@ -88,6 +160,8 @@ async def create_session(
|
||||
account_id=current_user.account_id,
|
||||
team_id=current_user.team_id,
|
||||
language=data.language,
|
||||
origin=data.origin,
|
||||
ai_session_id=data.ai_session_id,
|
||||
)
|
||||
await db.commit()
|
||||
# Re-fetch with message_records loaded
|
||||
@@ -186,6 +260,7 @@ async def save_to_library(
|
||||
category_id=data.category_id,
|
||||
share_with_team=data.share_with_team,
|
||||
user_id=current_user.id,
|
||||
account_id=current_user.account_id,
|
||||
team_id=current_user.team_id,
|
||||
script_body=data.script_body,
|
||||
parameters_schema=data.parameters_schema,
|
||||
|
||||
@@ -5,7 +5,7 @@ import re
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, func, or_, literal
|
||||
from sqlalchemy import select, func, or_, literal, update as sa_update
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.api.deps import get_current_active_user
|
||||
@@ -374,6 +374,20 @@ async def generate_script(
|
||||
)
|
||||
db.add(generation)
|
||||
template.usage_count += 1
|
||||
|
||||
# FlowPilot Phase 3: bump the linked AI session's state_version so the
|
||||
# resolution-note preview cache invalidates. One-off scripts run outside
|
||||
# any FlowPilot session — in that case the UPDATE matches zero rows.
|
||||
if data.ai_session_id is not None:
|
||||
# Local import: scripts endpoint stays independent of AI-session
|
||||
# imports for non-AI generation paths.
|
||||
from app.models.ai_session import AISession
|
||||
await db.execute(
|
||||
sa_update(AISession)
|
||||
.where(AISession.id == data.ai_session_id)
|
||||
.values(state_version=AISession.state_version + 1)
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(generation)
|
||||
|
||||
|
||||
315
backend/app/api/endpoints/session_facts.py
Normal file
315
backend/app/api/endpoints/session_facts.py
Normal file
@@ -0,0 +1,315 @@
|
||||
"""Session fact endpoints — the "What we know" CRUD surface for a FlowPilot session.
|
||||
|
||||
All routes are sub-resources of `/ai-sessions/{session_id}`. Tenant isolation is
|
||||
enforced by RLS on `session_facts.account_id`; a user from another account
|
||||
literally cannot see or write facts for this session.
|
||||
|
||||
Editability rule (per FLOWPILOT-MIGRATION.md Section 7.3):
|
||||
- `user_note` and `ai_synthesis` facts are editable at the card level.
|
||||
- `question` and `diagnostic_check` facts are read-only at the card level —
|
||||
edit the source question/check instead. PATCH returns 403 for those.
|
||||
|
||||
Fact promotion writes always bump `ai_sessions.state_version` so the
|
||||
resolution-note preview cache invalidates (Section 5.5).
|
||||
"""
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.deps import get_current_active_user, get_db, require_engineer_or_admin
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.session_fact import SessionFact
|
||||
from app.models.user import User
|
||||
from app.schemas.session_fact import (
|
||||
SessionFactCreateRequest,
|
||||
SessionFactListResponse,
|
||||
SessionFactPromoteRequest,
|
||||
SessionFactResponse,
|
||||
SessionFactUpdateRequest,
|
||||
)
|
||||
from app.services.fact_synthesis_service import (
|
||||
FactSynthesisService,
|
||||
list_facts_for_session,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/ai-sessions/{session_id}", tags=["session-facts"])
|
||||
|
||||
# Source types whose facts can be edited at the card level (Section 7.3).
|
||||
_EDITABLE_SOURCE_TYPES = frozenset({"user_note", "ai_synthesis"})
|
||||
|
||||
|
||||
def _to_response(fact: SessionFact) -> SessionFactResponse:
|
||||
"""Wrap an ORM SessionFact in the response model with the editable flag."""
|
||||
return SessionFactResponse(
|
||||
id=fact.id,
|
||||
session_id=fact.session_id,
|
||||
text=fact.text,
|
||||
source_type=fact.source_type, # type: ignore[arg-type]
|
||||
source_ref=fact.source_ref,
|
||||
source_summary=fact.source_summary,
|
||||
created_by=fact.created_by,
|
||||
created_at=fact.created_at,
|
||||
updated_at=fact.updated_at,
|
||||
editable=fact.source_type in _EDITABLE_SOURCE_TYPES,
|
||||
)
|
||||
|
||||
|
||||
async def _load_session_or_404(db: AsyncSession, session_id: UUID) -> AISession:
|
||||
"""Load the session via RLS-scoped SELECT. Returns 404 if missing/cross-tenant.
|
||||
|
||||
Tenant isolation: RLS on `ai_sessions` filters by current account, so a
|
||||
cross-tenant access returns no rows and we 404 (rather than 403, which
|
||||
would leak the row's existence).
|
||||
"""
|
||||
result = await db.execute(select(AISession).where(AISession.id == session_id))
|
||||
session = result.scalar_one_or_none()
|
||||
if session is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Session not found")
|
||||
return session
|
||||
|
||||
|
||||
async def _load_fact_or_404(
|
||||
db: AsyncSession, session_id: UUID, fact_id: UUID
|
||||
) -> SessionFact:
|
||||
"""Load a non-deleted fact for the session. 404 if missing or already deleted."""
|
||||
result = await db.execute(
|
||||
select(SessionFact).where(
|
||||
SessionFact.id == fact_id,
|
||||
SessionFact.session_id == session_id,
|
||||
SessionFact.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
fact = result.scalar_one_or_none()
|
||||
if fact is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Fact not found")
|
||||
return fact
|
||||
|
||||
|
||||
# ── List ──
|
||||
|
||||
@router.get("/facts", response_model=SessionFactListResponse)
|
||||
async def list_facts(
|
||||
session_id: UUID,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> SessionFactListResponse:
|
||||
"""List facts for a session, oldest first."""
|
||||
await _load_session_or_404(db, session_id)
|
||||
facts = await list_facts_for_session(db, session_id)
|
||||
return SessionFactListResponse(facts=[_to_response(f) for f in facts])
|
||||
|
||||
|
||||
# ── Create (manual user note) ──
|
||||
|
||||
@router.post("/facts", response_model=SessionFactResponse, status_code=201)
|
||||
async def create_fact(
|
||||
session_id: UUID,
|
||||
body: SessionFactCreateRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> SessionFactResponse:
|
||||
"""Create a manual fact (the "+ Add a note" UI affordance).
|
||||
|
||||
Always recorded as `source_type=user_note`. Source-typed creation goes
|
||||
through `/facts/promote` so the originating item ID is captured.
|
||||
"""
|
||||
session = await _load_session_or_404(db, session_id)
|
||||
service = FactSynthesisService(db)
|
||||
try:
|
||||
fact = await service.create_fact(
|
||||
session_id=session.id,
|
||||
account_id=session.account_id,
|
||||
user_id=current_user.id,
|
||||
source_type="user_note",
|
||||
text=body.text,
|
||||
summary=body.summary,
|
||||
source_ref=None,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
await db.commit()
|
||||
await db.refresh(fact)
|
||||
return _to_response(fact)
|
||||
|
||||
|
||||
# ── Update ──
|
||||
|
||||
@router.patch("/facts/{fact_id}", response_model=SessionFactResponse)
|
||||
async def update_fact(
|
||||
session_id: UUID,
|
||||
fact_id: UUID,
|
||||
body: SessionFactUpdateRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> SessionFactResponse:
|
||||
"""Edit fact text or summary.
|
||||
|
||||
Returns 403 for `question` and `diagnostic_check`-sourced facts: the
|
||||
source item is the canonical input, so editing the fact card would
|
||||
desync the two. Engineers edit the source instead.
|
||||
"""
|
||||
fact = await _load_fact_or_404(db, session_id, fact_id)
|
||||
if fact.source_type not in _EDITABLE_SOURCE_TYPES:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail=(
|
||||
f"Facts sourced from {fact.source_type!r} are read-only at the "
|
||||
"card level. Edit the originating question or diagnostic check instead."
|
||||
),
|
||||
)
|
||||
|
||||
if body.text is None and body.summary is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="At least one of `text` or `summary` must be provided",
|
||||
)
|
||||
|
||||
service = FactSynthesisService(db)
|
||||
try:
|
||||
fact = await service.update_fact(fact, text=body.text, summary=body.summary)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
await db.commit()
|
||||
await db.refresh(fact)
|
||||
return _to_response(fact)
|
||||
|
||||
|
||||
# ── Soft delete ──
|
||||
|
||||
@router.delete("/facts/{fact_id}", status_code=204)
|
||||
async def delete_fact(
|
||||
session_id: UUID,
|
||||
fact_id: UUID,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> None:
|
||||
"""Soft-delete a fact. All source types are deletable.
|
||||
|
||||
Soft delete (rather than hard) preserves provenance for audit and lets
|
||||
accidental deletes be recovered if needed. The `editable` flag does NOT
|
||||
control deletion — even read-only facts can be removed when the
|
||||
underlying question/check turned out to be wrong.
|
||||
"""
|
||||
fact = await _load_fact_or_404(db, session_id, fact_id)
|
||||
service = FactSynthesisService(db)
|
||||
await service.soft_delete_fact(fact)
|
||||
await db.commit()
|
||||
|
||||
|
||||
# ── Promote (AI marker + engineer-driven) ──
|
||||
|
||||
@router.post("/facts/promote", response_model=SessionFactResponse, status_code=201)
|
||||
async def promote_fact(
|
||||
session_id: UUID,
|
||||
body: SessionFactPromoteRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> SessionFactResponse:
|
||||
"""Convert a question answer / check result into a fact.
|
||||
|
||||
Two modes:
|
||||
|
||||
- `proposed_text` provided → persisted as-is.
|
||||
- `raw_input` provided → server drafts text/summary via FactSynthesisService.
|
||||
|
||||
Exactly one of the two must be set. The engineer-facing UI typically uses
|
||||
`proposed_text` after letting the engineer review/edit a draft.
|
||||
"""
|
||||
if (body.proposed_text is None) == (body.raw_input is None):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Exactly one of `proposed_text` or `raw_input` must be provided",
|
||||
)
|
||||
if body.source_type == "ai_synthesis" and body.source_ref is not None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="`source_ref` must be null for source_type=ai_synthesis",
|
||||
)
|
||||
|
||||
session = await _load_session_or_404(db, session_id)
|
||||
service = FactSynthesisService(db)
|
||||
|
||||
text = body.proposed_text
|
||||
summary = body.proposed_summary
|
||||
if text is None:
|
||||
# Synthesize via LLM. Caller must hint which task-lane item the input
|
||||
# came from so we can shape the prompt appropriately.
|
||||
raw = body.raw_input or ""
|
||||
if body.source_type == "question":
|
||||
draft = await service.synthesize_from_question(
|
||||
question_text=_lookup_task_lane_text(session, body.source_ref, "questions"),
|
||||
raw_answer=raw,
|
||||
)
|
||||
elif body.source_type == "diagnostic_check":
|
||||
draft = await service.synthesize_from_check(
|
||||
check_label=_lookup_task_lane_text(session, body.source_ref, "actions"),
|
||||
check_output=raw,
|
||||
)
|
||||
else:
|
||||
# ai_synthesis with raw_input: the raw input IS the synthesis.
|
||||
# Re-run through the question synthesizer with an empty question
|
||||
# so the conservative prompt still applies.
|
||||
draft = await service.synthesize_from_question(
|
||||
question_text="(none — synthesizing from engineer summary)",
|
||||
raw_answer=raw,
|
||||
)
|
||||
text = draft["text"]
|
||||
summary = summary or draft["summary"]
|
||||
if not text:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail=(
|
||||
"Synthesizer found no substantive fact in the input. "
|
||||
"Edit the input or supply `proposed_text` directly."
|
||||
),
|
||||
)
|
||||
|
||||
try:
|
||||
fact = await service.create_fact(
|
||||
session_id=session.id,
|
||||
account_id=session.account_id,
|
||||
user_id=current_user.id,
|
||||
source_type=body.source_type,
|
||||
text=text,
|
||||
summary=summary,
|
||||
source_ref=body.source_ref,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(fact)
|
||||
return _to_response(fact)
|
||||
|
||||
|
||||
def _lookup_task_lane_text(
|
||||
session: AISession, source_ref: UUID | None, list_key: str
|
||||
) -> str:
|
||||
"""Find the originating question text / action label from pending_task_lane.
|
||||
|
||||
Falls back to a generic placeholder if the source item is no longer in
|
||||
the lane (e.g., the AI dropped it from a later turn). The synthesizer is
|
||||
forgiving — an empty/generic question still produces a useful fact when
|
||||
the engineer's answer is substantive on its own.
|
||||
"""
|
||||
if source_ref is None:
|
||||
return ""
|
||||
lane = session.pending_task_lane or {}
|
||||
items = lane.get(list_key) or []
|
||||
sref = str(source_ref)
|
||||
for item in items:
|
||||
if isinstance(item, dict) and str(item.get("id")) == sref:
|
||||
return str(item.get("text") or item.get("label") or "")
|
||||
return ""
|
||||
@@ -1,19 +1,24 @@
|
||||
"""Handoff endpoints — unified park/escalate.
|
||||
|
||||
POST /ai-sessions/{id}/handoff — Create handoff
|
||||
POST /ai-sessions/{id}/handoff — Create handoff
|
||||
GET /ai-sessions/{id}/handoffs — Handoff history
|
||||
POST /ai-sessions/{id}/handoffs/{hid}/claim — Claim session
|
||||
GET /ai-sessions/queue — Team queue
|
||||
GET /ai-sessions/queue — Team queue
|
||||
GET /ai-sessions/escalations/stream — SSE: live escalation arrivals
|
||||
"""
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from typing import Annotated
|
||||
from typing import Annotated, AsyncGenerator
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Request, status
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.deps import get_current_active_user, get_db
|
||||
from app.api.deps import get_current_active_user, get_db, require_engineer_or_admin
|
||||
from app.core.escalation_bus import bus as escalation_bus
|
||||
from app.models.user import User
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.session_handoff import SessionHandoff
|
||||
@@ -36,6 +41,7 @@ router = APIRouter(prefix="/ai-sessions/{session_id}", tags=["session-handoffs"]
|
||||
async def create_handoff(
|
||||
session_id: UUID,
|
||||
body: HandoffCreateRequest,
|
||||
background_tasks: BackgroundTasks,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
) -> HandoffResponse:
|
||||
@@ -58,11 +64,32 @@ async def create_handoff(
|
||||
engineer_notes=body.engineer_notes,
|
||||
user_id=current_user.id,
|
||||
priority=body.priority,
|
||||
target_user_id=body.target_user_id,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
# For escalate: generate documentation + push to PSA before commit so
|
||||
# the handoff and the PSA-state changes land atomically.
|
||||
if handoff.intent == "escalate":
|
||||
await manager.finalize_escalation(handoff, session, current_user.id)
|
||||
|
||||
await db.commit()
|
||||
|
||||
# Best-effort notification dispatch AFTER commit so we never email about
|
||||
# a rolled-back handoff. Failures are swallowed inside the manager —
|
||||
# handoff creation is authoritative; notifications are advisory.
|
||||
if handoff.intent == "escalate":
|
||||
from app.services.handoff_manager import enrich_escalation_async
|
||||
|
||||
await manager.dispatch_escalation_notifications(handoff)
|
||||
# AI enrichment (Sonnet assessment + enhanced escalation_package)
|
||||
# runs in the background after the response is sent so the
|
||||
# escalating engineer doesn't wait on 15-25s of model latency.
|
||||
background_tasks.add_task(
|
||||
enrich_escalation_async, handoff.id, current_user.id
|
||||
)
|
||||
|
||||
return HandoffResponse.model_validate(handoff)
|
||||
|
||||
|
||||
@@ -86,10 +113,16 @@ async def list_handoffs(
|
||||
async def claim_handoff(
|
||||
session_id: UUID,
|
||||
handoff_id: UUID,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
current_user: Annotated[User, Depends(require_engineer_or_admin)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
) -> HandoffResponse:
|
||||
"""Claim a handed-off session."""
|
||||
"""Claim a handed-off session.
|
||||
|
||||
Role-gated to engineer/admin/owner — viewers cannot claim. The race-condition
|
||||
story (two seniors clicking Pick Up simultaneously) depends on auth gating
|
||||
for audit integrity. Codex review flagged this as wedge-relevant; locked
|
||||
in-scope for Escalation Mode v1.
|
||||
"""
|
||||
manager = HandoffManager(db)
|
||||
try:
|
||||
handoff = await manager.claim_session(
|
||||
@@ -114,3 +147,83 @@ async def get_queue(
|
||||
team_id=current_user.team_id,
|
||||
account_id=current_user.account_id,
|
||||
)
|
||||
|
||||
|
||||
# ─── Live escalation arrivals (SSE) ──────────────────────────────────────────
|
||||
#
|
||||
# Streams `handoff_created` events to subscribers in the same account_id as
|
||||
# the new handoff. Connected EscalationQueue instances prepend the new card
|
||||
# with the locked 200ms slide-in. Account-scoped: cross-tenant leakage is
|
||||
# prevented at the bus.publish boundary (only handoff.account_id subscribers
|
||||
# are notified) and re-enforced here by binding the subscription to
|
||||
# current_user.account_id.
|
||||
#
|
||||
# Heartbeat: a `: keepalive\n\n` SSE comment every 25s keeps the connection
|
||||
# alive through Railway / nginx default 60s idle timeouts. Reconnect policy
|
||||
# is on the client (browser EventSource auto-reconnects; our fetch-based
|
||||
# reader retries with backoff).
|
||||
|
||||
|
||||
_HEARTBEAT_INTERVAL_S = 25
|
||||
_QUEUE_GET_TIMEOUT_S = 25 # < heartbeat so heartbeat fires reliably
|
||||
|
||||
|
||||
@queue_router.get("/escalations/stream")
|
||||
async def stream_escalations(
|
||||
request: Request,
|
||||
current_user: Annotated[
|
||||
User,
|
||||
Depends(require_engineer_or_admin, scope="function"),
|
||||
],
|
||||
):
|
||||
"""SSE stream of new escalation arrivals for the current user's account.
|
||||
|
||||
Role-gated to engineer/admin/owner so viewers can't subscribe (matches
|
||||
the queue + claim role surface). One open connection per browser tab is
|
||||
expected; the bus handles fan-out.
|
||||
"""
|
||||
if not current_user.account_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="No account"
|
||||
)
|
||||
|
||||
account_id = current_user.account_id
|
||||
|
||||
async def event_generator() -> AsyncGenerator[str, None]:
|
||||
queue = await escalation_bus.subscribe(account_id)
|
||||
try:
|
||||
# Initial hello so the client knows the stream is live.
|
||||
yield (
|
||||
"event: ready\n"
|
||||
f"data: {json.dumps({'account_id': str(account_id)})}\n\n"
|
||||
)
|
||||
|
||||
while True:
|
||||
if await request.is_disconnected():
|
||||
break
|
||||
try:
|
||||
event = await asyncio.wait_for(
|
||||
queue.get(), timeout=_QUEUE_GET_TIMEOUT_S
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
# Heartbeat keeps the connection alive through proxies.
|
||||
yield ": keepalive\n\n"
|
||||
continue
|
||||
|
||||
event_type = event.get("type", "message")
|
||||
yield (
|
||||
f"event: {event_type}\n"
|
||||
f"data: {json.dumps(event)}\n\n"
|
||||
)
|
||||
finally:
|
||||
await escalation_bus.unsubscribe(account_id, queue)
|
||||
|
||||
return StreamingResponse(
|
||||
event_generator(),
|
||||
media_type="text/event-stream",
|
||||
headers={
|
||||
"Cache-Control": "no-cache",
|
||||
"Connection": "keep-alive",
|
||||
"X-Accel-Buffering": "no",
|
||||
},
|
||||
)
|
||||
|
||||
759
backend/app/api/endpoints/session_suggested_fixes.py
Normal file
759
backend/app/api/endpoints/session_suggested_fixes.py
Normal file
@@ -0,0 +1,759 @@
|
||||
"""Suggested-fix + resolution-note / escalation-package preview-and-post endpoints.
|
||||
|
||||
Phase 3: active suggested fix lookup + decision recording, resolution-note
|
||||
preview with state_version cache.
|
||||
|
||||
Phase 4: resolution-note POST (writeback to PSA + mark resolved), escalation
|
||||
package preview + POST (writeback + mark escalated). Local-only path when
|
||||
the session has no linked PSA ticket: markdown is stored on the session and
|
||||
the status flipped, no external call.
|
||||
|
||||
Per FLOWPILOT-MIGRATION.md Sections 5.2 + 5.4.
|
||||
"""
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.deps import get_current_active_user, get_db, require_engineer_or_admin
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.session_suggested_fix import SessionSuggestedFix
|
||||
from app.models.user import User
|
||||
from app.schemas.session_suggested_fix import (
|
||||
EscalationPackagePostRequest,
|
||||
ResolutionNotePostRequest,
|
||||
ResolutionNotePreviewResponse,
|
||||
ResolutionPostResponse,
|
||||
SessionSuggestedFixDecisionRequest,
|
||||
SessionSuggestedFixDecisionResponse,
|
||||
SessionSuggestedFixOutcomeRequest,
|
||||
SessionSuggestedFixResponse,
|
||||
SessionSuggestedFixScriptRequest,
|
||||
)
|
||||
from app.models.draft_template import DraftTemplate
|
||||
from app.models.session_fact import SessionFact
|
||||
from app.services.escalation_package_generator import EscalationPackageGeneratorService
|
||||
from app.services.preview_cache import preview_cache
|
||||
from app.services.psa_writeback_service import (
|
||||
PSAStatusVerificationError,
|
||||
PSAWritebackService,
|
||||
)
|
||||
from app.services.resolution_note_generator import ResolutionNoteGeneratorService
|
||||
from app.services.template_extraction_service import extract_parameters as _extract_template_parameters
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/ai-sessions/{session_id}", tags=["session-suggested-fixes"])
|
||||
|
||||
|
||||
async def _load_session_or_404(db: AsyncSession, session_id: UUID) -> AISession:
|
||||
"""RLS-scoped session load. 404 covers both missing and cross-tenant."""
|
||||
result = await db.execute(select(AISession).where(AISession.id == session_id))
|
||||
session = result.scalar_one_or_none()
|
||||
if session is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Session not found")
|
||||
return session
|
||||
|
||||
|
||||
# ── Suggested fix: active ──────────────────────────────────────────────────
|
||||
|
||||
@router.get(
|
||||
"/suggested-fixes/active",
|
||||
response_model=SessionSuggestedFixResponse,
|
||||
)
|
||||
async def get_active_suggested_fix(
|
||||
session_id: UUID,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> SessionSuggestedFixResponse:
|
||||
"""Return the current active suggested fix (`superseded_at IS NULL`) or 404.
|
||||
|
||||
A session has at most one active fix. Multiple historical rows persist
|
||||
for audit, but only the most-recent un-superseded one is returned here.
|
||||
"""
|
||||
await _load_session_or_404(db, session_id)
|
||||
result = await db.execute(
|
||||
select(SessionSuggestedFix)
|
||||
.where(
|
||||
SessionSuggestedFix.session_id == session_id,
|
||||
SessionSuggestedFix.superseded_at.is_(None),
|
||||
)
|
||||
.order_by(SessionSuggestedFix.created_at.desc())
|
||||
)
|
||||
fix = result.scalars().first()
|
||||
if fix is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No active suggested fix for this session",
|
||||
)
|
||||
return SessionSuggestedFixResponse.model_validate(fix)
|
||||
|
||||
|
||||
# ── Suggested fix: decision ────────────────────────────────────────────────
|
||||
|
||||
@router.post(
|
||||
"/suggested-fixes/{fix_id}/decision",
|
||||
response_model=SessionSuggestedFixDecisionResponse,
|
||||
)
|
||||
async def record_decision(
|
||||
session_id: UUID,
|
||||
fix_id: UUID,
|
||||
body: SessionSuggestedFixDecisionRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> SessionSuggestedFixDecisionResponse:
|
||||
"""Record the engineer's path choice on a suggested fix.
|
||||
|
||||
Phase 3 recorded the choice and (for `dismissed`) superseded the fix.
|
||||
Phase 5 adds side effects: one_off / draft_template return the rendered
|
||||
script; draft_template also creates a `draft_templates` row via the
|
||||
TemplateExtractionService; build_template returns a redirect to the
|
||||
Script Builder.
|
||||
"""
|
||||
session_obj = await _load_session_or_404(db, session_id)
|
||||
|
||||
result = await db.execute(
|
||||
select(SessionSuggestedFix).where(
|
||||
SessionSuggestedFix.id == fix_id,
|
||||
SessionSuggestedFix.session_id == session_id,
|
||||
)
|
||||
)
|
||||
fix = result.scalar_one_or_none()
|
||||
if fix is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Suggested fix not found"
|
||||
)
|
||||
|
||||
# Once a fix has been superseded we still record the engineer's
|
||||
# decision (it's a historical signal — "engineer dismissed the
|
||||
# interim hypothesis"), but `dismissed` on a superseded row would
|
||||
# be redundant noise.
|
||||
if fix.superseded_at is not None and body.decision == "dismissed":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="This fix is already superseded by a newer suggestion",
|
||||
)
|
||||
|
||||
fix.user_decision = body.decision
|
||||
if body.decision == "dismissed" and fix.superseded_at is None:
|
||||
fix.superseded_at = datetime.now(timezone.utc)
|
||||
|
||||
# Engineer's choice changes the bundle the resolution-note preview sees,
|
||||
# so bump state_version too.
|
||||
await db.execute(
|
||||
update(AISession)
|
||||
.where(AISession.id == session_id)
|
||||
.values(state_version=AISession.state_version + 1)
|
||||
)
|
||||
|
||||
rendered_script: str | None = None
|
||||
draft_template_id: UUID | None = None
|
||||
redirect_path: str | None = None
|
||||
|
||||
# Phase 5 side effects. All three non-dismiss paths assume the fix has
|
||||
# either a script_template_id (template match — use the dedicated
|
||||
# /scripts/generate endpoint from the frontend, not this one) or an
|
||||
# ai_drafted_script (custom script — this is the entry point).
|
||||
if body.decision in ("one_off", "draft_template", "build_template"):
|
||||
drafted = body.edited_script or fix.ai_drafted_script
|
||||
if not drafted:
|
||||
# Template-matched fixes take the regular /scripts/generate path.
|
||||
# If a fix somehow reaches here without a drafted script AND
|
||||
# without a template, that's a client-side wiring bug.
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=(
|
||||
"Suggested fix has no ai_drafted_script — use "
|
||||
"/api/v1/scripts/generate for template-matched fixes."
|
||||
),
|
||||
)
|
||||
rendered_script = drafted.strip()
|
||||
|
||||
if body.decision == "draft_template":
|
||||
# TemplateExtractionService proposes the parameterization. Runs
|
||||
# under the same transaction so a failure rolls back the decision.
|
||||
session_ctx = await _summarize_session_for_extraction(db, session_id)
|
||||
extraction = await _extract_template_parameters(
|
||||
script_body=rendered_script or "",
|
||||
session_context=session_ctx,
|
||||
ticket_context=None, # ticket context wiring lands in Phase 5 polish
|
||||
)
|
||||
|
||||
draft = DraftTemplate(
|
||||
account_id=session_obj.account_id,
|
||||
source_session_id=session_obj.id,
|
||||
source_user_id=current_user.id,
|
||||
script_body=extraction["templated_body"] or (rendered_script or ""),
|
||||
proposed_parameters={"parameters": extraction["parameters"]},
|
||||
proposed_name=fix.title[:200] if fix.title else None,
|
||||
status="pending",
|
||||
)
|
||||
db.add(draft)
|
||||
await db.flush()
|
||||
draft_template_id = draft.id
|
||||
|
||||
if body.decision == "build_template":
|
||||
# Frontend navigates to the Script Builder preloaded with the
|
||||
# drafted body. The builder wires the full parameterization flow;
|
||||
# we hand it a scratch-pad query string, not persistent state.
|
||||
redirect_path = (
|
||||
f"/scripts/builder?from_session={session_obj.id}&fix={fix.id}"
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(fix)
|
||||
|
||||
return SessionSuggestedFixDecisionResponse(
|
||||
id=fix.id,
|
||||
user_decision=fix.user_decision, # type: ignore[arg-type]
|
||||
rendered_script=rendered_script,
|
||||
draft_template_id=draft_template_id,
|
||||
redirect_path=redirect_path,
|
||||
)
|
||||
|
||||
|
||||
# ── Suggested fix: apply (stamp applied_at) ──────────────────────────────
|
||||
|
||||
@router.post(
|
||||
"/suggested-fixes/{fix_id}/apply",
|
||||
response_model=SessionSuggestedFixResponse,
|
||||
)
|
||||
async def apply_suggested_fix(
|
||||
session_id: UUID,
|
||||
fix_id: UUID,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> SessionSuggestedFixResponse:
|
||||
"""Stamp applied_at when the engineer clicks Apply in the ProposalBanner.
|
||||
|
||||
This does NOT change status (fix remains 'proposed'). Status only flips
|
||||
when the engineer records an outcome via PATCH /outcome.
|
||||
|
||||
Rules:
|
||||
- Fix must be in 'proposed' status; any other status → 409.
|
||||
- Idempotent: if applied_at is already set, returns 200 with the unchanged row.
|
||||
- Bumps ai_sessions.state_version so resolve/escalate preview generators
|
||||
know the fix has entered the verifying phase.
|
||||
"""
|
||||
await _load_session_or_404(db, session_id)
|
||||
|
||||
result = await db.execute(
|
||||
select(SessionSuggestedFix).where(
|
||||
SessionSuggestedFix.id == fix_id,
|
||||
SessionSuggestedFix.session_id == session_id,
|
||||
)
|
||||
)
|
||||
fix = result.scalar_one_or_none()
|
||||
if fix is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Suggested fix not found"
|
||||
)
|
||||
|
||||
if fix.status != "proposed":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"Apply is only valid from 'proposed'; fix is already '{fix.status}'",
|
||||
)
|
||||
|
||||
# Idempotent: already stamped → return as-is without bumping state_version again.
|
||||
if fix.applied_at is not None:
|
||||
return SessionSuggestedFixResponse.model_validate(fix)
|
||||
|
||||
fix.applied_at = datetime.now(timezone.utc)
|
||||
|
||||
# Bump state_version so preview generators see the verifying-phase signal.
|
||||
await db.execute(
|
||||
update(AISession)
|
||||
.where(AISession.id == session_id)
|
||||
.values(state_version=AISession.state_version + 1)
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(fix)
|
||||
return SessionSuggestedFixResponse.model_validate(fix)
|
||||
|
||||
|
||||
# ── Suggested fix: outcome ────────────────────────────────────────────────
|
||||
|
||||
@router.patch(
|
||||
"/suggested-fixes/{fix_id}/outcome",
|
||||
response_model=SessionSuggestedFixResponse,
|
||||
)
|
||||
async def patch_suggested_fix_outcome(
|
||||
session_id: UUID,
|
||||
fix_id: UUID,
|
||||
body: SessionSuggestedFixOutcomeRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> SessionSuggestedFixResponse:
|
||||
"""Record the engineer's outcome for an applied fix.
|
||||
|
||||
See `SessionSuggestedFixOutcomeRequest` for transition rules.
|
||||
"""
|
||||
await _load_session_or_404(db, session_id)
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
result = await db.execute(
|
||||
select(SessionSuggestedFix).where(
|
||||
SessionSuggestedFix.id == fix_id,
|
||||
SessionSuggestedFix.session_id == session_id,
|
||||
)
|
||||
)
|
||||
fix = result.scalar_one_or_none()
|
||||
if fix is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Suggested fix not found"
|
||||
)
|
||||
|
||||
if body.outcome == "applied_partial" and not (body.notes and body.notes.strip()):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="notes are required when outcome is applied_partial",
|
||||
)
|
||||
|
||||
TERMINAL = {"applied_success", "applied_failed", "dismissed"}
|
||||
if fix.status in TERMINAL:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"Fix is already in terminal status {fix.status!r}",
|
||||
)
|
||||
|
||||
fix.status = body.outcome
|
||||
if body.outcome == "applied_partial":
|
||||
fix.partial_notes = (body.notes or "").strip() or None
|
||||
elif body.outcome == "applied_failed":
|
||||
fix.failure_reason = (body.notes or "").strip() or None
|
||||
fix.verified_at = now
|
||||
elif body.outcome == "applied_success":
|
||||
fix.verified_at = now
|
||||
# dismissed: no timestamp/notes stamping
|
||||
|
||||
if fix.applied_at is None and body.outcome != "dismissed":
|
||||
fix.applied_at = now
|
||||
|
||||
# Clear any pending AI outcome proposal — engineer has taken a terminal action.
|
||||
fix.ai_outcome_proposal = None
|
||||
|
||||
# Outcome changes the bundle that resolution-note/escalation-package
|
||||
# previews see, so bump state_version inside the same transaction —
|
||||
# mirrors the pattern in record_decision above.
|
||||
await db.execute(
|
||||
update(AISession)
|
||||
.where(AISession.id == session_id)
|
||||
.values(state_version=AISession.state_version + 1)
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(fix)
|
||||
return SessionSuggestedFixResponse.model_validate(fix)
|
||||
|
||||
|
||||
# ── Suggested fix: attach drafted script ─────────────────────────────────────
|
||||
|
||||
@router.patch(
|
||||
"/suggested-fixes/{fix_id}/script",
|
||||
response_model=SessionSuggestedFixResponse,
|
||||
)
|
||||
async def patch_suggested_fix_script(
|
||||
session_id: UUID,
|
||||
fix_id: UUID,
|
||||
body: SessionSuggestedFixScriptRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> SessionSuggestedFixResponse:
|
||||
"""Attach an engineer-drafted script to a suggested fix.
|
||||
|
||||
Called by the inline Script Builder tab on Submit. Does NOT stamp
|
||||
applied_at — a draft is not an application. Bumps state_version so
|
||||
the Resolve/Escalate preview bundles regenerate.
|
||||
"""
|
||||
await _load_session_or_404(db, session_id)
|
||||
|
||||
fix = await db.scalar(
|
||||
select(SessionSuggestedFix).where(
|
||||
SessionSuggestedFix.id == fix_id,
|
||||
SessionSuggestedFix.session_id == session_id,
|
||||
)
|
||||
)
|
||||
if fix is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Suggested fix not found")
|
||||
|
||||
TERMINAL = {"applied_success", "applied_failed", "dismissed"}
|
||||
if fix.status in TERMINAL:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"Fix is already in terminal status {fix.status!r}",
|
||||
)
|
||||
|
||||
fix.ai_drafted_script = body.ai_drafted_script
|
||||
fix.ai_drafted_parameters = body.ai_drafted_parameters
|
||||
|
||||
# Bump state_version on the parent session — previews cached by
|
||||
# (session_id, state_version) must regenerate to reflect the new draft.
|
||||
await db.execute(
|
||||
update(AISession)
|
||||
.where(AISession.id == session_id)
|
||||
.values(state_version=AISession.state_version + 1)
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(fix)
|
||||
return SessionSuggestedFixResponse.model_validate(fix)
|
||||
|
||||
|
||||
# ── Suggested fix: clear AI outcome proposal ("Not yet") ─────────────────────
|
||||
|
||||
@router.delete(
|
||||
"/suggested-fixes/{fix_id}/ai-outcome-proposal",
|
||||
response_model=SessionSuggestedFixResponse,
|
||||
)
|
||||
async def clear_ai_outcome_proposal(
|
||||
session_id: UUID,
|
||||
fix_id: UUID,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> SessionSuggestedFixResponse:
|
||||
"""Explicitly dismiss the AI-proposed outcome banner ("Not yet").
|
||||
|
||||
Clears `ai_outcome_proposal` without touching status or state_version
|
||||
(this is pure UI state, not outcome data). Idempotent: returns 200 even
|
||||
when the field is already null. After this call the banner will not
|
||||
re-surface on the next refreshSessionDerived unless the AI emits a new
|
||||
proposal.
|
||||
"""
|
||||
await _load_session_or_404(db, session_id)
|
||||
|
||||
result = await db.execute(
|
||||
select(SessionSuggestedFix).where(
|
||||
SessionSuggestedFix.id == fix_id,
|
||||
SessionSuggestedFix.session_id == session_id,
|
||||
)
|
||||
)
|
||||
fix = result.scalar_one_or_none()
|
||||
if fix is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Suggested fix not found"
|
||||
)
|
||||
|
||||
fix.ai_outcome_proposal = None
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(fix)
|
||||
return SessionSuggestedFixResponse.model_validate(fix)
|
||||
|
||||
|
||||
async def _summarize_session_for_extraction(
|
||||
db: AsyncSession, session_id: UUID,
|
||||
) -> str:
|
||||
"""Compact fact list for TemplateExtractionService context.
|
||||
|
||||
We don't send the full chat transcript — the extractor only needs enough
|
||||
signal to decide which values in the script are session-specific (and
|
||||
therefore worth parameterizing).
|
||||
"""
|
||||
result = await db.execute(
|
||||
select(SessionFact)
|
||||
.where(
|
||||
SessionFact.session_id == session_id,
|
||||
SessionFact.deleted_at.is_(None),
|
||||
)
|
||||
.order_by(SessionFact.created_at.asc())
|
||||
)
|
||||
facts = list(result.scalars().all())
|
||||
if not facts:
|
||||
return ""
|
||||
lines = [f"- {f.text}" for f in facts]
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
# ── Resolution note preview ────────────────────────────────────────────────
|
||||
|
||||
@router.post(
|
||||
"/resolution-note/preview",
|
||||
response_model=ResolutionNotePreviewResponse,
|
||||
)
|
||||
async def resolution_note_preview(
|
||||
session_id: UUID,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> ResolutionNotePreviewResponse:
|
||||
"""Generate (or return cached) draft markdown for the Resolve note.
|
||||
|
||||
Cache key: `(resolution_note, session_id, state_version)`. State_version is
|
||||
bumped by every fact / suggested-fix / script-generation write, so two
|
||||
consecutive calls with no intervening writes return the same cached
|
||||
payload (and won't pay for a Sonnet call).
|
||||
|
||||
Posted to PSA in Phase 4. Until then, this endpoint is read-only.
|
||||
"""
|
||||
await _load_session_or_404(db, session_id)
|
||||
gen = ResolutionNoteGeneratorService(db)
|
||||
try:
|
||||
payload = await gen.generate_or_get_cached(session_id)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.exception("Resolution note preview failed for session %s", session_id)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=f"Resolution-note generator error ({type(e).__name__})",
|
||||
)
|
||||
return ResolutionNotePreviewResponse(**payload)
|
||||
|
||||
|
||||
# ── Phase 4: escalation-package preview ────────────────────────────────────
|
||||
|
||||
@router.post(
|
||||
"/escalation-package/preview",
|
||||
response_model=ResolutionNotePreviewResponse,
|
||||
)
|
||||
async def escalation_package_preview(
|
||||
session_id: UUID,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> ResolutionNotePreviewResponse:
|
||||
"""Generate (or return cached) draft markdown for the Escalate handoff package.
|
||||
|
||||
Same caching story as the resolution-note preview: keyed on
|
||||
`(session_id, state_version)`. Separate cache kind so a Resolve preview
|
||||
and an Escalate preview for the same state can coexist.
|
||||
"""
|
||||
await _load_session_or_404(db, session_id)
|
||||
gen = EscalationPackageGeneratorService(db)
|
||||
try:
|
||||
payload = await gen.generate_or_get_cached(session_id)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.exception("Escalation package preview failed for session %s", session_id)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=f"Escalation-package generator error ({type(e).__name__})",
|
||||
)
|
||||
return ResolutionNotePreviewResponse(**payload)
|
||||
|
||||
|
||||
# ── Phase 4: Resolve & post ────────────────────────────────────────────────
|
||||
|
||||
@router.post(
|
||||
"/resolution-note/post",
|
||||
response_model=ResolutionPostResponse,
|
||||
)
|
||||
async def post_resolution_note(
|
||||
session_id: UUID,
|
||||
body: ResolutionNotePostRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> ResolutionPostResponse:
|
||||
"""Commit the engineer-edited resolution note and close the session.
|
||||
|
||||
Three outcomes:
|
||||
- **External post + status verified** — session.status='resolved',
|
||||
markdown + external_id + posted_at persisted, CW status flipped to
|
||||
the configured Resolved status ID and re-fetch-verified.
|
||||
- **External post only** — markdown posted, but no cw_resolved_status_id
|
||||
configured → session.status='resolved', `status_transition_skipped_reason`
|
||||
explains the skip. Not an error — posting the note is meaningful.
|
||||
- **Local-only** — session has no linked PSA ticket → markdown stored on
|
||||
`resolution_note_markdown`, session.status='resolved', outcome =
|
||||
'resolved_local'. No external call.
|
||||
|
||||
Status verification failure raises 502: the engineer intended to close
|
||||
the ticket but we cannot confirm it actually closed. Surfacing silent
|
||||
success would be a footgun.
|
||||
"""
|
||||
session_obj = await _load_session_or_404(db, session_id)
|
||||
if session_obj.status not in ("active", "paused", "requesting_escalation", "escalated"):
|
||||
# Already-resolved sessions shouldn't be re-posted; caller should
|
||||
# query first. escalated→resolved is allowed (engineer revised course).
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"Session is already {session_obj.status}",
|
||||
)
|
||||
|
||||
service = PSAWritebackService(db)
|
||||
summary = (body.resolution_summary or body.markdown.strip().splitlines()[0])[:500]
|
||||
|
||||
# Local-only path — no PSA ticket linked, nothing to post.
|
||||
if not session_obj.psa_ticket_id or not session_obj.psa_connection_id:
|
||||
session_obj.resolution_note_markdown = body.markdown.strip()
|
||||
session_obj.status = "resolved"
|
||||
session_obj.resolved_at = datetime.now(timezone.utc)
|
||||
session_obj.resolution_summary = summary
|
||||
await db.commit()
|
||||
return ResolutionPostResponse(
|
||||
outcome="resolved_local",
|
||||
session_status=session_obj.status,
|
||||
)
|
||||
|
||||
try:
|
||||
posted = await service.post_resolution_note(session_obj, body.markdown)
|
||||
except Exception as e:
|
||||
logger.exception("post_resolution_note failed for session %s", session_id)
|
||||
await db.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=f"PSA post failed ({type(e).__name__})",
|
||||
)
|
||||
|
||||
# Attempt the status transition if configured; failed verification is
|
||||
# surfaced loudly (status_code 502) per the ConnectWise anti-silent-
|
||||
# success principle. Not configured → skip with a reason, not an error.
|
||||
target_status_id = await service.resolved_status_id_for_account(session_obj.account_id)
|
||||
verified_status_id: int | None = None
|
||||
verified_status_name: str | None = None
|
||||
skipped_reason: str | None = None
|
||||
if target_status_id is None:
|
||||
skipped_reason = (
|
||||
"No cw_resolved_status_id configured in account_settings.preferences — "
|
||||
"note posted, status unchanged."
|
||||
)
|
||||
else:
|
||||
try:
|
||||
result = await service.transition_ticket_status(session_obj, target_status_id)
|
||||
verified_status_id = result["verified_status_id"]
|
||||
verified_status_name = result["verified_status_name"]
|
||||
except PSAStatusVerificationError as e:
|
||||
logger.error("Status verification failed for session %s: %s", session_id, e)
|
||||
# Note was already posted — roll that partial side effect back in
|
||||
# the session record (the CW note itself can't be un-posted).
|
||||
await db.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=str(e),
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception("Status transition failed for session %s", session_id)
|
||||
await db.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=f"PSA status transition error ({type(e).__name__})",
|
||||
)
|
||||
|
||||
session_obj.status = "resolved"
|
||||
session_obj.resolved_at = datetime.now(timezone.utc)
|
||||
session_obj.resolution_summary = summary
|
||||
await db.commit()
|
||||
|
||||
return ResolutionPostResponse(
|
||||
outcome="resolved",
|
||||
session_status=session_obj.status,
|
||||
external_id=posted["external_id"],
|
||||
posted_at=posted["posted_at"],
|
||||
verified_status_id=verified_status_id,
|
||||
verified_status_name=verified_status_name,
|
||||
status_transition_skipped_reason=skipped_reason,
|
||||
)
|
||||
|
||||
|
||||
# ── Phase 4: Escalate & post ──────────────────────────────────────────────
|
||||
|
||||
@router.post(
|
||||
"/escalation-package/post",
|
||||
response_model=ResolutionPostResponse,
|
||||
)
|
||||
async def post_escalation_package(
|
||||
session_id: UUID,
|
||||
body: EscalationPackagePostRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
_: None = Depends(require_engineer_or_admin),
|
||||
) -> ResolutionPostResponse:
|
||||
"""Commit the engineer-edited escalation package and mark the session escalated.
|
||||
|
||||
Structure mirrors post_resolution_note:
|
||||
- Local-only when no PSA ticket: markdown stored, session.status='escalated'.
|
||||
- PSA post: internal-analysis note (handoff is for the next engineer,
|
||||
not the customer), optional status transition via cw_escalated_status_id,
|
||||
re-fetch verified.
|
||||
"""
|
||||
session_obj = await _load_session_or_404(db, session_id)
|
||||
if session_obj.status not in ("active", "paused", "resolved"):
|
||||
# resolved→escalated is allowed (engineer realized they need help
|
||||
# after closing); escalated→escalated would be a no-op, block it.
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"Session is already {session_obj.status}",
|
||||
)
|
||||
|
||||
service = PSAWritebackService(db)
|
||||
reason = body.escalation_reason or body.markdown.strip().splitlines()[0][:500]
|
||||
|
||||
if not session_obj.psa_ticket_id or not session_obj.psa_connection_id:
|
||||
session_obj.escalation_package_markdown = body.markdown.strip()
|
||||
session_obj.status = "escalated"
|
||||
session_obj.escalation_reason = reason
|
||||
await db.commit()
|
||||
return ResolutionPostResponse(
|
||||
outcome="escalated_local",
|
||||
session_status=session_obj.status,
|
||||
)
|
||||
|
||||
try:
|
||||
posted = await service.post_escalation_package(session_obj, body.markdown)
|
||||
except Exception as e:
|
||||
logger.exception("post_escalation_package failed for session %s", session_id)
|
||||
await db.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=f"PSA post failed ({type(e).__name__})",
|
||||
)
|
||||
|
||||
target_status_id = await service.escalated_status_id_for_account(session_obj.account_id)
|
||||
verified_status_id: int | None = None
|
||||
verified_status_name: str | None = None
|
||||
skipped_reason: str | None = None
|
||||
if target_status_id is None:
|
||||
skipped_reason = (
|
||||
"No cw_escalated_status_id configured — package posted, status unchanged."
|
||||
)
|
||||
else:
|
||||
try:
|
||||
result = await service.transition_ticket_status(session_obj, target_status_id)
|
||||
verified_status_id = result["verified_status_id"]
|
||||
verified_status_name = result["verified_status_name"]
|
||||
except PSAStatusVerificationError as e:
|
||||
logger.error("Status verification failed for session %s: %s", session_id, e)
|
||||
await db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.exception("Status transition failed for session %s", session_id)
|
||||
await db.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||
detail=f"PSA status transition error ({type(e).__name__})",
|
||||
)
|
||||
|
||||
session_obj.status = "escalated"
|
||||
session_obj.escalation_reason = reason
|
||||
await db.commit()
|
||||
|
||||
return ResolutionPostResponse(
|
||||
outcome="escalated",
|
||||
session_status=session_obj.status,
|
||||
external_id=posted["external_id"],
|
||||
posted_at=posted["posted_at"],
|
||||
verified_status_id=verified_status_id,
|
||||
verified_status_name=verified_status_name,
|
||||
status_transition_skipped_reason=skipped_reason,
|
||||
)
|
||||
|
||||
|
||||
# ── Helper used by tests ───────────────────────────────────────────────────
|
||||
|
||||
def _clear_preview_cache_for_tests() -> None:
|
||||
"""Reset the singleton cache between tests."""
|
||||
preview_cache._store.clear() # noqa: SLF001 — test-only access
|
||||
@@ -20,6 +20,7 @@ from app.core.audit import log_audit
|
||||
from app.core.rate_limit import limiter
|
||||
|
||||
router = APIRouter(tags=["shares"])
|
||||
public_router = APIRouter(tags=["shares"])
|
||||
|
||||
|
||||
def build_share_response(share: SessionShare) -> ShareResponse:
|
||||
@@ -206,7 +207,7 @@ async def _get_optional_user(request: Request, db: AsyncSession) -> Optional[Use
|
||||
return None
|
||||
|
||||
|
||||
@router.get("/share/{share_token}", response_model=SharePublicView)
|
||||
@public_router.get("/share/{share_token}", response_model=SharePublicView)
|
||||
@limiter.limit("30/minute")
|
||||
async def access_share(
|
||||
share_token: str,
|
||||
|
||||
@@ -161,7 +161,7 @@ async def get_sidebar_stats(
|
||||
select(func.count()).where(
|
||||
and_(
|
||||
esc_scope,
|
||||
AISession.status == "requesting_escalation",
|
||||
AISession.status.in_(("requesting_escalation", "escalated")),
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -24,6 +24,8 @@ from app.api.endpoints import (
|
||||
branding,
|
||||
categories,
|
||||
copilot,
|
||||
device_types,
|
||||
draft_templates,
|
||||
feedback,
|
||||
flow_proposals,
|
||||
flowpilot_analytics,
|
||||
@@ -32,6 +34,7 @@ from app.api.endpoints import (
|
||||
invite,
|
||||
kb_accelerator,
|
||||
maintenance_schedules,
|
||||
network_diagrams,
|
||||
notifications,
|
||||
onboarding,
|
||||
public_templates,
|
||||
@@ -39,8 +42,10 @@ from app.api.endpoints import (
|
||||
scripts,
|
||||
script_builder,
|
||||
session_branches,
|
||||
session_facts,
|
||||
session_handoffs,
|
||||
session_resolutions,
|
||||
session_suggested_fixes,
|
||||
sessions,
|
||||
shared,
|
||||
shares,
|
||||
@@ -73,9 +78,11 @@ api_router = APIRouter()
|
||||
# ---------------------------------------------------------------------------
|
||||
api_router.include_router(auth.router)
|
||||
api_router.include_router(shared.router) # Public share links (no auth)
|
||||
api_router.include_router(shares.public_router) # Public session share links (optional auth)
|
||||
api_router.include_router(beta_signup.router)
|
||||
api_router.include_router(webhooks.router) # Stripe webhook receiver
|
||||
api_router.include_router(public_templates.router) # Public gallery (no auth, rate-limited)
|
||||
api_router.include_router(survey.router) # Public survey flow (no auth, rate-limited)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Admin endpoints — super_admin only
|
||||
@@ -93,7 +100,6 @@ api_router.include_router(admin_settings.router)
|
||||
api_router.include_router(admin_categories.router)
|
||||
api_router.include_router(admin_survey.router)
|
||||
api_router.include_router(admin_gallery.router)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# User-facing endpoints — tenant context required
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -121,7 +127,6 @@ api_router.include_router(ai_fix.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(ai_chat.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(copilot.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(assistant_chat.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(survey.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(tree_transfer.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(ai_suggestions.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(kb_accelerator.router, dependencies=_tenant_deps)
|
||||
@@ -130,9 +135,15 @@ api_router.include_router(integrations.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(onboarding.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(branding.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(supporting_data.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(network_diagrams.router, dependencies=_tenant_deps)
|
||||
# session_handoffs queue router must come before ai_sessions to avoid conflict
|
||||
api_router.include_router(session_handoffs.queue_router, dependencies=_tenant_deps)
|
||||
api_router.include_router(session_resolutions.router, dependencies=_tenant_deps)
|
||||
# session_facts mounts under /ai-sessions/{id}/facts — register before ai_sessions
|
||||
# so the {session_id}/facts subpaths take precedence over any future generic catchalls.
|
||||
api_router.include_router(session_facts.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(session_suggested_fixes.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(draft_templates.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(ai_sessions.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(flow_proposals.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(flowpilot_analytics.router, dependencies=_tenant_deps)
|
||||
@@ -142,3 +153,4 @@ api_router.include_router(script_builder.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(beta_feedback.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(session_branches.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(session_handoffs.router, dependencies=_tenant_deps)
|
||||
api_router.include_router(device_types.router, dependencies=_tenant_deps)
|
||||
|
||||
@@ -40,7 +40,7 @@ CRITICAL BEHAVIORS:
|
||||
- Act as a senior engineer, not a chatbot. Use your domain knowledge to SUGGEST diagnostic steps, not just record what the user says.
|
||||
- When the user describes a problem area, demonstrate understanding by naming specific sub-categories, common causes, and relevant tools.
|
||||
- Challenge assumptions constructively: "Before we go down that path, have you considered checking X first? In my experience, that resolves 60% of these cases."
|
||||
- Capture SPECIFIC commands with exact syntax. Not "check the service" but "Get-Service ADSync | Select-Object Status, StartType".
|
||||
- Capture SPECIFIC commands with exact syntax (PowerShell/CLI invocations the engineer would actually paste into a shell), not vague directives like "check the service".
|
||||
- Include expected outcomes for every action: what does success look like?
|
||||
- Surface edge cases proactively: "What about multi-forest environments?" or "Does this change if they have conditional access policies?"
|
||||
- Explain WHY the diagnostic order matters: "We check connectivity before auth because a network issue masquerades as an auth failure."
|
||||
@@ -74,7 +74,7 @@ STRUCTURAL RULES:
|
||||
- All IDs must be unique strings (use descriptive slugs like "check-service-status")
|
||||
|
||||
CROSS-REFERENCE / LOOP-BACK PATTERN:
|
||||
When a troubleshooting path needs to loop back (e.g., after remediation, re-verify from an earlier checkpoint), set next_node_id to the target node's ID. Example: an action node "restart-ssh-service" can set next_node_id to "verify-ssh-connection" (an ancestor decision node) to create a re-verification loop.
|
||||
When a troubleshooting path needs to loop back (e.g., after remediation, re-verify from an earlier checkpoint), set next_node_id to the target node's ID — including ancestor decision nodes for re-verification loops. The target ID must already exist somewhere in the tree.
|
||||
"""
|
||||
|
||||
INTERVIEW_PROTOCOL = """
|
||||
@@ -85,7 +85,7 @@ Ask broad questions to understand the problem domain and scope:
|
||||
- What type of issue is this flow for?
|
||||
- Who is the target audience? (Tier 1 help desk, Tier 2, Tier 3?)
|
||||
- What environment assumptions? (On-prem, hybrid, specific vendors?)
|
||||
Demonstrate domain expertise immediately. If the user says "Azure AD Sync failures," show understanding: "Are you primarily seeing password hash sync issues, object attribute sync failures, or full directory sync errors?"
|
||||
Demonstrate domain expertise immediately. When the user names a technology, ask a follow-up that proves you know its common failure modes — a sub-categorization question that only someone fluent in that area would think to ask. Use vocabulary native to whatever the user actually mentioned, not stock examples from past conversations.
|
||||
DO NOT emit [TREE_UPDATE] during scoping. You are still understanding the problem.
|
||||
|
||||
PHASE 2 - DISCOVERY (current_phase: discovery):
|
||||
@@ -130,7 +130,7 @@ Your response is natural conversational text. When the tree structure changes, i
|
||||
|
||||
3. Metadata capture (when you learn the flow's name, description, or tags):
|
||||
[METADATA]
|
||||
{"name": "...", "description": "...", "tags": ["..."]}
|
||||
{"name": "<flow name>", "description": "<one-sentence summary>", "tags": ["<tag1>", "<tag2>"]}
|
||||
[/METADATA]
|
||||
|
||||
IMPORTANT:
|
||||
@@ -172,8 +172,8 @@ STRUCTURAL RULES:
|
||||
- All IDs must be unique descriptive slugs (e.g., "check-dns-resolution", not UUIDs)
|
||||
- The last step MUST be type "procedure_end"
|
||||
- Use section_headers to organize steps into logical phases
|
||||
- Commands are arrays of objects: [{"code": "Get-Service ADSync", "label": "Check sync service", "language": "powershell"}]
|
||||
- Descriptions support [VAR:variable_name] interpolation for intake form variables (e.g., "Connect to [VAR:server_name] via RDP")
|
||||
- Commands are arrays of objects: [{"code": "<exact command>", "label": "<short label>", "language": "powershell|bash|cmd"}]
|
||||
- Descriptions support [VAR:variable_name] interpolation for intake form variables. Pick variable names that fit the procedure being built — do not reuse names from prior conversations.
|
||||
|
||||
VARIABLE INTERPOLATION:
|
||||
When the procedure needs per-execution input (server name, IP address, client name, etc.), use [VAR:variable_name] syntax in descriptions and commands. These map to intake form fields that the engineer fills in before starting.
|
||||
@@ -188,7 +188,7 @@ Understand the process being documented:
|
||||
- Who will execute it? (Tier 1 help desk, Tier 2, senior engineers?)
|
||||
- What environment context? (Specific vendor, on-prem vs cloud, tools available?)
|
||||
- Will this need per-execution input? (server name, client info, IP addresses → intake form fields)
|
||||
Demonstrate domain expertise: if the user says "Exchange Online mailbox migration," show understanding: "Are we covering full tenant-to-tenant migration, on-prem to Exchange Online cutover, or individual mailbox moves with hybrid?"
|
||||
Demonstrate domain expertise: when the user names a process, ask a sub-categorization question that distinguishes which variant of that process they mean (the variants will differ by technology — use vocabulary specific to whatever the user mentioned, not examples from prior chats).
|
||||
DO NOT emit [STEPS_UPDATE] during scoping. You are still understanding the process.
|
||||
|
||||
PHASE 2 - DISCOVERY (current_phase: discovery):
|
||||
@@ -238,12 +238,12 @@ Your response is natural conversational text. When the step structure changes, i
|
||||
|
||||
3. Metadata capture (when you learn the flow's name, description, or tags):
|
||||
[METADATA]
|
||||
{"name": "...", "description": "...", "tags": ["..."]}
|
||||
{"name": "<flow name>", "description": "<one-sentence summary>", "tags": ["<tag1>", "<tag2>"]}
|
||||
[/METADATA]
|
||||
|
||||
4. Intake form suggestion (when intake form fields are identified):
|
||||
[INTAKE_FORM]
|
||||
[{"variable_name": "server_name", "label": "Server Name", "field_type": "text", "required": true, "placeholder": "e.g., DC01", "group_name": "Server Details", "display_order": 1}]
|
||||
[{"variable_name": "<snake_case_name>", "label": "<Human Label>", "field_type": "text|password|select|textarea|number|boolean", "required": true|false, "placeholder": "<short hint, optional>", "group_name": "<section heading, optional>", "display_order": <integer>}]
|
||||
[/INTAKE_FORM]
|
||||
|
||||
IMPORTANT:
|
||||
@@ -659,12 +659,12 @@ Requirements:
|
||||
|
||||
Also provide metadata as a separate JSON object after the steps:
|
||||
[METADATA]
|
||||
{"name": "...", "description": "...", "tags": ["..."]}
|
||||
{"name": "<flow name>", "description": "<one-sentence summary>", "tags": ["<tag1>", "<tag2>"]}
|
||||
[/METADATA]
|
||||
|
||||
If we discussed intake form fields, also include:
|
||||
[INTAKE_FORM]
|
||||
[{"variable_name": "server_name", "label": "Server Name", "field_type": "text", "required": true, "placeholder": "e.g., DC01", "group_name": "Server Details", "display_order": 1}]
|
||||
[{"variable_name": "<snake_case_name>", "label": "<Human Label>", "field_type": "text|password|select|textarea|number|boolean", "required": true|false, "placeholder": "<short hint, optional>", "group_name": "<section heading, optional>", "display_order": <integer>}]
|
||||
[/INTAKE_FORM]"""
|
||||
else:
|
||||
generation_instruction = """Based on our entire conversation, generate the COMPLETE and FINAL TreeStructure JSON for this flow.
|
||||
@@ -681,7 +681,7 @@ Requirements:
|
||||
|
||||
Also provide metadata as a separate JSON object after the tree:
|
||||
[METADATA]
|
||||
{"name": "...", "description": "...", "tags": ["..."]}
|
||||
{"name": "<flow name>", "description": "<one-sentence summary>", "tags": ["<tag1>", "<tag2>"]}
|
||||
[/METADATA]"""
|
||||
|
||||
provider_messages.append({"role": "user", "content": generation_instruction})
|
||||
|
||||
@@ -199,7 +199,10 @@ async def generate_fixes(
|
||||
|
||||
try:
|
||||
text, in_tok, out_tok = await provider.generate_json(
|
||||
system_prompt=FIX_SYSTEM_PROMPT,
|
||||
system_prompt=[
|
||||
{"type": "text", "text": FIX_SYSTEM_PROMPT},
|
||||
# cacheable: stable constant across all fix attempts
|
||||
],
|
||||
messages=messages,
|
||||
max_tokens=2048,
|
||||
)
|
||||
@@ -232,7 +235,11 @@ async def generate_fixes(
|
||||
|
||||
try:
|
||||
text2, in_tok2, out_tok2 = await provider.generate_json(
|
||||
system_prompt=FIX_SYSTEM_PROMPT,
|
||||
system_prompt=[
|
||||
{"type": "text", "text": FIX_SYSTEM_PROMPT},
|
||||
# cacheable: stable constant; retry reads the cached
|
||||
# system block from the first attempt above
|
||||
],
|
||||
messages=messages,
|
||||
max_tokens=2048,
|
||||
)
|
||||
|
||||
@@ -3,16 +3,169 @@ AI Provider abstraction layer.
|
||||
|
||||
Supports Gemini (google-genai) and Anthropic (anthropic) as interchangeable
|
||||
backends for JSON generation used by the AI Flow Builder.
|
||||
|
||||
## Prompt caching (Anthropic only)
|
||||
|
||||
Callers may pass `system_prompt` as either:
|
||||
|
||||
- `str` — backward-compatible, uncached.
|
||||
- `list[SystemBlock]` — Anthropic structured system blocks. Each block is a
|
||||
dict of shape `{"type": "text", "text": str, "cache_control": {...}?}`.
|
||||
|
||||
Caching policy (policy α, per Phase 0.1 design):
|
||||
- If any block in the list carries an explicit `cache_control` key, that
|
||||
caller-authored configuration is honored verbatim.
|
||||
- If no block carries `cache_control`, the provider applies
|
||||
`cache_control: {"type": "ephemeral"}` to the first block only. First block
|
||||
is the common "large static prefix" case (e.g. system prompt, reference data).
|
||||
|
||||
Gemini ignores cache_control and concatenates list blocks into one system
|
||||
string — callers should not rely on Gemini for cache-hit behavior.
|
||||
|
||||
TODO(phase0-verify): When a dev environment is available, verify cache-hit
|
||||
behavior by hitting any FlowPilot endpoint twice within the 5-minute
|
||||
ephemeral TTL. First call should emit `anthropic.cache` with
|
||||
`cache_creation_input_tokens > 0`; second call with `cache_read_input_tokens > 0`.
|
||||
If the second call returns zero reads, inspect the prefix for silent
|
||||
invalidators (timestamps, unsorted JSON keys, varying tool list ordering).
|
||||
"""
|
||||
|
||||
import logging
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import AsyncIterator
|
||||
from typing import Any
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Anthropic structured system block. See module docstring for caching policy.
|
||||
SystemBlock = dict[str, Any]
|
||||
|
||||
|
||||
def _normalize_system_for_anthropic(
|
||||
system_prompt: str | list[SystemBlock],
|
||||
) -> str | list[SystemBlock]:
|
||||
"""Return the value to pass as the `system=` parameter to the Anthropic API.
|
||||
|
||||
- Plain strings pass through untouched (uncached path).
|
||||
- Lists are returned as structured system blocks. If no block in the list
|
||||
carries an explicit `cache_control`, `cache_control: {"type": "ephemeral"}`
|
||||
is applied to the FIRST block only (policy α).
|
||||
- Caller-authored `cache_control` is never overwritten.
|
||||
"""
|
||||
if isinstance(system_prompt, str):
|
||||
return system_prompt
|
||||
|
||||
if not system_prompt:
|
||||
# Empty list is not a meaningful system prompt — pass empty string so
|
||||
# Anthropic treats this as "no system prompt" rather than erroring.
|
||||
return ""
|
||||
|
||||
blocks = [dict(b) for b in system_prompt]
|
||||
already_cached = any("cache_control" in b for b in blocks)
|
||||
|
||||
if not already_cached:
|
||||
blocks[0]["cache_control"] = {"type": "ephemeral"}
|
||||
|
||||
return blocks
|
||||
|
||||
|
||||
def _flatten_system_for_gemini(
|
||||
system_prompt: str | list[SystemBlock],
|
||||
) -> str:
|
||||
"""Gemini has no structured system blocks; concatenate list entries."""
|
||||
if isinstance(system_prompt, str):
|
||||
return system_prompt
|
||||
return "\n\n".join(b.get("text", "") for b in system_prompt)
|
||||
|
||||
|
||||
def build_anthropic_chat_messages(
|
||||
history: list[dict[str, Any]],
|
||||
new_message: str,
|
||||
images: list[dict[str, Any]] | None = None,
|
||||
format_reminder: str | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Construct the Anthropic `messages` payload for a cached multi-turn chat.
|
||||
|
||||
Responsibilities:
|
||||
- Copy the valid history messages in order.
|
||||
- Apply `cache_control: ephemeral` to the LAST history message so the entire
|
||||
conversation prefix is cached across turns. The new user message stays
|
||||
uncached (it changes each turn).
|
||||
- Append `format_reminder` to the new user message if provided. The reminder
|
||||
is invisible to storage (caller's concern) but helps enforce structured
|
||||
output compliance at generation time.
|
||||
- If `images` are provided, render the new user message as a multimodal
|
||||
content block list (images first, then text). Otherwise, render it as
|
||||
a plain string.
|
||||
|
||||
This helper is Anthropic-specific: the cache-breakpoint pattern, ephemeral
|
||||
cache_control, and multimodal block shape are all Anthropic conventions.
|
||||
Do not call it from Gemini code paths.
|
||||
"""
|
||||
messages: list[dict[str, Any]] = []
|
||||
for msg in history:
|
||||
messages.append({"role": msg["role"], "content": msg["content"]})
|
||||
|
||||
# Cache breakpoint on the last existing history message so the entire
|
||||
# conversation prefix is cached across turns. Safe only when there IS a
|
||||
# history message; otherwise the new message is the only message.
|
||||
if messages:
|
||||
last = messages[-1]
|
||||
messages[-1] = {
|
||||
"role": last["role"],
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": last["content"],
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
effective_text = new_message + (format_reminder or "")
|
||||
|
||||
if images:
|
||||
content_blocks: list[dict[str, Any]] = []
|
||||
for img in images:
|
||||
content_blocks.append(
|
||||
{
|
||||
"type": "image",
|
||||
"source": {
|
||||
"type": "base64",
|
||||
"media_type": img["media_type"],
|
||||
"data": img["data"],
|
||||
},
|
||||
}
|
||||
)
|
||||
content_blocks.append({"type": "text", "text": effective_text})
|
||||
messages.append({"role": "user", "content": content_blocks})
|
||||
else:
|
||||
messages.append({"role": "user", "content": effective_text})
|
||||
|
||||
return messages
|
||||
|
||||
|
||||
def _log_anthropic_cache_usage(usage: Any, model: str) -> None:
|
||||
"""Emit a structured log line capturing cache_read / cache_creation tokens."""
|
||||
cache_read = getattr(usage, "cache_read_input_tokens", 0) or 0
|
||||
cache_creation = getattr(usage, "cache_creation_input_tokens", 0) or 0
|
||||
input_tokens = getattr(usage, "input_tokens", 0) or 0
|
||||
output_tokens = getattr(usage, "output_tokens", 0) or 0
|
||||
if cache_read or cache_creation:
|
||||
logger.info(
|
||||
"anthropic.cache",
|
||||
extra={
|
||||
"event": "anthropic.cache",
|
||||
"model": model,
|
||||
"cache_read_input_tokens": cache_read,
|
||||
"cache_creation_input_tokens": cache_creation,
|
||||
"input_tokens": input_tokens,
|
||||
"output_tokens": output_tokens,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class AIProvider(ABC):
|
||||
"""Abstract base class for AI providers."""
|
||||
@@ -20,14 +173,16 @@ class AIProvider(ABC):
|
||||
@abstractmethod
|
||||
async def generate_json(
|
||||
self,
|
||||
system_prompt: str,
|
||||
messages: list[dict[str, str]],
|
||||
system_prompt: str | list[SystemBlock],
|
||||
messages: list[dict[str, Any]],
|
||||
max_tokens: int = 4096,
|
||||
) -> tuple[str, int, int]:
|
||||
"""Generate a JSON response from the AI model.
|
||||
|
||||
Args:
|
||||
system_prompt: System-level instruction for the model.
|
||||
system_prompt: System-level instruction. Plain `str` is uncached
|
||||
(Anthropic) or used as-is (Gemini). `list[SystemBlock]` enables
|
||||
Anthropic prompt caching per module-docstring policy.
|
||||
messages: List of message dicts with "role" and "content" keys.
|
||||
max_tokens: Maximum output tokens.
|
||||
|
||||
@@ -39,37 +194,25 @@ class AIProvider(ABC):
|
||||
@abstractmethod
|
||||
async def generate_text(
|
||||
self,
|
||||
system_prompt: str,
|
||||
messages: list[dict[str, str]],
|
||||
system_prompt: str | list[SystemBlock],
|
||||
messages: list[dict[str, Any]],
|
||||
max_tokens: int = 4096,
|
||||
) -> tuple[str, int, int]:
|
||||
"""Generate a text response from the AI model (no JSON constraint).
|
||||
|
||||
Args:
|
||||
system_prompt: System-level instruction for the model.
|
||||
messages: List of message dicts with "role" and "content" keys.
|
||||
max_tokens: Maximum output tokens.
|
||||
|
||||
Returns:
|
||||
Tuple of (response_text, input_tokens, output_tokens).
|
||||
See `generate_json` for argument semantics.
|
||||
"""
|
||||
...
|
||||
|
||||
async def generate_text_stream(
|
||||
self,
|
||||
system_prompt: str,
|
||||
messages: list[dict[str, str]],
|
||||
system_prompt: str | list[SystemBlock],
|
||||
messages: list[dict[str, Any]],
|
||||
max_tokens: int = 4096,
|
||||
) -> "AsyncIterator[str]":
|
||||
"""Stream a text response token by token.
|
||||
|
||||
Args:
|
||||
system_prompt: System-level instruction for the model.
|
||||
messages: List of message dicts with "role" and "content" keys.
|
||||
max_tokens: Maximum output tokens.
|
||||
|
||||
Yields:
|
||||
Text chunks as they are generated.
|
||||
See `generate_json` for argument semantics.
|
||||
"""
|
||||
raise NotImplementedError("Streaming not supported for this provider")
|
||||
# Make this an async generator to satisfy type checker
|
||||
@@ -85,14 +228,15 @@ class GeminiProvider(AIProvider):
|
||||
|
||||
async def generate_json(
|
||||
self,
|
||||
system_prompt: str,
|
||||
messages: list[dict[str, str]],
|
||||
system_prompt: str | list[SystemBlock],
|
||||
messages: list[dict[str, Any]],
|
||||
max_tokens: int = 4096,
|
||||
) -> tuple[str, int, int]:
|
||||
from google import genai
|
||||
from google.genai import types as genai_types
|
||||
|
||||
client = genai.Client(api_key=self._api_key)
|
||||
system_text = _flatten_system_for_gemini(system_prompt)
|
||||
|
||||
# Convert messages to Gemini Content format
|
||||
contents: list[genai_types.Content] = []
|
||||
@@ -106,7 +250,7 @@ class GeminiProvider(AIProvider):
|
||||
)
|
||||
|
||||
config = genai_types.GenerateContentConfig(
|
||||
system_instruction=system_prompt,
|
||||
system_instruction=system_text,
|
||||
max_output_tokens=max_tokens,
|
||||
response_mime_type="application/json",
|
||||
)
|
||||
@@ -137,14 +281,15 @@ class GeminiProvider(AIProvider):
|
||||
|
||||
async def generate_text(
|
||||
self,
|
||||
system_prompt: str,
|
||||
messages: list[dict[str, str]],
|
||||
system_prompt: str | list[SystemBlock],
|
||||
messages: list[dict[str, Any]],
|
||||
max_tokens: int = 4096,
|
||||
) -> tuple[str, int, int]:
|
||||
from google import genai
|
||||
from google.genai import types as genai_types
|
||||
|
||||
client = genai.Client(api_key=self._api_key)
|
||||
system_text = _flatten_system_for_gemini(system_prompt)
|
||||
|
||||
contents: list[genai_types.Content] = []
|
||||
for msg in messages:
|
||||
@@ -157,7 +302,7 @@ class GeminiProvider(AIProvider):
|
||||
)
|
||||
|
||||
config = genai_types.GenerateContentConfig(
|
||||
system_instruction=system_prompt,
|
||||
system_instruction=system_text,
|
||||
max_output_tokens=max_tokens,
|
||||
# No response_mime_type — allow free-form text
|
||||
)
|
||||
@@ -214,16 +359,17 @@ class AnthropicProvider(AIProvider):
|
||||
|
||||
async def generate_json(
|
||||
self,
|
||||
system_prompt: str,
|
||||
messages: list[dict[str, str]],
|
||||
system_prompt: str | list[SystemBlock],
|
||||
messages: list[dict[str, Any]],
|
||||
max_tokens: int = 4096,
|
||||
) -> tuple[str, int, int]:
|
||||
client = _get_anthropic_client(self._api_key, self._timeout)
|
||||
normalized_system = _normalize_system_for_anthropic(system_prompt)
|
||||
|
||||
response = await client.messages.create(
|
||||
model=self._model,
|
||||
max_tokens=max_tokens,
|
||||
system=system_prompt,
|
||||
system=normalized_system,
|
||||
messages=messages,
|
||||
)
|
||||
|
||||
@@ -231,12 +377,14 @@ class AnthropicProvider(AIProvider):
|
||||
input_tokens = response.usage.input_tokens
|
||||
output_tokens = response.usage.output_tokens
|
||||
|
||||
_log_anthropic_cache_usage(response.usage, self._model)
|
||||
|
||||
return text, input_tokens, output_tokens
|
||||
|
||||
async def generate_text(
|
||||
self,
|
||||
system_prompt: str,
|
||||
messages: list[dict[str, str]],
|
||||
system_prompt: str | list[SystemBlock],
|
||||
messages: list[dict[str, Any]],
|
||||
max_tokens: int = 4096,
|
||||
) -> tuple[str, int, int]:
|
||||
# Anthropic doesn't differentiate between JSON and text mode
|
||||
@@ -244,20 +392,28 @@ class AnthropicProvider(AIProvider):
|
||||
|
||||
async def generate_text_stream(
|
||||
self,
|
||||
system_prompt: str,
|
||||
messages: list[dict[str, str]],
|
||||
system_prompt: str | list[SystemBlock],
|
||||
messages: list[dict[str, Any]],
|
||||
max_tokens: int = 4096,
|
||||
) -> AsyncIterator[str]:
|
||||
client = _get_anthropic_client(self._api_key, self._timeout)
|
||||
normalized_system = _normalize_system_for_anthropic(system_prompt)
|
||||
|
||||
async with client.messages.stream(
|
||||
model=self._model,
|
||||
max_tokens=max_tokens,
|
||||
system=system_prompt,
|
||||
system=normalized_system,
|
||||
messages=messages,
|
||||
) as stream:
|
||||
async for text in stream.text_stream:
|
||||
yield text
|
||||
# Per Anthropic SDK, get_final_message() resolves the stream's
|
||||
# final usage object (including cache_read/cache_creation tokens).
|
||||
try:
|
||||
final = await stream.get_final_message()
|
||||
_log_anthropic_cache_usage(final.usage, self._model)
|
||||
except Exception as exc: # best-effort telemetry, never fail the stream
|
||||
logger.debug("anthropic.cache streaming usage unavailable: %s", exc)
|
||||
|
||||
|
||||
def get_ai_provider(model: str | None = None) -> AIProvider:
|
||||
|
||||
@@ -89,8 +89,10 @@ Additional rules:
|
||||
5. Use unique node IDs prefixed with the branch context (e.g., "gpo-check-link")
|
||||
6. Build the tree bottom-up in your head: create solution/leaf nodes first, then build parent nodes referencing their IDs
|
||||
|
||||
Few-shot example showing correct action node next_node_id usage:
|
||||
{"id": "dns-root", "type": "decision", "question": "Can the client resolve any DNS names?", "help_text": "Run: nslookup google.com", "options": [{"id": "dns-opt-none", "label": "No — nslookup times out or returns 'server failed'", "next_node_id": "dns-check-service"}, {"id": "dns-opt-partial", "label": "Some names resolve but others fail", "next_node_id": "dns-check-specific"}], "children": [{"id": "dns-check-service", "type": "action", "title": "Check DNS Client Service", "description": "Verify the DNS Client service is running on the affected machine", "commands": ["Get-Service -Name Dnscache | Select-Object Status,StartType"], "expected_outcome": "Status should be Running", "next_node_id": "dns-service-solution"}, {"id": "dns-service-solution", "type": "solution", "title": "DNS Service Was Stopped", "description": "The DNS Client service was stopped, preventing all name resolution", "resolution_steps": ["Run: Start-Service Dnscache", "Set startup type: Set-Service Dnscache -StartupType Automatic", "Flush cache: ipconfig /flushdns", "Test: nslookup google.com"]}, {"id": "dns-check-specific", "type": "solution", "title": "Selective DNS Failure — Stale or Missing Records", "description": "Some records resolve correctly, indicating DNS is functional but specific records are stale or missing", "resolution_steps": ["Check DNS server for missing A/CNAME records", "Clear DNS cache on the DNS server: Clear-DnsServerCache", "Flush client cache: ipconfig /flushdns", "Verify with: nslookup <failing-hostname>"]}]}"""
|
||||
SHAPE-ONLY schema example (do not copy this content verbatim — it shows
|
||||
how IDs link, NOT what to ask or run; your real tree must reflect the
|
||||
branch the user described):
|
||||
{"id": "<root-slug>", "type": "decision", "question": "<diagnostic question for THIS branch>", "help_text": "<optional hint>", "options": [{"id": "<opt-1>", "label": "<observable answer 1>", "next_node_id": "<child-1>"}, {"id": "<opt-2>", "label": "<observable answer 2>", "next_node_id": "<child-2>"}], "children": [{"id": "<child-1>", "type": "action", "title": "<what to do>", "description": "<details>", "commands": ["<exact command for THIS branch>"], "expected_outcome": "<what success looks like>", "next_node_id": "<sibling-id>"}, {"id": "<sibling-id>", "type": "solution", "title": "<resolution title>", "description": "<resolution description>", "resolution_steps": ["<step 1>", "<step 2>"]}, {"id": "<child-2>", "type": "solution", "title": "<other resolution>", "description": "<...>", "resolution_steps": ["<step 1>"]}]}"""
|
||||
|
||||
|
||||
CORRECTIVE_PROMPT_TEMPLATE = """Your previous JSON was invalid for ResolutionFlow's tree schema.
|
||||
@@ -146,7 +148,10 @@ async def scaffold_branches(
|
||||
user_message += f"Environment: {', '.join(tags)}\n"
|
||||
|
||||
raw_text, input_tokens, output_tokens = await provider.generate_json(
|
||||
system_prompt=SCAFFOLD_SYSTEM_PROMPT,
|
||||
system_prompt=[
|
||||
{"type": "text", "text": SCAFFOLD_SYSTEM_PROMPT},
|
||||
# cacheable: stable constant across all scaffold calls
|
||||
],
|
||||
messages=[{"role": "user", "content": user_message}],
|
||||
max_tokens=2048,
|
||||
)
|
||||
@@ -207,7 +212,13 @@ async def generate_branch_detail(
|
||||
|
||||
for attempt in range(3):
|
||||
raw_text, input_tokens, output_tokens = await provider.generate_json(
|
||||
system_prompt=BRANCH_DETAIL_SYSTEM_PROMPT,
|
||||
system_prompt=[
|
||||
{"type": "text", "text": BRANCH_DETAIL_SYSTEM_PROMPT},
|
||||
# cacheable: stable constant. Retries in this loop re-read the
|
||||
# cached system block rather than paying full input cost each
|
||||
# attempt — the ~2.5k-token prompt with few-shot example is
|
||||
# the dominant cost here.
|
||||
],
|
||||
messages=messages,
|
||||
max_tokens=8192,
|
||||
)
|
||||
|
||||
@@ -111,6 +111,14 @@ class Settings(BaseSettings):
|
||||
GOOGLE_AI_API_KEY: Optional[str] = None
|
||||
AI_MODEL_GEMINI: str = "gemini-2.5-flash"
|
||||
AI_MODEL_ANTHROPIC: str = "claude-sonnet-4-6"
|
||||
# 15s is generous for the click-path; Claude usually returns a 500-token
|
||||
# diagnostic in 4-8s but tail latency on the assessment prompt has hit
|
||||
# 12-14s in the field. Going below this leaves too many escalations with
|
||||
# the "Assessment unavailable — model didn't respond in time" placeholder
|
||||
# the senior sees on the magic-moment screen. Real fix is async generation
|
||||
# (kick off, persist when done, surface "still computing" with refresh) —
|
||||
# that's a follow-up; bumping the bound keeps the wedge demo coherent.
|
||||
ESCALATION_AI_ASSESSMENT_TIMEOUT_SECONDS: int = 15
|
||||
|
||||
# Model tier routing — maps action types to model tiers
|
||||
AI_MODEL_TIERS: dict[str, str] = {
|
||||
@@ -128,6 +136,24 @@ class Settings(BaseSettings):
|
||||
"variable_inference": "fast",
|
||||
"kb_convert": "standard",
|
||||
"script_build": "standard",
|
||||
"network_diagram_generate": "standard",
|
||||
# FlowPilot migration Phase 2 — short, latency-sensitive transformation
|
||||
# of an engineer's answer/check output into a candidate fact.
|
||||
# Doc Section 6.6 sets Haiku as the default; instrumentation tracks
|
||||
# disputed_fact_rate so we can escalate to Sonnet if quality drops.
|
||||
"fact_synthesis": "fast",
|
||||
# FlowPilot migration Phase 3 — resolution-note preview that ships to
|
||||
# the customer ticket. Sonnet because customer-facing artifact quality
|
||||
# matters more than latency; the in-process state_version cache keeps
|
||||
# cost manageable.
|
||||
"resolution_note": "standard",
|
||||
# FlowPilot migration Phase 4 — escalation handoff package. Parallel
|
||||
# to resolution_note: Sonnet, same cache story, no MCP.
|
||||
"escalation_package": "standard",
|
||||
# FlowPilot migration Phase 5 — extract a parameter schema from a
|
||||
# concrete rendered script so a draft_template can be proposed.
|
||||
# Creates a persistent library artifact on accept, so Sonnet.
|
||||
"template_extraction": "standard",
|
||||
}
|
||||
|
||||
def get_model_for_action(self, action_type: str) -> str:
|
||||
|
||||
105
backend/app/core/escalation_bus.py
Normal file
105
backend/app/core/escalation_bus.py
Normal file
@@ -0,0 +1,105 @@
|
||||
"""In-memory pub/sub bus for live escalation events.
|
||||
|
||||
Single-process, non-durable. When a handoff fires, every connected SSE
|
||||
subscriber for the same `account_id` receives the event. Subscribers come
|
||||
and go as senior techs open and close the EscalationQueue page.
|
||||
|
||||
Pre-PMF scale (3 pilots × 5-20 techs/MSP = ~15-60 concurrent subscribers
|
||||
total, single Railway replica) makes in-memory the right call. When the
|
||||
deployment scales horizontally, swap this for Redis pub/sub or similar —
|
||||
the public surface (`publish` / `subscribe`) is intentionally narrow so
|
||||
the swap is local.
|
||||
|
||||
Events are JSON-serializable dicts. `publish()` is non-blocking (drops the
|
||||
event if a subscriber's queue is full rather than back-pressuring the
|
||||
caller). `subscribe()` MUST be paired with `unsubscribe()` in a finally
|
||||
block, or you leak queues.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Bound how many unconsumed events can sit in a subscriber's queue before
|
||||
# we start dropping. 64 is generous for the queue-page use case; if a
|
||||
# subscriber is that far behind, they're probably gone or stuck.
|
||||
_QUEUE_MAXSIZE = 64
|
||||
|
||||
|
||||
class EscalationBus:
|
||||
"""Account-scoped pub/sub for escalation arrival events."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._subscribers: dict[UUID, set[asyncio.Queue[dict[str, Any]]]] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
@staticmethod
|
||||
def _normalize_account_id(account_id: UUID | str) -> UUID:
|
||||
return account_id if isinstance(account_id, UUID) else UUID(str(account_id))
|
||||
|
||||
async def subscribe(self, account_id: UUID | str) -> asyncio.Queue[dict[str, Any]]:
|
||||
"""Register a new subscriber queue for an account.
|
||||
|
||||
Caller must invoke `unsubscribe(account_id, queue)` when the
|
||||
consumer disconnects.
|
||||
"""
|
||||
normalized_account_id = self._normalize_account_id(account_id)
|
||||
queue: asyncio.Queue[dict[str, Any]] = asyncio.Queue(
|
||||
maxsize=_QUEUE_MAXSIZE
|
||||
)
|
||||
async with self._lock:
|
||||
self._subscribers.setdefault(normalized_account_id, set()).add(queue)
|
||||
return queue
|
||||
|
||||
async def unsubscribe(
|
||||
self, account_id: UUID | str, queue: asyncio.Queue[dict[str, Any]]
|
||||
) -> None:
|
||||
normalized_account_id = self._normalize_account_id(account_id)
|
||||
async with self._lock:
|
||||
subs = self._subscribers.get(normalized_account_id)
|
||||
if subs is None:
|
||||
return
|
||||
subs.discard(queue)
|
||||
if not subs:
|
||||
self._subscribers.pop(normalized_account_id, None)
|
||||
|
||||
async def publish(self, account_id: UUID | str, event: dict[str, Any]) -> int:
|
||||
"""Fan event out to every subscriber for `account_id`.
|
||||
|
||||
Returns the number of subscribers that successfully received the
|
||||
event. Drops the event for any subscriber whose queue is full
|
||||
(logs at warning level).
|
||||
"""
|
||||
normalized_account_id = self._normalize_account_id(account_id)
|
||||
async with self._lock:
|
||||
subs = list(self._subscribers.get(normalized_account_id, ()))
|
||||
if not subs:
|
||||
return 0
|
||||
delivered = 0
|
||||
for queue in subs:
|
||||
try:
|
||||
queue.put_nowait(event)
|
||||
delivered += 1
|
||||
except asyncio.QueueFull:
|
||||
logger.warning(
|
||||
"EscalationBus: dropped event for full subscriber queue "
|
||||
"(account_id=%s, event=%s)",
|
||||
normalized_account_id,
|
||||
event.get("type", "?"),
|
||||
)
|
||||
return delivered
|
||||
|
||||
def subscriber_count(self, account_id: UUID | str) -> int:
|
||||
"""Diagnostic — number of active subscribers for an account."""
|
||||
normalized_account_id = self._normalize_account_id(account_id)
|
||||
return len(self._subscribers.get(normalized_account_id, ()))
|
||||
|
||||
|
||||
# Module-level singleton. FastAPI imports this; `subscribe()` and `publish()`
|
||||
# are coroutine-safe via the internal Lock.
|
||||
bus = EscalationBus()
|
||||
@@ -153,48 +153,29 @@ Identify values that would change between executions (server names, IPs, usernam
|
||||
|
||||
## Output Format
|
||||
|
||||
Return a JSON object:
|
||||
Return a JSON object with this SHAPE (DO NOT copy the placeholders below
|
||||
verbatim — fill each field with content derived from the actual KB article
|
||||
the engineer attached, NOT from this schema):
|
||||
```json
|
||||
{
|
||||
"title": "Procedure title derived from the article",
|
||||
"description": "Brief description of what this procedure accomplishes",
|
||||
"title": "<procedure title derived from the article>",
|
||||
"description": "<brief description of what this procedure accomplishes>",
|
||||
"steps": [
|
||||
{
|
||||
"id": "unique-step-id",
|
||||
"type": "step",
|
||||
"content": "Open Server Manager and navigate to Add Roles on [VAR:server_name]",
|
||||
"confidence": 0.95,
|
||||
"source_excerpt": "Step 1: Open Server Manager on DC01..."
|
||||
},
|
||||
{
|
||||
"id": "warning-dns",
|
||||
"type": "warning",
|
||||
"content": "WARNING: This will restart DNS and cause brief connectivity loss",
|
||||
"confidence": 0.90,
|
||||
"source_excerpt": "Note: Restarting DNS will cause a brief outage"
|
||||
},
|
||||
{
|
||||
"id": "section-verification",
|
||||
"type": "section_header",
|
||||
"content": "Verification Steps",
|
||||
"confidence": 1.0,
|
||||
"source_excerpt": "Verification"
|
||||
"id": "<unique-kebab-case-id>",
|
||||
"type": "step|warning|section_header",
|
||||
"content": "<step body — may include [VAR:<your_variable>] interpolation>",
|
||||
"confidence": <float 0.0-1.0>,
|
||||
"source_excerpt": "<the verbatim sentence/phrase from the article that this step came from>"
|
||||
}
|
||||
],
|
||||
"intake_form": [
|
||||
{
|
||||
"variable_name": "server_name",
|
||||
"label": "Server Name",
|
||||
"field_type": "text",
|
||||
"required": true,
|
||||
"display_order": 1
|
||||
},
|
||||
{
|
||||
"variable_name": "ip_address",
|
||||
"label": "IP Address",
|
||||
"field_type": "text",
|
||||
"required": true,
|
||||
"display_order": 2
|
||||
"variable_name": "<snake_case_name fitting THIS procedure>",
|
||||
"label": "<Human Label>",
|
||||
"field_type": "text|password|select|textarea|number|boolean",
|
||||
"required": true|false,
|
||||
"display_order": <integer>
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -425,7 +406,12 @@ async def convert_document(
|
||||
|
||||
try:
|
||||
raw_text, input_tokens, output_tokens = await provider.generate_json(
|
||||
system_prompt=system_prompt,
|
||||
system_prompt=[
|
||||
{"type": "text", "text": system_prompt},
|
||||
# cacheable: one of two stable constants (TROUBLESHOOTING_SYSTEM_PROMPT
|
||||
# or PROCEDURAL_SYSTEM_PROMPT) selected by target_type. Each
|
||||
# variant caches independently by text content.
|
||||
],
|
||||
messages=[{"role": "user", "content": user_message}],
|
||||
max_tokens=16384,
|
||||
)
|
||||
|
||||
@@ -56,6 +56,12 @@ from .session_handoff import SessionHandoff
|
||||
from .session_resolution_output import SessionResolutionOutput
|
||||
from .template_tree import TemplateTree
|
||||
from .platform_step import PlatformStep
|
||||
from .device_type import DeviceType
|
||||
from .network_diagram import NetworkDiagram
|
||||
from .session_fact import SessionFact
|
||||
from .session_suggested_fix import SessionSuggestedFix
|
||||
from .draft_template import DraftTemplate
|
||||
from .account_settings import AccountSettings
|
||||
|
||||
__all__ = [
|
||||
"User",
|
||||
@@ -126,4 +132,10 @@ __all__ = [
|
||||
"SessionResolutionOutput",
|
||||
"TemplateTree",
|
||||
"PlatformStep",
|
||||
"DeviceType",
|
||||
"NetworkDiagram",
|
||||
"SessionFact",
|
||||
"SessionSuggestedFix",
|
||||
"DraftTemplate",
|
||||
"AccountSettings",
|
||||
]
|
||||
|
||||
99
backend/app/models/account_settings.py
Normal file
99
backend/app/models/account_settings.py
Normal file
@@ -0,0 +1,99 @@
|
||||
"""Per-account settings with a JSONB preferences grab-bag.
|
||||
|
||||
Rows are created lazily on first write. Reads of a missing row return the
|
||||
caller-supplied default — no upfront row creation per account.
|
||||
|
||||
Settings live in `preferences` until they meet the promotion criteria in
|
||||
Section 4.6 of FLOWPILOT-MIGRATION.md (hot path / validation / joins), at
|
||||
which point a future migration adds a typed column and the helpers prefer it.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import DateTime, ForeignKey, text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB, insert as pg_insert
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.sql import select
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.account import Account
|
||||
|
||||
|
||||
class AccountSettings(Base):
|
||||
"""One row per account. Created lazily on first `set_setting` call."""
|
||||
__tablename__ = "account_settings"
|
||||
|
||||
account_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("accounts.id", ondelete="CASCADE"),
|
||||
primary_key=True,
|
||||
)
|
||||
preferences: Mapped[dict[str, Any]] = mapped_column(
|
||||
JSONB, nullable=False, default=dict, server_default=text("'{}'::jsonb")
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc),
|
||||
onupdate=lambda: datetime.now(timezone.utc),
|
||||
)
|
||||
|
||||
account: Mapped["Account"] = relationship("Account", foreign_keys=[account_id])
|
||||
|
||||
@classmethod
|
||||
async def get_setting(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
account_id: uuid.UUID,
|
||||
key: str,
|
||||
default: Any = None,
|
||||
) -> Any:
|
||||
"""Return preferences[key] for the account, or `default` if no row/no key.
|
||||
|
||||
Never creates a row — this is the pure-read path.
|
||||
"""
|
||||
result = await db.execute(
|
||||
select(cls.preferences).where(cls.account_id == account_id)
|
||||
)
|
||||
prefs = result.scalar_one_or_none()
|
||||
if prefs is None:
|
||||
return default
|
||||
return prefs.get(key, default)
|
||||
|
||||
@classmethod
|
||||
async def set_setting(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
account_id: uuid.UUID,
|
||||
key: str,
|
||||
value: Any,
|
||||
) -> None:
|
||||
"""Upsert preferences[key] = value for the account.
|
||||
|
||||
Creates the row on first write; on subsequent writes, merges the key
|
||||
into the existing preferences JSON without clobbering other keys.
|
||||
Uses PostgreSQL's `||` jsonb merge operator via ON CONFLICT DO UPDATE.
|
||||
"""
|
||||
stmt = pg_insert(cls).values(
|
||||
account_id=account_id,
|
||||
preferences={key: value},
|
||||
)
|
||||
stmt = stmt.on_conflict_do_update(
|
||||
index_elements=[cls.account_id],
|
||||
set_={
|
||||
# Merge the new {key: value} into the existing preferences.
|
||||
# The `||` operator on jsonb overwrites matching keys and keeps
|
||||
# all other keys intact.
|
||||
"preferences": cls.preferences.op("||")(stmt.excluded.preferences),
|
||||
"updated_at": text("now()"),
|
||||
},
|
||||
)
|
||||
await db.execute(stmt)
|
||||
@@ -10,7 +10,7 @@ from typing import Optional, Any, TYPE_CHECKING
|
||||
from sqlalchemy import String, Text, DateTime, ForeignKey, Boolean, Integer, Float, CheckConstraint
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB, TSVECTOR
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
@@ -46,6 +46,7 @@ class AISession(Base):
|
||||
"confidence_tier IN ('guided', 'exploring', 'discovery')",
|
||||
name="ck_ai_sessions_confidence_tier",
|
||||
),
|
||||
sa.Index("idx_ai_sessions_search", "search_vector", postgresql_using="gin"),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
@@ -150,6 +151,18 @@ class AISession(Base):
|
||||
Text, nullable=True,
|
||||
comment="Why escalated (set on escalation)",
|
||||
)
|
||||
search_vector: Mapped[Optional[str]] = mapped_column(
|
||||
TSVECTOR,
|
||||
sa.Computed(
|
||||
"to_tsvector('english', "
|
||||
"coalesce(problem_summary, '') || ' ' || "
|
||||
"coalesce(resolution_summary, '') || ' ' || "
|
||||
"coalesce(escalation_reason, '') || ' ' || "
|
||||
"coalesce(problem_domain, ''))",
|
||||
persisted=True,
|
||||
),
|
||||
nullable=True,
|
||||
)
|
||||
escalation_package: Mapped[Optional[dict[str, Any]]] = mapped_column(
|
||||
JSONB, nullable=True,
|
||||
comment="Context package for receiving engineer: steps_tried, hypotheses, suggestions",
|
||||
@@ -214,6 +227,38 @@ class AISession(Base):
|
||||
comment="Current task lane state: {questions: [...], actions: [...]}",
|
||||
)
|
||||
|
||||
# ── Resolution / Escalation artifacts (Phase 1 — FlowPilot migration) ──
|
||||
# Markdown of the posted note + PSA external ID for round-trip traceability.
|
||||
resolution_note_markdown: Mapped[Optional[str]] = mapped_column(
|
||||
Text, nullable=True,
|
||||
comment="Final Resolve note markdown, as posted to the PSA",
|
||||
)
|
||||
resolution_note_posted_at: Mapped[Optional[datetime]] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True,
|
||||
)
|
||||
resolution_note_external_id: Mapped[Optional[str]] = mapped_column(
|
||||
String(128), nullable=True,
|
||||
comment="PSA (e.g. CW) ticket-note ID returned at post time",
|
||||
)
|
||||
escalation_package_markdown: Mapped[Optional[str]] = mapped_column(
|
||||
Text, nullable=True,
|
||||
comment="Final Escalate handoff package markdown, as posted to the PSA",
|
||||
)
|
||||
escalation_package_posted_at: Mapped[Optional[datetime]] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True,
|
||||
)
|
||||
escalation_package_external_id: Mapped[Optional[str]] = mapped_column(
|
||||
String(128), nullable=True,
|
||||
comment="PSA ticket-note ID for the escalation package",
|
||||
)
|
||||
# Incremented atomically by any write that invalidates the resolution
|
||||
# note preview cache (facts, suggested fixes, script generations).
|
||||
# See FLOWPILOT-MIGRATION.md Section 5.5.
|
||||
state_version: Mapped[int] = mapped_column(
|
||||
Integer, nullable=False, default=0, server_default=sa.text("0"),
|
||||
comment="Monotonic preview-cache version; bumped on state-changing writes",
|
||||
)
|
||||
|
||||
# ── Branching ──
|
||||
is_branching: Mapped[bool] = mapped_column(
|
||||
default=False,
|
||||
|
||||
47
backend/app/models/device_type.py
Normal file
47
backend/app/models/device_type.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Device type model for network diagrams."""
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import String, Boolean, Integer, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class DeviceType(Base):
|
||||
"""A device type for network diagram nodes (platform or account-custom)."""
|
||||
__tablename__ = "device_types"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
slug: Mapped[str] = mapped_column(
|
||||
String(50), nullable=False,
|
||||
comment="Unique identifier used in diagram node data",
|
||||
)
|
||||
label: Mapped[str] = mapped_column(
|
||||
String(100), nullable=False,
|
||||
comment="Display name",
|
||||
)
|
||||
category: Mapped[str] = mapped_column(
|
||||
String(50), nullable=False,
|
||||
comment="network, compute, storage, cloud, endpoint, infrastructure, security",
|
||||
)
|
||||
is_system: Mapped[bool] = mapped_column(
|
||||
Boolean, nullable=False, default=False,
|
||||
comment="True for built-in types that cannot be deleted",
|
||||
)
|
||||
account_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("accounts.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
comment="Platform account for system types, tenant account for custom types",
|
||||
)
|
||||
sort_order: Mapped[int] = mapped_column(
|
||||
Integer, nullable=False, default=0,
|
||||
comment="Display order within category",
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
91
backend/app/models/draft_template.py
Normal file
91
backend/app/models/draft_template.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""Draft template model — scripts generated during a session, pending templatization.
|
||||
|
||||
Created when an engineer picks "Run now, templatize after resolve" in the
|
||||
three-option dialog. Post-resolve, the TemplatizePrompt component reads pending
|
||||
drafts and lets the engineer accept (promotes to `script_templates`) or reject.
|
||||
"""
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import (
|
||||
Text, DateTime, ForeignKey, String, CheckConstraint,
|
||||
)
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.account import Account
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.user import User
|
||||
from app.models.script_template import ScriptCategory, ScriptTemplate
|
||||
|
||||
|
||||
class DraftTemplate(Base):
|
||||
"""A session-generated script pending conversion to a reusable template."""
|
||||
__tablename__ = "draft_templates"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"status IN ('pending', 'accepted', 'rejected')",
|
||||
name="ck_draft_templates_status",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
account_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("accounts.id"),
|
||||
nullable=False,
|
||||
)
|
||||
source_session_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("ai_sessions.id"),
|
||||
nullable=False,
|
||||
)
|
||||
source_user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id"),
|
||||
nullable=False,
|
||||
)
|
||||
script_body: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
proposed_parameters: Mapped[dict[str, Any]] = mapped_column(
|
||||
JSONB, nullable=False
|
||||
)
|
||||
proposed_name: Mapped[str | None] = mapped_column(String(200), nullable=True)
|
||||
proposed_category_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("script_categories.id"),
|
||||
nullable=True,
|
||||
)
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(32), nullable=False, default="pending"
|
||||
)
|
||||
resolved_at: Mapped[datetime | None] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True
|
||||
)
|
||||
# Set when status transitions to 'accepted' and the draft is promoted
|
||||
# to a real script_templates row.
|
||||
promoted_template_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("script_templates.id"),
|
||||
nullable=True,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
|
||||
account: Mapped["Account"] = relationship("Account", foreign_keys=[account_id])
|
||||
source_session: Mapped["AISession"] = relationship(
|
||||
"AISession", foreign_keys=[source_session_id]
|
||||
)
|
||||
source_user: Mapped["User"] = relationship("User", foreign_keys=[source_user_id])
|
||||
proposed_category: Mapped["ScriptCategory | None"] = relationship(
|
||||
"ScriptCategory", foreign_keys=[proposed_category_id]
|
||||
)
|
||||
promoted_template: Mapped["ScriptTemplate | None"] = relationship(
|
||||
"ScriptTemplate", foreign_keys=[promoted_template_id]
|
||||
)
|
||||
53
backend/app/models/network_diagram.py
Normal file
53
backend/app/models/network_diagram.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""Network diagram model."""
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import String, Text, Boolean, DateTime, ForeignKey, text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.user import User
|
||||
|
||||
|
||||
class NetworkDiagram(Base):
|
||||
"""A network topology diagram scoped to one account."""
|
||||
__tablename__ = "network_diagrams"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
account_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("accounts.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
client_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
asset_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
nodes: Mapped[list[dict[str, Any]]] = mapped_column(JSONB, nullable=False, server_default=text("'[]'::jsonb"))
|
||||
edges: Mapped[list[dict[str, Any]]] = mapped_column(JSONB, nullable=False, server_default=text("'[]'::jsonb"))
|
||||
thumbnail_url: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
is_archived: Mapped[bool] = mapped_column(
|
||||
Boolean, nullable=False, default=False,
|
||||
)
|
||||
created_by: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id"),
|
||||
nullable=True,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc),
|
||||
onupdate=lambda: datetime.now(timezone.utc),
|
||||
)
|
||||
|
||||
creator: Mapped["User | None"] = relationship("User", foreign_keys=[created_by])
|
||||
@@ -62,6 +62,16 @@ class ScriptBuilderSession(Base):
|
||||
nullable=True,
|
||||
comment="Link to FlowPilot session if launched from there",
|
||||
)
|
||||
origin: Mapped[str] = mapped_column(
|
||||
String(20),
|
||||
nullable=False,
|
||||
default="standalone",
|
||||
comment=(
|
||||
"Session origin — 'standalone' (from /script-builder) or "
|
||||
"'pilot_inline' (from FlowPilot Script Builder tab). "
|
||||
"Invariant: pilot_inline rows must have ai_session_id set."
|
||||
),
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
|
||||
@@ -78,6 +78,20 @@ class ScriptTemplate(Base):
|
||||
is_gallery_featured: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=text("false"), index=True)
|
||||
gallery_sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text("0"))
|
||||
usage_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text("0"))
|
||||
# ── Provenance (Phase 1 — FlowPilot migration) ──
|
||||
# Populated when a template is promoted from a post-resolve draft_templates row.
|
||||
# Powers the Script Library provenance chip:
|
||||
# "generated from CW #X · resolved by Y · used N times"
|
||||
source_session_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("ai_sessions.id"), nullable=True,
|
||||
)
|
||||
source_user_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("users.id"), nullable=True,
|
||||
)
|
||||
source_ticket_ref: Mapped[Optional[str]] = mapped_column(
|
||||
String(64), nullable=True,
|
||||
comment="Human-readable PSA ticket ref for display, e.g. 'CW #48307'",
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
|
||||
79
backend/app/models/session_fact.py
Normal file
79
backend/app/models/session_fact.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Session fact model — the "What we know" backing store for a FlowPilot session.
|
||||
|
||||
A fact is an atomic, engineer-readable statement of what has been confirmed
|
||||
during troubleshooting. Facts accumulate across the session and drive the
|
||||
resolution note preview.
|
||||
|
||||
`source_ref` is a polymorphic pointer to a task-lane item inside
|
||||
`ai_sessions.pending_task_lane` JSON — it is NOT a FK. Integrity is enforced
|
||||
at the service layer per the FLOWPILOT-MIGRATION design doc Section 4.2.
|
||||
Phase 2 assigns stable UUIDs to those task-lane items so `source_ref` has
|
||||
something reliable to point to.
|
||||
"""
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import Text, DateTime, ForeignKey, String, CheckConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.account import Account
|
||||
from app.models.user import User
|
||||
|
||||
|
||||
class SessionFact(Base):
|
||||
"""A single fact in the What-we-know section of a session's task lane."""
|
||||
__tablename__ = "session_facts"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"source_type IN ('question', 'diagnostic_check', 'user_note', 'ai_synthesis')",
|
||||
name="ck_session_facts_source_type",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
session_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("ai_sessions.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
)
|
||||
account_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("accounts.id"),
|
||||
nullable=False,
|
||||
)
|
||||
text: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
source_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
# Pointer to a task-lane item UUID inside ai_sessions.pending_task_lane.
|
||||
# NOT a FK. Null for `user_note` and `ai_synthesis` sources.
|
||||
source_ref: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True), nullable=True
|
||||
)
|
||||
source_summary: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
created_by: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id"),
|
||||
nullable=False,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc),
|
||||
onupdate=lambda: datetime.now(timezone.utc),
|
||||
)
|
||||
deleted_at: Mapped[datetime | None] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True
|
||||
)
|
||||
|
||||
session: Mapped["AISession"] = relationship("AISession", foreign_keys=[session_id])
|
||||
account: Mapped["Account"] = relationship("Account", foreign_keys=[account_id])
|
||||
creator: Mapped["User"] = relationship("User", foreign_keys=[created_by])
|
||||
100
backend/app/models/session_suggested_fix.py
Normal file
100
backend/app/models/session_suggested_fix.py
Normal file
@@ -0,0 +1,100 @@
|
||||
"""Session suggested-fix model — AI-proposed resolution path for a session.
|
||||
|
||||
A session can have multiple suggested fixes over its lifetime as the AI's
|
||||
understanding evolves. Only one is active at a time (superseded_at IS NULL);
|
||||
emitting a new [SUGGEST_FIX] marker supersedes the prior active one.
|
||||
"""
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import (
|
||||
Text, DateTime, ForeignKey, String, Integer, CheckConstraint,
|
||||
)
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.account import Account
|
||||
from app.models.script_template import ScriptTemplate
|
||||
|
||||
|
||||
class SessionSuggestedFix(Base):
|
||||
"""One AI-proposed fix for a FlowPilot session."""
|
||||
__tablename__ = "session_suggested_fixes"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"confidence_pct BETWEEN 0 AND 100",
|
||||
name="ck_session_suggested_fixes_confidence_pct",
|
||||
),
|
||||
CheckConstraint(
|
||||
"user_decision IS NULL OR user_decision IN ("
|
||||
"'one_off', 'draft_template', 'build_template', 'dismissed')",
|
||||
name="ck_session_suggested_fixes_user_decision",
|
||||
),
|
||||
CheckConstraint(
|
||||
"status IN ('proposed', 'applied_success', 'applied_failed', "
|
||||
"'applied_partial', 'dismissed')",
|
||||
name="ck_session_suggested_fixes_status",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
session_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("ai_sessions.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
)
|
||||
account_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("accounts.id"),
|
||||
nullable=False,
|
||||
)
|
||||
title: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
description: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
confidence_pct: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
script_template_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("script_templates.id"),
|
||||
nullable=True,
|
||||
)
|
||||
# Populated only when there's no matching template and the AI has
|
||||
# drafted a session-specific script.
|
||||
ai_drafted_script: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
ai_drafted_parameters: Mapped[dict[str, Any] | None] = mapped_column(
|
||||
JSONB, nullable=True
|
||||
)
|
||||
user_decision: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
||||
# Outcome dimension — did the fix work? Orthogonal to user_decision.
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(20), nullable=False, default="proposed"
|
||||
)
|
||||
applied_at: Mapped[datetime | None] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True
|
||||
)
|
||||
verified_at: Mapped[datetime | None] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True
|
||||
)
|
||||
partial_notes: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
failure_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
ai_outcome_proposal: Mapped[dict[str, Any] | None] = mapped_column(
|
||||
JSONB, nullable=True
|
||||
)
|
||||
# Set when a newer suggested fix supersedes this one.
|
||||
superseded_at: Mapped[datetime | None] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
|
||||
session: Mapped["AISession"] = relationship("AISession", foreign_keys=[session_id])
|
||||
account: Mapped["Account"] = relationship("Account", foreign_keys=[account_id])
|
||||
script_template: Mapped["ScriptTemplate | None"] = relationship(
|
||||
"ScriptTemplate", foreign_keys=[script_template_id]
|
||||
)
|
||||
@@ -20,6 +20,7 @@ from .psa_connection import (
|
||||
PSATicketSearchResult, PSATicketStatusItem,
|
||||
PsaPostRequest, PsaPostResponse, PsaPreviewResponse, PsaPostLogResponse,
|
||||
PsaMemberMappingResponse, PsaMemberMappingSaveRequest, PsaMemberResponse, AutoMatchResult,
|
||||
PSABoardResponse,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
@@ -50,4 +51,5 @@ __all__ = [
|
||||
"PSATicketSearchResult", "PSATicketStatusItem",
|
||||
"PsaPostRequest", "PsaPostResponse", "PsaPreviewResponse", "PsaPostLogResponse",
|
||||
"PsaMemberMappingResponse", "PsaMemberMappingSaveRequest", "PsaMemberResponse", "AutoMatchResult",
|
||||
"PSABoardResponse",
|
||||
]
|
||||
|
||||
@@ -28,6 +28,111 @@ class ActivityEntry(BaseModel):
|
||||
from_attributes = True
|
||||
|
||||
|
||||
# --- Admin Accounts & People Search ---
|
||||
|
||||
class AdminUserListItem(BaseModel):
|
||||
id: UUID
|
||||
email: EmailStr
|
||||
name: str
|
||||
role: str
|
||||
is_super_admin: bool = False
|
||||
is_active: bool = True
|
||||
account_id: Optional[UUID] = None
|
||||
account_role: Optional[str] = None
|
||||
account_name: Optional[str] = None
|
||||
account_display_code: Optional[str] = None
|
||||
created_at: datetime
|
||||
last_login: Optional[datetime] = None
|
||||
deleted_at: Optional[datetime] = None
|
||||
|
||||
|
||||
class AdminUserListResponse(BaseModel):
|
||||
items: list[AdminUserListItem]
|
||||
total: int
|
||||
page: int
|
||||
per_page: int
|
||||
|
||||
|
||||
class AdminAccountMember(BaseModel):
|
||||
id: UUID
|
||||
email: EmailStr
|
||||
name: str
|
||||
role: str
|
||||
is_super_admin: bool = False
|
||||
is_active: bool = True
|
||||
account_role: Optional[str] = None
|
||||
created_at: datetime
|
||||
last_login: Optional[datetime] = None
|
||||
deleted_at: Optional[datetime] = None
|
||||
|
||||
|
||||
class AdminAccountOwnerSummary(BaseModel):
|
||||
id: UUID
|
||||
name: str
|
||||
email: EmailStr
|
||||
|
||||
|
||||
class AdminAccountSubscriptionSummary(BaseModel):
|
||||
id: UUID
|
||||
plan: str
|
||||
status: str
|
||||
billing_interval: Optional[str] = None
|
||||
current_period_end: Optional[datetime] = None
|
||||
cancel_at_period_end: bool = False
|
||||
|
||||
|
||||
class AdminAccountUsageSummary(BaseModel):
|
||||
tree_count: int = 0
|
||||
session_count_this_month: int = 0
|
||||
|
||||
|
||||
class AdminAccountInviteSummary(BaseModel):
|
||||
id: UUID
|
||||
email: EmailStr
|
||||
role: str
|
||||
expires_at: Optional[datetime] = None
|
||||
created_at: datetime
|
||||
used_at: Optional[datetime] = None
|
||||
|
||||
|
||||
class AdminAccountListItem(BaseModel):
|
||||
id: UUID
|
||||
name: str
|
||||
display_code: str
|
||||
created_at: datetime
|
||||
owner_id: Optional[UUID] = None
|
||||
owner: Optional[AdminAccountOwnerSummary] = None
|
||||
subscription: Optional[AdminAccountSubscriptionSummary] = None
|
||||
usage: AdminAccountUsageSummary = Field(default_factory=AdminAccountUsageSummary)
|
||||
member_count: int = 0
|
||||
active_member_count: int = 0
|
||||
pending_invite_count: int = 0
|
||||
sso_enabled: bool = False
|
||||
branding_company_name: Optional[str] = None
|
||||
members: list[AdminAccountMember] = Field(default_factory=list)
|
||||
|
||||
|
||||
class AdminAccountListResponse(BaseModel):
|
||||
items: list[AdminAccountListItem]
|
||||
total: int
|
||||
page: int
|
||||
per_page: int
|
||||
|
||||
|
||||
class AdminAccountDetailResponse(AdminAccountListItem):
|
||||
invites: list[AdminAccountInviteSummary] = Field(default_factory=list)
|
||||
|
||||
|
||||
class AdminAccountCreate(BaseModel):
|
||||
name: str = Field(..., min_length=1, max_length=255)
|
||||
plan: Literal["free", "pro", "team"] = "free"
|
||||
owner_email: Optional[EmailStr] = Field(None, description="Email of an existing user to set as owner")
|
||||
|
||||
|
||||
class AdminAccountUpdate(BaseModel):
|
||||
name: str = Field(..., min_length=1, max_length=255)
|
||||
|
||||
|
||||
# --- Audit Logs ---
|
||||
|
||||
class AuditLogEntry(BaseModel):
|
||||
@@ -215,7 +320,7 @@ class AdminUserCreate(BaseModel):
|
||||
name: str = Field(..., min_length=1, max_length=255)
|
||||
account_mode: Literal["existing", "personal"]
|
||||
account_display_code: Optional[str] = Field(None, description="Required when account_mode='existing'")
|
||||
account_role: Optional[Literal["engineer", "viewer"]] = Field(None, description="Required when account_mode='existing'")
|
||||
account_role: Optional[Literal["owner", "admin", "engineer", "viewer"]] = Field(None, description="Required when account_mode='existing'")
|
||||
send_email: bool = True
|
||||
|
||||
|
||||
|
||||
37
backend/app/schemas/device_type.py
Normal file
37
backend/app/schemas/device_type.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""Pydantic schemas for device types."""
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class DeviceTypeCreate(BaseModel):
|
||||
slug: str = Field(min_length=1, max_length=50, pattern=r"^[a-z0-9\-]+$")
|
||||
label: str = Field(min_length=1, max_length=100)
|
||||
category: str = Field(
|
||||
min_length=1, max_length=50,
|
||||
pattern=r"^(network|compute|storage|cloud|endpoint|infrastructure|security)$",
|
||||
)
|
||||
sort_order: int = Field(default=0, ge=0)
|
||||
|
||||
|
||||
class DeviceTypeUpdate(BaseModel):
|
||||
label: str | None = Field(default=None, min_length=1, max_length=100)
|
||||
category: str | None = Field(
|
||||
default=None, min_length=1, max_length=50,
|
||||
pattern=r"^(network|compute|storage|cloud|endpoint|infrastructure|security)$",
|
||||
)
|
||||
sort_order: int | None = Field(default=None, ge=0)
|
||||
|
||||
|
||||
class DeviceTypeResponse(BaseModel):
|
||||
id: UUID
|
||||
slug: str
|
||||
label: str
|
||||
category: str
|
||||
is_system: bool
|
||||
account_id: UUID
|
||||
sort_order: int
|
||||
created_at: datetime
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
68
backend/app/schemas/draft_template.py
Normal file
68
backend/app/schemas/draft_template.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""Pydantic schemas for FlowPilot Phase 6 draft templates.
|
||||
|
||||
A draft is the engineer's "Run now, templatize after resolve" path output:
|
||||
the script ran for the ticket, and the AI proposed a parameterization.
|
||||
Post-resolve, the engineer accepts (promotes to a real template) or rejects.
|
||||
|
||||
See FLOWPILOT-MIGRATION.md Section 5.3.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any, Literal
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
DraftStatus = Literal["pending", "accepted", "rejected"]
|
||||
|
||||
|
||||
class DraftTemplateResponse(BaseModel):
|
||||
id: UUID
|
||||
account_id: UUID
|
||||
source_session_id: UUID
|
||||
source_user_id: UUID
|
||||
script_body: str
|
||||
proposed_parameters: dict[str, Any]
|
||||
proposed_name: str | None
|
||||
proposed_category_id: UUID | None
|
||||
status: DraftStatus
|
||||
resolved_at: datetime | None
|
||||
promoted_template_id: UUID | None
|
||||
created_at: datetime
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class DraftTemplateListResponse(BaseModel):
|
||||
drafts: list[DraftTemplateResponse]
|
||||
|
||||
|
||||
class DraftTemplateAcceptRequest(BaseModel):
|
||||
"""Engineer's confirmation that this draft should become a real template.
|
||||
|
||||
Engineer may override the AI's proposed name / category and edit the
|
||||
parameter schema before promotion. Body and parameters_schema are
|
||||
persisted to the new `script_templates` row.
|
||||
"""
|
||||
name: str = Field(..., min_length=1, max_length=200)
|
||||
category_id: UUID
|
||||
description: str | None = Field(None, max_length=2000)
|
||||
# Final parameter schema in the Script Generator's standard shape.
|
||||
# See ScriptTemplate.parameters_schema for the contract.
|
||||
parameters_schema: dict[str, Any]
|
||||
# Optional last-minute edits to the script body. Defaults to the draft's
|
||||
# `script_body` (which TemplateExtractionService produced as the templated
|
||||
# form with `{{ key }}` placeholders).
|
||||
edited_body: str | None = Field(None, min_length=1, max_length=50_000)
|
||||
|
||||
|
||||
class DraftTemplateAcceptResponse(BaseModel):
|
||||
draft_id: UUID
|
||||
promoted_template_id: UUID
|
||||
template_slug: str
|
||||
|
||||
|
||||
class DraftTemplateRejectResponse(BaseModel):
|
||||
draft_id: UUID
|
||||
status: Literal["rejected"]
|
||||
@@ -124,3 +124,26 @@ class FlowPilotDashboard(BaseModel):
|
||||
confidence_breakdown: ConfidenceBreakdown
|
||||
knowledge_coverage: KnowledgeCoverage
|
||||
psa_metrics: PsaMetrics | None = None
|
||||
|
||||
|
||||
class EscalationMetrics(BaseModel):
|
||||
"""In-product time-to-first-action metric for the Escalation Mode wedge.
|
||||
|
||||
NOTE: this is the *in-product* metric (post-claim time-to-first-action). The
|
||||
"minutes recovered" sales claim requires a manual baseline measurement of the
|
||||
pre-Escalation-Mode verbal-handoff time. See
|
||||
docs/plans/2026-04-27-escalation-mode-wedge-design.md for the two-metric
|
||||
framing — do not roll this number alone into "minutes recovered."
|
||||
"""
|
||||
|
||||
period: str
|
||||
n_handoffs_claimed: int
|
||||
n_handoffs_with_action: int
|
||||
avg_seconds_to_first_action: float | None = None
|
||||
median_seconds_to_first_action: float | None = None
|
||||
p95_seconds_to_first_action: float | None = None
|
||||
metric_definition: str = (
|
||||
"elapsed_seconds(first ai_session_step in session where "
|
||||
"created_at > SessionHandoff.claimed_at) — measures post-claim activity "
|
||||
"lag, NOT verbal-handoff savings. Pair with manual baseline."
|
||||
)
|
||||
|
||||
145
backend/app/schemas/network_diagram.py
Normal file
145
backend/app/schemas/network_diagram.py
Normal file
@@ -0,0 +1,145 @@
|
||||
"""Pydantic schemas for network diagrams."""
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class Position(BaseModel):
|
||||
x: float
|
||||
y: float
|
||||
|
||||
|
||||
class DeviceProperties(BaseModel):
|
||||
hostname: str | None = None
|
||||
ip: str | None = None
|
||||
subnet: str | None = None
|
||||
vendor: str | None = None
|
||||
model: str | None = None
|
||||
role: str | None = None
|
||||
vlan: str | None = None
|
||||
notes: str | None = None
|
||||
status: str = Field(default="unknown", pattern=r"^(unknown|online|offline|degraded)$")
|
||||
|
||||
|
||||
class NodeStyle(BaseModel):
|
||||
width: float | None = None
|
||||
height: float | None = None
|
||||
|
||||
|
||||
class DiagramNode(BaseModel):
|
||||
id: str
|
||||
type: str
|
||||
label: str
|
||||
position: Position
|
||||
properties: DeviceProperties = Field(default_factory=DeviceProperties)
|
||||
nodeType: str | None = None
|
||||
style: NodeStyle | None = None
|
||||
parentId: str | None = None
|
||||
|
||||
|
||||
class DiagramEdge(BaseModel):
|
||||
id: str
|
||||
source: str
|
||||
target: str
|
||||
label: str | None = None
|
||||
connectionType: str = "ethernet"
|
||||
speed: str | None = None
|
||||
notes: str | None = None
|
||||
routing: str | None = None
|
||||
|
||||
|
||||
class NetworkDiagramCreate(BaseModel):
|
||||
name: str = Field(min_length=1, max_length=255)
|
||||
client_name: str | None = None
|
||||
asset_name: str | None = None
|
||||
description: str | None = None
|
||||
nodes: list[DiagramNode] = Field(default_factory=list)
|
||||
edges: list[DiagramEdge] = Field(default_factory=list)
|
||||
|
||||
|
||||
class NetworkDiagramUpdate(BaseModel):
|
||||
name: str | None = Field(default=None, min_length=1, max_length=255)
|
||||
client_name: str | None = None
|
||||
asset_name: str | None = None
|
||||
description: str | None = None
|
||||
nodes: list[DiagramNode] | None = None
|
||||
edges: list[DiagramEdge] | None = None
|
||||
|
||||
|
||||
class NetworkDiagramResponse(BaseModel):
|
||||
id: UUID
|
||||
account_id: UUID
|
||||
name: str
|
||||
client_name: str | None = None
|
||||
asset_name: str | None = None
|
||||
description: str | None = None
|
||||
nodes: list[DiagramNode] = Field(default_factory=list)
|
||||
edges: list[DiagramEdge] = Field(default_factory=list)
|
||||
thumbnail_url: str | None = None
|
||||
is_archived: bool = False
|
||||
created_by: UUID | None = None
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class NetworkDiagramListItem(BaseModel):
|
||||
id: UUID
|
||||
name: str
|
||||
client_name: str | None = None
|
||||
description: str | None = None
|
||||
node_count: int = 0
|
||||
category_counts: dict[str, int] = Field(default_factory=dict)
|
||||
thumbnail_url: str | None = None
|
||||
created_by: UUID | None = None
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class ExistingBounds(BaseModel):
|
||||
minX: float
|
||||
maxX: float
|
||||
minY: float
|
||||
maxY: float
|
||||
|
||||
|
||||
class AIGenerateRequest(BaseModel):
|
||||
description: str = Field(min_length=1, max_length=5000)
|
||||
client_name: str | None = None
|
||||
mode: str = Field(default="replace", pattern=r"^(replace|merge)$")
|
||||
existingBounds: ExistingBounds | None = None
|
||||
|
||||
|
||||
class AIGenerateResponse(BaseModel):
|
||||
nodes: list[DiagramNode]
|
||||
edges: list[DiagramEdge]
|
||||
suggestedName: str | None = None
|
||||
notes: str | None = None
|
||||
|
||||
|
||||
class DiagramImportRequest(BaseModel):
|
||||
schemaVersion: int = Field(ge=1, le=1)
|
||||
name: str = Field(min_length=1, max_length=255)
|
||||
client_name: str | None = None
|
||||
description: str | None = None
|
||||
nodes: list[DiagramNode] = Field(default_factory=list)
|
||||
edges: list[DiagramEdge] = Field(default_factory=list)
|
||||
|
||||
|
||||
class DiagramImportResponse(BaseModel):
|
||||
diagram: NetworkDiagramResponse
|
||||
warnings: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class DiagramExportResponse(BaseModel):
|
||||
schemaVersion: int = 1
|
||||
name: str
|
||||
client_name: str | None = None
|
||||
description: str | None = None
|
||||
nodes: list[DiagramNode]
|
||||
edges: list[DiagramEdge]
|
||||
exportedAt: str
|
||||
@@ -53,9 +53,13 @@ class PSATicketSearchResult(BaseModel):
|
||||
id: str
|
||||
summary: str
|
||||
company_name: str | None = None
|
||||
company_id: str | None = None
|
||||
board_name: str | None = None
|
||||
board_id: int | None = None
|
||||
status_name: str | None = None
|
||||
status_id: int | None = None
|
||||
priority_name: str | None = None
|
||||
priority_id: int | None = None
|
||||
closed: bool = False
|
||||
|
||||
|
||||
@@ -111,13 +115,13 @@ class PsaPostLogResponse(BaseModel):
|
||||
|
||||
|
||||
class PsaMemberMappingResponse(BaseModel):
|
||||
id: str
|
||||
id: str | None = None # None for users without a mapping
|
||||
user_id: str
|
||||
user_email: str
|
||||
user_name: str
|
||||
external_member_id: str
|
||||
external_member_name: str
|
||||
matched_by: str
|
||||
external_member_id: str | None = None
|
||||
external_member_name: str | None = None
|
||||
matched_by: str | None = None
|
||||
|
||||
|
||||
class PsaMemberMappingSaveRequest(BaseModel):
|
||||
@@ -136,3 +140,8 @@ class PsaMemberResponse(BaseModel):
|
||||
class AutoMatchResult(BaseModel):
|
||||
matched: list[PsaMemberMappingResponse]
|
||||
unmatched_users: int
|
||||
|
||||
|
||||
class PSABoardResponse(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
|
||||
65
backend/app/schemas/psa_tickets.py
Normal file
65
backend/app/schemas/psa_tickets.py
Normal file
@@ -0,0 +1,65 @@
|
||||
"""Normalized DTOs for ticket management endpoints."""
|
||||
from __future__ import annotations
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class PSAResourceSchema(BaseModel):
|
||||
member_id: int
|
||||
member_name: str
|
||||
member_identifier: str
|
||||
is_rf_user: bool = False
|
||||
|
||||
|
||||
class PSATicketCreatedSchema(BaseModel):
|
||||
id: int
|
||||
summary: str
|
||||
board_name: str
|
||||
status_name: str
|
||||
priority_name: str
|
||||
company_name: str
|
||||
resources: list[PSAResourceSchema] = []
|
||||
|
||||
|
||||
class PSATicketStatusUpdateSchema(BaseModel):
|
||||
ticket_id: int
|
||||
previous_status: str
|
||||
new_status: str
|
||||
new_status_id: int
|
||||
|
||||
|
||||
class TicketCreatePayloadSchema(BaseModel):
|
||||
summary: str
|
||||
company_id: int
|
||||
board_id: int
|
||||
status_id: int
|
||||
priority_id: int
|
||||
description: str | None = None
|
||||
assigned_member_id: int | None = None
|
||||
|
||||
|
||||
class TicketListResponseSchema(BaseModel):
|
||||
items: list = []
|
||||
total: int = 0
|
||||
page: int = 1
|
||||
page_size: int = 25
|
||||
|
||||
|
||||
class AiParseRequestSchema(BaseModel):
|
||||
prompt: str
|
||||
|
||||
|
||||
class AiParseResponseSchema(BaseModel):
|
||||
summary: str | None = None
|
||||
company_id: int | None = None
|
||||
board_id: int | None = None
|
||||
priority_id: int | None = None
|
||||
status_id: int | None = None
|
||||
assigned_member_id: int | None = None
|
||||
description: str | None = None
|
||||
missing_fields: list[str] = []
|
||||
warnings: list[str] = []
|
||||
|
||||
|
||||
class PSAPrioritySchema(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
@@ -1,18 +1,27 @@
|
||||
"""Pydantic schemas for the AI Script Builder."""
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from typing import Literal, Optional
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class ScriptBuilderCreateRequest(BaseModel):
|
||||
"""Request to start a new builder session."""
|
||||
"""Request to start (or get-or-create, for inline origin) a builder session.
|
||||
|
||||
When `origin='pilot_inline'`, `ai_session_id` is REQUIRED and must
|
||||
reference a pilot session owned by the current user. The endpoint's
|
||||
get-or-create semantics kick in: if a pilot_inline session already
|
||||
exists for (user_id, ai_session_id), that row is returned instead of
|
||||
creating a duplicate.
|
||||
"""
|
||||
language: str = Field(
|
||||
default="powershell",
|
||||
pattern=r"^(powershell|bash|python)$",
|
||||
description="Script language",
|
||||
)
|
||||
origin: Literal["standalone", "pilot_inline"] = "standalone"
|
||||
ai_session_id: UUID | None = None
|
||||
|
||||
|
||||
class ScriptBuilderMessageRequest(BaseModel):
|
||||
|
||||
81
backend/app/schemas/session_fact.py
Normal file
81
backend/app/schemas/session_fact.py
Normal file
@@ -0,0 +1,81 @@
|
||||
"""Pydantic schemas for the FlowPilot "What we know" session facts.
|
||||
|
||||
See FLOWPILOT-MIGRATION.md Section 4.2 for the data model rationale.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Literal
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
# AI-emittable source types are a subset (`user_note` is engineer-only).
|
||||
AIEmittableSourceType = Literal["question", "diagnostic_check", "ai_synthesis"]
|
||||
SourceType = Literal["question", "diagnostic_check", "user_note", "ai_synthesis"]
|
||||
|
||||
|
||||
class SessionFactResponse(BaseModel):
|
||||
"""A single fact card in the What-we-know panel."""
|
||||
id: UUID
|
||||
session_id: UUID
|
||||
text: str
|
||||
source_type: SourceType
|
||||
source_ref: UUID | None
|
||||
source_summary: str | None
|
||||
created_by: UUID
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
# `editable` is computed server-side so the client doesn't have to
|
||||
# re-encode the editability rule. It mirrors the PATCH endpoint's
|
||||
# 403 policy: only user_note and ai_synthesis facts are editable.
|
||||
editable: bool
|
||||
|
||||
model_config = {"from_attributes": False}
|
||||
|
||||
|
||||
class SessionFactListResponse(BaseModel):
|
||||
facts: list[SessionFactResponse]
|
||||
|
||||
|
||||
class SessionFactCreateRequest(BaseModel):
|
||||
"""Engineer-created manual fact (the "+ Add a note" affordance).
|
||||
|
||||
The endpoint hard-codes source_type="user_note" — manual creation cannot
|
||||
spoof a question/check origin. Source-type-bound creation goes through
|
||||
`/promote` instead.
|
||||
"""
|
||||
text: str = Field(..., min_length=1, max_length=2000)
|
||||
summary: str | None = Field(None, max_length=200)
|
||||
|
||||
|
||||
class SessionFactUpdateRequest(BaseModel):
|
||||
"""Edit an existing fact's text or summary.
|
||||
|
||||
The endpoint returns 403 when the fact's source_type is `question` or
|
||||
`diagnostic_check` — those facts must be edited at the source item.
|
||||
"""
|
||||
text: str | None = Field(None, min_length=1, max_length=2000)
|
||||
summary: str | None = Field(None, max_length=200)
|
||||
|
||||
|
||||
class SessionFactPromoteRequest(BaseModel):
|
||||
"""Promote a question answer / check result into a fact.
|
||||
|
||||
Two modes:
|
||||
- **Direct**: caller provides `proposed_text` (and optionally `proposed_summary`).
|
||||
The fact is persisted as-is. Used by the AI [PROMOTE] marker path and by the
|
||||
engineer's "edit then save" affordance.
|
||||
- **Synthesize**: caller provides `raw_input` (the engineer's typed answer or
|
||||
the check output) and the server drafts `text`/`summary` via the
|
||||
FactSynthesisService. The draft is persisted immediately for now —
|
||||
the supervisor-staging review is a future enhancement (out of scope per
|
||||
Section 12).
|
||||
|
||||
Exactly one of `proposed_text` or `raw_input` must be set.
|
||||
"""
|
||||
source_type: AIEmittableSourceType
|
||||
source_ref: UUID | None = None
|
||||
proposed_text: str | None = Field(None, min_length=1, max_length=2000)
|
||||
proposed_summary: str | None = Field(None, max_length=200)
|
||||
raw_input: str | None = Field(None, min_length=1, max_length=10_000)
|
||||
@@ -10,6 +10,11 @@ class HandoffCreateRequest(BaseModel):
|
||||
intent: str = Field(..., pattern="^(park|escalate)$")
|
||||
engineer_notes: str | None = None
|
||||
priority: str = Field("normal", pattern="^(normal|elevated)$")
|
||||
# Optional escalation target — if set, only this user is the named
|
||||
# recipient. Notification dispatch fans out to all engineer/admin/owner
|
||||
# users in the account either way; this just records the original
|
||||
# engineer's preferred recipient on the session for audit/UX.
|
||||
target_user_id: UUID | None = None
|
||||
|
||||
|
||||
class HandoffResponse(BaseModel):
|
||||
|
||||
166
backend/app/schemas/session_suggested_fix.py
Normal file
166
backend/app/schemas/session_suggested_fix.py
Normal file
@@ -0,0 +1,166 @@
|
||||
"""Pydantic schemas for session suggested fixes (Phase 3).
|
||||
|
||||
See FLOWPILOT-MIGRATION.md Section 5.2.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any, Literal
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
UserDecision = Literal["one_off", "draft_template", "build_template", "dismissed"]
|
||||
|
||||
# "dismissed" here is the outcome dimension — orthogonal to UserDecision's
|
||||
# "dismissed" (script-path choice), though the migration backfill aligns
|
||||
# them for pre-existing rows.
|
||||
FixStatus = Literal[
|
||||
"proposed",
|
||||
"applied_success",
|
||||
"applied_failed",
|
||||
"applied_partial",
|
||||
"dismissed",
|
||||
]
|
||||
|
||||
|
||||
class SessionSuggestedFixResponse(BaseModel):
|
||||
id: UUID
|
||||
session_id: UUID
|
||||
title: str
|
||||
description: str
|
||||
confidence_pct: int
|
||||
script_template_id: UUID | None
|
||||
ai_drafted_script: str | None
|
||||
ai_drafted_parameters: dict[str, Any] | None
|
||||
user_decision: UserDecision | None
|
||||
superseded_at: datetime | None
|
||||
created_at: datetime
|
||||
status: FixStatus
|
||||
applied_at: datetime | None
|
||||
verified_at: datetime | None
|
||||
partial_notes: str | None
|
||||
failure_reason: str | None
|
||||
ai_outcome_proposal: dict[str, Any] | None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class SessionSuggestedFixDecisionRequest(BaseModel):
|
||||
"""Engineer's path choice on a suggested fix.
|
||||
|
||||
Server-side side effects per Section 5.2:
|
||||
- one_off: record decision, return the rendered (AI-drafted or
|
||||
engineer-edited) script. No persistent library artifact created.
|
||||
- draft_template: same as one_off, plus TemplateExtractionService
|
||||
proposes a parameterization and a draft_templates row is created.
|
||||
- build_template: return a redirect payload pointing at the Script
|
||||
Builder page, pre-loaded with the drafted script body.
|
||||
- dismissed: mark the fix superseded.
|
||||
|
||||
For one_off / draft_template, the engineer may have edited the drafted
|
||||
script or its parameters in the dialog. The final versions are sent
|
||||
back here so we persist what will actually run.
|
||||
"""
|
||||
decision: UserDecision
|
||||
# Present for one_off / draft_template — the engineer's final version of
|
||||
# the drafted script after any inline edits. Omit to use the fix's
|
||||
# `ai_drafted_script` verbatim.
|
||||
edited_script: str | None = Field(None, min_length=1, max_length=50_000)
|
||||
# Parameter values used when rendering (informational, stored on the
|
||||
# draft_template row so a reviewer can see what the first run used).
|
||||
parameters_used: dict[str, Any] | None = None
|
||||
|
||||
|
||||
class SessionSuggestedFixDecisionResponse(BaseModel):
|
||||
"""Returned after recording a decision."""
|
||||
id: UUID
|
||||
user_decision: UserDecision
|
||||
# Populated for one_off / draft_template — the script to display/run.
|
||||
rendered_script: str | None = None
|
||||
# Populated for draft_template — the ID of the draft_templates row so
|
||||
# the post-resolve TemplatizePrompt can fetch it in Phase 6.
|
||||
draft_template_id: UUID | None = None
|
||||
# Populated for build_template — where to send the engineer next.
|
||||
redirect_path: str | None = Field(
|
||||
None,
|
||||
description="Where to send the engineer next (e.g. /scripts/builder?... for build_template)",
|
||||
)
|
||||
|
||||
|
||||
# Subset of FixStatus that the engineer can set via the outcome endpoint —
|
||||
# `proposed` is excluded because you can't un-decide a fix back to "proposed".
|
||||
FixOutcome = Literal[
|
||||
"applied_success", "applied_failed", "applied_partial", "dismissed"
|
||||
]
|
||||
|
||||
|
||||
class SessionSuggestedFixOutcomeRequest(BaseModel):
|
||||
"""Engineer-reported outcome of applying a suggested fix.
|
||||
|
||||
Writes to session_suggested_fixes.status and companion columns. This is
|
||||
orthogonal to `user_decision` (which records which script-path the
|
||||
engineer took); outcome captures whether the fix actually worked.
|
||||
|
||||
Allowed transitions:
|
||||
- from `proposed` or `applied_partial`: any outcome is valid
|
||||
(partial is parked, not terminal — the engineer may update notes,
|
||||
abandon via dismiss, or advance to success/failed)
|
||||
- from any terminal outcome (`applied_success`, `applied_failed`,
|
||||
`dismissed`): server returns 409
|
||||
"""
|
||||
outcome: FixOutcome
|
||||
# Required for applied_partial, optional for applied_failed, ignored otherwise.
|
||||
notes: str | None = Field(None, max_length=500)
|
||||
|
||||
|
||||
class SessionSuggestedFixScriptRequest(BaseModel):
|
||||
"""Engineer-submitted drafted script for a suggested fix.
|
||||
|
||||
Called when the inline Script Builder tab's Submit action fires. The
|
||||
fix must be non-terminal (still proposed/applied_partial). Setting
|
||||
the script does NOT stamp applied_at — a draft is not an application.
|
||||
"""
|
||||
ai_drafted_script: str = Field(..., min_length=1, max_length=50_000)
|
||||
ai_drafted_parameters: dict[str, Any] | None = None
|
||||
|
||||
|
||||
# ── Resolution note preview ────────────────────────────────────────────────
|
||||
|
||||
class ResolutionNotePreviewResponse(BaseModel):
|
||||
markdown: str
|
||||
target_ticket_ref: str | None
|
||||
state_version: int
|
||||
from_cache: bool
|
||||
|
||||
|
||||
# ── Phase 4: Resolve + Escalate post ───────────────────────────────────────
|
||||
|
||||
class ResolutionNotePostRequest(BaseModel):
|
||||
"""Engineer-edited resolution markdown. Server posts to PSA + marks resolved."""
|
||||
markdown: str = Field(..., min_length=1, max_length=20_000)
|
||||
# Optional override for resolution summary shown on the session listing;
|
||||
# defaults to the first line of the markdown if omitted.
|
||||
resolution_summary: str | None = Field(None, max_length=500)
|
||||
|
||||
|
||||
class EscalationPackagePostRequest(BaseModel):
|
||||
markdown: str = Field(..., min_length=1, max_length=20_000)
|
||||
# Free-text reason shown in session listings and escalation queue.
|
||||
escalation_reason: str | None = Field(None, max_length=500)
|
||||
|
||||
|
||||
class ResolutionPostResponse(BaseModel):
|
||||
"""Response shape for both Resolve/Escalate POST endpoints."""
|
||||
# "resolved" / "escalated" / "resolved_local" / "escalated_local"
|
||||
# The _local variants indicate the session has no linked PSA ticket —
|
||||
# markdown is stored, session state is updated, nothing was posted externally.
|
||||
outcome: str
|
||||
session_status: str
|
||||
external_id: str | None = None
|
||||
posted_at: datetime | None = None
|
||||
# Populated when a status transition was attempted and verified. None
|
||||
# when no target status is configured in account_settings.preferences.
|
||||
verified_status_id: int | None = None
|
||||
verified_status_name: str | None = None
|
||||
status_transition_skipped_reason: str | None = None
|
||||
@@ -68,4 +68,6 @@ class RoleUpdate(BaseModel):
|
||||
|
||||
|
||||
class AccountRoleUpdate(BaseModel):
|
||||
account_role: str = Field(..., pattern="^(engineer|viewer)$")
|
||||
# Ownership changes must go through the explicit transfer-ownership flow so
|
||||
# account.owner_id stays consistent with user.account_role.
|
||||
account_role: str = Field(..., pattern="^(admin|engineer|viewer)$")
|
||||
|
||||
@@ -10,10 +10,32 @@ Uses Anthropic prompt caching to reduce cost on multi-turn conversations:
|
||||
|
||||
Optionally connects to Microsoft Learn via Anthropic's MCP connector
|
||||
for real-time documentation lookups (controlled by ENABLE_MCP_MICROSOFT_LEARN).
|
||||
|
||||
## Architectural note — this module is the one MCP/beta chat caller
|
||||
|
||||
`chat_call_cached` below is the ONLY caller in the codebase that uses
|
||||
Anthropic's `client.beta.messages.create` endpoint, MCP servers, multimodal
|
||||
user messages, and the retry-without-MCP fallback. It is deliberately NOT
|
||||
routed through `AnthropicProvider` — MCP/beta/images are features of exactly
|
||||
one optional Anthropic beta endpoint and do not belong in a provider-agnostic
|
||||
abstraction that also serves Gemini.
|
||||
|
||||
If a new caller needs the same (MCP, beta, images, history caching) bundle,
|
||||
call `chat_call_cached` directly rather than pushing those concerns into
|
||||
`AnthropicProvider`. Cached-system-block plumbing is shared with the provider
|
||||
via `_normalize_system_for_anthropic` / `build_anthropic_chat_messages` /
|
||||
`_log_anthropic_cache_usage` in `app.core.ai_provider` — cache primitives are
|
||||
reusable, but the MCP/beta orchestration stays here.
|
||||
"""
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from app.core.ai_provider import (
|
||||
_get_anthropic_client,
|
||||
_log_anthropic_cache_usage,
|
||||
_normalize_system_for_anthropic,
|
||||
build_anthropic_chat_messages,
|
||||
)
|
||||
from app.core.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -40,29 +62,31 @@ Every response you write MUST follow this exact structure:
|
||||
1. **1-3 sentences of analysis** (what the symptoms tell you)
|
||||
2. **[QUESTIONS] marker** with 1-3 questions for the engineer (if you need info)
|
||||
3. **[ACTIONS] marker** with 1-4 diagnostic commands to run (if applicable)
|
||||
4. **[PROMOTE] marker(s)** when the engineer's most recent message confirmed a fact \
|
||||
worth recording (optional; see "Promoting facts" below)
|
||||
|
||||
You MUST include at least one marker ([QUESTIONS] or [ACTIONS]) in every response. \
|
||||
A response with only prose and no markers is INVALID and will break the UI.
|
||||
A response with only prose and no markers is INVALID and will break the UI. \
|
||||
[PROMOTE] is optional and IN ADDITION to the required markers, never a replacement.
|
||||
|
||||
### Complete example of a correct first response:
|
||||
### Format-only schema (DO NOT reuse the literal text below)
|
||||
|
||||
User: "Outlook disconnects every 10-15 min, Teams drops too, only this one user, WiFi"
|
||||
The structure to follow is shown below using PLACEHOLDERS. The placeholders \
|
||||
are not real questions or commands — they describe the SHAPE of valid output. \
|
||||
Your real response must contain analysis and markers tailored to the actual \
|
||||
ticket the engineer just sent. Reusing any placeholder text (or text from a \
|
||||
prior unrelated example you've seen) verbatim is a bug.
|
||||
|
||||
Your response:
|
||||
|
||||
Both apps dropping on the same 10-15 min cycle on WiFi points to a network-layer \
|
||||
timeout — likely DHCP lease renewal, AP roaming, or NIC power management. Single-user \
|
||||
scope narrows it to this endpoint.
|
||||
Analysis prose: 1-3 sentences specific to the engineer's symptoms.
|
||||
|
||||
[QUESTIONS]
|
||||
[{"text": "Is this user on a laptop or desktop?", "context": "Laptops have power management and docking transitions that cause WiFi drops"},
|
||||
{"text": "Are they on corporate WiFi or working from home?", "context": "Corporate WiFi with multiple APs can cause roaming disconnects"}]
|
||||
[{"text": "<one short, specific question about THIS ticket>", "context": "<one-sentence justification, optional>"},
|
||||
{"text": "<another specific question>", "context": "<...>"}]
|
||||
[/QUESTIONS]
|
||||
|
||||
[ACTIONS]
|
||||
[{"label": "Check DHCP lease time", "command": "ipconfig /all | Select-String -Pattern 'DHCP|IPv4|Lease|Gateway'", "description": "Short lease times (under 1 hour) cause brief drops at renewal"},
|
||||
{"label": "Check NIC power management", "command": "Get-NetAdapterPowerManagement | Select Name, AllowComputerToTurnOffDevice", "description": "If True, Windows is likely killing the adapter during idle periods"},
|
||||
{"label": "Check WiFi signal and AP", "command": "netsh wlan show interfaces", "description": "Shows current BSSID, signal strength, and whether they are bouncing between APs"}]
|
||||
[{"label": "<short imperative label for THIS ticket>", "command": "<exact PowerShell or shell command, omit for GUI-only steps>", "description": "<one sentence explaining what the output reveals>"},
|
||||
{"label": "<...>", "command": "<...>", "description": "<...>"}]
|
||||
[/ACTIONS]
|
||||
|
||||
### Rules
|
||||
@@ -90,6 +114,128 @@ information is no longer needed to resolve the issue. Default to keeping them.
|
||||
**Both markers are stripped from display** — the engineer sees them as interactive UI cards, \
|
||||
not raw JSON. Put analysis BEFORE markers. Markers go at the END of your response.
|
||||
|
||||
## Promoting facts to "What we know"
|
||||
|
||||
The engineer has a "What we know" panel that holds confirmed facts about this \
|
||||
session. Each confirmed fact stays visible to the engineer for the rest of the \
|
||||
session and feeds the resolution note posted to the customer ticket. Surface \
|
||||
facts there using a `[PROMOTE]` marker.
|
||||
|
||||
**When to emit [PROMOTE]:**
|
||||
- The engineer just answered a [QUESTIONS] item with a substantive answer that \
|
||||
rules something in or out
|
||||
- The engineer just shared diagnostic-check output that confirmed a finding
|
||||
- You synthesized a new conclusion from two or more prior facts
|
||||
|
||||
**When NOT to emit [PROMOTE]:**
|
||||
- The engineer's answer was "unknown", "I don't know", or a clarifying question \
|
||||
back to you
|
||||
- The diagnostic output was empty, errored, or inconclusive
|
||||
- You're re-stating something already in What we know
|
||||
- The "fact" is your own hypothesis, not something the engineer confirmed
|
||||
|
||||
**[PROMOTE] marker format:**
|
||||
Each fact is its own block. You may emit multiple blocks per response.
|
||||
|
||||
[PROMOTE]
|
||||
{"source_type": "question", "source_ref": "<task_lane_item_id>", "text": "<one short past-tense sentence stating what is now confirmed FROM THIS TICKET>", "summary": "<3-7 word provenance label specific to what the fact rules in/out>"}
|
||||
[/PROMOTE]
|
||||
|
||||
- `source_type` is one of: `"question"` (fact derived from a question's answer), \
|
||||
`"diagnostic_check"` (fact derived from a check's output), or `"ai_synthesis"` \
|
||||
(you combined prior facts).
|
||||
- `source_ref` is the `id` field of the originating task-lane item — the \
|
||||
[QUESTIONS] and [ACTIONS] payloads you receive in conversation context include \
|
||||
an `id` for each item. Copy that UUID verbatim. For `ai_synthesis`, OMIT \
|
||||
`source_ref` (or set it to null).
|
||||
- `text` is a short past-tense sentence stating what's now confirmed. Use ONLY \
|
||||
information present in the engineer's CURRENT message — never invent specifics, \
|
||||
never reuse phrasing from past tickets or example payloads.
|
||||
- `summary` names the diagnostic value (what the fact rules in or out), 3-7 \
|
||||
words, no period.
|
||||
|
||||
**Strict rule:** [PROMOTE] is for confirmed facts only. If you're not certain \
|
||||
the engineer's message confirms the fact, do not emit a [PROMOTE]. Hallucinated \
|
||||
facts get posted to customer tickets and will erode trust in the system.
|
||||
|
||||
## Proposing a fix with [SUGGEST_FIX]
|
||||
|
||||
When you have a concrete proposed resolution path with reasonable confidence, \
|
||||
emit a `[SUGGEST_FIX]` marker. This populates the "Suggested fix" card the \
|
||||
engineer can act on (run a script, build a template, etc.). A new \
|
||||
[SUGGEST_FIX] supersedes any prior suggested fix on the session — emit a fresh \
|
||||
one whenever your top hypothesis changes meaningfully.
|
||||
|
||||
**When to emit [SUGGEST_FIX]:**
|
||||
- You have a concrete resolution path (not just "investigate further")
|
||||
- Confidence is at least ~50% — below that, keep diagnosing
|
||||
- Either a known Script Library template applies, OR you can draft a script \
|
||||
that resolves the issue end-to-end
|
||||
|
||||
**When NOT to emit [SUGGEST_FIX]:**
|
||||
- You're still narrowing causes and the fix depends on the next answer
|
||||
- The "fix" is just running another diagnostic — that goes in [ACTIONS]
|
||||
- Two paths are equally likely — fork or ask first, suggest later
|
||||
|
||||
**[SUGGEST_FIX] marker format (one block per response, last one wins).**
|
||||
Schema below — DO NOT copy these placeholders into your real response, fill \
|
||||
each field with content specific to the actual ticket:
|
||||
|
||||
[SUGGEST_FIX]
|
||||
{"title": "<short imperative summary of the fix, ≤200 chars>", "description": "<one short paragraph: root cause + how the fix resolves it>", "confidence": <integer 0-100>, "script_template_slug": "<slug-of-existing-template-or-omit>"}
|
||||
[/SUGGEST_FIX]
|
||||
|
||||
- `title`: short imperative summary, ≤ 200 chars
|
||||
- `description`: one short paragraph explaining the root cause and the fix
|
||||
- `confidence`: integer 0-100 (what you'd bet this resolves the ticket)
|
||||
- `script_template_slug`: slug of an existing Script Library template if one \
|
||||
applies; OMIT or set null otherwise
|
||||
- `ai_drafted_script`: full script body if no template matches (only when \
|
||||
`script_template_slug` is null/omitted)
|
||||
- `ai_drafted_parameters`: optional JSON object of suggested parameter values \
|
||||
for the drafted script
|
||||
|
||||
The marker is stripped from display — the engineer sees the suggested fix as \
|
||||
an interactive card with confidence badge, not raw JSON.
|
||||
|
||||
## Reporting fix outcome with [FIX_OUTCOME]
|
||||
|
||||
When the engineer clearly indicates in chat that a previously proposed fix
|
||||
worked, didn't work, or was partially applied, emit a [FIX_OUTCOME] marker
|
||||
on its own lines. This surfaces a "confirm outcome?" banner in the UI — it
|
||||
does NOT mark the fix resolved on its own; the engineer confirms via the UI.
|
||||
|
||||
**When to emit [FIX_OUTCOME]:**
|
||||
- The engineer states the user's problem is resolved after applying the fix
|
||||
(affirmative resolution language → outcome="success")
|
||||
- The engineer states the issue persists after applying the fix
|
||||
(→ outcome="failure")
|
||||
- The engineer describes applying only part of the fix
|
||||
(→ outcome="partial")
|
||||
|
||||
**When NOT to emit [FIX_OUTCOME]:**
|
||||
- The engineer is still verifying (user rebooting, testing, etc.)
|
||||
- The outcome is ambiguous or inferred rather than stated
|
||||
- No [SUGGEST_FIX] has been emitted this session
|
||||
|
||||
**[FIX_OUTCOME] marker format (one block per response, on its own lines).**
|
||||
Schema below — DO NOT copy these placeholders into your real response, fill \
|
||||
each field with content specific to the actual ticket:
|
||||
|
||||
[FIX_OUTCOME]
|
||||
{"fix_id": "<uuid-of-the-active-suggested-fix>",
|
||||
"outcome": "<success|failure|partial>",
|
||||
"reason": "<one-line-quote-or-paraphrase-of-what-the-engineer-said>"}
|
||||
[/FIX_OUTCOME]
|
||||
|
||||
- `fix_id`: the UUID of the active suggested fix (provided in session context)
|
||||
- `outcome`: one of `"success"`, `"failure"`, or `"partial"`
|
||||
- `reason`: one-line paraphrase of what the engineer said — derived from \
|
||||
their CURRENT message, not invented
|
||||
|
||||
The marker is stripped from display — the engineer sees a "confirm outcome?" \
|
||||
banner in the UI, not raw JSON.
|
||||
|
||||
## Using the Team's Flow Library
|
||||
Your team has built troubleshooting flows in ResolutionFlow. When relevant flows \
|
||||
appear in the context below, reference them by name so the engineer can launch them \
|
||||
@@ -117,7 +263,7 @@ forking, branching, or paths to the engineer. You just continue the conversation
|
||||
The fork marker is metadata that the system uses behind the scenes.
|
||||
|
||||
**You MUST fork when:**
|
||||
- Symptoms affect multiple applications or layers (e.g., Outlook AND Teams dropping)
|
||||
- Symptoms affect multiple applications or layers simultaneously
|
||||
- The problem could be endpoint-side OR infrastructure-side
|
||||
- Multiple well-known causes match the exact same symptom pattern
|
||||
|
||||
@@ -132,11 +278,6 @@ to those, not a replacement. Do NOT ask questions in prose — put them in [QUES
|
||||
|
||||
Structure: 1-3 sentences of analysis → [QUESTIONS] and/or [ACTIONS] → [FORK] at the very end.
|
||||
|
||||
Example flow:
|
||||
- Engineer: "Outlook disconnects every 15 min, Teams drops too, only one user"
|
||||
- You: "The 10-15 min pattern with both apps points to network layer."
|
||||
- Then: [QUESTIONS] marker, then [ACTIONS] marker, then [FORK] marker last.
|
||||
|
||||
The fork marker is stripped from display — the engineer never sees it. \
|
||||
The system creates branches silently. Based on the engineer's answer, you pick \
|
||||
the most relevant branch to investigate first.
|
||||
@@ -144,7 +285,7 @@ the most relevant branch to investigate first.
|
||||
To create a fork, append this marker AFTER your [QUESTIONS]/[ACTIONS] markers:
|
||||
|
||||
[FORK]
|
||||
{"fork_reason": "Brief reason", "options": [{"label": "Short name", "description": "One sentence"}, {"label": "Another", "description": "One sentence"}]}
|
||||
{"fork_reason": "<one short sentence: why these branches need independent investigation>", "options": [{"label": "<short hypothesis name for branch 1>", "description": "<one sentence: what this branch will check>"}, {"label": "<branch 2 name>", "description": "<...>"}]}
|
||||
[/FORK]
|
||||
|
||||
2-4 options. Never mention "fork", "branch", or "path" in your visible text.
|
||||
@@ -154,12 +295,49 @@ To create a fork, append this marker AFTER your [QUESTIONS]/[ACTIONS] markers:
|
||||
- If a question is clearly outside your domain, say so briefly and redirect.
|
||||
- Never fabricate error codes, KB article numbers, or CLI flags. If unsure, say so.
|
||||
|
||||
## SPIN-OFF TICKET CREATION
|
||||
|
||||
When you identify a second distinct issue that is clearly separate from the primary topic \
|
||||
of this session, suggest creating a spin-off ticket using the [ACTIONS] marker below. \
|
||||
Use this sparingly — only when the issue is genuinely independent, not for every tangential mention.
|
||||
Use `create_spin_off_ticket` as the command value for this action.
|
||||
|
||||
Format:
|
||||
[ACTIONS]
|
||||
[
|
||||
{
|
||||
"label": "Create ticket: <brief issue title>",
|
||||
"command": "<spin-off ticket action command>",
|
||||
"description": "<one sentence description of the separate issue>"
|
||||
}
|
||||
]
|
||||
[/ACTIONS]
|
||||
|
||||
## FINAL REMINDER — THIS OVERRIDES EVERYTHING ABOVE
|
||||
Every single response MUST contain [QUESTIONS] and/or [ACTIONS] markers with valid JSON. \
|
||||
No exceptions. Not even when forking. A response without at least one of these markers \
|
||||
will crash the UI. If you are unsure, include both. The markers are REQUIRED output, not optional.
|
||||
If any tasks in the engineer's message are marked `_(not yet completed)_`, re-include them \
|
||||
in your markers unless you are ≥75% confident that information is no longer relevant.
|
||||
[PROMOTE] markers are OPTIONAL and IN ADDITION to the required ones — emit them only \
|
||||
when the engineer's most recent message confirmed something worth recording, and copy \
|
||||
the originating item's `id` into `source_ref` verbatim.
|
||||
[SUGGEST_FIX] is OPTIONAL — emit one at most per response, only when you have a \
|
||||
concrete proposed resolution at ~50%+ confidence. A new [SUGGEST_FIX] supersedes \
|
||||
any prior suggested fix.
|
||||
[FIX_OUTCOME] is OPTIONAL — emit one at most per response, only when the engineer \
|
||||
has clearly stated the outcome in their current message.
|
||||
|
||||
ANTI-PARROT RULE: The schemas above use placeholders in `<angle brackets>` to show \
|
||||
the SHAPE of valid output. Your real questions, actions, facts, and suggested fixes \
|
||||
must be derived from the engineer's CURRENT message — never copy placeholder text, \
|
||||
never reuse content from a prior unrelated session, never invent ticket-specific \
|
||||
details (usernames, hostnames, IPs, error codes, application names, ticket numbers) \
|
||||
that the engineer has not stated. The technology, vocabulary, and named entities in \
|
||||
your output must match the technology, vocabulary, and named entities in the \
|
||||
engineer's most recent message. If the engineer's ticket is about a different \
|
||||
domain than the last ticket you saw, your output must reflect the new domain — \
|
||||
do not let the previous ticket's specifics bleed into the new one.
|
||||
"""
|
||||
|
||||
|
||||
@@ -184,7 +362,7 @@ async def _call_ai(
|
||||
to include alongside the new_message as vision content.
|
||||
"""
|
||||
if settings.AI_PROVIDER == "anthropic" and settings.ANTHROPIC_API_KEY:
|
||||
return await _call_anthropic_cached(
|
||||
return await chat_call_cached(
|
||||
system_base, rag_context, history, new_message, max_tokens,
|
||||
images=images,
|
||||
)
|
||||
@@ -202,7 +380,18 @@ async def _call_ai(
|
||||
)
|
||||
|
||||
|
||||
async def _call_anthropic_cached(
|
||||
# Appended to every chat turn's user message immediately before generation.
|
||||
# Invisible to storage (unified_chat_service strips markers before persisting),
|
||||
# but critical for structured output compliance — the model emits invalid
|
||||
# responses often enough without it that removing this reminder regresses UX.
|
||||
_CHAT_FORMAT_REMINDER = (
|
||||
"\n\n[SYSTEM: Remember — your response MUST end with [QUESTIONS] "
|
||||
"and/or [ACTIONS] markers containing valid JSON arrays. "
|
||||
"Responses without markers break the UI.]"
|
||||
)
|
||||
|
||||
|
||||
async def chat_call_cached(
|
||||
system_base: str,
|
||||
rag_context: str,
|
||||
history: list[dict[str, Any]],
|
||||
@@ -210,79 +399,56 @@ async def _call_anthropic_cached(
|
||||
max_tokens: int,
|
||||
images: list[dict[str, Any]] | None = None,
|
||||
) -> tuple[str, int, int]:
|
||||
"""Call Anthropic with prompt caching on system prompt and history.
|
||||
"""Call Anthropic's chat surface with caching, MCP, images, and retry-without-MCP.
|
||||
|
||||
Uses structured system blocks so the static base prompt is cached
|
||||
independently from the per-query RAG context. Optionally connects
|
||||
to Microsoft Learn via MCP for real-time documentation lookups.
|
||||
This is the ONE MCP/beta/multimodal chat caller. It is deliberately NOT
|
||||
routed through `AnthropicProvider`. See module docstring for rationale.
|
||||
|
||||
Responsibilities unique to this function (not in the provider):
|
||||
- Anthropic beta endpoint (`client.beta.messages.create`)
|
||||
- Microsoft Learn MCP connector wiring (optional via ENABLE_MCP_MICROSOFT_LEARN)
|
||||
- Retry-without-MCP fallback when the MCP server misbehaves
|
||||
- Multimodal image blocks in the user message
|
||||
- Format-reminder append for structured-output compliance
|
||||
- Telemetry (`mcp.turn`, `mcp.fallback`) for Phase 0.5 MCP usage signal
|
||||
|
||||
Cache plumbing is shared with the provider via helpers in `ai_provider`:
|
||||
`_normalize_system_for_anthropic` (policy α — ephemeral on first block if
|
||||
none specified), `build_anthropic_chat_messages` (history cache breakpoint +
|
||||
multimodal user message + format reminder), `_log_anthropic_cache_usage`.
|
||||
"""
|
||||
import anthropic
|
||||
|
||||
client = anthropic.AsyncAnthropic(
|
||||
api_key=settings.ANTHROPIC_API_KEY,
|
||||
client = _get_anthropic_client(
|
||||
settings.ANTHROPIC_API_KEY,
|
||||
timeout=settings.AI_REQUEST_TIMEOUT_SECONDS,
|
||||
)
|
||||
|
||||
# System prompt as structured blocks:
|
||||
# Block 1: static base prompt (cached)
|
||||
# Block 2: RAG context (changes per query, not cached)
|
||||
# System prompt as structured blocks. The static base is cacheable; the
|
||||
# RAG context changes per query and must NOT be cached — so we mark the
|
||||
# base explicitly and leave the RAG block unmarked. `_normalize_system`
|
||||
# honors caller-authored cache_control verbatim (policy α).
|
||||
system_blocks: list[dict[str, Any]] = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": system_base,
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
# cacheable: static system prompt, stable across all turns of all sessions
|
||||
},
|
||||
]
|
||||
if rag_context:
|
||||
system_blocks.append({"type": "text", "text": rag_context})
|
||||
system_blocks.append(
|
||||
{"type": "text", "text": rag_context}
|
||||
# uncached: RAG retrieval varies per query
|
||||
)
|
||||
normalized_system = _normalize_system_for_anthropic(system_blocks)
|
||||
|
||||
# Build messages with cache breakpoint on conversation history
|
||||
messages: list[dict[str, Any]] = []
|
||||
for msg in history:
|
||||
messages.append({"role": msg["role"], "content": msg["content"]})
|
||||
|
||||
# Place cache breakpoint on the last history message so the entire
|
||||
# conversation prefix is cached across turns
|
||||
if messages:
|
||||
last = messages[-1]
|
||||
messages[-1] = {
|
||||
"role": last["role"],
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": last["content"],
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
# Add the new user message (uncached — it's new each turn)
|
||||
# Append a format reminder to the user message so the model sees it
|
||||
# immediately before generating. This is invisible to the user (stripped
|
||||
# before storage) but critical for structured output compliance.
|
||||
format_reminder = (
|
||||
"\n\n[SYSTEM: Remember — your response MUST end with [QUESTIONS] "
|
||||
"and/or [ACTIONS] markers containing valid JSON arrays. "
|
||||
"Responses without markers break the UI.]"
|
||||
messages = build_anthropic_chat_messages(
|
||||
history=history,
|
||||
new_message=new_message,
|
||||
images=images,
|
||||
format_reminder=_CHAT_FORMAT_REMINDER,
|
||||
)
|
||||
reminded_message = new_message + format_reminder
|
||||
|
||||
# If images are attached, build multimodal content blocks
|
||||
if images:
|
||||
content_blocks: list[dict[str, Any]] = []
|
||||
for img in images:
|
||||
content_blocks.append({
|
||||
"type": "image",
|
||||
"source": {
|
||||
"type": "base64",
|
||||
"media_type": img["media_type"],
|
||||
"data": img["data"],
|
||||
},
|
||||
})
|
||||
content_blocks.append({"type": "text", "text": reminded_message})
|
||||
messages.append({"role": "user", "content": content_blocks})
|
||||
else:
|
||||
messages.append({"role": "user", "content": reminded_message})
|
||||
|
||||
# MCP server config (optional — controlled by settings)
|
||||
mcp_servers = anthropic.NOT_GIVEN
|
||||
@@ -304,12 +470,13 @@ async def _call_anthropic_cached(
|
||||
]
|
||||
|
||||
_mcp_active = mcp_servers is not anthropic.NOT_GIVEN
|
||||
_mcp_fallback_triggered = False
|
||||
|
||||
try:
|
||||
response = await client.beta.messages.create(
|
||||
model=settings.AI_MODEL_ANTHROPIC,
|
||||
max_tokens=max_tokens,
|
||||
system=system_blocks,
|
||||
system=normalized_system,
|
||||
messages=messages,
|
||||
mcp_servers=mcp_servers,
|
||||
tools=tools,
|
||||
@@ -326,14 +493,24 @@ async def _call_anthropic_cached(
|
||||
or isinstance(e, (anthropic.BadRequestError, anthropic.APIStatusError))
|
||||
)
|
||||
if _is_mcp_error:
|
||||
_mcp_fallback_triggered = True
|
||||
logger.warning(
|
||||
"MCP server error (%s), retrying without MCP: %s",
|
||||
type(e).__name__, e,
|
||||
)
|
||||
# Phase 0.5 telemetry: per-turn fallback event.
|
||||
logger.info(
|
||||
"mcp.fallback",
|
||||
extra={
|
||||
"event": "mcp.fallback",
|
||||
"mcp_error_type": type(e).__name__,
|
||||
"mcp_error_message": str(e)[:500],
|
||||
},
|
||||
)
|
||||
response = await client.messages.create(
|
||||
model=settings.AI_MODEL_ANTHROPIC,
|
||||
max_tokens=max_tokens,
|
||||
system=system_blocks,
|
||||
system=normalized_system,
|
||||
messages=messages,
|
||||
)
|
||||
else:
|
||||
@@ -355,18 +532,27 @@ async def _call_anthropic_cached(
|
||||
input_tokens = usage.input_tokens
|
||||
output_tokens = usage.output_tokens
|
||||
|
||||
# Log MCP tool usage
|
||||
# Phase 0.5 telemetry: per-turn MCP event. Emitted for every turn that
|
||||
# reached this code path (i.e., AI_PROVIDER=anthropic chat). `mcp_available`
|
||||
# reflects whether MCP was actually wired into the request (scope (ii) from
|
||||
# the Phase 0.5 design — Anthropic code path AND flag on). `mcp_invoked`
|
||||
# reflects whether the model chose to call an MCP tool on this turn.
|
||||
logger.info(
|
||||
"mcp.turn",
|
||||
extra={
|
||||
"event": "mcp.turn",
|
||||
"mcp_available": _mcp_active,
|
||||
"mcp_invoked": bool(mcp_tools_used),
|
||||
"mcp_tools": mcp_tools_used,
|
||||
"mcp_fallback_triggered": _mcp_fallback_triggered,
|
||||
},
|
||||
)
|
||||
|
||||
# Human-readable log retained for grep-based inspection.
|
||||
if mcp_tools_used:
|
||||
logger.info("MCP tools used: %s", ", ".join(mcp_tools_used))
|
||||
|
||||
# Log cache performance
|
||||
cache_read = getattr(usage, "cache_read_input_tokens", 0) or 0
|
||||
cache_creation = getattr(usage, "cache_creation_input_tokens", 0) or 0
|
||||
if cache_read or cache_creation:
|
||||
logger.info(
|
||||
"Anthropic cache: read=%d creation=%d input=%d output=%d",
|
||||
cache_read, cache_creation, input_tokens, output_tokens,
|
||||
)
|
||||
_log_anthropic_cache_usage(usage, settings.AI_MODEL_ANTHROPIC)
|
||||
|
||||
return text, input_tokens, output_tokens
|
||||
|
||||
|
||||
309
backend/app/services/escalation_package_generator.py
Normal file
309
backend/app/services/escalation_package_generator.py
Normal file
@@ -0,0 +1,309 @@
|
||||
"""EscalationPackageGeneratorService — drafts the handoff package for a session.
|
||||
|
||||
Parallel to ResolutionNoteGeneratorService but oriented around handoff to
|
||||
another engineer instead of closing the ticket. The output markdown follows
|
||||
FLOWPILOT-MIGRATION.md Section 6.3:
|
||||
|
||||
## Problem
|
||||
## What we've confirmed
|
||||
## What we've tried
|
||||
## Current hypothesis
|
||||
## Suggested next steps
|
||||
|
||||
Same caching story as resolution-note previews: keyed on
|
||||
`(session_id, ai_sessions.state_version)` via `preview_cache`, invalidated by
|
||||
any fact / suggested-fix / script-generation write.
|
||||
|
||||
Model: Sonnet (`escalation_package` action tier per Section 6.6). MCP off.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.ai_provider import get_ai_provider
|
||||
from app.core.config import settings
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.script_template import ScriptGeneration, ScriptTemplate
|
||||
from app.models.session_fact import SessionFact
|
||||
from app.models.session_suggested_fix import SessionSuggestedFix
|
||||
from app.services.preview_cache import preview_cache
|
||||
from app.services.script_template_engine import ScriptTemplateEngine
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
_ESCALATION_SYSTEM_PROMPT = """\
|
||||
You produce structured escalation handoff packages for an MSP troubleshooting \
|
||||
platform. The package is read by the next engineer picking up the ticket; it \
|
||||
must give them a running start without making them re-read the chat transcript.
|
||||
|
||||
Output exactly this markdown structure, no preamble, no closing remarks, no \
|
||||
extra headings:
|
||||
|
||||
## Problem
|
||||
<one short paragraph stating the issue the first engineer was working on, \
|
||||
past tense, no hedging. Derived from the session's intake/title and incident \
|
||||
header.>
|
||||
|
||||
## What we've confirmed
|
||||
<bulleted list of facts from the "What we know" section, each a short line. \
|
||||
If there are no facts, write "Nothing confirmed yet." and continue.>
|
||||
|
||||
## What we've tried
|
||||
<Bulleted list of diagnostic checks run and scripts generated during the \
|
||||
session. The content of this section also depends on the outcome recorded for \
|
||||
the active suggested fix, as given in the input bundle under "Outcome status":>
|
||||
|
||||
- applied_failed: List the fix as a tried path. Include the failure reason if \
|
||||
provided. State that it did not resolve the issue.
|
||||
- applied_partial: Include the fix as a partially tried path. Include partial \
|
||||
notes if provided. Indicate it was not fully completed or not verified.
|
||||
- applied_success: Note that the fix was applied and verified but escalation \
|
||||
is still needed for another reason (unusual — reflect this accurately).
|
||||
- dismissed: Do not mention the fix as a tried path; it was only considered.
|
||||
- proposed (no outcome yet): Do not list it here; it goes in Current hypothesis.
|
||||
|
||||
If nothing has been tried at all (no checks, no scripts, no applied/partial \
|
||||
fix), write "No diagnostic actions run yet." and continue.
|
||||
|
||||
## Current hypothesis
|
||||
<The content depends on the outcome recorded for the active suggested fix:>
|
||||
|
||||
- proposed (no outcome yet): State the fix title and confidence. If confidence \
|
||||
is below 60% or there is no active fix, say "No leading hypothesis yet — \
|
||||
symptoms are still being narrowed."
|
||||
- applied_failed or dismissed: Say the proposed fix did not hold or was set \
|
||||
aside. State any remaining uncertainty.
|
||||
- applied_partial: Note the partial application and what remains open.
|
||||
- applied_success: Unusual in an escalate path — state the fix resolved the \
|
||||
original symptom but a new or related issue requires escalation.
|
||||
|
||||
## Suggested next steps
|
||||
<bulleted list of 2-4 concrete next actions the receiving engineer should \
|
||||
take. Prefer specifics: commands to run, tickets to check, people to contact. \
|
||||
Derive from the gap between confirmed facts and a complete resolution. \
|
||||
If the active suggested fix failed (applied_failed), inform the next steps \
|
||||
accordingly — e.g. suggest alternatives or deeper investigation paths, \
|
||||
drawing on the failure reason if provided. \
|
||||
If the fix is partially applied (applied_partial), the first step is typically \
|
||||
to complete or verify it. \
|
||||
If the fix is still proposed (no outcome), the first step is to try it if \
|
||||
confidence is high (>80%).>
|
||||
|
||||
Strict rules:
|
||||
- Use ONLY the input I provide. Never invent command names, KB articles, or \
|
||||
configuration specifics that aren't in the input.
|
||||
- Do not include placeholder text like "TBD" or empty bullets.
|
||||
- Do not include the engineer's name, the AI's name, session IDs, or the \
|
||||
chat transcript verbatim.
|
||||
- Markdown headings exactly as shown (## level), no bolding.
|
||||
- The tone is a peer handing off to a peer, not a status report.
|
||||
"""
|
||||
|
||||
|
||||
class EscalationPackageGeneratorService:
|
||||
"""Generates and caches the five-section Escalate handoff markdown."""
|
||||
|
||||
KIND = "escalation_package"
|
||||
|
||||
def __init__(self, db: AsyncSession) -> None:
|
||||
self.db = db
|
||||
|
||||
async def generate_or_get_cached(
|
||||
self, session_id: UUID, *, force: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
session = await self._load_session(session_id)
|
||||
cached = preview_cache.get(self.KIND, session.id, session.state_version) if not force else None
|
||||
if cached is not None:
|
||||
return {**cached, "from_cache": True}
|
||||
|
||||
markdown = await self._render(session)
|
||||
target = self._target_ticket_ref(session)
|
||||
payload = {
|
||||
"markdown": markdown,
|
||||
"target_ticket_ref": target,
|
||||
"state_version": session.state_version,
|
||||
}
|
||||
preview_cache.set(self.KIND, session.id, session.state_version, payload)
|
||||
return {**payload, "from_cache": False}
|
||||
|
||||
# ── Internals (parallel to ResolutionNoteGenerator) ───────────────────
|
||||
|
||||
async def _load_session(self, session_id: UUID) -> AISession:
|
||||
result = await self.db.execute(
|
||||
select(AISession).where(AISession.id == session_id)
|
||||
)
|
||||
session = result.scalar_one_or_none()
|
||||
if session is None:
|
||||
raise ValueError(f"Session {session_id} not found")
|
||||
return session
|
||||
|
||||
async def _render(self, session: AISession) -> str:
|
||||
facts = await self._load_facts(session.id)
|
||||
active_fix = await self._load_active_fix(session.id)
|
||||
gens = await self._load_redacted_generations(session.id)
|
||||
|
||||
bundle = self._build_input_bundle(session, facts, active_fix, gens)
|
||||
|
||||
model = settings.get_model_for_action("escalation_package")
|
||||
provider = get_ai_provider(model=model)
|
||||
|
||||
system_blocks: list[dict[str, Any]] = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": _ESCALATION_SYSTEM_PROMPT,
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
# cacheable: identical across every escalation-package preview call
|
||||
},
|
||||
]
|
||||
|
||||
try:
|
||||
text, _in, _out = await provider.generate_text(
|
||||
system_prompt=system_blocks,
|
||||
messages=[{"role": "user", "content": bundle}],
|
||||
max_tokens=1400,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Escalation package generation failed for session %s", session.id)
|
||||
raise
|
||||
return text.strip()
|
||||
|
||||
async def _load_facts(self, session_id: UUID) -> list[SessionFact]:
|
||||
result = await self.db.execute(
|
||||
select(SessionFact)
|
||||
.where(
|
||||
SessionFact.session_id == session_id,
|
||||
SessionFact.deleted_at.is_(None),
|
||||
)
|
||||
.order_by(SessionFact.created_at.asc())
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def _load_active_fix(self, session_id: UUID) -> SessionSuggestedFix | None:
|
||||
result = await self.db.execute(
|
||||
select(SessionSuggestedFix)
|
||||
.where(
|
||||
SessionSuggestedFix.session_id == session_id,
|
||||
SessionSuggestedFix.superseded_at.is_(None),
|
||||
)
|
||||
.order_by(SessionSuggestedFix.created_at.desc())
|
||||
)
|
||||
return result.scalars().first()
|
||||
|
||||
async def _load_redacted_generations(
|
||||
self, session_id: UUID
|
||||
) -> list[dict[str, Any]]:
|
||||
result = await self.db.execute(
|
||||
select(ScriptGeneration)
|
||||
.where(ScriptGeneration.ai_session_id == session_id)
|
||||
.order_by(ScriptGeneration.created_at.asc())
|
||||
)
|
||||
gens = list(result.scalars().all())
|
||||
if not gens:
|
||||
return []
|
||||
|
||||
template_ids = {g.template_id for g in gens}
|
||||
tpl_result = await self.db.execute(
|
||||
select(ScriptTemplate).where(ScriptTemplate.id.in_(template_ids))
|
||||
)
|
||||
templates_by_id = {t.id: t for t in tpl_result.scalars().all()}
|
||||
|
||||
engine = ScriptTemplateEngine()
|
||||
out: list[dict[str, Any]] = []
|
||||
for g in gens:
|
||||
tpl = templates_by_id.get(g.template_id)
|
||||
sensitive_keys: set[str] = set()
|
||||
schema = (tpl.parameters_schema if tpl else {}) or {}
|
||||
params = schema.get("parameters") if isinstance(schema, dict) else None
|
||||
if isinstance(params, list):
|
||||
for p in params:
|
||||
if isinstance(p, dict) and p.get("field_type") == "password":
|
||||
k = p.get("key") or p.get("variable_name")
|
||||
if isinstance(k, str):
|
||||
sensitive_keys.add(k)
|
||||
redacted_params = engine.redact_sensitive(g.parameters_used or {}, sensitive_keys)
|
||||
out.append({
|
||||
"template_name": tpl.name if tpl else "(unknown template)",
|
||||
"template_slug": tpl.slug if tpl else None,
|
||||
"parameters_used": redacted_params,
|
||||
"created_at": g.created_at.isoformat(),
|
||||
})
|
||||
return out
|
||||
|
||||
@staticmethod
|
||||
def _target_ticket_ref(session: AISession) -> str | None:
|
||||
if not session.psa_ticket_id:
|
||||
return None
|
||||
return f"CW #{session.psa_ticket_id}"
|
||||
|
||||
@staticmethod
|
||||
def _build_input_bundle(
|
||||
session: AISession,
|
||||
facts: list[SessionFact],
|
||||
active_fix: SessionSuggestedFix | None,
|
||||
generations: list[dict[str, Any]],
|
||||
) -> str:
|
||||
lines: list[str] = []
|
||||
lines.append("# Session context")
|
||||
lines.append(f"Title: {session.title or '(untitled)'}")
|
||||
if session.problem_summary:
|
||||
lines.append(f"Problem summary: {session.problem_summary}")
|
||||
if session.problem_domain:
|
||||
lines.append(f"Domain: {session.problem_domain}")
|
||||
intake_text = (session.intake_content or {}).get("text") if isinstance(session.intake_content, dict) else None
|
||||
if intake_text:
|
||||
lines.append(f"Intake message: {intake_text}")
|
||||
if session.psa_ticket_id:
|
||||
lines.append(f"Linked PSA ticket: CW #{session.psa_ticket_id}")
|
||||
|
||||
lines.append("")
|
||||
lines.append("# Confirmed facts (What we know)")
|
||||
if not facts:
|
||||
lines.append("(none)")
|
||||
else:
|
||||
for f in facts:
|
||||
tag = f.source_type
|
||||
summary = f" — {f.source_summary}" if f.source_summary else ""
|
||||
lines.append(f"- [{tag}] {f.text}{summary}")
|
||||
|
||||
lines.append("")
|
||||
lines.append("# Diagnostic checks run during the session")
|
||||
check_facts = [f for f in facts if f.source_type == "diagnostic_check"]
|
||||
if not check_facts and not generations:
|
||||
lines.append("(none)")
|
||||
else:
|
||||
for f in check_facts:
|
||||
lines.append(f"- {f.text}")
|
||||
for g in generations:
|
||||
lines.append(f"- Ran script {g['template_name']} (slug={g['template_slug']})")
|
||||
if g["parameters_used"]:
|
||||
lines.append(f" parameters: {g['parameters_used']}")
|
||||
|
||||
lines.append("")
|
||||
lines.append("# Active suggested fix (current hypothesis)")
|
||||
if active_fix is None:
|
||||
lines.append("(no active suggested fix)")
|
||||
else:
|
||||
lines.append(f"Title: {active_fix.title}")
|
||||
lines.append(f"Confidence: {active_fix.confidence_pct}%")
|
||||
lines.append(f"Description: {active_fix.description}")
|
||||
lines.append(f"Outcome status: {active_fix.status}")
|
||||
if active_fix.applied_at:
|
||||
lines.append(f"Applied at: {active_fix.applied_at.isoformat()}")
|
||||
if active_fix.verified_at:
|
||||
lines.append(f"Verified at: {active_fix.verified_at.isoformat()}")
|
||||
if active_fix.partial_notes:
|
||||
lines.append(f"Partial notes: {active_fix.partial_notes}")
|
||||
if active_fix.failure_reason:
|
||||
lines.append(f"Failure reason: {active_fix.failure_reason}")
|
||||
|
||||
lines.append("")
|
||||
lines.append(
|
||||
"Produce the five-section escalation handoff now. Use only the input above."
|
||||
)
|
||||
return "\n".join(lines)
|
||||
285
backend/app/services/fact_synthesis_service.py
Normal file
285
backend/app/services/fact_synthesis_service.py
Normal file
@@ -0,0 +1,285 @@
|
||||
"""FactSynthesisService — converts engineer answers and check output into facts.
|
||||
|
||||
Two paths feed this service:
|
||||
|
||||
1. **AI marker path (the common case).** When the model emits a `[PROMOTE]`
|
||||
marker in the chat stream, `unified_chat_service` parses the marker (which
|
||||
already contains the engineer-readable `text` and short provenance `summary`)
|
||||
and calls `create_fact` directly. No LLM call is needed — the model already
|
||||
wrote the fact.
|
||||
|
||||
2. **Engineer-driven synthesize path.** The "+ Promote to What we know" affordance
|
||||
in the UI sends a raw answer or check output and asks the server to draft
|
||||
`text` + `summary` for review. `synthesize_from_question` /
|
||||
`synthesize_from_check` make a small Haiku call for that draft. The engineer
|
||||
confirms (or edits) before persistence, so the LLM output is never
|
||||
silently posted to a customer ticket.
|
||||
|
||||
Either way, persistence funnels through `create_fact`, which ALSO bumps
|
||||
`ai_sessions.state_version` so the resolution-note preview cache invalidates
|
||||
(see FLOWPILOT-MIGRATION.md Section 5.5).
|
||||
|
||||
Model tier is `fact_synthesis` in `settings.ACTION_MODEL_MAP` (Haiku per
|
||||
Section 6.6). MCP is intentionally disabled for synthesis — these are
|
||||
pure transformations of input, not research calls.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.ai_provider import get_ai_provider
|
||||
from app.core.config import settings
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.session_fact import SessionFact
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Conservative synthesis prompt. Hallucinated specifics are a trust-killer
|
||||
# because facts feed the resolution note posted to customer tickets — the
|
||||
# prompt makes "no fact" an explicit, valid output.
|
||||
_SYNTHESIS_SYSTEM_PROMPT = """\
|
||||
You convert one engineer answer or one diagnostic-check output into a single \
|
||||
candidate fact for a troubleshooting session's "What we know" log.
|
||||
|
||||
Return strict JSON with this shape:
|
||||
{
|
||||
"text": "<one short sentence stating what is now known, in past tense>",
|
||||
"summary": "<3-7 word provenance label, e.g. 'rules out tenant/license'>"
|
||||
}
|
||||
|
||||
If the answer/output does NOT contain a substantive fact (e.g. the engineer \
|
||||
typed 'unknown', the command failed, the output is empty), return:
|
||||
{
|
||||
"text": null,
|
||||
"summary": null
|
||||
}
|
||||
|
||||
Strict rules:
|
||||
- Use ONLY information present in the input. Never add details that were not stated.
|
||||
- Do not speculate, infer causes, or extrapolate. State only what the input proves.
|
||||
- The text is a fact a colleague could verify by looking at the original answer/output.
|
||||
- The summary names the diagnostic value (what this fact rules in or out), not the topic.
|
||||
- Output ONLY the JSON object, no prose, no markdown fences.
|
||||
"""
|
||||
|
||||
|
||||
class FactSynthesisService:
|
||||
"""Persists session facts and (optionally) drafts them via an LLM call.
|
||||
|
||||
Methods that touch the database take an `AsyncSession` and assume the
|
||||
caller commits. `create_fact` flushes so the returned row has a primary key.
|
||||
"""
|
||||
|
||||
def __init__(self, db: AsyncSession) -> None:
|
||||
self.db = db
|
||||
|
||||
# ── Persistence ────────────────────────────────────────────────────────
|
||||
|
||||
async def create_fact(
|
||||
self,
|
||||
*,
|
||||
session_id: UUID,
|
||||
account_id: UUID,
|
||||
user_id: UUID,
|
||||
source_type: str,
|
||||
text: str,
|
||||
summary: str | None = None,
|
||||
source_ref: UUID | None = None,
|
||||
) -> SessionFact:
|
||||
"""Persist a fact and bump the session's preview-cache version.
|
||||
|
||||
`source_ref` MUST be None for `user_note` and `ai_synthesis` sources;
|
||||
for `question` and `diagnostic_check` it should point at the stable
|
||||
UUID of the originating task-lane item. The DB has no FK constraint
|
||||
on `source_ref` (the target lives inside JSONB) — integrity is enforced
|
||||
here.
|
||||
"""
|
||||
if source_type not in ("question", "diagnostic_check", "user_note", "ai_synthesis"):
|
||||
raise ValueError(f"Invalid source_type: {source_type}")
|
||||
|
||||
if source_type in ("user_note", "ai_synthesis") and source_ref is not None:
|
||||
# `source_ref` is a back-pointer to a question/check; user notes
|
||||
# and AI-synthesized facts have no source item to point at.
|
||||
raise ValueError(
|
||||
f"source_ref must be None for source_type={source_type}"
|
||||
)
|
||||
|
||||
text = (text or "").strip()
|
||||
if not text:
|
||||
raise ValueError("Fact text cannot be empty")
|
||||
|
||||
fact = SessionFact(
|
||||
session_id=session_id,
|
||||
account_id=account_id,
|
||||
text=text,
|
||||
source_type=source_type,
|
||||
source_ref=source_ref,
|
||||
source_summary=(summary or "").strip() or None,
|
||||
created_by=user_id,
|
||||
)
|
||||
self.db.add(fact)
|
||||
|
||||
# Invalidate any preview cached against the prior state_version.
|
||||
# Single UPDATE so the bump is atomic relative to the fact insert
|
||||
# within this transaction; concurrent writers serialize on the row.
|
||||
await self.db.execute(
|
||||
update(AISession)
|
||||
.where(AISession.id == session_id)
|
||||
.values(state_version=AISession.state_version + 1)
|
||||
)
|
||||
await self.db.flush()
|
||||
return fact
|
||||
|
||||
async def soft_delete_fact(self, fact: SessionFact) -> None:
|
||||
"""Mark a fact deleted and bump state_version."""
|
||||
from datetime import datetime, timezone
|
||||
|
||||
fact.deleted_at = datetime.now(timezone.utc)
|
||||
await self.db.execute(
|
||||
update(AISession)
|
||||
.where(AISession.id == fact.session_id)
|
||||
.values(state_version=AISession.state_version + 1)
|
||||
)
|
||||
await self.db.flush()
|
||||
|
||||
async def update_fact(
|
||||
self,
|
||||
fact: SessionFact,
|
||||
*,
|
||||
text: str | None = None,
|
||||
summary: str | None = None,
|
||||
) -> SessionFact:
|
||||
"""Update an editable fact and bump state_version.
|
||||
|
||||
Caller is responsible for the editability check — only `user_note`
|
||||
and `ai_synthesis` facts may be edited at the card level. The
|
||||
endpoint enforces this and returns 403 for the read-only types.
|
||||
"""
|
||||
if text is not None:
|
||||
stripped = text.strip()
|
||||
if not stripped:
|
||||
raise ValueError("Fact text cannot be empty")
|
||||
fact.text = stripped
|
||||
if summary is not None:
|
||||
fact.source_summary = summary.strip() or None
|
||||
|
||||
await self.db.execute(
|
||||
update(AISession)
|
||||
.where(AISession.id == fact.session_id)
|
||||
.values(state_version=AISession.state_version + 1)
|
||||
)
|
||||
await self.db.flush()
|
||||
return fact
|
||||
|
||||
# ── LLM-backed drafting ────────────────────────────────────────────────
|
||||
|
||||
async def synthesize_from_question(
|
||||
self, *, question_text: str, raw_answer: str
|
||||
) -> dict[str, str | None]:
|
||||
"""Draft `{text, summary}` from a question + engineer's free-text answer.
|
||||
|
||||
Returns `{"text": None, "summary": None}` when the answer doesn't
|
||||
contain a substantive fact — caller should not persist in that case.
|
||||
"""
|
||||
return await self._synthesize(
|
||||
user_input=(
|
||||
f"Question asked: {question_text.strip()}\n"
|
||||
f"Engineer's answer: {raw_answer.strip()}"
|
||||
),
|
||||
)
|
||||
|
||||
async def synthesize_from_check(
|
||||
self, *, check_label: str, check_output: str
|
||||
) -> dict[str, str | None]:
|
||||
"""Draft `{text, summary}` from a diagnostic check label + its output."""
|
||||
return await self._synthesize(
|
||||
user_input=(
|
||||
f"Diagnostic check: {check_label.strip()}\n"
|
||||
f"Output:\n{check_output.strip()}"
|
||||
),
|
||||
)
|
||||
|
||||
async def _synthesize(self, *, user_input: str) -> dict[str, str | None]:
|
||||
"""Single Haiku call with the conservative synthesis prompt."""
|
||||
model = settings.get_model_for_action("fact_synthesis")
|
||||
provider = get_ai_provider(model=model)
|
||||
|
||||
# Cache the system prompt — it's identical across every synthesis call.
|
||||
system_blocks: list[dict[str, Any]] = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": _SYNTHESIS_SYSTEM_PROMPT,
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
# cacheable: identical across all fact-synthesis calls
|
||||
},
|
||||
]
|
||||
|
||||
try:
|
||||
text, _in, _out = await provider.generate_json(
|
||||
system_prompt=system_blocks,
|
||||
messages=[{"role": "user", "content": user_input}],
|
||||
max_tokens=200,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Fact synthesis LLM call failed")
|
||||
return {"text": None, "summary": None}
|
||||
|
||||
return self._parse_synthesis_response(text)
|
||||
|
||||
@staticmethod
|
||||
def _parse_synthesis_response(raw: str) -> dict[str, str | None]:
|
||||
"""Tolerant parse: strip fences, accept null fields, ignore extras."""
|
||||
cleaned = raw.strip()
|
||||
if cleaned.startswith("```"):
|
||||
cleaned = re.sub(r"^```(?:json)?\s*", "", cleaned)
|
||||
cleaned = re.sub(r"\s*```$", "", cleaned)
|
||||
|
||||
try:
|
||||
data = json.loads(cleaned)
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
logger.warning("Fact synthesis returned non-JSON: %r", raw[:200])
|
||||
return {"text": None, "summary": None}
|
||||
|
||||
if not isinstance(data, dict):
|
||||
return {"text": None, "summary": None}
|
||||
|
||||
text = data.get("text")
|
||||
summary = data.get("summary")
|
||||
if text is not None and not isinstance(text, str):
|
||||
text = None
|
||||
if summary is not None and not isinstance(summary, str):
|
||||
summary = None
|
||||
|
||||
# Treat empty strings the same as null — "no substantive fact".
|
||||
if isinstance(text, str) and not text.strip():
|
||||
text = None
|
||||
if isinstance(summary, str) and not summary.strip():
|
||||
summary = None
|
||||
|
||||
return {"text": text, "summary": summary}
|
||||
|
||||
|
||||
async def list_facts_for_session(
|
||||
db: AsyncSession, session_id: UUID
|
||||
) -> list[SessionFact]:
|
||||
"""List non-deleted facts for a session, oldest first.
|
||||
|
||||
RLS filters by tenant; the explicit account_id check is unnecessary here.
|
||||
"""
|
||||
result = await db.execute(
|
||||
select(SessionFact)
|
||||
.where(
|
||||
SessionFact.session_id == session_id,
|
||||
SessionFact.deleted_at.is_(None),
|
||||
)
|
||||
.order_by(SessionFact.created_at.asc())
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
@@ -330,6 +330,7 @@ async def start_session(
|
||||
# 7. Create first step
|
||||
step = _create_step_from_parsed(
|
||||
session_id=session.id,
|
||||
account_id=session.account_id,
|
||||
step_order=0,
|
||||
parsed=parsed,
|
||||
input_tokens=input_tokens,
|
||||
@@ -433,6 +434,7 @@ async def process_response(
|
||||
# Create new step
|
||||
step = _create_step_from_parsed(
|
||||
session_id=session.id,
|
||||
account_id=session.account_id,
|
||||
step_order=session.step_count - 1,
|
||||
parsed=parsed,
|
||||
input_tokens=input_tokens,
|
||||
@@ -630,8 +632,10 @@ async def pickup_session(
|
||||
allow_team_access=True, team_id=team_id,
|
||||
)
|
||||
|
||||
if session.status != "requesting_escalation":
|
||||
raise ValueError(f"Session is {session.status}, not requesting_escalation")
|
||||
if session.status not in ("requesting_escalation", "escalated"):
|
||||
raise ValueError(
|
||||
f"Session is {session.status}, not in an escalated state"
|
||||
)
|
||||
|
||||
# Can't pick up your own session
|
||||
if session.user_id == user_id:
|
||||
@@ -694,6 +698,7 @@ async def pickup_session(
|
||||
briefing_step = AISessionStep(
|
||||
id=uuid.uuid4(),
|
||||
session_id=session.id,
|
||||
account_id=session.account_id,
|
||||
branch_id=session.active_branch_id if session.is_branching else None,
|
||||
step_order=session.step_count,
|
||||
step_type="action",
|
||||
@@ -765,6 +770,7 @@ async def pickup_session(
|
||||
|
||||
next_step = _create_step_from_parsed(
|
||||
session_id=session.id,
|
||||
account_id=session.account_id,
|
||||
step_order=session.step_count - 1,
|
||||
parsed=parsed,
|
||||
input_tokens=input_tokens,
|
||||
@@ -997,6 +1003,7 @@ async def generate_status_update(
|
||||
step = AISessionStep(
|
||||
id=uuid.uuid4(),
|
||||
session_id=session.id,
|
||||
account_id=session.account_id,
|
||||
branch_id=session.active_branch_id if session.is_branching else None,
|
||||
step_order=session.step_count,
|
||||
step_type="status_update",
|
||||
@@ -1440,6 +1447,7 @@ def _format_engineer_response(request: StepResponseRequest) -> str:
|
||||
|
||||
def _create_step_from_parsed(
|
||||
session_id: UUID,
|
||||
account_id: UUID,
|
||||
step_order: int,
|
||||
parsed: dict[str, Any],
|
||||
input_tokens: int,
|
||||
@@ -1487,6 +1495,7 @@ def _create_step_from_parsed(
|
||||
return AISessionStep(
|
||||
id=uuid.uuid4(),
|
||||
session_id=session_id,
|
||||
account_id=account_id,
|
||||
branch_id=branch_id,
|
||||
step_order=step_order,
|
||||
step_type=step_type if parsed["type"] != "resolution_suggestion" else "action",
|
||||
|
||||
@@ -3,7 +3,17 @@
|
||||
Creates handoff snapshots, AI assessments (for escalations), claim workflow,
|
||||
and queue queries. Dual-writes to ai_sessions.escalation_package for
|
||||
backward compatibility with the existing escalation queue.
|
||||
|
||||
For intent='escalate', `create_handoff` also runs the legacy enrichment
|
||||
that the deprecated `/escalate` endpoint used to do directly: setting
|
||||
`escalated_to_id`, building the AI-enhanced escalation_package (Sonnet),
|
||||
and recording escalation_reason. `finalize_escalation` then generates the
|
||||
SessionDocumentation and pushes to PSA. `dispatch_escalation_notifications`
|
||||
fans out the bell-icon AppNotification + external channels (Slack/Teams)
|
||||
on top of per-user emails. The `/escalate` endpoint is now a thin shim
|
||||
calling these in sequence.
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
@@ -11,10 +21,17 @@ from uuid import UUID
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.email import EmailService
|
||||
from app.core.escalation_bus import bus as escalation_bus
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.session_branch import SessionBranch
|
||||
from app.models.session_handoff import SessionHandoff
|
||||
from app.models.user import User
|
||||
from app.schemas.ai_session import SessionDocumentation
|
||||
from app.services.notification_service import notify
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -32,28 +49,56 @@ class HandoffManager:
|
||||
engineer_notes: str | None,
|
||||
user_id: UUID,
|
||||
priority: str = "normal",
|
||||
target_user_id: UUID | None = None,
|
||||
) -> SessionHandoff:
|
||||
"""Create a handoff (park or escalate).
|
||||
|
||||
Generates snapshot, updates session status, dual-writes to
|
||||
escalation_package for backward compat.
|
||||
|
||||
For intent='escalate' also: sets `session.escalation_reason` and
|
||||
optionally `session.escalated_to_id`, builds the AI-enhanced
|
||||
escalation package (the rich one the legacy `/escalate` path used
|
||||
to produce), and merges the handoff metadata into it. Self-targeting
|
||||
is rejected with ValueError, matching legacy behavior.
|
||||
"""
|
||||
# Eager-load steps + user — _build_escalation_package_enhanced and
|
||||
# finalize_escalation iterate over session.steps to compose the
|
||||
# legacy enriched package and the SessionDocumentation, and the
|
||||
# notify() dispatcher reads session.user.name. Without selectinload
|
||||
# the async session raises MissingGreenlet on attribute access.
|
||||
result = await self.db.execute(
|
||||
select(AISession).where(AISession.id == session_id)
|
||||
select(AISession)
|
||||
.options(
|
||||
selectinload(AISession.steps),
|
||||
selectinload(AISession.user),
|
||||
)
|
||||
.where(AISession.id == session_id)
|
||||
)
|
||||
session = result.scalar_one_or_none()
|
||||
if not session:
|
||||
raise ValueError(f"Session {session_id} not found")
|
||||
|
||||
# Generate snapshot
|
||||
if intent == "escalate":
|
||||
if target_user_id and target_user_id == user_id:
|
||||
raise ValueError(
|
||||
"Cannot escalate a session to yourself. Use pause instead."
|
||||
)
|
||||
if session.status not in ("active", "paused"):
|
||||
raise ValueError(
|
||||
f"Cannot escalate session in status: {session.status}"
|
||||
)
|
||||
|
||||
# Generate snapshot — fast, no AI calls.
|
||||
snapshot = await self._generate_snapshot(session)
|
||||
|
||||
# Generate AI assessment for escalations
|
||||
ai_assessment = None
|
||||
ai_assessment_data = None
|
||||
if intent == "escalate":
|
||||
ai_assessment, ai_assessment_data = await self._generate_ai_assessment(session)
|
||||
|
||||
# AI enrichment (assessment + enhanced escalation_package) is now
|
||||
# deferred to a background task scheduled by the endpoint after
|
||||
# commit — both calls hit Sonnet and together can take 15-25s,
|
||||
# which is too long to block the click path. The handoff row lands
|
||||
# immediately with `ai_assessment=None`; the magic-moment screen
|
||||
# shows "Assessment still computing" until enrich_async finishes
|
||||
# and the senior refreshes (or, eventually, polls).
|
||||
handoff = SessionHandoff(
|
||||
session_id=session_id,
|
||||
account_id=session.account_id,
|
||||
@@ -61,8 +106,8 @@ class HandoffManager:
|
||||
intent=intent,
|
||||
source_branch_id=session.active_branch_id,
|
||||
snapshot=snapshot,
|
||||
ai_assessment=ai_assessment,
|
||||
ai_assessment_data=ai_assessment_data,
|
||||
ai_assessment=None,
|
||||
ai_assessment_data=None,
|
||||
engineer_notes=engineer_notes,
|
||||
priority=priority,
|
||||
)
|
||||
@@ -73,10 +118,17 @@ class HandoffManager:
|
||||
session.status = "paused"
|
||||
elif intent == "escalate":
|
||||
session.status = "escalated"
|
||||
session.escalation_reason = engineer_notes
|
||||
if target_user_id:
|
||||
session.escalated_to_id = target_user_id
|
||||
|
||||
session.handoff_count = (session.handoff_count or 0) + 1
|
||||
|
||||
# Dual-write for backward compat
|
||||
# Dual-write the minimal escalation_package shape now. The async
|
||||
# enrichment task overwrites this with the AI-enhanced shape
|
||||
# (`steps_tried`, `remaining_hypotheses`, etc.) when it completes —
|
||||
# consumers that read these fields (PSA writeback, legacy
|
||||
# SessionBriefing) tolerate either shape.
|
||||
session.escalation_package = {
|
||||
"snapshot": snapshot,
|
||||
"intent": intent,
|
||||
@@ -87,6 +139,227 @@ class HandoffManager:
|
||||
await self.db.flush()
|
||||
return handoff
|
||||
|
||||
async def finalize_escalation(
|
||||
self,
|
||||
handoff: SessionHandoff,
|
||||
session: AISession,
|
||||
user_id: UUID,
|
||||
) -> tuple[SessionDocumentation | None, dict[str, Any]]:
|
||||
"""Post-create enrichment for intent='escalate' handoffs.
|
||||
|
||||
Generates the SessionDocumentation + pushes documentation to PSA if
|
||||
a ticket is linked. Returns (documentation, psa_result) so the
|
||||
legacy `/escalate` shim can map back to SessionCloseResponse. Safe
|
||||
to call only when handoff.intent == 'escalate' — for park, returns
|
||||
a no-op no-PSA dict.
|
||||
"""
|
||||
if handoff.intent != "escalate":
|
||||
return None, {
|
||||
"psa_push_status": "no_psa",
|
||||
"psa_push_error": None,
|
||||
"member_mapping_warning": None,
|
||||
}
|
||||
|
||||
# Lazy import to avoid circular dependency: flowpilot_engine imports
|
||||
# plenty of services at module load time and we don't want
|
||||
# handoff_manager pulled into that graph at import.
|
||||
from app.services.flowpilot_engine import (
|
||||
_generate_documentation,
|
||||
_push_to_psa,
|
||||
)
|
||||
|
||||
documentation = _generate_documentation(session)
|
||||
psa_result = await _push_to_psa(session, user_id, self.db)
|
||||
|
||||
# Bell-icon AppNotification rows + external account-level channels
|
||||
# (Slack/Teams webhooks, shared escalations inboxes). This is the
|
||||
# `notify()` call the legacy /escalate path used to make directly,
|
||||
# and it has to happen BEFORE the endpoint commits so the
|
||||
# AppNotification rows land atomically with the handoff. Per-user
|
||||
# emails come after commit in dispatch_escalation_notifications —
|
||||
# those are pure IO with no persistent state.
|
||||
try:
|
||||
engineer_user = (
|
||||
await self.db.execute(
|
||||
select(User).where(User.id == user_id)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
engineer_name = (
|
||||
engineer_user.name
|
||||
if engineer_user and engineer_user.name
|
||||
else "Unknown"
|
||||
)
|
||||
target_user_ids = (
|
||||
[session.escalated_to_id] if session.escalated_to_id else None
|
||||
)
|
||||
await notify(
|
||||
"session.escalated",
|
||||
handoff.account_id,
|
||||
{
|
||||
"session_id": str(handoff.session_id),
|
||||
"engineer_name": engineer_name,
|
||||
"escalation_reason": handoff.engineer_notes or "",
|
||||
"problem_summary": session.problem_summary or "N/A",
|
||||
# Surface the PSA ticket id in the bell-icon title so two
|
||||
# similarly-worded escalations are still distinguishable
|
||||
# at a glance.
|
||||
"psa_ticket_id": session.psa_ticket_id,
|
||||
},
|
||||
self.db,
|
||||
target_user_ids=target_user_ids,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"notify() dispatch failed for handoff %s", handoff.id
|
||||
)
|
||||
|
||||
return documentation, psa_result
|
||||
|
||||
async def _build_enhanced_escalation_package(
|
||||
self,
|
||||
session: AISession,
|
||||
user_id: UUID,
|
||||
) -> dict[str, Any]:
|
||||
"""Lazy wrapper around the legacy enhanced-package builder.
|
||||
|
||||
The builder lives in flowpilot_engine; we only need it for the
|
||||
escalate path. Failures are caught here so handoff creation never
|
||||
depends on the optional Sonnet enrichment — return the minimal
|
||||
shape on failure.
|
||||
"""
|
||||
try:
|
||||
from app.services.flowpilot_engine import (
|
||||
_build_escalation_package_enhanced,
|
||||
)
|
||||
return await _build_escalation_package_enhanced(session, user_id)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Enhanced escalation package build failed for session %s; "
|
||||
"falling back to minimal package",
|
||||
session.id,
|
||||
)
|
||||
return {}
|
||||
|
||||
|
||||
async def dispatch_escalation_notifications(
|
||||
self, handoff: SessionHandoff
|
||||
) -> int:
|
||||
"""Email engineer-or-admin users in the account about a new escalation.
|
||||
|
||||
Call this AFTER `db.commit()` has succeeded — sending email for a
|
||||
rolled-back handoff is the kind of trust-erosion bug that makes pilot
|
||||
customers stop trusting the tool. Returns the number of recipients
|
||||
successfully emailed (best-effort, not authoritative).
|
||||
|
||||
Failures are logged but never raise: the wedge demo's reliability
|
||||
story is "handoff creation always succeeds; notification is best-effort,"
|
||||
not "handoff creation depends on the email service being up." This is
|
||||
the graceful-degradation regression the eng + codex reviews both
|
||||
flagged as critical.
|
||||
|
||||
Per-channel delivery records (Codex correction on the dead
|
||||
`notification_sent` boolean) are a v1.x story — for now the
|
||||
application logs are the audit trail.
|
||||
"""
|
||||
if handoff.intent != "escalate":
|
||||
return 0
|
||||
|
||||
# Publish to the in-memory bus first so connected senior-tech inboxes
|
||||
# see the new card slide in within ~1s of escalate. This path is
|
||||
# fire-and-forget (no IO, just memory) so it can sit ahead of the
|
||||
# email fan-out.
|
||||
try:
|
||||
await escalation_bus.publish(
|
||||
handoff.account_id,
|
||||
{
|
||||
"type": "handoff_created",
|
||||
"handoff_id": str(handoff.id),
|
||||
"session_id": str(handoff.session_id),
|
||||
"priority": handoff.priority,
|
||||
"engineer_notes": handoff.engineer_notes or "",
|
||||
"created_at": handoff.created_at.isoformat()
|
||||
if handoff.created_at
|
||||
else None,
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"EscalationBus publish failed for handoff %s", handoff.id
|
||||
)
|
||||
|
||||
try:
|
||||
recipients = (
|
||||
await self.db.execute(
|
||||
select(User).where(
|
||||
User.account_id == handoff.account_id,
|
||||
User.id != handoff.handed_off_by,
|
||||
User.account_role.in_(("owner", "admin", "engineer")),
|
||||
User.is_active.is_(True),
|
||||
User.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
).scalars().all()
|
||||
|
||||
if not recipients:
|
||||
logger.info(
|
||||
"No notification recipients for handoff %s in account %s",
|
||||
handoff.id,
|
||||
handoff.account_id,
|
||||
)
|
||||
return 0
|
||||
|
||||
# Pull session for the email subject. Fall back to a generic title
|
||||
# if the session is gone (e.g. cascade delete mid-dispatch).
|
||||
session_result = await self.db.execute(
|
||||
select(AISession).where(AISession.id == handoff.session_id)
|
||||
)
|
||||
session = session_result.scalar_one_or_none()
|
||||
problem = (
|
||||
session.problem_summary if session and session.problem_summary
|
||||
else "an active session"
|
||||
)
|
||||
|
||||
title = f"New escalation: {problem}"
|
||||
notes = (handoff.engineer_notes or "").strip()
|
||||
body = (
|
||||
"A teammate has escalated a session and is asking for help.\n\n"
|
||||
f"Reason: {notes if notes else 'No reason provided.'}\n"
|
||||
f"Priority: {handoff.priority}"
|
||||
)
|
||||
link_url = (
|
||||
f"{settings.FRONTEND_URL.rstrip('/')}/escalations"
|
||||
if settings.FRONTEND_URL
|
||||
else None
|
||||
)
|
||||
|
||||
results = await asyncio.gather(
|
||||
*[
|
||||
EmailService.send_notification_email(
|
||||
to_email=r.email,
|
||||
title=title,
|
||||
body=body,
|
||||
link_url=link_url,
|
||||
)
|
||||
for r in recipients
|
||||
],
|
||||
return_exceptions=True,
|
||||
)
|
||||
sent = sum(1 for r in results if r is True)
|
||||
logger.info(
|
||||
"Escalation notifications dispatched for handoff %s: %d/%d recipients",
|
||||
handoff.id,
|
||||
sent,
|
||||
len(recipients),
|
||||
)
|
||||
return sent
|
||||
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Escalation notification dispatch failed for handoff %s",
|
||||
handoff.id,
|
||||
)
|
||||
return 0
|
||||
|
||||
async def _generate_snapshot(self, session: AISession) -> dict[str, Any]:
|
||||
"""Generate a snapshot of the session state at handoff time."""
|
||||
snapshot: dict[str, Any] = {
|
||||
@@ -187,6 +460,24 @@ class HandoffManager:
|
||||
logger.exception("Failed to generate AI assessment")
|
||||
return None, None
|
||||
|
||||
async def _generate_ai_assessment_with_timeout(
|
||||
self, session: AISession
|
||||
) -> tuple[str | None, dict[str, Any] | None]:
|
||||
"""Generate optional escalation assessment within the click-path budget."""
|
||||
timeout = settings.ESCALATION_AI_ASSESSMENT_TIMEOUT_SECONDS
|
||||
try:
|
||||
return await asyncio.wait_for(
|
||||
self._generate_ai_assessment(session),
|
||||
timeout=timeout,
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(
|
||||
"Escalation AI assessment timed out after %ss for session %s",
|
||||
timeout,
|
||||
session.id,
|
||||
)
|
||||
return None, None
|
||||
|
||||
async def generate_briefing(
|
||||
self, handoff_id: UUID, claiming_user_id: UUID
|
||||
) -> str:
|
||||
@@ -288,3 +579,113 @@ class HandoffManager:
|
||||
})
|
||||
|
||||
return queue_items
|
||||
|
||||
|
||||
async def enrich_escalation_async(handoff_id: UUID, user_id: UUID) -> None:
|
||||
"""Run the AI enrichment for an escalation handoff in the background.
|
||||
|
||||
Scheduled by `/escalate` and `/handoff` (intent=escalate) endpoints via
|
||||
FastAPI BackgroundTasks. Opens its own DB session because the request
|
||||
session is closed by the time this runs. Generates:
|
||||
|
||||
1. The legacy AI-enhanced escalation_package (Sonnet, ~5-10s) — saved
|
||||
to `session.escalation_package`, preserving the `intent` /
|
||||
`engineer_notes` / `handoff_id` keys the dual-write set so legacy
|
||||
consumers keep working.
|
||||
2. The diagnostic AI assessment (Sonnet, ~4-15s) — saved to
|
||||
`handoff.ai_assessment` and `handoff.ai_assessment_data`.
|
||||
|
||||
On completion publishes a `handoff_assessment_ready` event on the
|
||||
escalation bus so any connected magic-moment screen can refresh
|
||||
without a manual reload. Failures are logged but never propagated —
|
||||
the click-path-side handoff creation already committed, so worst case
|
||||
the senior sees the "Assessment still computing" placeholder until
|
||||
they refresh manually.
|
||||
"""
|
||||
from app.core.database import async_session_maker
|
||||
from app.core.escalation_bus import bus as escalation_bus
|
||||
|
||||
async with async_session_maker() as db:
|
||||
try:
|
||||
result = await db.execute(
|
||||
select(SessionHandoff).where(SessionHandoff.id == handoff_id)
|
||||
)
|
||||
handoff = result.scalar_one_or_none()
|
||||
if not handoff or handoff.intent != "escalate":
|
||||
return
|
||||
|
||||
session_result = await db.execute(
|
||||
select(AISession)
|
||||
.options(selectinload(AISession.steps), selectinload(AISession.user))
|
||||
.where(AISession.id == handoff.session_id)
|
||||
)
|
||||
session = session_result.scalar_one_or_none()
|
||||
if not session:
|
||||
logger.warning(
|
||||
"enrich_escalation_async: session %s gone for handoff %s",
|
||||
handoff.session_id,
|
||||
handoff_id,
|
||||
)
|
||||
return
|
||||
|
||||
manager = HandoffManager(db)
|
||||
|
||||
# Build the enhanced package (Sonnet). Don't fail the whole
|
||||
# task if it errors — the assessment is independently useful.
|
||||
try:
|
||||
enhanced_pkg = await manager._build_enhanced_escalation_package(
|
||||
session, user_id
|
||||
)
|
||||
if enhanced_pkg:
|
||||
enhanced_pkg["intent"] = "escalate"
|
||||
enhanced_pkg["engineer_notes"] = handoff.engineer_notes
|
||||
enhanced_pkg["handoff_id"] = str(handoff.id)
|
||||
if isinstance(session.escalation_package, dict):
|
||||
enhanced_pkg.setdefault(
|
||||
"snapshot", session.escalation_package.get("snapshot")
|
||||
)
|
||||
session.escalation_package = enhanced_pkg
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"enrich_escalation_async: enhanced package build failed for handoff %s",
|
||||
handoff_id,
|
||||
)
|
||||
|
||||
# Generate the diagnostic AI assessment.
|
||||
try:
|
||||
ai_assessment, ai_assessment_data = (
|
||||
await manager._generate_ai_assessment_with_timeout(session)
|
||||
)
|
||||
handoff.ai_assessment = ai_assessment
|
||||
handoff.ai_assessment_data = ai_assessment_data
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"enrich_escalation_async: assessment generation failed for handoff %s",
|
||||
handoff_id,
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
|
||||
try:
|
||||
await escalation_bus.publish(
|
||||
handoff.account_id,
|
||||
{
|
||||
"type": "handoff_assessment_ready",
|
||||
"handoff_id": str(handoff.id),
|
||||
"session_id": str(handoff.session_id),
|
||||
"has_assessment": handoff.ai_assessment is not None,
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"enrich_escalation_async: bus publish failed for handoff %s",
|
||||
handoff_id,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"enrich_escalation_async failed for handoff %s", handoff_id
|
||||
)
|
||||
try:
|
||||
await db.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
151
backend/app/services/network_diagram_ai_service.py
Normal file
151
backend/app/services/network_diagram_ai_service.py
Normal file
@@ -0,0 +1,151 @@
|
||||
"""AI service for generating network diagrams from natural language."""
|
||||
import json
|
||||
import logging
|
||||
|
||||
from app.core.ai_provider import get_ai_provider
|
||||
from app.core.config import settings
|
||||
from app.schemas.network_diagram import (
|
||||
AIGenerateRequest,
|
||||
AIGenerateResponse,
|
||||
DiagramNode,
|
||||
DiagramEdge,
|
||||
DeviceProperties,
|
||||
Position,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
SYSTEM_PROMPT_TEMPLATE = """You are a network diagram generator for MSP engineers.
|
||||
Given a plain English description of a network, you must return ONLY valid JSON with no markdown, no explanation, no preamble.
|
||||
|
||||
Return this exact structure:
|
||||
{{
|
||||
"nodes": [
|
||||
{{
|
||||
"id": "unique-string",
|
||||
"type": "device-type-slug",
|
||||
"label": "device label",
|
||||
"position": {{ "x": number, "y": number }},
|
||||
"properties": {{
|
||||
"hostname": "string or null",
|
||||
"ip": "string or null",
|
||||
"subnet": "string or null",
|
||||
"vendor": "string or null",
|
||||
"model": "string or null",
|
||||
"role": "string or null",
|
||||
"vlan": "string or null",
|
||||
"notes": "string or null",
|
||||
"status": "unknown"
|
||||
}}
|
||||
}}
|
||||
],
|
||||
"edges": [
|
||||
{{
|
||||
"id": "unique-string",
|
||||
"source": "node-id",
|
||||
"target": "node-id",
|
||||
"label": "connection label or null",
|
||||
"connectionType": "ethernet|fiber|wifi|vpn|vlan|wan",
|
||||
"speed": "string or null",
|
||||
"notes": "string or null"
|
||||
}}
|
||||
],
|
||||
"suggestedName": "short descriptive diagram name",
|
||||
"notes": "any important assumptions or missing info, or null"
|
||||
}}
|
||||
|
||||
Available device type slugs: {available_slugs}
|
||||
|
||||
Position nodes thoughtfully in a logical network topology layout.
|
||||
Use x/y coordinates between 0 and 1200 for x, 0 and 800 for y.
|
||||
Place WAN/internet at top, core network in middle, endpoints at bottom.
|
||||
{merge_instructions}"""
|
||||
|
||||
MERGE_INSTRUCTIONS = """
|
||||
IMPORTANT: You are ADDING devices to an existing diagram. Do NOT replace existing devices.
|
||||
The existing diagram occupies this bounding box: minX={minX}, maxX={maxX}, minY={minY}, maxY={maxY}.
|
||||
Place all new nodes OUTSIDE this bounding box — below (y > {maxY} + 100) or to the right (x > {maxX} + 100).
|
||||
You may create edges that connect new nodes to existing nodes if the description implies a connection.
|
||||
Use these existing node IDs for connections: {existing_node_ids}"""
|
||||
|
||||
|
||||
async def generate_diagram(
|
||||
request: AIGenerateRequest,
|
||||
available_slugs: list[str],
|
||||
existing_node_ids: list[str] | None = None,
|
||||
) -> AIGenerateResponse:
|
||||
merge_instructions = ""
|
||||
if request.mode == "merge" and request.existingBounds:
|
||||
b = request.existingBounds
|
||||
merge_instructions = MERGE_INSTRUCTIONS.format(
|
||||
minX=b.minX, maxX=b.maxX, minY=b.minY, maxY=b.maxY,
|
||||
existing_node_ids=", ".join(existing_node_ids or []),
|
||||
)
|
||||
|
||||
system_prompt = SYSTEM_PROMPT_TEMPLATE.format(
|
||||
available_slugs=", ".join(available_slugs),
|
||||
merge_instructions=merge_instructions,
|
||||
)
|
||||
|
||||
model = settings.get_model_for_action("network_diagram_generate")
|
||||
provider = get_ai_provider(model)
|
||||
|
||||
messages = [{"role": "user", "content": request.description}]
|
||||
|
||||
response_text, input_tokens, output_tokens = await provider.generate_json(
|
||||
system_prompt=system_prompt,
|
||||
messages=messages,
|
||||
max_tokens=4096,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"Network diagram AI generation: input_tokens=%d, output_tokens=%d",
|
||||
input_tokens, output_tokens,
|
||||
)
|
||||
|
||||
try:
|
||||
data = json.loads(response_text)
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error("Failed to parse AI response as JSON: %s", e)
|
||||
raise ValueError("AI generated an invalid response, please try again")
|
||||
|
||||
try:
|
||||
nodes = []
|
||||
for raw_node in data.get("nodes", []):
|
||||
node_type = raw_node.get("type", "server")
|
||||
if node_type not in available_slugs:
|
||||
logger.warning("Unknown device type '%s', falling back to 'server'", node_type)
|
||||
node_type = "server"
|
||||
|
||||
nodes.append(DiagramNode(
|
||||
id=raw_node["id"],
|
||||
type=node_type,
|
||||
label=raw_node.get("label", node_type),
|
||||
position=Position(**raw_node.get("position", {"x": 0, "y": 0})),
|
||||
properties=DeviceProperties(**{
|
||||
k: v for k, v in raw_node.get("properties", {}).items()
|
||||
if k in DeviceProperties.model_fields
|
||||
}),
|
||||
))
|
||||
|
||||
edges = []
|
||||
for raw_edge in data.get("edges", []):
|
||||
edges.append(DiagramEdge(
|
||||
id=raw_edge["id"],
|
||||
source=raw_edge["source"],
|
||||
target=raw_edge["target"],
|
||||
label=raw_edge.get("label"),
|
||||
connectionType=raw_edge.get("connectionType", "ethernet"),
|
||||
speed=raw_edge.get("speed"),
|
||||
notes=raw_edge.get("notes"),
|
||||
))
|
||||
except KeyError as e:
|
||||
logger.warning("AI response missing required field: %s", e)
|
||||
raise ValueError(f"AI generated incomplete data (missing {e}), please try again")
|
||||
|
||||
return AIGenerateResponse(
|
||||
nodes=nodes,
|
||||
edges=edges,
|
||||
suggestedName=data.get("suggestedName"),
|
||||
notes=data.get("notes"),
|
||||
)
|
||||
@@ -371,13 +371,35 @@ async def _send_teams_message(
|
||||
def _build_notification_title(event: str, payload: dict[str, Any]) -> str:
|
||||
"""Human-readable title per event type."""
|
||||
titles = {
|
||||
"session.escalated": "Session escalated by {engineer_name}",
|
||||
# Distinguishability matters in the bell panel: with a generic title
|
||||
# ("Session escalated by Jane") two different escalations from the
|
||||
# same junior look like a duplicate notification. Including a short
|
||||
# problem snippet (and ticket number if present) lets the senior
|
||||
# tell them apart at a glance.
|
||||
"session.escalated": "Escalation from {engineer_name}{ticket_suffix}: {problem_snippet}",
|
||||
"session.high_priority": "High-priority session started: {ticket_number}",
|
||||
"proposal.pending": "New flow proposal: {title}",
|
||||
"proposal.approved": "Flow proposal approved: {title}",
|
||||
"knowledge_gap.detected": "Knowledge gap detected: {gap_type}",
|
||||
"test": "Test Notification from ResolutionFlow",
|
||||
}
|
||||
|
||||
# Build the escalation-specific derived fields. Done here rather than at
|
||||
# the call site so every dispatch path (legacy /escalate shim, /handoff,
|
||||
# any future entry point) gets consistent formatting without each one
|
||||
# having to repeat the snippet logic.
|
||||
if event == "session.escalated":
|
||||
problem = (payload.get("problem_summary") or "").strip()
|
||||
if not problem or problem.upper() == "N/A":
|
||||
problem_snippet = "(no summary provided)"
|
||||
elif len(problem) > 70:
|
||||
problem_snippet = problem[:67].rstrip() + "…"
|
||||
else:
|
||||
problem_snippet = problem
|
||||
ticket = payload.get("psa_ticket_id") or payload.get("ticket_number")
|
||||
ticket_suffix = f" · #{ticket}" if ticket else ""
|
||||
payload = {**payload, "problem_snippet": problem_snippet, "ticket_suffix": ticket_suffix}
|
||||
|
||||
template = titles.get(event, f"Notification: {event}")
|
||||
try:
|
||||
return template.format(**payload)
|
||||
@@ -405,7 +427,12 @@ def _build_notification_body(event: str, payload: dict[str, Any]) -> str:
|
||||
def _build_notification_link(event: str, payload: dict[str, Any]) -> Optional[str]:
|
||||
"""In-app link per event type. Returns path (no host)."""
|
||||
links: dict[str, str] = {
|
||||
"session.escalated": "/pilot/{session_id}",
|
||||
# ?pickup=true triggers the senior-tech handoff/pickup flow on the
|
||||
# session page (magic-moment screen for handoff-based escalations,
|
||||
# legacy SessionBriefing for `requesting_escalation` sessions).
|
||||
# Without it the senior lands on a session-detail GET they can't
|
||||
# access pre-pickup, which the user perceives as a dead notification.
|
||||
"session.escalated": "/pilot/{session_id}?pickup=true",
|
||||
"session.high_priority": "/pilot/{session_id}",
|
||||
"proposal.pending": "/review-queue",
|
||||
"proposal.approved": "/review-queue",
|
||||
|
||||
52
backend/app/services/preview_cache.py
Normal file
52
backend/app/services/preview_cache.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""In-process preview cache for FlowPilot resolution-note / escalation-package previews.
|
||||
|
||||
Phase 3 implementation per FLOWPILOT-MIGRATION.md Section 5.5:
|
||||
- Cache key: `(kind, session_id, state_version)` — no TTL needed, state_version
|
||||
is the source of truth.
|
||||
- Invalidation: any write to session_facts, session_suggested_fixes, or
|
||||
script_generations bumps `ai_sessions.state_version`. Old entries simply
|
||||
stop being looked up and leak harmlessly until process restart.
|
||||
- Storage: plain dict, single-process. When Session Sharing brings Redis,
|
||||
swap the storage without changing the call sites.
|
||||
|
||||
Bound: best-effort soft cap of 5000 entries. When exceeded we drop the
|
||||
oldest insertion. Not a TTL — at current scale, the cap is more about
|
||||
resident-memory hygiene than correctness.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import OrderedDict
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
_MAX_ENTRIES = 5000
|
||||
|
||||
|
||||
class _PreviewCache:
|
||||
def __init__(self) -> None:
|
||||
self._store: OrderedDict[tuple[str, UUID, int], Any] = OrderedDict()
|
||||
|
||||
def get(self, kind: str, session_id: UUID, state_version: int) -> Any | None:
|
||||
key = (kind, session_id, state_version)
|
||||
if key not in self._store:
|
||||
return None
|
||||
# Touch on access so LRU eviction is meaningful.
|
||||
self._store.move_to_end(key)
|
||||
return self._store[key]
|
||||
|
||||
def set(self, kind: str, session_id: UUID, state_version: int, value: Any) -> None:
|
||||
key = (kind, session_id, state_version)
|
||||
self._store[key] = value
|
||||
self._store.move_to_end(key)
|
||||
# Evict oldest if over cap. OrderedDict.popitem(last=False) is O(1).
|
||||
while len(self._store) > _MAX_ENTRIES:
|
||||
self._store.popitem(last=False)
|
||||
|
||||
def invalidate_session(self, session_id: UUID) -> None:
|
||||
"""Drop all entries for a session — used when the session is deleted."""
|
||||
keys = [k for k in self._store if k[1] == session_id]
|
||||
for k in keys:
|
||||
del self._store[k]
|
||||
|
||||
|
||||
preview_cache = _PreviewCache()
|
||||
@@ -11,6 +11,11 @@ from app.services.psa.types import (
|
||||
PSAMember,
|
||||
PSAConfiguration,
|
||||
PSATimeEntry,
|
||||
PSABoard,
|
||||
PaginatedTicketResult,
|
||||
PSAResource,
|
||||
PSACreatedTicket,
|
||||
TicketCreatePayload,
|
||||
)
|
||||
|
||||
|
||||
@@ -27,7 +32,7 @@ class AutotaskProvider(PSAProvider):
|
||||
async def get_ticket(self, ticket_id: str) -> PSATicket:
|
||||
raise NotImplementedError("Autotask integration coming soon")
|
||||
|
||||
async def search_tickets(self, query: str, **filters) -> list[PSATicket]:
|
||||
async def search_tickets(self, query: str, **filters) -> PaginatedTicketResult:
|
||||
raise NotImplementedError("Autotask integration coming soon")
|
||||
|
||||
async def post_note(
|
||||
@@ -58,6 +63,9 @@ class AutotaskProvider(PSAProvider):
|
||||
async def list_members(self) -> list[PSAMember]:
|
||||
raise NotImplementedError("Autotask integration coming soon")
|
||||
|
||||
async def list_boards(self) -> list[PSABoard]:
|
||||
raise NotImplementedError("list_boards not implemented for this provider")
|
||||
|
||||
async def get_ticket_configurations(self, ticket_id: str) -> list[PSAConfiguration]:
|
||||
raise NotImplementedError("Autotask integration coming soon")
|
||||
|
||||
@@ -70,3 +78,18 @@ class AutotaskProvider(PSAProvider):
|
||||
work_type: str | None = None,
|
||||
) -> PSATimeEntry:
|
||||
raise NotImplementedError("Autotask integration coming soon")
|
||||
|
||||
async def list_resources(self, ticket_id: int) -> list[PSAResource]:
|
||||
raise NotImplementedError("Autotask integration coming soon")
|
||||
|
||||
async def add_resource(self, ticket_id: int, member_id: int) -> PSAResource:
|
||||
raise NotImplementedError("Autotask integration coming soon")
|
||||
|
||||
async def remove_resource(self, ticket_id: int, member_id: int) -> None:
|
||||
raise NotImplementedError("Autotask integration coming soon")
|
||||
|
||||
async def create_ticket(self, payload: TicketCreatePayload) -> PSACreatedTicket:
|
||||
raise NotImplementedError("Autotask integration coming soon")
|
||||
|
||||
async def list_priorities(self) -> list[dict]:
|
||||
raise NotImplementedError("Autotask integration coming soon")
|
||||
|
||||
@@ -12,6 +12,11 @@ from .types import (
|
||||
PSAMember,
|
||||
PSAConfiguration,
|
||||
PSATimeEntry,
|
||||
PSABoard,
|
||||
PaginatedTicketResult,
|
||||
PSAResource,
|
||||
PSACreatedTicket,
|
||||
TicketCreatePayload,
|
||||
)
|
||||
|
||||
|
||||
@@ -27,7 +32,7 @@ class PSAProvider(ABC):
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def search_tickets(self, query: str, **filters) -> list[PSATicket]:
|
||||
async def search_tickets(self, query: str, **filters) -> PaginatedTicketResult:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
@@ -64,6 +69,10 @@ class PSAProvider(ABC):
|
||||
async def list_members(self) -> list[PSAMember]:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def list_boards(self) -> list[PSABoard]:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def get_ticket_configurations(self, ticket_id: str) -> list[PSAConfiguration]:
|
||||
...
|
||||
@@ -78,3 +87,23 @@ class PSAProvider(ABC):
|
||||
work_type: str | None = None,
|
||||
) -> PSATimeEntry:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def list_resources(self, ticket_id: int) -> list[PSAResource]:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def add_resource(self, ticket_id: int, member_id: int) -> PSAResource:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def remove_resource(self, ticket_id: int, member_id: int) -> None:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def create_ticket(self, payload: TicketCreatePayload) -> PSACreatedTicket:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def list_priorities(self) -> list[dict]:
|
||||
...
|
||||
|
||||
@@ -7,6 +7,7 @@ from datetime import datetime, timezone
|
||||
|
||||
from app.services.psa.base import PSAProvider
|
||||
from app.services.psa.cache import psa_cache
|
||||
from app.services.psa.exceptions import PSAError
|
||||
from app.services.psa.types import (
|
||||
ConnectionTestResult,
|
||||
PSATicket,
|
||||
@@ -16,6 +17,11 @@ from app.services.psa.types import (
|
||||
PSAMember,
|
||||
PSAConfiguration,
|
||||
PSATimeEntry,
|
||||
PSABoard,
|
||||
PaginatedTicketResult,
|
||||
PSAResource,
|
||||
PSACreatedTicket,
|
||||
TicketCreatePayload,
|
||||
)
|
||||
from .client import ConnectWiseClient
|
||||
|
||||
@@ -54,34 +60,62 @@ class ConnectWiseProvider(PSAProvider):
|
||||
)
|
||||
return self._map_ticket(data)
|
||||
|
||||
async def search_tickets(self, query: str, **filters) -> list[PSATicket]:
|
||||
"""Search CW tickets by summary. Supports board_id and status_id filters."""
|
||||
async def search_tickets(self, query: str, **filters) -> PaginatedTicketResult:
|
||||
"""Search CW tickets by summary. Supports board_id, status_id, member_identifier,
|
||||
unassigned, board_ids, page, and page_size filters. Returns paginated result."""
|
||||
page_size = filters.get("page_size", 10)
|
||||
page = filters.get("page", 1)
|
||||
|
||||
params: dict = {
|
||||
"fields": "id,summary,company,board,status,priority,closedFlag",
|
||||
"orderBy": "id desc",
|
||||
"pageSize": 25,
|
||||
"orderBy": "priority/sort asc,dateEntered desc",
|
||||
"pageSize": page_size,
|
||||
"page": page,
|
||||
}
|
||||
|
||||
# Build CW condition query
|
||||
conditions: list[str] = []
|
||||
if query:
|
||||
conditions.append(f"summary contains '{query}'")
|
||||
# Sanitize: strip single quotes to prevent CW condition injection
|
||||
safe_query = query.replace("'", "")
|
||||
conditions.append(f"summary contains '{safe_query}'")
|
||||
if filters.get("board_id"):
|
||||
conditions.append(f"board/id = {filters['board_id']}")
|
||||
if filters.get("status_id"):
|
||||
conditions.append(f"status/id = {filters['status_id']}")
|
||||
elif filters.get("status_name"):
|
||||
safe_status = str(filters["status_name"]).replace("'", "")
|
||||
conditions.append(f"status/name = '{safe_status}'")
|
||||
if not filters.get("include_closed", False):
|
||||
conditions.append("closedFlag = false")
|
||||
if filters.get("member_identifier") is not None:
|
||||
conditions.append(f"resources contains '{filters['member_identifier']}'")
|
||||
if filters.get("unassigned", False):
|
||||
conditions.append("resources = null")
|
||||
board_ids: list[int] = filters.get("board_ids") or []
|
||||
if board_ids:
|
||||
board_list = ", ".join(str(bid) for bid in board_ids)
|
||||
conditions.append(f"board/id in ({board_list})")
|
||||
if filters.get("company_id"):
|
||||
conditions.append(f"company/id = {int(filters['company_id'])}")
|
||||
|
||||
if conditions:
|
||||
params["conditions"] = " and ".join(conditions)
|
||||
condition_str = " and ".join(conditions) if conditions else ""
|
||||
if condition_str:
|
||||
params["conditions"] = condition_str
|
||||
|
||||
data = await self.client.get("/service/tickets", params=params)
|
||||
count_params: dict = {}
|
||||
if condition_str:
|
||||
count_params["conditions"] = condition_str
|
||||
|
||||
return [
|
||||
self._map_ticket(t)
|
||||
for t in (data if isinstance(data, list) else [])
|
||||
]
|
||||
# Fire page fetch + count in parallel
|
||||
data, count_data = await asyncio.gather(
|
||||
self.client.get("/service/tickets", params=params),
|
||||
self.client.get("/service/tickets/count", params=count_params),
|
||||
)
|
||||
|
||||
items = [self._map_ticket(t) for t in (data if isinstance(data, list) else [])]
|
||||
total = count_data.get("count", len(items)) if isinstance(count_data, dict) else len(items)
|
||||
|
||||
return PaginatedTicketResult(items=items, total=total, page=page, page_size=page_size)
|
||||
|
||||
async def get_ticket_configurations(
|
||||
self, ticket_id: str
|
||||
@@ -232,13 +266,30 @@ class ConnectWiseProvider(PSAProvider):
|
||||
async def update_ticket_status(
|
||||
self, ticket_id: str, status_id: int
|
||||
) -> PSATicket:
|
||||
"""Update a CW ticket's status using JSON Patch format."""
|
||||
"""Update a CW ticket's status using JSON Patch format.
|
||||
|
||||
Verifies CW actually applied the change — CW silently returns 200 when
|
||||
a status id is invalid for the ticket's board. We check the response
|
||||
body's status.id matches what we sent, and raise PSAError if not.
|
||||
"""
|
||||
patch_body = [
|
||||
{"op": "replace", "path": "status", "value": {"id": status_id}}
|
||||
]
|
||||
data = await self.client.patch(
|
||||
f"/service/tickets/{ticket_id}", json_body=patch_body
|
||||
)
|
||||
applied = (data.get("status") or {}) if isinstance(data, dict) else {}
|
||||
applied_id = applied.get("id")
|
||||
if applied_id != status_id:
|
||||
logger.warning(
|
||||
"CW status PATCH for ticket %s returned status id=%s instead of %s",
|
||||
ticket_id, applied_id, status_id,
|
||||
)
|
||||
raise PSAError(
|
||||
f"ConnectWise did not apply status {status_id} "
|
||||
f"(still {applied.get('name') or applied_id}). "
|
||||
"The status may not be valid for this ticket's board."
|
||||
)
|
||||
return self._map_ticket(data)
|
||||
|
||||
async def list_members(self) -> list[PSAMember]:
|
||||
@@ -270,6 +321,32 @@ class ConnectWiseProvider(PSAProvider):
|
||||
psa_cache.set(cache_key, result, ttl_seconds=900)
|
||||
return result
|
||||
|
||||
async def list_boards(self) -> list[PSABoard]:
|
||||
"""List active CW service boards (cached 1 hour)."""
|
||||
cache_key = "boards"
|
||||
cached = psa_cache.get(cache_key)
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
data = await self.client.get(
|
||||
"/service/boards",
|
||||
params={
|
||||
"fields": "id,name,inactiveFlag",
|
||||
"conditions": "inactiveFlag = false",
|
||||
"pageSize": 100,
|
||||
},
|
||||
)
|
||||
result = [
|
||||
PSABoard(
|
||||
id=b["id"],
|
||||
name=b["name"],
|
||||
inactive=b.get("inactiveFlag", False),
|
||||
)
|
||||
for b in (data if isinstance(data, list) else [])
|
||||
]
|
||||
psa_cache.set(cache_key, result, ttl_seconds=3600)
|
||||
return result
|
||||
|
||||
# ── Ticket Context ────────────────────────────────────────────────
|
||||
|
||||
async def get_ticket_context(
|
||||
@@ -536,7 +613,7 @@ class ConnectWiseProvider(PSAProvider):
|
||||
if work_type:
|
||||
payload["workType"] = {"name": work_type}
|
||||
|
||||
data = await self._client.post("/time/entries", payload)
|
||||
data = await self.client.post("/time/entries", payload)
|
||||
return PSATimeEntry(
|
||||
id=str(data["id"]),
|
||||
ticket_id=ticket_id,
|
||||
@@ -551,16 +628,247 @@ class ConnectWiseProvider(PSAProvider):
|
||||
@staticmethod
|
||||
def _map_ticket(data: dict) -> PSATicket:
|
||||
"""Map a CW ticket JSON dict to a PSATicket."""
|
||||
company = data.get("company") or {}
|
||||
board = data.get("board") or {}
|
||||
status = data.get("status") or {}
|
||||
priority = data.get("priority") or {}
|
||||
return PSATicket(
|
||||
id=str(data["id"]),
|
||||
id=str(data.get("id", "")),
|
||||
summary=data.get("summary", ""),
|
||||
company_name=data.get("company", {}).get("name"),
|
||||
company_id=str(data["company"]["id"]) if data.get("company") else None,
|
||||
board_name=data.get("board", {}).get("name"),
|
||||
board_id=data.get("board", {}).get("id"),
|
||||
status_name=data.get("status", {}).get("name"),
|
||||
status_id=data.get("status", {}).get("id"),
|
||||
priority_name=data.get("priority", {}).get("name"),
|
||||
priority_id=data.get("priority", {}).get("id"),
|
||||
company_name=company.get("name"),
|
||||
company_id=str(company.get("id")) if company.get("id") else None,
|
||||
board_name=board.get("name"),
|
||||
board_id=board.get("id"),
|
||||
status_name=status.get("name"),
|
||||
status_id=status.get("id"),
|
||||
priority_name=priority.get("name"),
|
||||
priority_id=priority.get("id"),
|
||||
closed=data.get("closedFlag", False),
|
||||
)
|
||||
|
||||
# ── Resource management ───────────────────────────────────────────
|
||||
|
||||
# Schedule type id for "Service Ticket" resources — CW's canonical type for ticket co-assignees
|
||||
_SCHEDULE_TYPE_SERVICE_TICKET = 4
|
||||
|
||||
async def _get_ticket_owner(self, ticket_id: int) -> dict | None:
|
||||
"""Fetch the ticket's current owner (MemberReference) or None if unassigned."""
|
||||
data = await self.client.get(
|
||||
f"/service/tickets/{ticket_id}",
|
||||
params={"fields": "id,owner"},
|
||||
)
|
||||
if not isinstance(data, dict):
|
||||
return None
|
||||
owner_raw = data.get("owner")
|
||||
return owner_raw if isinstance(owner_raw, dict) and owner_raw.get("id") else None
|
||||
|
||||
async def _list_ticket_schedule_entries(self, ticket_id: int) -> list[dict]:
|
||||
"""List schedule entries for a ticket's co-assignees.
|
||||
|
||||
Returns raw CW schedule entry dicts with at least id and member info.
|
||||
"""
|
||||
data = await self.client.get(
|
||||
"/schedule/entries",
|
||||
params={
|
||||
"conditions": (
|
||||
f"type/id={self._SCHEDULE_TYPE_SERVICE_TICKET} AND objectId={ticket_id}"
|
||||
),
|
||||
"fields": "id,member,name",
|
||||
"pageSize": 100,
|
||||
},
|
||||
)
|
||||
return data if isinstance(data, list) else []
|
||||
|
||||
async def list_resources(self, ticket_id: int) -> list[PSAResource]:
|
||||
"""List members assigned to a CW ticket.
|
||||
|
||||
Merges the `owner` MemberReference (primary assignee) with schedule entries
|
||||
of type 4 (Service Ticket resources — co-assignees). Deduped by member id.
|
||||
"""
|
||||
owner = await self._get_ticket_owner(ticket_id)
|
||||
entries = await self._list_ticket_schedule_entries(ticket_id)
|
||||
members = await self.list_members()
|
||||
by_id = {str(m.id): m for m in members}
|
||||
|
||||
seen_ids: set[str] = set()
|
||||
results: list[PSAResource] = []
|
||||
|
||||
if owner is not None:
|
||||
owner_id = str(owner.get("id"))
|
||||
m = by_id.get(owner_id)
|
||||
if m:
|
||||
results.append(PSAResource(
|
||||
member_id=int(m.id),
|
||||
member_name=m.name,
|
||||
member_identifier=m.identifier,
|
||||
))
|
||||
else:
|
||||
results.append(PSAResource(
|
||||
member_id=int(owner.get("id") or 0),
|
||||
member_name=str(owner.get("name") or ""),
|
||||
member_identifier=str(owner.get("identifier") or ""),
|
||||
))
|
||||
seen_ids.add(owner_id)
|
||||
|
||||
for entry in entries:
|
||||
entry_member = entry.get("member") if isinstance(entry, dict) else None
|
||||
if not isinstance(entry_member, dict):
|
||||
continue
|
||||
mid = str(entry_member.get("id") or "")
|
||||
if not mid or mid in seen_ids:
|
||||
continue
|
||||
m = by_id.get(mid)
|
||||
if m:
|
||||
results.append(PSAResource(
|
||||
member_id=int(m.id),
|
||||
member_name=m.name,
|
||||
member_identifier=m.identifier,
|
||||
))
|
||||
else:
|
||||
results.append(PSAResource(
|
||||
member_id=int(entry_member.get("id") or 0),
|
||||
member_name=str(entry_member.get("name") or ""),
|
||||
member_identifier=str(entry_member.get("identifier") or ""),
|
||||
))
|
||||
seen_ids.add(mid)
|
||||
|
||||
return results
|
||||
|
||||
async def add_resource(self, ticket_id: int, member_id: int) -> PSAResource:
|
||||
"""Assign a member to a CW ticket.
|
||||
|
||||
- If the ticket has no owner, set the target as `owner` (CW's canonical
|
||||
primary assignee field). CW typically mirrors this into the derived
|
||||
`resources` string automatically.
|
||||
- If the ticket is already owned by someone else, add the target as a
|
||||
co-assignee via a schedule entry of type 4 (Service Ticket). The
|
||||
existing owner is not changed.
|
||||
- Idempotent when target is already owner or already has a schedule entry.
|
||||
"""
|
||||
members = await self.list_members()
|
||||
target = next((m for m in members if str(m.id) == str(member_id)), None)
|
||||
if target is None:
|
||||
raise PSAError(f"Member {member_id} not found")
|
||||
|
||||
current_owner = await self._get_ticket_owner(ticket_id)
|
||||
|
||||
if current_owner is None:
|
||||
# Primary assign — set owner
|
||||
await self.client.patch(
|
||||
f"/service/tickets/{ticket_id}",
|
||||
json_body=[{"op": "replace", "path": "owner", "value": {"id": int(target.id)}}],
|
||||
)
|
||||
elif str(current_owner.get("id")) != str(target.id):
|
||||
# Ticket owned by someone else — add as co-assignee via schedule entry.
|
||||
# Idempotent: skip if a schedule entry already exists for this member.
|
||||
existing = await self._list_ticket_schedule_entries(ticket_id)
|
||||
already_assigned = any(
|
||||
str((e.get("member") or {}).get("id") or "") == str(target.id)
|
||||
for e in existing
|
||||
)
|
||||
if not already_assigned:
|
||||
await self.client.post(
|
||||
"/schedule/entries",
|
||||
json_body={
|
||||
"member": {"id": int(target.id)},
|
||||
"objectId": int(ticket_id),
|
||||
"type": {"id": self._SCHEDULE_TYPE_SERVICE_TICKET},
|
||||
"name": target.name or target.identifier or f"Member {target.id}",
|
||||
},
|
||||
)
|
||||
# else: already the owner — idempotent no-op
|
||||
|
||||
return PSAResource(
|
||||
member_id=int(target.id),
|
||||
member_name=target.name,
|
||||
member_identifier=target.identifier,
|
||||
)
|
||||
|
||||
async def remove_resource(self, ticket_id: int, member_id: int) -> None:
|
||||
"""Remove a member from a CW ticket (idempotent).
|
||||
|
||||
- If the target is the current owner, clear the owner field.
|
||||
- Otherwise, delete their schedule entry (Service Ticket type).
|
||||
"""
|
||||
members = await self.list_members()
|
||||
target = next((m for m in members if str(m.id) == str(member_id)), None)
|
||||
if target is None:
|
||||
return
|
||||
|
||||
current_owner = await self._get_ticket_owner(ticket_id)
|
||||
|
||||
if current_owner is not None and str(current_owner.get("id")) == str(target.id):
|
||||
# Unassign the owner. Try RFC 6902 "remove" first; fall back to
|
||||
# "replace" with null if CW rejects it.
|
||||
try:
|
||||
await self.client.patch(
|
||||
f"/service/tickets/{ticket_id}",
|
||||
json_body=[{"op": "remove", "path": "owner"}],
|
||||
)
|
||||
except PSAError:
|
||||
await self.client.patch(
|
||||
f"/service/tickets/{ticket_id}",
|
||||
json_body=[{"op": "replace", "path": "owner", "value": None}],
|
||||
)
|
||||
return
|
||||
|
||||
# Not the owner — find and delete the schedule entry for this member.
|
||||
entries = await self._list_ticket_schedule_entries(ticket_id)
|
||||
for entry in entries:
|
||||
entry_member = entry.get("member") if isinstance(entry, dict) else None
|
||||
if isinstance(entry_member, dict) and str(entry_member.get("id") or "") == str(target.id):
|
||||
entry_id = entry.get("id")
|
||||
if entry_id:
|
||||
await self.client.delete(f"/schedule/entries/{entry_id}")
|
||||
break
|
||||
|
||||
# ── Ticket creation ───────────────────────────────────────────────
|
||||
|
||||
async def create_ticket(self, payload: TicketCreatePayload) -> PSACreatedTicket:
|
||||
"""Create a new CW service ticket."""
|
||||
body: dict = {
|
||||
"summary": payload.summary,
|
||||
"board": {"id": payload.board_id},
|
||||
"company": {"id": payload.company_id},
|
||||
"status": {"id": payload.status_id},
|
||||
"priority": {"id": payload.priority_id},
|
||||
}
|
||||
if payload.description:
|
||||
body["initialDescription"] = payload.description
|
||||
if payload.assigned_member_id:
|
||||
body["owner"] = {"id": payload.assigned_member_id}
|
||||
|
||||
data = await self.client.post("/service/tickets", json_body=body)
|
||||
|
||||
ticket_id = data.get("id") if isinstance(data, dict) else None
|
||||
resources: list[PSAResource] = []
|
||||
if ticket_id and payload.assigned_member_id:
|
||||
try:
|
||||
resources = await self.list_resources(ticket_id)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
company = (data.get("company") or {}) if isinstance(data, dict) else {}
|
||||
board = (data.get("board") or {}) if isinstance(data, dict) else {}
|
||||
status = (data.get("status") or {}) if isinstance(data, dict) else {}
|
||||
priority = (data.get("priority") or {}) if isinstance(data, dict) else {}
|
||||
|
||||
return PSACreatedTicket(
|
||||
id=ticket_id or 0,
|
||||
summary=data.get("summary", payload.summary) if isinstance(data, dict) else payload.summary,
|
||||
board_name=board.get("name", ""),
|
||||
status_name=status.get("name", ""),
|
||||
priority_name=priority.get("name", ""),
|
||||
company_name=company.get("name", ""),
|
||||
resources=resources,
|
||||
)
|
||||
|
||||
# ── Priorities ────────────────────────────────────────────────────
|
||||
|
||||
async def list_priorities(self) -> list[dict]:
|
||||
"""List CW service priorities."""
|
||||
data = await self.client.get("/service/priorities", params={"pageSize": 50})
|
||||
return [
|
||||
{"id": p.get("id"), "name": p.get("name")}
|
||||
for p in (data if isinstance(data, list) else [])
|
||||
]
|
||||
|
||||
@@ -11,6 +11,11 @@ from app.services.psa.types import (
|
||||
PSAMember,
|
||||
PSAConfiguration,
|
||||
PSATimeEntry,
|
||||
PSABoard,
|
||||
PaginatedTicketResult,
|
||||
PSAResource,
|
||||
PSACreatedTicket,
|
||||
TicketCreatePayload,
|
||||
)
|
||||
|
||||
|
||||
@@ -27,7 +32,7 @@ class HaloPSAProvider(PSAProvider):
|
||||
async def get_ticket(self, ticket_id: str) -> PSATicket:
|
||||
raise NotImplementedError("Halo PSA integration coming soon")
|
||||
|
||||
async def search_tickets(self, query: str, **filters) -> list[PSATicket]:
|
||||
async def search_tickets(self, query: str, **filters) -> PaginatedTicketResult:
|
||||
raise NotImplementedError("Halo PSA integration coming soon")
|
||||
|
||||
async def post_note(
|
||||
@@ -58,6 +63,9 @@ class HaloPSAProvider(PSAProvider):
|
||||
async def list_members(self) -> list[PSAMember]:
|
||||
raise NotImplementedError("Halo PSA integration coming soon")
|
||||
|
||||
async def list_boards(self) -> list[PSABoard]:
|
||||
raise NotImplementedError("list_boards not implemented for this provider")
|
||||
|
||||
async def get_ticket_configurations(self, ticket_id: str) -> list[PSAConfiguration]:
|
||||
raise NotImplementedError("Halo PSA integration coming soon")
|
||||
|
||||
@@ -70,3 +78,18 @@ class HaloPSAProvider(PSAProvider):
|
||||
work_type: str | None = None,
|
||||
) -> PSATimeEntry:
|
||||
raise NotImplementedError("Halo PSA integration coming soon")
|
||||
|
||||
async def list_resources(self, ticket_id: int) -> list[PSAResource]:
|
||||
raise NotImplementedError("Halo PSA integration coming soon")
|
||||
|
||||
async def add_resource(self, ticket_id: int, member_id: int) -> PSAResource:
|
||||
raise NotImplementedError("Halo PSA integration coming soon")
|
||||
|
||||
async def remove_resource(self, ticket_id: int, member_id: int) -> None:
|
||||
raise NotImplementedError("Halo PSA integration coming soon")
|
||||
|
||||
async def create_ticket(self, payload: TicketCreatePayload) -> PSACreatedTicket:
|
||||
raise NotImplementedError("Halo PSA integration coming soon")
|
||||
|
||||
async def list_priorities(self) -> list[dict]:
|
||||
raise NotImplementedError("Halo PSA integration coming soon")
|
||||
|
||||
@@ -67,6 +67,46 @@ class PSATimeEntry(BaseModel):
|
||||
created_at: str | None = None
|
||||
|
||||
|
||||
class PSABoard(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
inactive: bool = False
|
||||
|
||||
|
||||
class PaginatedTicketResult(BaseModel):
|
||||
items: list[PSATicket]
|
||||
total: int
|
||||
page: int
|
||||
page_size: int
|
||||
|
||||
|
||||
class PSAResource(BaseModel):
|
||||
member_id: int
|
||||
member_name: str
|
||||
member_identifier: str
|
||||
is_rf_user: bool = False
|
||||
|
||||
|
||||
class PSACreatedTicket(BaseModel):
|
||||
id: int
|
||||
summary: str
|
||||
board_name: str
|
||||
status_name: str
|
||||
priority_name: str
|
||||
company_name: str
|
||||
resources: list[PSAResource] = []
|
||||
|
||||
|
||||
class TicketCreatePayload(BaseModel):
|
||||
summary: str
|
||||
company_id: int
|
||||
board_id: int
|
||||
status_id: int
|
||||
priority_id: int
|
||||
description: str | None = None
|
||||
assigned_member_id: int | None = None
|
||||
|
||||
|
||||
class NoteType:
|
||||
INTERNAL_ANALYSIS = "internal_analysis"
|
||||
RESOLUTION = "resolution"
|
||||
|
||||
223
backend/app/services/psa_writeback_service.py
Normal file
223
backend/app/services/psa_writeback_service.py
Normal file
@@ -0,0 +1,223 @@
|
||||
"""PSA writeback for FlowPilot Phase 4 — Resolve + Escalate round-trip.
|
||||
|
||||
Three primitives:
|
||||
|
||||
- `post_resolution_note` — post the engineer-edited resolution markdown to
|
||||
the PSA ticket, store `{external_id, posted_at}` on the session.
|
||||
- `post_escalation_package` — same pattern for the Escalate flow.
|
||||
- `transition_ticket_status` — patch the ticket status, then re-fetch and
|
||||
verify the change actually took. Failed verification raises loudly so the
|
||||
UI never reports silent success (per the existing ConnectWise integration
|
||||
principle called out in FLOWPILOT-MIGRATION.md Section 6.5 and CLAUDE.md).
|
||||
|
||||
The target status IDs live in `account_settings.preferences`
|
||||
(`cw_resolved_status_id`, `cw_escalated_status_id`). When unset, the status
|
||||
transition is a no-op and the endpoint response says so — we do not guess a
|
||||
default because CW status IDs are board-specific.
|
||||
|
||||
Local-only path: callers handle sessions without `psa_ticket_id` before
|
||||
calling this service. Nothing here tries to "post locally" — the service's
|
||||
job ends at the PSA boundary.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.account_settings import AccountSettings
|
||||
from app.models.ai_session import AISession
|
||||
from app.services.psa.exceptions import PSAConnectionError
|
||||
from app.services.psa.registry import get_provider_for_connection
|
||||
from app.services.psa.types import NoteType
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PSAStatusVerificationError(RuntimeError):
|
||||
"""Raised when a ticket status transition didn't stick on re-fetch.
|
||||
|
||||
The `update_ticket_status` call returned OK but the subsequent
|
||||
`get_ticket` still shows the prior status (or some unrelated one).
|
||||
This is the exact failure mode CLAUDE.md flags as a ConnectWise
|
||||
anti-pattern: reporting success when nothing changed.
|
||||
"""
|
||||
|
||||
def __init__(self, ticket_id: str, expected_status_id: int, observed_status: Any) -> None:
|
||||
super().__init__(
|
||||
f"Ticket {ticket_id} status transition to {expected_status_id} "
|
||||
f"did not verify — observed {observed_status!r} after re-fetch."
|
||||
)
|
||||
self.ticket_id = ticket_id
|
||||
self.expected_status_id = expected_status_id
|
||||
self.observed_status = observed_status
|
||||
|
||||
|
||||
class PSAWritebackService:
|
||||
"""Thin orchestration over the PSA provider for FlowPilot writebacks.
|
||||
|
||||
Instances are per-request — the AsyncSession is the one handling the
|
||||
current HTTP call, and the provider is resolved lazily from the session's
|
||||
`psa_connection_id`.
|
||||
"""
|
||||
|
||||
def __init__(self, db: AsyncSession) -> None:
|
||||
self.db = db
|
||||
|
||||
# ── Public API ────────────────────────────────────────────────────────
|
||||
|
||||
async def post_resolution_note(
|
||||
self, session: AISession, markdown: str
|
||||
) -> dict[str, Any]:
|
||||
"""Post `markdown` as a resolution note on the linked CW ticket.
|
||||
|
||||
On success, persists `resolution_note_markdown`, `_posted_at`,
|
||||
`_external_id` on the session and returns the same triple. Caller is
|
||||
responsible for committing the transaction.
|
||||
"""
|
||||
return await self._post_note(
|
||||
session=session,
|
||||
markdown=markdown,
|
||||
note_type=NoteType.RESOLUTION,
|
||||
markdown_col="resolution_note_markdown",
|
||||
posted_at_col="resolution_note_posted_at",
|
||||
external_id_col="resolution_note_external_id",
|
||||
kind="resolution",
|
||||
)
|
||||
|
||||
async def post_escalation_package(
|
||||
self, session: AISession, markdown: str
|
||||
) -> dict[str, Any]:
|
||||
"""Post `markdown` as an escalation handoff note on the CW ticket."""
|
||||
return await self._post_note(
|
||||
session=session,
|
||||
markdown=markdown,
|
||||
# Internal-analysis visibility: the handoff is for the next engineer,
|
||||
# not the customer. CW fires no notifications, keeps the note internal.
|
||||
note_type=NoteType.INTERNAL_ANALYSIS,
|
||||
markdown_col="escalation_package_markdown",
|
||||
posted_at_col="escalation_package_posted_at",
|
||||
external_id_col="escalation_package_external_id",
|
||||
kind="escalation",
|
||||
)
|
||||
|
||||
async def transition_ticket_status(
|
||||
self,
|
||||
session: AISession,
|
||||
target_status_id: int,
|
||||
) -> dict[str, Any]:
|
||||
"""PATCH ticket status, then re-fetch and verify.
|
||||
|
||||
Returns `{"success": True, "verified_status_id": <int>, "verified_status_name": <str>}`
|
||||
when the observed status matches. Raises `PSAStatusVerificationError`
|
||||
when the transition didn't take (most common real-world failure: CW
|
||||
requires certain fields before allowing a status change to
|
||||
Resolved — the PATCH returns 200 but the status silently stays put).
|
||||
"""
|
||||
if not session.psa_ticket_id or not session.psa_connection_id:
|
||||
raise ValueError("Session has no linked PSA ticket for status transition")
|
||||
|
||||
provider = await get_provider_for_connection(session.psa_connection_id, self.db)
|
||||
await provider.update_ticket_status(
|
||||
ticket_id=session.psa_ticket_id, status_id=target_status_id,
|
||||
)
|
||||
|
||||
# Verify by re-fetch — this is the load-bearing step.
|
||||
verification = await provider.get_ticket(session.psa_ticket_id)
|
||||
observed_id = getattr(verification, "status_id", None)
|
||||
observed_name = getattr(verification, "status_name", None)
|
||||
if observed_id != target_status_id:
|
||||
raise PSAStatusVerificationError(
|
||||
ticket_id=session.psa_ticket_id,
|
||||
expected_status_id=target_status_id,
|
||||
observed_status={"id": observed_id, "name": observed_name},
|
||||
)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"verified_status_id": observed_id,
|
||||
"verified_status_name": observed_name,
|
||||
}
|
||||
|
||||
async def resolved_status_id_for_account(
|
||||
self, account_id: UUID
|
||||
) -> int | None:
|
||||
"""Return the configured CW "Resolved" status ID for the account.
|
||||
|
||||
None means "no transition configured" — callers should skip the
|
||||
transition (posting the note is still meaningful). This lives in
|
||||
account_settings.preferences per the Phase 1 JSONB grab-bag design.
|
||||
"""
|
||||
raw = await AccountSettings.get_setting(self.db, account_id, "cw_resolved_status_id", None)
|
||||
return self._coerce_status_id(raw)
|
||||
|
||||
async def escalated_status_id_for_account(
|
||||
self, account_id: UUID
|
||||
) -> int | None:
|
||||
raw = await AccountSettings.get_setting(self.db, account_id, "cw_escalated_status_id", None)
|
||||
return self._coerce_status_id(raw)
|
||||
|
||||
# ── Internals ─────────────────────────────────────────────────────────
|
||||
|
||||
async def _post_note(
|
||||
self,
|
||||
*,
|
||||
session: AISession,
|
||||
markdown: str,
|
||||
note_type: str,
|
||||
markdown_col: str,
|
||||
posted_at_col: str,
|
||||
external_id_col: str,
|
||||
kind: str,
|
||||
) -> dict[str, Any]:
|
||||
if not session.psa_ticket_id or not session.psa_connection_id:
|
||||
raise ValueError(f"Session has no linked PSA ticket for {kind} post")
|
||||
|
||||
markdown = (markdown or "").strip()
|
||||
if not markdown:
|
||||
raise ValueError(f"{kind} markdown is empty")
|
||||
|
||||
try:
|
||||
provider = await get_provider_for_connection(session.psa_connection_id, self.db)
|
||||
except PSAConnectionError:
|
||||
# Connection could have been deleted or deactivated since session
|
||||
# creation — propagate as a clear error for the endpoint to surface.
|
||||
logger.exception(
|
||||
"PSA connection %s is no longer available for session %s",
|
||||
session.psa_connection_id, session.id,
|
||||
)
|
||||
raise
|
||||
|
||||
posted = await provider.post_note(
|
||||
ticket_id=session.psa_ticket_id,
|
||||
text=markdown,
|
||||
note_type=note_type,
|
||||
)
|
||||
|
||||
posted_at = datetime.now(timezone.utc)
|
||||
setattr(session, markdown_col, markdown)
|
||||
setattr(session, posted_at_col, posted_at)
|
||||
setattr(session, external_id_col, str(posted.id) if posted.id else None)
|
||||
|
||||
return {
|
||||
"external_id": str(posted.id) if posted.id else None,
|
||||
"posted_at": posted_at,
|
||||
"kind": kind,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _coerce_status_id(raw: Any) -> int | None:
|
||||
if raw is None:
|
||||
return None
|
||||
try:
|
||||
return int(raw)
|
||||
except (TypeError, ValueError):
|
||||
logger.warning(
|
||||
"Non-integer CW status ID in account_settings.preferences: %r",
|
||||
raw,
|
||||
)
|
||||
return None
|
||||
342
backend/app/services/resolution_note_generator.py
Normal file
342
backend/app/services/resolution_note_generator.py
Normal file
@@ -0,0 +1,342 @@
|
||||
"""ResolutionNoteGeneratorService — drafts the structured Resolve note for a session.
|
||||
|
||||
Produces the four-section markdown that ships to the customer ticket (per
|
||||
FLOWPILOT-MIGRATION.md Section 6.2):
|
||||
|
||||
## Problem
|
||||
## What we confirmed
|
||||
## Root cause
|
||||
## Resolution
|
||||
|
||||
The output is the *draft* — engineers review and edit in the preview popover
|
||||
before clicking Confirm & post (Phase 4). Caching is keyed on
|
||||
`(session_id, ai_sessions.state_version)` per Section 5.5; the cache lives in
|
||||
`preview_cache` and invalidates automatically when any fact / suggested fix /
|
||||
script generation bumps the session's state_version.
|
||||
|
||||
Model: Sonnet (`resolution_note` action tier — quality matters because the
|
||||
output is customer-facing). MCP intentionally disabled — this is a summary
|
||||
of existing state, not a research task.
|
||||
|
||||
Sensitive parameter values in script_generations are redacted using the
|
||||
script template's `parameters_schema` (`field_type: "password"`). Existing
|
||||
ScriptTemplateEngine.redact_sensitive handles the substitution.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.ai_provider import get_ai_provider
|
||||
from app.core.config import settings
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.script_template import ScriptGeneration, ScriptTemplate
|
||||
from app.models.session_fact import SessionFact
|
||||
from app.models.session_suggested_fix import SessionSuggestedFix
|
||||
from app.services.preview_cache import preview_cache
|
||||
from app.services.script_template_engine import ScriptTemplateEngine
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
_RESOLUTION_NOTE_SYSTEM_PROMPT = """\
|
||||
You produce structured resolution notes for an MSP troubleshooting platform. \
|
||||
The notes are posted as ticket notes in the customer's PSA, so they must read \
|
||||
like a competent senior engineer summarized the work — not like an AI \
|
||||
narration. Your output goes in front of paying customers.
|
||||
|
||||
Output exactly this markdown structure, no preamble, no closing remarks, no \
|
||||
extra headings:
|
||||
|
||||
## Problem
|
||||
<one short paragraph stating the issue the engineer worked on, derived from the \
|
||||
session's intake/title and the incident header. Past tense. No "user reported" \
|
||||
hedging — state the problem directly.>
|
||||
|
||||
## What we confirmed
|
||||
<bulleted list of facts from the "What we know" section, each one a short line. \
|
||||
Group similar facts together; do not invent connecting prose. If there are no \
|
||||
facts, write "Nothing was confirmed." and skip to Root cause.>
|
||||
|
||||
## Root cause
|
||||
<one short paragraph naming the root cause based on the active suggested fix \
|
||||
and confirmed facts. If the suggested fix is low-confidence (<60%) or absent, \
|
||||
say "Root cause not definitively isolated." and explain what is suspected based \
|
||||
on facts.>
|
||||
|
||||
## Resolution
|
||||
<The content of this section depends on the outcome recorded for the active \
|
||||
suggested fix, as given in the input bundle under "fix.status":>
|
||||
|
||||
- applied_success: Write in past tense using closure language. State that the \
|
||||
fix was applied and verified as working. If verified_at is provided, you may \
|
||||
reference it as the time resolution was confirmed. Example phrasing: \
|
||||
"Applied <fix title>; confirmed working."
|
||||
- applied_failed: Acknowledge that the proposed fix did not resolve the issue \
|
||||
and was discarded. If failure_reason is provided, include it. Then describe \
|
||||
the actual resolution path taken (derived from facts and scripts run). This \
|
||||
state means the engineer resolved the issue another way; the note should cover \
|
||||
that actual resolution, not just the failed attempt.
|
||||
- applied_partial: Note that the fix was partially applied. If partial_notes \
|
||||
are provided, include them. Then describe the final resolution path taken.
|
||||
- dismissed: Treat the fix as considered and set aside. Do not center the note \
|
||||
on it. Describe the resolution based on what was actually confirmed and done.
|
||||
- proposed (no outcome yet): Write "Resolution not yet applied — fix proposed: \
|
||||
<fix title>." Pull verbatim script names and template references when available.
|
||||
|
||||
Strict rules:
|
||||
- Use ONLY the facts and state I provide. Never invent specifics that are not \
|
||||
in the input.
|
||||
- Do not include placeholder text like "TBD", "TODO", or empty bullets.
|
||||
- Do not include the engineer's name, the AI's name, internal session IDs, or \
|
||||
the session's chat transcript.
|
||||
- Markdown headings exactly as shown (## level), no bolding the headings.
|
||||
- No trailing whitespace, no double-blank lines, no horizontal rules.
|
||||
"""
|
||||
|
||||
|
||||
class ResolutionNoteGeneratorService:
|
||||
"""Generates and caches the four-section Resolve note markdown."""
|
||||
|
||||
KIND = "resolution_note"
|
||||
|
||||
def __init__(self, db: AsyncSession) -> None:
|
||||
self.db = db
|
||||
|
||||
async def generate_or_get_cached(
|
||||
self, session_id: UUID, *, force: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""Return the preview for the session.
|
||||
|
||||
Reads `(KIND, session_id, state_version)` from the in-process cache;
|
||||
on miss, generates fresh markdown and stores under the same key.
|
||||
`force=True` bypasses the cache and refreshes the cached entry.
|
||||
|
||||
Returns `{"markdown": str, "target_ticket_ref": str | None,
|
||||
"state_version": int, "from_cache": bool}`.
|
||||
"""
|
||||
session = await self._load_session(session_id)
|
||||
cached = preview_cache.get(self.KIND, session.id, session.state_version) if not force else None
|
||||
if cached is not None:
|
||||
return {**cached, "from_cache": True}
|
||||
|
||||
markdown = await self._render(session)
|
||||
target = self._target_ticket_ref(session)
|
||||
payload = {
|
||||
"markdown": markdown,
|
||||
"target_ticket_ref": target,
|
||||
"state_version": session.state_version,
|
||||
}
|
||||
preview_cache.set(self.KIND, session.id, session.state_version, payload)
|
||||
return {**payload, "from_cache": False}
|
||||
|
||||
# ── Internals ─────────────────────────────────────────────────────────
|
||||
|
||||
async def _load_session(self, session_id: UUID) -> AISession:
|
||||
result = await self.db.execute(
|
||||
select(AISession).where(AISession.id == session_id)
|
||||
)
|
||||
session = result.scalar_one_or_none()
|
||||
if session is None:
|
||||
raise ValueError(f"Session {session_id} not found")
|
||||
return session
|
||||
|
||||
async def _render(self, session: AISession) -> str:
|
||||
"""Build the prompt input bundle, call the model, return markdown."""
|
||||
facts = await self._load_facts(session.id)
|
||||
active_fix = await self._load_active_fix(session.id)
|
||||
gens = await self._load_redacted_generations(session.id)
|
||||
|
||||
bundle = self._build_input_bundle(session, facts, active_fix, gens)
|
||||
|
||||
model = settings.get_model_for_action("resolution_note")
|
||||
provider = get_ai_provider(model=model)
|
||||
|
||||
# Cache the system prompt — identical across every preview call for
|
||||
# every session. Per-session bundle is in the user message, uncached.
|
||||
system_blocks: list[dict[str, Any]] = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": _RESOLUTION_NOTE_SYSTEM_PROMPT,
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
# cacheable: identical across every resolution-note preview call
|
||||
},
|
||||
]
|
||||
|
||||
try:
|
||||
text, _in, _out = await provider.generate_text(
|
||||
system_prompt=system_blocks,
|
||||
messages=[{"role": "user", "content": bundle}],
|
||||
max_tokens=1200,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Resolution note generation failed for session %s", session.id)
|
||||
raise
|
||||
return text.strip()
|
||||
|
||||
async def _load_facts(self, session_id: UUID) -> list[SessionFact]:
|
||||
result = await self.db.execute(
|
||||
select(SessionFact)
|
||||
.where(
|
||||
SessionFact.session_id == session_id,
|
||||
SessionFact.deleted_at.is_(None),
|
||||
)
|
||||
.order_by(SessionFact.created_at.asc())
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def _load_active_fix(self, session_id: UUID) -> SessionSuggestedFix | None:
|
||||
result = await self.db.execute(
|
||||
select(SessionSuggestedFix)
|
||||
.where(
|
||||
SessionSuggestedFix.session_id == session_id,
|
||||
SessionSuggestedFix.superseded_at.is_(None),
|
||||
)
|
||||
.order_by(SessionSuggestedFix.created_at.desc())
|
||||
)
|
||||
return result.scalars().first()
|
||||
|
||||
async def _load_redacted_generations(
|
||||
self, session_id: UUID
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Pull script_generations for the session, redacting password params.
|
||||
|
||||
Password fields are inferred from the linked template's
|
||||
`parameters_schema` (`field_type: "password"`). The existing
|
||||
ScriptTemplateEngine.redact_sensitive handles the substitution.
|
||||
"""
|
||||
result = await self.db.execute(
|
||||
select(ScriptGeneration)
|
||||
.where(ScriptGeneration.ai_session_id == session_id)
|
||||
.order_by(ScriptGeneration.created_at.asc())
|
||||
)
|
||||
gens = list(result.scalars().all())
|
||||
if not gens:
|
||||
return []
|
||||
|
||||
template_ids = {g.template_id for g in gens}
|
||||
tpl_result = await self.db.execute(
|
||||
select(ScriptTemplate).where(ScriptTemplate.id.in_(template_ids))
|
||||
)
|
||||
templates_by_id = {t.id: t for t in tpl_result.scalars().all()}
|
||||
|
||||
engine = ScriptTemplateEngine()
|
||||
out: list[dict[str, Any]] = []
|
||||
for g in gens:
|
||||
tpl = templates_by_id.get(g.template_id)
|
||||
sensitive_keys = self._sensitive_keys_from_schema(
|
||||
(tpl.parameters_schema if tpl else {}) or {}
|
||||
)
|
||||
redacted_params = engine.redact_sensitive(g.parameters_used or {}, sensitive_keys)
|
||||
out.append({
|
||||
"template_name": tpl.name if tpl else "(unknown template)",
|
||||
"template_slug": tpl.slug if tpl else None,
|
||||
"parameters_used": redacted_params,
|
||||
"created_at": g.created_at.isoformat(),
|
||||
})
|
||||
return out
|
||||
|
||||
@staticmethod
|
||||
def _sensitive_keys_from_schema(schema: dict[str, Any]) -> set[str]:
|
||||
"""Extract password-typed parameter keys from a template's schema.
|
||||
|
||||
The schema shape is `{"parameters": [{"key": "...", "field_type": "password", ...}]}`
|
||||
per the existing Script Generator convention. Tolerate both that shape
|
||||
and the simpler `{"key": {"field_type": "password"}}` form.
|
||||
"""
|
||||
keys: set[str] = set()
|
||||
params = schema.get("parameters") if isinstance(schema, dict) else None
|
||||
if isinstance(params, list):
|
||||
for p in params:
|
||||
if isinstance(p, dict) and p.get("field_type") == "password":
|
||||
k = p.get("key") or p.get("variable_name")
|
||||
if isinstance(k, str):
|
||||
keys.add(k)
|
||||
elif isinstance(schema, dict):
|
||||
for k, v in schema.items():
|
||||
if isinstance(v, dict) and v.get("field_type") == "password":
|
||||
keys.add(k)
|
||||
return keys
|
||||
|
||||
@staticmethod
|
||||
def _target_ticket_ref(session: AISession) -> str | None:
|
||||
"""Display ref for the linked PSA ticket, e.g. 'CW #48291'.
|
||||
|
||||
ConnectWise is the only PSA wired today (per the Phase 1 constraint),
|
||||
so a CW prefix is reasonable. Other PSAs will need provider-aware
|
||||
formatting in Phase 4.
|
||||
"""
|
||||
if not session.psa_ticket_id:
|
||||
return None
|
||||
return f"CW #{session.psa_ticket_id}"
|
||||
|
||||
@staticmethod
|
||||
def _build_input_bundle(
|
||||
session: AISession,
|
||||
facts: list[SessionFact],
|
||||
active_fix: SessionSuggestedFix | None,
|
||||
generations: list[dict[str, Any]],
|
||||
) -> str:
|
||||
"""Compose the structured input the LLM sees for one preview call."""
|
||||
lines: list[str] = []
|
||||
lines.append("# Session context")
|
||||
lines.append(f"Title: {session.title or '(untitled)'}")
|
||||
if session.problem_summary:
|
||||
lines.append(f"Problem summary: {session.problem_summary}")
|
||||
if session.problem_domain:
|
||||
lines.append(f"Domain: {session.problem_domain}")
|
||||
intake_text = (session.intake_content or {}).get("text") if isinstance(session.intake_content, dict) else None
|
||||
if intake_text:
|
||||
lines.append(f"Intake message: {intake_text}")
|
||||
if session.psa_ticket_id:
|
||||
lines.append(f"Linked PSA ticket: CW #{session.psa_ticket_id}")
|
||||
|
||||
lines.append("")
|
||||
lines.append("# Confirmed facts (What we know)")
|
||||
if not facts:
|
||||
lines.append("(none)")
|
||||
else:
|
||||
for f in facts:
|
||||
tag = f.source_type
|
||||
summary = f" — {f.source_summary}" if f.source_summary else ""
|
||||
lines.append(f"- [{tag}] {f.text}{summary}")
|
||||
|
||||
lines.append("")
|
||||
lines.append("# Active suggested fix")
|
||||
if active_fix is None:
|
||||
lines.append("(no active suggested fix)")
|
||||
else:
|
||||
lines.append(f"Title: {active_fix.title}")
|
||||
lines.append(f"Confidence: {active_fix.confidence_pct}%")
|
||||
lines.append(f"Description: {active_fix.description}")
|
||||
if active_fix.user_decision:
|
||||
lines.append(f"Engineer decision: {active_fix.user_decision}")
|
||||
lines.append(f"Outcome status: {active_fix.status}")
|
||||
if active_fix.applied_at:
|
||||
lines.append(f"Applied at: {active_fix.applied_at.isoformat()}")
|
||||
if active_fix.verified_at:
|
||||
lines.append(f"Verified at: {active_fix.verified_at.isoformat()}")
|
||||
if active_fix.partial_notes:
|
||||
lines.append(f"Partial notes: {active_fix.partial_notes}")
|
||||
if active_fix.failure_reason:
|
||||
lines.append(f"Failure reason: {active_fix.failure_reason}")
|
||||
|
||||
lines.append("")
|
||||
lines.append("# Scripts run during the session (passwords redacted)")
|
||||
if not generations:
|
||||
lines.append("(none)")
|
||||
else:
|
||||
for g in generations:
|
||||
lines.append(f"- {g['template_name']} (slug={g['template_slug']})")
|
||||
if g["parameters_used"]:
|
||||
lines.append(f" parameters: {g['parameters_used']}")
|
||||
|
||||
lines.append("")
|
||||
lines.append(
|
||||
"Produce the four-section resolution note now. Use only the input above."
|
||||
)
|
||||
return "\n".join(lines)
|
||||
@@ -5,6 +5,7 @@ from uuid import UUID
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.session_resolution_output import SessionResolutionOutput
|
||||
@@ -21,7 +22,9 @@ class ResolutionOutputGenerator:
|
||||
|
||||
async def generate_all(self, session_id: UUID) -> list[SessionResolutionOutput]:
|
||||
result = await self.db.execute(
|
||||
select(AISession).where(AISession.id == session_id)
|
||||
select(AISession)
|
||||
.options(selectinload(AISession.steps))
|
||||
.where(AISession.id == session_id)
|
||||
)
|
||||
session = result.scalar_one_or_none()
|
||||
if not session:
|
||||
|
||||
@@ -148,6 +148,8 @@ async def create_session(
|
||||
team_id: UUID | None,
|
||||
language: str,
|
||||
initial_prompt: str | None = None,
|
||||
origin: str = "standalone",
|
||||
ai_session_id: UUID | None = None,
|
||||
) -> ScriptBuilderSession:
|
||||
"""Create a new Script Builder session."""
|
||||
session = ScriptBuilderSession(
|
||||
@@ -155,6 +157,8 @@ async def create_session(
|
||||
account_id=account_id,
|
||||
team_id=team_id,
|
||||
language=language,
|
||||
origin=origin,
|
||||
ai_session_id=ai_session_id,
|
||||
)
|
||||
db.add(session)
|
||||
await db.flush()
|
||||
@@ -220,7 +224,15 @@ async def send_message(
|
||||
model = settings.get_model_for_action("script_build")
|
||||
provider = get_ai_provider(model=model)
|
||||
ai_text, input_tokens, output_tokens = await provider.generate_text(
|
||||
system_prompt=system_prompt,
|
||||
system_prompt=[
|
||||
{"type": "text", "text": system_prompt},
|
||||
# cacheable: SYSTEM_PROMPT_TEMPLATE with a per-session language
|
||||
# substitution. Two sessions on the same language share a cache
|
||||
# entry; different languages cache independently. Conversation
|
||||
# history (ai_messages) is NOT cached at this layer — if that
|
||||
# becomes a cost driver, route script_builder through the chat
|
||||
# wrapper (0.4) which handles history caching.
|
||||
],
|
||||
messages=ai_messages,
|
||||
max_tokens=8192,
|
||||
)
|
||||
@@ -287,15 +299,22 @@ async def list_sessions(
|
||||
user_id: UUID,
|
||||
limit: int = 20,
|
||||
offset: int = 0,
|
||||
*,
|
||||
include_inline: bool = False,
|
||||
) -> list[ScriptBuilderSession]:
|
||||
"""List user's builder sessions ordered by updated_at desc."""
|
||||
result = await db.execute(
|
||||
"""List user's builder sessions ordered by updated_at desc.
|
||||
|
||||
By default (include_inline=False) excludes pilot_inline sessions so the
|
||||
/script-builder dashboard only shows standalone sessions.
|
||||
"""
|
||||
stmt = (
|
||||
select(ScriptBuilderSession)
|
||||
.where(ScriptBuilderSession.user_id == user_id)
|
||||
.order_by(ScriptBuilderSession.updated_at.desc())
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
if not include_inline:
|
||||
stmt = stmt.where(ScriptBuilderSession.origin == "standalone")
|
||||
stmt = stmt.order_by(ScriptBuilderSession.updated_at.desc()).limit(limit).offset(offset)
|
||||
result = await db.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
@@ -313,13 +332,23 @@ async def delete_session(
|
||||
return True
|
||||
|
||||
|
||||
async def count_user_sessions(db: AsyncSession, user_id: UUID) -> int:
|
||||
"""Count active builder sessions for a user."""
|
||||
result = await db.execute(
|
||||
select(func.count(ScriptBuilderSession.id)).where(
|
||||
ScriptBuilderSession.user_id == user_id
|
||||
)
|
||||
async def count_user_sessions(
|
||||
db: AsyncSession,
|
||||
user_id: UUID,
|
||||
*,
|
||||
include_inline: bool = False,
|
||||
) -> int:
|
||||
"""Count active builder sessions for a user.
|
||||
|
||||
By default (include_inline=False) excludes pilot_inline sessions so they
|
||||
don't consume slots against the MAX_SESSIONS_PER_USER cap.
|
||||
"""
|
||||
stmt = select(func.count(ScriptBuilderSession.id)).where(
|
||||
ScriptBuilderSession.user_id == user_id
|
||||
)
|
||||
if not include_inline:
|
||||
stmt = stmt.where(ScriptBuilderSession.origin == "standalone")
|
||||
result = await db.execute(stmt)
|
||||
return result.scalar_one()
|
||||
|
||||
|
||||
@@ -331,6 +360,7 @@ async def save_to_library(
|
||||
category_id: UUID | None,
|
||||
share_with_team: bool,
|
||||
user_id: UUID,
|
||||
account_id: UUID,
|
||||
team_id: UUID | None,
|
||||
script_body: str | None = None,
|
||||
parameters_schema: dict | None = None,
|
||||
@@ -372,6 +402,7 @@ async def save_to_library(
|
||||
id=uuid_mod.uuid4(),
|
||||
category_id=resolved_category_id,
|
||||
created_by=user_id,
|
||||
account_id=account_id,
|
||||
team_id=team_id if share_with_team else None,
|
||||
name=name,
|
||||
slug=slug,
|
||||
|
||||
201
backend/app/services/template_extraction_service.py
Normal file
201
backend/app/services/template_extraction_service.py
Normal file
@@ -0,0 +1,201 @@
|
||||
"""TemplateExtractionService — propose a parameter schema from a rendered script.
|
||||
|
||||
Phase 5 of the FlowPilot migration. Called when an engineer chooses
|
||||
"Run now, templatize after resolve" on a suggested fix with no existing
|
||||
library match. The service looks at the concrete script (with the values
|
||||
the engineer is about to run with) and session/ticket context, then
|
||||
proposes a parameterization that future engineers could use from the
|
||||
Script Library.
|
||||
|
||||
Design choices (per FLOWPILOT-MIGRATION.md Section 6.4):
|
||||
|
||||
- **Conservative by default.** Prefer fewer parameters. Environment-agnostic
|
||||
values (like a command name) should not be parameterized. The prompt calls
|
||||
that out explicitly.
|
||||
- **Round-trip check.** After the LLM proposes parameters, we validate that
|
||||
the templated body renders back to the original script when given the
|
||||
extracted parameter values. Failures log a warning and the caller falls
|
||||
back to a single-parameter "raw script" proposal.
|
||||
- **Model:** Sonnet (`template_extraction` tier). Creates a persistent
|
||||
library artifact — quality matters more than latency.
|
||||
|
||||
Output shape mirrors the Script Generator's parameter schema:
|
||||
{
|
||||
"parameters": [
|
||||
{"key": "<snake>", "label": "<human>", "type": "text|password|select|...",
|
||||
"inferred_from": "<session fact / ticket field / ai guess>"}
|
||||
],
|
||||
"templated_body": "<script with {{ key }} placeholders>",
|
||||
}
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from app.core.ai_provider import get_ai_provider
|
||||
from app.core.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
_EXTRACTION_SYSTEM_PROMPT = """\
|
||||
You are a senior MSP engineer drafting a reusable script template from a \
|
||||
concrete script that resolved one ticket. Your job is to identify the values \
|
||||
in the script that would change for a different invocation — those become \
|
||||
parameters — and replace them with {{ snake_case }} placeholders.
|
||||
|
||||
Return strict JSON with this shape:
|
||||
{
|
||||
"parameters": [
|
||||
{
|
||||
"key": "<snake_case, ASCII>",
|
||||
"label": "<Short human label, Title Case>",
|
||||
"type": "text" | "password" | "select" | "boolean" | "number" | "textarea",
|
||||
"inferred_from": "<short sentence naming the session fact or ticket \
|
||||
field this value came from; or 'ai best-guess' when neither>"
|
||||
}
|
||||
],
|
||||
"templated_body": "<the original script with each parameterized value \
|
||||
replaced by {{ key }} matching the parameters above>"
|
||||
}
|
||||
|
||||
Rules:
|
||||
- Prefer FEWER parameters. If a value looks environment-agnostic — a cmdlet \
|
||||
name, a standard path like C:\\Windows\\System32, a Microsoft-documented URL \
|
||||
— keep it hardcoded.
|
||||
- Secret-looking values (passwords, API keys, client secrets) MUST be \
|
||||
parameterized with type=password.
|
||||
- The templated_body MUST render back to the original script when the \
|
||||
parameter values from the context are substituted in. Preserve all whitespace, \
|
||||
comments, and casing.
|
||||
- If the script has no meaningful parameters (e.g. it's a single read-only \
|
||||
cmdlet like Get-Service), return parameters=[] and templated_body = original.
|
||||
- No markdown fences, no prose, only the JSON object.
|
||||
"""
|
||||
|
||||
|
||||
async def extract_parameters(
|
||||
*,
|
||||
script_body: str,
|
||||
session_context: str | None = None,
|
||||
ticket_context: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Return `{parameters, templated_body}` for the given rendered script.
|
||||
|
||||
On LLM failure or malformed output, returns a conservative fallback:
|
||||
the original body with no parameters proposed. Callers can still create
|
||||
a `draft_templates` row from this — the engineer reviews and refines
|
||||
before accepting in the post-resolve prompt (Phase 6).
|
||||
"""
|
||||
model = settings.get_model_for_action("template_extraction")
|
||||
provider = get_ai_provider(model=model)
|
||||
|
||||
input_lines = [
|
||||
"# Script to templatize",
|
||||
"```",
|
||||
script_body.strip(),
|
||||
"```",
|
||||
]
|
||||
if session_context:
|
||||
input_lines.extend(["", "# Session context (facts, symptoms)", session_context.strip()])
|
||||
if ticket_context:
|
||||
input_lines.extend(["", "# Ticket context (company, user, priority)", ticket_context.strip()])
|
||||
user_input = "\n".join(input_lines)
|
||||
|
||||
system_blocks: list[dict[str, Any]] = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": _EXTRACTION_SYSTEM_PROMPT,
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
# cacheable: identical across every extraction call
|
||||
},
|
||||
]
|
||||
|
||||
try:
|
||||
text, _in, _out = await provider.generate_json(
|
||||
system_prompt=system_blocks,
|
||||
messages=[{"role": "user", "content": user_input}],
|
||||
max_tokens=3000,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("TemplateExtractionService LLM call failed; returning fallback")
|
||||
return _fallback(script_body)
|
||||
|
||||
parsed = _parse_response(text)
|
||||
if parsed is None:
|
||||
return _fallback(script_body)
|
||||
|
||||
# Round-trip validation: render parsed["templated_body"] with the
|
||||
# `inferred_from` values and confirm it matches the original. We don't
|
||||
# have the engineer's values yet here (those come at runtime), but we
|
||||
# can at least check that every {{ key }} in templated_body maps to a
|
||||
# declared parameter. A mismatch means the LLM referenced an undeclared
|
||||
# placeholder — conservative fallback.
|
||||
declared_keys = {p.get("key") for p in parsed["parameters"] if isinstance(p, dict)}
|
||||
referenced_keys = set(re.findall(r"\{\{\s*(\w+)\s*\}\}", parsed["templated_body"]))
|
||||
missing = referenced_keys - declared_keys
|
||||
if missing:
|
||||
logger.warning(
|
||||
"TemplateExtractionService: templated_body references undeclared "
|
||||
"keys %s; using fallback",
|
||||
sorted(missing),
|
||||
)
|
||||
return _fallback(script_body)
|
||||
|
||||
return parsed
|
||||
|
||||
|
||||
def _parse_response(raw: str) -> dict[str, Any] | None:
|
||||
"""Tolerant parse. Returns None on any structural problem."""
|
||||
cleaned = raw.strip()
|
||||
if cleaned.startswith("```"):
|
||||
cleaned = re.sub(r"^```(?:json)?\s*", "", cleaned)
|
||||
cleaned = re.sub(r"\s*```$", "", cleaned)
|
||||
try:
|
||||
data = json.loads(cleaned)
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
logger.warning("TemplateExtractionService returned non-JSON: %r", raw[:200])
|
||||
return None
|
||||
|
||||
if not isinstance(data, dict):
|
||||
return None
|
||||
params = data.get("parameters")
|
||||
body = data.get("templated_body")
|
||||
if not isinstance(params, list) or not isinstance(body, str):
|
||||
logger.warning("TemplateExtractionService missing parameters or templated_body")
|
||||
return None
|
||||
|
||||
# Validate each parameter shape. Drop malformed entries rather than
|
||||
# failing the whole response — the engineer will review before accept.
|
||||
valid_params: list[dict[str, Any]] = []
|
||||
allowed_types = {"text", "password", "select", "boolean", "number", "textarea", "multi_text"}
|
||||
for p in params:
|
||||
if not isinstance(p, dict):
|
||||
continue
|
||||
key = p.get("key")
|
||||
if not isinstance(key, str) or not re.match(r"^[a-z_][a-z0-9_]*$", key):
|
||||
continue
|
||||
ptype = p.get("type", "text")
|
||||
if ptype not in allowed_types:
|
||||
ptype = "text"
|
||||
valid_params.append({
|
||||
"key": key,
|
||||
"label": p.get("label") or key.replace("_", " ").title(),
|
||||
"type": ptype,
|
||||
"inferred_from": p.get("inferred_from") or "ai best-guess",
|
||||
})
|
||||
|
||||
return {"parameters": valid_params, "templated_body": body}
|
||||
|
||||
|
||||
def _fallback(script_body: str) -> dict[str, Any]:
|
||||
"""Conservative no-op result: zero parameters, body unchanged.
|
||||
|
||||
Used when the LLM call fails or returns unusable output. The engineer
|
||||
can still save this as a draft and refine in the post-resolve prompt —
|
||||
it just won't propose a parameterization for them.
|
||||
"""
|
||||
return {"parameters": [], "templated_body": script_body}
|
||||
116
backend/app/services/ticket_service.py
Normal file
116
backend/app/services/ticket_service.py
Normal file
@@ -0,0 +1,116 @@
|
||||
"""Ticket mutation service — wraps PSA provider, resolves is_rf_user flag."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.psa_connection import PsaConnection
|
||||
from app.models.psa_member_mapping import PsaMemberMapping
|
||||
from app.schemas.psa_tickets import (
|
||||
PSAResourceSchema,
|
||||
PSATicketCreatedSchema,
|
||||
PSATicketStatusUpdateSchema,
|
||||
)
|
||||
from app.services.psa.registry import get_provider_for_account
|
||||
from app.services.psa.types import TicketCreatePayload
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def _get_mapped_member_ids(account_id: UUID, db: AsyncSession) -> set[int]:
|
||||
"""Return set of external_member_id ints that are mapped to RF users."""
|
||||
conn_result = await db.execute(
|
||||
select(PsaConnection).where(PsaConnection.account_id == account_id)
|
||||
)
|
||||
conn = conn_result.scalar_one_or_none()
|
||||
if not conn:
|
||||
return set()
|
||||
mappings = await db.execute(
|
||||
select(PsaMemberMapping).where(PsaMemberMapping.psa_connection_id == conn.id)
|
||||
)
|
||||
return {int(m.external_member_id) for m in mappings.scalars().all() if m.external_member_id}
|
||||
|
||||
|
||||
async def list_resources(
|
||||
account_id: UUID, ticket_id: int, db: AsyncSession
|
||||
) -> list[PSAResourceSchema]:
|
||||
provider = await get_provider_for_account(account_id, db)
|
||||
mapped_ids = await _get_mapped_member_ids(account_id, db)
|
||||
resources = await provider.list_resources(ticket_id)
|
||||
return [
|
||||
PSAResourceSchema(
|
||||
member_id=r.member_id,
|
||||
member_name=r.member_name,
|
||||
member_identifier=r.member_identifier,
|
||||
is_rf_user=r.member_id in mapped_ids,
|
||||
)
|
||||
for r in resources
|
||||
]
|
||||
|
||||
|
||||
async def add_resource(
|
||||
account_id: UUID, ticket_id: int, member_id: int, db: AsyncSession
|
||||
) -> PSAResourceSchema:
|
||||
provider = await get_provider_for_account(account_id, db)
|
||||
mapped_ids = await _get_mapped_member_ids(account_id, db)
|
||||
resource = await provider.add_resource(ticket_id, member_id)
|
||||
return PSAResourceSchema(
|
||||
member_id=resource.member_id,
|
||||
member_name=resource.member_name,
|
||||
member_identifier=resource.member_identifier,
|
||||
is_rf_user=resource.member_id in mapped_ids,
|
||||
)
|
||||
|
||||
|
||||
async def remove_resource(
|
||||
account_id: UUID, ticket_id: int, member_id: int, db: AsyncSession
|
||||
) -> None:
|
||||
provider = await get_provider_for_account(account_id, db)
|
||||
await provider.remove_resource(ticket_id, member_id)
|
||||
|
||||
|
||||
async def update_status(
|
||||
account_id: UUID, ticket_id: int, status_id: int, db: AsyncSession
|
||||
) -> PSATicketStatusUpdateSchema:
|
||||
provider = await get_provider_for_account(account_id, db)
|
||||
# get current status before updating
|
||||
ticket = await provider.get_ticket(str(ticket_id))
|
||||
previous_status = ticket.status_name or ""
|
||||
await provider.update_ticket_status(str(ticket_id), status_id)
|
||||
# get new status name from statuses list
|
||||
statuses = await provider.get_ticket_statuses(ticket.board_id or 0)
|
||||
new_status = next((s.name for s in statuses if s.id == status_id), str(status_id))
|
||||
return PSATicketStatusUpdateSchema(
|
||||
ticket_id=ticket_id,
|
||||
previous_status=previous_status,
|
||||
new_status=new_status,
|
||||
new_status_id=status_id,
|
||||
)
|
||||
|
||||
|
||||
async def create_ticket(
|
||||
account_id: UUID, payload: TicketCreatePayload, db: AsyncSession
|
||||
) -> PSATicketCreatedSchema:
|
||||
provider = await get_provider_for_account(account_id, db)
|
||||
mapped_ids = await _get_mapped_member_ids(account_id, db)
|
||||
result = await provider.create_ticket(payload)
|
||||
return PSATicketCreatedSchema(
|
||||
id=result.id,
|
||||
summary=result.summary,
|
||||
board_name=result.board_name,
|
||||
status_name=result.status_name,
|
||||
priority_name=result.priority_name,
|
||||
company_name=result.company_name,
|
||||
resources=[
|
||||
PSAResourceSchema(
|
||||
member_id=r.member_id,
|
||||
member_name=r.member_name,
|
||||
member_identifier=r.member_identifier,
|
||||
is_rf_user=r.member_id in mapped_ids,
|
||||
)
|
||||
for r in result.resources
|
||||
],
|
||||
)
|
||||
@@ -3,22 +3,41 @@
|
||||
Replaces assistant_chat_service for new chat sessions. Messages are stored
|
||||
in ai_sessions.conversation_messages JSONB. Reuses the same AI calling
|
||||
infrastructure and system prompt from assistant_chat_service.
|
||||
|
||||
## Markers parsed here
|
||||
- `[QUESTIONS]` / `[ACTIONS]` — task-lane items shown to the engineer
|
||||
- `[FORK]` — diagnostic forking, creates SessionBranch rows
|
||||
- `[PROMOTE]` (Phase 2) — surfaces a fact to the What-we-know section.
|
||||
Items in pending_task_lane carry stable UUIDs (assigned here) so PROMOTE
|
||||
source_refs survive across turns even when the model re-emits the same
|
||||
question/action.
|
||||
- `[SUGGEST_FIX]` (Phase 3) — proposes a resolution path for the session.
|
||||
Each new emission supersedes the previous active row (sets superseded_at)
|
||||
so there's exactly one active fix at a time.
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import uuid as _uuid
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import update
|
||||
|
||||
from app.models.ai_session import AISession
|
||||
from app.models.script_template import ScriptTemplate
|
||||
from app.models.session_suggested_fix import SessionSuggestedFix
|
||||
from app.services.assistant_chat_service import (
|
||||
ASSISTANT_SYSTEM_PROMPT,
|
||||
_call_ai,
|
||||
_auto_title,
|
||||
)
|
||||
from app.services.fact_synthesis_service import FactSynthesisService
|
||||
from app.services.rag_service import search as rag_search, build_rag_context, extract_suggested_flows
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -147,6 +166,378 @@ def _parse_questions_marker(ai_content: str) -> tuple[str, list[dict[str, Any]]
|
||||
return cleaned, valid_questions
|
||||
|
||||
|
||||
def _parse_promote_marker(ai_content: str) -> tuple[str, list[dict[str, Any]] | None]:
|
||||
"""Extract one or more [PROMOTE]...[/PROMOTE] JSON blocks from AI response.
|
||||
|
||||
Each block contains a JSON object describing a candidate fact:
|
||||
{"source_type": "question"|"diagnostic_check"|"ai_synthesis",
|
||||
"source_ref": "<task_lane_item_uuid>" | null,
|
||||
"text": "<fact text>",
|
||||
"summary": "<short provenance, optional>"}
|
||||
|
||||
Returns (cleaned_content, list_of_items_or_None). All matched blocks are
|
||||
stripped from display text. Invalid items are dropped silently with a
|
||||
warning — a malformed PROMOTE should never break the chat response.
|
||||
|
||||
Per FLOWPILOT-MIGRATION.md Section 8.1, the model emits text + summary
|
||||
inline so no LLM round-trip is needed to persist the fact.
|
||||
"""
|
||||
blocks = list(re.finditer(r"\[PROMOTE\]\s*([\s\S]*?)\s*\[/PROMOTE\]", ai_content))
|
||||
if not blocks:
|
||||
return ai_content, None
|
||||
|
||||
items: list[dict[str, Any]] = []
|
||||
for m in blocks:
|
||||
raw = m.group(1).strip()
|
||||
if raw.startswith("```"):
|
||||
raw = re.sub(r"^```(?:json)?\s*", "", raw)
|
||||
raw = re.sub(r"\s*```$", "", raw)
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except (json.JSONDecodeError, ValueError) as e:
|
||||
logger.warning("Failed to parse [PROMOTE] block: %s", e)
|
||||
continue
|
||||
|
||||
if not isinstance(data, dict):
|
||||
logger.warning("[PROMOTE] block must be a JSON object, got %s", type(data).__name__)
|
||||
continue
|
||||
|
||||
source_type = data.get("source_type")
|
||||
text = (data.get("text") or "").strip()
|
||||
summary = (data.get("summary") or "").strip() or None
|
||||
source_ref_raw = data.get("source_ref")
|
||||
|
||||
if source_type not in ("question", "diagnostic_check", "ai_synthesis"):
|
||||
# `user_note` is engineer-only, not an AI-emittable type.
|
||||
logger.warning("Invalid [PROMOTE] source_type=%r, skipping", source_type)
|
||||
continue
|
||||
if not text:
|
||||
logger.warning("[PROMOTE] block missing text, skipping")
|
||||
continue
|
||||
|
||||
source_ref: UUID | None = None
|
||||
if source_ref_raw:
|
||||
try:
|
||||
source_ref = UUID(str(source_ref_raw))
|
||||
except (ValueError, AttributeError):
|
||||
logger.warning("[PROMOTE] source_ref %r is not a valid UUID, dropping ref", source_ref_raw)
|
||||
source_ref = None
|
||||
|
||||
# `ai_synthesis` must NEVER carry a source_ref (no question/check item
|
||||
# to point at) — surface mistakes from the model rather than tripping
|
||||
# the FactSynthesisService validation later.
|
||||
if source_type == "ai_synthesis":
|
||||
source_ref = None
|
||||
|
||||
items.append({
|
||||
"source_type": source_type,
|
||||
"source_ref": source_ref,
|
||||
"text": text,
|
||||
"summary": summary,
|
||||
})
|
||||
|
||||
# Strip all PROMOTE blocks from display content — engineers see facts in
|
||||
# the What-we-know panel, not as raw markers in the chat.
|
||||
cleaned = re.sub(r"\[PROMOTE\]\s*[\s\S]*?\s*\[/PROMOTE\]", "", ai_content).strip()
|
||||
|
||||
return cleaned, items or None
|
||||
|
||||
|
||||
def _assign_stable_task_lane_ids(
|
||||
prev_lane: dict[str, Any] | None,
|
||||
questions: list[dict[str, Any]] | None,
|
||||
actions: list[dict[str, Any]] | None,
|
||||
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
|
||||
"""Assign stable UUIDs to task-lane items, preserving them across turns.
|
||||
|
||||
The model often re-emits the same question/action across multiple turns
|
||||
(it is told to keep `_(not yet completed)_` items alive). When the
|
||||
question text matches a prior turn's, we keep the prior UUID so any
|
||||
`session_facts.source_ref` pointing at it stays valid.
|
||||
|
||||
Match key:
|
||||
- Questions: exact `text`
|
||||
- Actions: exact `label`
|
||||
|
||||
Returns the questions/actions lists augmented with an `id` field.
|
||||
"""
|
||||
prev_questions = (prev_lane or {}).get("questions") or []
|
||||
prev_actions = (prev_lane or {}).get("actions") or []
|
||||
|
||||
prev_q_ids: dict[str, str] = {
|
||||
str(q.get("text") or "").strip(): str(q["id"])
|
||||
for q in prev_questions
|
||||
if isinstance(q, dict) and q.get("id") and q.get("text")
|
||||
}
|
||||
prev_a_ids: dict[str, str] = {
|
||||
str(a.get("label") or "").strip(): str(a["id"])
|
||||
for a in prev_actions
|
||||
if isinstance(a, dict) and a.get("id") and a.get("label")
|
||||
}
|
||||
|
||||
out_questions: list[dict[str, Any]] = []
|
||||
for q in questions or []:
|
||||
text = str(q.get("text") or "").strip()
|
||||
existing = prev_q_ids.get(text) if text else None
|
||||
out_questions.append({
|
||||
**q,
|
||||
"id": existing or str(_uuid.uuid4()),
|
||||
})
|
||||
|
||||
out_actions: list[dict[str, Any]] = []
|
||||
for a in actions or []:
|
||||
label = str(a.get("label") or "").strip()
|
||||
existing = prev_a_ids.get(label) if label else None
|
||||
out_actions.append({
|
||||
**a,
|
||||
"id": existing or str(_uuid.uuid4()),
|
||||
})
|
||||
|
||||
return out_questions, out_actions
|
||||
|
||||
|
||||
def _parse_suggest_fix_marker(
|
||||
ai_content: str,
|
||||
) -> tuple[str, dict[str, Any] | None]:
|
||||
"""Extract a single [SUGGEST_FIX]...[/SUGGEST_FIX] JSON block from AI response.
|
||||
|
||||
The block contains:
|
||||
{"title": "...", "description": "...", "confidence": 0..100,
|
||||
"script_template_slug": "..." | null,
|
||||
"ai_drafted_script": "..." | null,
|
||||
"ai_drafted_parameters": {...} | null}
|
||||
|
||||
Per FLOWPILOT-MIGRATION.md Section 8.2. Only the LAST block in the response
|
||||
is honored — if the model emits multiple, only its final view of the fix
|
||||
matters; earlier ones in the same turn are stale even before persistence.
|
||||
|
||||
Returns (cleaned_content, fix_dict_or_None). Marker stripped from display.
|
||||
"""
|
||||
blocks = list(re.finditer(r"\[SUGGEST_FIX\]\s*([\s\S]*?)\s*\[/SUGGEST_FIX\]", ai_content))
|
||||
if not blocks:
|
||||
return ai_content, None
|
||||
|
||||
# Take the last block — most-recent intent wins within a single turn.
|
||||
last = blocks[-1]
|
||||
raw = last.group(1).strip()
|
||||
if raw.startswith("```"):
|
||||
raw = re.sub(r"^```(?:json)?\s*", "", raw)
|
||||
raw = re.sub(r"\s*```$", "", raw)
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except (json.JSONDecodeError, ValueError) as e:
|
||||
logger.warning("Failed to parse [SUGGEST_FIX] block: %s", e)
|
||||
return re.sub(r"\[SUGGEST_FIX\]\s*[\s\S]*?\s*\[/SUGGEST_FIX\]", "", ai_content).strip(), None
|
||||
|
||||
if not isinstance(data, dict):
|
||||
return re.sub(r"\[SUGGEST_FIX\]\s*[\s\S]*?\s*\[/SUGGEST_FIX\]", "", ai_content).strip(), None
|
||||
|
||||
title = (data.get("title") or "").strip()
|
||||
description = (data.get("description") or "").strip()
|
||||
confidence = data.get("confidence")
|
||||
if not title or not description or not isinstance(confidence, (int, float)):
|
||||
logger.warning("[SUGGEST_FIX] missing required fields, dropping")
|
||||
return re.sub(r"\[SUGGEST_FIX\]\s*[\s\S]*?\s*\[/SUGGEST_FIX\]", "", ai_content).strip(), None
|
||||
|
||||
confidence_int = max(0, min(100, int(round(float(confidence)))))
|
||||
|
||||
parsed = {
|
||||
"title": title[:200],
|
||||
"description": description,
|
||||
"confidence_pct": confidence_int,
|
||||
"script_template_slug": (data.get("script_template_slug") or None),
|
||||
"ai_drafted_script": (data.get("ai_drafted_script") or None),
|
||||
"ai_drafted_parameters": data.get("ai_drafted_parameters") if isinstance(data.get("ai_drafted_parameters"), dict) else None,
|
||||
}
|
||||
|
||||
cleaned = re.sub(r"\[SUGGEST_FIX\]\s*[\s\S]*?\s*\[/SUGGEST_FIX\]", "", ai_content).strip()
|
||||
return cleaned, parsed
|
||||
|
||||
|
||||
def _parse_fix_outcome_marker(
|
||||
ai_content: str,
|
||||
) -> tuple[str, dict[str, Any] | None]:
|
||||
"""Extract a single [FIX_OUTCOME]...[/FIX_OUTCOME] JSON block.
|
||||
|
||||
Block shape:
|
||||
{"fix_id": "<uuid>", "outcome": "success"|"failure"|"partial",
|
||||
"reason": "<one-line>"}
|
||||
|
||||
Emitted by the AI when the engineer clearly indicates in chat that a
|
||||
prior suggested fix worked, didn't work, or was partially applied.
|
||||
The marker PROPOSES an outcome — the engineer confirms via the UI.
|
||||
Only the last block in a response is honored.
|
||||
"""
|
||||
blocks = list(re.finditer(
|
||||
r"\[FIX_OUTCOME\]\s*([\s\S]*?)\s*\[/FIX_OUTCOME\]", ai_content,
|
||||
))
|
||||
if not blocks:
|
||||
return ai_content, None
|
||||
|
||||
last = blocks[-1]
|
||||
raw = last.group(1).strip()
|
||||
if raw.startswith("```"):
|
||||
raw = re.sub(r"^```(?:json)?\s*", "", raw)
|
||||
raw = re.sub(r"\s*```$", "", raw)
|
||||
|
||||
cleaned = re.sub(
|
||||
r"\[FIX_OUTCOME\]\s*[\s\S]*?\s*\[/FIX_OUTCOME\]", "", ai_content,
|
||||
).strip()
|
||||
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except (json.JSONDecodeError, ValueError) as e:
|
||||
logger.warning("Failed to parse [FIX_OUTCOME] block: %s", e)
|
||||
return cleaned, None
|
||||
|
||||
if not isinstance(data, dict):
|
||||
return cleaned, None
|
||||
|
||||
fix_id = str(data.get("fix_id") or "").strip()
|
||||
outcome = str(data.get("outcome") or "").strip().lower()
|
||||
reason = str(data.get("reason") or "").strip()
|
||||
|
||||
if not fix_id or outcome not in {"success", "failure", "partial"}:
|
||||
logger.warning("[FIX_OUTCOME] missing/invalid fields, dropping")
|
||||
return cleaned, None
|
||||
|
||||
return cleaned, {"fix_id": fix_id, "outcome": outcome, "reason": reason}
|
||||
|
||||
|
||||
async def _persist_suggested_fix(
|
||||
*,
|
||||
db: AsyncSession,
|
||||
session: AISession,
|
||||
fix: dict[str, Any],
|
||||
) -> None:
|
||||
"""Supersede the prior active fix and insert the new one. Bumps state_version.
|
||||
|
||||
A session has at most one active suggested fix (`superseded_at IS NULL`).
|
||||
Emitting [SUGGEST_FIX] is the only way to introduce a new one; the
|
||||
engineer's user_decision is recorded via the decision endpoint.
|
||||
"""
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
# Mark any prior active rows for this session as superseded.
|
||||
await db.execute(
|
||||
update(SessionSuggestedFix)
|
||||
.where(
|
||||
SessionSuggestedFix.session_id == session.id,
|
||||
SessionSuggestedFix.superseded_at.is_(None),
|
||||
)
|
||||
.values(superseded_at=now)
|
||||
)
|
||||
|
||||
# Resolve script_template_slug → script_template_id if provided.
|
||||
script_template_id = None
|
||||
slug = fix.get("script_template_slug")
|
||||
if slug:
|
||||
result = await db.execute(
|
||||
select(ScriptTemplate).where(ScriptTemplate.slug == slug)
|
||||
)
|
||||
tpl = result.scalar_one_or_none()
|
||||
if tpl is not None:
|
||||
script_template_id = tpl.id
|
||||
else:
|
||||
logger.warning(
|
||||
"SUGGEST_FIX referenced unknown script_template_slug=%r — "
|
||||
"treating as no template match", slug,
|
||||
)
|
||||
|
||||
new_fix = SessionSuggestedFix(
|
||||
session_id=session.id,
|
||||
account_id=session.account_id,
|
||||
title=fix["title"],
|
||||
description=fix["description"],
|
||||
confidence_pct=fix["confidence_pct"],
|
||||
script_template_id=script_template_id,
|
||||
ai_drafted_script=fix.get("ai_drafted_script"),
|
||||
ai_drafted_parameters=fix.get("ai_drafted_parameters"),
|
||||
)
|
||||
db.add(new_fix)
|
||||
|
||||
# Bump preview-cache version atomically with the supersession+insert.
|
||||
await db.execute(
|
||||
update(AISession)
|
||||
.where(AISession.id == session.id)
|
||||
.values(state_version=AISession.state_version + 1)
|
||||
)
|
||||
await db.flush()
|
||||
|
||||
|
||||
async def _record_ai_outcome_proposal(
|
||||
*,
|
||||
db: AsyncSession,
|
||||
session: AISession,
|
||||
proposal: dict[str, Any],
|
||||
) -> None:
|
||||
"""Persist the AI's proposed outcome on the active fix.
|
||||
|
||||
Writes to session_suggested_fixes.ai_outcome_proposal. Frontend polls
|
||||
the active fix and renders the AI-confirming banner state when this is
|
||||
non-null. Does NOT mutate the fix's status — the engineer's confirmation
|
||||
click via PATCH /outcome is what changes the status.
|
||||
|
||||
Drops silently when the fix_id isn't a valid UUID or doesn't belong to
|
||||
this session.
|
||||
"""
|
||||
try:
|
||||
fix_uuid = UUID(proposal["fix_id"])
|
||||
except (ValueError, KeyError, TypeError):
|
||||
logger.warning("[FIX_OUTCOME] invalid fix_id, dropping")
|
||||
return
|
||||
|
||||
await db.execute(
|
||||
update(SessionSuggestedFix)
|
||||
.where(
|
||||
SessionSuggestedFix.id == fix_uuid,
|
||||
SessionSuggestedFix.session_id == session.id,
|
||||
)
|
||||
.values(ai_outcome_proposal=proposal)
|
||||
)
|
||||
await db.flush()
|
||||
|
||||
|
||||
async def _persist_promote_items(
|
||||
*,
|
||||
db: AsyncSession,
|
||||
session: AISession,
|
||||
user_id: UUID,
|
||||
items: list[dict[str, Any]],
|
||||
) -> None:
|
||||
"""Persist parsed [PROMOTE] items as session_facts. Failures are logged.
|
||||
|
||||
A malformed PROMOTE must never break the chat response — the engineer
|
||||
still gets the AI's analysis; the missing fact can be added manually.
|
||||
"""
|
||||
if not items:
|
||||
return
|
||||
service = FactSynthesisService(db)
|
||||
for item in items:
|
||||
try:
|
||||
await service.create_fact(
|
||||
session_id=session.id,
|
||||
account_id=session.account_id,
|
||||
user_id=user_id,
|
||||
source_type=item["source_type"],
|
||||
text=item["text"],
|
||||
summary=item["summary"],
|
||||
source_ref=item["source_ref"],
|
||||
)
|
||||
except ValueError:
|
||||
# Validation failure (e.g. empty text after strip, or
|
||||
# source_ref-on-ai_synthesis race). Log and continue — losing
|
||||
# one fact is better than aborting the whole chat turn.
|
||||
logger.warning(
|
||||
"Skipping invalid PROMOTE item for session %s: %r",
|
||||
session.id, item, exc_info=True,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to persist PROMOTE item for session %s", session.id
|
||||
)
|
||||
|
||||
|
||||
async def create_chat_session(
|
||||
user_id: UUID,
|
||||
account_id: UUID,
|
||||
@@ -251,10 +642,14 @@ async def send_chat_message(
|
||||
if session.status == "paused":
|
||||
session.status = "active"
|
||||
|
||||
# Check for fork, actions, and questions markers in branch response too
|
||||
# Check for fork, actions, questions, promote, and suggest_fix markers
|
||||
# in branch response too
|
||||
branch_display, branch_fork_data = _parse_fork_marker(ai_content)
|
||||
branch_display, branch_actions_data = _parse_actions_marker(branch_display)
|
||||
branch_display, branch_questions_data = _parse_questions_marker(branch_display)
|
||||
branch_display, branch_promote_items = _parse_promote_marker(branch_display)
|
||||
branch_display, branch_suggest_fix = _parse_suggest_fix_marker(branch_display)
|
||||
branch_display, branch_outcome_proposal = _parse_fix_outcome_marker(branch_display)
|
||||
if branch_display != ai_content:
|
||||
# Store stripped content in branch history
|
||||
msgs[-1] = {"role": "assistant", "content": branch_display}
|
||||
@@ -288,15 +683,42 @@ async def send_chat_message(
|
||||
except Exception:
|
||||
logger.exception("Failed to create fork within branch for session %s", session.id)
|
||||
|
||||
# Persist task lane state on session
|
||||
# Persist task lane state on session — assign stable UUIDs so any
|
||||
# PROMOTE marker emitted later can reference the same items.
|
||||
if branch_questions_data or branch_actions_data:
|
||||
stable_qs, stable_as = _assign_stable_task_lane_ids(
|
||||
session.pending_task_lane,
|
||||
branch_questions_data,
|
||||
branch_actions_data,
|
||||
)
|
||||
session.pending_task_lane = {
|
||||
"questions": branch_questions_data or [],
|
||||
"actions": branch_actions_data or [],
|
||||
"questions": stable_qs,
|
||||
"actions": stable_as,
|
||||
}
|
||||
else:
|
||||
session.pending_task_lane = None
|
||||
|
||||
# Persist any PROMOTE items emitted in this turn. Done AFTER the
|
||||
# task-lane write so source_refs to brand-new items would still
|
||||
# land on persisted UUIDs (the model can also reference IDs from
|
||||
# the previous turn, which were already persisted).
|
||||
if branch_promote_items:
|
||||
await _persist_promote_items(
|
||||
db=db, session=session, user_id=user_id, items=branch_promote_items,
|
||||
)
|
||||
|
||||
# Persist a [SUGGEST_FIX] if the branch turn included one.
|
||||
if branch_suggest_fix:
|
||||
await _persist_suggested_fix(
|
||||
db=db, session=session, fix=branch_suggest_fix,
|
||||
)
|
||||
|
||||
# Persist a [FIX_OUTCOME] proposal if the branch turn included one.
|
||||
if branch_outcome_proposal is not None:
|
||||
await _record_ai_outcome_proposal(
|
||||
db=db, session=session, proposal=branch_outcome_proposal,
|
||||
)
|
||||
|
||||
suggested_flows = extract_suggested_flows(
|
||||
await rag_search(query=message, account_id=account_id, db=db, limit=8)
|
||||
)
|
||||
@@ -343,9 +765,22 @@ async def send_chat_message(
|
||||
# Check for questions marker in AI response
|
||||
display_content, questions_data = _parse_questions_marker(display_content)
|
||||
|
||||
# Check for promote markers — facts the AI is surfacing to What we know.
|
||||
display_content, promote_items = _parse_promote_marker(display_content)
|
||||
|
||||
# Check for a [SUGGEST_FIX] marker — supersedes the prior active fix.
|
||||
display_content, suggest_fix_data = _parse_suggest_fix_marker(display_content)
|
||||
|
||||
# Check for a [FIX_OUTCOME] proposal — AI confirms a prior fix's outcome.
|
||||
display_content, outcome_proposal = _parse_fix_outcome_marker(display_content)
|
||||
|
||||
logger.info(
|
||||
"Marker parsing results — actions: %s, questions: %s, fork: %s, raw_length: %d, display_length: %d",
|
||||
"Marker parsing results — actions: %s, questions: %s, fork: %s, "
|
||||
"promote: %d, suggest_fix: %s, outcome_proposal: %s, "
|
||||
"raw_length: %d, display_length: %d",
|
||||
bool(actions_data), bool(questions_data), bool(fork_data),
|
||||
len(promote_items or []), bool(suggest_fix_data),
|
||||
bool(outcome_proposal),
|
||||
len(ai_content), len(display_content),
|
||||
)
|
||||
|
||||
@@ -410,15 +845,36 @@ async def send_chat_message(
|
||||
logger.exception("Failed to create fork for session %s", session_id)
|
||||
# Fork failed but chat message still sent — don't break the response
|
||||
|
||||
# Persist task lane state on session
|
||||
# Persist task lane state on session — assign stable UUIDs so any PROMOTE
|
||||
# marker (this turn or a later one) can reference the same items.
|
||||
if questions_data or actions_data:
|
||||
stable_qs, stable_as = _assign_stable_task_lane_ids(
|
||||
session.pending_task_lane, questions_data, actions_data,
|
||||
)
|
||||
session.pending_task_lane = {
|
||||
"questions": questions_data or [],
|
||||
"actions": actions_data or [],
|
||||
"questions": stable_qs,
|
||||
"actions": stable_as,
|
||||
}
|
||||
else:
|
||||
session.pending_task_lane = None
|
||||
|
||||
# Persist any PROMOTE items emitted in this turn. Done after task-lane
|
||||
# assignment so source_refs the model invented this turn already exist.
|
||||
if promote_items:
|
||||
await _persist_promote_items(
|
||||
db=db, session=session, user_id=user_id, items=promote_items,
|
||||
)
|
||||
|
||||
# Persist a [SUGGEST_FIX] if this turn included one — supersedes prior fix.
|
||||
if suggest_fix_data:
|
||||
await _persist_suggested_fix(db=db, session=session, fix=suggest_fix_data)
|
||||
|
||||
# Persist a [FIX_OUTCOME] proposal if this turn included one.
|
||||
if outcome_proposal is not None:
|
||||
await _record_ai_outcome_proposal(
|
||||
db=db, session=session, proposal=outcome_proposal,
|
||||
)
|
||||
|
||||
suggested_flows = extract_suggested_flows(rag_results)
|
||||
|
||||
return display_content, suggested_flows, session, fork_metadata, actions_data, questions_data
|
||||
|
||||
@@ -27,6 +27,7 @@ markers =
|
||||
slow: marks tests as slow (deselect with '-m "not slow"')
|
||||
integration: marks tests as integration tests
|
||||
unit: marks tests as unit tests
|
||||
rls: opt-in RLS migration and policy tests (run with RUN_RLS_TESTS=1)
|
||||
|
||||
# Ignore paths
|
||||
testpaths = tests
|
||||
@@ -34,6 +35,9 @@ testpaths = tests
|
||||
# Warnings
|
||||
filterwarnings =
|
||||
error
|
||||
ignore:unclosed <socket\.socket.*:ResourceWarning
|
||||
ignore:unclosed transport .*:ResourceWarning
|
||||
ignore:unclosed event loop .*:ResourceWarning
|
||||
ignore::DeprecationWarning
|
||||
ignore::PendingDeprecationWarning
|
||||
ignore::pluggy.PluggyTeardownRaisedWarning
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
# Include production dependencies
|
||||
-r requirements.txt
|
||||
|
||||
# Testing
|
||||
pytest==7.4.3
|
||||
pytest-asyncio==0.23.0
|
||||
# Testing — pytest-asyncio 0.24+ requires pytest>=8.2
|
||||
pytest==8.4.2
|
||||
pytest-asyncio==0.24.0
|
||||
pytest-xdist==3.6.1
|
||||
httpx>=0.27.0
|
||||
pytest-cov==4.1.0
|
||||
pytest-cov==5.0.0
|
||||
|
||||
# Code quality
|
||||
black==24.1.1
|
||||
|
||||
375
backend/scripts/seed_phase9_qa_fixtures.py
Normal file
375
backend/scripts/seed_phase9_qa_fixtures.py
Normal file
@@ -0,0 +1,375 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Seed Phase 9 QA fixtures: 4 ai_sessions + matching suggested_fixes that
|
||||
exercise the five Phase 9 components which gate on a backend-emitted
|
||||
`SUGGEST_FIX` action and don't fire reliably in normal local sessions.
|
||||
|
||||
Usage:
|
||||
cd backend
|
||||
python -m scripts.seed_phase9_qa_fixtures
|
||||
python -m scripts.seed_phase9_qa_fixtures --reset # delete & recreate
|
||||
|
||||
Targets the super-admin from `seed_test_users.py`
|
||||
(admin@resolutionflow.example.com) and their account. UUIDs are
|
||||
deterministic (UUID5 over a fixed namespace) so re-runs are idempotent
|
||||
without --reset.
|
||||
|
||||
Sessions created:
|
||||
|
||||
| # | Title | Phase 9 component reached when… |
|
||||
|---|---------------------------------|-------------------------------------------------------|
|
||||
| A | Phase 9 QA — no-template path | ChatTabStrip + ScriptBuilderTab + ProposalBanner |
|
||||
| B | Phase 9 QA — drafted-script | InlineNoTemplateDialog + ProposalBanner |
|
||||
| C | Phase 9 QA — template match | TemplateMatchPanel + ProposalBanner |
|
||||
| D | Phase 9 QA — verify state | EscalateInterceptDialog (with new "partial" choice) |
|
||||
|
||||
Run /qa, then in the browser go to /pilot, click each session in the
|
||||
sidebar, and exercise its Phase 9 surface. The session URLs are printed
|
||||
at the end.
|
||||
"""
|
||||
import argparse
|
||||
import asyncio
|
||||
import sys
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
ADMIN_EMAIL = "admin@resolutionflow.example.com"
|
||||
|
||||
# Deterministic UUIDs so re-running the seeder updates rather than duplicates.
|
||||
NS = uuid.UUID("00000000-0000-0000-0000-000000000901")
|
||||
SESSION_A = uuid.uuid5(NS, "session-A-no-template")
|
||||
SESSION_B = uuid.uuid5(NS, "session-B-drafted-script")
|
||||
SESSION_C = uuid.uuid5(NS, "session-C-template-match")
|
||||
SESSION_D = uuid.uuid5(NS, "session-D-verify-state")
|
||||
FIX_A = uuid.uuid5(NS, "fix-A")
|
||||
FIX_B = uuid.uuid5(NS, "fix-B")
|
||||
FIX_C = uuid.uuid5(NS, "fix-C")
|
||||
FIX_D = uuid.uuid5(NS, "fix-D")
|
||||
CATEGORY_QA = uuid.uuid5(NS, "category-qa-fixtures")
|
||||
TEMPLATE_QA = uuid.uuid5(NS, "template-qa-fixtures")
|
||||
|
||||
DRAFTED_SCRIPT = """\
|
||||
# Phase 9 QA fixture — AI-drafted PowerShell to flush DNS and
|
||||
# restart the FortiClient service. Not for production use.
|
||||
ipconfig /flushdns
|
||||
Restart-Service -Name "FortiSslvpnDaemon" -Force
|
||||
Get-Service -Name "FortiSslvpnDaemon" | Format-Table -AutoSize
|
||||
"""
|
||||
|
||||
TEMPLATE_BODY = """\
|
||||
# Phase 9 QA fixture — canned template that the AI matches against.
|
||||
param([string]$ServiceName = "FortiSslvpnDaemon")
|
||||
Restart-Service -Name $ServiceName -Force
|
||||
Get-Service -Name $ServiceName | Select-Object Status, Name
|
||||
"""
|
||||
|
||||
|
||||
async def main(reset: bool = False) -> None:
|
||||
db_url = (
|
||||
settings.ADMIN_DATABASE_URL
|
||||
if hasattr(settings, "ADMIN_DATABASE_URL") and settings.ADMIN_DATABASE_URL
|
||||
else settings.DATABASE_URL
|
||||
)
|
||||
engine = create_async_engine(db_url, echo=False)
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
async with engine.begin() as conn:
|
||||
# ─── Locate the admin user + account ───────────────────────────
|
||||
row = (
|
||||
await conn.execute(
|
||||
text(
|
||||
"SELECT id, account_id FROM users WHERE email = :email LIMIT 1"
|
||||
),
|
||||
{"email": ADMIN_EMAIL},
|
||||
)
|
||||
).first()
|
||||
if row is None:
|
||||
print(
|
||||
f"ERROR: user {ADMIN_EMAIL!r} not found. Run "
|
||||
"`python -m scripts.seed_test_users` first.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(2)
|
||||
user_id, account_id = row
|
||||
|
||||
if reset:
|
||||
await conn.execute(
|
||||
text(
|
||||
"DELETE FROM session_suggested_fixes WHERE id = ANY(:ids)"
|
||||
),
|
||||
{"ids": [FIX_A, FIX_B, FIX_C, FIX_D]},
|
||||
)
|
||||
await conn.execute(
|
||||
text("DELETE FROM ai_sessions WHERE id = ANY(:ids)"),
|
||||
{"ids": [SESSION_A, SESSION_B, SESSION_C, SESSION_D]},
|
||||
)
|
||||
await conn.execute(
|
||||
text("DELETE FROM script_templates WHERE id = :id"),
|
||||
{"id": TEMPLATE_QA},
|
||||
)
|
||||
await conn.execute(
|
||||
text("DELETE FROM script_categories WHERE id = :id"),
|
||||
{"id": CATEGORY_QA},
|
||||
)
|
||||
|
||||
# ─── Script category + template (for Session C) ────────────────
|
||||
await conn.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO script_categories (id, name, slug, sort_order, is_active, created_at, updated_at)
|
||||
VALUES (:id, 'QA Fixtures', 'qa-fixtures', 999, true, :now, :now)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
"""
|
||||
),
|
||||
{"id": CATEGORY_QA, "now": now},
|
||||
)
|
||||
await conn.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO script_templates (
|
||||
id, category_id, account_id, created_by, name, slug,
|
||||
description, script_body, language, parameters_schema,
|
||||
default_values, validation_rules, tags, complexity,
|
||||
requires_elevation, requires_modules, created_at, updated_at
|
||||
)
|
||||
VALUES (
|
||||
:id, :cat_id, :acct_id, :user_id,
|
||||
'QA Fixture: Restart Forti Service',
|
||||
'qa-fixture-restart-forti-service',
|
||||
'Phase 9 QA fixture template for TemplateMatchPanel testing.',
|
||||
:body, 'powershell',
|
||||
'{}'::jsonb, '{}'::jsonb, '{}'::jsonb, '[]'::jsonb,
|
||||
'beginner', false, '[]'::jsonb,
|
||||
:now, :now
|
||||
)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": TEMPLATE_QA,
|
||||
"cat_id": CATEGORY_QA,
|
||||
"acct_id": account_id,
|
||||
"user_id": user_id,
|
||||
"body": TEMPLATE_BODY,
|
||||
"now": now,
|
||||
},
|
||||
)
|
||||
|
||||
# ─── 4 sessions ────────────────────────────────────────────────
|
||||
# `canAct` in the chat header gates Resolve/Escalate on
|
||||
# `messages.length >= 2`, so each fixture seeds two synthetic
|
||||
# conversation messages — enough to enable the buttons that drive
|
||||
# the Phase 9 surfaces.
|
||||
seed_messages = (
|
||||
'['
|
||||
'{"role":"user","content":"QA fixture: see seed_phase9_qa_fixtures.py"},'
|
||||
'{"role":"assistant","content":"This session is a Phase 9 QA fixture. The suggested fix below is pre-seeded — drive it from the UI."}'
|
||||
']'
|
||||
)
|
||||
sessions = [
|
||||
(SESSION_A, "Phase 9 QA — no-template path"),
|
||||
(SESSION_B, "Phase 9 QA — drafted-script path"),
|
||||
(SESSION_C, "Phase 9 QA — template-match path"),
|
||||
(SESSION_D, "Phase 9 QA — verify state (Escalate intercept)"),
|
||||
]
|
||||
for sid, title in sessions:
|
||||
await conn.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO ai_sessions (
|
||||
id, user_id, account_id, session_type, title,
|
||||
intake_type, intake_content, status, confidence_tier,
|
||||
confidence_score, conversation_messages,
|
||||
total_input_tokens, total_output_tokens, step_count,
|
||||
is_branching, state_version,
|
||||
handoff_count, total_active_seconds, total_parked_seconds,
|
||||
created_at, updated_at
|
||||
)
|
||||
VALUES (
|
||||
:id, :user_id, :acct_id, 'chat', :title,
|
||||
'free_text', '{"text": "QA fixture session"}'::jsonb,
|
||||
'active', 'discovery',
|
||||
0.0, (:msgs)::jsonb,
|
||||
0, 0, 0,
|
||||
false, 0,
|
||||
0, 0, 0,
|
||||
:now, :now
|
||||
)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
title = EXCLUDED.title,
|
||||
status = EXCLUDED.status,
|
||||
conversation_messages = EXCLUDED.conversation_messages,
|
||||
updated_at = EXCLUDED.updated_at
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": sid,
|
||||
"user_id": user_id,
|
||||
"acct_id": account_id,
|
||||
"title": title,
|
||||
"msgs": seed_messages,
|
||||
"now": now,
|
||||
},
|
||||
)
|
||||
|
||||
# ─── 4 suggested fixes ─────────────────────────────────────────
|
||||
# Fix A — no template, no draft → ChatTabStrip + ScriptBuilderTab
|
||||
await _upsert_fix(
|
||||
conn, fix_id=FIX_A, session_id=SESSION_A, account_id=account_id,
|
||||
title="Restart the FortiClient daemon and flush DNS",
|
||||
description=(
|
||||
"Error -8 on FortiClient SSL VPN typically clears after a "
|
||||
"service restart on the endpoint. No matching template; "
|
||||
"no AI draft yet — engineer should choose Build Template "
|
||||
"or One-Off in the Script Builder tab."
|
||||
),
|
||||
confidence_pct=72,
|
||||
script_template_id=None,
|
||||
ai_drafted_script=None,
|
||||
status="proposed",
|
||||
applied_at=None,
|
||||
now=now,
|
||||
)
|
||||
|
||||
# Fix B — drafted script, no template → InlineNoTemplateDialog
|
||||
await _upsert_fix(
|
||||
conn, fix_id=FIX_B, session_id=SESSION_B, account_id=account_id,
|
||||
title="Run AI-drafted PowerShell to recover SSL VPN",
|
||||
description=(
|
||||
"AI drafted a session-specific script because no library "
|
||||
"template matched. Inline dialog should offer Save-as-template, "
|
||||
"Run-once, or Discard."
|
||||
),
|
||||
confidence_pct=68,
|
||||
script_template_id=None,
|
||||
ai_drafted_script=DRAFTED_SCRIPT,
|
||||
status="proposed",
|
||||
applied_at=None,
|
||||
now=now,
|
||||
)
|
||||
|
||||
# Fix C — template match → TemplateMatchPanel
|
||||
await _upsert_fix(
|
||||
conn, fix_id=FIX_C, session_id=SESSION_C, account_id=account_id,
|
||||
title="Match: QA Fixture Restart Forti Service",
|
||||
description=(
|
||||
"AI matched an existing library template. The match panel "
|
||||
"should render with the parameterization preview and an "
|
||||
"explicit 'I ran this' action."
|
||||
),
|
||||
confidence_pct=88,
|
||||
script_template_id=TEMPLATE_QA,
|
||||
ai_drafted_script=None,
|
||||
status="proposed",
|
||||
applied_at=None,
|
||||
now=now,
|
||||
)
|
||||
|
||||
# Fix D — applied_at set, status='proposed' → verify state.
|
||||
# Hitting Escalate from this state opens EscalateInterceptDialog.
|
||||
await _upsert_fix(
|
||||
conn, fix_id=FIX_D, session_id=SESSION_D, account_id=account_id,
|
||||
title="Verifying: post-apply tunnel reconnect",
|
||||
description=(
|
||||
"Engineer marked the fix as Applied; we're now in the "
|
||||
"verify window. Clicking Escalate from here should open "
|
||||
"the EscalateInterceptDialog with the four outcome choices "
|
||||
"(worked / didn't / partial / never-applied)."
|
||||
),
|
||||
confidence_pct=80,
|
||||
script_template_id=None,
|
||||
ai_drafted_script=DRAFTED_SCRIPT,
|
||||
status="proposed",
|
||||
applied_at=now - timedelta(minutes=2),
|
||||
now=now,
|
||||
)
|
||||
|
||||
await engine.dispose()
|
||||
|
||||
print()
|
||||
print("=" * 64)
|
||||
print(" Phase 9 QA fixtures ready.")
|
||||
print("=" * 64)
|
||||
print()
|
||||
print(f" Sign in as : {ADMIN_EMAIL}")
|
||||
print(f" Then visit : http://docker-01:5173/pilot")
|
||||
print(f" Pick from the History sidebar:")
|
||||
print(f" A. Phase 9 QA — no-template path (ChatTabStrip + ScriptBuilderTab)")
|
||||
print(f" B. Phase 9 QA — drafted-script path (InlineNoTemplateDialog)")
|
||||
print(f" C. Phase 9 QA — template-match path (TemplateMatchPanel)")
|
||||
print(f" D. Phase 9 QA — verify state (EscalateInterceptDialog)")
|
||||
print()
|
||||
print(f" Re-run with --reset to wipe and recreate.")
|
||||
print()
|
||||
|
||||
|
||||
async def _upsert_fix(
|
||||
conn,
|
||||
*,
|
||||
fix_id: uuid.UUID,
|
||||
session_id: uuid.UUID,
|
||||
account_id: uuid.UUID,
|
||||
title: str,
|
||||
description: str,
|
||||
confidence_pct: int,
|
||||
script_template_id: uuid.UUID | None,
|
||||
ai_drafted_script: str | None,
|
||||
status: str,
|
||||
applied_at: datetime | None,
|
||||
now: datetime,
|
||||
) -> None:
|
||||
await conn.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO session_suggested_fixes (
|
||||
id, session_id, account_id, title, description,
|
||||
confidence_pct, script_template_id, ai_drafted_script,
|
||||
status, applied_at, created_at
|
||||
)
|
||||
VALUES (
|
||||
:id, :sid, :acct, :title, :desc,
|
||||
:conf, :tmpl, :draft,
|
||||
:status, :applied, :now
|
||||
)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
title = EXCLUDED.title,
|
||||
description = EXCLUDED.description,
|
||||
confidence_pct = EXCLUDED.confidence_pct,
|
||||
script_template_id = EXCLUDED.script_template_id,
|
||||
ai_drafted_script = EXCLUDED.ai_drafted_script,
|
||||
status = EXCLUDED.status,
|
||||
applied_at = EXCLUDED.applied_at,
|
||||
superseded_at = NULL
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": fix_id,
|
||||
"sid": session_id,
|
||||
"acct": account_id,
|
||||
"title": title,
|
||||
"desc": description,
|
||||
"conf": confidence_pct,
|
||||
"tmpl": script_template_id,
|
||||
"draft": ai_drafted_script,
|
||||
"status": status,
|
||||
"applied": applied_at,
|
||||
"now": now,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Seed Phase 9 QA fixtures.")
|
||||
parser.add_argument(
|
||||
"--reset",
|
||||
action="store_true",
|
||||
help="Delete and recreate the fixtures.",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
asyncio.run(main(reset=args.reset))
|
||||
@@ -161,8 +161,8 @@ async def main() -> None:
|
||||
if cfg["plan"] is not None:
|
||||
await conn.execute(
|
||||
text("""
|
||||
INSERT INTO subscriptions (id, account_id, plan, status, created_at, updated_at)
|
||||
VALUES (:id, :aid, :plan, 'active', :now, :now)
|
||||
INSERT INTO subscriptions (id, account_id, plan, status, cancel_at_period_end, created_at, updated_at)
|
||||
VALUES (:id, :aid, :plan, 'active', false, :now, :now)
|
||||
"""),
|
||||
{"id": uuid.uuid4(), "aid": account_id, "plan": cfg["plan"], "now": now},
|
||||
)
|
||||
|
||||
@@ -4,8 +4,9 @@ Pytest configuration and fixtures for integration tests.
|
||||
Provides test database setup, client fixtures, and authentication helpers.
|
||||
"""
|
||||
|
||||
import os
|
||||
import asyncio
|
||||
from typing import AsyncGenerator, Generator
|
||||
from typing import AsyncGenerator
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
@@ -14,30 +15,130 @@ from sqlalchemy.pool import NullPool
|
||||
|
||||
from app.main import app
|
||||
from app.core.database import Base, get_db
|
||||
from app.core.admin_database import get_admin_db
|
||||
from app.core.config import settings
|
||||
# Import every model module so all tables are registered with Base.metadata
|
||||
# before the test_db fixture calls create_all. app.main imports models lazily
|
||||
# (inside scheduler functions and route modules), which is fine at runtime
|
||||
# but leaves the metadata incomplete at fixture-setup time — surfacing as
|
||||
# "relation X does not exist" errors for any model whose route/scheduler
|
||||
# hasn't been loaded yet. The `from app import models` form avoids
|
||||
# shadowing the `app` FastAPI instance imported just above.
|
||||
from app import models as _models # noqa: F401
|
||||
|
||||
# Disable invite code requirement for tests
|
||||
settings.REQUIRE_INVITE_CODE = False
|
||||
|
||||
# Test database URL (separate from production)
|
||||
# Use DATABASE_TEST_URL env var if set (e.g. inside Docker where host is 'db'),
|
||||
# otherwise fall back to localhost for local development.
|
||||
import os
|
||||
TEST_DATABASE_URL = os.environ.get(
|
||||
"DATABASE_URL",
|
||||
os.environ.get(
|
||||
"DATABASE_TEST_URL",
|
||||
"postgresql+asyncpg://postgres:postgres@localhost:5432/patherly_test",
|
||||
),
|
||||
# Test database URL — NEVER reuse DATABASE_URL. The test_db fixture does
|
||||
# `DROP SCHEMA public CASCADE` on every test; if DATABASE_URL (which normally
|
||||
# points at the dev/prod DB) leaked into this value, running `pytest tests/`
|
||||
# would silently nuke the dev database. Only DATABASE_TEST_URL is honored,
|
||||
# and the safety assertion below refuses to run against a DB whose name
|
||||
# doesn't contain "test".
|
||||
_BASE_TEST_DATABASE_URL = os.environ.get(
|
||||
"DATABASE_TEST_URL",
|
||||
"postgresql+asyncpg://postgres:postgres@localhost:5432/resolutionflow_test",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def event_loop() -> Generator:
|
||||
"""Create an instance of the default event loop for each test case."""
|
||||
loop = asyncio.get_event_loop_policy().new_event_loop()
|
||||
yield loop
|
||||
loop.close()
|
||||
def _worker_db_url(base_url: str) -> str:
|
||||
"""Per-worker DB URL for pytest-xdist parallelization.
|
||||
|
||||
pytest-xdist sets PYTEST_XDIST_WORKER to 'gw0', 'gw1', ... per worker
|
||||
process. Each worker needs its own database so the per-test
|
||||
`DROP SCHEMA public CASCADE` doesn't race across workers. Master/serial
|
||||
runs (no xdist) keep the base DB. The base DB is created by the postgres
|
||||
service container; per-worker DBs are CREATE DATABASE-d on first import
|
||||
by `_ensure_worker_db_exists` below.
|
||||
"""
|
||||
worker = os.environ.get("PYTEST_XDIST_WORKER")
|
||||
if not worker or worker == "master":
|
||||
return base_url
|
||||
head, tail = base_url.rsplit("/", 1)
|
||||
db_name, _, query = tail.partition("?")
|
||||
suffix = f"?{query}" if query else ""
|
||||
return f"{head}/{db_name}_{worker}{suffix}"
|
||||
|
||||
|
||||
def _ensure_worker_db_exists(worker_url: str, base_url: str) -> None:
|
||||
"""Create the per-worker DB if it doesn't exist. Runs synchronously at
|
||||
conftest import time (before any async test machinery), using psycopg2
|
||||
against the postgres maintenance DB. No-op when not running under xdist.
|
||||
"""
|
||||
if worker_url == base_url:
|
||||
return
|
||||
head, tail = worker_url.rsplit("/", 1)
|
||||
worker_db = tail.partition("?")[0]
|
||||
# Strip the +asyncpg dialect for sync psycopg2 + connect to 'postgres'.
|
||||
sync_head = head.replace("+asyncpg", "")
|
||||
admin_url = f"{sync_head}/postgres"
|
||||
# Lazy import — psycopg2 is a transitive backend dep; not imported at
|
||||
# module top to keep the conftest light when xdist isn't in use.
|
||||
from sqlalchemy import create_engine
|
||||
engine = create_engine(admin_url, isolation_level="AUTOCOMMIT")
|
||||
try:
|
||||
with engine.begin() as conn:
|
||||
exists = conn.execute(
|
||||
sa.text("SELECT 1 FROM pg_database WHERE datname = :n"),
|
||||
{"n": worker_db},
|
||||
).scalar()
|
||||
if not exists:
|
||||
# Identifier interpolation is safe — worker_db is built from
|
||||
# the trusted base URL + 'gw\d+' worker suffix.
|
||||
conn.execute(sa.text(f'CREATE DATABASE "{worker_db}"'))
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
TEST_DATABASE_URL = _worker_db_url(_BASE_TEST_DATABASE_URL)
|
||||
_ensure_worker_db_exists(TEST_DATABASE_URL, _BASE_TEST_DATABASE_URL)
|
||||
|
||||
# Belt-and-suspenders: refuse to run tests against a DB whose name doesn't
|
||||
# contain "test". Parses the last path segment of the URL (everything after
|
||||
# the final '/', with query string stripped) so credentials / hosts that
|
||||
# happen to contain "test" can't bypass the check.
|
||||
_test_db_name = TEST_DATABASE_URL.rsplit("/", 1)[-1].split("?", 1)[0].lower()
|
||||
assert "test" in _test_db_name, (
|
||||
f"Refusing to run tests against database {_test_db_name!r} — "
|
||||
f"the DB name must contain 'test'. Set DATABASE_TEST_URL to a dedicated "
|
||||
f"test database (e.g. resolutionflow_test)."
|
||||
)
|
||||
|
||||
_RUN_RLS_TESTS = os.environ.get("RUN_RLS_TESTS") == "1"
|
||||
_RLS_ISOLATION_FILE = "test_rls_isolation.py"
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(config, items):
|
||||
"""Keep migration-managed RLS checks out of the default create_all suite."""
|
||||
if _RUN_RLS_TESTS:
|
||||
return
|
||||
|
||||
selected = []
|
||||
deselected = []
|
||||
for item in items:
|
||||
item_path = getattr(item, "path", None) or getattr(item, "fspath", None)
|
||||
if item_path and str(item_path).endswith(_RLS_ISOLATION_FILE):
|
||||
deselected.append(item)
|
||||
else:
|
||||
selected.append(item)
|
||||
|
||||
if deselected:
|
||||
config.hook.pytest_deselected(items=deselected)
|
||||
items[:] = selected
|
||||
|
||||
|
||||
@pytest.hookimpl(trylast=True, hookwrapper=True)
|
||||
def pytest_runtest_teardown(item, nextitem):
|
||||
"""Close pytest-asyncio's post-test clean loop before warnings collect it."""
|
||||
yield
|
||||
policy = asyncio.get_event_loop_policy()
|
||||
try:
|
||||
loop = policy.get_event_loop()
|
||||
except RuntimeError:
|
||||
return
|
||||
if not loop.is_running() and not loop.is_closed():
|
||||
loop.close()
|
||||
policy.set_event_loop(None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -104,6 +205,7 @@ async def test_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
# Dispose engine first so all pooled connections are released,
|
||||
# then reconnect to perform the schema teardown cleanly.
|
||||
await engine.dispose()
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
# Drop all tables after test (CASCADE for circular FKs)
|
||||
teardown_engine = create_async_engine(
|
||||
@@ -117,6 +219,7 @@ async def test_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
await conn.execute(sa.text("CREATE SCHEMA public"))
|
||||
finally:
|
||||
await teardown_engine.dispose()
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -131,6 +234,11 @@ async def client(test_db: AsyncSession):
|
||||
yield test_db
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
# Endpoints that use get_admin_db (register, admin routes, service accounts)
|
||||
# must also hit the test DB; otherwise they leak into the real admin DB.
|
||||
# RLS is not enabled in the test schema (create_all, not alembic), so sharing
|
||||
# the same session is safe.
|
||||
app.dependency_overrides[get_admin_db] = override_get_db
|
||||
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||||
|
||||
@@ -19,8 +19,116 @@ class TestAdminEndpoints:
|
||||
"/api/v1/admin/users", headers=admin_auth_headers
|
||||
)
|
||||
assert response.status_code == 200
|
||||
users = response.json()
|
||||
assert len(users) >= 2 # admin + test_user
|
||||
payload = response.json()
|
||||
assert payload["total"] >= 2 # admin + test_user
|
||||
assert len(payload["items"]) >= 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_users_supports_search(
|
||||
self, client: AsyncClient, admin_auth_headers: dict, test_user: dict
|
||||
):
|
||||
"""Test admin people search by user email."""
|
||||
response = await client.get(
|
||||
"/api/v1/admin/users",
|
||||
params={"search": test_user["email"]},
|
||||
headers=admin_auth_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert payload["total"] >= 1
|
||||
assert any(item["email"] == test_user["email"] for item in payload["items"])
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_accounts_as_admin(
|
||||
self, client: AsyncClient, admin_auth_headers: dict
|
||||
):
|
||||
"""Test listing accounts with member data."""
|
||||
response = await client.get(
|
||||
"/api/v1/admin/accounts", headers=admin_auth_headers
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert payload["total"] >= 1
|
||||
assert len(payload["items"]) >= 1
|
||||
assert "members" in payload["items"][0]
|
||||
assert "subscription" in payload["items"][0]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_account_as_admin(
|
||||
self, client: AsyncClient, admin_auth_headers: dict
|
||||
):
|
||||
"""Test creating an empty account from admin."""
|
||||
response = await client.post(
|
||||
"/api/v1/admin/accounts",
|
||||
json={"name": "Acme Customer", "plan": "pro"},
|
||||
headers=admin_auth_headers,
|
||||
)
|
||||
assert response.status_code == 201
|
||||
payload = response.json()
|
||||
assert payload["name"] == "Acme Customer"
|
||||
assert payload["subscription"]["plan"] == "pro"
|
||||
assert payload["display_code"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_account_detail_as_admin(
|
||||
self, client: AsyncClient, admin_auth_headers: dict, test_user: dict
|
||||
):
|
||||
"""Test fetching account detail for management view."""
|
||||
account_id = test_user["user_data"]["account_id"]
|
||||
response = await client.get(
|
||||
f"/api/v1/admin/accounts/{account_id}",
|
||||
headers=admin_auth_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert payload["id"] == account_id
|
||||
assert "members" in payload
|
||||
assert "invites" in payload
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_account_name_as_admin(
|
||||
self, client: AsyncClient, admin_auth_headers: dict, test_user: dict
|
||||
):
|
||||
"""Test renaming an account from admin detail view."""
|
||||
account_id = test_user["user_data"]["account_id"]
|
||||
response = await client.put(
|
||||
f"/api/v1/admin/accounts/{account_id}",
|
||||
json={"name": "Renamed Customer Account"},
|
||||
headers=admin_auth_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert payload["id"] == account_id
|
||||
assert payload["name"] == "Renamed Customer Account"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_account_plan(
|
||||
self, client: AsyncClient, admin_auth_headers: dict, test_user: dict
|
||||
):
|
||||
"""Test changing an account's subscription plan."""
|
||||
account_id = test_user["user_data"]["account_id"]
|
||||
response = await client.put(
|
||||
f"/api/v1/admin/accounts/{account_id}/subscription/plan",
|
||||
json={"plan": "pro"},
|
||||
headers=admin_auth_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["plan"] == "pro"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_extend_account_trial(
|
||||
self, client: AsyncClient, admin_auth_headers: dict, test_user: dict
|
||||
):
|
||||
"""Test starting or extending an account trial."""
|
||||
account_id = test_user["user_data"]["account_id"]
|
||||
response = await client.put(
|
||||
f"/api/v1/admin/accounts/{account_id}/subscription/extend-trial",
|
||||
json={"days": 14},
|
||||
headers=admin_auth_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "trialing"
|
||||
assert response.json()["current_period_end"] is not None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_users_as_non_admin(
|
||||
|
||||
@@ -74,19 +74,25 @@ def _mock_ai_provider(text: str, input_tokens: int = 100, output_tokens: int = 2
|
||||
@pytest.fixture
|
||||
def enable_ai():
|
||||
"""Temporarily enable AI by setting a fake API key."""
|
||||
original = settings.ANTHROPIC_API_KEY
|
||||
original_anthropic = settings.ANTHROPIC_API_KEY
|
||||
original_google = settings.GOOGLE_AI_API_KEY
|
||||
settings.ANTHROPIC_API_KEY = "test-key-fake"
|
||||
settings.GOOGLE_AI_API_KEY = None
|
||||
yield
|
||||
settings.ANTHROPIC_API_KEY = original
|
||||
settings.ANTHROPIC_API_KEY = original_anthropic
|
||||
settings.GOOGLE_AI_API_KEY = original_google
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def disable_ai():
|
||||
"""Ensure AI is disabled."""
|
||||
original = settings.ANTHROPIC_API_KEY
|
||||
original_anthropic = settings.ANTHROPIC_API_KEY
|
||||
original_google = settings.GOOGLE_AI_API_KEY
|
||||
settings.ANTHROPIC_API_KEY = None
|
||||
settings.GOOGLE_AI_API_KEY = None
|
||||
yield
|
||||
settings.ANTHROPIC_API_KEY = original
|
||||
settings.ANTHROPIC_API_KEY = original_anthropic
|
||||
settings.GOOGLE_AI_API_KEY = original_google
|
||||
|
||||
|
||||
# ── Quota endpoint ──
|
||||
|
||||
@@ -66,6 +66,7 @@ async def test_create_fork(client: AsyncClient, test_user, auth_headers, test_db
|
||||
|
||||
step = AISessionStep(
|
||||
session_id=session.id,
|
||||
account_id=session.account_id,
|
||||
step_order=0,
|
||||
step_type="question",
|
||||
content={"text": "What's the issue?"},
|
||||
@@ -119,7 +120,7 @@ async def test_switch_branch(client: AsyncClient, test_user, auth_headers, test_
|
||||
root = await manager.create_root_branch(session.id)
|
||||
|
||||
step = AISessionStep(
|
||||
session_id=session.id, step_order=0, step_type="question",
|
||||
session_id=session.id, account_id=session.account_id, step_order=0, step_type="question",
|
||||
content={"text": "test"}, confidence_at_step=0.5,
|
||||
)
|
||||
test_db.add(step)
|
||||
@@ -197,7 +198,7 @@ async def test_get_branch_tree(client: AsyncClient, test_user, auth_headers, tes
|
||||
root = await manager.create_root_branch(session.id)
|
||||
|
||||
step = AISessionStep(
|
||||
session_id=session.id, step_order=0, step_type="question",
|
||||
session_id=session.id, account_id=session.account_id, step_order=0, step_type="question",
|
||||
content={"text": "test"}, confidence_at_step=0.5,
|
||||
)
|
||||
test_db.add(step)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user