@scrylog/cli 0.1.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 (129) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +836 -0
  3. package/package.json +75 -0
  4. package/src/api/docs/sessions.keys.ts +6 -0
  5. package/src/api/docs/sessions.ts +81 -0
  6. package/src/api/docs/system.keys.ts +5 -0
  7. package/src/api/docs/system.ts +66 -0
  8. package/src/api/docs/usage.keys.ts +3 -0
  9. package/src/api/docs/usage.ts +24 -0
  10. package/src/api/docs/webhooks.keys.ts +5 -0
  11. package/src/api/docs/webhooks.ts +78 -0
  12. package/src/api/index.ts +18 -0
  13. package/src/api/lib/auth.ts +43 -0
  14. package/src/api/lib/errors.ts +28 -0
  15. package/src/api/lib/route.ts +18 -0
  16. package/src/api/models/claude.ts +4 -0
  17. package/src/api/models/codex.ts +4 -0
  18. package/src/api/models/opencode.ts +11 -0
  19. package/src/api/routes/sessions.ts +47 -0
  20. package/src/api/routes/system.ts +103 -0
  21. package/src/api/routes/usage.ts +15 -0
  22. package/src/api/routes/webhooks/claude.ts +161 -0
  23. package/src/api/routes/webhooks/codex.ts +175 -0
  24. package/src/api/routes/webhooks/opencode.ts +107 -0
  25. package/src/api/routes.ts +93 -0
  26. package/src/api/schemas/common.ts +21 -0
  27. package/src/api/schemas/sessions.ts +72 -0
  28. package/src/api/schemas/usage.ts +50 -0
  29. package/src/api/schemas/webhooks/claude.ts +32 -0
  30. package/src/api/schemas/webhooks/codex.ts +34 -0
  31. package/src/api/schemas/webhooks/opencode.ts +29 -0
  32. package/src/api/services/claude-session.ts +9 -0
  33. package/src/api/services/codex-session.ts +8 -0
  34. package/src/api/services/opencode-session.ts +73 -0
  35. package/src/classifier.ts +206 -0
  36. package/src/cli/commands.ts +498 -0
  37. package/src/cli/prompt.ts +44 -0
  38. package/src/daemon/cli.ts +27 -0
  39. package/src/daemon/collectors/claude/README.md +108 -0
  40. package/src/daemon/collectors/claude/collector.ts +413 -0
  41. package/src/daemon/collectors/claude/discovery.ts +148 -0
  42. package/src/daemon/collectors/claude/hooks.ts +181 -0
  43. package/src/daemon/collectors/claude/index.ts +8 -0
  44. package/src/daemon/collectors/claude/jsonl.ts +85 -0
  45. package/src/daemon/collectors/claude/transcript.ts +246 -0
  46. package/src/daemon/collectors/codex/README.md +147 -0
  47. package/src/daemon/collectors/codex/collector.ts +661 -0
  48. package/src/daemon/collectors/codex/discovery.ts +104 -0
  49. package/src/daemon/collectors/codex/hooks.ts +179 -0
  50. package/src/daemon/collectors/codex/index.ts +7 -0
  51. package/src/daemon/collectors/codex/jsonl.ts +108 -0
  52. package/src/daemon/collectors/codex/transcript.ts +299 -0
  53. package/src/daemon/collectors/git.ts +295 -0
  54. package/src/daemon/collectors/opencode/README.md +106 -0
  55. package/src/daemon/collectors/opencode/collector.ts +543 -0
  56. package/src/daemon/collectors/opencode/discovery.ts +257 -0
  57. package/src/daemon/collectors/opencode/index.ts +5 -0
  58. package/src/daemon/collectors/opencode/session-builder.ts +59 -0
  59. package/src/daemon/collectors/opencode-db.ts +282 -0
  60. package/src/daemon/collectors/process.ts +201 -0
  61. package/src/daemon/collectors/tmux/README.md +128 -0
  62. package/src/daemon/collectors/tmux/collector.ts +472 -0
  63. package/src/daemon/collectors/tmux/enrichment.ts +241 -0
  64. package/src/daemon/collectors/tmux/index.ts +4 -0
  65. package/src/daemon/collectors/tmux/parse.ts +128 -0
  66. package/src/daemon/collectors/usage.ts +193 -0
  67. package/src/daemon/index.ts +207 -0
  68. package/src/daemon/scheduler.ts +86 -0
  69. package/src/daemon/store.ts +535 -0
  70. package/src/detect.ts +143 -0
  71. package/src/hooks/claude.ts +596 -0
  72. package/src/hooks/codex.ts +517 -0
  73. package/src/legacy/classifier.ts +215 -0
  74. package/src/legacy/detect.ts +143 -0
  75. package/src/mcp/client-server.ts +21 -0
  76. package/src/mcp/http-handler.ts +126 -0
  77. package/src/mcp/index.ts +61 -0
  78. package/src/mcp/resources/config-current.ts +19 -0
  79. package/src/mcp/resources/index.ts +11 -0
  80. package/src/mcp/resources/sessions-active.ts +19 -0
  81. package/src/mcp/resources/sessions-all.ts +19 -0
  82. package/src/mcp/schemas/sessions.ts +3 -0
  83. package/src/mcp/server.ts +17 -0
  84. package/src/mcp/services/api-adapter.ts +48 -0
  85. package/src/mcp/services/data-source.ts +22 -0
  86. package/src/mcp/services/store-adapter.ts +40 -0
  87. package/src/mcp/tools/get-active-sessions.ts +16 -0
  88. package/src/mcp/tools/get-session-children.ts +24 -0
  89. package/src/mcp/tools/get-session-messages.ts +24 -0
  90. package/src/mcp/tools/get-session.ts +23 -0
  91. package/src/mcp/tools/index.ts +15 -0
  92. package/src/mcp/tools/list-sessions.ts +16 -0
  93. package/src/neo-blessed.d.ts +4 -0
  94. package/src/oc/index.ts +302 -0
  95. package/src/service/manager.ts +242 -0
  96. package/src/shared/config.ts +478 -0
  97. package/src/shared/ipc.ts +80 -0
  98. package/src/shared/logger.ts +248 -0
  99. package/src/shared/paths.ts +73 -0
  100. package/src/shared/platform.ts +110 -0
  101. package/src/shared/session-view.ts +241 -0
  102. package/src/shared/types.ts +299 -0
  103. package/src/shared/utils.ts +339 -0
  104. package/src/tui/app.ts +920 -0
  105. package/src/tui/client.ts +149 -0
  106. package/src/tui/index.ts +181 -0
  107. package/src/types.ts +113 -0
  108. package/src/usage/README.md +323 -0
  109. package/src/usage/api.ts +99 -0
  110. package/src/usage/client.ts +3 -0
  111. package/src/usage/clients/anthropic-oauth-usage.ts +178 -0
  112. package/src/usage/clients/anthropic-web-usage.ts +245 -0
  113. package/src/usage/clients/claude-cli-probe.ts +187 -0
  114. package/src/usage/clients/codex-oauth-usage.ts +113 -0
  115. package/src/usage/clients/opencode-go-usage.ts +441 -0
  116. package/src/usage/index.ts +19 -0
  117. package/src/usage/mappers.ts +273 -0
  118. package/src/usage/planner.ts +54 -0
  119. package/src/usage/providers/claude.ts +113 -0
  120. package/src/usage/providers/codex.ts +34 -0
  121. package/src/usage/providers/opencode-go.ts +37 -0
  122. package/src/usage/resolver.ts +4 -0
  123. package/src/usage/resolvers/browser-cookies.ts +198 -0
  124. package/src/usage/resolvers/claude-auth.ts +99 -0
  125. package/src/usage/resolvers/claude-cli.ts +24 -0
  126. package/src/usage/resolvers/claude-web.ts +16 -0
  127. package/src/usage/resolvers/codex-auth.ts +117 -0
  128. package/src/usage/resolvers/opencode-go-web.ts +14 -0
  129. package/src/usage/types.ts +190 -0
