-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompact.ts
More file actions
1119 lines (991 loc) · 32.1 KB
/
compact.ts
File metadata and controls
1119 lines (991 loc) · 32.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* trail compact command
*
* Compresses multiple trajectories into a single compacted summary.
* Useful for reducing context after PR merges by organizing similar
* decisions into grouped, understandable summaries.
*
* Default behavior: compact only trajectories that haven't been compacted yet.
*/
import { execFileSync } from "node:child_process";
import {
existsSync,
mkdirSync,
readFileSync,
unlinkSync,
writeFileSync,
} from "node:fs";
import { dirname, join } from "node:path";
import type { Command } from "commander";
import { getCompactionConfig } from "../../compact/config.js";
import { generateCompactionMarkdown } from "../../compact/markdown.js";
import {
type CompactedTrajectoryMetadata,
type LLMCompactedOutput,
mergeCompactionWithMetadata,
parseCompactionResponse,
} from "../../compact/parser.js";
import { buildCompactionPrompt } from "../../compact/prompts.js";
import {
AnthropicProvider,
CLIProvider,
type CompactionLLM,
type Message,
OpenAIProvider,
resolveProvider,
} from "../../compact/provider.js";
import { serializeForLLM } from "../../compact/serializer.js";
import { generateRandomId } from "../../core/id.js";
import type { Decision, Trajectory } from "../../core/types.js";
import { FileStorage, getSearchPaths } from "../../storage/file.js";
/**
* A group of related decisions
*/
interface DecisionGroup {
category: string;
decisions: Array<{
question: string;
chosen: string;
reasoning: string;
fromTrajectory: string;
}>;
}
/**
* Compacted trajectory summary — extends the shared metadata type from parser.ts
* with mechanical compaction fields and optional LLM output.
*/
interface CompactedTrajectory extends CompactedTrajectoryMetadata {
decisionGroups: DecisionGroup[];
keyLearnings: string[];
keyFindings: string[];
workflowId?: string;
narrative?: string;
decisions?: LLMCompactedOutput["decisions"];
conventions?: LLMCompactedOutput["conventions"];
lessons?: LLMCompactedOutput["lessons"];
openQuestions?: string[];
}
/**
* Index entry with compaction tracking
*/
interface IndexEntry {
title: string;
status: string;
startedAt: string;
completedAt?: string;
path: string;
compactedInto?: string;
}
interface TrajectoryIndex {
version: number;
lastUpdated: string;
trajectories: Record<string, IndexEntry>;
}
interface CompactCommandOptions {
since?: string;
until?: string;
ids?: string;
workflow?: string;
pr?: string;
branch?: string;
commits?: string;
all?: boolean;
llm?: boolean;
mechanical?: boolean;
focus?: string;
markdown?: boolean;
discardSources?: boolean;
dryRun?: boolean;
output?: string;
}
interface DiscardSourcesSummary {
removedIndexEntries: number;
deletedJsonFiles: number;
deletedMarkdownFiles: number;
deletedTraceFiles: number;
}
interface LLMCompactionPlan {
messages: Message[];
estimatedInputTokens: number;
estimatedOutputTokens: number;
focusAreas: string[];
}
export function registerCompactCommand(program: Command): void {
program
.command("compact")
.description(
"Compact trajectories into a summarized form (default: uncompacted only)",
)
.option(
"--since <date>",
"Include trajectories since this date (ISO format or relative like '7d')",
)
.option(
"--until <date>",
"Include trajectories until this date (ISO format)",
)
.option("--ids <ids>", "Comma-separated list of trajectory IDs to compact")
.option(
"--workflow <id>",
"Compact trajectories with the specified workflow ID",
)
.option("--pr <number>", "Compact trajectories associated with a PR number")
.option(
"--branch <name>",
"Compact trajectories with commits not in the specified branch (e.g., main)",
)
.option(
"--commits <shas>",
"Comma-separated commit SHAs to match trajectories against",
)
.option("--all", "Include all trajectories, even previously compacted ones")
.option("--llm", "Use LLM-based compaction when a provider is available")
.option("--no-llm", "Disable LLM-based compaction")
.option("--mechanical", "Force the original mechanical compaction flow")
.option(
"--focus <areas>",
"Comma-separated focus areas to emphasize in LLM compaction",
)
.option("--markdown", "Also write a Markdown companion file")
.option("--no-markdown", "Skip writing a Markdown companion file")
.option(
"--discard-sources",
"After saving the compaction, delete source trajectory JSON/MD/trace files and remove their index entries",
)
.option("--dry-run", "Preview what would be compacted without saving")
.option("--output <path>", "Output path for compacted trajectory")
.action(async (options: CompactCommandOptions) => {
const trajectories = await loadTrajectories(options);
if (trajectories.length === 0) {
if (
options.all ||
options.since ||
options.ids ||
options.workflow ||
options.pr ||
options.branch ||
options.commits
) {
console.log("No trajectories found matching criteria");
} else {
console.log(
"No uncompacted trajectories found. Use --all to include previously compacted.",
);
}
return;
}
console.log(`Compacting ${trajectories.length} trajectories...\n`);
const config = getCompactionConfig();
const provider = await resolveProvider(config);
const useLLM = shouldUseLLM(options, provider !== null);
const markdownEnabled = options.markdown !== false;
const mechanicalCompacted = compactTrajectories(
trajectories,
options.workflow,
);
if (!useLLM || provider === null) {
if (options.llm && provider === null && !options.mechanical) {
console.log(
"No LLM provider detected; falling back to mechanical compaction.\n",
);
}
if (options.dryRun) {
console.log("=== DRY RUN - Preview ===\n");
printCompactedSummary(mechanicalCompacted);
return;
}
const outputPath =
options.output ||
getDefaultOutputPath(mechanicalCompacted, options.workflow);
saveCompactionArtifacts(
mechanicalCompacted,
outputPath,
markdownEnabled,
);
if (options.discardSources) {
const discardSummary = discardSourceTrajectories(trajectories);
printDiscardSummary(discardSummary);
} else {
await markTrajectoriesAsCompacted(
trajectories,
mechanicalCompacted.id,
);
}
console.log(`\nCompacted trajectory saved to: ${outputPath}`);
if (markdownEnabled) {
console.log(
`Markdown summary saved to: ${getMarkdownOutputPath(outputPath)}`,
);
}
printCompactedSummary(mechanicalCompacted);
return;
}
const llmPlan = buildLLMCompactionPlan(
trajectories,
parseFocusAreas(options.focus),
config.maxInputTokens,
config.maxOutputTokens,
);
console.log(
`Using ${getProviderLabel(provider)} compaction${config.model ? ` with model ${config.model}` : ""}.`,
);
console.log(
`Estimated: ~${llmPlan.estimatedInputTokens} input tokens, ~${llmPlan.estimatedOutputTokens} output tokens`,
);
if (options.dryRun) {
printLLMDryRun(llmPlan, config.model, options.workflow);
return;
}
const llmOutput = await provider.complete(llmPlan.messages, {
maxTokens: config.maxOutputTokens,
temperature: config.temperature,
jsonMode: provider instanceof OpenAIProvider,
});
const llmCompacted = parseCompactionResponse(llmOutput);
const mergedCompaction = mergeCompactionWithMetadata(
{
id: mechanicalCompacted.id,
version: mechanicalCompacted.version,
type: mechanicalCompacted.type,
compactedAt: mechanicalCompacted.compactedAt,
sourceTrajectories: mechanicalCompacted.sourceTrajectories,
dateRange: mechanicalCompacted.dateRange,
summary: mechanicalCompacted.summary,
filesAffected: mechanicalCompacted.filesAffected,
commits: mechanicalCompacted.commits,
},
llmCompacted,
);
const compacted: CompactedTrajectory = {
...mechanicalCompacted,
...mergedCompaction,
};
const outputPath =
options.output || getDefaultOutputPath(compacted, options.workflow);
saveCompactionArtifacts(compacted, outputPath, markdownEnabled);
if (options.discardSources) {
const discardSummary = discardSourceTrajectories(trajectories);
printDiscardSummary(discardSummary);
} else {
await markTrajectoriesAsCompacted(trajectories, compacted.id);
}
console.log(`\nCompacted trajectory saved to: ${outputPath}`);
if (markdownEnabled) {
console.log(
`Markdown summary saved to: ${getMarkdownOutputPath(outputPath)}`,
);
}
printCompactedSummary(compacted);
});
}
async function loadTrajectories(options: {
since?: string;
until?: string;
ids?: string;
workflow?: string;
pr?: string;
branch?: string;
commits?: string;
all?: boolean;
}): Promise<Trajectory[]> {
const trajectories: Trajectory[] = [];
const targetIds = options.ids
? options.ids.split(",").map((s) => s.trim())
: null;
// Parse date filters
const sinceDate = options.since ? parseRelativeDate(options.since) : null;
const untilDate = options.until ? new Date(options.until) : null;
// Get commits on current branch not in target branch
const branchCommits = options.branch
? getBranchCommits(options.branch)
: null;
// Parse --commits into a set with both full and short forms
const targetCommits = options.commits
? new Set(
options.commits.split(",").flatMap((sha) => {
const trimmed = sha.trim();
if (!trimmed) return [];
return [trimmed, trimmed.slice(0, 7)];
}),
)
: null;
// Get compaction state from index
const compactedIds = options.all
? new Set<string>()
: getCompactedTrajectoryIds();
const searchPaths = getSearchPaths();
const seenIds = new Set<string>();
for (const searchPath of searchPaths) {
if (!existsSync(searchPath)) continue;
// Set env var only for the synchronous FileStorage constructor, then
// immediately restore to avoid leaking state across async boundaries.
const originalDataDir = process.env.TRAJECTORIES_DATA_DIR;
process.env.TRAJECTORIES_DATA_DIR = searchPath;
const storage = new FileStorage();
if (originalDataDir !== undefined) {
process.env.TRAJECTORIES_DATA_DIR = originalDataDir;
} else {
// biome-ignore lint/performance/noDelete: process.env requires delete to truly unset (assignment stores string "undefined")
delete process.env.TRAJECTORIES_DATA_DIR;
}
await storage.initialize();
const summaries = await storage.list({
status: "completed",
limit: Number.MAX_SAFE_INTEGER,
});
for (const summary of summaries) {
if (seenIds.has(summary.id)) continue;
// Skip already compacted (unless --all)
if (compactedIds.has(summary.id)) continue;
// Filter by IDs if specified
if (targetIds && !targetIds.includes(summary.id)) continue;
// Filter by date range
const startDate = new Date(summary.startedAt);
if (sinceDate && startDate < sinceDate) continue;
if (untilDate && startDate > untilDate) continue;
// Load full trajectory
const trajectory = await storage.get(summary.id);
if (trajectory) {
seenIds.add(summary.id);
// Filter by workflow if specified
if (options.workflow && trajectory.workflowId !== options.workflow) {
continue;
}
// Filter by PR if specified
if (options.pr) {
const escaped = options.pr.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
// Match "#N" or "PR #N" / "PR N" patterns, requiring word boundaries
// to avoid false matches on words containing "pr" (e.g., "Improve")
const prPattern = new RegExp(
`#${escaped}\\b|\\bPR\\s*#?\\s*${escaped}\\b`,
"i",
);
const matchesPR =
prPattern.test(trajectory.task.title) ||
prPattern.test(trajectory.task.description || "") ||
trajectory.commits.some((c) => prPattern.test(c));
if (!matchesPR) continue;
}
// Filter by branch if specified
if (branchCommits) {
const hasMatchingCommit = trajectory.commits.some(
(c) => branchCommits.has(c.slice(0, 7)) || branchCommits.has(c),
);
if (!hasMatchingCommit && trajectory.commits.length > 0) continue;
// Include trajectories with no commits (they might still be relevant)
}
// Filter by commits if specified
if (targetCommits) {
const hasMatchingCommit = trajectory.commits.some(
(c) => targetCommits.has(c) || targetCommits.has(c.slice(0, 7)),
);
if (!hasMatchingCommit) continue;
}
trajectories.push(trajectory);
}
}
}
return trajectories;
}
/**
* Get commits that are on the current branch but not in the target branch
*/
function getBranchCommits(targetBranch: string): Set<string> {
const commits = new Set<string>();
try {
// Get commits on HEAD that are not in target branch
const output = execFileSync(
"git",
["log", `${targetBranch}..HEAD`, "--format=%H"],
{
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
},
);
for (const line of output.trim().split("\n")) {
if (line) {
commits.add(line);
commits.add(line.slice(0, 7)); // Also add short hash
}
}
} catch {
// Not in a git repo or branch doesn't exist
console.warn(
`Warning: Could not get commits for branch comparison with ${targetBranch}`,
);
}
return commits;
}
/**
* Get IDs of trajectories that have already been compacted
*/
function getCompactedTrajectoryIds(): Set<string> {
const compacted = new Set<string>();
const searchPaths = getSearchPaths();
for (const searchPath of searchPaths) {
const indexPath = join(searchPath, "index.json");
if (!existsSync(indexPath)) continue;
try {
const indexContent = readFileSync(indexPath, "utf-8");
const index = JSON.parse(indexContent) as TrajectoryIndex;
for (const [id, entry] of Object.entries(index.trajectories || {})) {
if (entry.compactedInto) {
compacted.add(id);
}
}
} catch {
// Index doesn't exist or is malformed
}
}
return compacted;
}
/**
* Mark trajectories as having been compacted into a specific compaction
*/
async function markTrajectoriesAsCompacted(
trajectories: Trajectory[],
compactedIntoId: string,
): Promise<void> {
const searchPaths = getSearchPaths();
for (const searchPath of searchPaths) {
const indexPath = join(searchPath, "index.json");
if (!existsSync(indexPath)) continue;
try {
const indexContent = readFileSync(indexPath, "utf-8");
const index = JSON.parse(indexContent) as TrajectoryIndex;
let updated = false;
for (const traj of trajectories) {
if (index.trajectories[traj.id]) {
index.trajectories[traj.id].compactedInto = compactedIntoId;
updated = true;
}
}
if (updated) {
index.lastUpdated = new Date().toISOString();
writeFileSync(indexPath, JSON.stringify(index, null, 2));
}
} catch {
// Index doesn't exist or is malformed
}
}
}
/**
* Remove raw source trajectories after a durable compacted artifact has
* been written. This keeps compaction as the long-lived record and makes
* the index reflect only material that should remain visible.
*/
function discardSourceTrajectories(
trajectories: Trajectory[],
): DiscardSourcesSummary {
const sourceIds = new Set(trajectories.map((trajectory) => trajectory.id));
const summary: DiscardSourcesSummary = {
removedIndexEntries: 0,
deletedJsonFiles: 0,
deletedMarkdownFiles: 0,
deletedTraceFiles: 0,
};
for (const searchPath of getSearchPaths()) {
const indexPath = join(searchPath, "index.json");
if (!existsSync(indexPath)) continue;
let index: TrajectoryIndex;
try {
const indexContent = readFileSync(indexPath, "utf-8");
const parsedIndex = JSON.parse(indexContent) as unknown;
if (!isTrajectoryIndex(parsedIndex)) {
continue;
}
index = parsedIndex;
} catch {
// Keep behavior consistent with markTrajectoriesAsCompacted: malformed
// indexes are ignored instead of blocking an already-saved compaction.
continue;
}
let updated = false;
for (const id of sourceIds) {
const entry = index.trajectories[id];
if (!entry) continue;
if (deleteFileIfExists(entry.path)) {
summary.deletedJsonFiles += 1;
}
if (deleteFileIfExists(getMarkdownOutputPath(entry.path))) {
summary.deletedMarkdownFiles += 1;
}
if (deleteFileIfExists(getTraceOutputPath(entry.path))) {
summary.deletedTraceFiles += 1;
}
delete index.trajectories[id];
summary.removedIndexEntries += 1;
updated = true;
}
if (updated) {
index.lastUpdated = new Date().toISOString();
writeFileSync(indexPath, JSON.stringify(index, null, 2));
}
}
return summary;
}
function deleteFileIfExists(path: string): boolean {
if (!existsSync(path)) {
return false;
}
unlinkSync(path);
return true;
}
function isTrajectoryIndex(value: unknown): value is TrajectoryIndex {
if (value === null || typeof value !== "object") {
return false;
}
const candidate = value as Partial<TrajectoryIndex>;
return (
candidate.trajectories !== null &&
typeof candidate.trajectories === "object" &&
!Array.isArray(candidate.trajectories)
);
}
function getTraceOutputPath(outputPath: string): string {
return outputPath.endsWith(".json")
? outputPath.slice(0, -".json".length).concat(".trace.json")
: `${outputPath}.trace.json`;
}
function printDiscardSummary(summary: DiscardSourcesSummary): void {
console.log(
`Discarded source trajectories: ${summary.removedIndexEntries} index entries, ${summary.deletedJsonFiles} JSON files, ${summary.deletedMarkdownFiles} Markdown files, ${summary.deletedTraceFiles} trace files`,
);
}
function parseRelativeDate(input: string): Date {
// Handle relative dates like "7d", "2w", "1m"
const match = input.match(/^(\d+)([dwmh])$/);
if (match) {
const amount = Number.parseInt(match[1], 10);
const unit = match[2];
const now = new Date();
switch (unit) {
case "h":
return new Date(now.getTime() - amount * 60 * 60 * 1000);
case "d":
return new Date(now.getTime() - amount * 24 * 60 * 60 * 1000);
case "w":
return new Date(now.getTime() - amount * 7 * 24 * 60 * 60 * 1000);
case "m":
return new Date(now.getTime() - amount * 30 * 24 * 60 * 60 * 1000);
}
}
// Otherwise try to parse as ISO date
return new Date(input);
}
function compactTrajectories(
trajectories: Trajectory[],
workflowId?: string,
): CompactedTrajectory {
const allDecisions: Array<{
decision: Decision;
fromTrajectory: string;
timestamp: number;
}> = [];
const allLearnings: string[] = [];
const allFindings: string[] = [];
const allFiles = new Set<string>();
const allCommits = new Set<string>();
const allAgents = new Set<string>();
let totalEvents = 0;
// Extract data from all trajectories
for (const traj of trajectories) {
// Collect agents
for (const agent of traj.agents) {
allAgents.add(agent.name);
}
// Collect files and commits
for (const file of traj.filesChanged) {
allFiles.add(file);
}
for (const commit of traj.commits) {
allCommits.add(commit);
}
// Extract decisions from chapters
for (const chapter of traj.chapters) {
totalEvents += chapter.events.length;
for (const event of chapter.events) {
if (event.type === "decision" && event.raw) {
const decision = event.raw as Decision;
allDecisions.push({
decision,
fromTrajectory: traj.id,
timestamp: event.ts,
});
}
if (event.type === "finding" && event.content) {
allFindings.push(event.content);
}
}
}
// Extract learnings from retrospective
if (traj.retrospective?.learnings) {
allLearnings.push(...traj.retrospective.learnings);
}
// Also extract decisions from retrospective
if (traj.retrospective?.decisions) {
for (const decision of traj.retrospective.decisions) {
allDecisions.push({
decision,
fromTrajectory: traj.id,
timestamp: new Date(traj.completedAt || traj.startedAt).getTime(),
});
}
}
}
// Group decisions by category/topic
const decisionGroups = groupDecisions(allDecisions);
// Dedupe learnings
const uniqueLearnings = [...new Set(allLearnings)];
// Calculate date range
const dates = trajectories.map((t) => new Date(t.startedAt).getTime());
const minDate = new Date(Math.min(...dates));
const maxDate = new Date(
Math.max(
...trajectories.map((t) =>
new Date(t.completedAt || t.startedAt).getTime(),
),
),
);
return {
id: `compact_${generateRandomId()}`,
version: 1,
type: "compacted",
compactedAt: new Date().toISOString(),
workflowId,
sourceTrajectories: trajectories.map((t) => t.id),
dateRange: {
start: minDate.toISOString(),
end: maxDate.toISOString(),
},
summary: {
totalDecisions: allDecisions.length,
totalEvents,
uniqueAgents: [...allAgents],
},
decisionGroups,
keyLearnings: uniqueLearnings,
keyFindings: [...new Set(allFindings)],
filesAffected: [...allFiles],
commits: [...allCommits],
};
}
function groupDecisions(
decisions: Array<{
decision: Decision;
fromTrajectory: string;
timestamp: number;
}>,
): DecisionGroup[] {
// Simple categorization based on keywords in the question/reasoning
const categories: Record<string, DecisionGroup> = {};
const categoryKeywords: Record<string, string[]> = {
architecture: [
"architecture",
"structure",
"pattern",
"design",
"module",
"component",
],
api: ["api", "endpoint", "rest", "graphql", "http", "request", "response"],
database: ["database", "schema", "migration", "query", "sql", "model"],
testing: ["test", "spec", "coverage", "assertion", "mock"],
security: [
"security",
"auth",
"permission",
"token",
"credential",
"encrypt",
],
performance: ["performance", "optimize", "cache", "speed", "memory"],
tooling: ["tool", "config", "build", "lint", "format", "ci", "cd"],
naming: ["name", "rename", "convention", "format"],
compliance: ["spec", "standard", "compliance", "convention", "align"],
};
for (const { decision, fromTrajectory } of decisions) {
const text = `${decision.question} ${decision.reasoning}`.toLowerCase();
let matchedCategory = "other";
for (const [category, keywords] of Object.entries(categoryKeywords)) {
if (keywords.some((kw) => text.includes(kw))) {
matchedCategory = category;
break;
}
}
if (!categories[matchedCategory]) {
categories[matchedCategory] = {
category: matchedCategory,
decisions: [],
};
}
categories[matchedCategory].decisions.push({
question: decision.question,
chosen: decision.chosen,
reasoning: decision.reasoning,
fromTrajectory,
});
}
// Sort categories by number of decisions
return Object.values(categories).sort(
(a, b) => b.decisions.length - a.decisions.length,
);
}
function shouldUseLLM(
options: Pick<CompactCommandOptions, "llm" | "mechanical">,
providerAvailable: boolean,
): boolean {
if (options.mechanical) {
return false;
}
if (options.llm === false) {
return false;
}
if (options.llm === true) {
return providerAvailable;
}
return providerAvailable;
}
function buildLLMCompactionPlan(
trajectories: Trajectory[],
focusAreas: string[],
maxInputTokens: number,
maxOutputTokens: number,
): LLMCompactionPlan {
const serialized = serializeForLLM(trajectories, maxInputTokens);
const messages = buildCompactionPrompt(serialized, {
focusAreas,
maxOutputTokens,
});
return {
messages,
estimatedInputTokens: estimateTokens(
messages.map((message) => message.content).join("\n\n"),
),
estimatedOutputTokens: maxOutputTokens,
focusAreas,
};
}
function parseFocusAreas(focus?: string): string[] {
if (!focus) {
return [];
}
return focus
.split(",")
.map((area) => area.trim())
.filter(Boolean);
}
function estimateTokens(text: string): number {
return Math.max(1, Math.ceil(text.length / 4));
}
function printLLMDryRun(
plan: LLMCompactionPlan,
model: string | undefined,
workflowId?: string,
): void {
console.log("=== DRY RUN - LLM Prompt Preview ===\n");
console.log(
`Estimated: ~${plan.estimatedInputTokens} input tokens, ~${plan.estimatedOutputTokens} output tokens`,
);
if (model) {
console.log(`Configured model: ${model}`);
}
if (workflowId) {
console.log(`Workflow: ${workflowId}`);
}
if (plan.focusAreas.length > 0) {
console.log(`Focus: ${plan.focusAreas.join(", ")}`);
}
console.log("");
for (const message of plan.messages) {
console.log(`[${message.role.toUpperCase()}]`);
console.log(message.content);
console.log("");
}
}
function getProviderLabel(provider: CompactionLLM): string {
if (provider instanceof OpenAIProvider) {
return "OpenAI";
}
if (provider instanceof AnthropicProvider) {
return "Anthropic";
}
if (provider instanceof CLIProvider) {
return `CLI (${provider.cliName})`;
}
return "LLM";
}
function getDefaultOutputPath(
compacted: CompactedTrajectory,
workflowId?: string,
): string {
const trajDir = process.env.TRAJECTORIES_DATA_DIR || ".trajectories";
const compactedDir = join(trajDir, "compacted");
if (!existsSync(compactedDir)) {
mkdirSync(compactedDir, { recursive: true });
}
if (workflowId) {
return join(compactedDir, `workflow-${workflowId}.json`);
}
const dateStr = new Date().toISOString().slice(0, 10);
return join(compactedDir, `${compacted.id}_${dateStr}.json`);
}
function saveCompactionArtifacts(
compacted: CompactedTrajectory,
outputPath: string,
markdownEnabled: boolean,
): void {
const dir = dirname(outputPath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(outputPath, JSON.stringify(compacted, null, 2));
if (markdownEnabled) {
writeFileSync(
getMarkdownOutputPath(outputPath),
renderCompactionMarkdown(compacted),
);
}
}
function getMarkdownOutputPath(outputPath: string): string {
return outputPath.endsWith(".json")
? outputPath.slice(0, -".json".length).concat(".md")
: `${outputPath}.md`;
}
function renderCompactionMarkdown(compacted: CompactedTrajectory): string {
if (compacted.narrative) {
return generateCompactionMarkdown(
compacted as Parameters<typeof generateCompactionMarkdown>[0],
);
}
const decisionGroups =
compacted.decisionGroups.length > 0
? compacted.decisionGroups
.map((group) => {
const decisions =
group.decisions.length > 0
? group.decisions
.map(
(decision) =>
`- ${decision.question} -> ${decision.chosen} (${decision.fromTrajectory})`,
)
.join("\n")
: "- None";
return `## ${capitalize(group.category)}\n${decisions}`;
})
.join("\n\n")
: "## Decision Groups\n- None";
const learnings =
compacted.keyLearnings.length > 0
? compacted.keyLearnings.map((learning) => `- ${learning}`).join("\n")
: "- None";