Skip to content

feat(runtime): project committed agent events as graph records - #1442

Merged
likun666661 merged 3 commits into
mainfrom
feat/1341-committed-graph-projection
Jul 24, 2026
Merged

likun666661 merged 3 commits into
mainfrom
feat/1341-committed-graph-projection

Conversation

@likun666661

@likun666661 likun666661 commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Summary

This is the first implementation slice of #1341.

  • Add a read-only stream-graph projection over existing durable Agent runtime facts.
  • Bind one durable Session to an operator identity and project each session-inline AgentRun as an activation.
  • Convert each committed RuntimeEvent into one reference-only graph record with a stable record ID, immutable eventTime, a deterministic source orderKey, activation-local predecessor lineage, and bounded semantic facets.
  • Keep records byte-stable across incremental observations: a late record may sort before an earlier observation, but it never renumbers or mutates an existing record.
  • Exclude every partial: true event from graph input. The source ordinal counts committed events only, so legacy partial-row retention cannot renumber committed facts.
  • Reconstruct operator/activation trace state through a pure, deterministic, idempotent replay reducer.
  • Project every graph record into an always-on main-agent supervisor meta-stream. Permission and user-question facts become attention signals; terminal facts become milestone signals.
  • Keep supervisor attention orthogonal to graph lifecycle. Nonterminal activations remain running; only terminal RuntimeEvents derive completed / failed / aborted / cancelled.
  • Filter legacy same-session child AgentRuns from Session reads and fail closed if the pure projection is explicitly given a non-session-inline run.

Authority and supervision boundary

This PR deliberately keeps the graph read-only.

The original RuntimeEvent remains authoritative. Graph records and supervisor meta-records contain source identity and lifecycle metadata but do not copy message text, tool arguments/results, or other large payloads. The supervisor projection reuses the same graph recordId and RuntimeEvent source reference, so it is a routing/read projection rather than a second fact.

The main-agent supervisor is structurally present beside the graph and observes every record without becoming a serial data-path gate. Attention signals identify facts that require semantic supervision; later auditable scheduler control records will carry the resulting intervention.

The adapter fails closed when immutable event reads are unavailable, identities are ambiguous, records conflict, source ordering is invalid, or an activation contains committed facts after its terminal record.

Deliberate non-goals

  • No graph execution, topology mutation, operator admission, cancellation, or retry.
  • No resource scheduler or permits.
  • No supervisor control-record authority yet.
  • No graph-wide completed predicate. Observed child terminals cannot prove that topology and admission are closed; that requires a later explicit closing protocol.
  • No Agent runtime rewrite and no changes to agent_spawn / agent_swarm behavior.

This addresses the first architectural tensions raised on #1341: projection remains honest while it is read-only, downstream graph inputs are committed facts only, records remain stable as concurrent observations arrive, and the always-on supervisor is visible without importing human-interaction wait states into graph lifecycle.

Validation

  • npm run typecheck
  • focused stream-graph projection suite: 10 passed
  • Runtime full suite: 2,445 passed, 7 skipped, 2 known unrelated macOS sandbox failures:
    • /usr/local executable-root ordering assertion
    • Homebrew rg unable to load PCRE2 inside the operation-scoped Seatbelt sandbox
  • Biome check on all changed files
  • git diff --check

Refs #1341

中文说明

这是 #1341 的第一个实现切片。

  • 在现有持久化 Agent runtime facts 之上增加只读 stream-graph projection。
  • 一个持久 Session 绑定一个 operator identity;其中每个 session-inline AgentRun 投影为一次 activation。
  • 每个 committed RuntimeEvent 转换为一条只保存引用的 graph record,包含稳定 record ID、不可变 eventTime、确定性 source orderKey、activation 内 predecessor lineage,以及有界语义 facets。
  • 增量观察不会改变旧 record:晚到 record 可以排到之前观察到的 record 前面,但不能重新编号或修改已有 record。
  • 所有 partial: true 事件都不会进入 graph input;source ordinal 只计算 committed events,历史 partial row 是否保留不会导致 committed facts 重编号。
  • 通过纯函数、确定性、幂等的 replay reducer 重建 operator/activation trace state。
  • 每条 graph record 都同时进入主 Agent 的 always-on supervisor meta-stream。Permission 与 user-question facts 产生 attention signal;terminal facts 产生 milestone signal。
  • Supervisor attention 与 graph lifecycle 正交。非终止 activation 始终保持 running;只有 terminal RuntimeEvent 才产生 completed / failed / aborted / cancelled。
  • 从 Session 读取时过滤历史 same-session child AgentRuns;纯 projection 若显式收到 non-session-inline run 则 fail closed。

