Skip to content

refactor(runtime): simplify Session facts and scale distributed CLI and Edge #1239

Open

Description

@XuPeng-SH

Problem

Astra should support the same logical Session through CLI + Edge + Server and Server-only, including many users on different machines and multiple Server instances. The current combination of local and remote journals, execution state, projections, and background observers makes that goal too expensive and difficult to reason about. Long histories add further work even when a client needs only the recent working set.

The goal is a simpler, robust, high-performance design: fewer independently maintained states, less MatrixOne traffic and data, and a clear execution and recovery story. Local CLI/Edge storage should carry as much of the local work as appropriate; Server-only must still persist the context and execution facts needed to resume and display a Session. This is about general capabilities, not special cases for a particular prompt or test.

Evidence on current main

Baseline: bebb535cef44c9b2a226b9cc57df75e9e778e0a8. The following are code observations and isolated experiments, not a production capacity measurement:

  • Each connected Edge has a two-second pending-dispatch database poll, while a separate batched observer also checks for cross-Server wakeups. Assuming 1,000 continuously connected Edges and normal scheduling, the per-connection poll alone implies roughly 500 SELECTs/s, plus roughly 33 registration heartbeat updates/s. These are source-derived estimates, not measured throughput.
  • In an isolated MatrixOne v4.2.6 test using the actual dispatch schema and a prepared wake query, 1,000 owners had 100,000 completed dispatches and no pending dispatches. A 128-Edge empty wake query scanned 12,800 rows. That is one synthetic distribution, not a claim about every production plan.
  • A bound, quiet TUI refreshes its Run tree every 15 seconds; its handler reads the Session and its Runs. If 1,000 such TUIs are idle, that implies roughly 67 requests/s and at least 133 related SELECTs/s, before additional reads. This estimate applies to bound TUIs, not all CLI processes.
  • In an isolated MatrixOne v4.2.6 experiment with the actual Run schema and full prepared production query, one user's 10,000 Runs were distributed across 100 Sessions; the target Session had 100 Runs. EXPLAIN ANALYZE showed 10,000 table-scan input rows to select that Session's 100 Runs. LIMIT and an index declaration alone therefore do not establish bounded physical work. This resembles matrixone#31856, but the same root cause has not been established.
  • Local conversation-log reads can read the entire journal to find a snapshot or suffix. The one-shot commit path also rereads the journal around its CAS. These are specific paths, not a claim that every TUI turn reads the whole file. Repeated full reads make long Sessions increasingly expensive.

The prior 1,000-connection exercise for #1236 used simulated clients from one load host for 10 seconds and did not establish SQL cost, sustained capacity, distributed real CLI behavior, or 1,000 concurrently active tasks.

Design direction

  1. One Session fact model and one execution backbone. Inventory the owners and consumers of local conversation records, Server transcript/context, Run events, and Edge invocation records. Preserve one logical identity, ordering, and execution ownership for the same Session across modes. Keep full user-visible history distinct from the compact model working context. Allow multiple observers and concurrent independent Sessions; serialize only execution that actually shares a Session's existing owner, not an entire workspace.
  2. Use local storage where it exists, and persist what sharing requires. Reuse the existing local Session and Edge invocation journals instead of adding another authoritative state machine. Synchronize only facts needed for shared history, recovery, billing, and required audit, with bounded idempotent asynchronous batches where semantics allow. In Server-only mode, persist the same logical facts in shared storage. Remove redundant projections or outbox state only after identifying their consumers and preserving their actual behavior.
  3. Make consistency visible. A local fact awaiting sync must not be presented as durably available on another device. If the original machine is offline, another device can only see facts already persisted to a shared location. Preserve ordering, explicit unknown tool outcomes, and correct recovery; do not blindly repeat a side effect when its result is unknown.
  4. Use live delivery rather than continuous per-connection database discovery. Reuse existing event and authenticated relay capabilities where possible, including correct routing when the caller and Edge are attached to different Servers. Eliminate duplicate idle polling paths and replace periodic TUI full-tree refresh with event-driven updates plus bounded replay or on-demand recovery. Handle missed events, disconnects, Server failure, slow consumers, and reconnect without requiring a new mandatory broker or parallel coordination service.
  5. Bound long-history work. Append and read recent local facts without reloading the full journal; use snapshots, cursors, or rebuildable offsets only where useful. Page user history and assemble a bounded model working set. For Server reads, verify the actual MatrixOne physical plan and rows scanned on realistic cardinalities, not just SQL LIMIT or index names. Do not replace one unbounded scan with another.

