diff --git a/changelog.d/fixed-18813-paused-resume-role-gate.md b/changelog.d/fixed-18813-paused-resume-role-gate.md new file mode 100644 index 0000000000..83b76aeb58 --- /dev/null +++ b/changelog.d/fixed-18813-paused-resume-role-gate.md @@ -0,0 +1 @@ +- A paused agent no longer shows an empty slot where its resume button belongs: the focused agent panel kept the toggle it first rendered (its fast path never added one, and the panel ignored the role), so a panel opened before the viewer's role settled stayed without resume until reload. Agent cards, the detail panel and the cadence table now repaint when the role settles, and a viewer below owner (or one still being checked) sees the pause/resume control disabled with "Needs the owner role" or "Checking your access..." instead of nothing. A paused sandboxed agent offers plain resume rather than "start & resume" (it has no session to start), and the cadence table shows one `paused` chip with its reason on hover instead of repeating it in every mode column (#18813). diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index b86a4695b0..e2aa88ec1b 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -248,6 +248,8 @@ Full entry: [Agent Logs](dashboard-sections/workflow-and-agents.md#agent-logs). Each worker Hive runs, what it is doing now, and buttons to pause, restart or configure it. +The pause/resume control is owner-only but never absent: below the owner role, or while the role is still being checked, it renders disabled with the tooltip "Needs the owner role" or "Checking your access...". Cards and the detail panel repaint when the role settles. A paused sandboxed agent offers plain `resume` (no session to start). The cadence table shows a single `paused` chip (with its reason on hover) beside the agent name; the mode columns show plain `paused` text. + Full entry: [Agents](dashboard-sections/workflow-and-agents.md#agents). ## Hive activity monitor diff --git a/src/pkg/dashboard/agent_paused_toggle_role_18813.test.js b/src/pkg/dashboard/agent_paused_toggle_role_18813.test.js new file mode 100644 index 0000000000..50f2c7b93a --- /dev/null +++ b/src/pkg/dashboard/agent_paused_toggle_role_18813.test.js @@ -0,0 +1,119 @@ +// #18813: a paused agent must always show a reachable (or explained) resume +// control, and the role-gated slot repaints when the role settles. +const test = require('node:test'); +const assert = require('node:assert'); +const fs = require('node:fs'); +const path = require('node:path'); + +const js = n => fs.readFileSync(path.join(__dirname, 'static-src', 'js', n), 'utf8'); +const cards = js('agent-cards.js'); +const detail = js('agent-detail.js'); +const gov = js('governor-view.js'); + +// Pull a top-level `function name(...) {...}` out of a source by brace matching. +function fn(src, name) { + const start = src.indexOf('function ' + name + '('); + assert.ok(start >= 0, name + ' not found'); + let i = src.indexOf('{', src.indexOf(')', start)); + let depth = 0; + for (; i < src.length; i++) { + if (src[i] === '{') depth++; + else if (src[i] === '}' && --depth === 0) return src.slice(start, i + 1); + } + throw new Error('unbalanced ' + name); +} +function consts(src, names) { + return names.map(n => src.match(new RegExp('const ' + n + ' = [^;]+;'))[0]).join('\n'); +} + +const win = {}; +const esc = s => String(s).replace(/&/g, '&').replace(/ 'no cadence', () => 'busy'); + +function setRole(role, settled) { + win._hiveRole = role; + if (settled) win._hiveRoleSettled = true; else delete win._hiveRoleSettled; +} +const paused = (o) => Object.assign({ name: 'reviewer', paused: true, state: 'paused', enabled: true }, o); +const matrix = [ + paused({ sandboxed: true }), paused({ sandboxed: false }), + paused({ sandboxed: true, state: 'running' }), paused({ sandboxed: false, state: 'running' }), +]; + +test('role unknown: control is disabled with the checking hint, never empty', () => { + setRole(undefined, false); + assert.strictEqual(api.agentToggleGateReason(), 'Checking your access...'); + for (const a of matrix) { + const h = api.agentGatedToggleHtml(a, true, false); + assert.ok(/]* disabled /.test(h)); + assert.ok(h.includes('title="Checking your access..."')); + assert.ok(h.includes('resume')); + assert.ok(!h.includes('data-action')); + } +}); + +test('role read and read-write: disabled with the owner hint', () => { + for (const role of ['read', 'read-write', 'merger']) { + setRole(role, true); + assert.strictEqual(api.agentToggleGateReason(), 'Needs the owner role'); + const h = api.agentGatedToggleHtml(paused({ sandboxed: true }), true, false); + assert.ok(h.includes('title="Needs the owner role"') && h.includes(' disabled ')); + } + setRole(undefined, true); // /api/role gave up + assert.strictEqual(api.agentToggleGateReason(), 'Needs the owner role'); +}); + +test('role settles to owner: the live resume control appears', () => { + setRole(undefined, false); + assert.notStrictEqual(api.agentToggleGateReason(), ''); + setRole('owner', true); + assert.strictEqual(api.agentToggleGateReason(), ''); + for (const a of matrix) { + const h = api.pausedAgentActionHtml(a); + assert.ok(h.includes('data-action=')); + assert.ok(!h.includes(' disabled')); + } +}); + +test('sandboxed paused agent gets plain resume, not start & resume', () => { + const sb = paused({ sandboxed: true }); + assert.ok(api.pausedAgentActionHtml(sb).includes('data-action="toggleAgent"')); + assert.ok(api.pausedAgentActionHtml(sb).includes('▶ resume')); + assert.ok(!api.pausedAgentActionHtml(sb).includes('start & resume')); + assert.ok(!api.agentGatedToggleHtml(sb, true, false).includes('start & resume')); + const plain = paused({ sandboxed: false }); + assert.ok(api.pausedAgentActionHtml(plain).includes('data-action="startResumeAgent"')); + assert.ok(api.agentGatedToggleHtml(plain, true, false).includes('start & resume')); +}); + +test('running agent below owner shows a disabled pause', () => { + setRole('read', true); + const h = api.agentGatedToggleHtml({ name: 'deps', state: 'running' }, false, false); + assert.ok(h.includes('pause') && h.includes(' disabled ') && h.includes('Needs the owner role')); +}); + +test('card grid, detail and fast path use the gate and repaint on role settle', () => { + assert.ok(cards.includes('(window._hiveRoleListeners = window._hiveRoleListeners || []).push(agentsOnRoleReady)')); + assert.ok(fn(cards, 'agentsOnRoleReady').includes('renderAgents(window._lastAgents)')); + assert.ok(fn(cards, 'agentsOnRoleReady').includes('ocRenderAgentDetail()')); + assert.ok(detail.includes('agentToggleGateReason()].join'), 'role is part of the detail fingerprint'); + assert.ok(detail.includes('agentGatedToggleHtml(a, true, false)')); + assert.ok(!/!dashboardRoleAtLeast\(window\._hiveRole \|\| 'read', 'owner'\)\s*\? ''/.test(detail), 'no silent empty slot'); +}); + +test('collapsed cadence row: one paused chip, plain text in the mode columns', () => { + assert.ok(gov.includes("showPaused ? 'paused' : val")); + const chipUses = gov.match(/pausedBadgeHtml/g) || []; + // definition + the single name-column use + assert.strictEqual(chipUses.length, 2); +}); diff --git a/src/pkg/dashboard/static-src/js/agent-cards.js b/src/pkg/dashboard/static-src/js/agent-cards.js index f72670ee33..94b6b42ba9 100644 --- a/src/pkg/dashboard/static-src/js/agent-cards.js +++ b/src/pkg/dashboard/static-src/js/agent-cards.js @@ -717,7 +717,7 @@ function agentToggleTitle(a, isPaused, isOff) { if (isPaused) { const rest = []; - if (a.state !== 'running') rest.push('its session is down (use \u21bb restart)'); + if (a.state !== 'running' && a.sandboxed !== true) rest.push('its session is down (use \u21bb restart)'); if (a.offByCadence === true) rest.push('its cadence for the current governor mode is off (\u2699\uFE0F \u2192 Cadences)'); if (a.unscheduledInMode === true) rest.push(`it has a cadence only in ${agentCadenceModesLabel(a)}, none for the current governor mode (\u2699\uFE0F \u2192 Cadences)`); if (a.noCadence === true) rest.push('no governor mode gives it a cadence (\u2699\uFE0F \u2192 Cadences)'); @@ -763,7 +763,12 @@ return ``; } function pausedAgentActionHtml(a) { - if (a.state === 'running') { + /* A sandboxed agent has no long-lived session: it sits in state 'paused' + or 'idle' and runs each kick in a throwaway container, so "session + down" is never its blocker. Offering "start & resume" there chained a + restart that has nothing to restart (#18813); plain resume is the + whole action. */ + if (a.state === 'running' || a.sandboxed === true) { return ``; } const rest = []; @@ -774,6 +779,34 @@ if (rest.length > 0) title += ` Still not enough on its own: ${rest.join('; ')}.`; return ``; } + /* Owner-gated pause/resume slot (#18813). Never an empty slot: a viewer + below owner, or one whose role has not settled yet, sees the control + DISABLED with the reason in its tooltip. agentToggleGateReason returns + '' when the viewer may act. The role is "known" under the same rule the + Inbox uses: /api/role answered or gave up. */ + const AGENT_GATE_NEEDS_OWNER = 'Needs the owner role'; + const AGENT_GATE_CHECKING = 'Checking your access...'; + function agentRoleKnown() { return window._hiveRoleSettled === true || window._hiveRole !== undefined; } + function agentToggleGateReason() { + if (dashboardRoleAtLeast(window._hiveRole || 'read', 'owner')) return ''; + return agentRoleKnown() ? AGENT_GATE_NEEDS_OWNER : AGENT_GATE_CHECKING; + } + function agentGatedToggleHtml(a, isPaused, isOff) { + const reason = agentToggleGateReason() || AGENT_GATE_NEEDS_OWNER; + const label = isPaused + ? (a.state === 'running' || a.sandboxed === true ? '\u25b6 resume' : '\u25b6 start & resume') + : '\u23f8 pause'; + const cls = isPaused ? 'btn-danger paused' : 'btn-secondary running'; + return ``; + } + /* Repaint the role-gated agent surfaces the moment the role settles. The + cards used to be left as rendered with whatever role was known then. */ + function agentsOnRoleReady() { + if (window._lastAgents) renderAgents(window._lastAgents); + if (typeof ocRenderAgentDetail === 'function') ocRenderAgentDetail(); + if (typeof renderGovernorFromLastStatus === 'function') renderGovernorFromLastStatus(); + } + (window._hiveRoleListeners = window._hiveRoleListeners || []).push(agentsOnRoleReady); function neverScheduledChipHtml(a, configTarget) { if (a.noCadence !== true) return ''; const who = a.displayName || a.name; @@ -935,7 +968,7 @@ // Healthy-but-governor-paused → hollow green dot (see detail-header renderer). const dotState = isOff ? 'off-healthy' : dotCls; const canToggle = a.beadRole !== 'supervisor'; - const canPauseToggle = canToggle && dashboardRoleAtLeast(window._hiveRole || 'read', 'owner'); + const canPauseToggle = canToggle && agentToggleGateReason() === ''; /* RESUME LEADS (#5594). A paused agent's card offers its pause toggle FIRST and unconditionally; the governor-cadence gear must never take the slot, or the pause stays hidden until the gear's blocker is @@ -952,13 +985,15 @@ only lever that means anything here, so it is the only one shown. */ const toggleBtn = (canToggle && !agentIsDisabled(a)) ? (isPaused && canPauseToggle ? pausedAgentActionHtml(a) + : isPaused + ? agentGatedToggleHtml(a, true, false) : isOff ? cadenceOffAgentActionHtml(a) : isOnDemand ? `` : canPauseToggle ? `` - : '') : ''; + : agentGatedToggleHtml(a, false, false)) : ''; // liveSummary is pushed via SSE every 5s — no separate fetch needed let summaryEl = ''; if (a.liveSummary) { diff --git a/src/pkg/dashboard/static-src/js/agent-detail.js b/src/pkg/dashboard/static-src/js/agent-detail.js index 8d7260fcb2..492fe455a1 100644 --- a/src/pkg/dashboard/static-src/js/agent-detail.js +++ b/src/pkg/dashboard/static-src/js/agent-detail.js @@ -675,7 +675,7 @@ // The fingerprint guards the fields only the full template renders // (method/model/pins): without it a CLI or model switch stayed stale // in the focused panel until a hard refresh. - const cfgSig = [a.cli || '', a.model || '', a.pinnedCli ? 1 : 0, a.pinnedModel ? 1 : 0, a.pinnedBoth ? 1 : 0, a.displayName || '', a.emoji || '', a.color || '', JSON.stringify(a.subAgentModels || [])].join('|'); + const cfgSig = [a.cli || '', a.model || '', a.pinnedCli ? 1 : 0, a.pinnedModel ? 1 : 0, a.pinnedBoth ? 1 : 0, a.displayName || '', a.emoji || '', a.color || '', JSON.stringify(a.subAgentModels || []), agentToggleGateReason()].join('|'); if (existingSummary && detail.dataset.agent === a.name && detail.dataset.cfgSig === cfgSig) { if (a.detailSummary) { const newText = escBlock(a.detailSummary); @@ -695,7 +695,12 @@ // button would otherwise persist until the user navigates away. const fastToggle = detail.querySelector('.oc-detail-actions .btn-toggle'); if (fastToggle && !isOnDemand) { - if (isPaused) { + if (!isPaused && !isOff && agentToggleGateReason() !== '') { + // Below owner / role unknown: keep the disabled, explained control. + fastToggle.outerHTML = agentGatedToggleHtml(a, false, false); + } else if (isPaused && agentToggleGateReason() !== '') { + fastToggle.outerHTML = agentGatedToggleHtml(a, true, false); + } else if (isPaused) { // Paused gets the full combined-action rebuild — '▶ start & // resume' when the session is also down — so data-action tracks // the session state too, not just the label (#5594 follow-up). @@ -836,8 +841,8 @@ ? '' : isOnDemand ? '' - : !dashboardRoleAtLeast(window._hiveRole || 'read', 'owner') - ? '' + : agentToggleGateReason() !== '' && !isOff + ? agentGatedToggleHtml(a, isPaused, false) : isPaused ? pausedAgentActionHtml(a) : isOff diff --git a/src/pkg/dashboard/static-src/js/governor-view.js b/src/pkg/dashboard/static-src/js/governor-view.js index 60b3cb3e6d..681729afdf 100644 --- a/src/pkg/dashboard/static-src/js/governor-view.js +++ b/src/pkg/dashboard/static-src/js/governor-view.js @@ -456,7 +456,7 @@ const colCls = isActive ? `col-active mode-${m}` : ''; const stateCls = isContinuous ? ' continuous' : showOnDemand ? ' on-demand' : showOff ? ' off' : showPaused ? ' paused' : ''; const dot = (isActive && isWorking && !agentPaused && !showOff && !showOnDemand && !agentDisabled && !isContinuous) ? '' : ''; - const badge = isContinuous ? '∞ continuous' : showOnDemand ? 'on demand' : showOff ? 'no cadence' : showPaused ? pausedBadgeHtml : val; + const badge = isContinuous ? '∞ continuous' : showOnDemand ? 'on demand' : showOff ? 'no cadence' : showPaused ? 'paused' : val; if (agentDisabled) { return `${badge}`; } diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 16cb4fca7b..e9017820a4 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -13627,7 +13627,7 @@

