forked from odysseus-dev/odysseus
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathside_threads.py
More file actions
1727 lines (1550 loc) · 69.7 KB
/
Copy pathside_threads.py
File metadata and controls
1727 lines (1550 loc) · 69.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Excursos (side threads) — pure logic + DB access, no FastAPI.
CONTRATO_EXCURSOS.md, Lote A. The idea (from ThoughtDAG): a side thread is a
normal Faustus session wired to its "parent" by a `branch` cable
(`core.database.SessionWire`); what the model sees is exactly what is
cabled to the node, nothing more. A side thread never copies the parent's
messages the way `/fork` does — `inherited_context` (the hook this module
exists for) reads the wire back at prompt-build time instead, so the parent
transcript is the single source of truth for everything downstream of it.
Every public function here takes `owner: Optional[str]`. When it is not
None, any session the function reads must belong to that owner — a session
belonging to someone else is treated exactly like a session that does not
exist (never leaked, never distinguished in an error). `owner=None` means
single-user / unscoped mode, matching how `routes.session_routes.
_verify_session_owner` and the rest of the codebase treat the nullable
`sessions.owner` column.
No function in this module is ever called by agent tooling — every wire is
created by a human through the HTTP routes in `routes/side_thread_routes.py`
(CONTRATO_EXCURSOS: "ningún cable se crea sin que lo pida un humano por la
API; ninguna tool de agente").
"""
from __future__ import annotations
import hashlib
import json
import logging
import uuid
from datetime import datetime, timezone
from types import SimpleNamespace
from typing import Any, Dict, List, Optional, Tuple
from sqlalchemy import func
from core.database import (
ChatMessage as DbChatMessage,
Document as DbDocument,
Session as DbSession,
SessionLocal,
SessionWire as DbSessionWire,
)
from src.model_context import estimate_tokens
from src.prompt_security import untrusted_context_message
logger = logging.getLogger(__name__)
# F2 ("materiales cableados") limits, enforced in `add_material`/`update_material`.
MAX_QUOTES = 8
MAX_QUOTE_CHARS = 4000
MAX_NOTE_CHARS = 8000
MAX_DOC_FULL_CHARS = 40000
# Recursion / traversal ceilings (CONTRATO_EXCURSOS: "Profundidad máx 8" for
# inherited context, "Profundidad máxima 32" for the thought map). Bounding
# these is not an optimization — a corrupted or (despite the invariant)
# cyclic wire graph must degrade to a truncated result, never an infinite
# loop inside a chat request.
MAX_INHERITED_DEPTH = 8
MAX_THOUGHT_MAP_DEPTH = 32
_MISSING_ANCHOR_NOTE = (
"[Note: the turn this side thread branched from no longer exists in the "
"parent conversation; the whole remaining parent transcript is included "
"instead]"
)
class SideThreadError(Exception):
"""A request the caller must fix — the routes hand this back verbatim as
the flat ``{"error", "error_class"}`` body CONTRATO_EXCURSOS requires,
so this module never has to know about HTTP and the routes never have
to re-derive a status code or message from a generic exception.
"""
def __init__(self, message: str, error_class: str, status: int = 400):
super().__init__(message)
self.error_class = error_class
self.status = status
# ---------------------------------------------------------------------------
# Small DB-only helpers shared by every function below
# ---------------------------------------------------------------------------
def _owned_row(db, session_id: Optional[str], owner: Optional[str]) -> Optional[DbSession]:
"""The `sessions` row for `session_id`, or None if absent or another
owner's — the "treated as inexistent" rule applied at the DB layer."""
if not session_id:
return None
row = db.query(DbSession).filter(DbSession.id == session_id).first()
if row is None:
return None
if owner is not None and row.owner != owner:
return None
return row
def _owned_document(db, document_id: Optional[str], owner: Optional[str]) -> Optional[DbDocument]:
"""The `documents` row for `document_id`, owner-checked the same way
`_owned_row` treats sessions: absent or another owner's is "does not
exist", `owner=None` is unscoped (single-user mode). This is the same
field `routes/chat_routes.py::_doc_context_messages` filters on
(`Document.owner`); it is reimplemented locally rather than imported
because that helper's own null-owner handling is tied to HTTP auth
semantics this module does not share (see the module docstring)."""
if not document_id:
return None
row = db.query(DbDocument).filter(DbDocument.id == document_id).first()
if row is None:
return None
if owner is not None and row.owner != owner:
return None
return row
def _owned_session_via_manager(session_manager, owner: Optional[str], session_id: Optional[str]):
"""`session_manager.get_session(session_id)`, owner-checked, tolerant of
a missing/foreign session (returns None rather than raising) — every
caller here treats "not mine" and "does not exist" identically."""
if session_manager is None or not session_id:
return None
try:
sess = session_manager.get_session(session_id)
except KeyError:
return None
except Exception:
logger.debug("side_threads: get_session(%s) failed", session_id, exc_info=True)
return None
if sess is None:
return None
if owner is not None and getattr(sess, "owner", None) != owner:
return None
return sess
def _json_loads_or_none(raw: Optional[str]):
"""Tolerant JSON parse for the `quotes`/`ranges` TEXT columns — `None`
for an absent, empty or corrupt value rather than raising, since these
are display/rendering inputs, never load-bearing for correctness."""
if not raw:
return None
try:
return json.loads(raw)
except Exception:
return None
def _wire_to_dict(wire: DbSessionWire) -> Dict[str, Any]:
return {
"id": wire.id,
"kind": wire.kind,
"owner": wire.owner,
"source_session_id": wire.source_session_id,
"target_session_id": wire.target_session_id,
"anchor_index": wire.anchor_index,
"anchor_passage": wire.anchor_passage,
"depth": wire.depth,
"context_order": wire.context_order,
"archived": bool(wire.archived),
"source_fingerprint": wire.source_fingerprint,
"document_id": getattr(wire, "document_id", None),
"note_text": getattr(wire, "note_text", None),
"quotes": _json_loads_or_none(getattr(wire, "quotes", None)),
"ranges": _json_loads_or_none(getattr(wire, "ranges", None)),
"created_at": wire.created_at.isoformat() if wire.created_at else None,
}
def _row_meta(row_or_msg) -> Dict[str, Any]:
"""The parsed `metadata` dict for a persisted `chat_messages` row (a raw
`core.database.ChatMessage`) or an already-hydrated
`core.models.ChatMessage` — tolerant of a missing, already-a-dict, or
corrupt JSON value; always a dict, never `None`, so callers can
`.get(...)` straight off the result.
`meta_data` (the raw DB row's JSON column, aliased in Python because the
underlying column is literally named ``metadata``) is checked FIRST and
decisively: every SQLAlchemy declarative instance carries its OWN
`.metadata` class attribute — the mapper's schema registry
(`core.database.Base.metadata`), never `None` and never the message's
own metadata — so testing `.metadata is None` first (as earlier code
here did) always found that registry object instead and silently
treated every raw DB row as metadata-less.
"""
if hasattr(row_or_msg, "meta_data"):
raw = row_or_msg.meta_data
if not raw:
return {}
try:
meta = json.loads(raw) if isinstance(raw, str) else raw
except Exception:
return {}
return meta if isinstance(meta, dict) else {}
meta = getattr(row_or_msg, "metadata", None)
return meta if isinstance(meta, dict) else {}
def _is_slash(row_or_msg) -> bool:
"""True for a persisted slash-command reply (`metadata.source == 'slash'`
on a `core.models.ChatMessage`, or the equivalent parsed
`chat_messages.metadata` JSON on a raw DB row) — UI chatter that never
reaches the model, mirroring `core.models.Session.get_context_messages`.
"""
return _row_meta(row_or_msg).get("source") == "slash"
def _history_rows(session_id: str) -> List[DbChatMessage]:
"""Every `chat_messages` row for a session, oldest first, straight from
the DB — used by the DB-only functions (`add_reference`, `wires_for`,
chain titles) that have no `SessionManager` to hydrate through. Side
threads are expected to stay small, so reading the whole transcript here
is simplicity over a micro-optimization, not a scalability bet.
"""
db = SessionLocal()
try:
return (
db.query(DbChatMessage)
.filter(DbChatMessage.session_id == session_id)
.order_by(DbChatMessage.timestamp)
.all()
)
finally:
db.close()
def _first_user_title(rows: List[DbChatMessage]) -> Optional[str]:
for row in rows:
if row.role != "user" or _is_slash(row):
continue
content = row.content if isinstance(row.content, str) else str(row.content or "")
return content[:60]
return None
def _chain_titles(session_id: str, max_depth: int = MAX_INHERITED_DEPTH) -> List[str]:
"""Root-to-leaf opening questions for a side thread's ancestor chain —
the `Trail (upstream questions): q1 → q2 → q3` line in `reference_block`.
Walks `branch` wires upward from `session_id` itself (so the chain's own
opening question is the LAST entry, closest to the quoted exchange),
through each ancestor excurso, collecting the first non-slash user
message of each hop. Self-contained (opens its own DB session) since it
is reused from both DB-only and SessionManager-backed callers, and it is
pure text — nothing here needs SessionManager's caching semantics.
"""
chain: List[str] = []
seen = set()
current = session_id
depth = 0
db = SessionLocal()
try:
while current and depth < max_depth and current not in seen:
seen.add(current)
rows = (
db.query(DbChatMessage)
.filter(DbChatMessage.session_id == current)
.order_by(DbChatMessage.timestamp)
.all()
)
title = _first_user_title(rows)
if title:
chain.append(title)
wire = (
db.query(DbSessionWire)
.filter(DbSessionWire.kind == "branch", DbSessionWire.target_session_id == current)
.first()
)
if wire is None:
break
current = wire.source_session_id
depth += 1
finally:
db.close()
chain.reverse()
return chain
def _reference_block_for_db(session_id: str, name: str, depth: str) -> str:
"""DB-only convenience: load a session's (slash-filtered) transcript and
render its `[Reference: ...]` block, for callers (`add_reference`,
`wires_for`) that have no `SessionManager` handy."""
rows = [r for r in _history_rows(session_id) if not _is_slash(r)]
chain_titles = _chain_titles(session_id) if depth == "quote" else []
return reference_block(SimpleNamespace(name=name, history=rows), depth, chain_titles)
# ---------------------------------------------------------------------------
# Pure helpers
# ---------------------------------------------------------------------------
def fingerprint(session) -> str:
"""Short content identity of a session's transcript, for staleness
detection on a `reference` wire.
Hashes the total message count plus the last two messages' role+content
(12 hex chars of sha256) — cheap enough to run on every `wires_for` call.
Growing the side thread, or editing its tail, changes the hash; renaming
the session or editing an earlier message does not. `session` needs only
a `.history` of role/content-bearing entries (a `core.models.ChatMessage`
list, a list of dicts, or raw `core.database.ChatMessage` rows all work).
"""
history = list(getattr(session, "history", None) or [])
parts = [str(len(history))]
for msg in history[-2:]:
role = getattr(msg, "role", None)
if role is None and isinstance(msg, dict):
role = msg.get("role")
content = getattr(msg, "content", None)
if content is None and isinstance(msg, dict):
content = msg.get("content")
parts.append(f"{role}:{content}")
digest = hashlib.sha256("\x00".join(parts).encode("utf-8", "replace")).hexdigest()
return digest[:12]
def document_fingerprint(doc: DbDocument) -> str:
"""F2's staleness identity for a wired document: a short hash of its
current content plus `version_count`. `version_count` alone would miss
an edit that landed without bumping it (unlikely but not contractually
guaranteed by every write path); the content hash alone would miss
nothing changing while a version row is still added. Together, either
one changing flips this."""
content = doc.current_content or ""
digest = hashlib.sha256(content.encode("utf-8", "replace")).hexdigest()[:12]
return f"{digest}:{doc.version_count or 0}"
def note_fingerprint(note_text: Optional[str]) -> str:
"""F2's staleness identity for a `note` material — the note's own text
IS the source, so this is just its content hash (12 hex chars, same
shape as `fingerprint`/`document_fingerprint` for a uniform
`source_fingerprint` column)."""
return hashlib.sha256((note_text or "").encode("utf-8", "replace")).hexdigest()[:12]
def reference_block(source_session, depth: str, chain_titles: List[str]) -> str:
"""Render one `[Reference: ...]` block — the sole channel a side
thread's content re-enters another conversation through (CONTRATO_
EXCURSOS: "un bloque de referencia explícito", never an automatic
replay or a raw history splice).
Pure: reads only `source_session.name` and `.history` (role/content
entries, already slash-filtered by the caller), so it is testable
without touching the database. `depth='full'` renders every Q/A pair
with no trail; the default `'quote'` renders the trail (when
`chain_titles` is non-empty) plus only the latest exchange.
"""
name = getattr(source_session, "name", "") or ""
history = list(getattr(source_session, "history", None) or [])
lines = [f"[Reference: {name}]"]
def _role(msg):
role = getattr(msg, "role", None)
if role is None and isinstance(msg, dict):
role = msg.get("role")
return role
def _text(msg):
content = getattr(msg, "content", None)
if content is None and isinstance(msg, dict):
content = msg.get("content")
return content if isinstance(content, str) else str(content if content is not None else "")
if depth == "full":
for msg in history:
role = _role(msg)
if role == "user":
lines.append(f"Q: {_text(msg)}")
elif role == "assistant":
lines.append(f"A: {_text(msg)}")
return "\n".join(lines)
# 'quote' (default): trail of upstream questions, then only the latest exchange.
if chain_titles:
lines.append("Trail (upstream questions): " + " → ".join(chain_titles))
last_user = next((m for m in reversed(history) if _role(m) == "user"), None)
last_assistant = next((m for m in reversed(history) if _role(m) == "assistant"), None)
if last_user is not None:
lines.append(f"Q: {_text(last_user)}")
if last_assistant is not None:
lines.append(f"A: {_text(last_assistant)}")
return "\n".join(lines)
def _load_document_for_wire(document_id: Optional[str]) -> Optional[DbDocument]:
"""Best-effort live fetch of a wired document, no owner check.
The wire's own creation/update already verified ownership once; reading
it back at prompt-build/preview time re-checking on every turn would
just be a second DB round trip for the same answer. A document that
vanished (or changed owner) since simply renders nothing here, exactly
like a `reference`'s source session disappearing in `_branch_chain`."""
if not document_id:
return None
db = SessionLocal()
try:
return db.query(DbDocument).filter(DbDocument.id == document_id).first()
finally:
db.close()
def material_block(wire: DbSessionWire) -> Optional[Dict[str, Any]]:
"""Render one `document`/`note` material wire into the message the model
receives (F2). Returns `None` for a `document` wire whose document has
since been deleted — the caller drops it, the same "source vanished, the
block just is not there" posture `_branch_chain`/`_reference_blocks`
already take, never a placeholder that looks like real content.
`note`: the user's own words, trusted, wrapped only enough to mark it as
a standing note rather than the live turn (`[Note]\\n<text>`) — see F2:
"texto del propio usuario, confiable".
`document`: routed through `src.prompt_security.untrusted_context_message`,
same as `routes/chat_routes.py::_doc_context_messages`'s transient doc
chips — the model reads it as something it was told, not something the
user typed. `depth='selection'` numbers the pinned quotes; `depth='full'`
reads `Document.current_content` LIVE (so a `full` material always shows
the document's current text, never a frozen copy) truncated to
`MAX_DOC_FULL_CHARS` with a trailing notice.
"""
if wire.kind == "note":
return {"role": "user", "content": "[Note]\n" + (wire.note_text or "")}
doc = _load_document_for_wire(wire.document_id)
if doc is None:
return None
title = doc.title or wire.document_id or "Untitled"
if wire.depth == "full":
content = doc.current_content or ""
if len(content) > MAX_DOC_FULL_CHARS:
body = content[:MAX_DOC_FULL_CHARS] + "\n\n[…truncated at 40,000 characters…]"
else:
body = content
else:
quotes = _json_loads_or_none(wire.quotes) or []
body = "\n\n".join(f'{i + 1}. "{q}"' for i, q in enumerate(quotes)) or "(no quotes recorded)"
return untrusted_context_message(f"wired document context: {title}", body)
def material_fingerprint(wire: DbSessionWire) -> Optional[str]:
"""The CURRENT source fingerprint for a material wire — `None` when a
`document` wire's document has been deleted (nothing to be stale
against; `wires_for` treats that as `stale: False`, matching how a
`reference` with a vanished source is handled)."""
if wire.kind == "note":
return note_fingerprint(wire.note_text)
doc = _load_document_for_wire(wire.document_id)
return document_fingerprint(doc) if doc is not None else None
def _stale_turns_for_wire(session_id: str, wire_id: str, current_fingerprint: Optional[str]) -> Dict[str, Any]:
"""F1: `{"count", "last_index"}` — how many of `session_id`'s OWN
assistant turns were saved with `metadata.side_thread_wires` recording
this wire at a fingerprint that is no longer `current_fingerprint`.
This is a different, more precise question than `wires_for`'s `stale`
flag (which only says the wire itself is stale *right now*): it asks
which already-written REPLIES were composed against an earlier version
of the block, so the Studio can flag exactly those turns for a targeted
"regenerate" instead of a blanket "something changed" banner.
`current_fingerprint=None` (source vanished) reports nothing stale —
there is no current version to compare against.
"""
if current_fingerprint is None:
return {"count": 0, "last_index": None}
count = 0
last_index: Optional[int] = None
for idx, row in enumerate(_history_rows(session_id)):
if row.role != "assistant":
continue
snapshot = _row_meta(row).get("side_thread_wires")
if not isinstance(snapshot, list):
continue
for entry in snapshot:
if isinstance(entry, dict) and entry.get("wire_id") == wire_id:
if entry.get("fingerprint") != current_fingerprint:
count += 1
last_index = idx
break
return {"count": count, "last_index": last_index}
def _current_source_fingerprint(db, owner: Optional[str], wire: DbSessionWire) -> Optional[str]:
"""The live fingerprint of a wire's source, dispatched by kind — the one
number `wires_for`'s `stale` and `_stale_turns_for_wire` both compare
the stored `source_fingerprint` (or a saved snapshot entry) against."""
if wire.kind == "reference":
src_row = _owned_row(db, wire.source_session_id, owner)
if src_row is None:
return None
return fingerprint(SimpleNamespace(history=_history_rows(src_row.id)))
if wire.kind in ("document", "note"):
return material_fingerprint(wire)
return None
def _wire_label(db, owner: Optional[str], wire: DbSessionWire) -> str:
"""Human-readable name for a wire's source, for `stale_turns_map`'s
per-turn `{"wire_id", "label"}` entries — the excurso's name, the
document's title, or the literal word "Note" (matching the `[Note]`
block itself)."""
if wire.kind == "reference":
src_row = _owned_row(db, wire.source_session_id, owner)
return src_row.name if src_row is not None else "(removed)"
if wire.kind == "document":
doc = _owned_document(db, wire.document_id, owner)
return doc.title if doc is not None else "(removed)"
if wire.kind == "note":
return "Note"
return wire.kind
# ---------------------------------------------------------------------------
# Creating a side thread
# ---------------------------------------------------------------------------
def create_side_thread(
session_manager,
owner: Optional[str],
parent_id: str,
anchor_index: int,
passage: Optional[str] = None,
question: Optional[str] = None,
) -> Dict[str, Any]:
"""Create a brand-new session wired to `parent_id` by a `branch` cable.
Unlike `/api/session/{id}/fork` (`routes/history/history_routes.py`),
NOTHING is copied into the new session's history — inheritance is
entirely through the wire, read back by `inherited_context` at
prompt-build time. The new session starts empty and the parent's own
history is never touched (byte-for-byte, message-for-message).
`question`, when given, is returned as-is (never sent to any model
here) — the caller (the route) hands it back for the Studio to seed the
new session's composer with.
"""
parent = _owned_session_via_manager(session_manager, owner, parent_id)
if parent is None:
raise SideThreadError(f"Session {parent_id} not found", "excursos.not_found", 404)
history_len = len(parent.history or [])
if (
not isinstance(anchor_index, int)
or isinstance(anchor_index, bool)
or anchor_index < 0
or anchor_index >= history_len
):
raise SideThreadError(
f"anchor_index {anchor_index!r} is out of range for a "
f"{history_len}-message parent history",
"excursos.anchor_out_of_range",
400,
)
passage_clean = str(passage)[:2000] if passage else None
base = passage_clean or (str(question) if question else None) or (parent.name or "")
new_name = "↳ " + (base[:48] if base else (parent.name or ""))
new_id = str(uuid.uuid4())
parent_owner = getattr(parent, "owner", None)
session_manager.create_session(
session_id=new_id,
name=new_name,
endpoint_url=parent.endpoint_url,
model=parent.model,
rag=False,
owner=parent_owner,
folder=getattr(parent, "folder", None),
mode=getattr(parent, "mode", None),
project_id=getattr(parent, "project_id", None),
)
db = SessionLocal()
try:
wire = DbSessionWire(
id=str(uuid.uuid4()),
owner=parent_owner,
kind="branch",
source_session_id=parent_id,
target_session_id=new_id,
anchor_index=anchor_index,
anchor_passage=passage_clean,
depth="quote",
context_order=0,
archived=False,
source_fingerprint=None,
created_at=datetime.now(timezone.utc),
)
db.add(wire)
db.commit()
db.refresh(wire)
wire_dict = _wire_to_dict(wire)
finally:
db.close()
try:
from src.event_bus import fire_event
fire_event("session_created", parent_owner)
except Exception:
logger.debug("session_created event dispatch failed for side thread", exc_info=True)
return {"session_id": new_id, "wire": wire_dict}
# ---------------------------------------------------------------------------
# References ("traer de vuelta")
# ---------------------------------------------------------------------------
def add_reference(owner: Optional[str], source_id: str, target_id: str, depth: str = "quote") -> Dict[str, Any]:
"""Wire `source_id` (a side thread) back into `target_id` as one
`[Reference: ...]` block. Idempotent: an existing non-archived
`reference` wire between the same pair is returned unchanged rather than
duplicated — clicking "Traer de vuelta" twice is a no-op, not two blocks.
"""
if source_id == target_id:
raise SideThreadError("A session cannot reference itself", "excursos.self_reference", 400)
if depth not in ("quote", "full"):
raise SideThreadError(f"Unknown reference depth: {depth!r}", "excursos.bad_depth", 400)
db = SessionLocal()
try:
source_row = _owned_row(db, source_id, owner)
target_row = _owned_row(db, target_id, owner)
if source_row is None or target_row is None:
raise SideThreadError("Session not found", "excursos.not_found", 404)
pair = (
db.query(DbSessionWire)
.filter(
DbSessionWire.kind == "reference",
DbSessionWire.source_session_id == source_id,
DbSessionWire.target_session_id == target_id,
)
.order_by(DbSessionWire.archived.asc(), DbSessionWire.created_at.desc())
.all()
)
existing = next((w for w in pair if not w.archived), None)
withdrawn = next((w for w in pair if w.archived), None)
if existing is not None:
wire = existing
elif withdrawn is not None:
# "Withdraw" archives the wire; "Wire" again must bring THAT wire
# back, not stack a second row behind it (seen live: the panel
# showed one child, the table grew one orphan per toggle). The
# user asked for it fresh, so depth and fingerprint follow the
# request and the block is current again.
withdrawn.archived = False
withdrawn.depth = depth
withdrawn.source_fingerprint = fingerprint(SimpleNamespace(history=_history_rows(source_id)))
db.commit()
db.refresh(withdrawn)
wire = withdrawn
else:
max_order = (
db.query(func.max(DbSessionWire.context_order))
.filter(DbSessionWire.kind == "reference", DbSessionWire.target_session_id == target_id)
.scalar()
)
next_order = (max_order + 1) if max_order is not None else 0
wire = DbSessionWire(
id=str(uuid.uuid4()),
owner=source_row.owner if source_row.owner is not None else owner,
kind="reference",
source_session_id=source_id,
target_session_id=target_id,
depth=depth,
context_order=next_order,
archived=False,
source_fingerprint=fingerprint(SimpleNamespace(history=_history_rows(source_id))),
created_at=datetime.now(timezone.utc),
)
db.add(wire)
db.commit()
db.refresh(wire)
wire_dict = _wire_to_dict(wire)
source_name = source_row.name
finally:
db.close()
# `tokens` (route response shape: {"wire","tokens"}) needs the rendered
# block, which needs the source's own transcript — computed after the
# write commits so a slow render never holds the DB session open.
block = _reference_block_for_db(source_id, source_name, wire_dict["depth"])
tokens = estimate_tokens([{"role": "user", "content": block}])
return {"wire": wire_dict, "tokens": tokens}
def update_reference(
owner: Optional[str],
wire_id: str,
*,
depth: Optional[str] = None,
archived: Optional[bool] = None,
context_order: Optional[int] = None,
refresh: bool = False,
) -> Dict[str, Any]:
"""Patch a `reference` wire. `refresh=True` recomputes
`source_fingerprint` from the side thread's CURRENT tail — the user
"accepting" a side thread that grew since it was last wired in, clearing
`stale`."""
if depth is not None and depth not in ("quote", "full"):
raise SideThreadError(f"Unknown reference depth: {depth!r}", "excursos.bad_depth", 400)
db = SessionLocal()
try:
wire = (
db.query(DbSessionWire)
.filter(DbSessionWire.id == wire_id, DbSessionWire.kind == "reference")
.first()
)
if wire is None:
raise SideThreadError(f"Reference {wire_id} not found", "excursos.not_found", 404)
if owner is not None:
if _owned_row(db, wire.source_session_id, owner) is None or _owned_row(db, wire.target_session_id, owner) is None:
raise SideThreadError(f"Reference {wire_id} not found", "excursos.not_found", 404)
if depth is not None:
wire.depth = depth
if archived is not None:
wire.archived = bool(archived)
if context_order is not None:
wire.context_order = int(context_order)
if refresh:
wire.source_fingerprint = fingerprint(SimpleNamespace(history=_history_rows(wire.source_session_id)))
db.commit()
db.refresh(wire)
return {"wire": _wire_to_dict(wire)}
finally:
db.close()
def remove_reference(owner: Optional[str], wire_id: str) -> bool:
"""DELETE a `reference` wire physically. The side thread it pointed at
is untouched and keeps existing — only the cable is gone."""
db = SessionLocal()
try:
wire = (
db.query(DbSessionWire)
.filter(DbSessionWire.id == wire_id, DbSessionWire.kind == "reference")
.first()
)
if wire is None:
return False
if owner is not None:
if _owned_row(db, wire.source_session_id, owner) is None or _owned_row(db, wire.target_session_id, owner) is None:
return False
db.delete(wire)
db.commit()
return True
finally:
db.close()
# ---------------------------------------------------------------------------
# Materials — documents and notes wired directly into a session (F2)
# ---------------------------------------------------------------------------
def add_material(
owner: Optional[str],
session_id: str,
*,
kind: str,
document_id: Optional[str] = None,
depth: str = "selection",
quotes: Optional[List[str]] = None,
ranges: Optional[List[Dict[str, int]]] = None,
note_text: Optional[str] = None,
) -> Dict[str, Any]:
"""Wire a document fragment or a free-form note into `session_id`'s own
context PERMANENTLY (CONTRATO_CABLES2 F2) — unlike the transient,
single-turn document chips `routes/chat_routes.py::_doc_context_messages`
builds, a material wire stays cabled turn after turn until the user
withdraws (`archived`) or removes it.
`kind='document'`: `document_id` must belong to `owner` (same ownership
rule `_doc_context_messages` applies). `depth='selection'` requires 1-8
non-empty `quotes`, each <= `MAX_QUOTE_CHARS`; `depth='full'` ignores
`quotes`/`ranges` and always reads the document's live content when the
block is built. Idempotent on (document, depth, quotes): wiring the same
selection twice returns the existing wire rather than duplicating it.
`kind='note'`: `note_text` must be 1-`MAX_NOTE_CHARS` chars; `depth` is
ignored. Never deduplicated — two notes are two notes, even with
identical text, since (unlike a document quote) there is no shared
upstream identity two calls could be "the same wiring" of.
"""
if kind not in ("document", "note"):
raise SideThreadError(f"Unknown material kind: {kind!r}", "excursos.bad_kind", 400)
db = SessionLocal()
try:
target_row = _owned_row(db, session_id, owner)
if target_row is None:
raise SideThreadError(f"Session {session_id} not found", "excursos.not_found", 404)
owner_for_wire = target_row.owner if target_row.owner is not None else owner
def _next_order() -> int:
max_order = (
db.query(func.max(DbSessionWire.context_order))
.filter(
DbSessionWire.kind.in_(("document", "note")),
DbSessionWire.target_session_id == session_id,
)
.scalar()
)
return (max_order + 1) if max_order is not None else 0
if kind == "document":
if depth not in ("selection", "full"):
raise SideThreadError(f"Unknown material depth: {depth!r}", "excursos.bad_depth", 400)
doc = _owned_document(db, document_id, owner)
if doc is None:
raise SideThreadError(f"Document {document_id} not found", "excursos.not_found", 404)
clean_quotes: List[str] = []
if depth == "selection":
raw_quotes = [q for q in (quotes or []) if isinstance(q, str) and q.strip()]
if not raw_quotes:
raise SideThreadError(
"depth='selection' requires at least one quote", "excursos.quotes_required", 400
)
if len(raw_quotes) > MAX_QUOTES:
raise SideThreadError(
f"At most {MAX_QUOTES} quotes are allowed", "excursos.quotes_required", 400
)
for q in raw_quotes:
if len(q) > MAX_QUOTE_CHARS:
raise SideThreadError(
f"A quote exceeds {MAX_QUOTE_CHARS} characters", "excursos.quotes_required", 400
)
clean_quotes = raw_quotes
clean_ranges = [r for r in ranges if isinstance(r, dict)] if isinstance(ranges, list) else None
existing = (
db.query(DbSessionWire)
.filter(
DbSessionWire.kind == "document",
DbSessionWire.target_session_id == session_id,
DbSessionWire.document_id == document_id,
DbSessionWire.archived == False, # noqa: E712
)
.all()
)
match = next(
(
w for w in existing
if w.depth == depth and (_json_loads_or_none(w.quotes) or None) == (clean_quotes or None)
),
None,
)
if match is not None:
wire = match
else:
wire = DbSessionWire(
id=str(uuid.uuid4()),
owner=owner_for_wire,
kind="document",
source_session_id=session_id,
target_session_id=session_id,
depth=depth,
context_order=_next_order(),
archived=False,
source_fingerprint=document_fingerprint(doc),
document_id=document_id,
quotes=json.dumps(clean_quotes) if clean_quotes else None,
ranges=json.dumps(clean_ranges) if clean_ranges else None,
created_at=datetime.now(timezone.utc),
)
db.add(wire)
db.commit()
db.refresh(wire)
else: # note
text = (note_text or "").strip()
if not text or len(text) > MAX_NOTE_CHARS:
raise SideThreadError(
f"note_text must be 1-{MAX_NOTE_CHARS} characters", "excursos.empty_note", 400
)
wire = DbSessionWire(
id=str(uuid.uuid4()),
owner=owner_for_wire,
kind="note",
source_session_id=session_id,
target_session_id=session_id,
depth="selection",
context_order=_next_order(),
archived=False,
source_fingerprint=note_fingerprint(text),
note_text=text,
created_at=datetime.now(timezone.utc),
)
db.add(wire)
db.commit()
db.refresh(wire)
wire_dict = _wire_to_dict(wire)
block = material_block(wire)
tokens = estimate_tokens([block]) if block else 0
return {"wire": wire_dict, "tokens": tokens}
finally:
db.close()
def update_material(
owner: Optional[str],
wire_id: str,
*,
depth: Optional[str] = None,
note_text: Optional[str] = None,
archived: Optional[bool] = None,
context_order: Optional[int] = None,
refresh: bool = False,
) -> Dict[str, Any]:
"""Patch a `document`/`note` material wire.
`refresh=True` recomputes `source_fingerprint` from the document's
CURRENT content (or the note's current text) — the same "I accept the
new version" gesture `update_reference(..., refresh=True)` offers for
excursos, clearing `stale`. Editing `note_text` refreshes the
fingerprint automatically in the same call: for a note the wire IS the
source, so there is no separate "upstream changed" event to accept
later — the edit itself is the new source.
"""
if depth is not None and depth not in ("selection", "full"):
raise SideThreadError(f"Unknown material depth: {depth!r}", "excursos.bad_depth", 400)
db = SessionLocal()
try:
wire = (
db.query(DbSessionWire)
.filter(DbSessionWire.id == wire_id, DbSessionWire.kind.in_(("document", "note")))
.first()
)
if wire is None:
raise SideThreadError(f"Material {wire_id} not found", "excursos.not_found", 404)
if owner is not None and _owned_row(db, wire.target_session_id, owner) is None:
raise SideThreadError(f"Material {wire_id} not found", "excursos.not_found", 404)
if depth is not None:
if wire.kind != "document":
raise SideThreadError("depth only applies to document materials", "excursos.bad_depth", 400)
if depth == "selection" and not (_json_loads_or_none(wire.quotes) or None):
raise SideThreadError(
"depth='selection' requires at least one quote", "excursos.quotes_required", 400
)
wire.depth = depth
if note_text is not None:
if wire.kind != "note":
raise SideThreadError("note_text only applies to note materials", "excursos.empty_note", 400)
text = note_text.strip()
if not text or len(text) > MAX_NOTE_CHARS:
raise SideThreadError(
f"note_text must be 1-{MAX_NOTE_CHARS} characters", "excursos.empty_note", 400
)
wire.note_text = text
wire.source_fingerprint = note_fingerprint(text)
if archived is not None:
wire.archived = bool(archived)
if context_order is not None:
wire.context_order = int(context_order)
if refresh:
fp = material_fingerprint(wire)
if fp is not None:
wire.source_fingerprint = fp
db.commit()
db.refresh(wire)
return {"wire": _wire_to_dict(wire)}
finally:
db.close()
def remove_material(owner: Optional[str], wire_id: str) -> bool:
"""DELETE a `document`/`note` material wire physically."""
db = SessionLocal()
try:
wire = (
db.query(DbSessionWire)
.filter(DbSessionWire.id == wire_id, DbSessionWire.kind.in_(("document", "note")))
.first()
)
if wire is None:
return False
if owner is not None and _owned_row(db, wire.target_session_id, owner) is None:
return False
db.delete(wire)
db.commit()
return True
finally:
db.close()
# ---------------------------------------------------------------------------
# Reading the graph
# ---------------------------------------------------------------------------
def wires_for(owner: Optional[str], session_id: str) -> Dict[str, Any]:
"""Everything the Studio's wiring panel needs for one session: its