sideby 0.21.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/README.md +1 -0
  2. package/dist/build.json +1 -1
  3. package/dist/src/cli/commands.js +115 -10
  4. package/dist/src/cli/format.js +3 -2
  5. package/dist/src/cli/json-schemas.js +81 -46
  6. package/dist/src/cli/main.js +6 -3
  7. package/dist/src/core/accounts.d.ts +6 -1
  8. package/dist/src/core/accounts.js +16 -3
  9. package/dist/src/core/ask.d.ts +2 -1
  10. package/dist/src/core/ask.js +6 -3
  11. package/dist/src/core/claude-settings-file.d.ts +57 -0
  12. package/dist/src/core/claude-settings-file.js +104 -2
  13. package/dist/src/core/config.d.ts +29 -15
  14. package/dist/src/core/config.js +40 -2
  15. package/dist/src/core/create.js +4 -1
  16. package/dist/src/core/desktop-app.js +5 -1
  17. package/dist/src/core/handoff-overview.d.ts +4 -1
  18. package/dist/src/core/handoff-overview.js +3 -2
  19. package/dist/src/core/handoff-rules.d.ts +34 -0
  20. package/dist/src/core/handoff-rules.js +115 -0
  21. package/dist/src/core/handoff-select.js +45 -28
  22. package/dist/src/core/handoff-status.d.ts +37 -7
  23. package/dist/src/core/handoff-status.js +95 -19
  24. package/dist/src/core/handoff.d.ts +4 -1
  25. package/dist/src/core/handoff.js +8 -5
  26. package/dist/src/core/inbound.d.ts +11 -3
  27. package/dist/src/core/inbound.js +26 -74
  28. package/dist/src/core/launch.js +6 -4
  29. package/dist/src/core/quota-levels.d.ts +6 -0
  30. package/dist/src/core/quota-levels.js +8 -0
  31. package/dist/src/core/remote-control.d.ts +10 -12
  32. package/dist/src/core/remote-control.js +29 -86
  33. package/dist/src/core/tuning-apply.js +12 -20
  34. package/dist/src/families/agy/index.d.ts +8 -0
  35. package/dist/src/families/agy/index.js +53 -0
  36. package/dist/src/families/agy/quota.d.ts +9 -0
  37. package/dist/src/families/agy/quota.js +110 -0
  38. package/dist/src/families/index.js +7 -0
  39. package/dist/src/families/logos.d.ts +10 -0
  40. package/dist/src/families/logos.js +12 -1
  41. package/dist/src/menubar/script.js +26 -3
  42. package/dist/src/menubar/snapshot.d.ts +23 -6
  43. package/dist/src/menubar/snapshot.js +126 -39
  44. package/dist/src/menubar/strings.d.ts +12 -2
  45. package/dist/src/menubar/strings.js +12 -2
  46. package/dist/src/panel/handler.d.ts +2 -1
  47. package/dist/src/panel/handler.js +81 -45
  48. package/dist/src/panel/i18n.d.ts +57 -7
  49. package/dist/src/panel/i18n.js +156 -56
  50. package/dist/src/panel/page-logic.d.ts +47 -19
  51. package/dist/src/panel/page-logic.js +33 -7
  52. package/dist/src/panel/page-script.js +263 -94
  53. package/dist/src/panel/page-styles.js +105 -8
  54. package/dist/src/panel/page.js +3 -3
  55. package/dist/src/plugins/check.js +27 -13
  56. package/dist/src/plugins/loader.d.ts +11 -0
  57. package/dist/src/plugins/loader.js +47 -1
  58. package/dist/src/runtime.d.ts +5 -2
  59. package/dist/src/runtime.js +12 -5
  60. package/dist/src/types.d.ts +35 -5
  61. package/package.json +2 -2
  62. package/schemas/config.json +16 -1
  63. package/schemas/families.json +8 -5
  64. package/schemas/handoff-disable.json +22 -0
  65. package/schemas/handoff-enable.json +22 -0
  66. package/schemas/handoff-rules.json +302 -0
  67. package/schemas/handoff-status.json +26 -0
  68. package/schemas/list.json +5 -0
  69. package/schemas/menubar-snapshot.json +309 -2
  70. package/skills/sideby/SKILL.md +3 -2
  71. package/skills/sideby/references/accounts.md +1 -0
  72. package/skills/sideby/references/handoff.md +7 -3
  73. package/skills/sideby/references/quota.md +2 -0
@@ -1,7 +1,9 @@
1
1
  // The Panel page's inline script. Plain browser JavaScript in a string (no template literals inside, so String.raw
2
2
  // keeps it verbatim); the pure helpers from page-logic.ts are prepended so the tested code is the code that runs.
3
+ import { groupsToAccept } from '../core/inbound.js';
3
4
  import { quotaIsStale } from '../core/quota-levels.js';
