byterover-cli 1.0.4 → 1.1.0

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 (172) hide show
  1. package/README.md +24 -11
  2. package/dist/commands/curate.js +1 -1
  3. package/dist/commands/hook-prompt-submit.d.ts +27 -0
  4. package/dist/commands/hook-prompt-submit.js +39 -0
  5. package/dist/commands/main.d.ts +13 -0
  6. package/dist/commands/main.js +53 -2
  7. package/dist/commands/query.js +1 -1
  8. package/dist/commands/status.js +8 -3
  9. package/dist/constants.d.ts +2 -2
  10. package/dist/constants.js +2 -2
  11. package/dist/core/domain/cipher/llm/registry.js +53 -2
  12. package/dist/core/domain/cipher/llm/types.d.ts +2 -0
  13. package/dist/core/domain/cipher/process/types.d.ts +7 -0
  14. package/dist/core/domain/cipher/session/session-metadata.d.ts +178 -0
  15. package/dist/core/domain/cipher/session/session-metadata.js +147 -0
  16. package/dist/core/domain/cipher/tools/constants.d.ts +1 -0
  17. package/dist/core/domain/cipher/tools/constants.js +1 -0
  18. package/dist/core/domain/entities/agent.d.ts +16 -0
  19. package/dist/core/domain/entities/agent.js +24 -0
  20. package/dist/core/domain/entities/connector-type.d.ts +9 -0
  21. package/dist/core/domain/entities/connector-type.js +8 -0
  22. package/dist/core/domain/entities/event.d.ts +1 -1
  23. package/dist/core/domain/entities/event.js +2 -0
  24. package/dist/core/domain/errors/task-error.d.ts +4 -0
  25. package/dist/core/domain/errors/task-error.js +7 -0
  26. package/dist/core/domain/knowledge/markdown-writer.d.ts +15 -18
  27. package/dist/core/domain/knowledge/markdown-writer.js +232 -34
  28. package/dist/core/domain/knowledge/relation-parser.d.ts +25 -39
  29. package/dist/core/domain/knowledge/relation-parser.js +39 -61
  30. package/dist/core/domain/transport/schemas.d.ts +77 -2
  31. package/dist/core/domain/transport/schemas.js +51 -2
  32. package/dist/core/interfaces/cipher/i-session-persistence.d.ts +133 -0
  33. package/dist/core/interfaces/cipher/i-session-persistence.js +7 -0
  34. package/dist/core/interfaces/cipher/message-types.d.ts +6 -0
  35. package/dist/core/interfaces/connectors/connector-types.d.ts +57 -0
  36. package/dist/core/interfaces/connectors/i-connector-manager.d.ts +72 -0
  37. package/dist/core/interfaces/connectors/i-connector.d.ts +54 -0
  38. package/dist/core/interfaces/connectors/i-connector.js +1 -0
  39. package/dist/core/interfaces/executor/i-curate-executor.d.ts +2 -2
  40. package/dist/core/interfaces/i-context-file-reader.d.ts +3 -0
  41. package/dist/core/interfaces/i-file-service.d.ts +7 -0
  42. package/dist/core/interfaces/usecase/i-connectors-use-case.d.ts +3 -0
  43. package/dist/core/interfaces/usecase/i-connectors-use-case.js +1 -0
  44. package/dist/core/interfaces/usecase/{i-clear-use-case.d.ts → i-reset-use-case.d.ts} +1 -1
  45. package/dist/core/interfaces/usecase/i-reset-use-case.js +1 -0
  46. package/dist/hooks/init/update-notifier.d.ts +1 -0
  47. package/dist/hooks/init/update-notifier.js +10 -1
  48. package/dist/infra/cipher/agent/agent-schemas.d.ts +6 -6
  49. package/dist/infra/cipher/agent/service-initializer.js +4 -4
  50. package/dist/infra/cipher/file-system/binary-utils.d.ts +7 -12
  51. package/dist/infra/cipher/file-system/binary-utils.js +46 -31
  52. package/dist/infra/cipher/file-system/context-tree-file-system-factory.js +3 -2
  53. package/dist/infra/cipher/file-system/file-system-service.js +1 -0
  54. package/dist/infra/cipher/http/internal-llm-http-service.js +3 -5
  55. package/dist/infra/cipher/interactive-loop.js +3 -1
  56. package/dist/infra/cipher/llm/context/context-manager.d.ts +2 -2
  57. package/dist/infra/cipher/llm/context/context-manager.js +63 -18
  58. package/dist/infra/cipher/llm/formatters/gemini-formatter.d.ts +13 -0
  59. package/dist/infra/cipher/llm/formatters/gemini-formatter.js +146 -15
  60. package/dist/infra/cipher/llm/generators/byterover-content-generator.js +6 -2
  61. package/dist/infra/cipher/llm/internal-llm-service.js +2 -2
  62. package/dist/infra/cipher/llm/thought-parser.d.ts +21 -0
  63. package/dist/infra/cipher/llm/thought-parser.js +27 -0
  64. package/dist/infra/cipher/llm/tool-output-processor.d.ts +10 -0
  65. package/dist/infra/cipher/llm/tool-output-processor.js +80 -7
  66. package/dist/infra/cipher/process/process-service.js +11 -3
  67. package/dist/infra/cipher/session/chat-session.d.ts +7 -2
  68. package/dist/infra/cipher/session/chat-session.js +90 -52
  69. package/dist/infra/cipher/session/session-metadata-store.d.ts +52 -0
  70. package/dist/infra/cipher/session/session-metadata-store.js +406 -0
  71. package/dist/infra/cipher/system-prompt/contributors/context-tree-structure-contributor.d.ts +6 -7
  72. package/dist/infra/cipher/system-prompt/contributors/context-tree-structure-contributor.js +57 -18
  73. package/dist/infra/cipher/tools/implementations/curate-tool.js +132 -36
  74. package/dist/infra/cipher/tools/implementations/read-file-tool.js +38 -17
  75. package/dist/infra/cipher/tools/implementations/search-knowledge-tool.d.ts +7 -0
  76. package/dist/infra/cipher/tools/implementations/search-knowledge-tool.js +303 -0
  77. package/dist/infra/cipher/tools/implementations/task-tool.js +1 -0
  78. package/dist/infra/cipher/tools/index.d.ts +1 -0
  79. package/dist/infra/cipher/tools/index.js +1 -0
  80. package/dist/infra/cipher/tools/tool-manager.js +1 -0
  81. package/dist/infra/cipher/tools/tool-registry.js +7 -0
  82. package/dist/infra/connectors/connector-manager.d.ts +32 -0
  83. package/dist/infra/connectors/connector-manager.js +156 -0
  84. package/dist/infra/connectors/hook/hook-connector-config.d.ts +52 -0
  85. package/dist/infra/connectors/hook/hook-connector-config.js +41 -0
  86. package/dist/infra/connectors/hook/hook-connector.d.ts +46 -0
  87. package/dist/infra/connectors/hook/hook-connector.js +231 -0
  88. package/dist/infra/{rule → connectors/rules}/legacy-rule-detector.d.ts +2 -2
  89. package/dist/infra/{rule → connectors/rules}/legacy-rule-detector.js +1 -1
  90. package/dist/infra/connectors/rules/rules-connector-config.d.ts +95 -0
  91. package/dist/infra/{rule/agent-rule-config.js → connectors/rules/rules-connector-config.js} +10 -10
  92. package/dist/infra/connectors/rules/rules-connector.d.ts +41 -0
  93. package/dist/infra/connectors/rules/rules-connector.js +204 -0
  94. package/dist/infra/{rule/rule-template-service.d.ts → connectors/shared/template-service.d.ts} +3 -3
  95. package/dist/infra/{rule/rule-template-service.js → connectors/shared/template-service.js} +1 -1
  96. package/dist/infra/context-tree/file-context-file-reader.js +4 -0
  97. package/dist/infra/context-tree/file-context-tree-writer-service.d.ts +5 -2
  98. package/dist/infra/context-tree/file-context-tree-writer-service.js +20 -5
  99. package/dist/infra/core/executors/curate-executor.d.ts +2 -2
  100. package/dist/infra/core/executors/curate-executor.js +7 -7
  101. package/dist/infra/core/executors/query-executor.d.ts +12 -0
  102. package/dist/infra/core/executors/query-executor.js +62 -1
  103. package/dist/infra/core/task-processor.d.ts +2 -2
  104. package/dist/infra/file/fs-file-service.d.ts +7 -0
  105. package/dist/infra/file/fs-file-service.js +15 -1
  106. package/dist/infra/process/agent-worker.d.ts +2 -2
  107. package/dist/infra/process/agent-worker.js +626 -142
  108. package/dist/infra/process/constants.d.ts +1 -1
  109. package/dist/infra/process/constants.js +1 -1
  110. package/dist/infra/process/ipc-types.d.ts +17 -4
  111. package/dist/infra/process/ipc-types.js +3 -3
  112. package/dist/infra/process/parent-heartbeat.d.ts +47 -0
  113. package/dist/infra/process/parent-heartbeat.js +118 -0
  114. package/dist/infra/process/process-manager.d.ts +89 -1
  115. package/dist/infra/process/process-manager.js +293 -9
  116. package/dist/infra/process/task-queue-manager.d.ts +13 -0
  117. package/dist/infra/process/task-queue-manager.js +19 -0
  118. package/dist/infra/process/transport-handlers.d.ts +3 -0
  119. package/dist/infra/process/transport-handlers.js +82 -5
  120. package/dist/infra/process/transport-worker.js +9 -69
  121. package/dist/infra/repl/commands/connectors-command.d.ts +8 -0
  122. package/dist/infra/repl/commands/{gen-rules-command.js → connectors-command.js} +21 -10
  123. package/dist/infra/repl/commands/index.js +8 -4
  124. package/dist/infra/repl/commands/init-command.js +11 -7
  125. package/dist/infra/repl/commands/new-command.d.ts +14 -0
  126. package/dist/infra/repl/commands/new-command.js +61 -0
  127. package/dist/infra/repl/commands/query-command.js +22 -2
  128. package/dist/infra/repl/commands/{clear-command.d.ts → reset-command.d.ts} +2 -2
  129. package/dist/infra/repl/commands/{clear-command.js → reset-command.js} +11 -11
  130. package/dist/infra/transport/socket-io-transport-client.d.ts +68 -0
  131. package/dist/infra/transport/socket-io-transport-client.js +283 -7
  132. package/dist/infra/usecase/connectors-use-case.d.ts +59 -0
  133. package/dist/infra/usecase/connectors-use-case.js +203 -0
  134. package/dist/infra/usecase/init-use-case.d.ts +8 -43
  135. package/dist/infra/usecase/init-use-case.js +29 -253
  136. package/dist/infra/usecase/logout-use-case.js +2 -2
  137. package/dist/infra/usecase/pull-use-case.js +5 -5
  138. package/dist/infra/usecase/push-use-case.js +5 -5
  139. package/dist/infra/usecase/{clear-use-case.d.ts → reset-use-case.d.ts} +5 -5
  140. package/dist/infra/usecase/{clear-use-case.js → reset-use-case.js} +7 -8
  141. package/dist/infra/usecase/space-list-use-case.js +3 -3
  142. package/dist/infra/usecase/space-switch-use-case.js +3 -3
  143. package/dist/resources/prompts/curate.yml +75 -13
  144. package/dist/resources/prompts/explore.yml +34 -0
  145. package/dist/resources/prompts/query-orchestrator.yml +112 -0
  146. package/dist/resources/prompts/system-prompt.yml +12 -2
  147. package/dist/resources/tools/curate.txt +60 -15
  148. package/dist/resources/tools/search_knowledge.txt +32 -0
  149. package/dist/templates/sections/brv-instructions.md +98 -0
  150. package/dist/tui/components/inline-prompts/inline-confirm.js +2 -2
  151. package/dist/tui/components/onboarding/onboarding-flow.js +14 -10
  152. package/dist/tui/components/onboarding/welcome-box.js +1 -1
  153. package/dist/tui/contexts/onboarding-context.d.ts +4 -0
  154. package/dist/tui/contexts/onboarding-context.js +14 -2
  155. package/dist/tui/views/command-view.js +19 -0
  156. package/dist/utils/file-validator.d.ts +1 -1
  157. package/dist/utils/file-validator.js +34 -35
  158. package/dist/utils/type-guards.d.ts +5 -0
  159. package/dist/utils/type-guards.js +7 -0
  160. package/oclif.manifest.json +32 -6
  161. package/package.json +4 -1
  162. package/dist/config/context-tree-domains.d.ts +0 -29
  163. package/dist/config/context-tree-domains.js +0 -29
  164. package/dist/core/interfaces/usecase/i-generate-rules-use-case.d.ts +0 -3
  165. package/dist/infra/repl/commands/gen-rules-command.d.ts +0 -7
  166. package/dist/infra/rule/agent-rule-config.d.ts +0 -19
  167. package/dist/infra/usecase/generate-rules-use-case.d.ts +0 -61
  168. package/dist/infra/usecase/generate-rules-use-case.js +0 -285
  169. /package/dist/core/interfaces/{usecase/i-clear-use-case.js → connectors/connector-types.js} +0 -0
  170. /package/dist/core/interfaces/{usecase/i-generate-rules-use-case.js → connectors/i-connector-manager.js} +0 -0
  171. /package/dist/infra/{rule → connectors/shared}/constants.d.ts +0 -0
  172. /package/dist/infra/{rule → connectors/shared}/constants.js +0 -0
