feat(webui): inspect file context in a live graph sidebar (#6562)
* feat(webui): inspect file context in a live graph sidebar * refactor(webui): place preview controls in the shared sidebar header * fix(webui): synchronize npm lockfile for graph dependencies * fix(webui): preserve compacted context and refine graph inspection * fix(context): harden provenance, request usage, and drawer lifecycle * fix(webui): align source skeleton and browser fixtures * perf(context): reuse source analysis and capture request graphs once * perf(webui): compact graph history and render graph motion on canvas * fix(webui): remove redundant context sidebar header * style(webui): clarify context replay icon * perf(webui): pace context replay on an adaptive timeline * feat(webui): briefly reveal labels for new context nodes * fix(webui): reveal new labels only for loaded context files * fix(webui): restart graph animation clock after idle * fix(webui): reveal labels when replay reverses node exit * refactor(context): project file-use facts without retention tracking * refactor(context): observe file reads outside agent execution * fix(webui): animate context sidebar closing * refactor(context): remove unused graph and usage panel code * refactor(context): isolate prompt observation from execution metadata * fix(webui): clarify cache rate and show context token usage * fix(webui): abbreviate context token counts in thousands * fix(webui): omit token unit from context usage details * fix(webui): restore large source preview highlighting and panel elevation * fix(webui): distinguish modifier scrolling from keyboard focus * fix(webui): align preview tab and panel corner radii * fix(webui): attach file tabs to the header divider * fix(webui): use one action to toggle file preview mode * fix(webui): use hover hint for context replay * fix(webui): share composer layout between graph and file sidebars * fix(webui): align sidebar bottom with composer surface * fix(webui): restore mobile graph drawing and touch file details * test(webui): align legacy cache assertions with current label * perf(context): retain source analysis and bound graph animation work * style(webui): add light elevation to user messages * test(webui): await lazy graph scene before replay assertions * fix(webui): align preview skeleton and browser contracts
This commit is contained in:
1 parent
2b44088413
commit
519ed81132
132 files changed
+4673
-1159
No files matched your search
@@ -8,6 +8,8 @@ from enum import StrEnum
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
|
||||
from nanobot.session.history_graph import encode_graph_fields, graph_size_adjustment
|
||||
|
||||
MAX_HISTORY_FACT_BYTES = 8 * 1024 * 1024
|
||||
HISTORY_GENERATION_METADATA_KEY = "_history_generation"
|
||||
|
||||
@@ -40,13 +42,23 @@ class HistoryFact:
|
||||
created_at_ms: int = 0
|
||||
|
||||
def encode(self) -> str:
|
||||
record = json.dumps({
|
||||
"kind": self.kind.value, "content": dict(self.content),
|
||||
content = encode_graph_fields(self.content)
|
||||
payload = {
|
||||
"kind": self.kind.value, "content": content,
|
||||
"turn_id": self.turn_id, "phase": self.phase,
|
||||
"sequence": self.sequence, "created_at_ms": self.created_at_ms,
|
||||
}, ensure_ascii=False, allow_nan=False, separators=(",", ":"))
|
||||
if len(record.encode("utf-8")) > MAX_HISTORY_FACT_BYTES:
|
||||
}
|
||||
record = json.dumps(payload, ensure_ascii=False, allow_nan=False, separators=(",", ":"))
|
||||
stored_bytes = len(record.encode("utf-8"))
|
||||
adjustment = graph_size_adjustment(content)
|
||||
logical_bytes = stored_bytes + adjustment
|
||||
if max(stored_bytes, logical_bytes) > MAX_HISTORY_FACT_BYTES:
|
||||
raise ValueError("history fact exceeds the 8 MiB storage limit")
|
||||
if adjustment:
|
||||
payload["uncompressed_bytes"] = logical_bytes
|
||||
record = json.dumps(payload, ensure_ascii=False, allow_nan=False, separators=(",", ":"))
|
||||
if len(record.encode("utf-8")) > MAX_HISTORY_FACT_BYTES:
|
||||
raise ValueError("history fact exceeds the 8 MiB storage limit")
|
||||
return record
|
||||
|
||||
@classmethod
|
||||
@@ -116,14 +128,14 @@ class HistoryReader:
|
||||
((HistoryKind.COMPLETION,), "DESC"),
|
||||
((HistoryKind.ANSWER, HistoryKind.ANSWER_SEGMENT, HistoryKind.ANSWER_FRAGMENT), "DESC")):
|
||||
row = self._db.execute(
|
||||
f"SELECT position, length(CAST(record AS BLOB)) FROM history_entries WHERE {clause} "
|
||||
f"SELECT position, coalesce(json_extract(record, '$.uncompressed_bytes'), length(CAST(record AS BLOB))) FROM history_entries WHERE {clause} "
|
||||
f"AND kind IN ({','.join('?' for _ in kinds)}) ORDER BY position {order} LIMIT 1",
|
||||
(*params, *kinds),
|
||||
).fetchone()
|
||||
if row is not None:
|
||||
priorities.append((row[0], row[1]))
|
||||
candidates = self._db.execute(
|
||||
f"SELECT position, length(CAST(record AS BLOB)) FROM history_entries WHERE {clause} ORDER BY position DESC LIMIT ?",
|
||||
f"SELECT position, coalesce(json_extract(record, '$.uncompressed_bytes'), length(CAST(record AS BLOB))) FROM history_entries WHERE {clause} ORDER BY position DESC LIMIT ?",
|
||||
(*params, max_records),
|
||||
)
|
||||
positions: set[int] = set()
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
"""Self-contained graph values for history facts; encoding runs in the storage worker."""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
import zlib
|
||||
from collections.abc import Generator, Mapping
|
||||
from contextlib import contextmanager
|
||||
from contextvars import ContextVar
|
||||
from functools import lru_cache
|
||||
from typing import Any, cast
|
||||
|
||||
from nanobot.context_graph import (
|
||||
ContextEdge,
|
||||
ContextGraph,
|
||||
ContextNode,
|
||||
ContextSnapshot,
|
||||
parse_context_graph,
|
||||
)
|
||||
|
||||
_PREFIX = "graph-columns-v1:"
|
||||
_MAX_BYTES = 8 * 1024 * 1024
|
||||
_CACHEABLE_BYTES = 128 * 1024
|
||||
|
||||
|
||||
@lru_cache(maxsize=16)
|
||||
def _compress_small(serialized: str) -> str | None:
|
||||
return _compress(serialized)
|
||||
|
||||
|
||||
def _compress(serialized: str) -> str | None:
|
||||
graph = parse_context_graph(json.loads(serialized))
|
||||
if graph is None:
|
||||
return None
|
||||
paths = {node["path"]: index for index, node in enumerate(graph["nodes"])}
|
||||
columns = [
|
||||
[[node["path"], node["name"], node["tokens"], node["state"]] for node in graph["nodes"]],
|
||||
[index for edge in graph["edges"] for index in (paths[edge["source"]], paths[edge["target"]])],
|
||||
]
|
||||
raw = json.dumps(columns, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
|
||||
return f"{_PREFIX}{len(serialized.encode('utf-8'))}:{len(raw)}:" + base64.b64encode(zlib.compress(raw, level=1)).decode("ascii")
|
||||
|
||||
|
||||
def encode_graph(value: object) -> object:
|
||||
if not isinstance(value, dict):
|
||||
return value
|
||||
value = cast(dict[str, object], value)
|
||||
serialized = json.dumps(value, ensure_ascii=False, separators=(",", ":"), allow_nan=False)
|
||||
size = len(serialized.encode("utf-8"))
|
||||
if size > _MAX_BYTES:
|
||||
raise ValueError("history graph exceeds the 8 MiB storage limit")
|
||||
encoded = _compress_small(serialized) if size <= _CACHEABLE_BYTES else _compress(serialized)
|
||||
return encoded if encoded is not None else value
|
||||
|
||||
|
||||
def encode_graph_fields(content: Mapping[str, Any]) -> dict[str, Any]:
|
||||
result = dict(content)
|
||||
if "context_graph" in result:
|
||||
result["context_graph"] = encode_graph(result["context_graph"])
|
||||
snapshot = result.get("context_snapshot")
|
||||
if isinstance(snapshot, dict) and "graph" in snapshot:
|
||||
snapshot = cast(dict[str, Any], snapshot)
|
||||
result["context_snapshot"] = {**snapshot, "graph": encode_graph(snapshot["graph"])}
|
||||
return result
|
||||
|
||||
|
||||
class GraphDecoder:
|
||||
"""One projection owns validated shared entities, including changing snapshots."""
|
||||
def __init__(self) -> None:
|
||||
self.graphs: dict[str, ContextGraph | None] = {}
|
||||
self.nodes: dict[tuple[object, ...], ContextNode] = {}
|
||||
self.edges: dict[tuple[object, object], ContextEdge] = {}
|
||||
self.sizes: dict[int, int] = {}
|
||||
|
||||
def node(self, path: object, name: object, tokens: object, state: object) -> ContextNode | None:
|
||||
key = (path, name, type(tokens), tokens, state)
|
||||
try:
|
||||
cached = self.nodes.get(key)
|
||||
except TypeError:
|
||||
return None
|
||||
if cached is not None:
|
||||
return cached
|
||||
if not isinstance(path, str) or not isinstance(name, str) or type(tokens) is not int or tokens < 0 or state not in ("loaded", "discovered"):
|
||||
return None
|
||||
node: ContextNode = {"path": path, "name": name, "tokens": tokens,
|
||||
"state": "loaded" if state == "loaded" else "discovered"}
|
||||
self.nodes[key] = node
|
||||
self.sizes[id(node)] = len(json.dumps(node, ensure_ascii=False, separators=(",", ":")).encode("utf-8"))
|
||||
return node
|
||||
|
||||
def edge(self, source: str, target: str) -> ContextEdge:
|
||||
key = (source, target)
|
||||
if key not in self.edges:
|
||||
edge: ContextEdge = {"source": source, "target": target}
|
||||
self.edges[key] = edge
|
||||
self.sizes[id(edge)] = len(json.dumps(edge, ensure_ascii=False, separators=(",", ":")).encode("utf-8"))
|
||||
return self.edges[key]
|
||||
|
||||
def inline(self, value: object) -> ContextGraph | None:
|
||||
if not isinstance(value, dict):
|
||||
return None
|
||||
value = cast(dict[str, object], value)
|
||||
raw_nodes, raw_edges = value.get("nodes"), value.get("edges")
|
||||
if not isinstance(raw_nodes, list) or not isinstance(raw_edges, list):
|
||||
return None
|
||||
nodes: list[ContextNode] = []
|
||||
for raw in cast(list[object], raw_nodes):
|
||||
if not isinstance(raw, dict):
|
||||
return None
|
||||
row = cast(dict[str, object], raw)
|
||||
node = self.node(row.get("path"), row.get("name"), row.get("tokens"), row.get("state"))
|
||||
if node is None:
|
||||
return None
|
||||
nodes.append(node)
|
||||
paths = {node["path"] for node in nodes}
|
||||
edges: list[ContextEdge] = []
|
||||
for raw in cast(list[object], raw_edges):
|
||||
if not isinstance(raw, dict):
|
||||
return None
|
||||
row = cast(dict[str, object], raw)
|
||||
source, target = row.get("source"), row.get("target")
|
||||
if not isinstance(source, str) or not isinstance(target, str) or source not in paths or target not in paths:
|
||||
return None
|
||||
edges.append(self.edge(source, target))
|
||||
return {"nodes": nodes, "edges": edges}
|
||||
|
||||
def columns(self, value: object, logical_bytes: int) -> ContextGraph | None:
|
||||
if not isinstance(value, list) or len(cast(list[object], value)) != 2:
|
||||
return None
|
||||
rows, indices = cast(list[object], value)
|
||||
if not isinstance(rows, list) or not isinstance(indices, list) or len(cast(list[object], indices)) % 2:
|
||||
return None
|
||||
nodes: list[ContextNode] = []
|
||||
for row in cast(list[object], rows):
|
||||
if not isinstance(row, list) or len(cast(list[object], row)) != 4:
|
||||
return None
|
||||
path, name, tokens, state = cast(list[object], row)
|
||||
node = self.node(path, name, tokens, state)
|
||||
if node is None:
|
||||
return None
|
||||
nodes.append(node)
|
||||
indices = cast(list[object], indices)
|
||||
if any(type(index) is not int or not 0 <= index < len(nodes) for index in indices):
|
||||
return None
|
||||
positions = cast(list[int], indices)
|
||||
edges = [self.edge(nodes[positions[i]]["path"], nodes[positions[i + 1]]["path"])
|
||||
for i in range(0, len(positions), 2)]
|
||||
# Do not trust a small declared logical size to bypass the replay byte budget.
|
||||
size = 23 + max(0, len(nodes) - 1) + max(0, len(edges) - 1)
|
||||
size += sum(self.sizes[id(node)] for node in nodes) + sum(self.sizes[id(edge)] for edge in edges)
|
||||
return {"nodes": nodes, "edges": edges} if size <= logical_bytes else None
|
||||
|
||||
def graph(self, value: object) -> ContextGraph | None:
|
||||
if isinstance(value, str):
|
||||
key = value
|
||||
elif isinstance(value, dict):
|
||||
try:
|
||||
key = json.dumps(value, ensure_ascii=False, separators=(",", ":"), allow_nan=False)
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
else:
|
||||
return None
|
||||
if key in self.graphs:
|
||||
return self.graphs[key]
|
||||
self.graphs[key] = None
|
||||
if isinstance(value, str):
|
||||
if not value.startswith(_PREFIX) or len(value) > _MAX_BYTES * 2:
|
||||
return None
|
||||
try:
|
||||
logical_text, size_text, encoded = value[len(_PREFIX):].split(":", 2)
|
||||
logical_size, size = int(logical_text), int(size_text)
|
||||
if not 0 <= logical_size <= _MAX_BYTES or not 0 <= size <= _MAX_BYTES:
|
||||
return None
|
||||
raw = base64.b64decode(encoded, validate=True)
|
||||
inflater = zlib.decompressobj()
|
||||
expanded = inflater.decompress(raw, _MAX_BYTES + 1)
|
||||
if len(expanded) != size or not inflater.eof or inflater.unused_data:
|
||||
return None
|
||||
graph = self.columns(json.loads(expanded), logical_size)
|
||||
except (ValueError, zlib.error, UnicodeDecodeError):
|
||||
return None
|
||||
else:
|
||||
graph = self.inline(cast(object, value))
|
||||
self.graphs[key] = graph
|
||||
return graph
|
||||
|
||||
|
||||
_decoder: ContextVar[GraphDecoder | None] = ContextVar("history_graph_decoder", default=None)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def graph_decoding() -> Generator[None]:
|
||||
token = _decoder.set(GraphDecoder())
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
_decoder.reset(token)
|
||||
|
||||
|
||||
def decode_graph(value: object) -> ContextGraph | None:
|
||||
return (_decoder.get() or GraphDecoder()).graph(value)
|
||||
|
||||
|
||||
def decode_snapshot(value: object) -> ContextSnapshot | None:
|
||||
if not isinstance(value, dict):
|
||||
return None
|
||||
snapshot = cast(dict[str, object], value)
|
||||
tokens, window = snapshot.get("estimated_tokens"), snapshot.get("context_window_tokens")
|
||||
if type(tokens) is not int or tokens < 0 or type(window) is not int or window <= 0:
|
||||
return None
|
||||
graph = decode_graph(snapshot.get("graph"))
|
||||
return {"graph": graph, "estimated_tokens": tokens, "context_window_tokens": window} if graph is not None else None
|
||||
|
||||
|
||||
def expand_graph_fields(content: Mapping[str, Any]) -> dict[str, Any]:
|
||||
result = dict(content)
|
||||
for field, decode in (("context_graph", decode_graph), ("context_snapshot", decode_snapshot)):
|
||||
if field in result:
|
||||
decoded = decode(result[field])
|
||||
if decoded is None:
|
||||
result.pop(field)
|
||||
else:
|
||||
result[field] = decoded
|
||||
return result
|
||||
|
||||
|
||||
def graph_size_adjustment(content: Mapping[str, Any]) -> int:
|
||||
"""Retain the original replay byte budget despite storage compression."""
|
||||
values = [content.get("context_graph")]
|
||||
snapshot = content.get("context_snapshot")
|
||||
if isinstance(snapshot, dict):
|
||||
values.append(cast(dict[str, object], snapshot).get("graph"))
|
||||
adjustment = 0
|
||||
for value in values:
|
||||
if isinstance(value, str) and value.startswith(_PREFIX):
|
||||
try:
|
||||
size = int(value[len(_PREFIX):].split(":", 1)[0])
|
||||
except ValueError:
|
||||
continue
|
||||
if 0 <= size <= _MAX_BYTES:
|
||||
adjustment += size - len(value) - 2 # ASCII encoding plus JSON quotes.
|
||||
return adjustment
|
||||
|
||||
|
||||
def history_record_size(record: str) -> int:
|
||||
stored = len(record.encode("utf-8"))
|
||||
if '"uncompressed_bytes":' not in record:
|
||||
return stored
|
||||
value = json.loads(record).get("uncompressed_bytes")
|
||||
return value if type(value) is int and 0 <= value <= _MAX_BYTES else stored
|
||||
@@ -12,6 +12,7 @@ from typing import Any, TypeVar, cast
|
||||
|
||||
from nanobot.providers.base import ProviderConversationState
|
||||
from nanobot.session.display_history import HistoryFact, HistoryKind, HistoryReader
|
||||
from nanobot.session.history_graph import history_record_size
|
||||
from nanobot.session.history_visibility import is_hidden_history_message
|
||||
from nanobot.session.manager import (
|
||||
Session,
|
||||
@@ -428,8 +429,25 @@ class SqliteSessionStore:
|
||||
|
||||
def append_history(
|
||||
self, key: str, facts: list[HistoryFact], *, expected_generation: str | None,
|
||||
) -> None:
|
||||
encoded = [(fact, fact.encode()) for fact in facts]
|
||||
skip_invalid: bool = False,
|
||||
) -> list[str]:
|
||||
encoded: list[tuple[HistoryKind, int, str, str]] = []
|
||||
errors: list[str] = []
|
||||
for fact in facts:
|
||||
try:
|
||||
record = fact.encode()
|
||||
# Freeze all transaction inputs from the encoded snapshot; never
|
||||
# revisit mutable content after serialization has completed.
|
||||
preview = ""
|
||||
if fact.kind in {HistoryKind.INPUT, HistoryKind.ANSWER, HistoryKind.ANSWER_SEGMENT}:
|
||||
preview = message_preview_text({"content": json.loads(record)["content"].get("text", "")})
|
||||
encoded.append((fact.kind, fact.created_at_ms, record, preview))
|
||||
except (ValueError, TypeError) as exc:
|
||||
if not skip_invalid:
|
||||
raise
|
||||
errors.append(str(exc))
|
||||
if not encoded and errors:
|
||||
return errors
|
||||
with self.transaction() as db:
|
||||
row = db.execute("SELECT generation FROM sessions WHERE key=?", (key,)).fetchone()
|
||||
if row is None or (expected_generation is not None and row[0] != expected_generation):
|
||||
@@ -441,7 +459,7 @@ class SqliteSessionStore:
|
||||
).fetchone()
|
||||
ordinal, position = (int(tail[0]), int(tail[1])) if tail is not None else (0, 0)
|
||||
completed = tail is not None and bool(tail[2])
|
||||
for fact, record in encoded:
|
||||
for kind, created_at_ms, record, preview in encoded:
|
||||
if completed:
|
||||
ordinal, position = ordinal + 1, 0
|
||||
if position == 0:
|
||||
@@ -449,25 +467,24 @@ class SqliteSessionStore:
|
||||
"INSERT INTO history_turns VALUES (?, ?, 0, 0, 0, 0)", (key, ordinal),
|
||||
)
|
||||
db.execute("INSERT INTO history_entries VALUES (?, ?, ?, ?, ?)", (
|
||||
key, ordinal, position, fact.kind.value, record,
|
||||
key, ordinal, position, kind.value, record,
|
||||
))
|
||||
completed = fact.kind is HistoryKind.COMPLETION
|
||||
completed = kind is HistoryKind.COMPLETION
|
||||
db.execute(
|
||||
"""UPDATE history_turns SET record_count=record_count+1, byte_count=byte_count+?,
|
||||
user_count=user_count+?, completed=? WHERE session_key=? AND ordinal=?""",
|
||||
(len(record.encode("utf-8")), int(fact.kind is HistoryKind.INPUT),
|
||||
(history_record_size(record), int(kind is HistoryKind.INPUT),
|
||||
int(completed), key, ordinal),
|
||||
)
|
||||
position += 1
|
||||
if fact.kind in {HistoryKind.INPUT, HistoryKind.ANSWER, HistoryKind.ANSWER_SEGMENT}:
|
||||
preview = message_preview_text({"content": fact.content.get("text", "")})
|
||||
if preview:
|
||||
field = "user_preview" if fact.kind is HistoryKind.INPUT else "preview"
|
||||
db.execute(f"UPDATE history_heads SET {field}=? WHERE session_key=? AND {field}=''", (preview, key))
|
||||
if preview:
|
||||
field = "user_preview" if kind is HistoryKind.INPUT else "preview"
|
||||
db.execute(f"UPDATE history_heads SET {field}=? WHERE session_key=? AND {field}=''", (preview, key))
|
||||
db.execute("UPDATE history_heads SET updated_at_ms=max(updated_at_ms, ?) WHERE session_key=?",
|
||||
(fact.created_at_ms, key))
|
||||
(created_at_ms, key))
|
||||
if encoded:
|
||||
db.execute("UPDATE history_heads SET revision=revision+1 WHERE session_key=?", (key,))
|
||||
return errors
|
||||
|
||||
def copy_history_prefix(self, source_key: str, target: Session, before_input: int) -> bool:
|
||||
"""Copy a display prefix alongside its canonical fork, in the caller's transaction."""
|
||||
|
||||
@@ -725,12 +725,14 @@ class SessionState:
|
||||
|
||||
async def append_history(
|
||||
self, key: str, facts: Sequence[HistoryFact], *, expected_generation: str | None,
|
||||
) -> None:
|
||||
"""Append to an existing session; None selects its current generation."""
|
||||
skip_invalid: bool = False,
|
||||
) -> list[str]:
|
||||
"""Serialize owned facts in the worker; None selects the current generation."""
|
||||
records = list(facts)
|
||||
await self._execute(
|
||||
return await self._execute(
|
||||
key,
|
||||
lambda: self._store.append_history(key, deepcopy(records), expected_generation=expected_generation),
|
||||
lambda: self._store.append_history(key, records, expected_generation=expected_generation,
|
||||
skip_invalid=skip_invalid),
|
||||
operation_name="append_history",
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user