@adhdev/daemon-standalone 0.9.11 → 0.9.13

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.
package/dist/index.js CHANGED
@@ -31263,6 +31263,29 @@ var require_dist2 = __commonJS({
31263
31263
  }
31264
31264
  await this.sendMessage(promptText);
31265
31265
  }
31266
+ async writeToPty(data) {
31267
+ if (!this.ptyProcess) throw new Error(`${this.cliName} is not running`);
31268
+ await this.ptyProcess.write(data);
31269
+ }
31270
+ resetPendingSendState(reason) {
31271
+ this.isWaitingForResponse = false;
31272
+ this.responseBuffer = "";
31273
+ this.currentTurnScope = null;
31274
+ this.submitPendingUntil = 0;
31275
+ this.clearIdleFinishCandidate(reason);
31276
+ if (this.responseTimeout) {
31277
+ clearTimeout(this.responseTimeout);
31278
+ this.responseTimeout = null;
31279
+ }
31280
+ if (this.submitRetryTimer) {
31281
+ clearTimeout(this.submitRetryTimer);
31282
+ this.submitRetryTimer = null;
31283
+ }
31284
+ if (this.finishRetryTimer) {
31285
+ clearTimeout(this.finishRetryTimer);
31286
+ this.finishRetryTimer = null;
31287
+ }
31288
+ }
31266
31289
  async sendMessage(text) {
31267
31290
  if (!this.ptyProcess) throw new Error(`${this.cliName} is not running`);
31268
31291
  const allowInputDuringGeneration = this.provider.allowInputDuringGeneration === true;
@@ -31357,19 +31380,29 @@ var require_dist2 = __commonJS({
31357
31380
  if (this.isWaitingForResponse) this.finishResponse();
31358
31381
  }, this.timeouts.maxResponse);
31359
31382
  };