Favor deletion and convergence of existing state, SQL, and paths over adding caches, tables, queues, or compatibility layers. Retain the existing authentication/revocation and local execution boundaries, necessary usage/audit facts, and the frozen MOI Gate contract. A fresh design needs no legacy migration or compatibility layer; this is not authorization to delete existing user data. The local Codex rollout/journal and bounded-tail scanning implementation is a useful reference, while Astra's multi-Server and cross-device semantics still require their own solution.

Acceptance

  • Map the facts, owners, readers, and retained/removed projections across CLI, Edge, and Server-only. Show what state, SQL paths, and code are removed, separately accounting for implementation, tests, and documentation.
  • Measure before and after against a real MatrixOne instance. Report foreground and background SQL and transactions, rows scanned, pool/lock wait, storage/network bytes, Server CPU/RSS, connection resources, and p50/p95/p99 latency. Include real query plans and data distributions.
  • Validate at least 1,000 real CLI/Edge clients distributed across multiple machines, connected to at least two Servers for a sustained period (target at least one hour). Measure idle separately from 100 and 1,000 active tasks, including simple conversation, actual tool calls, multiple Sessions per user, and multiple observers of one Session. Report offered, completed, failed, and queued work; do not present online connections as active throughput.
  • Steady-state idle clients do not each cause scheduled Session, Run-tree, or dispatch SQL. Any node-level recovery work is bounded and its rate is reported. Verify reconnect and token-refresh bursts do not create a request storm.
  • Exercise Sessions with 100, 10,000, and 100,000 historical facts/Runs. Recent-window reads and appends must not require reading all local or Server history; paged history must remain complete and ordered. Measure actual scan and resource growth.
  • Test cross-Server routing, Server/CLI restart, network loss and lost acknowledgements, backlog, unknown tool results, multiple clients, Session deletion, credential revocation, user isolation, and MOI Gate behavior. A failure must not be reported as success or cause duplicate side effects.
  • Verify user-visible behavior and real recovery paths, including Server-only durable resume and honest visibility of unsynchronized local facts. Do not claim scale from mocks, a short idle test, or a single load host.

Related work and boundary

