@awebai/oats 0.63.0 → 0.64.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.
@@ -185,6 +185,51 @@ export function clusterInstances(instances, { links = instanceLinks } = {}) {
185
185
  return clusters.sort((a, b) => rank(a.head, b.head)).map(({ head: _head, ...c }) => c);
186
186
  }
187
187
 
188
+ /** `clusterInstances` output ordered by last focus (#1383; the command palette's order, never the sidebar's).
189
+ * `lastFocus` is Map<instanceId, time>. A node's key is the latest focus time over the node and all of its
190
+ * descendants; a node with no focus anywhere in its subtree has none. Clusters are ordered by their key, most
191
+ * recent first, and so is each set of nodes at one level of a cluster: its depth-0 roots, and the children of
192
+ * each parent. Nodes without a key follow every node with one, in their input order (a stable sort), so an
193
+ * empty record returns the input order. A child stays under its parent: each cluster is walked again in
194
+ * pre-order, every depth unchanged. Parentage is read from the pre-order and the depths alone (a node's
195
+ * children are the following rows one level deeper, until a row at its depth or above), never from names: the
196
+ * tree was built by identity, and a malformed cycle's depth-0 leftovers are roots like any other. Returns new
197
+ * cluster objects over the same rows; the input is not mutated. */
198
+ export function recencyOrder(clusters, lastFocus) {
199
+ const timeOf = (inst) => {
200
+ const time = lastFocus?.get(instanceId(inst));
201
+ return Number.isFinite(time) ? time : null;
202
+ };
203
+ const recentFirst = (a, b) => (a.key === null ? (b.key === null ? 0 : 1) : b.key === null ? -1 : b.key - a.key);
204
+ const ranked = clusters.map((cluster) => {
205
+ const roots = [];
206
+ const open = []; // the path from a root to the last row read
207
+ for (const inst of cluster.instances) {
208
+ const node = { inst, depth: inst.depth || 0, key: timeOf(inst), kids: [] };
209
+ while (open.length && open.at(-1).depth >= node.depth) open.pop();
210
+ (open.length ? open.at(-1).kids : roots).push(node);
211
+ open.push(node);
212
+ }
213
+ // Keys bottom-up, then each level sorted (stable) and walked again in pre-order.
214
+ const keyOf = (node) => {
215
+ for (const kid of node.kids) {
216
+ const key = keyOf(kid);
217
+ if (key !== null && (node.key === null || key > node.key)) node.key = key;
218
+ }
219
+ return node.key;
220
+ };
221
+ const instances = [];
222
+ const walk = (nodes) => {
223
+ for (const node of [...nodes].sort(recentFirst)) { instances.push(node.inst); walk(node.kids); }
224
+ };
225
+ const top = { key: null, kids: roots };
226
+ keyOf(top);
227
+ walk(roots);
228
+ return { key: top.key, cluster: { ...cluster, instances } };
229
+ });
230
+ return ranked.sort(recentFirst).map(({ cluster }) => cluster);
231
+ }
232
+
188
233
  /** Find the roster instance a UI reference means. References carry the
189
234
  * display name plus whatever identity the caller knows (home/agentsRoot —
190
235
  * sidebar rows know both; older callers pass a bare name). Identity match
@@ -449,12 +494,17 @@ export function visibleClusters(allInstances, visibleInstances, { links = instan
449
494
  * [{ key, label, clusters: [{ key, instances }] }], each cluster projected to its visible members, which carry
450
495
  * their tree depth. A group's key is unique even when two roots share a name (other agents roots or hosts), and
451
496
  * does not depend on what is visible: a cluster's is `cluster:<its place among the full roster's multi-member
452
- * clusters>:<name>`, a run's `independent:<instanceId of its first member in the full roster>`. */
453
- export function rosterGroups(allInstances, visibleInstances, { links = instanceLinks } = {}) {
497
+ * clusters>:<name>`, a run's `independent:<instanceId of its first member in the full roster>`.
498
+ * `lastFocus` (Map<instanceId, time>, optional; the command palette's, #1383): the full roster's clusters are
499
+ * ordered by last focus (`recencyOrder`) before the groups form by the same rule, so a run is the consecutive
500
+ * single clusters of that order, keyed by its first member there. What is visible still decides only which rows
501
+ * are listed, never the order. Without it, the roster's own order (the sidebar's and the TUI's). */
502
+ export function rosterGroups(allInstances, visibleInstances, { links = instanceLinks, lastFocus } = {}) {
454
503
  const visibleIds = new Set(visibleInstances.map((i) => instanceId(i)));
455
504
  const groups = [];
456
505
  let place = 0, run = null;
457
- for (const c of clusterInstances(allInstances, { links })) {
506
+ const clusters = clusterInstances(allInstances, { links });
507
+ for (const c of lastFocus instanceof Map ? recencyOrder(clusters, lastFocus) : clusters) {
458
508
  const shown = { ...c, instances: c.instances.filter((i) => visibleIds.has(instanceId(i))) };
459
509
  if (c.instances.length > 1) {
460
510
  run = null;
@@ -5,7 +5,7 @@
5
5
  * A deployment is `{ id, machine, path, label, local, reachable, identityFrom, primary, stale?, reason? }`
6
6
  * and a row carries `deployment: { id }`. Nothing is derived from a host name or a path beyond the
7
7
  * display label. A view with one deployment has one section and no heading. */
8
- import { rosterGroups } from './instance-tree.mjs';
8
+ import { rosterGroups, instanceId } from './instance-tree.mjs';
9
9
  import { machineLabelParts } from './deployment-label.mjs';
10
10
  import { requireTerms } from './client-terms.mjs';
11
11
 
@@ -51,16 +51,32 @@ export const rowStale = (row, deployments) => {
51
51
  * Sections with no visible row are left out. Each is labelled by its machine, with the path tail
52
52
  * only when one machine holds two (deployment-label.mjs machineLabels), in the client's terms
53
53
  * (client-terms.mjs: the label of a deployment that names no machine). Returns
54
- * `[{ deployment|null, label|null, groups }]`, a group being `{ key, label, clusters }` as before. */
55
- export function rosterSections(instances, visible, deployments, terms) {
54
+ * `[{ deployment|null, label|null, groups }]`, a group being `{ key, label, clusters }` as before.
55
+ * `lastFocus` (Map<instanceId, time>, optional; the command palette's, #1383): each section's groups follow
56
+ * last focus (rosterGroups), and the sections are ordered by the latest focus over each one's full roster,
57
+ * most recent first, then the panel's order. Without it, the panel's order (the sidebar's and the TUI's). */
58
+ export function rosterSections(instances, visible, deployments, terms, { lastFocus } = {}) {
56
59
  requireTerms(terms, 'rosterSections');
57
- const groupsOf = (all, shown) => rosterGroups(all, shown); // the relation groups the palette lists too
60
+ const recency = lastFocus instanceof Map ? { lastFocus } : {};
61
+ const groupsOf = (all, shown) => rosterGroups(all, shown, recency); // the relation groups the palette lists too
58
62
  if (!isMultiDeployment(deployments)) return [{ deployment: null, label: null, groups: groupsOf(instances, visible) }];
59
63
  const sectionOf = splitByDeployment(deployments);
60
64
  const all = sectionOf(instances), shown = sectionOf(visible), labels = machineLabelParts(deployments, terms);
61
65
  const label = ({ machine, tail }) => (tail ? `${machine} · ${tail}` : machine);
62
- return deployments.map(d => ({ deployment: d, parts: labels.get(d.id), all: all.get(d.id), shown: shown.get(d.id) }))
63
- .filter(s => s.shown.length)
66
+ const sections = deployments.map(d => ({ deployment: d, parts: labels.get(d.id), all: all.get(d.id), shown: shown.get(d.id) }));
67
+ if (recency.lastFocus) {
68
+ const latest = rows => rows.reduce((max, row) => {
69
+ const time = lastFocus.get(instanceId(row));
70
+ return Number.isFinite(time) && (max === null || time > max) ? time : max;
71
+ }, null);
72
+ const keyed = new Map(sections.map(s => [s, latest(s.all)]));
73
+ // Stable: sections without a focus keep the panel's order, after those with one.
74
+ sections.sort((a, b) => {
75
+ const ka = keyed.get(a), kb = keyed.get(b);
76
+ return ka === null ? (kb === null ? 0 : 1) : kb === null ? -1 : kb - ka;
77
+ });
78
+ }
79
+ return sections.filter(s => s.shown.length)
64
80
  .map(s => ({ deployment: s.deployment, label: label(s.parts), machine: s.parts.machine, tail: s.parts.tail, groups: groupsOf(s.all, s.shown) }));
65
81
  }
66
82