dsh-flakefinder 0.4.23 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/USAGE.md CHANGED
@@ -2,31 +2,50 @@
2
2
 
3
3
  ## 完全权限下的确认
4
4
 
5
- 插件保留单独确认要求。会话审批策略为 `never` 时不能弹框,会明确说明操作未执行;请切到可询问模式,或由用户明确设置 `writeApproval: false`。其它宿主拒绝仍生效,不自动放宽权限。
5
+ 插件保留单独确认要求,并在审批弹窗中展示即将写入或移除的完整用例列表(以及隔离原因/关联 issue)。会话审批策略为 `never` 时不能弹框,会明确说明操作未执行;请切到可询问模式,或由用户明确设置 `writeApproval: false`。其它宿主拒绝仍生效,不自动放宽权限。
6
6
 
7
7
  [返回简介](../README.md) · [更新记录](../CHANGELOG.md) · [验证记录](VALIDATION.md)
8
8
 
9
- ## 本次改进
9
+ ## 核心能力
10
10
 
11
- 相对 quarantineFile 默认在当前工作区的 .flakefinder.json。取消会停止当前测试进程;同一数据目录的并发写入不会丢失历史或隔离条目。损坏文件会报错并保留,先备份后检查。
11
+ - **工作区感知与隔离**:历史记录按工作区分别存储于 `dataDir/workspaces/<hash>.json`(兼容旧版 `history.json`);相对 `quarantineFile` 默认落在当前工作区 `.flakefinder.json`,路径自动规范化为 POSIX 相对路径并拒绝越界绝对路径。
12
+ - **高效精准复跑**:支持按用例名过滤(`testName`)、早停模式(`until-flaky` / `verify-fix`)、受控并发(`concurrency` 1–4 路)、默认跳过覆盖率(`noCoverage`)以及随机顺序打乱与种子复现(`shuffle` / `seed`)。
13
+ - **根因诊断与置信度**:自动聚合去重错误聚类(归一化时间戳/UUID/端口等噪声并脱敏密钥堆栈),输出通过/失败时间线(`✓`/`✗`/`⏱`/`S`/`-`)、通过与失败耗时对比、6 类根因模式提示(`cold-start` / `state-leak` / `timeout-race` / `order-dependent` / `teardown-crash` / `intermittent`),以及全绿时的 95% 置信度残余偶发率上界。
14
+ - **隔离闭环与报告**:`flaky_detect` 自动对照 `.flakefinder.json` 标注 `[新增未隔离]`、`[已隔离]` 与可解除隔离候选;`flaky_report` 汇总历史高频不稳定 Top 5 榜单与超期未复查提醒。
15
+ - **安全与容错**:取消信号立即中止测试进程且不写脏历史;并发写入受文件锁保护;损坏的 JSON 文件报错并保留原文件;超 60 个用例时自动精简 `stable-pass` 条目保护上下文窗口。
12
16
 
13
17
  ## 工具
14
18
 
15
19
  | 工具 | 作用 | 写操作 |
16
20
  | :-- | :-- | :-- |
17
- | `flaky_detect` | 重复运行测试 N 次,判定 stable-pass / stable-fail / flaky | 否(写历史) |
18
- | `flaky_history` | 查询检测历史,按目标过滤 | 否 |
19
- | `flaky_report` | 汇总隔离清单 + 历史,输出稳定性报告 | 否 |
20
- | `flaky_quarantine` | 把 flaky 用例写入 `.flakefinder.json` 隔离清单 | 是(审批门) |
21
- | `flaky_clear` | 从隔离清单移除已恢复的用例 | 是(审批门) |
22
-
23
- 支持框架:
24
-
25
- - vitest:读取 `node_modules/vitest/vitest.mjs`,使用 JSON reporter
26
- - jest:读取 `node_modules/jest/bin/jest.js`,使用 `--json --outputFile`
27
- - pytest:`python -m pytest --junitxml`,解析 JUnit XML(支持 setup.cfg / pyproject / pytest.ini / tox.ini 探测与 XML 实体解码)
28
- - node:test:`node --test --test-reporter=tap`,解析 TAP(含 SKIP 指令归类)
29
- - `framework: auto` 按 vitest → jest → pytest → node:test 自动探测
21
+ | `flaky_detect` | 多轮复跑测试并输出判定、时间线、错误聚类、根因模式与隔离对照 | 否(写历史) |
22
+ | `flaky_history` | 查询检测历史,支持按 `target` 过滤与 `scope`(`workspace` / `all`)切换 | 否 |
23
+ | `flaky_report` | 汇总隔离清单、高频不稳定 Top 5、超期隔离提醒与历史趋势 | 否 |
24
+ | `flaky_quarantine` | 把 flaky 用例写入 `.flakefinder.json`(支持 `issue`、`lastFailureRate` 与增量更新) | 是(审批门) |
25
+ | `flaky_clear` | 从隔离清单移除已恢复稳定的用例 | 是(审批门) |
26
+ | `flaky_health` | 工作区感知自检:Node 版本、自动探测框架、Python/pytest、清单与历史可读性、默认配置 | 否 |
27
+
28
+ ### `flaky_detect` 参数
29
+
30
+ | 参数 | 类型 | 默认 | 说明 |
31
+ | :-- | :-- | :-- | :-- |
32
+ | `target` | `string`(必填) | — | 测试文件、目录或 glob(相对工作区,不能以 `-` 开头) |
33
+ | `framework` | `enum` | `auto` | `auto` / `vitest` / `jest` / `pytest` / `node`(也接受别名 `node:test`、`py`) |
34
+ | `runs` | `integer` | `defaultRuns` | 重复次数(`3`–`maxRuns`) |
35
+ | `testName` | `string` | — | 按用例名过滤(Vitest/Jest `-t`、pytest `-k`、`node:test` `--test-name-pattern`) |
36
+ | `mode` | `enum` | `full` | `full`(跑满次数)/ `until-flaky`(确认不稳定即早停)/ `verify-fix`(首次失败即早停) |
37
+ | `concurrency` | `integer` | `defaultConcurrency` | 并发 worker 数(`1`–`4`) |
38
+ | `shuffle` | `boolean` | `false` | 是否打乱执行顺序(Vitest/Jest 生效;未指定 `seed` 时每轮生成不同随机种子) |
39
+ | `seed` | `integer` | — | 随机种子(`0`–`2147483647`),配合 `shuffle` 复现特定顺序失败 |
40
+ | `noCoverage` | `boolean` | `noCoverage` 配置 | 是否关闭覆盖率采集加速复跑(Vitest/Jest 生效,默认 `true`) |
41
+
42
+ ### 支持框架
43
+
44
+ - **vitest**:从工作区向上查找 `node_modules/vitest/vitest.mjs`(适配 monorepo hoisted 依赖),使用 JSON reporter;支持 `-t`、`--coverage.enabled=false`、`--sequence.shuffle --sequence.seed=...`。
45
+ - **jest**:从工作区向上查找 `node_modules/jest/bin/jest.js`,使用 `--json --outputFile`;单并发非打乱默认带 `--runInBand`;支持 `-t`、`--coverage=false`、`--randomize --seed=...`。
46
+ - **pytest**:按 `显式 pythonPath > DSH_FLAKEFINDER_PYTHON > VIRTUAL_ENV > 工作区 .venv/venv > 默认 python/python3` 解析解释器,运行 `python -m pytest -q --junitxml=...`(支持 `pytest.ini` / `tox.ini` / `pyproject.toml` / `setup.cfg` 探测、`-k` 过滤、CDATA 与 XML 实体解码及 `<system-err>` 回退)。
47
+ - **node:test**:运行 `node --test --test-reporter=tap`,支持 `--test-name-pattern=...`、层级 TAP 13(含 YAML 诊断块与堆栈提取)、`SKIP`/`TODO` 指令与 `Bail out!` 识别。
48
+ - `framework: auto` 按 `vitest → jest → pytest → node:test` 优先级自动探测。
30
49
 
31
50
  ## 安装
32
51
 
@@ -41,6 +60,9 @@ dsh plugin --profile web add dsh-flakefinder
41
60
  name: 'dsh-flakefinder'
42
61
  config:
43
62
  defaultRuns: 5
63
+ defaultConcurrency: 1
64
+ noCoverage: true
65
+ staleQuarantineDays: 14
44
66
  writeApproval: true