本 PR 刻意保持 graph 只读。原始 RuntimeEvent 仍是权威事实;graph record 与 supervisor meta-record 不复制消息正文、工具参数/结果或大块 payload。Supervisor projection 复用同一个 graph recordId 与 RuntimeEvent source reference,因此它是 routing/read projection,而不是第二套事实。

主 Agent supervisor 在结构上始终位于 graph 旁边,观察每条 record,但不会成为串行数据路径门禁。Attention signal 标记需要语义监督的事实;后续可审计 scheduler control record 再承载 supervisor 的干预结果。

本切片仍不执行 graph,不修改 topology,不做 admission/cancel/retry,不引入 resource scheduler,也暂不建立 supervisor control-record authority 或 graph-wide completed 协议。

@likun666661

Copy link
Copy Markdown
Member Author

After thinking through the stream-graph boundary more carefully, I think three changes are needed before merging. The first two are blockers.

[P1] Keep the stable source ordering; do not turn its snapshot rank into record time

The projection already has a viable deterministic comparator for a single-host runtime. The problem is assigning logicalTime: index + 1 after sorting the current snapshot. That dense rank is not stable as the graph grows.

For example, the first read may contain only A(ts=100), so A receives logical time 1. A later read can discover a committed record B(ts=90) from another concurrently running operator. Reprojection then gives B time 1 and changes A to time 2, while A keeps the same recordId. Combining observations therefore produces the same record identity with conflicting contents.

On the current single-machine runtime, the simplest honest model is to keep the immutable absolute RuntimeEvent.ts as event time and use a deterministic tie-break tuple for equal timestamps, such as (event.ts, run.createdAt, operatorId, runId, source event ordinal, event.id). Late records may sort before already observed records, but they must not mutate the existing records. If the field remains named logicalTime, equal values must be allowed and replay must use the stable tie-breaker; otherwise eventTime/a structured orderKey would describe the semantics better.

Please add an incremental regression test: project A, then project A+B where B has an earlier timestamp, assert A is byte-for-byte unchanged, and verify replay/dedup across observations does not conflict.

[P1] Do not derive graph-level blocked from human-interaction RuntimeEvents

permission_request and user_question_request are Runtime/Host interaction facts, not graph-scheduler wait-state authority. This graph is driven by subagent records; a human does not enter a child Session and resume an operator. At most, a subagent emits a semantic/policy signal, the main-agent supervisor observes the meta-stream, and the supervisor changes the schedule (AddOperator, ReplaceInput, Retry, Cancel, request more evidence, etc.).

The current reducer imports the interactive Session model into the graph by converting those facets into blocked, then partially tries to undo that state with permission_decision. That is the wrong abstraction, and it also leaves a user-question activation falsely blocked while the Runtime has already resumed.

In this read-only slice, nonterminal activation state should remain running; terminal RuntimeEvents can still derive completed/failed/aborted/cancelled. Permission/question records may remain observable facets, or preferably be normalized as supervisor/policy attention signals, but they should not mutate graph lifecycle state. If graph-level waiting/blocking is needed later, it should come from explicit durable scheduler/control records such as dependency wait, admission wait, resource wait, or supervisor pause—not be inferred from child Session interaction events.

[P2] Enforce the documented session-inline run boundary

readCommittedAgentGraphProjection() currently projects every result of listSessionRuns(), although the contract says each session-inline AgentRun becomes an activation. Sessions can still contain legacy child AgentRuns, which would then be misclassified as repeated activations of the bound operator. Please filter with isSessionInlineRun (as the existing Runtime read model does), or fail closed when a non-inline run is supplied, and add compatibility coverage for a Session containing both inline and legacy child runs.

@likun666661

Copy link
Copy Markdown
Member Author

The three issues from my previous review are addressed correctly: records now keep immutable event time plus a stable source order key, supervisor attention no longer mutates graph lifecycle, and Session reads enforce the session-inline boundary. The new regression coverage looks good.

One replay identity blocker remains:

[P1] Preserve the one-to-one Session ↔ operator binding across merged observations

assertGraphIdentity() rejects binding one Session to multiple operators within a projection, but replayAgentGraphRecords() only checks the forward direction (one operatorId cannot point to two sessionIds). It does not enforce the reverse direction across records.

Two independently valid observations of the same graphId can therefore project the same Session/RuntimeEvent under operator one and later under operator two. Because recordId includes operatorId, the two records have different identities, so dedup/conflict detection does not catch this. Merging them currently reconstructs both operators against the same Session and double-projects the same runtime fact.

Please maintain a reverse sessionId -> operatorId index during replay and reject a second owner, matching the invariant already enforced by assertGraphIdentity(). Add a regression that separately projects the same Session/event under two operator IDs, combines the records, and expects replay to fail.

Once this is fixed, I do not see another merge blocker.

@likun666661
likun666661 marked this pull request as ready for review July 24, 2026 07:56
@likun666661
likun666661 merged commit 5ce0ce9 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