4
- import { aiTaskAccounts, aliasArgsOf, aliasIssue, askBridge, askBridged, askCopy, attentionReasons, attentionWhy, autostartNote, cacheHitRate, canLeaveLocal, categoryOrder, codeSwapOutcome, fileSize, fixPrompt, foldSkips, foldToolCalls, formatDuration, handoffChangeOf, handoffHintAccount, handoffTaskFacts, highlightParts, histFilters, historyEmpty, historyWhen, hotkeyFromKey, hotkeyPressKind, inDisplayOrder, loginClusters, loginKey, maskEmail, matchesQuery, mdBlocks, mdInline, menuPlacement, mergeDaily, mergeUsage, messageChip, messageNews, newAccountDir, nextReset, panelFixes, panelHint, panelLang, panelMigration, panelPrefs, passedWindow, pinnedIn, quotaAskable, quotaPressure, remoteControlSummary, remoteControlTodo, reorder, rowBadge, runCodeSwap, sendBlock, sendFileProblem, sendResultView, sessionAge, sessionLabelParts, shortCwd, shortSessionLabel, sortAccounts, sortSessions, steadyOrder, stripSecrets, suggestAlias, suggestName, switchFailure, tailInView, taskFindings, updatedAge, widthFollower, widthTier, windowState, withPinned, } from './page-logic.js';
5
+ import { remoteControlTodo } from '../core/remote-control.js';
6
+ import { aiTaskAccounts, aliasArgsOf, aliasIssue, askBridge, askBridged, askCopy, attentionReasons, attentionWhy, autostartNote, bindingWindow, cacheHitRate, canLeaveLocal, categoryOrder, codeSwapOutcome, enableOutcome, fileSize, fixPrompt, foldSkips, foldToolCalls, formatDuration, handoffChangeOf, handoffHintAccount, handoffTaskFacts, highlightParts, histFilters, historyEmpty, historyWhen, hotkeyFromKey, hotkeyPressKind, inDisplayOrder, loginClusters, loginKey, maskEmail, matchesQuery, mdBlocks, mdInline, menuPlacement, mergeDaily, mergeUsage, messageChip, messageNews, newAccountDir, nextReset, panelFixes, panelHint, panelLang, panelMigration, panelPrefs, passedWindow, pinnedIn, quotaAskable, quotaPressure, remoteControlSummary, reorder, rowBadge, runCodeSwap, saveHandoffChange, sendBlock, sendFileProblem, sendResultView, sessionAge, sessionLabelParts, shortCwd, shortSessionLabel, sortAccounts, sortSessions, steadyOrder, stripSecrets, suggestAlias, suggestName, switchFailure, tailInView, taskFindings, updatedAge, widthFollower, widthTier, windowState, withPinned, } from './page-logic.js';
5
7
  const LOGIC = [
6
8
  quotaIsStale,
7
9
  aiTaskAccounts,
@@ -56,6 +58,9 @@ const LOGIC = [
56
58
  quotaPressure,
57
59
  remoteControlSummary,
58
60
  remoteControlTodo,
61
+ groupsToAccept,
62
+ saveHandoffChange,
63
+ enableOutcome,
59
64
  reorder,
60
65
  rowBadge,
61
66
  sendBlock,
@@ -79,6 +84,7 @@ const LOGIC = [
79
84
  widthFollower,
80
85
  widthTier,
81
86
  windowState,
87
+ bindingWindow,
82
88
  withPinned,
83
89
  ]
84
90
  .map((fn) => fn.toString())
@@ -380,6 +386,9 @@ const MAIN = String.raw `
380
386
  grid: 'M4 4h7v7H4zM13 4h7v7h-7zM4 13h7v7H4zM13 13h7v7h-7z',
381
387
  list: 'M9 6h11M9 12h11M9 18h11M4 6h.01M4 12h.01M4 18h.01',
382
388
  sliders: 'M4 7h10M18 7h2M4 17h4M12 17h8M16 5v4M10 15v4',
389
+ // Lucide "users" and "message-square" (ISC license)
390
+ users: 'M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2M9 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM22 21v-2a4 4 0 0 0-3-3.87M16 3.13a4 4 0 0 1 0 7.75',
391
+ messages: 'M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z',
383
392
  clock: 'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18zM12 7v5l3 2',
384
393
  mail: 'M3 6h18v12H3zM3 7l9 6 9-6',
385
394
  globe: 'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18zM3 12h18M12 3c2.5 2.5 3.8 5.5 3.8 9s-1.3 6.5-3.8 9c-2.5-2.5-3.8-5.5-3.8-9S9.5 5.5 12 3z',
@@ -592,6 +601,7 @@ const MAIN = String.raw `
592
601
  function famTitle(a) { return a.familyTitle || family(a.family).title }
593
602
  // The email as the page shows it: masked everywhere while "Hide emails" is on (copying still gives the real one).
594
603
  function emailText(email) { return prefs.hideEmail ? maskEmail(email) : email }
604
+ let logoWashes = 0
595
605
  // A Family's mark: the logo its FamilyDef sets (checked by the loader), or its initials on a color derived from the id.
596
606
  // size: '' (cards), 'sm' (rows, groups, dialogs) or 'xs' (inside a chip; no initials fallback there).
597
607
  function badge(id, size) {
@@ -601,10 +611,36 @@ const MAIN = String.raw `
601
611
  const svg = document.createElementNS(SVG_NS, 'svg')
602
612
  svg.setAttribute('viewBox', '0 0 24 24')
603
613
  svg.setAttribute('focusable', 'false')
604
- const path = document.createElementNS(SVG_NS, 'path')
605
- path.setAttribute('d', logo.path)
606
- if (logo.fillRule) path.setAttribute('fill-rule', logo.fillRule)
607
- svg.append(path)
614
+ const shape = (fill) => {
615
+ const path = document.createElementNS(SVG_NS, 'path')
616
+ path.setAttribute('d', logo.path)
617
+ if (logo.fillRule) path.setAttribute('fill-rule', logo.fillRule)
618
+ if (fill) path.setAttribute('fill', fill)
619
+ return path
620
+ }
621
+ svg.append(shape(null))
622
+ // A multicoloured mark: each wash is the shape again, filled with a gradient that fades out (ids unique per page).
623
+ if (Array.isArray(logo.gradients) && logo.gradients.length) {
624
+ const defs = document.createElementNS(SVG_NS, 'defs')
625
+ svg.prepend(defs)
626
+ for (const g of logo.gradients) {
627
+ const id = 'logo-wash-' + (++logoWashes)
628
+ const lg = document.createElementNS(SVG_NS, 'linearGradient')
629
+ lg.setAttribute('id', id)
630
+ lg.setAttribute('gradientUnits', 'userSpaceOnUse')
631
+ lg.setAttribute('x1', String(g.from[0])); lg.setAttribute('y1', String(g.from[1]))
632
+ lg.setAttribute('x2', String(g.to[0])); lg.setAttribute('y2', String(g.to[1]))
633
+ for (const [offset, opacity] of [['0', '1'], ['1', '0']]) {
634
+ const stop = document.createElementNS(SVG_NS, 'stop')
635
+ stop.setAttribute('offset', offset)
636
+ stop.setAttribute('stop-color', g.color)
637
+ stop.setAttribute('stop-opacity', opacity)
638
+ lg.append(stop)
639
+ }
640
+ defs.append(lg)
641
+ svg.append(shape('url(#' + id + ')'))
642
+ }
643
+ }
608
644
  return h('span', { class: cls + ' badge-logo', css: logo.color ? { color: logo.color } : null, title: logo.title || family(id).title, 'aria-hidden': 'true' }, svg)
609
645
  }
610
646
  if (size === 'xs') return null
@@ -918,7 +954,8 @@ const MAIN = String.raw `
918
954
  a.identity && a.identity.email ? { icon: 'mail', label: t('menu.copyEmail'), hint: emailText(a.identity.email), run: () => copyText(a.identity.email) } : null,
919
955
  ]
920
956
  }
921
- function likeItem(a) { return BOOT.readOnly ? null : { icon: 'userPlus', label: t('menu.like'), run: () => openCreate({ family: a.family, api: a.kind === 'api' }) } }
957
+ // A Main-only Family (ADR-0047) has no other Account to create.
958
+ function likeItem(a) { return BOOT.readOnly || family(a.family).mainOnly ? null : { icon: 'userPlus', label: t('menu.like'), run: () => openCreate({ family: a.family, api: a.kind === 'api' }) } }
922
959
  // Grouped by what the user wants to do: open, copy, manage. A Same Login row (ADR-0026) passes its other
923
960
  // directories: the menu is the one of the directory the row is named after, and each other directory is one item
924
961
  // that opens its own actions in the same menu, so the shared actions appear once.
@@ -1245,18 +1282,20 @@ const MAIN = String.raw `
1245
1282
  }
1246
1283
  // A window's cell: percent and countdown on one line, the bar under them. The label shows where no column
1247
1284
  // header does (cards, narrow screens).
1248
- function meter(w, now, stale, observedAt) {
1285
+ function meter(w, now, stale, observedAt, withLength) {
1249
1286
  const st = windowState(w, now, WARN, FAIL)
1250
1287
  const passed = passedWindow(w, observedAt, now)
1251
1288
  const left = prefs.quota === 'left'
1252
1289
  const shown = left ? 100 - st.pct : st.pct
1253
1290
  const asOf = stale ? t('quota.asOf', { ago: ago(observedAt) }) : ''
1254
1291
  const reset = passed ? t('quota.resetShort') : t('quota.resetsIn', { d: formatDuration(Date.parse(w.resetsAt) - now, lang) })
1255
- const title = [passed ? t('quota.resetNoData', { date: day(passed.since) }) : t('quota.resetsAt', { at: clock(w.resetsAt) }), asOf].filter(Boolean).join(' · ')
1292
+ // The wide list's column says only the model group, so the hover names which of its limits this is.
1293
+ const title = [withLength ? winTitle(w.label) : '', passed ? t('quota.resetNoData', { date: day(passed.since) }) : t('quota.resetsAt', { at: clock(w.resetsAt) }), asOf].filter(Boolean).join(' · ')
1256
1294
  return h('div', { class: 'meter lvl-' + st.level + (stale || passed ? ' lvl-stale' : ''), title },
1257
1295
  h('div', { class: 'm-top' },
1258
1296
  // Cards show the short window name ("5 小时"); the list and its column titles keep the full one.
1259
- h('span', { class: 'm-label' }, h('span', { class: 'm-full' }, winTitle(winKey(w))), h('span', { class: 'm-short' }, winShort(winKey(w)))),
1297
+ // withLength: the group has other limits too, so name which one this is ("Gemini Models · 5-hour").
1298
+ h('span', { class: 'm-label' }, h('span', { class: 'm-full' }, winTitle(winKey(w)) + (withLength ? ' · ' + winTitle(w.label) : '')), h('span', { class: 'm-short' }, winShort(winKey(w)) + (withLength ? ' · ' + winShort(w.label) : ''))),
1260
1299
  h('span', { class: 'm-pct' }, shown + '%', h('span', { class: 'm-unit' }, ' ' + t(left ? 'quota.unitLeft' : 'quota.unitUsed')),
1261
1300
  null),
1262
1301
  h('span', { class: 'm-reset' + (passed ? ' m-passed' : '') }, Number.isFinite(Date.parse(w.resetsAt)) || passed ? reset : '?')),
@@ -1307,8 +1346,10 @@ const MAIN = String.raw `
1307
1346
  // widest Family's, for the wide list's shared columns).
1308
1347
  return h('div', { class: 'quota' + (stale ? ' is-stale' : ''), title, css: wins.length ? { '--qr': String(cols.length) } : null },
1309
1348
  wins.length ? cols.map((c) => {
1310
- const w = wins.find((x) => winKey(x) === c)
1311
- return w ? meter(w, now, stale, q.observedAt) : noMeter(winTitle(c), winShort(c))
1349
+ // A model group with several limits (Antigravity's 5-hour and weekly) shows the one that binds first.
1350
+ const same = wins.filter((x) => winKey(x) === c)
1351
+ const w = bindingWindow(same, now, WARN, FAIL)
1352
+ return w ? meter(w, now, stale, q.observedAt, same.length > 1) : noMeter(winTitle(c), winShort(c))
1312
1353
  }) : h('div', { class: 'q-note' }, t('quota.noWindows')),
1313
1354
  // Old numbers keep their colours and say their age once, under the bars.
1314
1355
  stale && wins.length ? h('div', { class: 'q-asof', title: failed || t('quota.asOf', { ago: ago(q.observedAt) }) }, icon('clock'), q.fetchError ? t('quota.fetchFailed') + ' · ' : '', t('quota.staleShort', { ago: agoShort(now - Date.parse(q.observedAt)) })) : null)
@@ -2775,11 +2816,14 @@ const MAIN = String.raw `
2775
2816
  const mine = useUi && useUi.gid === g.id
2776
2817
  const busy = mine && (useUi.step === 'asking' || useUi.step === 'busy')
2777
2818
  const asking = mine && (useUi.step === 'confirm' || busy)
2778
- const rows = [h('p', { class: 'small' }, g.members.length > 1 ? t('tune.groupShared', { list: g.members.join(', ') }) : h('b', { class: 'mono' }, g.members[0]))]
2779
- rows.push(h('div', { class: 'tune-vals' },
2780
- h('div', null, h('span', { class: 'muted small' }, t('tune.now')), h('div', { class: 'tune-num' }, g.current === null ? defaultSize(claudeModels(entries)) : tuneSize(g.current))),
2781
- pick ? h('div', null, h('span', { class: 'muted small' }, t('tune.best')), h('div', { class: 'tune-num tune-best' }, tokens(pick.threshold))) : null))
2782
- if (pick && pick.savedPercent !== null) rows.push(h('p', { class: 'small' }, t('tune.saved', { n: Math.round(pick.savedPercent) })))
2819
+ const rows = [h('div', { class: 'tg-head' }, badge(entries[0] ? entries[0].family : 'claude', 'sm'),
2820
+ h('p', { class: 'small' }, g.members.length > 1 ? t('tune.groupShared', { list: g.members.join(', ') }) : h('b', { class: 'mono' }, g.members[0])))]
2821
+ // Now, the suggestion and what it saves read as one strip, like the Accounts summary.
2822
+ const saved = pick && pick.savedPercent !== null ? Math.round(pick.savedPercent) : null
2823
+ rows.push(h('div', { class: 'tg-stats' },
2824
+ h('div', { class: 'tg-stat' }, h('span', { class: 'stat-label' }, t('tune.now')), h('div', { class: 'tune-num' }, g.current === null ? defaultSize(claudeModels(entries)) : tuneSize(g.current))),
2825
+ pick ? h('div', { class: 'tg-stat' }, h('span', { class: 'stat-label' }, t('tune.best')), h('div', { class: 'tune-num tune-best' }, tokens(pick.threshold))) : null,
2826
+ saved !== null ? h('div', { class: 'tg-stat', title: t('tune.saved', { n: saved }) }, h('span', { class: 'stat-label' }, t('tune.savedLabel')), h('div', { class: 'tune-num' }, t('tune.savedValue', { n: saved }))) : null))
2783
2827
  if (pick) rows.push(h('p', { class: 'muted small' }, t('tune.basis', { model: pick.model, ref: pick.ref })))
2784
2828
  if (pick) {
2785
2829
  const seen = entries.find((e) => e.ref === pick.ref)
@@ -2914,9 +2958,21 @@ const MAIN = String.raw `
2914
2958
  if (canTune) tabs.push(['tuning', t('page.tuning')])
2915
2959
  if (canHandoff) tabs.push(['handoff', t('page.handoff')])
2916
2960
  nav.hidden = tabs.length < 2
2917
- setKids(nav, h('div', { class: 'seg', role: 'presentation' }, tabs.map(([id, label]) =>
2918
- h('button', { class: 'seg-btn', type: 'button', role: 'tab', 'aria-selected': curPage === id ? 'true' : 'false', 'aria-busy': id === 'sessions' && sp.opening ? 'true' : null, onclick: () => setPage(id) }, id === 'sessions' && sp.opening ? [spinner(), ' '] : null, label,
2919
- id === 'sessions' && pasteCount() ? h('span', { class: 'seg-badge', 'data-tip': t('msgn.badge', { n: pasteCount() }), 'aria-label': t('msgn.badge', { n: pasteCount() }) }, String(pasteCount())) : null))))
2961
+ // Underline tabs: each page has its mark, the Accounts tab its count and Auto Handoff a dot while it is on.
2962
+ const tabIcon = { accounts: 'users', sessions: 'messages', tuning: 'sliders', handoff: 'swap' }
2963
+ const nAccounts = S.state ? S.state.accounts.length : 0
2964
+ const handoffOn = Boolean(ahState() && ahState().settings.auto)
2965
+ // Redrawn only when something on it changes, so a refresh never replays the underline.
2966
+ const navShape = JSON.stringify([lang, curPage, tabs, nAccounts, handoffOn, sp.opening, pasteCount()])
2967
+ if (nav._drawn !== navShape) {
2968
+ nav._drawn = navShape
2969
+ swapKids(nav, [h('div', { class: 'tabs', role: 'presentation' }, tabs.map(([id, label]) =>
2970
+ h('button', { class: 'tab', type: 'button', role: 'tab', 'data-key': 'tab:' + id, 'aria-selected': curPage === id ? 'true' : 'false', 'aria-busy': id === 'sessions' && sp.opening ? 'true' : null, onclick: () => setPage(id) },
2971
+ id === 'sessions' && sp.opening ? spinner() : icon(tabIcon[id]), h('span', { class: 'tab-label', 'data-label': label }, label),
2972
+ id === 'accounts' && nAccounts ? h('span', { class: 'tab-count' }, String(nAccounts)) : null,
2973
+ id === 'handoff' && handoffOn ? h('span', { class: 'tab-dot', 'aria-label': t('ah.on') }) : null,
2974
+ id === 'sessions' && pasteCount() ? h('span', { class: 'seg-badge', 'data-tip': t('msgn.badge', { n: pasteCount() }), 'aria-label': t('msgn.badge', { n: pasteCount() }) }, String(pasteCount())) : null)))])
2975
+ }
2920
2976
  const away = curPage !== 'accounts' && tabs.some(([id]) => id === curPage)
2921
2977
  // With no account yet the empty state is the whole page: no summary, no health check.
2922
2978
  const none = Boolean(S.state && !S.state.accounts.length)
@@ -3026,7 +3082,7 @@ const MAIN = String.raw `
3026
3082
  return ib.loading
3027
3083
  }
3028
3084
  function inboundGroups() { return (ib.data && ib.data.groups) || [] }
3029
- function inboundTodo() { return inboundGroups().filter((g) => g.state === 'default' || g.state === 'hold') }
3085
+ function inboundTodo() { return groupsToAccept(inboundGroups()) }
3030
3086
  function inboundWaiting() { return inboundGroups().filter((g) => g.waits).flatMap((g) => g.accounts).map(shortRef) }
3031
3087
  function inboundShape() { return inboundGroups().length > 0 }
3032
3088
  const refList = (refs) => refs.join(lang === 'zh' ? '、' : ', ')
@@ -3093,8 +3149,8 @@ const MAIN = String.raw `
3093
3149
  return h('li', { class: 'rc-row' },
3094
3150
  h('div', null, h('code', null, g.file), ' ', h('span', { class: 'chip chip-sm' + (tone ? ' chip-' + tone : '') }, t(key))),
3095
3151
  h('div', { class: 'muted small' }, t('rc.usedBy', { refs: refList(g.accounts.map(shortRef)) })),
3096
- g.reason ? h('div', { class: 'muted small' }, g.reason) : null,
3097
- !BOOT.readOnly && todo.includes(g) ? h('div', { class: 'small rc-change' }, t('inb.change', { from: g.state === 'hold' ? '"hold"' : t('inb.notSet') })) : null)
3152
+ g.reason ? h('div', { class: 'muted small' }, g.why ? t('settings.why.' + g.why) : g.reason) : null,
3153
+ !BOOT.readOnly && todo.includes(g) ? h('div', { class: 'small rc-change' }, keyChange('crossSessionInbound', g.state === 'hold' ? '"hold"' : null, '"accept"')) : null)
3098
3154
  })))
3099
3155
  if (BOOT.readOnly) body.push(h('p', { class: 'small' }, t('inb.readOnly')))
3100
3156
  else if (todo.length) body.push(h('p', { class: 'small muted' }, t('inb.confirmNote')))
@@ -3112,8 +3168,9 @@ const MAIN = String.raw `
3112
3168
  async function inboundAccept() {
3113
3169
  inboundBusy(true)
3114
3170
  try {
3115
- const r = await api('/api/messages/inbound/accept', {})
3116
- if (r.ok) {
3171
+ const r = await api('/api/messages/inbound/accept', { confirm: true, files: inboundTodo().map((g) => g.file) })
3172
+ if (r.changedSince) toast(t('as.changedSince'), { kind: 'fail' })
3173
+ else if (r.ok) {
3117
3174
  if (dlg.kind === 'inbound' && dlg.el.open) dlg.el.close()
3118
3175
  toast(t('inb.done'), { kind: 'ok', key: 'inb', action: { label: t('toast.undo'), run: inboundUndo }, duration: 12000 })
3119
3176
  } else toast(t('inb.failed', { msg: r.message }), { kind: 'fail' })
@@ -3124,7 +3181,7 @@ const MAIN = String.raw `
3124
3181
  async function inboundUndo() {
3125
3182
  inboundBusy(true)
3126
3183
  try {
3127
- const r = await api('/api/messages/inbound/undo', {})
3184
+ const r = await api('/api/messages/inbound/undo', { confirm: true })
3128
3185
  if (r.ok && dlg.kind === 'inbound' && dlg.el.open) dlg.el.close()
3129
3186
  toast(r.ok ? t('inb.undone') : r.message, { kind: r.ok ? 'ok' : 'fail', key: 'inb' })
3130
3187
  } catch (e) { toast(t('toast.undoFailed', { msg: e.message }), { kind: 'fail' }) }
@@ -4065,7 +4122,7 @@ const MAIN = String.raw `
4065
4122
  open.addEventListener('click', () => openHistoryView(s, open))
4066
4123
  const more = h('button', { class: 'icon-btn more', type: 'button', 'aria-haspopup': 'menu', 'aria-expanded': 'false', 'data-key': 'hist-more-' + k, 'aria-label': t('hist.more', { title }), 'data-tip': t('menu.title') }, icon('more'))
4067
4124
  more.addEventListener('click', () => historyMenu(more, s))
4068
- const view = h('button', { class: 'btn btn-sm hist-view', type: 'button', 'data-key': 'hist-view-' + k, 'aria-label': t('hist.openLabel', { title }) }, icon('eye'), h('span', null, t('hist.viewShort')))
4125
+ const view = h('button', { class: 'icon-btn hist-view', type: 'button', 'data-key': 'hist-view-' + k, 'aria-label': t('hist.openLabel', { title }), 'data-tip': t('hist.viewShort') }, icon('eye'))
4069
4126
  view.addEventListener('click', () => openHistoryView(s, view))
4070
4127
  const cont = h('button', { class: 'btn btn-sm hist-cont', type: 'button', 'data-key': 'hist-cont-' + k, 'aria-label': t('hist.continueFor', { title }), 'data-tip': t(s.cwdGone ? 'hist.continueGoneTip' : 'hist.continueTip') }, icon('terminal'), h('span', null, t('hist.continueShort')))
4071
4128
  cont.addEventListener('click', () => copyContinue(cont, s))
@@ -4536,6 +4593,7 @@ const MAIN = String.raw `
4536
4593
  // New account. The form is built once per opening so typing keeps focus; only its derived parts re-render.
4537
4594
  let nf = null
4538
4595
  function familyStatus(f) {
4596
+ if (f.mainOnly) return 'main-only'
4539
4597
  const hasMain = S.state && S.state.accounts.some((a) => a.family === f.id && a.isMain)
4540
4598
  if (hasMain) return 'ready'
4541
4599
  return f.installed ? 'no-main' : 'missing'
@@ -4543,7 +4601,7 @@ const MAIN = String.raw `
4543
4601
  function openCreate(prefill) {
4544
4602
  if (BOOT.readOnly || !S.state) return
4545
4603
  const fams = S.state.families.slice().sort((x, y) => {
4546
- const rank = (f) => ({ ready: 0, 'no-main': 1, missing: 2 })[familyStatus(f)]
4604
+ const rank = (f) => ({ ready: 0, 'no-main': 1, missing: 2, 'main-only': 3 })[familyStatus(f)]
4547
4605
  return rank(x) - rank(y)
4548
4606
  })
4549
4607
  const ready = fams.filter((f) => familyStatus(f) === 'ready')
@@ -4623,7 +4681,7 @@ const MAIN = String.raw `
4623
4681
  renderPreview()
4624
4682
  })
