Skip to content

fix: return fresh Session per call in _get_kba_session(), remove global session cache#21

Closed
Copilot wants to merge 14 commits intomainfrom
copilot/sub-pr-20
Closed

fix: return fresh Session per call in _get_kba_session(), remove global session cache#21
Copilot wants to merge 14 commits intomainfrom
copilot/sub-pr-20

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 10, 2026

_get_kba_session() cached a single global Session instance. Any caller using it as a context manager (with _get_kba_session() as session:) closes the session on exit, leaving the global pointing to a dead session for all subsequent requests — and leaking state across requests.

Changes

  • operations.py — drop _kba_session global; _get_kba_session() now returns Session(_kba_db_engine) on every call (only the engine is cached). All 8 KBA operation handlers updated to use the context-manager protocol:
# Before — shared session, closed by first context-manager caller
session = _get_kba_session()
kba_service = get_kba_service(session)
return kba_service.list_drafts(filters)

# After — fresh session, properly scoped and closed
with _get_kba_session() as session:
    kba_service = get_kba_service(session)
    return kba_service.list_drafts(filters)
  • auto_gen_service.py — remove _get_kba_service() helper (returned a KBAService wrapping an unmanaged, never-closed session). run_auto_generation() now opens an explicit context-managed session inline.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

SubSonic731 and others added 13 commits March 3, 2026 17:09
- struktur aufgeräumt
- README.md angepasst
- learning_mechanism.md plan erstellt
- desing fixes
Database & Backend:
- Add search_questions column migration in operations.py (ALTER TABLE for existing databases)
- Add /api/kba/drafts/{id}/replace endpoint in app.py
- Fix backward compatibility in kba_service.py (_table_to_draft, _draft_to_table)
- Add search questions generation to replace_draft workflow
- Fix NULL constraint errors by ensuring empty strings for required fields
- Update related_tickets validation: accept INC + 9-12 digits (was fixed at 12)

Frontend:
- Add Text component import to KBADrafterPage.jsx (fix TypeError)
- Add full-screen blur overlay with centered spinner during KBA generation
- Show overlay for both new draft creation and replacement operations
- Update styles: loadingOverlay with backdrop-filter blur effect

Documentation:
- Update kba_prompts.py: clarify related_tickets format with examples
- Update GENERAL.md: correct related_tickets format specification

Fixes #1 - KBA drafts not loading (missing DB column)
Fixes #2 - Replace endpoint not found (405 error)
Fixes #3 - Ticket ID validation too strict
- Add "Zurück zu Entwurf" button for reviewed status KBAs
- Add handleUnreview() handler to update status from "reviewed" to "draft"
- Import ArrowUndo24Regular icon for the unreview action
- Allow users to continue editing KBAs after review without deletion

This enables editing of reviewed KBAs that need changes before publishing.
… improvements

- Add ticket viewer dialog to display original incident details
  * New "Ticket" button in KBA header with DocumentSearch icon
  * Modal dialog showing incident data (ID, summary, status, priority, assignee, notes, resolution)
  * Backend endpoint /api/csv-tickets/by-incident/<incident_id> for incident ID lookup
  * Frontend API function getCSVTicketByIncident()

- Add unreview functionality for reviewed KBAs
  * "Zurück zu Entwurf" button with ArrowUndo icon
  * Allows resetting reviewed KBAs back to draft status for further editing

- Redesign KBA overview list
  * Replace corner delete button with professional overflow menu (⋮)
  * Horizontal layout: content left, status badge right-aligned, menu button
  * Menu component with delete option

- Add status filter dropdown to KBA overview
  * Filter options: All, draft, reviewed, published
  * Dropdown in card header for easy filtering

- Align EditableList "Add" button width with input fields
  * Use invisible placeholder buttons for exact width matching
  * Ensures consistent layout regardless of allowReorder setting

Files modified:
- frontend/src/features/kba-drafter/KBADrafterPage.jsx
- frontend/src/features/kba-drafter/components/EditableList.jsx
- frontend/src/services/api.js
- backend/app.py
- Fix delete draft error: use response.items instead of response.drafts
- Make AutoGenSettings card collapsible with chevron icon
  - Starts collapsed to reduce visual dominance
  - Smooth slide-down animation when expanded
  - Status badge visible in collapsed header
  - Clickable header with keyboard support (Enter key)
When clicking on a draft from the list after scrolling down,
the page now automatically scrolls to the top with a smooth animation.
This ensures users always start at the beginning of the draft content.
…changes

Replace native window.confirm() with ConfirmDialog component for better UX
consistency and modern appearance. Adds centered warning modal when user
attempts to discard unsaved changes (close draft, switch to preview, or
load different draft).

Changes:
- Add unsavedChangesDialogOpen and pendingAction states
- Update toggleEditMode, loadDraft, and handleClose to trigger modal
- Add handleDiscardChanges and handleCancelDiscard handlers
- Add ConfirmDialog with warning intent at end of component
Fixes:
- Fix CSV folder case mismatch (CSV -> csv) in app.py and operations.py
- Remove duplicate get_ticket_by_incident_id method in csv_data.py
- Replace inefficient len(session.exec().all()) with SQL COUNT(*) in kba_service.py
- Replace hardcoded placeholder credentials with env var lookups in kba_service.py
- Fix scheduler swallowing exceptions (remove bare raise, return None)
- Add settings reload at start of each scheduler run to fix race condition
- Add generation_warnings field to surface search questions failures to users
- Add schema migration for generation_warnings column

Tests:
- Add 19 Playwright e2e tests for KBA Drafter feature covering:
  page load, navigation, LLM health status, draft generation,
  draft display, draft list, editing, review workflow,
  duplicate handling, and backend API integration

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…dependency

- LiteLLM is now the default LLM backend (no .env or API key needed)
- Multistage model fallback chain: claude-sonnet-4 → gpt-4o → gpt-4o-mini
- OpenAI SDK still used when OPENAI_API_KEY is explicitly set
- agents.py and workbench service use ChatLiteLLM when no OpenAI key
- Added csv_ticket_stats and csv_sla_breach_tickets to agent tools
- Added KBA Drafter to Playwright nav tests and menu screenshots
- Added e2e tests: publish, delete, status filter, ticket viewer
- 32 unit tests + 5 live integration tests for LLM service
- Updated .env.example with LiteLLM-first documentation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI mentioned this pull request Mar 10, 2026
Base automatically changed from kba-draft-review-fixes to main March 10, 2026 21:52
… session cache

Co-authored-by: abossard <86611+abossard@users.noreply.github.com>
Copilot AI changed the title [WIP] WIP address feedback from review on Kba draft fix: return fresh Session per call in _get_kba_session(), remove global session cache Mar 10, 2026
@abossard abossard closed this Mar 18, 2026
@abossard abossard deleted the copilot/sub-pr-20 branch March 18, 2026 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants