somora 2026.1001.2 → 2026.1001.3
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/README.md +6 -0
- package/config.example.yaml +10 -0
- package/docs/api.md +13 -5
- package/docs/setup.md +48 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/src/cli/somora.ts +56 -0
- package/src/config/reload.ts +1 -0
- package/src/config/types.ts +16 -0
- package/src/server/index.ts +20 -1
- package/src/server/update-check.ts +202 -0
- package/web/dist/assets/index-BA5mmiV0.css +32 -0
- package/web/dist/assets/{index-BaVv6pB2.js → index-CrkkIfd9.js} +42 -42
- package/web/dist/assets/index-CrkkIfd9.js.map +1 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-BaVv6pB2.js.map +0 -1
- package/web/dist/assets/index-V3FV3Z25.css +0 -32
package/README.md
CHANGED
|
@@ -164,6 +164,12 @@ recall; **Tailscale** for HTTPS to the web and mobile clients
|
|
|
164
164
|
clipboard APIs); **ripgrep** for `file_search`; **Chromium or Chrome** for
|
|
165
165
|
the shared browser.
|
|
166
166
|
|
|
167
|
+
somora makes one request of its own: once a day it asks somora.ai
|
|
168
|
+
whether a newer version exists (version, OS, Node version and CPU in the
|
|
169
|
+
User-Agent, nothing else; somora.ai counts installations from its
|
|
170
|
+
server log). `DO_NOT_TRACK=1` or `updateCheck.enabled: false` switches it
|
|
171
|
+
off — [docs/setup.md → The daily update check](docs/setup.md#the-daily-update-check--what-somoraai-sees).
|
|
172
|
+
|
|
167
173
|
[docs/setup.md](docs/setup.md) is the full install walkthrough,
|
|
168
174
|
[docs/models.md](docs/models.md) lists the models known to run with somora
|
|
169
175
|
and their tested config blocks.
|
package/config.example.yaml
CHANGED
|
@@ -723,6 +723,16 @@ providers:
|
|
|
723
723
|
# # lifted for questions about a month / what changed / when
|
|
724
724
|
# pageSupport: 0.3 # a page matching in several sections gains support (0 = off)
|
|
725
725
|
|
|
726
|
+
# The daily update check — the one request somora makes on its own:
|
|
727
|
+
# once a day GET https://somora.ai/api/latest-version with the version,
|
|
728
|
+
# OS, Node version and CPU in the User-Agent, nothing else. somora.ai
|
|
729
|
+
# logs the request with its IP address and counts installations from
|
|
730
|
+
# that (docs/setup.md → "The daily update check"). DO_NOT_TRACK=1 or a
|
|
731
|
+
# CI variable in the environment switch it off as well.
|
|
732
|
+
# updateCheck:
|
|
733
|
+
# enabled: true
|
|
734
|
+
# endpoint: https://somora.ai/api/latest-version # a mirror of your own
|
|
735
|
+
|
|
726
736
|
# Obsidian vault + wiki layer (docs/wiki.md, docs/dream-phases.md).
|
|
727
737
|
# The vault is read as a recall source for every agent; the wiki is a
|
|
728
738
|
# subfolder of it that Deep writes (memory → wiki consolidation) and
|
package/docs/api.md
CHANGED
|
@@ -62,9 +62,9 @@ it on Tailscale or on `127.0.0.1` and tunnel.
|
|
|
62
62
|
|
|
63
63
|
## Versioning & stability
|
|
64
64
|
|
|
65
|
-
- The version returned by `GET /version` is
|
|
66
|
-
`2026.
|
|
67
|
-
|
|
65
|
+
- The version returned by `GET /version` is calendar-based and valid
|
|
66
|
+
semver: `2026.1001.2` is the second build of 1 October 2026. Several
|
|
67
|
+
builds a day are common during active work.
|
|
68
68
|
- Most endpoints listed here are stable — the TUI and web both use
|
|
69
69
|
them, and breaking them would break the shipped clients.
|
|
70
70
|
- Endpoints marked **⚠ experimental** may change shape in any bump.
|
|
@@ -78,13 +78,21 @@ it on Tailscale or on `127.0.0.1` and tunnel.
|
|
|
78
78
|
|
|
79
79
|
### `GET /version`
|
|
80
80
|
|
|
81
|
-
Returns the running somora version
|
|
81
|
+
Returns the running somora version and, once the daily update check has
|
|
82
|
+
answered, what somora.ai says is current ([setup.md → The daily update
|
|
83
|
+
check](setup.md#the-daily-update-check--what-somoraai-sees)).
|
|
82
84
|
|
|
83
85
|
```bash
|
|
84
86
|
curl https://<host>:18737/version
|
|
85
|
-
# { "version": "2026.
|
|
87
|
+
# { "version": "2026.1001.2", "update": null }
|
|
88
|
+
# { "version": "2026.1001.2",
|
|
89
|
+
# "update": { "latestVersion": "2026.1005.1", "available": true, "note": "update Node first" } }
|
|
86
90
|
```
|
|
87
91
|
|
|
92
|
+
`update` is `null` until the first check has run (or when it is switched
|
|
93
|
+
off); `available` is true when `latestVersion` is newer than `version`;
|
|
94
|
+
`note` is optional.
|
|
95
|
+
|
|
88
96
|
### `GET /healthz`
|
|
89
97
|
|
|
90
98
|
Liveness probe. Returns the plain-text `ok` with status 200. Use it
|
package/docs/setup.md
CHANGED
|
@@ -916,6 +916,54 @@ auto-cleanup entirely (manual `sentinel delete` only). Recurring
|
|
|
916
916
|
triggers don't auto-GC — they end up in `paused` or `error` and you
|
|
917
917
|
choose when to remove them.
|
|
918
918
|
|
|
919
|
+
## The daily update check — what somora.ai sees
|
|
920
|
+
|
|
921
|
+
somora runs on your machine and talks to the model providers you
|
|
922
|
+
configured. There is exactly one request it makes on its own: once a
|
|
923
|
+
day the server asks somora.ai whether a newer version exists, and the
|
|
924
|
+
answer shows up as an update notice in the web client's taskbar, in
|
|
925
|
+
`somora server status` and in the server log (`update.available`).
|
|
926
|
+
`somora update` itself keeps using npm.
|
|
927
|
+
|
|
928
|
+
```http
|
|
929
|
+
GET https://somora.ai/api/latest-version
|
|
930
|
+
User-Agent: somora/2026.1001.2 (linux; node/22.23.2; x64; server)
|
|
931
|
+
```
|
|
932
|
+
|
|
933
|
+
That is the whole request: no body, no install identifier, no machine
|
|
934
|
+
id, no random token — the version, operating system, Node.js version,
|
|
935
|
+
CPU and whether the server or the CLI asked. The answer is
|
|
936
|
+
`{"version": "…", "note": "…"}`; the note is an optional sentence from
|
|
937
|
+
us to everyone ("update Node first"), at most 500 characters.
|
|
938
|
+
|
|
939
|
+
**What we do with it.** Like any web server, somora.ai logs the request
|
|
940
|
+
together with its IP address and the approximate location Cloudflare
|
|
941
|
+
derives from it (country, region, city). From those logs we count how
|
|
942
|
+
many installations ask — per day, per version, per operating system —
|
|
943
|
+
to know whether the project is used and what to test on. The raw logs
|
|
944
|
+
are deleted after 90 days, daily totals are kept, nothing is published
|
|
945
|
+
and nothing is passed on. Counting by IP address is approximate by
|
|
946
|
+
nature: two instances behind one router look alike, and many home
|
|
947
|
+
connections change their address.
|
|
948
|
+
|
|
949
|
+
**Switching it off.** Any of these stops the request entirely:
|
|
950
|
+
|
|
951
|
+
- `DO_NOT_TRACK=1` in the service's environment (the common
|
|
952
|
+
convention), or
|
|
953
|
+
- `updateCheck.enabled: false` in `config.yaml`, or
|
|
954
|
+
- a `CI` variable in the environment — a test pipeline is not an
|
|
955
|
+
installation.
|
|
956
|
+
|
|
957
|
+
`somora telemetry show` prints the request as it would be sent, why the
|
|
958
|
+
check is on or off, and when it last ran. `updateCheck.endpoint` points
|
|
959
|
+
the check at a mirror of your own. The last answer lives in
|
|
960
|
+
`~/.somora/update-check.json`.
|
|
961
|
+
|
|
962
|
+
The first check runs one to six minutes after the server starts (a
|
|
963
|
+
random delay, so a fleet restarting together does not knock in unison);
|
|
964
|
+
after a success the next one is a day later, after a failure an hour
|
|
965
|
+
later. A failed check never affects anything else.
|
|
966
|
+
|
|
919
967
|
## HTTPS (Tailscale) — required for the web client at scale
|
|
920
968
|
|
|
921
969
|
The web client opens **one persistent SSE connection per chat window**.
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "somora",
|
|
3
|
-
"version": "2026.1001.
|
|
3
|
+
"version": "2026.1001.3",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "somora",
|
|
9
|
-
"version": "2026.1001.
|
|
9
|
+
"version": "2026.1001.3",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@anthropic-ai/claude-agent-sdk": "^0.3.152",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "somora",
|
|
3
|
-
"version": "2026.1001.
|
|
3
|
+
"version": "2026.1001.3",
|
|
4
4
|
"description": "A self-hosted multi-agent harness: your agents, your models, one server — with memory, a shared wiki, a web desktop, a phone app and a terminal client.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/cli/somora.ts
CHANGED
|
@@ -24,6 +24,8 @@ import { fileURLToPath } from 'node:url';
|
|
|
24
24
|
import { SOMORA_VERSION } from '../version.ts';
|
|
25
25
|
import { buildSystemdUnit, extractCustomEnvLines, nodePathLine } from './systemd-unit.ts';
|
|
26
26
|
import { allowScriptsArgs, compareVersions, parseUpdateArgs } from './update-args.ts';
|
|
27
|
+
import { load as parseYaml } from 'js-yaml';
|
|
28
|
+
import { DEFAULT_UPDATE_ENDPOINT, readState, statusFrom, type UpdateCheckStatus } from '../server/update-check.ts';
|
|
27
29
|
import {
|
|
28
30
|
LAUNCHD_LABEL, launchdAvailable, launchdLoaded, launchdPid, launchdPlistPath, launchdRestart, launchdStart,
|
|
29
31
|
launchdStop, nodeDirOnPath, writeLaunchdPlist,
|
|
@@ -80,6 +82,7 @@ Usage:
|
|
|
80
82
|
what is installed, install with npm into ~/.somora/lsp
|
|
81
83
|
somora wiki migrate [step] [id] move a grown wiki onto the folder template (docs/wiki.md):
|
|
82
84
|
guided, or plan|judge|status|approve|dry-run|run|undo
|
|
85
|
+
somora telemetry show what the daily update check sends to somora.ai, and when
|
|
83
86
|
somora update [<version>|--edge] install from npm + rebake systemd + restart
|
|
84
87
|
(run \`somora update --help\` for options)
|
|
85
88
|
somora --version show version
|
|
@@ -377,6 +380,12 @@ function cmdServerStatus(): number {
|
|
|
377
380
|
} else {
|
|
378
381
|
process.stdout.write(`lockfile: ${LOCKFILE_PATH} (none — no server running)\n`);
|
|
379
382
|
}
|
|
383
|
+
const update = updateStatus();
|
|
384
|
+
if (update.latestVersion) {
|
|
385
|
+
process.stdout.write(update.updateAvailable
|
|
386
|
+
? ` update: ${update.latestVersion} is available — run \`somora update\`${update.note ? ` (${update.note})` : ''}\n`
|
|
387
|
+
: ` update: none (${update.latestVersion} is current)\n`);
|
|
388
|
+
}
|
|
380
389
|
if (launchdAvailable()) {
|
|
381
390
|
const installed = existsSync(launchdPlistPath());
|
|
382
391
|
const pid = installed ? launchdPid() : null;
|
|
@@ -578,6 +587,51 @@ async function cmdUpdate(args: string[]): Promise<number> {
|
|
|
578
587
|
return 0;
|
|
579
588
|
}
|
|
580
589
|
|
|
590
|
+
// ─── telemetry ──────────────────────────────────────────────────────
|
|
591
|
+
|
|
592
|
+
function updateStatus(): UpdateCheckStatus {
|
|
593
|
+
const file = join(SOMORA_HOME, 'config.yaml');
|
|
594
|
+
let enabled = true;
|
|
595
|
+
let endpoint = DEFAULT_UPDATE_ENDPOINT;
|
|
596
|
+
try {
|
|
597
|
+
const raw = parseYaml(readFileSync(file, 'utf8')) as { updateCheck?: { enabled?: unknown; endpoint?: unknown } } | null;
|
|
598
|
+
if (raw?.updateCheck?.enabled === false) enabled = false;
|
|
599
|
+
if (typeof raw?.updateCheck?.endpoint === 'string') endpoint = raw.updateCheck.endpoint;
|
|
600
|
+
} catch {
|
|
601
|
+
/* no config yet — defaults */
|
|
602
|
+
}
|
|
603
|
+
return statusFrom({ version: SOMORA_VERSION, configEnabled: enabled, endpoint, surface: 'cli', state: readState(join(SOMORA_HOME, 'update-check.json')) });
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/** `somora telemetry show [--json]` — the whole truth about the one
|
|
607
|
+
* request somora makes to somora.ai. */
|
|
608
|
+
async function cmdTelemetry(args: string[]): Promise<number> {
|
|
609
|
+
const sub = args[0];
|
|
610
|
+
if (sub !== 'show') {
|
|
611
|
+
process.stdout.write('Usage:\n somora telemetry show [--json] what the daily update check sends, where, and when it last ran\n');
|
|
612
|
+
return sub === undefined || sub === '--help' || sub === '-h' ? 0 : 2;
|
|
613
|
+
}
|
|
614
|
+
const s = updateStatus();
|
|
615
|
+
if (args.includes('--json')) {
|
|
616
|
+
process.stdout.write(`${JSON.stringify(s, null, 2)}\n`);
|
|
617
|
+
return 0;
|
|
618
|
+
}
|
|
619
|
+
const why: Record<UpdateCheckStatus['reason'], string> = {
|
|
620
|
+
enabled: 'enabled (updateCheck.enabled in config.yaml)',
|
|
621
|
+
'do-not-track': 'disabled by DO_NOT_TRACK',
|
|
622
|
+
'automated-environment': 'disabled in an automated environment (CI is set)',
|
|
623
|
+
'config-disabled': 'disabled in config.yaml (updateCheck.enabled: false)',
|
|
624
|
+
};
|
|
625
|
+
process.stdout.write(`daily update check: ${why[s.reason]}\n`);
|
|
626
|
+
process.stdout.write(` request: GET ${s.endpoint}\n`);
|
|
627
|
+
process.stdout.write(` header: User-Agent: ${s.userAgent}\n`);
|
|
628
|
+
process.stdout.write(' body: none — no identifier, no machine id, nothing else\n');
|
|
629
|
+
process.stdout.write(` last run: ${s.lastCheckedAt ? new Date(s.lastCheckedAt).toISOString() : 'never'}\n`);
|
|
630
|
+
process.stdout.write(` answer: ${s.latestVersion ? `latest ${s.latestVersion}${s.updateAvailable ? ' — newer than this install' : ' — this install is current'}` : '—'}${s.note ? `\n note: ${s.note}` : ''}\n`);
|
|
631
|
+
process.stdout.write(' somora.ai logs the request with its IP address and counts installations from that; details: docs/setup.md → "The daily update check".\n');
|
|
632
|
+
return 0;
|
|
633
|
+
}
|
|
634
|
+
|
|
581
635
|
// ─── main ────────────────────────────────────────────────────────────
|
|
582
636
|
|
|
583
637
|
async function main(): Promise<number> {
|
|
@@ -612,6 +666,8 @@ async function main(): Promise<number> {
|
|
|
612
666
|
}
|
|
613
667
|
case 'update':
|
|
614
668
|
return await cmdUpdate(rest);
|
|
669
|
+
case 'telemetry':
|
|
670
|
+
return await cmdTelemetry(rest);
|
|
615
671
|
case 'codex': {
|
|
616
672
|
const { runCodexCli } = await import('./codex.ts');
|
|
617
673
|
return await runCodexCli(rest);
|
package/src/config/reload.ts
CHANGED
|
@@ -20,6 +20,7 @@ export const RESTART_REQUIRED_SECTIONS: readonly string[] = [
|
|
|
20
20
|
'stt', // voice backends are wired at boot
|
|
21
21
|
'tts',
|
|
22
22
|
'sentinel', // scheduler + retention read at boot
|
|
23
|
+
'updateCheck', // the daily check is scheduled at boot
|
|
23
24
|
'tmux', // attention watcher configured at boot
|
|
24
25
|
'web', // static mounts decided at boot
|
|
25
26
|
'mobile',
|
package/src/config/types.ts
CHANGED
|
@@ -1636,6 +1636,21 @@ export const SentinelConfigSchema = z
|
|
|
1636
1636
|
.default({ completedRetentionDays: 7 });
|
|
1637
1637
|
export type SentinelConfig = z.infer<typeof SentinelConfigSchema>;
|
|
1638
1638
|
|
|
1639
|
+
// The daily update check (src/server/update-check.ts): the one request
|
|
1640
|
+
// somora makes to somora.ai. No identifier, no body — version, OS, Node
|
|
1641
|
+
// version and CPU in the User-Agent. somora.ai logs the request like
|
|
1642
|
+
// any web server and counts installations from those logs
|
|
1643
|
+
// (docs/setup.md → "The daily update check"). DO_NOT_TRACK=1 and CI
|
|
1644
|
+
// override this section.
|
|
1645
|
+
export const UpdateCheckConfigSchema = z
|
|
1646
|
+
.object({
|
|
1647
|
+
enabled: z.boolean().default(true),
|
|
1648
|
+
/** Where to ask. A self-hosted mirror can point this elsewhere. */
|
|
1649
|
+
endpoint: z.string().url().default('https://somora.ai/api/latest-version'),
|
|
1650
|
+
})
|
|
1651
|
+
.default({ enabled: true, endpoint: 'https://somora.ai/api/latest-version' });
|
|
1652
|
+
export type UpdateCheckConfig = z.infer<typeof UpdateCheckConfigSchema>;
|
|
1653
|
+
|
|
1639
1654
|
// Skills budget. Mirrors OpenClaw's defaults — they've shipped 53 real
|
|
1640
1655
|
// skills against these limits for months, so we treat them as known-
|
|
1641
1656
|
// good rather than re-deriving. Configurable via config.yaml per
|
|
@@ -2212,6 +2227,7 @@ export const ConfigSchema = z.object({
|
|
|
2212
2227
|
browser: BrowserConfigSchema,
|
|
2213
2228
|
projects: ProjectsConfigSchema,
|
|
2214
2229
|
sentinel: SentinelConfigSchema,
|
|
2230
|
+
updateCheck: UpdateCheckConfigSchema,
|
|
2215
2231
|
obsidian: ObsidianConfigSchema,
|
|
2216
2232
|
wiki: WikiConfigSchema,
|
|
2217
2233
|
attachments: AttachmentsConfigSchema,
|
package/src/server/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { randomUUID } from 'node:crypto';
|
|
|
15
15
|
import { readFileSync } from 'node:fs';
|
|
16
16
|
import { createSecureServer as createHttp2SecureServer } from 'node:http2';
|
|
17
17
|
import { TlsKeeper, type SecureContextHolder } from './tls-keeper.ts';
|
|
18
|
+
import { UpdateChecker } from './update-check.ts';
|
|
18
19
|
import {
|
|
19
20
|
homedir,
|
|
20
21
|
cpus as osCpus,
|
|
@@ -1615,7 +1616,25 @@ app.get(
|
|
|
1615
1616
|
// reads package.json), so this is effectively a static snapshot for
|
|
1616
1617
|
// the lifetime of the process. JSON keeps room for future fields
|
|
1617
1618
|
// (build-time, git SHA, env name) without breaking the client.
|
|
1618
|
-
|
|
1619
|
+
// The daily update check (docs/setup.md → "The daily update check").
|
|
1620
|
+
const updateChecker = new UpdateChecker({
|
|
1621
|
+
version: SOMORA_VERSION,
|
|
1622
|
+
endpoint: config.updateCheck.endpoint,
|
|
1623
|
+
statePath: joinPath(SOMORA_HOME_DIR, 'update-check.json'),
|
|
1624
|
+
configEnabled: config.updateCheck.enabled,
|
|
1625
|
+
surface: 'server',
|
|
1626
|
+
log: logger,
|
|
1627
|
+
});
|
|
1628
|
+
updateChecker.start();
|
|
1629
|
+
|
|
1630
|
+
/** Version plus, when known, what somora.ai says is current. */
|
|
1631
|
+
app.get('/version', (c) => {
|
|
1632
|
+
const u = updateChecker.status();
|
|
1633
|
+
return c.json({
|
|
1634
|
+
version: SOMORA_VERSION,
|
|
1635
|
+
update: u.latestVersion ? { latestVersion: u.latestVersion, available: u.updateAvailable, ...(u.note ? { note: u.note } : {}) } : null,
|
|
1636
|
+
});
|
|
1637
|
+
});
|
|
1619
1638
|
|
|
1620
1639
|
// ─── Config reload + restart ─────────────────────────────────────────
|
|
1621
1640
|
// Web taskbar gear + TUI /reload, /restart. Reload re-reads config.yaml,
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Daily update check — the one request somora makes to somora.ai.
|
|
2
|
+
//
|
|
3
|
+
// Once a day the server asks `GET https://somora.ai/api/latest-version`
|
|
4
|
+
// whether a newer version exists and shows the answer in the clients.
|
|
5
|
+
// The request carries no body and no identifier, only the program's
|
|
6
|
+
// User-Agent line (version, OS, Node version, CPU, server|cli). Like
|
|
7
|
+
// any web server, somora.ai logs the request with its IP address and
|
|
8
|
+
// counts installations from those logs — documented in docs/setup.md,
|
|
9
|
+
// never published. Off with DO_NOT_TRACK=1, in CI, or
|
|
10
|
+
// `updateCheck.enabled: false`.
|
|
11
|
+
|
|
12
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
13
|
+
import { dirname } from 'node:path';
|
|
14
|
+
|
|
15
|
+
import { compareVersions } from '../cli/update-args.ts';
|
|
16
|
+
|
|
17
|
+
export const DEFAULT_UPDATE_ENDPOINT = 'https://somora.ai/api/latest-version';
|
|
18
|
+
const INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
19
|
+
const RETRY_AFTER_FAILURE_MS = 60 * 60 * 1000;
|
|
20
|
+
const FIRST_DELAY_MS = 60 * 1000;
|
|
21
|
+
const FIRST_JITTER_MS = 5 * 60 * 1000;
|
|
22
|
+
const TICK_MS = 60 * 60 * 1000;
|
|
23
|
+
const TIMEOUT_MS = 3000;
|
|
24
|
+
const NOTE_MAX = 500;
|
|
25
|
+
|
|
26
|
+
export type UpdateSurface = 'server' | 'cli';
|
|
27
|
+
|
|
28
|
+
export type UpdateCheckReason = 'enabled' | 'do-not-track' | 'automated-environment' | 'config-disabled';
|
|
29
|
+
|
|
30
|
+
export interface UpdateCheckState {
|
|
31
|
+
lastCheckedAt?: number;
|
|
32
|
+
lastSuccessAt?: number;
|
|
33
|
+
latestVersion?: string;
|
|
34
|
+
note?: string;
|
|
35
|
+
lastError?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface UpdateCheckStatus {
|
|
39
|
+
enabled: boolean;
|
|
40
|
+
reason: UpdateCheckReason;
|
|
41
|
+
endpoint: string;
|
|
42
|
+
userAgent: string;
|
|
43
|
+
lastCheckedAt: number | null;
|
|
44
|
+
latestVersion: string | null;
|
|
45
|
+
note: string | null;
|
|
46
|
+
/** The running version is older than the published one. */
|
|
47
|
+
updateAvailable: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isTruthy(v: string | undefined): boolean {
|
|
51
|
+
return v !== undefined && !['', '0', 'false', 'no', 'off'].includes(v.trim().toLowerCase());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Why the check is on or off — env wins over config, as everywhere. */
|
|
55
|
+
export function resolveUpdateCheckReason(configEnabled: boolean, env: NodeJS.ProcessEnv = process.env): UpdateCheckReason {
|
|
56
|
+
if (isTruthy(env.DO_NOT_TRACK)) return 'do-not-track';
|
|
57
|
+
// CI jobs are not installations and should not count as such.
|
|
58
|
+
if (isTruthy(env.CI)) return 'automated-environment';
|
|
59
|
+
if (!configEnabled) return 'config-disabled';
|
|
60
|
+
return 'enabled';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function buildUserAgent(version: string, surface: UpdateSurface, p: { platform: string; node: string; arch: string } = { platform: process.platform, node: process.versions.node, arch: process.arch }): string {
|
|
64
|
+
return `somora/${version} (${p.platform}; node/${p.node}; ${p.arch}; ${surface})`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The server's answer, validated; null when it is not usable. */
|
|
68
|
+
export function parseLatestVersion(body: unknown): { version: string; note?: string } | null {
|
|
69
|
+
if (!body || typeof body !== 'object') return null;
|
|
70
|
+
const v = (body as { version?: unknown }).version;
|
|
71
|
+
if (typeof v !== 'string' || !/^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/.test(v.trim())) return null;
|
|
72
|
+
const n = (body as { note?: unknown }).note;
|
|
73
|
+
const note = typeof n === 'string' && n.trim() ? n.trim().slice(0, NOTE_MAX) : undefined;
|
|
74
|
+
return { version: v.trim(), ...(note ? { note } : {}) };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function readState(path: string): UpdateCheckState {
|
|
78
|
+
try {
|
|
79
|
+
const j = JSON.parse(readFileSync(path, 'utf8')) as UpdateCheckState;
|
|
80
|
+
return j && typeof j === 'object' ? j : {};
|
|
81
|
+
} catch {
|
|
82
|
+
return {};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function writeState(path: string, state: UpdateCheckState): void {
|
|
87
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
88
|
+
const tmp = `${path}.tmp`;
|
|
89
|
+
writeFileSync(tmp, JSON.stringify(state, null, 2));
|
|
90
|
+
renameSync(tmp, path);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Is a check due? Once a day after a success, an hour after a failure. */
|
|
94
|
+
export function isDue(state: UpdateCheckState, now: number = Date.now()): boolean {
|
|
95
|
+
if (state.lastSuccessAt && now - state.lastSuccessAt < INTERVAL_MS) return false;
|
|
96
|
+
if (state.lastCheckedAt && !state.lastSuccessAt && now - state.lastCheckedAt < RETRY_AFTER_FAILURE_MS) return false;
|
|
97
|
+
if (state.lastCheckedAt && state.lastSuccessAt && state.lastCheckedAt > state.lastSuccessAt && now - state.lastCheckedAt < RETRY_AFTER_FAILURE_MS) return false;
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function statusFrom(args: { version: string; configEnabled: boolean; endpoint: string; surface: UpdateSurface; state: UpdateCheckState; env?: NodeJS.ProcessEnv }): UpdateCheckStatus {
|
|
102
|
+
const reason = resolveUpdateCheckReason(args.configEnabled, args.env);
|
|
103
|
+
const latest = args.state.latestVersion ?? null;
|
|
104
|
+
return {
|
|
105
|
+
enabled: reason === 'enabled',
|
|
106
|
+
reason,
|
|
107
|
+
endpoint: args.endpoint,
|
|
108
|
+
userAgent: buildUserAgent(args.version, args.surface),
|
|
109
|
+
lastCheckedAt: args.state.lastSuccessAt ?? null,
|
|
110
|
+
latestVersion: latest,
|
|
111
|
+
note: args.state.note ?? null,
|
|
112
|
+
updateAvailable: latest !== null && compareVersions(latest, args.version) > 0,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface UpdateCheckerOpts {
|
|
117
|
+
version: string;
|
|
118
|
+
endpoint: string;
|
|
119
|
+
statePath: string;
|
|
120
|
+
configEnabled: boolean;
|
|
121
|
+
surface: UpdateSurface;
|
|
122
|
+
log: { info(o: Record<string, unknown>): void; warn(o: Record<string, unknown>): void };
|
|
123
|
+
fetchImpl?: typeof fetch;
|
|
124
|
+
env?: NodeJS.ProcessEnv;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export class UpdateChecker {
|
|
128
|
+
private state: UpdateCheckState;
|
|
129
|
+
private timers: NodeJS.Timeout[] = [];
|
|
130
|
+
private readonly fetchImpl: typeof fetch;
|
|
131
|
+
|
|
132
|
+
constructor(private readonly opts: UpdateCheckerOpts) {
|
|
133
|
+
this.state = readState(opts.statePath);
|
|
134
|
+
this.fetchImpl = opts.fetchImpl ?? fetch;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
status(): UpdateCheckStatus {
|
|
138
|
+
return statusFrom({ ...this.opts, state: this.state });
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Boot: one check after a short random delay (so a fleet restarting
|
|
142
|
+
* at once does not knock in unison), then an hourly look at the clock. */
|
|
143
|
+
start(): void {
|
|
144
|
+
const s = this.status();
|
|
145
|
+
if (!s.enabled) {
|
|
146
|
+
this.opts.log.info({ msg: 'update.check_disabled', reason: s.reason });
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const first = setTimeout(() => {
|
|
150
|
+
void this.checkIfDue();
|
|
151
|
+
const tick = setInterval(() => void this.checkIfDue(), TICK_MS);
|
|
152
|
+
tick.unref();
|
|
153
|
+
this.timers.push(tick);
|
|
154
|
+
}, FIRST_DELAY_MS + Math.floor(Math.random() * FIRST_JITTER_MS));
|
|
155
|
+
first.unref();
|
|
156
|
+
this.timers.push(first);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
stop(): void {
|
|
160
|
+
for (const t of this.timers) clearTimeout(t);
|
|
161
|
+
this.timers = [];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async checkIfDue(now: number = Date.now()): Promise<'checked' | 'skipped'> {
|
|
165
|
+
if (!isDue(this.state, now)) return 'skipped';
|
|
166
|
+
await this.check(now);
|
|
167
|
+
return 'checked';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** One request, result into the state file. Never throws. */
|
|
171
|
+
async check(now: number = Date.now()): Promise<UpdateCheckStatus> {
|
|
172
|
+
const { endpoint, log } = this.opts;
|
|
173
|
+
this.state = { ...this.state, lastCheckedAt: now };
|
|
174
|
+
try {
|
|
175
|
+
const res = await this.fetchImpl(endpoint, {
|
|
176
|
+
method: 'GET',
|
|
177
|
+
headers: { 'User-Agent': buildUserAgent(this.opts.version, this.opts.surface), Accept: 'application/json' },
|
|
178
|
+
redirect: 'manual',
|
|
179
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
180
|
+
});
|
|
181
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
182
|
+
const parsed = parseLatestVersion(await res.json());
|
|
183
|
+
if (!parsed) throw new Error('unusable answer');
|
|
184
|
+
this.state = { lastCheckedAt: now, lastSuccessAt: now, latestVersion: parsed.version, ...(parsed.note ? { note: parsed.note } : {}) };
|
|
185
|
+
const available = compareVersions(parsed.version, this.opts.version) > 0;
|
|
186
|
+
log.info({ msg: available ? 'update.available' : 'update.check', latest: parsed.version, running: this.opts.version, ...(parsed.note ? { note: parsed.note } : {}) });
|
|
187
|
+
} catch (err) {
|
|
188
|
+
const message = (err as Error).name === 'TimeoutError' ? 'timed out after 3 s' : (err as Error).message;
|
|
189
|
+
// The same failure every hour is one fact, not twenty-four: warn
|
|
190
|
+
// once, then only when the reason changes.
|
|
191
|
+
const repeat = this.state.lastError === message;
|
|
192
|
+
this.state = { ...this.state, lastError: message };
|
|
193
|
+
log[repeat ? 'info' : 'warn']({ msg: repeat ? 'update.check_failed_again' : 'update.check_failed', endpoint, err: message });
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
writeState(this.opts.statePath, this.state);
|
|
197
|
+
} catch (err) {
|
|
198
|
+
log.warn({ msg: 'update.state_write_failed', err: (err as Error).message });
|
|
199
|
+
}
|
|
200
|
+
return this.status();
|
|
201
|
+
}
|
|
202
|
+
}
|