4625
4683
  const n = S.state.accounts.filter((a) => a.family === f.id).length
4626
- const sub = st === 'ready' ? tn('nf.accounts', n) : st === 'no-main' ? tx('nf.runOnce', { bin: h('code', null, f.bin) }) : t(f.brokenBin ? 'nf.broken' : 'nf.notInstalled')
4684
+ const sub = st === 'ready' ? tn('nf.accounts', n) : st === 'main-only' ? t('nf.mainOnly') : st === 'no-main' ? tx('nf.runOnce', { bin: h('code', null, f.bin) }) : t(f.brokenBin ? 'nf.broken' : 'nf.notInstalled')
4627
4685
  return h('label', { class: 'fam-card' + (st === 'ready' ? '' : ' is-off') }, input, badge(f.id, 'sm'),
4628
4686
  h('span', { class: 'fam-text' }, h('span', { class: 'fam-title' }, f.title), h('span', { class: 'fam-sub' }, sub)))
4629
4687
  })
@@ -4855,7 +4913,7 @@ const MAIN = String.raw `
4855
4913
  const cur = s.families[f]
4856
4914
  orders[f] = { policy: cur && cur.policy === 'order' ? 'order' : 'pressure', order: cur ? cur.order.slice() : [] }
4857
4915
  }
4858
- return { auto: s.auto, sameFamily: s.sameFamily, prepareAt: s.prepareAt, threshold: s.threshold, wait: s.waitIfResetWithinMinutes, countdown: s.countdownSeconds, orders }
4916
+ return { auto: s.auto, sameFamily: s.sameFamily, prepareAt: s.prepareAt, threshold: s.threshold, wait: s.waitIfResetWithinMinutes, countdown: s.countdownSeconds, orders, crossOrganization: s.crossOrganization.slice() }
4859
4917
  }
4860
4918
  // "Let AI tune it" (spec §3.29): the AI gets the facts of sideby handoff status --json, as far as the page has them.
4861
4919
  function askHandoff() {
@@ -4864,11 +4922,14 @@ const MAIN = String.raw `
4864
4922
  const status = ah && ah.status ? ah.status : null
4865
4923
  openAsk({ task: 'handoff', context: { handoff: handoffTaskFacts(st.settings, st.starters, S.state.accounts, Date.now(), status), version: BOOT.version } })
4866
4924
  }
4867
- async function openHandoff() {
4925
+ // focus: the field a rule's "Change…" is about, so the dialog opens at it.
4926
+ async function openHandoff(focus) {
4868
4927
  if (!ahState()) return
4869
4928
  ah = { form: handoffForm(), loading: true }
4870
4929
  showDialog('handoff')
4871
4930
  renderHandoff()
4931
+ const field = typeof focus === 'string' ? document.getElementById('ah-' + focus) : null
4932
+ if (field) field.focus()
4872
4933
  await loadHandoffStatus()
4873
4934
  }
4874
4935
  async function loadHandoffStatus() {
@@ -4932,6 +4993,24 @@ const MAIN = String.raw `
4932
4993
  } else kids.push(h('p', { class: 'muted small' }, t('ah.byQuotaHint')))
4933
4994
  return h('div', { class: 'ah-fam' }, kids)
4934
4995
  }
4996
+ // Accounts that may take over although signed in to another organization (config handoff.crossOrganization).
4997
+ function ahCrossOrganization() {
4998
+ const list = ah.form.crossOrganization
4999
+ const accounts = S.state.accounts.filter((a) => a.kind !== 'api')
5000
+ if (!accounts.length) return null
5001
+ return h('details', { class: 'ah-fam', open: list.length > 0 },
5002
+ h('summary', null, h('b', null, t('ah.crossOrg'))),
5003
+ h('p', { class: 'muted small' }, t('ah.crossOrgHint')),
5004
+ h('div', { class: 'ah-cross' }, accounts.map((a) => {
5005
+ const input = h('input', { type: 'checkbox', disabled: BOOT.readOnly })
5006
+ input.checked = list.includes(a.ref) || (a.aliases || []).some((n) => list.includes(n))
5007
+ input.addEventListener('change', () => ahEdit((f) => {
5008
+ f.crossOrganization = f.crossOrganization.filter((r) => r !== a.ref && !(a.aliases || []).includes(r))
5009
+ if (input.checked) f.crossOrganization.push(a.ref)
5010
+ }))
5011
+ return h('label', { class: 'check' }, input, h('span', { class: 'title-logo' }, badge(a.family, 'sm'), h('span', { class: 'mono' }, accountLabel(a.ref))))
5012
+ })))
5013
+ }
4935
5014
  function ahReadiness() {
4936
5015
  if (ah.loading) return h('p', { class: 'muted' }, spinner(), ' ', t('ah.checking'))
4937
5016
  if (ah.error) return errorNote(ah.error, loadHandoffStatus)
@@ -4964,6 +5043,7 @@ const MAIN = String.raw `
4964
5043
  ahNumber('countdown', t('ah.countdown'), 0, 60)))
4965
5044
  if (f.prepareAt >= f.threshold) body.push(h('p', { class: 'note note-warn' }, t('ah.badLevels')))
