File size: 40,044 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { randomUUID } from 'node:crypto';
import { constants as fsConstants } from 'node:fs';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as Diff from 'diff';
import type { Config } from '../config/config.js';
import { Storage } from '../config/storage.js';
import { flattenMemory } from '../config/memory.js';
import { loadSkillFromFile, loadSkillsFromDir } from '../skills/skillLoader.js';
import {
  type AppliedSkillPatchTarget,
  type InboxMemoryPatchKind,
  applyParsedPatchesWithAllowedRoots,
  applyParsedSkillPatches,
  findDisallowedMemoryPatchTarget,
  getInboxMemoryPatchSourcePath,
  getMemoryPatchTargetValidationContext,
  isResolvedMemoryPatchTargetAllowed,
  hasParsedPatchHunks,
  isProjectSkillPatchTarget,
  listInboxPatchFiles,
  listValidInboxPatchFiles,
  normalizeInboxMemoryPatchPath,
  resolveMemoryPatchTargetWithinAllowedSet,
  validateParsedSkillPatchHeaders,
} from '../services/memoryPatchUtils.js';
import { readExtractionState } from '../services/memoryService.js';
import type { MessageActionReturn } from './types.js';

export type { InboxMemoryPatchKind } from '../services/memoryPatchUtils.js';
export { getAllowedMemoryPatchRoots } from '../services/memoryPatchUtils.js';

export function showMemory(config: Config): MessageActionReturn {
  const memoryContent = flattenMemory(config.getUserMemory());
  const fileCount = config.getGeminiMdFileCount() || 0;
  let content: string;

  if (memoryContent.length > 0) {
    content = `Current memory content from ${fileCount} file(s):\n\n---\n${memoryContent}\n---`;
  } else {
    content = 'Memory is currently empty.';
  }

  return {
    type: 'message',
    messageType: 'info',
    content,
  };
}

export async function refreshMemory(
  config: Config,
): Promise<MessageActionReturn> {
  await config.getMemoryContextManager()?.refresh();
  const memoryContent = flattenMemory(config.getUserMemory());
  const fileCount = config.getGeminiMdFileCount();

  config.updateSystemInstructionIfInitialized();
  let content: string;

  if (memoryContent.length > 0) {
    content = `Memory reloaded successfully. Loaded ${memoryContent.length} characters from ${fileCount} file(s)`;
  } else {
    content = 'Memory reloaded successfully. No memory content found';
  }

  return {
    type: 'message',
    messageType: 'info',
    content,
  };
}

export function listMemoryFiles(config: Config): MessageActionReturn {
  const filePaths = config.getGeminiMdFilePaths() || [];
  const fileCount = filePaths.length;
  let content: string;

  if (fileCount > 0) {
    content = `There are ${fileCount} GEMINI.md file(s) in use:\n\n${filePaths.join(
      '\n',
    )}`;
  } else {
    content = 'No GEMINI.md files in use.';
  }

  return {
    type: 'message',
    messageType: 'info',
    content,
  };
}

/**
 * Represents a skill found in the extraction inbox.
 */
export interface InboxSkill {
  /** Directory name in the inbox. */
  dirName: string;
  /** Skill name from SKILL.md frontmatter. */
  name: string;
  /** Skill description from SKILL.md frontmatter. */
  description: string;
  /** Raw SKILL.md content for preview. */
  content: string;
  /** When the skill was extracted (ISO string), if known. */
  extractedAt?: string;
}

/**
 * Scans the skill extraction inbox and returns structured data
 * for each extracted skill.
 */
export async function listInboxSkills(config: Config): Promise<InboxSkill[]> {
  const skillsDir = config.storage.getProjectSkillsMemoryDir();

  let entries: Array<import('node:fs').Dirent>;
  try {
    entries = await fs.readdir(skillsDir, { withFileTypes: true });
  } catch {
    return [];
  }

  const dirs = entries.filter((e) => e.isDirectory());
  if (dirs.length === 0) {
    return [];
  }

  // Load extraction state to get dates
  const memoryDir = config.storage.getProjectMemoryTempDir();
  const statePath = path.join(memoryDir, '.extraction-state.json');
  const state = await readExtractionState(statePath);

  // Build a map: skillDirName → extractedAt
  const skillDateMap = new Map<string, string>();
  for (const run of state.runs) {
    for (const skillName of run.skillsCreated) {
      skillDateMap.set(skillName, run.runAt);
    }
  }

  const skills: InboxSkill[] = [];
  for (const dir of dirs) {
    const skillPath = path.join(skillsDir, dir.name, 'SKILL.md');
    const skillDef = await loadSkillFromFile(skillPath);
    if (!skillDef) continue;

    let content = '';
    try {
      content = await fs.readFile(skillPath, 'utf-8');
    } catch {
      // Best-effort — preview will be empty
    }

    skills.push({
      dirName: dir.name,
      name: skillDef.name,
      description: skillDef.description,
      content,
      extractedAt: skillDateMap.get(dir.name),
    });
  }

  return skills;
}

export type InboxSkillDestination = 'global' | 'project';

