GeezerVison is showing github.com — refactor: backlink / wikilink store の _scanId race-prevention pattern を createScanAction factory に共通化 (#228) by ymnao · Pull Request #234 · ymnao/scripta
Conversation
概要
backlink / wikilink store の _scanId race-condition prevention pattern を共通 factory createScanAction に抽出。両 store の scan 関数で重複していた「stale な非同期スキャン結果を破棄するロジック」を 1 箇所に集約し、後続 scanner 系 store 追加時の再利用を可能にする。
関連 Issue
Closes #228
移行 Stage
該当なし (内部 refactor、移行 Stage には依存しない)
変更内容
新規 src/stores/createScanAction.ts
createScanAction<TState, TArgs, TResult>(opts) factory。_scanId 単調増加で in-flight な scan の戻り値を弾く共通ロジックを所有
api は thunk (() => fn) で受ける。ES module の live binding 解決を実際の scan 呼び出し時まで遅延させる設計で、commands モジュールを partial mock している無関係なテスト (live-preview / AppLayout 等) で strict-mock エラーが出るのを構造的に防ぐ
beforeScan?: (state, args) => Omit<Partial<TState>, "_scanId" | "loading"> callback で backlink の「target 切替時の事前 clear」のような store 固有の前処理を表現
applyResult / beforeScan の戻り型を Omit<Partial<TState>, "_scanId" | "loading"> に絞り、factory 所有の不変条件 (scanId 単調増加 / loading flip) を compile error で守る。consumer の typo や copy-paste で _scanId: 0 を返しても TS が弾く
SetState / GetState は zustand の StoreApi['setState'] overload (replace=true で full state 要求) が generic 内で narrowing しきれないため、本 factory が必要な分だけ narrower な local 型に絞っている (理由を 3 行コメントで明示)
src/stores/backlink.ts / src/stores/wikilink.ts
- inline
scan: async (...) を createScanAction(...)(set, get) 呼び出しに置換
- backlink の
targetChanged 判定による backlinks 事前 clear は beforeScan callback で表現
- wikilink は差分の無い素直な置換
- 既存テスト (backlink.test.ts / wikilink.test.ts) は 無変更で全 pass = 観測可能な振る舞い完全保存の証拠
新規 src/stores/createScanAction.test.ts (8 cases)
- success: api 成功時の applyResult 適用 + loading clear
- error: api 失敗時の console.error + loading clear、既存 items は保持
- stale scan (success path): 古い in-flight 結果が新しい scan に上書きされない
- stale scan (error path): 古い in-flight error が新しい scan の loading を上書きしない
- beforeScan (異なる key): 事前 clear が走る
- beforeScan (同一 key): items 保持
_scanId 単調増加
- 外部 reset で
_scanId が bump された後の継続
出所
HANDOFF 36-37 § 1 v0.6.0 quick win backlog (#225-#228 のうち最後の 1 件)、セッション 30 の /simplify で抽出された未対応項目。
/simplify Phase 2 適用 4 件 + 4 件 reject
適用 (3 angle 以上収束):
api thunk 化 (Altitude × Simplification × Reuse) — 各 consumer の 3 行コメント (計 6 行) が factory JSDoc 1 箇所に集約、引数の再列挙不要
Promise<void> 戻り値注釈削除 — TS 推論で十分
[, targetFilePath] → [_workspacePath, targetFilePath] — 穴あき destructure を named skip に
- factory JSDoc に
api thunk の理由を集約 — consumer 側コメント削減
Reject: StoreApi<TState>['setState'] 採用 — zustand overload で narrowing 失敗、3 箇所の cast が必要なまま増えたため local narrower 型を維持。理由を 3 行コメントで明示
/code-review max 結果
5 correctness angles + 4 cleanup angles + 1 conventions angle で 10 agent 並列実行。
Correctness バグ無し:
- Angle B (removed-behavior) / Angle C (cross-file caller breakage) / Efficiency / Conventions は全
[]
適用 1 件 (4 angle 収束):
- F1:
applyResult / beforeScan の戻り型を Omit<Partial<TState>, "_scanId" | "loading"> で絞り、factory 所有の不変条件を型で守る
別 issue 候補 (本 PR で skip、将来検討)
- applyResult inside try{} (Angle E): 現状 consumer の applyResult が throw すると "Failed to scan ..." として log される。現 consumer は pure literal で実害なしだが、将来の consumer が validation を入れた場合に debugging が混乱する可能性。catch を 2 段にする deeper fix の余地
- opts.api() thunk-throw 経路 (Angle E): 同様に thunk 内 throw が API error 扱い。現 thunk は
() => fn で throw 不可能、低リスク
- reset の
_scanId+1 重複 (Reuse + Altitude): backlink/wikilink reset がそれぞれ _scanId: get()._scanId + 1 を hand-roll。factory に createScanInvalidator を追加する案あり、3 件目の scanner store 追加時に再検討
- stale-scan test の重複 (Reuse): factory test が race semantics を所有した今、backlink.test.ts:112 / wikilink.test.ts:101 の同等テストは defense in depth でしかない。残置でも害なし、剥がすかは別判断
vi.fn().mockResolvedValue の typing 不整合 (Angle D): success test 1 件のみ generic 未指定。他テストは vi.fn<Signature>() 形式。inconsistency 修正は別 PR
- deferred promise pattern の test scaffolding 重複 (Simplification): 5 箇所で
let resolveX; const p = new Promise(r => resolveX = r) パターン。createDeferred<T>() ヘルパ抽出は意図不鮮明化のため見送り
- beforeScan ordering test が ordering を assert していない (Angle A): test 名と assertion の不整合。最終状態で十分推論可能だが test 名を rename する案
動作確認
-
pnpm test (vitest unit) — 98 test files / 2226 tests pass (+8 新規 / 既存 backlink+wikilink test 無変更で全 pass)
-
pnpm typecheck (tsconfig.web.json / tsconfig.node.json) — clean
-
pnpm lint (biome check) — clean
- /simplify (Phase 1 = 4 angle parallel, Phase 2 = 4 件適用 + 1 件 reject)
- /code-review max (10 finder angle 並列) — 適用 1 件 + 別 issue 候補 7 件記録
- e2e (ローカル EPERM、CI で確認)
スクリーンショット
UI 変更なし (内部 refactor)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
… を createScanAction factory に共通化 (#228)
- `src/stores/createScanAction.ts` 新規: `_scanId` 単調増加で stale な非同期スキャン結果を破棄する factory。`api` は thunk (`() => fn`) で受けて ES module live binding 解決を scan 呼び出し時まで遅延 (vitest の partial mock テストへの影響回避)。`beforeScan` callback で backlink の target 切替時の pre-clear を表現
- `applyResult` / `beforeScan` の戻り型を `Omit<Partial<TState>, "_scanId" | "loading">` で絞り、factory 所有の不変条件を compile error で守る
- `src/stores/backlink.ts`: scan 22 行を factory 呼び出しに置換、currentTargetPath / targetChanged 分岐は beforeScan で表現
- `src/stores/wikilink.ts`: scan 13 行を factory 呼び出しに置換 (差分なし)
- `src/stores/createScanAction.test.ts` 新規: 8 ケース (success / error / stale 破棄 [success/error 双方] / beforeScan [異なる key / 同一 key] / _scanId 単調増加 / 外部 reset 後の継続)
- 既存 backlink.test.ts / wikilink.test.ts は無変更で全 pass、26 tests + 2226 unit tests 全 green
Closes #228
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Jun 26, 2026
ymnao
added a commit
that referenced
this pull request
Jun 26, 2026
- `CHANGELOG.md` に v0.6.0 セクションを追加
- Internal: #206 → #229 (AppLayout useShallow), #208 → #230 (settings
_schemaVersion + MIGRATIONS), #207 → #231 (2 モード e2e 振り分け +
ADR-0009 集約), #225 → #232 (onNavigate wikilink target),
#226 → #233 (useCollapseToggle hook), #228 → #234 (createScanAction
factory), #227 → #235 (producer-side lineContent trim)
- `package.json` の `version` を `0.5.0` → `0.6.0`
- HANDOFF の規約通り、本 commit では `pnpm <script>` を呼ばない
(lockfile / node_modules には触れない)
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.