Where to get help

// The fingerprint guards the fields only the full template renders // (method/model/pins): without it a CLI or model switch stayed stale // in the focused panel until a hard refresh. - const cfgSig = [a.cli || '', a.model || '', a.pinnedCli ? 1 : 0, a.pinnedModel ? 1 : 0, a.pinnedBoth ? 1 : 0, a.displayName || '', a.emoji || '', a.color || '', JSON.stringify(a.subAgentModels || [])].join('|'); + const cfgSig = [a.cli || '', a.model || '', a.pinnedCli ? 1 : 0, a.pinnedModel ? 1 : 0, a.pinnedBoth ? 1 : 0, a.displayName || '', a.emoji || '', a.color || '', JSON.stringify(a.subAgentModels || []), agentToggleGateReason()].join('|'); if (existingSummary && detail.dataset.agent === a.name && detail.dataset.cfgSig === cfgSig) { if (a.detailSummary) { const newText = escBlock(a.detailSummary); @@ -13647,7 +13647,12 @@

Where to get help

// button would otherwise persist until the user navigates away. const fastToggle = detail.querySelector('.oc-detail-actions .btn-toggle'); if (fastToggle && !isOnDemand) { - if (isPaused) { + if (!isPaused && !isOff && agentToggleGateReason() !== '') { + // Below owner / role unknown: keep the disabled, explained control. + fastToggle.outerHTML = agentGatedToggleHtml(a, false, false); + } else if (isPaused && agentToggleGateReason() !== '') { + fastToggle.outerHTML = agentGatedToggleHtml(a, true, false); + } else if (isPaused) { // Paused gets the full combined-action rebuild — '▶ start & // resume' when the session is also down — so data-action tracks // the session state too, not just the label (#5594 follow-up). @@ -13788,8 +13793,8 @@