45
67
  ```
46
68
 
@@ -55,25 +77,34 @@ dsh plugin --profile web remove dsh-flakefinder
55
77
  ## 使用示例
56
78
 
57
79
  ```text
58
- 用户:src/checkout.test.ts 最近老失败,帮我判断一下
80
+ 用户:src/checkout.test.ts 里的定时器用例最近老飘红,帮我查一下
81
+ Agent:
82
+ flaky_detect(target="src/checkout.test.ts", testName="定时器恢复", runs=10, concurrency=2, mode="until-flaky")
83
+ → 判定:不稳定(flaky);实际运行 4/10 次;时间线 ✓✓✗✓;模式:超时/竞态;错误聚类 [1次]:TimeoutError
84
+ → flaky_quarantine(tests=["src/checkout.test.ts > 定时器恢复"], reason="定时器竞态", issue="#12", lastFailureRate=25)
85
+
86
+ 用户:修完定时器和清理钩子了,帮我复测验证
59
87
  Agent:
60
- flaky_detect(target="src/checkout.test.ts", runs=5)
61
- → 判定:flaky;3/5 通过,失败集中在 useFakeTimers 用例
62
- → flaky_quarantine(tests=["src/checkout.test.ts > 定时器恢复"], reason="定时器竞态")
88
+ flaky_detect(target="src/checkout.test.ts", testName="定时器恢复", runs=15, mode="verify-fix")
89
+ → 判定:稳定通过;95% 置信下残余偶发率 ≤ 18.1%;命中可解除隔离候选
90
+ → flaky_clear(tests=["src/checkout.test.ts > 定时器恢复"])
63
91
  ```
64
92
 
65
93
  ## 配置
66
94
 
67
95
  | 字段 | 默认 | 说明 |
68
96
  | :-- | :-- | :-- |
69
- | `defaultRuns` | `5` | `flaky_detect` 默认重复次数(3-20) |
70
- | `maxRuns` | `20` | 单次允许的最大重复次数(3-50) |
71
- | `timeoutMs` | `120000` | 单轮测试运行超时 |
72
- | `graceMs` | `10000` | 超时后宽限 |
97
+ | `defaultRuns` | `5` | `flaky_detect` 默认重复次数(`3`–`20`) |
98
+ | `maxRuns` | `20` | 单次允许的最大重复次数(`3`–`50`) |
99
+ | `defaultConcurrency` | `1` | `flaky_detect` 默认并发 worker 数(`1`–`4`) |
100
+ | `noCoverage` | `true` | 默认关闭覆盖率采集以加速复跑(Vitest/Jest 生效) |
101
+ | `staleQuarantineDays` | `14` | `flaky_report` 超期未复查隔离项提醒阈值(`1`–`365` 天) |
102
+ | `timeoutMs` | `120000` | 单轮测试运行超时(毫秒,`10000`–`600000`) |
103
+ | `graceMs` | `10000` | 超时后宽限(毫秒,`1000`–`120000`) |
73
104
  | `writeApproval` | `true` | 隔离清单写操作是否走审批门 |
74
- | `dataDir` | `DSH_HOME/.dsh-flakefinder` | 历史存储目录 |
75
- | `quarantineFile` | `<cwd>/.flakefinder.json` | 隔离清单路径 |
76
- | `pythonPath` | `DSH_FLAKEFINDER_PYTHON` 或 `python`/`python3` | pytest 使用的 Python 解释器 |
105
+ | `dataDir` | `DSH_HOME/.dsh-flakefinder` | 历史存储根目录(按工作区分文件) |
106
+ | `quarantineFile` | `<cwd>/.flakefinder.json` | 隔离清单路径(相对路径按当前会话工作区解析) |
107
+ | `pythonPath` | 自动探测 `.venv` 或 `python`/`python3` | pytest 使用的 Python 解释器(显式配置或 `DSH_FLAKEFINDER_PYTHON` 优先) |
77
108
 
78
109
  ## 隔离清单格式
79
110
 
@@ -85,42 +116,45 @@ Agent:
85
116
  "file": "src/checkout.test.ts",
86
117
  "name": "使用假定时器后恢复真实定时器",
87
118
  "reason": "定时器竞态,见 issue #12",
88
- "since": "2026-08-16T00:00:00.000Z"
119
+ "since": "2026-08-16T00:00:00.000Z",
120
+ "updatedAt": "2026-08-20T00:00:00.000Z",
121
+ "lastFailureRate": 25,
122
+ "issue": "#12"
89
123
  }
90
124
  ]
91
125
  }
92
126
  ```
