dsh-tg-bridge 1.51.0 → 1.51.1

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,20 @@
1
1
  # CHANGELOG — DSH Telegram bridge
2
2
 
3
+ ## 1.51.1
4
+
5
+ - **Stale turn markers are swept, not left behind.** A marker named a turn that
6
+ was over, and `say.mjs` reads two live markers in one folder as ambiguous and
7
+ refuses to place a line. The old check ran only for the folder that asked, so
8
+ a marker left by a turn in another folder stayed forever; and a marker whose
9
+ pid Windows had handed to some other process looked alive, because the only
10
+ test was `process.kill(pid, 0)`. Eight such markers had piled up in one bot's
11
+ folder before anyone looked. `sweep()` now runs on every marker write and
12
+ every read, drops a marker whose process is gone or whose file is older than
13
+ any turn could be (12 h — the age limit exists for pid reuse, not for long
14
+ turns), and deletes the file it read rather than the file its pid names: the
15
+ two are the same until one of them is wrong, and then a sweep would take a
16
+ live turn with it. 4 tests.
17
+
3
18
  ## 1.51.0
4
19
 
5
20
  - **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.1",
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/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);