dsh-tg-bridge 1.51.0 → 1.51.2

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/docs/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # CHANGELOG — DSH Telegram bridge
2
2
 
3
+ ## 1.51.2
4
+
5
+ - **`say.mjs` sweeps stale markers too.** 1.51.1 taught the bridge to sweep, but
6
+ `say.mjs` runs as its own process and reads the marker folder itself: it does
7
+ not import `turn/active.mjs`, so the sweep never ran on the path that suffers
8
+ from a stale marker — two live markers in one folder is a refusal to place a
9
+ line, and a marker from yesterday is not live. The same rule now lives there,
10
+ with the same 12 h age limit for pid reuse, and it deletes the file it read.
11
+ 1 test.
12
+
13
+ ## 1.51.1
14
+
15
+ - **Stale turn markers are swept, not left behind.** A marker named a turn that
16
+ was over, and `say.mjs` reads two live markers in one folder as ambiguous and
17
+ refuses to place a line. The old check ran only for the folder that asked, so
18
+ a marker left by a turn in another folder stayed forever; and a marker whose
19
+ pid Windows had handed to some other process looked alive, because the only
20
+ test was `process.kill(pid, 0)`. Eight such markers had piled up in one bot's
21
+ folder before anyone looked. `sweep()` now runs on every marker write and
22
+ every read, drops a marker whose process is gone or whose file is older than
23
+ any turn could be (12 h — the age limit exists for pid reuse, not for long
24
+ turns), and deletes the file it read rather than the file its pid names: the
25
+ two are the same until one of them is wrong, and then a sweep would take a
26
+ live turn with it. 4 tests.
27
+
3
28
  ## 1.51.0
4
29
 
5
30
  - **A second instance keeps its own 💭 lines, markers and steer files.**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-tg-bridge",
3
- "version": "1.51.0",
3
+ "version": "1.51.2",
4
4
  "description": "Drive DeepSeek Harness from Telegram: one project, model and session per forum topic",
5
5
  "type": "module",
6
6
  "main": "plugin-bridge.mjs",
package/say.mjs CHANGED
@@ -19,7 +19,7 @@
19
19
  //
20
20
  // With `--steer` the chat/thread come from the job file and an ack marker is
21
21
  // written, which suppresses the bridge's fallback line.
22
- import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
22
+ import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';
23
23
  import { homedir } from 'node:os';
24
24
  import { join } from 'node:path';
25
25
 
@@ -106,9 +106,17 @@ function activeMarker() {
106
106
  if (!name.endsWith('.json')) continue;
107
107
  try {
108
108
  const record = JSON.parse(readFileSync(join(dir, name), 'utf8'));
109
+ // Swept on read, like the bridge does: this file runs after the turn and
110
+ // after the process that wrote the marker may be gone. Two live markers
111
+ // in one folder is a refusal, and a marker from yesterday is not live.
112
+ if (stale(record)) {
113
+ try {
114
+ unlinkSync(join(dir, name));
115
+ } catch {}
116
+ continue;
117
+ }
109
118
  if (!record?.chatId) continue;
110
119
  if (String(record.cwd || '').toLowerCase() !== want) continue;
111
- if (!alive(record.pid)) continue;
112
120
  hits.push(record);
113
121
  } catch {}
114
122
  }
@@ -136,6 +144,22 @@ function alive(pid) {
136
144
  }
137
145
  }
138
146
 
147
+ /**
148
+ * Is this marker from a turn that is over?
149
+ *
150
+ * Same rule as `turn/active.mjs`, and it has to be here too: this file runs as
151
+ * its own process and does not import the bridge's marker code. The age limit
152
+ * exists for pid reuse — Windows hands the same number out again, and a marker
153
+ * from yesterday then looks alive because a browser owns that pid.
154
+ */
155
+ const MAX_TURN_MS = 12 * 60 * 60 * 1000;
156
+
157
+ function stale(record) {
158
+ const at = Number(record?.at || 0);
159
+ if (at > 0 && Date.now() - at > MAX_TURN_MS) return true;
160
+ return !alive(record?.pid);
161
+ }
162
+
139
163
  /**
140
164
  * Send one plain chat line and report the message id, or null.
141
165
  *
package/turn/active.mjs CHANGED
@@ -35,6 +35,7 @@ export function activeDir() {
35
35
  export function markActive({ pid, key, chatId, threadId, cwd, thought }) {
36
36
  try {
37
37
  mkdirSync(activeDir(), { recursive: true });
38
+ sweep();
38
39
  const path = join(activeDir(), `${pid}.json`);
39
40
  const record = {
40
41
  pid,
@@ -62,12 +63,14 @@ export function clearActive(pid) {
62
63
  /**
63
64
  * Live markers for one folder, ignoring files whose process is gone.
64
65
  *
65
- * Staleness is judged by the pid, not by age: a turn that runs for an hour is
66
- * normal, and a killed bridge leaves a marker nobody will ever clear.
66
+ * The whole folder is swept first, not just the entries this folder would use:
67
+ * a marker from another folder is still a marker nobody clears. Eight of them
68
+ * had piled up in one bot's folder before anyone looked.
67
69
  */
68
70
  export function activeTargets(cwd) {
69
71
  const want = String(cwd || '').trim().toLowerCase();
70
72
  if (!want) return [];
73
+ sweep();
71
74
  let names = [];
72
75
  try {
73
76
  names = readdirSync(activeDir()).filter((name) => name.endsWith('.json'));
@@ -80,16 +83,53 @@ export function activeTargets(cwd) {
80
83
  const record = JSON.parse(readFileSync(join(activeDir(), name), 'utf8'));
81
84
  if (!record?.chatId) continue;
82
85
  if (String(record.cwd || '').trim().toLowerCase() !== want) continue;
83
- if (!alive(record.pid)) {
84
- clearActive(record.pid);
85
- continue;
86
- }
87
86
  found.push(record);
88
87
  } catch {}
89
88
  }
90
89
  return found;
91
90
  }
92
91
 
92
+ /**
93
+ * Drop every marker whose turn is over. A handful of small files, so: on read.
94
+ *
95
+ * Deletes the file it read, not the file its pid names: the two are the same
96
+ * until one of them is wrong, and then the sweep would take a live turn with it.
97
+ */
98
+ export function sweep() {
99
+ let names = [];
100
+ try {
101
+ names = readdirSync(activeDir()).filter((name) => name.endsWith('.json'));
102
+ } catch {
103
+ return;
104
+ }
105
+ for (const name of names) {
106
+ let record = null;
107
+ try {
108
+ record = JSON.parse(readFileSync(join(activeDir(), name), 'utf8'));
109
+ } catch {}
110
+ if (record && !stale(record)) continue;
111
+ try {
112
+ rmSync(join(activeDir(), name), { force: true });
113
+ } catch {}
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Is this marker from a turn that is over?
119
+ *
120
+ * Two ways to be over: the process is gone, or the file is older than any turn
121
+ * could be. The age limit is not about long turns — it is about pid reuse.
122
+ * Windows hands the same number out again, and a marker from yesterday then
123
+ * looks alive because a browser now owns that pid.
124
+ */
125
+ const MAX_TURN_MS = 12 * 60 * 60 * 1000;
126
+
127
+ function stale(record) {
128
+ const at = Number(record?.at || 0);
129
+ if (at > 0 && Date.now() - at > MAX_TURN_MS) return true;
130
+ return !alive(record?.pid);
131
+ }
132
+
93
133
  /** Is that process still there? A shell cannot see a child it did not start. */
94
134
  function alive(pid) {
95
135
  const id = Number(pid);