4966
5045
  for (const fam of st.starters) body.push(ahFamily(fam))
5046
+ body.push(ahCrossOrganization())
4967
5047
  // The result of Review or Apply sits right under the form, where the button that made it is.
4968
5048
  if (ah.applyError) body.push(errorNote(ah.applyError, null))
4969
5049
  if (ah.saved) body.push(h('p', { class: 'note' }, t('ah.saved', { file: ah.saved })))
@@ -5003,6 +5083,8 @@ const MAIN = String.raw `
5003
5083
  ah.preview = null
5004
5084
  toast(r.settings.auto ? t('toast.ahOn') : t('toast.ahOff'))
5005
5085
  await load({ silent: true })
5086
+ // The Handoff Rules behind the dialog show the new settings too.
5087
+ if (curPage === 'handoff') loadHandoffPage()
5006
5088
  if (ahState()) ah.form = handoffForm()
5007
5089
  ah.loading = true
5008
5090
  ah.status = null
@@ -5138,7 +5220,7 @@ const MAIN = String.raw `
5138
5220
  // ---------- the Auto Handoff page ----------
5139
5221
  // Off: one sentence and one button. On: where each account's work would go next, the last hand-overs and what is
5140
5222
  // still missing. Turning on is one plan shown first (hp.preview) and one confirmation; no commands to copy.
5141
- const hp = { data: null, plan: null, loading: false, error: null, preview: null, previewing: false, applying: false, failed: null, changed: false, sameFamily: false, sameTouched: false, busyOff: false, askSame: false }
5223
+ const hp = { data: null, plan: null, loading: false, error: null, preview: null, previewing: false, applying: false, failed: null, changed: false, busyOff: false, askSame: false, askApi: false }
5142
5224
  async function loadHandoffPage() {
5143
5225
  // One read at a time: a second refresh while one runs would end its spinner early.
5144
5226
  if (hp.loading) return
@@ -5154,8 +5236,8 @@ const MAIN = String.raw `
5154
5236
  hp.loading = false
5155
5237
  renderHandoffPage()
5156
5238
  }
5157
- // Off, the box on this page decides; on, "finish setting up" keeps whatever is set.
5158
- function hpBody() { return ahState().settings.auto ? {} : { sameFamily: hp.sameFamily } }
5239
+ // The Handoff Rules on this page write each setting as it is switched, so turning on keeps whatever is set.
5240
+ function hpBody() { return {} }
5159
5241
  async function hpPreview() {
5160
5242
  hp.previewing = true
5161
5243
  hp.failed = null
@@ -5171,11 +5253,10 @@ const MAIN = String.raw `
5171
5253
  renderHandoffPage()
5172
5254
  try {
5173
5255
  const r = await api('/api/handoff/enable', Object.assign(hpBody(), { confirm: true, expect: hp.preview && hp.preview.key }))
5174
- const bad = r.results.find((x) => !x.ok)
5175
- // A plan that changed since it was shown is shown again; one with nothing left to do means it is already on.
5176
- hp.changed = Boolean(r.changed) && !r.plan.alreadyOn
5177
- if (hp.changed || r.plan.blocked) hp.preview = r.plan
5178
- else if (bad) hp.failed = { step: bad, plan: r.plan }
5256
+ const out = enableOutcome(r)
5257
+ hp.changed = out.kind === 'changed'
5258
+ if (out.kind === 'changed' || out.kind === 'blocked') hp.preview = out.plan
5259
+ else if (out.kind === 'failed') hp.failed = { step: out.step, plan: out.plan }
5179
5260
  else { hp.preview = null; toast(t('toast.ahOn')) }
5180
5261
  await load({ silent: true })
5181
5262
  await loadHandoffPage()
@@ -5183,14 +5264,16 @@ const MAIN = String.raw `
5183
5264
  hp.applying = false
5184
5265
  renderHandoffPage()
5185
5266
  }
5186
- async function hpChange(change, message) {
5267
+ // Every change on this page applies at once and its toast carries the undo (spec §3.17 "接力规则").
5268
+ async function hpChange(change, message, opts) {
5187
5269
  hp.busyOff = true
5188
5270
  renderHandoffPage()
5189
5271
  try {
5190
- await api('/api/handoff/configure', { change, confirm: true })
5272
+ const { undo } = await saveHandoffChange(api, change, opts)
5191
5273
  hp.preview = null
5192
5274
  hp.askSame = false
5193
- toast(message)
5275
+ hp.askApi = false
5276
+ toast(message, undo ? { action: { label: t('toast.undo'), run: () => hpChange(undo, t('hp.undone'), { noUndo: true }) }, duration: 12000 } : undefined)
5194
5277
  await load({ silent: true })
5195
5278
  await loadHandoffPage()
5196
5279
  } catch (e) { hp.failed = { error: e } }
@@ -5230,39 +5313,116 @@ const MAIN = String.raw `
5230
5313
  return h('p', { class: 'note note-fail', role: 'alert' }, t('hp.stepFailed', { step: hpStepTitle(f.step), why: f.step.message || '' }) + ' ' + t('hp.tryAgain'))
5231
5314
  }
5232
5315
  function hpDecision(d, starts) {
5233
- const thr = ahState().settings.threshold
5234
- if (d.kind === 'pick') return starts.includes('quota') ? t('hp.goesTo', { to: accountLabel(d.ref), p: thr }) : t('hp.goesToLimit', { to: accountLabel(d.ref) })
5316
+ if (d.kind === 'pick') return starts.includes('quota') ? t('hp.goesTo', { to: accountLabel(d.ref) }) : t('hp.goesToLimit', { to: accountLabel(d.ref) })
5235
5317
  if (d.kind === 'wait') return t('hp.waits', { at: clock(d.until) })
5236
5318
  return d.earliestReset ? t('hp.noneBack', { ref: accountLabel(d.earliestReset.ref), at: clock(d.earliestReset.at) }) : t('hp.none')
5237
5319
  }
5238
- // The one case where reaching the threshold does not switch (spec §3.17 "选号" 1); none when waiting is off.
5239
- function hpWaitRule(st) {
5240
- const w = st.settings.waitIfResetWithinMinutes
5241
- return w > 0 ? ' ' + t('hp.waitRule', { w }) : null
5242
- }
5243
5320
  function hpRows(d) {
5244
5321
  if (!d.takeover.length) return h('p', { class: 'muted small' }, t('hp.noAccounts'))
5245
5322
  return h('ul', { class: 'hp-rows' }, d.takeover.map((r) => {
5246
5323
  const a = S.state.accounts.find((x) => x.ref === r.ref)
5247
5324
  const p = a ? quotaPressure(a, Date.now()) : -1
5248
5325
  return h('li', null,
5249
- h('span', { class: 'title-logo' }, badge(r.family, 'sm'), h('b', { class: 'mono' }, accountLabel(r.ref))),
5250
- h('span', { class: 'muted' }, p >= 0 ? t('hp.used', { p: Math.round(p) }) : t('hp.noQuota')),
5251
- h('span', null, hpDecision(r.decision, r.starts)))
5326
+ h('span', { class: 'title-logo' }, badge(r.family, 'sm'), h('span', { class: 'mono' }, accountLabel(r.ref))),
5327
+ h('span', { class: 'muted num' }, p >= 0 ? t('hp.used', { p: Math.round(p) }) : t('hp.noQuota')),
5328
+ h('span', { class: r.decision.kind === 'none' ? 'muted' : null }, hpDecision(r.decision, r.starts)))
5252
5329
  }))
5253
5330
  }
