Skip to content

[backplane][transport] 抽取 backplane work 共享序列化与事件构造 helper #14

@beachspainc

Description

@beachspainc

现状与证据

以下 src/cobnet/backplane/work/* 多个模块存在重复实现,且语义高度一致,适合作为 backplane worker transport 通用层抽取:

  • _json_dumps

    • src/cobnet/backplane/work/ws/server.py:46
    • src/cobnet/backplane/work/ws/client.py:37
    • src/cobnet/backplane/work/process.py:60
    • src/cobnet/backplane/work/http/server.py:41
    • src/cobnet/backplane/work/http/client.py:42
    • src/cobnet/backplane/work/http/app.py:42
  • _error_event

    • src/cobnet/backplane/work/ws/server.py:51
    • src/cobnet/backplane/work/ws/client.py:42
    • src/cobnet/backplane/work/process.py:65
    • src/cobnet/backplane/work/http/server.py:46
    • src/cobnet/backplane/work/http/client.py:47
  • _event_from_result

    • src/cobnet/backplane/work/http/server.py:67
    • src/cobnet/backplane/work/http/client.py:58

问题点:

  • _json_dumps 仅差返回类型(str/bytes)与调用方底层序列化器来源,完全可以用统一参数封装。
  • _error_event 主要差 origin 默认值/入参细节,核心是 error_event(...) 的同构封装。
  • _event_from_result 在 HTTP server/client 两处重复了 WorkerResult -> WorkerEvent 的 done/error 映射逻辑。

建议

src/cobnet/backplane/work 增加通用 helper,例如 encoding.py / protocol.py

  • 统一 JSON 编码函数:serialize_worker_json(obj, *, return_bytes: bool = False)(可复用 host.asgi.json_text/json_bytes
  • 统一错误事件构造:build_error_event(request, *, origin="driver", message, error_type="Error", **extra)
  • 统一结果转换:build_terminal_event(request, result, *, require_identity=True)(可兼容现有 WorkerResult 与 raw 值)

并替换以上 13 处重复定义。

与既有 Issue 的关系

目前 open issues 中(#8-#13)未包含 backplane worker transport 的这一类重复实现。

验收

  • 新增 src/cobnet/backplane/work 通用辅助后,移除上述重复实现。
  • 保持现有行为与错误字段不变(包括 originerror/error_info 与流量编码语义)。
  • 补充最小回归(WS/HTTP/stdin JSONL 各一条)。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions