@phenx-inc/ctlsurf 0.1.7 → 0.1.8

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.
@@ -3648,7 +3648,7 @@ var ConversationBridge = class {
3648
3648
  // Virtual terminal for processing escape sequences into rendered text
3649
3649
  terminal;
3650
3650
  lastSnapshotLines = 0;
3651
- // how many lines we've already sent
3651
+ pendingWrites = 0;
3652
3652
  constructor() {
3653
3653
  this.terminal = new import_headless.Terminal({ cols: 120, rows: 50, scrollback: 1e4 });
3654
3654
  }
@@ -3658,18 +3658,25 @@ var ConversationBridge = class {
3658
3658
  startSession() {
3659
3659
  this.terminal.reset();
3660
3660
  this.lastSnapshotLines = 0;
3661
+ this.pendingWrites = 0;
3661
3662
  this.inputBuffer = "";
3662
3663
  this.sessionActive = true;
3663
3664
  console.log("[bridge] Session started");
3664
3665
  }
3665
3666
  /**
3666
3667
  * Feed terminal output data into the bridge.
3667
- * The headless terminal processes all escape sequences (cursor moves,
3668
- * line clears, color codes) so the buffer contains clean rendered text.
3668
+ * xterm.write() is async we track pending writes and only flush
3669
+ * when all writes have been processed into the buffer.
3669
3670
  */
3670
3671
  feedOutput(data) {
3671
3672
  if (!this.sessionActive) return;
3672
- this.terminal.write(data);
3673
+ this.pendingWrites++;
3674
+ this.terminal.write(data, () => {
3675
+ this.pendingWrites--;
3676
+ this.scheduleFlush();
3677
+ });
3678
+ }
3679
+ scheduleFlush() {
3673
3680
  if (this.flushTimer) {
3674
3681
  clearTimeout(this.flushTimer);
3675
3682
  }
@@ -3686,16 +3693,18 @@ var ConversationBridge = class {
3686
3693
  this.inputBuffer = "";
3687
3694
  }
3688
3695
  }
3689
- /**
3690
- * Resize the virtual terminal to match the actual PTY dimensions.
3691
- */
3692
3696
  resize(cols, rows) {
3693
3697
  this.terminal.resize(cols, rows);
3694
3698
  }
3695
3699
  /**
3696
3700
  * Read new content from the terminal buffer since the last flush.
3701
+ * Only flushes when all pending writes have completed.
3697
3702
  */
3698
3703
  flush() {
3704
+ if (this.pendingWrites > 0) {
3705
+ this.scheduleFlush();
3706
+ return;
3707
+ }
3699
3708
  const buf = this.terminal.buffer.active;
3700
3709
  const totalLines = buf.baseY + buf.cursorY + 1;
3701
3710
  if (totalLines <= this.lastSnapshotLines) return;
@@ -3720,8 +3729,12 @@ var ConversationBridge = class {
3720
3729
  content
3721
3730
  });
3722
3731
  }
3723
- endSession() {
3732
+ async endSession() {
3724
3733
  if (!this.sessionActive) return;
3734
+ if (this.pendingWrites > 0) {
3735
+ await new Promise((resolve) => setTimeout(resolve, 500));
3736
+ }
3737
+ this.pendingWrites = 0;
3725
3738
  this.flush();
3726
3739
  if (this.flushTimer) {
3727
3740
  clearTimeout(this.flushTimer);
@@ -4223,7 +4236,7 @@ var Orchestrator = class {
4223
4236
  // ─── PTY & Agent ────────────────────────────────
4224
4237
  async spawnAgent(agent, cwd) {
4225
4238
  if (this.ptyManager) {
4226
- this.bridge.endSession();
4239
+ await this.bridge.endSession();
4227
4240
  this.ptyManager.kill();
4228
4241
  }
4229
4242
  this.currentAgent = agent;
@@ -4239,9 +4252,9 @@ var Orchestrator = class {
4239
4252
  this.streamTerminalData(data);
4240
4253
  });
4241
4254
  const thisPtyManager = this.ptyManager;
4242
- this.ptyManager.onExit((exitCode) => {
4255
+ this.ptyManager.onExit(async (exitCode) => {
4243
4256
  this.events.onPtyExit(exitCode);
4244
- this.bridge.endSession();
4257
+ await this.bridge.endSession();
4245
4258
  if (thisPtyManager === this.ptyManager && this.currentAgent && isCodingAgent(this.currentAgent)) {
4246
4259
  this.workerWs.disconnect();
4247
4260
  }
@@ -4264,7 +4277,7 @@ var Orchestrator = class {
4264
4277
  this.workerWs.sendTerminalResize(cols, rows);
4265
4278
  }
4266
4279
  async killAgent() {
4267
- this.bridge.endSession();
4280
+ await this.bridge.endSession();
4268
4281
  this.ptyManager?.kill();
4269
4282
  this.ptyManager = null;
4270
4283
  if (this.currentAgent && isCodingAgent(this.currentAgent)) {
@@ -4313,7 +4326,7 @@ var Orchestrator = class {
4313
4326
  }
4314
4327
  // ─── Shutdown ───────────────────────────────────
4315
4328
  async shutdown() {
4316
- this.bridge.endSession();
4329
+ await this.bridge.endSession();
4317
4330
  this.ptyManager?.kill();
4318
4331
  this.ptyManager = null;
4319
4332
  this.workerWs.disconnect();