dsh-tg-bridge 1.28.1 → 1.30.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/aset-profile.mjs +9 -2
- package/bridge-kick.mjs +104 -4
- package/bridge.mjs +468 -461
- package/commands-registry.mjs +65 -65
- package/context-meter.mjs +84 -0
- package/control/agents.mjs +80 -55
- package/control/cmd-bridge.mjs +445 -409
- package/control/cmd-views.mjs +12 -0
- package/control/release.mjs +204 -156
- package/control.mjs +123 -123
- package/cost.mjs +179 -0
- package/handle-message.mjs +497 -499
- package/help-explain.mjs +42 -42
- package/i18n/en/core.mjs +5 -1
- package/i18n/en/cost.mjs +17 -0
- package/i18n/en/ctrl.mjs +1 -0
- package/i18n/en/release.mjs +18 -0
- package/i18n/en/sandbox.mjs +10 -0
- package/i18n/en.mjs +11 -9
- package/i18n/ru/core.mjs +5 -1
- package/i18n/ru/cost.mjs +17 -0
- package/i18n/ru/ctrl.mjs +1 -0
- package/i18n/ru/release.mjs +17 -0
- package/i18n/ru/sandbox.mjs +10 -0
- package/i18n/ru.mjs +11 -9
- package/i18n/zh/core.mjs +5 -1
- package/i18n/zh/cost.mjs +17 -0
- package/i18n/zh/ctrl.mjs +1 -0
- package/i18n/zh/release.mjs +17 -0
- package/i18n/zh/sandbox.mjs +10 -0
- package/i18n/zh.mjs +4 -2
- package/install-check.mjs +200 -0
- package/menu-card.mjs +220 -220
- package/notes.mjs +25 -1
- package/package.json +8 -1
- package/panel.mjs +208 -193
- package/price-cache.mjs +57 -0
- package/prices.mjs +23 -9
- package/progress.mjs +401 -353
- package/sandbox-note.mjs +141 -0
- package/stability.mjs +341 -0
- package/thoughts.mjs +262 -244
- package/turn-events.mjs +74 -0
- package/turn.mjs +12 -0
package/aset-profile.mjs
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
import { createHash } from 'node:crypto';
|
|
16
16
|
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
17
17
|
import { homedir } from 'node:os';
|
|
18
|
-
import { join } from 'node:path';
|
|
18
|
+
import { join, resolve, sep } from 'node:path';
|
|
19
19
|
import { setAgentPreset, setModel, setSubagents } from './patch-rows.mjs';
|
|
20
20
|
import { sandboxModeOf, setPermission } from './permission-rows.mjs';
|
|
21
21
|
|
|
@@ -160,10 +160,17 @@ export function pruneDerived(keep = [], { profiles = PROFILES } = {}) {
|
|
|
160
160
|
} catch {
|
|
161
161
|
return removed;
|
|
162
162
|
}
|
|
163
|
+
// A recursive delete needs a containment check, not just a name pattern: the
|
|
164
|
+
// root comes from `DSH_HOME`, and `DSH_HOME=C:\` would put "derived profiles"
|
|
165
|
+
// wherever that machine happens to have a matching directory. The prefix check
|
|
166
|
+
// stays as the second line of defence.
|
|
167
|
+
const root = resolve(profiles);
|
|
163
168
|
for (const entry of entries) {
|
|
164
169
|
if (!entry.isDirectory() || !entry.name.startsWith(DERIVED_PREFIX) || wanted.has(entry.name)) continue;
|
|
170
|
+
const target = resolve(join(profiles, entry.name));
|
|
171
|
+
if (target !== root && !target.startsWith(root.endsWith(sep) ? root : root + sep)) continue;
|
|
165
172
|
try {
|
|
166
|
-
rmSync(
|
|
173
|
+
rmSync(target, { recursive: true, force: true });
|
|
167
174
|
removed.push(entry.name);
|
|
168
175
|
} catch {
|
|
169
176
|
// A profile in use by a running turn stays until the next sweep.
|
package/bridge-kick.mjs
CHANGED
|
@@ -6,6 +6,13 @@
|
|
|
6
6
|
// otherwise it exits immediately. A live pid that refuses to die (EPERM) counts
|
|
7
7
|
// as alive: refusing to start a second poller is what prevents 409 conflicts.
|
|
8
8
|
//
|
|
9
|
+
// It also owns the one failure the bridge cannot report about itself. A release
|
|
10
|
+
// that throws while it loads never runs a turn, so the turn path records nothing
|
|
11
|
+
// and no rollback is ever considered — the tick is the only thing still watching.
|
|
12
|
+
// When the install keeps dying without answering, or the record already says the
|
|
13
|
+
// version on disk is faulted, this puts the last proven release back before it
|
|
14
|
+
// starts the bridge again.
|
|
15
|
+
//
|
|
9
16
|
// Exit codes: 0 = nothing to do or started, 2 = start failed.
|
|
10
17
|
|
|
11
18
|
import { spawn, spawnSync } from 'node:child_process';
|
|
@@ -18,6 +25,12 @@ const DIR = join(DSH_HOME, 'tg-bridge');
|
|
|
18
25
|
const BRIDGE = join(DIR, 'bridge.mjs');
|
|
19
26
|
const PID_FILE = join(DIR, 'bridge.pid');
|
|
20
27
|
const KICK_LOG = join(DIR, 'kick.log');
|
|
28
|
+
const STATE_FILE = join(DIR, 'state.json');
|
|
29
|
+
const STARTS_FILE = join(DIR, 'kick-starts.json');
|
|
30
|
+
|
|
31
|
+
/** How many dead starts, inside this window, mean "this release does not run". */
|
|
32
|
+
const CRASH_WINDOW_MS = 15 * 60 * 1000;
|
|
33
|
+
const CRASH_STARTS = 2;
|
|
21
34
|
|
|
22
35
|
// Shared with setup.mjs: a restart holds this lock, so a tick that lands in the
|
|
23
36
|
// middle of one must not start a second poller.
|
|
@@ -101,12 +114,86 @@ function bridgePids() {
|
|
|
101
114
|
return (r.stdout || '').split(/\r?\n/).map((v) => Number(v.trim())).filter((v) => Number.isInteger(v) && v > 0);
|
|
102
115
|
}
|
|
103
116
|
|
|
104
|
-
|
|
117
|
+
/** The starts this tick has issued, and when. */
|
|
118
|
+
function readStarts() {
|
|
119
|
+
try {
|
|
120
|
+
const raw = JSON.parse(readFileSync(STARTS_FILE, 'utf8'));
|
|
121
|
+
return Array.isArray(raw) ? raw.filter((v) => Number.isFinite(Number(v))) : [];
|
|
122
|
+
} catch {
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function writeStarts(list) {
|
|
128
|
+
try {
|
|
129
|
+
writeFileSync(STARTS_FILE, JSON.stringify(list), 'utf8');
|
|
130
|
+
} catch {}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Did the last starts all end without the bridge ever becoming healthy?
|
|
135
|
+
*
|
|
136
|
+
* A start is "dead" when the pid it produced is gone and no bridge is running,
|
|
137
|
+
* which is exactly the situation this tick is in when it decides to start one.
|
|
138
|
+
* The count is reset by a healthy pid, so a laptop that reboots twice in an hour
|
|
139
|
+
* is not mistaken for a broken release.
|
|
140
|
+
*/
|
|
141
|
+
function crashLoop(now = Date.now()) {
|
|
142
|
+
const recent = readStarts().filter((at) => now - Number(at) <= CRASH_WINDOW_MS);
|
|
143
|
+
writeStarts(recent);
|
|
144
|
+
return recent.length >= CRASH_STARTS;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function readState() {
|
|
148
|
+
try {
|
|
149
|
+
return JSON.parse(readFileSync(STATE_FILE, 'utf8').replace(/^\uFEFF/, ''));
|
|
150
|
+
} catch {
|
|
151
|
+
return {};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Put the last proven release back when the version on disk cannot run.
|
|
157
|
+
*
|
|
158
|
+
* Loaded lazily and inside a try/catch: this tick must never be the reason a
|
|
159
|
+
* working bridge stays down, and `stability.mjs` imports the release module that
|
|
160
|
+
* shells out to npm. If the import fails, the tick starts the bridge as it is.
|
|
161
|
+
*/
|
|
162
|
+
async function maybeRollback(evidence) {
|
|
163
|
+
let target = '';
|
|
164
|
+
try {
|
|
165
|
+
const { startupRollback, applyRollback } = await import('./stability.mjs');
|
|
166
|
+
const state = readState();
|
|
167
|
+
const { bridgeVersion } = await import('./control/release.mjs');
|
|
168
|
+
const installed = bridgeVersion();
|
|
169
|
+
target = startupRollback(state, installed, evidence);
|
|
170
|
+
if (!target) return '';
|
|
171
|
+
log(`release ${installed} has not proven itself (${evidence.crashLoop ? 'crash loop' : 'recorded faults'}): restoring ${target}`);
|
|
172
|
+
const result = await applyRollback(state, { version: target, lang: 'en', t: (_l, key) => key, log });
|
|
173
|
+
// The stamp the next tick reads. It is set even when the restore failed: a
|
|
174
|
+
// supervisor that retries a broken registry every sixty seconds is a log
|
|
175
|
+
// loop, not a recovery.
|
|
176
|
+
const release = state.release && typeof state.release === 'object' ? state.release : (state.release = {});
|
|
177
|
+
release.startupRolledBackAt = Date.now();
|
|
178
|
+
// The state file is the record the next tick reads: write it even when the
|
|
179
|
+
// restore failed, so a repeated failure is visible instead of retried blind.
|
|
180
|
+
try {
|
|
181
|
+
const { writeAtomic } = await import('./state.mjs');
|
|
182
|
+
writeAtomic(STATE_FILE, JSON.stringify(state, null, 2));
|
|
183
|
+
} catch {}
|
|
184
|
+
log(`rollback to ${target}: ${result.ok ? `ok via ${result.via}` : `failed (${(result.problems || []).join(' | ').slice(0, 200)})`}`);
|
|
185
|
+
return result.ok ? target : '';
|
|
186
|
+
} catch (error) {
|
|
187
|
+
log(`rollback skipped: ${String(error?.message ?? error).slice(0, 200)}`);
|
|
188
|
+
return '';
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function main() {
|
|
105
193
|
if (!acquireLock('kick')) {
|
|
106
194
|
log('restart in progress (setup holds the lock), skipping this tick');
|
|
107
195
|
return 0;
|
|
108
|
-
}
|
|
109
|
-
if (!existsSync(BRIDGE)) {
|
|
196
|
+
} if (!existsSync(BRIDGE)) {
|
|
110
197
|
log('bridge.mjs missing');
|
|
111
198
|
return 2;
|
|
112
199
|
}
|
|
@@ -120,6 +207,8 @@ function main() {
|
|
|
120
207
|
// the minute tick costs one kill(0) instead of a PowerShell process scan.
|
|
121
208
|
if (filePid && alive(filePid)) {
|
|
122
209
|
if (restartPending) log(`marker present but pid ${filePid} alive`);
|
|
210
|
+
// A live bridge proves the release can start: forget the crash history.
|
|
211
|
+
writeStarts([]);
|
|
123
212
|
return 0;
|
|
124
213
|
}
|
|
125
214
|
// Only when the pid file is stale or missing: scan for a bridge that started
|
|
@@ -130,6 +219,13 @@ function main() {
|
|
|
130
219
|
return 0;
|
|
131
220
|
}
|
|
132
221
|
|
|
222
|
+
const loop = crashLoop();
|
|
223
|
+
const restored = await maybeRollback({ crashLoop: loop });
|
|
224
|
+
if (restored) {
|
|
225
|
+
// The restored files are on disk; this start is the test of them, and the
|
|
226
|
+
// crash counter starts from zero so one bad release cannot fail twice.
|
|
227
|
+
writeStarts([]);
|
|
228
|
+
}
|
|
133
229
|
log(restartPending ? 'marker present and no bridge: starting' : 'no bridge: starting');
|
|
134
230
|
// spawn(), NOT spawnSync(): spawnSync waits for the child to exit, and this
|
|
135
231
|
// child is meant to outlive the tick.
|
|
@@ -146,8 +242,12 @@ function main() {
|
|
|
146
242
|
return 2;
|
|
147
243
|
}
|
|
148
244
|
child.unref();
|
|
245
|
+
if (!restored) writeStarts([...readStarts(), Date.now()]);
|
|
149
246
|
log(`start issued pid=${child.pid ?? '?'}`);
|
|
150
247
|
return 0;
|
|
151
248
|
}
|
|
152
249
|
|
|
153
|
-
process.exit(
|
|
250
|
+
main().then((code) => process.exit(code), (error) => {
|
|
251
|
+
log(`tick failed: ${String(error?.message ?? error).slice(0, 200)}`);
|
|
252
|
+
process.exit(2);
|
|
253
|
+
});
|