Skip to content

[design] are S3/S6/S9 + the dual-store one anti-pattern (state mirrored across N sites, synced by hand)? if so the boundary is the lever #717

Closed as not planned

Description

@caomengxuan666

Why this is being filed

I did an external differential run (GNU bash 5.2.21/5.3.0 as oracle, Windows x64) and then read docs/pitfall-taxonomy-and-governance.md, docs/dual-store-invariant.md, docs/semantic-ownership.tsv and #641. This is an outside observation about how the families are grouped — not a bug. If this is already written down somewhere I did not read, close it.

The observation

The taxonomy lists these as separate 乙类 families, and #641 frames the store separately:

From outside they read as one pattern: a piece of state is duplicated across N sites and kept consistent by hand, so every new site or new field is a new divergence point. The taxonomy's §1 already says the carrier has "\x1f 解码散布 50+ 文件" and calls that "M1 持续发生的结构性原因" — that is the same sentence, with N = 50+.

Mapping the recent external findings onto it (my own, for concreteness):

family the "N sites" recent instance
carrier / M1 50+ decode sites #707 tr [:cntrl:] skips carried bytes; niubash#383 / #695 wc -c measures the encoding
dual-store writers + the mirror #641 perf line (908× at K=5000) + "correctness bugs where the two stores disagree"
S6 subshell manual snapshot/restore list #702 set -E not inherited by ( )
S3 fd model N builtin output paths #699 2>&1 >file order, write-error routing; #701 read from /dev/fd/N
S9 traps (no ownership model) #702; #345 (closed), #441 (closed)
M5 / S13 merge plane "merge 丢修复" is the same pattern in the process dimension

§2's conclusion — "两个根因喂养了大半个事故库" — already names it for S1/S2. The question is whether it generalises to the runtime-state families and the store, because that changes the priority order.

The falsifiable part

If the frame is right, then the leading indicator is "sites touched per new state", not the bug count:

  • adding a property to the execution context should touch one place (a context struct), not a snapshot/restore list;
  • adding a consumer of expanded text should be impossible to forget (carrier_text should not be passable across a module boundary without decoding — a newtype, not a convention);
  • adding a writer to the variable store should not require a mirror update at all.

Prediction: if mirroring is the root, new S3/S6/S9 instances should keep arriving even when no new state was added — which is what the closed list behind #713 shows (≥ 10 fixes, four open today). If instead a 4-week window shows no new instances once no new state is added, the frame is wrong → close this.

What I would not ask for

