T4.1 - B4: the counts come from the server, and disagreement is said out loud

The defect B4 names is not that the numbers were wrong. On one browser with one
cache they were right. It is that they were derived from the caller's own
localStorage, so two people on the same project saw different numbers and neither
was told.

SERVER. /api/wps/metrics now returns everything the creator's dashboard shows -
total, mine, release-ready, on hold, overdue, est/actual hours, by_status,
by_discipline, progress (overall and per discipline) and the gating list. It
already existed for a subset; the rest was being summed in the browser.

Two things moved to the server rather than being duplicated there:

  - PROGRESS_WEIGHT, the status-to-percentage table. It was PROGRESS_W in
    wp-creation-app.js; the JS copy is deleted rather than left in place, because
    two copies of a weighting table is how the two drift apart.
  - "release-ready" now counts `waitingOn` predecessors as blocking, which the
    browser did and the old endpoint did not. Without that the phrase would have
    changed meaning the moment the dashboard stopped computing it locally.

New GET /api/projects/{id}/summary gives the launcher the SOP state it was
reading out of localStorage.

by_location is shaped for CR-018 in wave 6, per the task's instruction not to
build a shape that cannot group by building/floor/sector. It reports its own
dimensions alongside the groups:

  {"dimensions": ["location"], "groups": [{"key": {...}, "total": n,
    "release_ready": n, "on_hold": n, "overdue": n, "by_status": {...}}]}

Today a package carries one free-text `location`, so that is the one dimension.
_location_key() already prefers structured building/floor/sector when present, so
CR-004 changes the dimensions and the keys and leaves the response shape alone.

CLIENT. The launcher's SOP card and the dashboard's tiles, chips, progress bars
and gating panel all read the server. There is deliberately no cache fallback: a
silently-stale number that looks authoritative is the thing being removed, so a
failed request renders an explicit error and a retry.

Writes flush through the outbox before the counts are re-read (dashRefreshAfterWrite).
Without that the refresh races the push and shows pre-write totals - the same
stale number arriving by a different route.

THE ONE COUNT STILL COMPUTED LOCALLY, stated rather than skipped: the board table
is a LIST of the packages this browser holds, which is what keeps the field view
working offline, and its header counts rows. Rather than pretend otherwise, it is
reconciled against the server's total and shows "this browser has N of M" when
they differ - usually a save that has not finished syncing. Nobody now sees a
number that disagrees with the project without being told, which is what B4 asks
for.

VERIFICATION. tests/aggregates_check.py, 16 checks, all passing. It tests what was
broken rather than whether the totals are right - the latter passed before this
change:

  - root and pat get byte-identical aggregates for the same project
  - the dashboard shows the server's total after localStorage is POISONED with a
    different package list; it cannot do that if it is summing the cache
  - a simulated outage renders "Counts unavailable", names the failure, offers a
    retry, renders no tiles beside it, and announces via role=alert
  - the launcher reports the SOP complete against a cache that says otherwise,
    and says "Could not check" when the request fails
  - by_location's groups are keyed by its declared dimensions, carry their own
    rollups, and sum to the project total

browser_check 71/71, f_items 5 FIXED / F6 REPRODUCES.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-15 19:24:12 -05:00
parent 12c0e5ca74
commit b670ae719d
4 changed files with 572 additions and 62 deletions

View File

