diff --git a/.factory/breaker.sh b/.factory/breaker.sh index 934ed5c..fc4aa03 100755 --- a/.factory/breaker.sh +++ b/.factory/breaker.sh @@ -15,8 +15,12 @@ LIB="$(dirname "$0")/factory_lib.py" python3 "${LIB}" breaker "${LOCKSDIR}/floor.json" "${LOCKSDIR}/ledger.jsonl" && exit 0 rc=$? if [ "${rc}" -eq 3 ]; then - echo "成本熔断(R4):ledger 累计超 floor 上限,停止派发;人工复核 locks/floor.json 与 ledger.jsonl 后方可恢复" >&2 + # 熔断原因已由 factory_lib breaker_check 打到 stderr("熔断:连续失败 N 次 + # (上限 M),需人工介入" 或 "熔断:今日已跑 N 次(上限 M)")。此处不再包装: + # 曾固定输出"成本熔断 ledger 累计超 floor 上限",与连续失败类熔断事实不符, + # 误导排查方向(2026-09-05 实证)。 + : else - echo "成本熔断(R4):breaker 自身故障(floor.json/ledger.jsonl 缺失或损坏,见上方堆栈),fail-closed 停止派发;人工复核 locks/floor.json 与 ledger.jsonl" >&2 + echo "breaker 门自身故障(floor.json/ledger.jsonl 缺失或损坏,见上方堆栈):fail-closed 停止派发;人工复核 floor.json 与 ledger.jsonl" >&2 fi exit "${rc}" diff --git a/.factory/dispatch.sh b/.factory/dispatch.sh index 98f6e22..b2f619a 100755 --- a/.factory/dispatch.sh +++ b/.factory/dispatch.sh @@ -2,8 +2,8 @@ # dispatch.sh — S2 派发器入口 shim(零 LLM,设计 A3/§7)。 # # 进程编排(后台链并发槽/收割、mkdir+PID 硬锁、watch 循环、TERM/HUP 放锁) -# 已下沉 factory_lib.py dispatch 子命令(decisions.md ADR-005:bash 进程 -# 原语缺陷类在 Python 形态下结构性不可表达)。本文件只保留入口: +# 已下沉 factory_lib.py dispatch 子命令(docs/adr/ADR-005-dispatch-orchestration-python.md: +# bash 进程原语缺陷类在 Python 形态下结构性不可表达)。本文件只保留入口: # CLI/env 契约不变——--dry-run(DRY=1 同义)/--watch/--interval N # (INTERVAL 同义,默认 300s)/MAX_PARALLEL/FACTORY_MERGE_METHOD/ # FACTORY_AUTO_MERGE/GH_REPO/NODE_TIMEOUT,退出码语义不变(2=环境缺失、 diff --git a/.factory/downstream-check.sh b/.factory/downstream-check.sh index c98ff21..a7519b7 100755 --- a/.factory/downstream-check.sh +++ b/.factory/downstream-check.sh @@ -2,7 +2,8 @@ # downstream-check.sh — 中心仓对下游仓清单的集中巡检/追平。 # # 定位:替代「逐仓手工追平」。中心仓是工具链主权方(三态分发); -# 本脚本读 .factory/downstream.json(skip 态清单,仓特定数据),对每个 +# 本脚本读下游仓清单(ADR-012:真实清单住 downstream.local.json,gitignored; +# tracked downstream.json 仅空模板),对每个 # 下游仓调**中心版** sync-from-upstream.sh——下游副本滞后/缺失也照常 # 工作(鸡生蛋免疫);巡检一律以中心仓 main 为锚(发布线,非工作分支)。 # @@ -16,12 +17,17 @@ # 进程已死自动清锁重试一次(照抄 cron-dispatch 语义)。 # # 首次移植的新仓不入本清单——先走 README「移植到其他仓库」四步, -# 再由人工登记进 downstream.json。 +# 再由人工登记进 downstream.local.json(gitignored,ADR-012——勿写 +# tracked 模板,那正是 ADR-012 要杜绝的污染路径)。 set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" CENTER="$(cd "$SCRIPT_DIR/.." && pwd)" MANIFEST="$SCRIPT_DIR/downstream.json" +# ADR-012:真实清单住 downstream.local.json(gitignored 仓特定数据); +# tracked 模板仅文档占位,local 存在则优先 +LOCAL_MANIFEST="$SCRIPT_DIR/downstream.local.json" +[ -f "$LOCAL_MANIFEST" ] && MANIFEST="$LOCAL_MANIFEST" SYNC="$SCRIPT_DIR/sync-from-upstream.sh" LOCK="$SCRIPT_DIR/locks/downstream-check.lock" OUT_FILE="$(mktemp "${TMPDIR:-/tmp}/.factory-downstream-check.XXXXXX")" @@ -54,7 +60,9 @@ for r in rows: if not isinstance(r, dict) or not isinstance(r.get("path"), str) or not r["path"].strip(): sys.exit(1) print(r["path"])' "$MANIFEST" 2>/dev/null)" \ - || { echo "下游清单损坏(需非空 repos[].path): $MANIFEST" >&2; exit 2; } + || { _hint="——真实清单写 .factory/downstream.local.json(gitignored,ADR-012)" + case "$MANIFEST" in *downstream.local.json) _hint="——修复或删除该 local 清单(它已在 ADR-012 载体位)";; esac + echo "下游清单损坏(需非空 repos[].path): $MANIFEST $_hint" >&2; exit 2; } mkdir -p "$SCRIPT_DIR/locks" # 净克隆首跑:gitignored 目录缺失时 shlock ENOENT 被误读为锁被持 if ! /usr/bin/shlock -f "$LOCK" -p $$; then diff --git a/.factory/factory-lease.sh b/.factory/factory-lease.sh index 5b865b3..23761e5 100755 --- a/.factory/factory-lease.sh +++ b/.factory/factory-lease.sh @@ -118,12 +118,37 @@ _lease_sw_fresh() { # _lease_sw_fresh → 0=未过期 1=已过期;租 [ $(( $1 + ${_SW_SECS:-${FACTORY_LEASE_SECS:-900}} )) -gt "$now" ] } +_lease_sw_restore() { # → 不可覆盖放回:ln 原子判位后统一 rm 源 + # 检查-移动([ -e ] || mv)在检查与 mv 之间可被并发 claimer 占位,mv 会覆盖 + # 新主锁(双赢家);ln 硬链原子(dst 已存在即失败)——成功=dst 得内容删源链; + # 失败=位上有更新主,弃残本(其主 hb/fence 自毙,单写者仍成立)。 + ln "$1" "$2" 2>/dev/null || true + rm -f "$1" +} + _lease_sw_claim() { # → 成功打印 epoch,失败 return 1 local key="$1" secs="$2" mid="$3" local lock ctr mt held last new_ep stale _lease_sw_notice lock="$(_lease_sw_path "$key" .lock)"; ctr="$(_lease_sw_path "$key" .epoch)" mkdir -p "${lock%/*}" || return 1 + # 墓碑调和(PR #116 CodeRabbit):release 在 mv 后/放回前被 SIGKILL → .rel.* + # 会截留仍有效(新鲜)的持有者锁。建主锁前先处理:新鲜墓碑 = 活锁被截留 + # → 不可覆盖放回(_lease_sw_restore 的 ln 判位;随后正常流程读新鲜即拒, + # ≤ 租期后自解);过期/坏内容 = 死锁残渣 → 清理。 + for tomb in "${lock}".rel.*; do + [ -e "$tomb" ] || continue + _SW_MID=; _SW_EPOCH=; _SW_SECS="" + if _lease_sw_read "$tomb"; then + mt="$(_lease_mtime "$tomb")" + case "$mt" in ''|*[!0-9]*) rm -f "$tomb"; continue ;; esac + if _lease_sw_fresh "$mt"; then + _lease_sw_restore "$tomb" "$lock" + continue + fi + fi + rm -f "$tomb" + done held=0; _SW_MID=; _SW_EPOCH=; _SW_SECS="$secs" # 读失败时新鲜度按本 claim 租期判(原语义) if [ -f "$lock" ]; then _lease_sw_read "$lock" || true # 坏内容 → held=0(判代退回计数器) @@ -142,8 +167,7 @@ _lease_sw_claim() { # → 成功打印 epoch,失败 return # 并拒(放回位已有新锁则弃残本——输者 hb/fence 自毙,单写者仍成立)。 mt="$(_lease_mtime "$stale")" if _lease_sw_fresh "$mt"; then - [ -e "$lock" ] || mv "$stale" "$lock" 2>/dev/null - rm -f "$stale" + _lease_sw_restore "$stale" "$lock" return 1 fi rm -f "$stale" @@ -219,8 +243,8 @@ _lease_sw_release() { # → 尽力释放(幂等):持 if [ "$_SW_MID" = "$mid" ] && [ "$_SW_EPOCH" = "$epoch" ]; then rm -f "$tomb" else - [ -e "$lock" ] || mv "$tomb" "$lock" 2>/dev/null - rm -f "$tomb" + # 放回语义见 _lease_sw_restore:ln 原子判位,绝不 mv 覆盖并发新主 + _lease_sw_restore "$tomb" "$lock" fi return 0 } diff --git a/.factory/factory-lib.sh b/.factory/factory-lib.sh index e37d411..268ff2a 100755 --- a/.factory/factory-lib.sh +++ b/.factory/factory-lib.sh @@ -21,7 +21,11 @@ source "${REPO}/.factory/factory-lease.sh" omp_node() { # omp_node [omp-opts...] -- # omp CLI 唯一执行点(ADR-009 引擎收口,设计 §4 runNode 的 bash 形态): # 链/批次/PR 门/反哺全部节点经此 spawn——换引擎(SDK 直连等)只改本函数。 - # 契约:--no-session 恒加(物理级 fresh context,A1);--max-time 必填; + # 契约:--no-session 恒加(物理级 fresh context,A1);--no-extensions 恒加 + # (扩展发现把无头 -p 节点 hook 进 feishu 网关 attach:恒定 ~19s 附加税,且 + # attach 劣化即整预算静默死锁——#165 r2/r3 review 连挂实证,空 diff 也烧满 + # 17m 零输出;--no-extensions 后平凡探针 7.9s。显式 -e 不受影响,launchd + # 网关 daemon 独立托管亦不受影响);--max-time 必填; # opts 透传(--no-tools / --config …);prompt 为 "--" 之后的剩余参数整体。 # 返回 omp 进程退出码(调用方自持 metric/失败语义)。 local _cwd="$1" _log="$2" _tmo="$3" @@ -32,7 +36,7 @@ omp_node() { # omp_node [omp-opts...] -- _opts+=("$1"); shift done shift - (cd "${_cwd}" && omp -p "$*" --no-session ${_opts[@]+"${_opts[@]}"} \ + (cd "${_cwd}" && omp -p "$*" --no-session --no-extensions ${_opts[@]+"${_opts[@]}"} \ --max-time "${_tmo}" < /dev/null) > "${_log}" 2>&1 } @@ -50,10 +54,14 @@ issue_label_swap() { # issue_label_swap <"删,删"|空> <"加,加"> —— 单 local -a args=("issue" "set-labels" "${ISSUE}") [ -n "${1:-}" ] && args+=(--remove "${1}") [ -n "${2:-}" ] && args+=(--add "${2}") - if python3 "${REPO}/.factory/hosting.py" "${args[@]}" >/dev/null 2>&1; then + local err + if err="$(python3 "${REPO}/.factory/hosting.py" "${args[@]}" 2>&1 >/dev/null)"; then echo " [label] -${1:-} +${2}" else + # stderr 落档(#207 实证:>/dev/null 2>&1 把 HostingError 里的 gh + # 失败原因吞掉,[error] 行无从排查)——hosting 侧原因回显在错误行下。 echo "[error] 标签转移失败:-${1:-} +${2}(issue #${ISSUE})" >&2 + [ -n "${err}" ] && printf ' [hosting] %s\n' "${err}" >&2 return 1 fi } @@ -90,16 +98,30 @@ issue_reject() { # issue_reject —— 拒绝的 # 落标同时原子移除的标签。链入口传 "factory:triaging, # factory:in-progress";批次入口(零标签 issue)传 "" # 判据源;回执渲染于同目录 reject-receipt.md - # 失败语义分两级:落标失败 return 1——裁决未落定,调用方终止/跳过; + # 失败语义分两级:落标调用失败先复核远端标签态(#207 实证:gh 非零 + # 但标签已落远端——退出码 ≠ 落定事实),factory:rejected 确在列即按 + # 已落定继续回执,确证缺失才 return 1(裁决未落定,调用方终止/跳过); # 回执生成/评论失败仅告警——裁决已由标签落定,回执是透明度而非门, # 正文留档可手动补发。评论经 issue_comment 唯一出口,标记中和不因入口 # 不同而绕过;回执刻意不含裸标记——标记评论通道保留给人类手动覆盖。 # 回执带幂等键:同轮次重放(诈尸残窗/人工重跑)查重跳过;链侧带轮次 - # ROUND、批次侧固定 batch——同 issue 两入口的回执键天然互不冲突。 + # ROUND、批次侧固定 rbatch(r 前缀恒在,ROUND 缺省展开 batch)——同 + # issue 两入口的回执键天然互不冲突。 local marker="factory:receipt:issue-${ISSUE}:r${ROUND:-batch}" local dir dir="$(dirname "$2")" - issue_label_swap "${1:-}" "factory:rejected" || return 1 + if ! issue_label_swap "${1:-}" "factory:rejected"; then + # 假阴性复核(#207:mutation landed + client reported failure—— + # 2026-09-20 11:01:01Z 标签已落 GitHub 而 gh 报非零):落标调用失败 + # ≠ 裁决未落定。factory:rejected 在远端 = 裁决已生效,回执必须跟上 + # (steering 审查报告规范:只落标不发判据 = 不可审计的静默拒绝); + # 确证缺失才按未落定终止。 + if ! python3 "${REPO}/.factory/hosting.py" issue get-labels "${ISSUE}" 2>/dev/null \ + | grep -q '"factory:rejected"'; then + return 1 + fi + echo " [warn] 落标调用失败但 factory:rejected 已在远端(issue #${ISSUE})——按已落定继续回执" >&2 + fi if python3 "${REPO}/.factory/factory_lib.py" receipt "$2" \ > "${dir}/reject-receipt.md" 2>/dev/null; then if issue_comment "${dir}/reject-receipt.md" "${marker}" >/dev/null 2>&1; then diff --git a/.factory/factory_lib.py b/.factory/factory_lib.py index 3682877..48a7b87 100755 --- a/.factory/factory_lib.py +++ b/.factory/factory_lib.py @@ -8,7 +8,7 @@ evidence_suites 保证触及的 skills 套件必产出 verbose 证据段。 3. 熔断判定曾藏于 dispatch.sh heredoc,无法独立验证边界(跨天/重置/上限)。 4. dispatch 进程编排(后台链/wait/并发槽/硬锁)曾为 bash 进程原语,缺陷类 - 聚集(decisions.md ADR-002);2026-08-24 下沉本文件(ADR-005)。 + 聚集(docs/adr/ADR-002-a3-maintenance-ledger.md);2026-08-24 下沉本文件(ADR-005)。 CLI: factory_lib.py parse # 解析 agent 输出 JSON @@ -29,6 +29,7 @@ import signal import subprocess import sys +import tempfile import time from pathlib import Path @@ -52,20 +53,74 @@ class CircuitOpen(RuntimeError): def parse_agent_json(text: str, allowed: set[str]) -> dict: - """从 agent stdout 提取(唯一)JSON 裁决对象。 - - fence 优先(```json {...} ```),裸 JSON 贪心兜底;两者均取捕获组 1—— - 组 0 含围栏字面量,loads 必炸(见模块 docstring 缺陷 1)。 - verdict 不在 allowed → ValueError(fail-closed,不让坏裁决流入链)。 + r"""从 agent stdout 扫描返回首个 verdict 合法的完整 JSON 裁决对象。 + + fence 优先(```json 是 LLM 显式结构化输出信号,穷尽全部 fence 才轮到 + 裸对象——首个 fence 裁决非法不压制后位合法 fence);其后按文档序逐 `{` + 偏移 raw_decode——#207 实证贪心 `\{.*\}` 把「重复 JSON / 带花括号 + 尾文」从首 `{` 拼到末 `}`(Extra data: char 429)一次即崩;多对象 + 并存取首个合法者(重复块即恢复形态)。顶层 verdict 契约(PR #211 + Sourcery 评论1):对象一旦完整解析,其内部偏移全部丧失资格—— + 外层 verdict 非法时嵌套 evidence/元数据携带的合法 verdict 不代表 + 裁决(防坏裁决借嵌套混入链);坏 verdict 跳过其整个对象继续扫后续 + 顶层;解码失败的对象按字符串感知平衡范围整体跳过(未闭合则跳到 + 输入末尾——CodeRabbit 评论2:坏外层不得放行嵌套 verdict); + 无任何合法对象 → ValueError(fail-closed)。 """ - m = re.search(r"```json\s*(\{.*?\})\s*```", text, re.S) or re.search(r"(\{.*\})", text, re.S) - if not m: - raise ValueError("输出中未找到 JSON 对象") - d = json.loads(m.group(1)) - verdict = d.get("verdict") - if verdict not in allowed: - raise ValueError(f"verdict={verdict!r} 不在 {sorted(allowed)}") - return d + dec = json.JSONDecoder() + + def _decode(i: int) -> tuple[dict, int] | None: + try: + obj, end = dec.raw_decode(text, i) + except ValueError: + return None + return (obj, end) if isinstance(obj, dict) else None + + def _balanced_end(start: int) -> int | None: + """depth-0 `{` 起字符串感知扫描平衡闭区间终点(闭 `}` 后一位); + 未闭合返回 None(其后内容视为对象内部,一并放弃)。""" + depth = 0 + in_str = esc = False + for i in range(start, len(text)): + ch = text[i] + if in_str: + if esc: + esc = False + elif ch == "\\": + esc = True + elif ch == '"': + in_str = False + elif ch == '"': + in_str = True + elif ch == "{": + depth += 1 + elif ch == "}": + depth -= 1 + if depth == 0: + return i + 1 + return None + + for m in re.finditer(r"```json\s*(\{)", text): + hit = _decode(m.start(1)) + if hit is not None and hit[0].get("verdict") in allowed: + return hit[0] + # 文档序扫描:对象一旦完整解析,[i, end) 内的嵌套偏移全部跳过 + # ——顶层 verdict 契约(嵌套 verdict 不代表裁决) + skip_until = 0 + for i in sorted({i for i, ch in enumerate(text) if ch == "{"}): + if i < skip_until: + continue + hit = _decode(i) + if hit is None: + # 解码失败的对象整体跳过其平衡范围,不许嵌套 `{` 接管裁决 + end = _balanced_end(i) + skip_until = len(text) if end is None else end + continue + obj, skip_until = hit + if obj.get("verdict") in allowed: + return obj + raise ValueError( + f"输出中未找到 JSON 裁决对象(合法 verdict ∈ {sorted(allowed)})") def evidence_suites(changed_files: list[str]) -> list[str]: @@ -116,14 +171,19 @@ def _load_ledger(path: str) -> list[dict]: # 节点预算(S3 实测校准:#2/#5 链——裁决器秒级、prime/plan/review 分钟级、 # implement 十分钟级)。env 覆盖:FACTORY_TIMEOUT_(单节点)> # FACTORY_TIMEOUT(全局兜底)> 下表默认。 +# 重校准(2026-09-04,#113/#123/#124/#131 四链 28 节点次,ok-run P95): +# 口径 max(P95×1.2, 下列默认) 向上取整分钟——implement 1534s→31m、 +# review 845s→17m 上调;prime 687.6s / plan 882s / 裁决器均低于默认,不动。 +# 注意 report 子命令的建议口径(仅 ok-run P95+2m)不含撞顶截尾观测, +# 手工复核时应将「no-artifact 且 secs≥预算」的行按预算值计入下界。 NODE_TIMEOUTS = { - "triage": "5m", # 无工具裁决器,实测 ~10s - "holdout": "5m", # 无工具验证器,实测 ~15s - "prime": "15m", - "plan": "15m", - "review": "15m", - "pr-review": "15m", - "implement": "30m", # P95 未知前保守;ledger.secs 积累后按分布再调 + "triage": "5m", # 无工具裁决器,P95 144s + "holdout": "5m", # 无工具验证器,P95 86s + "prime": "20m", # 旧样 P95×1.2=687.6s 已过期:现测 865/884s 贴顶、900s 撞顶(#165 r6)→ 20m + "plan": "20m", # 旧样 P95×1.2=882s 已过期:#165 r4 实测 901s 压线过 → 20m + "review": "30m", # 17m 撞顶(原 15m→17m 仍不够):#165 r2/r3 1026/1021s 败、r4 1020s 压线 → 30m + "pr-review": "15m", # 无 ok-run 样本,维持默认 + "implement": "31m", # P95 1534s×1.2=1840.8s → 31m(原 30m 撞顶 #113 r1) } @@ -140,7 +200,9 @@ def node_metric_line(node: str, t0: int, now: int, status: str) -> str: def node_timeout(name: str, env: dict | None = None) -> str: - env = env if env is not None else {} + # 未显式传 env 时读进程环境:CLI(factory_lib.py timeout )由此获得 + # FACTORY_TIMEOUT_*/FACTORY_TIMEOUT 覆盖能力(launchd/cron 免 PR 调预算)。 + env = env if env is not None else os.environ per_node = env.get(f"FACTORY_TIMEOUT_{name.upper().replace('-', '_')}") return per_node or env.get("FACTORY_TIMEOUT") or NODE_TIMEOUTS.get(name, "15m") @@ -286,6 +348,100 @@ def docstring_gate_cmd() -> str | None: raise RuntimeError("docstring_gate_cmd 禁含换行(read -r -a 只取首行,shlex 多行拆词,两侧 argv 分歧)") return v +_PARALLEL_STACKS = frozenset({ + "pytest", "maven", "gradle", "jest", "vitest", "go", "cargo", "phpunit", "dotnet", +}) +_PARALLEL_SEGMENT_KEYS = frozenset({"tag", "name", "argv", "shell", "cwd", "intra", "stack"}) + + +def parallel_gate_cfg() -> dict | None: + """并行测试门配置(可选键 parallel_gate,缺省不启用;ADR-016)。 + + 与 docstring_gate_cmd 同为**可选键**范式:键缺失 → None(本仓未采用 + 并行门,零行为变化);键存在 → 严格校验,损坏即 RuntimeError + (fail-closed:可选 ≠ 静默降级为旧串行门——门是裁决路径,配置坏了 + 必须炸,不能悄悄不并行或漏跑段)。schema 拒绝未知键:本键的失效 + 形态是「静默慢」(拼错 intra/segments 导致能力没开、段没跑),与 + final_gate_cmd 的禁引号约束同理,宁可误伤不可漏拦。 + + 结构:{"workers": int≥0(0=不限并发,缺省 0), "segments": [ + {"tag": 唯一标识(禁换行——宿主脚本按行回收失败段清单), + "name": 显示头(缺省=tag), "argv": [词...] XOR "shell": "命令串", + "cwd": 段工作目录(相对仓根,缺省=仓根), + "intra": "off"|"auto"(段内并行档,缺省 off——顺序敏感测试所在 + 段保持串行,auto 是逐段显式 opt-in), + "stack": 显式测试栈(缺省按段 cwd 构建文件探测)}]}。 + argv 词 "$PY" 在执行期替换为解释器(env PYTHON 或 python3,逐字 + 保留宿主仓门禁脚本的 PY 语义);shell 段以 bash -o pipefail 执行, + $1 = 解释器占位。 + """ + if "parallel_gate" not in _LOCAL_CFG: + return None + raw = _LOCAL_CFG["parallel_gate"] + try: + if not isinstance(raw, dict): + raise ValueError("parallel_gate 须为对象") + if unknown := set(raw) - {"segments", "workers"}: + raise ValueError(f"未知顶层键: {sorted(unknown)}") + segs_raw = raw.get("segments") + if not isinstance(segs_raw, list) or not segs_raw: + raise ValueError("segments 须为非空数组") + seen: set[str] = set() + segs: list[dict] = [] + for s in segs_raw: + if not isinstance(s, dict): + raise ValueError("段须为对象") + tag = s.get("tag") + if not isinstance(tag, str) or not tag.strip(): + raise ValueError("段缺 tag(非空字符串,失败聚合标识)") + if "\n" in tag or "\r" in tag: + raise ValueError(f"段 tag 禁换行(失败清单按行回收): {tag}") + if tag in seen: + raise ValueError(f"段 tag 重复: {tag}") + seen.add(tag) + if bad_keys := set(s) - _PARALLEL_SEGMENT_KEYS: + raise ValueError(f"段 {tag} 未知键: {sorted(bad_keys)}") + # 二选一按「键存在」判(PR #220 评论):{"argv": [], "shell": + # "x"} 若按值判会双双通过 XOR,空 argv 落到 spawn 期才炸 + has_argv = "argv" in s + has_shell = "shell" in s + if has_argv == has_shell: + raise ValueError(f"段 {tag}: argv(词数组)与 shell(命令串)二选一") + if has_argv and ( + not isinstance(s["argv"], list) + or not s["argv"] + or not all(isinstance(w, str) and w for w in s["argv"]) + ): + raise ValueError(f"段 {tag}: argv 词须为非空字符串") + if has_shell and ( + not isinstance(s["shell"], str) or not s["shell"].strip() + ): + raise ValueError(f"段 {tag}: shell 须为非空字符串") + intra = s.get("intra", "off") + if intra not in ("off", "auto"): + raise ValueError(f"段 {tag}: intra 取值 off|auto(得到 {intra!r})") + stack = s.get("stack") + if stack is not None and stack not in _PARALLEL_STACKS: + raise ValueError(f"段 {tag}: 未知 stack {stack!r}(合法: {sorted(_PARALLEL_STACKS)})") + norm = {"tag": tag, "name": s.get("name") or tag, "intra": intra} + if "argv" in s: + norm["argv"] = s["argv"] + if "shell" in s: + norm["shell"] = s["shell"] + if "cwd" in s: + if not isinstance(s["cwd"], str) or not s["cwd"].strip(): + raise ValueError(f"段 {tag}: cwd 须为非空字符串") + norm["cwd"] = s["cwd"] + if stack is not None: + norm["stack"] = stack + segs.append(norm) + workers = raw.get("workers", 0) + if not isinstance(workers, int) or isinstance(workers, bool) or workers < 0: + raise ValueError("workers 须为 ≥0 整数(0=不限并发)") + return {"workers": workers, "segments": segs} + except (KeyError, TypeError, ValueError) as exc: + raise RuntimeError(f"factory-local.json parallel_gate 不可用(fail-closed): {exc}") from exc + def repo_vars_text() -> str: """拼进工作节点 prompt 的「仓库参数」段(prompts 零宿主专名的注入面)。 @@ -377,14 +533,18 @@ def neutralize_marker(text: str) -> str: return text def rejected_reconcile(issues: list[dict]) -> list[dict]: - """open+factory:rejected issue → 人工处置活动对账(纯函数)。 + """open+factory:rejected issue → 回执存在性 + 人工处置活动对账(纯函数)。 - 闭环缺口(2026-08-23 审计实证):4 个 rejected issue 的修复已由人工 + 闭环缺口一(2026-08-23 审计实证):4 个 rejected issue 的修复已由人工 feedback PR 吸收进 main,但 issue 仍 open 挂 rejected——链的 reject 语义是"不修",reject→人工路径有效但没有回写闭环,"已修未关"只能靠 人工审计发现。本函数不判定"是否已修复"(语义判断,机器不可判定), - 只暴露处置信号:reject 回执之后的人工评论数(bot 回执标题为界)。 + 只暴露处置信号:reject 回执之后的人工评论数(回执幂等 marker 为界)。 有后续人工评论 = 大概率已处置,提示复核关闭;零评论 = 静默滞留。 + 闭环缺口二(2026-09-20 #207 实证):落标假阴性早退 → 只落标无回执 + ——只落标不发判据是不可审计的静默拒绝(steering 审查报告规范), + has_receipt=False 即链完整性违规。无回执时不计人工评论(提交/ + 裁决前评论 ≠ 处置信号,缺回执本身已是要点名的违规)。 输出仅报告(dispatch 每轮尾部 echo),不动作——铁律 4:零 LLM 纯 bash 调用,关闭决策永远归人类。 """ @@ -392,22 +552,66 @@ def rejected_reconcile(issues: list[dict]) -> list[dict]: for it in issues: comments = [c for c in (it.get("comments") or []) if isinstance(c, dict)] + # 回执判据 = 幂等 marker(issue_reject 评论尾埋 + # ,hosting 两平台均保留注释): + # 标题子串可被人工评论复述冒充、屏蔽完整性违规(PR #211 + # Sourcery 评论2);number 缺失的畸形条目 marker 永不命中 → + # has_receipt=False(fail-closed 方向) + marker = f"factory:receipt:issue-{it.get('number')}:r" bot_idx = [i for i, c in enumerate(comments) - if "工厂 triage 裁决:reject" in str(c.get("body") or "")] - after = comments[(max(bot_idx) + 1):] if bot_idx else comments + if marker in str(c.get("body") or "")] + after = comments[(max(bot_idx) + 1):] if bot_idx else [] # 人工评论 = 有 author 且非 [bot] 后缀(GitHub bot 通用标识)且 - # 非链回执;缺 author 的畸形条目不计(报告宁少勿多) + # 非链回执(marker 同上——人工复述标题是真人工活动,应计数); + # 缺 author 的畸形条目不计(报告宁少勿多) human = [c for c in after if str(c.get("author") or "") and not str(c.get("author")).endswith("[bot]") - and "工厂 triage 裁决" not in str(c.get("body") or "")] + and marker not in str(c.get("body") or "")] out.append({ "number": it.get("number"), "title": str(it.get("title") or "")[:60], + "has_receipt": bool(bot_idx), "human_comments_after_reject": len(human), }) return out +def regression_routing(issue: dict, lease_alive: bool | None = None) -> str: + """已有 open 回归 issue 的标签态 → 失败结果投递路由(纯函数)。 + + L2 滞留接线(2026-09-15 issue #165 实证):日回归失败复用 open 的 + [factory-regression] issue 时只看标题不看标签态,失败结果会投进 + 派发器永久跳过的容器(in-progress 留守 / rejected 死信筒)。 + 路由规则(与 fix-issue.sh §7.5 留守语义、dispatch_liveness 第三死法 + 同一滞留判据:标签在而租约不活): + - factory:in-progress 在标: + - lease_alive is False(滞留:零改动轮留守 / 链早亡)→ "wake": + 留守是防"已验证诉求"被重派,新失败 ≠ 同一诉求,移除 + in-progress 唤醒重派(容器复用); + - lease_alive 为 True(修复链在途)→ "live":只评论追加不唤醒 + ——剥掉存活链的 in-progress 会使 §7.5 留守对该轮失效(审查 + major:同一概念两种判据的矛盾,2026-09-16 修复); + - lease_alive 为 None(形态未知:PG 租约在库中 / 判定失败)→ + "live":保守不自动唤醒,人工处置(与 liveness 降级一致)。 + - factory:rejected 在标 → "new":人工已判噪音,新失败不得进人工 + 已拒容器,开新 issue 承载(旧容器不动,拒裁不推翻); + - 其余(零标签待 triage / in-review / needs-human)→ "append": + 原样追加——in-review 有 PR 评审面、needs-human 本就等人看。 + in-progress 与 rejected 互斥(状态机单请求原子换标),若数据异常 + 并存,wake/live 优先(可自动处置的路由优先)。 + """ + labels = set(issue.get("labels") or []) + # Sourcery 目标形态叠加:lift-return-into-if(return 提入分支)× + # assign-if-exp(if/else-return 化三元)——elif 链各分支直接 return、 + # 内层用三元、无尾随语句(PR #192 门禁第四轮) + if "factory:in-progress" in labels: + return "wake" if lease_alive is False else "live" + elif "factory:rejected" in labels: + return "new" + else: + return "append" + + RECEIPT_CLOSURE_NOTE = ( "> 处置协议:走人工 PR 修复本 issue 时,PR 描述请带 `Closes #<编号>`" "——合并即自动关闭,避免「已修未关」滞留(reject→人工路径的闭环盲区)。" @@ -463,7 +667,7 @@ def reject_receipt(triage: dict) -> str: return "\n".join(lines) # ═════════════════════════════════════════════════════════════════════ -# dispatch 进程编排(2026-08-24 自 dispatch.sh 下沉,decisions.md ADR-005) +# dispatch 进程编排(2026-08-24 自 dispatch.sh 下沉,docs/adr/ADR-005-dispatch-orchestration-python.md) # # 动机:ADR-002 记账的缺陷类——jobs 表/wait 落空(0d947f60)、管道吞码 # (61c119c2)、管道早退(a4d81930)、trap 吞错(c749ac5e)——全部是 @@ -798,15 +1002,19 @@ def _final_sync(cfg: _DispatchCfg) -> None: def _reconcile_rejected(cfg: _DispatchCfg) -> None: - """rejected 存量对账(reject→人工闭环缺口,2026-08-23 审计)。 - 只报告不动作(铁律 4):有 reject 后人工评论的 → 提示复核关闭; + """rejected 存量对账(reject→人工闭环缺口,2026-08-23 审计; + 2026-09-20 #207 补回执存在性)。只报告不动作(铁律 4):缺回执的 → + 点名补发(链完整性违规);有 reject 后人工评论的 → 提示复核关闭; 零评论的 → 静默滞留计数。关闭决策永远归人类。""" for r in rejected_reconcile(_hosting_json( cfg, "issue list(rejected)", lambda: cfg.adapter.issue_list(state="open", label="factory:rejected", limit=100, comments=True))): c, t = r["human_comments_after_reject"], r["title"] - if c > 0: + if not r["has_receipt"]: + print(f" [rejected] #{r['number']} 缺回执评论(只落标不发判据 = " + f"不可审计的静默拒绝,#207 同型)——需补发({t})") + elif c > 0: print(f" [rejected] #{r['number']} 裁决后有 {c} 条人工评论——已处置?复核关闭({t})") else: print(f" [rejected] #{r['number']} 静默滞留(无后续人工评论,{t})") @@ -816,9 +1024,26 @@ def _upstream_sync_check(cfg: _DispatchCfg) -> int: """M2 上游同步检查(设计 §11.2):零 LLM、不占 R4 预算。 不复用 fix-issue 链(guard PERIMETER 含 .factory/,链按设计拦工具链 自变更);exit 0 = 同步已推进 → 当轮即止(自我指涉护栏:后续派发仍跑 - 内存旧脚本,下一轮生效);1/2/3 不阻断本轮派发。""" + 内存旧脚本,下一轮生效);1/2/3 不阻断本轮派发。 + + 根仓免跑(2026-09-05 裁决:本仓库是根仓、无上游):锁文件缺 + upstream 字段 = 无上游声明 → 直接跳过,不再触发脚本 exit 2 噪音 + (此前 103 轮 dispatch 日志刷「缺 upstream 字段」)。fork 侧 + sync --apply 写锁带 upstream=根仓 URL → 走正常检查自动追平。""" check = cfg.factory / "upstream-sync-check.sh" - if os.access(check, os.X_OK) and (cfg.factory / "upstream-lock.json").is_file(): + lock = cfg.factory / "upstream-lock.json" + if os.access(check, os.X_OK) and lock.is_file(): + try: + payload = json.loads(lock.read_text(encoding="utf-8")) + if not isinstance(payload, dict): + raise ValueError("upstream-lock.json must contain an object") + declared = payload.get("upstream") + except (OSError, ValueError): + print("(upstream-lock.json 不可读或非对象——跳过上游同步,需人工处置)") + return 0 + if not declared and not os.environ.get("FACTORY_UPSTREAM"): + print("(根仓无上游声明,免上游同步——fork 侧仍自动追平)") + return 0 if subprocess.run(["bash", str(check)]).returncode == 0: print("上游同步已推进,本轮派发即止(下轮生效)") return 0 @@ -919,6 +1144,9 @@ def dispatch_main(args: list[str]) -> int: return 2 if not adapter.auth_ok(): print("托管平台不可用(hosting auth:gh 凭据或云效令牌)", file=sys.stderr) + if diag := getattr(adapter, "auth_diagnose", lambda: "")(): + # 留痕可回溯(2026-09-05 02:00 事故:auth_ok 失败细节被丢弃) + print(f"hosting auth 诊断: {diag}", file=sys.stderr) return 2 # 主树锚定:git-common-dir 在 worktree 中指向主 .git,据此回到主树 # .factory(39b6b8ec 硬锁语义);非 git 环境退回 CWD 仓 .factory @@ -944,6 +1172,239 @@ def dispatch_main(args: list[str]) -> int: release_dispatch_lock(lock_dir) +# ═════════════════════════════════════════════════════════════════════ +# 并行测试门编排(ADR-016:自宿主仓门禁脚本下沉,能力随 full 面分发) +# +# 动机与 ADR-005 同型:段 fan-out(后台任务表/两轮 wait/按序回放/失败 +# 聚合/日志保留)长期驻留宿主仓的 bash 门禁脚本,能力无法到达下游 +# java/php/dotnet/go 等多语言仓。下沉本模块后:编排一次实现, +# DISTRIBUTION full 面零新增文件即达全部下游;段清单/并发上限/段内 +# 并行档 = 数据(factory-local.json parallel_gate 可选键,缺省不启用 +# ——未采用下游零行为变化);语言适配表 = 代码(保守档:fork 级分发, +# 无进程内线程交错)。bash 边角语义类缺陷(wait 落空/管道吞码/trap +# 吞错,ADR-002 记账)在新消费方结构上不可表达。 +# ═════════════════════════════════════════════════════════════════════ + + +def detect_stack(root: Path) -> str | None: + """构建文件探测 → 测试栈 id(best-effort;段配置 stack 显式指定优先)。 + + 覆盖 full 面下游语言谱系(java/gradle/go/rust/php/dotnet/js/ + python),构建系统标记优先于包管理器标记,多语言混合目录取第一 + 命中。未命中 → None:段内并行档自动降级串行并提示(见 + intra_parallel_args),不猜。 + """ + if (root / "pom.xml").is_file() or (root / "mvnw").is_file(): + return "maven" + for m in ("build.gradle", "build.gradle.kts", "settings.gradle", "settings.gradle.kts"): + if (root / m).is_file(): + return "gradle" + if (root / "go.mod").is_file(): + return "go" + if (root / "Cargo.toml").is_file(): + return "cargo" + for m in ("phpunit.xml", "phpunit.xml.dist"): + if (root / m).is_file(): + return "phpunit" + if any(root.glob("*.csproj")) or any(root.glob("*.sln")): + return "dotnet" + pkg = root / "package.json" + if pkg.is_file(): + try: + d = json.loads(pkg.read_text(encoding="utf-8")) + except (OSError, ValueError): + d = {} + deps = {*(d.get("dependencies") or {}), *(d.get("devDependencies") or {})} + test_cmd = str((d.get("scripts") or {}).get("test") or "") + if "vitest" in deps or "vitest" in test_cmd: + return "vitest" + if "jest" in deps or "jest" in test_cmd: + return "jest" + if (root / "pytest.ini").is_file() or (root / "conftest.py").is_file(): + return "pytest" + pyproject = root / "pyproject.toml" + if pyproject.is_file() and "[tool.pytest" in pyproject.read_text( + encoding="utf-8", errors="replace"): + return "pytest" + return None + + +def _xdist_available(py: str) -> bool: + """pytest-xdist 软依赖探测(缺席 → 段内串行 + ℹ️ 提示,不炸门)。""" + return subprocess.run([py, "-c", "import xdist"], capture_output=True).returncode == 0 + + +def intra_parallel_args(stack: str | None, py: str) -> tuple[list[str], str | None]: + """测试栈 → 段内并行 argv 后缀 + 观测提示行(保守档,ADR-016)。 + + 保守档原则:只注入「模块级/fork 级分发」参数——测试类分布到并行 + 单元、单元内保持串行;不注入进程内线程交错类参数(如 Maven + -Dparallel=methods 这类顺序敏感放大器)。框架默认已并行的栈零附加 + 参数(诚实声明,不重复计功);无保守参数可注的栈同样声明而非 + 硬凑。顺序敏感测试所在段的逃生舱 = 段级 intra:off(显式回退串行)。 + """ + if stack == "pytest": + if _xdist_available(py): + return ["-n", "auto"], None + return [], "未装 pytest-xdist:pytest 段内串行降级(pip install pytest-xdist 提速)" + if stack == "maven": + # -T 1C 模块级并行;forkCount=1C 测试类跨 fork 分发 + + # reuseForks 复用 JVM——类内方法仍串行(Surefire 保守档) + return ["-T", "1C", "-DforkCount=1C", "-DreuseForks=true"], None + if stack == "gradle": + # 任务级并行。测试 fork 并行(maxParallelForks)是构建脚本面, + # CLI 无保守注入点——需要时在仓内 build 配置 + return ["--parallel"], None + if stack == "jest": + return ["--maxWorkers=50%"], None + if stack in ("vitest", "go", "cargo"): + return [], f"{stack} 默认已并行(进程/包/线程池内建),无附加参数" + if stack in ("phpunit", "dotnet"): + return [], f"{stack} 无 CLI 保守并行参数(paratest / xunit.runner.json 属仓面配置),段内串行" + return [], "未识别测试栈:段内串行(段配置 stack 显式指定可启用并行档)" + + +# 中断清理限宽:SIGTERM 后最多等这么久,仍存活的段进程组 SIGKILL 收尾 +_TERM_GRACE_SECS = 5.0 + + +def _kill_group(pgid: int, sig: int) -> None: + """向段进程组发信号;组已消失(已退出/已收割)则静默——清理不放大异常。""" + try: + os.killpg(pgid, sig) + except (ProcessLookupError, PermissionError): + pass + +def _seg_log_path(log_dir: Path, i: int, tag: str) -> Path: + """段日志路径:序号前缀保回放序可读;tag 清洗禁路径分隔符。""" + return log_dir / f"{i:02d}-{tag.replace('/', '__')}.log" + + +def run_parallel_gate( + repo_root: Path | None = None, + cfg: dict | None = None, + failed_tags_path: str | None = None, +) -> int: + """执行并行测试门:段 fan-out + 可选有界并发 + 按配置序回放。 + + 返回 0=全绿 / 1=段失败 / 2=配置错误(fail-closed,不产出裁决)。 + 段日志私有化于 tempfile.mkdtemp(尊重 TMPDIR):全绿即删;失败保留 + 并打印路径(事后取证语义对齐宿主仓门禁脚本的段日志保留)。回放 + 序=配置段序、与完成序解耦——输出 diff 稳定,先完成的段不抢跑。 + 失败段 tag 逐行写 failed_tags_path(供宿主脚本聚合串行段失败后一次 + 裁决;无失败=空文件)。workers=0 不限并发(缺省),>0 有界槽位 + 轮转(CI 资源受限仓的节流面)。段独立进程组(start_new_session) + 执行;中断(BaseException)按组收尸:SIGTERM → 限宽等待 → SIGKILL + → 收割直接子进程,段日志保留取证,原始异常照抛(PR #220 评论)。 + """ + root = Path(repo_root) if repo_root is not None else Path(__file__).resolve().parent.parent + if cfg is None: + # 损坏配置 RuntimeError 在此转 rc 2(PR #220 评论):宿主脚本仅 + # 对 2 走 fail-closed 立即终止,traceback(rc 1)会漏进普通段 + # 失败路径 + try: + cfg = parallel_gate_cfg() + except RuntimeError as exc: + print(exc, file=sys.stderr) + return 2 + if cfg is None: + print("未配置 parallel_gate(可选键缺失),无并行门可执行", file=sys.stderr) + return 2 + py = os.environ.get("PYTHON") or "python3" + + # 执行前预算:cwd 存在性 + intra 档解析 + 注记去重(原子性:任何段 + # 配置错误在 spawn 前拦截,不产生半跑状态——对齐 fail-closed 范式) + suffixes: list[list[str]] = [] + notes: list[str] = [] + for seg in cfg["segments"]: + cwd = root / seg.get("cwd", ".") + if not cwd.is_dir(): + print(f"段 {seg['tag']}: cwd 不存在: {cwd}", file=sys.stderr) + return 2 + if "argv" in seg and seg.get("intra", "off") == "auto": + suffix, note = intra_parallel_args(seg.get("stack") or detect_stack(cwd), py) + suffixes.append(suffix) + if note and note not in notes: + notes.append(note) + else: + suffixes.append([]) + for n in notes: + print(f"ℹ️ {n}") + + segs = cfg["segments"] + log_dir = Path(tempfile.mkdtemp(prefix="factory-parallel-gate.")) + rcs: list[int | None] = [None] * len(segs) + failed: list[str] = [] + running: list[tuple[int, subprocess.Popen]] = [] # try 外可见:异常清理要收它 + try: + pending = list(range(len(segs))) + workers = cfg["workers"] + while pending or running: + while pending and (workers == 0 or len(running) < workers): + i = pending.pop(0) + seg = segs[i] + if "argv" in seg: + words = [py if w == "$PY" else w for w in seg["argv"]] + suffixes[i] + else: + # shell 段:bash -c 名义参数 "_" 占 $0、解释器进 $1 + # (宿主脚本 $PY 占位约定的等价面);pipefail 保管道 + # 段退出码真实(吞码缺陷类,ADR-002 记账) + words = ["bash", "-o", "pipefail", "-c", seg["shell"], "_", py] + # 独立进程组(会话组长,pgid=pid):中断清理 killpg 收整组, + # 段的测试后代(xdist worker 等)不残留为孤儿 + with open(_seg_log_path(log_dir, i, seg["tag"]), "wb") as log_fh: + running.append((i, subprocess.Popen( + words, cwd=str(root / seg.get("cwd", ".")), + stdout=log_fh, stderr=subprocess.STDOUT, + start_new_session=True))) + still: list[tuple[int, subprocess.Popen]] = [] + for i, p in running: + if (rc := p.poll()) is None: + still.append((i, p)) + else: + rcs[i] = rc + running = still + if running: + time.sleep(0.05) + + for i, seg in enumerate(segs): + print(f"── {seg.get('name') or seg['tag']}") + text = _seg_log_path(log_dir, i, seg["tag"]).read_text( + encoding="utf-8", errors="replace") + sys.stdout.write(text if not text or text.endswith("\n") else text + "\n") + print() + if rcs[i] != 0: + failed.append(seg["tag"]) + except BaseException: + # 中断(KeyboardInterrupt/信号)也要让取证路径可观测,不留哑尸体: + # 逐组 SIGTERM → 限宽等待 → SIGKILL → 收割直接子进程。限宽窗内的 + # 二次中断与超时同路(升级 SIGKILL),清理异常不外抛、不覆盖原始异常 + print(f"⚠️ 并行门中断,段日志保留: {log_dir}", file=sys.stderr) + for _i, p in running: + _kill_group(p.pid, signal.SIGTERM) + deadline = time.monotonic() + _TERM_GRACE_SECS + for _i, p in running: + try: + p.wait(timeout=max(0.0, deadline - time.monotonic())) + except BaseException: # TimeoutExpired 与限宽窗内二次中断同路:升级收割 + _kill_group(p.pid, signal.SIGKILL) + try: + p.wait() + except OSError: + pass + raise + if failed_tags_path is not None: + Path(failed_tags_path).write_text( + "".join(f"{t}\n" for t in failed), encoding="utf-8") + if failed: + print(f"❌ 失败段: {' '.join(failed)}") + print(f"❌ 段日志保留: {log_dir}", file=sys.stderr) + return 1 + shutil.rmtree(log_dir, ignore_errors=True) + print(f"✅ 并行段 {len(segs)}/{len(segs)} 全绿") + return 0 + + def main(argv: list[str]) -> int: if len(argv) < 2: print(__doc__, file=sys.stderr) @@ -968,6 +1429,37 @@ def main(argv: list[str]) -> int: for r in rejected_reconcile(json.load(sys.stdin)): print(f"{r['number']}\t{r['human_comments_after_reject']}\t{r['title']}") return 0 + if cmd == "regression-routing": + # regression-routing [alive|dead|unknown] < issue.json —— 日回归 + # 失败投递路由(wake|live|new|append,见 regression_routing); + # alive/dead/unknown = 滞留 issue 的租约存活态(调用方经 + # lease-fresh 判定),缺省 unknown(保守不唤醒) + state = {"alive": True, "dead": False}.get(argv[2] if len(argv) > 2 + else "unknown") + print(regression_routing(json.loads(sys.stdin.read()), state)) + return 0 + if cmd == "lease-fresh": + # lease-fresh —— 单写者锁文件存活判定:内容第 5 字段 + # = 租期秒(缺省回退 900),mtime+租期 > now = 活;文件缺失/ + # 不可读/内容畸形 = 死(SIGKILL 残锁过期同判)。退出码 0=活 1=死 + # 2=参数错。PG 形态(SUPABASE_DB)租约在库中,调用方不得用本 + # 命令的输出断言死活(保守 unknown)。 + lock = Path(argv[2]) + try: + line = lock.read_text(encoding="utf-8").strip().splitlines()[0] + except (OSError, IndexError): + return 1 + parts = line.split("|") + # 回退链对齐权威 _lease_sw_fresh(factory-lease.sh:118): + # 第5字段缺 → FACTORY_LEASE_SECS env → 900 + fallback = os.environ.get("FACTORY_LEASE_SECS", "900") + fallback = fallback if fallback.isdigit() else "900" + secs = parts[4] if len(parts) > 4 and parts[4].isdigit() else fallback + try: + mtime = lock.stat().st_mtime + except OSError: + return 1 + return 0 if mtime + int(secs) > time.time() else 1 if cmd == "sanitize": # sanitize ... —— 评论出口标记中和:原地写回(无变化则跳过, # 幂等)。issue_comment 发送前必经;详见 neutralize_marker @@ -1015,8 +1507,11 @@ def pct(xs, q): print(f"{node:10s} n={len(xs):2d} p50={pct(xs,0.5):5d}s p95={p95:5d}s 预算={cur} 建议≤{suggest}m") return 0 if cmd == "suites": + # NUL 分隔(PR #116 CodeRabbit):套件名可含空格(skills/foo bar/...), + # 换行 + shell for 词拆分($(...) 去换行按 IFS 切)会拆碎名、静默跳过 + # 证据段;消费端(fix-issue.sh/validate-pr.sh)用 read -d '' 逐条保真。 for s in evidence_suites(argv[2:]): - print(s) + sys.stdout.write(s + "\0") return 0 if cmd == "final-gate": # final-gate —— 确定性测试门命令(ADR-009 唯一取值口;fix-issue.sh @@ -1028,6 +1523,25 @@ def pct(xs, q): if (v := docstring_gate_cmd()) is not None: print(v) return 0 + if cmd == "parallel-gate": + # parallel-gate [--failed-tags ] —— 并行测试门(ADR-016 段 + # fan-out + 段内并行档;parallel_gate 可选键缺失 → exit 2 + # fail-closed 不产出裁决;--failed-tags 供宿主脚本聚合失败段 + # tag、追加串行段失败后一次裁决,无失败=空文件) + ftp: str | None = None + rest = argv[2:] + j = 0 + while j < len(rest): + if rest[j] == "--failed-tags": + if j + 1 >= len(rest): + print("parallel-gate: --failed-tags 缺路径参数", file=sys.stderr) + return 2 + ftp = rest[j + 1] + j += 2 + else: + print(f"parallel-gate: 未知参数 {rest[j]!r}", file=sys.stderr) + return 2 + return run_parallel_gate(failed_tags_path=ftp) if cmd == "local-str": # local-str —— 单字符串键输出(feedback-upstream 上游指针等;ADR-009) print(_local_str(argv[2])) diff --git a/.factory/feedback-upstream.sh b/.factory/feedback-upstream.sh index f6e5fbd..f10f7be 100755 --- a/.factory/feedback-upstream.sh +++ b/.factory/feedback-upstream.sh @@ -15,6 +15,16 @@ set -euo pipefail REPO="$(git rev-parse --show-toplevel 2>/dev/null)" || { echo "不在仓库内" >&2; exit 2; } FACTORY="$REPO/.factory" +# 账本读写锚定主树(同 fix-issue.sh 锁锚定语义;回退判空而非拼接兜底: +# 空输出拼 "/.factory" 是非空死路径、回退永不触发——源仓#73 审查 F2): +# REPO=show-toplevel 在链 worktree 中解析为链树,账本随链分支删除即丢 → +# collect_pending 回潮重复开 PR(源仓#73)。锚定求值失败退回 $FACTORY。 +MAIN_FACTORY="$(git rev-parse --path-format=absolute --git-common-dir 2>/dev/null \ + | sed 's#/\.git$##' || true)" +MAIN_FACTORY="${MAIN_FACTORY:+$MAIN_FACTORY/.factory}" +MAIN_FACTORY="${MAIN_FACTORY:-$FACTORY}" +LEDGER="${MAIN_FACTORY}/feedback-log.jsonl" +MAIN_WT="${MAIN_FACTORY%/.factory}" # ADR-009 引擎收口:omp_node 定义在 factory-lib.sh(本脚本只消费 omp_node; # issue_* 出口函数不用——ISSUE 未定义无碍,函数体不在此触发) source "${FACTORY}/factory-lib.sh" @@ -36,12 +46,12 @@ say() { printf '%s\n' "$*"; } die() { printf '✗ %s\n' "$*" >&2; exit 1; } # --- 1. 待反哺候选(trailer ∨ bootstrap,− 账本;旧→新) --- -PENDING="$(python3 "$FACTORY/feedback.py" pending)" || die "候选收集失败" +PENDING="$(python3 "$FACTORY/feedback.py" pending --ledger "$LEDGER")" || die "候选收集失败" # --- 1.5 疑似已随演化反哺(SHA 语义缺口):内容经 cherry-pick+适配演化 # 反哺、SHA 未直樱桃的提交会永久 pending(源仓#66 实证,曾靠人工识别 # 孪生补录)。确定性文件集覆盖判定 → 跳过 + 亮清单,人工确认后 # feedback.py record 补录清账(不自动入账:覆盖是强信号非等价证明) -SUPERSEDED="$(python3 "$FACTORY/feedback.py" superseded)" || die "superseded 收集失败" +SUPERSEDED="$(python3 "$FACTORY/feedback.py" superseded --ledger "$LEDGER")" || die "superseded 收集失败" if [ -n "$SUPERSEDED" ]; then say "疑似已随演化反哺 $(printf '%s\n' "$SUPERSEDED" | wc -l | tr -d ' ') 条(跳过;人工确认后 feedback.py record 补录):" printf '%s\n' "$SUPERSEDED" | sed 's/^/ /' @@ -127,7 +137,7 @@ say "上游 worktree: $WT 分支: $BRANCH (基点 $FREMOTE/main@${BASE:0:9})" # --- 3.5 漂移报告(上游独有/两侧分歧,仅报告不动作;对 worktree 检出内容, # 上游 bare 无工作树,2026-08-22 前的磁盘直比已不可行) --- -python3 "$FACTORY/feedback.py" report "$WT" +python3 "$FACTORY/feedback.py" report "$WT" --ledger "$LEDGER" [ "$DRY" = 1 ] && { say "[dry-run] 到此为止,未做任何变更"; exit 0; } # 跨仓对象:上游对象库没有本仓提交,cherry-pick 前临时挂源 remote 拉取 @@ -146,7 +156,7 @@ fi # --- 3.6 依赖闭包(fail-closed):候选脚本引用的 .factory 资产必须 # 上游已有 ∨ 候选随行;防 PR #18 只带主脚本、配套件断链复演 --- -python3 "$FACTORY/feedback.py" closure "$WT" +python3 "$FACTORY/feedback.py" closure "$WT" --ledger "$LEDGER" # --- 4. cherry-pick:clean 保真,conflicted 交适配节点 --- CONFLICTED=() @@ -165,7 +175,7 @@ done <<< "$PENDING" # patches/.patch + manifest.json 生成在 feedback.py adapt-prep # (2026-08-28 自此处 heredoc 下沉:git 子进程编排归 Python,铁律 4; # 亦是 killpg 门[只扫 *.py]与 pipe 门[只扫 *.sh]双盲缝隙的收口) -python3 "$FACTORY/feedback.py" adapt-prep "$FB_DIR" "$PENDING" \ +python3 "$FACTORY/feedback.py" adapt-prep "$FB_DIR" "$PENDING" --ledger "$LEDGER" \ ${CONFLICTED[@]+"${CONFLICTED[@]}"} PROMPT="$(cat "$FACTORY/prompts/feedback-adapt.md") @@ -253,8 +263,10 @@ PR_BODY="$FB_DIR/pr-body.md" echo echo "适配说明: 见 ARTIFACT;上游门禁 gauntlet 全层绿。" } > "$PR_BODY" +# base 显式(PR #116):上游 fetch/补基硬编码 main,不猜 gh 默认分支 PR_URL="$(FACTORY_HOSTING=github python3 "${REPO}/.factory/hosting.py" pr create \ --repo "$UPSTREAM_REPO" --head "$BRANCH" \ + --base main \ --title "factory: 反哺 ${SELF_ID} 工厂改进(${N_TOTAL} commits)" \ --body-file "$PR_BODY" \ | python3 -c 'import json,sys; print(json.load(sys.stdin).get("url") or "")')" \ @@ -268,12 +280,25 @@ say "✓ 上游 PR: $PR_URL" "${GITUP[@]}" branch -qD "$BRANCH" >/dev/null 2>&1 \ || say "⚠ 本地分支 $BRANCH 删除失败(远端 PR 不受影响,可人工清理)" -# --- 9. 账本回写(本仓 .factory/feedback-log.jsonl;提交但不推送) --- +# --- 9. 账本回写(主树 .factory/feedback-log.jsonl;提交但不推送) --- +# 提交守卫(fail-safe:写盘必成,提交可延):主树非 main(账本不随特性 +# 分支走,同回潮根因)或派发器锁被持(避开与链窗口竞争 main 前移)时不 +# 提交——账本已在盘,随下次人工提交;pathspec 形式防夹带主树其他变更。 PR_NUM="${PR_URL##*/}" ARGS=(record "$PR_URL") while IFS=$'\t' read -r sha subject; do ARGS+=("$sha:$subject"); done <<< "$PENDING" -python3 "$FACTORY/feedback.py" "${ARGS[@]}" -git -C "$REPO" add .factory/feedback-log.jsonl -git -C "$REPO" commit -q -m "chore(factory): 反哺账本 → ${UPSTREAM_REPO}#${PR_NUM}" -say "✓ 账本已提交(未推送,随下次人工推送): .factory/feedback-log.jsonl" +python3 "$FACTORY/feedback.py" "${ARGS[@]}" --ledger "$LEDGER" +if [ "$(git -C "$MAIN_WT" symbolic-ref --short HEAD 2>/dev/null)" = "main" ] \ + && [ ! -e "${MAIN_FACTORY}/locks/dispatcher" ]; then + # -f:账本语义是跨仓跟踪资产(提交进 main 才不随分支丢),per-repo + # .gitignore 的「运行时产物」忽略条目不得拦下提交——裸 add 对 ignored + # 路径退出 1,set -e 下 PR 已开后崩(源仓#73 审查 F1,本仓 .gitignore + # 即忽略该文件) + git -C "$MAIN_WT" add -f .factory/feedback-log.jsonl + git -C "$MAIN_WT" commit -q -m "chore(factory): 反哺账本 → ${UPSTREAM_REPO}#${PR_NUM}" \ + -- .factory/feedback-log.jsonl + say "✓ 账本已提交(未推送,随下次人工推送): .factory/feedback-log.jsonl" +else + say "⚠ 账本已落盘未提交(主树非 main 或派发器锁被持): ${LEDGER}(请人工提交)" +fi say "完成: ${PR_URL}(人工 review & merge)" diff --git a/.factory/feedback.py b/.factory/feedback.py index 5c422a1..4e805e9 100755 --- a/.factory/feedback.py +++ b/.factory/feedback.py @@ -443,7 +443,7 @@ def _cmd_record(upstream_pr, args, ledger_path): def _usage_exit(): """未知/缺失子命令 → 用法说明到 stderr,退出码 2。""" - print("用法: feedback.py pending|superseded|status|closure |" + print("用法: feedback.py [--ledger ] pending|superseded|status|closure |" "report |adapt-prep ...|" "record :...", file=sys.stderr) @@ -451,9 +451,17 @@ def _usage_exit(): def main(): + # --ledger 账本显式路径(编排器传主树锚定值):默认值跟随 + # __file__,链 worktree 中被执行的是链树副本——账本随链分支删除即丢, + # collect_pending 回潮重复开 PR(源仓#73)。先剥旗标再分派, + # 各子命令的 sys.argv 位置读法不受旗标位置影响。 + ledger_path = pathlib.Path(__file__).parent / "feedback-log.jsonl" + if "--ledger" in sys.argv: + i = sys.argv.index("--ledger") + ledger_path = pathlib.Path(sys.argv[i + 1]) + del sys.argv[i:i + 2] cmd = sys.argv[1] if len(sys.argv) > 1 else "" here = pathlib.Path(__file__).parent - ledger_path = here / "feedback-log.jsonl" commits = _gather_commits() commits, ledger, pending = _build_pending(commits, ledger_path) diff --git a/.factory/fix-issue.sh b/.factory/fix-issue.sh index 96215bb..2b49549 100755 --- a/.factory/fix-issue.sh +++ b/.factory/fix-issue.sh @@ -26,7 +26,15 @@ HOST="python3 ${REPO}/.factory/hosting.py" # 托管平台抽象(ADR-008) DIR="${REPO}/.factory/artifacts/issue-${ISSUE}" BRANCH="factory/issue-${ISSUE}" WT="${REPO}/.factory/worktrees/issue-${ISSUE}" # 链独立 worktree(多驱动隔离) -BASE_BRANCH="${FACTORY_BASE_BRANCH:-main}" # 两级回退:env → main(PR #61 Sourcery 意图;hosting 形态平台配置走 env,forge.json 中间级随 forge 退役) +# 基线分支两级解析(issue #133):env 显式 → origin 实际默认分支;两者皆无 +# 即 fail-closed 拒猜——固定回退 main 会绕过 hosting.py Codeup 拒猜不变量 +# (targetBranch 因仓而异,master 仓静默落错基线)。PR #61 的 env 级保留。 +# 分步而非 $(...) 内嵌:set -e 下替换失败会带 git rc 直接终止,|| 兜底接不住。 +BASE_BRANCH="${FACTORY_BASE_BRANCH:-}" +if [ -z "${BASE_BRANCH}" ]; then + BASE_BRANCH="$(git -C "${REPO}" symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed 's|^origin/||' || true)" +fi +[ -n "${BASE_BRANCH}" ] || { echo "[base] FACTORY_BASE_BRANCH 未配置且 origin/HEAD 默认分支不可读:拒猜基线,fail-closed 终止(issue #133)" >&2; exit 2; } # 链副作用共享库:issue 评论唯一出口 + 拒绝单一动作(契约见库头注释) source "${REPO}/.factory/factory-lib.sh" node_timeout() { python3 "${REPO}/.factory/factory_lib.py" timeout "$1"; } # 分级预算:裁决器5m/工作节点15m/implement 30m @@ -145,16 +153,37 @@ $(python3 "${REPO}/.factory/factory_lib.py" repo-vars) 任务参数: - ISSUE_DIR: ${DIR} - 仓库根: ${WT}(链独立 worktree,勿越界改主工作区) -- issue 编号: ${ISSUE}" +- issue 编号: ${ISSUE} +- 节点预算(硬击杀线,编排器 --max-time): $(node_timeout "${name}")" t0=$(date +%s) + # B1: 节点起点标记(产物 mtime 参照)。创建失败也是节点死亡——先落 node-fail + # 再退(Sourcery 复审);DIR 整体不可写时 printf 同败,由 trap 的 chain-abort 兜底 + if ! touch "${DIR}/.${name}-t0" 2>/dev/null; then + printf 'node-fail %s round=%s node=%s reason=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${ROUND:-0}" "${name}" "marker-create" >> "${DIR}/chain-history" 2>/dev/null || true + return 1 + fi if ! omp_node "${WT}" "${DIR}/${name}.log" "$(node_timeout "${name}")" -- "${prompt}"; then _node_metric "${name}" "${t0}" "fail" >> "${DIR}/node-metrics.jsonl" + printf 'node-fail %s round=%s node=%s reason=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${ROUND:-0}" "${name}" "omp-exit" >> "${DIR}/chain-history" 2>/dev/null || true echo " 节点 ${name} 失败(详见 ${DIR}/${name}.log)" >&2; return 1 fi t1=$(date +%s) - if ! grep -q "ARTIFACT:" "${DIR}/${name}.log"; then + # B1(2026-09-05):产物判定由"stdout 含 ARTIFACT: 字面行"改为"prompts 声明的 + # 固定产物文件存在且 mtime ≥ 节点起点标记"。容忍 agent 末行格式漂移(实证:计划 + # 完整产出但末行写"产物:/path/plan.json"被 grep 误杀);产物缺失/未更新仍 + # fail-closed(上一轮残留物 mtime < 起点标记,不误判通过)。无声明节点回退 + # stdout ARTIFACT: 行检查。 + artifact="$(grep -m1 -oE 'ARTIFACT: \$ISSUE_DIR/[^` ]+' "${REPO}/.factory/prompts/${name}.md" | sed 's#.*/##' || true)" + if [ -z "${artifact}" ]; then + grep -q "ARTIFACT:" "${DIR}/${name}.log" || { + _node_metric "${name}" "${t0}" "no-artifact" >> "${DIR}/node-metrics.jsonl" + printf 'node-fail %s round=%s node=%s reason=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${ROUND:-0}" "${name}" "no-artifact" >> "${DIR}/chain-history" 2>/dev/null || true + echo " 节点 ${name} 未声明产物(缺 ARTIFACT 行)" >&2; return 1 + } + elif [ ! -f "${DIR}/${artifact}" ] || [ "${DIR}/${artifact}" -ot "${DIR}/.${name}-t0" ]; then _node_metric "${name}" "${t0}" "no-artifact" >> "${DIR}/node-metrics.jsonl" - echo " 节点 ${name} 未声明产物(缺 ARTIFACT 行)" >&2; return 1 + printf 'node-fail %s round=%s node=%s reason=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${ROUND:-0}" "${name}" "stale-artifact" >> "${DIR}/chain-history" 2>/dev/null || true + echo " 节点 ${name} 产物缺失/未更新(${artifact} 不存在或早于节点起点标记)" >&2; return 1 fi _node_metric "${name}" "${t0}" "ok" >> "${DIR}/node-metrics.jsonl" printf ' 耗时 %ss\n' "$(( t1 - t0 ))" @@ -207,11 +236,13 @@ ${cmts} if ! omp_node "${REPO}" "${DIR}/triage.log" "$(node_timeout triage)" --no-tools \ --config "${REPO}/.factory/omp-isolated.yml" -- "${prompt}"; then _node_metric triage "${t0}" "fail" >> "${DIR}/node-metrics.jsonl" + printf 'node-fail %s round=%s node=%s reason=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${ROUND:-0}" "triage" "omp-exit" >> "${DIR}/chain-history" 2>/dev/null || true echo " triage 节点失败(详见 ${DIR}/triage.log)" >&2; return 1 fi _node_metric triage "${t0}" "ok" >> "${DIR}/node-metrics.jsonl" python3 "${REPO}/.factory/factory_lib.py" parse "${DIR}/triage.log" "${DIR}/triage.json" accept,reject \ - || { echo " triage 输出无法解析为 JSON(见 factory_lib.parse_agent_json)" >&2; return 1; } + || { printf 'node-fail %s round=%s node=%s reason=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${ROUND:-0}" "triage" "parse" >> "${DIR}/chain-history" 2>/dev/null || true; \ + echo " triage 输出无法解析为 JSON(见 factory_lib.parse_agent_json)" >&2; return 1; } } run_holdout() { # 物理隔离验证器:--no-tools + 输入全部内联,agent 无任何工具 @@ -237,11 +268,13 @@ ${out} if ! omp_node "${REPO}" "${DIR}/holdout.log" "$(node_timeout holdout)" --no-tools \ --config "${REPO}/.factory/omp-isolated.yml" -- "${prompt}"; then _node_metric holdout "${t0}" "fail" >> "${DIR}/node-metrics.jsonl" + printf 'node-fail %s round=%s node=%s reason=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${ROUND:-0}" "holdout" "omp-exit" >> "${DIR}/chain-history" 2>/dev/null || true echo " holdout 节点失败(详见 ${DIR}/holdout.log)" >&2; return 1 fi _node_metric holdout "${t0}" "ok" >> "${DIR}/node-metrics.jsonl" python3 "${REPO}/.factory/factory_lib.py" parse "${DIR}/holdout.log" "${DIR}/holdout.json" PASS,FAIL \ - || { echo " holdout 输出无法解析为 JSON(见 factory_lib.parse_agent_json)" >&2; return 1; } + || { printf 'node-fail %s round=%s node=%s reason=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${ROUND:-0}" "holdout" "parse" >> "${DIR}/chain-history" 2>/dev/null || true; \ + echo " holdout 输出无法解析为 JSON(见 factory_lib.parse_agent_json)" >&2; return 1; } } @@ -279,6 +312,14 @@ if [ "${DRY}" = 0 ]; then ROUND=$(( $(grep -c 'chain-start' "${DIR}/chain-history" 2>/dev/null || echo 0) + 1 )) # 清上一轮裁决产物:防陈旧 triage.json/holdout.json 污染本轮判定与台账分类 rm -f "${DIR}/triage.json" "${DIR}/holdout.json" + # 归档上一轮过程产物 → *-pre-r${ROUND}.*(A:跨轮回流,下轮 prime/plan 的 + # 增量输入。上轮死在更早节点时文件属更早轮——pre-rN 语义 =「本轮开始时的 + # 内容」仍为真;B1 判定不受影响:归档即移走,旧文件不可能冒充本轮产物) + for _af in prime.md plan.json implement.md review.md; do + if [ -f "${DIR}/${_af}" ]; then + mv -f "${DIR}/${_af}" "${DIR}/${_af%.*}-pre-r${ROUND}.${_af##*.}" || true + fi + done CHAIN_T0=$(date +%s) echo "chain-start $(date -u +%Y-%m-%dT%H:%M:%SZ) round=${ROUND}" >> "${DIR}/chain-history" # 台账(EXIT 时写):{ts, issue, round, type, exit, secs}——重派率/首轮通过率 jq 一行可算。 @@ -307,12 +348,29 @@ if [ "${DRY}" = 0 ]; then fi fi mkdir -p "${REPO}/.factory/locks" - # issue 值加引号:Codeup 编号是字符串(KFPT-18),%s 裸出产出 - # {"issue": KFPT-18} 非法 JSON,jq 消费方崩(下游仓实测) + # issue 值加引号:Codeup 编号是字符串(T-18),%s 裸出产出 + # {"issue": T-18} 非法 JSON,jq 消费方崩(下游仓实测) printf '{"ts": "%s", "issue": "%s", "round": %s, "type": "%s", "exit": %s, "secs": %s}\n' \ "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${ISSUE}" "${ROUND}" "${kind}" "${rc}" \ "$(( $(date +%s) - CHAIN_T0 ))" >> "${REPO}/.factory/locks/ledger.jsonl" } + # #14 补遗:抢救未提交 WIP——rev-list=0 时 trap 的 salvage push 无物可推 + # (#165 r1-r3 实证:implement 未提交即死,WIP 随 worktree 强删湮灭)。 + # intent-to-add 让 untracked 入 diff;binary diff 快照到 ISSUE_DIR 供下轮 + # implement 作证据输入(只读参考,不自动续作);空 diff 不留噪声文件。 + # best-effort:恒 return 0,任何失败不中断清理链(#23 纪律)。 + salvage_wip() { + local rc="$1" patch + if [ "${rc}" -eq 0 ] || [ ! -d "${WT}" ]; then return 0; fi + patch="${DIR}/salvage-r${ROUND}.patch" + git -C "${WT}" add -N . >/dev/null 2>&1 || true + if ! git -C "${WT}" diff HEAD --binary > "${patch}" 2>/dev/null || [ ! -s "${patch}" ]; then + rm -f "${patch}" 2>/dev/null || true + else + echo " [salvage] 未提交 WIP 快照 → ${patch}" + fi + return 0 + } # 失败清理 + 台账 + 产出抢救 + worktree 回收: # - set +e 首动作(#23):trap 是状态机复位的唯一保障,内部任一命令 # 非零不得中止清理链——trap 失败模式收敛为"多打日志"而非"静默中断" @@ -322,6 +380,10 @@ if [ "${DRY}" = 0 ]; then # 下轮 -B 重置回基线孤儿化,implement 成果湮灭)。--force:下轮 # 从基线重跑后非 FF,远端镜像语义 = 最新一轮产出;推送失败仅告警 # 不阻断后续清理(网络故障不应二次放大为状态残留) + # - rc≠0 追加 chain-abort 行到 chain-history(A:下轮 prime/plan 的死因 + # 输入);未提交 WIP 由 salvage_wip 快照到 ISSUE_DIR,均须先于 worktree + # 强删。SIGKILL 下 trap 整体失守,此通道同殁——dispatcher 侧检测为 + # 残余缺口,另行立项 # - 非零退出移除流转标签回零标签态(可重试);无论成败都记账; # worktree 无论成败一并回收 # - 清理顺序(PR#34 审查修复):标签清理在 lease_cleanup **之前**——清标 @@ -330,7 +392,7 @@ if [ "${DRY}" = 0 ]; then # 或作为人工债务可见。lease_cleanup 收心跳+放租约,放清理链末尾。 # D1: 本 trap 覆盖早期放锁 trap,故自带锁释放;派发链 MANUAL_LOCK=0 不动锁 # shellcheck disable=SC2154 # rc 于本 trap 行内由 rc=$? 赋值,shellcheck 不解析 trap 字符串 - trap 'rc=$?; set +e; write_ledger "${rc}"; if [ "${rc}" -ne 0 ] && [ "$(git -C "${REPO}" rev-list --count ${BASE_BRANCH}.."${BRANCH}" 2>/dev/null || echo 0)" -gt 0 ]; then git -C "${REPO}" push --force --no-verify origin "${BRANCH}" >/dev/null 2>&1 && echo " [salvage] 失败链产出已推送 origin/${BRANCH}" || echo " [warn] 失败链产出推送失败,产出仅在本地分支 ${BRANCH}" >&2; fi; git -C "${REPO}" worktree remove --force "${WT}" >/dev/null 2>&1 || true; [ "${rc}" -ne 0 ] && { issue_label remove factory:triaging; issue_label remove factory:accepted; issue_label remove factory:in-progress; }; lease_cleanup; [ "${MANUAL_LOCK}" = 1 ] && rm -rf "${LOCKDIR:-}" 2>/dev/null' EXIT + trap 'rc=$?; set +e; write_ledger "${rc}"; if [ "${rc}" -ne 0 ]; then printf "chain-abort %s round=%s exit=%s\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${ROUND}" "${rc}" >> "${DIR}/chain-history" 2>/dev/null || true; fi; if [ "${rc}" -ne 0 ] && [ "$(git -C "${REPO}" rev-list --count ${BASE_BRANCH}.."${BRANCH}" 2>/dev/null || echo 0)" -gt 0 ]; then git -C "${REPO}" push --force --no-verify origin "${BRANCH}" >/dev/null 2>&1 && echo " [salvage] 失败链产出已推送 origin/${BRANCH}" || echo " [warn] 失败链产出推送失败,产出仅在本地分支 ${BRANCH}" >&2; fi; salvage_wip "${rc}"; git -C "${REPO}" worktree remove --force "${WT}" >/dev/null 2>&1 || true; [ "${rc}" -ne 0 ] && { issue_label remove factory:triaging; issue_label remove factory:accepted; issue_label remove factory:in-progress; }; lease_cleanup; [ "${MANUAL_LOCK}" = 1 ] && rm -rf "${LOCKDIR:-}" 2>/dev/null' EXIT else echo "[dry-run] hosting issue view #${ISSUE} → ${DIR}/issue.json" echo "[dry-run] label: +factory:triaging(裁决后 → accepted|rejected)" @@ -423,7 +485,26 @@ if [ "${DRY}" = 0 ]; then while IFS= read -r -d '' f; do CHANGED+=("${f}"); done \ < <(git -C "${WT}" diff --name-only -z "${BASE_BRANCH}...${BRANCH}" 2>/dev/null \ || git -C "${WT}" diff --name-only -z HEAD~1) - python3 "${REPO}/.factory/guard.py" --files ${CHANGED[@]+"${CHANGED[@]}"} + + # 权威改动面判定(#170 评审修正):CHANGED 空还可能源于 diff 命令静默 + # 失败(三点式 2>/dev/null 与 HEAD~1 兜底双败),不可与真零改动混同。 + # log 失败 = 改动面不可判定 → fail-closed;guard 跳过与 §7.5 收官共用 + # 本值,不二次取样。 + COMMITS="$(git -C "${WT}" log --oneline "${BASE_BRANCH}..${BRANCH}")" \ + || { echo "[zero-diff] git log ${BASE_BRANCH}..${BRANCH} 失败:改动面不可判定,fail-closed 终止" >&2; exit 1; } + if [ "${#CHANGED[@]}" -gt 0 ]; then + python3 "${REPO}/.factory/guard.py" --files ${CHANGED[@]+"${CHANGED[@]}"} + elif [ -n "${COMMITS}" ]; then + # 有提交却收不到 diff:diff 收集异常,保持既有 fail-closed 不变量 + # (旧路径死于 guard exit 2,此处显式退出并说明原因)。 + echo "[guard] CHANGED 空但 ${BASE_BRANCH}..${BRANCH} 有提交:diff 收集异常,fail-closed 终止" >&2 + exit 1 + else + # 零改动轮(issue #165 round-4 实证):bash 3.2 空数组经 ${CHANGED[@]+…} + # 展开消失,`--files` 零路径 = 用法错误 → guard fail-closed exit 2 误杀 + # 全绿链。零改动 = 无周界面可查,跳过 guard。 + echo "[guard] 零改动轮:无周界面可查,guard 跳过" + fi # ADR-009 门命令数据化:final_gate_cmd 来自 factory-local.json(fail-closed: # factory_lib 加载失败此处即非零终止);read -ra 拆词为 argv 数组执行。 GATE_CMD="$(python3 "${REPO}/.factory/factory_lib.py" final-gate)" @@ -443,12 +524,14 @@ if [ "${DRY}" = 0 ]; then fi # 证据段:触及的测试套件以 -v 重跑附于末尾——holdout 不许推测, # 需要可引用的测试名/参数化用例名(-q 点号无法建立诉求对应关系) - for suite in $(python3 "${REPO}/.factory/factory_lib.py" suites ${CHANGED[@]+"${CHANGED[@]}"}); do + # NUL 分隔消费(PR #116):套件名可含空格(skills/foo bar/...),for 词拆分 + # 会拆碎名致证据段静默跳过;suites 产出 NUL → read -d '' 逐条保真 + while IFS= read -r -d '' suite; do [ -d "${WT}/${suite}" ] || continue echo "" >> "${DIR}/tests-output.txt" echo "── 证据段(verbose): ${suite}" >> "${DIR}/tests-output.txt" (cd "${WT}/${suite}" && python3 -m pytest -o addopts="" -v) >> "${DIR}/tests-output.txt" 2>&1 || true - done + done < <(python3 "${REPO}/.factory/factory_lib.py" suites ${CHANGED[@]+"${CHANGED[@]}"}) else echo "[dry-run] guard.py --files + 测试门(final_gate_cmd) → ${DIR}/tests-output.txt(脚本生成)" echo "[dry-run] docstring 门(docstring_gate_cmd,未配置则跳过) → ${DIR}/docstring-output.txt" @@ -475,6 +558,30 @@ PYA || { echo "holdout=FAIL,链终止(不建 PR;evidence 已存 chain-history)"; exit 1; } fi +# --- 7.5 零改动轮收官:无 diff 即无 PR(issue #165 round-4 实证) --- +# 纯验证轮的产出是工件与 issue 评论,不是代码。若继续走 §8,push 会把 +# BASE_BRANCH 领先 origin 的提交整段带进 PR diff(如未推的 main 提交), +# 污染人类评审面。holdout PASS 后零提交 → exit 0 收官:rc=0 不触发 +# trap 清标,accepted/in-progress 留守,派发器按设计跳过滞留 +# in-progress 的 issue(见文件头 S2 注释),验证轮不再被重派。 +if [ "${DRY}" = 0 ] && [ -z "${COMMITS}" ]; then + echo "[zero-diff] 本轮零仓内改动(纯验证轮,holdout 已 PASS):不 push、不建 PR,exit 0 收官。" + # 终态可见性(2026-09-15 issue #165 实证):留守 in-progress 在 GitHub 侧 + # 无任何痕迹,人类看不见链已收官。终态评论补痕;幂等键防诈尸残窗重发。 + # 评论失败仅告警——链已 PASS,可见性是透明度而非门。 + cat > "${DIR}/zero-diff-receipt.md" <&2 + exit 0 +fi # --- 8. 开 PR(S1 到此为止:merge 由人类决定,铁律 5) --- if [ "${DRY}" = 0 ]; then # --no-verify:新分支首推无 @{push},lefthook {push_files} 模板必然 exit 128; diff --git a/.factory/hosting.py b/.factory/hosting.py index 8eb528e..cab30b9 100755 --- a/.factory/hosting.py +++ b/.factory/hosting.py @@ -41,6 +41,8 @@ (c) 无标签事件时间线 → label history 不可表达。fail-closed exit 2。 CLI(bash 侧调用面;py 侧 import current_adapter): + --repo R:仓库本地路径(目录形态,平台按该目录 remote 自解析);slug + 形态(owner/repo)须显式 FACTORY_HOSTING 锚定平台,否则 fail-closed(#134) hosting.py auth ok hosting.py label ensure hosting.py label history @@ -60,12 +62,17 @@ hosting.py pr merge

--method merge|squash|rebase """ import argparse +import contextlib import datetime +import hashlib import json import os import re +import shutil import subprocess import sys +import tempfile +import time import urllib.error import urllib.parse import urllib.request @@ -81,14 +88,103 @@ def __init__(self, msg, code=1): self.code = code +# --------------------------------------------------------------------------- +# Codeup add「预检+POST」的跨进程互斥(PR #116 CodeRabbit):锁原语与工厂 +# 调度锁同语义(mkdir 原子 + pid 活性判死接管),但锚点不同——hosting CLI +# 常跨 cwd/worktree 调用,无统一仓内锚点,锁根取机器级临时目录 +# (FACTORY_LOCK_DIR 显式覆盖:测试隔离/多租户)。互斥对象=同仓 +# (adapter.repo 视角)同 PR 同名 add 的双进程写;粒度偏粗无害 +# (串行化而非错误化)。 +# --------------------------------------------------------------------------- + +_LABEL_LOCK_TIMEOUT = 5.0 # 等锁最久;超时 fail-closed(code=1)拒放行双写 +_LABEL_LOCK_STALE_GRACE = 2.0 # 无 pid 残锁的年龄宽限(建锁→写 pid 的崩溃窗) + + +def _label_lock_root() -> str: + """add 互斥锁根:FACTORY_LOCK_DIR 显式(测试隔离)→ 系统临时目录 + (跨进程共享——传输层锁须机器级可见;与工厂调度锁的主树锚点两域不重叠)。""" + return os.environ.get("FACTORY_LOCK_DIR") or os.path.join( + tempfile.gettempdir(), "factory-hosting-locks") + + +def _pid_alive(pid: int) -> bool: + """pid 活性(kill(pid, 0) 语义):EPERM=进程存在按活;内容异常 + (垃圾/超 C int)不可判定 → 保守按活(等锁超时 fail-closed,不误清)。""" + try: + os.kill(pid, 0) + return True + except ProcessLookupError: + return False + except (PermissionError, ValueError, OverflowError): + return True + # 等锁超时 fail-closed(code=1)也不误清可能活着的持锁主 + + +def _lock_owner(lock_dir: str) -> str: + """锁内 pid 文件内容;无/读失败 → ""(建锁与写 pid 之间的崩溃窗形态)。""" + try: + with open(os.path.join(lock_dir, "pid"), encoding="ascii") as fh: + return fh.read().strip() + except OSError: + return "" + + +def _lock_stale(lock_dir: str, owner: str) -> bool: + """死者残锁判定:有 pid 且进程已死 → 可接管;pid 内容异常按活等 + (宁可等超时 fail-closed,不清活主);无 pid → 目录年龄超宽限判死 + (崩溃残锁可清;活主写 pid 在微秒级,不可能越过宽限)。""" + if owner: + try: + pid = int(owner) + except ValueError: + return False + return not _pid_alive(pid) + try: + return time.time() - os.stat(lock_dir).st_mtime > _LABEL_LOCK_STALE_GRACE + except OSError: + return False # 目录已被他人清掉:外层重试 mkdir 收敛 + + +def _label_lock_dir(repo: str, p: int, name: str) -> str: + """同名 add 的互斥锁目录:sha1(repo|p|name) 折叠——repo/name 来自外部 + 输入,目录名不得含不可信路径字符。""" + h = hashlib.sha1(f"{repo}|{p}|{name}".encode()).hexdigest()[:16] + return os.path.join(_label_lock_root(), h) + + # --------------------------------------------------------------------------- # GitHub slug 解析(自 factory_lib.py 迁入——平台选择逻辑归本层, # 核心脚本不再各自扫 remote) # --------------------------------------------------------------------------- -_SLUG_RE = re.compile( - r"^(?:[A-Za-z0-9_.-]+@)?(?:github\.com|ssh\.github\.com|github-wop-bot)(?::\d+)?[/:]" - r"(?P[^/]+/[^/]+?)(?:\.git)?/?$") +# 主机白名单基础面 = github 双域名;bot ssh Host 别名等机器特定主机走 +# FACTORY_SLUG_EXTRA_HOSTS(逗号分隔)注入(ADR-012:公开仓零内部标识, +# 配置数据化 env 两级回退——与 PR #61 平台配置同模式)。token 严格校验 +# [A-Za-z0-9_.-]+(进 regex 交替支,防注入),坏配置 fail-closed 抛错 +# 而非静默降级。缓存按 env 值键控:同进程 env 不变零重编译 +_SLUG_HOST_ENV = "FACTORY_SLUG_EXTRA_HOSTS" +_slug_re_cache = (None, None) + + +def _slug_re(): + env = os.environ.get(_SLUG_HOST_ENV, "") + if _slug_re_cache[0] == env: + return _slug_re_cache[1] + hosts = "github\\.com|ssh\\.github\\.com" + if env.strip(): + toks = [t.strip() for t in env.split(",") if t.strip()] + for t in toks: + if not re.fullmatch(r"[A-Za-z0-9_.-]+", t): + raise ValueError( + f"{_SLUG_HOST_ENV} 含非法主机名: {t!r}" + "(仅允许 [A-Za-z0-9_.-])") + hosts += "|" + "|".join(re.escape(t) for t in toks) + cre = re.compile( + rf"^(?:[A-Za-z0-9_.-]+@)?(?:{hosts})(?::\d+)?[/:]" + r"(?P[^/]+/[^/]+?)(?:\.git)?/?$") + globals()["_slug_re_cache"] = (env, cre) + return cre _SLUG_VALID = re.compile(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+") @@ -98,7 +194,7 @@ def extract_slug(urls): owner/repo 白名单双闸。GH_REPO 显式指定不经此函数。""" for u in urls: t = re.sub(r"^(?:ssh|git|https?)://", "", (u or "").strip()) - m = _SLUG_RE.match(t) + m = _slug_re().match(t) if m and _SLUG_VALID.fullmatch(m.group("slug")): return m.group("slug") return "" @@ -169,6 +265,9 @@ def _gh_json(self, args, repo_override=None): def _issue(d): return {"number": d.get("number"), "state": _GH_STATE.get(d.get("state"), "open"), "title": d.get("title") or "", "body": d.get("body") or "", + # updatedAt ISO8601(缺省 None):dispatch-liveness rejected + # 滞留阈值用;非 gh 后端无此字段则该项检测自动跳过 + "updatedAt": d.get("updatedAt"), "labels": [l["name"] for l in d.get("labels") or []], "comments": [{"author": (c.get("author") or {}).get("login") or "", "body": c.get("body") or ""} @@ -193,6 +292,20 @@ def auth_ok(self): return False # gh CLI 不在 PATH(环境缺失,非凭据问题) return r.returncode == 0 + def auth_diagnose(self) -> str: + """auth_ok 失败的留痕诊断:返回 gh auth status 的 stderr(非空时,否则 stdout),并去除首尾空白。 + + 【2026-09-05 02:00 6 仓瞬断事故(内部 SDK 仓)】auth_ok 只回布尔,失败 + stderr 被丢弃 → 事后无法回溯是 keyring/网络/过期哪种。dispatch + preflight 失败路径调用,输出附着 dispatch 日志(同 bare 事故的 + 诊断附着模式)。""" + try: + r = subprocess.run(["gh", "auth", "status"], + capture_output=True, text=True) + except FileNotFoundError: + return "gh CLI 不在 PATH(auth_ok 同因失败)" + return (r.stderr if r.stderr.strip() else r.stdout).strip() + def issue_view(self, n, repo=None): return self._issue(self._gh_json( ["issue", "view", str(n), @@ -204,7 +317,7 @@ def issue_labels(self, n, repo=None): def issue_list(self, state="open", label=None, limit=100, comments=False, repo=None): - fields = "number,state,title,body,labels" + (",comments" if comments else "") + fields = "number,state,title,body,labels,updatedAt" + (",comments" if comments else "") args = ["issue", "list", "--state", state, "--limit", str(limit), "--json", fields] if label: @@ -221,7 +334,18 @@ def issue_set_labels(self, n, add=(), remove=(), repo=None): return True r = self._gh(args, repo) if r.returncode != 0: - raise HostingError(f"issue #{n} 标签设置失败: {r.stderr.strip()[:200]}") + # 假阴性和解(#207 实证:gh 非零但服务端已应用标签—— + # "mutation landed + client reported failure"):失败路径重读 + # 远端标签态,目标已达成即幂等成功,裁决不被传输层误报否决; + # 复核不可用/目标未达成 → 原 fail-closed 错误透传。 + try: + labels = set(self.issue_labels(n, repo)) + except HostingError: + labels = None + if labels is None or not set(add) <= labels or set(remove) & labels: + raise HostingError( + f"issue #{n} 标签设置失败: {r.stderr.strip()[:200]}") + return True return True def issue_comment(self, n, body, marker=None, repo=None): @@ -271,13 +395,29 @@ def pr_set_labels(self, p, add=(), remove=(), repo=None): return True r = self._gh(args, repo) if r.returncode != 0: - raise HostingError(f"pr #{p} 标签设置失败: {r.stderr.strip()[:200]}") + # 假阴性和解:同 issue_set_labels(#207 实证类)——pr view + # 重读标签态复核,目标已达成即幂等成功;复核不可用/未达成 + # → 原 fail-closed 错误透传。 + try: + d = self._gh_json(["pr", "view", str(p), "--json", "labels"], repo) + labels = {l.get("name") for l in d.get("labels") or []} + except HostingError: + labels = None + if labels is None or not set(add) <= labels or set(remove) & labels: + raise HostingError( + f"pr #{p} 标签设置失败: {r.stderr.strip()[:200]}") + return True return True def pr_create(self, head, title, body, label=None, base=None, repo=None): + """创建 PR → 中立 {number,url}。--base 显式必填:默认分支因仓而异 + (gh 猜默认 = 静默落错基线),同 codeup 形态 fail-closed(PR #116)。""" + if not base: + raise HostingError( + "github pr create 需要 --base(默认分支因仓而异,勿猜默认)", + code=2) args = ["pr", "create", "--head", head, "--title", title, "--body", body] - if base: - args += ["--base", base] + args += ["--base", base] if label: args += ["--label", label] r = self._gh(args, repo) @@ -401,7 +541,7 @@ def _space_id(self, what="issue"): "FACTORY_SPACES_CONF", os.path.expanduser("~/.config/factory/codeup-spaces.conf")) ns_map = {} - try: + with contextlib.suppress(OSError): with open(conf, encoding="utf-8") as f: for line in f: line = line.strip() @@ -409,11 +549,9 @@ def _space_id(self, what="issue"): continue k, v = line.split("|", 1) ns_map[k.strip()] = v.strip() - except OSError: - pass org, path = self._remote() if org and path and "/" in path and ns_map: - # namespace = path 首段(org/group/ → group); + # namespace = path 首段(gtsp/open-platform/ → gtsp); # 旧 [1] 取到中间层 open-platform,映射永不命中(php#17) ns = path.split("/")[0] if ns in ns_map: @@ -555,10 +693,8 @@ def _wi_text(raw): """description 双形态({"htmlValue":...} JSON 串 / 裸 HTML)→ 纯文本。""" s = raw or "" if s.lstrip().startswith("{"): - try: + with contextlib.suppress(ValueError): s = json.loads(s).get("htmlValue") or "" - except ValueError: - pass # HTML 注释段原样保留:dedupe marker()与 labels 块 # ()靠注释承载——剥标签会把注释 # 一起剥掉,marker 永不命中(#67 实现期实证)。labels 块的剥离 @@ -589,7 +725,7 @@ def _wi_normalize(self, wi, comments=True): "comments": cs} def _wi_get(self, n): - """双键寻址:serialNumber(KFPT-16)或 24-hex id 均 200(live)。""" + """双键寻址:serialNumber(T-16)或 24-hex id 均 200(live)。""" _, org = self._cfg() r = self._req( "GET", @@ -742,7 +878,7 @@ def issue_create(self, title, body, label=None, repo=None): d = d or {} wid = d.get("id") # 【live 2026-08-26】create 响应只含 24-hex id,无 serialNumber - # (实测项目 KFPT-21);人类可读编号(KFPT-N)须回查 + # (实测项目 T-21);人类可读编号(T-N)须回查 # 详情。回查失败降级 id(view/编辑两种键都认,但人在界面引用 # 序号——宁可多一次 GET) number = d.get("serialNumber") or wid @@ -798,7 +934,7 @@ def _pr_labels(self, p): active = {self._marker_label(m["content"]) for m in markers if not m["resolved"] and m["content"].startswith(_CU_LABEL_ADD)} names = [] - try: + with contextlib.suppress(HostingError): payload = self._req("GET", f"{self._base()}/changeRequests/{p}/labels") items = payload if isinstance(payload, list) else (payload.get("result") or []) for l in items: @@ -808,8 +944,6 @@ def _pr_labels(self, p): if ok and name.startswith(_CU_FACTORY_PREFIX) and name not in active: continue # 工厂类标投影失真:以标记生命周期为准(仅读成功时) names.append(name) - except HostingError: - pass # 类标读失败不阻断详情(标记评论仍可承载) if ok: names += active return sorted({n for n in names if n}) @@ -817,7 +951,7 @@ def _pr_labels(self, p): @staticmethod def _marker_label(content): # 平台开放输入:恰为前缀/前缀+空白时切片为空,splitlines()[0] 抛 - # IndexError(CodeRabbit wop-skills#14);空标记按无标记处理 + # IndexError(CodeRabbit xx-skills#14);空标记按无标记处理 rest = content[len(_CU_LABEL_ADD):].strip() return rest.splitlines()[0].strip() if rest else "" @@ -861,8 +995,10 @@ def pr_list(self, state="open", label=None, limit=100, repo=None): return out[:limit] def _label_markers(self, p, name): - # 同名 label 的未 resolved 标记(add/remove 幂等预检共用); - # 普通循环逐条判别(无重复索引/无海象,规避 Sourcery 两口径分歧) + """同名 label 的未 resolved add 标记(add/remove 幂等预检共用)。 + + 普通循环逐条判别(无重复索引/无海象,规避 Sourcery 两口径分歧)。 + """ out = [] for m in self._marker_comments(p): c = m["content"] @@ -873,9 +1009,11 @@ def _label_markers(self, p, name): return out def pr_set_labels(self, p, add=(), remove=(), repo=None): - # 评论标记模型(#66,承载平台缺口 b):remove = 置 resolved - # (内容保留,轮次计数不减——对齐 GitHub label-add 事件语义); - # add = 发标记评论 + 类标 Link 平台原生补充(两载体并存)。 + """评论标记模型(#66,承载平台缺口 b):remove = 置 resolved + (内容保留,轮次计数不减——对齐 GitHub label-add 事件语义); + add = 标记评论 + 类标 Link 平台原生补充(两载体并存)。 + add 的预检+POST 以跨进程锁串行(PR #116):双进程同时过预检再 + 各 POST = 双未 resolved 标记;锁内重预检命中 → 幂等跳过。""" for name in remove: hits = self._label_markers(p, name) if not hits: @@ -887,15 +1025,18 @@ def pr_set_labels(self, p, add=(), remove=(), repo=None): body={"resolved": True}) for name in add: # 幂等(对齐 remove 分支):已有同名未 resolved 标记则跳过 - # ——重试/双写场景重复 POST 会堆未 resolved 重复标记(php#17) - if hits := self._label_markers(p, name): - print(f"[hosting] add {name}: 已有未 resolved 标记(幂等跳过)", - file=sys.stderr) - continue - self._req("POST", f"{self._base()}/changeRequests/{p}/comments", - body={"comment_type": "GLOBAL_COMMENT", - "content": f"{_CU_LABEL_ADD}{name}", - "resolved": False}) + # ——重试/双写场景重复 POST 会堆未 resolved 重复标记(php#17)。 + # 预检+POST 在跨进程锁内(_add_label_lock):锁外预检=裸竞态 + # 窗口,后到者锁内重预检命中 → 幂等跳过,锁保证恰一次 POST。 + with self._add_label_lock(p, name): + if hits := self._label_markers(p, name): + print(f"[hosting] add {name}: 已有未 resolved 标记(幂等跳过)", + file=sys.stderr) + continue + self._req("POST", f"{self._base()}/changeRequests/{p}/comments", + body={"comment_type": "GLOBAL_COMMENT", + "content": f"{_CU_LABEL_ADD}{name}", + "resolved": False}) # 类标 Link best-effort:不存在(label create 未破案,界面人工路径) # 时降级告警——标记评论已承载状态机语义,链不因平台类标缺失受阻 ids = [] @@ -916,6 +1057,55 @@ def pr_set_labels(self, p, add=(), remove=(), repo=None): file=sys.stderr) return True + @contextlib.contextmanager + def _add_label_lock(self, p, name): + """add「预检+POST」互斥上下文(见模块段注释):mkdir 原子占锁 + + pid 活性判死接管 + 等锁有界;超时 raise HostingError(code=1)——宁可 + 让调用链失败重试(重试以幂等跳过收敛),不放行双写。""" + lock_dir = _label_lock_dir(self.repo, p, name) + deadline = time.monotonic() + _LABEL_LOCK_TIMEOUT + # 锁根常驻:ensure 父目录(多进程并发 makedirs exist_ok 原子无害) + os.makedirs(_label_lock_root(), exist_ok=True) + while True: + try: + os.mkdir(lock_dir) + except FileExistsError as e: + owner = _lock_owner(lock_dir) + if _lock_stale(lock_dir, owner): + # 判死与清除分离(PR #116 审查 P2):rename 领取清除权, + # 他者先接管则 rename 失败、活锁不被动;owner 复核兜底 + # 判死→改名间他者已 mkdir 新活锁的窗口(原样放回不删) + # ——与 lease 墓碑 check-move 竞态同族,杜绝误删活主。 + victim = f"{lock_dir}.dead.{os.getpid()}.{time.monotonic_ns()}" + try: + os.rename(lock_dir, victim) + except OSError: + continue # 他者已清/接管:不抢,重评估 + if _lock_owner(victim) != owner: + with contextlib.suppress(OSError): + os.rename(victim, lock_dir) # 判死后换新主:原样放回 + continue + shutil.rmtree(victim, ignore_errors=True) + continue # 死主残锁:清后重试(mkdir 收敛单赢家) + if time.monotonic() >= deadline: + raise HostingError( + f"add {name}: 同标签并发写互斥超时" + f"({_LABEL_LOCK_TIMEOUT:.0f}s,另一进程持锁)," + "fail-closed 拒双写", + code=1, + ) from e + time.sleep(0.05) + continue + break + with contextlib.suppress(OSError): + with open(os.path.join(lock_dir, "pid"), "w", + encoding="ascii") as fh: + fh.write(str(os.getpid())) + try: + yield + finally: + shutil.rmtree(lock_dir, ignore_errors=True) + def _label_id(self, name): # 【文档推导】ListProjectLabels → name→id payload = self._req("GET", f"{self._base()}/labels", @@ -1060,6 +1250,31 @@ def current_adapter(repo="."): raise HostingError(f"未知 FACTORY_HOSTING: {FACTORY_HOSTING}", code=2) +def _repo_split(raw): + """--repo 双语义拆分(PR #116 CodeRabbit)→ (local, slug)。 + + - 本地目录形态(存在,含 "."):探测/远端解析用真实路径(该目录 + remote 选平台、解析 slug),ops 收 slug=None——由目录 remote + 自解析(java#29 跨仓调用的本意;目录路径绝不进 gh --repo)。 + - owner/repo slug 形态:无 host 信息,平台检测只能落在 cwd remote—— + cwd 与目标仓不一致时 Codeup 会被误判 GitHub(issue #134)。仅当 + FACTORY_HOSTING 显式锚定平台时放行(slug 原样进 ops),否则 + fail-closed 拒收。 + 未传 --repo → (".", None)。 + """ + if raw is None: + return ".", None + if os.path.isdir(raw): + return raw, None + # 实时读 env(与 _detect_hosting 同、与模块常量 FACTORY_HOSTING 异): + # monkeypatch 无需 reload 即可测 + if not os.environ.get("FACTORY_HOSTING"): + raise HostingError( + "--repo slug 形态(owner/repo)无法判定托管平台(issue #134):" + "请传仓库本地路径,或显式设置 FACTORY_HOSTING 后再用 slug", code=2) + return ".", raw + + # --------------------------------------------------------------------------- # CLI # --------------------------------------------------------------------------- @@ -1306,12 +1521,16 @@ def _cmd_pr_merge(ad, args): def main(argv): - """CLI 入口:解析 → 取适配器 → 命令分派。""" + """CLI 入口:解析 → --repo 双语义拆分 → 取适配器 → 命令分派。""" args = _build_parser().parse_args(argv) try: # --repo 目标仓驱动平台检测(java#29 Sourcery:按 cwd 检测在 - # 跨仓 CLI 调用下会选错适配器);未注册 --repo 的子命令回退 "." - ad = current_adapter(getattr(args, "repo", None) or ".") + # 跨仓 CLI 调用下会选错适配器);拆分后探测用 local(目录真实 + # 路径或 "."),args.repo 改写为 slug 形态——目录形态收 None + # (slug 由该目录 remote 自解析),slug 形态原样进 ops。 + raw = getattr(args, "repo", None) + local, args.repo = _repo_split(raw) + ad = current_adapter(local) if args.cmd == "auth": sys.exit(0 if ad.auth_ok() else 1) diff --git a/.factory/mutations/run.py b/.factory/mutations/run.py index 679376e..5765ad6 100755 --- a/.factory/mutations/run.py +++ b/.factory/mutations/run.py @@ -6,9 +6,9 @@ 两种门: - guard(篡改类):guard.py --files 单文件,秒级。 -- tests(行为破坏类):run_tests.sh --no-lock 全量测试门(8 套件 + - badcase 双通道;--no-lock 跳过 plugin_lock/md_link_check——它们是 - blob 锁与链接门,不消费被注入的行为面),单条分钟级,输出带耗时。 +- tests(行为破坏类):final_gate_cmd 全量测试门(各仓自定;本仓 + 多套件 + badcase 双通道,blob 锁/链接门不消费被注入的行为面故以 + --no-lock 形参跳过),单条分钟级,输出带耗时。 - docstring(文档契约类):factory-local.json docstring_gate_cmd(可选门, 缺省不启用;未配置时 docstring 缺陷 SKIP,不构成全绿)——删除公开/内部 符号 docstring → 门应拦截(对外 API 100% + 内部 ≥80%),单条秒级。 @@ -126,7 +126,7 @@ def write_stamp(evidence: str | None = None) -> str | None: """全绿出口调用:当前周界 blob 写入 stamp(None = 无法绑定,不写)。 evidence 指向人工证据留档名;缺省时取 mutations/ 目录最新 - EVIDENCE-*.md(下游 wop-web-tools 反哺:静态默认文件名会过期—— + EVIDENCE-*.md(下游 xx-web-tools 反哺:静态默认文件名会过期—— 写戳引用不存在的留档 = stamp 说谎)。无留档如实记「无留档文件」。 """ import datetime @@ -244,7 +244,7 @@ def tracked_and_dirty(rel: str) -> bool: def run_gate(gate: str, target: str) -> int | None: """跑门返回退出码;超时返回 None(无效运行,见 judge)。 - 超时杀**整个进程组**(start_new_session + killpg):run_tests.sh 会 + 超时杀**整个进程组**(start_new_session + killpg):全量门会 派生 pytest 孙进程,只杀门直子会留下孤儿继续读注入中的 target ——finally 还原字节与孤儿运行并发,污染后续缺陷轮(PR #33 审查)。 """ @@ -338,7 +338,7 @@ def main() -> int: stamp_stale_banner() if args.only: wanted = {x.strip() for x in args.only.split(",") if x.strip()} - # 下游 wop-web-tools 反哺:未知 id 过滤结果为空仍以 0 退出写戳 + # 下游 xx-web-tools 反哺:未知 id 过滤结果为空仍以 0 退出写戳 # = 配置错误伪装成全量验证。缺失即拒绝(exit 2,对齐 guard 用法语义)。 all_ids = {d.id for d in defects} if missing := wanted - all_ids: @@ -352,7 +352,7 @@ def main() -> int: for d in defects: print(f"[{d.id}] {d.description}(gate={d.gate})") - # 下游 wop-web-tools 反哺:d.target 绝对路径会重置 REPO_ROOT 拼接 + # 下游 xx-web-tools 反哺:d.target 绝对路径会重置 REPO_ROOT 拼接 # (Path / 语义),`..` 可越仓——--defects 载外部 JSON 时指向仓外 # 文件注入+写回(进程非正常终止 = 注入残留落仓外)。resolve 后 # 必须仍在 REPO_ROOT 内。 diff --git a/.factory/prompts/implement.md b/.factory/prompts/implement.md index 52da77b..9db183e 100644 --- a/.factory/prompts/implement.md +++ b/.factory/prompts/implement.md @@ -8,6 +8,9 @@ 若任务与 `$ISSUE_DIR/issue.json` 评论冲突,停止并声明 blocked) - `$ISSUE_DIR/ralph-todo.md`(若存在且非空 = 修复轮:上一轮 review 回流的可行动发现清单,见「修复轮」节;首轮不存在) +- `$ISSUE_DIR/salvage-r*.patch`(若存在 = 上轮失败链的**未提交**工作快照: + 只作证据——上轮做到哪、什么思路;**不是续作指令**,不自动 apply, + 有效改动只由本轮 plan.json 决定) ## 纪律(违反任何一条 = 本节点失败) @@ -19,8 +22,22 @@ 将完整输出另存到 `$ISSUE_DIR/tests-output.txt`(holdout 节点的输入)。 随后对**触及的测试套件**以 `-v` 冗长模式重跑一次并附于该文件末尾: holdout 只见此文件且不允许推测,静默点号输出无法建立测试与诉求的 - 对应关系(证据饥饿),测试名与参数化用例名是必需证据 + 对应关系(证据饥饿),测试名与参数化用例名是必需证据。 + 若本次改动**新写了测试或针对性验证**(新增测试文件、新用例、独立验证 + 脚本),其 `-v` 输出必须**逐条追加**进 tests-output.txt,标注所属文件与 + 断言目标——holdout 以「issue 诉求 → 测试名」反向核对,新证据不落此 + 文件 = 诉求无对应证据 = 判 FAIL(历史教训:2026-09-03 #123 r3 implement + 全绿但 tests-output.txt 只有预存套件,新诉求零证据,holdout FAIL) 5. 完成后把改动提交到当前分支(不 push、不开 PR——那是链脚本的事) +6. **预算纪律**:本节点受编排器硬性时间预算击杀(--max-time,任务参数段 + 有具体值)。预算过半仍未进入最后一个任务时,裁剪范围并在日志中声明 + 裁剪,绝不把收尾动作压到预算尾部;可裁剪的旁支核验一律让位于 + 「输出与收尾协议」的三步 +7. **后台任务收割令**:收尾阶段不得等待任何后台/委派任务(审查、子代理、 + 异步作业)。委派出去的工作必须 await 收割其结果后才能进入收尾; + 预算内收割不完的,在日志记为「未收割」并放弃——不得以等待换取 + 完整性(历史死因:全部工作已完成并提交,末尾等待后台审查任务 + 耗尽预算,ARTIFACT 行未落地) ## 修复轮(`$ISSUE_DIR/ralph-todo.md` 存在且非空时) @@ -29,10 +46,18 @@ tests-output.txt 含 -v 证据)与纪律 5(提交)。执行日志以 「## 修复轮 N」为题**追加**到 `$ISSUE_DIR/implement.md`,不重写既有内容。 -## 输出 +## 输出与收尾协议 -执行日志用 write 工具写入 `$ISSUE_DIR/implement.md`: -每任务一节(改动文件、verify 命令与结果、跳过/blocked 说明), -末尾附 final_gate 结果摘要。 +执行日志用 write 工具**增量**写入 `$ISSUE_DIR/implement.md`:每任务一节 +(改动文件、verify 命令与结果、跳过/blocked/裁剪/未收割说明),末尾附 +final_gate 结果摘要。不要把日志写作堆积到收尾一次性完成——增量落盘的 +日志在节点死亡后仍是失败诊断与下轮回流的输入。 -stdout 最后一行输出:`ARTIFACT: $ISSUE_DIR/implement.md` +收尾三步,顺序不可换、紧凑执行: + +1. 提交全部改动(纪律 5;已提交的成果可被失败链 salvage 回收) +2. 日志落盘(含裁剪/未收割/blocked 说明) +3. stdout 最后一行输出:`ARTIFACT: $ISSUE_DIR/implement.md` + +> 产物声明是节点的唯一存活信号:工作全部完成但 ARTIFACT 行未落地 = +> 节点失败、整轮作废。宁可裁剪范围、宁可放弃未收割任务,不可晚声明。 diff --git a/.factory/prompts/plan.md b/.factory/prompts/plan.md index d7cce4d..ef5a5ea 100644 --- a/.factory/prompts/plan.md +++ b/.factory/prompts/plan.md @@ -9,6 +9,9 @@ implement 节点可以不做任何设计决策地照做。 验收标准在评论里,**以评论为准**,正文不可覆盖评论要求) - `$ISSUE_DIR/chain-history`(历史轮次:上轮 holdout FAIL evidence 已被 prime 提炼——若 prime 标记了上轮拒绝理由,对应修复必须是首个任务) +- `$ISSUE_DIR/plan-pre-r*.json`(若存在 = 上轮计划归档:delta-plan—— + 仍有效的任务**原样继承**,仅按 issue 评论与上轮死因(chain-history 的 + node-fail/chain-abort/holdout 行)重排/增删变化部分,不重写全量计划) ## 任务 diff --git a/.factory/prompts/prime.md b/.factory/prompts/prime.md index 533b455..f4a205d 100644 --- a/.factory/prompts/prime.md +++ b/.factory/prompts/prime.md @@ -9,6 +9,16 @@ - `$ISSUE_DIR/chain-history`(历史轮次证据:若含 `holdout ... verdict=FAIL`, 其 evidence 是上轮验证器的拒绝理由——**本轮必须针对性消除**:通常是 改动缺少可机械引用的验收证据,如为文档类改动补同步性测试) +- `$ISSUE_DIR/chain-history` 的 `node-fail`/`chain-abort` 行(若存在 = + 上轮链死因:`node-fail ... node=<名> reason=omp-exit|no-artifact| + stale-artifact` = 该节点被预算击杀/崩溃或产物判定 fail-closed; + `chain-abort ... exit=<码>` = 编排层死亡。上轮死因相关区域是本轮研究的 + 优先对象) +- `$ISSUE_DIR` 下的 `*-pre-r*.*` 归档(若存在 = 本轮开始前的过程产物: + prime 笔记/plan.json/implement 日志/review 结论,r 编号最大者最近)。 + **跨轮增量纪律**:仍成立的结论直接复用、不必重查——以 chain-history + 最近一次 `chain-start` 行的时间戳为 `git log --since` 起点,只研究此后 + 的变化面;全量重查仅限首层(无归档可用)时 - 仓库内自由阅读:任务参数「仓库参数」段所列阅读范围 ## 任务 diff --git a/.factory/regression/dispatch_liveness.py b/.factory/regression/dispatch_liveness.py index 4b6e78c..72f0402 100755 --- a/.factory/regression/dispatch_liveness.py +++ b/.factory/regression/dispatch_liveness.py @@ -11,6 +11,20 @@ 2. 进程根本没跑(LaunchAgent 断档,历史实测 13h):streak 计数文件 /.factory/locks/dispatch-fail-streak 正常应随每轮(600s)被 touch/重写;mtime 超过 FRESH_SECS 未更新 = 该仓库调度未运行,FAIL。 +3. GitHub 侧滞留(2026-09-15 issue #165 实证:零改动轮留守 in-progress + 5 天不可见,新失败投进死信筒): + a. open issue 挂 factory:in-progress 但 leases/issue:.lock 不存在 + ——链已收官/早亡,无存活租约(锁先于标签获取,标签在而锁不在 + = 滞留,无阈值窗口); + b. open issue 挂 factory:rejected:先验回执存在性——只落标无回执 + 评论 = 链完整性违规(2026-09-20 #207 实证:落标假阴性早退, + 只落标不发判据 = 不可审计的静默拒绝),不受宽限即时 FAIL; + 有回执且 updatedAt 超过 --stale-days 无人工跟进(拒裁回执评论 + 会刷新 updatedAt,故 updatedAt ≥ 拒裁时刻)才计滞留——dispatch + 尾部 rejected-reconcile 对账只在本地日志,本层把静默滞留升为 + 回归 FAIL。 + hosting 不可用(无凭据/离线/下游仓未配)→ note 跳过,不误报—— + 本地两层(stalled/streak)照常检查。 不 FAIL 的合法状态(每仓独立判定): - stalled/streak 均不存在:调度器未启用或从未失败——note 不 FAIL @@ -18,15 +32,22 @@ - 注册表中的仓库缺 .factory/(未接入工厂的仓库误注册):note 不 FAIL, 提示从 repos.conf 移除——registry 是用户侧文件,本检查只读不写。 -用法: python3 dispatch_liveness.py [--fresh-secs 93600] [--repos PATH] +用法: python3 dispatch_liveness.py [--fresh-secs 93600] [--stale-days 7] + [--repos PATH] --repos 默认 ~/.config/factory/repos.conf(hub 注册表,与 ~/.config/factory/dispatch-all.sh 同源——单一注册表两处消费)。 93600s = 26h:日回归节奏下,一轮正常 touch(600s)远远新鲜于阈值。 + --stale-days 默认 7:rejected 滞留人工跟进宽限(日)——rejected 按设计 + 等人,宽限内不告警。 退出码: 0=全部活/合法缺省 1=任一仓库死 2=参数/环境错误 """ from __future__ import annotations import argparse +import datetime +import json +import os +import subprocess import sys import time from pathlib import Path @@ -50,6 +71,125 @@ def registry_repos(registry: Path) -> list[Path]: return repos +def hosting_issues(factory: Path) -> list[dict] | None: + """hosting.py issue list → 归一化 issue 清单;不可用 → None(降级)。 + + 只读单次可判定调用(ADR-008 平台抽象,无 gh 直调)。超时/无凭据/ + 非 JSON 都归 None:本检查的信条是"合法缺省不误报",GitHub 侧层 + 降级时本地两层照常 FAIL 能力不受影响。 + cwd=repo:hosting.py 的 slug 解析走 `git -C . remote`——必须在 + 目标仓自身目录下运行,否则 CWD 仓库劫持 slug(多仓混查同一仓)。 + Codeup 后端工作项面未实装 → 报错归 None,同样降级 note。 + """ + factory = factory.resolve() + try: + r = subprocess.run( + [sys.executable, str(factory / "hosting.py"), + "issue", "list", "--state", "open", "--limit", "200", + "--comments"], + capture_output=True, text=True, timeout=90, cwd=str(factory.parent)) + except (OSError, subprocess.TimeoutExpired): + return None + if r.returncode != 0: + return None + try: + return json.loads(r.stdout) + except json.JSONDecodeError: + return None + + +def _gh_updated_age_secs(updated: str | None) -> float | None: + """gh updatedAt ISO8601 → 距今秒数;不可解析 → None(该检测跳过)。""" + if not updated: + return None + try: + dt = datetime.datetime.fromisoformat(updated.replace("Z", "+00:00")) + except ValueError: + return None + return (datetime.datetime.now(datetime.timezone.utc) - dt).total_seconds() + + +def _lease_lock_alive(lock: Path) -> bool: + """单写者锁文件存活:内容第 5 字段=租期秒(缺省 900), + mtime+租期 > now = 活;缺失/畸形/过期(SIGKILL 残锁)= 死。""" + # 回退链对齐权威 _lease_sw_fresh(factory-lease.sh:118): + # 第5字段缺 → FACTORY_LEASE_SECS env → 900 + fallback = os.environ.get("FACTORY_LEASE_SECS", "900") + fallback = int(fallback) if fallback.isdigit() else 900 + try: + line = lock.read_text(encoding="utf-8").strip().splitlines()[0] + parts = line.split("|") + secs = int(parts[4]) if len(parts) > 4 and parts[4].isdigit() else fallback + return lock.stat().st_mtime + secs > time.time() + except (OSError, IndexError, ValueError): + return False + + +def check_stalled_labels(repo: Path, stale_days: int, problems: list[str]) -> str: + """GitHub 侧滞留检查(第三死法)→ ok/note。FAIL 项追加 problems。 + + in-progress 滞留判据(锁先于标签获取、链存活期间持续持有)只在 + 单写者形态成立:PG 形态(SUPABASE_DB)租约在 factory_leases 表, + 本地锁文件从不存在——该形态下 in-progress 检测整体降级 note + (否则每个存活链每天误报滞留,2026-09-16 审查 major)。 + """ + tag = repo.name + factory = repo / ".factory" + pg_mode = bool(os.environ.get("SUPABASE_DB")) + issues = hosting_issues(factory) + if issues is None: + print(f"note: [{tag}] hosting 不可用——GitHub 侧滞留检测跳过(本地层已查)") + return "note" + n_ok = 0 + for it in issues: + labels = set(it.get("labels") or []) + n = it.get("number") + if "factory:in-progress" in labels: + if pg_mode: + # PG 形态租约在库,本地锁文件判据不适用——降级 note + continue + # 锁先于标签获取、链存活期间持续持有:标签在而锁不活 = 滞留 + lock = factory / "locks" / "leases" / f"issue:{n}.lock" + if not _lease_lock_alive(lock): + problems.append( + f"[{tag}] issue #{n} 滞留 factory:in-progress 无存活链租约" + "(零改动轮留守或链早亡)——日回归失败会唤醒重派," + "其余滞留请人工裁决关闭") + else: + n_ok += 1 + elif "factory:rejected" in labels: + # 回执存在性先行(#207 实证:落标假阴性早退 → 只落标无 + # 回执,只落标不发判据 = 不可审计的静默拒绝)——链完整性 + # 违规不进宽限时钟,即时 FAIL;回执判据 = 幂等 marker + # (issue_reject 评论尾埋;标题子串可被人工复述冒充,PR #211 + # Sourcery 评论2),与 factory_lib.rejected_reconcile 同一 + # 判据不得漂移。 + marker = f"factory:receipt:issue-{n}:r" + if all( + marker not in str(c.get("body") or "") + for c in (it.get("comments") or []) + if isinstance(c, dict) + ): + problems.append( + f"[{tag}] issue #{n} rejected 无回执评论——链完整性违规" + "(#207 同型),需补发回执") + continue + age = _gh_updated_age_secs(it.get("updatedAt")) + if age is not None and age > stale_days * 86400: + problems.append( + f"[{tag}] issue #{n} rejected 滞留 {age/86400:.1f}d 无人工跟进" + f"(阈值 {stale_days}d)——dispatch 日志尾部对账有明细," + "请人工裁决处置") + else: + n_ok += 1 + if pg_mode: + print(f"note: [{tag}] PG 租约形态——in-progress 滞留检测降级(租约在库," + "本地判据不适用);rejected 宽限检测照常") + return "note" + print(f"ok: [{tag}] GitHub 侧滞留检查:{len(issues)} open issue,{n_ok} 正常/宽限内") + return "ok" + + def check_repo(repo: Path, fresh_secs: int, problems: list[str]) -> str: """单仓库活性检查 → 状态行(ok/note)。FAIL 项追加到 problems。""" tag = repo.name @@ -86,6 +226,8 @@ def main() -> int: ap = argparse.ArgumentParser(description="dispatch 调度器活性检查(多仓:hub 注册表)") ap.add_argument("--fresh-secs", type=int, default=93600, help="streak 文件新鲜度阈值秒(默认 93600=26h)") + ap.add_argument("--stale-days", type=int, default=7, + help="rejected 滞留人工跟进宽限天数(默认 7)") ap.add_argument("--repos", default=str(DEFAULT_REGISTRY), help="hub 注册表路径(默认 ~/.config/factory/repos.conf)") args = ap.parse_args() @@ -101,6 +243,7 @@ def main() -> int: problems: list[str] = [] for repo in repos: check_repo(repo, args.fresh_secs, problems) + check_stalled_labels(repo, args.stale_days, problems) if problems: for p in problems: diff --git a/.factory/state.py b/.factory/state.py index 4c5148c..52f98e7 100755 --- a/.factory/state.py +++ b/.factory/state.py @@ -67,7 +67,7 @@ def _needs_fix_rounds(events): def _linked_issue(pr): """链约定:PR body 含 'Closes #N'。N 为 GitHub 数字或 Codeup 序号 - (KFPT-16,ADR-008)——统一按字符串处理。""" + (T-16,ADR-008)——统一按字符串处理。""" m = re.search(r"[Cc]loses #([\w][\w-]*)", (pr or {}).get("body") or "") return m[1] if m else None diff --git a/.factory/sync-from-upstream.sh b/.factory/sync-from-upstream.sh index cada761..2c2f6a7 100755 --- a/.factory/sync-from-upstream.sh +++ b/.factory/sync-from-upstream.sh @@ -17,7 +17,7 @@ # --repo 目标仓(默认:当前目录所属仓)——中心仓巡检以此驱动 # 下游追平,始终执行中心版脚本,免疫下游副本滞后 # --commit 仅与 --apply 组合:追平产物+锚点+blame-ignore 以单提交 -# 落库(factory: 上游同步追平()),落在当前分支不推送; +# 落库(chore(factory): 上游同步追平()),落在当前分支不推送; # 提交无法含自身 SHA,blame-ignore 滞后一条(本次记上次) # # 退出码: 0=干净/已同步 1=有漂移(--check)或应用失败 2=用法/上游不可用 @@ -145,8 +145,22 @@ _sr_clean() { # 0=干净 1=有 issue 2=CLI 异常 SR_GATE_ON=0 if [ "$MODE" = apply ] && command -v sourcery >/dev/null 2>&1; then SR_GATE_ON=1 - _sr_clean && echo "Sourcery 回归闸基线: .factory 干净" \ - || { echo "Sourcery 回归闸基线: .factory 已有 issue——先清零再追平(闸口径=PR gate)" >&2; exit 2; } + # 版本口径提示(issue #104):本地 CLI 版本与上游 CI pin 不一致时, + # 上游已清零的快照本地可报 issue——先排除口径差异再查真实回归。 + # rc 区分(Sourcery 审查):1=有 issue(才可能是版本口径差异); + # ≥2=CLI 执行失败,误诊为版本漂移会误导排查 + if _sr_clean; then + echo "Sourcery 回归闸基线: .factory 干净" + else + sr_status=$? + if [ "$sr_status" -eq 1 ]; then + echo "Sourcery 回归闸基线: .factory 已有 issue——先清零再追平(闸口径=PR gate)。" \ + "若上游 CI 曾全绿:疑似本地 CLI 版本口径差异(本地 $(sourcery --version 2>/dev/null || echo '?') vs 上游 sourcery-review-gate.yml 的 pin),对齐版本后复跑再定" >&2 + else + echo "Sourcery 回归闸执行失败(CLI 异常,退出码=$sr_status)——请检查本地 CLI 后重试" >&2 + fi + exit 2 + fi fi # 上游 mode+blob(git show 丢 mode,覆盖后须恢复执行位) @@ -171,7 +185,7 @@ while IFS=$'\t' read -r kind rel; do echo " [$kind] $rel: 本地缺失" [ "$kind" = full ] && DRIFT=1 if [ "$MODE" = apply ] && [ "$kind" = full ]; then - mkdir -p "$(dirname "$dst")" # 缺失父目录先建(wop-skills:tests/ 整缺,重定向即崩) + mkdir -p "$(dirname "$dst")" # 缺失父目录先建(xx-skills:tests/ 整缺,重定向即崩) # tmp+mv 原子替换(#103):$dst 可能是运行中脚本自身——bash 惰性逐段 # 读源文件,`> "$dst"` 直写截断同 inode,旧读位移落在新内容中途即 # syntax error 半同步态;同目录 rename 换 inode,旧 inode 保活至跑完 @@ -268,10 +282,10 @@ PY git -C "$REPO" add -- "$IGNORE" PREV_SYNC="$(git -C "$REPO" log -1 --format=%H -- "$LOCKFILE" 2>/dev/null || true)" if [ -n "$PREV_SYNC" ] && ! grep -q "^${PREV_SYNC}$" "$IGNORE" 2>/dev/null; then - printf '# factory: 上游同步追平(%s)\n%s\n' "${HEAD_SHA:0:9}" "$PREV_SYNC" >> "$IGNORE" + printf '# chore(factory): 上游同步追平(%s)\n%s\n' "${HEAD_SHA:0:9}" "$PREV_SYNC" >> "$IGNORE" git -C "$REPO" add -- "$IGNORE" fi - git -C "$REPO" commit -q -m "factory: 上游同步追平(${HEAD_SHA:0:9})" + git -C "$REPO" commit -q -m "chore(factory): 上游同步追平(${HEAD_SHA:0:9})" echo "已提交: $(git -C "$REPO" rev-parse --short HEAD)(当前分支,不推送)" fi # 无条件设置(幂等,绝对路径):无变更分支此前直接跳过,文件在而 diff --git a/.factory/tests/conftest.py b/.factory/tests/conftest.py index 60b5aa4..a5baf31 100644 --- a/.factory/tests/conftest.py +++ b/.factory/tests/conftest.py @@ -2,6 +2,8 @@ import sys from pathlib import Path +import pytest + sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # 测试密封性(steering/testing-standards.md):import 期剥离 hook 注入的 @@ -14,3 +16,20 @@ for _k in ("GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_COMMON_DIR", "GIT_NAMESPACE"): os.environ.pop(_k, None) + + +@pytest.fixture() +def private_tmp(tmp_path: Path) -> Path: + """泄漏断言隔离:被测进程 TMPDIR 注入 pytest 私有目录。 + + mktemp 泄漏断言(after-before 差集)原对共享系统 tempdir glob 同模板 + 文件(.factory-dist.* / .factory-stage.* / .factory-downstream-check.*), + 套件外进程瞬时写入同模板即随机打破差集(pre-push 闸两次 flake,隔离 + 重跑绿证实环境性;tests 顺序执行、其余三闸对本仓 no-op,污染源必在 + 套件外)。被测脚本 mktemp 模板 "${TMPDIR:-/tmp}/..." 原生尊重 TMPDIR + ——用例把 env["TMPDIR"] 指到本目录后,泄漏检测语义不变(仍检被测 + 进程自身暂存),断言 glob 对齐同一目录,外部写者不再可见。 + """ + d = tmp_path / "tmp" + d.mkdir() + return d diff --git a/.factory/tests/golden/factory-init/MISSION.md b/.factory/tests/golden/factory-init/MISSION.md new file mode 100644 index 0000000..fb73d86 --- /dev/null +++ b/.factory/tests/golden/factory-init/MISSION.md @@ -0,0 +1,53 @@ +# MISSION — fixture-repo 工厂使命(治理文件) + +> 状态:S0 草案 v0.1(tools/factory-init.py 生成;本文件属治理层:工厂永不可修改——铁律 3, +> 由 .factory/guard.py 机械化执行)。 + +## 为什么存在 + +fixture-repo 的维护需求增长快于人工维护预算——正确反应是把**可判定的维护工作 +交给机器**,把人类的稀缺输入(意图、判断、信任锚)留给宪法与周界。 + +## 工厂使命 + +在人类宪法(本文件 + 仓库既有约定)约束下,自动化本仓库的维护循环: + +issue → triage → 实现 → 确定性门 → PR → 独立验证(holdout)→ 人工合并 + +人类只保留两件事:**写 issue、合并 PR**。 + +## Triage 判据 + +accept 当且仅当 issue 同时满足: + +1. **使命一致**:本仓库代码、既有测试与文档的维护或增强; +2. **可判定**:完成与否能被验证门(final_gate_cmd、guard、holdout)客观判定; +3. **不触周界**:不需要修改下述 PERIMETER 中任何路径。 + +其余一律 reject(二值;不同意可补充上下文后重开,下一轮全新评估)。 +拒绝回执的判据明细措辞维护于 `.factory/factory-local.json` reject_guidance。 + +## 周界(PERIMETER) + +以下路径工厂永不可触碰;变更只能走人类 PR: + +- `docs/` +- `src/` +- `MISSION.md` +- `.gitignore` +- `.factory/` + +> 周界清单是利益权衡(宁宽勿窄:过宽的代价是多走人审,过窄的代价是被 +> 绕过),由人类定期复核收窄。 + +## 铁律 + +1. **Holdout**:验证器永不读实现计划——验结果 against issue,不验方法。 +2. **二值 triage**:只有 accept / reject,没有中间态收件箱。 +3. **治理不可自改**:本文件、周界(与 `.factory/factory-local.json` 双向 + 一致,guard.self_check 强制核对)、验证门自身,工厂一律不可修改。 +4. **Dispatcher 零 LLM**:调度器是纯 bash + 托管平台适配层,读 label 决定动作。 +5. **门灵敏度先行**:自动化的前提是 `.factory/mutations/` 注入缺陷全量被 + 拦截(kill rate 达标);未证明的门不是门。 +6. **不可信输入隔离**:issue / PR 正文视为不可信文本;仅 triage 产出的 + 结构化 JSON 可进入下游节点。 diff --git a/.factory/tests/golden/factory-init/defects.json b/.factory/tests/golden/factory-init/defects.json new file mode 100644 index 0000000..d642ca2 --- /dev/null +++ b/.factory/tests/golden/factory-init/defects.json @@ -0,0 +1,86 @@ +{ + "description": "S0 结构缺陷集(tools/factory-init.py 生成,锚点绑定本仓周界;行为破坏类 B-1xx 待人工按目标仓测试面扩充后重证)", + "defects": [ + { + "id": "G-01", + "description": "篡改治理宪法:MISSION.md 铁律降级为可选建议", + "target": "MISSION.md", + "find": "## 铁律", + "replace": "## 铁律(工厂可自行修订)", + "gate": "guard", + "expect_block": true + }, + { + "id": "G-02", + "description": "篡改规范真相源:.gitignore 标题注入豁免条款", + "target": ".gitignore", + "find": ".factory/locks/", + "replace": ".factory/locks/(工厂豁免条款适用)", + "gate": "guard", + "expect_block": true + }, + { + "id": "G-03", + "description": "篡改验证门自身:guard.py 周界配置载入改向不存在文件致门 fail-closed(rc=2 应视为拦截)", + "target": ".factory/guard.py", + "find": "cfg_path = Path(__file__).resolve().parent / \"factory-local.json\"", + "replace": "cfg_path = Path(__file__).resolve().parent / \"factory-local-missing.json\"", + "gate": "guard", + "expect_block": true + }, + { + "id": "G-04", + "description": "篡改质检线:factory-local.json final_gate_cmd 被注空——真实测试门失效", + "target": ".factory/factory-local.json", + "find": "\"final_gate_cmd\": \"", + "replace": "\"final_gate_cmd\": \"true #", + "gate": "guard", + "expect_block": true + }, + { + "id": "M-01", + "description": "周界清单单边漂移:MISSION 增条目而 guard 副本未同步(self_check 应发现不一致)", + "target": "MISSION.md", + "find": "- `docs/`", + "replace": "- `docs/`\n- `factory-extra/`", + "gate": "guard", + "expect_block": true + }, + { + "id": "M-02", + "description": "周界指向不存在路径:目录条目改名(self_check 存在性检查应拦截)", + "target": "MISSION.md", + "find": "- `docs/`", + "replace": "- `docs/renamed/`", + "gate": "guard", + "expect_block": true + }, + { + "id": "G-06", + "description": "点文件周界正向证据:任何触碰必拦——前缀匹配对点前缀路径有效", + "target": ".gitignore", + "find": ".factory/locks/", + "replace": "#.factory/locks/", + "gate": "guard", + "expect_block": true + }, + { + "id": "G-05", + "description": "normalize 点前缀退化实证:lstrip(\"./\") 连剥前导点/斜杠,周界失明放行——本条 expect_block=false 钉死该缺陷形态(失明门必放行);若 guard 意外拦截(rc≠0)说明 normalize 被改宽,须人审。正向 kill 证据由 G-06 承载", + "target": ".factory/guard.py", + "find": " p = path.strip().replace(\"\\\\\", \"/\")\n return p.removeprefix(\"./\")", + "replace": " return path.strip().lstrip(\"./\").replace(\"\\\\\", \"/\")", + "gate": "guard", + "expect_block": false + }, + { + "id": "B-01", + "description": "良性变更负例:CHANGELOG.md 标题追加维护署名,门必须放行(防门全拒)", + "target": "CHANGELOG.md", + "find": "# fixture-repo 变更日志", + "replace": "# fixture-repo 变更日志(工厂参与维护)", + "gate": "guard", + "expect_block": false + } + ] +} diff --git a/.factory/tests/golden/factory-init/factory-local.json b/.factory/tests/golden/factory-init/factory-local.json new file mode 100644 index 0000000..e733084 --- /dev/null +++ b/.factory/tests/golden/factory-init/factory-local.json @@ -0,0 +1,28 @@ +{ + "description": "工厂本地化配置(M4 + ADR-009):周界/判据措辞/门命令/仓库参数/上游指针是数据不是代码。本文件每仓一份(skip 分发,永不跨仓覆盖)。fail-closed:缺失/损坏/缺键 = 使用方直接非零退出。周界须与 MISSION.md「周界(PERIMETER)」一致——guard.self_check 每次运行强制核对。S0 骨架:tools/factory-init.py 生成,TODO 标记字段待人工本地化。", + "perimeter": [ + "docs/", + "src/", + "MISSION.md", + ".gitignore", + ".factory/" + ], + "reject_guidance": { + "a": "判据a(使命一致):写明落点——fixture-repo 的哪个文件/模块,为何属维护而非范围变更(判据全文见 MISSION.md);", + "b": "判据b(可判定):把完成标准写成可机械验证的形式——验收 = 具体测试/脚本的断言;避免「持续/优化/修收」类开放措辞;", + "c": "判据c(不触周界):触及周界的部分拆成独立 issue 走人类 PR(清单见 MISSION.md);" + }, + "repo_identity": "fixture-repo", + "reading_scopes": [ + ".factory/", + "README.md", + "docs/", + "src/" + ], + "review_basis": "", + "final_gate_cmd": "false", + "pr_review_skills": [], + "upstream_repo": "", + "upstream_path": "/fake/upstream", + "feedback_branch_prefix": "feedback/fixture-repo" +} diff --git a/.factory/tests/golden/factory-init/mission-phase1.md b/.factory/tests/golden/factory-init/mission-phase1.md new file mode 100644 index 0000000..8461b57 --- /dev/null +++ b/.factory/tests/golden/factory-init/mission-phase1.md @@ -0,0 +1,49 @@ +# MISSION — fixture-repo 工厂使命(治理文件) + +> 状态:S0 草案 v0.1(tools/factory-init.py 生成;本文件属治理层:工厂永不可修改——铁律 3, +> 由 .factory/guard.py 机械化执行)。 + +## 为什么存在 + +fixture-repo 的维护需求增长快于人工维护预算——正确反应是把**可判定的维护工作 +交给机器**,把人类的稀缺输入(意图、判断、信任锚)留给宪法与周界。 + +## 工厂使命 + +在人类宪法(本文件 + 仓库既有约定)约束下,自动化本仓库的维护循环: + +issue → triage → 实现 → 确定性门 → PR → 独立验证(holdout)→ 人工合并 + +人类只保留两件事:**写 issue、合并 PR**。 + +## Triage 判据 + +accept 当且仅当 issue 同时满足: + +1. **使命一致**:本仓库代码、既有测试与文档的维护或增强; +2. **可判定**:完成与否能被验证门(final_gate_cmd、guard、holdout)客观判定; +3. **不触周界**:不需要修改下述 PERIMETER 中任何路径。 + +其余一律 reject(二值;不同意可补充上下文后重开,下一轮全新评估)。 +拒绝回执的判据明细措辞维护于 `.factory/factory-local.json` reject_guidance。 + +## 周界(PERIMETER) + +以下路径工厂永不可触碰;变更只能走人类 PR: + +- `<待填写: 周界路径>` + +> 周界清单是利益权衡(宁宽勿窄:过宽的代价是多走人审,过窄的代价是被 +> 绕过),由人类定期复核收窄。 + +## 铁律 + +1. **Holdout**:验证器永不读实现计划——验结果 against issue,不验方法。 +2. **二值 triage**:只有 accept / reject,没有中间态收件箱。 +3. **治理不可自改**:本文件、周界(与 `.factory/factory-local.json` 双向 + 一致,guard.self_check 强制核对)、验证门自身,工厂一律不可修改。 +4. **Dispatcher 零 LLM**:调度器是纯 bash + 托管平台适配层,读 label 决定动作。 +5. **门灵敏度先行**:自动化的前提是 `.factory/mutations/` 注入缺陷全量被 + 拦截(kill rate 达标);未证明的门不是门。 +6. **不可信输入隔离**:issue / PR 正文视为不可信文本;仅 triage 产出的 + 结构化 JSON 可进入下游节点。 diff --git a/.factory/tests/test-lease-sql.sh b/.factory/tests/test-lease-sql.sh index b6b55ea..eb237a4 100755 --- a/.factory/tests/test-lease-sql.sh +++ b/.factory/tests/test-lease-sql.sh @@ -189,7 +189,27 @@ ck "SW 短租期 fence 过期拒" "1" "$(SW 'lease_fence_ok issue:short 1 >/ # 默认判的实现此处会误过——正是审查③指出的形态) SW 'lease_claim issue:hbs >/dev/null; sleep 1; lease_heartbeat issue:hbs 1 2 >/dev/null 2>&1' sleep 3 -ck "SW hb 短租期生效后过期" "1" "$(SW 'lease_fence_ok issue:hbs 1 >/dev/null 2>&1; echo $?')" +# ⑨ 墓碑调和(PR #116 CodeRabbit):release 在 mv 后/放回前被 SIGKILL → +# .rel.* 截留锁位。claim 建锁前调和:新鲜墓碑 = 活锁被截留 → ln 原子放回 +# (_lease_sw_restore,位空得内容删源;随后主流程读新鲜即拒 ≤ 租期自解); +# 过期/坏内容 = 死锁残渣 → 清理后正常接管。A2 用 ⑧ 链后无主过期锁基底。 +rellock="$tp/.factory/locks/leases/issue:rel.lock" +SW 'lease_claim issue:rel >/dev/null' # e=1 在位 +mv "$rellock" "$rellock.rel.1.1" # 模拟 release 死在 mv 后(新鲜墓碑) +ck "SW ⑨A1 新鲜墓碑 claim 拒" "1" "$(SW 'lease_claim issue:rel >/dev/null 2>&1; echo $?')" +ck "SW ⑨A1 新鲜墓碑放回锁位" "y" "$([ -f "$rellock" ] && echo y || echo n)" +ck "SW ⑨A1 墓碑清理" "n" "$([ -e "$rellock.rel.1.1" ] && echo y || echo n)" +touch -t 200001010000 "$rellock" # 过期 → 死锁残渣形态 +mv "$rellock" "$rellock.rel.2.2" +ck "SW ⑨A2 过期墓碑接管 e=2" "2" "$(SW 'lease_claim issue:rel')" +ck "SW ⑨A2 残渣清且锁在位" "y" "$([ -f "$rellock" ] && [ ! -e "$rellock.rel.2.2" ] && echo y || echo n)" +# ⑨c/d _lease_sw_restore 原子判位原语(ln 硬链:dst 空 → 得内容删源链; +# dst 已占 → ln 失败弃残本、不碰位上现有内容)——直接驱动 helper 断言 +# 判位语义,而非经 claim 间接观察 +ck "SW ⑨c restore 位空放回" "dst=fresh src=n" \ + "$(env -u SUPABASE_DB REPO="$tp" bash -c "source '${LEASE_SH}'; echo fresh > '$tp/rc-src'; _lease_sw_restore '$tp/rc-src' '$tp/rc-dst'; printf 'dst=%s src=%s\n' \"\$(cat '$tp/rc-dst')\" \"\$([ -e '$tp/rc-src' ] && echo y || echo n)\"" 2>/dev/null)" +ck "SW ⑨d restore 位占弃残" "dst=other src=n" \ + "$(env -u SUPABASE_DB REPO="$tp" bash -c "source '${LEASE_SH}'; echo fresh > '$tp/rd-src'; echo other > '$tp/rd-dst'; _lease_sw_restore '$tp/rd-src' '$tp/rd-dst'; printf 'dst=%s src=%s\n' \"\$(cat '$tp/rd-dst')\" \"\$([ -e '$tp/rd-src' ] && echo y || echo n)\"" 2>/dev/null)" # 双态边界:已设 SUPABASE_DB 但 psql 不可达 = 配置错误,仍 fail-closed(不降级) if command -v psql >/dev/null; then diff --git a/.factory/tests/test_b1_fallback.py b/.factory/tests/test_b1_fallback.py new file mode 100644 index 0000000..2d6ba1d --- /dev/null +++ b/.factory/tests/test_b1_fallback.py @@ -0,0 +1,108 @@ +"""B1 产物判定「无声明回退」回归(2026-09-05)——真实执行 run_node 分支逻辑。 + +事故背景:438aa0f 引入 B1 后,artifact 提取行(`grep … | sed …`)缺 `|| true`。 +fix-issue.sh 顶部 `set -euo pipefail` 下,prompts 无 ARTIFACT 声明时 grep +无匹配 exit 1 → 管道失败(pipefail)→ 命令替换失败 → 赋值语句非零 → +errexit 杀整个脚本,永远到不了 `-z` 回退分支(stdout ARTIFACT: 行检查)。 +Sourcery 在 PR #144 合并前自动修复(sed 后补 `|| true`)——本测试锚定该 +行为防回归: + +- 提取 fix-issue.sh 的 run_node/_node_metric 函数体(sed 锚定函数名起止, + 不执行顶层主流程/锁/trap),在 `set -euo pipefail` 子进程内 source 后 + 真实调用 run_node —— 不是复制判定逻辑,穿的是真实脚本代码。 +- 场景 A:prompts 无 ARTIFACT 声明 + 节点 log 含 `ARTIFACT:` 行 + → 提取得空值 → 走 `-z` 回退 → 节点 ok(exit 0、metrics 记 ok)。 + 修复前 errexit 在此杀进程,测试红。 +- 场景 B:prompts 无 ARTIFACT 声明 + log 无 `ARTIFACT:` 行 + → 回退判定失败 → return 1(no-artifact 失败路径,非 errexit 死)。 + +运行:python3 -m pytest .factory/tests -q(与其他沙箱测试同构:tmp git 仓 ++ .factory 拷贝 + PATH 桩;omp_node/node_timeout 只记录调用)。 +""" + +import json +import os +import shutil +import subprocess +from pathlib import Path + +FACTORY = Path(__file__).resolve().parents[1] + +_OMP_STUB = """#!/bin/sh +echo "omp_node $*" >> "${STUB_CALLS:?}" +if [ "${STUB_MODE:-ok}" != "bare" ]; then + printf 'done\\nARTIFACT: $ISSUE_DIR/%s\\n' "${STUB_ARTIFACT:-foo.md}" >> "$2" +else + printf 'done\\n' >> "$2" +fi +exit 0 +""" +_TIMEOUT_STUB = "#!/bin/sh\necho 30m\n" + +_BASH_DRIVER = r"""#!/usr/bin/env bash +set -euo pipefail +REPO="$1"; DIR="$2"; WT="$3"; ISSUE="99"; DRY=0 +export PATH="$4:$PATH" +export STUB_CALLS="$5" STUB_MODE="$6" + awk '/^run_node\(\)/{f=1} /^run_triage\(\)/{f=0} f' "$REPO/.factory/fix-issue.sh" > "$DIR/.fn-extract.sh" + source "$DIR/.fn-extract.sh" + run_node foo +""" + + +def _sandbox(tmp_path: Path, stub_mode: str) -> dict: + """tmp git 仓 + .factory 拷贝 + 无声明 foo.md + 桩 bin;返回路径/调用标记。""" + repo = tmp_path / "repo" + repo.mkdir() + env = dict(os.environ, GIT_CONFIG_GLOBAL="/dev/null", GIT_CONFIG_SYSTEM="/dev/null") + subprocess.run(["git", "init", "-q", str(repo)], check=True, env=env) + factory = repo / ".factory" + shutil.copytree(FACTORY, factory, + ignore=shutil.ignore_patterns("artifacts", "worktrees", "__pycache__", "locks")) + # 无 ARTIFACT 声明的测试节点 prompt(场景核心:grep 提取为空) + (factory / "prompts" / "foo.md").write_text( + "# foo 节点\n\n输出计划正文即可。\n", encoding="utf-8") + bindir = tmp_path / "bin" + bindir.mkdir() + calls = tmp_path / "calls.txt" + (bindir / "omp_node").write_text(_OMP_STUB, encoding="utf-8") + (bindir / "node_timeout").write_text(_TIMEOUT_STUB, encoding="utf-8") + for f in bindir.iterdir(): + f.chmod(0o755) + issue_dir = repo / "issue-99" + issue_dir.mkdir() + return {"repo": str(repo), "dir": str(issue_dir), "bin": str(bindir), + "calls": str(calls), "mode": stub_mode} + + +def _run_node(tmp_path: Path, stub_mode: str) -> subprocess.CompletedProcess: + s = _sandbox(tmp_path, stub_mode) + return subprocess.run( + ["bash", "-c", _BASH_DRIVER, "driver", + s["repo"], s["dir"], s["repo"], s["bin"], s["calls"], s["mode"]], + capture_output=True, text=True, timeout=60) + + +def _metrics(sandbox_dir: str) -> list[dict]: + p = Path(sandbox_dir) / "node-metrics.jsonl" + if not p.exists(): + return [] + return [json.loads(line) for line in p.read_text(encoding="utf-8").splitlines()] + + +def test_no_artifact_prompt_falls_back_to_stdout(tmp_path): + """场景 A:prompts 无声明 + log 含 ARTIFACT 行 → 回退通过,不 errexit 死。""" + r = _run_node(tmp_path, stub_mode="ok") + assert r.returncode == 0, f"errexit 杀进程或非预期失败:\n{r.stdout}\n{r.stderr}" + assert "节点 foo 未声明产物" not in r.stderr # 走了通过路径 + metrics = _metrics(str(tmp_path / "repo" / "issue-99")) + assert metrics and metrics[-1]["status"] == "ok", f"台账缺 ok:{metrics}" + + +def test_no_artifact_prompt_and_no_stdout_line_fails_closed(tmp_path): + """场景 B:prompts 无声明 + log 无 ARTIFACT 行 → no-artifact return 1。""" + r = _run_node(tmp_path, stub_mode="bare") + assert r.returncode == 1, f"应为 no-artifact 失败(1),实得 {r.returncode}:\n{r.stdout}\n{r.stderr}" + assert "未声明产物(缺 ARTIFACT 行)" in r.stderr + metrics = _metrics(str(tmp_path / "repo" / "issue-99")) + assert metrics and metrics[-1]["status"] == "no-artifact", f"台账缺 no-artifact:{metrics}" diff --git a/.factory/tests/test_breaker_wiring.py b/.factory/tests/test_breaker_wiring.py index 39c0696..fe3e5ac 100644 --- a/.factory/tests/test_breaker_wiring.py +++ b/.factory/tests/test_breaker_wiring.py @@ -26,8 +26,11 @@ FACTORY = Path(__file__).resolve().parents[1] -TRIP_MSG = ("成本熔断(R4):ledger 累计超 floor 上限,停止派发;" - "人工复核 locks/floor.json 与 ledger.jsonl 后方可恢复") +# D(2026-09-05):breaker.sh 不再固定包装"成本熔断 ledger 累计超 floor"误导 +# 文案,rc=3 时透传 factory_lib breaker_check 的 CircuitOpen 真实消息 +# ("熔断:今日已跑 N 次" / "熔断:连续失败 N 次…需人工介入")。 +RUNS_TRIP_MSG = "熔断:今日已跑 1 次(上限 1)" # TRIP_FLOOR + 1 条今日成功行 +STREAK_TRIP_MSG = "熔断:连续失败 3 次(上限 3),需人工介入" # FLOOR + 3 连败 FAIL_CLOSED_MSG = "fail-closed 停止派发" FLOOR = {"max_runs_per_day": 10, "max_consecutive_failures": 3} TRIP_FLOOR = {"max_runs_per_day": 1, "max_consecutive_failures": 3} @@ -85,7 +88,16 @@ def _run(cmd: list[str], repo: Path, tmp_path: Path, *, with_stubs: bool, env = {"PATH": path, "HOME": os.environ.get("HOME", "/tmp"), "GH_REPO": "sandbox/repo", "SENTINEL_MARK": str(tmp_path / "sentinel"), "STUB_CALLS": str(tmp_path / "calls"), + # 沙箱仓无 origin remote,fix-issue 基线拒猜 fail-closed(#133) + # 会在熔断接线前拦截——显式注入 env 级基线,保持测试聚焦接线序 + "FACTORY_BASE_BRANCH": "main", "GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_SYSTEM": "/dev/null"} + # TZ 透传:_e() 用本进程 date.today() 写台账,breaker 子进程用其自身 + # date.today() 判「今日」——两边时区不一致时(本机 shell TZ=UTC 而系统 + # 时区 Asia/Shanghai,UTC 16:00-24:00 窗口两时区跨日)台账日期对不上, + # 熔断误放行 rc=0(2026-09-08 实证 4 败;CI 全链 UTC 一致故绿) + if "TZ" in os.environ: + env["TZ"] = os.environ["TZ"] env |= (extra_env or {}) return subprocess.run(cmd, cwd=repo, env=env, capture_output=True, text=True, timeout=120) @@ -118,12 +130,12 @@ def test_daily_cap_trips_exit_3(self, tmp_path): r = self._gate(tmp_path, json.dumps({"max_runs_per_day": 1, "max_consecutive_failures": 3}), _e(0) + "\n") assert r.returncode == 3 - assert TRIP_MSG in r.stderr + assert RUNS_TRIP_MSG in r.stderr def test_streak_trips_exit_3(self, tmp_path): r = self._gate(tmp_path, json.dumps(FLOOR), "".join(_e(1) + "\n" for _ in range(3))) assert r.returncode == 3 - assert TRIP_MSG in r.stderr + assert STREAK_TRIP_MSG in r.stderr assert "连续失败" in r.stderr # factory_lib 判定明细先行 def test_pass_silent_exit_0(self, tmp_path): @@ -166,7 +178,7 @@ def _dispatch(self, tmp_path: Path, floor: str, ledger: str, extra_env=None): def test_trips_before_any_downstream(self, tmp_path): repo, r = self._dispatch(tmp_path, json.dumps(TRIP_FLOOR), _e(0) + "\n") assert r.returncode == 3 - assert TRIP_MSG in r.stderr + assert RUNS_TRIP_MSG in r.stderr # 死在门口:无 gh 数据面调用(仅启动时的 hosting auth 探测)、无下游脚本 gh_calls = [ln.rstrip() for ln in _stub_lines(tmp_path) if ln.startswith("gh ")] assert gh_calls == ["gh auth status"] # dispatch_main 的 hosting auth 探测(ADR-008) @@ -189,6 +201,16 @@ def test_passes_through_full_round(self, tmp_path): assert any("pr list" in ln for ln in _stub_lines(tmp_path)) # gh 数据面已触达 + def test_auth_failure_attaches_diagnosis(self, tmp_path): + """preflight auth 失败留痕(2026-09-05 02:00 事故):无 gh CLI → + exit 2 + stderr 附 auth_diagnose 诊断,日志可回溯环境/凭据归因。""" + repo = _sandbox(tmp_path, json.dumps(FLOOR), "") + r = _run(["bash", ".factory/dispatch.sh"], repo, tmp_path, with_stubs=False) + assert r.returncode == 2 + assert "托管平台不可用" in r.stderr + assert "hosting auth 诊断: gh CLI 不在 PATH" in r.stderr + assert "熔断" not in r.stderr + # ---- 3. fix-issue.sh:AI 节点/租约/锁之前 ---- class TestFixIssueWiring: @@ -200,7 +222,7 @@ def _fix(self, tmp_path: Path, floor: str, ledger: str, args=None): def test_trips_before_lock_and_gh(self, tmp_path): repo, r = self._fix(tmp_path, json.dumps(TRIP_FLOOR), _e(0) + "\n") assert r.returncode == 5 # 本地映射码(3 已被锁竞争占用,见接线注释) - assert TRIP_MSG in r.stderr + assert RUNS_TRIP_MSG in r.stderr # 连互斥锁都未占、未到 gh 探测 assert not (repo / ".factory/locks/dispatcher").exists() assert "需要 gh CLI" not in r.stderr @@ -254,7 +276,7 @@ def test_trips_before_triage_and_dispatch(self, tmp_path): repo, r = self._cron(tmp_path, json.dumps(TRIP_FLOOR), _e(0) + "\n") assert r.returncode == 3 log = (repo / ".factory/locks/dispatch.log").read_text(encoding="utf-8") - assert TRIP_MSG in log # 停摆信息随块重定向落 dispatch.log + assert RUNS_TRIP_MSG in log # 停摆信息随块重定向落 dispatch.log assert _sentinel_hit(tmp_path) == [] # triage-batch / dispatch 均未跑 def test_passes_through_to_both_callees(self, tmp_path): diff --git a/.factory/tests/test_dispatch.py b/.factory/tests/test_dispatch.py index 0f6c82a..62d24a6 100644 --- a/.factory/tests/test_dispatch.py +++ b/.factory/tests/test_dispatch.py @@ -221,21 +221,31 @@ def test_extract_slug_ssh_dot_github_host(self): ["ssh://git@ssh.github.com:443/im47cn/awesome-rules.git"] ) == "im47cn/awesome-rules" assert extract_slug(["ssh://ssh.github.com/o/r.git"]) == "o/r" - def test_extract_slug_github_wop_bot_alias(self): - """~/.ssh/config 的 github-wop-bot Host 别名(wop-platform 托管 - 机器事实配置):host 锚定须认别名——无 remote 重命名兜底(新 - checkout 即用)。2026-09-01:origin 别名 URL 曾被拒 → dispatch - exit 2 停摆(与 ssh.github.com 同族回归)。""" + def test_extract_slug_extra_host_env(self, monkeypatch): + """ADR-012:bot ssh Host 别名是机器事实配置(~/.ssh/config), + 经 FACTORY_SLUG_EXTRA_HOSTS 注入白名单(env 两级回退,与 PR #61 + 平台配置同模式)——公开仓零内部标识。2026-09-01 回归保真:别名 + URL 未被认曾致 dispatch exit 2 停摆,故配置注入路径必须有测。""" + monkeypatch.setenv("FACTORY_SLUG_EXTRA_HOSTS", "ci-bot-host") assert extract_slug( - ["git@github-wop-bot:wop-platform/wop-go-sdk.git"] - ) == "wop-platform/wop-go-sdk" - assert extract_slug(["git@github-wop-bot.com:o/r.git"]) == "" + ["git@ci-bot-host:some-org/xx-go-sdk.git"] + ) == "some-org/xx-go-sdk" + # 未配置时同 host 被拒:主机锚定防伪装语义不因配置缺省放宽 + monkeypatch.delenv("FACTORY_SLUG_EXTRA_HOSTS") + assert extract_slug(["git@ci-bot-host:some-org/xx-go-sdk.git"]) == "" + # 近似 host 不放宽(ci-bot-host.com 非 ehost.com) + monkeypatch.setenv("FACTORY_SLUG_EXTRA_HOSTS", "ehost.com") + assert extract_slug(["git@ehost.com.evil.io:o/r.git"]) == "" + # 非法 token(regex 注入面)fail-closed 抛错而非静默降级 + monkeypatch.setenv("FACTORY_SLUG_EXTRA_HOSTS", "a(b") + with pytest.raises(ValueError): + extract_slug(["git@github.com:o/r.git"]) def test_extract_slug_codeup_rejected(self): """Codeup(阿里云效)URL 不匹配 GitHub 锚定:未接入仓 fail-closed 空串,不得误解析为 GitHub slug(ADR-008 平台隔离)。""" assert extract_slug( - ["git@codeup.aliyun.com:000000000000000000000000/group/x.git"] + ["git@codeup.aliyun.com:610b3c9d86508f8da8b08436/gtsp/x.git"] ) == "" def test_extract_slug_spoof_hosts_rejected(self): @@ -253,10 +263,10 @@ def test_codeup_remote_derivation(self, monkeypatch): a = hosting_mod.CodeupAdapter(repo=".") cases = [ - ("https://codeup.aliyun.com/6ab/group/x.git", ("6ab", "group/x")), - ("https://codeup.aliyun.com/6ab/group/x", ("6ab", "group/x")), - ("ssh://git@codeup.aliyun.com:22/6ab/group/x.git", ("6ab", "group/x")), - ("git@codeup.aliyun.com:6ab/group/x.git", ("6ab", "group/x")), + ("https://codeup.aliyun.com/6ab/gtsp/x.git", ("6ab", "gtsp/x")), + ("https://codeup.aliyun.com/6ab/gtsp/x", ("6ab", "gtsp/x")), + ("ssh://git@codeup.aliyun.com:22/6ab/gtsp/x.git", ("6ab", "gtsp/x")), + ("git@codeup.aliyun.com:6ab/gtsp/x.git", ("6ab", "gtsp/x")), ("https://codeup.aliyun.com/only-org", (None, None)), ] for url, want in cases: @@ -278,8 +288,8 @@ def detect(url): lambda *args, **kw: SimpleNamespace(stdout=url + "\n", returncode=0)) return hosting_mod._detect_hosting(".") - assert detect("https://codeup.aliyun.com/6ab/group/x.git") == "codeup" - assert detect("git@codeup.aliyun.com:6ab/group/x.git") == "codeup" + assert detect("https://codeup.aliyun.com/6ab/gtsp/x.git") == "codeup" + assert detect("git@codeup.aliyun.com:6ab/gtsp/x.git") == "codeup" assert detect("https://codeup.aliyun.com.evil.com/6ab/x.git") == "github" assert detect("https://evil.com/codeup.aliyun.com/6ab/x.git") == "github" assert detect("https://github.com/o/r.git") == "github" diff --git a/.factory/tests/test_dispatch_liveness_stall.py b/.factory/tests/test_dispatch_liveness_stall.py new file mode 100644 index 0000000..bb59da0 --- /dev/null +++ b/.factory/tests/test_dispatch_liveness_stall.py @@ -0,0 +1,211 @@ +"""dispatch_liveness 滞留检测负控制(第三死法:GitHub 侧滞留)。 + +2026-09-15 issue #165 实证:零改动轮留守 in-progress 5 天不可见。 +本套件钉死三种滞留(in-progress 无租约 / rejected 无回执 / rejected +超宽限无跟进)必须进 problems(= 回归 FAIL),以及合法状态(链存活 +持锁 / 有回执宽限内 / hosting 不可用)不得误报。hosting 子进程一律 +monkeypatch,零网络零 git。 +""" +import json +import sys +import types +from datetime import datetime, timedelta, timezone +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "regression")) +import dispatch_liveness as dl + + +class _FakeRun: + def __init__(self, rc=0, stdout=""): + self.returncode = rc + self.stdout = stdout + self.stderr = "" + + +def _mk_repo(tmp_path: Path, locks: dict = None) -> Path: + """临时仓骨架:/.factory/{locks/leases,regression}""" + repo = tmp_path / "r" + (repo / ".factory" / "locks" / "leases").mkdir(parents=True) + (repo / ".factory" / "regression").mkdir(parents=True) + for name, content in (locks or {}).items(): + (repo / ".factory" / "locks" / "leases" / name).write_text(content) + return repo + + +def _patch_hosting(monkeypatch, issues=None, unavailable=False): + def fake_run(cmd, **kw): + assert kw.get("cwd"), "hosting.py 须以目标仓为 cwd 运行(slug 解析)" + if unavailable: + return _FakeRun(rc=1) + return _FakeRun(stdout=json.dumps(issues or [])) + monkeypatch.setattr(dl.subprocess, "run", fake_run) + + +def _iso(dt: datetime) -> str: + return dt.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + + +# -- in-progress 滞留:#165 事故形态 ---------------------------------------- + +def test_in_progress_without_lock_fails(tmp_path, monkeypatch, capsys): + _patch_hosting(monkeypatch, issues=[ + {"number": 165, "labels": ["factory:accepted", "factory:in-progress"], + "updatedAt": _iso(datetime.now(timezone.utc))}]) + repo = _mk_repo(tmp_path) # 无 issue:165.lock + problems = [] + assert dl.check_stalled_labels(repo, 7, problems) == "ok" + assert any("#165" in p and "in-progress" in p for p in problems), problems + + +def test_in_progress_with_live_lock_ok(tmp_path, monkeypatch, capsys): + _patch_hosting(monkeypatch, issues=[ + {"number": 165, "labels": ["factory:in-progress"], + "updatedAt": _iso(datetime.now(timezone.utc))}]) + repo = _mk_repo(tmp_path, locks={"issue:165.lock": "mid|9|123|0|900"}) + problems = [] + assert dl.check_stalled_labels(repo, 7, problems) == "ok" + assert not problems + + +def test_in_progress_stale_lock_fails(tmp_path, monkeypatch, capsys): + # 审查 minor #7:链被 SIGKILL 留下过期残锁——mtime+租期 < now 须判滞留 + import os + _patch_hosting(monkeypatch, issues=[ + {"number": 165, "labels": ["factory:in-progress"], + "updatedAt": _iso(datetime.now(timezone.utc))}]) + repo = _mk_repo(tmp_path, locks={"issue:165.lock": "mid|9|123|0|900"}) + lock = repo / ".factory" / "locks" / "leases" / "issue:165.lock" + old = datetime.now().timestamp() - 2000 + os.utime(lock, (old, old)) + problems = [] + assert dl.check_stalled_labels(repo, 7, problems) == "ok" + assert any("#165" in p for p in problems), "过期残锁被当活链(滞留漏报)" + + +def test_pg_mode_degrades_in_progress_check(tmp_path, monkeypatch, capsys): + # 审查 major #2:PG 形态租约在库、本地锁文件从不存在—— + # in-progress 检测必须整体降级,否则每个存活链每天误报滞留 + _patch_hosting(monkeypatch, issues=[ + {"number": 165, "labels": ["factory:in-progress"], + "updatedAt": _iso(datetime.now(timezone.utc))}]) + monkeypatch.setenv("SUPABASE_DB", "postgres://x") + repo = _mk_repo(tmp_path) # 无锁文件(PG 形态常态) + problems = [] + assert dl.check_stalled_labels(repo, 7, problems) == "note" + assert not problems + assert "PG" in capsys.readouterr().out + + +# -- rejected 滞留:宽限阈值 -------------------------------------------------- +RECEIPT_HEADING = "## 工厂 triage 裁决:reject —— 判据 b 不通过" +RECEIPT = (f"{RECEIPT_HEADING}\n" + "") + + +def test_rejected_beyond_grace_fails(tmp_path, monkeypatch, capsys): + old = datetime.now(timezone.utc) - timedelta(days=10) + _patch_hosting(monkeypatch, issues=[ + {"number": 104, "labels": ["factory:rejected"], + "updatedAt": _iso(old), + "comments": [{"author": "im47cn", "body": RECEIPT}]}]) + repo = _mk_repo(tmp_path) + problems = [] + assert dl.check_stalled_labels(repo, 7, problems) == "ok" + assert any("#104" in p and "rejected" in p for p in problems), problems + + +def test_rejected_within_grace_ok(tmp_path, monkeypatch, capsys): + recent = datetime.now(timezone.utc) - timedelta(days=2) + _patch_hosting(monkeypatch, issues=[ + {"number": 104, "labels": ["factory:rejected"], + "updatedAt": _iso(recent), + "comments": [{"author": "im47cn", "body": RECEIPT}]}]) + repo = _mk_repo(tmp_path) + problems = [] + assert dl.check_stalled_labels(repo, 7, problems) == "ok" + assert not problems + + +def test_rejected_missing_updatedat_skips(tmp_path, monkeypatch, capsys): + _patch_hosting(monkeypatch, issues=[ + {"number": 104, "labels": ["factory:rejected"], "updatedAt": None, + "comments": [{"author": "im47cn", "body": RECEIPT}]}]) + repo = _mk_repo(tmp_path) + problems = [] + dl.check_stalled_labels(repo, 7, problems) + assert not problems + +def test_rejected_no_receipt_fails_immediately(tmp_path, monkeypatch): + """#207 事故形态:只落标无回执——链完整性违规即时 FAIL,不进宽限 + 时钟(新鲜 updatedAt 否则宽限 OK),有回执才轮到滞留判定。""" + _patch_hosting(monkeypatch, issues=[ + {"number": 207, "labels": ["factory:rejected"], + "updatedAt": _iso(datetime.now(timezone.utc))}]) + repo = _mk_repo(tmp_path) + problems = [] + assert dl.check_stalled_labels(repo, 7, problems) == "ok" + assert any("#207" in p and "无回执" in p for p in problems), problems + assert all("滞留" not in p for p in problems), problems + + +def test_rejected_pre_receipt_comments_do_not_count(tmp_path, monkeypatch): + """提交讨论评论 ≠ 回执——回执判据 = 幂等 marker(issue_reject 评论 + 尾埋),判据与 rejected_reconcile 同源不得漂移。""" + _patch_hosting(monkeypatch, issues=[ + {"number": 104, "labels": ["factory:rejected"], + "updatedAt": _iso(datetime.now(timezone.utc)), + "comments": [{"author": "im47cn", "body": "按指引重投"}]}]) + repo = _mk_repo(tmp_path) + problems = [] + dl.check_stalled_labels(repo, 7, problems) + assert any("无回执" in p for p in problems), problems + +def test_rejected_human_quoted_heading_not_receipt(tmp_path, monkeypatch): + """PR #211 Sourcery 评论2:人工复述回执标题(无 marker)不算回执 + ——判据与 rejected_reconcile 同为幂等 marker,两层不得漂移。""" + _patch_hosting(monkeypatch, issues=[ + {"number": 104, "labels": ["factory:rejected"], + "updatedAt": _iso(datetime.now(timezone.utc)), + "comments": [{"author": "im47cn", "body": RECEIPT_HEADING}]}]) + repo = _mk_repo(tmp_path) + problems = [] + dl.check_stalled_labels(repo, 7, problems) + assert any("无回执" in p for p in problems), problems + + +# -- 降级:hosting 不可用不误报 ---------------------------------------------- + +def test_hosting_unavailable_degrades_to_note(tmp_path, monkeypatch, capsys): + _patch_hosting(monkeypatch, unavailable=True) + repo = _mk_repo(tmp_path) + problems = [] + assert dl.check_stalled_labels(repo, 7, problems) == "note" + assert not problems + assert "跳过" in capsys.readouterr().out + + +# -- 工具函数 ------------------------------------------------------------------ + +def test_updated_age_parses_and_rejects(): + now = datetime.now(timezone.utc) + assert dl._gh_updated_age_secs(_iso(now - timedelta(hours=1))) < 3700 + assert dl._gh_updated_age_secs(None) is None + assert dl._gh_updated_age_secs("not-a-date") is None + assert dl._gh_updated_age_secs("") is None + + +# -- 端到端:problems 非空 → main 退出码 1(负控制:滞留必须翻红回归)-------- + +def test_stall_drives_nonzero_exit(tmp_path, monkeypatch, capsys): + _patch_hosting(monkeypatch, issues=[ + {"number": 165, "labels": ["factory:in-progress"], + "updatedAt": _iso(datetime.now(timezone.utc))}]) + repo = _mk_repo(tmp_path) + monkeypatch.setattr(dl, "DEFAULT_REGISTRY", tmp_path / "none.conf") + monkeypatch.setattr(dl, "REPO", repo) # 无注册表 → 单仓自检指向夹具仓 + monkeypatch.delenv("SUPABASE_DB", raising=False) + monkeypatch.setattr(dl.sys, "argv", + ["dispatch_liveness.py", "--stale-days", "7"]) + rc = dl.main() + assert rc == 1, "滞留未驱动 FAIL(回归层对死信筒失明)" diff --git a/.factory/tests/test_downstream_check.py b/.factory/tests/test_downstream_check.py index 66ecaa8..481b97f 100644 --- a/.factory/tests/test_downstream_check.py +++ b/.factory/tests/test_downstream_check.py @@ -165,7 +165,7 @@ def test_apply_commit_levels_fleet(self, fleet): assert "[已追平] ../dn-drift" in proc.stdout assert "[干净] ../dn-clean" in proc.stdout assert _rev(dn_drift, "log", "-1", "--format=%s") \ - .startswith("factory: 上游同步追平(") + .startswith("chore(factory): 上游同步追平(") assert (dn_drift / ".git-blame-ignore-revs").exists() # 复查:全干净,且不产生新提交(空转链舰队级回归) n_drift = _rev(dn_drift, "rev-list", "--count", "HEAD") @@ -194,25 +194,48 @@ def test_missing_repo_path_error_continues(self, fleet): assert "[错误] ../nope" in proc.stderr assert "[干净] ../dn-clean" in proc.stdout, "单仓失败不中断其余巡检" +class TestADR012LocalManifest: + """ADR-012:真实清单住 downstream.local.json(gitignored),存在时 + 优先于 tracked 模板——公开中心仓零内部标识。""" + + def test_local_manifest_precedence(self, fleet): + center, dn_clean, _, _ = fleet + (center / ".factory/downstream.local.json").write_text( + json.dumps({"repos": [{"path": "../dn-clean"}]}, + ensure_ascii=False, indent=2), encoding="utf-8") + proc = _patrol(center) + assert proc.returncode == 0, proc.stdout + proc.stderr + assert "[干净] ../dn-clean" in proc.stdout + + def test_empty_tracked_template_fail_closed_with_guidance(self, fleet): + center, _, _, _ = fleet + (center / ".factory/downstream.json").write_text( + json.dumps({"repos": []}), encoding="utf-8") + proc = _patrol(center) + assert proc.returncode == 2, proc.stdout + proc.stderr + assert "downstream.local.json" in proc.stderr + + class TestPR120ReviewRegressions: """PR #120 审查评论回归:mktemp 暂存泄漏——trap 晚于 mktemp 安装。""" - def test_usage_error_leaks_no_temp_file(self, tmp_path): + def test_usage_error_leaks_no_temp_file(self, tmp_path, private_tmp): """review 1:用法错误(trap 安装前的早退路径)不得泄漏暂存文件—— 修复前 trap 装于清单检查与加锁之后,mktemp 至 trap 之间的 exit 2 泄漏 OUT_FILE。""" - tmp_root = os.environ.get("TMPDIR") or "/tmp" - before = set(glob.glob(f"{tmp_root}/.factory-downstream-check.*")) + env = git_env() + env["TMPDIR"] = str(private_tmp) # 泄漏断言隔离,见 conftest private_tmp + before = set(glob.glob(f"{private_tmp}/.factory-downstream-check.*")) proc = subprocess.run( ["bash", str(FACTORY / "downstream-check.sh"), "--frobnicate"], - cwd=tmp_path, env=git_env(), capture_output=True, text=True, + cwd=tmp_path, env=env, capture_output=True, text=True, ) assert proc.returncode == 2 assert "未知参数" in proc.stderr - after = set(glob.glob(f"{tmp_root}/.factory-downstream-check.*")) + after = set(glob.glob(f"{private_tmp}/.factory-downstream-check.*")) assert after - before == set(), "trap 安装前早退不得泄漏暂存(review 1)" - def test_lock_competition_keeps_foreign_lock(self, fleet, tmp_path): + def test_lock_competition_keeps_foreign_lock(self, fleet, private_tmp): """review 1 修正:他实例持锁时本实例 exit 0 退出,不得删除对方 锁文件——修复前 trap 提前并入 $LOCK,竞争退出时 rm 掉对方锁, 破坏互斥。活 PID 锁:content=本测试进程 PID(kill -0 存活)。""" @@ -220,15 +243,16 @@ def test_lock_competition_keeps_foreign_lock(self, fleet, tmp_path): lock = center / ".factory/locks/downstream-check.lock" lock.parent.mkdir(parents=True, exist_ok=True) lock.write_text(f"{os.getpid()}\n", encoding="utf-8") - tmp_root = os.environ.get("TMPDIR") or "/tmp" - before = set(glob.glob(f"{tmp_root}/.factory-downstream-check.*")) + env = git_env() + env["TMPDIR"] = str(private_tmp) # 泄漏断言隔离,见 conftest private_tmp + before = set(glob.glob(f"{private_tmp}/.factory-downstream-check.*")) proc = subprocess.run( ["bash", str(center / ".factory/downstream-check.sh")], - cwd=center, env=git_env(), capture_output=True, text=True, + cwd=center, env=env, capture_output=True, text=True, ) assert proc.returncode == 0 assert "巡检锁被持" in proc.stderr assert lock.exists(), "竞争退出不得删除对方锁(互斥)" assert lock.read_text(encoding="utf-8").strip() == str(os.getpid()) - after = set(glob.glob(f"{tmp_root}/.factory-downstream-check.*")) + after = set(glob.glob(f"{private_tmp}/.factory-downstream-check.*")) assert after - before == set(), "竞争退出仍须清自身暂存(review 1)" diff --git a/.factory/tests/test_factory_init.py b/.factory/tests/test_factory_init.py new file mode 100644 index 0000000..995ecfc --- /dev/null +++ b/.factory/tests/test_factory_init.py @@ -0,0 +1,391 @@ +"""test_factory_init —— tools/factory-init.py 接入 CLI 的 E2E 套件。 + +先行失败清单(2026-10-12,测试先于实现——本文件先红后绿): + +输入边界: + F1 init 目标路径不存在 → exit 2,零写入 + F2 init 目标非 git 仓 → exit 2 + F3 目标已有 .factory/ 且异源(MISSION 无生成标记)→ exit 2 拒绝 + F4 目标已有非工厂生成的 MISSION.md → exit 2 拒绝覆盖(数据丢失路径) + F5 --perimeter 含占位符 `<…>` → exit 2 + F6 --perimeter 路径在目标仓不存在 → exit 2 且列缺失清单 + F7 周界无目录条目 / 缺 G-02 载体(非 MISSION 周界文件)/ 缺 B-01 载体 + (周界外真实文档)→ exit 2,指明缺什么 + F8 未知子命令 / 缺参 → exit 2 + usage + F9 第一段幂等:重跑重生成骨架,字节不变,exit 1 + F10 doctor:注册表含注释/空行/坏路径 → 逐项 ✗ 报告不崩溃(exit 1 非 2) + +信任边界: + F11 doctor 零凭据值:plist 凭据只探存在性,stdout 不含值子串 + F12 零越界写:除注册表(--no-register 关断)外不写目标仓与上游仓之外; + 上游仓工作树跑完保持 porcelain 干净 + F13 生成配置自洽:第二段完成后 guard.self_check 真跑 rc=0 + (MISSION 与 factory-local.json 周界同源生成,结构性一致) + F14 .factory/ 与 MISSION.md 未在 --perimeter 中 → 自动补入并告知 + (治理不可自改的机械化前提) + F20 Sourcery 阻断回归:可伪造 MARKER + 自带 .factory/guard.py → + 第二段执行前须对上游 full 面验源(字节一致),不一致 exit 2 零执行 + +并发/重复: + F15 注册表注册幂等:第二段重跑(已完成态重证)不产生重复行 + +数据丢失: + F16 目标仓既有文件字节不变(full 面只写 .factory/,骨架只写 MISSION.md) + F17 skip 面零拷贝:上游 DISTRIBUTION.json / README.md / artifacts / locks / + 上游 defects.json 锚点(B-101 等)不出现在目标 + F18 验证失败(mutations 有缺陷注入失败)→ exit 2 且不注册、不建标签 + ——半成品不得宣告完成 + F19 骨架确定性:同输入两次生成字节一致(禁时间戳/随机) + +E2E: + E1 fixture 空仓两段式全流程真跑(拷贝 → 骨架 → 填充 → self_check → + mutations kill-rate 真跑)→ 产物对 golden 字节比对 + E2 可重跑命令 = 本文件:python3 -m pytest .factory/tests/test_factory_init.py + -o addopts= -q(重跑两次结果一致;产物内容确定性由 F19/E1 保证) + +分发约束:tools/ 不在 DISTRIBUTION full 面——下游仓 running 本文件时 +工具不存在,skipif(对齐 test_sourcery_gate 仓侧闸先例)。 +""" + +from __future__ import annotations + +import json +import os +import re +import subprocess +import sys +from pathlib import Path + +import pytest + +REPO_ROOT = Path(__file__).resolve().parents[2] +TOOL = REPO_ROOT / "tools" / "factory-init.py" + +if not TOOL.exists(): + # 模块级守卫:skipif 只护执行期,模块级 read_text 在收集期先炸(下游 + # 整目录收集即 error)——allow_module_level 整文件跳过 + pytest.skip("tools/factory-init.py 不在本仓(下游分发面)", allow_module_level=True) + +REGISTRY_ENV = "FACTORY_REGISTRY" +PLIST_ENV = "FACTORY_HUB_PLIST" + +LABELS = ("factory:accepted", "factory:rejected", "factory:needs-review", "factory:in-progress") + +# 生成标记值从工具源码提取(不硬编码副本,防双真相源漂移) +MARKER_VAL = re.search(r'MARKER = "([^"]+)"', TOOL.read_text(encoding="utf-8"))[1] + + +def run_tool(args: list[str], env_extra: dict[str, str] | None = None) -> subprocess.CompletedProcess: + env = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")} | (env_extra or {}) + return subprocess.run( + [sys.executable, str(TOOL), *args], + capture_output=True, text=True, env=env, timeout=600, + ) + + +def git(repo: Path, *args: str) -> subprocess.CompletedProcess: + return subprocess.run( + ["git", "-C", str(repo), *args], capture_output=True, text=True, + env={k: v for k, v in os.environ.items() if not k.startswith("GIT_")}, + ) + + +def make_fixture(tmp_path: Path, name: str = "fixture-repo") -> Path: + """空仓 + 最小业务文件(周界内外齐备:docs//src/ 在周界,CHANGELOG 在周界外)。""" + repo = tmp_path / name + (repo / "docs").mkdir(parents=True) + (repo / "src").mkdir() + (repo / "README.md").write_text(f"# {name}\n\n业务自述文件(周界外文档)。\n", encoding="utf-8") + (repo / "CHANGELOG.md").write_text(f"# {name} 变更日志\n\n- 0.1.0 初版。\n", encoding="utf-8") + (repo / "docs" / "spec.md").write_text("# 规范\n\n正文。\n", encoding="utf-8") + (repo / "src" / "main.py").write_text("def main() -> int:\n return 0\n", encoding="utf-8") + (repo / ".gitignore").write_text(".factory/locks/\n*.pyc\n", encoding="utf-8") + git(repo, "init", "-q") + git(repo, "add", "-A") + git(repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "init") + return repo + + +def base_env(tmp_path: Path) -> dict[str, str]: + return { + REGISTRY_ENV: str(tmp_path / "registry" / "repos.conf"), + PLIST_ENV: str(tmp_path / "empty.plist"), + } + + +PERIMETER_ARG = "docs/,src/,MISSION.md,.gitignore" + + +class TestInputBoundaries: + """F1-F9 输入边界。""" + + def test_f1_nonexistent_path(self, tmp_path): + r = run_tool(["init", str(tmp_path / "nope")], base_env(tmp_path)) + assert r.returncode == 2 + assert not (tmp_path / "nope").exists() + + def test_f2_not_git_repo(self, tmp_path): + plain = tmp_path / "plain" + plain.mkdir() + r = run_tool(["init", str(plain)], base_env(tmp_path)) + assert r.returncode == 2 + assert not (plain / ".factory").exists() + + def test_f3_existing_foreign_factory(self, tmp_path): + repo = make_fixture(tmp_path) + (repo / ".factory").mkdir() + (repo / ".factory" / "README.md").write_text("别人的安装\n", encoding="utf-8") + r = run_tool(["init", str(repo)], base_env(tmp_path)) + assert r.returncode == 2 + assert (repo / ".factory" / "README.md").read_text(encoding="utf-8") == "别人的安装\n" + + def test_f4_foreign_mission_refused(self, tmp_path): + repo = make_fixture(tmp_path) + (repo / "MISSION.md").write_text("# 人类手写的宪法\n", encoding="utf-8") + r = run_tool(["init", str(repo)], base_env(tmp_path)) + assert r.returncode == 2 + assert (repo / "MISSION.md").read_text(encoding="utf-8") == "# 人类手写的宪法\n" + + def test_f5_placeholder_perimeter(self, tmp_path): + repo = make_fixture(tmp_path) + r = run_tool(["init", str(repo), "--perimeter", "docs/,<待填写>"], base_env(tmp_path)) + assert r.returncode == 2 + + def test_f6_perimeter_path_missing(self, tmp_path): + repo = make_fixture(tmp_path) + r = run_tool(["init", str(repo), "--perimeter", "docs/,ghost-dir/"], base_env(tmp_path)) + assert r.returncode == 2 + assert "ghost-dir" in r.stdout + r.stderr + + def test_f7_missing_carriers(self, tmp_path): + # 无目录条目 → M-01/M-02 锚点不可派生 + repo = make_fixture(tmp_path) + r = run_tool(["init", str(repo), "--perimeter", "README.md"], base_env(tmp_path)) + assert r.returncode == 2 + # 全部周界文件都是 MISSION.md → G-02 无载体 + repo2 = make_fixture(tmp_path, "fixture-repo2") + r2 = run_tool(["init", str(repo2), "--perimeter", "MISSION.md"], base_env(tmp_path)) + assert r2.returncode == 2 + + def test_f8_usage(self, tmp_path): + assert run_tool([]).returncode == 2 + assert run_tool(["wat"]).returncode == 2 + assert run_tool(["init"]).returncode == 2 + + def test_f9_phase1_idempotent(self, tmp_path): + repo = make_fixture(tmp_path) + r1 = run_tool(["init", str(repo), "--upstream", "/fake/upstream"], base_env(tmp_path)) + assert r1.returncode == 1 + m1 = (repo / "MISSION.md").read_bytes() + f1 = (repo / ".factory" / "factory-local.json").read_bytes() + r2 = run_tool(["init", str(repo), "--upstream", "/fake/upstream"], base_env(tmp_path)) + assert r2.returncode == 1 + assert (repo / "MISSION.md").read_bytes() == m1 + assert (repo / ".factory" / "factory-local.json").read_bytes() == f1 + + def test_f10_doctor_bad_registry(self, tmp_path): + reg = tmp_path / "registry" / "repos.conf" + reg.parent.mkdir(parents=True) + reg.write_text("# 注释\n\n/not/a/repo\n", encoding="utf-8") + r = run_tool(["doctor"], {PLIST_ENV: str(tmp_path / "empty.plist"), REGISTRY_ENV: str(reg)}) + assert r.returncode == 1 # 有 ✗ 但自身未崩 + assert "/not/a/repo" in r.stdout + + +class TestTrustBoundaries: + """F11-F14 信任边界。""" + + def test_f11_doctor_never_prints_secret_values(self, tmp_path): + plist = tmp_path / "hub.plist" + secret = "pt-super-secret-token-value-xyz" + plist.write_text( + '\n' + f'EnvironmentVariablesYUNXIAO_ACCESS_TOKEN' + f'{secret}' + "\n", + encoding="utf-8", + ) + r = run_tool(["doctor"], base_env(tmp_path) | {PLIST_ENV: str(plist)}) + assert secret not in r.stdout + assert "YUNXIAO_ACCESS_TOKEN" in r.stdout # 键名可见,值不可见 + + def test_f12_no_write_outside_target(self, tmp_path): + repo = make_fixture(tmp_path) + before = git(REPO_ROOT, "status", "--porcelain").stdout + run_tool(["init", str(repo), "--upstream", "/fake/upstream", "--no-register"], + base_env(tmp_path)) + run_tool(["init", str(repo), "--perimeter", PERIMETER_ARG, "--no-labels", + "--upstream", "/fake/upstream", "--no-register"], base_env(tmp_path)) + after = git(REPO_ROOT, "status", "--porcelain").stdout + assert after == before # 上游仓零污染(本套件自身文件变更由 Git 层管辖) + + def test_f13_selfcheck_green_after_phase2(self, tmp_path): + repo = make_fixture(tmp_path) + run_tool(["init", str(repo), "--upstream", "/fake/upstream"], base_env(tmp_path)) + run_tool(["init", str(repo), "--perimeter", PERIMETER_ARG, "--no-labels", + "--upstream", "/fake/upstream", "--no-register"], base_env(tmp_path)) + chk = subprocess.run( + [sys.executable, "-c", + "import sys; sys.path.insert(0, '.factory'); import guard; guard.self_check(); print('OK')"], + capture_output=True, text=True, cwd=str(repo), + env={k: v for k, v in os.environ.items() if not k.startswith("GIT_")}, + ) + assert chk.returncode == 0, chk.stderr + + def test_f20_forgeable_marker_no_exec(self, tmp_path): + """Sourcery 阻断回归:可伪造的 MARKER + 自带 .factory/guard.py 不得被执行。 + 现有 .factory 与上游 full 面不一致(或缺失)→ exit 2,恶意代码零执行。""" + repo = make_fixture(tmp_path) + (repo / "MISSION.md").write_text( + f"# 宪法\n\n\n", encoding="utf-8") + attacker_factory = repo / ".factory" / "mutations" + attacker_factory.mkdir(parents=True) + # 拷真上游 guard.py 再注入恶意尾码——模拟攻击者在标记仓内预置执行体 + guard = repo / ".factory" / "guard.py" + guard.write_text( + (REPO_ROOT / ".factory" / "guard.py").read_text(encoding="utf-8") + + "\nimport pathlib; pathlib.Path('PWNED').write_text('x')\n", + encoding="utf-8") + r = run_tool(["init", str(repo), "--perimeter", "docs/,docs/spec.md,MISSION.md", + "--no-labels", "--no-register", "--upstream", "/fake/upstream"], + base_env(tmp_path)) + assert r.returncode == 2, r.stdout + r.stderr + assert not (repo / "PWNED").exists() + + def test_f14_auto_ensure_governance_perimeter(self, tmp_path): + repo = make_fixture(tmp_path) + run_tool(["init", str(repo), "--perimeter", PERIMETER_ARG, "--no-labels", + "--upstream", "/fake/upstream", "--no-register"], base_env(tmp_path)) + mission = (repo / "MISSION.md").read_text(encoding="utf-8") + assert "- `MISSION.md`" in mission + assert "- `.factory/`" in mission + + +class TestConcurrencyAndDataLoss: + """F15-F19 并发重复与数据丢失。""" + + def _complete(self, repo: Path, tmp_path: Path) -> subprocess.CompletedProcess: + return run_tool( + ["init", str(repo), "--perimeter", PERIMETER_ARG, "--no-labels", + "--upstream", "/fake/upstream", "--no-register"], + {**base_env(tmp_path), REGISTRY_ENV: str(tmp_path / "reg" / "repos.conf")}, + ) + + def test_f15_registry_idempotent(self, tmp_path): + repo = make_fixture(tmp_path) + env = {**base_env(tmp_path), REGISTRY_ENV: str(tmp_path / "reg" / "repos.conf")} + run_tool(["init", str(repo), "--perimeter", PERIMETER_ARG, "--no-labels", + "--upstream", "/fake/upstream"], env) + run_tool(["init", str(repo), "--perimeter", PERIMETER_ARG, "--no-labels", + "--upstream", "/fake/upstream"], env) + lines = (tmp_path / "reg" / "repos.conf").read_text(encoding="utf-8").splitlines() + assert len([ln for ln in lines if ln.strip()]) == 1 + + def test_f16_existing_files_untouched(self, tmp_path): + repo = make_fixture(tmp_path) + before = { + p: p.read_bytes() + for p in (repo / "README.md", repo / "CHANGELOG.md", repo / "docs" / "spec.md", + repo / "src" / "main.py", repo / ".gitignore") + } + self._complete(repo, tmp_path) + for p, content in before.items(): + assert p.read_bytes() == content, p + + def test_f17_skip_face_not_copied(self, tmp_path): + repo = make_fixture(tmp_path) + self._complete(repo, tmp_path) + f = repo / ".factory" + assert not (f / "DISTRIBUTION.json").exists() + assert not (f / "README.md").exists() + assert not (f / "artifacts").exists() + assert not (f / "locks").exists() + defects = json.loads((f / "mutations" / "defects.json").read_text(encoding="utf-8")) + ids = {d["id"] for d in defects["defects"]} + assert "B-101" not in ids # 上游仓特定锚点不得随拷贝流入 + + def test_f18_gate_fail_no_register(self, tmp_path): + # 重复周界条目 → MISSION 行重复 → M-01/M-02 锚点 count≠1 → + # mutations FAIL-config → exit 2 且不注册(锚点唯一性由门收口, + # 锚点扫描修复后 B-01 侧不再有廉价触发器) + repo = make_fixture(tmp_path) + env = {**base_env(tmp_path), REGISTRY_ENV: str(tmp_path / "reg2" / "repos.conf")} + r = run_tool(["init", str(repo), "--perimeter", "docs/,docs/,MISSION.md,.gitignore", + "--no-labels", "--upstream", "/fake/upstream"], env) + assert r.returncode == 2 + assert not (tmp_path / "reg2").exists() + + def test_f19b_frontmatter_anchor(self, tmp_path): + """真实仓试跑回归(archify DESIGN.md 首行 `---` ×2):锚点派生须 + 跳过非唯一行,取前 scan 行中恰好出现一次的行。""" + repo = tmp_path / "fm-repo" + (repo / "docs").mkdir(parents=True) + (repo / "README.md").write_text("# fm-repo\n\n自述。\n", encoding="utf-8") + (repo / "docs" / "spec.md").write_text( + "---\ntitle: 规范\n---\n\n# 规范\n\n正文。\n", encoding="utf-8") + git(repo, "init", "-q") + git(repo, "add", "-A") + git(repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "init") + r = run_tool(["init", str(repo), "--perimeter", "docs/,docs/spec.md,MISSION.md", + "--no-labels", "--no-register", + "--upstream", "/fake/upstream"], base_env(tmp_path)) + assert r.returncode == 0, r.stdout + r.stderr + defects = json.loads( + (repo / ".factory" / "mutations" / "defects.json").read_text(encoding="utf-8")) + g02 = next(d for d in defects["defects"] if d["id"] == "G-02") + assert g02["find"] != "---" # front matter 分隔符(×2 非唯一)不得当选 + + def test_f19_skeleton_deterministic(self, tmp_path): + repo_a = make_fixture(tmp_path, "det-a") + repo_b = make_fixture(tmp_path, "det-b") + run_tool(["init", str(repo_a), "--upstream", "/fake/upstream"], base_env(tmp_path)) + run_tool(["init", str(repo_b), "--upstream", "/fake/upstream"], base_env(tmp_path)) + # 仓名不同属预期差异:改名后比对 + a = (repo_a / "MISSION.md").read_text(encoding="utf-8").replace("det-a", "X") + b = (repo_b / "MISSION.md").read_text(encoding="utf-8").replace("det-b", "X") + assert a == b + fa = (repo_a / ".factory" / "factory-local.json").read_text(encoding="utf-8").replace("det-a", "X") + fb = (repo_b / ".factory" / "factory-local.json").read_text(encoding="utf-8").replace("det-b", "X") + assert fa == fb + + +class TestGoldenE2E: + """E1 两段式全流程 + golden 字节比对(确定性产物锁存)。""" + + GOLDEN = Path(__file__).resolve().parent / "golden" / "factory-init" + + def test_e1_full_flow_golden(self, tmp_path): + repo = make_fixture(tmp_path) + r1 = run_tool(["init", str(repo), "--upstream", "/fake/upstream"], base_env(tmp_path)) + assert r1.returncode == 1 + phase1_mission = (repo / "MISSION.md").read_bytes() + r2 = run_tool(["init", str(repo), "--perimeter", PERIMETER_ARG, "--no-labels", + "--upstream", "/fake/upstream", "--no-register"], base_env(tmp_path)) + assert r2.returncode == 0, r2.stdout + r2.stderr + + pairs = [ + ("mission-phase1.md", phase1_mission), + ("MISSION.md", (repo / "MISSION.md").read_bytes()), + ("factory-local.json", (repo / ".factory" / "factory-local.json").read_bytes()), + ("defects.json", (repo / ".factory" / "mutations" / "defects.json").read_bytes()), + ] + for golden_name, actual in pairs: + golden = self.GOLDEN / golden_name + if golden.exists(): + assert actual == golden.read_bytes(), f"golden 漂移: {golden_name}" + else: + golden.parent.mkdir(parents=True, exist_ok=True) + golden.write_bytes(actual) + pytest.fail(f"golden 缺失已生成待人工审核: {golden}") + + # kill rate 实证:evidence-stamp 绑定要求 factory-local.json 已跟踪 + # (perimeter_blob 走 git ls-files——未跟踪不写 stamp 是诚实语义)。 + # 落库后重跑第二段 = 已完成态全量重证(顺带覆盖幂等重证路径)。 + git(repo, "add", "-A") + git(repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "s0") + r3 = run_tool(["init", str(repo), "--perimeter", PERIMETER_ARG, "--no-labels", + "--upstream", "/fake/upstream", "--no-register"], base_env(tmp_path)) + assert r3.returncode == 0, r3.stdout + r3.stderr + stamp = json.loads( + (repo / ".factory" / "mutations" / "evidence-stamp.json").read_text(encoding="utf-8")) + assert stamp.get("perimeter_blob") diff --git a/.factory/tests/test_factory_lib.py b/.factory/tests/test_factory_lib.py index 62b5fd2..b8c0e2f 100644 --- a/.factory/tests/test_factory_lib.py +++ b/.factory/tests/test_factory_lib.py @@ -82,6 +82,65 @@ def test_parse_multiline_nested_braces(self): d = parse_agent_json(text, self.VERDICTS) assert d["verdict"] == "PASS" + def test_parse_duplicated_json_blobs(self): + """#207 首次尝试实证崩形:裁决 JSON 被整段重复(Extra data: + char 429)。旧贪心兜底从首个左花括号拼到末个右花括号再 + json.loads 必炸;逐偏移 raw_decode 取首个完整对象。""" + blob = '{"verdict": "reject", "reasons": ["判据b: 不通过"]}' + d = parse_agent_json(blob + blob, {"accept", "reject"}) + assert d["verdict"] == "reject" + + def test_parse_trailing_prose_with_braces(self): + """裸 JSON 后跟含花括号尾文(fence 丢失形态)——旧贪心兜底 + 同样把尾文花括号拼进来;逐偏移扫描在首个合法对象处停。""" + text = ('{"verdict": "FAIL", "evidence": "x"}\n' + '附注:详见 {附录A} 与 {附录B}') + assert parse_agent_json(text, self.VERDICTS)["verdict"] == "FAIL" + + def test_parse_fence_priority_over_earlier_bare(self): + """fence 内对象优先于正文更早出现的裸对象——fence 是 LLM + 显式结构化输出信号(#207 重写后保序语义锚)。""" + text = ('{"verdict": "PASS", "evidence": "正文里的裸对象"}\n' + '```json\n{"verdict": "FAIL", "evidence": "fence 裁决"}\n```') + assert parse_agent_json(text, self.VERDICTS)["verdict"] == "FAIL" + + def test_parse_nested_verdict_not_accepted(self): + """PR #211 Sourcery 评论1:外层 verdict 非法时,嵌套对象携带的 + 合法 verdict 不具裁决资格——顶层 fail-closed 契约(防坏裁决借 + evidence/元数据嵌套混入链),整对象跳过后 ValueError。""" + text = '{"verdict": "MAYBE", "evidence": {"verdict": "reject"}}' + with pytest.raises(ValueError, match="verdict"): + parse_agent_json(text, {"accept", "reject"}) + + def test_parse_disallowed_outer_then_valid_sibling_recovers(self): + """坏 verdict 顶层对象被整体跳过后继续扫后续顶层——多对象恢复 + 不因嵌套封堵回退(重复块恢复的邻接形态)。""" + text = '{"verdict": "MAYBE"} {"verdict": "reject"}' + assert parse_agent_json(text, {"accept", "reject"})["verdict"] == "reject" + + def test_parse_all_fences_scanned_before_bare(self): + """PR #211 CodeRabbit 评论1:fence 优先 = 穷尽全部 fence。首个 + fence 裁决非法时,正文更早出现的合法裸对象不得抢先后位合法 + fence——fence 是更强的结构化输出信号,优先级须穷尽兑现。""" + text = ('{"verdict": "PASS", "evidence": "正文裸对象"}\n' + '```json\n{"verdict": "MAYBE"}\n```\n' + '```json\n{"verdict": "FAIL", "evidence": "后位 fence"}\n```') + assert parse_agent_json(text, self.VERDICTS)["verdict"] == "FAIL" + + def test_parse_unclosed_outer_nested_verdict_rejected(self): + """PR #211 CodeRabbit 评论2:外层对象未闭合时,解码失败不得 + 落到嵌套 `{` 接受其 verdict——坏对象按字符串感知平衡范围整体 + 跳过(未闭合则跳到输入末尾),fail-closed。""" + text = '{"verdict": "MAYBE", "evidence": {"verdict": "reject"}' + with pytest.raises(ValueError, match="verdict"): + parse_agent_json(text, {"accept", "reject"}) + + def test_parse_failed_prose_brace_skips_balanced_range(self): + """解码失败的散文花括号按平衡范围跳过(未闭合吞到末尾的对偶 + 边界):`{附录A}` 平衡闭合后,其后合法顶层对象仍可恢复。""" + text = '附注 {附录A}\n{"verdict": "FAIL", "evidence": "x"}' + assert parse_agent_json(text, self.VERDICTS)["verdict"] == "FAIL" + class TestEvidenceSuites: def test_skills_change_yields_suite(self): @@ -160,17 +219,34 @@ def test_adjudicators_get_tight_budget(self): def test_implement_gets_full_budget(self): from factory_lib import node_timeout - assert node_timeout("implement") == "30m" + # 2026-09-04 重校准:ok-run P95 1534s × 1.2 = 1840.8s → 31m + assert node_timeout("implement") == "31m" + + def test_review_recalibrated_30m(self): + from factory_lib import node_timeout + # 2026-09-04:P95 845s×1.2 → 17m;2026-09-10:17m 撞顶(#165 r2/r3 1026/1021s 败)→ 30m + assert node_timeout("review") == "30m" def test_unknown_node_defaults_15m(self): from factory_lib import node_timeout assert node_timeout("mystery") == "15m" + def test_prime_plan_raised_20m(self): + from factory_lib import node_timeout + # 2026-09-10:prime 900s 撞顶(#165 r6)、plan 901s 压线(r4)→ 各升至 20m + assert node_timeout("prime") == "20m" + assert node_timeout("plan") == "20m" + def test_per_node_env_override_wins(self): from factory_lib import node_timeout env = {"FACTORY_TIMEOUT_IMPLEMENT": "45m", "FACTORY_TIMEOUT": "9m"} assert node_timeout("implement", env) == "45m" + def test_process_env_used_when_env_omitted(self, monkeypatch): + from factory_lib import node_timeout + monkeypatch.setenv("FACTORY_TIMEOUT_REVIEW", "42m") + assert node_timeout("review") == "42m" + def test_global_env_fallback(self): from factory_lib import node_timeout assert node_timeout("plan", {"FACTORY_TIMEOUT": "9m"}) == "9m" @@ -514,3 +590,34 @@ def test_main_subcommand_prints_command(self, capsys, monkeypatch): {"final_gate_cmd": "python3 tools/final_gate.py"}) assert factory_lib.main(["factory_lib.py", "final-gate"]) == 0 assert capsys.readouterr().out == "python3 tools/final_gate.py\n" + +class TestSuitesNulOutput: + """suites 子命令 NUL 分隔(PR #116 CodeRabbit):套件名可含空格 + (skills/doc review/…),换行分隔 + shell for 词拆分($(…) 去换行 + 按 IFS 切)会把空格名拆碎、消费端静默跳过证据段;\0 让 + fix-issue.sh/validate-pr.sh 的 read -d '' 逐条保真取回。""" + + def test_space_in_suite_name_survives(self): + """带空格目录名产出带空格套件名——NUL 形态的成因锚点:若实现 + 按空白/换行切分即碎名(静默丢证据段)。""" + assert evidence_suites(["skills/doc review/scripts/g.py"]) == [ + "skills/doc review/scripts" + ] + + def test_single_suite_nul_terminated_no_newline(self, capsys): + """单套件:输出恰一个 NUL 结尾条目、零换行——词拆分形态在此 + 碎名/多词,read -d '' 收敛单条。""" + assert factory_lib.main(["factory_lib.py", "suites", + "skills/api-guard/scripts/api_check.py"]) == 0 + assert capsys.readouterr().out == "skills/api-guard/scripts\0" + + def test_multi_suite_roundtrip_read_d(self, capsys): + """多条目 + 空格名:复刻消费端 read -d '' 解析逐条保真(回归 + fix-issue.sh/validate-pr.sh 消费协议,PR #116 B)。""" + files = ["skills/api-guard/scripts/a.py", + "skills/doc review/scripts/g.py"] + assert factory_lib.main(["factory_lib.py", "suites", *files]) == 0 + raw = capsys.readouterr().out + assert raw.endswith("\0") + assert raw[:-1].split("\0") == [ + "skills/api-guard/scripts", "skills/doc review/scripts"] diff --git a/.factory/tests/test_feedback_ledger_anchor.py b/.factory/tests/test_feedback_ledger_anchor.py new file mode 100644 index 0000000..42ea1ec --- /dev/null +++ b/.factory/tests/test_feedback_ledger_anchor.py @@ -0,0 +1,120 @@ +"""账本主树锚定回归(源仓#73):--ledger 覆盖默认跟随路径。 + +失效模式在「账本路径跟随被执行副本」——链 worktree 中 __file__ 落链树, +账本随链分支删除即丢,collect_pending 回潮重复开 PR。CLI 路径真跑 +(gitenv 密闭),生产布局 = feedback.py 随仓分发进夹具;锚定表达式与 +§9 接线沿用 backstop 测试哲学:正则提取分发脚本真实块(改脚本不改 +测试即红),不做逻辑复制。运行:python3 -m pytest .factory/tests -q +""" +import json +import re +import subprocess +from pathlib import Path + +from gitenv import git_env + +FACTORY = Path(__file__).resolve().parents[1] + +# feedback-upstream.sh 头部锚定三连赋值整体块(rev-parse 求值 + 判空拼 +# 后缀 + 判空回退;值内无双引号,跨行匹配到收尾引号) +_ANCHOR = re.search( + r'MAIN_FACTORY="\$\(git rev-parse[^"]+"\nMAIN_FACTORY=[^\n]+\nMAIN_FACTORY=[^\n]+', + (FACTORY / "feedback-upstream.sh").read_text(encoding="utf-8"), +)[0] + + +def _git(cwd: Path, *args: str) -> str: + return subprocess.run(["git", *args], cwd=cwd, env=git_env(), + check=True, capture_output=True, text=True).stdout + + +def _mk_repo(tmp: Path) -> Path: + """生产布局夹具:feedback.py + factory-local.json 随仓提交。import 期 + fail-closed 读 port_point(移植首提 SHA,反哺扫描下界)——先种子提交 + 再记其 SHA,两步消解「首提内记录自身」自指;移植提交即待反哺候选 + (trailer 标记,HEAD)。""" + repo = tmp / "repo" + (repo / ".factory").mkdir(parents=True) + (repo / ".factory" / "feedback.py").write_text( + (FACTORY / "feedback.py").read_text(encoding="utf-8"), encoding="utf-8") + env = git_env() + for args in (("init", "-q", "-b", "main"), ("config", "user.email", "t@t"), + ("config", "user.name", "t")): + subprocess.run(["git", *args], cwd=repo, env=env, check=True, + capture_output=True) + for path, text in ((repo / "seed.txt", "s\n"),): + path.write_text(text, encoding="utf-8") + for args in (("add", "-A"), ("commit", "-q", "-m", "seed")): + subprocess.run(["git", *args], cwd=repo, env=env, check=True, + capture_output=True) + (repo / ".factory" / "factory-local.json").write_text(json.dumps( + {"upstream_repo": "o/r", "port_point": _sha(repo)}), encoding="utf-8") + for args in (("add", "-A"), + ("commit", "-q", "-m", "port factory\n\nUpstream-Feedback: yes")): + subprocess.run(["git", *args], cwd=repo, env=env, check=True, + capture_output=True) + return repo + + +def _sha(repo: Path) -> str: + return _git(repo, "rev-parse", "HEAD").strip() + + +def _py(cwd: Path, *args: str) -> str: + return subprocess.run(["python3", ".factory/feedback.py", *args], + cwd=cwd, env=git_env(), check=True, + capture_output=True, text=True).stdout + + +def test_record_from_chain_wtree_writes_main_tree_ledger(tmp_path): + repo = _mk_repo(tmp_path) + sha = _sha(repo) + assert sha in _py(repo, "pending") # 前提:未记账时候选在列 + _git(repo, "worktree", "add", "-q", "-b", "chain", str(tmp_path / "chain")) + chain = tmp_path / "chain" + main_ledger = repo / ".factory" / "feedback-log.jsonl" + _py(chain, "record", "https://x/p/1", f"{sha}:subj", + "--ledger", str(main_ledger)) + # 账本落主树、链树无副本 → 删链分支不丢账本 + assert main_ledger.exists() + assert not (chain / ".factory" / "feedback-log.jsonl").exists() + _git(repo, "worktree", "remove", "--force", str(chain)) + _git(repo, "branch", "-qD", "chain") + # 链分支已删仍不回潮:账本在主树生效(issue 验收 1) + assert sha not in _py(repo, "pending") + + +def test_record_twice_idempotent_for_pending(tmp_path): + repo = _mk_repo(tmp_path) + sha = _sha(repo) + main_ledger = repo / ".factory" / "feedback-log.jsonl" + for _ in range(2): + _py(repo, "record", "https://x/p/1", f"{sha}:subj", + "--ledger", str(main_ledger)) + # 双记账按 SHA 集合去重,无双计数回潮(issue 验收 2) + assert sha not in _py(repo, "pending", "--ledger", str(main_ledger)) + + +def test_anchor_expression_resolves_main_tree(tmp_path): + # 提取分发脚本真实锚定块(含判空拼接/回退两行),在链 worktree cwd + # 求值 → 主树 .factory + repo = _mk_repo(tmp_path) + _git(repo, "worktree", "add", "-q", "-b", "chain", str(tmp_path / "chain")) + out = subprocess.run(["bash", "-c", f'{_ANCHOR}; printf %s "$MAIN_FACTORY"'], + cwd=tmp_path / "chain", env=git_env(), check=True, + capture_output=True, text=True).stdout + assert out == str(repo / ".factory") + + +def test_s9_wiring_anchored_commit_with_guards(): + """接线钉(grep 级):六 py 调用点全锚定(F3:删一处 --ledger 全套 + 仍绿即病态绿)、§9 提交锚 MAIN_WT + 双守卫 + pathspec + add -f + (F1:ignored 裸 add 退出 1)+ 判空回退接线(F2)。""" + src = (FACTORY / "feedback-upstream.sh").read_text(encoding="utf-8") + assert src.count('--ledger "$LEDGER"') == 6 # 含 adapt-prep 惰性路径(F4) + assert "add -f .factory/feedback-log.jsonl" in src + assert 'git -C "$MAIN_WT" commit' in src + assert "-- .factory/feedback-log.jsonl" in src + assert "symbolic-ref --short HEAD" in src + assert "locks/dispatcher" in src + assert "MAIN_FACTORY:-$FACTORY" in src diff --git a/.factory/tests/test_fix_issue_base_branch.py b/.factory/tests/test_fix_issue_base_branch.py new file mode 100644 index 0000000..1d9970a --- /dev/null +++ b/.factory/tests/test_fix_issue_base_branch.py @@ -0,0 +1,116 @@ +"""BASE_BRANCH 两级解析(issue #133)——提取真实块在沙箱执行。 + +原固定回退 main 绕过 hosting.py 的 Codeup 拒猜不变量(master 仓静默落错 +基线)。三态钉死:env 显式优先 / origin/HEAD 实际默认分支 / 两者皆无 +fail-closed。与 trap 测试同哲学:不复制逻辑,正则提取真实块(改脚本不 +改测试即红)。 + +解析块现有两份逐字拷贝(fix-issue.sh / upstream-sync-check.sh,后者为 +#133 收口遗留补齐)——按脚本 parametrize 提取,钉住拷贝等价:任一侧 +漂移(丢 2>/dev/null、改 sed)即红。 +""" + +import re +import subprocess +import tempfile +from pathlib import Path + +import pytest + +from gitenv import git_env + +FACTORY = Path(__file__).resolve().parents[1] + +_SCRIPTS = ["fix-issue.sh", "upstream-sync-check.sh"] + +_BLOCK_RE = re.compile( + r'\n(BASE_BRANCH="\$\{FACTORY_BASE_BRANCH:-\}"[^\n]*\n.*?\[ -n "\$\{BASE_BRANCH\}" \] \|\|[^\n]*\n)', re.S) + + +def _block(script: str) -> str: + return _BLOCK_RE.search( + (FACTORY / script).read_text(encoding="utf-8"))[1] + +_SANDBOX = r"""#!/usr/bin/env bash +set -euo pipefail +REPO="__REPO__" +__BLOCK__ +echo "RESOLVED=${BASE_BRANCH}" +""" + + +def _env(): + env = git_env() + env.pop("FACTORY_BASE_BRANCH", None) + return env + + +def _run(repo: Path, script: str, extra_env=None): + with tempfile.TemporaryDirectory() as sh_dir: + sh = Path(sh_dir) / "sandbox.sh" + sh.write_text(_SANDBOX.replace("__REPO__", str(repo)) + .replace("__BLOCK__", _block(script))) + env = _env() + env.update(extra_env or {}) + return subprocess.run(["/bin/bash", str(sh)], env=env, + capture_output=True, text=True) + + +def _mk_clone(tmp: Path, default_branch: str) -> Path: + """clone 自带提交的仓(默认分支可指定),origin/HEAD 由 clone 设置。""" + seed = tmp / "seed" + seed.mkdir() + env = git_env() + (seed / "README.md").write_text("# t\n", encoding="utf-8") + for args in (("init", "-q", "-b", default_branch), + ("config", "user.email", "t@t"), + ("config", "user.name", "t"), ("add", "-A"), + ("commit", "-q", "-m", "init")): + subprocess.run(["git", *args], cwd=seed, env=env, check=True, + capture_output=True) + # 空 bare 仓 clone 不设 origin/HEAD(实测 fatal: not a symbolic ref), + # 须经 --bare 带提交克隆后再 clone + subprocess.run(["git", "clone", "-q", "--bare", str(seed), + str(tmp / "up.git")], env=env, check=True, + capture_output=True) + subprocess.run(["git", "clone", "-q", str(tmp / "up.git"), str(tmp / "dn")], + env=env, check=True, capture_output=True) + return tmp / "dn" + + +def test_blocks_are_identical(): + # 拷贝等价不变量显式钉死(Sourcery PR #256):行为测试只钉三态语义, + # 行为等价的文本漂移(如 sed 表达式微调)须靠逐字相等断言拦截 + assert _block("fix-issue.sh") == _block("upstream-sync-check.sh") + + +@pytest.mark.parametrize("script", _SCRIPTS) +def test_env_override_wins(tmp_path, script): + dn = _mk_clone(tmp_path, "main") + r = _run(dn, script, {"FACTORY_BASE_BRANCH": "custom-base"}) + assert r.returncode == 0 + assert "RESOLVED=custom-base" in r.stdout + + +@pytest.mark.parametrize("script", _SCRIPTS) +def test_origin_head_master_repo_resolves_master(tmp_path, script): + """Codeup master 仓场景(#133 核心):无 env 时读实际默认分支而非 main。""" + dn = _mk_clone(tmp_path, "master") + r = _run(dn, script) + assert r.returncode == 0 + assert "RESOLVED=master" in r.stdout + + +@pytest.mark.parametrize("script", _SCRIPTS) +def test_no_origin_no_env_fail_closed(tmp_path, script): + """裸仓(无 origin/HEAD)且无 env:拒猜基线 fail-closed(issue #133)。""" + lone = tmp_path / "lone" + lone.mkdir() + env = git_env() + for args in (("init", "-q", "-b", "main"), ("config", "user.email", "t@t"), + ("config", "user.name", "t")): + subprocess.run(["git", *args], cwd=lone, env=env, check=True, + capture_output=True) + r = _run(lone, script) + assert r.returncode == 2 + assert "issue #133" in r.stderr diff --git a/.factory/tests/test_fix_issue_pr_body.py b/.factory/tests/test_fix_issue_pr_body.py index 42d81e8..e92dc14 100644 --- a/.factory/tests/test_fix_issue_pr_body.py +++ b/.factory/tests/test_fix_issue_pr_body.py @@ -1,7 +1,7 @@ """fix-issue.sh 建 PR 正文须含 Closes #${ISSUE}(链约定,state.py _linked_issue 消费)。 回归锚:d6e6b57「PR 评审修复波」误删 --body-file 行 → PR 无正文、 -合并后 issue 不自动关(CodeRabbit wop-skills#14 实锤,正文原文经 git +合并后 issue 不自动关(CodeRabbit xx-skills#14 实锤,正文原文经 git 历史还原)。整链 mock 建 PR 成本高于收益,此守卫以源文本锚定最小 契约面(脚本即执行计划):body 行丢失即测试失败。 """ diff --git a/.factory/tests/test_fix_issue_trap.py b/.factory/tests/test_fix_issue_trap.py index c1a1b24..169229a 100644 --- a/.factory/tests/test_fix_issue_trap.py +++ b/.factory/tests/test_fix_issue_trap.py @@ -32,6 +32,9 @@ WR_RC=__WR_RC__ PUSH_RC=__PUSH_RC__ COUNT=__COUNT__ +ROUND=7 +DIR="__DIR__" +salvage_wip() { echo "salvage_wip:$*" >> "$CALLS"; } write_ledger() { echo "ledger:$1" >> "$CALLS"; return "$WR_RC"; } git() { echo "git:$*" >> "$CALLS" @@ -49,16 +52,19 @@ def _run_trap(rc, wr_rc=0, push_rc=0, count="3"): - """跑真实 trap 载荷一次,返回 (calls 行列表, LOCKDIR 是否已释放)。""" + """跑真实 trap 载荷一次,返回 (calls 行列表, LOCKDIR 是否已释放, chain-history 行)。""" with tempfile.TemporaryDirectory() as tmp: lockdir = Path(tmp) / "dispatcher" lockdir.mkdir() (lockdir / "pid").write_text("42") + issue_dir = Path(tmp) / "issue-dir" + issue_dir.mkdir() calls = Path(tmp) / "calls" script = (SANDBOX .replace("__REPO__", tmp) .replace("__WT__", str(Path(tmp) / "wt")) .replace("__LOCKDIR__", str(lockdir)) + .replace("__DIR__", str(issue_dir)) .replace("__CALLS__", str(calls)) .replace("__WR_RC__", str(wr_rc)) .replace("__PUSH_RC__", str(push_rc)) @@ -69,7 +75,9 @@ def _run_trap(rc, wr_rc=0, push_rc=0, count="3"): sh.write_text(script) subprocess.run(["/bin/bash", str(sh)], capture_output=True) lines = calls.read_text().splitlines() if calls.exists() else [] - return lines, not lockdir.exists() + hist = issue_dir / "chain-history" + history = hist.read_text(encoding="utf-8").splitlines() if hist.exists() else [] + return lines, not lockdir.exists(), history def _kinds(lines): @@ -78,7 +86,7 @@ def _kinds(lines): def test_trap_first_command_failure_still_cleans(): """#23 核心:write_ledger 非零不得中止 trap——标签复位/worktree 回收/放锁全达成。""" - lines, lock_released = _run_trap(rc=1, wr_rc=1) + lines, lock_released, _ = _run_trap(rc=1, wr_rc=1) assert "ledger:1" in lines # 失败的命令确实执行过 assert "label" in _kinds(lines) assert any("worktree" in l and "remove" in l for l in lines) @@ -87,14 +95,14 @@ def test_trap_first_command_failure_still_cleans(): def test_salvage_push_on_failure(): """#14 核心:失败 + 分支有新提交 → push 抢救(--force --no-verify)。""" - lines, _ = _run_trap(rc=1, count="3") + lines, _, _ = _run_trap(rc=1, count="3") push = [l for l in lines if l.startswith("git:") and "push" in l] assert push and "--force" in push[0] and "--no-verify" in push[0] def test_push_failure_does_not_block_cleanup(): """#14 验收3:push 失败(网络)仅降级——后续 worktree 回收/标签/放锁照常。""" - lines, lock_released = _run_trap(rc=1, push_rc=1) + lines, lock_released, _ = _run_trap(rc=1, push_rc=1) assert any(l.startswith("git:") and "push" in l for l in lines) assert any("worktree" in l and "remove" in l for l in lines) assert "label" in _kinds(lines) @@ -103,7 +111,7 @@ def test_push_failure_does_not_block_cleanup(): def test_no_salvage_push_on_success(): """成功路径(rc=0)不 push——成功路径推送在步骤 8(既有行为不变)。""" - lines, lock_released = _run_trap(rc=0, count="3") + lines, lock_released, _ = _run_trap(rc=0, count="3") assert not any(l.startswith("git:") and "push" in l for l in lines) assert any("worktree" in l and "remove" in l for l in lines) assert lock_released # 旧 set -e 形态此处会中断放锁 @@ -111,7 +119,7 @@ def test_no_salvage_push_on_success(): def test_no_salvage_when_branch_has_no_commits(): """分支无新提交(main.."${BRANCH}" 计数 0,早期失败)不产生空推送。""" - lines, _ = _run_trap(rc=1, count="0") + lines, _, _ = _run_trap(rc=1, count="0") assert not any(l.startswith("git:") and "push" in l for l in lines) assert "label" in _kinds(lines) @@ -120,7 +128,7 @@ def test_failure_cleanup_keeps_needs_human(): 自身不动标签)是枚举式 triaging/accepted/in-progress——needs-human 终态不清(同 rejected 待遇)。剥掉它 = issue 回零标签态 → dispatch 下轮重派 → 链再熔断,死循环对人类重新隐形化。""" - lines, _ = _run_trap(rc=5, count="0") + lines, _, _ = _run_trap(rc=5, count="0") ops = {ln.split(":", 1)[1] for ln in lines if ln.startswith("label:")} assert ops == {"remove factory:triaging", "remove factory:accepted", @@ -129,8 +137,30 @@ def test_failure_cleanup_keeps_needs_human(): def test_label_cleanup_before_lease_release(): """PR#34:失败清标在 lease_cleanup 之前——清标也是副作用出口,须持有效租约过围栏。""" - lines, _ = _run_trap(rc=1) + lines, _, _ = _run_trap(rc=1) labels = [i for i, ln in enumerate(lines) if ln.startswith("label:")] lease = [i for i, ln in enumerate(lines) if ln.startswith("lease:")] assert labels and lease # 两者都执行 assert max(labels) < min(lease) # 且清标先于放租约 + + +def test_chain_abort_line_on_failure(): + """A:rc≠0 → chain-history 追加 chain-abort 行(下轮 prime/plan 的死因输入)。""" + _, _, history = _run_trap(rc=1, count="0") + assert len(history) == 1 and re.fullmatch( + r"chain-abort \S+ round=7 exit=1", history[0]) + + +def test_no_chain_abort_on_success(): + """成功链不落 chain-abort 行——成功证据由 holdout 行承载,不双写。""" + _, _, history = _run_trap(rc=0, count="3") + assert not history + + +def test_salvage_wip_before_worktree_removal(): + """#14 补遗:未提交 WIP 快照必须先于 worktree 强删——顺序颠倒快照到的就是空仓。""" + lines, _, _ = _run_trap(rc=1, count="0") + sv = [i for i, l in enumerate(lines) if l.startswith("salvage_wip:")] + wt = [i for i, l in enumerate(lines) + if l.startswith("git:") and "worktree" in l and "remove" in l] + assert sv and wt and sv[0] < wt[0] diff --git a/.factory/tests/test_guard_fresh_clone.py b/.factory/tests/test_guard_fresh_clone.py index 9561121..c990756 100644 --- a/.factory/tests/test_guard_fresh_clone.py +++ b/.factory/tests/test_guard_fresh_clone.py @@ -58,7 +58,7 @@ def test_absent_non_ignored_still_blocked(tmp_path): def test_rev_parse_failure_fails_closed(tmp_path): - """CodeRabbit wop-skills#14:REPO_ROOT 非 git 仓(rev-parse 失败)时 + """CodeRabbit xx-skills#14:REPO_ROOT 非 git 仓(rev-parse 失败)时 main_root 不得退化为空串相对 CWD 解析(fail-open)——git 不可用走 check-ignore fail-closed(与下方 git 异常立场一致),不静默放行。""" root = _build_repo(tmp_path, PERIMETER) # .localcfg/ 缺失非 ignored 检查路径 diff --git a/.factory/tests/test_hosting.py b/.factory/tests/test_hosting.py index 960f7fa..766ef05 100644 --- a/.factory/tests/test_hosting.py +++ b/.factory/tests/test_hosting.py @@ -11,9 +11,12 @@ (conftest 注入 .factory 到 sys.path) """ import json +import os import re import subprocess import sys +import threading +import time from pathlib import Path import pytest @@ -94,12 +97,70 @@ def test_set_labels_empty_remove_omits_flag(self): args = [c for c in calls if c[0][:2] == ("issue", "edit")][0][0] assert "--remove-label" not in args # bash 3.2 空参守卫的 py 侧等价 + def test_set_labels_false_negative_reconciles(self): + """#207 实证形态:gh edit 非零但标签已落服务端(mutation + landed + client reported failure)——失败路径重读标签态, + 目标已达成即幂等成功,裁决不被传输层误报否决。""" + calls = [] + ad = hosting.GitHubAdapter() + ad.slug = lambda o=None: "o/r" + + def fake_gh(args, repo_override=None, stdin=None): + calls.append(tuple(args)) + if args[:2] == ["issue", "edit"]: + return _cp(rc=1, err="gh: POST api.github.com: 502") + return _cp(out=json.dumps( + {"labels": [{"name": "factory:rejected"}]})) + ad._gh = fake_gh + assert ad.issue_set_labels(9, add=["factory:rejected"], + remove=["factory:triaging"]) is True + assert calls[1][:2] == ("issue", "view") # 复核是读路径 + + def test_set_labels_true_failure_raises(self): + """复核确证目标未达成 → 原 fail-closed 透传(和解不吞真失败)。""" + ad = hosting.GitHubAdapter() + ad.slug = lambda o=None: "o/r" + + def fake_gh(args, repo_override=None, stdin=None): + if args[:2] == ["issue", "edit"]: + return _cp(rc=1, err="boom") + return _cp(out=json.dumps( + {"labels": [{"name": "factory:triaging"}]})) + ad._gh = fake_gh + with pytest.raises(hosting.HostingError): + ad.issue_set_labels(9, add=["factory:rejected"]) + + def test_set_labels_readback_failure_raises(self): + """复核读自身失败(网络仍断)→ 不视作已落定,fail-closed。""" + ad = hosting.GitHubAdapter() + ad.slug = lambda o=None: "o/r" + ad._gh = lambda a, r=None, s=None: _cp(rc=1, err="still down") + with pytest.raises(hosting.HostingError): + ad.issue_set_labels(9, add=["factory:rejected"]) + + def test_pr_set_labels_false_negative_reconciles(self): + """PR 侧镜像:同构假阴性和解(pr view 重读标签态)。""" + calls = [] + ad = hosting.GitHubAdapter() + ad.slug = lambda o=None: "o/r" + + def fake_gh(args, repo_override=None, stdin=None): + calls.append(tuple(args)) + if args[:2] == ["pr", "edit"]: + return _cp(rc=1, err="gh: 502") + return _cp(out=json.dumps( + {"labels": [{"name": "factory:in-progress"}]})) + ad._gh = fake_gh + assert ad.pr_set_labels(12, add=["factory:in-progress"]) is True + assert calls[1][:2] == ("pr", "view") + def test_pr_create_overrides_repo(self): calls = [] ad = self._ad(calls) ad._gh = lambda args, repo_override=None, stdin=None: ( calls.append((tuple(args), repo_override)) or _cp(out="https://x/pull/12")) - out = ad.pr_create("br", "t", "b", label="l", repo="up/stream") + out = ad.pr_create("br", "t", "b", label="l", base="main", + repo="up/stream") assert out["number"] == 12 assert calls[0][1] == "up/stream" # feedback-upstream 的上游仓显式覆盖 @@ -120,7 +181,53 @@ def test_label_history_neutral_events(self): assert hist == [{"op": "add", "label": "factory:needs-fix"}, {"op": "remove", "label": "factory:needs-fix"}, {"op": "add", "label": "other"}] + def test_pr_create_requires_explicit_base(self): + """CodeRabbit E(GitHub 侧镜像 codeup):--base 显式必填——gh + 猜默认分支(默认分支因仓而异)会静默落错基线;缺省 raise + HostingError code=2(fail-closed,绝不条件拼接静默省略)。""" + ad = hosting.GitHubAdapter() + ad.slug = lambda o=None: "o/r" + with pytest.raises(hosting.HostingError) as e: + ad.pr_create("br", "t", "b") + assert e.value.code == 2 + assert "--base" in str(e.value) + + def test_pr_create_explicit_base_into_gh_args(self): + """显式 base 进 gh 参数(args 含 --base 且后一位为主干名)—— + 宿主脚本(feedback-upstream/upstream-sync-check)base 落点锚。""" + calls = [] + ad = self._ad(calls) + ad._gh = lambda args, repo_override=None, stdin=None: ( + calls.append((tuple(args), repo_override)) or _cp(out="https://x/pull/12")) + assert ad.pr_create("br", "t", "b", base="main")["number"] == 12 + args = calls[0][0] + assert args[args.index("--base") + 1] == "main" + + def test_auth_diagnose_reports_gh_stderr(self): + """auth 失败留痕:gh auth status 原始 stderr 返回(可回溯诊断)。""" + ad = hosting.GitHubAdapter() + orig = subprocess.run + hosting.subprocess.run = lambda *a, **k: _cp(rc=1, err="auth failed: bad keyring") + try: + diag = ad.auth_diagnose() + finally: + hosting.subprocess.run = orig + assert diag == "auth failed: bad keyring" + + def test_auth_diagnose_gh_missing_from_path(self): + """无 gh CLI → 诊断标明环境缺失(区别于凭据/网络失败)。""" + ad = hosting.GitHubAdapter() + orig = subprocess.run + def _boom(*a, **k): + raise FileNotFoundError + + hosting.subprocess.run = _boom + try: + diag = ad.auth_diagnose() + finally: + hosting.subprocess.run = orig + assert "gh CLI 不在 PATH" in diag # ── Codeup:请求形状 + 缺口 fail-closed ──────────────────────────── @@ -269,17 +376,17 @@ def test_pr_create_requires_base_fail_closed(self, monkeypatch): def test_space_id_maps_path_namespace(self, monkeypatch, tmp_path): """namespace = remote path 首段(php#17 Sourcery): - org/group/ 的中间层不是 namespace,旧 [1] 索引 + gtsp/open-platform/ 的中间层不是 namespace,旧 [1] 索引 永不命中 conf 键;单段 path(无 namespace 层)fail-closed。""" conf = tmp_path / "spaces.conf" - conf.write_text("group|SID-1\n", encoding="utf-8") + conf.write_text("gtsp|SID-1\n", encoding="utf-8") monkeypatch.setenv("YUNXIAO_ACCESS_TOKEN", "t") monkeypatch.setenv("CODEUP_ORG_ID", "org") monkeypatch.setenv("CODEUP_REPO_ID", "42") monkeypatch.setenv("FACTORY_SPACES_CONF", str(conf)) monkeypatch.delenv("CODEUP_SPACE_ID", raising=False) ad = hosting.CodeupAdapter() - ad._remote = lambda: ("610b3c9d", "org/group/gateway") + ad._remote = lambda: ("610b3c9d", "gtsp/open-platform/gtsp-xx-gateway") assert ad._space_id() == "SID-1" ad._remote = lambda: ("610b3c9d", "plain-repo") with pytest.raises(hosting.HostingError) as e: @@ -414,7 +521,7 @@ def fake_req(method, path, body=None, query=None, _retry_rdc=True): assert "降级空集" in capsys.readouterr().err def test_marker_label_prefix_only_returns_empty(self, monkeypatch): - """CodeRabbit wop-skills#14:标记评论恰为前缀/前缀+空白时切片为空, + """CodeRabbit xx-skills#14:标记评论恰为前缀/前缀+空白时切片为空, 旧实现 splitlines()[0] 抛 IndexError(平台开放输入不可约束)—— 空标记按无标记处理,不越过 HostingError 边界。""" ad = self._ad(monkeypatch, {}) @@ -479,7 +586,7 @@ class TestCodeupWorkItemFace: WI_DESC = json.dumps({"htmlValue": "

正文

\n"}, ensure_ascii=False) - WI = {"result": {"id": "wid1", "serialNumber": "KFPT-18", + WI = {"result": {"id": "wid1", "serialNumber": "T-18", "subject": "标题", "logicalStatus": "NORMAL", "description": WI_DESC, "labels": None}, @@ -499,10 +606,10 @@ def fake_req(method, path, body=None, query=None, _retry_rdc=True): return payload if path.endswith("/comments"): return self.WI["_comments"] - if path.endswith("/workitems/KFPT-18") or path.endswith("/workitems/wid1"): + if path.endswith("/workitems/T-18") or path.endswith("/workitems/wid1"): return self.WI["result"] if path.endswith("/workitems:search"): - return {"result": [self.WI["result"], {"id": "wid2", "serialNumber": "KFPT-19", + return {"result": [self.WI["result"], {"id": "wid2", "serialNumber": "T-19", "subject": "旧", "logicalStatus": "FINISHED", "description": ""}]} raise hosting.HostingError(f"mock 未路由: {method} {path}") @@ -512,8 +619,8 @@ def fake_req(method, path, body=None, query=None, _retry_rdc=True): def test_issue_view_normalizes_and_strips_marker(self, monkeypatch): monkeypatch.setenv("CODEUP_ISSUE_LABELS", "description") ad = self._ad(monkeypatch) - n = ad.issue_view("KFPT-18") - assert n["number"] == "KFPT-18" and n["state"] == "open" + n = ad.issue_view("T-18") + assert n["number"] == "T-18" and n["state"] == "open" assert n["title"] == "标题" assert n["body"] == "正文" # HTML 剥离 + 标记块剥离 assert "factory:labels" not in n["body"] @@ -522,14 +629,14 @@ def test_issue_view_normalizes_and_strips_marker(self, monkeypatch): def test_issue_view_native_labels(self, monkeypatch): wi = {**self.WI["result"], "labels": ["factory:rejected"], "description": ""} - ad = self._ad(monkeypatch, routes={("GET", "/workitems/KFPT-16"): wi}) - assert ad.issue_labels("KFPT-16") == ["factory:rejected"] + ad = self._ad(monkeypatch, routes={("GET", "/workitems/T-16"): wi}) + assert ad.issue_labels("T-16") == ["factory:rejected"] def test_issue_list_paginates_filters_state_and_label(self, monkeypatch): monkeypatch.setenv("CODEUP_ISSUE_LABELS", "description") ad = self._ad(monkeypatch) out = ad.issue_list(state="open", label="factory:accepted", limit=10) - assert [i["number"] for i in out] == ["KFPT-18"] # FINISHED 滤出+label 过滤 + assert [i["number"] for i in out] == ["T-18"] # FINISHED 滤出+label 过滤 m, p, body, _q = ad.seen[0] assert (m, p.endswith("/workitems:search")) == ("POST", True) assert body["category"] == "Task" and body["spaceId"] == "sp1" # category 必填(live) @@ -544,15 +651,15 @@ def test_issue_list_requires_space_env(self, monkeypatch): def test_issue_set_labels_native_put(self, monkeypatch): wi = {**self.WI["result"], "labels": ["keep"]} - ad = self._ad(monkeypatch, routes={("GET", "/workitems/KFPT-18"): wi}) - ad.issue_set_labels("KFPT-18", add=["factory:triaging"], remove=["keep"]) + ad = self._ad(monkeypatch, routes={("GET", "/workitems/T-18"): wi}) + ad.issue_set_labels("T-18", add=["factory:triaging"], remove=["keep"]) put = [s for s in ad.seen if s[0] == "PUT"][-1] assert put[2] == {"labels": ["factory:triaging"]} # 排序去重 def test_issue_set_labels_description_rw(self, monkeypatch): monkeypatch.setenv("CODEUP_ISSUE_LABELS", "description") ad = self._ad(monkeypatch) - ad.issue_set_labels("KFPT-18", add=["factory:in-progress"]) + ad.issue_set_labels("T-18", add=["factory:in-progress"]) put = [s for s in ad.seen if s[0] == "PUT"][-1] assert put[2]["formatType"] == "MARKDOWN" # 读-改-写保留原始载体格式(JSON 串不解包):原 accepted 保留+新增 @@ -562,7 +669,7 @@ def test_issue_set_labels_description_rw(self, monkeypatch): '\n\n') # 移除唯一标签 = 块消失,原文保留 ad2 = self._ad(monkeypatch) - ad2.issue_set_labels("KFPT-18", remove=["factory:accepted"]) + ad2.issue_set_labels("T-18", remove=["factory:accepted"]) put2 = [s for s in ad2.seen if s[0] == "PUT"][-1] assert put2[2]["description"] == '{"htmlValue": "

正文

\\n"}' @@ -571,11 +678,11 @@ def test_issue_comment_uses_id_and_marker_dedupe(self, monkeypatch, capsys): ad = self._ad(monkeypatch, routes={ ("GET", "/workitems/wid1/comments"): [ {"author": {"name": "x"}, "content": "

旧

"}]}) - assert ad.issue_comment("KFPT-18", "回执", marker="m1") is True # dedupe 命中 + assert ad.issue_comment("T-18", "回执", marker="m1") is True # dedupe 命中 assert "dedupe" in capsys.readouterr().err posts = [s for s in ad.seen if s[0] == "POST"] assert not posts - ad.issue_comment("KFPT-18", "新评论") + ad.issue_comment("T-18", "新评论") posts = [s for s in ad.seen if s[0] == "POST"] assert posts[-1][1].endswith("/workitems/wid1/comments") # serialNumber→id assert posts[-1][2]["contentType"] == "markdown" @@ -613,13 +720,13 @@ def fake_req(method, path, body=None, query=None, _retry_rdc=True): raise hosting.HostingError("codeup GET fields HTTP 404: x") return fields if fields is not None else self.FIELDS # 【live 2026-08-26】create 响应只含 24-hex id(无 serialNumber/ - # detailUrl)——KFPT-21 实测;详情回查才有可读编号 + # detailUrl)——T-21 实测;详情回查才有可读编号 if method == "POST" and path.endswith("/workitems"): return create_resp or {"result": {"id": "wid123"}} if fail_detail: raise hosting.HostingError("codeup GET detail HTTP 500: x") - return detail_resp or {"result": {"serialNumber": "KFPT-42", - "detailUrl": "https://x/KFPT-42"}} + return detail_resp or {"result": {"serialNumber": "T-42", + "detailUrl": "https://x/T-42"}} ad._req = fake_req return ad, calls @@ -656,7 +763,7 @@ def test_create_payload_shape(self, monkeypatch): assert cfvs["6"] == "22" # list=末档 option id(str) assert 1 not in cfvs and "1" not in cfvs # NativeField 走本体 assert 7 not in cfvs and "7" not in cfvs # 非 required 不带 - assert out == {"number": "KFPT-42", "url": "https://x/KFPT-42"} + assert out == {"number": "T-42", "url": "https://x/T-42"} def test_fields_fetch_failure_degrades_with_warning(self, monkeypatch, capsys): ad, calls = self._ad(monkeypatch, fail_fields=True) @@ -664,7 +771,7 @@ def test_fields_fetch_failure_degrades_with_warning(self, monkeypatch, capsys): assert "warn" in capsys.readouterr().err # 降级可见不静默 posts = [c for c in calls if c[0] == "POST" and c[1].endswith("/workitems")] assert posts and "customFieldValues" not in posts[-1][2] # POST 仍发出 - assert out["number"] == "KFPT-42" + assert out["number"] == "T-42" def test_create_returns_serial_via_detail_lookup(self, monkeypatch): """live 契约:create 响应仅 24-hex id;serialNumber 由详情回查取得。""" @@ -673,7 +780,7 @@ def test_create_returns_serial_via_detail_lookup(self, monkeypatch): gets = [(m, p) for m, p, _b in calls if m == "GET"] assert any(p.endswith("/workitems/wid123") for _m, p in gets), \ "必须回查详情端点" - assert out == {"number": "KFPT-42", "url": "https://x/KFPT-42"} + assert out == {"number": "T-42", "url": "https://x/T-42"} def test_detail_lookup_failure_degrades_to_id(self, monkeypatch, capsys): """回查失败降级返回 id + stderr 告警(可读性损失可见,不阻断)。""" @@ -686,10 +793,10 @@ def test_create_resp_with_serial_still_wins(self, monkeypatch): """若平台未来在 create 响应补 serialNumber:有 id 仍回查(详情 是 detailUrl 权威源);无 id 才直取响应字段。""" ad, _calls = self._ad( - monkeypatch, create_resp={"result": {"serialNumber": "KFPT-9"}}) + monkeypatch, create_resp={"result": {"serialNumber": "T-9"}}) out = ad.issue_create("t", "b") # create 无 id → 不回查,直接响应字段 - assert out["number"] == "KFPT-9" + assert out["number"] == "T-9" class TestCodeupEndpointFallback: @@ -906,3 +1013,178 @@ def test_cli_gate_fails_closed(self, capsys): "## 验收\n- [ ] x", "--label", "factory:accepted"]) assert e.value.code == 2 assert "预检未过" in capsys.readouterr().err + +class TestRepoSplitRouting: + """CodeRabbit D:--repo 双语义拆分(_repo_split)——目录形态(java#29 + 跨仓调用:平台检测用目录真实路径、slug 由该目录 remote 自解析)vs + owner/repo slug 形态(检测回退 "."、slug 原样进 ops)。拆分点是 + 「按 cwd 检测选错适配器」与「目录路径误当 slug 进 gh --repo」的 + 共同单点。""" + + def test_split_three_shapes(self, tmp_path, monkeypatch): + d = str(tmp_path) + assert hosting._repo_split(d) == (d, None) + # slug 形态需 FACTORY_HOSTING 锚定(#134):未设即拒收 + monkeypatch.delenv("FACTORY_HOSTING", raising=False) + with pytest.raises(hosting.HostingError, match="issue #134"): + hosting._repo_split("up/stream") + monkeypatch.setenv("FACTORY_HOSTING", "github") + assert hosting._repo_split("up/stream") == (".", "up/stream") + assert hosting._repo_split(None) == (".", None) + + def test_main_slug_repo_detects_dot_passes_slug(self, monkeypatch): + """slug 形态(FACTORY_HOSTING 已锚定):平台检测落在 cwd("."); + pr view 的 --repo 原样进 ops(gh --repo up/stream 语义)。""" + seen = {} + monkeypatch.setenv("FACTORY_HOSTING", "github") + + class _Ad: + def pr_view(self, p, repo=None): + seen["repo"] = repo + return {"number": 1} + + monkeypatch.setattr(hosting, "current_adapter", lambda local: ( + seen.__setitem__("local", local) or _Ad())) + hosting.main(["pr", "view", "1", "--repo", "up/stream"]) + assert seen == {"local": ".", "repo": "up/stream"} + + def test_main_slug_repo_without_hosting_fail_closed(self, monkeypatch, capsys): + """slug 形态且 FACTORY_HOSTING 未设:fail-closed exit 2(#134), + 不得静默按 cwd remote 选适配器(Codeup 仓会被误判 GitHub)。""" + monkeypatch.delenv("FACTORY_HOSTING", raising=False) + monkeypatch.setattr(hosting, "current_adapter", + lambda local: pytest.fail("不应走到适配器构造")) + with pytest.raises(SystemExit) as e: + hosting.main(["pr", "view", "1", "--repo", "up/stream"]) + assert e.value.code == 2 + assert "issue #134" in capsys.readouterr().err + + def test_main_dir_repo_detects_dir_ops_none(self, monkeypatch, tmp_path): + """目录形态:检测用目录真实路径(远端解析/平台选择以其为准); + ops 收 repo=None——目录路径绝不进 gh --repo。""" + seen = {} + d = str(tmp_path) + + class _Ad: + def pr_view(self, p, repo=None): + seen["repo"] = repo + return {"number": 1} + + monkeypatch.setattr(hosting, "current_adapter", lambda local: ( + seen.__setitem__("local", local) or _Ad())) + hosting.main(["pr", "view", "1", "--repo", d]) + assert seen == {"local": d, "repo": None} + + +class TestLabelAddCrossProcessLock: + """CodeRabbit C:add「预检+POST」跨进程互斥(_add_label_lock)—— + mkdir 原子占锁 + pid 活性判死接管 + 无 pid 残锁年龄宽限 + 有界等锁 + fail-closed。缺陷:双进程同仓同 PR 同名 add 各过预检再各 POST = + 双未 resolved 标记(状态机重复转移);锁内重预检幂等收敛恰一次 + POST。""" + + def test_active_owner_timeout_fail_closed(self, monkeypatch, tmp_path): + """活主持锁 → 等锁超时 HostingError code=1(fail-closed 拒双 + 写):不排队不静默放行,宁可链失败重试(重试幂等收敛)。""" + monkeypatch.setenv("FACTORY_LOCK_DIR", str(tmp_path)) + monkeypatch.setattr(hosting, "_LABEL_LOCK_TIMEOUT", 0.2) + ad = hosting.CodeupAdapter() + lock_dir = hosting._label_lock_dir(".", 7, "factory:needs-review") + os.makedirs(lock_dir) + Path(lock_dir, "pid").write_text(str(os.getpid()), encoding="ascii") + with pytest.raises(hosting.HostingError) as e: + with ad._add_label_lock(7, "factory:needs-review"): + pass + assert e.value.code == 1 + assert "fail-closed" in str(e.value) + + def test_dead_owner_lock_taken_over(self, monkeypatch, tmp_path): + """死者残锁(pid 活性判死)→ 清后接管:with 正常进入、退出清 + 目录——残锁不永久卡链(需人工清锁即锁故障)。""" + monkeypatch.setenv("FACTORY_LOCK_DIR", str(tmp_path)) + ad = hosting.CodeupAdapter() + lock_dir = hosting._label_lock_dir(".", 7, "factory:needs-fix") + os.makedirs(lock_dir) + Path(lock_dir, "pid").write_text("99999999", encoding="ascii") + assert hosting._lock_stale(lock_dir, "99999999") is True + with ad._add_label_lock(7, "factory:needs-fix"): + assert os.path.isdir(lock_dir) # 持锁期间在位 + assert not os.path.isdir(lock_dir) # 退出清理 + + def test_pidless_crash_dir_reclaimed_after_grace(self, monkeypatch, tmp_path): + """建锁→写 pid 的崩溃窗残锁(无 pid):年龄超宽限才判死可接管, + 宽限内按活等(不抢正在建锁的活主);宽限 0 即刻接管。""" + monkeypatch.setenv("FACTORY_LOCK_DIR", str(tmp_path)) + monkeypatch.setattr(hosting, "_LABEL_LOCK_STALE_GRACE", 0.0) + ad = hosting.CodeupAdapter() + lock_dir = hosting._label_lock_dir(".", 7, "factory:approved") + os.makedirs(lock_dir) # 无 pid:崩溃窗形态 + with ad._add_label_lock(7, "factory:approved"): + assert os.path.isdir(lock_dir) + assert not os.path.isdir(lock_dir) + + def test_parallel_add_posts_exactly_once(self, monkeypatch, tmp_path): + """并发回归(核心契约):两独立适配器实例(双进程/双链形态) + 同仓同 PR 同名 add 并发——锁串行化 + 后到者锁内重预检幂等跳过, + POST comments 恰一次;无锁实现此处必双 POST。""" + monkeypatch.setenv("FACTORY_LOCK_DIR", str(tmp_path)) + # CI 无 YUNXIAO_ACCESS_TOKEN:pr_set_labels 入口校验先于 _req stub 生效 + monkeypatch.setenv("YUNXIAO_ACCESS_TOKEN", "t") + markers = [] + posts = [] + barrier = threading.Barrier(2) + + def make_ad(): + ad = hosting.CodeupAdapter() + + def fake_req(method, path, body=None, query=None, + _retry_rdc=True): + if method == "POST" and path.endswith("/comments/list"): + return {"result": [ + {"id": f"c-{i}", "content": c} + for i, c in enumerate(markers)]} + if method == "POST" and path.endswith("/changeRequests/7/comments"): + posts.append(body["content"]) + markers.append(body["content"]) + time.sleep(0.05) # 放大持锁窗:后到线程必撞上等锁 + return {"success": True} + return {"success": True, "result": []} # 类标 Link 兜底 + ad._req = fake_req + return ad + + results = [] + + def worker(): + try: + barrier.wait() + make_ad().pr_set_labels(7, add=["factory:needs-review"]) + results.append("ok") + except Exception as exc: # 子线程异常收敛进结果,不裸抛 + results.append(repr(exc)) + + threads = [threading.Thread(target=worker) for _ in range(2)] + for t in threads: + t.start() + for t in threads: + t.join() + assert results == ["ok", "ok"] + assert len(posts) == 1 + assert len(markers) == 1 + def test_huge_garbage_pid_waits_then_fail_closed(self, monkeypatch, + tmp_path): + """pid 内容超 C int 巨值(损坏写,审查 P3):按活等不误清、不裸 + traceback(OverflowError 穿透即破坏 fail-closed docstring 契约), + 等锁超时抛 code=1。""" + monkeypatch.setenv("FACTORY_LOCK_DIR", str(tmp_path)) + monkeypatch.setattr(hosting, "_LABEL_LOCK_TIMEOUT", 0.2) + ad = hosting.CodeupAdapter() + lock_dir = hosting._label_lock_dir(".", 7, "factory:needs-human") + os.makedirs(lock_dir) + Path(lock_dir, "pid").write_text("100000000000000000000", + encoding="ascii") + owner = hosting._lock_owner(lock_dir) + assert hosting._lock_stale(lock_dir, owner) is False # 按活等 + with pytest.raises(hosting.HostingError) as e: + with ad._add_label_lock(7, "factory:needs-human"): + pass + assert e.value.code == 1 diff --git a/.factory/tests/test_issue_reject_receipt.py b/.factory/tests/test_issue_reject_receipt.py new file mode 100644 index 0000000..dc55829 --- /dev/null +++ b/.factory/tests/test_issue_reject_receipt.py @@ -0,0 +1,132 @@ +"""issue_reject 回执沙箱——2026-09-20 #207 静默拒绝事故形态。 + +回归优先:真实函数按名从 factory-lib.sh 正则提取(零拷贝,源漂移即 +测试失败),在 git-free 夹具沙箱执行(REPO 指向临时目录,hosting/ +factory_lib/lease 全部桩替,零网络零真实 issue)。三态钉死后修复的 +两级失败语义: +- 假阴性:set-labels 非零 + 远端 factory:rejected 已落 → 按已落定继续 + 发回执(#207 修复锚:旧实现 || return 1 早退,回执永不执行); +- 真失败:非零 + 复核确证缺失 → return 1 不发评论; +- 成功路径:落标成功 → 回执照常。 +机器评论正文不得含 [factory:rejected] 子串(state.py 毒丸扫描)。 +""" +import re +import subprocess +from pathlib import Path + +import pytest + +FACTORY = Path(__file__).resolve().parents[1] +LIB = (FACTORY / "factory-lib.sh").read_text(encoding="utf-8") + +STUB_HOSTING = '''\ +#!/usr/bin/env python3 +"""沙箱桩:set-labels 由 env 注入成败;get-labels 打印 REMOTE_LABELS; +comment 追加到 COMMENT_LOG(不真实发送)。""" +import json +import os +import sys + +args = sys.argv[1:] +if args[:2] == ["issue", "set-labels"]: + if os.environ.get("LABEL_RESULT") != "ok": + print("HostingError: gh issue edit 失败: POST api.github.com: 502", + file=sys.stderr) + sys.exit(1) +elif args[:2] == ["issue", "get-labels"]: + print(json.dumps(json.loads(os.environ.get("REMOTE_LABELS", "[]")))) + sys.exit(0) +elif args[:2] == ["issue", "comment"]: + with open(os.environ["COMMENT_LOG"], "a", encoding="utf-8") as log: + body_file = args[args.index("--body-file") + 1] + marker = (args[args.index("--marker") + 1] + if "--marker" in args else "") + log.write(f"--- marker={marker}\\n" + + open(body_file, encoding="utf-8").read()) + sys.exit(0) +sys.exit(0) +''' + +STUB_FACTORY_LIB = '''\ +#!/usr/bin/env python3 +"""沙箱桩:sanitize no-op(正文安全非本套件主题);receipt 打固定 +回执正文(刻意不含 [factory:rejected] 子串——state.py 毒丸不变量)。""" +import sys + +if sys.argv[1] == "sanitize": + sys.exit(0) +if sys.argv[1] == "receipt": + print("## 工厂 triage 裁决:reject —— 判据 b 不通过\\n\\n- 判据b: 不通过\\n") + sys.exit(0) +''' + +STUB_LEASE = "lease_guard() { return 0; }\n" + + +def _fn(name: str) -> str: + # 函数体行全缩进、收尾 } 顶格——非贪婪到首个顶格 } 即函数边界 + m = re.search(rf"\n({name}\(\) \{{.*?\n\}})\n", LIB, re.S) + assert m, f"factory-lib.sh 缺 {name}() —— 正则提取漂移,源已变" + return m.group(1) + "\n" + + +FUNCS = "".join(_fn(n) for n in + ("issue_label_swap", "issue_comment", "issue_reject")) + + +@pytest.fixture() +def sandbox(tmp_path: Path): + repo = tmp_path / "repo" + fdir = repo / ".factory" + fdir.mkdir(parents=True) + (fdir / "hosting.py").write_text(STUB_HOSTING, encoding="utf-8") + (fdir / "factory_lib.py").write_text(STUB_FACTORY_LIB, encoding="utf-8") + (fdir / "factory-lease.sh").write_text(STUB_LEASE, encoding="utf-8") + (fdir / "triage.json").write_text( + '{"verdict": "reject", "reasons": ["判据b: 不通过"]}', + encoding="utf-8") + return repo + + +def _run(repo: Path, env_extra: dict) -> subprocess.CompletedProcess: + # set -uo pipefail(无 -e):issue_reject 内部自持失败语义;先 + # source 桩 lease——对齐真实形态(factory-lib.sh 顶部 source + # factory-lease.sh 提供 lease_guard,正则提取的函数体不含该行) + script = (f"set -uo pipefail\n" + f"source '{repo}/.factory/factory-lease.sh'\n" + f"{FUNCS}\nissue_reject '' .factory/triage.json\n") + env = {"REPO": str(repo), "ISSUE": "207", "PATH": "/usr/bin:/bin", + "COMMENT_LOG": str(repo / "comment.log"), **env_extra} + return subprocess.run(["bash", "-c", script], cwd=str(repo), env=env, + capture_output=True, text=True) + + +def test_false_negative_posts_receipt(sandbox): + """#207 事故核心形态:set-labels 非零 + 远端已落 → 回执不丢 + (旧实现 || return 1 早退、回执永不执行——本回归锚)。""" + r = _run(sandbox, {"LABEL_RESULT": "fail", + "REMOTE_LABELS": '["factory:rejected"]'}) + assert r.returncode == 0, r.stderr + log = (sandbox / "comment.log").read_text(encoding="utf-8") + assert "factory:receipt:issue-207:rbatch" in log + out = r.stdout + r.stderr + assert "按已落定继续回执" in out + assert "[hosting]" in out # 落标 stderr 落档回显(Fix B) + + +def test_true_failure_aborts_without_receipt(sandbox): + """复核确证 factory:rejected 缺失 → return 1 不发评论(裁决未落定, + 调用方终止——和解不掩盖真失败)。""" + r = _run(sandbox, {"LABEL_RESULT": "fail", "REMOTE_LABELS": "[]"}) + assert r.returncode == 1 + assert not (sandbox / "comment.log").exists() + assert "按已落定继续回执" not in r.stdout + r.stderr + + +def test_success_path_posts_receipt(sandbox): + """快乐路径:落标成功 → 回执照常(两级语义改动不回归)。""" + r = _run(sandbox, {"LABEL_RESULT": "ok"}) + assert r.returncode == 0, r.stderr + log = (sandbox / "comment.log").read_text(encoding="utf-8") + assert "factory:receipt:issue-207:rbatch" in log + assert "[factory:rejected]" not in log # state.py 毒丸不变量 diff --git a/.factory/tests/test_mutations_run.py b/.factory/tests/test_mutations_run.py index f3e99ab..15ed5c7 100644 --- a/.factory/tests/test_mutations_run.py +++ b/.factory/tests/test_mutations_run.py @@ -120,26 +120,42 @@ def _assert_group_dead(pgid: int, *, timeout: float = 10.0) -> None: pytest.fail(f"进程组 {pgid} 在 {timeout}s 后未消失(仍有成员)") def _slow_gate(tmp_path): - """夹具门:自报 pgid、派生 sleep 孙进程后挂起。""" - pgid_file = tmp_path / "pgid" + """夹具门:派生 sleep 孙进程后挂起(组内有真活成员,只杀 leader + 会留孤儿,断言才有杀组语义面)。""" gate = tmp_path / "slow_gate.sh" - gate.write_text( - f"#!/bin/bash\necho $$ > {pgid_file}\nsleep 60 &\nwait\n", - encoding="utf-8") + gate.write_text("#!/bin/bash\nsleep 60 &\nwait\n", encoding="utf-8") gate.chmod(0o755) - return pgid_file, gate + return gate + + +def _capture_gate_pgid(monkeypatch): + """从派生侧捕获门的 pgid:start_new_session 下 pgid == Popen pid + (run.py 同一不变式)。刻意不做门内 echo $$ > 文件握手——xdist 多 + worker 负载下 bash 启动可晚于 1s 超时杀组,文件永不落盘(2026-09-20 + 实证 FileNotFoundError)。""" + real_popen = mut.subprocess.Popen + captured = [] + + def _popen(*args, **kwargs): + proc = real_popen(*args, **kwargs) + captured.append(proc.pid) + return proc + + monkeypatch.setattr(mut.subprocess, "Popen", _popen) + return captured + def test_timeout_kills_process_group(tmp_path, monkeypatch): """超时杀整个进程组(PR #33 审查):只杀 bash 直子会留孤儿继续读 - 注入中的 target,还原窗口被污染。夹具门自报 pgid(start_new_session - 下 == 自身 pid)、派生 sleep 孙进程后挂起;断言超时后整组无存活。""" - import time as _time - pgid_file, gate = _slow_gate(tmp_path) + 注入中的 target,还原窗口被污染。夹具门派生 sleep 孙进程后挂起; + 断言超时后整组无存活(pgid 取自 Popen 派生侧,见 _capture_gate_pgid)。""" + gate = _slow_gate(tmp_path) + pgids = _capture_gate_pgid(monkeypatch) monkeypatch.setattr(mut, "FINAL_GATE", [str(gate)]) monkeypatch.setattr(mut, "TESTS_TIMEOUT", 1) - t0 = _time.monotonic() assert mut.run_gate("tests", "whatever") is None - _assert_group_dead(int(pgid_file.read_text().strip())) + _assert_group_dead(pgids[0]) + def test_timeout_sigkill_eperm_tolerated(tmp_path, monkeypatch): @@ -149,7 +165,8 @@ def test_timeout_sigkill_eperm_tolerated(tmp_path, monkeypatch): import errno import os import signal - pgid_file, gate = _slow_gate(tmp_path) + gate = _slow_gate(tmp_path) + pgids = _capture_gate_pgid(monkeypatch) real_killpg = os.killpg def killpg_then_eperm(pgid, sig): @@ -161,7 +178,7 @@ def killpg_then_eperm(pgid, sig): monkeypatch.setattr(mut, "FINAL_GATE", [str(gate)]) monkeypatch.setattr(mut, "TESTS_TIMEOUT", 1) assert mut.run_gate("tests", "whatever") is None - _assert_group_dead(int(pgid_file.read_text().strip())) + _assert_group_dead(pgids[0]) def test_probe_tolerates_macos_zombie_window(monkeypatch): @@ -410,7 +427,7 @@ class TestMainSmoke: """ def test_main_only_nonexistent_id_config_error(self, monkeypatch, capsys): - """--only 含未知 id → 配置错误 rc 2(下游 wop-web-tools 反哺:过滤空 + """--only 含未知 id → 配置错误 rc 2(下游 xx-web-tools 反哺:过滤空 清单以 0 退出写戳 = 配置错误伪装成全量验证;rc 2 对齐 guard 用法错误 语义。原 exit0 断言固化的正是此缺陷,已翻转)。""" monkeypatch.setattr(mut, "write_stamp", lambda *a, **k: None) # 不动 evidence-stamp @@ -421,7 +438,7 @@ def test_main_only_nonexistent_id_config_error(self, monkeypatch, capsys): def test_main_target_outside_repo_fails_config(self, monkeypatch, capsys, tmp_path): """target 越出 REPO_ROOT(`..` 与绝对路径)→ FAIL-config → rc 1 - (下游 wop-web-tools 反哺:--defects 载外部 JSON 时 `..` 可指向仓外 + (下游 xx-web-tools 反哺:--defects 载外部 JSON 时 `..` 可指向仓外 文件注入+写回,进程非正常终止 = 注入残留落仓外)。""" for target in ("../escape-probe-918273", str(tmp_path / "outside-918273")): cfg = {"defects": [{ @@ -445,7 +462,7 @@ def test_main_missing_defects_file_raises(self, monkeypatch, tmp_path): def test_write_stamp_glob_latest_evidence(self, tmp_path, monkeypatch): """write_stamp() 缺省取 mutations/ 目录最新 EVIDENCE-*.md(下游 - wop-web-tools 反哺:静态默认文件名会过期,写戳引用不存在的留档 = + xx-web-tools 反哺:静态默认文件名会过期,写戳引用不存在的留档 = stamp 说谎)。显式 evidence 参数仍原样直写。""" (tmp_path / "EVIDENCE-2026-08-24.md").write_text("old", encoding="utf-8") (tmp_path / "EVIDENCE-2026-08-26.md").write_text("new", encoding="utf-8") diff --git a/.factory/tests/test_parallel_gate.py b/.factory/tests/test_parallel_gate.py new file mode 100644 index 0000000..3d420d7 --- /dev/null +++ b/.factory/tests/test_parallel_gate.py @@ -0,0 +1,522 @@ +"""并行测试门(ADR-016)测试:配置校验 / 栈探测 / 保守档适配 / 编排契约 / CLI。 + +覆盖面: +- parallel_gate_cfg:可选键缺失 → None;存在即严格校验 fail-closed + RuntimeError、拒未知键(顶层与段级)、argv XOR shell、tag 唯一且禁 + 换行(宿主脚本按行回收失败清单)、intra/stack/workers 取值域。 +- detect_stack:构建文件 → 栈 id(maven/gradle/go/cargo/phpunit/dotnet/ + vitest 先于 jest/pytest),未识别 → None 不猜。 +- intra_parallel_args:保守档适配表(fork 级分发,无进程内线程交错), + pytest-xdist 缺席软降级带提示。 +- run_parallel_gate:配置序回放(与完成序解耦)、失败 tag 逐行落盘 + (尾带换行)、段日志失败保留/成功即删(TMPDIR 私有化断言)、 + workers 有界并发、$PY 词替换、shell 段 pipefail、cwd 预检 fail-closed、 + 损坏配置转 rc 2、段独立进程组 + 中断收尸(SIGTERM→限宽→SIGKILL)。 +- main parallel-gate:未知参数/缺路径 → 2,--failed-tags 透传。 + +运行:python3 -m pytest .factory/tests/test_parallel_gate.py -o addopts= -q +(conftest 注入 .factory 到 sys.path) +""" +from __future__ import annotations + +import json +import os +import tempfile +import time + +import pytest + +import factory_lib as fl + + +_REAL_SLEEP = time.sleep # 中断测试 monkeypatch fl.time.sleep 后,真睡轮询仍可用 + + +def _assert_dead(pid, timeout=5.0): + """轮询断言进程已死(僵尸先收割——本测试进程即段进程之父)。""" + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + os.kill(pid, 0) + except ProcessLookupError: + return + try: + os.waitpid(pid, os.WNOHANG) # 已退出未 wait 的僵尸在此收走 + except ChildProcessError: + pass + _REAL_SLEEP(0.02) + pytest.fail(f"进程 {pid} 清理后仍存活") + + +def _argv_seg(tag, code, *extra_words, **extra): + return {"tag": tag, "argv": ["$PY", "-c", code, *extra_words]} | extra + + +# ───────────────────────── parallel_gate_cfg ───────────────────────── + +class TestParallelGateCfg: + def _cfg(self, monkeypatch, raw): + monkeypatch.setattr(fl, "_LOCAL_CFG", {"parallel_gate": raw}) + return fl.parallel_gate_cfg() + + def test_missing_key_returns_none(self, monkeypatch): + monkeypatch.setattr(fl, "_LOCAL_CFG", {"final_gate_cmd": "x"}) + assert fl.parallel_gate_cfg() is None + + def test_minimal_normalization(self, monkeypatch): + cfg = self._cfg(monkeypatch, { + "segments": [{"tag": "s1", "argv": ["$PY", "-m", "pytest", "-q"]}], + }) + assert cfg == {"workers": 0, "segments": [ + {"tag": "s1", "name": "s1", "intra": "off", + "argv": ["$PY", "-m", "pytest", "-q"]}]} + + def test_name_explicit_and_empty_fallback(self, monkeypatch): + cfg = self._cfg(monkeypatch, {"segments": [ + {"tag": "a", "name": "显示名", "argv": ["x"]}, + {"tag": "b", "name": "", "shell": "true"}, + ]}) + assert cfg is not None + assert [s["name"] for s in cfg["segments"]] == ["显示名", "b"] + + def test_full_segment_normalization(self, monkeypatch): + cfg = self._cfg(monkeypatch, {"workers": 2, "segments": [ + {"tag": "a", "cwd": "sub", "intra": "auto", "stack": "maven", + "shell": "mvn test"}]}) + assert cfg == {"workers": 2, "segments": [ + {"tag": "a", "name": "a", "intra": "auto", "stack": "maven", + "cwd": "sub", "shell": "mvn test"}]} + + @pytest.mark.parametrize("raw", [ + [], # parallel_gate 非对象 + {"segments": [], "workers": 0}, # 空段表 + {"segments": "x"}, # 段表非数组 + {"segments": ["x"]}, # 段非对象 + {"segments": [{}], "typo": 1}, # 未知顶层键 + 缺 tag + {"segments": [{"tag": "a"}], "extra": 1}, + ]) + def test_structural_rejections(self, monkeypatch, raw): + with pytest.raises(RuntimeError, match="fail-closed"): + self._cfg(monkeypatch, raw) + + @pytest.mark.parametrize("tag", ["", " ", "a\nb", "a\rb"]) + def test_bad_tags_rejected(self, monkeypatch, tag): + with pytest.raises(RuntimeError, match="tag"): + self._cfg(monkeypatch, {"segments": [{"tag": tag, "argv": ["x"]}]}) + + def test_duplicate_tag_rejected(self, monkeypatch): + with pytest.raises(RuntimeError, match="重复"): + self._cfg(monkeypatch, {"segments": [ + {"tag": "a", "argv": ["x"]}, {"tag": "a", "shell": "true"}]}) + + @pytest.mark.parametrize("seg", [ + {"tag": "a"}, # argv/shell 全无 + {"tag": "a", "argv": ["x"], "shell": "true"}, # argv/shell 并存 + {"tag": "a", "argv": []}, # 空词组(键在即占二选一位) + {"tag": "a", "argv": [], "shell": "true"}, # 按值判会双双漏过 XOR + {"tag": "a", "argv": ["ok", ""]}, # 空词 + {"tag": "a", "argv": "x"}, # argv 非数组 + {"tag": "a", "shell": " "}, # 空白命令串 + {"tag": "a", "shell": 42}, # shell 非字符串 + {"tag": "a", "argv": ["x"], "intra": "on"}, # intra 取值域 + {"tag": "a", "argv": ["x"], "stack": "pytest2"}, # 栈白名单 + {"tag": "a", "argv": ["x"], "typo": 1}, # 段级未知键 + {"tag": "a", "argv": ["x"], "cwd": " "}, # cwd 非空串 + ]) + def test_segment_rejections(self, monkeypatch, seg): + with pytest.raises(RuntimeError): + self._cfg(monkeypatch, {"segments": [seg]}) + + def test_argv_shell_value_errors_are_distinct(self, monkeypatch): + """空词组/空白命令串各自报值错误(键存在已占二选一位)。""" + with pytest.raises(RuntimeError, match="argv 词须为非空字符串"): + self._cfg(monkeypatch, {"segments": [{"tag": "a", "argv": []}]}) + with pytest.raises(RuntimeError, match="shell 须为非空字符串"): + self._cfg(monkeypatch, {"segments": [{"tag": "a", "shell": " "}]}) + + @pytest.mark.parametrize("workers", [-1, "2", 1.5, True]) + def test_bad_workers_rejected(self, monkeypatch, workers): + with pytest.raises(RuntimeError, match="workers"): + self._cfg(monkeypatch, { + "segments": [{"tag": "a", "argv": ["x"]}], "workers": workers}) + + def test_stack_whitelist_membership(self, monkeypatch): + raw = [{"tag": s, "argv": ["x"], "stack": s} + for s in sorted(fl._PARALLEL_STACKS)] + cfg = self._cfg(monkeypatch, {"segments": raw}) + assert cfg is not None + assert {seg["stack"] for seg in cfg["segments"]} == set(fl._PARALLEL_STACKS) + + +# ───────────────────────── detect_stack ───────────────────────── + +class TestDetectStack: + def _stack(self, tmp_path, files, contents=None): + contents = contents or {} + for name in files: + p = tmp_path / name + p.parent.mkdir(parents=True, exist_ok=True) + p.write_text(contents.get(name, "x"), encoding="utf-8") + return fl.detect_stack(tmp_path) + + @pytest.mark.parametrize(("files", "expect"), [ + (["pom.xml"], "maven"), + (["mvnw"], "maven"), + (["build.gradle"], "gradle"), + (["build.gradle.kts"], "gradle"), + (["settings.gradle"], "gradle"), + (["go.mod"], "go"), + (["Cargo.toml"], "cargo"), + (["phpunit.xml"], "phpunit"), + (["phpunit.xml.dist"], "phpunit"), + (["app.csproj"], "dotnet"), + (["solution.sln"], "dotnet"), + (["pytest.ini"], "pytest"), + (["conftest.py"], "pytest"), + ([], None), + ]) + def test_markers(self, tmp_path, files, expect): + assert self._stack(tmp_path, files) == expect + + def test_pyproject_tool_pytest(self, tmp_path): + assert self._stack(tmp_path, ["pyproject.toml"], + {"pyproject.toml": "[tool.pytest.ini_options]\n"}) == "pytest" + assert fl.detect_stack(tmp_path) == "pytest" + + def test_package_json_dep_based(self, tmp_path): + deps = {"dependencies": {"vitest": "^1.0"}} + (tmp_path / "package.json").write_text( + json.dumps(deps), encoding="utf-8") + assert fl.detect_stack(tmp_path) == "vitest" + + def test_package_json_jest_dep(self, tmp_path): + (tmp_path / "package.json").write_text( + json.dumps({"devDependencies": {"jest": "^29"}}), encoding="utf-8") + assert fl.detect_stack(tmp_path) == "jest" + + def test_vitest_precedes_jest(self, tmp_path): + (tmp_path / "package.json").write_text(json.dumps( + {"devDependencies": {"jest": "^29", "vitest": "^1.0"}}), + encoding="utf-8") + assert fl.detect_stack(tmp_path) == "vitest" + + def test_package_json_test_script(self, tmp_path): + (tmp_path / "package.json").write_text(json.dumps( + {"scripts": {"test": "jest --ci"}}), encoding="utf-8") + assert fl.detect_stack(tmp_path) == "jest" + + def test_corrupt_package_json_not_guessed(self, tmp_path): + (tmp_path / "package.json").write_text("{broken", encoding="utf-8") + assert fl.detect_stack(tmp_path) is None + + def test_build_markers_precede_package_json(self, tmp_path): + self._stack(tmp_path, ["pom.xml"]) + (tmp_path / "package.json").write_text( + json.dumps({"devDependencies": {"jest": "^29"}}), encoding="utf-8") + assert fl.detect_stack(tmp_path) == "maven" + + +# ───────────────────────── intra_parallel_args ───────────────────────── + +class TestIntraParallelArgs: + @pytest.mark.parametrize(("stack", "argv", "note_frag"), [ + ("maven", ["-T", "1C", "-DforkCount=1C", "-DreuseForks=true"], None), + ("gradle", ["--parallel"], None), + ("jest", ["--maxWorkers=50%"], None), + ("vitest", [], "默认已并行"), + ("go", [], "默认已并行"), + ("cargo", [], "默认已并行"), + ("phpunit", [], "段内串行"), + ("dotnet", [], "段内串行"), + (None, [], "未识别测试栈"), + ]) + def test_conservative_adapters(self, stack, argv, note_frag): + got_argv, got_note = fl.intra_parallel_args(stack, "python3") + assert got_argv == argv + if note_frag is None: + assert got_note is None + else: + assert note_frag in got_note + + def test_pytest_with_xdist(self, monkeypatch): + monkeypatch.setattr(fl, "_xdist_available", lambda _py: True) + assert fl.intra_parallel_args("pytest", "python3") == (["-n", "auto"], None) + + def test_pytest_xdist_missing_soft_degrades(self, monkeypatch): + monkeypatch.setattr(fl, "_xdist_available", lambda _py: False) + argv, note = fl.intra_parallel_args("pytest", "python3") + assert argv == [] + assert note is not None and "pytest-xdist" in note + + def test_maven_excludes_thread_interleaving(self): + """保守档契约:Maven 不注入 -Dparallel=methods(顺序敏感放大器)。""" + argv, _ = fl.intra_parallel_args("maven", "python3") + assert all("parallel=" not in w for w in argv) + + +# ───────────────────────── run_parallel_gate ───────────────────────── + +class TestRunParallelGate: + @pytest.fixture(autouse=True) + def _env(self, monkeypatch, private_tmp): + monkeypatch.delenv("PYTHON", raising=False) + monkeypatch.setenv("TMPDIR", str(private_tmp)) + monkeypatch.setattr(tempfile, "tempdir", str(private_tmp)) + self.tmp = private_tmp + + def _gate(self, root, segs, failed, workers=0): + return fl.run_parallel_gate( + repo_root=root, cfg={"workers": workers, "segments": segs}, + failed_tags_path=str(root / failed)) + + def test_green_replays_in_config_order_and_cleans_logs( + self, tmp_path, capsys): + rc = self._gate(tmp_path, [ + _argv_seg("slow-print", + "import time; time.sleep(0.3); print('slow-done')"), + _argv_seg("fast-print", "print('fast-done')"), + ], failed="tags.txt") + out = capsys.readouterr().out + assert rc == 0 + assert "✅ 并行段 2/2 全绿" in out + # 回放序=配置序:快段先完成也不抢跑 + assert out.index("── slow-print") < out.index("── fast-print") + assert out.index("slow-done") < out.index("fast-done") + assert (tmp_path / "tags.txt").read_text(encoding="utf-8") == "" + assert not list(self.tmp.glob("factory-parallel-gate.*")) + + def test_failure_retains_logs_and_writes_tags( + self, tmp_path, capsys): + rc = self._gate(tmp_path, [ + _argv_seg("good", "print('good-out')"), + _argv_seg("boom", "print('boom-err'); import sys; sys.exit(3)"), + ], failed="tags.txt") + out, err = capsys.readouterr() + assert rc == 1 + assert "❌ 失败段: boom" in out + assert "❌ 段日志保留" in err + assert out.index("── good") < out.index("── boom") + assert "boom-err" in out # 失败段日志已回放 + # 失败 tag 逐行落盘,尾带换行(宿主 read -r 逐行回收契约) + assert (tmp_path / "tags.txt").read_text(encoding="utf-8") == "boom\n" + # 段日志保留(成功即删的失败面) + retained = list(self.tmp.glob("factory-parallel-gate.*")) + assert len(retained) == 1 + assert any("boom-err" in p.read_text(encoding="utf-8") + for p in retained[0].glob("*.log")) + + def test_failure_tags_follow_config_order(self, tmp_path): + rc = self._gate(tmp_path, [ + _argv_seg("fail-late", + "import time; time.sleep(0.25); import sys; sys.exit(1)"), + _argv_seg("fail-early", "import sys; sys.exit(1)"), + ], failed="tags.txt") + assert rc == 1 + assert (tmp_path / "tags.txt").read_text(encoding="utf-8") \ + == "fail-late\nfail-early\n" + + def test_py_word_replaced_by_interpreter(self, tmp_path, capsys): + rc = self._gate(tmp_path, [ + _argv_seg("pyv", "import sys; print('exe=' + sys.executable)"), + ], failed="tags.txt") + assert rc == 0 + assert "exe=" in capsys.readouterr().out + + def test_shell_segment_gets_interpreter_via_dollar1( + self, tmp_path, capsys): + rc = self._gate(tmp_path, [ + {"tag": "sh", "shell": "\"$1\" -c 'print(\"sh-interp-ok\")'"}, + ], failed="tags.txt") + assert rc == 0 + assert "sh-interp-ok" in capsys.readouterr().out + + def test_shell_pipefail_propagates_real_code(self, tmp_path): + """管道段退出码不被 cat/tail 吞(ADR-002 缺陷类在编排器内的对应面)。""" + rc = self._gate(tmp_path, [ + {"tag": "piped", + "shell": "\"$1\" -c 'import sys; sys.exit(4)' | cat"}, + ], failed="tags.txt") + assert rc == 1 + assert (tmp_path / "tags.txt").read_text(encoding="utf-8") == "piped\n" + + def test_workers_one_serializes_segments(self, tmp_path): + m = tmp_path / "m" + code = ("import sys,time\n" + "p=sys.argv[1]\n" + "open(p+'.{s}','w').write(str(time.time()))\n" + "time.sleep(0.3)\n" + "open(p+'.{e}','w').write(str(time.time()))\n") + segs = [_argv_seg(t, code.format(s=f"{t}1", e=f"{t}2"), str(m)) + for t in ("wa", "wb")] + rc = self._gate(tmp_path, segs, failed="tags.txt", workers=1) + assert rc == 0 + # 有界=1:后段起点不早于前段终点(wa 先入槽) + assert float((tmp_path / "m.wb1").read_text()) \ + >= float((tmp_path / "m.wa2").read_text()) + + def test_workers_zero_overlaps_segments(self, tmp_path): + m = tmp_path / "m" + code = ("import sys,time\n" + "p=sys.argv[1]\n" + "open(p+'.{s}','w').write(str(time.time()))\n" + "time.sleep(0.3)\n" + "open(p+'.{e}','w').write(str(time.time()))\n" + "open(p+'.done','w').write('x')\n") + segs = [_argv_seg(t, code.format(s="start", e="end"), str(m) + t) + for t in ("oa", "ob")] + rc = self._gate(tmp_path, segs, failed="tags.txt") + assert rc == 0 + # 不限并发:后段在前段睡眠窗口内已起跑(真 fan-out 契约) + assert float((tmp_path / "mob.start").read_text()) \ + < float((tmp_path / "moa.end").read_text()) + + def test_missing_cwd_fails_closed_before_spawn(self, tmp_path, capsys): + rc = self._gate(tmp_path, [ + _argv_seg("ghost", "print('never')", cwd="no-such-dir"), + ], failed="tags.txt") + assert rc == 2 + assert "cwd 不存在" in capsys.readouterr().err + assert not (tmp_path / "tags.txt").exists() # 半跑状态零产出 + assert not list(self.tmp.glob("factory-parallel-gate.*")) + + def test_unrecognized_stack_note_deduplicated(self, tmp_path, capsys): + rc = self._gate(tmp_path, [ + _argv_seg("p1", "pass", intra="auto", stack="phpunit"), + _argv_seg("p2", "pass", intra="auto", stack="phpunit"), + ], failed="tags.txt") + assert rc == 0 + assert capsys.readouterr().out.count("phpunit 无 CLI 保守并行参数") == 1 + + def test_none_cfg_and_no_config_returns_2(self, tmp_path, monkeypatch, capsys): + monkeypatch.setattr(fl, "parallel_gate_cfg", lambda: None) + rc = fl.run_parallel_gate(repo_root=tmp_path) + assert rc == 2 + assert "未配置 parallel_gate" in capsys.readouterr().err + + def test_corrupted_cfg_converted_to_rc2_not_traceback( + self, tmp_path, monkeypatch, capsys): + """损坏配置 RuntimeError → rc 2:宿主脚本仅对 2 走 fail-closed 终止。""" + monkeypatch.setattr(fl, "_LOCAL_CFG", {"parallel_gate": 42}) + assert fl.run_parallel_gate(repo_root=tmp_path) == 2 + assert "fail-closed" in capsys.readouterr().err + + def test_segments_run_as_own_session_leader(self, tmp_path): + """start_new_session 契约:段进程即会话组长(getsid==getpid)。""" + rc = self._gate(tmp_path, [ + _argv_seg("grp", "import os, sys\n" + "sys.exit(0 if os.getsid(0) == os.getpid() else 3)"), + ], failed="tags.txt") + assert rc == 0 + + def test_interrupt_terminates_segment_group( + self, tmp_path, capsys, monkeypatch): + """KeyboardInterrupt:SIGTERM 收段组并收割,段日志保留取证。""" + pid_file = tmp_path / "seg.pid" + code = ("import os, time\n" + f"with open({str(pid_file)!r}, 'w') as f:\n" + " f.write(str(os.getpid()))\n" + "time.sleep(30)\n") + + fired = [] + + def _ctrl_c_once(secs): + # 单次 Ctrl-C 建模:段起跑后只抛一次,其余真睡——否则清理路径 + # 里 Popen.wait 的内部轮询 sleep 会连环再抛,收尸永不执行 + if pid_file.exists() and not fired: + fired.append(1) + raise KeyboardInterrupt + _REAL_SLEEP(min(secs, 0.05)) + + monkeypatch.setattr(fl.time, "sleep", _ctrl_c_once) + with pytest.raises(KeyboardInterrupt): + fl.run_parallel_gate(repo_root=tmp_path, cfg={"workers": 0, "segments": [ + {"tag": "hang", "argv": ["$PY", "-c", code]}]}) + assert "并行门中断,段日志保留" in capsys.readouterr().err + _assert_dead(int(pid_file.read_text())) + assert list(self.tmp.glob("factory-parallel-gate.*")) # 取证保留 + + def test_interrupt_sigkills_term_ignoring_group( + self, tmp_path, monkeypatch): + """TERM 免疫段(trap '' TERM):限宽后 SIGKILL 收组。""" + pid_file = tmp_path / "stubborn.pid" + monkeypatch.setattr(fl, "_TERM_GRACE_SECS", 0.3) + + fired = [] + + def _ctrl_c_once(secs): + # 同上:单次 Ctrl-C,其余真睡 + if pid_file.exists() and not fired: + fired.append(1) + raise KeyboardInterrupt + _REAL_SLEEP(min(secs, 0.05)) + + monkeypatch.setattr(fl.time, "sleep", _ctrl_c_once) + with pytest.raises(KeyboardInterrupt): + fl.run_parallel_gate(repo_root=tmp_path, cfg={"workers": 0, "segments": [ + {"tag": "stubborn", + "shell": f"echo $$ > '{pid_file}'; trap '' TERM; " + "while :; do sleep 30; done"}]}) + _assert_dead(int(pid_file.read_text())) + + def test_second_interrupt_during_grace_still_reaps_and_raises_original( + self, tmp_path, capsys, monkeypatch): + """限宽窗内二次中断:升级 SIGKILL 收割,原始异常照抛(不被顶替)。""" + pid_file = tmp_path / "twice.pid" + + class _SecondCtrlC(BaseException): + pass + + fired = [] + + def _ctrl_c_twice(secs): + # 第 1 次中断主循环轮询;第 2 次落进清理 p.wait 的限宽窗—— + # 未同路升级的清理会从 wait 逃逸,跳过 SIGKILL/收割并顶替原始异常 + if pid_file.exists() and len(fired) < 2: + fired.append(1) + if len(fired) == 1: + raise KeyboardInterrupt + raise _SecondCtrlC() + _REAL_SLEEP(min(secs, 0.05)) + + monkeypatch.setattr(fl.time, "sleep", _ctrl_c_twice) + with pytest.raises(KeyboardInterrupt): + fl.run_parallel_gate(repo_root=tmp_path, cfg={"workers": 0, "segments": [ + {"tag": "twice", + "shell": f"echo $$ > '{pid_file}'; trap '' TERM; " + "while :; do sleep 30; done"}]}) + assert "并行门中断,段日志保留" in capsys.readouterr().err + _assert_dead(int(pid_file.read_text())) + + +# ───────────────────────── CLI(main parallel-gate) ───────────────────────── + +class TestParallelGateCli: + def test_unknown_arg_rejected(self, capsys): + assert fl.main(["factory_lib.py", "parallel-gate", "--bogus"]) == 2 + assert "未知参数" in capsys.readouterr().err + + def test_failed_tags_missing_path_rejected(self, capsys): + assert fl.main(["factory_lib.py", "parallel-gate", "--failed-tags"]) == 2 + assert "缺路径参数" in capsys.readouterr().err + + def test_dispatch_passes_failed_tags_path(self, monkeypatch): + seen = {} + + def fake_root(**kwargs): + seen.update(kwargs) + return 5 + + monkeypatch.setattr(fl, "run_parallel_gate", fake_root) + assert fl.main(["factory_lib.py", "parallel-gate", "--failed-tags", "/tmp/x"]) == 5 + assert seen == {"failed_tags_path": "/tmp/x"} + + def test_dispatch_default_no_failed_tags(self, monkeypatch): + seen = {} + + def fake_root(**kwargs): + seen.update(kwargs) + return 0 + + monkeypatch.setattr(fl, "run_parallel_gate", fake_root) + assert fl.main(["factory_lib.py", "parallel-gate"]) == 0 + assert seen == {"failed_tags_path": None} diff --git a/.factory/tests/test_regression_routing.py b/.factory/tests/test_regression_routing.py new file mode 100644 index 0000000..6bf3f9f --- /dev/null +++ b/.factory/tests/test_regression_routing.py @@ -0,0 +1,110 @@ +"""regression_routing 负控制:日回归失败投递路由(L2 滞留接线)。 + +2026-09-15 issue #165 实证:旧实现只看标题复用 open 回归 issue,失败结果 +投进 in-progress 留守(派发器永久跳过)的容器,5 天静默。本套件钉死 +路由决策三态与 #165 事故形态——若路由回退为 append-only,用例即红。 +""" +import io +import json +import sys +import time + +import factory_lib + + +def _issue(labels): + return {"number": 165, "title": "[factory-regression] x", "labels": labels} + + +# -- 正向:四态路由(lease_alive 维度)-------------------------------------- + +def test_in_progress_stalled_routes_wake(): + # #165 事故形态:零改动轮留守 accepted+in-progress,租约已死 + assert factory_lib.regression_routing( + _issue(["factory:accepted", "factory:in-progress"]), + lease_alive=False) == "wake" + + +def test_in_progress_live_chain_routes_live(): + # 审查 major(2026-09-16):修复链在途时剥 in-progress 会使 §7.5 + # 留守对该轮失效——必须只追加不唤醒 + assert factory_lib.regression_routing( + _issue(["factory:in-progress"]), lease_alive=True) == "live" + + +def test_in_progress_unknown_lease_routes_live(): + # PG 形态/判定失败:保守不自动唤醒 + assert factory_lib.regression_routing( + _issue(["factory:in-progress"]), lease_alive=None) == "live" + + +def test_in_progress_default_unknown_routes_live(): + assert factory_lib.regression_routing( + _issue(["factory:in-progress"])) == "live" + + +def test_rejected_routes_new(): + assert factory_lib.regression_routing(_issue(["factory:rejected"])) == "new" + + +def test_plain_and_human_states_route_append(): + for labels in ([], ["factory:accepted"], ["factory:in-review"], + ["factory:needs-human"]): + assert factory_lib.regression_routing(_issue(labels)) == "append" + + +# -- 负控制:事故路径必须不是 append ---------------------------------------- + +def test_in_progress_stalled_must_not_append_or_live(): + # 负控制(#165 事故回归面):滞留态若路由 append/live,失败结果进死信筒 + assert factory_lib.regression_routing( + _issue(["factory:in-progress"]), lease_alive=False) == "wake" + + +def test_live_chain_must_not_wake(): + # 负控制(审查 major):存活链被唤醒 = 留守语义被破坏 + 同诉求重派 + assert factory_lib.regression_routing( + _issue(["factory:in-progress"]), lease_alive=True) != "wake" + + +def test_rejected_must_not_append(): + # 负控制:rejected 容器若路由 append,新失败进人工已拒死信筒 + assert factory_lib.regression_routing(_issue(["factory:rejected"])) != "append" + + +def test_anomalous_both_labels_prefers_wake(): + # 状态机互斥被数据打破时,可自动处置的路由优先(须以滞留态为前提) + assert factory_lib.regression_routing( + _issue(["factory:in-progress", "factory:rejected"]), + lease_alive=False) == "wake" + + +def test_missing_labels_key_tolerated(): + assert factory_lib.regression_routing({"number": 1}) == "append" + + +# -- CLI 子命令:stdin JSON → stdout 决策 ----------------------------------- + +def test_cli_subcommand(monkeypatch, capfd): + monkeypatch.setattr("sys.stdin", io.StringIO(json.dumps(_issue( + ["factory:accepted", "factory:in-progress"])))) + rc = factory_lib.main(["factory_lib.py", "regression-routing", "dead"]) + out = capfd.readouterr().out.strip() + assert rc == 0 and out == "wake" + + +def test_cli_lease_fresh(tmp_path, capfd): + lock = tmp_path / "issue:1.lock" + lock.write_text("mid|7|123|0|900") # 新建文件 mtime=now → 活 + assert factory_lib.main(["factory_lib.py", "lease-fresh", str(lock)]) == 0 + import os + old = time.time() - 2000 + os.utime(lock, (old, old)) # mtime+900 < now → 过期残锁 + assert factory_lib.main(["factory_lib.py", "lease-fresh", str(lock)]) == 1 + assert factory_lib.main( + ["factory_lib.py", "lease-fresh", str(tmp_path / "none.lock")]) == 1 + # 畸形内容(单字段非数字)回退 900:新鲜 mtime 仍判活,过期判死 + lock.write_text("garbage") + assert factory_lib.main(["factory_lib.py", "lease-fresh", str(lock)]) == 0 + os.utime(lock, (old, old)) + assert factory_lib.main(["factory_lib.py", "lease-fresh", str(lock)]) == 1 diff --git a/.factory/tests/test_rejected_reconcile.py b/.factory/tests/test_rejected_reconcile.py index 2a807ea..b239e4b 100644 --- a/.factory/tests/test_rejected_reconcile.py +++ b/.factory/tests/test_rejected_reconcile.py @@ -2,6 +2,7 @@ 场景锚定真实事故形态:4 个 rejected issue 修复已进 main 但 issue 未关。 机器不可判定"是否已修复",只暴露处置信号(回执后人工评论数)。 +2026-09-20 #207:只落标无回执 = 链完整性违规,has_receipt 一等字段。 """ import json @@ -16,6 +17,12 @@ RECEIPT_BODY = "## 工厂 triage 裁决:reject —— 判据 b 不通过" +def _receipt(n): + """真实链回执形态(线上实证):判据正文 + 尾埋幂等 marker 注释 + (issue_comment --marker 追加,渲染不可见但 body 稳定携带)。""" + return f"{RECEIPT_BODY}\n" + + def _issue(n, comments): return {"number": n, "title": f"issue-{n}", "comments": comments} @@ -24,9 +31,10 @@ def test_human_comment_after_receipt_counted(): """回执后的人工评论 = 处置信号(审计实证的「已修未关」形态)。""" it = rejected_reconcile([_issue(23, [ {"author": "sourcery-ai[bot]", "body": "review"}, - {"author": "im47cn", "body": RECEIPT_BODY}, # 链回执(bot 语义) + {"author": "im47cn", "body": _receipt(23)}, # 链回执(marker) {"author": "im47cn", "body": "已人工修复,PR #27"}, # 回执后人工评论 ])])[0] + assert it["has_receipt"] is True assert it["human_comments_after_reject"] == 1 @@ -34,7 +42,7 @@ def test_bot_and_receipt_comments_excluded(): """回执本体与 bot 评论不计——回执前的人工评论也不计(裁决前语境)。""" it = rejected_reconcile([_issue(24, [ {"author": "im47cn", "body": "裁决前的讨论"}, # 回执前:不算 - {"author": "im47cn", "body": RECEIPT_BODY}, + {"author": "im47cn", "body": _receipt(24)}, {"author": "github-actions[bot]", "body": "CI done"}, # bot:不算 ])])[0] assert it["human_comments_after_reject"] == 0 @@ -43,18 +51,42 @@ def test_bot_and_receipt_comments_excluded(): def test_latest_receipt_wins_when_multiple(): """多轮回执取最后一轮为界——重投后再拒,只看最新裁决之后。""" it = rejected_reconcile([_issue(5, [ - {"author": "im47cn", "body": RECEIPT_BODY}, # round0 回执 + {"author": "im47cn", "body": _receipt(5)}, # round0 回执 {"author": "im47cn", "body": "按指引重投"}, # round0 后(应忽略) - {"author": "im47cn", "body": RECEIPT_BODY}, # round1 回执 + {"author": "im47cn", "body": _receipt(5)}, # round1 回执 ])])[0] + assert it["has_receipt"] is True assert it["human_comments_after_reject"] == 0 -def test_no_receipt_counts_all_human(): - """无回执(历史形态/批次落标无评论)→ 全部人工评论计为处置信号。""" +def test_no_receipt_flags_integrity_violation(): + """无回执(#207 实证形态:落标假阴性早退 → 只落标无评论)= 链 + 完整性违规:has_receipt=False 优先报告;提交/裁决前的人工评论 + ≠ 处置信号,不计(2026-09-20 语义修正,旧契约曾全量计数)。""" it = rejected_reconcile([_issue(3, [ {"author": "im47cn", "body": "人工处置说明"}, ])])[0] + assert it["has_receipt"] is False + assert it["human_comments_after_reject"] == 0 + +def test_human_quoted_heading_is_not_receipt(): + """PR #211 Sourcery 评论2:人工评论复述回执标题(无 marker)不得 + 冒充链回执——has_receipt=False 完整性违规照报,不进宽限语义。""" + it = rejected_reconcile([_issue(31, [ + {"author": "im47cn", "body": RECEIPT_BODY}, # 人工复述标题,无 marker + ])])[0] + assert it["has_receipt"] is False + assert it["human_comments_after_reject"] == 0 + + +def test_human_quoted_heading_after_receipt_counts_as_human(): + """真回执之后人工复述标题讨论 = 人工活动(marker 判据不再被标题 + 排除误吞)——有人跟进就该计为处置信号。""" + it = rejected_reconcile([_issue(32, [ + {"author": "im47cn", "body": _receipt(32)}, + {"author": "im47cn", "body": RECEIPT_BODY}, # 人工引用回执标题 + ])])[0] + assert it["has_receipt"] is True assert it["human_comments_after_reject"] == 1 @@ -66,6 +98,7 @@ def test_malformed_entries_fail_open(): {"number": 3, "title": "t", "comments": [42, {"body": "x"}]}, ]) assert [r["human_comments_after_reject"] for r in out] == [0, 0, 0] + assert [r["has_receipt"] for r in out] == [False, False, False] def test_title_truncated(): @@ -78,7 +111,7 @@ def test_title_truncated(): def test_cli_tsv_smoke(): """CLI: stdin JSON → TSV(number\tcount\ttitle)——dispatch 消费契约。""" payload = json.dumps([_issue(23, [ - {"author": "im47cn", "body": RECEIPT_BODY}, + {"author": "im47cn", "body": _receipt(23)}, {"author": "im47cn", "body": "已修"}, ])]) proc = subprocess.run( diff --git a/.factory/tests/test_salvage_wip.py b/.factory/tests/test_salvage_wip.py new file mode 100644 index 0000000..f48771a --- /dev/null +++ b/.factory/tests/test_salvage_wip.py @@ -0,0 +1,85 @@ +"""salvage_wip 真实逻辑回归(#14 补遗,#165 r1-r3 事故)——提取真实函数体执行。 + +事故背景:#165 r1-r3 implement 未提交即死(rev-list=0),EXIT trap 的 +salvage push 无物可推,WIP 随 `worktree remove --force` 湮灭。salvage_wip +兜底未提交面:intent-to-add 使 untracked 入 diff,binary diff 快照到 +ISSUE_DIR(下轮 implement 的证据输入,不自动续作)。 + +对齐 test_b1_fallback 的提取哲学:正则锚定函数名提取真实代码源化执行, +不复制逻辑——改 fix-issue.sh 不改本测试即红。 + +运行:python3 -m pytest .factory/tests -q +""" + +import re +import subprocess +from pathlib import Path + +FACTORY = Path(__file__).resolve().parents[1] + +_FUNC = re.search( + r"\n( salvage_wip\(\) \{.*?\n \})\n", + (FACTORY / "fix-issue.sh").read_text(encoding="utf-8"), re.S) +assert _FUNC is not None, "fix-issue.sh 缺 salvage_wip 函数体——锚定失效即红" +FUNC = _FUNC[1] +_DRIVER = r"""#!/usr/bin/env bash +set -euo pipefail +DIR="__DIR__" +WT="__WT__" +ROUND=3 +__FUNC__ +salvage_wip "$1" +""" + + +def _git(repo: Path, *args: str) -> None: + subprocess.run(["git", "-C", str(repo), *args], check=True, capture_output=True) + + +def _repo(tmp_path: Path, dirty: bool) -> Path: + """临时 git 仓 + 基线提交;dirty=True 时留未提交改动 + untracked。""" + repo = tmp_path / "wt" + repo.mkdir() + _git(repo, "init", "-q") + _git(repo, "config", "user.email", "t@t") + _git(repo, "config", "user.name", "t") + (repo / "f1").write_text("base\n", encoding="utf-8") + _git(repo, "add", ".") + _git(repo, "commit", "-qm", "base") + if dirty: + (repo / "f1").write_text("modified\n", encoding="utf-8") + (repo / "f2").write_text("untracked\n", encoding="utf-8") + return repo + + +def _run_salvage(tmp_path: Path, rc: str, dirty: bool): + repo = _repo(tmp_path, dirty) + issue_dir = tmp_path / "issue" + issue_dir.mkdir() + drv = tmp_path / "drv.sh" + drv.write_text(_DRIVER + .replace("__DIR__", str(issue_dir)) + .replace("__WT__", str(repo)) + .replace("__FUNC__", FUNC), encoding="utf-8") + r = subprocess.run(["/bin/bash", str(drv), rc], capture_output=True, text=True) + assert r.returncode == 0, r.stderr # #23 纪律:best-effort 恒 return 0 + patch = issue_dir / "salvage-r3.patch" + return patch.read_text(encoding="utf-8") if patch.exists() else None + + +def test_dirty_worktree_snapshots_tracked_and_untracked(tmp_path): + """rc≠0 + 有未提交面:patch 含 tracked 修改与 untracked 新文件。""" + patch = _run_salvage(tmp_path, "1", dirty=True) + assert patch, "未提交 WIP 未落快照" + assert "modified" in patch # tracked 修改入快照 + assert "untracked" in patch and "new file" in patch # -N 使 untracked 入 diff + + +def test_clean_worktree_leaves_no_patch(tmp_path): + """rc≠0 但零改动:不留噪声 patch 文件。""" + assert _run_salvage(tmp_path, "1", dirty=False) is None + + +def test_success_skips_snapshot(tmp_path): + """rc=0(成功链):不产 patch——成功路径产物在分支本身。""" + assert _run_salvage(tmp_path, "0", dirty=True) is None diff --git a/.factory/tests/test_sourcery_gate.py b/.factory/tests/test_sourcery_gate.py new file mode 100644 index 0000000..3b53e05 --- /dev/null +++ b/.factory/tests/test_sourcery_gate.py @@ -0,0 +1,231 @@ +"""Sourcery 闸语言口径与引号展开回归(2026-09-05)——真实执行 sourcery-gate.sh。 + +事故背景:issue #123——纯 PHP 仓本地 sourcery 硬闸是安慰剂(.php 实测不 +扫描,送审零输出静默空转 = fail-open);CI 侧各仓 yml 同病。PR #141 +(ac71ae0 + 21351a6)修 tools/git/lefthook/sourcery-gate.sh 与 +.github/workflows/sourcery-review-gate.yml:SUPPORTED=py/ts/js 进闸面, +UNSENT=go/java/cs/php 显式披露不进闸面。21351a6 另修 yml 侧字符串分词/ +glob 真缺陷(文件名含空格会拆参)。#141 无自动化测试——本测试锚定该 +行为契约防回归: + +- opt-in:无 .sourcery.yaml → 跳过(fail-safe);未装 sourcery CLI → 跳过 +- 纯面外语集 → 显式降级披露 + exit 0 + 不调用 sourcery(防静默 fail-open) +- 混合集 → 只 review 实测支持子集 + ⚠ 披露未送审清单 +- 无语言文件 → 跳过;.lefthook/(上游分发面)文件不审判 +- 含空格文件名 → 单参数传递(21351a6 引号展开修复锚定) +- sourcery review 非零 → 闸透传非零并给跳过提示(硬闸语义) +- CLI 在装但认证/订阅失效(rc≠0 + 认证类错误输出)→ 显式降级 exit 0 + (2026-09-13 防砖化:trial 到期不得拦死一切 push;CI 侧 fail-closed 兜底) + +运行:python3 -m pytest .factory/tests -q(沙箱同构:tmp 工作目录 + +PATH 桩 sourcery 记录 args/rc,真实调用目标脚本本体)。 + +与 Sourcery CLI 版本无关:不依赖真实 sourcery,桩记录调用形状。 +""" + +import os +import subprocess +from pathlib import Path + +import pytest + +FACTORY = Path(__file__).resolve().parents[1] # .factory/ +GATE = FACTORY.parent / "tools/git/lefthook/sourcery-gate.sh" + +# 仓侧闸脚本在 .factory 周界外,DISTRIBUTION.json 无法分发——未采用 +# tools/git/lefthook 布局的下游仓整文件缺失,15 用例永红(2026-09-27 +# etf-radar 追平实证)。opt-in 闸本就是仓侧选择,缺脚本即跳过。 +pytestmark = pytest.mark.skipif( + not GATE.exists(), + reason="仓侧 opt-in 闸脚本缺失(tools/git/lefthook 布局未采用),跳过", +) + +_SOURCERY_STUB = """#!/bin/sh +echo "argc=$#" >> "$STUB_LOG" +for a in "$@"; do printf 'arg=%s\\n' "$a" >> "$STUB_LOG"; done +[ -n "${STUB_OUT:-}" ] && printf '%s\\n' "$STUB_OUT" +exit "${STUB_RC:-0}" +""" + +_BASE_PATH = "/usr/bin:/bin:/usr/sbin:/sbin" + + +def _run_gate(tmp_path: Path, files: list[str], *, sourcery_yaml=True, + with_cli=True, stub_rc=0, stub_out="") -> tuple[subprocess.CompletedProcess, list[str]]: + """tmp 工作目录 + 可选 .sourcery.yaml + 可选桩 sourcery;返回 (result, stub 调用行)。""" + work = tmp_path / "work" + work.mkdir() + if sourcery_yaml: + (work / ".sourcery.yaml").write_text("# 评审保留清单(测试桩)\n", encoding="utf-8") + for f in files: + p = work / f + p.parent.mkdir(parents=True, exist_ok=True) + p.touch() + path = _BASE_PATH + log = tmp_path / "stub.log" + if with_cli: + bindir = tmp_path / "bin" + bindir.mkdir() + (bindir / "sourcery").write_text(_SOURCERY_STUB, encoding="utf-8") + (bindir / "sourcery").chmod(0o755) + path = f"{bindir}:{path}" + else: + bindir = tmp_path / "empty-bin" + bindir.mkdir() + path = str(bindir) + env = dict(os.environ, PATH=path, STUB_LOG=str(log), STUB_RC=str(stub_rc), + STUB_OUT=stub_out) + r = subprocess.run(["/bin/bash", str(GATE), *files], cwd=str(work), env=env, + capture_output=True, text=True, timeout=30) + calls = log.read_text(encoding="utf-8").splitlines() if log.exists() else [] + return r, calls + + +def test_pure_unsent_language_degrades_without_calling(tmp_path): + """纯 php(实测不扫描)→ 降级披露 + exit 0 + sourcery 不调用(#123 主诉)。""" + r, calls = _run_gate(tmp_path, ["a.php"]) + assert r.returncode == 0, r.stderr + assert "实测支持面外" in r.stdout and "降级跳过" in r.stdout + assert "该集不经 Sourcery 评审" in r.stdout + assert calls == [], f"纯面外语不应调用 sourcery:{calls}" + + +def test_untested_languages_disclosed_not_silently_passed(tmp_path): + """go/java/cs(未实测面)→ 披露 + 不调用(防静默 fail-open)。""" + r, calls = _run_gate(tmp_path, ["a.go", "b.java", "c.cs"]) + assert r.returncode == 0, r.stderr + assert "实测支持面外" in r.stdout + assert calls == [], f"未实测语言不应进闸面:{calls}" + + +def test_mixed_reviews_supported_discloses_unsent(tmp_path): + """py+php 混合 → 只 review py + ⚠ 披露 php 清单。""" + r, calls = _run_gate(tmp_path, ["a.py", "b.php"]) + assert r.returncode == 0, r.stderr + assert "⚠ 1 个语言文件未送审" in r.stdout and "b.php" in r.stdout + assert "review --check:1 个实测支持文件" in r.stdout + assert calls and calls[0] == "argc=5", f"应精确 5 参:{calls}" + assert "arg=a.py" in calls and "arg=b.php" not in calls, calls + + +def test_supported_languages_all_reviewed(tmp_path): + """py/ts/js 全进闸面,单次调用精确传参。""" + r, calls = _run_gate(tmp_path, ["x.py", "y.ts", "z.js"]) + assert r.returncode == 0, r.stderr + assert "review --check:3 个实测支持文件" in r.stdout + assert calls and calls[0] == "argc=7", calls # review --check --config .sourcery.yaml + 3 文件 + for want in ["arg=review", "arg=--check", "arg=--config", + "arg=.sourcery.yaml", "arg=x.py", "arg=y.ts", "arg=z.js"]: + assert want in calls, f"缺 {want}:{calls}" + + +def test_filename_with_spaces_stays_single_argument(tmp_path): + """含空格文件名 → 单参数传递(21351a6 引号展开修复锚定,分词/glob 回归)。""" + r, calls = _run_gate(tmp_path, ["my file.py"]) + assert r.returncode == 0, r.stderr + assert calls and calls[0] == "argc=5", f"5 参(含整名文件)实得:{calls}" + assert "arg=my file.py" in calls, f"文件名被拆参:{calls}" + assert "arg=my" not in calls and "arg=file.py" not in calls, f"分词复现:{calls}" + + +def test_lefthook_upstream_mirror_excluded(tmp_path): + """.lefthook/(上游分发面)不审判——即使扩展名在支持面内。""" + r, calls = _run_gate(tmp_path, [".lefthook/hook.py", "real.py"]) + assert r.returncode == 0, r.stderr + assert "review --check:1 个实测支持文件" in r.stdout + assert calls and "arg=real.py" in calls and "arg=.lefthook/hook.py" not in calls, calls + + +def test_sourcery_failure_blocks_with_skip_hint(tmp_path): + """sourcery review 非零 → 闸透传非零 + 跳过提示(硬闸语义)。""" + r, calls = _run_gate(tmp_path, ["a.py"], stub_rc=1) + assert r.returncode == 1, r.stderr + assert "存在未解决 issue,push 被拦" in r.stdout + assert "--no-verify" in r.stdout + + +def test_no_language_files_skips(tmp_path): + """无语言文件变更 → 跳过 exit 0(README/LICENSE 不进任何面)。""" + r, calls = _run_gate(tmp_path, ["README.md", "LICENSE"]) + assert r.returncode == 0, r.stderr + assert "无语言文件变更,跳过" in r.stdout + assert calls == [] + + +def test_not_opted_in_skips(tmp_path): + """无 .sourcery.yaml → fail-safe 跳过(opt-in 门禁)。""" + r, calls = _run_gate(tmp_path, ["a.py"], sourcery_yaml=False) + assert r.returncode == 0, r.stderr + assert "未 opt-in" in r.stdout + assert calls == [] + + +def test_missing_cli_skips(tmp_path): + """未装 sourcery CLI → fail-safe 跳过(不因环境缺失误伤)。""" + r, calls = _run_gate(tmp_path, ["a.py"], with_cli=False) + assert r.returncode == 0, r.stderr + assert "未安装 sourcery CLI" in r.stdout + assert calls == [] + + +def test_auth_failure_degrades_explicitly(tmp_path): + """CLI 在装但认证/订阅失效(rc≠0 + 认证类错误输出)→ 显式降级 exit 0(防砖化)。 + + 2026-09-13:trial 到期后 review 若认证失败,闸不得拦死一切含 py/ts/js 的 + push——降级须显式(可审计非静默),且 CI 侧仍 fail-closed 兜底。 + """ + r, calls = _run_gate(tmp_path, ["a.py"], stub_rc=1, + stub_out="Error: Your trial has ended. Please update your payment method.") + assert r.returncode == 0, r.stdout + r.stderr + assert "认证/订阅失效" in r.stdout and "降级" in r.stdout + assert "push 被拦" not in r.stdout, "认证失效不得按代码 issue 拦截" + assert calls and calls[0] == "argc=5", calls # 确曾真实调过 CLI(非提前跳过) + + +def test_issue_failure_with_output_still_blocks(tmp_path): + """rc≠0 + issue 表格输出(无认证关键词)→ 仍硬拦(降级仅限认证失败形态)。""" + r, calls = _run_gate(tmp_path, ["a.py"], stub_rc=1, + stub_out="tools/x.py:10-12 reintroduce-else\nTotal: 1 issue") + assert r.returncode == 1, r.stdout + r.stderr + assert "push 被拦" in r.stdout and "--no-verify" in r.stdout + assert "认证/订阅失效" not in r.stdout, "issue 输出被误判为认证失败" + + +def test_auth_keyword_in_issue_diagnostic_still_blocks(tmp_path): + """认证词混入 issue 诊断(评审 #178 bug_risk 场景)→ issue 形态优先,仍硬拦。 + + 文件名含 unauthorized、错误行含 subscription 等宽词散落形态不得放行; + 降级仅当输出无 issue 计数且认证词锚定在错误行/完整句式上。 + """ + r, calls = _run_gate( + tmp_path, ["a.py"], stub_rc=1, + stub_out="tools/unauthorized.py:10-12 use-named-expression\n" + " • 1 issue detected.\n" + "Error: subscription quota exceeded") # 宽词 + issue 计数混合 + assert r.returncode == 1, r.stdout + r.stderr + assert "push 被拦" in r.stdout, "含 issue 计数的输出不得降级" + assert "认证/订阅失效" not in r.stdout + + +def test_pure_error_line_auth_degrades(tmp_path): + """行首锚定的认证错误行(无 issue 计数)→ 降级放行(401/login 形态)。""" + r, calls = _run_gate(tmp_path, ["a.py"], stub_rc=1, + stub_out="Error: 401 Unauthorized. Please log in with `sourcery login`.") + assert r.returncode == 0, r.stdout + r.stderr + assert "认证/订阅失效" in r.stdout + assert "push 被拦" not in r.stdout + + +def test_trial_banner_with_issues_still_blocks(tmp_path): + """试用提示横幅(ends on,进行时)+ issue 计数 → 仍硬拦(在期真实形态)。 + + 横幅是 stderr 噪音且用词为 "ends on"(非 ended/expired),不得触发降级; + 本形态即试用期内含 issue 的真实 push 输出,是防误降级的主线场景。 + """ + r, calls = _run_gate( + tmp_path, ["a.py"], stub_rc=1, + stub_out="Your trial ends on 2026-09-13, update your payment method\n" + " • 4 issues detected.") + assert r.returncode == 1, r.stdout + r.stderr + assert "push 被拦" in r.stdout + assert "认证/订阅失效" not in r.stdout diff --git a/.factory/tests/test_state.py b/.factory/tests/test_state.py index e16de94..7e79601 100644 --- a/.factory/tests/test_state.py +++ b/.factory/tests/test_state.py @@ -154,10 +154,10 @@ def test_helpers(): {"op": "remove", "label": "factory:needs-fix"}] assert state._needs_fix_rounds(ev) == 1 assert state._needs_fix_rounds(None) == 0 - assert state._linked_issue(_pr()) == "9" # ADR-008:编号统一字符串(Codeup 序号 KFPT-16) + assert state._linked_issue(_pr()) == "9" # ADR-008:编号统一字符串(Codeup 序号 T-16) assert state._linked_issue({"body": None}) is None assert state._linked_issue({"body": "无关正文"}) is None - assert state._linked_issue({"body": "Closes #KFPT-16"}) == "KFPT-16" + assert state._linked_issue({"body": "Closes #T-16"}) == "T-16" def test_cli_table_and_plan(tmp_path): diff --git a/.factory/tests/test_sync_from_upstream.py b/.factory/tests/test_sync_from_upstream.py index b5209dc..92ab163 100644 --- a/.factory/tests/test_sync_from_upstream.py +++ b/.factory/tests/test_sync_from_upstream.py @@ -1,7 +1,7 @@ """sync-from-upstream.sh 回归测试 —— 补齐缺失文件分支(脚本首测)。 缺陷→测试映射: -- 缺失父目录写入崩溃(wop-skills 2026-08-31 事故:tests/ 目录整缺, +- 缺失父目录写入崩溃(xx-skills 2026-08-31 事故:tests/ 目录整缺, 「本地缺失」补齐分支对不存在路径直接重定向 → No such file or directory, apply 中途崩、锚点未写)→ TestApplyMissingParentDir:mkdir -p 补齐 + blob 落地 + mode 恢复 + 锚点写入 + rc=0 @@ -21,7 +21,6 @@ """ import glob -import os import json import re import subprocess @@ -184,7 +183,7 @@ def test_first_commit_bootstrap_blame_ignore(self, repos): proc = self._run(dn, str(up), "--apply", "--commit", "--anchor", "main") assert proc.returncode == 0, proc.stdout + proc.stderr assert _head_count(dn) == 2, "单提交落库(fixture 1 + 追平 1)" - assert _head_subject(dn) == f"factory: 上游同步追平({anchor[:9]})" + assert _head_subject(dn) == f"chore(factory): 上游同步追平({anchor[:9]})" ignore = dn / ".git-blame-ignore-revs" lines = ignore.read_text(encoding="utf-8").splitlines() assert lines and lines[0].startswith("#"), "带说明头" @@ -230,7 +229,7 @@ def test_second_sync_records_previous_sync_sha(self, repos): proc = self._run(dn, str(up), "--apply", "--commit", "--anchor", "main") assert proc.returncode == 0, proc.stdout + proc.stderr assert _head_count(dn) == 3 - assert _head_subject(dn) == f"factory: 上游同步追平({anchor2[:9]})" + assert _head_subject(dn) == f"chore(factory): 上游同步追平({anchor2[:9]})" lines = (dn / ".git-blame-ignore-revs").read_text(encoding="utf-8").splitlines() assert first_sync in lines, "滞后一条:本轮记上一轮追平提交" @@ -392,8 +391,8 @@ def test_synced_repo_bootstrap_makes_no_commit_but_sets_config(self, repos): assert status.strip() == "?? .git-blame-ignore-revs", \ "首建 ignore 留工作树(untracked),随下次真追平入库" - def test_sourcery_rejection_leaves_no_temp_files(self, repos, tmp_path): - """评论 3:Sourcery 拒绝(set -e 中途退出)后 /tmp 无暂存文件泄漏。""" + def test_sourcery_rejection_leaves_no_temp_files(self, repos, tmp_path, private_tmp): + """评论 3:Sourcery 拒绝(set -e 中途退出)后无暂存文件泄漏。""" up, dn, _ = repos fake = tmp_path / "bin" fake.mkdir() @@ -407,15 +406,15 @@ def test_sourcery_rejection_leaves_no_temp_files(self, repos, tmp_path): (fake / "sourcery").chmod(0o755) env = _run_env() env["PATH"] = f"{fake}:{env['PATH']}" - tmp_root = os.environ.get("TMPDIR") or "/tmp" - before = (set(glob.glob(f"{tmp_root}/.factory-stage.*")) - | set(glob.glob(f"{tmp_root}/.factory-dist.*"))) + env["TMPDIR"] = str(private_tmp) # 泄漏断言隔离,见 conftest private_tmp + before = (set(glob.glob(f"{private_tmp}/.factory-stage.*")) + | set(glob.glob(f"{private_tmp}/.factory-dist.*"))) proc = self._run(dn, str(up), "--apply", "--anchor", "main", env=env) assert proc.returncode == 1, "追平后闸返回 1 → 拦截退出" assert "Sourcery 回归闸拦截" in proc.stderr - after = (set(glob.glob(f"{tmp_root}/.factory-stage.*")) - | set(glob.glob(f"{tmp_root}/.factory-dist.*"))) - assert after - before == set(), "中途退出不得泄漏 /tmp 暂存文件(评论 3)" + after = (set(glob.glob(f"{private_tmp}/.factory-stage.*")) + | set(glob.glob(f"{private_tmp}/.factory-dist.*"))) + assert after - before == set(), "中途退出不得泄漏暂存文件(评论 3)" class TestPR120ReviewRegressions: """PR #120 审查评论回归:mktemp 暂存泄漏——trap 晚于首个 mktemp。""" @@ -426,7 +425,7 @@ def _run(self, dn, *args, env=None): cwd=dn, env=env or _run_env(), capture_output=True, text=True, ) - def test_second_mktemp_failure_leaks_no_dist_file(self, repos, tmp_path): + def test_second_mktemp_failure_leaks_no_dist_file(self, repos, tmp_path, private_tmp): """review 1:第二个 mktemp(STAGE_FILE)失败时首个(DIST_FILE) 不得泄漏——trap 须紧随首个 mktemp 安装(修复前两个 mktemp 均完成 后才装 trap,第二个失败即泄漏)。计数 fake:首调放行次调失败。""" @@ -441,11 +440,11 @@ def test_second_mktemp_failure_leaks_no_dist_file(self, repos, tmp_path): (fake / "mktemp").chmod(0o755) env = _run_env() env["PATH"] = f"{fake}:{env['PATH']}" - tmp_root = os.environ.get("TMPDIR") or "/tmp" - before = set(glob.glob(f"{tmp_root}/.factory-dist.*")) + env["TMPDIR"] = str(private_tmp) # 泄漏断言隔离,见 conftest private_tmp + before = set(glob.glob(f"{private_tmp}/.factory-dist.*")) proc = self._run(dn, str(up), "--check", "--anchor", "main", env=env) assert proc.returncode != 0, "第二个 mktemp 失败须中断脚本" - after = set(glob.glob(f"{tmp_root}/.factory-dist.*")) + after = set(glob.glob(f"{private_tmp}/.factory-dist.*")) assert after - before == set(), "第二个 mktemp 失败不得泄漏首个暂存(review 1)" class TestSelfOverwriteSafety: diff --git a/.factory/tests/test_triage_holdout_nodefail.py b/.factory/tests/test_triage_holdout_nodefail.py new file mode 100644 index 0000000..f516c8f --- /dev/null +++ b/.factory/tests/test_triage_holdout_nodefail.py @@ -0,0 +1,164 @@ +"""triage/holdout/marker 失败路径 node-fail 证据回归(PR #172 Sourcery 复审)。 + +缺口:run_triage/run_holdout 的失败出口(进程失败/裁决解析失败)与 +run_node 起点标记创建失败原本只留通用 chain-abort——下轮 prime 无法 +区分"裁决器死"与"编排死"。本测试沿用 test_b1_fallback 的提取哲学: +按函数名切源码执行真实函数体,桩 omp_node 可控退码/日志, +factory_lib.parse 走真实现——parse 失败即真实解析器判定,非桩回声。 + +marker-create 用例:issue 目录 chmod 0555 后 touch 新文件被拒(目录 +不可写),但 append 到已存在的 chain-history 不受目录位影响(文件权限 +独立于目录权限)——恰好隔离出"标记创建失败而台账可写"的观测窗口。 + +运行:python3 -m pytest .factory/tests -q +""" + +import json +import shutil +import subprocess +from pathlib import Path + +FACTORY = Path(__file__).resolve().parents[1] + + +def _func_body(name: str) -> str: + lines = (FACTORY / "fix-issue.sh").read_text(encoding="utf-8").splitlines() + start = next((i for i, l in enumerate(lines) + if l.startswith(name + "() {")), None) + assert start is not None, f"fix-issue.sh 缺 {name} 定义——锚定失效即红" + end = next((i for i in range(start + 1, len(lines)) if lines[i] == "}"), None) + assert end is not None, f"{name} 函数体未按列 0 闭括号闭合——锚定失效即红" + return "\n".join(lines[start:end + 1]) + + +TRIAGE = _func_body("run_triage") +HOLDOUT = _func_body("run_holdout") +RUN_NODE = _func_body("run_node") + +_OMP_STUB = """#!/bin/sh +case "${STUB_MODE:?}" in + fail) exit 1 ;; + garbage) printf '思考中……未见 JSON\\n' >> "$2"; exit 0 ;; + *) printf '{"verdict":"%s"}\\n' "${STUB_MODE}" >> "$2"; exit 0 ;; +esac +""" + +_DRIVER = r"""#!/usr/bin/env bash +set -euo pipefail +REPO="$1"; DIR="$2"; WT="$1"; ISSUE="99"; DRY=0; ROUND=3 +export PATH="$3:$PATH" +export STUB_MODE="$4" +node_timeout() { echo 30m; } +_node_metric() { :; } +json_field() { python3 "${REPO}/.factory/factory_lib.py" jfield "$@"; } +__FUNC__ +__CALL__ +""" + + +def _sandbox(tmp_path: Path) -> str: + repo = tmp_path / "repo" + repo.mkdir() + shutil.copytree(FACTORY, repo / ".factory", + ignore=shutil.ignore_patterns( + "artifacts", "worktrees", "__pycache__", "locks")) + (repo / "MISSION.md").write_text("mission stub\n", encoding="utf-8") + (repo / ".factory" / "prompts" / "foo.md").write_text( + "# foo 节点\n\n输出计划正文即可。\n", encoding="utf-8") + bindir = tmp_path / "bin" + bindir.mkdir() + (bindir / "omp_node").write_text(_OMP_STUB, encoding="utf-8") + (bindir / "node_timeout").write_text("#!/bin/sh\necho 30m\n", encoding="utf-8") + for f in bindir.iterdir(): + f.chmod(0o755) + issue_dir = repo / "issue-99" + issue_dir.mkdir() + (issue_dir / "issue.json").write_text(json.dumps({ + "title": "标题", "body": "正文", + "comments": [{"author": "u", "body": "评论"}]}), encoding="utf-8") + (issue_dir / "tests-output.txt").write_text("全部通过\n", encoding="utf-8") + return str(issue_dir) + + +def _run(tmp_path: Path, func_src: str, call: str, mode: str, setup=None): + issue_dir = _sandbox(tmp_path) + if setup: + setup(Path(issue_dir)) + drv = tmp_path / "drv.sh" + drv.write_text(_DRIVER.replace("__FUNC__", func_src) + .replace("__CALL__", call), encoding="utf-8") + r = subprocess.run(["/bin/bash", str(drv), str(tmp_path / "repo"), + issue_dir, str(tmp_path / "bin"), mode], + capture_output=True, text=True, timeout=60) + return r, issue_dir + + +def _history(issue_dir: str) -> list: + p = Path(issue_dir) / "chain-history" + return p.read_text(encoding="utf-8").splitlines() if p.exists() else [] + + +def test_triage_omp_failure_leaves_node_fail(tmp_path): + """裁决器进程失败(omp 退码非零)→ node-fail omp-exit 行,非只有 chain-abort。""" + r, d = _run(tmp_path, TRIAGE, "run_triage", "fail") + assert r.returncode == 1, r.stderr + rows = _history(d) + assert rows and "node=triage" in rows[-1] and "reason=omp-exit" in rows[-1] \ + and "round=3" in rows[-1], rows + + +def test_triage_parse_failure_leaves_node_fail(tmp_path): + """裁决输出无法解析为 JSON(真实 factory_lib.parse 判定)→ node-fail parse 行。""" + r, d = _run(tmp_path, TRIAGE, "run_triage", "garbage") + assert r.returncode == 1, r.stderr + rows = _history(d) + assert rows and "node=triage" in rows[-1] and "reason=parse" in rows[-1], rows + + +def test_triage_success_writes_no_node_fail(tmp_path): + """裁决成功(verdict=accept 经真实 parse)→ 不落 node-fail,triage.json 落盘。""" + r, d = _run(tmp_path, TRIAGE, "run_triage", "accept") + assert r.returncode == 0, r.stderr + assert not _history(d) + assert (Path(d) / "triage.json").exists() + + +def test_holdout_omp_failure_leaves_node_fail(tmp_path): + """验证器进程失败 → node-fail omp-exit 行。""" + r, d = _run(tmp_path, HOLDOUT, "run_holdout", "fail") + assert r.returncode == 1, r.stderr + rows = _history(d) + assert rows and "node=holdout" in rows[-1] and "reason=omp-exit" in rows[-1], rows + + +def test_holdout_parse_failure_leaves_node_fail(tmp_path): + """验证输出无法解析 → node-fail parse 行。""" + r, d = _run(tmp_path, HOLDOUT, "run_holdout", "garbage") + assert r.returncode == 1, r.stderr + rows = _history(d) + assert rows and "node=holdout" in rows[-1] and "reason=parse" in rows[-1], rows + + +def test_holdout_success_writes_no_node_fail(tmp_path): + """验证成功(verdict=PASS)→ 不落 node-fail,holdout.json 落盘。""" + r, d = _run(tmp_path, HOLDOUT, "run_holdout", "PASS") + assert r.returncode == 0, r.stderr + assert not _history(d) + assert (Path(d) / "holdout.json").exists() + + +def test_marker_create_failure_leaves_node_fail(tmp_path): + """起点标记创建失败(目录不可写)→ node-fail marker-create 行再退出。""" + + def setup(d: Path): + (d / "chain-history").write_text("", encoding="utf-8") + d.chmod(0o555) + + try: + r, d = _run(tmp_path, RUN_NODE, "run_node foo", "fail", setup=setup) + assert r.returncode == 1, r.stderr + rows = _history(d) + assert rows and "node=foo" in rows[-1] \ + and "reason=marker-create" in rows[-1], rows + finally: + (tmp_path / "repo" / "issue-99").chmod(0o755) # 还原权限供 tmp 清理 diff --git a/.factory/tests/test_upstream_sync_check.py b/.factory/tests/test_upstream_sync_check.py index fbbf0ab..1f52b19 100644 --- a/.factory/tests/test_upstream_sync_check.py +++ b/.factory/tests/test_upstream_sync_check.py @@ -1,4 +1,4 @@ -"""upstream-sync-check.sh 退出码契约回归(wop-skills PR#14 Sourcery 评论 1)。 +"""upstream-sync-check.sh 退出码契约回归(xx-skills PR#14 Sourcery 评论 1)。 缺陷:sync --check 的致命 rc=2(用法/上游不可用/锚点不可解析)原被 `&& {}` 短路吞掉——输出不带 [local]/[full] 标记时落入收尾分支,以「仅 local 面漂移」 @@ -12,6 +12,7 @@ 凭据面用 fake hosting.py 顶替(auth ok 恒 0,其余调用计数 + 非 0)—— 与 gitenv PATH 白名单配合,测试链零出网。 """ +from __future__ import annotations import json import subprocess @@ -83,10 +84,14 @@ def synced(tmp_path: Path): class TestSyncRcContract: - def _run(self, dn: Path, *args: str) -> subprocess.CompletedProcess: + def _run(self, dn: Path, *args: str, extra_env: dict | None = None) -> subprocess.CompletedProcess: + # 夹具仓无 origin remote,基线拒猜 fail-closed(#133 收口)会提前 + # exit 2——显式注入 env 级基线,保持测试聚焦 rc 契约矩阵 + env = git_env({"FACTORY_BASE_BRANCH": "main"}) + env.update(extra_env or {}) return subprocess.run( ["bash", str(dn / ".factory/upstream-sync-check.sh"), *args], - cwd=dn, env=git_env(), capture_output=True, text=True, + cwd=dn, env=env, capture_output=True, text=True, ) def test_clean_reports_no_action(self, synced): @@ -115,6 +120,17 @@ def test_fatal_sync_rc2_propagates(self, synced): lines = calls.read_text().splitlines() assert all("auth" in line for line in lines), f"rc=2 不得触发 issue/PR 流: {lines}" + def test_base_branch_fail_closed_without_origin_head(self, synced): + """#133 收口:无 env 且夹具仓无 origin/HEAD → 拒猜基线 exit 2, + 先于任何远端副作用(fake hosting 仅见 auth 探测)。""" + _, dn, calls = synced + proc = self._run(dn, extra_env={"FACTORY_BASE_BRANCH": ""}) + assert proc.returncode == 2 + assert "issue #133" in proc.stderr + # 基线块先于凭据探测:零 hosting 调用(连 auth 探测都不触发) + assert not calls.exists(), \ + f"基线 fail-closed 须先于任何 hosting 调用(含凭据探测): {calls}" + def test_full_drift_dryrun_exits_1(self, synced): """full 漂移 rc=1 语义不回归:--dry-run 报告并 exit 1(人工介入信号)。""" up, dn, calls = synced @@ -125,3 +141,64 @@ def test_full_drift_dryrun_exits_1(self, synced): assert "[dry-run] full 漂移存在" in proc.stdout lines = calls.read_text().splitlines() assert all("auth" in line for line in lines), f"dry-run 不得触发远端命令: {lines}" + + +class TestDispatchRootRepoSkip: + """调用方 factory_lib._upstream_sync_check 根仓免跑契约(2026-09-05)。 + + 噪音 C 溯源:根仓锁文件缺 upstream 字段(本仓是源仓,无上游可同步), + 但 dispatch 每轮仍跑 upstream-sync-check.sh → 脚本 exit 2 刷日志 103 轮。 + 裁决:根仓免跑(锁缺 upstream = 无上游声明),fork 侧 sync --apply + 写锁带 upstream=根仓 URL → 走正常检查自动追平。""" + import factory_lib + + def _mk(self, tmp_path, lock: dict | None): + from types import SimpleNamespace + factory = tmp_path / ".factory" + factory.mkdir() + check = factory / "upstream-sync-check.sh" + check.write_text("#!/usr/bin/env bash\necho invoked > $(dirname $0)/invoked-marker\nexit 0\n", encoding="utf-8") + check.chmod(0o755) + if lock is not None: + (factory / "upstream-lock.json").write_text( + json.dumps(lock, ensure_ascii=False), encoding="utf-8") + return SimpleNamespace(factory=factory) + + def test_root_repo_skips_without_upstream_field(self, tmp_path, capsys): + """根仓:锁缺 upstream 字段 → 免跑(脚本不被调、无噪音、rc 0)。""" + cfg = self._mk(tmp_path, {"anchor": "20f6a63"}) + assert self.factory_lib._upstream_sync_check(cfg) == 0 + assert "根仓无上游声明,免上游同步" in capsys.readouterr().out + assert not (cfg.factory / "invoked-marker").exists(), "根仓不得调用 check 脚本" + + def test_fork_repo_runs_check_with_upstream_field(self, tmp_path, capsys): + """fork:锁带 upstream=根仓 URL → 正常跑脚本(自动追平不回归)。""" + cfg = self._mk(tmp_path, {"anchor": "x", "upstream": "https://github.com/im47cn/awesome-rules"}) + assert self.factory_lib._upstream_sync_check(cfg) == 0 + assert "上游同步已推进" in capsys.readouterr().out + assert (cfg.factory / "invoked-marker").exists(), "fork 侧必须跑 check 脚本" + + def test_corrupt_lock_skips_with_disclosure(self, tmp_path, capsys): + """坏 JSON 锁:不可读 → 免跑 + 披露需人工处置(不静默、不刷噪音)。""" + cfg = self._mk(tmp_path, None) + (cfg.factory / "upstream-lock.json").write_text("{broken", encoding="utf-8") + assert self.factory_lib._upstream_sync_check(cfg) == 0 + assert "upstream-lock.json 不可读" in capsys.readouterr().out + assert not (cfg.factory / "invoked-marker").exists() + + def test_valid_json_non_dict_lock_skips_with_disclosure(self, tmp_path, capsys): + """sourcery issue(bug_risk):合法 JSON 非对象([]/null/"bad")→ .get 崩—— + 须校验 dict 形态后披露免跑(不 AttributeError)。""" + cfg = self._mk(tmp_path, None) + (cfg.factory / "upstream-lock.json").write_text("[]", encoding="utf-8") + assert self.factory_lib._upstream_sync_check(cfg) == 0 + assert "不可读或非对象" in capsys.readouterr().out + assert not (cfg.factory / "invoked-marker").exists(), "非对象锁不得调用 check 脚本" + + def test_env_declared_upstream_still_runs_check(self, tmp_path, capsys, monkeypatch): + """sourcery issue(broader_impact):锁缺 upstream 但 FACTORY_UPSTREAM env 已设 + (check 脚本 L45 支持的合法上游源)→ 不得误免跑,须调脚本。""" + monkeypatch.setenv("FACTORY_UPSTREAM", "https://github.com/im47cn/awesome-rules") + cfg = self._mk(tmp_path, {"anchor": "x"}) + assert self.factory_lib._upstream_sync_check(cfg) == 0 + assert (cfg.factory / "invoked-marker").exists(), "env 声明上游的 fork 必须跑 check 脚本" diff --git a/.factory/upstream-lock.json b/.factory/upstream-lock.json index 73b01c9..091dded 100644 --- a/.factory/upstream-lock.json +++ b/.factory/upstream-lock.json @@ -1,5 +1,5 @@ { - "anchor": "d2db89b7995461d3167253a0711e444e9970c44f", - "upstream": "/Users/dreambt/sources/awesome-rules", - "synced_at": "2026-09-03T06:01:26+00:00" + "anchor": "181618c32492fb41f5a2a14f52515449fdc1c9eb", + "upstream": "/Users/dreambt/sources/awesome-factory", + "synced_at": "2026-11-01T03:45:40+00:00" } diff --git a/.factory/upstream-sync-check.sh b/.factory/upstream-sync-check.sh index f954192..0ae33b0 100755 --- a/.factory/upstream-sync-check.sh +++ b/.factory/upstream-sync-check.sh @@ -25,7 +25,7 @@ # # 退出码: 0 = 无漂移或 PR 已开(同步已推进,调用方当轮即止) # 1 = 漂移存在但推进失败(gauntlet 红/分支推失败)→ 人工介入 -# 2 = 用法/上游不可用/锁文件缺 upstream 字段 +# 2 = 用法/上游不可用/锁文件缺 upstream 字段/基线不可解析(#133) # 3 = 无凭据降级(本地已报告,无远端副作用) set -euo pipefail @@ -37,6 +37,16 @@ FACTORY="$REPO/.factory" HOST="python3 ${FACTORY}/hosting.py" LOCK="$FACTORY/upstream-lock.json" +# 基线分支两级解析(与 fix-issue.sh 同不变量,issue #133 收口):env 显式 → +# origin 实际默认分支;两者皆无 fail-closed 拒猜——固定回退 main 在 +# master 仓上落错基线。分步执行:set -e 下 $(...) 内嵌失败带 git rc 直接 +# 终止(fix-issue #133 实测 rc=128),|| 兜底接不住。 +BASE_BRANCH="${FACTORY_BASE_BRANCH:-}" +if [ -z "${BASE_BRANCH}" ]; then + BASE_BRANCH="$(git -C "${REPO}" symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed 's|^origin/||' || true)" +fi +[ -n "${BASE_BRANCH}" ] || { echo "[base] FACTORY_BASE_BRANCH 未配置且 origin/HEAD 默认分支不可读:拒猜基线,fail-closed 终止(issue #133)" >&2; exit 2; } + # 上游路径:锁文件 upstream 字段(M2 约定)> 环境变量 > 退出 2 UP="$(python3 -c ' import json, sys @@ -114,7 +124,9 @@ print(json.loads(open(sys.argv[1]).read())["anchor"][:9])' "$LOCK")" fi git commit -qm "chore(factory): 上游同步追平 ${ANCHOR}(M2 确定性 PR 流)" git push -q --no-verify origin "HEAD:refs/heads/${BR}" - PR_URL="$(${HOST} pr create --head "$BR" --title "factory: 上游同步追平(${ANCHOR})" \ + # base 显式(PR #116 拒猜 + #133 收口):两级解析值,不猜默认 + PR_URL="$(${HOST} pr create --head "$BR" --title "chore(factory): 上游同步追平(${ANCHOR})" \ + --base "${BASE_BRANCH}" \ --label factory:needs-review \ --body "M2 确定性 PR 流(设计 §11.2):full 面漂移自动追平,机器执行、人工合并。 diff --git a/.factory/validate-pr.sh b/.factory/validate-pr.sh index 8647dc5..510f6f8 100755 --- a/.factory/validate-pr.sh +++ b/.factory/validate-pr.sh @@ -69,12 +69,14 @@ if [ "${DRY}" = 0 ]; then fail docstring "docstring 门失败(详见 ${DIR}/docstring-output.txt)" 1 fi fi - for suite in $(python3 "${REPO}/.factory/factory_lib.py" suites "${CHANGED[@]}"); do + # NUL 分隔消费(PR #116):套件名可含空格(skills/foo bar/...),for 词拆分 + # 会拆碎名致证据段静默跳过;suites 产出 NUL → read -d '' 逐条保真 + while IFS= read -r -d '' suite; do [ -d "${REPO}/${suite}" ] || continue echo "" >> "${DIR}/tests-output.txt" echo "── 证据段(verbose): ${suite}" >> "${DIR}/tests-output.txt" (cd "${REPO}/${suite}" && python3 -m pytest -o addopts="" -v) >> "${DIR}/tests-output.txt" 2>&1 || true - done + done < <(python3 "${REPO}/.factory/factory_lib.py" suites "${CHANGED[@]}") else echo "[dry-run] 测试门(final_gate_cmd) → ${DIR}/tests-output.txt + 证据段" echo "[dry-run] docstring 门(docstring_gate_cmd,未配置则跳过) → ${DIR}/docstring-output.txt" diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 43733cb..01140b9 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -1,3 +1,5 @@ # factory: 追平提交忽略清单(git blame --ignore-revs 消噪) # factory: 上游同步追平(d2db89b79) 5bb3fc79ea0bad767b2a600f63f855e275d5a62d +# factory: 上游同步追平(181618c32) +f031676f2ef3311ba8d4d7d75481943d5a9d6075