5254
5331
  function hpRecent(d) {
5255
5332
  if (!d.recent.length) return h('p', { class: 'muted small' }, t('hp.noneYet'))
5256
- return h('ul', { class: 'hp-rows' }, d.recent.map((r) => h('li', null,
5333
+ return h('ul', { class: 'hp-rows hp-recent' }, d.recent.map((r) => h('li', null,
5257
5334
  h('span', { class: 'mono' }, accountLabel(r.from) + ' → ' + accountLabel(r.to)),
5258
- h('span', { class: 'muted' }, clock(r.at) + ' · ' + ago(r.at)),
5335
+ h('span', { class: 'muted num' }, clock(r.at) + ' · ' + ago(r.at)),
5259
5336
  h('span', { class: 'muted' }, r.trigger ? t('hp.why.' + r.trigger) : ''))))
5260
5337
  }
5261
- function hpSameFamilyBox(checked, onchange) {
5262
- const input = h('input', { type: 'checkbox', disabled: BOOT.readOnly || hp.busyOff || hp.applying })
5263
- input.checked = checked
5264
- input.addEventListener('change', () => onchange(input.checked))
5265
- return h('div', { class: 'check-row' }, h('label', { class: 'check' }, input, h('span', null, h('b', null, t('hp.sameFamily')), h('span', { class: 'muted small' }, t('ah.sameFamilyWarn')))))
5338
+ // ---- Handoff Rules (spec §3.17 "接力规则"): every rule in effect, each next to the switch that changes it ----
5339
+ function hpRuleText(r) {
5340
+ const p = r.params
5341
+ const thr = ahState().settings.threshold
5342
+ switch (r.id) {
5343
+ case 'when': return p.adaptive ? t('hp.rule.whenAdaptive', { prepare: p.prepareAt, p: p.threshold, max: p.max }) : t('hp.rule.when', { prepare: p.prepareAt, p: p.threshold })
5344
+ case 'windows': return t('hp.rule.windows')
5345
+ case 'takeover': return t('hp.rule.takeover', { limit: p.limit, p: thr })
5346
+ case 'wait': return p.minutes > 0 ? t('hp.rule.wait', { w: p.minutes }) : t('hp.rule.noWait')
5347
+ case 'sameFamily': return t(p.on ? 'hp.rule.sameOn' : 'hp.rule.sameOff')
5348
+ case 'otherFamilies': return p.on ? t('hp.rule.otherOn') : t('hp.rule.otherOff')
5349
+ case 'api': return t(p.on ? 'hp.rule.apiOn' : 'hp.rule.apiOff')
5350
+ case 'organization': return p.allowed.length ? t('hp.rule.orgExcept', { refs: p.allowed.map(accountLabel).join(', ') }) : t('hp.rule.org')
5351
+ case 'order': return t('hp.rule.order', { host: family(p.family).title, order: p.order.map(accountLabel).join(' → ') })
5352
+ case 'countdown': return p.seconds > 0 ? t('hp.rule.countdown', { s: p.seconds }) : t('hp.rule.noCountdown')
5353
+ }
5354
+ return r.text
5355
+ }
5356
+ // An on/off pair like Remote Control's; onPick(true|false) runs only when the side changes.
5357
+ function hpSwitch(key, label, on, onPick) {
5358
+ return h('div', { class: 'seg', role: 'group', 'aria-label': label }, [true, false].map((v) =>
5359
+ h('button', { class: 'seg-btn', type: 'button', 'data-key': key + ':' + v, disabled: BOOT.readOnly || hp.busyOff, 'aria-pressed': String(on === v), onclick: () => { if (on !== v) onPick(v) } }, t(v ? 'hp.on' : 'hp.off'))))
5360
+ }
5361
+ function hpEditBtn(key) {
5362
+ const field = { when: 'prepareAt', wait: 'wait', countdown: 'countdown' }[key]
5363
+ return h('button', { class: 'link-btn', type: 'button', 'data-key': 'edit:' + key, onclick: () => openHandoff(field) }, t('hp.edit'))
5364
+ }
5365
+ function hpConfirm(key, warn, cancel, confirmLabel, run) {
5366
+ return h('div', { class: 'ah-fam hp-confirm', 'data-enter': key },
5367
+ h('p', { class: 'note note-warn' }, warn),
5368
+ h('div', { class: 'row-actions' },
5369
+ h('button', { class: 'btn btn-sm', type: 'button', disabled: hp.busyOff, onclick: cancel }, t('btn.cancel')),
5370
+ h('button', { class: 'btn btn-sm btn-primary', type: 'button', 'data-key': 'confirm:' + key, disabled: BOOT.readOnly || hp.busyOff, onclick: run }, confirmLabel)))
5371
+ }
5372
+ // The AIs in the order they take over; ↑↓ write familyOrder at once.
5373
+ function hpAiOrder(order) {
5374
+ const move = (i, d) => { const l = order.slice(); const x = l[i]; l[i] = l[i + d]; l[i + d] = x; hpChange({ familyOrder: l }, t('hp.aiOrderSaved')) }
5375
+ return h('ol', { class: 'ah-order hp-ai-order' }, order.map((f, i) => h('li', null,
5376
+ h('span', { class: 'title-logo' }, badge(f, 'sm'), (i + 1) + '. ' + family(f).title),
5377
+ h('button', { class: 'btn btn-sm', type: 'button', 'data-key': 'up:' + f, disabled: BOOT.readOnly || hp.busyOff || i === 0, 'aria-label': t('ah.up'), onclick: () => move(i, -1) }, '↑'),
5378
+ h('button', { class: 'btn btn-sm', type: 'button', 'data-key': 'down:' + f, disabled: BOOT.readOnly || hp.busyOff || i === order.length - 1, 'aria-label': t('ah.down'), onclick: () => move(i, 1) }, '↓'))))
5379
+ }
5380
+ // ctl sits right of the rule's text; below takes the full width under it (a sub-switch, a confirmation, the AI order).
5381
+ function hpRuleControls(r) {
5382
+ const p = r.params
5383
+ switch (r.id) {
5384
+ case 'when': return { ctl: [hpEditBtn('when')], below: [h('div', { class: 'hp-rule-sub' }, h('span', { class: 'muted small' }, t('hp.adaptive')),
5385
+ hpSwitch('adaptive', t('hp.adaptive'), p.adaptive, (v) => hpChange({ adaptive: v }, t(v ? 'hp.adaptiveOn' : 'hp.adaptiveOff'))))] }
5386
+ case 'wait': case 'countdown': case 'order': case 'organization': return { ctl: [hpEditBtn(r.id)], below: [] }
5387
+ case 'sameFamily': return { ctl: [hpSwitch('same', t('hp.sameFamily'), p.on, (v) => {
5388
+ if (v) { hp.askSame = true; renderHandoffPage() } else hpChange({ sameFamily: false }, t('hp.sameOff'))
5389
+ })], below: [hp.askSame && !p.on ? hpConfirm('same', t('ah.sameFamilyWarn'), () => { hp.askSame = false; renderHandoffPage() }, t('hp.sameConfirm'), () => hpChange({ sameFamily: true }, t('hp.sameOn'))) : null] }
5390
+ case 'otherFamilies': return { ctl: [hpSwitch('other', t('hp.otherFamilies'), p.on, (v) => hpChange({ otherFamilies: v }, t(v ? 'hp.otherOn' : 'hp.otherOff')))],
5391
+ below: [p.on && p.order.length > 1 ? hpAiOrder(p.order) : null] }
5392
+ case 'api': return { ctl: [hpSwitch('api', t('hp.api'), p.on, (v) => {
5393
+ if (v) { hp.askApi = true; renderHandoffPage() } else hpChange({ api: false }, t('hp.apiOff'))
5394
+ })], below: [!p.accounts ? h('p', { class: 'muted small' }, t('hp.noApi')) : null,
5395
+ hp.askApi && !p.on ? hpConfirm('api', t('hp.apiWarn'), () => { hp.askApi = false; renderHandoffPage() }, t('hp.apiConfirm'), () => hpChange({ api: true }, t('hp.apiOn'))) : null] }
5396
+ }
5397
+ return { ctl: [], below: [] }
5398
+ }
5399
+ function hpRules(d) {
5400
+ if (!d || !d.rules) return null
5401
+ return [h('div', { class: 'page-sec-head' }, h('h3', { class: 'page-sec' }, t('hp.rules')),
5402
+ h('p', { class: 'muted small' }, t('hp.rulesNote'))),
5403
+ h('ol', { class: 'hp-rules' }, d.rules.map((r) => {
5404
+ const c = hpRuleControls(r)
5405
+ return h('li', { 'data-rule': r.id },
5406
+ h('div', { class: 'hp-rule-text' }, hpRuleText(r)),
5407
+ h('div', { class: 'hp-rule-ctl' }, c.ctl),
5408
+ ...c.below.filter(Boolean).map((el) => h('div', { class: 'hp-rule-below' }, el)))
5409
+ }))]
5410
+ }
5411
+ // When no account could take over anywhere, say which switch opens the way instead of leaving a dead end.
5412
+ function hpNoneHint(d) {
5413
+ if (!d.takeover.length || d.takeover.some((r) => r.decision.kind !== 'none')) return null
5414
+ const st = ahState().settings
5415
+ return st.sameFamily && st.otherFamilies ? null : h('p', { class: 'note', 'data-enter': 'none-hint' }, t('hp.noneAll'))
5416
+ }
5417
+ // Up to ADR-0046 API accounts took over by themselves; this asks once, where Auto Handoff is on.
5418
+ function hpApiBanner() {
5419
+ const st = ahState()
5420
+ if (!st || !st.apiUndecided || BOOT.readOnly) return null
5421
+ return h('div', { class: 'banner banner-warn', role: 'status', 'data-enter': 'api-banner' },
5422
+ h('div', null, h('b', null, t('hp.apiAskTitle') + ' '), t('hp.apiAskBody')),
5423
+ h('div', { class: 'banner-actions' },
5424
+ h('button', { class: 'btn btn-sm btn-primary', type: 'button', disabled: hp.busyOff, onclick: () => hpChange({ api: false }, t('hp.apiKeptOff')) }, t('hp.apiKeepOff')),
5425
+ h('button', { class: 'btn btn-sm', type: 'button', disabled: hp.busyOff, onclick: () => hpChange({ api: true }, t('hp.apiOn')) }, t('hp.apiKeepOn'))))
5266
5426
  }
5267
5427
  function renderHandoffPage() {
5268
5428
  const box = $('handoff-page')
@@ -5273,34 +5433,36 @@ const MAIN = String.raw `
5273
5433
  h('h2', { id: 'h-handoff' }, t('hp.title')),
5274
5434
  h('div', { class: 'row-actions' },
5275
5435
  h('button', { class: 'btn btn-sm', type: 'button', onclick: askHandoff }, icon('bot'), t('ask.title.handoff')),
5276
- h('button', { class: 'btn btn-sm', type: 'button', onclick: openHandoff }, t('hp.advanced')),
5436
+ h('button', { class: 'link-btn small', type: 'button', onclick: () => openHandoff() }, t('hp.advanced')),
5277
5437
  hp.loading ? spinner() : null))]
5278
5438
  const on = st.settings.auto
5279
5439
  if (hp.loading && !hp.data) kids.push(h('p', { class: 'muted' }, spinner(), ' ', t('hp.loading')))
5280
5440
  if (hp.error) kids.push(errorNote(hp.error, loadHandoffPage))
5281
5441
  if (BOOT.readOnly) kids.push(h('p', { class: 'note' }, t('banner.readOnly')))
5442
+ const d = hp.data
5443
+ // One status card on top: whether it is on, what that means, and the one action that changes it.
5444
+ const status = (text, action) => h('div', { class: 'hp-status' + (on ? ' is-on' : '') },
5445
+ h('span', { class: 'hp-status-ic' }, icon('swap')),
5446
+ h('div', { class: 'hp-status-text' }, h('b', null, on ? t('hp.statusOn') : t('hp.statusOff')), h('p', { class: 'muted' }, text)),
5447
+ action)
5282
5448
  if (!on) {
5283
- kids.push(h('p', null, t('hp.intro')))
5284
- kids.push(h('ol', { class: 'hp-how' }, h('li', null, t('hp.how1')), h('li', null, t('hp.how2', { p: st.settings.threshold }), hpWaitRule(st)), h('li', null, t('hp.how3', { s: st.settings.countdownSeconds }))))
5285
- if (!S.state.accounts.some((a) => a.kind !== 'api' && st.starters.includes(a.family))) kids.push(h('p', { class: 'note' }, t('hp.noStarters')))
5286
- else {
5287
- if (!hp.preview) {
5288
- // Until the person touches the box it shows what is set now, so turning on again keeps an earlier choice.
5289
- if (!hp.sameTouched) hp.sameFamily = st.settings.sameFamily
5290
- kids.push(hpSameFamilyBox(hp.sameFamily, (v) => { hp.sameFamily = v; hp.sameTouched = true }))
5291
- kids.push(h('div', { class: 'row-actions' }, h('button', { class: 'btn btn-primary', type: 'button', disabled: BOOT.readOnly || hp.previewing, 'aria-busy': hp.previewing ? 'true' : null, onclick: hpPreview }, hp.previewing ? spinner() : null, t('hp.turnOn'))))
5292
- } else kids.push(hpPreviewCard())
5293
- }
5449
+ const starters = S.state.accounts.some((a) => a.kind !== 'api' && st.starters.includes(a.family))
5450
+ kids.push(status(t('hp.intro'), starters && !hp.preview ? h('button', { class: 'btn btn-primary', type: 'button', 'data-key': 'turn-on', disabled: BOOT.readOnly || hp.previewing, 'aria-busy': hp.previewing ? 'true' : null, onclick: hpPreview }, hp.previewing ? spinner() : null, t('hp.turnOn')) : null))
5451
+ if (!starters) kids.push(h('p', { class: 'note' }, t('hp.noStarters')))
5452
+ else if (hp.preview) kids.push(hpPreviewCard())
5453
+ kids.push(hpRules(d))
5454
+ if (d && d.takeover.length) kids.push(h('h3', { class: 'page-sec' }, t('hp.takeoverIf')), hpRows(d), hpNoneHint(d))
5294
5455
  } else {
5295
- kids.push(h('p', null, h('span', { class: 'chip chip-ok' }, t('ah.on')), ' ', t('hp.onNote', { p: st.settings.threshold }), hpWaitRule(st)))
5296
- const d = hp.data
5456
+ kids.push(status(t('hp.onNote'),
5457
+ h('button', { class: 'btn', type: 'button', 'data-key': 'turn-off', disabled: BOOT.readOnly || hp.busyOff, onclick: () => hpChange({ auto: false }, t('toast.ahOff')) }, t('hp.turnOff'))))
5297
5458
  if (d) {
5298
- kids.push(h('h3', { class: 'small' }, t('hp.takeover')), hpRows(d))
5299
- kids.push(h('h3', { class: 'small' }, t('hp.recent')), hpRecent(d))
5459
+ kids.push(h('h3', { class: 'page-sec' }, t('hp.takeover')), hpRows(d), hpNoneHint(d))
5460
+ kids.push(...(hpRules(d) || []))
5461
+ kids.push(h('h3', { class: 'page-sec' }, t('hp.recent')), hpRecent(d))
5300
5462
  }
5301
5463
  const plan = hp.plan
5302
5464
  if (plan && (plan.blocked || plan.steps.length)) {
5303
- kids.push(h('h3', { class: 'small' }, t('hp.missing')))
5465
+ kids.push(h('h3', { class: 'page-sec' }, t('hp.missing')))
5304
5466
  if (!hp.preview) {
5305
5467
  if (plan.blocked) kids.push(h('p', { class: 'note note-fail' }, t('hp.blocked', { why: plan.blocked })), codeCopy('npm i -g sideby'))
5306
5468
  else {
@@ -5310,18 +5472,9 @@ const MAIN = String.raw `
5310
5472
  } else kids.push(hpPreviewCard())
5311
5473
  } else if (plan) kids.push(...hpManual(plan))
5312
5474
  kids.push(h('p', { class: 'muted small' }, t('hp.onlySideby')))
5313
- kids.push(hpSameFamilyBox(st.settings.sameFamily, (v) => {
5314
- if (v) { hp.askSame = true; renderHandoffPage() } else { hp.askSame = false; hpChange({ sameFamily: false }, t('hp.sameOff')) }
5315
- }))
5316
- if (hp.askSame && !st.settings.sameFamily) kids.push(h('div', { class: 'ah-fam' },
5317
- h('p', { class: 'note note-warn' }, t('ah.sameFamilyWarn')),
5318
- h('div', { class: 'row-actions' },
5319
- h('button', { class: 'btn', type: 'button', disabled: hp.busyOff, onclick: () => { hp.askSame = false; renderHandoffPage() } }, t('btn.cancel')),
5320
- h('button', { class: 'btn btn-primary', type: 'button', disabled: BOOT.readOnly || hp.busyOff, onclick: () => { hp.askSame = false; hpChange({ sameFamily: true }, t('hp.sameOn')) } }, t('hp.sameConfirm')))))
5321
- kids.push(h('div', { class: 'row-actions' }, h('button', { class: 'btn', type: 'button', disabled: BOOT.readOnly || hp.busyOff, onclick: () => hpChange({ auto: false }, t('toast.ahOff')) }, hp.busyOff ? spinner() : null, t('hp.turnOff'))))
5322
5475
  }
5323
5476
  kids.push(hpFailed())
5324
- setKids(box, kids)
5477
+ swapKids(box, kids)
5325
5478
  }
5326
5479
 
5327
5480
  // ---------- Remote Control (spec §3.22) ----------
@@ -5330,13 +5483,18 @@ const MAIN = String.raw `
5330
5483
  // target: the side the dialog's switch was moved to, waiting for its confirmation; null while nothing is chosen.
5331
5484
  const rc = { confirm: false, busy: false, target: null }
5332
5485
  const shortRef = (ref) => ref.replace(/^claude:/, '')
5486
+ // One settings key's change: the key and literal values in the code font, "not set" in words.
5487
+ function keyChange(key, from, to) {
5488
+ const val = (v) => (v === null ? t('rc.notSet') : h('code', null, v))
5489
+ return h('span', { class: 'key-change' }, h('code', null, key), ' ', val(from), ' → ', val(to))
5490
+ }
5333
5491
  function remoteControlBanner() {
5334
5492
  const groups = S.state.remoteControl || []
5335
5493
  // API Accounts never sign in to claude.ai, so Remote Control cannot reach their sessions.
5336
5494
  const api = new Set(S.state.accounts.filter((a) => a.kind === 'api').map((a) => a.ref))
5337
5495
  const refs = groups.filter((g) => g.state === 'on').flatMap((g) => g.accounts).filter((r) => !api.has(r)).map(shortRef)
5338
5496
  if (!refs.length || prefs.rcHide) return null
5339
- const todo = groups.filter((g) => g.state === 'on' || g.state === 'default')
5497
+ const todo = remoteControlTodo(groups, 'off')
5340
5498
  const text = h('div', null, h('b', null, t('rc.title') + ' '), tn('rc.body', refs.length, { refs: refs.join(', ') }))
5341
5499
  if (BOOT.readOnly) return h('div', { class: 'banner banner-warn', role: 'status' }, h('div', null, text, ' ', t('rc.readOnly')))
5342
5500
  if (!rc.confirm)
@@ -5345,8 +5503,14 @@ const MAIN = String.raw `
5345
5503
  h('button', { class: 'btn btn-sm btn-primary', type: 'button', onclick: () => { rc.confirm = true; renderBanners() } }, t('rc.turnOff')),
5346
5504
  h('button', { class: 'btn btn-sm', type: 'button', onclick: () => { prefs.rcHide = true; savePrefs(); renderBanners(); toast(t('rc.hidden')) } }, t('rc.keep'))))
