nisse-core: group modules into packages by architecture section
A pure move; behaviour unchanged. - models (L9, L7): llm, catalog, probe, render, render_anthropic, context - log (L7, L6): canonical, views - state (L8): store (was state), reconcile, worker, cancel, dump - knowledge (L10, L6.4): vault, memory - conversation (L5): transport, matrix - execution (L13): ssh (was execution), testing - authority (P3, P4, L4): actions - gitea, oplog, envfile stay at the top until each gains a second module. Every import, monkeypatch target, script and doc reference rewritten. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J5Utyfzw7gF7UhjRDEtjTk
This commit is contained in:
1 parent
06759d5d70
commit
e351d0a74e
117 files changed
+291
-265
No files matched your search
@@ -0,0 +1,2 @@
|
||||
"""The canonical log (architecture L7) and what each role is shown of it
|
||||
(L6)."""
|
||||
@@ -0,0 +1,173 @@
|
||||
"""Canonical transcript schema (architecture L7).
|
||||
|
||||
The durable record is this event log, never a provider-shaped ``messages``
|
||||
array; provider request bodies are rendered from it at call time. Canonical
|
||||
tool-call IDs are minted locally — provider IDs are mapped at the boundary and
|
||||
never used as a join key.
|
||||
|
||||
Transcript appends are the conversation record itself, not state-machine
|
||||
mutations, so they are deliberately outside the L12 state-change taxonomy.
|
||||
"""
|
||||
|
||||
import uuid
|
||||
from dataclasses import asdict, dataclass, fields
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class UserMessage:
|
||||
text: str
|
||||
source: str # e.g. a Matrix event ID
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AssistantText:
|
||||
text: str
|
||||
model: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ToolCall:
|
||||
call_id: str # canonical, minted locally via mint_call_id()
|
||||
tool: str
|
||||
arguments: dict[str, Any]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ToolResult:
|
||||
call_id: str
|
||||
result: str
|
||||
success: bool
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PlanSubmitted:
|
||||
plan: str
|
||||
model: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GateOpened:
|
||||
gate: str
|
||||
reason: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DelegationStarted:
|
||||
brief: str
|
||||
role: str
|
||||
model: str # the alias asked for, e.g. "remote-architecture"
|
||||
caps: dict[str, Any] # turns / tokens / wall-clock limits
|
||||
# What the alias resolved to at task start. An alias is free to change
|
||||
# models between tasks, so eval history (L9) keyed on the alias alone
|
||||
# would average results across whatever it happened to point at.
|
||||
backing_model: str = ""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DelegationCompleted:
|
||||
handback: str
|
||||
artifacts: list[str]
|
||||
tokens: int
|
||||
duration_s: float
|
||||
exit_status: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Fact:
|
||||
"""Something the orchestrator knows that the models must too (L6, L7):
|
||||
a plan approved, a milestone finished, a PR opened. Written by the
|
||||
orchestrator; every role sees it, as a statement rather than a turn."""
|
||||
|
||||
text: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ActionVerdict:
|
||||
"""What the fast path made of a tool call before it ran (P4, L4): a
|
||||
record for the log, the operator and the classifier's evaluation set,
|
||||
never shown to a model."""
|
||||
|
||||
call_id: str
|
||||
tool: str
|
||||
effect: str # harmless | contained | unknown | dangerous | refused
|
||||
rule: str
|
||||
reason: str
|
||||
decision: str # ran | refused | asked | allowed | classified
|
||||
# Who decided: the fast path's rules, or the classifier (L4), with the
|
||||
# model it ran on and exactly what it was shown (the evaluation set, L9).
|
||||
source: str = "fast_path"
|
||||
model: str = ""
|
||||
input: str = ""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Compaction:
|
||||
summary: str
|
||||
model: str
|
||||
replaces: list[int] # [first_id, last_id] inclusive range replaced
|
||||
|
||||
|
||||
CanonicalEvent = (
|
||||
UserMessage
|
||||
| AssistantText
|
||||
| ToolCall
|
||||
| ToolResult
|
||||
| PlanSubmitted
|
||||
| GateOpened
|
||||
| DelegationStarted
|
||||
| DelegationCompleted
|
||||
| Fact
|
||||
| ActionVerdict
|
||||
| Compaction
|
||||
)
|
||||
|
||||
EVENT_TYPES: dict[str, type] = {
|
||||
"user_message": UserMessage,
|
||||
"assistant_text": AssistantText,
|
||||
"tool_call": ToolCall,
|
||||
"tool_result": ToolResult,
|
||||
"plan_submitted": PlanSubmitted,
|
||||
"gate_opened": GateOpened,
|
||||
"delegation_started": DelegationStarted,
|
||||
"delegation_completed": DelegationCompleted,
|
||||
"fact": Fact,
|
||||
"action_verdict": ActionVerdict,
|
||||
"compaction": Compaction,
|
||||
}
|
||||
|
||||
_TYPE_NAMES = {cls: name for name, cls in EVENT_TYPES.items()}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class StoredEvent:
|
||||
id: int
|
||||
ts: str
|
||||
event: CanonicalEvent
|
||||
# The work item it belongs to (None: agent-level, L6) and the role that
|
||||
# produced it (L7). Rendering uses both.
|
||||
work_item: str | None = None
|
||||
actor: str = ""
|
||||
|
||||
|
||||
def mint_call_id() -> str:
|
||||
return f"call-{uuid.uuid4().hex}"
|
||||
|
||||
|
||||
def event_type(event: CanonicalEvent) -> str:
|
||||
return _TYPE_NAMES[type(event)]
|
||||
|
||||
|
||||
def to_payload(event: CanonicalEvent) -> dict[str, Any]:
|
||||
return asdict(event)
|
||||
|
||||
|
||||
def from_payload(type_name: str, payload: dict[str, Any]) -> CanonicalEvent:
|
||||
cls = EVENT_TYPES.get(type_name)
|
||||
if cls is None:
|
||||
raise ValueError(f"unknown canonical event type: {type_name!r}")
|
||||
known = {f.name for f in fields(cls)}
|
||||
unknown = payload.keys() - known
|
||||
if unknown:
|
||||
raise ValueError(f"unknown fields for {type_name}: {sorted(unknown)}")
|
||||
return cls(**payload)
|
||||
@@ -0,0 +1,168 @@
|
||||
"""Per-role views of a work item's transcript (architecture L6; Development
|
||||
Plan M1.2).
|
||||
|
||||
The log records every role's events in one work item. A model must not read
|
||||
another role's turns as its own: in the second live run the planner, shown
|
||||
the executor's tool calls as assistant turns, took that work for its own
|
||||
(#78). So what a role is shown is projected here, before rendering:
|
||||
|
||||
- its own events, unchanged;
|
||||
- what the operator said, as a user message labelled with where it came from;
|
||||
- the orchestrator's facts and gates, as labelled statements;
|
||||
- another model role's work only where it needs it, and only as a labelled
|
||||
report or narration, never as a turn.
|
||||
|
||||
The projection keeps each event's id, so cursors (``after``) and compaction
|
||||
ranges still line up with the log. Everything not the viewer's own becomes a
|
||||
``UserMessage``: from where the viewer sits, it is all input.
|
||||
"""
|
||||
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import replace
|
||||
|
||||
from nisse_core.log.canonical import (
|
||||
ActionVerdict,
|
||||
AssistantText,
|
||||
DelegationCompleted,
|
||||
DelegationStarted,
|
||||
Fact,
|
||||
GateOpened,
|
||||
PlanSubmitted,
|
||||
StoredEvent,
|
||||
ToolCall,
|
||||
ToolResult,
|
||||
UserMessage,
|
||||
)
|
||||
|
||||
# How much of another role's tool result a narration line keeps.
|
||||
NARRATION_RESULT_CHARS = 300
|
||||
|
||||
|
||||
def operator_label(source: str) -> str:
|
||||
"""Where an operator message came from, for its label."""
|
||||
origin, _, idle = source.partition(" (sent while idle")
|
||||
if origin.startswith("transport:"):
|
||||
channel = "Matrix"
|
||||
elif origin.startswith("gitea:pr:"):
|
||||
number = origin.split(":")[2]
|
||||
channel = f"comment on PR #{number}"
|
||||
elif origin.startswith("gitea:issue-body:"):
|
||||
channel = f"issue #{origin.split(':')[2]}"
|
||||
elif origin.startswith("gitea:issue:"):
|
||||
number = origin.split(":")[2]
|
||||
channel = f"comment on plan issue #{number}"
|
||||
elif origin.startswith("gitea:"):
|
||||
channel = "Gitea comment"
|
||||
else:
|
||||
channel = origin or "unknown"
|
||||
if idle:
|
||||
channel += ", sent while idle"
|
||||
return f"operator, {channel}"
|
||||
|
||||
|
||||
def _said(stored: StoredEvent, label: str, text: str) -> StoredEvent:
|
||||
return replace(
|
||||
stored,
|
||||
event=UserMessage(text=f"[{label}] {text}", source=f"view:{stored.actor}"),
|
||||
)
|
||||
|
||||
|
||||
def _narration(call: ToolCall, result: ToolResult | None) -> str:
|
||||
target = call.arguments.get("path") or call.arguments.get("command")
|
||||
step = f"{call.tool}({str(target)[:80]})" if target else call.tool
|
||||
if result is None:
|
||||
return f"{step} → no result recorded"
|
||||
outcome = "ok" if result.success else "failed"
|
||||
text = result.result.strip()
|
||||
if len(text) > NARRATION_RESULT_CHARS:
|
||||
text = text[:NARRATION_RESULT_CHARS] + " …"
|
||||
return f"{step} → {outcome}: {text}" if text else f"{step} → {outcome}"
|
||||
|
||||
|
||||
def project(
|
||||
events: list[StoredEvent],
|
||||
viewer: str,
|
||||
shown_by_viewer: Mapping[str, Mapping[str, str]],
|
||||
) -> list[StoredEvent]:
|
||||
"""The events as ``viewer`` should be shown them, in log order.
|
||||
``shown_by_viewer``: for each viewer, the roles whose work it is shown,
|
||||
and how: "report" keeps only the handback, "narrate" keeps a line per
|
||||
step. Anything not listed is not shown."""
|
||||
# Verdicts are records about calls, never part of any role's context.
|
||||
events = [e for e in events if not isinstance(e.event, ActionVerdict)]
|
||||
shown = shown_by_viewer.get(viewer)
|
||||
if shown is None:
|
||||
# A role with no view defined (the expert is stateless, the
|
||||
# classifier sees only an action) reads the log as it is, with
|
||||
# results kept after their calls.
|
||||
return _results_follow_calls(list(events))
|
||||
results = {
|
||||
e.event.call_id: e.event for e in events if isinstance(e.event, ToolResult)
|
||||
}
|
||||
out: list[StoredEvent] = []
|
||||
for stored in events:
|
||||
event, actor = stored.event, stored.actor
|
||||
if isinstance(event, PlanSubmitted):
|
||||
# A record for a helper's scope and the dump, not a turn: the
|
||||
# driver's own call already carries what was agreed.
|
||||
if viewer == "helper":
|
||||
out.append(_said(stored, "approved plan", event.plan))
|
||||
continue
|
||||
if isinstance(event, DelegationStarted):
|
||||
# The harness handing a task over: to the helper given it, a
|
||||
# message from the harness, never its own turn (a request must
|
||||
# end on the user's side; models refuse assistant prefill).
|
||||
if actor == viewer:
|
||||
out.append(_said(stored, "orchestrator", f"your task: {event.brief}"))
|
||||
continue
|
||||
if actor == viewer:
|
||||
out.append(stored)
|
||||
elif actor == "operator" and isinstance(event, UserMessage):
|
||||
out.append(_said(stored, operator_label(event.source), event.text))
|
||||
elif isinstance(event, Fact):
|
||||
out.append(_said(stored, "orchestrator", event.text))
|
||||
elif isinstance(event, GateOpened):
|
||||
out.append(
|
||||
_said(stored, "orchestrator", f"gate {event.gate}: {event.reason}")
|
||||
)
|
||||
elif shown.get(actor) == "report" and isinstance(event, DelegationCompleted):
|
||||
out.append(
|
||||
_said(
|
||||
stored,
|
||||
f"{actor} report, {event.exit_status}",
|
||||
event.handback,
|
||||
)
|
||||
)
|
||||
elif shown.get(actor) == "narrate":
|
||||
if isinstance(event, ToolCall):
|
||||
line = _narration(event, results.get(event.call_id))
|
||||
out.append(_said(stored, actor, line))
|
||||
elif isinstance(event, AssistantText) and event.text.strip():
|
||||
out.append(_said(stored, actor, event.text.strip()))
|
||||
return _results_follow_calls(out)
|
||||
|
||||
|
||||
def _results_follow_calls(events: list[StoredEvent]) -> list[StoredEvent]:
|
||||
"""Keep each tool result right after its call, as both API formats
|
||||
require: anything recorded while a call was in flight (a fact the
|
||||
harness wrote while handling it, say) moves to just after the results.
|
||||
A call with no result yet keeps its place; the renderer answers it."""
|
||||
out: list[StoredEvent] = []
|
||||
pending: set[str] = set()
|
||||
held: list[StoredEvent] = []
|
||||
for stored in events:
|
||||
event = stored.event
|
||||
if isinstance(event, ToolCall):
|
||||
pending.add(event.call_id)
|
||||
out.append(stored)
|
||||
elif isinstance(event, ToolResult) and event.call_id in pending:
|
||||
pending.discard(event.call_id)
|
||||
out.append(stored)
|
||||
if not pending:
|
||||
out.extend(held)
|
||||
held = []
|
||||
elif pending:
|
||||
held.append(stored)
|
||||
else:
|
||||
out.append(stored)
|
||||
return out + held
|
||||
Reference in new issue
Block a user