dsh-tg-bridge 1.41.2 → 1.42.0

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,28 @@
1
1
  # CHANGELOG — DSH Telegram bridge
2
2
 
3
+ ## 1.42.0
4
+
5
+ - **The workspace group is written by the host that owns it.** 1.39.0 closed
6
+ issue #3 by editing `workspace.json` from the bridge process, with the
7
+ caveat that a running `dsh web` holds that file in memory and can drop the id
8
+ on its next write. The addendum on the issue named the better place: the
9
+ shipped DSH plugin runs inside the host, so it can call
10
+ `workspace.attachSession` itself. `plugin-bridge.mjs` now reconciles once at
11
+ boot and every 30 s: every persisted session that no workspace lists and whose
12
+ folder matches one gets attached through the public API, and a folder that
13
+ matches nothing stays Ungrouped. Subagents and child sessions are never grouped
14
+ on their own, an id already listed is untouched, and a host without
15
+ `workspaceRegistry` / `sessionPersistence` is left quiet rather than broken.
16
+ Seven tests drive `reconcileOnce` with a fake context, so the grouping itself
17
+ is checked without booting a desktop.
18
+
19
+ ## 1.41.2
20
+
21
+ - **An empty note file is removed when its turn ends.** Every turn creates a
22
+ per-turn thought file; a turn that wrote no note left it at 0 bytes, and one
23
+ real folder had 269 of them out of 285. The card is finished before the unlink
24
+ runs, so nothing reads the file afterwards, and a file with a line is kept.
25
+
3
26
  ## 1.41.1
4
27
 
5
28
  - **A note cannot land in General while its turn is elsewhere.** `say.mjs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-tg-bridge",
3
- "version": "1.41.2",
3
+ "version": "1.42.0",
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/plugin-bridge.mjs CHANGED
@@ -103,6 +103,87 @@ export function startBridge({
103
103
  }
104
104
 
105
105
  export function apply(ctx, config = {}) {
106
- if (config.autoStart === false) return { started: false, reason: 'disabled' };
107
- return startBridge({ ctx });
106
+ const started = config.autoStart === false ? { started: false, reason: 'disabled' } : startBridge({ ctx });
107
+ // The membership list is the host's to write. Issue #3 pointed at the right
108
+ // shape: the bridge edits `workspace.json` from a separate process, and a live
109
+ // host holds that file in memory, so its next write can drop the id the bridge
110
+ // just added. This runs inside the host, so it can attach what the headless
111
+ // turns created without racing anyone.
112
+ reconcileSessions(ctx);
113
+ return started;
108
114
  }
115
+
116
+ /** How often an id created by a headless turn is looked for, in ms. */
117
+ const RECONCILE_MS = 30000;
118
+
119
+ /**
120
+ * Attach the sessions nobody grouped, from inside the host that owns the list.
121
+ *
122
+ * `sessionPersistence.list()` re-reads the artifacts on every call, so a session
123
+ * written by the spawned `dsh --profile headless` is visible here even though it
124
+ * was never live in this process. `attachSession` rejects an id whose folder is
125
+ * not the workspace's path, which is the check — a wrong group is worse than
126
+ * Ungrouped, because it claims the session ran somewhere it never did.
127
+ *
128
+ * Exported so a test can drive it with a fake context: booting a desktop to
129
+ * check "does the group follow the folder" would test the desktop, not this.
130
+ *
131
+ * @returns {{ attached: number, skipped: number, reason?: string }}
132
+ */
133
+ export async function reconcileOnce(ctx) {
134
+ const listRaw = ctx?.workspaceRegistry?.list;
135
+ const persistRaw = ctx?.sessionPersistence?.list;
136
+ if (typeof listRaw !== 'function' || typeof persistRaw !== 'function') {
137
+ return { attached: 0, skipped: 0, reason: 'no-services' };
138
+ }
139
+ const spaces = (await listRaw.call(ctx.workspaceRegistry)) ?? [];
140
+ const sessions = (await persistRaw.call(ctx.sessionPersistence)) ?? [];
141
+ const known = new Set(spaces.flatMap((space) => space?.sessionIds ?? []));
142
+ let attached = 0;
143
+ let skipped = 0;
144
+ for (const entry of sessions) {
145
+ const header = entry?.header ?? entry;
146
+ const id = String(header?.id ?? '');
147
+ // Subagents and children belong to their parent's story, not to a group of
148
+ // their own; an id with no folder cannot be matched, so it is left alone.
149
+ if (!id || known.has(id) || !header?.cwd) continue;
150
+ if (header.origin === 'subagent' || header.parentSession !== undefined) {
151
+ skipped += 1;
152
+ continue;
153
+ }
154
+ let done = false;
155
+ for (const space of spaces) {
156
+ if (typeof space?.attachSession !== 'function') continue;
157
+ try {
158
+ await space.attachSession(id);
159
+ done = true;
160
+ break;
161
+ } catch {
162
+ // The folder is not this workspace's: try the next one.
163
+ }
164
+ }
165
+ if (done) {
166
+ attached += 1;
167
+ known.add(id);
168
+ } else {
169
+ skipped += 1;
170
+ }
171
+ }
172
+ return { attached, skipped };
173
+ }
174
+
175
+ /** Run `reconcileOnce` on a timer while the host lives; quiet when it cannot. */
176
+ function reconcileSessions(ctx) {
177
+ if (typeof ctx?.workspaceRegistry?.list !== 'function') return;
178
+ if (typeof ctx?.sessionPersistence?.list !== 'function') return;
179
+ const tick = () => {
180
+ void reconcileOnce(ctx).catch(() => {});
181
+ };
182
+ tick();
183
+ const timer = setInterval(tick, RECONCILE_MS);
184
+ timer?.unref?.();
185
+ if (typeof ctx?.on === 'function') {
186
+ ctx.on('dispose', () => clearInterval(timer));
187
+ }
188
+ }
189
+