-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathservice.py
More file actions
269 lines (231 loc) · 10.7 KB
/
Copy pathservice.py
File metadata and controls
269 lines (231 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""Public onboarding orchestration — :func:`start_onboarding`,
:func:`submit_onboarding_message`.
Orchestrates the 4 sub-modules:
1. :mod:`session_store` — TTL sweep, session lifecycle.
2. :mod:`questions` — the catalogue + ``OnboardingPatch`` shape.
3. :mod:`extraction` — Sonnet patch extraction with one-shot retry.
4. :mod:`aria_mcp.client` — the only sanctioned KB write path.
These are the two functions wired into ``modules/kb/router.py`` as
``POST /api/v1/kb/equipment/{cell_id}/onboarding/{start,message}``.
"""
from __future__ import annotations
import logging
import uuid
from typing import Any
from agents.kb_builder._ws_stub import broadcast_stub
from agents.kb_builder.onboarding import session_store
from agents.kb_builder.onboarding.extraction import extract_patch
from agents.kb_builder.onboarding.questions import QUESTIONS
from agents.kb_builder.onboarding.session_store import OnboardingSession
from aria_mcp.client import mcp_client
from core.database import db
from core.exceptions import ConflictError, NotFoundError, ValidationFailedError
from core.json_fields import decode_record
from modules.kb.repository import JSON_FIELDS, KbRepository
log = logging.getLogger("aria.kb_builder.onboarding.service")
def _format_vibration_value(thresholds: dict[str, Any]) -> str:
"""Return a human phrase for the vibration value extracted from the PDF.
Picks ``nominal`` first, then ``alert`` (PDFs sometimes only document the
alert level), and falls back to a neutral phrase when neither is set —
e.g. when ``bootstrap_thresholds`` filled the entry with a null-stub.
The unit defaults to ``mm/s`` because the field name (``vibration_mm_s``)
already carries it; if the PDF reports a different unit we honour it.
"""
entry = thresholds.get("vibration_mm_s") or {}
value = entry.get("nominal")
if value is None:
value = entry.get("alert")
if value is None:
return "the manufacturer's value"
unit = entry.get("unit") or "mm/s"
# ``value`` may come back as int, float or string from the JSONB column —
# str() keeps the rendering robust without importing Decimal.
return f"{value} {unit}"
async def start_onboarding(cell_id: int) -> dict[str, Any]:
"""Create an onboarding session for ``cell_id`` and return the first question.
Gates (any failure raises and prevents session creation):
1. ``equipment_kb`` row must exist for ``cell_id`` (else
:class:`NotFoundError` → 404).
2. ``structured_data.thresholds`` must be non-empty (else
:class:`ConflictError` → 409 — operator must upload a PDF first;
otherwise the first vibration patch trips
``_assert_thresholds_cover_signal_keys`` at HTTP 500. See issue #19 §3).
3. No active session may exist for the cell (else
:class:`ConflictError` → 409).
Returns:
``{session_id, cell_id, question_index: 0, question: str,
total_questions: int}``.
"""
session_store.cleanup_expired()
async with db.pool.acquire() as conn:
rec = await KbRepository(conn).get_by_cell(cell_id)
if rec is None:
raise NotFoundError(f"No equipment_kb row for cell {cell_id}")
kb_data = decode_record(rec, JSON_FIELDS)
structured = kb_data.get("structured_data") or {}
if not structured or not structured.get("thresholds"):
raise ConflictError(
f"Upload a PDF manual first (POST /kb/equipment/{cell_id}/upload) "
"before starting onboarding. The KB must have at least one threshold."
)
existing_sid = session_store.SESSIONS_BY_CELL.get(cell_id)
if existing_sid and existing_sid in session_store.SESSIONS:
raise ConflictError(
f"Onboarding already in progress for cell {cell_id} "
f"(session {existing_sid}). Complete or wait for TTL to expire."
)
session_id = str(uuid.uuid4())
session = OnboardingSession(session_id=session_id, cell_id=cell_id)
session_store.SESSIONS[session_id] = session
session_store.SESSIONS_BY_CELL[cell_id] = session_id
log.info("onboarding: started session=%s cell=%d", session_id, cell_id)
first = QUESTIONS[0]
# Render Q1 with the manufacturer's vibration threshold extracted from the
# PDF — that single value is the "aha" moment of scene 1 (operator sees
# the spec, gives their observed value, ARIA recalibrates).
question_text = first["text"].format(
mfr_value=_format_vibration_value(structured.get("thresholds") or {})
)
return {
"session_id": session_id,
"cell_id": cell_id,
"question_index": first["index"],
"question": question_text,
"total_questions": len(QUESTIONS),
}
async def submit_onboarding_message(session_id: str, answer: str) -> dict[str, Any]:
"""Process one operator answer and advance the session.
Pipeline per question:
1. Resolve session (drop if expired).
2. Look up the current question's ``patch_hint``.
3. :func:`~agents.kb_builder.onboarding.extraction.extract_patch` —
Sonnet w/ Pydantic validation + 1 retry.
4. ``mcp_client.call_tool("update_equipment_kb", ...)`` with
``source="onboarding"`` and ``calibrated_by="operator"``. On the final
question (index ``len(QUESTIONS) - 1``) we also pass
``onboarding_complete=True`` so both the column and
``kb_meta.onboarding_complete`` flip — Sentinel (M4.2) keys off this.
5. Return either the next question or ``{complete: True, kb: ...}``.
The session record is dropped from the store once Q4 succeeds. If Sonnet
extraction fails twice or the MCP write errors, the session stays at the
same ``question_index`` so the operator can re-answer.
Args:
session_id: ID returned by :func:`start_onboarding`.
answer: Free-text operator answer.
Returns:
Either ``{session_id, question_index, question, total_questions}`` or
``{session_id, complete: True, kb: dict}``.
Raises:
NotFoundError: When the session id is unknown or has expired.
ValueError | ValidationError: When Sonnet keeps emitting bad JSON.
ValidationFailedError: When the MCP write fails or the session is
already past Q4.
"""
session_store.cleanup_expired()
session = session_store.SESSIONS.get(session_id)
if session is None:
raise NotFoundError(
f"Onboarding session {session_id} not found or expired (TTL "
f"{session_store.SESSION_TTL // 60} min)"
)
if session.question_index >= len(QUESTIONS):
# Defensive: a completed session should already be dropped, but if a
# client races two /message calls we surface a clear error instead of
# an IndexError on QUESTIONS.
session_store.drop(session)
raise ValidationFailedError(
f"Session {session_id} already completed all {len(QUESTIONS)} questions"
)
question = QUESTIONS[session.question_index]
is_final = session.question_index == len(QUESTIONS) - 1
# Step 1 — extract structured patch (may raise ValidationError after retry).
patch = await extract_patch(answer, question["patch_hint"], session.cell_id)
# Step 2 — write via MCP (the only sanctioned write path).
tool_args: dict[str, Any] = {
"cell_id": session.cell_id,
"structured_data_patch": patch,
"source": "onboarding",
"calibrated_by": "operator",
}
if is_final:
tool_args["onboarding_complete"] = True
result = await mcp_client.call_tool("update_equipment_kb", tool_args)
if result.is_error:
# Keep the session at the same question_index so the operator can
# re-answer. Surface the tool's own message for debuggability.
raise ValidationFailedError(
f"KB update failed for cell {session.cell_id}: {result.content}"
)
# Record the exchange and advance.
session.messages.append(
{"question_index": session.question_index, "answer": answer, "patch": patch}
)
session.question_index += 1
# M3.6 — emit per-question progress AFTER the MCP write completes
# (issue #22 acceptance #5: "All events are emitted AFTER the
# corresponding MCP write completes (not before)"). Will become
# ``ws_manager.broadcast`` once M4.1 (#23) lands.
await broadcast_stub(
"ui_render",
{
"agent": "kb_builder",
"component": "kb_progress",
"props": {
"cell_id": session.cell_id,
"steps": [
{
"label": f"Question {q['index'] + 1}/{len(QUESTIONS)}",
"status": (
"done"
if q["index"] < session.question_index
else (
"in_progress" if q["index"] == session.question_index else "pending"
)
),
}
for q in QUESTIONS
],
},
"turn_id": None, # set by orchestrator ContextVar after M4.1 (#23)
},
)
if is_final:
# Re-read the row through the repository so the response shape matches
# ``EquipmentKbOut`` (the router serialises it for the client).
async with db.pool.acquire() as conn:
rec = await KbRepository(conn).get_by_cell(session.cell_id)
session_store.drop(session)
log.info("onboarding: completed session=%s cell=%d", session_id, session.cell_id)
if rec is None:
# Should be impossible — update_equipment_kb just returned success.
raise NotFoundError(f"No equipment_kb row for cell {session.cell_id} after onboarding")
# M3.6 — final card AFTER the DB re-read so the frontend's re-fetch
# via ``GET /api/v1/kb/equipment/{cell_id}`` (M8.2) sees the
# post-onboarding KB. Will become ``ws_manager.broadcast`` after M4.1.
await broadcast_stub(
"ui_render",
{
"agent": "kb_builder",
"component": "equipment_kb_card",
"props": {
"cell_id": session.cell_id,
"highlight_fields": [
"thresholds.vibration_mm_s",
"failure_patterns",
],
},
"turn_id": None, # set by orchestrator ContextVar after M4.1 (#23)
},
)
return {
"session_id": session_id,
"complete": True,
"kb": decode_record(rec, JSON_FIELDS),
}
next_q = QUESTIONS[session.question_index]
return {
"session_id": session_id,
"question_index": next_q["index"],
"question": next_q["text"],
"total_questions": len(QUESTIONS),
}