function isValidInboxSkillDirName(dirName: string): boolean {
  return (
    dirName.length > 0 &&
    dirName !== '.' &&
    dirName !== '..' &&
    !dirName.includes('/') &&
    !dirName.includes('\\')
  );
}

function isValidInboxPatchFileName(fileName: string): boolean {
  return (
    fileName.length > 0 &&
    fileName !== '.' &&
    fileName !== '..' &&
    !fileName.includes('/') &&
    !fileName.includes('\\')
  );
}

async function getSkillNameForConflictCheck(
  skillDir: string,
  fallbackName: string,
): Promise<string> {
  const skill = await loadSkillFromFile(path.join(skillDir, 'SKILL.md'));
  return skill?.name ?? fallbackName;
}

/**
 * Copies an inbox skill to the target skills directory.
 */
export async function moveInboxSkill(
  config: Config,
  dirName: string,
  destination: InboxSkillDestination,
): Promise<{ success: boolean; message: string }> {
  if (!isValidInboxSkillDirName(dirName)) {
    return {
      success: false,
      message: 'Invalid skill name.',
    };
  }

  const skillsDir = config.storage.getProjectSkillsMemoryDir();
  const sourcePath = path.join(skillsDir, dirName);

  try {
    await fs.access(sourcePath);
  } catch {
    return {
      success: false,
      message: `Skill "${dirName}" not found in inbox.`,
    };
  }

  const targetBase =
    destination === 'global'
      ? Storage.getUserSkillsDir()
      : config.storage.getProjectSkillsDir();
  const targetPath = path.join(targetBase, dirName);
  const skillName = await getSkillNameForConflictCheck(sourcePath, dirName);

  try {
    await fs.access(targetPath);
    return {
      success: false,
      message: `A skill named "${skillName}" already exists in ${destination} skills.`,
    };
  } catch {
    // Target doesn't exist — good
  }

  const existingTargetSkills = await loadSkillsFromDir(targetBase);
  if (existingTargetSkills.some((skill) => skill.name === skillName)) {
    return {
      success: false,
      message: `A skill named "${skillName}" already exists in ${destination} skills.`,
    };
  }

  await fs.mkdir(targetBase, { recursive: true });
  await fs.cp(sourcePath, targetPath, { recursive: true });

  // Remove from inbox after successful copy
  await fs.rm(sourcePath, { recursive: true, force: true });

  const label =
    destination === 'global' ? '~/.gemini/skills' : '.gemini/skills';
  return {
    success: true,
    message: `Moved "${dirName}" to ${label}.`,
  };
}

/**
 * Removes a skill from the extraction inbox.
 */
export async function dismissInboxSkill(
  config: Config,
  dirName: string,
): Promise<{ success: boolean; message: string }> {
  if (!isValidInboxSkillDirName(dirName)) {
    return {
      success: false,
      message: 'Invalid skill name.',
    };
  }

  const skillsDir = config.storage.getProjectSkillsMemoryDir();
  const sourcePath = path.join(skillsDir, dirName);

  try {
    await fs.access(sourcePath);
  } catch {
    return {
      success: false,
      message: `Skill "${dirName}" not found in inbox.`,
    };
  }

  await fs.rm(sourcePath, { recursive: true, force: true });

  return {
    success: true,
    message: `Dismissed "${dirName}" from inbox.`,
  };
}

/**
 * A parsed patch entry from a unified diff, representing changes to a single file.
 */
export interface InboxPatchEntry {
  /** Absolute path to the target file (or '/dev/null' for new files). */
  targetPath: string;
  /** The unified diff text for this single file. */
  diffContent: string;
}

/**
 * Represents a .patch file found in the extraction inbox.
 */
export interface InboxPatch {
  /** The .patch filename (e.g. "update-docs-writer.patch"). */
  fileName: string;
  /** Display name (filename without .patch extension). */
  name: string;
  /** Per-file entries parsed from the patch. */
  entries: InboxPatchEntry[];
  /** When the patch was extracted (ISO string), if known. */
  extractedAt?: string;
}

/**
 * One target file inside a memory patch (most patches will have a single entry).
 */
export interface InboxMemoryPatchEntry {
  /** Absolute path of the markdown file the patch will modify. */
  targetPath: string;
  /** Unified diff for this single file (used for UI preview). */
  diffContent: string;
  /** True when this entry creates a new file (`/dev/null` source). */
  isNewFile: boolean;
}

/**
 * Represents the AGGREGATED inbox state for one memory kind. Even when the
 * extraction agent has produced multiple `.patch` files under
 * `<memoryDir>/.inbox/<kind>/` (e.g. across several sessions), the inbox
 * surfaces them as ONE entry per kind. Apply runs each underlying patch in
 * sequence; Dismiss removes them all.
 */
export interface InboxMemoryPatch {
  /** Memory tier — one entry per kind in the inbox. */
  kind: InboxMemoryPatchKind;
  /**
   * Stable identifier for this consolidated entry. Set to the kind itself
   * (`"private"` or `"global"`); kept in the type for backwards-compat with
   * the per-file API the dialog passes through.
   */
  relativePath: string;
  /** Display name shown in the inbox row (e.g. `"Private memory"`). */
  name: string;
  /** All hunks from all underlying source patches, concatenated in order. */
  entries: InboxMemoryPatchEntry[];
  /** Basenames of the underlying `.patch` files being aggregated. */
  sourceFiles: string[];
  /** Most recent mtime across the source files (ISO string), if known. */
  extractedAt?: string;
}

interface StagedInboxPatchTarget {
  targetPath: string;
  tempPath: string;
  original: string;
  isNewFile: boolean;
  mode?: number;
}

/**
 * Reconstructs a unified diff string for a single ParsedDiff entry.
 */
function formatParsedDiff(parsed: Diff.StructuredPatch): string {
  const lines: string[] = [];
  if (parsed.oldFileName) {
    lines.push(`--- ${parsed.oldFileName}`);
  }
  if (parsed.newFileName) {
    lines.push(`+++ ${parsed.newFileName}`);
  }
  for (const hunk of parsed.hunks) {
    lines.push(
      `@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@`,
    );
    for (const line of hunk.lines) {
      lines.push(line);
    }
  }
  return lines.join('\n');
}

function getErrorMessage(error: unknown): string {
  return error instanceof Error ? error.message : String(error);
}

async function getFileMtimeIso(filePath: string): Promise<string | undefined> {
  try {
    const stats = await fs.stat(filePath);
    return stats.mtime.toISOString();
  } catch {
    return undefined;
  }
}

async function patchTargetsProjectSkills(
  targetPaths: string[],
  config: Config,
) {
  for (const targetPath of targetPaths) {
    if (await isProjectSkillPatchTarget(targetPath, config)) {
      return true;
    }
  }
  return false;
}

async function getPatchExtractedAt(
  patchPath: string,
): Promise<string | undefined> {
  try {
    const stats = await fs.stat(patchPath);
    return stats.mtime.toISOString();
  } catch {
    return undefined;
  }
}

function formatMemoryKindLabel(kind: InboxMemoryPatchKind): string {
  switch (kind) {
    case 'private':
      return 'Private memory';
    case 'global':
      return 'Global memory';
    default:
      return kind;
  }
}

/**
 * Scans `<memoryDir>/.inbox/{private,global}/` and returns ONE consolidated
 * inbox entry per kind. Each entry aggregates all hunks from every valid
 * underlying `.patch` file. Patches that fail validation (unparseable, no
 * hunks, target outside the allowed target set) are silently skipped so they
 * don't pollute the inbox UI.
 */
export async function listInboxMemoryPatches(
  config: Config,
): Promise<InboxMemoryPatch[]> {
  const kinds: InboxMemoryPatchKind[] = ['private', 'global'];
  const aggregated: InboxMemoryPatch[] = [];

  for (const kind of kinds) {
    const validationContext = await getMemoryPatchTargetValidationContext(
      config,
      kind,
    );
    const patchFiles = await listInboxPatchFiles(config, kind);

    const aggregatedEntries: InboxMemoryPatchEntry[] = [];
    const sourceFiles: string[] = [];
    let latestMtime: string | undefined;

    for (const sourcePath of patchFiles) {
      let content: string;
      try {
        content = await fs.readFile(sourcePath, 'utf-8');
      } catch {
        continue;
      }

      let parsed: Diff.StructuredPatch[];
      try {
        parsed = Diff.parsePatch(content);
      } catch {
        continue;
      }
      if (!hasParsedPatchHunks(parsed)) {
        continue;
      }

      const validated = validateParsedSkillPatchHeaders(parsed);
      if (!validated.success) {
        continue;
      }

      // Skip the entire source file if ANY of its targets escapes the kind's
      // allowed target set.
      const targetsAllAllowed = await Promise.all(
        validated.patches.map(
          async (header) =>
            (await resolveMemoryPatchTargetWithinAllowedSet(
              header.targetPath,
              validationContext,
            )) !== undefined,
        ),
      );
      if (!targetsAllAllowed.every(Boolean)) {
        continue;
      }

      for (const [index, header] of validated.patches.entries()) {
        aggregatedEntries.push({
          targetPath: header.targetPath,
          isNewFile: header.isNewFile,
          diffContent: formatParsedDiff(parsed[index]),
        });
      }

      sourceFiles.push(path.basename(sourcePath));

      const mtime = await getFileMtimeIso(sourcePath);
      if (mtime && (!latestMtime || mtime > latestMtime)) {
        latestMtime = mtime;
      }
    }

    if (aggregatedEntries.length === 0) {
      continue;
    }

    aggregated.push({
      kind,
      relativePath: kind,
      name: formatMemoryKindLabel(kind),
      entries: aggregatedEntries,
      sourceFiles,
      extractedAt: latestMtime,
    });
  }

  return aggregated;
}

/**
 * Applies an inbox memory patch atomically and removes the patch on success.
 *
 * Process:
 *   1. Parse + validate the patch headers (absolute paths only, no `a/`/`b/`).
 *   2. Dry-run the patch against the current target content (or empty for
 *      `/dev/null` creation patches).
 *   3. Stage the patched content to a temp file, then rename into place.
 *   4. On any failure, restore previous content from the staged snapshot and
 *      leave the inbox patch intact for retry.
 */
/**
 * Applies one inbox memory entry. Two modes:
 *   - Aggregate mode (`relativePath === kind`): walk every `.patch` file in
 *     the kind's inbox directory and apply each one in lexical order. Each
 *     file is its own atomic transaction; failures don't block subsequent
 *     successes. Returns an aggregated summary (e.g. "Applied 3 of 4 sub-
 *     patches; 1 failed: …").
 *   - Single-file mode (legacy): `relativePath` points at a specific
 *     `.patch` filename. Used by tests and direct callers.
 */
export async function applyInboxMemoryPatch(
  config: Config,
  kind: InboxMemoryPatchKind,
  relativePath: string,
): Promise<{ success: boolean; message: string }> {
  if (relativePath === kind) {
    return applyAllInboxPatchesForKind(config, kind);
  }

  const normalizedPath = normalizeInboxMemoryPatchPath(relativePath);
  if (!normalizedPath) {
    return { success: false, message: 'Invalid memory patch path.' };
  }

  const sourcePath = await getInboxMemoryPatchSourcePath(
    config,
    kind,
    normalizedPath,
  );
  if (!sourcePath) {
    return { success: false, message: 'Invalid memory patch path.' };
  }

  return applyMemoryPatchFile(config, kind, sourcePath, normalizedPath);
}

async function applyAllInboxPatchesForKind(
  config: Config,
  kind: InboxMemoryPatchKind,
): Promise<{ success: boolean; message: string }> {
  // Only attempt patches the user actually saw in the inbox listing.
  // Files that were filtered (bad headers, escape allowed root, etc.) stay
  // on disk untouched.
  const patchFiles = await listValidInboxPatchFiles(config, kind);
  if (patchFiles.length === 0) {
    return {
      success: false,
      message: `No ${kind} memory patches in inbox.`,
    };
  }

  const successes: string[] = [];
  const failures: Array<{ name: string; reason: string }> = [];
  let pointersAddedAcrossPatches: string[] = [];

  for (const sourcePath of patchFiles) {
    const basename = path.basename(sourcePath);
    const result = await applyMemoryPatchFile(
      config,
      kind,
      sourcePath,
      basename,
    );
    if (result.success) {
      successes.push(basename);
      // Surface auto-added MEMORY.md pointer info if present.
      const pointerMatch = result.message.match(
        /Auto-added MEMORY\.md pointer for ([^.]+)\./,
      );
      if (pointerMatch) {
        pointersAddedAcrossPatches.push(pointerMatch[1]);
      }
    } else {
      failures.push({ name: basename, reason: result.message });
    }
  }

  // De-dup pointer notes (same sibling could have been mentioned twice).
  pointersAddedAcrossPatches = Array.from(new Set(pointersAddedAcrossPatches));

  const total = successes.length + failures.length;
  if (failures.length === 0) {
    const pointerNote =
      pointersAddedAcrossPatches.length > 0
        ? ` Auto-added MEMORY.md pointer(s) for ${pointersAddedAcrossPatches.join('; ')}.`
        : '';
    return {
      success: true,
      message: `Applied all ${successes.length} ${kind} memory patch${
        successes.length === 1 ? '' : 'es'
      }.${pointerNote}`,
    };
  }

  const failureSummary = failures
    .map((f) => `"${f.name}" — ${f.reason}`)
    .join('; ');
  // Any failure → success=false so the dialog keeps the inbox entry visible
  // (the user needs to see and retry/dismiss the remaining sub-patches).
  // The successful sub-patches have already been removed from disk by
  // applyMemoryPatchFile, so the next listing will show only the failures.
  return {
    success: false,
    message:
      `Applied ${successes.length} of ${total} ${kind} memory patches. ` +
      `${failures.length} failed: ${failureSummary}`,
  };
}

async function canonicalizeDirIfPresent(dirPath: string): Promise<string> {
  try {
    return await fs.realpath(dirPath);
  } catch {
    return path.resolve(dirPath);
  }
}

/**
 * Returns the basenames of any sibling .md files (not MEMORY.md itself) that
 * are being CREATED by this patch under `<memoryDir>/` directly.
 */
function findSiblingCreations(
  appliedResults: readonly AppliedSkillPatchTarget[],
  memoryDir: string,
): AppliedSkillPatchTarget[] {
  return appliedResults.filter((entry) => {
    if (!entry.isNewFile) return false;
    const targetDir = path.dirname(path.resolve(entry.targetPath));
    if (targetDir !== memoryDir) return false;
    const basename = path.basename(entry.targetPath);
    if (basename.toLowerCase() === 'memory.md') return false;
    return basename.toLowerCase().endsWith('.md');
  });
}

interface AutoPointerAugmentation {
  /** Patch results, possibly with a synthesized/extended MEMORY.md entry. */
  results: AppliedSkillPatchTarget[];
  /** Sibling basenames a pointer was auto-added for (empty if none). */
  pointersAdded: string[];
}