@@ -1 +1 @@
1
- export declare const CURATE_MAX_CONCURRENT = 1;
1
+ export declare const CURATE_MAX_CONCURRENT = 3;
@@ -1 +1 @@
1
- export const CURATE_MAX_CONCURRENT = 1;
1
+ export const CURATE_MAX_CONCURRENT = 3;
@@ -2,17 +2,22 @@
2
2
  * IPC Types - Shared types for Node.js IPC between parent and child processes.
3
3
  *
4
4
  * Architecture v0.5.0:
5
- * - IPC is used ONLY for process lifecycle (ready, ping/pong, shutdown, error)
5
+ * - IPC is used ONLY for process lifecycle (ready, ping/pong, shutdown, error, health-check)
6
6
  * - Task communication uses Socket.IO (NOT IPC)
7
7
  *
8
8
  * Message flows:
9
- * - Parent → Child: IPCCommand (ping, shutdown)
10
- * - Child → Parent: IPCResponse variants (ready, pong, stopped, error)
9
+ * - Parent → Child: IPCCommand (ping, shutdown, health-check)
10
+ * - Child → Parent: IPCResponse variants (ready, pong, stopped, error, health-check-result)
11
11
  */
12
12
  /**
13
13
  * Commands sent from parent (ProcessManager) to child processes.
14
+ * - ping: Heartbeat check
15
+ * - shutdown: Graceful shutdown request
16
+ * - health-check: Fix #3 - Verify connection health after sleep/wake
14
17
  */
