@legendapp/spark 0.0.1-next.7 → 0.0.1-next.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/package.json +8 -8
  2. package/schema.json +9 -0
  3. package/skills/spark/SKILL.md +1 -1
  4. package/skills/spark/UPGRADING.md +34 -0
  5. package/vendor/node_modules/@legendapp/spark-ai/package.json +3 -3
  6. package/vendor/node_modules/@legendapp/spark-audio/package.json +2 -2
  7. package/vendor/node_modules/@legendapp/spark-auth-session/package.json +3 -3
  8. package/vendor/node_modules/@legendapp/spark-cli/dist/add-desktop.js +1 -1
  9. package/vendor/node_modules/@legendapp/spark-cli/dist/build.js +16 -4
  10. package/vendor/node_modules/@legendapp/spark-cli/dist/create.js +16 -4
  11. package/vendor/node_modules/@legendapp/spark-cli/dist/init-template.cjs +2 -1
  12. package/vendor/node_modules/@legendapp/spark-cli/dist/macos-release.js +4 -0
  13. package/vendor/node_modules/@legendapp/spark-cli/dist/package-manager-config.js +3 -1
  14. package/vendor/node_modules/@legendapp/spark-cli/dist/project.js +10 -4
  15. package/vendor/node_modules/@legendapp/spark-cli/dist/release-driver.js +35 -0
  16. package/vendor/node_modules/@legendapp/spark-cli/dist/release.json +18 -18
  17. package/vendor/node_modules/@legendapp/spark-cli/dist/updates.js +18 -8
  18. package/vendor/node_modules/@legendapp/spark-cli/dist/windows.js +1 -1
  19. package/vendor/node_modules/@legendapp/spark-cli/package.json +4 -4
  20. package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/app.json +5 -1
  21. package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/gitignore +2 -1
  22. package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/package.json +8 -7
  23. package/vendor/node_modules/@legendapp/spark-cli/templates/universal/app.json +5 -1
  24. package/vendor/node_modules/@legendapp/spark-cli/templates/universal/gitignore +2 -1
  25. package/vendor/node_modules/@legendapp/spark-cli/templates/universal/package.json +9 -8
  26. package/vendor/node_modules/@legendapp/spark-cli/templates/windows/app.json +5 -1
  27. package/vendor/node_modules/@legendapp/spark-cli/templates/windows/gitignore +2 -1
  28. package/vendor/node_modules/@legendapp/spark-cli/templates/windows/package.json +9 -8
  29. package/vendor/node_modules/@legendapp/spark-clipboard/package.json +2 -2
  30. package/vendor/node_modules/@legendapp/spark-codex/cpp/HybridCodexAppServer.mm +88 -76
  31. package/vendor/node_modules/@legendapp/spark-codex/package.json +2 -2
  32. package/vendor/node_modules/@legendapp/spark-commands/package.json +5 -5
  33. package/vendor/node_modules/@legendapp/spark-context-menu/package.json +2 -2
  34. package/vendor/node_modules/@legendapp/spark-desktop-app/RNDesktopApp.podspec +12 -1
  35. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/RNDesktopApp.mm +8 -1
  36. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkDesktop.h +8 -0
  37. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkDesktop.mm +84 -21
  38. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkInstance.h +27 -0
  39. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkInstance.mm +207 -0
  40. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkTestDriver.h +44 -0
  41. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkTestDriver.mm +349 -0
  42. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkWindow.mm +3 -1
  43. package/vendor/node_modules/@legendapp/spark-desktop-app/package.json +1 -1
  44. package/vendor/node_modules/@legendapp/spark-desktop-app/src/api.ts +39 -6
  45. package/vendor/node_modules/@legendapp/spark-desktop-app/src/contracts/menu.ts +21 -7
  46. package/vendor/node_modules/@legendapp/spark-desktop-config/accessibility-state.cjs +50 -0
  47. package/vendor/node_modules/@legendapp/spark-desktop-config/app.plugin.cjs +9 -1
  48. package/vendor/node_modules/@legendapp/spark-desktop-config/autolinking.cjs +8 -1
  49. package/vendor/node_modules/@legendapp/spark-desktop-config/config.cjs +1 -1
  50. package/vendor/node_modules/@legendapp/spark-desktop-config/config.d.cts +7 -1
  51. package/vendor/node_modules/@legendapp/spark-desktop-config/package.json +4 -4
  52. package/vendor/node_modules/@legendapp/spark-desktop-config/rtl-scroll-view.cjs +144 -0
  53. package/vendor/node_modules/@legendapp/spark-desktop-config/rtl-text-alignment.cjs +60 -0
  54. package/vendor/node_modules/@legendapp/spark-desktop-config/rtl-text-input-alignment.cjs +112 -0
  55. package/vendor/node_modules/@legendapp/spark-desktop-config/schema.json +9 -0
  56. package/vendor/node_modules/@legendapp/spark-desktop-config/test-driver.cjs +11 -0
  57. package/vendor/node_modules/@legendapp/spark-desktop-config/test-driver.d.cts +2 -0
  58. package/vendor/node_modules/@legendapp/spark-desktop-config/text-input-accessibility.cjs +47 -0
  59. package/vendor/node_modules/@legendapp/spark-desktop-config/updates.cjs +4 -2
  60. package/vendor/node_modules/@legendapp/spark-desktop-config/validation.cjs +2 -1
  61. package/vendor/node_modules/@legendapp/spark-desktop-config/windows.plugin.cjs +1 -1
  62. package/vendor/node_modules/@legendapp/spark-desktop-host/AppDelegate.mm +18 -6
  63. package/vendor/node_modules/@legendapp/spark-desktop-host/package.json +2 -2
  64. package/vendor/node_modules/@legendapp/spark-desktop-host/windows/application.inc +61 -15
  65. package/vendor/node_modules/@legendapp/spark-desktop-host/windows/launch.inc +67 -0
  66. package/vendor/node_modules/@legendapp/spark-desktop-links/package.json +2 -2
  67. package/vendor/node_modules/@legendapp/spark-desktop-shortcuts/package.json +2 -2
  68. package/vendor/node_modules/@legendapp/spark-desktop-windows/RNDesktopWindows.podspec +3 -0
  69. package/vendor/node_modules/@legendapp/spark-desktop-windows/macos/RNDesktopWindows.mm +5 -2
  70. package/vendor/node_modules/@legendapp/spark-desktop-windows/macos/window-manager/RNWindowManager.mm +22 -3
  71. package/vendor/node_modules/@legendapp/spark-desktop-windows/package.json +3 -3
  72. package/vendor/node_modules/@legendapp/spark-documents/package.json +7 -7
  73. package/vendor/node_modules/@legendapp/spark-drag-drop/package.json +2 -2
  74. package/vendor/node_modules/@legendapp/spark-file-dialog/RNFileDialog.podspec +1 -0
  75. package/vendor/node_modules/@legendapp/spark-file-dialog/ios/RNFileDialog.mm +153 -64
  76. package/vendor/node_modules/@legendapp/spark-file-dialog/package.json +2 -2
  77. package/vendor/node_modules/@legendapp/spark-file-dialog/src/index.ts +19 -7
  78. package/vendor/node_modules/@legendapp/spark-file-dialog/src/native.ts +5 -1
  79. package/vendor/node_modules/@legendapp/spark-file-dialog/src/options.ts +42 -4
  80. package/vendor/node_modules/@legendapp/spark-file-dialog/src/types.ts +18 -2
  81. package/vendor/node_modules/@legendapp/spark-file-system/package.json +2 -2
  82. package/vendor/node_modules/@legendapp/spark-global-shortcuts/package.json +2 -2
  83. package/vendor/node_modules/@legendapp/spark-html/package.json +2 -2
  84. package/vendor/node_modules/@legendapp/spark-markdown/package.json +2 -2
  85. package/vendor/node_modules/@legendapp/spark-markdown/src/Markdown.tsx +2 -1
  86. package/vendor/node_modules/@legendapp/spark-message-dialog/macos/RNDesktopMessageDialog.mm +113 -12
  87. package/vendor/node_modules/@legendapp/spark-message-dialog/package.json +2 -2
  88. package/vendor/node_modules/@legendapp/spark-message-dialog/src/index.ts +110 -14
  89. package/vendor/node_modules/@legendapp/spark-native-menu/ios/RNNativeMenu.mm +116 -8
  90. package/vendor/node_modules/@legendapp/spark-native-menu/package.json +2 -2
  91. package/vendor/node_modules/@legendapp/spark-native-menu/src/api.ts +63 -18
  92. package/vendor/node_modules/@legendapp/spark-native-menu/src/hooks.ts +9 -3
  93. package/vendor/node_modules/@legendapp/spark-notifications/package.json +2 -2
  94. package/vendor/node_modules/@legendapp/spark-processes/package.json +2 -2
  95. package/vendor/node_modules/@legendapp/spark-rich-text/package.json +2 -2
  96. package/vendor/node_modules/@legendapp/spark-secure-storage/package.json +2 -2
  97. package/vendor/node_modules/@legendapp/spark-settings/package.json +3 -3
  98. package/vendor/node_modules/@legendapp/spark-settings-ui/package.json +4 -4
  99. package/vendor/node_modules/@legendapp/spark-settings-ui/src/index.tsx +5 -2
  100. package/vendor/node_modules/@legendapp/spark-sqlite/package.json +3 -3
  101. package/vendor/node_modules/@legendapp/spark-system/macos/RNDesktopSystem.mm +13 -3
  102. package/vendor/node_modules/@legendapp/spark-system/macos/SparkAppearancePreferences.h +9 -0
  103. package/vendor/node_modules/@legendapp/spark-system/macos/SparkAppearancePreferences.mm +31 -0
  104. package/vendor/node_modules/@legendapp/spark-system/macos/SparkDockTile.h +24 -0
  105. package/vendor/node_modules/@legendapp/spark-system/macos/SparkDockTile.mm +101 -0
  106. package/vendor/node_modules/@legendapp/spark-system/package.json +2 -2
  107. package/vendor/node_modules/@legendapp/spark-system/src/index.ts +73 -7
  108. package/vendor/node_modules/@legendapp/spark-tray/package.json +2 -2
  109. package/vendor/node_modules/@legendapp/spark-ui/macos/RNSparkButton.mm +2 -0
  110. package/vendor/node_modules/@legendapp/spark-ui/macos/RNSparkLayoutDirection.h +17 -0
  111. package/vendor/node_modules/@legendapp/spark-ui/macos/RNSparkTextInput.mm +6 -1
  112. package/vendor/node_modules/@legendapp/spark-ui/macos/appkit-split-view/RNSidebarSplitViewComponent.mm +55 -8
  113. package/vendor/node_modules/@legendapp/spark-ui/macos/glass-effect-view/RNGlassEffectView.mm +11 -7
  114. package/vendor/node_modules/@legendapp/spark-ui/macos/native-select/RNNativeSegmentedControl.mm +4 -0
  115. package/vendor/node_modules/@legendapp/spark-ui/macos/native-select/RNNativeSelect.mm +4 -0
  116. package/vendor/node_modules/@legendapp/spark-ui/macos/sf-symbol/RNSFSymbol.mm +4 -0
  117. package/vendor/node_modules/@legendapp/spark-ui/macos/sidebar/RNSidebar.mm +77 -14
  118. package/vendor/node_modules/@legendapp/spark-ui/macos/swipe-actions/RNSwipeActions.mm +31 -7
  119. package/vendor/node_modules/@legendapp/spark-ui/macos/text-input-search/RNTextInputSearch.mm +25 -1
  120. package/vendor/node_modules/@legendapp/spark-ui/package.json +2 -2
  121. package/vendor/node_modules/@legendapp/spark-ui/src/sidebar/SidebarNativeComponent.ts +5 -0
  122. package/vendor/node_modules/@legendapp/spark-ui/src/sidebar/index.tsx +19 -8
  123. package/vendor/node_modules/@legendapp/spark-updates/macos/RNDesktopUpdates.mm +25 -13
  124. package/vendor/node_modules/@legendapp/spark-updates/macos/SparkUpdateState.h +39 -0
  125. package/vendor/node_modules/@legendapp/spark-updates/package.json +2 -2
  126. package/vendor/node_modules/@legendapp/spark-updates/src/index.ts +45 -9
  127. package/vendor/node_modules/@legendapp/spark-webview/package.json +2 -2
  128. package/vendor/node_modules/@legendapp/spark-window-options/package.json +1 -1
