Significant rewrite of FLOWPILOT-MIGRATION.md after post-Codex plan review
and the Phase 0 in-flight audit. Archives the pre-rewrite version as
FLOWPILOT-MIGRATION-v1.md and keeps the Codex review under
CODEX-FlowAssist-Migration-PLAN.md for traceability.
Substantive changes that affect implementation:
- Section 0.1 adds a spec-drift note listing corrections integrated into
this revision (API namespace, task-lane item UUIDs, account_settings
creation, missing /tickets/ai-parse endpoint).
- Section 2 adds "Task lane item ID" terminology — stable UUID assigned
to items inside ai_sessions.pending_task_lane so session_facts.source_ref
has something reliable to point to.
- Section 4.1 adds ai_sessions.state_version (INTEGER NOT NULL DEFAULT 0)
and escalation_package_external_id. state_version drives preview cache
invalidation; incremented atomically on writes to facts / suggested
fixes / script_generations.
- Section 4.6 creates account_settings as a new table with JSONB
preferences column, lazy row creation, and a promotion rule for when a
setting should graduate to a typed column.
- Section 5 namespaces all session-scoped routes under
/api/v1/ai-sessions/{id}/... to match the existing codebase pattern.
- Section 5.5 documents the preview caching strategy (state_version
keyed, 500ms client debounce, Redis planned).
- Section 6.6 adds per-service MCP capability flags alongside the model
tier flags.
- Section 7.1 makes the /assistant -> /pilot redirect include the
session-deep-link path and preserve the session ID.
- Section 8.2 adds supersession semantics for [SUGGEST_FIX] markers.
- Section 9 Phase 1 now explicitly includes account_settings and
state_version; Phase 3 uses state_version-keyed caching; Phase 5
mentions MCP inheritance via chat_call_cached wrapper.
- Section 11 adds a dedicated test plan (migrations, backend, frontend,
manual QA).
- Section 14 captures the eight planning decisions made during the
Phase 0 conversation so they are traceable.
No code changes in this commit.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
10 KiB
10 KiB
FlowPilot Migration Plan: Phase 0 Through Phase 7
Summary
- Stay code-change-free until execution is explicitly requested.
- Implement in commit-sized phases, with Phase 0 as a prerequisite for AI-heavy Phases 2+.
- Use this repo’s existing
/api/v1/ai-sessionsAPI namespace instead of the doc’s generic/sessionspath. - Move the existing chat-first
AssistantChatPageto/pilot;/assistantbecomes a permanent redirect. - Keep
ai_sessions.session_typefor compatibility, but the user-facing product becomes one FlowPilot surface.
Phase 0: Prompt Caching Infrastructure
- Consolidate Anthropic prompt caching into
backend/app/core/ai_provider.py, then route all Anthropic calls through that provider. - Preserve the existing cached behavior from
assistant_chat_service, but remove the private duplicate cached implementation once provider parity exists. - Add cache-control blocks for static system prompt sections and stable tool/context prefixes; keep volatile user messages outside the cached prefix.
- Update one-shot AI generators and
/tickets/ai-parseto separate stable context from changing request content. - Instrument every Anthropic response with
cache_read_input_tokensandcache_creation_input_tokens. - Acceptance: two identical FlowPilot/provider-backed calls within 5 minutes show creation tokens on the first call and read tokens on the second.
Phase 1: Schema + Route Rename
- Add Alembic migration after current repo head with:
session_factssession_suggested_fixesdraft_templatesaccount_settings- new artifact columns on
ai_sessions - provenance columns on
script_templates
- Create
account_settingsas one lazy row per account:account_idprimary key, FK toaccounts(id)with cascade deletepreferences JSONB NOT NULL DEFAULT '{}'- timestamps
get_setting(key, default)helper on the SQLAlchemy modeltemplatize_prompt_enableddefault read astruewhen the row/key is absent
- Apply RLS to all new tenant-scoped tables using the repo’s
app.current_account_idpolicy pattern. - Route
/pilotand/pilot/:sessionIdto the existing chat UI; redirect/assistantand/assistant/:sessionIdpermanently. - Update sidebar, command palette, dashboard cards, session list links, and visible labels from “AI Assistant”/ResolutionAssist to “FlowPilot” where they describe the troubleshooting surface.
- Acceptance:
/pilotrenders the chat UI,/assistantredirects, RLS grep/check passes, and no Phase 2 UI is introduced yet.
Phase 2: What We Know
- Add
FactSynthesisServicefor conservative conversion of answers/check outputs into facts. - Add fact APIs under
/api/v1/ai-sessions/{id}/facts:- list, create manual note, update editable fact, soft-delete, promote source item.
- Extend
unified_chat_servicemarker parsing with[PROMOTE]; do not create a separate marker pipeline. - Because current questions/checks live in
ai_sessions.pending_task_laneJSON, Phase 2 must assign stable UUIDs to task-lane questions/actions/checks when they are first persisted.session_facts.source_refpoints to those stable JSON item IDs; it remains polymorphic and unconstrained at the DB level. - Add frontend task lane components under the new FlowPilot component namespace:
TaskLaneWhatWeKnowWhatWeKnowItemAddNoteButton- moved/refactored Questions and Diagnostic Checks sections
- Place What We Know above Questions. Facts from questions/checks are read-only at the fact card level; manual and AI-synthesis facts are editable.
- Acceptance: answering a question or completing a check can promote a fact within 2 seconds; manual notes persist; page reload preserves facts; cross-account access is blocked.
Phase 3: Suggested Fix + Resolve Preview
- Add suggested-fix APIs under
/api/v1/ai-sessions/{id}/suggested-fixes:- get active suggested fix
- record decision for one-off/draft-template/build-template/dismissed
- Extend
unified_chat_servicemarker parsing with[SUGGEST_FIX]; supersede the prior active fix when a new one is persisted. - Add
ResolutionNoteGeneratorServicethat builds the fixed markdown shape:- Problem
- What we confirmed
- Root cause
- Resolution
- Add preview endpoint at
/api/v1/ai-sessions/{id}/resolution-note/preview. - Generate the preview from
ai_sessions,session_facts, active suggested fix, and linked script generations; redact sensitive script parameters. - Cache preview output by session-state version or content hash; invalidate on fact/suggested-fix/script-generation writes.
- Add
SuggestedFix,ResolveButton, andResolutionNotePreviewpopover. Debounce preview refresh to 500ms. - Acceptance: a session with facts and a suggested fix shows a four-section preview; editing a fact refreshes preview; human review confirms no unsupported claims.
Phase 4: Resolve + Escalate Writebacks
- Add
EscalationPackageGeneratorServicewith handoff-oriented markdown:- Problem
- What we’ve confirmed
- What we’ve tried
- Current hypothesis
- Suggested next steps
- Add preview/post endpoints under
/api/v1/ai-sessions/{id}:/resolution-note/preview/resolution-note/post/escalation-package/preview/escalation-package/post
- Extend PSA writeback service using the existing PSA provider registry and
post_noteseam. - Implement “confirm and fire”: engineer edits preview, clicks Confirm & post, then server posts to PSA and stores result metadata.
- Ticket status transitions must verify by re-fetching status; failed verification is surfaced as an error, not silent success.
- Resolving without a linked PSA ticket stores markdown and marks the session resolved without external posting.
- Acceptance: ConnectWise test ticket receives the note/package, status verification works, and unlinked sessions resolve locally.
Phase 5: Inline Script Generator Integration
- Add inline Script Generator components:
TemplateMatchPanelNoTemplateDialogParameterizationPreview
- For template matches, clicking the suggested fix opens the existing Script Generator flow with parameters prefilled from facts, ticket context, account/PSA config, and AI-suggested values.
- For no-template matches, show the three-option dialog:
- Run as one-off
- Run now, templatize after resolve
- Build as template now
- Persist the selected path on
session_suggested_fixes.user_decision. - Add
TemplateExtractionServicefor converting concrete scripts into proposed parameter schemas and templated bodies. - Link every script generation back to
ai_sessionsvia existingscript_generations.ai_session_id. Cmd+K → scriptopens the inline generator from the FlowPilot session; no Resolve keyboard shortcut is added.- Acceptance: matched templates prefill parameters; no-match flow shows three options; all options produce the correct session/template side effects.
Phase 6: Post-Resolve Templatize Prompt
- Add
TemplatizePromptafter successful Resolve only when:- the account setting allows prompts
- the session has pending
draft_templates - the user chose “Run now, templatize after resolve”
- Accept flow creates a real
script_templatesrow with:source_session_idsource_user_idsource_ticket_ref- accepted parameter schema/body edits
- Skip flow marks the draft rejected.
- “Don’t ask me again for this team” writes
{"templatize_prompt_enabled": false}toaccount_settings.preferences. - Script Library shows a pending-drafts badge/count for the account.
- Acceptance: accept creates a visible template with provenance; skip creates no template; disabled prompt is respected on the next resolve.
Phase 7: Polish
- Match the authoritative mockup HTML for spacing, colors, typography, and component structure; use PNGs for visual target confirmation.
- Add loading states for fact synthesis, preview generation, template extraction, PSA post/verify, and script generation.
- Add empty states for:
- no facts
- no questions
- no checks
- no suggested fix
- no pending draft templates
- Add keyboard shortcuts except Resolve:
Cmd+Kcommand paletteCmd+Entersend composerCmd+Gscript generator
- At widths below 1200px, collapse the task lane into a bottom drawer.
- Use existing design tokens where present; add missing tokens only if needed to match the mockups.
- Acceptance: major screens visually compare within the doc’s tolerance, no horizontal scroll at 1280px, mobile task lane works, and shortcuts do not conflict with browser reload.
Public Interfaces
- New backend routes use
/api/v1/ai-sessions/{id}/..., not/api/v1/sessions/{id}/.... - Existing chat creation/message APIs remain compatible.
session_typeremains queryable and stored, but frontend routing no longer sends chat sessions to/assistant.- New persistent entities:
session_factssession_suggested_fixesdraft_templatesaccount_settings
- New persisted artifact columns on
ai_sessionsstore resolution/escalation markdown and PSA post metadata.
Test Plan
- Migration tests:
- fresh DB upgrade succeeds
- downgrade succeeds if the repo expects reversible migrations
- new tables have RLS enabled/forced
- tenant policy includes
app.current_account_id
- Backend tests:
- fact CRUD and promotion authorization
- suggested-fix supersession and decision persistence
- preview generation cache invalidation
- Resolve/Escalate local-only behavior without PSA
- PSA status verification failure path
- draft-template accept/reject behavior
- Frontend tests:
- route redirects
- task lane rendering and persistence
- inline editing and preview refresh
- script generator option flows
- templatize prompt settings behavior
- responsive drawer behavior
- Manual QA:
- run through one ConnectWise linked Resolve
- run through one Escalate
- run one template-match script path
- run one no-template draft-template path through post-resolve save
Assumptions
- Phase 0 is included and must be complete before Phase 2 begins.
- No Resolve keyboard shortcut in this migration.
- Templatize prompt defaults to enabled.
- Resolution notes use engineer review plus Confirm & post, not supervisor staging.
- Existing component folders may be renamed opportunistically, but behavior and route migration matter more than directory-name purity.
- No backfill of What We Know for old sessions.
- Team Wiki compilation, SharePoint integration, marketplace sharing, and confidence-tier UI are out of scope.