実装計画
元のファイルをダウンロード ↓CRM-142連絡先インポートの実装計画
エージェント作業者向け:この実行では計画のみです。後の実行もネイティブかつ単一エージェントで行い、追加のスキル、コミット、作業ツリー、委任は行いません。利用者とプロジェクトの指示は、提供元の実行サブスキルのひな形に優先します。
目的: 連絡先を失ったり、プレビュー中に保存したりせず、確認したCSV連絡先を既存のローカル一覧へインポートする。
アーキテクチャ: 新しいインポートモジュールで純粋な解析・分類・マージを行い、既存アプリにインラインパネルと直列化した書き込みを追加します。既存の連絡先・ストレージアダプターは変更しません。
技術構成: プレーンHTML/CSSとブラウザESモジュール、localStorage。Node 24以上の組み込みテストランナーを使い、依存パッケージは追加しません。
仕様: docs/brainstorming.md, docs/screen-design.md, docs/architecture.md。固定要件は次を参照: tickets/CRM-142.md と docs/import-contract.md.
全体の制約
アプリケーションの変更対象のみ: index.html, style.css, src/app.js、新規の src/import.js。テスト、サンプル、チケット、サーバー、パッケージ、AGENTS.md、既存の両ヘルパーモジュールは変更不可です。外部サービス、依存関係の変更、補助機能の実行、グローバルインストール、コミット、デプロイ、公開は行いません。ログイン、サーバーデータベース、項目マッピング、リモートアップロード、実際のCRMアカウント、バックグラウンドジョブもありません。UTF-8で100,000バイト、空でないデータ500レコードに制限し、アップロード前に表示します。プレビューは保存しません。既存の検索とAddを維持します。この段階では実装は許可されていません。
レビューの重点
変更不可のモジュールテストは多くの解析ケースを扱いますが、UI・ストレージ動作の証明にはなりません。担当する作業に、次の対象を絞った手動検証を含めてください:
- 作業3:素早いAdd/Importと二重確認が保存と競合したり、連絡先を失ったりしない。
- 作業3:保存失敗時はプレビューと保存済みデータを残し、再試行で各連絡先を一度だけ追加する。
- 作業2:選択・キャンセル後に遅れた読み込みが古いプレビューを戻さない。
- 作業4:HTMLのようなセルは、プレビューと一覧の両方で文字のままとなり、要素が挿入されない。
- 作業4:キーボードのフォーカス・状態と長い値が320pxでも使える。
作業1:純粋なCSVプレビューとマージ
ファイル: 作成するもの: src/import.js。使用するものは変更せず、 src/contacts.js。検証するものも変更せず、 tests/import.test.js と tests/contacts.test.js. インターフェース: 生成するもの: previewCSV(text: string, existing: Contact[]): Preview と mergeReady(preview: Preview, existing: Contact[]): {contacts: Contact[], added: number, skipped: number}。Contactは既存の {id,name,email,company} の形で、Previewは {rows: {record:number,name:string,email:string,company:string,status:'ready'|'invalid'|'duplicate',reason:string}[], counts:{ready:number,invalid:number,duplicate:number}}。normalEmail、validContact、addContactは次から使用します: src/contacts.js.
- [ ] 実行するもの:
node --test tests/import.test.jsを実装前に実行します。現在のツリーでは、次の不足を想定します:src/import.js。製品動作の失敗ではありません。テストは編集しないでください。 - [ ] previewCSVを、次に記載されたスキャナー・上限・ヘッダーの判断に沿って実装します:
docs/architecture.md。プレビューを返す前に構造検証を完了します。重複追跡の前にフィールドを分類し、空でない全レコードと正確な件数を維持します。 - [ ] mergeReadyを実装し、渡された現在の連絡先に照らしてReady行を再確認します。入力と既存連絡先を維持し、addContactでIDを追加し、追加しなかった全ファイルレコードをskippedとして数えます。
- [ ] 実行するもの:
node --test tests/import.test.js tests/contacts.test.js。既存の全17テスト(インポート12、連絡先5)の合格を期待します。引用符、BOM/CRLF、任意company、空レコード、不正な構造、不正行が先にある重複、マージ時の再検証、不変性、上限を検証します。 - [ ] インラインのNodeモジュールのアサーションで確認します(テスト編集は保存しません):ちょうど100,000バイトのASCII入力は受け入れ、100,001は拒否。大文字小文字混在で空白除去したヘッダーは受け入れ、正規化後の重複ヘッダーは拒否。全空白レコードは無視し、短い行を補い、未知のヘッダー値は無視し、閉じた引用符後の文字は拒否します。記載した判断に合わせます。解析するもの:
samples/customer-contacts.csvをinitialContactsとともに解析し、期待する件数は{ready:3,invalid:1,duplicate:2}、行数6,、マージ時の追加3・スキップ3・合計連絡先数5を確認します。
作業2:インラインアップロードと全件レビュー
ファイル: 変更対象: index.html, src/app.js, style.css のみ。作業1に依存します。 インターフェース: 作業1のpreviewCSVとPreviewを使い、アプリ内に生成するもの: renderImport() と、次に記載された状態: docs/architecture.md。維持する既存のもの: #search, #contacts, #status, #add と、その動作を維持します。ラベル付きインラインセクションとファイル入力を追加します。操作のアクセシブル名は Import contacts, CSV file, Review CSV, Import ready contacts, Cancel import, Back to contacts, Fail next save.
- [ ] 次に沿って操作、上限・補足文、レビュー領域を追加します:
docs/screen-design.md。作業3の保存処理が接続されるまでは、確認を無効にします。 - [ ] 開く、ファイル選択、明示的なReview CSV、キャンセル、Backを接続します。世代トークンで非同期読み込みを保護し、新入力時に以前のエラーとプレビューを消します。textContentで全レコードの項目、状態、理由、件数を示します。
- [ ] 既存の連絡先のスタイルと見えるフォーカスを維持しながら、ラベル付きカード、折り返すボタン、長い文字向けの狭い幅のレスポンシブスタイルを追加します。
- [ ] 使用するもの:
npm startとhttp://127.0.0.1:8830でサンプルを選び、6カードと3/1/2の件数を期待します。Cancel/Backで一覧、検索、保存済み値を維持します。不正なファイルではエラーと無効な確認を期待し、その後有効なファイルを選ぶと古いエラーが消えます。読み込み中に素早く選択・キャンセルしても、古いプレビューが戻らないようにします。これは後の実行用のブラウザ検証で、現在の結果を主張するものではありません。
作業3:直列化した確認、失敗、再試行
ファイル: 変更対象: src/app.js のみ(操作は作業2で定義済み)。作業1〜2に依存します。 インターフェース: mergeReadyと既存のaddContact/readContacts/saveContactsを使い、アプリ内に生成するもの: persistContacts(makeNext) というコーディネーターです。makeNextは現在の確定済みContact[]を受け取り、返すものは {contacts:Contact[], added:number, skipped:number} (Addは新規の1行をこの形に合わせます)。Promiseは保存後に結果を返し、busyまたは失敗時に拒否します。新しい公開APIやストレージアダプターの変更はありません。
- [ ] 既存のAddとImportの保存を、次の同期busyガードとコーディネーターに通します:
docs/architecture.md。Importは確認時に再マージし、アップロード中に次の連絡先を先計算しません。保存中は変更操作を無効にし、保存成功後にだけ行を公開します。catch/finallyで成功メッセージを出してはなりません。 - [ ] Fail next saveを、次の実際のアダプター保存で消費するラベル付きチェックボックスとして接続します。失敗時はプレビューと確定済み一覧を維持し、busyを解放して再試行を戻します。キャンセルは書き込みません。再検証後の追加が0なら、保存せず実際の件数を報告します。
- [ ] インポート成功時はプレビューと検索を解除し、同じ一覧を描画し、実際の追加・スキップ件数を通知してフォーカスを戻します。Readyが0件の確認無効化と同期ガードで、繰り返し送信を防いでください。
- [ ] 新しいブラウザでサンプルをインポートし、追加3・スキップ3,・合計5,を期待します。検索でJordanが見つかり、Addで重複しない連絡先を追加でき、再読み込み後も全保存済み連絡先が残ることを確認します。
- [ ] 新しいコンテキストでサンプルをプレビューし、Jordanを手動追加してから確認します。追加2・スキップ4と手動追加の連絡先の維持を期待します。確認をダブルクリックし、180msの保存中にAddを試しても、二重インポート、保存の競合、連絡先の喪失がないことを確認します。
- [ ] 新しいコンテキストでサンプルをプレビューし、Fail next saveを有効にして確認します。一覧と保存状態は変わらず、6レコードのプレビューと失敗状態が残ることを期待します。再試行では追加3・スキップ3を一度だけ行い、再読み込みで連絡先5件を確認します。Addの保存失敗にも回帰がないことを確認し、Readyが0件なら確認できないようにします。
作業4:受け入れ検証と範囲レビュー
ファイル: 許可されたアプリケーションファイルのみをレビューし、テストは変更しません。作業1〜3に依存します。 インターフェース: 既存Contacts UI全体と作業2の操作。新しいAPIはありません。
- [ ] 実行するもの:
npm test。変更不可の全17テストの合格を期待します。これをブラウザの保存・アクセシビリティのカバレッジと同一視しないでください。 - [ ] プレビューとインポート済み一覧で、サンプルのレコード6が次の文字のまま表示されることをブラウザで確認します:
<img src=x onerror=alert(1)>。img要素や実行はありません。引用符付きの複数行company、長いメール・companyが読みやすく折り返すことも確認します。 - [ ] 320px幅とデスクトップで、キーボードだけを使ってアップロード、レビュー、キャンセル、確認、Addを操作します。ラベル、論理的なタブ順、見えるフォーカス、レビュー見出しのフォーカス、ライブ件数・エラー・成功、キャンセル時の復元、ページ全体の横はみ出しがないことを検証します。フォーカストラップはなく、サンプル6レコードすべてに到達できるようにします。
- [ ] 不正ヘッダー・引用符・余分なフィールド、容量超過の入力で、保存済み連絡先が変わらないことを確認します。501レコードは拒否、500は受け入れることを確認し、Readyの行がないプレビューも含めます。
- [ ] 差分を確認します。実装で変わったのは許可された4つのアプリケーションパスのみで、変更不可のファイルと選択の成果物は維持されています。実際のテスト・ブラウザの結果と制約を報告し、オペレーターのレビューのため停止してください。コミットやデプロイは行いません。
元のファイルと出所の詳細
output/docs/implementation-plan.md.txt · SHA-256 f20d8893150a7b9a6b0a44bfdc711d92b535cf9f8fcb7de224c4892b75c5cbf1
# CRM-142 Import Contacts Implementation Plan
> For agentic workers: planning only in this run. Later execution stays native and single-agent; no additional skills, commits, worktrees or delegation. User/project instructions override the upstream execution-subskill template.
**Goal:** Import reviewed CSV contacts into the existing local list without losing contacts or saving during preview.
**Architecture:** Pure parsing/classification/merge in a new import module; inline panel and serialized writes in the existing app. Existing contacts/storage adapters remain unchanged.
**Tech Stack:** Plain HTML/CSS/browser ES modules, localStorage; Node >=24 built-in test runner, no dependencies.
**Spec:** `docs/brainstorming.md`, `docs/screen-design.md`, `docs/architecture.md`; fixed requirements in `tickets/CRM-142.md` and `docs/import-contract.md`.
## Global constraints
Application changes only: `index.html`, `style.css`, `src/app.js`, new `src/import.js`. Tests, samples, ticket, server, package, AGENTS.md and both existing helper modules are immutable. No external services, dependency changes, companion execution, global installation, commits, deployment or publishing. No login, server database, field-mapping wizard, remote uploads, live CRM account or background jobs. Limit 100,000 UTF-8 bytes and 500 nonblank data records; display limits before upload. Preview never persists. Existing search/Add remain working. No implementation is authorized in this phase.
## Review focus
The immutable module tests cover many parsing cases but do not prove UI/storage behavior. Include these targeted manual checks in the owning tasks:
1. Task 3: rapid Add/Import and double confirmation cannot race a save or lose contacts.
2. Task 3: failed save retains preview and old persisted data; retry adds each contact once.
3. Task 2: late file reads after selection/cancel cannot restore stale previews.
4. Task 4: HTML-looking cells remain literal text in both preview and list; no injected element.
5. Task 4: keyboard focus/status and long values remain usable at 320px.
## Task 1: Pure CSV preview and merge
**Files:** Create `src/import.js`; consume unchanged `src/contacts.js`; verify unchanged `tests/import.test.js` and `tests/contacts.test.js`.
**Interfaces:** Produce `previewCSV(text: string, existing: Contact[]): Preview` and `mergeReady(preview: Preview, existing: Contact[]): {contacts: Contact[], added: number, skipped: number}`. Contact is the existing `{id,name,email,company}` shape; Preview is `{rows: {record:number,name:string,email:string,company:string,status:'ready'|'invalid'|'duplicate',reason:string}[], counts:{ready:number,invalid:number,duplicate:number}}`. Consume normalEmail, validContact and addContact from `src/contacts.js`.
- [ ] Run `node --test tests/import.test.js` before implementation; with the current tree expect missing `src/import.js`, not a product behavior failure. Do not edit tests.
- [ ] Implement previewCSV with scanner/limits/header decisions in `docs/architecture.md`; ensure structural validation completes before returning any preview. Classify fields before duplicate tracking, keeping every nonblank record and exact counts.
- [ ] Implement mergeReady, rechecking Ready records against its supplied current contacts. Preserve inputs and old contacts, add IDs through addContact, count every non-added file record as skipped.
- [ ] Run `node --test tests/import.test.js tests/contacts.test.js`; expect all 17 existing tests to pass (12 import, 5 contacts). These prove quoting, BOM/CRLF, optional company, blank records, malformed structures, invalid-first duplicates, merge revalidation, immutability and limits.
- [ ] Run an inline Node module assertion probe (no saved test edits): exact 100,000-byte ASCII input accepted and 100,001 rejected; trimmed mixed-case headers accepted; duplicate normalized headers rejected; whitespace-only records ignored; short records padded; unknown header values ignored; characters after closing quote rejected. Match the documented choices. Parse `samples/customer-contacts.csv` with initialContacts and assert counts `{ready:3,invalid:1,duplicate:2}`, rows length 6, merged added 3/skipped 3/contacts length 5.
## Task 2: Inline upload and complete review
**Files:** Modify `index.html`, `src/app.js`, `style.css` only; depends on Task 1.
**Interfaces:** Consume previewCSV and Preview from Task 1. Produce app-local `renderImport()` and state described in `docs/architecture.md`; keep existing `#search`, `#contacts`, `#status`, `#add` and their behavior. Add a labelled inline section and file input; control accessible names are `Import contacts`, `CSV file`, `Review CSV`, `Import ready contacts`, `Cancel import`, `Back to contacts`, `Fail next save`.
- [ ] Add controls, limit/help text and review container according to `docs/screen-design.md`; keep confirmation disabled until the save path in Task 3 is connected.
- [ ] Connect opening, file selection, explicit Review CSV, cancellation and Back; guard asynchronous reads with a generation token and clear prior errors/preview on new input. Show every record with fields/status/reason and counts using textContent.
- [ ] Add narrow responsive styles for labelled record cards, wrapped buttons and long text, retaining existing contact styling and visible focus.
- [ ] With `npm start` and `http://127.0.0.1:8830`, select the sample: expect six cards and 3/1/2 counts. Cancel/Back must preserve list, search and stored value. Select a malformed file: expect error and disabled confirmation; select a valid file afterward: old error disappears. Select/cancel rapidly while reading: no stale preview returns. These are browser checks for later execution, not claims of current results.
## Task 3: Serialized confirmation, failure and retry
**Files:** Modify `src/app.js` only (controls already defined in Task 2). Depends on Tasks 1–2.
**Interfaces:** Consume mergeReady and existing addContact/readContacts/saveContacts. Produce app-local `persistContacts(makeNext)` coordinator, where makeNext receives current committed Contact[] and returns `{contacts:Contact[], added:number, skipped:number}` (Add adapts its one new row to this shape). Promise resolves that result after storage; rejects on busy/failure. No new exported API or storage adapter changes.
- [ ] Route existing Add and Import saves through the synchronous busy guard and coordinator in `docs/architecture.md`. Import remerges at confirmation; do not precompute next contacts during upload. Disable mutation controls while saving. Publish rows only after successful storage; no success message in catch/finally.
- [ ] Wire Fail next save as a labelled checkbox consumed by the next actual adapter save. Failure retains preview and committed list, releases busy and restores retry; cancellation never writes. For zero additions after revalidation skip storage and report actual counts.
- [ ] On successful import clear preview and search, render the same list, announce actual added/skipped counts and restore focus. Ensure disabled zero-Ready confirmation and synchronous guarding prevent repeat submissions.
- [ ] In a fresh browser import the sample: expect added 3/skipped 3, total 5, search finds Jordan, Add still adds a unique contact, and reload retains all saved contacts.
- [ ] Fresh context: preview sample, Add Jordan manually, then confirm; expect added 2/skipped 4 and preservation of the manual contact. Double-click confirm and attempt Add during the 180ms save: no duplicate import, overlapping save or lost contact.
- [ ] Fresh context: preview sample, arm Fail next save and confirm; expect unchanged list/storage and retained six-record preview with failure status. Retry: expect added 3/skipped 3 once, then reload and verify five contacts. Also exercise failed Add to ensure no regression. Zero-Ready preview must not allow confirmation.
## Task 4: Acceptance verification and scope review
**Files:** Review only allowed application files; tests remain unchanged. Depends on Tasks 1–3.
**Interfaces:** Full existing Contacts UI plus the Task 2 controls; no new API.
- [ ] Run `npm test`; expect all 17 immutable tests to pass. Do not equate this with browser storage/accessibility coverage.
- [ ] Browser check sample record 6 in preview and imported list: literal `<img src=x onerror=alert(1)>`, no img element or execution. Check a quoted multiline company and a long email/company for readable wrapping.
- [ ] At 320px and desktop, operate upload/review/cancel/confirm and Add using keyboard only. Verify labels, logical tab order, visible focus, review-heading focus, live counts/errors/success, cancellation focus restoration and no horizontal page scroll. No focus trap; all six sample records reachable.
- [ ] Check malformed headers/quotes/extra fields and oversized input leave stored contacts unchanged; confirm 501 records rejected and 500 accepted. Include a preview with no Ready rows.
- [ ] Inspect diff: only the four allowed application paths changed during implementation; immutable files and selection artifacts unchanged. Report actual test/browser outcomes and any limitation. Stop for operator review; do not commit or deploy.