@@ -1,10 +1,11 @@
11import { COLORS, isColor, type GroupColor } from "../shared/colors";
2−import { labelOf, repairLabel, safeEmoji, stripTitle } from "../shared/label";
2+import { labelOf, repairLabel, safeEmoji, stripTitle, titleKey } from "../shared/label";
33import { colorFor, isInternalUrl, promptAddress, provisionalTitle } from "../shared/url";
44import { pathKey } from "./engine";
55import { membersHash, memberUrls, settled, toItems, type Member, type NameItem } from "./naming";
66import { addToGroup, createManagedGroup, groupExists, ungroup, type Runtime } from "./runtime";
7−import { markDirty, type State } from "./state";
7+import type { HostReply } from "./host";
8+import { markDirty, type GroupRecord, type HostError, type HostErrorCode, type State, type TabRecord } from "./state";
89
910/** Section 3/9: "Organize all tabs" — cluster ungrouped tabs in batches sized to the model's window. */
1011
@@ -151,88 +152,192 @@ export interface OrganizeReport {
151152 error?: string;
152153}
153154
154−/** Existing groups a batch may add tabs to: extension-managed topic groups in this window. */
155+/** Existing groups a batch may add tabs to: Diagonal's topic groups in this window, the ones in use first. */
155156export function existingGroupsFor(state: State, windowId: number, extraIds: number[] = []): { ids: number[]; list: ExistingGroup[] } {
157+ const members = new Map<number, TabRecord[]>();
158+ for (const t of Object.values(state.tabs)) if (t.groupId !== -1) members.set(t.groupId, [...(members.get(t.groupId) ?? []), t]);
159+ const inUse = (g: GroupRecord): number =>
160+ Math.max(g.dirtyAt ?? 0, g.lastNamedAt ?? 0, ...(members.get(g.id) ?? []).map((t) => t.lastActivatedAt ?? 0));
156161 const groups = Object.values(state.groups)
157− .filter((g) => g.windowId === windowId && g.managed && g.origin !== "tidy" && (g.stripTitle || g.title))
158− .sort((a, b) => Number(extraIds.includes(b.id)) - Number(extraIds.includes(a.id)))
159− .slice(0, EXISTING_GROUPS_CAP);
162+ .filter((g) => g.windowId === windowId && g.managed && g.origin !== "tidy" && !g.restored && (g.stripTitle || g.title))
163+ .map((g) => ({ g, first: extraIds.includes(g.id), at: inUse(g) }))
164+ .sort((a, b) => Number(b.first) - Number(a.first) || b.at - a.at)
165+ .slice(0, EXISTING_GROUPS_CAP)
166+ .map(({ g }) => g);
160167 const list = groups.map((g, i) => ({
161168 g: i,
162169 title: labelOf(g.stripTitle || g.title),
163− samples: Object.values(state.tabs)
164− .filter((t) => t.groupId === g.id)
165− .slice(0, 2)
166− .map((t) => t.title.slice(0, 60)),
170+ samples: (members.get(g.id) ?? []).slice(0, 2).map((t) => t.title.slice(0, 60)),
167171 }));
168172 return { ids: groups.map((g) => g.id), list };
169173}
170174
171−export async function organizeWindow(rt: Runtime, windowId: number): Promise<OrganizeReport> {
175+/** Errors about what was sent rather than about the host: a smaller or plainer request may go through. */
176+const CONTENT_ERRORS = new Set<HostErrorCode>(["OVER_BUDGET", "GUARDRAIL", "BAD_MODEL_OUTPUT"]);
177+const MAX_TIMEOUT_SPLITS = 1;
178+
179+/** One organize call, with the retries that can rescue the same tabs: strict output, then no page text. */
180+async function askOrganize(rt: Runtime, part: Member[], existingGroups: ExistingGroup[], withDescriptions: boolean): Promise<HostReply<OrganizeResult>> {
181+ const settings = rt.settings();
182+ const payload: OrganizePayload = {
183+ items: toItems(part, settings, withDescriptions),
184+ existingGroups,
185+ allowNew: part.length >= 2,
186+ maxGroups: maxGroupsFor(part.length),
187+ minGroupSize: settings.organizeMinGroupSize,
188+ };
189+ let reply = await rt.host<OrganizeResult>("organize", payload);
190+ if (!reply.ok && reply.error.code === "BAD_MODEL_OUTPUT") reply = await rt.host<OrganizeResult>("organize", payload, { strict: true });
191+ if (!reply.ok && reply.error.code === "GUARDRAIL" && payload.items.some((i) => i.description)) {
192+ // What a page says is the likeliest thing to trip Apple's filter; its title and address rarely do.
193+ payload.items = toItems(part, settings, false);
194+ reply = await rt.host<OrganizeResult>("organize", payload);
195+ }
196+ return reply;
197+}
198+
199+/** One organize run over a window: what it made, what it moved, and whether the host stopped it. */
200+export interface SortRun {
201+ windowId: number;
202+ createdIds: number[];
203+ previous: Record<number, number>;
204+ grouped: number;
205+ left: number;
206+ timeoutSplits: number;
207+ /** Auto-organize: remember what was looked at, so the same tabs are not sent again unchanged. */
208+ stamp: boolean;
209+ stopped?: HostError;
210+}
211+
212+export const newRun = (windowId: number, stamp: boolean): SortRun => ({ windowId, createdIds: [], previous: {}, grouped: 0, left: 0, timeoutSplits: 0, stamp });
213+
214+/**
215+ * Sort one part of a window. A reply about the tabs themselves (too long, refused by Apple's filter, not
216+ * valid output) splits the part in halves, so one page the model won't handle leaves the others sorted; a
217+ * tab that fails alone is set aside until its page or title changes. Tabs that changed while the model was
218+ * thinking are left out of its answer.
219+ */
220+export async function sortPart(rt: Runtime, part: Member[], run: SortRun, probe = false): Promise<void> {
221+ if (!part.length || run.stopped) return;
222+ const { ids: existingIds, list: existingGroups } = existingGroupsFor(rt.state(), run.windowId, run.createdIds);
223+ if (part.length < 2 && !existingIds.length && !probe) {
224+ run.left += part.length;
225+ return; // one tab and nothing to join: no call can place it (a probe still learns whether the model takes it)
226+ }
227+ const reply = await askOrganize(rt, part, existingGroups, true);
228+ if (!reply.ok) {
229+ const e = reply.error;
230+ const timedOut = e.code === "TIMEOUT" && part.length > 4 && run.timeoutSplits < MAX_TIMEOUT_SPLITS;
231+ if (part.length > 1 && (CONTENT_ERRORS.has(e.code) || timedOut)) {
232+ if (timedOut) run.timeoutSplits++;
233+ const half = Math.ceil(part.length / 2);
234+ await sortPart(rt, part.slice(0, half), run, true);
235+ await sortPart(rt, part.slice(half), run, true);
236+ return;
237+ }
238+ if (CONTENT_ERRORS.has(e.code)) {
239+ const rec = rt.state().tabs[part[0].id];
240+ if (rec) rec.organizeRefused = organizedKeyOf(part[0]);
241+ rt.log("organize: set aside a tab the model would not sort", e.code);
242+ run.left += 1;
243+ return;
244+ }
245+ run.stopped = e;
246+ return;
247+ }
172248 const settings = rt.settings();
249+ const plan = planFromResult(reply.result, part.map((m) => m.id), existingIds, settings.organizeMinGroupSize, maxGroupsFor(part.length), (ids) =>
250+ colorFor(part.find((m) => m.id === ids[0])?.url ?? ""),
251+ );
252+ const unchanged = await stillAsSent(rt, part, run.windowId);
253+ const trimmed = trimPlan(rt.state(), plan, unchanged, settings.organizeMinGroupSize);
254+ const failed = await applyPlan(rt, trimmed, run.windowId, part, run.previous, run.createdIds);
255+ const placed = [...trimmed.joins, ...trimmed.creates].reduce((n, x) => n + x.tabIds.filter((id) => !failed.has(id)).length, 0);
256+ run.grouped += placed;
257+ run.left += part.length - placed;
258+ if (run.stamp) {
259+ const s = rt.state();
260+ for (const m of part) {
261+ const rec = s.tabs[m.id];
262+ if (rec && unchanged.has(m.id) && !failed.has(m.id)) rec.organizedKey = organizedKeyOf(m);
263+ }
264+ }
265+}
266+
267+/** The tabs of a part that are still loose, in this window, on the page and title the model saw. */
268+async function stillAsSent(rt: Runtime, part: Member[], windowId: number): Promise<Set<number>> {
269+ const live = new Map((await chrome.tabs.query({ windowId }).catch(() => [])).map((t) => [t.id!, t]));
270+ const s = rt.state();
271+ return new Set(
272+ part
273+ .filter((m) => {
274+ const t = live.get(m.id);
275+ return !!t && organizable(t) && !s.tabs[m.id]?.keepLoose && organizedKeyOf(t) === organizedKeyOf(m);
276+ })
277+ .map((m) => m.id),
278+ );
279+}
280+
281+/** A plan with the tabs that moved on taken out: a new group left too small is not made, a join into a group gone is dropped. */
282+export function trimPlan(state: State, plan: Plan, keep: Set<number>, minGroupSize: number): Plan {
283+ const out: Plan = { joins: [], creates: [], leftovers: plan.leftovers.filter((id) => keep.has(id)) };
284+ for (const j of plan.joins) {
285+ const tabIds = j.tabIds.filter((id) => keep.has(id));
286+ const g = state.groups[j.groupId];
287+ if (tabIds.length && g?.managed && !g.restored) out.joins.push({ ...j, tabIds });
288+ else out.leftovers.push(...tabIds);
289+ }
290+ for (const c of plan.creates) {
291+ const tabIds = c.tabIds.filter((id) => keep.has(id));
292+ if (tabIds.length >= Math.max(2, minGroupSize)) out.creates.push({ ...c, tabIds });
293+ else out.leftovers.push(...tabIds);
294+ }
295+ return out;
296+}
297+
298+export async function organizeWindow(rt: Runtime, windowId: number): Promise<OrganizeReport> {
299+ const s0 = rt.state();
173300 const tabs = (await chrome.tabs.query({ windowId })).filter((t) => organizable(t)).sort((a, b) => a.index - b.index);
174301 const report: OrganizeReport = { grouped: 0, groups: 0, left: 0 };
175− if (tabs.length < 2) {
302+ // A tab the model already would not sort, on the same page, is not sent again.
303+ const sendable = tabs.filter((t) => s0.tabs[t.id!]?.organizeRefused !== organizedKeyOf(t));
304+ if (sendable.length < 2) {
176305 report.left = tabs.length;
177306 return report;
178307 }
179− const members: Member[] = tabs.map((t) => ({
180− id: t.id!,
181− index: t.index,
182− url: t.url ?? "",
183− title: t.title ?? "",
184− status: t.status,
185− description: rt.state().tabs[t.id!]?.description,
186− lastActive: t.lastAccessed ?? 0,
187− }));
188− const previous: Record<number, number> = {};
189− const createdIds: number[] = [];
190− const queue = makeBatches(members);
308+ const run = newRun(windowId, false);
191309 rt.state().inFlight = { groupId: -1, startedAt: rt.now() };
192310 rt.refreshBadge();
193311 try {
194− while (queue.length) {
195− const batch = queue.shift()!;
196− const { ids: existingIds, list: existingGroups } = existingGroupsFor(rt.state(), windowId, createdIds);
197− const maxGroups = maxGroupsFor(batch.length);
198− const payload: OrganizePayload = {
199− items: toItems(batch, settings),
200− existingGroups,
201− allowNew: true,
202− maxGroups,
203− minGroupSize: settings.organizeMinGroupSize,
204− };
205− let reply = await rt.host<OrganizeResult>("organize", payload);
206− if (!reply.ok && reply.error.code === "OVER_BUDGET" && batch.length > 1) {
207− const half = Math.ceil(batch.length / 2);
208− queue.unshift(batch.slice(0, half), batch.slice(half));
209− continue;
210− }
211− if (!reply.ok && reply.error.code === "BAD_MODEL_OUTPUT") reply = await rt.host<OrganizeResult>("organize", payload, { strict: true });
212− if (!reply.ok) {
213− report.error = `${reply.error.code}: ${reply.error.message}`;
214− report.left += batch.length + queue.flat().length;
215− break;
216− }
217− const batchIds = batch.map((m) => m.id);
218− const plan = planFromResult(reply.result, batchIds, existingIds, settings.organizeMinGroupSize, maxGroups, (ids) =>
219− colorFor(members.find((m) => m.id === ids[0])?.url ?? ""),
220− );
221− await applyPlan(rt, plan, windowId, members, previous, createdIds);
222− report.grouped += plan.joins.reduce((n, j) => n + j.tabIds.length, 0) + plan.creates.reduce((n, c) => n + c.tabIds.length, 0);
223− report.left += plan.leftovers.length;
312+ for (const batch of makeBatches(sendable.map((t) => toMember(rt.state(), t)))) {
313+ await sortPart(rt, batch, run);
314+ if (run.stopped) break;
224315 }
225316 } finally {
226317 const s = rt.state();
227318 if (s.inFlight?.groupId === -1) s.inFlight = undefined;
228− report.groups = createdIds.length;
229− if (Object.keys(previous).length) s.lastOrganize = { at: rt.now(), previous };
319+ report.grouped = run.grouped;
320+ report.groups = run.createdIds.length;
321+ report.left = tabs.length - run.grouped;
322+ if (run.stopped) report.error = `${run.stopped.code}: ${run.stopped.message}`;
323+ if (Object.keys(run.previous).length) s.lastOrganize = { at: rt.now(), previous: run.previous };
230324 rt.commit();
231325 rt.refreshBadge();
232326 }
233327 return report;
234328}
235329
330+const toMember = (s: State, t: chrome.tabs.Tab): Member => ({
331+ id: t.id!,
332+ index: t.index,
333+ url: t.url ?? "",
334+ title: t.title ?? "",
335+ status: t.status,
336+ description: s.tabs[t.id!]?.description,
337+ lastActive: t.lastAccessed ?? 0,
338+});
339+
340+/** Carry out a plan; returns the tabs whose move failed, which stay as they were. */
236341export async function applyPlan(
237342 rt: Runtime,
238343 plan: Plan,
@@ -240,11 +345,15 @@ export async function applyPlan(
240345 members: Member[],
241346 previous: Record<number, number>,
242347 createdIds: number[],
243−): Promise<void> {
348+): Promise<Set<number>> {
244349 const settings = rt.settings();
245350 const byId = new Map(members.map((m) => [m.id, m]));
351+ const failed = new Set<number>();
246352 for (const j of plan.joins) {
247− if (!(await addToGroup(rt, j.tabIds, j.groupId))) continue;
353+ if (!(await addToGroup(rt, j.tabIds, j.groupId))) {
354+ for (const id of j.tabIds) failed.add(id);
355+ continue;
356+ }
248357 for (const id of j.tabIds) previous[id] = -1;
249358 const g = rt.state().groups[j.groupId];
250359 if (g) {
@@ -265,7 +374,10 @@ export async function applyPlan(
265374 lastNamedAt: named ? rt.now() : undefined,
266375 dirty: false,
267376 });
268− if (groupId === undefined) continue;
377+ if (groupId === undefined) {
378+ for (const id of c.tabIds) failed.add(id);
379+ continue;
380+ }
269381 createdIds.push(groupId);
270382 for (const id of c.tabIds) previous[id] = -1;
271383 const g = rt.state().groups[groupId];
@@ -274,6 +386,7 @@ export async function applyPlan(
274386 rt.naming.touch(groupId);
275387 }
276388 }
389+ return failed;
277390}
278391
279392/** Undo within the hour: every tab goes back to the group it was in (organize only takes loose tabs). */
@@ -301,7 +414,7 @@ export async function undoOrganize(rt: Runtime): Promise<number> {
301414}
302415
303416/** What auto-organize remembers about a tab: it looks again only when the page or title changes. */
304−export const organizedKeyOf = (t: { url?: string; title?: string }): string => `${pathKey(t.url)}\n${t.title ?? ""}`;
417+export const organizedKeyOf = (t: { url?: string; title?: string }): string => `${pathKey(t.url)}\n${titleKey(t.title)}`;
305418
306419export const AUTO_RETRY_MS = 300_000;
307420export const AUTO_CONTINUE_MS = 1_500;
@@ -379,59 +492,25 @@ export class AutoOrganizer {
379492 const s = rt.state();
380493 // Still-loading tabs are skipped here; finishing the load touches the window again. Discarded tabs keep
381494 // their title and address, so they're sorted like loaded ones.
382− const loose = tabs.filter((t) => organizable(t) && settled(t) && !s.tabs[t.id!]?.keepLoose);
495+ const loose = tabs.filter((t) => {
496+ const rec = s.tabs[t.id!];
497+ return organizable(t) && settled(t) && !rec?.keepLoose && rec?.organizeRefused !== organizedKeyOf(t);
498+ });
383499 const fresh = loose.filter((t) => s.tabs[t.id!]?.organizedKey !== organizedKeyOf(t));
384500 if (!fresh.length) return;
385− const { ids: existingIds, list: existingGroups } = existingGroupsFor(s, windowId);
386− if (loose.length < 2 && !existingIds.length) return; // one loose tab and nothing to join: wait for company
387− const toMember = (t: chrome.tabs.Tab): Member => ({
388− id: t.id!,
389− index: t.index,
390− url: t.url ?? "",
391− title: t.title ?? "",
392− status: t.status,
393− description: s.tabs[t.id!]?.description,
394− lastActive: t.lastAccessed ?? 0,
395− });
396− const ordered = [...fresh, ...loose.filter((t) => !fresh.includes(t))].map(toMember);
501+ if (loose.length < 2 && !existingGroupsFor(s, windowId).ids.length) return; // one loose tab and nothing to join: wait for company
502+ const ordered = [...fresh, ...loose.filter((t) => !fresh.includes(t))].map((t) => toMember(s, t));
397503 const batch = makeBatches(ordered)[0];
398− const maxGroups = maxGroupsFor(batch.length);
399− const payload: OrganizePayload = {
400− items: toItems(batch, settings),
401− existingGroups,
402− allowNew: batch.length >= 2,
403− maxGroups,
404− minGroupSize: settings.organizeMinGroupSize,
405− };
406− let reply = await rt.host<OrganizeResult>("organize", payload);
407− if (!reply.ok && reply.error.code === "OVER_BUDGET" && batch.length > 1) {
408− // Rare with the budgeted batch; try once with half, the rest follows on the next run.
409− const half = batch.slice(0, Math.ceil(batch.length / 2));
410− payload.items = toItems(half, settings);
411− payload.maxGroups = maxGroupsFor(half.length);
412− payload.allowNew = half.length >= 2;
413− batch.length = half.length;
414− reply = await rt.host<OrganizeResult>("organize", payload);
415− }
416− if (!reply.ok && reply.error.code === "BAD_MODEL_OUTPUT") reply = await rt.host<OrganizeResult>("organize", payload, { strict: true });
417− if (!reply.ok) {
418− rt.log("auto-organize", reply.error.code);
504+ const run = newRun(windowId, true);
505+ await sortPart(rt, batch, run);
506+ rt.commit();
507+ if (run.stopped) {
508+ rt.log("auto-organize", run.stopped.code);
419509 this.deps.scheduleFallback(rt.now() + AUTO_RETRY_MS);
420510 return;
421511 }
422− const batchIds = batch.map((m) => m.id);
423− const plan = planFromResult(reply.result, batchIds, existingIds, settings.organizeMinGroupSize, payload.maxGroups, (ids) =>
424− colorFor(batch.find((m) => m.id === ids[0])?.url ?? ""),
425− );
426− await applyPlan(rt, plan, windowId, batch, {}, []);
427− const live = rt.state();
428− for (const m of batch) {
429− const rec = live.tabs[m.id];
430− if (rec) rec.organizedKey = organizedKeyOf(m);
431− }
432− rt.commit();
433− const remaining = fresh.filter((t) => !batchIds.includes(t.id!));
434− if (remaining.length) this.touch(windowId, AUTO_CONTINUE_MS);
512+ const sent = new Set(batch.map((m) => m.id));
513+ if (fresh.some((t) => !sent.has(t.id!))) this.touch(windowId, AUTO_CONTINUE_MS);
435514 }
436515}
437516