dsh-project-flow 0.10.2 → 0.10.4

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/CHANGELOG.md CHANGED
@@ -14,6 +14,50 @@ dsh-project-flow 的版本变更。版本号遵循语义化版本。
14
14
 
15
15
  ---
16
16
 
17
+ ## [0.10.4] - 2026-12-05
18
+
19
+ ### 配了 flowRoot 反而一个会话都用不了
20
+
21
+ 用户配了绝对 `flowRoot`(工作流固定在某一个目录),结果工作模式一次都开不起来。
22
+
23
+ 原因在两道门都写着「配了绝对 flowRoot 就不许挪,直接拒绝」。设计时的想法是:配置写死了目录,说明用户明确表态只在那儿用,插件不该自作主张把他带过去。这条规则的假设是「用户会在那个目录里开会话」——但实际不是。
24
+
25
+ 真机数据:656 个会话的 cwd 分布是 434 个 C 盘默认工作区、140 个盘符根、83 个另一个目录,**没有一个落在工作流目录里**。于是 `/flow`、`project_run`、`project_resume` 三条路全被拒。用户看到的就是「插件坏了,只有某一个对话能用」——而那个对话只能用,是因为我手动 `cd` 进去过。
26
+
27
+ 现在配了绝对 `flowRoot` 时,直接把这次的工作目录指到那个目录,不拒绝。三处都改了:`runner/tools/run.mjs`、`runner/tools/resume.mjs`、`commands/slash.mjs`。
28
+
29
+ 配置写 `flowRoot` 的意思是「工作流固定在这儿」,不是「我会自觉把会话切过去」。
30
+
31
+ ### flowRootTargetOf 不再往下探一层
32
+
33
+ 以前不管配没配,都去试 `<root>/工作流/.project-flow`。于是用户把根配成外层目录,数据落进它下面的「工作流」子目录——配置写一层、项目在另一层,两边对不上,`D:\` 底下长出了六个 `.project-flow`。
34
+
35
+ 现在配了就是配了,`flowRootTargetOf` 原样返回配置的目录;只有没配时才继续按老规则往下探。
36
+
37
+ ### 测试
38
+
39
+ `tests/flow-gate.test.mjs` 从 24 条加到 27 条:任何目录(C 盘默认工作区、盘符根、随便一个目录、根本身)都算出同一个目标;配了绝对路径不再往下探;没配时那条往下探的规则没被顺手改坏。
40
+
41
+ ## [0.10.3] - 2026-12-05
42
+
43
+ ### 带 BOM 的 state.json 让项目从面板上消失
44
+
45
+ 真机上 9 个项目只显示 8 个。少的是 `opc-deck-v4`:它的 `state.json` 开头带 `EF BB BF`(UTF-8 BOM),`JSON.parse` 见到直接抛,`readState` 返回 `undefined`,面板那边 `if (state === undefined) continue` 就把它跳过了——一声不响。
46
+
47
+ Windows 上这玩意儿到处都是:记事本另存、某些 PowerShell 写法、从别的机器拷文件,都会带。以前只有 `splitFrontMatter`(Markdown 那条路)处理过 BOM,JSON 这条路没管。
48
+
49
+ 现在 `shared/util.mjs` 加了 `stripBom`,用在所有 JSON 读取点:`readState`、`writeState`、`readContract`、`readRememberedContext`、`readOverride`、`readLedger`、锁文件、`readEvidence`、以及从会话存储里取 cwd 那一处。
50
+
51
+ ### writeState 里藏着同一个坑
52
+
53
+ `writeState` 读旧 state 失败时 `previous = {}`,然后 `{ ...{}, ...patch }` 写回去——`stage`、`requirements`、`attempts`、`cost` 全没了,只剩这一次的 patch。带 BOM 的 state.json 正好会走到这条路上。
54
+
55
+ 这和 0.10.1 修的 `writeOverride` 是同一个病。那边丢的是配置,这边丢的是运行时状态,更难发现——文件还在、还能解析,只是内容变成了刚写的那几个键。
56
+
57
+ ### 测试
58
+
59
+ `tests/panel-report.test.mjs` 从 9 条加到 10 条:手工拼一个带 BOM 的 `state.json` 丢给 `panelSnapshot`,断言那个项目还在、阶段读得出来。
60
+
17
61
  ## [0.10.2] - 2026-12-05
18
62
 
19
63
  修「工作模式开起来了,状态栏却什么都没变」。三条独立的原因,叠在一起。
@@ -18,7 +18,7 @@ import { StopRequested } from '../runner/stage.mjs'
18
18
  import { ARTIFACT, COPYRIGHT, FLOW_GATE_MODES, INTENT_CONFIDENCE_FLOOR, LICENSE_NOTICE, STAGE_LABEL } from '../shared/constants.mjs'
19
19
  import { messageOf } from '../shared/util.mjs'
20
20
  import { listProjectLines, statusLine, writePanelFile } from '../workspace/panel.mjs'
21
- import { cwdOf, ensureFlowRoot, flowGateMessage, flowModeOf, flowRootConfigured, insideFlow, watchRootOf } from '../workspace/paths.mjs'
21
+ import { cwdOf, ensureFlowRoot, flowGateMessage, flowModeOf, flowRootConfigured, flowRootTargetOf, insideFlow, watchRootOf } from '../workspace/paths.mjs'
22
22
  import { buildResumePlan, pickResumable } from '../workspace/resume.mjs'
23
23
  import { pfState } from '../state/registry.mjs'
24
24
 
@@ -41,10 +41,15 @@ ctx.inject(['commands'], commandsCtx => {
41
41
  // 不在工作流目录里就先替他建出来,然后把这次的工作目录指过去。
42
42
  // 之后调 project_run 时会显式带上 cwd,所以这里改的值要一路传下去。
43
43
  //
44
- // 但配了绝对 flowRoot 就不挪:那代表用户明确说了「只在那儿用」。
45
- // 擅自换地方等于把他带到一个错的目录里开工,底下会另起一个
46
- // .project-flow,项目就散了。
44
+ // 配了绝对 flowRoot 时,直接干在配置的那个目录里,不拒绝。
45
+ // 以前这里是「配了绝对路径就不挪,让人自己切过去」——真机器上 656 个
46
+ // 会话没一个 cwd 落在工作流目录里,于是 /flow 一次也没开起来过。
47
+ // 配置写 flowRoot 的意思是「工作流固定在这儿」,不是「我会自觉切过去」。
47
48
  let created = false
49
+ if (flowRootConfigured(gateSettings) && !insideFlow(gateCwd, gateSettings)) {
50
+ gateCwd = flowRootTargetOf(gateCwd, gateSettings)
51
+ created = true
52
+ }
48
53
  if (!flowRootConfigured(gateSettings)
49
54
  && gateSettings.autoCreateFlowDir === true
50
55
  && flowModeOf(gateSettings) === 'strict'
@@ -5,7 +5,7 @@ import { join, dirname } from 'node:path'
5
5
  import { homedir } from 'node:os'
6
6
  import { readFileSync } from 'node:fs'
7
7
  import { mkdir, readFile } from 'node:fs/promises'
8
- import { writeFileAtomic } from '../shared/util.mjs'
8
+ import { stripBom, writeFileAtomic } from '../shared/util.mjs'
9
9
  import { DEFAULT_SNAPSHOT_EXCLUDES, FLOAT_SETTINGS } from '../shared/constants.mjs'
10
10
 
11
11
  const pick = (value, fallback) => (value === undefined || value === '' ? fallback : value)
@@ -104,7 +104,7 @@ const readOverride = () => {
104
104
  const merged = {}
105
105
  for (const file of overridePaths().slice().reverse()) {
106
106
  try {
107
- const parsed = JSON.parse(readFileSync(file, 'utf8'))
107
+ const parsed = JSON.parse(stripBom(readFileSync(file, 'utf8')))
108
108
  if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)) {
109
109
  Object.assign(merged, parsed)
110
110
  }
package/evidence.mjs CHANGED
@@ -17,6 +17,7 @@ import { execFile } from 'node:child_process'
17
17
  import { writeFile, readFile, readdir, stat, rename, rm } from 'node:fs/promises'
18
18
  import { homedir } from 'node:os'
19
19
  import { join } from 'node:path'
20
+ import { stripBom } from './shared/util.mjs'
20
21
 
21
22
  export const EVIDENCE_FILE = 'Completion_Evidence.json'
22
23
  export const EVIDENCE_SCHEMA_VERSION = 1
@@ -97,7 +98,7 @@ async function detectIn(cwd) {
97
98
  // "echo \"Error: no test specified\"" 不算测试。
98
99
  if (await has('package.json')) {
99
100
  try {
100
- const pkg = JSON.parse(await readFile(join(cwd, 'package.json'), 'utf8'))
101
+ const pkg = JSON.parse(stripBom(await readFile(join(cwd, 'package.json'), 'utf8')))
101
102
  const script = pkg?.scripts?.test
102
103
  if (typeof script === 'string' && script.trim() !== '' && !/no test specified/i.test(script)) {
103
104
  return { command: 'npm test', why: 'package.json 里有 scripts.test' }
@@ -499,7 +500,7 @@ export async function writeEvidence(artifacts, evidence) {
499
500
  /** 读回来。补丁里给 project_status 之类的只读工具用。 */
500
501
  export async function readEvidence(artifacts) {
501
502
  try {
502
- return JSON.parse(await readFile(join(artifacts, EVIDENCE_FILE), 'utf8'))
503
+ return JSON.parse(stripBom(await readFile(join(artifacts, EVIDENCE_FILE), 'utf8')))
503
504
  } catch {
504
505
  return undefined
505
506
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-project-flow",
3
- "version": "0.10.2",
3
+ "version": "0.10.4",
4
4
  "private": false,
5
5
  "description": "Four-stage project workflow: requirement, plan, build, QA, with an intent gate, a rework loop, self-healing recovery, a watchdog that stops runaway runs, snapshot rollback with a retention cap, a single status light, and a machine-readable Completion_Evidence.json.",
6
6
  "type": "module",
@@ -3,7 +3,7 @@
3
3
  */
4
4
  import { join } from 'node:path'
5
5
  import { readFile, mkdir, rename, writeFile, rm } from 'node:fs/promises'
6
- import { isAlive, nowIso, stateDir, writeFileAtomic } from '../shared/util.mjs'
6
+ import { isAlive, nowIso, stateDir, stripBom, writeFileAtomic } from '../shared/util.mjs'
7
7
 
8
8
  export function lockFile(cwd, project) {
9
9
  return join(stateDir(cwd, project), 'run.lock')
@@ -11,7 +11,7 @@ export function lockFile(cwd, project) {
11
11
 
12
12
  export async function readLock(cwd, project) {
13
13
  try {
14
- const parsed = JSON.parse(await readFile(lockFile(cwd, project), 'utf8'))
14
+ const parsed = JSON.parse(stripBom(await readFile(lockFile(cwd, project), 'utf8')))
15
15
  return parsed !== null && typeof parsed === 'object' ? parsed : undefined
16
16
  } catch {
17
17
  return undefined
@@ -7,7 +7,7 @@ import { createHash } from 'node:crypto'
7
7
  import { BUILTIN_FLOW, loadFlow } from '../flow.schema.mjs'
8
8
  import { parseRequirementChecks, requirementFingerprint } from '../checks/requirement.mjs'
9
9
  import { ARTIFACT, CONTEXT_FILE, CONTRACT_FILE, CONTRACT_MAX_CHARS } from '../shared/constants.mjs'
10
- import { filled, nowIso, stateDir, writeFileAtomic } from '../shared/util.mjs'
10
+ import { filled, nowIso, stateDir, stripBom, writeFileAtomic } from '../shared/util.mjs'
11
11
 
12
12
  export async function writeState(cwd, project, patch) {
13
13
  const dir = stateDir(cwd, project)
@@ -15,7 +15,11 @@ export async function writeState(cwd, project, patch) {
15
15
  const file = join(dir, 'state.json')
16
16
  let previous = {}
17
17
  try {
18
- previous = JSON.parse(await readFile(file, 'utf8'))
18
+ // 必须 stripBom:带 BOM 的 state.json 会让 JSON.parse 抛,然后 previous 被当成
19
+ // {} ——这一轮的 patch 覆盖上去,stage / requirements / attempts / cost 全没了。
20
+ // 这跟 writeOverride 里那个「读失败静默降级成空对象」是同一个坑,只是这边
21
+ // 丢的是运行时状态,比配置更难发现。
22
+ previous = JSON.parse(stripBom(await readFile(file, 'utf8')))
19
23
  } catch {
20
24
  previous = {}
21
25
  }
@@ -26,7 +30,7 @@ export async function writeState(cwd, project, patch) {
26
30
 
27
31
  export async function readState(cwd, project) {
28
32
  try {
29
- return JSON.parse(await readFile(join(stateDir(cwd, project), 'state.json'), 'utf8'))
33
+ return JSON.parse(stripBom(await readFile(join(stateDir(cwd, project), 'state.json'), 'utf8')))
30
34
  } catch {
31
35
  return undefined
32
36
  }
@@ -71,7 +75,7 @@ export function contractSha(contract) {
71
75
 
72
76
  export async function readContract(cwd, project) {
73
77
  try {
74
- const parsed = JSON.parse(await readFile(join(stateDir(cwd, project), CONTRACT_FILE), 'utf8'))
78
+ const parsed = JSON.parse(stripBom(await readFile(join(stateDir(cwd, project), CONTRACT_FILE), 'utf8')))
75
79
  if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return undefined
76
80
  return parsed
77
81
  } catch {
@@ -163,7 +167,7 @@ export function phaseReadKeys(phase) {
163
167
 
164
168
  export async function readRememberedContext(cwd) {
165
169
  try {
166
- const parsed = JSON.parse(await readFile(join(cwd, '.project-flow', CONTEXT_FILE), 'utf8'))
170
+ const parsed = JSON.parse(stripBom(await readFile(join(cwd, '.project-flow', CONTEXT_FILE), 'utf8')))
167
171
  if (parsed === null || typeof parsed !== 'object') return undefined
168
172
  return parsed
169
173
  } catch {
@@ -4,7 +4,7 @@
4
4
  import { join, isAbsolute, relative } from 'node:path'
5
5
  import { mkdirSync, appendFileSync, readFileSync } from 'node:fs'
6
6
  import { rm } from 'node:fs/promises'
7
- import { fileSha256, messageOf, nowIso, stateDir } from '../shared/util.mjs'
7
+ import { fileSha256, messageOf, nowIso, stateDir, stripBom } from '../shared/util.mjs'
8
8
  import { snapshotCommand } from '../snapshot/repo.mjs'
9
9
 
10
10
  export function ledgerDir(cwd, project) {
@@ -35,7 +35,7 @@ export function readLedger(cwd, project) {
35
35
  const path = ledgerPath(cwd, project)
36
36
  let text
37
37
  try {
38
- text = readFileSync(path, 'utf8')
38
+ text = stripBom(readFileSync(path, 'utf8'))
39
39
  } catch {
40
40
  return []
41
41
  }
@@ -4,7 +4,7 @@
4
4
  import { createExecuteRun } from './execute-run.mjs'
5
5
  import { ARTIFACT, FLOW_DIR_NAME } from '../../shared/constants.mjs'
6
6
  import { buildResumePlan } from '../../workspace/resume.mjs'
7
- import { cwdOf, ensureFlowRoot, flowGateMessage, flowModeOf, flowRootConfigured, insideFlow, rememberRoot } from '../../workspace/paths.mjs'
7
+ import { cwdOf, ensureFlowRoot, flowGateMessage, flowModeOf, flowRootConfigured, flowRootTargetOf, insideFlow, rememberRoot } from '../../workspace/paths.mjs'
8
8
  import { defineTool } from '../../shared/tool.mjs'
9
9
  import { isAbsolute } from 'node:path'
10
10
  import { pfState } from '../../state/registry.mjs'
@@ -60,7 +60,14 @@ const resumeTool = defineTool({
60
60
  const settings = settingsOf()
61
61
  let cwd = args.cwd !== undefined && isAbsolute(args.cwd) ? args.cwd : cwdOf(ctx, parent)
62
62
  const project = args.project
63
- // 续跑同一个道理:配了绝对 flowRoot 就不挪,让调用方按门控拒绝并说清该去哪。
63
+ // 配了绝对 flowRoot:这次就干在那儿,不拒绝。
64
+ //
65
+ // 以前这里是「配了就不挪,让门控拒绝并说清该去哪」。真机器上的结果是
66
+ // 656 个会话没一个 cwd 落在工作流目录里,续跑一次也起不来。用户配
67
+ // flowRoot 说的是「工作流固定在这儿」,不是「我只在这棵子树里开会话」。
68
+ if (flowRootConfigured(settings) && !insideFlow(cwd, settings)) {
69
+ cwd = flowRootTargetOf(cwd, settings)
70
+ }
64
71
  if (!flowRootConfigured(settings)
65
72
  && settings.autoCreateFlowDir === true
66
73
  && flowModeOf(settings) === 'strict'
@@ -3,7 +3,7 @@
3
3
  */
4
4
  import { createExecuteRun } from './execute-run.mjs'
5
5
  import { FLOW_DIR_NAME, INTENT_CONFIDENCE_FLOOR } from '../../shared/constants.mjs'
6
- import { assertIntentConverged, cwdOf, ensureFlowRoot, flowGateMessage, flowModeOf, flowRootConfigured, insideFlow, rememberReadiness, rememberRoot } from '../../workspace/paths.mjs'
6
+ import { assertIntentConverged, cwdOf, ensureFlowRoot, flowGateMessage, flowModeOf, flowRootConfigured, flowRootTargetOf, insideFlow, rememberReadiness, rememberRoot } from '../../workspace/paths.mjs'
7
7
  import { buildResumePlan } from '../../workspace/resume.mjs'
8
8
  import { defineTool } from '../../shared/tool.mjs'
9
9
  import { isAbsolute } from 'node:path'
@@ -176,13 +176,18 @@ const runTool = defineTool({
176
176
  throw new Error(`同时只能跑 ${settings.maxConcurrentProjects} 个项目,先等一个跑完`)
177
177
  }
178
178
  let cwd = args.cwd !== undefined && isAbsolute(args.cwd) ? args.cwd : cwdOf(ctx, parent)
179
+ // 配了绝对 flowRoot:这次就干在那儿,不拒绝。
180
+ //
181
+ // 以前这里是「配了就不挪,让门控拒绝并说清该去哪」。真机器上的结果是
182
+ // 656 个会话没一个 cwd 落在工作流目录里(434 个在默认工作区、140 个在
183
+ // 盘符根、83 个在别处),于是工作模式一次也开不起来——用户报的正是这个。
184
+ // 用户配 flowRoot 说的是「工作流固定在这儿」,不是「我只在这棵子树里开会话」。
185
+ if (flowRootConfigured(settings) && !insideFlow(cwd, settings)) {
186
+ cwd = flowRootTargetOf(cwd, settings)
187
+ }
179
188
  // 这道门是给人的,不是给模型的。但「建目录」这一步不该也压给人:
180
189
  // 不在工作流目录里就先替他建出来,然后把这次的工作目录指过去。
181
190
  // 边界没松——活还是在「工作流」里干——只是不用他自己去 mkdir 再切工作区。
182
- //
183
- // 但**配了绝对 flowRoot 就不许挪**:那代表用户明确表态「只在那儿用」。
184
- // 擅自把活挪过去,会在用户工程目录旁边另起一个 .project-flow,
185
- // 项目就散了——用户的原话是「默认工作区里不可以跑工作流」。
186
191
  let createError
187
192
  let createAttempt
188
193
  if (!flowRootConfigured(settings)
package/shared/util.mjs CHANGED
@@ -126,9 +126,25 @@ export function parseJsonBlock(text, marker) {
126
126
 
127
127
  export const filled = value => typeof value === 'string' && value.trim() !== ''
128
128
 
129
+ /**
130
+ * 去掉 UTF-8 BOM。
131
+ *
132
+ * Windows 上这东西到处都是:记事本另存、某些 PowerShell 写法、从别的机器拷过来
133
+ * 的文件,开头都可能带一个 0xFEFF。而 `JSON.parse` 见到它直接抛,报的是
134
+ * 「Unexpected token」,从错误信息里根本看不出是 BOM 惹的。
135
+ *
136
+ * `state.json` 尤其容易中招——它不是插件自己写的,用户编辑过、脚本生成过、
137
+ * 老版本留下过,都可能带。以前 `splitFrontMatter` 里单独处理过一次(Markdown
138
+ * 那条路),JSON 这条路没管:于是带 BOM 的 state.json 读出来是 undefined,
139
+ * 面板上那个项目直接消失,一声不响。
140
+ */
141
+ export function stripBom(text) {
142
+ return typeof text === 'string' && text.charCodeAt(0) === 0xFEFF ? text.slice(1) : text
143
+ }
144
+
129
145
  export function splitFrontMatter(text) {
130
146
  if (typeof text !== 'string') return { front: '', body: '' }
131
- const trimmed = text.replace(/^\uFEFF/u, '')
147
+ const trimmed = stripBom(text)
132
148
  if (!trimmed.startsWith('---')) return { front: '', body: trimmed }
133
149
  const firstBreak = trimmed.indexOf('\n')
134
150
  if (firstBreak === -1) return { front: '', body: trimmed }
@@ -18,6 +18,7 @@ import { FLOW_DIR_NAME } from '../shared/constants.mjs'
18
18
  import {
19
19
  insideFlow,
20
20
  flowRootOf,
21
+ flowRootTargetOf,
21
22
  flowGateMessage,
22
23
  flowModeOf,
23
24
  ensureFlowRoot,
@@ -181,6 +182,52 @@ check('配了 flowRoot 时提示语指向配置的位置', () => {
181
182
  return { ok: text.includes(ROOT), detail: text.slice(0, 160) }
182
183
  })
183
184
 
185
+ // ── 配了绝对 flowRoot:切过去,不是拒之门外 ──────────────────
186
+ //
187
+ // 这条是用户报的真故障。他的机器上 656 个会话,cwd 分布是:
188
+ // 434 个 C 盘 default-workspace、140 个盘符根、83 个 Aurabot —— 没有一个
189
+ // 落在工作流目录里。而当时的规则是「配了绝对 flowRoot 就不许挪,直接拒」,
190
+ // 于是工作模式一次也开不起来,看起来像插件坏了。
191
+ //
192
+ // 用户配 flowRoot 说的是「工作流固定在这儿」,不是「我会自觉把会话切过去」。
193
+
194
+ check('★ 配了绝对 flowRoot:任何目录都算出同一个目标', () => {
195
+ const settings = { flowRoot: ROOT, flowGate: 'strict' }
196
+ const cases = [
197
+ 'C:\\Users\\someone\\default-workspace',
198
+ 'D:\\',
199
+ 'D:\\随便一个目录',
200
+ ROOT,
201
+ ]
202
+ for (const dir of cases) {
203
+ const target = insideFlow(dir, settings) ? dir : flowRootTargetOf(dir, settings)
204
+ if (target !== ROOT) return { ok: false, detail: `${dir} → ${target},应为 ${ROOT}` }
205
+ }
206
+ return true
207
+ })
208
+
209
+ check('★ 配了绝对 flowRoot 就不再往下探「工作流」子目录', () => {
210
+ // 以前不管配没配都去试 `<root>/工作流/.project-flow`,于是用户把根配成外层,
211
+ // 数据落进子目录,配置和实际对不上。配了就是配了,别再替他改主意。
212
+ const settings = { flowRoot: ROOT, flowGate: 'strict' }
213
+ const target = flowRootTargetOf('C:\\anywhere', settings)
214
+ return {
215
+ ok: target === ROOT && !target.endsWith(`\\${FLOW_DIR_NAME}`),
216
+ detail: `得到 ${target}`,
217
+ }
218
+ })
219
+
220
+ check('没配 flowRoot 时仍然往下探那层「工作流」', () => {
221
+ // 这条别被上面那条顺手改坏了:没配置的时候,工作区根下面的「工作流」
222
+ // 子目录还是默认落点。
223
+ const settings = { flowRoot: '', flowGate: 'strict' }
224
+ const root = flowRootOf('D:\\somewhere', settings)
225
+ return {
226
+ ok: root.endsWith(FLOW_DIR_NAME),
227
+ detail: `flowRootOf 得到 ${root}`,
228
+ }
229
+ })
230
+
184
231
  await runChecks()
185
232
 
186
233
  let failed = 0
@@ -10,7 +10,8 @@
10
10
  import { test } from 'node:test'
11
11
  import assert from 'node:assert/strict'
12
12
  import { join } from 'node:path'
13
- import { readFile } from 'node:fs/promises'
13
+ import { readFile, mkdir, writeFile, rm, mkdtemp } from 'node:fs/promises'
14
+ import { tmpdir } from 'node:os'
14
15
  import { pathToFileURL } from 'node:url'
15
16
  import { LIVE_DIR, sourceText } from './paths.mjs'
16
17
 
@@ -126,4 +127,36 @@ test('挂掉的命令单独拎出来,带退出码', () => {
126
127
  const window = source.slice(at, at + 2200)
127
128
  assert.ok(window.includes("item.status === 'fail'"), 'fail 要进 failed')
128
129
  assert.ok(window.includes('exit: Number.isInteger(item.exit)'), '退出码要带上')
129
- })
130
+ })
131
+
132
+ test('★ 带 BOM 的 state.json 也读得出来,项目不会从面板上消失', async () => {
133
+ // 真机踩到过:opc-deck-v4 的 state.json 开头带 0xEF 0xBB 0xBF(记事本另存、
134
+ // PowerShell 写法、从别的机器拷,都会带)。JSON.parse 见到 BOM 直接抛,
135
+ // readState 返回 undefined,面板就把它跳过了——9 个项目只显示 8 个,
136
+ // 而且一声不响。
137
+ const home = await mkdtemp(join(tmpdir(), 'pf-bom-'))
138
+ const prevHome = process.env.DSH_HOME
139
+ process.env.DSH_HOME = home
140
+ try {
141
+ const root = join(home, '工作流')
142
+ const dir = join(root, '.project-flow', 'bomproj')
143
+ await mkdir(dir, { recursive: true })
144
+ const body = JSON.stringify({ stage: 4, status: 'passed', rework: 1 }, null, 2)
145
+ // 手工拼 BOM:0xEF 0xBB 0xBF
146
+ await writeFile(join(dir, 'state.json'), Buffer.concat([
147
+ Buffer.from([0xEF, 0xBB, 0xBF]),
148
+ Buffer.from(body + '\n', 'utf8'),
149
+ ]))
150
+ const settings = { flowGate: 'strict', flowRoot: '', maxRework: 2, maxConcurrentProjects: 2,
151
+ provider: 'spawn', intentModel: '', planModel: '', buildModel: '', checkModel: '',
152
+ planProvider: '', buildProvider: '', checkProvider: '', domain: 'auto', autoCreateFlowDir: true }
153
+ const snap = await mod.panelSnapshot(root, settings)
154
+ assert.equal(snap.projects.length, 1, '带 BOM 的项目必须还在')
155
+ assert.equal(snap.projects[0].project, 'bomproj')
156
+ assert.equal(snap.projects[0].stage, 4)
157
+ } finally {
158
+ if (prevHome === undefined) delete process.env.DSH_HOME
159
+ else process.env.DSH_HOME = prevHome
160
+ await rm(home, { recursive: true, force: true }).catch(() => {})
161
+ }
162
+ })
@@ -7,7 +7,7 @@ import { existsSync } from 'node:fs'
7
7
  import { mkdir, readFile } from 'node:fs/promises'
8
8
  import { tmpdir, homedir } from 'node:os'
9
9
  import { FLOW_DIR_NAME, FLOW_GATE_MODES, INTENT_CONFIDENCE_FLOOR, LAST_ROOT_FILE } from '../shared/constants.mjs'
10
- import { dirExists, messageOf, writeFileAtomic } from '../shared/util.mjs'
10
+ import { dirExists, messageOf, stripBom, writeFileAtomic } from '../shared/util.mjs'
11
11
  import { pfState } from '../state/registry.mjs'
12
12
 
13
13
  export function cwdOf(ctx, agent) {
@@ -241,6 +241,13 @@ export function flowRootConfigured(settings) {
241
241
 
242
242
  export function flowRootTargetOf(cwd, settings) {
243
243
  const root = flowRootOf(cwd, settings)
244
+ // 配了绝对 flowRoot 就原样用它,不再往下探一层。
245
+ //
246
+ // 以前不管配没配都去试 `<root>/工作流/.project-flow`:用户把根配成
247
+ // D:\新建文件夹 (4),数据其实在它下面的「工作流」子目录,于是配置写外层、
248
+ // 项目落内层,两边对不上。用户说的是「工作流就在 D:\工作流」这种直白意思,
249
+ // 配置写哪儿就该是哪儿——再探一层等于替他改主意。
250
+ if (flowRootConfigured(settings)) return root
244
251
  try {
245
252
  const nested = join(root, FLOW_DIR_NAME)
246
253
  if (existsSync(join(nested, '.project-flow'))) return nested
@@ -332,7 +339,7 @@ export async function cwdFromSessionStore(sid) {
332
339
  if (sid.includes('/') || sid.includes('\\') || sid.includes('..')) return undefined
333
340
  const file = join(stateHome(), 'storages', 'session_projcache', 'sessions', `${sid}.json`)
334
341
  try {
335
- const parsed = JSON.parse(await readFile(file, 'utf8'))
342
+ const parsed = JSON.parse(stripBom(await readFile(file, 'utf8')))
336
343
  const cwd = parsed?.record?.identity?.cwd
337
344
  return typeof cwd === 'string' && cwd !== '' && isAbsolute(cwd) ? cwd : undefined
338
345
  } catch {