Where to get help

? '' : isOnDemand ? '' - : !dashboardRoleAtLeast(window._hiveRole || 'read', 'owner') - ? '' + : agentToggleGateReason() !== '' && !isOff + ? agentGatedToggleHtml(a, isPaused, false) : isPaused ? pausedAgentActionHtml(a) : isOff @@ -32216,7 +32221,7 @@

All labels

function agentToggleTitle(a, isPaused, isOff) { if (isPaused) { const rest = []; - if (a.state !== 'running') rest.push('its session is down (use \u21bb restart)'); + if (a.state !== 'running' && a.sandboxed !== true) rest.push('its session is down (use \u21bb restart)'); if (a.offByCadence === true) rest.push('its cadence for the current governor mode is off (\u2699\uFE0F \u2192 Cadences)'); if (a.unscheduledInMode === true) rest.push(`it has a cadence only in ${agentCadenceModesLabel(a)}, none for the current governor mode (\u2699\uFE0F \u2192 Cadences)`); if (a.noCadence === true) rest.push('no governor mode gives it a cadence (\u2699\uFE0F \u2192 Cadences)'); @@ -32262,7 +32267,12 @@

All labels

return ``; } function pausedAgentActionHtml(a) { - if (a.state === 'running') { + /* A sandboxed agent has no long-lived session: it sits in state 'paused' + or 'idle' and runs each kick in a throwaway container, so "session + down" is never its blocker. Offering "start & resume" there chained a + restart that has nothing to restart (#18813); plain resume is the + whole action. */ + if (a.state === 'running' || a.sandboxed === true) { return ``; } const rest = []; @@ -32273,6 +32283,34 @@

All labels

if (rest.length > 0) title += ` Still not enough on its own: ${rest.join('; ')}.`; return ``; } + /* Owner-gated pause/resume slot (#18813). Never an empty slot: a viewer + below owner, or one whose role has not settled yet, sees the control + DISABLED with the reason in its tooltip. agentToggleGateReason returns + '' when the viewer may act. The role is "known" under the same rule the + Inbox uses: /api/role answered or gave up. */ + const AGENT_GATE_NEEDS_OWNER = 'Needs the owner role'; + const AGENT_GATE_CHECKING = 'Checking your access...'; + function agentRoleKnown() { return window._hiveRoleSettled === true || window._hiveRole !== undefined; } + function agentToggleGateReason() { + if (dashboardRoleAtLeast(window._hiveRole || 'read', 'owner')) return ''; + return agentRoleKnown() ? AGENT_GATE_NEEDS_OWNER : AGENT_GATE_CHECKING; + } + function agentGatedToggleHtml(a, isPaused, isOff) { + const reason = agentToggleGateReason() || AGENT_GATE_NEEDS_OWNER; + const label = isPaused + ? (a.state === 'running' || a.sandboxed === true ? '\u25b6 resume' : '\u25b6 start & resume') + : '\u23f8 pause'; + const cls = isPaused ? 'btn-danger paused' : 'btn-secondary running'; + return ``; + } + /* Repaint the role-gated agent surfaces the moment the role settles. The + cards used to be left as rendered with whatever role was known then. */ + function agentsOnRoleReady() { + if (window._lastAgents) renderAgents(window._lastAgents); + if (typeof ocRenderAgentDetail === 'function') ocRenderAgentDetail(); + if (typeof renderGovernorFromLastStatus === 'function') renderGovernorFromLastStatus(); + } + (window._hiveRoleListeners = window._hiveRoleListeners || []).push(agentsOnRoleReady); function neverScheduledChipHtml(a, configTarget) { if (a.noCadence !== true) return ''; const who = a.displayName || a.name; @@ -32434,7 +32472,7 @@

All labels

// Healthy-but-governor-paused → hollow green dot (see detail-header renderer). const dotState = isOff ? 'off-healthy' : dotCls; const canToggle = a.beadRole !== 'supervisor'; - const canPauseToggle = canToggle && dashboardRoleAtLeast(window._hiveRole || 'read', 'owner'); + const canPauseToggle = canToggle && agentToggleGateReason() === ''; /* RESUME LEADS (#5594). A paused agent's card offers its pause toggle FIRST and unconditionally; the governor-cadence gear must never take the slot, or the pause stays hidden until the gear's blocker is @@ -32451,13 +32489,15 @@

All labels

only lever that means anything here, so it is the only one shown. */ const toggleBtn = (canToggle && !agentIsDisabled(a)) ? (isPaused && canPauseToggle ? pausedAgentActionHtml(a) + : isPaused + ? agentGatedToggleHtml(a, true, false) : isOff ? cadenceOffAgentActionHtml(a) : isOnDemand ? `` : canPauseToggle ? `` - : '') : ''; + : agentGatedToggleHtml(a, false, false)) : ''; // liveSummary is pushed via SSE every 5s — no separate fetch needed let summaryEl = ''; if (a.liveSummary) { @@ -34062,7 +34102,7 @@

All labels

const colCls = isActive ? `col-active mode-${m}` : ''; const stateCls = isContinuous ? ' continuous' : showOnDemand ? ' on-demand' : showOff ? ' off' : showPaused ? ' paused' : ''; const dot = (isActive && isWorking && !agentPaused && !showOff && !showOnDemand && !agentDisabled && !isContinuous) ? '' : ''; - const badge = isContinuous ? '∞ continuous' : showOnDemand ? 'on demand' : showOff ? 'no cadence' : showPaused ? pausedBadgeHtml : val; + const badge = isContinuous ? '∞ continuous' : showOnDemand ? 'on demand' : showOff ? 'no cadence' : showPaused ? 'paused' : val; if (agentDisabled) { return `${badge}`; } diff --git a/src/pkg/dashboard/subagent_models_test.go b/src/pkg/dashboard/subagent_models_test.go index ef359940eb..d9c279fa0f 100644 --- a/src/pkg/dashboard/subagent_models_test.go +++ b/src/pkg/dashboard/subagent_models_test.go @@ -49,7 +49,7 @@ func TestSubAgentModelsDetailWiring(t *testing.T) { "row.olderGeneration ?", "⚠ older generation", "${subAgentModelsHtml(a)}", - "JSON.stringify(a.subAgentModels || [])].join('|')", + "JSON.stringify(a.subAgentModels || []), agentToggleGateReason()].join('|')", } { if !strings.Contains(html, snippet) { t.Errorf("missing detail rendering/refresh guard: %s", snippet)