93
127
 
94
- 隔离清单只记录,不修改测试源码;agent 跑测试前应先查阅 `flaky_report`。
128
+ 隔离清单只记录,不修改测试源码;agent 跑测试前应先查阅 `flaky_report`。重复对同一条目执行 `flaky_quarantine` 会保留原始 `since` 并更新 `reason`、`updatedAt` 及可选元数据。
95
129
 
96
130
  ## 工程
97
131
 
98
- - Node >= 22.13,TypeScript,仅依赖文件锁库
132
+ - Node `^22.19.0 || >=24.0.0`(即 `Node >= 22.19`),TypeScript,运行时依赖 `proper-lockfile` 与 `@stardustlc/dsh-updates`
99
133
  - 测试进程走 DSH 官方 subprocess 服务,argv 数组、无 shell
100
- - 全量单测 40+:解析、判定、存储、审批门、pytest 计划、subprocess 超时、注册与 manifest
101
- - `pnpm test`:构建 + `node --test`
134
+ - 完整自动化测试覆盖:适配器与配置、JSON/JUnit/TAP 13 诊断解析、错误聚类与根因模式、工作区存储隔离、批次并发与早停、工具集成与真实 `node --test` 端到端测试、审批门与 manifest
135
+ - `pnpm test`:构建(`tsc` + `build-updates.mjs`)+ `node --test "test/*.test.mjs"`
102
136
 
103
137
  ## 发布门禁
104
138
 
105
139
  1. `pnpm test` 全绿
106
- 2. `pnpm typecheck`
140
+ 2. `pnpm typecheck` 零报错
107
141
  3. 危险模式扫描(eval / child_process / 密钥零容忍)
108
142
  4. 真实 profile 冒烟:全新临时 profile 安装插件 → 真 boot → stderr 零报错
109
143
  5. manifest 自检(bundle patch / exports / files 白名单)
110
144
 
111
145
  ## 故障排查
112
146
 
113
- 先运行 `flaky_health`:它检查 Python 解释器与 pytest 是否可用,并汇总默认配置。
147
+ 先运行 `flaky_health`:它按当前工作区检查 Node 版本、自动探测到的框架路径、Python/pytest(纯 JS/TS 工作区中 pytest 缺失仅标记为可选提示,不会误报自检失败;存在 pytest 配置或显式配置 `pythonPath` 时缺失才会报 ❌)、`.flakefinder.json` 与历史文件可读性,并汇总默认配置。
114
148
 
115
149
  | 现象 | 常见原因 | 处理 |
116
150
  | :-- | :-- | :-- |
117
- | 判定「没有解析到用例」 | target 没匹配到文件,或目标文件里只有被跳过的用例 | 确认 target 指向真实测试文件/目录(支持 glob),必要时显式指定 `framework` |
118
- | 提示「有 N 次运行未产出报告」 | 框架 CLI 缺失、reporter 参数不被接受、pytest 缺少 junitxml 约束 | 先手动跑一次对应命令看报错;确认临时目录可写 |
119
- | `framework=vitest` 报找不到 `node_modules/vitest` | 没安装,或不在项目根目录调用 | 在装了框架的项目里调用,或显式指定 `framework` |
120
- | pytest 相关失败 | 解释器或 pytest 缺失 | `pip install pytest`,或用配置里的 `pythonPath` 指向正确解释器 |
121
- | 隔离/清除返回 0 条 | 引用格式或路径与清单不一致 | 用 `"文件路径 > 用例名"` 格式;先 `flaky_report` 看清单现有条目 |
151
+ | 判定「没有解析到用例」 | `target` 或 `testName` 没匹配到用例,或文件里只有被跳过的用例 | 确认 `target` 指向真实测试文件/目录,检查 `testName` 模式,必要时显式指定 `framework` |
152
+ | 提示「运行异常汇总」或超时 | 框架 CLI 缺失、单轮超时、TAP 输出超过 4MB 被截断、或断言后清理阶段崩溃 | 查看摘要里的错误聚类与 stderr 尾部;按需调大 `timeoutMs` 或用 `testName` 缩小范围 |
153
+ | `framework=vitest` 报找不到 `node_modules/vitest` | 未安装依赖,或工作区路径不在项目/monorepo 内 | 在安装了框架的仓库内调用,或显式指定 `framework` |
154
+ | Python 工作区 `flaky_health` 报 pytest 失败 | 虚拟环境未创建或未安装 pytest | 运行 `pip install pytest`,或在配置中设置 `pythonPath` 指向对应解释器 |
155
+ | 隔离/清除返回 0 条 | 引用格式或路径与清单不一致 | 用 `"相对路径 > 用例名"` 格式;先 `flaky_report` 看清单现有条目 |
122
156
 
123
- `flaky_detect` 的摘要会给出下一步:flaky 先隔离,稳定失败先单跑定位,稳定通过可以清除隔离。
157
+ `flaky_detect` 的摘要会给出下一步:flaky 先隔离并结合模式/错误聚类定位,稳定失败先单跑定位,稳定通过且命中已隔离项时可直接 `flaky_clear`。
124
158
 
125
159
  ## License
126
160
 
@@ -0,0 +1,123 @@
1
+ /**
2
+ * 测试框架适配器与注册表:覆盖 vitest / jest / pytest / node:test,
3
+ * 支持 monorepo 向上查找 node_modules、workspace .venv 自动探测、
4
+ * 精准过滤、关闭覆盖率与顺序打乱(shuffle + seed)。
5
+ * 全程 argv 数组、无 shell,避免 npx 下载与命令注入。
6
+ *
7
+ * @module dsh-flakefinder/adapters
8
+ */
9
+ import { type ResolvedFlakeConfig } from './config.js';
10
+ export type ConcreteFramework = 'vitest' | 'jest' | 'pytest' | 'node';
11
+ export type Framework = 'auto' | ConcreteFramework;
12
+ export type ReportKind = 'json' | 'tap' | 'junit';
13
+ export interface PlanBuildOptions {
14
+ /** 按用例名/模式过滤单个或多个用例。 */
15
+ testName?: string;
16
+ /** 是否关闭覆盖率采集以加速复跑(默认 true,对 vitest / jest 生效)。 */
17
+ noCoverage?: boolean;
18
+ /** 是否打乱测试执行顺序(对 vitest / jest 生效)。 */
19
+ shuffle?: boolean;
20
+ /** 随机种子(0-2147483647);与 shuffle 配合或单独传入用于复现。 */
21
+ seed?: number;
22
+ /** 外部批次并发数;Jest 在 concurrency<=1 且未开启 shuffle 时默认带 --runInBand。 */
23
+ concurrency?: number;
24
+ /** pythonPath 是否由用户显式配置(为 true 时跳过 .venv 自动探测)。 */
25
+ pythonPathExplicit?: boolean;
26
+ }
27
+ export interface TestPlan {
28
+ framework: ConcreteFramework;
29
+ reportKind: ReportKind;
30
+ reportPath: string;
31
+ argv: string[];
32
+ label: string;
33
+ seed?: number;
34
+ target?: string;
35
+ }
36
+ export interface AdapterContext {
37
+ cwd: string;
38
+ target: string;
39
+ runIndex: number;
40
+ token: string;
41
+ pythonPath?: string;
42
+ options?: PlanBuildOptions;
43
+ }
44
+ /** 面向未来框架扩展(如 Playwright / Go)的标准适配器契约。 */
45
+ export interface FrameworkAdapter {
46
+ readonly id: ConcreteFramework;
47
+ readonly aliases?: readonly string[];
48
+ readonly reportKind: ReportKind;
49
+ /** auto 探测优先级,数字越小越优先。 */
50
+ readonly priority: number;
51
+ /** 显式指定该框架时,是否要求本地入口必须存在。 */
52
+ readonly requireLocalOnExplicit?: boolean;
53
+ /** 探测当前工作区是否具备该框架(返回入口或配置文件路径,未找到返回 null)。 */
54
+ detect(cwd: string): string | null;
55
+ /** 显式指定但本地缺失时的中文指引。 */
56
+ missingMessage?(cwd: string): string;
57
+ /** 构建单轮执行的完整计划。 */
58
+ buildPlan(ctx: AdapterContext): TestPlan;
59
+ }
60
+ export type PythonPathSource = 'explicit' | 'virtual-env' | 'workspace-venv' | 'default';
61
+ export interface PythonResolution {
62
+ pythonPath: string;
63
+ source: PythonPathSource;
64
+ }
65
+ export interface EnvironmentDetection {
66
+ cwd: string;
67
+ defaultFramework: ConcreteFramework;
68
+ nodeVersion: string;
69
+ nodePath: string;
70
+ vitest: {
71
+ available: boolean;
72
+ entryPath: string | null;
73
+ };
74
+ jest: {
75
+ available: boolean;
76
+ entryPath: string | null;
77
+ };
78
+ pytest: {
79
+ configFound: boolean;
80
+ configPath: string | null;
81
+ pythonPath: string;
82
+ pythonSource: PythonPathSource;
83
+ };
84
+ frameworks: {
85
+ vitest: string | null;
86
+ jest: string | null;
87
+ pytestConfig: string | null;
88
+ pythonPath: string;
89
+ node: string;
90
+ };
91
+ }
92
+ /** 从 cwd 向上逐级查找本地 node_modules 里的 CLI 入口(适配 monorepo hoisted 依赖)。 */
93
+ export declare function findLocalModuleEntry(cwd: string, ...parts: string[]): string | null;
94
+ /** 在 cwd 中查找 pytest 配置文件路径。 */
95
+ export declare function findPytestConfig(cwd: string): string | null;
96
+ /**
97
+ * 解析 pytest 使用的 Python 解释器路径及来源。
98
+ * 当用户未显式配置 pythonPath / DSH_FLAKEFINDER_PYTHON 时,按顺序自动探测:
99
+ * VIRTUAL_ENV -> 向上查找 .venv / venv -> 平台默认 python / python3。
100
+ */
101
+ export declare function resolvePythonPathInfo(cwd: string, cfg?: Pick<ResolvedFlakeConfig, 'pythonPath' | 'pythonPathExplicit'>, runtime?: {
102
+ env?: NodeJS.ProcessEnv;
103
+ platform?: NodeJS.Platform;
104
+ }): PythonResolution;
105
+ /** 解析 pytest 使用的 Python 解释器路径。 */
106
+ export declare function resolvePythonPath(cwd: string, cfg?: Pick<ResolvedFlakeConfig, 'pythonPath' | 'pythonPathExplicit'>, runtime?: {
107
+ env?: NodeJS.ProcessEnv;
108
+ platform?: NodeJS.Platform;
109
+ }): string;
110
+ /** 注册一个测试框架适配器(覆盖同 id 适配器并更新别名映射)。 */
111
+ export declare function registerAdapter(adapter: FrameworkAdapter): void;
112
+ /** 按框架 id 获取适配器。 */
113
+ export declare function getAdapter(id: ConcreteFramework): FrameworkAdapter;
114
+ /** 返回按 auto 优先级排序的所有已注册适配器。 */
115
+ export declare function listAdapters(): FrameworkAdapter[];
116
+ /** 解析 framework 参数(含别名 node:test -> node、py -> pytest);非法值抛中文错误。 */
117
+ export declare function readFramework(raw: unknown): Framework;
118
+ /** 在 cwd 里探测本地框架 CLI 入口;显式指定时缺失会给出中文指引。 */
119
+ export declare function detectFramework(cwd: string, requested: Framework): ConcreteFramework;
120
+ /** 构建一次测试运行的执行计划。runIndex 仅用于给临时报告文件命名。 */
121
+ export declare function buildPlan(cwd: string, target: string, requested: Framework, runIndex: number, pythonPath?: string, options?: PlanBuildOptions): TestPlan;
122
+ /** 汇总当前工作区可用的测试框架与解释器环境,供 flaky_health 自检使用。 */
123
+ export declare function detectEnvironment(cwd: string, cfg?: Pick<ResolvedFlakeConfig, 'pythonPath' | 'pythonPathExplicit'>): EnvironmentDetection;