15
18
  export type IPCCommand = {
19
+ type: 'health-check';
20
+ } | {
16
21
  type: 'ping';
17
22
  } | {
18
23
  type: 'shutdown';
@@ -30,6 +35,14 @@ export type IPCErrorResponse = {
30
35
  error: string;
31
36
  type: 'error';
32
37
  };
38
+ /**
39
+ * Health-check result response (agent-only).
40
+ * Sent after health-check command to inform ProcessManager of connection status.
41
+ */
42
+ export type IPCHealthCheckResultResponse = {
43
+ success: boolean;
44
+ type: 'health-check-result';
45
+ };
33
46
  /**
34
47
  * Ready response variants.
35
48
  * - Transport: includes port number
@@ -46,7 +59,7 @@ export type IPCReadyWithPortResponse = {
46
59
  * Composite response types for each process type.
47
60
  */
48
61
  export type TransportIPCResponse = IPCErrorResponse | IPCPongResponse | IPCReadyWithPortResponse | IPCStoppedResponse;
49
- export type AgentIPCResponse = IPCErrorResponse | IPCPongResponse | IPCReadyResponse | IPCStoppedResponse;
62
+ export type AgentIPCResponse = IPCErrorResponse | IPCHealthCheckResultResponse | IPCPongResponse | IPCReadyResponse | IPCStoppedResponse;
50
63
  /**
51
64
  * Legacy aliases for gradual migration.
52
65
  * @deprecated Use IPCCommand instead
@@ -2,11 +2,11 @@
2
2
  * IPC Types - Shared types for Node.js IPC between parent and child processes.
3
3
  *
4
4
  * Architecture v0.5.0:
5
- * - IPC is used ONLY for process lifecycle (ready, ping/pong, shutdown, error)
5
+ * - IPC is used ONLY for process lifecycle (ready, ping/pong, shutdown, error, health-check)
6
6
  * - Task communication uses Socket.IO (NOT IPC)
7
7
  *
8
8
  * Message flows:
9
- * - Parent → Child: IPCCommand (ping, shutdown)
10
- * - Child → Parent: IPCResponse variants (ready, pong, stopped, error)
9
+ * - Parent → Child: IPCCommand (ping, shutdown, health-check)
10
+ * - Child → Parent: IPCResponse variants (ready, pong, stopped, error, health-check-result)
11
11
  */
12
12
  export {};
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Parent Heartbeat Monitoring - Shared utility for worker processes.
3
+ *
4
+ * Why this is needed:
5
+ * - When main process receives SIGKILL, it dies immediately
6
+ * - SIGKILL cannot be caught, so no cleanup happens
7
+ * - IPC 'disconnect' event may not fire
8
+ * - Child processes become orphans (PPID = 1)
9
+ *
10
+ * This module provides a reusable heartbeat monitor that:
11
+ * - Periodically checks if parent is still alive using signal 0
12
+ * - Self-terminates the child process if parent dies
13
+ * - Uses recursive setTimeout pattern (safer than setInterval)
14
+ */
15
+ /**
16
+ * Configuration for the parent heartbeat monitor.
17
+ */
18
+ export interface ParentHeartbeatConfig {
19
+ /** Async cleanup function to call before exit */
20
+ cleanup: () => Promise<void>;
21
+ /** Function to log messages */
22
+ log: (message: string) => void;
23
+ /** Optional pre-cleanup function (e.g., stopInstancePolling) */
24
+ preCleanup?: () => void;
25
+ }
26
+ /**
27
+ * Creates a parent heartbeat monitor.
28
+ *
29
+ * @param config - Configuration for the monitor
30
+ * @returns Object with start() and stop() methods
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * const heartbeat = createParentHeartbeat({
35
+ * log: agentLog,
36
+ * cleanup: stopAgent,
37
+ * })
38
+ *
39
+ * heartbeat.start()
40
+ * // Later...
41
+ * heartbeat.stop()
42
+ * ```
43
+ */
44
+ export declare function createParentHeartbeat(config: ParentHeartbeatConfig): {
45
+ start: () => void;
46
+ stop: () => void;
47
+ };
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Parent Heartbeat Monitoring - Shared utility for worker processes.
3
+ *
4
+ * Why this is needed:
5
+ * - When main process receives SIGKILL, it dies immediately
6
+ * - SIGKILL cannot be caught, so no cleanup happens
7
+ * - IPC 'disconnect' event may not fire
8
+ * - Child processes become orphans (PPID = 1)
9
+ *
10
+ * This module provides a reusable heartbeat monitor that:
11
+ * - Periodically checks if parent is still alive using signal 0
12
+ * - Self-terminates the child process if parent dies
13
+ * - Uses recursive setTimeout pattern (safer than setInterval)
14
+ */
15
+ /** Parent heartbeat check interval in milliseconds */
16
+ const PARENT_HEARTBEAT_INTERVAL_MS = 2000;
17
+ /**
18
+ * Type guard for NodeJS.ErrnoException.
19
+ * Used to safely access error.code without unsafe type assertions.
20
+ */
21
+ function isNodeError(error) {
22
+ return error !== null && typeof error === 'object' && 'code' in error;
23
+ }
24
+ /**
25
+ * Creates a parent heartbeat monitor.
26
+ *
27
+ * @param config - Configuration for the monitor
28
+ * @returns Object with start() and stop() methods
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * const heartbeat = createParentHeartbeat({
33
+ * log: agentLog,
34
+ * cleanup: stopAgent,
35
+ * })
36
+ *
37
+ * heartbeat.start()
38
+ * // Later...
39
+ * heartbeat.stop()
40
+ * ```
41
+ */
42
+ export function createParentHeartbeat(config) {
43
+ const state = {
44
+ isRunning: false,
45
+ parentPid: undefined,
46
+ };
47
+ const { cleanup, log, preCleanup } = config;
48
+ /**
49
+ * Check if parent is alive and schedule next check.
50
+ * Uses recursive setTimeout pattern:
51
+ * - No callback overlap possible
52
+ * - Clean cancellation (just set isRunning = false)
53
+ * - No orphan timers
54
+ */
55
+ const checkParent = () => {
56
+ // Stopped - don't schedule next check
57
+ if (!state.isRunning || !state.parentPid)
58
+ return;
59
+ // Check if parent is still alive using signal 0
60
+ // Signal 0 doesn't send any signal, just checks if process exists
61
+ try {
62
+ process.kill(state.parentPid, 0);
63
+ }
64
+ catch (error) {
65
+ // Only exit on ESRCH (No such process) - parent is genuinely dead
66
+ // EPERM (Operation not permitted) means parent exists but different privileges
67
+ const code = isNodeError(error) ? error.code : undefined;
68
+ if (code !== 'ESRCH') {
69
+ log(`Parent check failed with ${code ?? 'unknown'} (not ESRCH) - continuing`);
70
+ // Schedule next check (don't exit)
71
+ if (state.isRunning) {
72
+ setTimeout(checkParent, PARENT_HEARTBEAT_INTERVAL_MS);
73
+ }
74
+ return;
75
+ }
76
+ // Parent is dead (ESRCH) - self-terminate
77
+ log(`Parent process (${state.parentPid}) died - shutting down to prevent zombie`);
78
+ state.isRunning = false;
79
+ // Run pre-cleanup if provided
80
+ preCleanup?.();
81
+ // Cleanup and exit
82
+ cleanup()
83
+ .catch(() => { })
84
+ .finally(() => {
85
+ // eslint-disable-next-line n/no-process-exit, unicorn/no-process-exit
86
+ process.exit(0);
87
+ });
88
+ return;
89
+ }
90
+ // Schedule next check (only if still running)
91
+ if (state.isRunning) {
92
+ setTimeout(checkParent, PARENT_HEARTBEAT_INTERVAL_MS);
93
+ }
94
+ };
95
+ return {
96
+ /**
97
+ * Start the parent heartbeat monitoring.
98
+ * Safe to call multiple times - only starts once.
99
+ */
100
+ start() {
101
+ // Already running - don't start another
102
+ if (state.isRunning)
103
+ return;
104
+ state.isRunning = true;
105
+ state.parentPid = process.ppid;
106
+ // Start first check after delay
107
+ setTimeout(checkParent, PARENT_HEARTBEAT_INTERVAL_MS);
108
+ log(`Parent heartbeat monitoring started (PPID: ${state.parentPid})`);
109
+ },
110
+ /**
111
+ * Stop the parent heartbeat monitoring.
112
+ * With recursive setTimeout, just set flag to false - next check won't schedule.
113
+ */
114
+ stop() {
115
+ state.isRunning = false;
116
+ },
117
+ };
118
+ }
@@ -47,6 +47,13 @@ export type ProcessManagerConfig = {
47
47
  /** Timeout for process startup (ms) */
48
48
  startupTimeoutMs?: number;
49
49
  };
50
+ /**
51
+ * Options for starting the process manager.
52
+ */
53
+ export type ProcessStartOptions = {
54
+ /** Session ID to pass to Agent process (for stateful sessions) */
55
+ sessionId?: string;
56
+ };
50
57
  /**
51
58
  * ProcessManager - Spawns and manages Transport and Agent processes.
52
59
  *
@@ -57,11 +64,30 @@ export type ProcessManagerConfig = {
57
64
  * - Crash recovery: respawn on exit
58
65
  */
59
66
  export declare class ProcessManager {
67
+ /** Whether an Agent health-check is pending response */
68
+ private agentHealthCheckPending;
69
+ /** Timeout for Agent health-check response */
70
+ private agentHealthCheckTimeout?;
71
+ private currentSessionId?;
60
72
  private healthCheckInterval?;
73
+ /** Guard to prevent concurrent Agent restarts */
74
+ private isRestartingAgent;
75
+ /** Guard to prevent concurrent Transport restarts */
76
+ private isRestartingTransport;
61
77
  private lastHealthCheckTime;
78
+ /** Periodic health check interval (30s) */
79
+ private periodicHealthCheckInterval?;
80
+ /** Stored handler ref for idempotent listener setup (prevents accumulation on respawn) */
81
+ private runtimeMessageHandler?;
62
82
  private readonly shutdownTimeoutMs;
63
83
  private readonly startupTimeoutMs;
64
84
  private state;
85
+ /** Stored handler ref for Transport runtime messages (prevents accumulation on respawn) */
86
+ private transportMessageHandler?;
87
+ /** Whether a Transport ping is pending response */
88
+ private transportPingPending;
89
+ /** Timeout for Transport ping response */
90
+ private transportPingTimeout?;
65
91
  constructor(config?: ProcessManagerConfig);
66
92
  /**
67
93
  * Get current state.
@@ -84,9 +110,10 @@ export declare class ProcessManager {
84
110
  * 3. Start Agent Process with TRANSPORT_PORT env
85
111
  * 4. Wait for Agent 'ready'
86
112
  *
113
+ * @param options - Start options including session ID for stateful sessions
87
114
  * @throws Error if startup fails or times out
88
115
  */
89
- start(): Promise<void>;
116
+ start(options?: ProcessStartOptions): Promise<void>;
90
117
  /**
91
118
  * Stop all processes gracefully.
92
119
  *
@@ -100,11 +127,46 @@ export declare class ProcessManager {
100
127
  * In dev mode (tsx), import.meta.url points to src/ but workers need compiled .js from dist/
101
128
  */
102
129
  private getWorkerDir;
130
+ /**
131
+ * Handle runtime IPC messages from Agent process.
132
+ * Called by the message event handler set up in setupAgentRuntimeHandlers.
133
+ */
134
+ private handleAgentRuntimeMessage;
103
135
  /**
104
136
  * Handle system wake from sleep.
105
137
  * Verify processes are still alive and restart if needed.
138
+ * Triggers immediate health checks on both Transport and Agent.
106
139
  */
107
140
  private handleSystemWake;
141
+ /**
142
+ * Handle runtime IPC messages from Transport process.
143
+ * Called by the message event handler set up in setupTransportRuntimeHandlers.
144
+ */
145
+ private handleTransportRuntimeMessage;
146
+ /**
147
+ * Send health-check to Agent with timeout.
148
+ * If no response within timeout, Agent is considered stuck and restarted.
149
+ * Used by periodic health check and after system wake.
150
+ */
151
+ private healthCheckAgentWithTimeout;
152
+ /**
153
+ * Send ping to Transport with timeout.
154
+ * If no pong received within timeout, Transport is considered zombie and restarted.
155
+ */
156
+ private pingTransportWithTimeout;
157
+ /**
158
+ * Restart Agent process gracefully.
159
+ * Used when health-check fails after sleep/wake or periodic check.
160
+ * Guarded to prevent concurrent restart race conditions.
161
+ */
162
+ private restartAgent;
163
+ /**
164
+ * Restart Transport process gracefully.
165
+ * Used when ping timeout detects zombie process after sleep/wake or periodic check.
166
+ * Note: Agent must also be restarted since Transport port changes.
167
+ * Guarded to prevent concurrent restart race conditions.
168
+ */
169
+ private restartTransport;
108
170
  /**
109
171
  * Send IPC message to child process.
110
172
  */
@@ -113,10 +175,26 @@ export declare class ProcessManager {
113
175
  * Setup Agent crash recovery.
114
176
  */
115
177
  private setupAgentCrashRecovery;
178
+ /**
179
+ * Setup runtime message handlers for Agent process.
180
+ * Handles IPC messages that arrive during normal operation (not just startup).
181
+ *
182
+ * IMPORTANT: Uses stored handler reference to prevent listener accumulation.
183
+ * Each respawn calls this method, so we must remove the old listener first.
184
+ */
185
+ private setupAgentRuntimeHandlers;
116
186
  /**
117
187
  * Setup Transport crash recovery.
118
188
  */
119
189
  private setupTransportCrashRecovery;
190
+ /**
191
+ * Setup runtime message handlers for Transport process.
192
+ * Handles IPC messages that arrive during normal operation (pong for health check).
193
+ *
194
+ * IMPORTANT: Uses stored handler reference to prevent listener accumulation.
195
+ * Each respawn calls this method, so we must remove the old listener first.
196
+ */
197
+ private setupTransportRuntimeHandlers;
120
198
  /**
121
199
  * Start Agent Process.
122
200
  */
@@ -126,6 +204,11 @@ export declare class ProcessManager {
126
204
  * Detects system sleep by monitoring for large time gaps between checks.
127
205
  */
128
206
  private startHealthCheck;
207
+ /**
208
+ * Start periodic health check for Transport and Agent.
209
+ * Runs every 30s to detect zombie processes mid-session (not just after wake).
210
+ */
211
+ private startPeriodicHealthCheck;
129
212
  /**
130
213
  * Start Transport Process.
131
214
  * @returns The port Transport is listening on
@@ -139,6 +222,11 @@ export declare class ProcessManager {
139
222
  * Stop health check interval.
140
223
  */
141
224
  private stopHealthCheck;
225
+ /**
226
+ * Stop periodic health check interval.
227
+ * Also clears any pending agent health check timeout.
228
+ */
229
+ private stopPeriodicHealthCheck;
142
230
  /**
143
231
  * Stop Transport Process.
144
232
  */