Not "refactor everything". The cheap, order-preserving first steps are the ones where the boundary is already drawn:

  1. carrier: make the encoded form un-nameable outside the codec (newtype / #[repr]-private) so a consumer cannot hold carrier text by accident — turns M1 from a discipline into a type error.
  2. fd table: make it a value with sequenced operations, so 2>&1 >file cannot be applied out of order (redirection & fd: 2>&1 >file order, builtin write-error routing, closed-stderr status, subshell-uncaptured cd error, bare-cat same-file #699).
  3. context: one ContextState whose clone semantics define the sub-shell contract, so a new field is compiler-visible rather than a forgotten list entry (errexit/trap: set -e misfires in until ... break|continue; set -E not inherited by ( ) subshells #702, S6).

Each is local, and each removes a class rather than an instance — which is §4 rule 4 ("fix the invariant, not the symptom") applied to the runtime plane instead of the expansion plane.

Credit where due: platform-fd-inheritance.md is exactly the right way to close a cell (documented divergence + primary-source argument), and semantic-ownership.tsv is already the right shape for an owner registry. This issue is only asking whether the grouping is one pattern instead of four.

Activity

  1. added
    enhancement
    P2-medium
    on Oct 17, 2026
  2. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    核查结论(逐项读了代码与台账后)

    框架对一半,且对的那一半正是杠杆所在。"状态镜像于 N 处、手工同步"对进程内所有权成立(随每新增状态增长的那种);但 dual-store 的 mirror 与 __RUBASH_FD_*/FUNCTION_STDIN 属于 forkless 传输基座——GNU 靠 fork 的零代码拷贝,rubash 没有这个原语,这部分镜像不可消灭,只能类型化(§3.6 ADR 已裁决不追 fork 机制、只复刻四条语义)。逐家族核查:

    逐家族证据盘点

    声称的 "N sites" 实测(本仓 HEAD) 判定
    S6 子壳状态 ShellState 已落地(src/shell/state.rs:1-9 即隔离边界声明;clone_for_child_save@283、Clone@318);env_vars 等数十字段已收拢。落点证据:落地时实测两份手工清单(7 项 vs ~55 项)已漂移并打穿 alias/function 泄漏(§3.6 S6 实施记录);边界化之后此类泄漏零复发(现状 issue 里无一例状态泄漏) 框架成立且已被验证——边界就是杠杆,本仓自己证明了
    S9 trap 继承 唯一未边界化的大项:trap 表仍是 env_vars 里的冒号拼接字符串(TRAP_LIST/TRAP_PREFIX/TRAP_RESET @src/builtins/trap.rs:9-20,928);reset_for_subshell 被 7 处调用点各自触发(ast_exec.rs:498、command_substitution.rs:902/1256、compound_exec.rs:1460、embedded_mutations.rs:2037、pipeline_stages.rs:111、read_io.rs:583);ERR/errexit 判定门散在 4+ 处(function_calls.rs:485-641、ast_exec.rs:1302、command_dispatch.rs:248-262、trap_exec.rs:1198)。#719 修 #702 时只能给调用点新增变体 reset_for_subshell_child(两个入口生效),命令替换/管道/read 三条路径有意保留旧路径——结果 #720 立刻在同一接缝报出 4 个新 case(嵌套子壳过度触发、f() ( ) 平坦域、procsub、until 里的 return 归因)。修完即复发=手工清单的判据 最强反模式证据。事故史:#345、#441(closed)→ #702(open,2 case)→ #719 落地 → #720(4 case)。同类在 #713 台账里 closed 13 条
    S8/S3 诊断所有权 引擎已有 fd_table.write_endpoint(2)(compound_exec.rs:2318),但进程级 eprintln! 在主入口之外仍有 175 处(全仓 219),io::stdout()/stderr() 直写散布 48 文件(77 处);重定向通道 write_default_stderr 58 处/17 文件。三条通路并存 → #271、#510、#699 R1.2/R1.3(write-error 进不了命令自己的 2>file) 编译器看不见这个分布——这是静默的 N,边界(诊断只准走 fd_table 的 fd-2 端点)可单点收口
    S3 fd 模型 值语义 FdTable 已存在(src/executor/fd_table.rs:1-80:Rc<FileFd> 天然共享偏移;redirection.rs:891 apply_ordered_output_redirects 有序 apply)——边界已画出。残余镜像:__RUBASH_FD_* env 键 36 处/10 文件(compound_exec.rs:536-561、external_inner.rs:791 等;semantic-ownership.tsv redir 行已挂账 "remove remaining fd env mirror");FUNCTION_STDIN(_OFFSET) 传输镜像 165 引用/24 文件、18 处写点,其单位不变量靠 #711 的注释+测试钉住而非类型 部分收口中。#699 R1.1(2>&1 >file 失序)说明仍有未走有序 apply 的旁路路径
    #641 dual-store 镜像契约是 "每个 writer 返回前必须让 env_vars 反映 typed cell"(docs/dual-store-invariant.md),维护面 = 185 处 env_vars.insert + 16 文件 34 处 sync_typed_*;VarTable 的 Deref 让数百处值操作免改而扩散(var_table.rs:130-140 注释自认)。但:性能 908×/223× 是结构性成本已测;失同步 correctness 实际挂账命名的只有 #651 一条——#635-#639/#621 追到的是展开阶段序,非镜像失同步 不是最陡的杠杆。#638 已示范正确打法:改 reader 规则,不补 ~12 个 writer
    甲类 carrier shell_text_to_raw_bytes/bytes_to_shell_text 仍散布 39 文件,但 M1/M4 收口已落地(markers.rs 注册表 + 三边界入口);#707 属 M7 残余 已按 "修不变量而非补 N 处" 治理中,勿再动

    诚实权衡(反例)

    1. test(executor): pin fd 0 consumption cursor to transport units #711 的直接反证:fd-0 消费游标故意以传输单位镜像(pipeline_exec.rs:777-780 等 3 处),naive 的"统一为 raw"会把正确代码修成 bug(该 PR 实测复现了泄漏)。镜像不是万恶之源——无类型才是。这否定了 "给每个镜像点找单一 owner" 的机械化改法。
    2. forkless 约束:子进程继承必然经过字符串传输(Win32 句柄继承 POC 的 ERROR_87/白名单坑见 §3.6 阶段 2 实证,且 std raw_attribute 至今未进 stable)。消灭 __RUBASH_FD_* 的唯一途径是等/自建平台层,不是重构 shell 内部。
    3. 大改的代价:吸收 S9 进 ShellState 会让 trap 表从 env_vars 字符串变成 typed 字段——好处是 reset 变成一个 typed 函数而非 7 个调用点 × 语义组合;坏处是 trap 与 set -o(functrace/errtrace 联动 @ set/options.rs:343-349)和 fix(executor): until-break errexit attribution and -E subshell ERR inheritance (#702) #719 变体的三条 "有意保留" 路径需要一次说清,回归面是 trap/loop 全族。

    建议路径(增量边界,非大改)

    按今日 open 事故密度排序,先做两件:

    1. S9/S6 接缝=trap typed owner(第一名,事故最密):把 trap 表从 env_vars 字符串(trap.rs:9-20)收进 ShellState typed 字段,subshell 契约 = clone_for_child_save() 的一个 typed twist(errtrace/functrace 组合在唯一一处决定 DEBUG/ERR/RETURN 的去留),7 个 reset_for_subshell 调用点收敛为同一个函数;同时把 set -E ERR-trap edge cases: nested subshell fire count, f() ( ) flat region, process substitution, return-in-until attribution #720 第 4 条点名的那把总闸(command_dispatch.rs:262 归因门)一并定为一处语义。文件面:src/builtins/trap.rs、src/shell/state.rs、上表 7 个调用点文件、src/executor/trap_exec.rs。这是 fix(executor): until-break errexit attribution and -E subshell ERR inheritance (#702) #719 被迫打补丁变体的那个洞的正解。
    2. S3 诊断所有权(第二名,单 owner 可成):禁令扩写 §五/5+:"builtin 失败诊断只准经 fd_table.write_endpoint(2)"(通道已在,compound_exec.rs:2318),把 175 处 eprintln! 分批迁入;零行为变更即可先静态化。

    不做:dual-store 大改(留给 #616/#618 按 docs/dual-store-invariant.md 的 perf-slice 地图推进,reader-side 规则已由 #638/#650 验证);__RUBASH_FD_*/FUNCTION_STDIN 不消灭(forkless 传输基座,#711 结论:类型化+注释钉住,不是重构对象)。

    可证伪

    给维护者的显式选项

    选哪条由维护者定;本评论只主张:镜像分两类治理——进程内所有权状态的镜像用边界消灭,forkless 传输的镜像用类型钉住。

  3. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Supporting detail for the question in this issue, from a static pass over origin/master (d24ee0f0). Counts are measured; the recommendation is mine.

    Why the registry is a vocabulary, not a chokepoint

    markers.rs (963 lines) is thorough — one declaration site, Boundary::{Output,Storage,Reparse}, a MARKERS table, encode/decode constants, user_reachable, and tests/marker_leak_golden.rs. But the spread it was supposed to reduce is unchanged:

    • 108 files reference markers::, 788 references total
    • 176 non-definition decode-helper call sites across 47 files
    • 138 manual .replace(markers::…) decode chains across 31 files
    • 26 raw replace(CTLESC, "")

    So the registry standardised the names; it did not reduce the number of places that must remember to decode. That is the mirroring pattern this issue describes, in the carrier plane.

    Clearest single instance: materialize_expanded_command_word (command_prepare.rs:5) is the argv chokepoint, and pipeline_exec.rs:2415-2446 re-implements it inline — precisely the M1 shape that §1 of the taxonomy predicts.

    Recommendation

    Decode-at-the-boundary, hardened with a type, one subsystem at a time. Not a repo-wide CarrierText/DecodedText split — the repo already ruled that out (pitfall-taxonomy-and-governance.md:104-108), and that call looks right.

    Concretely: a VisibleText newtype (private field, no Deref/AsRef/Display, #[must_use] decode), applied in this order:

    1. Output boundary — locale.rs:369 → the sinks. Smallest call-site count, highest leak visibility.
    2. argv materializer — fold the 4 duplicate chains into the single chokepoint and delete pipeline_exec.rs's inline copy.
    3. Storage / reparse last.

    Ranked against the alternatives: the repo-wide newtype contradicts an existing owner decision; the lint-only route ships in <1 day but by construction cannot catch the actual M1 mode — "forgot to call the helper" has no textual signature.

    What must not change

    Byte-exact GNU parity; the in-band protocol itself; the zero-copy hot paths; glob's legitimate in-band CTLESC consumption. Main risk: an over-broad raw escape hatch re-opens the class — the hatch (if any) has to be narrower than the type, or the type buys nothing.

    Related

  4. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Cross-issue ownership book landed as a doc PR (link above): docs/audit/state-ownership-invariants.md. It turns this issue's question into a test — who writes it / who reads it / what a child inherits — and answers it seam by seam with file:line evidence, keeping the transport substrate (dual-store, __RUBASH_FD_*, FUNCTION_STDIN) out of the anti-pattern bucket on the #711 counter-example.

    Two mechanical additions to the design brief above:

    1. Recurrence signature as the re-open criterion — a fix in a seam followed by a sibling issue in the same seam (measured case: fix(executor): until-break errexit attribution and -E subshell ERR inheritance (#702) #719 fixed errexit/trap: set -e misfires in until ... break|continue; set -E not inherited by ( ) subshells #702, set -E ERR-trap edge cases: nested subshell fire count, f() ( ) flat region, process substitution, return-in-until attribution #720 filed four residuals within hours) re-opens the class, not just the instance. This is what makes "P1 before P2" defensible rather than taste.
    2. Per-step invariant tests are the acceptance: a source-guard test that fails when a new reset site appears outside TrapOwner, and the same for diagnostic writes; the lookup bucket's guard is the deterministic swap-exchange trigger (being built for niubash#392).

    Decisions requested in §4 of the doc: accept P1 (trap owner) first, P2 direction, dispatch P3 immediately (independent), and whether the recurrence signature becomes a formal re-open rule. #713's inherit/own matrix will be appended when that audit lands (marked pending in v1).

  5. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Closing this one on its own terms — the body said "If this is already written down somewhere I did not read, close it", and after reading further it is:

    What remains actionable from here is the concrete children, which are all open and untouched by this close:

    So: the observation is right, but it is the map's job, and the map is #641. Thanks — closing to keep the tracker free of a second map.

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

    P2-mediumenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions