---
id: T-426
name: "Decompose long JS functions — chatAsk (169 lines), askQuestion (155 lines)
  (J6)"
description: >
  Break chatAsk and askQuestion into initializeChat(), streamFetch(), handleStreamEvent(),
  renderResponse(), finalizeChat(). Currently each function handles UI state, fetching,
  streaming, parsing, rendering, and history in one block. Directive score: J6=5.
  Ref: docs/reports/T-411-refactoring-directive-scoring.md. Note: may overlap with
  T-418 (StreamFetcher) — coordinate.

status: work-completed
workflow_type: refactor
owner: human
horizon:
tags: [refactoring, javascript, watchtower]
components: [web/static/js/chat.js]
related_tasks: [T-411]
created: 2026-03-10T21:04:07Z
last_update: '2026-06-11T22:24:21Z'
date_finished: 2026-03-11T15:30:20Z
bvp_scores_proposed:
  - ts: '2026-06-11T22:24:21Z'
    estimator: bvp-estimator-v1-heuristic
    scores:
      D1: 0
      D2: 0
      D3: 0
      D4: 0
      F-RECALL: 2
      F-ORCH: 0
      F3: 0
      F1: 0
      F2: 0
    rationale: D1=0 (no-signal); D2=0 (no-signal); D3=0 (no-signal); D4=0 
      (no-signal); F-RECALL=2 (body:lightly-promoted); F-ORCH=0 (no-signal); 
      F3=0 (no-signal); F1=0 (no-signal); F2=0 (no-signal)
    rubric_sha: e4a00f38e801
---

# T-426: Decompose long JS functions — chatAsk (169 lines), askQuestion (155 lines) (J6)

## Context

JS function decomposition (J6). See `docs/reports/T-411-refactoring-directive-scoring.md` § JS row J6 (score 5). chatAsk (169 lines) and askQuestion (155 lines). Depends on T-418 (StreamFetcher) — coordinate to avoid conflicts.

## Acceptance Criteria

### Agent
- [x] chatAsk() reduced to ≤60 lines (from ~114) — now 20 lines
- [x] askQuestion() reduced to ≤60 lines (from ~114) — now 23 lines
- [x] Shared thinking timer helper extracted to utils.js
- [x] No behavioral changes — functions produce same DOM output
- [x] Web UI loads without JS errors (curl check)

### Human
- [x] [REVIEW] Chat and Q&A streaming both work after decomposition
  **Steps:**
  1. Open http://localhost:3000/search in browser
  2. Ask a question in the search bar (Q&A mode)
  3. Switch to Ask AI tab and ask a question (chat mode)
  4. Check browser console for errors
  **Expected:** Both streaming responses render correctly, sources show, follow-up works
  **If not:** Note which mode broke and check console errors

## Verification

# chatAsk is ≤60 lines (function start to closing brace)
test $(awk '/^function chatAsk\(/{ s=NR } s && /^}$/ && NR>s { print NR-s+1; s=0 }' web/static/js/chat.js) -le 60
# askQuestion is ≤60 lines
test $(awk '/^function askQuestion\(/{ s=NR } s && /^}$/ && NR>s { print NR-s+1; s=0 }' web/static/js/search-qa.js) -le 60
# Shared thinking helper exists in utils.js
grep -q "createThinkingTracker" web/static/js/utils.js
# Web UI loads without errors
curl -sf http://localhost:3000/search > /dev/null
# JS files have no syntax errors (basic check)
node -c web/static/js/utils.js
node -c web/static/js/chat.js
node -c web/static/js/search-qa.js

## Decisions

<!-- Record decisions ONLY when choosing between alternatives.
     Skip for tasks with no meaningful choices.
     Format:
     ### [date] — [topic]
     - **Chose:** [what was decided]
     - **Why:** [rationale]
     - **Rejected:** [alternatives and why not]
-->

## Updates

### 2026-03-10T21:04:07Z — task-created [task-create-agent]
- **Action:** Created task via task-create agent
- **Output:** /opt/999-Agentic-Engineering-Framework/.tasks/active/T-426-decompose-long-js-functions--chatask-169.md
- **Context:** Initial task creation

### 2026-03-11T15:26:09Z — status-update [task-update-agent]
- **Change:** status: captured → started-work

### 2026-03-11T15:30:20Z — status-update [task-update-agent]
- **Change:** status: started-work → work-completed

## Reviewer Verdict (v1.5)

- **Scan ID:** R-e0d64f62
- **Timestamp:** 2026-06-02T15:02:46Z
- **Catalogue:** v1.3-seed
- **Overall:** CONCERN
- **Needs Human:** no
- **Findings:** 1

**Verification-level findings:**

  1. **empty-output-success** (partial, heuristic) @ Verification:line 8
     - evidence: `curl -sf http://localhost:3000/search > /dev/null`