31360
- await new Promise((resolve12) => {
31383
+ await new Promise((resolve12, reject) => {
31361
31384
  let resolved = false;
31362
31385
  const resolveOnce = () => {
31363
31386
  if (resolved) return;
31364
31387
  resolved = true;
31365
31388
  resolve12();
31366
31389
  };
31390
+ const rejectOnce = (error48) => {
31391
+ if (resolved) return;
31392
+ this.resetPendingSendState("send_write_failed");
31393
+ resolved = true;
31394
+ reject(error48);
31395
+ };
31396
+ const writeRetryKey = (mode) => {
31397
+ void this.writeToPty(this.sendKey).catch((error48) => {
31398
+ LOG2.warn("CLI", `[${this.cliType}] ${mode} write failed: ${error48?.message || error48}`);
31399
+ });
31400
+ };
31367
31401
  const submit = () => {
31368
31402
  if (!this.ptyProcess) {
31369
31403
  resolveOnce();
31370
31404
  return;
31371
31405
  }
31372
- commitUserTurn();
31373
31406
  this.submitPendingUntil = 0;
31374
31407
  const screenText = this.terminalScreen.getText();
31375
31408
  this.recordTrace("submit_write", {
@@ -31377,7 +31410,6 @@ var require_dist2 = __commonJS({
31377
31410
  sendKey: this.sendKey,
31378
31411
  screenText: summarizeCliTraceText(screenText, 500)
31379
31412
  });
31380
- this.ptyProcess.write(this.sendKey);
31381
31413
  const retrySubmitIfStuck = (attempt) => {
31382
31414
  this.submitRetryTimer = null;
31383
31415
  if (!this.ptyProcess || !this.isWaitingForResponse || this.submitRetryUsed) return;
@@ -31397,19 +31429,21 @@ var require_dist2 = __commonJS({
31397
31429
  sendKey: this.sendKey,
31398
31430
  screenText: summarizeCliTraceText(screenText2, 500)
31399
31431
  });
31400
- this.ptyProcess.write(this.sendKey);
31432
+ writeRetryKey("submit_retry");
31401
31433
  if (attempt >= 3) {
31402
31434
  this.submitRetryUsed = true;
31403
31435
  return;
31404
31436
  }
31405
31437
  this.submitRetryTimer = setTimeout(() => retrySubmitIfStuck(attempt + 1), retryDelayMs);
31406
31438
  };
31407
- this.submitRetryTimer = setTimeout(() => retrySubmitIfStuck(1), retryDelayMs);
31408
- startResponseTimeout();
31409
- resolveOnce();
31439
+ void this.writeToPty(this.sendKey).then(() => {
31440
+ commitUserTurn();
31441
+ this.submitRetryTimer = setTimeout(() => retrySubmitIfStuck(1), retryDelayMs);
31442
+ startResponseTimeout();
31443
+ resolveOnce();
31444
+ }, rejectOnce);
31410
31445
  };
31411
31446
  if (this.submitStrategy === "immediate") {
31412
- commitUserTurn();
31413
31447
  this.submitPendingUntil = 0;
31414
31448
  this.recordTrace("submit_write", {
31415
31449
  mode: "immediate",
@@ -31417,37 +31451,38 @@ var require_dist2 = __commonJS({
31417
31451
  sendKey: this.sendKey,
31418
31452
  screenText: summarizeCliTraceText(this.terminalScreen.getText(), 500)
31419
31453
  });
31420
- this.ptyProcess.write(text + this.sendKey);
31421
- this.submitRetryTimer = setTimeout(() => {
31422
- this.submitRetryTimer = null;
31423
- if (!this.ptyProcess || !this.isWaitingForResponse || this.submitRetryUsed) return;
31424
- if (this.currentStatus === "waiting_approval") return;
31425
- if (this.hasMeaningfulResponseBuffer(normalizedPromptSnippet)) return;
31426
- const screenText = this.terminalScreen.getText();
31427
- if (!promptLikelyVisible(screenText, normalizedPromptSnippet)) return;
31428
- const liveApproval = this.runParseApproval(screenText) || this.runParseApproval(this.recentOutputBuffer);
31429
- if (liveApproval) return;
31430
- const liveStatus = this.runDetectStatus(screenText) || this.runDetectStatus(this.recentOutputBuffer);
31431
- if (liveStatus === "generating" || liveStatus === "waiting_approval") return;
31432
- LOG2.info("CLI", `[${this.cliType}] Retrying submit key for stuck prompt (attempt 1)`);
31433
- this.responseSettleIgnoreUntil = Date.now() + this.timeouts.outputSettle + 400;
31434
- this.recordTrace("submit_write", {
31435
- mode: "immediate_retry",
31436
- attempt: 1,
31437
- sendKey: this.sendKey,
31438
- screenText: summarizeCliTraceText(screenText, 500)
31439
- });
31440
- this.ptyProcess.write(this.sendKey);
31441
- this.submitRetryUsed = true;
31442
- }, retryDelayMs);
31443
- startResponseTimeout();
31444
- resolveOnce();
31454
+ void this.writeToPty(text + this.sendKey).then(() => {
31455
+ commitUserTurn();
31456
+ this.submitRetryTimer = setTimeout(() => {
31457
+ this.submitRetryTimer = null;
31458
+ if (!this.ptyProcess || !this.isWaitingForResponse || this.submitRetryUsed) return;
31459
+ if (this.currentStatus === "waiting_approval") return;
31460
+ if (this.hasMeaningfulResponseBuffer(normalizedPromptSnippet)) return;
31461
+ const screenText = this.terminalScreen.getText();
31462
+ if (!promptLikelyVisible(screenText, normalizedPromptSnippet)) return;
31463
+ const liveApproval = this.runParseApproval(screenText) || this.runParseApproval(this.recentOutputBuffer);
31464
+ if (liveApproval) return;
31465
+ const liveStatus = this.runDetectStatus(screenText) || this.runDetectStatus(this.recentOutputBuffer);
31466
+ if (liveStatus === "generating" || liveStatus === "waiting_approval") return;
31467
+ LOG2.info("CLI", `[${this.cliType}] Retrying submit key for stuck prompt (attempt 1)`);
31468
+ this.responseSettleIgnoreUntil = Date.now() + this.timeouts.outputSettle + 400;
31469
+ this.recordTrace("submit_write", {
31470
+ mode: "immediate_retry",
31471
+ attempt: 1,
31472
+ sendKey: this.sendKey,
31473
+ screenText: summarizeCliTraceText(screenText, 500)
31474
+ });
31475
+ writeRetryKey("immediate_retry");
31476
+ this.submitRetryUsed = true;
31477
+ }, retryDelayMs);
31478
+ startResponseTimeout();
31479
+ resolveOnce();
31480
+ }, rejectOnce);
31445
31481
  return;
31446
31482
  }
31447
31483
  if (submitDelayMs > 0) {
31448
31484
  this.submitPendingUntil = Date.now() + submitDelayMs;
31449
31485
  }
31450
- this.ptyProcess.write(text);
31451
31486
  this.recordTrace("submit_write", {
31452
31487
  mode: "type_then_submit",
31453
31488
  text: summarizeCliTraceText(text, 500),
@@ -31484,7 +31519,7 @@ var require_dist2 = __commonJS({
31484
31519
  }
31485
31520
  setTimeout(waitForEchoAndSubmit, 50);
31486
31521
  };
31487
- waitForEchoAndSubmit();
31522
+ void this.writeToPty(text).then(() => waitForEchoAndSubmit(), rejectOnce);
31488
31523
  });
31489
31524
  }
31490
31525
  getPartialResponse() {
@@ -31639,12 +31674,12 @@ var require_dist2 = __commonJS({
31639
31674
  isReady() {
31640
31675
  return this.ready;
31641
31676
  }
31642
- writeRaw(data) {
31677
+ async writeRaw(data) {
31643
31678
  this.recordTrace("write_raw", {
31644
31679
  keys: JSON.stringify(data),
31645
31680
  length: data.length
31646
31681
  });
31647
- this.ptyProcess?.write(data);
31682
+ await this.writeToPty(data);
31648
31683
  }
31649
31684
  resolveModal(buttonIndex) {
31650
31685
  let modal = this.activeModal || this.runParseApproval(this.recentOutputBuffer);
@@ -39552,14 +39587,14 @@ ${effect.notification.body || ""}`.trim();
39552
39587
  success: revealState.visible || focusState.focused
39553
39588
  };
39554
39589
  }
39555
- function handlePtyInput(h, args) {
39590
+ async function handlePtyInput(h, args) {
39556
39591
  const { cliType, data, targetSessionId } = args || {};
39557
39592
  if (!data) return { success: false, error: "data required" };
39558
39593
  const adapter = h.getCliAdapter(targetSessionId || cliType);
39559
39594
  if (!adapter || typeof adapter.writeRaw !== "function") {
39560
39595
  return { success: false, error: `CLI adapter not found: ${targetSessionId || cliType || "unknown"}` };
39561
39596
  }
39562
- adapter.writeRaw(data);
39597
+ await adapter.writeRaw(data);
39563
39598
  return { success: true };
39564
39599
  }
39565
39600
  function handlePtyResize(_h, args) {
@@ -39736,7 +39771,7 @@ ${effect.notification.body || ""}`.trim();
39736
39771
  if (cliCommand?.type === "send_message" && cliCommand.text) {
39737
39772
  await adapter.sendMessage(cliCommand.text);
39738
39773
  } else if (cliCommand?.type === "pty_write" && cliCommand.text && adapter.writeRaw) {
39739
- adapter.writeRaw(cliCommand.text + "\r");
39774
+ await adapter.writeRaw(cliCommand.text + "\r");
39740
39775
  }
39741
39776
  applyProviderPatch(h, args, parsed.payload);
39742
39777
  return {
@@ -40876,7 +40911,7 @@ ${effect.notification.body || ""}`.trim();
40876
40911
  if (cliCommand?.type === "send_message" && cliCommand.text) {
40877
40912
  await this.adapter.sendMessage(cliCommand.text);
40878
40913
  } else if (cliCommand?.type === "pty_write" && cliCommand.text) {
40879
- this.adapter.writeRaw(cliCommand.text + "\r");
40914
+ await this.adapter.writeRaw(cliCommand.text + "\r");
40880
40915
  }
40881
40916
  this.applyProviderResponse(parsed.payload, { phase: "immediate" });
40882
40917
  }
@@ -50561,7 +50596,7 @@ async (params) => {
50561
50596
  }
50562
50597
  try {
50563
50598
  if (typeof adapter.writeRaw === "function") {
50564
- adapter.writeRaw(keys);
50599
+ await adapter.writeRaw(keys);
50565
50600
  ctx.json(res, 200, { sent: true, type: target.type, instanceId: target.instanceId, keysLength: keys.length });
50566
50601
  } else {
50567
50602
  ctx.json(res, 400, { error: "writeRaw not available on this adapter" });
@@ -53480,7 +53515,7 @@ data: ${JSON.stringify(msg.data)}
53480
53515
  this.exitCallbacks.add(callback);
53481
53516
  }
53482
53517
  write(data) {
53483
- this.enqueue(async () => {
53518
+ return this.enqueue(async () => {
53484
53519
  let response = await this.client.request({
53485
53520
  type: "send_input",
53486
53521
  payload: {
@@ -53782,9 +53817,11 @@ data: ${JSON.stringify(msg.data)}
53782
53817
  };
53783
53818
  }
53784
53819
  enqueue(action) {
53785
- this.operationChain = this.operationChain.then(() => this.ready).then(action).catch((error48) => {
53820
+ const operation = this.operationChain.then(() => this.ready).then(action);
53821
+ this.operationChain = operation.catch((error48) => {
53786
53822
  LOG2.warn("CLI", `[session-host:${this.options.runtimeId}] ${error48?.message || error48}`);
53787
53823
  });
53824
+ return operation;
53788
53825
  }
53789
53826
  async closeClient(destroy = false) {
53790
53827
  if (this.closed) return;