Activity

  1. XuPeng-SH commented on Nov 17, 2026

    @XuPeng-SH
    CollaboratorAuthor

    补充 2026-11-17 确认的产品方向与实施顺序。当前核对基线为 main f000f7ec4(#1315 已合并)。

    CLI/Edge 优先本地持久化,不自动向云端保存完整对话和工具输出;基础 introspect、trace、reflect 证据必须保留并可扩展。 这是本 issue “本地承载本地工作”方向的进一步明确。云端共享摘要不等于完整内容已经备份,也不代表另一台设备具备恢复条件。

    数据归属

    数据 CLI/Edge 目标 Server-only
    完整对话、模型续聊上下文、工具输出及恢复数据 由执行 owner 写入已有本地 session/journal/checkpoint 由 Server 写入共享存储
    基础 trace、错误、耗时、用量和反思证据 本地持久化,通过有界窗口按需读取 云端紧凑持久化
    身份权限、Run/任务协调、必要审计与副作用回执、必要计量、状态摘要 云端保留共享所必需的事实 云端
    原始模型请求/响应报文 默认关闭;显式诊断开启 同样按显式诊断策略处理

    “必要计量”由已有计量 owner 负责;本地 trace 用量不能冒充完整账单。未知副作用和未确认回执不能因为减量、过期或重试而丢失。

    关键现状与设计约束

    当前 CLI 的模型循环、模型轮次准备和 canonical Context 仍由 Server 管理;本地 journal 不是这一 Server 恢复权威的直接替代品。仅关闭 journal 上传仍不能实现完整内容只存本地;直接停止 Server 的 canonical 写入会破坏续聊与故障恢复。

    因此需要先盘点真实 producer、reader 和恢复 owner,再调整执行/存储归属。目标是让同一个 backbone 支持 CLI/Edge 执行宿主,沿用现有 execution binding、journal、checkpoint、权限和副作用语义;不是恢复另一套 CLI agent loop,也不是让 Server 隐式访问用户文件。具体宿主接入与 owner 切换协议仍需落实和验证,尚未实现。

    实施顺序

    1. 先删除已经确认的冗余。

      • 重复同步事件不再重新 COUNT 全 session、查询最后事件、重写摘要:事件与摘要本已在同一事务提交。
      • 默认本地 introspect/reflect 使用已有有界证据;显式云端请求和缺少本地证据时的行为由 source policy 明确约束,不能把不可用伪装成空数据。
      • 移除本地完整请求/响应、逐轮诊断和 canonical journal 的不必要云端镜像;先核对消费者,保留仍由 Server 承担的恢复权威。这一步本身不宣称已完成本地持久化目标。
      • 删除重复全文、无消费者字段、无效兼容分支和空写入;不新增镜像表、缓存、状态机或补偿 worker。
    2. 再完成执行 owner 与存储 owner 的统一。

      • CLI/Edge 的完整 session 和恢复证据以本地为权威,云端保留共享控制与紧凑摘要;Server-only 保留对应的云端恢复能力。
      • 复用同一执行、提交、恢复和授权语义,避免按界面名称分叉生命周期。
      • 跨设备明确标注内容位于何处、哪些事实已共享;原设备离线时,未共享的完整内容不可凭空恢复。确有需要的共享/交接沿用明确授权和现有 custody 边界。
      • 给模型稳定、完整的消息、错误和工具通道,不用硬编码工具限制或额外审批代替存储设计。
    3. 控制长期增长并验证容量。

      • 普通 trace 保留身份、因果关联、结果、耗时、用量和错误事实;大正文通过已有本地引用读取,避免每轮复制历史。
      • 增量追加、有界读取;只清理可再生诊断或按明确保留策略处理的数据,不删除恢复及未知副作用证据。
      • 在现有事务与幂等边界内批量写;重放不更新,空批次不发 SQL;空闲成本不随每个客户端的定时数据库轮询线性增长。

    验证与当前进度

    本地已有一个尚未提交/发布的窄修复:移除同步重放的三条冗余 SQL 路径。真实隔离 MatrixOne 上,新增 SQL 观察断言先在基线上复现了全历史 COUNT、最后事件查询和摘要 UPDATE;修改后,按 nextest 独立测试进程执行的 5 项 sync_outbox_ 数据库测试通过,覆盖批量重放/混合记录、投影身份、惰性 session 创建、删除 fence 和同 session ID 的跨 owner 隔离。这不是整体优化完成、生产部署或千用户容量结论。

    继续沿用本 issue 原验收,不降低门槛:

    • 同环境前后对比 SQL/事务数、行数与扫描量、每轮本地/云端持久化字节、网络字节、池/锁等待、CPU/RSS 和 P50/P95/P99。
    • 分开测 1,000 用户空闲、100/1,000 活跃任务及 100/10,000/100,000 历史事实;保留原要求的多机器、至少两个 Server 和持续至少一小时验证。
    • 覆盖断网、重启、磁盘满、丢 ACK、重复投递、取消、未知工具结果、删除、凭证撤销和用户隔离;报告 offered/completed/failed/queued,不能用“千连接在线”代替真实吞吐。
    • 分别审查实现、测试和文档的增量;保留真实行为与 unhappy path 测试,不增加只验证自身实现的重复测试。

    职责分工:本 issue 继续拥有 CLI/Edge/Server 的同 Session 语义、执行与存储归属、同步和长历史规模化;#1186 继续拥有通用 schema/SQL/冗余产品状态清理;#1302 第 5 节保留同拓扑非模型延迟与持久化性能验收。复用已有优化,不把已合并工作重复计入收益。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions