File size: 43,538 Bytes
5f3bbdf
a598115
d9dbba5
 
11c849b
 
7d285f5
 
dfbb180
15b7bb6
4b5c9d9
5f3bbdf
93f665f
 
 
 
 
 
 
 
 
 
7d285f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ba2011
 
93f665f
0ba2011
7d285f5
 
a01d7d6
11c849b
 
 
 
 
 
 
 
 
2e54937
11c849b
15b7bb6
 
 
 
 
 
 
 
4b5c9d9
15b7bb6
 
a598115
 
 
 
 
 
 
 
15b7bb6
 
7d285f5
 
 
 
 
15b7bb6
 
4b5c9d9
15b7bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d285f5
15b7bb6
 
 
 
 
 
 
 
 
7d285f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15b7bb6
 
 
 
 
 
 
 
 
 
 
 
 
4b5c9d9
15b7bb6
 
 
 
 
 
 
 
 
 
4b5c9d9
15b7bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a01d7d6
0ba2011
161b919
a01d7d6
0ba2011
15b7bb6
 
0ba2011
 
7d285f5
 
 
 
 
179e028
 
 
 
 
7d285f5
179e028
 
 
2eb695e
 
 
 
 
d9dbba5
2e54937
d9dbba5
 
7d285f5
 
 
 
a818de2
 
 
d9dbba5
a818de2
d9dbba5
 
a598115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d285f5
 
 
a598115
 
7d285f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a598115
 
7d285f5
 
 
 
 
 
 
a598115
 
 
4b5c9d9
 
a598115
 
4b5c9d9
 
 
7d285f5
 
 
 
 
 
 
 
 
 
 
 
a598115
 
 
4b5c9d9
a598115
7d285f5
 
 
 
a598115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b5c9d9
 
a598115
4b5c9d9
 
e9a73cb
5f3bbdf
e9a73cb
d9dbba5
306f064
 
 
 
 
 
 
7d285f5
 
 
 
 
e9a73cb
 
0ba2011
 
 
 
d9dbba5
 
a01d7d6
d9dbba5
0ba2011
a01d7d6
0ba2011
11c849b
0ba2011
a01d7d6
 
 
 
5a0b87c
87099fe
 
161b919
87099fe
 
15b7bb6
 
 
 
 
87099fe
 
7d285f5
 
 
 
 
 
 
 
0ba2011
 
 
 
6e44748
 
 
5a0b87c
 
5dc2f11
 
 
5a0b87c
e9a73cb
5a0b87c
 
ea4bb9b
5f3bbdf
ea4bb9b
0ba2011
 
 
 
 
e9a73cb
 
0ba2011
7d285f5
dfbb180
 
 
 
 
 
 
15b7bb6
 
dfbb180
 
 
2e54937
dfbb180
 
 
 
0ba2011
ea4bb9b
7d285f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ba2011
11c849b
0ba2011
11c849b
 
 
 
 
2eb695e
b0457c6
 
 
 
 
 
 
 
 
11c849b
7d285f5
 
 
 
6686367
7d285f5
 
 
 
 
 
 
 
 
 
 
 
 
11c849b
 
7d285f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b5c9d9
7d285f5
4b5c9d9
 
a598115
 
 
 
 
 
 
7d285f5
b0457c6
 
 
 
 
 
7d285f5
b0457c6
 
4b5c9d9
 
 
 
 
 
b0457c6
 
 
a598115
 
 
 
 
 
7d285f5
a598115
 
7d285f5
a598115
 
 
 
 
2eb695e
4b5c9d9
 
 
0632984
4b5c9d9
 
 
 
a598115
b0457c6
 
 
 
0632984
a598115
b0457c6
 
 
 
 
4b5c9d9
 
 
 
b0457c6
6686367
b0457c6
 
7d285f5
 
 
 
 
 
 
 
 
b0457c6
4b5c9d9
7d285f5
4b5c9d9
 
7d285f5
 
b0457c6
7d285f5
 
 
 
 
d9dbba5
7d285f5
 
 
d9dbba5
2eb695e
7d285f5
 
 
b0457c6
7d285f5
 
 
 
 
 
 
 
 
 
b0457c6
 
 
15b7bb6
7d285f5
a598115
b0457c6
 
11c849b
 
 
 
 
 
 
15b7bb6
 
 
 
 
7d285f5
 
11c849b
 
7d285f5
 
 
11c849b
 
 
 
 
 
 
 
 
 
 
 
ea4bb9b
 
a3467e0
 
 
 
 
15b7bb6
 
a3467e0
 
7d285f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3467e0
 
93f665f
 
 
a3467e0
 
 
c578dca
a3467e0
15b7bb6
 
 
a3467e0
15b7bb6
 
 
 
 
 
 
 
 
a3467e0
c578dca
15b7bb6
 
 
 
c578dca
 
 
 
 
 
 
 
 
 
 
 
 
7d285f5
c578dca
 
 
 
e8f0b97
c578dca
 
 
 
 
 
93f665f
c578dca
 
 
a3467e0
 
 
15b7bb6
a3467e0
 
 
 
2e54937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d285f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a0b87c
2eb695e
5a0b87c
0ba2011
7d285f5
 
 
 
 
 
 
0ba2011
5a0b87c
0ba2011
2eb695e
0ba2011
 
 
7d285f5
0ba2011
5a0b87c
 
0ba2011
7d285f5
5a0b87c
 
0ba2011
 
 
5a0b87c
 
 
0ba2011
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
import fs from 'fs';
import { spawn, ChildProcess, execFileSync, execSync } from 'child_process';
import path from 'path';
import Docker from 'dockerode';
import { EventEmitter } from 'events';
import { IdxEngine } from '../idx/idx-engine';
import { buildWorkspaceImage, loadWorkspaceConfig, type CodeverseConfig } from './builder';
import { createDockerClient, probeDockerAvailability, resolveDockerClient } from './client';
import { HFStorage } from '../hf/storage';
import { resolveSafeProjectPath } from '../fs/isolation';
import { ENV_CONFIG } from '../env-config';

/**
 * Lighter process interface for reconnected sessions that don't have a full Node.js ChildProcess object.
 * We only require 'kill' and 'pid' for the watchdog and management tasks.
 */
type WorkspaceProcess = Pick<ChildProcess, 'kill' | 'pid'> & {
    on?: ChildProcess['on'];
    stdout?: ChildProcess['stdout'];
    stderr?: ChildProcess['stderr'];
};

type WorkspaceRuntimeKind = 'native' | 'docker';

interface NativeWorkspaceRuntimeEntry {
    kind: 'native';
    pid: number;
    port: number;
    process: WorkspaceProcess;
}

interface DockerWorkspaceRuntimeEntry {
    kind: 'docker';
    containerId: string;
    containerName: string;
    imageName: string;
    port: number;
    socketPath: string;
}

type WorkspaceRuntimeEntry = NativeWorkspaceRuntimeEntry | DockerWorkspaceRuntimeEntry;

/**
 * Registry for native workspace processes (IDE instances running outside Docker)
 * Map<workspaceId, { pid: number; port: number; process: WorkspaceProcess }>
 */
export const nativeProcesses = new Map<string, NativeWorkspaceRuntimeEntry>();
export const dockerWorkspaces = new Map<string, DockerWorkspaceRuntimeEntry>();

/**
 * Internal Provisioning Bus to multicast logs to concurrent clients.
 */
class ProvisioningBus extends EventEmitter {}
export const provisioningBus = new ProvisioningBus();

/**
 * Map to track active provisioning promises to prevent redundant creation loops.
 */
export const pendingProvisioning = new Map<string, Promise<WorkspaceOperationResult>>();

interface WorkspaceRuntimePaths {
    fullWorkspaceId: string;
    shortWorkspaceId: string;
    projectPath: string;
    runtimeRootPath: string;
    runtimeWorkspacePath: string;
    userDataPath: string;
    metadataPath: string;
    npmCachePath: string;
}

interface CodeServerLaunch {
    command: string;
    args: string[];
    label: string;
    usesNpx: boolean;
    useShell: boolean;
}

const SHORT_WORKSPACE_ID_LENGTH = 8;
const RUNTIME_ROOT_DIR_NAME = '.codeverse-runtime';
const DOCKER_CODE_SERVER_PORT = 8080;
const WORKSPACE_CONTAINER_NAME_PREFIX = 'codeverse-workspace-';
const WORKSPACE_RUNTIME_LABEL = 'codeverse.runtime';
const WORKSPACE_RUNTIME_LABEL_VALUE = 'workspace';
const WORKSPACE_ID_LABEL = 'codeverse.workspace.id';

function getWorkspaceRootPath(): string {
    return ENV_CONFIG.WORKSPACE_ROOT;
}

function getRuntimeRootPath(): string {
    return path.join(/*turbopackIgnore: true*/ getWorkspaceRootPath(), RUNTIME_ROOT_DIR_NAME);
}

function getShortWorkspaceId(id: string): string {
    return id.slice(0, SHORT_WORKSPACE_ID_LENGTH);
}

function isPathWithinParent(parentPath: string, targetPath: string): boolean {
    const normalizedParent = path.resolve(parentPath);
    const normalizedTarget = path.resolve(targetPath);

    return normalizedTarget === normalizedParent || normalizedTarget.startsWith(`${normalizedParent}${path.sep}`);
}

function getNativeWorkspaceEntry(id: string): NativeWorkspaceRuntimeEntry | undefined {
    const directEntry = nativeProcesses.get(id);
    if (directEntry) {
        return directEntry;
    }

    const prefixedKey = Array.from(nativeProcesses.keys()).find((key) => id.startsWith(key));
    return prefixedKey ? nativeProcesses.get(prefixedKey) : undefined;
}

function getDockerWorkspaceEntry(id: string): DockerWorkspaceRuntimeEntry | undefined {
    return dockerWorkspaces.get(id);
}

function getWorkspaceRuntimeEntry(id: string): WorkspaceRuntimeEntry | undefined {
    return getNativeWorkspaceEntry(id)
        ?? getDockerWorkspaceEntry(id);
}

function getWorkspaceContainerName(id: string): string {
    return `${WORKSPACE_CONTAINER_NAME_PREFIX}${id}`;
}

function getWorkspaceContainerLabels(config: Pick<WorkspaceConfig, 'id' | 'projectName' | 'userId'>): Record<string, string> {
    return {
        [WORKSPACE_RUNTIME_LABEL]: WORKSPACE_RUNTIME_LABEL_VALUE,
        [WORKSPACE_ID_LABEL]: config.id,
        'codeverse.workspace.project': config.projectName,
        'codeverse.workspace.user': config.userId,
    };
}

function getDockerBindSourcePath(hostPath: string): string {
    return path.resolve(hostPath);
}

function hasCustomDockerPackages(config: CodeverseConfig): boolean {
    return Boolean(
        config.packages?.apt?.length
        || config.packages?.npm?.length
    );
}

function getWorkspaceEnvironment(config: CodeverseConfig): string[] {
    return Object.entries(config.env ?? {}).map(([key, value]) => `${key}=${value}`);
}

async function ensureDockerImageAvailable(docker: Docker, imageName: string, log: (msg: string) => void): Promise<void> {
    try {
        await docker.getImage(imageName).inspect();
        return;
    } catch {
        log(`Pulling Docker image '${imageName}'...`);
    }

    await new Promise<void>((resolve, reject) => {
        docker.pull(imageName, (error: Error | null, stream?: NodeJS.ReadableStream) => {
            if (error || !stream) {
                reject(error ?? new Error(`Failed to pull Docker image '${imageName}'`));
                return;
            }

            docker.modem.followProgress(
                stream,
                (progressError: Error | null) => {
                    if (progressError) {
                        reject(progressError);
                        return;
                    }

                    resolve();
                },
                (event: { status?: string; id?: string }) => {
                    if (event.status) {
                        const progressLabel = event.id ? `${event.id}: ${event.status}` : event.status;
                        log(`[DOCKER:PULL] ${progressLabel}`);
                    }
                }
            );
        });
    });
}

function getPublishedPortForContainerInfo(info: Docker.ContainerInspectInfo): number | undefined {
    const bindings = info.NetworkSettings?.Ports?.[`${DOCKER_CODE_SERVER_PORT}/tcp`];
    const hostPort = bindings?.[0]?.HostPort;
    if (!hostPort) {
        return undefined;
    }

    const parsedPort = Number.parseInt(hostPort, 10);
    return Number.isFinite(parsedPort) ? parsedPort : undefined;
}

function getPublishedPortForListEntry(containerInfo: Docker.ContainerInfo): number | undefined {
    const publishedPort = containerInfo.Ports.find((portInfo) => portInfo.PrivatePort === DOCKER_CODE_SERVER_PORT && portInfo.PublicPort)?.PublicPort;
    return publishedPort && Number.isFinite(publishedPort) ? publishedPort : undefined;
}

async function resolveWorkspaceRuntimePaths(config: Pick<WorkspaceConfig, 'id' | 'userId' | 'projectName'>): Promise<WorkspaceRuntimePaths> {
    const shortWorkspaceId = getShortWorkspaceId(config.id);
    const runtimeRootPath = getRuntimeRootPath();
    const projectPath = await resolveSafeProjectPath(config.userId, config.projectName);

    return {
        fullWorkspaceId: config.id,
        shortWorkspaceId,
        projectPath,
        runtimeRootPath,
        runtimeWorkspacePath: path.join(/*turbopackIgnore: true*/ runtimeRootPath, shortWorkspaceId),
        userDataPath: path.join(/*turbopackIgnore: true*/ runtimeRootPath, `${shortWorkspaceId}-userdata`),
        metadataPath: path.join(/*turbopackIgnore: true*/ runtimeRootPath, `${shortWorkspaceId}.id`),
        npmCachePath: path.join(/*turbopackIgnore: true*/ runtimeRootPath, 'npm-cache'),
    };
}

function ensureRuntimeWorkspacePath(paths: WorkspaceRuntimePaths, log?: (msg: string) => void): string {
    if (!fs.existsSync(paths.projectPath)) {
        fs.mkdirSync(paths.projectPath, { recursive: true });
    }

    fs.mkdirSync(paths.runtimeRootPath, { recursive: true });
    fs.mkdirSync(paths.userDataPath, { recursive: true });
    fs.mkdirSync(paths.npmCachePath, { recursive: true });

    if (fs.existsSync(paths.runtimeWorkspacePath)) {
        try {
            const existingTargetPath = fs.realpathSync(paths.runtimeWorkspacePath);
            if (path.resolve(existingTargetPath) === path.resolve(paths.projectPath)) {
                fs.writeFileSync(paths.metadataPath, paths.fullWorkspaceId);
                return paths.runtimeWorkspacePath;
            }
        } catch {
            // Fall through and repair the runtime alias.
        }

        if (!isPathWithinParent(paths.runtimeRootPath, paths.runtimeWorkspacePath)) {
            throw new Error(`Unsafe runtime workspace path: ${paths.runtimeWorkspacePath}`);
        }

        fs.rmSync(paths.runtimeWorkspacePath, { recursive: true, force: true });
    }

    try {
        fs.symlinkSync(paths.projectPath, paths.runtimeWorkspacePath, process.platform === 'win32' ? 'junction' : 'dir');
        fs.writeFileSync(paths.metadataPath, paths.fullWorkspaceId);
        log?.(`Bound runtime alias ${paths.shortWorkspaceId} -> ${paths.projectPath}`);
        return paths.runtimeWorkspacePath;
    } catch (error) {
        const errorMessage = error instanceof Error ? error.message : String(error);
        fs.writeFileSync(paths.metadataPath, paths.fullWorkspaceId);
        log?.(`[WARN] Runtime alias creation failed. Falling back to direct project path: ${errorMessage}`);
        return paths.projectPath;
    }
}

/**
 * Checks if a native workspace is currently running.
 * Supports fuzzy matching for reconnected sessions that might use a prefix key.
 */
export function isNativeWorkspaceRunning(id: string): boolean {
    if (pendingProvisioning.has(id)) return false;
    return getNativeWorkspaceEntry(id) !== undefined;
}

export function isWorkspaceRunning(id: string): boolean {
    if (pendingProvisioning.has(id)) return false;
    return getWorkspaceRuntimeEntry(id) !== undefined;
}

/**
 * Returns the current runtime status of a workspace.
 */
export function getWorkspaceStatus(id: string): "ready" | "provisioning" | "offline" {
    if (pendingProvisioning.has(id)) return "provisioning";
    if (getWorkspaceRuntimeEntry(id)) return "ready";
    return "offline";
}

/**
 * Helper for async delays.
 */
const delay = (ms: number) => new Promise(res => setTimeout(res, ms));

/**
 * Finds an available port in the 8100-9000 range.
 */
function findAvailablePort(): number {
    const occupiedPorts = [
        ...Array.from(nativeProcesses.values()).map((entry) => entry.port),
        ...Array.from(dockerWorkspaces.values()).map((entry) => entry.port),
    ];
    let port = Math.floor(Math.random() * (9000 - 8100) + 8100);
    while (occupiedPorts.includes(port)) {
        port = Math.floor(Math.random() * (9000 - 8100) + 8100);
    }
    return port;
}

function resolveExecutableOnPath(candidates: string[]): string | null {
    const locatorCommand = process.platform === 'win32' ? 'where' : 'which';

    for (const candidate of candidates) {
        try {
            const output = execFileSync(locatorCommand, [candidate], {
                encoding: 'utf8',
                stdio: ['ignore', 'pipe', 'ignore'],
            });
            const resolvedPath = output
                .split(/\r?\n/)
                .map((line) => line.trim())
                .find(Boolean);
            if (resolvedPath) {
                return resolvedPath;
            }
        } catch {
            // Fall through to the next candidate.
        }
    }

    return null;
}

function resolveLocalToolNodeBinary(): string | null {
    if (ENV_CONFIG.CODE_SERVER_NODE_BIN && fs.existsSync(ENV_CONFIG.CODE_SERVER_NODE_BIN)) {
        return ENV_CONFIG.CODE_SERVER_NODE_BIN;
    }

    const localToolsRoot = path.join(process.cwd(), '.codeverse-tools');
    if (!fs.existsSync(localToolsRoot)) {
        return null;
    }

    const directNodePath = path.join(localToolsRoot, 'node', 'node.exe');
    if (fs.existsSync(directNodePath)) {
        return directNodePath;
    }

    const nodeDirs = fs.readdirSync(localToolsRoot, { withFileTypes: true })
        .filter((entry) => entry.isDirectory() && /^node-v22\..*-win-x64$/i.test(entry.name))
        .map((entry) => path.join(localToolsRoot, entry.name, 'node.exe'));

    return nodeDirs.find((candidate) => fs.existsSync(candidate)) ?? null;
}

function resolveLocalCodeServerEntry(): string | null {
    if (ENV_CONFIG.CODE_SERVER_ENTRY && fs.existsSync(ENV_CONFIG.CODE_SERVER_ENTRY)) {
        return ENV_CONFIG.CODE_SERVER_ENTRY;
    }

    const localEntryPath = path.join(process.cwd(), '.codeverse-tools', 'code-server', 'node_modules', 'code-server', 'out', 'node', 'entry.js');
    return fs.existsSync(localEntryPath) ? localEntryPath : null;
}

function getCurrentNodeMajorVersion(): number {
    const [majorVersion] = process.versions.node.split('.');
    return Number.parseInt(majorVersion ?? '0', 10);
}

function resolveCodeServerLaunch(): CodeServerLaunch {
    const overrideBinary = process.env.CODE_SERVER_BIN;
    if (overrideBinary) {
        const useShell = process.platform === 'win32' && /\.(cmd|bat)$/i.test(overrideBinary);
        return { command: overrideBinary, args: [], label: overrideBinary, usesNpx: false, useShell };
    }

    if (process.platform === 'win32') {
        const localToolNodeBinary = resolveLocalToolNodeBinary();
        const localCodeServerEntry = resolveLocalCodeServerEntry();
        if (localToolNodeBinary && localCodeServerEntry) {
            return {
                command: localToolNodeBinary,
                args: [localCodeServerEntry],
                label: 'local code-server toolchain',
                usesNpx: false,
                useShell: false
            };
        }

        const codeServerBinary = resolveExecutableOnPath(['code-server.exe', 'code-server']);
        if (codeServerBinary) {
            return { command: codeServerBinary, args: [], label: 'code-server', usesNpx: false, useShell: false };
        }

        if (getCurrentNodeMajorVersion() !== 22) {
            throw new Error('CODE_SERVER_WINDOWS_REQUIRES_NODE_22_OR_LOCAL_TOOLCHAIN');
        }

        const npxCliPath = path.join(path.dirname(process.execPath), 'node_modules', 'npm', 'bin', 'npx-cli.js');
        if (fs.existsSync(npxCliPath)) {
            return { command: process.execPath, args: [npxCliPath, '--yes', 'code-server'], label: 'node npx-cli.js code-server', usesNpx: true, useShell: false };
        }

        throw new Error('CODE_SERVER_BIN_NOT_FOUND');
    }

    const codeServerBinary = resolveExecutableOnPath(['code-server']);
    if (codeServerBinary) {
        return { command: codeServerBinary, args: [], label: 'code-server', usesNpx: false, useShell: false };
    }

    const npxBinary = resolveExecutableOnPath(['npx']);
    if (npxBinary) {
        return { command: npxBinary, args: ['--yes', 'code-server'], label: 'npx code-server', usesNpx: true, useShell: false };
    }

    throw new Error('CODE_SERVER_BIN_NOT_FOUND');
}

/**
 * Checks if Docker is available in the current environment.
 */
export async function isDockerAvailable(): Promise<{ available: boolean; reason?: string }> {
    if (process.env.SIMULATE_HF === 'true') {
        return { available: false, reason: "Hugging Face Simulation Mode (Artificial Sandbox)" };
    }
    
    if (process.env.SPACE_ID) {
        return { available: false, reason: "Hugging Face Space (Native Sandboxed)" };
    }

    const availability = await probeDockerAvailability(ENV_CONFIG.DOCKER_PROBE_TIMEOUT_MS);
    return availability.available
        ? { available: true }
        : { available: false, reason: availability.reason ?? "Docker daemon unreachable" };
}

/**
 * Stops a native workspace process.
 */
export async function stopNativeWorkspace(id: string): Promise<boolean> {
    const entry = nativeProcesses.get(id);
    if (entry) {
        try {
            entry.process.kill();
            nativeProcesses.delete(id);
            return true;
        } catch (e) {
            console.error(`[MANAGER] Failed to kill code-server ${id}:`, e);
            nativeProcesses.delete(id);
        }
    }
    return false;
}

/**
 * Gets the internal port for a native workspace process.
 * Supports fuzzy matching for reconnected sessions.
 */
export function getNativeWorkspacePort(id: string): number | undefined {
    if (pendingProvisioning.has(id)) {
        return undefined;
    }

    return getNativeWorkspaceEntry(id)?.port;
}

export function getWorkspacePort(id: string): number | undefined {
    if (pendingProvisioning.has(id)) {
        return undefined;
    }

    return getWorkspaceRuntimeEntry(id)?.port;
}

/**
 * Returns the global unified Android VNC port.
 */
export function getAndroidPort(): number | undefined {
    return 6080;
}

export interface WorkspaceConfig {
    id: string;
    userId: string;
    projectName: string;
    image?: string; 
    withAndroidEmulator?: boolean;
    onLog?: (msg: string) => void;
}

/**
 * Results for workspace operations.
 */
export interface WorkspaceOperationResult {
    success: boolean;
    containerId?: string;
    androidContainerId?: string;
    androidPort?: number;
    port?: string | number;
    appetizeUrl?: string;
    error?: string;
    runtime?: WorkspaceRuntimeKind;
    status?: 'hydrating' | 'ready';
}

/**
 * PREDICTIVE HYDRATION: Pre-warms Nix profile and SDKs.
 */
export async function prewarmWorkspace(config: WorkspaceConfig): Promise<void> {
    const runtimePaths = await resolveWorkspaceRuntimePaths(config);
    const workspacePath = ensureRuntimeWorkspacePath(runtimePaths);

    const idxConfig = IdxEngine.getIdxConfig(workspacePath);
    if (idxConfig) {
        // Run Nix sync in background if not already warmed
        IdxEngine.syncNixEnvironment(workspacePath, idxConfig, (msg) => {
            provisioningBus.emit(`log:${config.id}`, `[HYDRATE] ${msg}`);
        });
    }
}

interface WorkspaceHeartbeatOptions {
    port: number;
    log: (msg: string) => void;
    getFailureReason?: () => Promise<string | null>;
}

async function waitForWorkspaceHeartbeat({ port, log, getFailureReason }: WorkspaceHeartbeatOptions): Promise<string | null> {
    let attempts = 0;

    while (attempts < 60) {
        const failureReason = await getFailureReason?.();
        if (failureReason) {
            log(`[FATAL] IDE bootstrap aborted before handshake: ${failureReason}`);
            return failureReason;
        }

        const controller = new AbortController();
        const timeoutId = setTimeout(() => controller.abort(), 2000);

        try {
            const response = await fetch(`http://127.0.0.1:${port}`, { signal: controller.signal });
            if (response.ok) {
                log(`Handshake verified. Studio Engine Online.`);
                return null;
            }
        } catch {
            if (attempts % 5 === 0) log(`[INFO] Scanning for IDE heartbeat... (Attempt ${attempts}/60)`);
            if (attempts === 15) {
                const bootstrapHint = ENV_CONFIG.IDX_NIX_SYNC_ENABLED
                    ? 'Nix evaluation in progress. Cold boot detected.'
                    : 'IDE bootstrap still in progress.';
                log(`[INFO] ${bootstrapHint}`);
            }
            if (attempts === 45) log(`[WARN] Handshake threshold approaching. IDE core high load.`);
        } finally {
            clearTimeout(timeoutId);
        }

        await delay(1000);
        attempts++;
    }

    log(`[FATAL] Handshake timeout on 127.0.0.1:${port}.`);
    return "IDE_HANDSHAKE_TIMEOUT";
}

async function resolveExistingWorkspaceContainer(docker: Docker, workspaceId: string): Promise<Docker.ContainerInfo | null> {
    const containerName = getWorkspaceContainerName(workspaceId);
    const containers = await docker.listContainers({
        all: true,
        filters: {
            name: [containerName],
        },
    });

    const exactMatch = containers.find((container) => container.Names.some((name) => name === `/${containerName}`));
    return exactMatch ?? containers[0] ?? null;
}

function registerDockerWorkspaceRuntime(
    workspaceId: string,
    containerInfo: Docker.ContainerInspectInfo,
    socketPath: string
): DockerWorkspaceRuntimeEntry | null {
    const publishedPort = getPublishedPortForContainerInfo(containerInfo);
    if (!publishedPort) {
        return null;
    }

    const containerEntry: DockerWorkspaceRuntimeEntry = {
        kind: 'docker',
        containerId: containerInfo.Id,
        containerName: containerInfo.Name.replace(/^\//, ''),
        imageName: containerInfo.Config.Image,
        port: publishedPort,
        socketPath,
    };

    dockerWorkspaces.set(workspaceId, containerEntry);
    return containerEntry;
}

async function startDockerWorkspace(
    config: WorkspaceConfig,
    runtimePaths: WorkspaceRuntimePaths,
    workspacePath: string,
    log: (msg: string) => void
): Promise<WorkspaceOperationResult | null> {
    const dockerResolution = await resolveDockerClient(ENV_CONFIG.DOCKER_PROBE_TIMEOUT_MS);
    if (!dockerResolution) {
        return null;
    }

    const { docker, socketPath } = dockerResolution;
    const workspaceConfig = await loadWorkspaceConfig(workspacePath);
    const containerName = getWorkspaceContainerName(config.id);
    const existingContainerInfo = await resolveExistingWorkspaceContainer(docker, config.id);

    if (existingContainerInfo) {
        const existingContainer = docker.getContainer(existingContainerInfo.Id);
        const existingInspect = await existingContainer.inspect();

        if (!existingInspect.State.Running) {
            log(`Restarting existing Docker workspace container '${containerName}'...`);
            await existingContainer.start();
        } else {
            log(`Reusing active Docker workspace container '${containerName}'.`);
        }

        const runningInspect = await existingContainer.inspect();
        const runningEntry = registerDockerWorkspaceRuntime(config.id, runningInspect, socketPath);
        if (!runningEntry) {
            return { success: false, error: 'DOCKER_PORT_BINDING_MISSING' };
        }

        const heartbeatError = await waitForWorkspaceHeartbeat({
            port: runningEntry.port,
            log,
            getFailureReason: async () => {
                const info = await existingContainer.inspect();
                return info.State.Running ? null : `DOCKER_CONTAINER_EXITED_${info.State.ExitCode}`;
            },
        });

        if (heartbeatError) {
            dockerWorkspaces.delete(config.id);
            return { success: false, error: heartbeatError };
        }

        return {
            success: true,
            containerId: runningEntry.containerId,
            port: runningEntry.port,
            runtime: 'docker',
        };
    }

    let imageName = ENV_CONFIG.DOCKER_WORKSPACE_BASE_IMAGE;
    if (hasCustomDockerPackages(workspaceConfig)) {
        const buildResult = await buildWorkspaceImage(config.id, workspacePath, log, docker);
        imageName = buildResult.imageName;
    }

    await ensureDockerImageAvailable(docker, imageName, log);

    const port = findAvailablePort();
    const workspaceContainer = await docker.createContainer({
        name: containerName,
        Image: imageName,
        WorkingDir: '/home/coder/project',
        Env: [
            'HOME=/home/coder',
            'SHELL=/bin/bash',
            ...getWorkspaceEnvironment(workspaceConfig),
        ],
        Labels: getWorkspaceContainerLabels(config),
        ExposedPorts: {
            [`${DOCKER_CODE_SERVER_PORT}/tcp`]: {},
        },
        HostConfig: {
            Binds: [
                `${getDockerBindSourcePath(workspacePath)}:/home/coder/project`,
                `${getDockerBindSourcePath(runtimePaths.userDataPath)}:/home/coder/.local/share/code-server`,
            ],
            PortBindings: {
                [`${DOCKER_CODE_SERVER_PORT}/tcp`]: [{ HostIp: '127.0.0.1', HostPort: `${port}` }],
            },
        },
        Cmd: [
            '--auth', 'none',
            '--bind-addr', `0.0.0.0:${DOCKER_CODE_SERVER_PORT}`,
            '--disable-telemetry',
            '--disable-update-check',
            '--user-data-dir', '/home/coder/.local/share/code-server',
            '/home/coder/project',
        ],
    });

    await workspaceContainer.start();
    log(`Docker workspace container '${containerName}' started from image '${imageName}'.`);

    const createdInspect = await workspaceContainer.inspect();
    const createdEntry = registerDockerWorkspaceRuntime(config.id, createdInspect, socketPath);
    if (!createdEntry) {
        return { success: false, error: 'DOCKER_PORT_BINDING_MISSING' };
    }

    const heartbeatError = await waitForWorkspaceHeartbeat({
        port: createdEntry.port,
        log,
        getFailureReason: async () => {
            const info = await workspaceContainer.inspect();
            return info.State.Running ? null : `DOCKER_CONTAINER_EXITED_${info.State.ExitCode}`;
        },
    });

    if (heartbeatError) {
        try {
            await workspaceContainer.remove({ force: true });
        } catch {
            // Best-effort cleanup after failed boot.
        }
        dockerWorkspaces.delete(config.id);
        return { success: false, error: heartbeatError };
    }

    return {
        success: true,
        containerId: createdEntry.containerId,
        port: createdEntry.port,
        runtime: 'docker',
    };
}

/**
 * INTERNAL: Core provisioning logic with IDX support and auto-provisioning baseline.
 */
async function performProvisioning(config: WorkspaceConfig): Promise<WorkspaceOperationResult> {
    const log = (msg: string) => { 
        if (config.onLog) config.onLog(`[IDX:ENGINE] ${msg}`);
        provisioningBus.emit(`log:${config.id}`, msg);
    };

    try {
        log(`Provisioning hermetic environment for '${config.projectName}'...`);
        
        // 0. HF PERSISTENCE: Restore profile from Dataset if available
        try {
            await HFStorage.syncFromDataset((msg) => log(msg));
        } catch (e) {
            log(`[WARN] Persistent profile restoration failed: ${e instanceof Error ? e.message : String(e)}. Proceeding with clean environment.`);
        }

        // 1. Prepare Workspace Directory
        const runtimePaths = await resolveWorkspaceRuntimePaths(config);
        const workspacePath = ensureRuntimeWorkspacePath(runtimePaths, log);
        const userDataPath = runtimePaths.userDataPath;

        // 2. IDX Engine: Sync Environment
        const idxConfig = IdxEngine.getIdxConfig(workspacePath);
        log(`Declarative config detected (Packages: ${idxConfig.packages.length}). Initializing synchronization...`);

        await IdxEngine.syncNixEnvironment(workspacePath, idxConfig, (msg) => log(msg));

        const flagPath = path.join(/*turbopackIgnore: true*/ workspacePath, '.idx-created');
        if (!fs.existsSync(flagPath)) {
            if (idxConfig.onCreate) {
                log(`Executing onCreate lifecycle hook...`);
                await IdxEngine.runHook(workspacePath, 'onCreate', idxConfig.onCreate, (msg) => log(msg));
            }
            fs.writeFileSync(flagPath, new Date().toISOString());
        }

        const shouldPreferDockerRuntime = ENV_CONFIG.WORKSPACE_RUNTIME_PREFERENCE === 'docker'
            || (ENV_CONFIG.WORKSPACE_RUNTIME_PREFERENCE === 'auto' && process.platform === 'win32');
        if (shouldPreferDockerRuntime) {
            log(`Evaluating Docker runtime for workspace boot...`);
            const dockerResult = await startDockerWorkspace(config, runtimePaths, workspacePath, log);
            if (dockerResult) {
                if (dockerResult.success && idxConfig.onStart) {
                    log(`Executing background onStart lifecycle hooks...`);
                    IdxEngine.runHook(workspacePath, 'onStart', idxConfig.onStart, (msg) => log(msg), true);
                }

                const finalDockerResult: WorkspaceOperationResult = {
                    ...dockerResult,
                    androidPort: config.withAndroidEmulator ? 6080 : undefined,
                };

                if (dockerResult.success) {
                    provisioningBus.emit(`ready:${config.id}`, finalDockerResult);
                } else {
                    provisioningBus.emit(`error:${config.id}`, finalDockerResult);
                }

                return finalDockerResult;
            }

            if (ENV_CONFIG.WORKSPACE_RUNTIME_PREFERENCE === 'docker') {
                const dockerUnavailableResult: WorkspaceOperationResult = {
                    success: false,
                    error: 'DOCKER_DAEMON_UNREACHABLE',
                    runtime: 'docker',
                };
                provisioningBus.emit(`error:${config.id}`, dockerUnavailableResult);
                return dockerUnavailableResult;
            }

            log(`[INFO] Docker runtime unavailable. Falling back to native IDE process.`);
        }

        // 3. Spawn code-server natively
        const codeServerLaunch = resolveCodeServerLaunch();
        const port = findAvailablePort();
        const shellCommand = codeServerLaunch.command;
        const args = codeServerLaunch.args;
        const spawnCwd = (() => {
            try {
                return fs.realpathSync(workspacePath);
            } catch {
                return workspacePath;
            }
        })();

        const baseArgs = [
            '--auth', 'none',
            '--bind-addr', `127.0.0.1:${port}`,
            '--user-data-dir', userDataPath,
            '--disable-telemetry',
            '--disable-update-check',
            workspacePath,
        ];

        const spawnEnv: NodeJS.ProcessEnv = {
            ...process.env,
            HOME: workspacePath,
            npm_config_cache: runtimePaths.npmCachePath,
            npm_config_update_notifier: 'false',
        };
        delete spawnEnv.PORT;
        delete spawnEnv.SERVER_PORT;

        const launchArgs = [...args, ...baseArgs];

        log(`IDE launch prepared. Binary: ${shellCommand} | cwd: ${spawnCwd} | target: ${workspacePath}`);

        let child: ChildProcess;
        try {
            child = spawn(shellCommand, launchArgs, {
                env: spawnEnv,
                cwd: spawnCwd,
                shell: false,
            });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            throw new Error(`IDE_SPAWN_CONFIGURATION_REJECTED: ${errorMessage}`);
        }

        log(`Spawning VS Code Orchestrator via ${codeServerLaunch.label} (PID: ${child.pid})...`);
        let childExited = false;
        let childFailureReason: string | null = null;

        child.on('error', (err) => {
            childFailureReason = `IDE_BINARY_FAILURE: ${err.message}`;
            log(`[FATAL] IDE binary failure: ${err.message}`);
        });
        child.stdout?.on('data', (data) => {
            const out = data.toString().trim();
            if (out.includes('listening on')) log(`[IDX:UP] ${out}`);
            else if (out.length > 0) log(`[IDE:CORE] ${out}`);
        });

        child.stderr?.on('data', (data) => {
            const err = data.toString().trim();
            if (err.length > 0) log(`[IDE:ERR] ${err}`);
        });

        child.on('close', (code, signal) => {
            childExited = true;
            if (code !== 0 || signal) {
                childFailureReason = childFailureReason ?? `IDE_PROCESS_EXIT_${code ?? 'unknown'}${signal ? `_${signal}` : ''}`;
            }
            log(`[IDE:EXIT] IDE process died with code ${code} (Signal: ${signal})`);
            nativeProcesses.delete(config.id);
        });

        nativeProcesses.set(config.id, { kind: 'native', pid: child.pid!, port, process: child });

        const handshakeError = await waitForWorkspaceHeartbeat({
            port,
            log,
            getFailureReason: async () => {
                if (!childExited) {
                    return null;
                }

                const rawFailureMessage = childFailureReason ?? 'IDE_PROCESS_EXITED_BEFORE_HANDSHAKE';
                return codeServerLaunch.usesNpx
                    ? `${rawFailureMessage}. code-server could not be bootstrapped via npx. Install code-server globally or set CODE_SERVER_BIN.`
                    : rawFailureMessage;
            },
        });

        if (handshakeError) {
            const entry = nativeProcesses.get(config.id);
            if (entry) {
                entry.process.kill();
                nativeProcesses.delete(config.id);
            }
            const errResult = { success: false, error: handshakeError };
            provisioningBus.emit(`error:${config.id}`, errResult);
            return errResult;
        }

        if (idxConfig.onStart) {
            log(`Executing background onStart lifecycle hooks...`);
            IdxEngine.runHook(workspacePath, 'onStart', idxConfig.onStart, (msg) => log(msg), true);
        }

        const finalResult: WorkspaceOperationResult = {
            success: true,
            containerId: `native-${config.id}`,
            androidPort: config.withAndroidEmulator ? 6080 : undefined,
            port,
            runtime: 'native',
        };
        provisioningBus.emit(`ready:${config.id}`, finalResult);
        return finalResult;
    } catch (e) {
        const error = e instanceof Error ? e.message : String(e);
        log(`[FATAL] Provisioning pipeline collapsed: ${error}`);
        nativeProcesses.delete(config.id);
        dockerWorkspaces.delete(config.id);
        const errResult = { success: false, error: `PROVISIONING_FAILED: ${error}` };
        provisioningBus.emit(`error:${config.id}`, errResult);
        return errResult;
    }
}

/**
 * Workspace provisioner with ATOMIC single-instance locking.
 */
export async function startWorkspaceContainer(config: WorkspaceConfig): Promise<WorkspaceOperationResult> {
    const pendingWorkspace = pendingProvisioning.get(config.id);
    if (pendingWorkspace) {
        return await pendingWorkspace;
    }

    const existingRuntime = getWorkspaceRuntimeEntry(config.id);
    if (existingRuntime) {
        return {
            success: true,
            containerId: existingRuntime.kind === 'docker' ? existingRuntime.containerId : `native-${config.id}`,
            port: existingRuntime.port,
            runtime: existingRuntime.kind,
        };
    }

    let pending = pendingProvisioning.get(config.id);
    if (!pending) {
        pending = performProvisioning(config).finally(() => {
            pendingProvisioning.delete(config.id);
        });
        pendingProvisioning.set(config.id, pending);
    }

    return await pending;
}

/**
 * 🛠️ SELF-HEALING: Scans for running code-server instances to repopulate the proxy map.
 * This allows the IDE to survive server restarts or cold boots by probing active ports.
 */
export async function reconnectRunningWorkspaces() {
    const workspaceRoot = getWorkspaceRootPath();
    const runtimeRootPath = getRuntimeRootPath();
    
    console.log(`[BOOT] Probing filesystem segment: ${workspaceRoot} for existing sessions...`);
    try {
        const dockerResolution = await resolveDockerClient(ENV_CONFIG.DOCKER_PROBE_TIMEOUT_MS);
        if (dockerResolution) {
            const runningContainers = await dockerResolution.docker.listContainers({
                filters: {
                    label: [`${WORKSPACE_RUNTIME_LABEL}=${WORKSPACE_RUNTIME_LABEL_VALUE}`],
                },
            });

            for (const containerInfo of runningContainers) {
                const workspaceId = containerInfo.Labels?.[WORKSPACE_ID_LABEL];
                const port = getPublishedPortForListEntry(containerInfo);
                if (!workspaceId || !port) {
                    continue;
                }

                dockerWorkspaces.set(workspaceId, {
                    kind: 'docker',
                    containerId: containerInfo.Id,
                    containerName: containerInfo.Names[0]?.replace(/^\//, '') ?? getWorkspaceContainerName(workspaceId),
                    imageName: containerInfo.Image,
                    port,
                    socketPath: dockerResolution.socketPath,
                });
                console.log(`[RECONNECT] Restored Docker workspace ${workspaceId} on port ${port}.`);
            }
        }
    } catch (error) {
        console.warn(`[RECONNECT:WARN] Docker workspace restore failed: ${error instanceof Error ? error.message : String(error)}`);
    }

    try {
        // Find all code-server processes
        // Note: Using a more robust ps grep that works across most POSIX environments
        const psCmd = process.platform === 'win32' ? 'tasklist' : "ps aux | grep code-server | grep -v grep";
        const output = execSync(psCmd).toString();
        const lines = output.split('\n');
        
        for (const line of lines) {
            // Looking for: ... --bind-addr 127.0.0.1:8548 ... w/44c7597c
            const bindMatch = line.match(/--bind-addr 127\.0\.0\.1:(\d+)/);
            const userDataMatch = line.match(/\.codeverse-runtime[\/\\]([a-zA-Z0-9]{8})-userdata/);
            const runtimePathMatch = line.match(/\.codeverse-runtime[\/\\]([a-zA-Z0-9]{8})(?:\s|$)/);
            const legacyPathMatch = line.match(/[ /](?:w|workspaces)[\/\\]([a-zA-Z0-9]{8})/);
            
            if (bindMatch) {
                const shortId = userDataMatch?.[1] ?? runtimePathMatch?.[1] ?? legacyPathMatch?.[1];
                if (!shortId) {
                    continue;
                }

                const port = parseInt(bindMatch[1], 10);
                const metadataPath = path.join(runtimeRootPath, `${shortId}.id`);
                const legacyIdFile = path.join(workspaceRoot, shortId, '.codeverse-id');
                
                let foundFullId = "";
                if (fs.existsSync(metadataPath)) {
                    foundFullId = fs.readFileSync(metadataPath, 'utf-8').trim();
                } else if (fs.existsSync(legacyIdFile)) {
                    foundFullId = fs.readFileSync(legacyIdFile, 'utf-8').trim();
                } else {
                    // Fallback: If no ID file, we use the shortId as the temporary key.
                    foundFullId = shortId;
                    console.warn(`[RECONNECT:WARN] No .codeverse-id for session ${shortId}. Using prefix mapping.`);
                }
                
                if (foundFullId && !nativeProcesses.has(foundFullId)) {
                    // Capture PID reliably from ps output (column 2)
                    const psParts = line.trim().split(/\s+/);
                    const pid = parseInt(psParts[1]);
                    
                    console.log(`[RECONNECT] Identified active IDE ${foundFullId} (PID: ${pid}) on port ${port}. Restoration complete.`);
                    nativeProcesses.set(foundFullId, { 
                        kind: 'native',
                        pid, 
                        port, 
                        process: { 
                            pid,
                            kill: () => { 
                                try { 
                                    process.kill(pid, 'SIGKILL'); 
                                    return true;
                                } catch {
                                    try { execSync(`fuser -k ${port}/tcp`); } catch {}
                                    return true; 
                                }
                            }
                        } as WorkspaceProcess 
                    });
                }
            }
        }
    } catch {
        // No processes found or ps failed
    }
}

/**
 * 🟢 ENGINE WATCHDOG: Background health monitor for native IDE processes.
 */
function startEngineWatchdog() {
    setInterval(async () => {
        for (const [id, entry] of nativeProcesses.entries()) {
            try {
                // 1. Zombie Check
                try {
                    process.kill(entry.pid, 0); 
                } catch {
                    console.log(`[WATCHDOG] Process ${entry.pid} for ${id} is missing. Pruning.`);
                    nativeProcesses.delete(id);
                    continue;
                }

                // 2. Healthz Polling
                const controller = new AbortController();
                const timeoutId = setTimeout(() => controller.abort(), 2000);
                
                try {
                    const res = await fetch(`http://127.0.0.1:${entry.port}`, { signal: controller.signal });
                    if (!res.ok) throw new Error('Unhealthy');
                } catch {
                    console.warn(`[WATCHDOG] IDE ${id} (Port ${entry.port}) is non-responsive.`);
                    // Optional: force restart if unhealthy for multiple cycles
                } finally {
                    clearTimeout(timeoutId);
                }
            } catch (e) {
                console.error(`[WATCHDOG:ERR] ${e}`);
            }
        }
    }, 60000);
}

startEngineWatchdog();

async function stopDockerWorkspace(id: string): Promise<boolean> {
    const dockerEntry = dockerWorkspaces.get(id);
    const docker = dockerEntry
        ? createDockerClient(dockerEntry.socketPath)
        : (await resolveDockerClient(ENV_CONFIG.DOCKER_PROBE_TIMEOUT_MS))?.docker;

    if (!docker) {
        dockerWorkspaces.delete(id);
        return false;
    }

    const existingContainerInfo = await resolveExistingWorkspaceContainer(docker, id);
    if (!existingContainerInfo) {
        dockerWorkspaces.delete(id);
        return false;
    }

    const container = docker.getContainer(existingContainerInfo.Id);
    try {
        const inspectInfo = await container.inspect();
        if (inspectInfo.State.Running) {
            await container.stop({ t: 10 });
        }
    } catch {
        // Container may already be stopped or missing.
    }

    try {
        await container.remove({ force: true });
    } catch {
        // Container may already be removed.
    }

    dockerWorkspaces.delete(id);
    return true;
}

/**
 * Standardized stop method.
 */
export async function stopWorkspaceContainer(id: string): Promise<{ success: boolean }> {
    const nativeStopped = await stopNativeWorkspace(id);
    if (nativeStopped) {
        return { success: true };
    }

    const dockerStopped = await stopDockerWorkspace(id);
    return { success: dockerStopped };
}

/**
 * Modern Docker Manager class.
 */
export class DockerManager {
    async getContainerStatus(id: string): Promise<"running" | "stopped" | "not_found"> {
        if (isWorkspaceRunning(id)) return "running";
        return "stopped";
    }

    async stopContainer(id: string): Promise<boolean> {
        return (await stopWorkspaceContainer(id)).success;
    }

    async startWorkspace(config: WorkspaceConfig): Promise<boolean> {
        const result = await startWorkspaceContainer(config);
        return result.success;
    }
}

export const dockerManager = new DockerManager();