/**
 * MEMORY.md is the index that gets injected into future agent contexts.
 * Sibling .md files in `<memoryDir>/` are loaded ON DEMAND by the runtime
 * agent via `read_file` — but only IF MEMORY.md references them by name
 * (see `getUserProjectMemoryPaths`).
 *
 * If a private patch creates a sibling without also referencing it from
 * MEMORY.md, the new file would never be discoverable. Rather than rejecting
 * the patch (bad UX), we auto-bundle a MEMORY.md update that adds a
 * one-line pointer per orphan sibling. The augmented entry is then committed
 * atomically alongside the rest of the patch.
 *
 * If the patch already updates/creates MEMORY.md and the new content already
 * references the sibling, no augmentation is needed.
 */
async function augmentWithAutoPointers(
  config: Config,
  appliedResults: readonly AppliedSkillPatchTarget[],
): Promise<AutoPointerAugmentation> {
  const memoryDir = await canonicalizeDirIfPresent(
    config.storage.getProjectMemoryTempDir(),
  );
  const memoryMdPath = path.join(memoryDir, 'MEMORY.md');

  const siblingCreations = findSiblingCreations(appliedResults, memoryDir);
  if (siblingCreations.length === 0) {
    return { results: [...appliedResults], pointersAdded: [] };
  }

  // Locate (or initialize) the MEMORY.md entry we'll mutate.
  const existingIdx = appliedResults.findIndex(
    (entry) => path.resolve(entry.targetPath) === memoryMdPath,
  );
  let memoryEntry: AppliedSkillPatchTarget;
  if (existingIdx >= 0) {
    memoryEntry = { ...appliedResults[existingIdx] };
  } else {
    let originalContent = '';
    let isNewFile = true;
    try {
      originalContent = await fs.readFile(memoryMdPath, 'utf-8');
      isNewFile = false;
    } catch {
      // MEMORY.md doesn't exist yet — we'll create it with a default heading.
    }
    memoryEntry = {
      targetPath: memoryMdPath,
      original: originalContent,
      patched: isNewFile ? '# Project Memory\n' : originalContent,
      isNewFile,
    };
  }

  const pointersAdded: string[] = [];
  for (const sibling of siblingCreations) {
    const basename = path.basename(sibling.targetPath);
    // Resolve to absolute path so the runtime agent can `read_file` the
    // sibling directly without needing to know <memoryDir>.
    const absoluteTarget = path.resolve(sibling.targetPath);
    // Existing reference can be by either basename or absolute path; both count.
    if (
      memoryEntry.patched.includes(basename) ||
      memoryEntry.patched.includes(absoluteTarget)
    ) {
      continue; // Already referenced.
    }
    const stem = basename.replace(/\.md$/i, '').replace(/[-_]/g, ' ').trim();
    const pointer = `- See ${absoluteTarget} for ${stem || basename} notes.`;
    memoryEntry.patched = memoryEntry.patched.endsWith('\n')
      ? `${memoryEntry.patched}${pointer}\n`
      : `${memoryEntry.patched}\n${pointer}\n`;
    pointersAdded.push(basename);
  }

  if (pointersAdded.length === 0) {
    return { results: [...appliedResults], pointersAdded: [] };
  }

  const results = [...appliedResults];
  if (existingIdx >= 0) {
    results[existingIdx] = memoryEntry;
  } else {
    results.push(memoryEntry);
  }
  return { results, pointersAdded };
}

/**
 * Internal helper: parses, validates, and atomically commits a memory patch
 * file at a known absolute path. Separated from `applyInboxMemoryPatch` so the
 * path-resolution and patch-apply concerns stay testable independently.
 */
