Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: cancel paste retries on newer input (#3216)

[._.]/ Adam Eivy42dc46e6e446
diff --git a/server/services/shell.js b/server/services/shell.js
index c3d5d22af3..fdf74a2fbd 100644
--- a/server/services/shell.js
+++ b/server/services/shell.js
@@ -541,6 +541,7 @@ export function writeToSession(sessionId, data) {
const session = shellSessions.get(sessionId);
if (session) {
session.lastInputAt = Date.now();
+ session.inputRevision = (session.inputRevision || 0) + 1;
session.pty.write(data);
return true;
}
@@ -555,8 +556,8 @@ export function writeToSession(sessionId, data) {
* lands as one input event instead of N submits.
*
* The returned interval handle lets lifecycle-aware callers cancel pending
- * submission retries. The helper self-cancels when the session disappears or
- * after its attempt budget is spent.
+ * submission retries. The helper self-cancels when the session disappears,
+ * newer input arrives, or its attempt budget is spent.
*
* @param {string} sessionId
* @param {string} text - paste payload (no trailing newline; the Enter submits it)
@@ -566,16 +567,22 @@ export function writeToSession(sessionId, data) {
*/
export function pasteToSession(sessionId, text, { label = 'paste' } = {}) {
if (!writeToSession(sessionId, `\x1b[200~${text}\x1b[201~`)) return false;
+ const session = shellSessions.get(sessionId);
+ let expectedInputRevision = session.inputRevision;
const writeEnter = () => {
try {
writeToSession(sessionId, '\r');
+ expectedInputRevision = session.inputRevision;
} catch (err) {
// Timer callbacks run outside the request lifecycle, and a write to a
// live-but-broken PTY can throw.
console.error(`🐚 ${label} submit Enter failed for ${sessionId.slice(0, 8)}: ${err.message}`);
}
};
- return scheduleSubmitEnters(writeEnter, () => !shellSessions.has(sessionId));
+ return scheduleSubmitEnters(
+ writeEnter,
+ () => !shellSessions.has(sessionId) || session.inputRevision !== expectedInputRevision,
+ );
}
/**
diff --git a/server/services/shell.test.js b/server/services/shell.test.js
index 00fffedea4..aa365e1380 100644
--- a/server/services/shell.test.js
+++ b/server/services/shell.test.js
@@ -400,6 +400,17 @@ describe('pasteToSession', () => {
expect(pty.write.mock.calls.filter(([data]) => data === '\r')).toHaveLength(1);
});
+ it('cancels pending Enters when newer input arrives', () => {
+ shell.pasteToSession(id, 'hi');
+ expect(pty.write.mock.calls.filter(([data]) => data === '\r')).toHaveLength(1);
+
+ shell.writeToSession(id, 'new input');
+ vi.advanceTimersByTime(1400);
+
+ expect(pty.write.mock.calls.filter(([data]) => data === '\r')).toHaveLength(1);
+ expect(pty.write).toHaveBeenCalledWith('new input');
+ });
+
it('returns false and writes nothing for a missing session', () => {
expect(shell.pasteToSession('missing', 'hi')).toBe(false);
expect(pty.write).not.toHaveBeenCalled();