5347
5505
  const lines = todo.map((g) => h('li', null, h('code', null, g.file), ' ',
5348
- t('rc.change', { from: g.state === 'on' ? 'true' : t('rc.notSet'), to: 'false' }), ' ',
5506
+ keyChange('remoteControlAtStartup', g.state === 'on' ? 'true' : null, 'false'), ' ',
5349
5507
  h('span', { class: 'muted' }, t('rc.usedBy', { refs: g.accounts.map(shortRef).join(', ') }))))
5508
+ // Files sideby cannot change stay listed, as in the dialog, so "all" never hides one.
5509
+ for (const g of groups.filter((g) => g.state === 'blocked' || g.state === 'missing'))
5510
+ lines.push(h('li', null, h('code', null, g.file), ' ',
5511
+ h('span', { class: 'chip chip-sm' + (g.state === 'blocked' ? ' chip-fail' : '') }, t('rc.state.' + g.state)), ' ',
5512
+ h('span', { class: 'muted' }, t('rc.usedBy', { refs: g.accounts.map(shortRef).join(', ') })),
5513
+ g.reason ? h('div', { class: 'muted small' }, t('rc.skipped') + (g.why ? t('settings.why.' + g.why) : g.reason)) : null))
5350
5514
  return h('div', { class: 'banner banner-warn', role: 'status' },
5351
5515
  h('div', null, h('b', null, t('rc.confirmTitle')), h('ul', null, lines), h('div', { class: 'small' }, t('rc.confirmNote'))),
5352
5516
  h('div', { class: 'banner-actions' },
@@ -5359,8 +5523,10 @@ const MAIN = String.raw `
5359
5523
  if (dlg.kind === 'rc') renderRc()
5360
5524
  const fail = to === 'on' ? 'rc.failedOn' : 'rc.failed'
5361
5525
  try {
5362
- const r = await api('/api/remote-control/' + to, {})
5363
- if (r.ok) {
5526
+ const files = remoteControlTodo(S.state.remoteControl || [], to).map((g) => g.file)
5527
+ const r = await api('/api/remote-control/' + to, { confirm: true, files })
5528
+ if (r.changedSince) toast(t('as.changedSince'), { kind: 'fail' })
5529
+ else if (r.ok) {
5364
5530
  // Show the new state now; the reload that follows confirms it.
5365
5531
  const changed = new Set(remoteControlTodo(S.state.remoteControl || [], to).map((g) => g.file))
5366
5532
  S.state.remoteControl = (S.state.remoteControl || []).map((g) => changed.has(g.file) ? { ...g, state: to, undo: true } : g)
@@ -5377,7 +5543,7 @@ const MAIN = String.raw `
5377
5543
  }
5378
5544
  async function remoteControlUndo() {
5379
5545
  try {
5380
- const r = await api('/api/remote-control/undo', {})
5546
+ const r = await api('/api/remote-control/undo', { confirm: true })
5381
5547
  toast(r.ok ? t('rc.undone') : r.message, { kind: r.ok ? 'ok' : 'fail' })
5382
5548
  } catch (e) { toast(t('toast.undoFailed', { msg: e.message }), { kind: 'fail' }) }
5383
5549
  await load({ silent: true })
@@ -5891,7 +6057,7 @@ const MAIN = String.raw `
5891
6057
  return Boolean(u && u.skillUpdate && u.skillUpdate.installed && u.skillUpdate.installed.length)
5892
6058
  }
5893
6059
  // Example phrases: a click puts one in the Let AI do it box (spec §3.28).
5894
- const AI_ASKS = ['ai.ask.quota', 'ai.ask.pick', 'ai.ask.new', 'ai.ask.api', 'ai.ask.pin', 'ai.ask.relogin', 'ai.ask.fix', 'ai.ask.handoff', 'ai.ask.session', 'ai.ask.history', 'ai.ask.send', 'ai.ask.inbound', 'ai.ask.menubar', 'ai.ask.rc', 'ai.ask.tool']
6060
+ const AI_ASKS = ['ai.ask.quota', 'ai.ask.pick', 'ai.ask.new', 'ai.ask.api', 'ai.ask.pin', 'ai.ask.relogin', 'ai.ask.fix', 'ai.ask.handoff', 'ai.ask.handoffRules', 'ai.ask.session', 'ai.ask.history', 'ai.ask.send', 'ai.ask.inbound', 'ai.ask.menubar', 'ai.ask.rc', 'ai.ask.tool']
5895
6061
  function openRc() {
5896
6062
  if (!S.state || !rcSummary()) return
5897
6063
  rc.target = null
@@ -5910,12 +6076,12 @@ const MAIN = String.raw `
5910
6076
  const sw = h('div', { class: 'set-row rc-switch' }, h('span', null, t('rc.switch')),
5911
6077
  h('div', { class: 'seg', role: 'group', 'aria-label': t('rc.switch') }, ['on', 'off'].map((v) =>
5912
6078
  h('button', { class: 'seg-btn', type: 'button', disabled: BOOT.readOnly || rc.busy, 'aria-pressed': String((to || sum) === v), onclick: () => pick(v) }, t('rc.state.' + v)))))
5913
- const fromOf = (g) => g.state === 'on' ? 'true' : g.state === 'off' ? 'false' : t('rc.notSet')
6079
+ const fromOf = (g) => g.state === 'on' ? 'true' : g.state === 'off' ? 'false' : null
5914
6080
  const rows = groups.map((g) => h('li', { class: 'rc-row' },
5915
6081
  h('div', null, h('code', null, g.file), ' ', h('span', { class: 'chip chip-sm ' + (g.state === 'on' ? 'chip-warn' : g.state === 'off' ? 'chip-ok' : g.state === 'blocked' ? 'chip-fail' : '') }, t('rc.state.' + g.state))),
5916
6082
  h('div', { class: 'muted small' }, t('rc.usedBy', { refs: g.accounts.map(shortRef).join(', ') })),
5917
- g.reason ? h('div', { class: 'muted small' }, g.reason) : null,
5918
- changing.has(g.file) ? h('div', { class: 'small rc-change' }, t('rc.change', { from: fromOf(g), to: to === 'on' ? 'true' : 'false' })) : null))
6083
+ g.reason ? h('div', { class: 'muted small' }, g.why ? t('settings.why.' + g.why) : g.reason) : null,
6084
+ changing.has(g.file) ? h('div', { class: 'small rc-change' }, keyChange('remoteControlAtStartup', fromOf(g), to === 'on' ? 'true' : 'false')) : null))
5919
6085
  const body = [h('p', { class: 'small' }, t('rc.explain')), sw, h('ul', { class: 'rc-list' }, rows)]
5920
6086
  if (to === 'on') body.push(h('div', { class: 'note note-warn small' }, t('rc.onWarn')))
5921
6087
  else if (to === 'off') body.push(h('p', { class: 'small muted' }, t('rc.confirmNote')))
@@ -6671,6 +6837,9 @@ const MAIN = String.raw `
6671
6837
  const mb = curPage === 'accounts' ? menubarHint() : null
6672
6838
  if (mb) out.push(mb)
6673
6839
  }
6840
+ // On every page until answered: whether API accounts may still take over (ADR-0046).
6841
+ const apiBanner = hpApiBanner()
6842
+ if (apiBanner) out.push(apiBanner)
6674
6843
  const rcBanner = remoteControlBanner()
6675
6844
  if (rcBanner) out.push(rcBanner)
6676
6845
  const inbBanner = inboundBanner()