The implementation plan
Download original ↓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:
- Task 3: rapid Add/Import and double confirmation cannot race a save or lose contacts.
- Task 3: failed save retains preview and old persisted data; retry adds each contact once.
- Task 2: late file reads after selection/cancel cannot restore stale previews.
- Task 4: HTML-looking cells remain literal text in both preview and list; no injected element.
- 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.jsbefore implementation; with the current tree expect missingsrc/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.csvwith 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 startandhttp://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.
Original file & source details
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.