Skip to content

Commit 8cb02e5

Browse files
committed
fix(cli): align sessions cleanup label summary by visible width
1 parent b83a966 commit 8cb02e5

2 files changed

Lines changed: 76 additions & 4 deletions

File tree

‎src/commands/sessions-cleanup.test.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Sessions cleanup tests cover stale session cleanup and runtime output.
22
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import { visibleWidth } from "../../packages/terminal-core/src/ansi.js";
34
import type { SessionEntry } from "../config/sessions.js";
45
import type { RuntimeEnv } from "../runtime.js";
56

@@ -643,6 +644,76 @@ describe("sessionsCleanupCommand", () => {
643644
expectLogsToInclude(logs, "Total: 3 kept, 4 pruned");
644645
});
645646

647+
it("aligns the label summary columns for emoji and CJK labels", async () => {
648+
mocks.enforceSessionDiskBudget.mockResolvedValue(null);
649+
mocks.runSessionsCleanup.mockResolvedValue({
650+
mode: "warn",
651+
previewResults: [
652+
{
653+
summary: {
654+
agentId: "main",
655+
storePath: "/resolved/sessions.json",
656+
mode: "warn",
657+
dryRun: true,
658+
beforeCount: 2,
659+
afterCount: 2,
660+
missing: 0,
661+
dmScopeRetired: 0,
662+
pruned: 0,
663+
capped: 0,
664+
unreferencedArtifacts: {
665+
scannedFiles: 0,
666+
removedFiles: 0,
667+
freedBytes: 0,
668+
olderThanMs: 604800000,
669+
},
670+
diskBudget: null,
671+
wouldMutate: true,
672+
},
673+
beforeStore: {
674+
emojiKept: {
675+
sessionId: "emoji-kept",
676+
updatedAt: 2,
677+
model: "test:opus",
678+
label: "🔥修复",
679+
},
680+
plainKept: {
681+
sessionId: "plain-kept",
682+
updatedAt: 1,
683+
model: "test:opus",
684+
label: "plain",
685+
},
686+
},
687+
missingKeys: new Set<string>(),
688+
staleKeys: new Set<string>(),
689+
cappedKeys: new Set<string>(),
690+
budgetEvictedKeys: new Set<string>(),
691+
dmScopeRetiredKeys: new Set<string>(),
692+
},
693+
],
694+
appliedSummaries: [],
695+
});
696+
697+
const { runtime, logs } = makeRuntime();
698+
await sessionsCleanupCommand(
699+
{
700+
dryRun: true,
701+
},
702+
runtime,
703+
);
704+
705+
expectLogsToInclude(logs, "Summary by Label:");
706+
const summaryLogs = logs.slice(logs.indexOf("Summary by Label:") + 1);
707+
const emojiLine = summaryLogs.find((line) => line.includes("🔥修复"));
708+
const plainLine = summaryLogs.find((line) => line.includes("plain"));
709+
expect(emojiLine).toBeDefined();
710+
expect(plainLine).toBeDefined();
711+
// "🔥修复" is 6 visible columns (wide emoji + 2 CJK) but only 5 UTF-16 code
712+
// units; padding by code-unit length would shift the counts column left.
713+
const keptColumn = (line: string) => visibleWidth(line.slice(0, line.indexOf("1 kept")));
714+
expect(keptColumn(emojiLine ?? "")).toBe(keptColumn(plainLine ?? ""));
715+
});
716+
646717
it("returns grouped JSON for --all-agents dry-runs", async () => {
647718
mocks.resolveSessionStoreTargets.mockReturnValue([
648719
{ agentId: "main", storePath: "/resolved/main-sessions.json" },

‎src/commands/sessions-cleanup.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* It can delegate cleanup to a live gateway or run local store maintenance,
55
* with dry-run tables that explain every planned pruning action.
66
*/
7+
import { visibleWidth } from "../../packages/terminal-core/src/ansi.js";
78
import { sanitizeTerminalText } from "../../packages/terminal-core/src/safe-text.js";
89
import { isRich, theme } from "../../packages/terminal-core/src/theme.js";
910
import { getRuntimeConfig } from "../config/config.js";
@@ -129,15 +130,15 @@ function renderLabelSummaries(params: {
129130
if (summaries.length === 0) {
130131
return;
131132
}
132-
const labelPad = Math.max(...summaries.map((summary) => summary.label.length));
133+
const labelPad = Math.max(...summaries.map((summary) => visibleWidth(summary.label)));
133134
const totalKept = summaries.reduce((total, summary) => total + summary.kept, 0);
134135
const totalPruned = summaries.reduce((total, summary) => total + summary.pruned, 0);
135136
params.runtime.log("");
136137
params.runtime.log("Summary by Label:");
137138
for (const summary of summaries) {
138-
params.runtime.log(
139-
`${summary.label.padEnd(labelPad)} ${summary.kept} kept, ${summary.pruned} pruned`,
140-
);
139+
const remaining = labelPad - visibleWidth(summary.label);
140+
const paddedLabel = remaining > 0 ? `${summary.label}${" ".repeat(remaining)}` : summary.label;
141+
params.runtime.log(`${paddedLabel} ${summary.kept} kept, ${summary.pruned} pruned`);
141142
}
142143
params.runtime.log(`Total: ${totalKept} kept, ${totalPruned} pruned`);
143144
}

0 commit comments

Comments
 (0)