episoda 0.2.36 → 0.2.37
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.
|
@@ -2721,7 +2721,7 @@ var require_package = __commonJS({
|
|
|
2721
2721
|
"package.json"(exports2, module2) {
|
|
2722
2722
|
module2.exports = {
|
|
2723
2723
|
name: "episoda",
|
|
2724
|
-
version: "0.2.
|
|
2724
|
+
version: "0.2.36",
|
|
2725
2725
|
description: "CLI tool for Episoda local development workflow orchestration",
|
|
2726
2726
|
main: "dist/index.js",
|
|
2727
2727
|
types: "dist/index.d.ts",
|
|
@@ -6266,39 +6266,7 @@ async function getWorktreeInfoForModule(moduleUid) {
|
|
|
6266
6266
|
}
|
|
6267
6267
|
|
|
6268
6268
|
// src/utils/port-allocator.ts
|
|
6269
|
-
var PORT_RANGE_START = 3100;
|
|
6270
|
-
var PORT_RANGE_END = 3199;
|
|
6271
|
-
var PORT_WARNING_THRESHOLD = 80;
|
|
6272
6269
|
var portAssignments = /* @__PURE__ */ new Map();
|
|
6273
|
-
function allocatePort(moduleUid) {
|
|
6274
|
-
const existing = portAssignments.get(moduleUid);
|
|
6275
|
-
if (existing) {
|
|
6276
|
-
return existing;
|
|
6277
|
-
}
|
|
6278
|
-
const usedPorts = new Set(portAssignments.values());
|
|
6279
|
-
if (usedPorts.size >= PORT_WARNING_THRESHOLD) {
|
|
6280
|
-
console.warn(
|
|
6281
|
-
`[PortAllocator] Warning: ${usedPorts.size}/${PORT_RANGE_END - PORT_RANGE_START + 1} ports allocated`
|
|
6282
|
-
);
|
|
6283
|
-
}
|
|
6284
|
-
for (let port = PORT_RANGE_START; port <= PORT_RANGE_END; port++) {
|
|
6285
|
-
if (!usedPorts.has(port)) {
|
|
6286
|
-
portAssignments.set(moduleUid, port);
|
|
6287
|
-
console.log(`[PortAllocator] Assigned port ${port} to ${moduleUid}`);
|
|
6288
|
-
return port;
|
|
6289
|
-
}
|
|
6290
|
-
}
|
|
6291
|
-
throw new Error(
|
|
6292
|
-
`No available ports in range ${PORT_RANGE_START}-${PORT_RANGE_END}. ${portAssignments.size} modules are using all available ports.`
|
|
6293
|
-
);
|
|
6294
|
-
}
|
|
6295
|
-
function releasePort(moduleUid) {
|
|
6296
|
-
const port = portAssignments.get(moduleUid);
|
|
6297
|
-
if (port) {
|
|
6298
|
-
portAssignments.delete(moduleUid);
|
|
6299
|
-
console.log(`[PortAllocator] Released port ${port} from ${moduleUid}`);
|
|
6300
|
-
}
|
|
6301
|
-
}
|
|
6302
6270
|
function clearAllPorts() {
|
|
6303
6271
|
const count = portAssignments.size;
|
|
6304
6272
|
portAssignments.clear();
|
|
@@ -6531,7 +6499,7 @@ var Daemon = class _Daemon {
|
|
|
6531
6499
|
// EP837: Prevent concurrent commit syncs (backpressure guard)
|
|
6532
6500
|
this.commitSyncInProgress = false;
|
|
6533
6501
|
// EP843: Per-module mutex for tunnel operations
|
|
6534
|
-
// Prevents race conditions between
|
|
6502
|
+
// EP1003: Prevents race conditions between server-orchestrated tunnel commands
|
|
6535
6503
|
this.tunnelOperationLocks = /* @__PURE__ */ new Map();
|
|
6536
6504
|
// moduleUid -> operation promise
|
|
6537
6505
|
// EP929: Health check polling interval (restored from EP843 removal)
|
|
@@ -6816,7 +6784,7 @@ var Daemon = class _Daemon {
|
|
|
6816
6784
|
* EP843: Acquire a per-module lock for tunnel operations
|
|
6817
6785
|
*
|
|
6818
6786
|
* Prevents race conditions between:
|
|
6819
|
-
* -
|
|
6787
|
+
* - Server-orchestrated tunnel_start commands (EP1003)
|
|
6820
6788
|
* - module_state_changed event handler
|
|
6821
6789
|
* - Multiple rapid state transitions
|
|
6822
6790
|
*
|
|
@@ -7358,9 +7326,6 @@ var Daemon = class _Daemon {
|
|
|
7358
7326
|
this.syncMachineProjectPath(projectId, projectPath).catch((err) => {
|
|
7359
7327
|
console.warn("[Daemon] EP995: Project path sync failed:", err.message);
|
|
7360
7328
|
});
|
|
7361
|
-
this.autoStartTunnelsForProject(projectPath, projectId).catch((error) => {
|
|
7362
|
-
console.error(`[Daemon] EP819: Failed to auto-start tunnels:`, error);
|
|
7363
|
-
});
|
|
7364
7329
|
cleanupStaleCommits(projectPath).then((cleanupResult) => {
|
|
7365
7330
|
if (cleanupResult.deleted_count > 0) {
|
|
7366
7331
|
console.log(`[Daemon] EP950: Cleaned up ${cleanupResult.deleted_count} stale commit(s) on connect`);
|
|
@@ -7368,8 +7333,8 @@ var Daemon = class _Daemon {
|
|
|
7368
7333
|
}).catch((err) => {
|
|
7369
7334
|
console.warn("[Daemon] EP950: Cleanup on connect failed:", err.message);
|
|
7370
7335
|
});
|
|
7371
|
-
this.reconcileWorktrees(projectId, projectPath).catch((err) => {
|
|
7372
|
-
console.warn("[Daemon]
|
|
7336
|
+
this.reconcileWorktrees(projectId, projectPath, client).catch((err) => {
|
|
7337
|
+
console.warn("[Daemon] EP1003: Reconciliation report failed:", err.message);
|
|
7373
7338
|
});
|
|
7374
7339
|
});
|
|
7375
7340
|
client.on("module_state_changed", async (message) => {
|
|
@@ -7377,106 +7342,18 @@ var Daemon = class _Daemon {
|
|
|
7377
7342
|
const { moduleUid, state, previousState, branchName, devMode, checkoutMachineId } = message;
|
|
7378
7343
|
console.log(`[Daemon] EP843: Module ${moduleUid} state changed: ${previousState} \u2192 ${state}`);
|
|
7379
7344
|
if (devMode !== "local") {
|
|
7380
|
-
console.log(`[Daemon]
|
|
7345
|
+
console.log(`[Daemon] EP1003: State change for non-local module ${moduleUid} (mode: ${devMode || "unknown"})`);
|
|
7381
7346
|
return;
|
|
7382
7347
|
}
|
|
7383
7348
|
if (checkoutMachineId && checkoutMachineId !== this.deviceId) {
|
|
7384
|
-
console.log(`[Daemon]
|
|
7349
|
+
console.log(`[Daemon] EP1003: State change for ${moduleUid} handled by different machine: ${checkoutMachineId}`);
|
|
7385
7350
|
return;
|
|
7386
7351
|
}
|
|
7387
|
-
|
|
7388
|
-
|
|
7389
|
-
|
|
7390
|
-
|
|
7391
|
-
|
|
7392
|
-
const startingWork = previousState === "ready" && state === "doing";
|
|
7393
|
-
const tunnelNotRunning = !tunnelManager.hasTunnel(moduleUid);
|
|
7394
|
-
const needsCrashRecovery = isInActiveZone && tunnelNotRunning;
|
|
7395
|
-
if (startingWork || needsCrashRecovery) {
|
|
7396
|
-
if (tunnelManager.hasTunnel(moduleUid)) {
|
|
7397
|
-
console.log(`[Daemon] EP843: Tunnel already running for ${moduleUid}, skipping start`);
|
|
7398
|
-
return;
|
|
7399
|
-
}
|
|
7400
|
-
console.log(`[Daemon] EP956: Starting tunnel for ${moduleUid} (${previousState} \u2192 ${state})`);
|
|
7401
|
-
try {
|
|
7402
|
-
let worktree = await getWorktreeInfoForModule(moduleUid);
|
|
7403
|
-
if (!worktree) {
|
|
7404
|
-
console.error(`[Daemon] EP956: Cannot resolve worktree path for ${moduleUid} (missing config slugs)`);
|
|
7405
|
-
return;
|
|
7406
|
-
}
|
|
7407
|
-
if (!worktree.exists) {
|
|
7408
|
-
console.log(`[Daemon] EP956: No worktree for ${moduleUid} at ${worktree.path}, skipping tunnel`);
|
|
7409
|
-
return;
|
|
7410
|
-
}
|
|
7411
|
-
await this.updateModuleWorktreeStatus(moduleUid, "ready", worktree.path);
|
|
7412
|
-
const port = allocatePort(moduleUid);
|
|
7413
|
-
console.log(`[Daemon] EP956: Using worktree ${worktree.path} on port ${port}`);
|
|
7414
|
-
const devConfig = await (0, import_core10.loadConfig)();
|
|
7415
|
-
const devServerScript = devConfig?.project_settings?.worktree_dev_server_script;
|
|
7416
|
-
const devServerResult = await ensureDevServer(worktree.path, port, moduleUid, devServerScript);
|
|
7417
|
-
if (!devServerResult.success) {
|
|
7418
|
-
console.error(`[Daemon] EP956: Dev server failed for ${moduleUid}: ${devServerResult.error}`);
|
|
7419
|
-
releasePort(moduleUid);
|
|
7420
|
-
return;
|
|
7421
|
-
}
|
|
7422
|
-
const config2 = devConfig;
|
|
7423
|
-
const apiUrl = config2?.api_url || "https://episoda.dev";
|
|
7424
|
-
const startResult = await tunnelManager.startTunnel({
|
|
7425
|
-
moduleUid,
|
|
7426
|
-
port,
|
|
7427
|
-
onUrl: async (url) => {
|
|
7428
|
-
console.log(`[Daemon] EP956: Tunnel URL for ${moduleUid}: ${url}`);
|
|
7429
|
-
try {
|
|
7430
|
-
await fetchWithAuth(`${apiUrl}/api/modules/${moduleUid}/tunnel`, {
|
|
7431
|
-
method: "POST",
|
|
7432
|
-
body: JSON.stringify({ tunnel_url: url })
|
|
7433
|
-
});
|
|
7434
|
-
} catch (err) {
|
|
7435
|
-
console.warn(`[Daemon] EP956: Failed to report tunnel URL:`, err instanceof Error ? err.message : err);
|
|
7436
|
-
}
|
|
7437
|
-
},
|
|
7438
|
-
onStatusChange: (status, error) => {
|
|
7439
|
-
if (status === "error") {
|
|
7440
|
-
console.error(`[Daemon] EP956: Tunnel error for ${moduleUid}: ${error}`);
|
|
7441
|
-
}
|
|
7442
|
-
}
|
|
7443
|
-
});
|
|
7444
|
-
if (startResult.success) {
|
|
7445
|
-
console.log(`[Daemon] EP956: Tunnel started for ${moduleUid}`);
|
|
7446
|
-
} else {
|
|
7447
|
-
console.error(`[Daemon] EP956: Tunnel failed for ${moduleUid}: ${startResult.error}`);
|
|
7448
|
-
releasePort(moduleUid);
|
|
7449
|
-
}
|
|
7450
|
-
} catch (error) {
|
|
7451
|
-
console.error(`[Daemon] EP956: Error starting tunnel for ${moduleUid}:`, error);
|
|
7452
|
-
releasePort(moduleUid);
|
|
7453
|
-
}
|
|
7454
|
-
}
|
|
7455
|
-
if (state === "done" && wasInActiveZone) {
|
|
7456
|
-
console.log(`[Daemon] EP956: Stopping tunnel for ${moduleUid} (${previousState} \u2192 done)`);
|
|
7457
|
-
try {
|
|
7458
|
-
await tunnelManager.stopTunnel(moduleUid);
|
|
7459
|
-
releasePort(moduleUid);
|
|
7460
|
-
console.log(`[Daemon] EP956: Tunnel stopped and port released for ${moduleUid}`);
|
|
7461
|
-
const config2 = await (0, import_core10.loadConfig)();
|
|
7462
|
-
const apiUrl = config2?.api_url || "https://episoda.dev";
|
|
7463
|
-
try {
|
|
7464
|
-
await fetchWithAuth(`${apiUrl}/api/modules/${moduleUid}/tunnel`, {
|
|
7465
|
-
method: "POST",
|
|
7466
|
-
body: JSON.stringify({ tunnel_url: null })
|
|
7467
|
-
});
|
|
7468
|
-
} catch (err) {
|
|
7469
|
-
console.warn(`[Daemon] EP956: Failed to clear tunnel URL:`, err instanceof Error ? err.message : err);
|
|
7470
|
-
}
|
|
7471
|
-
this.cleanupModuleWorktree(moduleUid).catch((err) => {
|
|
7472
|
-
console.warn(`[Daemon] EP956: Async cleanup failed for ${moduleUid}:`, err instanceof Error ? err.message : err);
|
|
7473
|
-
});
|
|
7474
|
-
} catch (error) {
|
|
7475
|
-
console.error(`[Daemon] EP956: Error stopping tunnel for ${moduleUid}:`, error);
|
|
7476
|
-
releasePort(moduleUid);
|
|
7477
|
-
}
|
|
7478
|
-
}
|
|
7479
|
-
});
|
|
7352
|
+
if (previousState === "ready" && state === "doing") {
|
|
7353
|
+
console.log(`[Daemon] EP1003: Module ${moduleUid} entering doing - server will send tunnel_start`);
|
|
7354
|
+
} else if (state === "done") {
|
|
7355
|
+
console.log(`[Daemon] EP1003: Module ${moduleUid} entering done - server will send tunnel_stop`);
|
|
7356
|
+
}
|
|
7480
7357
|
}
|
|
7481
7358
|
});
|
|
7482
7359
|
client.on("error", (message) => {
|
|
@@ -7809,98 +7686,87 @@ var Daemon = class _Daemon {
|
|
|
7809
7686
|
* This self-healing mechanism catches modules that transitioned
|
|
7810
7687
|
* while the daemon was disconnected.
|
|
7811
7688
|
*/
|
|
7812
|
-
|
|
7813
|
-
|
|
7689
|
+
/**
|
|
7690
|
+
* EP1003: Report-only reconciliation
|
|
7691
|
+
* Daemon reports local state to server, server decides what commands to send.
|
|
7692
|
+
* This replaces autonomous worktree creation and tunnel starting.
|
|
7693
|
+
*/
|
|
7694
|
+
async reconcileWorktrees(projectId, projectPath, client) {
|
|
7695
|
+
console.log(`[Daemon] EP1003: Starting reconciliation report for project ${projectId}`);
|
|
7814
7696
|
try {
|
|
7815
7697
|
if (!this.deviceId) {
|
|
7816
|
-
console.log("[Daemon]
|
|
7698
|
+
console.log("[Daemon] EP1003: Cannot reconcile - deviceId not available yet");
|
|
7817
7699
|
return;
|
|
7818
7700
|
}
|
|
7819
7701
|
const config = await (0, import_core10.loadConfig)();
|
|
7820
7702
|
if (!config) return;
|
|
7821
7703
|
const apiUrl = config.api_url || "https://episoda.dev";
|
|
7822
|
-
const
|
|
7823
|
-
|
|
7824
|
-
|
|
7704
|
+
const controller = new AbortController();
|
|
7705
|
+
const timeoutId = setTimeout(() => controller.abort(), 1e4);
|
|
7706
|
+
let modulesResponse;
|
|
7707
|
+
try {
|
|
7708
|
+
modulesResponse = await fetchWithAuth(
|
|
7709
|
+
`${apiUrl}/api/modules?state=doing,review&dev_mode=local&checkout_machine_id=${this.deviceId}&project_id=${projectId}`,
|
|
7710
|
+
{ signal: controller.signal }
|
|
7711
|
+
);
|
|
7712
|
+
} finally {
|
|
7713
|
+
clearTimeout(timeoutId);
|
|
7714
|
+
}
|
|
7825
7715
|
if (!modulesResponse.ok) {
|
|
7826
|
-
console.warn(`[Daemon]
|
|
7716
|
+
console.warn(`[Daemon] EP1003: Failed to fetch modules for reconciliation: ${modulesResponse.status}`);
|
|
7827
7717
|
return;
|
|
7828
7718
|
}
|
|
7829
7719
|
const modulesData = await modulesResponse.json();
|
|
7830
7720
|
const modules = modulesData.modules || [];
|
|
7831
|
-
|
|
7832
|
-
|
|
7833
|
-
|
|
7834
|
-
|
|
7835
|
-
|
|
7836
|
-
const worktreeManager = new WorktreeManager(projectPath);
|
|
7837
|
-
const initialized = await worktreeManager.initialize();
|
|
7838
|
-
if (!initialized) {
|
|
7839
|
-
console.error(`[Daemon] EP995: Failed to initialize WorktreeManager`);
|
|
7840
|
-
return;
|
|
7841
|
-
}
|
|
7721
|
+
console.log(`[Daemon] EP1003: Building reconciliation report for ${modules.length} module(s)`);
|
|
7722
|
+
const tunnelManager = getTunnelManager();
|
|
7723
|
+
await tunnelManager.initialize();
|
|
7724
|
+
const moduleStatuses = [];
|
|
7725
|
+
const expectedModuleUids = new Set(modules.map((m) => m.uid));
|
|
7842
7726
|
for (const module2 of modules) {
|
|
7843
7727
|
const moduleUid = module2.uid;
|
|
7844
|
-
const branchName = module2.branch_name;
|
|
7845
7728
|
const worktree = await getWorktreeInfoForModule(moduleUid);
|
|
7846
|
-
|
|
7847
|
-
|
|
7848
|
-
|
|
7849
|
-
|
|
7850
|
-
|
|
7851
|
-
|
|
7852
|
-
|
|
7853
|
-
|
|
7854
|
-
|
|
7855
|
-
|
|
7856
|
-
|
|
7857
|
-
|
|
7858
|
-
|
|
7859
|
-
|
|
7860
|
-
|
|
7861
|
-
|
|
7862
|
-
|
|
7863
|
-
|
|
7864
|
-
|
|
7865
|
-
|
|
7866
|
-
|
|
7867
|
-
if (!newWorktree?.exists) {
|
|
7868
|
-
console.error(`[Daemon] EP995: Worktree still not found after creation`);
|
|
7869
|
-
continue;
|
|
7870
|
-
}
|
|
7871
|
-
const setupConfig = config.project_settings;
|
|
7872
|
-
const envVars = await fetchEnvVars2();
|
|
7873
|
-
console.log(`[Daemon] EP995: Starting setup for reconciled module ${moduleUid}`);
|
|
7874
|
-
await worktreeManager.updateWorktreeStatus(moduleUid, "pending");
|
|
7875
|
-
await this.updateModuleWorktreeStatus(moduleUid, "pending", newWorktree.path);
|
|
7876
|
-
this.runWorktreeSetupAsync(
|
|
7877
|
-
moduleUid,
|
|
7878
|
-
worktreeManager,
|
|
7879
|
-
setupConfig?.worktree_copy_files || [],
|
|
7880
|
-
setupConfig?.worktree_setup_script,
|
|
7881
|
-
newWorktree.path,
|
|
7882
|
-
envVars
|
|
7883
|
-
).then(() => {
|
|
7884
|
-
console.log(`[Daemon] EP995: Setup complete for reconciled ${moduleUid}`);
|
|
7885
|
-
this.startTunnelForModule(moduleUid, newWorktree.path);
|
|
7886
|
-
}).catch((err) => {
|
|
7887
|
-
console.error(`[Daemon] EP995: Setup failed for reconciled ${moduleUid}:`, err);
|
|
7729
|
+
const tunnelRunning = tunnelManager.hasTunnel(moduleUid);
|
|
7730
|
+
const tunnelInfo = tunnelManager.getTunnel(moduleUid);
|
|
7731
|
+
const status = {
|
|
7732
|
+
moduleUid,
|
|
7733
|
+
moduleState: module2.state,
|
|
7734
|
+
worktreeExists: worktree?.exists || false,
|
|
7735
|
+
worktreePath: worktree?.path,
|
|
7736
|
+
tunnelRunning,
|
|
7737
|
+
tunnelPort: tunnelInfo?.port
|
|
7738
|
+
};
|
|
7739
|
+
moduleStatuses.push(status);
|
|
7740
|
+
console.log(`[Daemon] EP1003: Module ${moduleUid}: worktree=${status.worktreeExists}, tunnel=${status.tunnelRunning}`);
|
|
7741
|
+
}
|
|
7742
|
+
const allTunnels = tunnelManager.getAllTunnels();
|
|
7743
|
+
const orphanTunnels = [];
|
|
7744
|
+
for (const tunnel of allTunnels) {
|
|
7745
|
+
if (!expectedModuleUids.has(tunnel.moduleUid)) {
|
|
7746
|
+
console.log(`[Daemon] EP1003: Detected orphan tunnel for ${tunnel.moduleUid} (port ${tunnel.port})`);
|
|
7747
|
+
orphanTunnels.push({
|
|
7748
|
+
moduleUid: tunnel.moduleUid,
|
|
7749
|
+
port: tunnel.port
|
|
7888
7750
|
});
|
|
7889
|
-
} else {
|
|
7890
|
-
await this.updateModuleWorktreeStatus(moduleUid, "ready", worktree.path);
|
|
7891
|
-
const tunnelManager = getTunnelManager();
|
|
7892
|
-
await tunnelManager.initialize();
|
|
7893
|
-
if (!tunnelManager.hasTunnel(moduleUid)) {
|
|
7894
|
-
console.log(`[Daemon] EP995: Module ${moduleUid} has worktree but no tunnel - starting...`);
|
|
7895
|
-
await this.startTunnelForModule(moduleUid, worktree.path);
|
|
7896
|
-
} else {
|
|
7897
|
-
console.log(`[Daemon] EP995: Module ${moduleUid} OK - worktree and tunnel exist`);
|
|
7898
|
-
}
|
|
7899
7751
|
}
|
|
7900
7752
|
}
|
|
7901
|
-
|
|
7753
|
+
if (orphanTunnels.length > 0) {
|
|
7754
|
+
console.log(`[Daemon] EP1003: Reporting ${orphanTunnels.length} orphan tunnel(s) for server cleanup`);
|
|
7755
|
+
}
|
|
7756
|
+
const report = {
|
|
7757
|
+
projectId,
|
|
7758
|
+
machineId: this.deviceId,
|
|
7759
|
+
modules: moduleStatuses,
|
|
7760
|
+
orphanTunnels: orphanTunnels.length > 0 ? orphanTunnels : void 0
|
|
7761
|
+
};
|
|
7762
|
+
console.log(`[Daemon] EP1003: Sending reconciliation report with ${moduleStatuses.length} module(s)`);
|
|
7763
|
+
await client.send({
|
|
7764
|
+
type: "reconciliation_report",
|
|
7765
|
+
report
|
|
7766
|
+
});
|
|
7767
|
+
console.log("[Daemon] EP1003: Reconciliation report sent - awaiting server commands");
|
|
7902
7768
|
} catch (error) {
|
|
7903
|
-
console.error("[Daemon]
|
|
7769
|
+
console.error("[Daemon] EP1003: Reconciliation error:", error instanceof Error ? error.message : error);
|
|
7904
7770
|
throw error;
|
|
7905
7771
|
}
|
|
7906
7772
|
}
|
|
@@ -8112,222 +7978,10 @@ var Daemon = class _Daemon {
|
|
|
8112
7978
|
throw error;
|
|
8113
7979
|
}
|
|
8114
7980
|
}
|
|
8115
|
-
|
|
8116
|
-
|
|
8117
|
-
|
|
8118
|
-
|
|
8119
|
-
const tunnelManager = getTunnelManager();
|
|
8120
|
-
await tunnelManager.initialize();
|
|
8121
|
-
if (tunnelManager.hasTunnel(moduleUid)) {
|
|
8122
|
-
console.log(`[Daemon] EP959: Tunnel already running for ${moduleUid}`);
|
|
8123
|
-
return;
|
|
8124
|
-
}
|
|
8125
|
-
try {
|
|
8126
|
-
const config = await (0, import_core10.loadConfig)();
|
|
8127
|
-
const apiUrl = config?.api_url || "https://episoda.dev";
|
|
8128
|
-
const devServerScript = config?.project_settings?.worktree_dev_server_script;
|
|
8129
|
-
const port = allocatePort(moduleUid);
|
|
8130
|
-
console.log(`[Daemon] EP959: Post-setup tunnel start for ${moduleUid} on port ${port}`);
|
|
8131
|
-
const devServerResult = await ensureDevServer(worktreePath, port, moduleUid, devServerScript);
|
|
8132
|
-
if (!devServerResult.success) {
|
|
8133
|
-
console.error(`[Daemon] EP959: Dev server failed for ${moduleUid}: ${devServerResult.error}`);
|
|
8134
|
-
releasePort(moduleUid);
|
|
8135
|
-
return;
|
|
8136
|
-
}
|
|
8137
|
-
const startResult = await tunnelManager.startTunnel({
|
|
8138
|
-
moduleUid,
|
|
8139
|
-
port,
|
|
8140
|
-
onUrl: async (url) => {
|
|
8141
|
-
console.log(`[Daemon] EP959: Tunnel URL for ${moduleUid}: ${url}`);
|
|
8142
|
-
try {
|
|
8143
|
-
await fetchWithAuth(`${apiUrl}/api/modules/${moduleUid}/tunnel`, {
|
|
8144
|
-
method: "POST",
|
|
8145
|
-
body: JSON.stringify({ tunnel_url: url })
|
|
8146
|
-
});
|
|
8147
|
-
} catch (err) {
|
|
8148
|
-
console.warn(`[Daemon] EP959: Failed to report tunnel URL:`, err instanceof Error ? err.message : err);
|
|
8149
|
-
}
|
|
8150
|
-
},
|
|
8151
|
-
onStatusChange: (status, error) => {
|
|
8152
|
-
if (status === "error") {
|
|
8153
|
-
console.error(`[Daemon] EP959: Tunnel error for ${moduleUid}: ${error}`);
|
|
8154
|
-
}
|
|
8155
|
-
}
|
|
8156
|
-
});
|
|
8157
|
-
if (startResult.success) {
|
|
8158
|
-
console.log(`[Daemon] EP959: Tunnel started for ${moduleUid}`);
|
|
8159
|
-
} else {
|
|
8160
|
-
console.error(`[Daemon] EP959: Tunnel failed for ${moduleUid}: ${startResult.error}`);
|
|
8161
|
-
releasePort(moduleUid);
|
|
8162
|
-
}
|
|
8163
|
-
} catch (error) {
|
|
8164
|
-
console.error(`[Daemon] EP959: Error starting tunnel for ${moduleUid}:`, error);
|
|
8165
|
-
releasePort(moduleUid);
|
|
8166
|
-
}
|
|
8167
|
-
}
|
|
8168
|
-
/**
|
|
8169
|
-
* EP819: Auto-start tunnels for active local modules on daemon connect/reconnect
|
|
8170
|
-
*
|
|
8171
|
-
* Queries for modules in doing/review state with dev_mode=local that don't have
|
|
8172
|
-
* an active tunnel_url, and starts tunnels for each.
|
|
8173
|
-
*/
|
|
8174
|
-
async autoStartTunnelsForProject(projectPath, projectUid) {
|
|
8175
|
-
console.log(`[Daemon] EP819: Checking for active local modules to auto-start tunnels...`);
|
|
8176
|
-
try {
|
|
8177
|
-
const config = await (0, import_core10.loadConfig)();
|
|
8178
|
-
if (!config?.access_token) {
|
|
8179
|
-
console.warn(`[Daemon] EP819: No access token, skipping tunnel auto-start`);
|
|
8180
|
-
return;
|
|
8181
|
-
}
|
|
8182
|
-
const apiUrl = config.api_url || "https://episoda.dev";
|
|
8183
|
-
const response = await fetchWithAuth(
|
|
8184
|
-
`${apiUrl}/api/modules?state=doing,review&fields=id,uid,dev_mode,tunnel_url,checkout_machine_id`
|
|
8185
|
-
);
|
|
8186
|
-
if (!response.ok) {
|
|
8187
|
-
console.warn(`[Daemon] EP819: Failed to fetch modules: ${response.status}`);
|
|
8188
|
-
return;
|
|
8189
|
-
}
|
|
8190
|
-
const data = await response.json();
|
|
8191
|
-
const modules = data.modules || [];
|
|
8192
|
-
const tunnelManager = getTunnelManager();
|
|
8193
|
-
await tunnelManager.initialize();
|
|
8194
|
-
const activeTunnelUids = tunnelManager.getActiveModuleUids();
|
|
8195
|
-
const validModuleUids = new Set(
|
|
8196
|
-
modules.filter(
|
|
8197
|
-
(m) => m.dev_mode === "local" && (!m.checkout_machine_id || m.checkout_machine_id === this.deviceId)
|
|
8198
|
-
).map((m) => m.uid)
|
|
8199
|
-
);
|
|
8200
|
-
const orphanedTunnels = activeTunnelUids.filter((uid) => !validModuleUids.has(uid));
|
|
8201
|
-
if (orphanedTunnels.length > 0) {
|
|
8202
|
-
console.log(`[Daemon] EP956: Found ${orphanedTunnels.length} orphaned tunnels to stop: ${orphanedTunnels.join(", ")}`);
|
|
8203
|
-
for (const orphanUid of orphanedTunnels) {
|
|
8204
|
-
try {
|
|
8205
|
-
await tunnelManager.stopTunnel(orphanUid);
|
|
8206
|
-
releasePort(orphanUid);
|
|
8207
|
-
console.log(`[Daemon] EP956: Stopped orphaned tunnel and released port for ${orphanUid}`);
|
|
8208
|
-
try {
|
|
8209
|
-
await fetchWithAuth(`${apiUrl}/api/modules/${orphanUid}/tunnel`, {
|
|
8210
|
-
method: "POST",
|
|
8211
|
-
body: JSON.stringify({ tunnel_url: null })
|
|
8212
|
-
});
|
|
8213
|
-
} catch (err) {
|
|
8214
|
-
console.warn(`[Daemon] EP956: Failed to clear tunnel URL for ${orphanUid}:`, err instanceof Error ? err.message : err);
|
|
8215
|
-
}
|
|
8216
|
-
} catch (err) {
|
|
8217
|
-
console.error(`[Daemon] EP956: Failed to stop orphaned tunnel ${orphanUid}:`, err instanceof Error ? err.message : err);
|
|
8218
|
-
}
|
|
8219
|
-
}
|
|
8220
|
-
}
|
|
8221
|
-
const localModulesNeedingTunnel = modules.filter(
|
|
8222
|
-
(m) => m.dev_mode === "local" && (!m.checkout_machine_id || m.checkout_machine_id === this.deviceId) && !tunnelManager.hasTunnel(m.uid)
|
|
8223
|
-
);
|
|
8224
|
-
if (localModulesNeedingTunnel.length === 0) {
|
|
8225
|
-
console.log(`[Daemon] EP819: No local modules need tunnel auto-start`);
|
|
8226
|
-
return;
|
|
8227
|
-
}
|
|
8228
|
-
console.log(`[Daemon] EP956: Found ${localModulesNeedingTunnel.length} local modules needing tunnels`);
|
|
8229
|
-
for (const module2 of localModulesNeedingTunnel) {
|
|
8230
|
-
const moduleUid = module2.uid;
|
|
8231
|
-
const worktree = await getWorktreeInfoForModule(moduleUid);
|
|
8232
|
-
if (!worktree) {
|
|
8233
|
-
console.warn(`[Daemon] EP956: Cannot resolve worktree for ${moduleUid} (missing config slugs)`);
|
|
8234
|
-
continue;
|
|
8235
|
-
}
|
|
8236
|
-
if (!worktree.exists) {
|
|
8237
|
-
console.log(`[Daemon] EP956: No worktree for ${moduleUid} at ${worktree.path}, skipping`);
|
|
8238
|
-
continue;
|
|
8239
|
-
}
|
|
8240
|
-
const port = allocatePort(moduleUid);
|
|
8241
|
-
console.log(`[Daemon] EP956: Auto-starting tunnel for ${moduleUid} at ${worktree.path} on port ${port}`);
|
|
8242
|
-
const reportTunnelStatus = async (statusData) => {
|
|
8243
|
-
try {
|
|
8244
|
-
const statusResponse = await fetchWithAuth(`${apiUrl}/api/modules/${moduleUid}/tunnel`, {
|
|
8245
|
-
method: "POST",
|
|
8246
|
-
body: JSON.stringify(statusData)
|
|
8247
|
-
});
|
|
8248
|
-
if (statusResponse.ok) {
|
|
8249
|
-
console.log(`[Daemon] EP819: Tunnel status reported for ${moduleUid}`);
|
|
8250
|
-
} else {
|
|
8251
|
-
console.warn(`[Daemon] EP819: Failed to report tunnel status: ${statusResponse.statusText}`);
|
|
8252
|
-
}
|
|
8253
|
-
} catch (reportError) {
|
|
8254
|
-
console.warn(`[Daemon] EP819: Error reporting tunnel status:`, reportError);
|
|
8255
|
-
}
|
|
8256
|
-
};
|
|
8257
|
-
(async () => {
|
|
8258
|
-
await this.withTunnelLock(moduleUid, async () => {
|
|
8259
|
-
if (tunnelManager.hasTunnel(moduleUid)) {
|
|
8260
|
-
console.log(`[Daemon] EP956: Tunnel already running for ${moduleUid}, skipping auto-start`);
|
|
8261
|
-
return;
|
|
8262
|
-
}
|
|
8263
|
-
const MAX_RETRIES = 3;
|
|
8264
|
-
const RETRY_DELAY_MS = 3e3;
|
|
8265
|
-
await reportTunnelStatus({
|
|
8266
|
-
tunnel_started_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
8267
|
-
tunnel_error: null
|
|
8268
|
-
});
|
|
8269
|
-
try {
|
|
8270
|
-
const devServerScript = config.project_settings?.worktree_dev_server_script;
|
|
8271
|
-
console.log(`[Daemon] EP956: Ensuring dev server is running for ${moduleUid} at ${worktree.path}...`);
|
|
8272
|
-
const devServerResult = await ensureDevServer(worktree.path, port, moduleUid, devServerScript);
|
|
8273
|
-
if (!devServerResult.success) {
|
|
8274
|
-
const errorMsg2 = `Dev server failed to start: ${devServerResult.error}`;
|
|
8275
|
-
console.error(`[Daemon] EP956: ${errorMsg2}`);
|
|
8276
|
-
await reportTunnelStatus({ tunnel_error: errorMsg2 });
|
|
8277
|
-
releasePort(moduleUid);
|
|
8278
|
-
return;
|
|
8279
|
-
}
|
|
8280
|
-
console.log(`[Daemon] EP956: Dev server ready on port ${port}`);
|
|
8281
|
-
let lastError;
|
|
8282
|
-
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
|
|
8283
|
-
console.log(`[Daemon] EP819: Starting tunnel for ${moduleUid} (attempt ${attempt}/${MAX_RETRIES})...`);
|
|
8284
|
-
const startResult = await tunnelManager.startTunnel({
|
|
8285
|
-
moduleUid,
|
|
8286
|
-
port,
|
|
8287
|
-
onUrl: async (url) => {
|
|
8288
|
-
console.log(`[Daemon] EP819: Tunnel URL for ${moduleUid}: ${url}`);
|
|
8289
|
-
await reportTunnelStatus({
|
|
8290
|
-
tunnel_url: url,
|
|
8291
|
-
tunnel_error: null
|
|
8292
|
-
});
|
|
8293
|
-
},
|
|
8294
|
-
onStatusChange: (status, error) => {
|
|
8295
|
-
if (status === "error") {
|
|
8296
|
-
console.error(`[Daemon] EP819: Tunnel error for ${moduleUid}: ${error}`);
|
|
8297
|
-
reportTunnelStatus({ tunnel_error: error || "Tunnel connection error" });
|
|
8298
|
-
} else if (status === "reconnecting") {
|
|
8299
|
-
console.log(`[Daemon] EP819: Tunnel reconnecting for ${moduleUid}...`);
|
|
8300
|
-
}
|
|
8301
|
-
}
|
|
8302
|
-
});
|
|
8303
|
-
if (startResult.success) {
|
|
8304
|
-
console.log(`[Daemon] EP819: Tunnel started successfully for ${moduleUid}`);
|
|
8305
|
-
return;
|
|
8306
|
-
}
|
|
8307
|
-
lastError = startResult.error;
|
|
8308
|
-
console.warn(`[Daemon] EP819: Tunnel start attempt ${attempt} failed: ${lastError}`);
|
|
8309
|
-
if (attempt < MAX_RETRIES) {
|
|
8310
|
-
console.log(`[Daemon] EP819: Retrying in ${RETRY_DELAY_MS}ms...`);
|
|
8311
|
-
await new Promise((resolve3) => setTimeout(resolve3, RETRY_DELAY_MS));
|
|
8312
|
-
}
|
|
8313
|
-
}
|
|
8314
|
-
const errorMsg = `Tunnel failed after ${MAX_RETRIES} attempts: ${lastError}`;
|
|
8315
|
-
console.error(`[Daemon] EP956: ${errorMsg}`);
|
|
8316
|
-
await reportTunnelStatus({ tunnel_error: errorMsg });
|
|
8317
|
-
releasePort(moduleUid);
|
|
8318
|
-
} catch (error) {
|
|
8319
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
8320
|
-
console.error(`[Daemon] EP956: Async tunnel startup error:`, error);
|
|
8321
|
-
await reportTunnelStatus({ tunnel_error: errorMsg });
|
|
8322
|
-
releasePort(moduleUid);
|
|
8323
|
-
}
|
|
8324
|
-
});
|
|
8325
|
-
})();
|
|
8326
|
-
}
|
|
8327
|
-
} catch (error) {
|
|
8328
|
-
console.error(`[Daemon] EP819: Error auto-starting tunnels:`, error);
|
|
8329
|
-
}
|
|
8330
|
-
}
|
|
7981
|
+
// EP1003: startTunnelForModule removed - server now orchestrates via tunnel_start commands
|
|
7982
|
+
// EP1003: autoStartTunnelsForProject removed - server now orchestrates via reconciliation
|
|
7983
|
+
// Recovery flow: daemon sends reconciliation_report → server processes and sends commands
|
|
7984
|
+
// Orphan tunnel cleanup is now also handled server-side via reconciliation report
|
|
8331
7985
|
// EP843: startTunnelPolling() removed - replaced by push-based state sync
|
|
8332
7986
|
// See module_state_changed handler for the new implementation
|
|
8333
7987
|
/**
|
|
@@ -8646,7 +8300,9 @@ var Daemon = class _Daemon {
|
|
|
8646
8300
|
method: "POST",
|
|
8647
8301
|
body: JSON.stringify({
|
|
8648
8302
|
tunnel_url: url,
|
|
8649
|
-
tunnel_error: null
|
|
8303
|
+
tunnel_error: null,
|
|
8304
|
+
restart_reason: "health_check_failure"
|
|
8305
|
+
// EP1003: Server can track restart causes
|
|
8650
8306
|
})
|
|
8651
8307
|
});
|
|
8652
8308
|
} catch (e) {
|