async function applyMemoryPatchFile(
  config: Config,
  kind: InboxMemoryPatchKind,
  patchPath: string,
  displayName: string,
): Promise<{ success: boolean; message: string }> {
  let content: string;
  try {
    content = await fs.readFile(patchPath, 'utf-8');
  } catch {
    return {
      success: false,
      message: `Memory patch "${displayName}" not found in inbox.`,
    };
  }

  let parsed: Diff.StructuredPatch[];
  try {
    parsed = Diff.parsePatch(content);
  } catch (error) {
    return {
      success: false,
      message: `Failed to parse memory patch "${displayName}": ${getErrorMessage(error)}`,
    };
  }
  if (!hasParsedPatchHunks(parsed)) {
    return {
      success: false,
      message: `Memory patch "${displayName}" contains no valid hunks.`,
    };
  }

  const validationContext = await getMemoryPatchTargetValidationContext(
    config,
    kind,
  );
  const disallowedTargetPath = await findDisallowedMemoryPatchTarget(
    parsed,
    validationContext,
  );
  if (disallowedTargetPath) {
    return {
      success: false,
      message: `Memory patch "${displayName}" targets a file outside the ${kind} memory root or target allowlist: ${disallowedTargetPath}`,
    };
  }

  const applied = await applyParsedPatchesWithAllowedRoots(
    parsed,
    validationContext.allowedRoots,
    {
      isResolvedTargetAllowed: (resolvedTargetPath) =>
        isResolvedMemoryPatchTargetAllowed(
          resolvedTargetPath,
          validationContext,
        ),
    },
  );
  if (!applied.success) {
    switch (applied.reason) {
      case 'missingTargetPath':
        return {
          success: false,
          message: `Memory patch "${displayName}" is missing a target file path.`,
        };
      case 'invalidPatchHeaders':
        return {
          success: false,
          message: `Memory patch "${displayName}" has invalid diff headers.`,
        };
      case 'outsideAllowedRoots':
        return {
          success: false,
          message: `Memory patch "${displayName}" targets a file outside the ${kind} memory root or target allowlist: ${applied.targetPath}`,
        };
      case 'newFileAlreadyExists':
        return {
          success: false,
          message: `Memory patch "${displayName}" declares a new file, but the target already exists: ${applied.targetPath}`,
        };
      case 'targetNotFound':
        return {
          success: false,
          message: `Target file not found: ${applied.targetPath}`,
        };
      case 'doesNotApply':
        return {
          success: false,
          message: applied.isNewFile
            ? `Memory patch "${displayName}" failed to apply for new file ${applied.targetPath}.`
            : `Memory patch does not apply cleanly to ${applied.targetPath}.`,
        };
      default:
        return {
          success: false,
          message: `Memory patch "${displayName}" could not be applied.`,
        };
    }
  }

  // Auto-bundle a MEMORY.md pointer for any sibling .md the patch creates
  // without referencing it from MEMORY.md. Without that pointer the new file
  // would never be loaded into a future session (see augmentWithAutoPointers).
  let pointersAdded: string[] = [];
  let resultsToCommit: AppliedSkillPatchTarget[] = [...applied.results];
  if (kind === 'private') {
    const augmented = await augmentWithAutoPointers(config, applied.results);
    resultsToCommit = augmented.results;
    pointersAdded = augmented.pointersAdded;
  }

  let stagedTargets: StagedInboxPatchTarget[];
  try {
    stagedTargets = await stageInboxPatchTargets(resultsToCommit);
  } catch (error) {
    return {
      success: false,
      message: `Memory patch "${displayName}" could not be staged: ${getErrorMessage(error)}.`,
    };
  }

  const committedTargets: StagedInboxPatchTarget[] = [];
  try {
    for (const stagedTarget of stagedTargets) {
      await fs.rename(stagedTarget.tempPath, stagedTarget.targetPath);
      committedTargets.push(stagedTarget);
    }
  } catch (error) {
    for (const committedTarget of committedTargets.reverse()) {
      try {
        await restoreCommittedInboxPatchTarget(committedTarget);
      } catch {
        // Best-effort rollback. We still report the commit failure below.
      }
    }
    await cleanupStagedInboxPatchTargets(
      stagedTargets.filter((target) => !committedTargets.includes(target)),
    );
    return {
      success: false,
      message: `Memory patch "${displayName}" could not be applied atomically: ${getErrorMessage(error)}.`,
    };
  }

  await fs.unlink(patchPath);

  const fileCount = resultsToCommit.length;
  const baseMessage = `Applied memory patch to ${fileCount} file${fileCount !== 1 ? 's' : ''}.`;
  const pointerNote =
    pointersAdded.length > 0
      ? ` Auto-added MEMORY.md pointer for ${pointersAdded
          .map((name) => `"${name}"`)
          .join(', ')} so the new sibling file is discoverable.`
      : '';
  return {
    success: true,
    message: `${baseMessage}${pointerNote}`,
  };
}

/**
 * Removes inbox memory patch(es) without applying. Two modes:
 *   - Aggregate (`relativePath === kind`): unlink every `.patch` file in the
 *     kind's inbox directory. Used by the consolidated inbox UI's Dismiss.
 *   - Single-file (legacy): unlink one specific `.patch` file.
 */
export async function dismissInboxMemoryPatch(
  config: Config,
  kind: InboxMemoryPatchKind,
  relativePath: string,
): Promise<{ success: boolean; message: string }> {
  if (relativePath === kind) {
    // Dismiss the same set of files the listing surfaced — leave the
    // already-filtered (bad-target, malformed) files alone for forensic
    // inspection.
    const patchFiles = await listValidInboxPatchFiles(config, kind);
    if (patchFiles.length === 0) {
      return {
        success: false,
        message: `No ${kind} memory patches in inbox.`,
      };
    }
    let removed = 0;
    for (const sourcePath of patchFiles) {
      try {
        await fs.unlink(sourcePath);
        removed += 1;
      } catch {
        // Best-effort: keep going if one delete fails.
      }
    }
    return {
      success: removed > 0,
      message: `Dismissed ${removed} ${kind} memory patch${
        removed === 1 ? '' : 'es'
      } from inbox.`,
    };
  }

  const normalizedPath = normalizeInboxMemoryPatchPath(relativePath);
  if (!normalizedPath) {
    return { success: false, message: 'Invalid memory patch path.' };
  }

  const sourcePath = await getInboxMemoryPatchSourcePath(
    config,
    kind,
    normalizedPath,
  );
  if (!sourcePath) {
    return { success: false, message: 'Invalid memory patch path.' };
  }

  try {
    await fs.access(sourcePath);
  } catch {
    return {
      success: false,
      message: `Memory patch "${normalizedPath}" not found in inbox.`,
    };
  }

  await fs.unlink(sourcePath);

  return {
    success: true,
    message: `Dismissed "${normalizedPath}" from inbox.`,
  };
}

async function findNearestExistingDirectory(
  startPath: string,
): Promise<string> {
  let currentPath = path.resolve(startPath);

  while (true) {
    try {
      const stats = await fs.stat(currentPath);
      if (stats.isDirectory()) {
        return currentPath;
      }
    } catch {
      // Keep walking upward until we find an existing directory.
    }

    const parentPath = path.dirname(currentPath);
    if (parentPath === currentPath) {
      return currentPath;
    }
    currentPath = parentPath;
  }
}

async function writeExclusiveFile(
  filePath: string,
  content: string,
  mode?: number,
): Promise<void> {
  const handle = await fs.open(filePath, 'wx');
  try {
    await handle.writeFile(content, 'utf-8');
  } finally {
    await handle.close();
  }

  if (mode !== undefined) {
    await fs.chmod(filePath, mode);
  }
}

async function cleanupStagedInboxPatchTargets(
  stagedTargets: StagedInboxPatchTarget[],
): Promise<void> {
  await Promise.allSettled(
    stagedTargets.map(async ({ tempPath }) => {
      try {
        await fs.unlink(tempPath);
      } catch {
        // Best-effort cleanup.
      }
    }),
  );
}

async function restoreCommittedInboxPatchTarget(
  stagedTarget: StagedInboxPatchTarget,
): Promise<void> {
  if (stagedTarget.isNewFile) {
    try {
      await fs.unlink(stagedTarget.targetPath);
    } catch {
      // Best-effort rollback.
    }
    return;
  }

  const restoreDir = await findNearestExistingDirectory(
    path.dirname(stagedTarget.targetPath),
  );
  const restorePath = path.join(
    restoreDir,
    `.${path.basename(stagedTarget.targetPath)}.${randomUUID()}.rollback`,
  );

  await writeExclusiveFile(
    restorePath,
    stagedTarget.original,
    stagedTarget.mode,
  );
  await fs.rename(restorePath, stagedTarget.targetPath);
}

async function stageInboxPatchTargets(
  targets: AppliedSkillPatchTarget[],
): Promise<StagedInboxPatchTarget[]> {
  const stagedTargets: StagedInboxPatchTarget[] = [];

  try {
    for (const target of targets) {
      let mode: number | undefined;
      if (!target.isNewFile) {
        await fs.access(target.targetPath, fsConstants.W_OK);
        mode = (await fs.stat(target.targetPath)).mode;
      }

      const tempDir = await findNearestExistingDirectory(
        path.dirname(target.targetPath),
      );
      const tempPath = path.join(
        tempDir,
        `.${path.basename(target.targetPath)}.${randomUUID()}.patch-tmp`,
      );

      await writeExclusiveFile(tempPath, target.patched, mode);
      stagedTargets.push({
        targetPath: target.targetPath,
        tempPath,
        original: target.original,
        isNewFile: target.isNewFile,
        mode,
      });
    }

    for (const target of stagedTargets) {
      if (!target.isNewFile) {
        continue;
      }
      await fs.mkdir(path.dirname(target.targetPath), { recursive: true });
    }

    return stagedTargets;
  } catch (error) {
    await cleanupStagedInboxPatchTargets(stagedTargets);
    throw error;
  }
}

/**
 * Scans the skill extraction inbox for .patch files and returns
 * structured data for each valid patch.
 */
export async function listInboxPatches(config: Config): Promise<InboxPatch[]> {
  const skillsDir = config.storage.getProjectSkillsMemoryDir();

  let entries: string[];
  try {
    entries = await fs.readdir(skillsDir);
  } catch {
    return [];
  }

  const patchFiles = entries.filter((e) => e.endsWith('.patch'));
  if (patchFiles.length === 0) {
    return [];
  }

  const patches: InboxPatch[] = [];
  for (const patchFile of patchFiles) {
    const patchPath = path.join(skillsDir, patchFile);
    try {
      const content = await fs.readFile(patchPath, 'utf-8');
      const parsed = Diff.parsePatch(content);
      if (!hasParsedPatchHunks(parsed)) continue;

      const patchEntries: InboxPatchEntry[] = parsed.map((p) => ({
        targetPath: p.newFileName ?? p.oldFileName ?? '',
        diffContent: formatParsedDiff(p),
      }));

      patches.push({
        fileName: patchFile,
        name: patchFile.replace(/\.patch$/, ''),
        entries: patchEntries,
        extractedAt: await getPatchExtractedAt(patchPath),
      });
    } catch {
      // Skip unreadable patch files
    }
  }

  return patches;
}

/**
 * Applies a .patch file from the inbox by reading each target file,
 * applying the diff, and writing the result. Deletes the patch on success.
 */
export async function applyInboxPatch(
  config: Config,
  fileName: string,
): Promise<{ success: boolean; message: string }> {
  if (!isValidInboxPatchFileName(fileName)) {
    return {
      success: false,
      message: 'Invalid patch file name.',
    };
  }

  const skillsDir = config.storage.getProjectSkillsMemoryDir();
  const patchPath = path.join(skillsDir, fileName);

  let content: string;
  try {
    content = await fs.readFile(patchPath, 'utf-8');
  } catch {
    return {
      success: false,
      message: `Patch "${fileName}" not found in inbox.`,
    };
  }

  let parsed: Diff.StructuredPatch[];
  try {
    parsed = Diff.parsePatch(content);
  } catch (error) {
    return {
      success: false,
      message: `Failed to parse patch "${fileName}": ${getErrorMessage(error)}`,
    };
  }
  if (!hasParsedPatchHunks(parsed)) {
    return {
      success: false,
      message: `Patch "${fileName}" contains no valid hunks.`,
    };
  }

  const validatedHeaders = validateParsedSkillPatchHeaders(parsed);
  if (!validatedHeaders.success) {
    return {
      success: false,
      message:
        validatedHeaders.reason === 'missingTargetPath'
          ? `Patch "${fileName}" is missing a target file path.`
          : `Patch "${fileName}" has invalid diff headers.`,
    };
  }

  if (
    !config.isTrustedFolder() &&
    (await patchTargetsProjectSkills(
      validatedHeaders.patches.map((patch) => patch.targetPath),
      config,
    ))
  ) {
    return {
      success: false,
      message:
        'Project skill patches are unavailable until this workspace is trusted.',
    };
  }

  // Dry-run first: verify all patches apply cleanly before writing anything.
  // Repeated file blocks are validated against the progressively patched content.
  const applied = await applyParsedSkillPatches(parsed, config);
  if (!applied.success) {
    switch (applied.reason) {
      case 'missingTargetPath':
        return {
          success: false,
          message: `Patch "${fileName}" is missing a target file path.`,
        };
      case 'invalidPatchHeaders':
        return {
          success: false,
          message: `Patch "${fileName}" has invalid diff headers.`,
        };
      case 'outsideAllowedRoots':
        return {
          success: false,
          message: `Patch "${fileName}" targets a file outside the global/workspace skill directories: ${applied.targetPath}`,
        };
      case 'newFileAlreadyExists':
        return {
          success: false,
          message: `Patch "${fileName}" declares a new file, but the target already exists: ${applied.targetPath}`,
        };
      case 'targetNotFound':
        return {
          success: false,
          message: `Target file not found: ${applied.targetPath}`,
        };
      case 'doesNotApply':
        return {
          success: false,
          message: applied.isNewFile
            ? `Patch "${fileName}" failed to apply for new file ${applied.targetPath}.`
            : `Patch does not apply cleanly to ${applied.targetPath}.`,
        };
      default:
        return {
          success: false,
          message: `Patch "${fileName}" could not be applied.`,
        };
    }
  }

  let stagedTargets: StagedInboxPatchTarget[];
  try {
    stagedTargets = await stageInboxPatchTargets(applied.results);
  } catch (error) {
    return {
      success: false,
      message: `Patch "${fileName}" could not be staged: ${getErrorMessage(error)}.`,
    };
  }

  const committedTargets: StagedInboxPatchTarget[] = [];
  try {
    for (const stagedTarget of stagedTargets) {
      await fs.rename(stagedTarget.tempPath, stagedTarget.targetPath);
      committedTargets.push(stagedTarget);
    }
  } catch (error) {
    for (const committedTarget of committedTargets.reverse()) {
      try {
        await restoreCommittedInboxPatchTarget(committedTarget);
      } catch {
        // Best-effort rollback. We still report the commit failure below.
      }
    }
    await cleanupStagedInboxPatchTargets(
      stagedTargets.filter((target) => !committedTargets.includes(target)),
    );
    return {
      success: false,
      message: `Patch "${fileName}" could not be applied atomically: ${getErrorMessage(error)}.`,
    };
  }

  // Remove the patch file
  await fs.unlink(patchPath);

  const fileCount = applied.results.length;
  return {
    success: true,
    message: `Applied patch to ${fileCount} file${fileCount !== 1 ? 's' : ''}.`,
  };
}

/**
 * Removes a .patch file from the extraction inbox.
 */
export async function dismissInboxPatch(
  config: Config,
  fileName: string,
): Promise<{ success: boolean; message: string }> {
  if (!isValidInboxPatchFileName(fileName)) {
    return {
      success: false,
      message: 'Invalid patch file name.',
    };
  }

  const skillsDir = config.storage.getProjectSkillsMemoryDir();
  const patchPath = path.join(skillsDir, fileName);

  try {
    await fs.access(patchPath);
  } catch {
    return {
      success: false,
      message: `Patch "${fileName}" not found in inbox.`,
    };
  }

  await fs.unlink(patchPath);

  return {
    success: true,
    message: `Dismissed "${fileName}" from inbox.`,
  };
}