summaryrefslogtreecommitdiff
path: root/makima/frontend/src/routes/mesh.tsx
blob: f210227299be288723302660b43d9acd2ec991f1 (plain) (blame)
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
import { useState, useCallback, useEffect, useRef, useMemo, type MouseEvent } from "react";
import { useParams, useNavigate } from "react-router";
import { Masthead } from "../components/Masthead";
import { TaskList } from "../components/mesh/TaskList";
import { TaskDetail } from "../components/mesh/TaskDetail";
import { TaskOutput } from "../components/mesh/TaskOutput";
import { UnifiedMeshChatInput } from "../components/mesh/UnifiedMeshChatInput";
import { ContractCompleteQuestion } from "../components/mesh/ContractCompleteQuestion";
import { useTasks } from "../hooks/useTasks";
import { useTaskSubscription, type TaskUpdateEvent, type TaskOutputEvent } from "../hooks/useTaskSubscription";
import type { TaskWithSubtasks, MeshChatContext, ContractSummary, ContractWithRelations, DaemonDirectory, TaskSummary, RepositoryHistoryEntry } from "../lib/api";
import { startTask as startTaskApi, stopTask as stopTaskApi, getTaskOutput, listContracts, getContract, getDaemonDirectories, continueTask as continueTaskApi, resumeSupervisor, branchTask, getRepositorySuggestions, getTaskDiff } from "../lib/api";
import { DirectoryInput } from "../components/mesh/DirectoryInput";
import { useAuth } from "../contexts/AuthContext";
import { useSupervisorQuestions } from "../contexts/SupervisorQuestionsContext";

// View modes for the task detail page
type ViewMode = "split" | "task" | "output";

// Minimum panel widths (in pixels)
const MIN_TASK_WIDTH = 300;
const MIN_OUTPUT_WIDTH = 200;

// TODO: Store task output in database for resuming from any device.
// Currently only persisted in localStorage which is device-specific.

// LocalStorage key prefix for task output
const STORAGE_KEY_PREFIX_OUTPUT = "makima-task-output-";

// Load persisted output from localStorage with deduplication
function loadPersistedOutput(taskId: string): TaskOutputEvent[] {
  try {
    const stored = localStorage.getItem(STORAGE_KEY_PREFIX_OUTPUT + taskId);
    if (!stored) return [];
    const entries = JSON.parse(stored) as TaskOutputEvent[];

    // Deduplicate consecutive identical entries (cleanup from previous bug)
    const deduplicated: TaskOutputEvent[] = [];
    for (const entry of entries) {
      const last = deduplicated[deduplicated.length - 1];
      if (
        !last ||
        last.messageType !== entry.messageType ||
        last.content !== entry.content ||
        last.toolName !== entry.toolName
      ) {
        deduplicated.push(entry);
      }
    }

    // Save cleaned up version if we removed duplicates
    if (deduplicated.length !== entries.length) {
      savePersistedOutput(taskId, deduplicated);
    }

    return deduplicated;
  } catch {
    return [];
  }
}

// Save output to localStorage
function savePersistedOutput(taskId: string, entries: TaskOutputEvent[]): void {
  try {
    localStorage.setItem(STORAGE_KEY_PREFIX_OUTPUT + taskId, JSON.stringify(entries));
  } catch {
    // Ignore storage errors
  }
}

// Clear output from localStorage
function clearPersistedOutput(taskId: string): void {
  try {
    localStorage.removeItem(STORAGE_KEY_PREFIX_OUTPUT + taskId);
  } catch {
    // Ignore storage errors
  }
}

export default function MeshPage() {
  const { id } = useParams<{ id: string }>();
  const navigate = useNavigate();
  const { isAuthenticated, isAuthConfigured, isLoading: authLoading } = useAuth();
  const { tasks, loading, error, conflict, clearConflict, fetchTask, fetchTasks, editTask, removeTask, hideTask, saveTask } = useTasks();
  const { pendingQuestions, submitAnswer } = useSupervisorQuestions();

  // Memoize pending question IDs for efficient lookup
  const pendingQuestionIds = useMemo(
    () => new Set(pendingQuestions.map(q => q.questionId)),
    [pendingQuestions]
  );

  // Filter contract_complete questions for the current task
  const contractCompleteQuestionsForTask = useMemo(
    () => pendingQuestions.filter(
      (q) => q.questionType === "contract_complete" && q.taskId === id
    ),
    [pendingQuestions, id]
  );

  // Handler for answering supervisor questions
  const handleAnswerQuestion = useCallback(async (questionId: string, response: string) => {
    await submitAnswer(questionId, response);
  }, [submitAnswer]);

  // Redirect to login if not authenticated
  useEffect(() => {
    if (!authLoading && isAuthConfigured && !isAuthenticated) {
      navigate("/login");
    }
  }, [authLoading, isAuthConfigured, isAuthenticated, navigate]);
  const [taskDetail, setTaskDetail] = useState<TaskWithSubtasks | null>(null);
  const [detailLoading, setDetailLoading] = useState(false);
  const [creating, setCreating] = useState(false);
  const [taskOutputEntries, setTaskOutputEntries] = useState<TaskOutputEvent[]>([]);
  const [isStreaming, setIsStreaming] = useState(false);
  // Contract selection modal state
  const [showContractModal, setShowContractModal] = useState(false);
  const [contracts, setContracts] = useState<ContractSummary[]>([]);
  const [contractsLoading, setContractsLoading] = useState(false);
  // Task creation modal (step 2)
  const [modalStep, setModalStep] = useState<1 | 2>(1);
  const [selectedContract, setSelectedContract] = useState<ContractWithRelations | null>(null);
  const [daemonDirectories, setDaemonDirectories] = useState<DaemonDirectory[]>([]);
  const [newTaskName, setNewTaskName] = useState("");
  const [newTaskRepoUrl, setNewTaskRepoUrl] = useState<string | null>(null);
  const [newTaskTargetPath, setNewTaskTargetPath] = useState("");
  // Standalone task repository selection state
  const [standaloneRepoType, setStandaloneRepoType] = useState<"remote" | "local">("remote");
  const [standaloneRepoUrl, setStandaloneRepoUrl] = useState("");
  const [standaloneRepoPath, setStandaloneRepoPath] = useState("");
  const [repoSuggestions, setRepoSuggestions] = useState<RepositoryHistoryEntry[]>([]);
  const [showRepoSuggestions, setShowRepoSuggestions] = useState(false);
  // Track which subtask's output we're viewing (null = parent task)
  const [viewingSubtaskId, setViewingSubtaskId] = useState<string | null>(null);
  const [viewingSubtaskName, setViewingSubtaskName] = useState<string | null>(null);
  // For supervisor tasks: all tasks in the contract (excluding the supervisor itself)
  const [contractTasks, setContractTasks] = useState<TaskSummary[]>([]);
  // Overlay diff content for viewing worktree changes
  const [overlayDiff, setOverlayDiff] = useState<string | undefined>(undefined);
  // View mode for the split panel layout
  const [viewMode, setViewMode] = useState<ViewMode>("split");
  // Width of the task panel as a percentage (0-100)
  const [taskPanelPercent, setTaskPanelPercent] = useState(66.67);
  // Track resizing state
  const [isResizing, setIsResizing] = useState(false);
  const containerRef = useRef<HTMLDivElement>(null);
  // Track which task we've loaded output for to avoid stale saves
  const loadedTaskIdRef = useRef<string | null>(null);

  // Handle task update events from WebSocket
  const handleTaskUpdate = useCallback(async (event: TaskUpdateEvent) => {
    // Always refresh task list when a new task is created (e.g., by supervisor)
    // This ensures newly spawned tasks appear in the sidebar immediately
    const isNewTask = event.updatedFields.includes("created");
    if (isNewTask) {
      fetchTasks();
    }

    // Refresh task list if we're viewing the list (no specific task selected)
    if (!id) {
      if (!isNewTask) {
        // Only fetch if we didn't already fetch for new task creation
        fetchTasks();
      }
      return;
    }

    // Check if this update is for the current task or one of its subtasks
    const isCurrentTask = event.taskId === id;
    const isSubtask = taskDetail?.subtasks.some((st) => st.id === event.taskId);

    // Refresh task detail if the update is for current task or any subtask
    // This ensures subtask status changes (e.g., when orchestrator starts them) are reflected
    if (isCurrentTask || isSubtask) {
      const updated = await fetchTask(id);
      if (updated) {
        setTaskDetail(updated);
      }
    }

    // Update streaming state based on status for current task
    if (isCurrentTask) {
      setIsStreaming(event.status === "running");
    }
  }, [id, fetchTask, fetchTasks, taskDetail?.subtasks]);

  // The task ID whose output we're currently viewing
  const activeOutputTaskId = viewingSubtaskId || id;

  // Handle task output events from WebSocket
  const handleTaskOutput = useCallback((event: TaskOutputEvent) => {
    // Only process output for the task we're currently viewing
    if (event.taskId === activeOutputTaskId) {
      setTaskOutputEntries((prev) => {
        // Deduplicate by checking if last entry is identical
        // This prevents duplicates from React StrictMode or WebSocket reconnects
        const lastEntry = prev[prev.length - 1];
        if (
          lastEntry &&
          lastEntry.messageType === event.messageType &&
          lastEntry.content === event.content &&
          lastEntry.toolName === event.toolName
        ) {
          return prev; // Skip duplicate
        }
        const newEntries = [...prev, event];
        // Persist to localStorage
        savePersistedOutput(event.taskId, newEntries);
        return newEntries;
      });
    }
  }, [activeOutputTaskId]);

  // Handle user input sent to task - show immediately in output
  const handleUserInput = useCallback((message: string) => {
    if (!activeOutputTaskId) return;
    const userEntry: TaskOutputEvent = {
      taskId: activeOutputTaskId,
      messageType: "user_input",
      content: message,
      isPartial: false,
    };
    setTaskOutputEntries((prev) => {
      const newEntries = [...prev, userEntry];
      savePersistedOutput(activeOutputTaskId, newEntries);
      return newEntries;
    });
  }, [activeOutputTaskId]);

  // Subscribe to task updates and output
  // When viewing a subtask's output, subscribe to that instead of the parent
  // Always subscribe to all updates so we see subtask status changes
  const { connected } = useTaskSubscription({
    taskId: id || null,
    subscribeAll: true, // Always subscribe to all - needed to see subtask updates
    subscribeOutput: !!activeOutputTaskId, // Subscribe to output when viewing a task
    outputTaskId: activeOutputTaskId || undefined, // Which task's output to subscribe to
    onUpdate: handleTaskUpdate,
    onOutput: handleTaskOutput,
  });

  // Load persisted output when task or viewed subtask changes
  useEffect(() => {
    if (activeOutputTaskId) {
      // First load from localStorage (instant, for local cache)
      const persisted = loadPersistedOutput(activeOutputTaskId);
      setTaskOutputEntries(persisted);
      loadedTaskIdRef.current = activeOutputTaskId;

      // Then fetch from API to get any output we missed
      // (e.g., subtask was running before we started viewing it)
      getTaskOutput(activeOutputTaskId)
        .then((response) => {
          if (response.entries.length > 0) {
            setTaskOutputEntries((prev) => {
              // API returns all historical entries in chronological order
              const apiEntries = response.entries.map(entry => ({
                taskId: entry.taskId,
                messageType: entry.messageType,
                content: entry.content,
                toolName: entry.toolName,
                toolInput: entry.toolInput,
                isError: entry.isError,
                costUsd: entry.costUsd,
                durationMs: entry.durationMs,
                isPartial: false,
              }));

              // If localStorage is empty, just use API data
              if (prev.length === 0) {
                savePersistedOutput(activeOutputTaskId, apiEntries);
                return apiEntries;
              }

              // localStorage has user_input entries in correct positions - trust its order
              // Only append API entries that we don't already have locally
              const localKeys = new Set(prev.map(e => `${e.messageType}:${e.content}`));
              const newFromApi = apiEntries.filter(e => !localKeys.has(`${e.messageType}:${e.content}`));

              // Keep local order (has user_input in correct spots), append new API data
              const merged = [...prev, ...newFromApi];
              savePersistedOutput(activeOutputTaskId, merged);
              return merged;
            });
          }
        })
        .catch((err) => {
          console.error("Failed to fetch task output:", err);
        });
    } else {
      setTaskOutputEntries([]);
      loadedTaskIdRef.current = null;
    }
    setIsStreaming(false);
  }, [activeOutputTaskId]);

  // Reset subtask view when navigating to a different parent task
  useEffect(() => {
    setViewingSubtaskId(null);
    setViewingSubtaskName(null);
    setOverlayDiff(undefined);
  }, [id]);

  // Toggle viewing a subtask's output (for running subtasks)
  const handleToggleSubtaskOutput = useCallback(
    (subtaskId: string, subtaskName: string) => {
      if (viewingSubtaskId === subtaskId) {
        // Already viewing this subtask, switch back to parent
        setViewingSubtaskId(null);
        setViewingSubtaskName(null);
      } else {
        // Switch to viewing this subtask's output
        setViewingSubtaskId(subtaskId);
        setViewingSubtaskName(subtaskName);
      }
    },
    [viewingSubtaskId]
  );

  // Request diff for the current task
  const handleRequestDiff = useCallback(async () => {
    if (!id) return;
    try {
      const result = await getTaskDiff(id);
      if (result.success && result.diff) {
        setOverlayDiff(result.diff);
      } else {
        setOverlayDiff(result.error || "Failed to get diff");
      }
    } catch (e) {
      console.error("Failed to get diff:", e);
      setOverlayDiff(e instanceof Error ? e.message : "Failed to get diff");
    }
  }, [id]);

  // Load task detail when URL has an id
  useEffect(() => {
    if (id) {
      setDetailLoading(true);
      fetchTask(id).then((detail) => {
        setTaskDetail(detail);
        setDetailLoading(false);
      });
    } else {
      setTaskDetail(null);
    }
  }, [id, fetchTask]);

  // For supervisor tasks: fetch all tasks in the contract (excluding the supervisor itself)
  useEffect(() => {
    if (taskDetail?.isSupervisor && taskDetail.contractId) {
      getContract(taskDetail.contractId)
        .then((contract) => {
          // Filter out the supervisor task itself
          const tasksExcludingSupervisor = contract.tasks.filter(
            (t) => t.id !== taskDetail.id
          );
          setContractTasks(tasksExcludingSupervisor);
        })
        .catch((err) => {
          console.error("Failed to fetch contract tasks for supervisor:", err);
          setContractTasks([]);
        });
    } else {
      setContractTasks([]);
    }
  }, [taskDetail?.isSupervisor, taskDetail?.contractId, taskDetail?.id]);

  // Fetch repository suggestions when standalone task modal is open
  useEffect(() => {
    if (showContractModal && modalStep === 2 && !selectedContract) {
      getRepositorySuggestions(standaloneRepoType, undefined, 10)
        .then((res) => {
          setRepoSuggestions(res.entries);
          setShowRepoSuggestions(res.entries.length > 0);
        })
        .catch(() => {
          setRepoSuggestions([]);
          setShowRepoSuggestions(false);
        });
    } else if (!showContractModal) {
      setRepoSuggestions([]);
      setShowRepoSuggestions(false);
    }
  }, [showContractModal, modalStep, selectedContract, standaloneRepoType]);

  // Apply a repository suggestion
  const applyRepoSuggestion = useCallback((suggestion: RepositoryHistoryEntry) => {
    if (suggestion.repositoryUrl) {
      setStandaloneRepoUrl(suggestion.repositoryUrl);
    }
    if (suggestion.localPath) {
      setStandaloneRepoPath(suggestion.localPath);
    }
    setShowRepoSuggestions(false);
  }, []);

  const handleSelectTask = useCallback(
    (taskId: string) => {
      navigate(`/exec/${taskId}`);
    },
    [navigate]
  );

  const handleBack = useCallback(() => {
    // If viewing a subtask, go back to parent
    if (taskDetail?.parentTaskId) {
      navigate(`/exec/${taskDetail.parentTaskId}`);
    } else {
      navigate("/exec");
    }
  }, [navigate, taskDetail]);

  const handleDelete = useCallback(
    async (taskId: string) => {
      if (confirm("Are you sure you want to delete this task?")) {
        const success = await removeTask(taskId);
        if (success && id === taskId) {
          // If deleting current task, go back
          if (taskDetail?.parentTaskId) {
            navigate(`/exec/${taskDetail.parentTaskId}`);
          } else {
            navigate("/exec");
          }
        }
      }
    },
    [removeTask, id, taskDetail, navigate]
  );

  const handleDismiss = useCallback(
    async (taskId: string) => {
      await hideTask(taskId);
    },
    [hideTask]
  );

  const handleStart = useCallback(
    async (taskId: string) => {
      try {
        const updated = await startTaskApi(taskId);
        setTaskDetail((prev) => prev ? { ...prev, ...updated } : prev);
      } catch (e) {
        console.error("Failed to start task:", e);
        alert(e instanceof Error ? e.message : "Failed to start task");
      }
    },
    []
  );

  const handleStop = useCallback(
    async (taskId: string) => {
      try {
        const updated = await stopTaskApi(taskId);
        setTaskDetail((prev) => prev ? { ...prev, ...updated } : prev);
      } catch (e) {
        console.error("Failed to stop task:", e);
        alert(e instanceof Error ? e.message : "Failed to stop task");
      }
    },
    []
  );

  const handleRestart = useCallback(
    async (taskId: string) => {
      try {
        // First stop the task
        await stopTaskApi(taskId);
        // Then start it again
        const updated = await startTaskApi(taskId);
        setTaskDetail((prev) => prev ? { ...prev, ...updated } : prev);
      } catch (e) {
        console.error("Failed to restart task:", e);
        alert(e instanceof Error ? e.message : "Failed to restart task");
      }
    },
    []
  );

  const handleContinue = useCallback(
    async (taskId: string) => {
      try {
        // Check if this is a supervisor task - use resumeSupervisor API instead
        if (taskDetail?.isSupervisor && taskDetail?.contractId) {
          const result = await resumeSupervisor(taskDetail.contractId, {
            resumeMode: "continue",
          });
          console.log(`[Mesh] Supervisor resumed, daemon: ${result.daemonId}`);
          // Refresh task detail to get updated state
          const updated = await fetchTask(taskId);
          if (updated) {
            setTaskDetail(updated);
          }
        } else {
          // Continue regular task with conversation context from previous run
          const result = await continueTaskApi(taskId);
          console.log(`[Mesh] Task continued with ${result.contextEntries} context entries`);
          setTaskDetail((prev) => prev ? { ...prev, ...result.task } : prev);
        }
      } catch (e) {
        console.error("Failed to continue task:", e);
        alert(e instanceof Error ? e.message : "Failed to continue task");
      }
    },
    [taskDetail?.isSupervisor, taskDetail?.contractId, fetchTask]
  );

  const handleSave = useCallback(
    async (taskId: string, name: string, description: string, plan: string, targetRepoPath?: string, completionAction?: string) => {
      if (!taskDetail) return;
      const result = await editTask(taskId, {
        name,
        description: description || undefined,
        plan,
        targetRepoPath: targetRepoPath || undefined,
        completionAction: completionAction as import("../lib/api").CompletionAction | undefined,
        version: taskDetail.version,
      });
      if (result) {
        setTaskDetail(result);
      }
    },
    [editTask, taskDetail]
  );

  const handleBranch = useCallback(
    async (taskId: string, message: string, name?: string) => {
      try {
        const result = await branchTask(taskId, {
          message,
          name,
          includeConversation: true,
        });
        console.log(`[Mesh] Task branched, new task ID: ${result.task.id}`);
        // Navigate to the new branched task
        navigate(`/exec/${result.task.id}`);
      } catch (e) {
        console.error("Failed to branch task:", e);
        throw e; // Re-throw so the modal can display the error
      }
    },
    [navigate]
  );

  // Open contract selection modal
  const handleCreate = useCallback(async () => {
    if (creating || contractsLoading) return;
    setContractsLoading(true);
    try {
      const [contractsResponse, directoriesResponse] = await Promise.all([
        listContracts(),
        getDaemonDirectories().catch(() => ({ directories: [] })),
      ]);
      setContracts(contractsResponse.contracts);
      setDaemonDirectories(directoriesResponse.directories);
      setModalStep(1);
      setSelectedContract(null);
      setNewTaskName("");
      setNewTaskRepoUrl(null);
      setNewTaskTargetPath("");
      setShowContractModal(true);
    } catch (e) {
      console.error("Failed to load contracts:", e);
    } finally {
      setContractsLoading(false);
    }
  }, [creating, contractsLoading]);

  // Handle contract selection and move to step 2
  const handleSelectContract = useCallback(async (contractSummary: ContractSummary) => {
    try {
      const contract = await getContract(contractSummary.id);
      setSelectedContract(contract);
      setNewTaskName(`Task for ${contract.name}`);
      // Pre-select primary repository if available
      const primaryRepo = contract.repositories.find((r) => r.isPrimary && r.status === "ready");
      if (primaryRepo) {
        setNewTaskRepoUrl(primaryRepo.repositoryUrl);
      } else {
        // Otherwise select first ready repository
        const firstReady = contract.repositories.find((r) => r.status === "ready");
        setNewTaskRepoUrl(firstReady?.repositoryUrl || null);
      }
      setModalStep(2);
    } catch (e) {
      console.error("Failed to load contract details:", e);
    }
  }, []);

  // Handle creating a standalone task (no contract)
  const handleCreateStandaloneTask = useCallback(() => {
    setSelectedContract(null);
    setNewTaskName("Standalone Task");
    setNewTaskRepoUrl(null);
    setNewTaskTargetPath("");
    setStandaloneRepoType("remote");
    setStandaloneRepoUrl("");
    setStandaloneRepoPath("");
    setModalStep(2);
  }, []);

  // Create task with configured options
  const handleCreateTask = useCallback(async () => {
    if (creating) return;
    setShowContractModal(false);
    setCreating(true);
    try {
      // For standalone tasks, use the standalone repo URL/path based on type
      let repoUrl = newTaskRepoUrl;
      let targetPath = newTaskTargetPath;

      if (!selectedContract) {
        // Standalone task - use the standalone repo fields
        if (standaloneRepoType === "remote" && standaloneRepoUrl) {
          repoUrl = standaloneRepoUrl;
        } else if (standaloneRepoType === "local" && standaloneRepoPath) {
          // For local paths, use targetRepoPath instead
          targetPath = standaloneRepoPath;
        }
      }

      const newTask = await saveTask({
        contractId: selectedContract?.id,
        name: newTaskName || (selectedContract ? `Task for ${selectedContract.name}` : "Standalone Task"),
        plan: "# Plan\n\nDescribe what this task should accomplish...",
        repositoryUrl: repoUrl || undefined,
        targetRepoPath: targetPath || undefined,
      });
      if (newTask) {
        navigate(`/exec/${newTask.id}`);
      }
    } finally {
      setCreating(false);
    }
  }, [creating, saveTask, navigate, selectedContract, newTaskName, newTaskRepoUrl, newTaskTargetPath, standaloneRepoType, standaloneRepoUrl, standaloneRepoPath]);

  // Close modal and reset state
  const handleCloseModal = useCallback(() => {
    setShowContractModal(false);
    setModalStep(1);
    setSelectedContract(null);
    setNewTaskName("");
    setNewTaskRepoUrl(null);
    setNewTaskTargetPath("");
    setStandaloneRepoType("remote");
    setStandaloneRepoUrl("");
    setStandaloneRepoPath("");
    setRepoSuggestions([]);
    setShowRepoSuggestions(false);
  }, []);

  // Callback when task is updated via CLI
  const handleTaskUpdatedFromCli = useCallback(async () => {
    if (id) {
      const updated = await fetchTask(id);
      if (updated) {
        setTaskDetail(updated);
      }
    }
    // Also refresh the task list
    fetchTasks();
  }, [id, fetchTask, fetchTasks]);

  // Calculate chat context based on current view
  const chatContext: MeshChatContext = useMemo(() => {
    if (!id) {
      return { type: "mesh" };
    }
    if (taskDetail?.parentTaskId) {
      return { type: "subtask", taskId: id, parentTaskId: taskDetail.parentTaskId };
    }
    return { type: "task", taskId: id };
  }, [id, taskDetail?.parentTaskId]);

  // Handle resizing of the split panel
  const handleResizeStart = useCallback((e: MouseEvent) => {
    e.preventDefault();
    setIsResizing(true);
  }, []);

  useEffect(() => {
    if (!isResizing) return;

    const handleMouseMove = (e: globalThis.MouseEvent) => {
      if (!containerRef.current) return;
      const containerRect = containerRef.current.getBoundingClientRect();
      const containerWidth = containerRect.width;
      const mouseX = e.clientX - containerRect.left;

      // Calculate percentage, respecting minimum widths
      const minTaskPercent = (MIN_TASK_WIDTH / containerWidth) * 100;
      const maxTaskPercent = ((containerWidth - MIN_OUTPUT_WIDTH) / containerWidth) * 100;
      const newPercent = Math.max(minTaskPercent, Math.min(maxTaskPercent, (mouseX / containerWidth) * 100));

      setTaskPanelPercent(newPercent);
    };

    const handleMouseUp = () => {
      setIsResizing(false);
    };

    document.addEventListener("mousemove", handleMouseMove);
    document.addEventListener("mouseup", handleMouseUp);

    return () => {
      document.removeEventListener("mousemove", handleMouseMove);
      document.removeEventListener("mouseup", handleMouseUp);
    };
  }, [isResizing]);

  // Cycle through view modes
  const cycleViewMode = useCallback(() => {
    setViewMode((current) => {
      if (current === "split") return "task";
      if (current === "task") return "output";
      return "split";
    });
  }, []);

  // Get label for current view mode
  const getViewModeLabel = (mode: ViewMode): string => {
    switch (mode) {
      case "split": return "Split";
      case "task": return "Task";
      case "output": return "Output";
    }
  };

  // Show loading state while checking auth
  if (authLoading) {
    return (
      <div className="relative z-10 h-screen flex flex-col overflow-hidden">
        <Masthead showTicker={false} showNav />
        <main className="flex-1 flex items-center justify-center">
          <div className="font-mono text-[#9bc3ff] text-sm">Loading...</div>
        </main>
      </div>
    );
  }

  // Don't render content if not authenticated (redirect will happen via useEffect)
  if (isAuthConfigured && !isAuthenticated) {
    return (
      <div className="relative z-10 h-screen flex flex-col overflow-hidden">
        <Masthead showTicker={false} showNav />
        <main className="flex-1 flex items-center justify-center">
          <div className="font-mono text-[#9bc3ff] text-sm">Redirecting to login...</div>
        </main>
      </div>
    );
  }

  return (
    <div className="relative z-10 h-screen flex flex-col overflow-hidden">
      <Masthead showTicker={false} showNav />

      <main className="flex-1 p-4 md:p-6 min-h-0 overflow-hidden flex flex-col">
        {error && (
          <div className="mb-4 p-3 border border-red-400/50 bg-red-400/10 text-red-400 font-mono text-sm shrink-0">
            {error}
          </div>
        )}

        {conflict?.hasConflict && (
          <div className="mb-4 p-3 border border-yellow-400/50 bg-yellow-400/10 text-yellow-400 font-mono text-sm shrink-0">
            <p>Version conflict detected. Please reload and try again.</p>
            <button
              onClick={clearConflict}
              className="mt-2 px-3 py-1 border border-yellow-400/30 hover:border-yellow-400/50 text-xs uppercase"
            >
              Dismiss
            </button>
          </div>
        )}

        {/* Main content area - conditional based on route */}
        <div className="flex-1 flex flex-col min-h-0 overflow-hidden gap-4">
          {id && taskDetail ? (
            <>
              {/* Header with connection status and view toggle */}
              <div className="flex items-center justify-between shrink-0">
                <div className="flex items-center gap-2">
                  <span
                    className={`w-2 h-2 rounded-full ${
                      connected ? "bg-green-400" : "bg-yellow-400 animate-pulse"
                    }`}
                  />
                  <span className="font-mono text-[10px] text-[#75aafc] uppercase">
                    {connected ? "Connected" : "Connecting..."}
                  </span>
                </div>
                {/* View mode toggle */}
                <button
                  onClick={cycleViewMode}
                  className="px-3 py-1 font-mono text-[10px] text-[#9bc3ff] border border-[rgba(117,170,252,0.25)] hover:border-[#3f6fb3] transition-colors uppercase"
                >
                  View: {getViewModeLabel(viewMode)}
                </button>
              </div>

              {/* Split panel layout */}
              <div
                ref={containerRef}
                className={`flex-1 flex min-h-0 overflow-hidden ${isResizing ? "select-none" : ""}`}
              >
                {/* Task detail panel */}
                {(viewMode === "split" || viewMode === "task") && (
                  <div
                    className="min-h-0 overflow-hidden"
                    style={{
                      width: viewMode === "split" ? `${taskPanelPercent}%` : "100%",
                      flexShrink: 0,
                    }}
                  >
                    <TaskDetail
                      task={taskDetail}
                      loading={detailLoading}
                      onBack={handleBack}
                      onSave={handleSave}
                      onDelete={handleDelete}
                      onStart={handleStart}
                      onStop={handleStop}
                      onRestart={handleRestart}
                      onContinue={handleContinue}
                      onSelectSubtask={handleSelectTask}
                      onToggleSubtaskOutput={handleToggleSubtaskOutput}
                      viewingSubtaskId={viewingSubtaskId}
                      onViewContract={(contractId) => navigate(`/contracts/${contractId}`)}
                      onBranch={handleBranch}
                      contractTasks={taskDetail.isSupervisor ? contractTasks : undefined}
                      overlayDiff={overlayDiff}
                      onRequestDiff={handleRequestDiff}
                    />
                  </div>
                )}

                {/* Resizable divider */}
                {viewMode === "split" && (
                  <div
                    className="w-1 shrink-0 cursor-col-resize bg-[rgba(117,170,252,0.15)] hover:bg-[rgba(117,170,252,0.35)] transition-colors group flex items-center justify-center"
                    onMouseDown={handleResizeStart}
                  >
                    <div className="w-0.5 h-8 bg-[rgba(117,170,252,0.3)] group-hover:bg-[rgba(117,170,252,0.5)] rounded-full" />
                  </div>
                )}

                {/* Output panel */}
                {(viewMode === "split" || viewMode === "output") && (
                  <div
                    className="panel min-h-0 overflow-hidden flex-1 flex flex-col"
                  >
                    {/* Contract complete questions - shown prominently at top */}
                    {contractCompleteQuestionsForTask.length > 0 && (
                      <div className="shrink-0 px-3 pt-3">
                        {contractCompleteQuestionsForTask.map((question) => (
                          <ContractCompleteQuestion
                            key={question.questionId}
                            question={question}
                            onAnswer={handleAnswerQuestion}
                          />
                        ))}
                      </div>
                    )}
                    <div className="flex-1 min-h-0 overflow-hidden">
                      <TaskOutput
                        entries={taskOutputEntries}
                        isStreaming={isStreaming || taskDetail.status === "running" || taskDetail.status === "starting"}
                        viewingSubtaskName={viewingSubtaskName}
                        onClearSubtaskView={viewingSubtaskId ? () => {
                          setViewingSubtaskId(null);
                          setViewingSubtaskName(null);
                        } : undefined}
                        onClear={() => {
                          setTaskOutputEntries([]);
                          if (activeOutputTaskId) {
                            clearPersistedOutput(activeOutputTaskId);
                          }
                        }}
                        taskId={activeOutputTaskId}
                        onUserInput={handleUserInput}
                        pendingQuestionIds={pendingQuestionIds}
                        onAnswerQuestion={handleAnswerQuestion}
                      />
                    </div>
                  </div>
                )}
              </div>
            </>
          ) : id && detailLoading ? (
            <div className="panel flex-1 flex items-center justify-center">
              <div className="font-mono text-[#9bc3ff] text-sm">Loading...</div>
            </div>
          ) : (
            <div className="flex-1 min-h-0 overflow-hidden">
              <TaskList
                tasks={tasks}
                loading={loading || creating}
                onSelect={handleSelectTask}
                onDelete={handleDelete}
                onDismiss={handleDismiss}
                onCreate={handleCreate}
              />
            </div>
          )}

          {/* Mesh Chat Input - always rendered to persist state across navigation */}
          <div className="shrink-0">
            <UnifiedMeshChatInput
              context={chatContext}
              onUpdate={id ? handleTaskUpdatedFromCli : fetchTasks}
            />
          </div>
        </div>
      </main>

      {/* Task Creation Modal (Two Steps) */}
      {showContractModal && (
        <div className="fixed inset-0 bg-black/70 flex items-center justify-center z-50">
          <div className="bg-[#0d1b2d] border border-[rgba(117,170,252,0.35)] max-w-lg w-full mx-4 max-h-[80vh] flex flex-col">
            <div className="p-4 border-b border-[rgba(117,170,252,0.15)] flex justify-between items-center">
              <div className="flex items-center gap-2">
                {modalStep === 2 && (
                  <button
                    onClick={() => setModalStep(1)}
                    className="text-[#7788aa] hover:text-[#9bc3ff] transition-colors"
                    title="Back to contract selection"
                  >
                    <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 19l-7-7 7-7" />
                    </svg>
                  </button>
                )}
                <h2 className="text-sm font-mono uppercase tracking-wide text-[#9bc3ff]">
                  {modalStep === 1 ? "Select Contract" : "Configure Task"}
                </h2>
              </div>
              <button
                onClick={handleCloseModal}
                className="text-[#7788aa] hover:text-[#9bc3ff] transition-colors"
              >
                <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                </svg>
              </button>
            </div>
            <div className="p-4 overflow-y-auto flex-1">
              {modalStep === 1 ? (
                // Step 1: Select Contract
                <div className="space-y-4">
                  {/* Standalone task option */}
                  <div className="pb-3 border-b border-[rgba(117,170,252,0.25)]">
                    <button
                      onClick={handleCreateStandaloneTask}
                      className="w-full text-left p-3 border border-[rgba(117,170,252,0.25)] bg-[#0a1525] hover:border-[#3f6fb3] hover:bg-[#0d1b2d] transition-colors"
                    >
                      <div className="flex items-center gap-2">
                        <svg className="w-4 h-4 text-[#9bc3ff]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 10V3L4 14h7v7l9-11h-7z" />
                        </svg>
                        <span className="text-[#9bc3ff] font-mono text-xs font-medium">Create Standalone Task</span>
                      </div>
                      <p className="text-[10px] font-mono text-[#7788aa] mt-1 ml-6">
                        Quick task without a contract. Good for one-off tasks.
                      </p>
                    </button>
                  </div>

                  {/* Contract selection */}
                  {contracts.length === 0 ? (
                    <div className="text-center py-4">
                      <p className="text-[#7788aa] font-mono text-xs mb-4">No contracts found.</p>
                      <button
                        onClick={() => {
                          handleCloseModal();
                          navigate("/contracts");
                        }}
                        className="px-4 py-2 bg-[#3f6fb3] border border-[#75aafc] text-white font-mono text-xs uppercase tracking-wide hover:bg-[#4a7fc3] transition-colors"
                      >
                        Create Contract
                      </button>
                    </div>
                  ) : (
                    <div className="space-y-2">
                      <div className="text-[10px] font-mono uppercase tracking-wide text-[#7788aa] px-1">
                        Or select a contract:
                      </div>
                      {contracts.map((contract) => (
                        <button
                          key={contract.id}
                          onClick={() => handleSelectContract(contract)}
                          className="w-full text-left p-3 border border-[rgba(117,170,252,0.15)] bg-[#0a1525] hover:border-[rgba(117,170,252,0.35)] hover:bg-[#0d1b2d] transition-colors"
                        >
                          <div className="flex items-center justify-between">
                            <span className="text-[#9bc3ff] font-mono text-xs">{contract.name}</span>
                            <span className="text-[10px] font-mono uppercase px-2 py-0.5 border border-[rgba(117,170,252,0.25)] text-[#75aafc]">
                              {contract.phase}
                            </span>
                          </div>
                          {contract.description && (
                            <p className="text-[10px] font-mono text-[#7788aa] mt-1 line-clamp-2">{contract.description}</p>
                          )}
                          <div className="flex gap-3 mt-2 text-[10px] font-mono text-[#556677]">
                            <span>{contract.taskCount} tasks</span>
                            <span>{contract.repositoryCount} repos</span>
                          </div>
                        </button>
                      ))}
                    </div>
                  )}
                </div>
              ) : (
                // Step 2: Configure Task
                <div className="space-y-4">
                  {/* Context badge */}
                  <div className="flex items-center gap-2 text-xs font-mono text-[#7788aa]">
                    {selectedContract ? (
                      <>
                        <span>Contract:</span>
                        <span className="text-[#9bc3ff]">{selectedContract.name}</span>
                      </>
                    ) : (
                      <>
                        <svg className="w-4 h-4 text-[#9bc3ff]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 10V3L4 14h7v7l9-11h-7z" />
                        </svg>
                        <span className="text-[#9bc3ff]">Standalone Task</span>
                      </>
                    )}
                  </div>

                  {/* Task name */}
                  <div className="space-y-1">
                    <label className="block text-[10px] font-mono uppercase tracking-wide text-[#7788aa]">Task Name</label>
                    <input
                      type="text"
                      value={newTaskName}
                      onChange={(e) => setNewTaskName(e.target.value)}
                      className="w-full bg-[#0a1525] border border-[rgba(117,170,252,0.25)] text-[#dbe7ff] font-mono text-sm px-3 py-2 outline-none focus:border-[#3f6fb3]"
                      placeholder="Task name"
                    />
                  </div>

                  {/* Repository selection - for contract tasks */}
                  {selectedContract && selectedContract.repositories.length > 0 && (
                    <div className="space-y-1">
                      <label className="block text-[10px] font-mono uppercase tracking-wide text-[#7788aa]">Repository</label>
                      <select
                        value={newTaskRepoUrl || ""}
                        onChange={(e) => setNewTaskRepoUrl(e.target.value || null)}
                        className="w-full bg-[#0a1525] border border-[rgba(117,170,252,0.25)] text-[#dbe7ff] font-mono text-sm px-3 py-2 outline-none focus:border-[#3f6fb3]"
                      >
                        <option value="">No repository</option>
                        {selectedContract.repositories
                          .filter((r) => r.status === "ready")
                          .map((repo) => (
                            <option key={repo.id} value={repo.repositoryUrl || repo.localPath || ""}>
                              {repo.name}
                              {repo.isPrimary && " (primary)"}
                            </option>
                          ))}
                      </select>
                      <p className="text-[10px] font-mono text-[#556677]">
                        The repository this task will work on.
                      </p>
                    </div>
                  )}

                  {/* Repository selection - for standalone tasks */}
                  {!selectedContract && (
                    <div className="space-y-3">
                      {/* Repository type selector */}
                      <div className="space-y-1">
                        <label className="block text-[10px] font-mono uppercase tracking-wide text-[#7788aa]">Repository Type (optional)</label>
                        <div className="flex gap-2">
                          <button
                            type="button"
                            onClick={() => setStandaloneRepoType("remote")}
                            className={`flex-1 px-3 py-2 font-mono text-xs uppercase transition-colors ${
                              standaloneRepoType === "remote"
                                ? "bg-[#0f3c78] text-[#dbe7ff] border border-[#75aafc]"
                                : "bg-[#0d1b2d] text-[#8b949e] border border-[#3f6fb3] hover:border-[#75aafc]"
                            }`}
                          >
                            Remote
                          </button>
                          <button
                            type="button"
                            onClick={() => setStandaloneRepoType("local")}
                            className={`flex-1 px-3 py-2 font-mono text-xs uppercase transition-colors ${
                              standaloneRepoType === "local"
                                ? "bg-[#0f3c78] text-[#dbe7ff] border border-[#75aafc]"
                                : "bg-[#0d1b2d] text-[#8b949e] border border-[#3f6fb3] hover:border-[#75aafc]"
                            }`}
                          >
                            Local
                          </button>
                        </div>
                      </div>

                      {/* Repository suggestions */}
                      {showRepoSuggestions && repoSuggestions.length > 0 && (
                        <div className="space-y-1">
                          <label className="block text-[10px] font-mono uppercase tracking-wide text-[#7788aa]">
                            Recent Repositories
                          </label>
                          <div className="border border-[rgba(117,170,252,0.2)] bg-[#0a1525] max-h-32 overflow-y-auto">
                            {repoSuggestions.map((suggestion) => (
                              <button
                                key={suggestion.id}
                                type="button"
                                onClick={() => applyRepoSuggestion(suggestion)}
                                className="w-full text-left px-3 py-2 font-mono text-xs hover:bg-[rgba(117,170,252,0.1)] border-b border-[rgba(117,170,252,0.1)] last:border-b-0"
                              >
                                <div className="flex items-center justify-between">
                                  <span className="text-[#9bc3ff] truncate">{suggestion.name}</span>
                                  <span className="text-[10px] text-[#556677] ml-2">
                                    {suggestion.useCount}×
                                  </span>
                                </div>
                                <div className="text-[10px] text-[#556677] truncate">
                                  {standaloneRepoType === "local" ? suggestion.localPath : suggestion.repositoryUrl}
                                </div>
                              </button>
                            ))}
                          </div>
                        </div>
                      )}

                      {/* Repository URL (for remote) */}
                      {standaloneRepoType === "remote" && (
                        <div className="space-y-1">
                          <label className="block text-[10px] font-mono uppercase tracking-wide text-[#7788aa]">
                            Repository URL
                          </label>
                          <input
                            type="text"
                            value={standaloneRepoUrl}
                            onChange={(e) => setStandaloneRepoUrl(e.target.value)}
                            placeholder="https://github.com/user/repo.git"
                            className="w-full bg-[#0a1525] border border-[rgba(117,170,252,0.25)] text-[#dbe7ff] font-mono text-sm px-3 py-2 outline-none focus:border-[#3f6fb3]"
                          />
                          <p className="text-[10px] font-mono text-[#556677]">
                            The remote repository this task will clone and work on.
                          </p>
                        </div>
                      )}

                      {/* Repository path (for local) */}
                      {standaloneRepoType === "local" && (
                        <div className="space-y-1">
                          <label className="block text-[10px] font-mono uppercase tracking-wide text-[#7788aa]">
                            Local Path
                          </label>
                          <DirectoryInput
                            value={standaloneRepoPath}
                            onChange={setStandaloneRepoPath}
                            suggestions={daemonDirectories}
                            placeholder="/path/to/your/local/repo"
                          />
                          <p className="text-[10px] font-mono text-[#556677]">
                            The local directory this task will work in.
                          </p>
                        </div>
                      )}
                    </div>
                  )}

                  {/* Target repo path with DirectoryInput - only for contract tasks when repo is selected */}
                  {selectedContract && newTaskRepoUrl && (
                    <div className="space-y-1">
                      <label className="block text-[10px] font-mono uppercase tracking-wide text-[#7788aa]">
                        Target Repository Path
                      </label>
                      <DirectoryInput
                        value={newTaskTargetPath}
                        onChange={setNewTaskTargetPath}
                        suggestions={daemonDirectories}
                        placeholder="/path/to/your/local/repo"
                        repoUrl={newTaskRepoUrl || undefined}
                      />
                      <p className="text-[10px] font-mono text-[#556677]">
                        Path where the task will push/merge changes. Leave empty to configure later.
                      </p>
                    </div>
                  )}

                  {/* Create button */}
                  <div className="pt-2">
                    <button
                      onClick={handleCreateTask}
                      disabled={creating}
                      className="w-full px-4 py-2 bg-[#3f6fb3] border border-[#75aafc] text-white font-mono text-xs uppercase tracking-wide hover:bg-[#4a7fc3] disabled:opacity-50 transition-colors"
                    >
                      {creating ? "Creating..." : "Create Task"}
                    </button>
                  </div>
                </div>
              )}
            </div>
          </div>
        </div>
      )}
    </div>
  );
}