Skip to content

feat(runtime): materialize trace graph topology - #1443

Merged
likun666661 merged 2 commits into
mainfrom
agent/1341-trace-graph-topology
Jul 24, 2026
Merged

likun666661 merged 2 commits into
mainfrom
agent/1341-trace-graph-topology

Conversation

@likun666661

@likun666661 likun666661 commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Summary

This is the second implementation slice of #1341, building on the committed-event projection merged in #1442.

  • Add an explicit read-only DAG topology over existing Session-backed operator bindings.
  • Validate graph identity, one-to-one operator/Session ownership, unique directed edges, known endpoints, and acyclicity.
  • Derive an input-order-independent topological order with deterministic root and sink sets.
  • Project each committed graph record as a stable, reference-only route across every direct outgoing edge; route identity binds the trace schema, edge ID, immutable endpoints, and source record.
  • Materialize an observable snapshot for operators, edges, source records, and routed inputs, including topology that has not produced RuntimeEvents yet.
  • Reuse committed-record replay as the validation and ordering authority, preserving deterministic/idempotent reconstruction under reordered and duplicate observations.
  • Keep route identity stable as later observations arrive.

Architectural boundary

This PR is still trace-only. It makes topology and record flow explicit without creating another execution authority.

A route says that an existing committed record is visible on one direct downstream edge. It references the source graph record and never copies message or tool payloads. It also does not recursively invent downstream work: only a RuntimeEvent emitted by that downstream operator can produce records on its own outgoing edges.

The main-agent supervisor remains structurally beside the graph. The existing always-on supervisor meta-stream continues to observe every source record independently; it is not consulted before a route appears and therefore cannot become a serial data-path gate.

Edges remain mechanically simple and route every committed source record. Operator-local readiness, buffering, joins, and semantic input selection belong to a later slice rather than a growing central scheduler DSL.

Validation and failure semantics

Topology materialization fails closed for:

  • empty graph/operator/Session/edge identities;
  • duplicate operator or Session ownership;
  • duplicate edge identities or parallel endpoint pairs;
  • unknown endpoints, self-loops, and cycles;
  • graph, operator, or Session ownership mismatches between topology and committed records;
  • any conflicting or invalid committed-record replay rejected by the feat(runtime): project committed agent events as graph records #1442 projection primitive.

Deliberate non-goals

  • No Agent runtime actions: no child creation, start, send, resume, cancel, or pause.
  • No operator readiness/admission protocol or resource permits.
  • No scheduler control-record authority.
  • No graph-wide completion or topology-closing protocol.
  • No Desktop/TUI renderer yet; this PR provides the deterministic snapshot such a renderer can consume.
  • No cyclic or distributed graph execution.

Validation

  • npm run typecheck
  • focused projection + trace suites: 18 passed
  • Runtime full suite: 2,454 passed, 7 skipped, 2 known unrelated macOS sandbox failures:
    • /usr/local executable-root ordering assertion
    • Homebrew rg cannot load PCRE2 inside the operation-scoped Seatbelt sandbox
  • Biome check on all changed files
  • git diff --check

Refs #1341

中文说明

这是 #1341 的第二个实现切片,建立在 #1442 已合并的 committed-event projection 之上。

  • 在现有 Session-backed operator binding 之上增加显式、只读的 DAG topology。
  • 校验 graph identity、operator/Session 一对一归属、edge 唯一性、endpoint 存在性与无环性。
  • 生成不受输入顺序影响的确定性拓扑序,以及稳定的 root/sink 集合。
  • 每条 committed graph record 都会在直接下游 edge 上生成稳定、只保存引用的 route;route identity 同时绑定 trace schema、edge ID、不可变 endpoints 与 source record。
  • 物化 operator、edge、source record 与 routed input 的可观测 snapshot;即使 RuntimeEvent 尚未产生,topology 仍然可见。
  • 复用 feat(runtime): project committed agent events as graph records #1442 的 committed-record replay 作为校验与排序权威,因此乱序或重复观察仍能确定、幂等地重建。
  • 后续观察加入时,已有 route identity 保持稳定。

本 PR 仍然是 trace-only。Route 只表示一条现有 committed record 在某条直接下游 edge 上可见,不复制 message/tool payload,也不会递归伪造下游执行。只有下游 operator 自己真实产生 RuntimeEvent,才会在它的 outgoing edge 上继续形成 record route。

主 Agent supervisor 始终在图旁监督。现有 always-on supervisor meta-stream 独立观察每条 source record;route 的形成无需等待 supervisor 批准,因此 supervisor 不会成为串行 data-path gate。

Edge 保持机械和简单:转发所有 committed source record。Operator-local readiness、buffering、join 和语义输入筛选留给后续切片,避免把中央 scheduler 扩展成领域 DSL。

本切片不会创建、启动、发消息、恢复、取消或暂停 child,不引入 readiness/admission、resource permits、scheduler control authority、graph-wide completion/closing protocol,也暂不实现 Desktop/TUI renderer。

@likun666661

Copy link
Copy Markdown
Member Author

The overall trace-only layering looks right, but I found two identity blockers before this is safe to merge.

[P1] Do not use unrestricted graph IDs as keys on ordinary JavaScript objects

operatorId and edgeId are only validated as non-empty strings, but the snapshot materializes them through ordinary {} dictionaries and inherited-property lookups (replay.operators[operatorId], operators[operatorId], and edges[edgeId]). Valid string IDs such as __proto__, constructor, or toString therefore do not behave like normal keys.

I reproduced both failure modes on this head:

  • A topology containing operatorId: "__proto__" and no records throws from cloneOperatorState(), because replay.operators["__proto__"] resolves to Object.prototype and is treated as runtime state.
  • A topology containing edgeId: "__proto__" returns a snapshot where Object.keys(snapshot.edges) is empty and Object.hasOwn(snapshot.edges, "__proto__") is false: assigning the edge invoked the prototype setter instead of creating an own entry.

This is both a correctness failure for an identity accepted by the public contract and a prototype-pollution boundary. Please use Map internally and convert with Object.fromEntries(), or use null-prototype dictionaries plus own-property checks. The same ordinary-object pattern exists in committed-record replay for operator and activation state, so it should be fixed there as well. Please add regressions covering reserved-key operator, activation, and edge IDs.

[P1] Bind route identity to the immutable edge endpoints

traceRouteId() currently hashes only { graphId, edgeId, sourceRecordId }, while the resulting route also contains sourceOperatorId and targetOperatorId.

I reproduced two snapshots for the same graph and source record, both using edgeId: "edge", with topologies source -> operator-a and source -> operator-b. They produce exactly the same routeId, but different targetOperatorId values. Any consumer merging incremental topology observations by route identity can therefore see one identity with conflicting contents—the same class of stability bug that the committed-record projection was designed to prevent.

Please either include fromOperatorId and toOperatorId (and a topology version if edge semantics may evolve) in the route identity, or establish and enforce an immutable graph-lifetime binding from edgeId to its endpoints. Add a regression that rebinds the same edge ID and asserts that the snapshots cannot produce conflicting routes with the same identity.

The focused suites and CI are green; these are contract gaps not covered by the current happy-path tests.

@likun666661
likun666661 marked this pull request as ready for review July 24, 2026 08:47
@likun666661
likun666661 merged commit 6f79958 into main Jul 24, 2026
3 checks passed
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.

1 participant