@@ -13,13 +13,13 @@
13
13
  "postinstall": "node node_modules/@legendapp/spark/init-template.cjs"
14
14
  },
15
15
  "dependencies": {
16
- "@legendapp/spark": "0.0.1-next.7",
16
+ "@legendapp/spark": "0.0.1-next.9",
17
17
  "expo": "54.0.37",
18
- "expo-desktop": "1.0.0",
19
- "expo-desktop-prebuild-config": "1.1.0",
20
- "expo-desktop-config-plugins": "1.2.0",
21
- "expo-desktop-metro-config": "54.81.0",
22
- "expo-desktop-template-bare-minimum": "54.81.1",
18
+ "expo-desktop": "1.0.6",
19
+ "expo-desktop-prebuild-config": "1.1.3",
20
+ "expo-desktop-config-plugins": "1.3.2",
21
+ "expo-desktop-metro-config": "54.81.3",
22
+ "expo-desktop-template-bare-minimum": "54.81.6",
23
23
  "react": "19.1.4",
24
24
  "react-dom": "19.1.4",
25
25
  "react-native": "0.81.6",
@@ -30,6 +30,7 @@
30
30
  "@react-native-runtimes/core": "0.1.0-alpha.2"
31
31
  },
32
32
  "overrides": {
33
+ "expo": "54.0.37",
33
34
  "@expo/cli": "54.0.27",
34
35
  "react-native-nitro-modules": "0.37.0",
35
36
  "react-native-webview": "16.0.0",
@@ -41,7 +42,7 @@
41
42
  "expo-keep-awake": "15.0.8",
42
43
  "expo-modules-core": "3.0.30",
43
44
  "expo-desktop-modules-core": "54.0.14",
44
- "expo-desktop-stubs": "54.0.14",
45
+ "expo-desktop-stubs": "54.0.15",
45
46
  "react": "19.1.4",
46
47
  "react-native": "0.81.6",
47
48
  "react-native-macos": "0.81.7",
@@ -10,6 +10,10 @@
10
10
  "macos",
11
11
  "windows"
12
12
  ],
13
- "icon": "./assets/icon.png"
13
+ "icon": "./assets/icon.png",
14
+ "macos": {
15
+ "version": "0.0.1",
16
+ "buildNumber": "1"
17
+ }
14
18
  }
15
19
  }
@@ -1,6 +1,7 @@
1
1
  node_modules/
2
2
  .threaded-runtime/
3
- .spark/
3
+ .spark/*
4
+ !.spark/version
4
5
  macos/
5
6
  dist/
6
7
  app.json
@@ -18,11 +18,11 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "expo": "54.0.37",
21
- "expo-desktop": "1.0.0",
22
- "expo-desktop-prebuild-config": "1.1.0",
23
- "expo-desktop-config-plugins": "1.2.0",
24
- "expo-desktop-metro-config": "54.81.0",
25
- "expo-desktop-template-bare-minimum": "54.81.1",
21
+ "expo-desktop": "1.0.6",
22
+ "expo-desktop-prebuild-config": "1.1.3",
23
+ "expo-desktop-config-plugins": "1.3.2",
24
+ "expo-desktop-metro-config": "54.81.3",
25
+ "expo-desktop-template-bare-minimum": "54.81.6",
26
26
  "react": "19.1.4",
27
27
  "react-native": "0.81.6",
28
28
  "react-native-macos": "0.81.7",
@@ -31,7 +31,7 @@
31
31
  "@types/react": "19.1.10",
32
32
  "react-native-windows": "0.81.35",
33
33
  "expo-desktop-modules-core": "54.0.14",
34
- "expo-desktop-stubs": "54.0.14",
34
+ "expo-desktop-stubs": "54.0.15",
35
35
  "@expo/ui": "0.2.0-beta.9",
36
36
  "expo-dev-client": "6.0.21",
37
37
  "expo-clipboard": "8.0.8",
@@ -41,9 +41,10 @@
41
41
  "react-native-web": "0.21.0",
42
42
  "uniwind": "1.6.3",
43
43
  "tailwindcss": "4.2.4",
44
- "@legendapp/spark": "0.0.1-next.7"
44
+ "@legendapp/spark": "0.0.1-next.9"
45
45
  },
46
46
  "overrides": {
47
+ "expo": "54.0.37",
47
48
  "@expo/cli": "54.0.27",
48
49
  "react-native-nitro-modules": "0.37.0",
49
50
  "react-native-webview": "16.0.0",
@@ -55,7 +56,7 @@
55
56
  "expo-keep-awake": "15.0.8",
56
57
  "expo-modules-core": "3.0.30",
57
58
  "expo-desktop-modules-core": "54.0.14",
58
- "expo-desktop-stubs": "54.0.14",
59
+ "expo-desktop-stubs": "54.0.15",
59
60
  "react": "19.1.4",
60
61
  "react-native": "0.81.6",
61
62
  "react-native-macos": "0.81.7",
@@ -6,6 +6,10 @@
6
6
  "platforms": [
7
7
  "windows"
8
8
  ],
9
- "icon": "./assets/icon.png"
9
+ "icon": "./assets/icon.png",
10
+ "macos": {
11
+ "version": "0.0.1",
12
+ "buildNumber": "1"
13
+ }
10
14
  }
11
15
  }
@@ -1,6 +1,7 @@
1
1
  node_modules/
2
2
  .threaded-runtime/
3
- .spark/
3
+ .spark/*
4
+ !.spark/version
4
5
  macos/
5
6
  dist/
6
7
  app.json
@@ -14,11 +14,11 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "expo": "54.0.37",
17
- "expo-desktop": "1.0.0",
18
- "expo-desktop-prebuild-config": "1.1.0",
19
- "expo-desktop-config-plugins": "1.2.0",
20
- "expo-desktop-metro-config": "54.81.0",
21
- "expo-desktop-template-bare-minimum": "54.81.1",
17
+ "expo-desktop": "1.0.6",
18
+ "expo-desktop-prebuild-config": "1.1.3",
19
+ "expo-desktop-config-plugins": "1.3.2",
20
+ "expo-desktop-metro-config": "54.81.3",
21
+ "expo-desktop-template-bare-minimum": "54.81.6",
22
22
  "react": "19.1.4",
23
23
  "react-dom": "19.1.4",
24
24
  "react-native": "0.81.6",
@@ -27,10 +27,11 @@
27
27
  "@types/react": "19.1.10",
28
28
  "react-native-windows": "0.81.35",
29
29
  "expo-desktop-modules-core": "54.0.14",
30
- "expo-desktop-stubs": "54.0.14",
31
- "@legendapp/spark": "0.0.1-next.7"
30
+ "expo-desktop-stubs": "54.0.15",
31
+ "@legendapp/spark": "0.0.1-next.9"
32
32
  },
33
33
  "overrides": {
34
+ "expo": "54.0.37",
34
35
  "@expo/cli": "54.0.27",
35
36
  "react-native-nitro-modules": "0.37.0",
36
37
  "react-native-webview": "16.0.0",
@@ -42,7 +43,7 @@
42
43
  "expo-keep-awake": "15.0.8",
43
44
  "expo-modules-core": "3.0.30",
44
45
  "expo-desktop-modules-core": "54.0.14",
45
- "expo-desktop-stubs": "54.0.14",
46
+ "expo-desktop-stubs": "54.0.15",
46
47
  "react": "19.1.4",
47
48
  "react-native": "0.81.6",
48
49
  "react-native-macos": "0.81.7",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/spark-clipboard",
3
- "version": "0.0.1-next.7",
3
+ "version": "0.0.1-next.9",
4
4
  "license": "MIT",
5
5
  "main": "src/index",
6
6
  "react-native": "src/index",
@@ -41,7 +41,7 @@
41
41
  ]
42
42
  },
43
43
  "dependencies": {
44
- "@legendapp/spark-desktop-app": "0.0.1-next.7"
44
+ "@legendapp/spark-desktop-app": "0.0.1-next.9"
45
45
  },
46
46
  "peerDependenciesMeta": {
47
47
  "expo-clipboard": {
@@ -290,6 +290,8 @@ struct TurnState {
290
290
  size_t preAckBytes = 0;
291
291
  bool preAckLimitExceeded = false;
292
292
  };
293
+ // Turn ids are only unique within one app-server process, so turn state is keyed by process generation.
294
+ using TurnKey = std::pair<uint64_t, std::string>;
293
295
 
294
296
  class CodexProcessSupervisor {
295
297
  public:
@@ -351,7 +353,12 @@ public:
351
353
  }
352
354
  struct RunGuard {
353
355
  CodexProcessSupervisor* owner;
354
- ~RunGuard() { std::lock_guard lock(owner->mutex_); --owner->activeRuns_; owner->condition_.notify_all(); }
356
+ ~RunGuard() {
357
+ std::lock_guard lock(owner->mutex_);
358
+ // Turns left from ended processes can only be claimed by an active run.
359
+ if (--owner->activeRuns_ == 0) std::erase_if(owner->turns_, [&](const auto& turn) { return turn.first.first != owner->processGeneration_; });
360
+ owner->condition_.notify_all();
361
+ }
355
362
  } runGuard{this};
356
363
  auto throwIfCancelled = [&]() {
357
364
  std::lock_guard lock(mutex_);
@@ -480,9 +487,13 @@ public:
480
487
  try {
481
488
  NSDictionary* turnResponse = request(@"turn/start", turnParams, deadline, acceptedCancellationGeneration, runProcessGeneration);
482
489
  #if defined(SPARK_CODEX_TESTING)
483
- if (const char* delay = std::getenv("CODEX_TEST_DELAY_AFTER_TURN_RESPONSE_MS")) {
484
- const int milliseconds = std::max(0, std::min(1000, std::atoi(delay)));
485
- std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
490
+ if (const char* accept = std::getenv("CODEX_TEST_ACCEPT_TURN_AFTER_EXIT")) {
491
+ // "next" also waits for a later process to start and end.
492
+ const bool next = std::string(accept) == "next";
493
+ std::unique_lock lock(mutex_);
494
+ condition_.wait_until(lock, deadline, [&]() {
495
+ return processGeneration_ != runProcessGeneration && (!next || (processGeneration_ == 0 && nextProcessGeneration_ > runProcessGeneration));
496
+ });
486
497
  }
487
498
  #endif
488
499
  throwIfCancelled();
@@ -495,13 +506,18 @@ public:
495
506
  "Codex app-server returned an invalid turn response. Update Codex, then reopen the app.");
496
507
  }
497
508
  std::lock_guard lock(mutex_);
498
- if (processGeneration_ != runProcessGeneration) throw std::runtime_error("Codex app-server restarted before the turn was accepted.");
499
- TurnState& state = turns_[turnId];
509
+ auto accepted = turns_.find({runProcessGeneration, turnId});
510
+ if (accepted == turns_.end()) {
511
+ // An ended process keeps the turns it reported; without one, nothing can finish this turn.
512
+ if (processGeneration_ != runProcessGeneration) throw std::runtime_error("Codex app-server restarted before the turn was accepted.");
513
+ accepted = turns_.emplace(TurnKey{runProcessGeneration, turnId}, TurnState()).first;
514
+ }
515
+ TurnState& state = accepted->second;
500
516
  state.threadId = threadId;
501
517
  preAckBytes_ -= state.preAckBytes;
502
518
  state.preAckBytes = 0;
503
519
  state.acknowledged = true;
504
- --pendingTurnStarts_;
520
+ if (processGeneration_ == runProcessGeneration) --pendingTurnStarts_;
505
521
  pendingTurnStart = false;
506
522
  } catch (...) {
507
523
  finishPendingTurnStart();
@@ -511,25 +527,21 @@ public:
511
527
 
512
528
  TurnState completed;
513
529
  {
530
+ // Ending a process finishes its turns (completed ones keep their result), so `done` covers exits too.
531
+ const TurnKey key{runProcessGeneration, turnId};
514
532
  std::unique_lock lock(mutex_);
515
- const bool finished = condition_.wait_until(lock, deadline, [&]() {
516
- const auto state = turns_.find(turnId);
517
- return processGeneration_ != runProcessGeneration || cancelGeneration_ != cancelGeneration ||
518
- (state != turns_.end() && state->second.done);
533
+ condition_.wait_until(lock, deadline, [&]() {
534
+ const auto state = turns_.find(key);
535
+ return cancelGeneration_ != cancelGeneration || (state != turns_.end() && state->second.done);
519
536
  });
520
- auto state = turns_.find(turnId);
537
+ if (const auto state = turns_.find(key); state != turns_.end()) {
538
+ completed = std::move(state->second);
539
+ turns_.erase(state);
540
+ }
541
+ retireTurnLocked(runProcessGeneration, turnId);
521
542
  if (cancelGeneration_ != cancelGeneration) {
522
543
  throw std::runtime_error("Codex request was cancelled.");
523
- } else if (processGeneration_ != runProcessGeneration) {
524
- const std::string detail = processError_.empty() ? "Codex app-server restarted while the request was running." : processError_;
525
- throw std::runtime_error(actionableError(detail));
526
- } else if (finished && state != turns_.end() && state->second.done) {
527
- completed = state->second;
528
- turns_.erase(state);
529
- retireTurnLocked(turnId);
530
- } else {
531
- turns_.erase(turnId);
532
- retireTurnLocked(turnId);
544
+ } else if (!completed.done) {
533
545
  lock.unlock();
534
546
  interruptTurn(threadId, turnId, runProcessGeneration);
535
547
  const int seconds = static_cast<int>(timeoutMs / 1000.0);
@@ -590,18 +602,11 @@ public:
590
602
  pid_ = -1;
591
603
  stdinFd_ = -1;
592
604
  initialized_ = false;
593
- processGeneration_ = 0;
594
605
  processError_ = "Codex app-server was stopped.";
595
- responses_.clear();
596
- ignoredResponseIds_.clear();
606
+ endGenerationLocked(processGeneration_, processError_);
597
607
  fallbackPids.assign(fallbackPids_.begin(), fallbackPids_.end());
598
608
  fallbackPids_.clear();
599
609
  useExecFallback_ = false;
600
- for (auto& [turnId, state] : turns_) {
601
- state.error = processError_;
602
- state.done = true;
603
- }
604
- condition_.notify_all();
605
610
  }
606
611
  if (stdinFd >= 0) {
607
612
  close(stdinFd);
@@ -627,8 +632,25 @@ public:
627
632
  #endif
628
633
 
629
634
  private:
630
- void retireTurnLocked(const std::string& turnId) {
631
- if (turnId.empty() || !retiredTurnIds_.insert(turnId).second) return;
635
+ // Ends a process generation without discarding its turns: completed turns keep their result for the run
636
+ // that collects them, and unfinished turns fail with the reason.
637
+ void endGenerationLocked(uint64_t generation, const std::string& reason) {
638
+ processGeneration_ = 0;
639
+ for (auto& [key, state] : turns_) {
640
+ if (key.first != generation) continue;
641
+ preAckBytes_ -= state.preAckBytes;
642
+ state.preAckBytes = 0;
643
+ if (!state.done) {
644
+ state.error = reason;
645
+ state.done = true;
646
+ }
647
+ }
648
+ pendingTurnStarts_ = 0;
649
+ condition_.notify_all();
650
+ }
651
+
652
+ void retireTurnLocked(uint64_t generation, const std::string& turnId) {
653
+ if (generation != processGeneration_ || turnId.empty() || !retiredTurnIds_.insert(turnId).second) return;
632
654
  retiredTurnOrder_.push_back(turnId);
633
655
  while (retiredTurnOrder_.size() > 512) {
634
656
  retiredTurnIds_.erase(retiredTurnOrder_.front());
@@ -636,9 +658,9 @@ private:
636
658
  }
637
659
  }
638
660
 
639
- TurnState* pendingOrActiveTurnLocked(const std::string& turnId) {
661
+ TurnState* pendingOrActiveTurnLocked(uint64_t generation, const std::string& turnId) {
640
662
  if (turnId.empty() || retiredTurnIds_.contains(turnId)) return nullptr;
641
- auto found = turns_.find(turnId);
663
+ auto found = turns_.find({generation, turnId});
642
664
  if (found != turns_.end()) return &found->second;
643
665
  if (pendingTurnStarts_ == 0) return nullptr;
644
666
  while (turns_.size() >= 128) {
@@ -652,7 +674,7 @@ private:
652
674
  }
653
675
  TurnState state;
654
676
  state.sequence = ++nextTurnSequence_;
655
- return &turns_.emplace(turnId, std::move(state)).first->second;
677
+ return &turns_.emplace(TurnKey{generation, turnId}, std::move(state)).first->second;
656
678
  }
657
679
 
658
680
  void setPreAckTextLocked(TurnState& state, std::string TurnState::*field, const std::string& value) {
@@ -1019,18 +1041,23 @@ private:
1019
1041
  userAgent_.clear();
1020
1042
  processError_.clear();
1021
1043
  stderrTail_.clear();
1022
- responses_.clear();
1023
1044
  ignoredResponseIds_.clear();
1024
- turns_.clear();
1025
- preAckBytes_ = 0;
1026
- pendingTurnStarts_ = 0;
1027
1045
  retiredTurnIds_.clear();
1028
1046
  retiredTurnOrder_.clear();
1029
1047
  }
1030
1048
 
1031
- std::thread([this, fd = outputPipe[0], generation]() { readStdout(fd, generation); }).detach();
1032
1049
  std::thread([this, fd = errorPipe[0], generation]() { readStderr(fd, generation); }).detach();
1033
- std::thread([this, pid, generation]() {
1050
+ std::thread([this, pid, generation, outputFd = outputPipe[0]]() {
1051
+ // Handle the exit only after every line the app-server wrote has been handled, so a reply or
1052
+ // turn/completed written just before exiting is never dropped.
1053
+ std::thread reader([this, outputFd, generation]() { readStdout(outputFd, generation); });
1054
+ siginfo_t exited{};
1055
+ while (waitid(P_PID, pid, &exited, WEXITED | WNOWAIT) < 0 && errno == EINTR) {
1056
+ }
1057
+ // Leftover members of the app-server's process group could hold stdout open; the unreaped leader
1058
+ // keeps the group id from being reused.
1059
+ kill(-pid, SIGTERM);
1060
+ reader.join();
1034
1061
  int status = 0;
1035
1062
  while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {
1036
1063
  }
@@ -1057,13 +1084,7 @@ private:
1057
1084
  pid_ = -1;
1058
1085
  stdinFd_ = -1;
1059
1086
  initialized_ = false;
1060
- processGeneration_ = 0;
1061
- responses_.clear();
1062
- ignoredResponseIds_.clear();
1063
- turns_.clear();
1064
- preAckBytes_ = 0;
1065
- pendingTurnStarts_ = 0;
1066
- condition_.notify_all();
1087
+ endGenerationLocked(processGeneration_, "Codex app-server restarted while the request was running.");
1067
1088
  }
1068
1089
  if (stdinFd >= 0) {
1069
1090
  close(stdinFd);
@@ -1084,12 +1105,8 @@ private:
1084
1105
  pid_ = -1;
1085
1106
  stdinFd_ = -1;
1086
1107
  initialized_ = false;
1087
- processGeneration_ = 0;
1088
1108
  processError_ = reason;
1089
- turns_.clear();
1090
- preAckBytes_ = 0;
1091
- pendingTurnStarts_ = 0;
1092
- condition_.notify_all();
1109
+ endGenerationLocked(generation, reason);
1093
1110
  }
1094
1111
  if (stdinFd >= 0) close(stdinFd);
1095
1112
  if (pid > 0) kill(-pid, SIGTERM);
@@ -1117,22 +1134,20 @@ private:
1117
1134
  std::string responseLine;
1118
1135
  {
1119
1136
  std::unique_lock lock(mutex_);
1120
- const bool received = condition_.wait_until(lock, deadline, [&]() {
1121
- return responses_.contains(requestId) || processGeneration_ != generation ||
1122
- (cancellationGeneration != UINT64_MAX && cancelGeneration_ != cancellationGeneration);
1137
+ const auto cancelled = [&]() { return cancellationGeneration != UINT64_MAX && cancelGeneration_ != cancellationGeneration; };
1138
+ condition_.wait_until(lock, deadline, [&]() {
1139
+ return responses_.contains(requestId) || processGeneration_ != generation || cancelled();
1123
1140
  });
1124
- if (!received) {
1141
+ // Responses are keyed by unique request ids and survive restarts; the waiter always removes its own.
1142
+ if (const auto response = responses_.find(requestId); response != responses_.end()) {
1143
+ responseLine = std::move(response->second);
1144
+ responses_.erase(response);
1145
+ } else {
1125
1146
  ignoredResponseIds_.insert(requestId);
1126
- throw std::runtime_error("Codex app-server did not respond in time.");
1127
- }
1128
- if (cancellationGeneration != UINT64_MAX && cancelGeneration_ != cancellationGeneration) {
1129
- throw std::runtime_error("Codex request was cancelled.");
1130
1147
  }
1131
- if (processGeneration_ != generation && !responses_.contains(requestId)) {
1132
- throw std::runtime_error(processError_.empty() ? "Codex app-server exited unexpectedly." : processError_);
1133
- }
1134
- responseLine = std::move(responses_[requestId]);
1135
- responses_.erase(requestId);
1148
+ if (cancelled()) throw std::runtime_error("Codex request was cancelled.");
1149
+ if (responseLine.empty() && processGeneration_ == generation) throw std::runtime_error("Codex app-server did not respond in time.");
1150
+ if (responseLine.empty()) throw std::runtime_error(processError_.empty() ? "Codex app-server exited unexpectedly." : processError_);
1136
1151
  }
1137
1152
  return parseJsonObject(responseLine, "Codex app-server returned invalid JSON");
1138
1153
  }
@@ -1322,7 +1337,7 @@ private:
1322
1337
  const std::string text = toString(stringValue(item[@"text"]));
1323
1338
  std::lock_guard lock(mutex_);
1324
1339
  if (processGeneration_ == generation && !turnId.empty()) {
1325
- if (TurnState* state = pendingOrActiveTurnLocked(turnId)) setPreAckTextLocked(*state, &TurnState::output, text);
1340
+ if (TurnState* state = pendingOrActiveTurnLocked(generation, turnId)) setPreAckTextLocked(*state, &TurnState::output, text);
1326
1341
  }
1327
1342
  }
1328
1343
  } else if ([method isEqualToString:@"error"]) {
@@ -1332,7 +1347,7 @@ private:
1332
1347
  const bool willRetry = boolValue(params[@"willRetry"]);
1333
1348
  std::lock_guard lock(mutex_);
1334
1349
  if (processGeneration_ == generation && !turnId.empty() && !willRetry) {
1335
- if (TurnState* state = pendingOrActiveTurnLocked(turnId)) setPreAckTextLocked(*state, &TurnState::error, errorMessage);
1350
+ if (TurnState* state = pendingOrActiveTurnLocked(generation, turnId)) setPreAckTextLocked(*state, &TurnState::error, errorMessage);
1336
1351
  }
1337
1352
  } else if ([method isEqualToString:@"turn/completed"]) {
1338
1353
  NSDictionary* turn = dictionaryValue(params[@"turn"]);
@@ -1352,7 +1367,7 @@ private:
1352
1367
  }
1353
1368
  std::lock_guard lock(mutex_);
1354
1369
  if (processGeneration_ == generation && !turnId.empty()) {
1355
- TurnState* existing = pendingOrActiveTurnLocked(turnId); if (existing == nullptr) return;
1370
+ TurnState* existing = pendingOrActiveTurnLocked(generation, turnId); if (existing == nullptr) return;
1356
1371
  TurnState& state = *existing;
1357
1372
  if (!output.empty()) {
1358
1373
  setPreAckTextLocked(state, &TurnState::output, output);
@@ -1386,7 +1401,8 @@ private:
1386
1401
  return;
1387
1402
  }
1388
1403
  processError_ = actionableError(message);
1389
- for (auto& [turnId, state] : turns_) {
1404
+ for (auto& [key, state] : turns_) {
1405
+ if (key.first != generation) continue;
1390
1406
  state.error = processError_;
1391
1407
  state.done = true;
1392
1408
  }
@@ -1404,7 +1420,6 @@ private:
1404
1420
  stdinFd_ = -1;
1405
1421
  pid_ = -1;
1406
1422
  initialized_ = false;
1407
- processGeneration_ = 0;
1408
1423
  std::string detail;
1409
1424
  if (WIFEXITED(status)) {
1410
1425
  detail = "Codex app-server exited with status " + std::to_string(WEXITSTATUS(status)) + ".";
@@ -1417,10 +1432,7 @@ private:
1417
1432
  detail += " " + stderrTail_;
1418
1433
  }
1419
1434
  processError_ = actionableError(detail);
1420
- turns_.clear();
1421
- preAckBytes_ = 0;
1422
- pendingTurnStarts_ = 0;
1423
- condition_.notify_all();
1435
+ endGenerationLocked(generation, detail);
1424
1436
  }
1425
1437
 
1426
1438
  static void throwForResponseError(NSDictionary* response, const std::string& context) {
@@ -1438,7 +1450,7 @@ private:
1438
1450
  std::map<int64_t, std::string> responses_;
1439
1451
  std::set<int64_t> ignoredResponseIds_;
1440
1452
  std::set<pid_t> fallbackPids_;
1441
- std::map<std::string, TurnState> turns_;
1453
+ std::map<TurnKey, TurnState> turns_;
1442
1454
  std::set<std::string> retiredTurnIds_;
1443
1455
  std::deque<std::string> retiredTurnOrder_;
1444
1456
  std::timed_mutex lifecycleMutex_;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/spark-codex",
3
- "version": "0.0.1-next.7",
3
+ "version": "0.0.1-next.9",
4
4
  "private": true,
5
5
  "main": "src/index.ts",
6
6
  "react-native": "src/index.ts",
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "license": "MIT",
12
12
  "dependencies": {
13
- "@legendapp/spark-desktop-app": "0.0.1-next.7",
13
+ "@legendapp/spark-desktop-app": "0.0.1-next.9",
14
14
  "react-native-nitro-modules": "0.37.0"
15
15
  },
16
16
  "files": [
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/spark-commands",
3
- "version": "0.0.1-next.7",
3
+ "version": "0.0.1-next.9",
4
4
  "private": true,
5
5
  "license": "MIT",
6
6
  "main": "src/index.tsx",
@@ -8,11 +8,11 @@
8
8
  "src"
9
9
  ],
10
10
  "dependencies": {
11
- "@legendapp/spark-ui": "0.0.1-next.7",
12
- "@legendapp/spark-desktop-shortcuts": "0.0.1-next.7",
13
- "@legendapp/spark-settings": "0.0.1-next.7",
11
+ "@legendapp/spark-ui": "0.0.1-next.9",
12
+ "@legendapp/spark-desktop-shortcuts": "0.0.1-next.9",
13
+ "@legendapp/spark-settings": "0.0.1-next.9",
14
14
  "@legendapp/state": "^3.0.0-beta.42",
15
- "@legendapp/spark-desktop-app": "0.0.1-next.7"
15
+ "@legendapp/spark-desktop-app": "0.0.1-next.9"
16
16
  },
17
17
  "peerDependencies": {
18
18
  "react": "^19.1.0",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/spark-context-menu",
3
- "version": "0.0.1-next.7",
3
+ "version": "0.0.1-next.9",
4
4
  "license": "MIT",
5
5
  "main": "src/index.ts",
6
6
  "react-native": "src/index.ts",
@@ -41,7 +41,7 @@
41
41
  ]
42
42
  },
43
43
  "dependencies": {
44
- "@legendapp/spark-desktop-app": "0.0.1-next.7"
44
+ "@legendapp/spark-desktop-app": "0.0.1-next.9"
45
45
  },
46
46
  "private": true
47
47
  }
@@ -10,7 +10,18 @@ Pod::Spec.new do |s|
10
10
  s.source = { :path => "." }
11
11
  s.platforms = { :osx => "14.0" }
12
12
  s.source_files = "macos/**/*.{h,m,mm}", "cpp/**/*.hpp"
13
- s.pod_target_xcconfig = { "CLANG_CXX_LANGUAGE_STANDARD" => "c++20" }
13
+ # The in-app test driver compiles in Debug builds, and in other builds only for apps that set macos.testDriverInRelease
14
+ # (the Spark config plugin exports SPARK_TEST_DRIVER_RELEASE to the Podfile; docs/test-driver.md#builds).
15
+ driver = { "SPARK_TEST_DRIVER" => ENV["SPARK_TEST_DRIVER_RELEASE"] == "1" ? "1" : "0", "SPARK_TEST_DRIVER[config=Debug]" => "1" }
16
+ # Without the driver its sources are excluded (SPARK_TEST_DRIVER_SOURCES_0) and SparkTestDriver.h declares inline no-ops.
17
+ # The app target gets the same setting, for the host's AppDelegate.
18
+ s.user_target_xcconfig = driver.merge("GCC_PREPROCESSOR_DEFINITIONS" => "$(inherited) SPARK_TEST_DRIVER=$(SPARK_TEST_DRIVER)")
19
+ s.pod_target_xcconfig = driver.merge(
20
+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
21
+ "GCC_PREPROCESSOR_DEFINITIONS" => "$(inherited) SPARK_TEST_DRIVER=$(SPARK_TEST_DRIVER)",
22
+ "SPARK_TEST_DRIVER_SOURCES_0" => "SparkTestDriver*.mm",
23
+ "EXCLUDED_SOURCE_FILE_NAMES" => "$(SPARK_TEST_DRIVER_SOURCES_$(SPARK_TEST_DRIVER))",
24
+ )
14
25
  s.dependency "React-Core"
15
26
  s.dependency "ReactCodegen"
16
27
  end
@@ -1,5 +1,6 @@
1
1
  #import "RNDesktopApp.h"
2
2
  #import "SparkDesktop.h"
3
+ #import "SparkInstance.h"
3
4
  @implementation RNDesktopApp { NSMutableSet<NSString *> *_guardIds; }
4
5
  - (instancetype)init { if ((self = [super init])) _guardIds = [NSMutableSet new]; return self; }
5
6
  RCT_EXPORT_MODULE(NativeDesktopApp)
@@ -26,7 +27,7 @@ RCT_EXPORT_MODULE(NativeDesktopApp)
26
27
  else reject(@"E_UNAVAILABLE", @"Customization is not enabled in this app", nil);
27
28
  }
28
29
  }
29
- else if ([method isEqual:@"context"]) resolve(SparkJSON(SparkContext()));
30
+ else if ([method isEqual:@"context"]) { NSMutableDictionary *context = [SparkContext() mutableCopy]; context[@"launch"] = SparkLaunchState(); resolve(SparkJSON(context)); }
30
31
  else if ([method isEqual:@"initialURL"]) resolve(SparkJSON(SparkInitialURL()));
31
32
  else if ([method isEqual:@"pendingURLs"]) resolve(SparkJSON(SparkPendingURLs()));
32
33
  else if ([method isEqual:@"quit"]) {
@@ -34,6 +35,12 @@ RCT_EXPORT_MODULE(NativeDesktopApp)
34
35
  // AppKit can enter a modal run loop while awaiting the JS decision. Do
35
36
  // not hold the main GCD queue that must deliver that decision.
36
37
  [NSRunLoop.mainRunLoop performBlock:^{ [NSApp terminate:nil]; }]; }
38
+ else if ([method isEqual:@"relaunch"]) {
39
+ NSArray *process = NSProcessInfo.processInfo.arguments;
40
+ NSArray *arguments = args[@"arguments"] ?: SparkInstanceRelaunchArguments(process);
41
+ if (![arguments isKindOfClass:NSArray.class] || [arguments indexOfObjectPassingTest:^BOOL(id item, NSUInteger index, BOOL *stop) { return ![item isKindOfClass:NSString.class]; }] != NSNotFound) { SparkInvalid(reject, @"Expected relaunch arguments"); return; }
42
+ SparkRelaunch(arguments, ^(NSDictionary *result, NSError *error) { if (error) SparkReject(reject, error); else resolve(SparkJSON(result)); });
43
+ }
37
44
  else if ([method isEqual:@"quitGuard"]) {
38
45
  NSString *identifier = args[@"id"];
39
46
  if (![identifier isKindOfClass:NSString.class] || !identifier.length) { SparkInvalid(reject, @"Expected quit guard ID"); return; }
@@ -3,7 +3,15 @@
3
3
  #import <React/RCTBridgeModule.h>
4
4
 
5
5
  FOUNDATION_EXPORT NSString * const SparkDesktopEvent;
6
+ // Call in applicationWillFinishLaunching:. YES: this process owns the project's
7
+ // instance. NO: another process does; this one forwards its launch and exits.
6
8
  FOUNDATION_EXPORT BOOL SparkAcquireInstance(void);
9
+ // Latched on first call during launch: --spark-safe-mode, or Shift held at launch.
10
+ FOUNDATION_EXPORT BOOL SparkSafeMode(void);
11
+ // { safeMode: "flag" | "modifier" | null, relaunched: bool, instance: "primary" | "test-driver" }.
12
+ FOUNDATION_EXPORT NSDictionary *SparkLaunchState(void);
13
+ // Runs the quit guards; on approval the started instance takes over, on veto it is killed.
14
+ FOUNDATION_EXPORT void SparkRelaunch(NSArray<NSString *> *arguments, void (^completion)(NSDictionary *result, NSError *error));
7
15
  FOUNDATION_EXPORT NSDictionary *SparkContext(void);
8
16
  FOUNDATION_EXPORT NSDictionary *SparkInitialProps(NSString *windowID, NSDictionary *props);
9
17
  FOUNDATION_EXPORT NSString *SparkNamespace(void);