@@ -1779,6 +1779,21 @@ const WPData = {
};
let dashFilter={status:'',discipline:'',q:'',flag:''};
// A write has to reach the server before the server can count it. The outbox is
// the only path writes take, so flush it and then re-read - otherwise the refresh
// races the push and shows the pre-write totals, which is the same stale number
// B4 removed, just arriving a different way.
function dashRefreshAfterWrite(){
const done = () => loadDashMetrics();
try {
if(typeof ProjectData!=='undefined' && ProjectData.flushSync){
Promise.resolve(ProjectData.flushSync()).then(done, done);
return;
}
} catch(e){}
done();
}
function dashToggleFlag(f){
if(f==='all'){ dashFilter={status:'',discipline:'',q:'',flag:''}; }
else { dashFilter.flag = dashFilter.flag===f ? '' : f; }
@@ -1790,8 +1805,10 @@ function dashSetStatus(s){ dashFilter.status = dashFilter.status===s ? '' : s; d
let dashPage=0, dashShowArchived=false, dashArchived=[];
const DASH_PAGE_SIZE=25;
// Weighted completion by status (0..1) so progress is smoother than done/not-done.
const PROGRESS_W={'Draft':0,'Scheduled':0.25,'Issue':0.4,'Issued':0.5,'In Progress':0.75,'QC':0.9,'Closed':1};
function wpProgress(p){ const w=PROGRESS_W[p.status]; return w==null?0:w; }
// The progress weights moved to server/app.py PROGRESS_WEIGHT at T4.1 (B4), so the
// bar is computed once for everyone instead of once per browser. Deleted here
// rather than left in place: a second copy of a weighting table is how the two
// drift apart, and nothing in this file reads it any more.
function dashGo(pg){ dashPage=pg; renderDashboard(); }
function dashToggleArchived(on){
dashShowArchived=!!on; dashPage=0;
@@ -1804,13 +1821,13 @@ function dashArchive(id){
if(!confirm('Archive "'+(p.number||p.subject||'this package')+'"? It will be hidden from the active board but kept for the record.')) return;
if(typeof ProjectData!=='undefined' && ProjectData.archiveWP) ProjectData.archiveWP(id,true);
const ix=savedPackages.findIndex(x=>x.id===id); if(ix>=0){ p.archived=true; dashArchived.unshift(p); savedPackages.splice(ix,1); }
saveStore(); renderSavedList(); renderDashboard(); toast('Archived '+(p.number||''));
saveStore(); renderSavedList(); toast('Archived '+(p.number||'')); dashRefreshAfterWrite();
}
function dashUnarchive(id){
const ix=dashArchived.findIndex(x=>x.id===id); const p=ix>=0?dashArchived[ix]:null; if(!p) return;
if(typeof ProjectData!=='undefined' && ProjectData.archiveWP) ProjectData.archiveWP(id,false);
p.archived=false; dashArchived.splice(ix,1); if(!savedPackages.some(x=>x.id===id)) savedPackages.push(p);
saveStore(); renderSavedList(); renderDashboard(); toast('Restored '+(p.number||''));
saveStore(); renderSavedList(); toast('Restored '+(p.number||'')); dashRefreshAfterWrite();
}
// Consistent colored status pill, reused by the dashboard board and the saved list.
function statusPill(s){
@@ -1832,30 +1849,74 @@ function showDashboard(){
document.querySelectorAll('.main > .card, .main > .nav-row').forEach(e=>e.style.display='none');
document.getElementById('pkg-output').style.display='none';
const dv=document.getElementById('dashboard-view'); if(dv) dv.style.display='';
currentView='Dashboard'; cmtUpdateCurStep(); renderDashboard();
currentView='Dashboard'; cmtUpdateCurStep();
// Ask the server every time the dashboard is opened. Counts that were correct
// when you last looked are not evidence that they are correct now.
dashMetrics=null; loadDashMetrics();
window.scrollTo({top:0,behavior:'smooth'}); track('dashboard_open');
}
// ── B4: dashboard counts come from the server ────────────────────────────────
// Every number on this dashboard used to be summed in the browser from
// savedPackages, which is this browser's localStorage. Two people on the same
// project saw different totals and neither was told. /api/wps/metrics computes
// them from the database instead, so the answer is the project's answer.
//
// There is no localStorage fallback. A silently-stale number that looks
// authoritative is the failure being fixed, so a failed fetch renders an error
// panel and a retry - not a zero, and not the last good answer.
let dashMetrics = null; // last successful server response
let dashMetricsErr = null; // Error, if the last fetch failed
let dashMetricsLoading = false;
function dashMetricsUrl(){
const q = [];
if(activeProjectId) q.push('project_id=' + encodeURIComponent(activeProjectId));
return '/api/wps/metrics' + (q.length ? '?' + q.join('&') : '');
}
function loadDashMetrics(){
dashMetricsLoading = true; dashMetricsErr = null;
return fetch(dashMetricsUrl(), { headers: { 'Accept': 'application/json' } })
.then(r => { if(!r.ok) throw new Error('HTTP ' + r.status); return r.json(); })
.then(m => { dashMetrics = m; dashMetricsErr = null; })
.catch(e => { dashMetricsErr = e; dashMetrics = null; })
.then(() => { dashMetricsLoading = false; renderDashboard(); });
}
function renderDashboard(){
const all=countableWPs();
const byStatus={}; STATUS_ORDER.concat(['Issue']).forEach(s=>byStatus[s]=0);
let estH=0, actH=0, ready=0, hold=0, overdue=0, mine=0; const byDisc={}; const meId=myUserId();
all.forEach(p=>{
byStatus[p.status]=(byStatus[p.status]||0)+1;
estH+=parseFloat(p.hours)||0; actH+=parseFloat(p.actualHrs)||0;
if(p.status==='Issue') hold++;
if(meId && p.assigneeId===meId) mine++;
if(!wpReleaseBlocked(p) && p.status!=='Closed' && p.status!=='Issue') ready++;
if(isOverdue(p)) overdue++;
(p.disciplines&&p.disciplines.length?p.disciplines:['(none)']).forEach(d=>byDisc[d]=(byDisc[d]||0)+1);
});
const m = dashMetrics;
const byStatus = m ? Object.assign({}, m.by_status) : {};
const byDisc = m ? Object.assign({}, m.by_discipline) : {};
if(m) STATUS_ORDER.concat(['Issue']).forEach(s=>{ if(byStatus[s]==null) byStatus[s]=0; });
const estH=m?m.est_hours:0, actH=m?m.actual_hours:0, ready=m?m.release_ready:0,
hold=m?m.on_hold:0, overdue=m?m.overdue:0, mine=m?m.mine:0;
const meId=myUserId();
// Clickable metric cards filter the board (flag-based); a card with no flag is static.
const card=(label,val,cls,flag)=>{
const active = flag && dashFilter.flag===flag ? ' dm-active' : '';
const attr = flag ? ` onclick="dashToggleFlag('${flag}')" title="Click to filter the board"` : '';
return `<div class="dash-metric ${cls||''}${active}"${attr}><div class="dm-val">${val}</div><div class="dm-label">${esc(label)}</div></div>`;
};
// The counts panel is server-derived; if that request failed, say so instead of
// rendering a row of zeros that reads as "this project is empty".
if(dashMetricsErr){
let eh = `<div class="dash-panel" role="alert"><div class="dash-panel-title">⚠ Counts unavailable</div>`;
eh += `<div class="field-hint">The dashboard totals are computed by the server so that everyone on this project sees the same numbers. That request failed (${esc(String(dashMetricsErr.message||dashMetricsErr))}), and this browser's own copy is not shown in its place because it can be out of date without looking it.</div>`;
eh += `<div class="material-actions"><button class="btn btn-ghost" onclick="loadDashMetrics()">Retry</button></div></div>`;
const host0=document.getElementById('dashboard-view');
if(host0){ host0.innerHTML = eh; }
return;
}
if(!m && dashMetricsLoading){
const host1=document.getElementById('dashboard-view');
if(host1){ host1.innerHTML = `<div class="dash-panel" role="status"><div class="field-hint">Loading project totals…</div></div>`; }
return;
}
if(!m){ loadDashMetrics(); return; }
let h=`<div class="dash-metrics">
${card('Total WPs', all.length, '', 'all')}
${card('Total WPs', m.total, '', 'all')}
${meId ? card('My WPs', mine, mine?'dm-blue':'', 'mine') : ''}
${card('Release-ready', ready, ready?'dm-green':'', 'ready')}
${card('On hold', hold, hold?'dm-red':'', 'onhold')}
@@ -1872,23 +1933,23 @@ function renderDashboard(){
h+=`<div class="dash-breakdown"><div><div class="dash-bd-title">By status</div>${statusChips||'—'}</div>
<div><div class="dash-bd-title">By discipline</div>${discChips}</div></div>`;
// progress by phase (discipline), weighted by status; archived excluded
const overallPct = all.length ? Math.round(all.reduce((s,p)=>s+wpProgress(p),0)/all.length*100) : 0;
const phaseGroups={};
all.forEach(p=>{ (p.disciplines&&p.disciplines.length?p.disciplines:['(none)']).forEach(d=>{ (phaseGroups[d]=phaseGroups[d]||[]).push(p); }); });
// progress by phase (discipline), weighted by status; archived excluded.
// The weights live in server/app.py PROGRESS_WEIGHT now, so one definition
// produces the bar for everyone rather than one per browser.
const overallPct = m.progress.overall_pct;
let prog=`<div class="dash-panel"><div class="dash-panel-title">Progress by phase</div>`;
prog+=`<div class="prog-row"><div class="prog-name"><strong>Overall</strong></div><div class="prog-bar"><div class="prog-fill" style="width:${overallPct}%"></div></div><div class="prog-pct">${overallPct}%</div></div>`;
Object.keys(phaseGroups).sort().forEach(d=>{ const g=phaseGroups[d]; const pct=g.length?Math.round(g.reduce((s,p)=>s+wpProgress(p),0)/g.length*100):0; const done=g.filter(p=>p.status==='Closed').length;
prog+=`<div class="prog-row"><div class="prog-name">${esc(d)}</div><div class="prog-bar"><div class="prog-fill" style="width:${pct}%"></div></div><div class="prog-pct">${pct}% <span class="prog-sub">${done}/${g.length}</span></div></div>`; });
m.progress.by_discipline.forEach(g=>{
prog+=`<div class="prog-row"><div class="prog-name">${esc(g.name)}</div><div class="prog-bar"><div class="prog-fill" style="width:${g.pct}%"></div></div><div class="prog-pct">${g.pct}% <span class="prog-sub">${g.done}/${g.total}</span></div></div>`; });
prog+=`<div class="field-hint" style="margin-top:8px">Weighted by status (Draft 0 · Scheduled 25 · Issued 50 · In&nbsp;Progress 75 · QC 90 · Closed 100%). Archived packages excluded.</div></div>`;
h+=prog;
// gating panel — what's blocking release
const gated=all.filter(p=>wpOpenConstraints(p).length>0);
// gating panel — what's blocking release, from the server
const gated=m.gating||[];
h+=`<div class="dash-panel"><div class="dash-panel-title">⛔ Gating constraints (${gated.length} package${gated.length===1?'':'s'} blocked)</div>`;
h+= gated.length ? `<table class="dash-table"><thead><tr><th>WP #</th><th>Subject</th><th>Blocked by</th></tr></thead><tbody>`+
gated.map(p=>`<tr><td class="row-label">${esc(p.number||'—')}</td><td>${esc(p.subject||'')}</td>
<td>${wpOpenConstraints(p).map(c=>esc(c.name)+(c.comment?` <span style="color:var(--text-dim)">(${esc(c.comment)})</span>`:'')).join('<br>')}</td></tr>`).join('')+
gated.map(g=>`<tr><td class="row-label">${esc(g.number||'—')}</td><td>${esc(g.subject||'')}</td>
<td>${(g.blocked_by||[]).map(c=>esc(c.name)+(c.comment?` <span style="color:var(--text-dim)">(${esc(c.comment)})</span>`:'')).join('<br>')}</td></tr>`).join('')+
`</tbody></table>` : `<div class="field-hint">No open constraints — every package is clear of gates.</div>`;
h+=`</div>`;
@@ -1921,7 +1982,18 @@ function renderDashboard(){
if(dashPage>=pages) dashPage=pages-1;
if(dashPage<0) dashPage=0;
const pageRows=rows.slice(dashPage*DASH_PAGE_SIZE, dashPage*DASH_PAGE_SIZE+DASH_PAGE_SIZE);
h+=`<div class="dash-panel"><div class="dash-panel-title">Work Packages (${totalRows})</div>
// The board is a LIST, not a rollup: it renders the packages this browser holds,
// which is what keeps the field view usable offline. Its header is therefore the
// only count on this page not computed by the server — so it is reconciled against
// the server's instead of being left to disagree in silence, which is the whole of
// what B4 objects to. A divergence means this browser's copy is behind (a pending
// outbox write, a stale tab), and saying so is more useful than hiding it.
const localCountable = (dashShowArchived ? WPData.list() : WPData.list().filter(p=>!p.archived))
.filter(p=>!p.split).length;
const drift = (!dashShowArchived && m && localCountable !== m.total)
? ` <span class="dash-chip chip-red" title="The server counts ${m.total} package(s) on this project; this browser is holding ${localCountable}. Usually a save that has not finished syncing.">this browser has ${localCountable} of ${m.total}</span>`
: '';
h+=`<div class="dash-panel"><div class="dash-panel-title">Work packages (${totalRows})${drift}</div>
<table class="dash-table"><thead><tr><th>WP #</th><th>Subject</th><th>Type</th><th>Discipline</th><th>Status</th><th>Gates</th><th>Due</th><th>Hrs</th><th></th></tr></thead><tbody>`;
if(!totalRows) h+=`<tr><td colspan="9" class="field-hint" style="padding:14px">No work packages match.</td></tr>`;
pageRows.forEach(p=>{
@@ -1970,7 +2042,8 @@ function dashIssue(id){
return;
}
if(!confirm('Issue work package "'+(p.number||p.subject)+'"? This marks it released to the field.')) return;
WPData.issue(id); renderDashboard(); renderSavedList(); toast('Issued '+(p.number||'')); track('dashboard_issue');
WPData.issue(id); renderSavedList(); toast('Issued '+(p.number||'')); track('dashboard_issue');
dashRefreshAfterWrite();
}
function dashView(i){ if(savedPackages[i]) renderPackage(savedPackages[i]); }
function dashEdit(i){ const p=savedPackages[i]; if(!p) return; editingId=p.id; loadPackageIntoForm(p); }