@@ -0,0 +1,32 @@
1
+ import { z } from "@hono/zod-openapi";
2
+
3
+ // Minimal validation: only 'sessionId' is essential. All other fields are optional
4
+ // and extra fields pass through transparently (.passthrough()) so future changes
5
+ // to the Claude hook format don't cause unexpected rejections.
6
+ export const ClaudeWebhookBodySchema = z
7
+ .object({
8
+ sessionId: z.string().openapi({ example: "abc-123-def" }),
9
+ eventType: z.string().optional().openapi({ example: "PreToolUse" }),
10
+ eventTime: z.number().optional(),
11
+ hookSeq: z.number().optional(),
12
+ state: z.enum(["working", "idle", "waiting-input", "error", "shell", "unknown"]).optional(),
13
+ hasAttention: z.boolean().optional(),
14
+ attentionKind: z.enum(["permission", "question", "turn", "none"]).optional(),
15
+ summary: z.string().optional(),
16
+ activeTool: z.string().optional(),
17
+ toolName: z.string().optional(),
18
+ toolInputSummary: z.string().optional(),
19
+ toolResultStatus: z.enum(["started", "completed", "error", "blocked", "denied", "unknown"]).optional(),
20
+ permissionMode: z.string().optional(),
21
+ startSource: z.string().optional(),
22
+ endReason: z.string().optional(),
23
+ errorType: z.string().optional(),
24
+ agentId: z.string().optional(),
25
+ agentType: z.string().optional(),
26
+ transcriptPath: z.string().optional(),
27
+ currentPath: z.string().optional(),
28
+ lastActivity: z.number().optional(),
29
+ parentSessionId: z.string().optional(),
30
+ detectionSource: z.literal("claude-hook").optional(),
31
+ })
32
+ .passthrough();
@@ -0,0 +1,34 @@
1
+ import { z } from "@hono/zod-openapi";
2
+
3
+ // Minimal validation: only 'sessionId' is essential. All other fields are optional
4
+ // and extra fields pass through transparently (.passthrough()) so future changes
5
+ // to the Codex hook format don't cause unexpected rejections.
6
+ export const CodexWebhookBodySchema = z
7
+ .object({
8
+ sessionId: z.string().openapi({ example: "abc-123-def" }),
9
+ eventType: z.string().optional().openapi({ example: "PreToolUse" }),
10
+ eventTime: z.number().optional(),
11
+ hookSeq: z.number().optional(),
12
+ state: z.enum(["working", "idle", "waiting-input", "error", "shell", "unknown"]).optional(),
13
+ hasAttention: z.boolean().optional(),
14
+ attentionKind: z.enum(["permission", "question", "turn", "none"]).optional(),
15
+ summary: z.string().optional(),
16
+ activeTool: z.string().optional(),
17
+ model: z.string().optional(),
18
+ toolName: z.string().optional(),
19
+ toolInputSummary: z.string().optional(),
20
+ toolResultStatus: z.enum(["started", "completed", "error", "blocked", "denied", "unknown"]).optional(),
21
+ permissionMode: z.string().optional(),
22
+ startSource: z.string().optional(),
23
+ endReason: z.string().optional(),
24
+ transcriptPath: z.string().optional(),
25
+ currentPath: z.string().optional(),
26
+ lastActivity: z.number().optional(),
27
+ parentSessionId: z.string().optional(),
28
+ forkedFromId: z.string().optional(),
29
+ turnId: z.string().optional(),
30
+ agentId: z.string().optional(),
31
+ agentType: z.string().optional(),
32
+ detectionSource: z.literal("codex-hook").optional(),
33
+ })
34
+ .passthrough();
@@ -0,0 +1,29 @@
1
+ import { z } from "@hono/zod-openapi";
2
+
3
+ // Minimal validation: only 'event' is essential. All other fields are optional
4
+ // and extra fields pass through transparently (.passthrough()) so future changes
5
+ // to the OpenCode hook format don't cause unexpected rejections.
6
+ export const OpencodeWebhookBodySchema = z
7
+ .object({
8
+ event: z.string().openapi({ example: "session.status" }),
9
+ sessionID: z.string().optional().openapi({ example: "ses_abc123" }),
10
+ directory: z.string().optional().openapi({ example: "/home/user/project" }),
11
+ title: z.string().optional().openapi({ example: "My session" }),
12
+ permissionID: z.string().optional(),
13
+ info: z
14
+ .object({
15
+ id: z.string().optional(),
16
+ title: z.string().optional(),
17
+ directory: z.string().optional(),
18
+ parentID: z.string().optional(),
19
+ time: z
20
+ .object({
21
+ created: z.number().optional(),
22
+ updated: z.number().optional(),
23
+ })
24
+ .optional(),
25
+ })
26
+ .optional(),
27
+ status: z.object({ type: z.string().optional() }).optional(),
28
+ })
29
+ .passthrough();
@@ -0,0 +1,9 @@
1
+ export const STATE_BEARING_EVENTS = new Set([
2
+ "SessionStart", "UserPromptSubmit", "PreToolUse", "PermissionRequest",
3
+ "PermissionDenied", "PostToolUse", "PostToolUseFailure", "PostToolBatch",
4
+ "Stop", "StopFailure", "SubagentStart", "SubagentStop", "SessionEnd",
5
+ ]);
6
+
7
+ export function isMetadataOnlyEvent(eventType: string): boolean {
8
+ return !STATE_BEARING_EVENTS.has(eventType);
9
+ }
@@ -0,0 +1,8 @@
1
+ export const STATE_BEARING_EVENTS = new Set([
2
+ "SessionStart", "UserPromptSubmit", "PreToolUse", "PermissionRequest",
3
+ "PostToolUse", "SubagentStart", "SubagentStop", "Stop",
4
+ ]);
5
+
6
+ export function isMetadataOnlyEvent(eventType: string): boolean {
7
+ return !STATE_BEARING_EVENTS.has(eventType);
8
+ }
@@ -0,0 +1,73 @@
1
+ import type { SessionModel } from "../../shared/types";
2
+ import type { OpencodeWebhookPayload, HookStateResult } from "../models/opencode";
3
+
4
+ export function mapHookState(
5
+ event: string,
6
+ payload: OpencodeWebhookPayload,
7
+ existing?: SessionModel,
8
+ ): HookStateResult {
9
+ const prevCount = (existing?.opencode?.pendingAttentionCount ?? 0);
10
+ let nextCount = prevCount;
11
+
12
+ if (event === "permission.updated" || event === "permission.asked" || event === "question.asked") {
13
+ nextCount = prevCount + 1;
14
+ } else if (event === "permission.replied" || event === "question.replied") {
15
+ nextCount = Math.max(0, prevCount - 1);
16
+ }
17
+
18
+ const hasAttention = nextCount > 0;
19
+
20
+ if (event === "session.error") {
21
+ return { state: "error", hasAttention: false, pendingAttentionCount: 0 };
22
+ }
23
+
24
+ if (hasAttention) {
25
+ return { state: "waiting-input", hasAttention: true, pendingAttentionCount: nextCount };
26
+ }
27
+
28
+ if (event === "session.status" && payload.status?.type) {
29
+ return {
30
+ state: (payload.status.type === "busy" || payload.status.type === "retry") ? "working" : "idle",
31
+ hasAttention: false,
32
+ pendingAttentionCount: nextCount,
33
+ };
34
+ }
35
+
36
+ if (event === "session.idle") {
37
+ return { state: "idle", hasAttention: false, pendingAttentionCount: nextCount };
38
+ }
39
+
40
+ if (event === "permission.replied" || event === "question.replied") {
41
+ return { state: existing?.state === "waiting-input" ? "idle" : (existing?.state ?? "idle"), hasAttention: false, pendingAttentionCount: nextCount };
42
+ }
43
+
44
+ return {
45
+ state: existing?.state ?? "idle",
46
+ hasAttention,
47
+ pendingAttentionCount: nextCount,
48
+ };
49
+ }
50
+
51
+ export function extractSessionParentId(
52
+ event: string,
53
+ payload: OpencodeWebhookPayload,
54
+ existing?: SessionModel,
55
+ ): string | undefined {
56
+ if (event !== "session.created" && event !== "session.updated") {
57
+ return existing?.parentSessionId;
58
+ }
59
+
60
+ const parentID = payload.info?.parentID;
61
+ if (!parentID) {
62
+ return existing?.parentSessionId;
63
+ }
64
+
65
+ // Only treat real session ids as parent sessions. Other event payloads may
66
+ // include message-part ancestry such as msg_* ids, which would incorrectly
67
+ // reclassify a root session as a hidden child row.
68
+ if (!parentID.startsWith("ses_")) {
69
+ return existing?.parentSessionId;
70
+ }
71
+
72
+ return `opencode:${parentID}`;
73
+ }
@@ -0,0 +1,206 @@
1
+ import { detectTool, getPreviewMatchPositions, previewSignals } from "./detect";
2
+ import type {
3
+ DetectionReason,
4
+ EnrichedPaneRecord,
5
+ PaneModel,
6
+ PaneSample,
7
+ PaneState,
8
+ ProcessInfo,
9
+ SnapshotHistory,
10
+ ToolKind,
11
+ } from "./types";
12
+ import { cleanSummaryText, compactWhitespace, normalizeCommandName, pickCompletionTail, pickMeaningfulTail, pickQuestionTail, pickSummaryTail } from "./shared/utils";
13
+
14
+ const DEBOUNCE_THRESHOLD = 2;
15
+
16
+ export function classifyPane(input: {
17
+ pane: EnrichedPaneRecord;
18
+ processTree: ProcessInfo[];
19
+ sample: PaneSample | null;
20
+ history?: SnapshotHistory;
21
+ now: number;
22
+ }): { pane: PaneModel; history: SnapshotHistory } {
23
+ const { pane, processTree, sample, history, now } = input;
24
+ const detectedTool = detectTool(pane.currentCommand, processTree);
25
+ const rawState = detectState(detectedTool.tool, pane.currentCommand, processTree, sample);
26
+
27
+ const fingerprint = [
28
+ detectedTool.tool,
29
+ rawState.state,
30
+ processTree.map((process) => `${process.pid}:${process.comm}`).join(","),
31
+ sample?.preview ?? "",
32
+ ].join("|");
33
+
34
+ const lastChangedAt = history?.lastChangedAt ?? now;
35
+ const stableCount = history?.lastFingerprint === fingerprint ? (history.stableCount + 1) : 1;
36
+
37
+ let tool: ToolKind = detectedTool.tool;
38
+ let state: PaneState = rawState.state;
39
+ let stateReasons = rawState.reasons;
40
+ const shouldBypassDebounce = rawState.state === "working"
41
+ && rawState.reasons.some((reason) => (
42
+ reason.label === "activity keywords"
43
+ || reason.label === "opencode footer activity"
44
+ || reason.label === "active agent signals"
45
+ ));
46
+
47
+ if (history && stableCount < DEBOUNCE_THRESHOLD && !shouldBypassDebounce) {
48
+ tool = history.tool;
49
+ state = history.state;
50
+ stateReasons = [
51
+ {
52
+ label: "debounced previous state",
53
+ detail: `holding ${history.state} until new signal stabilizes`,
54
+ },
55
+ ...rawState.reasons,
56
+ ];
57
+ }
58
+
59
+ const historyRecord: SnapshotHistory = {
60
+ tool,
61
+ state,
62
+ lastFingerprint: fingerprint,
63
+ lastChangedAt: ((stableCount === DEBOUNCE_THRESHOLD && history?.state !== state) || (shouldBypassDebounce && stableCount === 1)) ? now : lastChangedAt,
64
+ stableCount,
65
+ };
66
+
67
+ const processChain = processTree.map((process) => compactWhitespace(process.comm || normalizeCommandName(process.command)));
68
+ const summary = summarizeState(state, stateReasons, sample, processChain, pane.currentCommand);
69
+
70
+ return {
71
+ pane: {
72
+ ...pane,
73
+ processTree,
74
+ processChain,
75
+ sample,
76
+ tool,
77
+ state,
78
+ toolReasons: detectedTool.reasons,
79
+ stateReasons,
80
+ summary,
81
+ lastChangedAt: historyRecord.lastChangedAt,
82
+ stableForMs: Math.max(0, now - historyRecord.lastChangedAt),
83
+ hasAttention: state === "waiting-input",
84
+ },
85
+ history: historyRecord,
86
+ };
87
+ }
88
+
89
+ export function detectState(tool: ToolKind, currentCommand: string, processTree: ProcessInfo[], sample: PaneSample | null): { state: PaneState; reasons: DetectionReason[] } {
90
+ const reasons: DetectionReason[] = [];
91
+ const promptSignals = previewSignals(sample);
92
+ const previewMatches = getPreviewMatchPositions(sample);
93
+ const processStates = processTree.map((process) => process.state).join(",");
94
+ const commands = [currentCommand, ...processTree.map((process) => process.command)].join(" ");
95
+ const meaningfulPreview = sample ? pickMeaningfulTail(sample.lines, sample.preview) : "";
96
+
97
+ if (tool === "shell") {
98
+ reasons.push({ label: "shell pane", detail: compactWhitespace(currentCommand || processTree[0]?.command || "shell") });
99
+ return { state: "shell", reasons };
100
+ }
101
+
102
+ if (tool === "unknown") {
103
+ reasons.push({ label: "insufficient classification signals", detail: compactWhitespace(commands) || "unknown workload" });
104
+ return { state: "unknown", reasons };
105
+ }
106
+
107
+ const activityReason = promptSignals.find((reason) => reason.label === "activity keywords");
108
+ const footerReason = promptSignals.find((reason) => reason.label === "opencode footer activity");
109
+ const waitingReason = promptSignals.find((reason) => {
110
+ if (reason.label !== "input prompt keywords" && reason.label !== "prompt-like tail") {
111
+ return false;
112
+ }
113
+
114
+ return !/^>\s*implement\s*\{feature\}/i.test(reason.detail);
115
+ });
116
+ const completionReason = promptSignals.find((reason) => reason.label === "completion keywords");
117
+ const activeKeywords = /(thinking|generating|executing|applying|running|streaming|analyzing|diff|patch|searching|reading)/i;
118
+ const strongWorkingSignal = Boolean(
119
+ (activityReason && /(working\s*\(|background terminal running|waited for background terminal|created\s+\d+)/i.test(activityReason.detail))
120
+ || (tool === "opencode" && footerReason),
121
+ );
122
+ const hasRunnableProcessSignal = processTree.some((process) => /[RU]/.test(process.state));
123
+ const hasActiveProcessSignal = hasRunnableProcessSignal;
124
+ const hasFreshActivitySignal = Boolean(
125
+ previewMatches.activityIndex >= Math.max(previewMatches.promptIndex, previewMatches.completionIndex)
126
+ && (
127
+ (activityReason && (cleanSummaryText(activityReason.detail) === meaningfulPreview || activeKeywords.test(meaningfulPreview)))
128
+ || (tool === "opencode" && footerReason)
129
+ ),
130
+ );
131
+
132
+ if (completionReason && !hasActiveProcessSignal && previewMatches.completionIndex >= previewMatches.activityIndex) {
133
+ reasons.push({
134
+ label: "completed work detected",
135
+ detail: completionReason.detail,
136
+ });
137
+ return { state: waitingReason ? "waiting-input" : "idle", reasons: waitingReason ? [...reasons, waitingReason] : reasons };
138
+ }
139
+
140
+ if (strongWorkingSignal || hasFreshActivitySignal) {
141
+ if (footerReason && tool === "opencode") {
142
+ reasons.push(footerReason);
143
+ } else if (activityReason) {
144
+ reasons.push(activityReason);
145
+ }
146
+ return { state: "working", reasons };
147
+ }
148
+
149
+ if (waitingReason) {
150
+ reasons.push(waitingReason);
151
+ return { state: "waiting-input", reasons };
152
+ }
153
+
154
+ if (hasActiveProcessSignal) {
155
+ reasons.push(footerReason ?? activityReason ?? { label: "active agent signals", detail: meaningfulPreview || processStates || compactWhitespace(commands) });
156
+ return { state: "working", reasons };
157
+ }
158
+
159
+ reasons.push({ label: "agent present without active signals", detail: meaningfulPreview || compactWhitespace(commands) || tool });
160
+ return { state: "idle", reasons };
161
+ }
162
+
163
+ function summarizeState(
164
+ state: PaneState,
165
+ stateReasons: DetectionReason[],
166
+ sample: PaneSample | null,
167
+ processChain: string[],
168
+ currentCommand: string,
169
+ ): string {
170
+ const primaryReason = stateReasons.find((reason) => reason.label !== "debounced previous state")?.detail;
171
+ const hasCompletionReason = stateReasons.some((reason) => reason.label === "completed work detected" || reason.label === "completion keywords");
172
+ const preferredSummary = state === "waiting-input"
173
+ ? pickQuestionTail(sample?.lines ?? [], primaryReason ?? "")
174
+ : hasCompletionReason
175
+ ? pickCompletionTail(sample?.lines ?? [], primaryReason ?? "")
176
+ : pickSummaryTail(sample?.lines ?? [], primaryReason ?? "");
177
+
178
+ if (primaryReason && primaryReason.length > 0 && primaryReason === preferredSummary) {
179
+ return primaryReason;
180
+ }
181
+
182
+ if (sample?.lines.length) {
183
+ const meaningful = state === "waiting-input"
184
+ ? pickQuestionTail(sample.lines, sample.preview)
185
+ : hasCompletionReason
186
+ ? pickCompletionTail(sample.lines, sample.preview)
187
+ : pickSummaryTail(sample.lines, sample.preview);
188
+ if (meaningful) {
189
+ return meaningful;
190
+ }
191
+ }
192
+
193
+ if (sample?.preview) {
194
+ return sample.preview;
195
+ }
196
+
197
+ if (processChain.length > 0) {
198
+ return processChain.join(" -> ");
199
+ }
200
+
201
+ if (currentCommand) {
202
+ return currentCommand;
203
+ }
204
+
205
+ return state;
206
+ }