@sogni-ai/sogni-creative-agent-skill 3.18.1 โ 3.20.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/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/CHANGELOG.md +7 -0
- package/README.md +8 -4
- package/SKILL.md +5 -3
- package/desktop-extension/manifest.json +1 -1
- package/llm.txt +6 -4
- package/openclaw.plugin.json +1 -1
- package/package.json +3 -3
- package/references/models.md +15 -0
- package/skill-package.json +1 -1
- package/sogni-agent.mjs +14 -1
- package/sogni-app-id.mjs +250 -51
- package/version.mjs +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sogni-creative-agent",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.20.0",
|
|
4
4
|
"description": "Sogni Creative Agent: image, video, and music generation via Sogni AI's decentralized GPU network. Supports personas, persistent memories, style transfer, and multi-step creative workflows.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Sogni AI",
|
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [3.19.0](https://github.com/Sogni-AI/sogni-creative-agent-skill/compare/v3.18.1...v3.19.0) (2026-07-28)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* **cli:** lease per-process app IDs from a persistent slot pool ([#26](https://github.com/Sogni-AI/sogni-creative-agent-skill/issues/26)) ([ef0ae8b](https://github.com/Sogni-AI/sogni-creative-agent-skill/commit/ef0ae8b973e65f88a78b775fb1b791daca719d42))
|
|
7
|
+
|
|
1
8
|
# [3.18.1](https://github.com/Sogni-AI/sogni-creative-agent-skill/compare/v3.18.0...v3.18.1) (2026-07-28)
|
|
2
9
|
|
|
3
10
|
|
package/README.md
CHANGED
|
@@ -341,14 +341,18 @@ You can also skip the file and export `SOGNI_API_KEY` in your environment.
|
|
|
341
341
|
|
|
342
342
|
### Filesystem path overrides
|
|
343
343
|
|
|
344
|
-
Defaults live under `~/.config/sogni/` for credentials, last-render metadata, personas, memories, and personality.
|
|
344
|
+
Defaults live under `~/.config/sogni/` for credentials, last-render metadata, personas, memories, and personality.
|
|
345
|
+
|
|
346
|
+
**Running several agents at once** (Claude Code, Codex, OpenCode, hermes, ...) works out of the box: each process leases its own stable app ID from a persistent slot pool in `~/.config/sogni/app-ids/`, so concurrent runs never fight over one socket identity (SWITCH_CONNECTION 4015) and routine runs never mint new IDs against the per-address daily allowance (error 4061). Leases are released on exit and reclaimed automatically if a process dies. Long-lived daemons should pin their own `SOGNI_APP_ID` to stay out of the shared pool, and each harness can set its own `SOGNI_LAST_RENDER_PATH` so `--last` stays per-tool. Override individual paths with:
|
|
345
347
|
|
|
346
348
|
| Variable | Purpose |
|
|
347
349
|
|----------|---------|
|
|
348
350
|
| `SOGNI_CREDENTIALS_PATH` | Custom credentials file |
|
|
349
|
-
| `SOGNI_APP_ID` |
|
|
350
|
-
| `SOGNI_APP_ID_PATH` |
|
|
351
|
-
| `
|
|
351
|
+
| `SOGNI_APP_ID` | Pinned app ID (ephemeral/container homes and long-lived daemons; skips the pool) |
|
|
352
|
+
| `SOGNI_APP_ID_PATH` | Legacy single app-ID file mode for callers that manage their own concurrency |
|
|
353
|
+
| `SOGNI_APP_ID_POOL_DIR` | Persistent app-ID slot pool for concurrent agents (default: `~/.config/sogni/app-ids/`) |
|
|
354
|
+
| `SOGNI_APP_ID_POOL_MAX` | Maximum concurrent app-ID slots (default: 32) |
|
|
355
|
+
| `SOGNI_LAST_RENDER_PATH` | Where last-render state is persisted; give each agent harness its own file so `--last` never reads another tool's render |
|
|
352
356
|
| `SOGNI_MODEL_CATALOG_URL` | Model catalog API base URL (default: `https://api.sogni.ai/v1/model-catalog`) |
|
|
353
357
|
| `SOGNI_MODEL_CATALOG_CACHE_PATH` | Base path for the five-minute model catalog caches and ETags |
|
|
354
358
|
| `SOGNI_MEDIA_INBOUND_DIR` | Directory used by `--list-media` |
|
package/SKILL.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
name: sogni-creative-agent-skill
|
|
3
3
|
description: "Sogni Creative Agent Skill: agent skill and CLI for image, video, and music generation using Sogni AI's decentralized GPU network. Supports one-click image-folder loop reels, personas (named people with saved reference photos and voice clips), persistent memories, custom personality, style transfer, angle synthesis, Seedance/HappyHorse/LTX/WAN video, music/lyrics, hosted chat, durable workflows, replay records, and multi-step creative workflows. Ask the agent to \"draw\", \"generate\", \"create an image\", \"make a video/animate\", \"turn this image folder into a loop\", \"make music\", \"apply a style\", or \"generate me as a superhero\"."
|
|
4
4
|
metadata:
|
|
5
|
-
version: "3.
|
|
5
|
+
version: "3.20.0"
|
|
6
6
|
homepage: https://sogni.ai
|
|
7
7
|
openclaw:
|
|
8
8
|
emoji: "๐จ"
|
|
@@ -94,7 +94,7 @@ sogni-agent -o /tmp/cat.png "a cat wearing a hat" # โ avoid โ user can't
|
|
|
94
94
|
## Filesystem Paths and Overrides
|
|
95
95
|
|
|
96
96
|
- API key credentials file (read): `~/.config/sogni/credentials` (`SOGNI_CREDENTIALS_PATH`)
|
|
97
|
-
- Reusable
|
|
97
|
+
- Reusable app-ID slot pool (read/write): `~/.config/sogni/app-ids/slot-<n>` plus `.lease` files; new IDs use `sogni-agent-<uuid>` (`SOGNI_APP_ID_POOL_DIR`/`SOGNI_APP_ID_POOL_MAX`). Concurrent agent processes lease distinct slots automatically; the legacy single `~/.config/sogni/app-id` file migrates into slot-0 on first use. Set a stable `SOGNI_APP_ID` for ephemeral/container homes or long-lived daemons, or `SOGNI_APP_ID_PATH` for legacy single-file mode.
|
|
98
98
|
- Last render metadata (read/write): `~/.config/sogni/last-render.json` (`SOGNI_LAST_RENDER_PATH`)
|
|
99
99
|
- Model catalog: `https://api.sogni.ai/v1/model-catalog` (`SOGNI_MODEL_CATALOG_URL`)
|
|
100
100
|
- Model catalog cache (read/write, 5-minute TTL with ETag revalidation for model parameters and discovery): `~/.config/sogni/model-catalog-cache.json` (`SOGNI_MODEL_CATALOG_CACHE_PATH`)
|
|
@@ -226,6 +226,7 @@ sogni-agent -c photo.jpg -m qwen_image_edit_2511_fp8 "turn this into anime style
|
|
|
226
226
|
```
|
|
227
227
|
|
|
228
228
|
- For identity-preserving Krea edits, use `-m krea2_identity_edit_v1_2` with 1-2 context images; use `-m dark_beast_krea2_identity_edit_v1_2` for the Dark Beast Krea 2 identity edit LoRA. Both use 512-2048 px output, 8-12 steps, guidance 1, and default to 10 steps.
|
|
229
|
+
- For Krea 2 style/control LoRAs, use the base text-to-image model `-m krea2_turbo_fp8_scaled` with repeatable ordered `--lora <id> --lora-strength <n>` arguments (or comma-separated `--loras` / `--lora-strengths`). Up to 8 LoRAs may be stacked. Order matters, strengths are positional, omitted strengths default to 1, and many Krea 2 LoRAs are bipolar sliders whose negative values apply the inverse effect. Do not clamp them to 0-2. The first use of an uncached LoRA may pause while the worker downloads it.
|
|
229
230
|
- Do not route to `--photobooth` merely because the user asks to preserve a face in a style edit โ face-preserving full-image edits use `-c` with an image edit model. When context images are provided without `-m`, the CLI defaults to `qwen_image_edit_2511_fp8_lightning`; select `-m gpt-image-2` for up to 16 reference images and OpenAI-backed editing (Qwen supports up to 3; Krea identity edit supports up to 2).
|
|
230
231
|
|
|
231
232
|
### LTX video prompts
|
|
@@ -336,7 +337,8 @@ Eligible Sogni-hosted renders use Unlimited coverage when active; otherwise rend
|
|
|
336
337
|
|
|
337
338
|
- **Anything broken?** Run `sogni-agent doctor` first โ it checks Node, credentials (and file permissions), config-dir writability, ffmpeg, live auth, and version freshness, with a fix in every failure detail.
|
|
338
339
|
- **Auth errors:** check `SOGNI_API_KEY` or `~/.config/sogni/credentials` (key from https://dashboard.sogni.ai, account menu).
|
|
339
|
-
- **Error 4061 / too many app IDs:** the CLI
|
|
340
|
+
- **Error 4061 / too many app IDs:** the CLI leases stable IDs from the persistent pool in `~/.config/sogni/app-ids/`. Do not delete that directory between runs. For ephemeral/container homes, set the same `SOGNI_APP_ID` on every session. App IDs already registered today remain counted until 00:00 UTC, so an existing block may require waiting for that reset once after upgrading.
|
|
341
|
+
- **Kicked mid-render / SWITCH_CONNECTION 4015:** two processes shared one app ID. The slot pool prevents this for concurrent CLI runs; if a long-lived daemon also uses this account, give it its own pinned `SOGNI_APP_ID`.
|
|
340
342
|
- **Video size errors:** sizes are model-specific (WAN รท16 min 480 max 1536; LTX รท64, long side โค2048). The CLI auto-adjusts for local refs; `--strict-size` makes it fail with a suggested size instead. Details in [`references/models.md`](./references/models.md).
|
|
341
343
|
- **Timeouts:** try a faster model or raise `-t`.
|
|
342
344
|
- **No workers:** check https://sogni.ai for network status.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"manifest_version": "0.3",
|
|
3
3
|
"name": "sogni-creative-agent",
|
|
4
4
|
"display_name": "Sogni Creative Agent",
|
|
5
|
-
"version": "3.
|
|
5
|
+
"version": "3.20.0",
|
|
6
6
|
"description": "Generate images, video, and music on Sogni AI's decentralized GPU network โ with personas, memories, and safe video editing tools.",
|
|
7
7
|
"author": { "name": "Sogni AI", "url": "https://sogni.ai" },
|
|
8
8
|
"homepage": "https://sogni.ai",
|
package/llm.txt
CHANGED
|
@@ -74,10 +74,12 @@ chmod 600 ~/.config/sogni/credentials
|
|
|
74
74
|
You can also export `SOGNI_API_KEY` instead of writing the file. Confirm with
|
|
75
75
|
`sogni-agent doctor`.
|
|
76
76
|
|
|
77
|
-
The CLI
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
The CLI leases a stable app ID per process from a persistent slot pool in
|
|
78
|
+
`~/.config/sogni/app-ids/`, so concurrent agents (Claude Code, Codex,
|
|
79
|
+
OpenCode, hermes, ...) never share one socket identity and routine runs never
|
|
80
|
+
mint new IDs. Preserve that directory between sessions. In ephemeral
|
|
81
|
+
containers, set one stable `SOGNI_APP_ID` value; rotating IDs on each run can
|
|
82
|
+
trigger Sogni error 4061 (allowance resets at 00:00 UTC).
|
|
81
83
|
|
|
82
84
|
## Hosted API modes (require SOGNI_API_KEY)
|
|
83
85
|
|
package/openclaw.plugin.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"id": "sogni-creative-agent-skill",
|
|
3
3
|
"name": "Sogni Creative Agent Skill โ Image, Video & Music Generation",
|
|
4
4
|
"description": "Agent skill and CLI for Sogni AI image, video, and music generation.",
|
|
5
|
-
"version": "3.
|
|
5
|
+
"version": "3.20.0",
|
|
6
6
|
"skills": [
|
|
7
7
|
"."
|
|
8
8
|
],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sogni-ai/sogni-creative-agent-skill",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.20.0",
|
|
4
4
|
"description": "Sogni Creative Agent Skill: agent skill and CLI for Sogni AI image, video, and music generation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "sogni-agent.mjs",
|
|
@@ -87,13 +87,13 @@
|
|
|
87
87
|
"sogni-agent.mjs"
|
|
88
88
|
],
|
|
89
89
|
"dependencies": {
|
|
90
|
-
"@sogni-ai/sogni-intelligence-client": "3.
|
|
90
|
+
"@sogni-ai/sogni-intelligence-client": "3.9.0",
|
|
91
91
|
"execa": "^9.6.1",
|
|
92
92
|
"json5": "^2.2.3",
|
|
93
93
|
"sharp": "^0.34.5"
|
|
94
94
|
},
|
|
95
95
|
"overrides": {
|
|
96
|
-
"@sogni-ai/sogni-client": "5.1.0-alpha.
|
|
96
|
+
"@sogni-ai/sogni-client": "5.1.0-alpha.24"
|
|
97
97
|
},
|
|
98
98
|
"openclaw": {
|
|
99
99
|
"extensions": [
|
package/references/models.md
CHANGED
|
@@ -67,6 +67,21 @@ For Krea 2 Turbo, hosted/chat planning may use the creative-agent selector
|
|
|
67
67
|
`krea-2-turbo`; direct CLI `-m` uses the worker model ID
|
|
68
68
|
`krea2_turbo_fp8_scaled`.
|
|
69
69
|
|
|
70
|
+
Krea 2 Turbo supports ordered LoRA stacks:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
sogni-agent -m krea2_turbo_fp8_scaled \
|
|
74
|
+
--lora krea2-detail-enhancer --lora-strength 3 \
|
|
75
|
+
--lora krea2-amateur --lora-strength -2 \
|
|
76
|
+
"candid editorial street portrait at dusk"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Use no more than 8 LoRAs. `loraStrengths[i]` controls `loras[i]`, and changing
|
|
80
|
+
the order can change the result. Omitted strengths default to 1. Many Krea 2
|
|
81
|
+
LoRAs are bipolar sliders, so negative values are valid and apply the inverse
|
|
82
|
+
effect. Follow the range documented for each LoRA rather than assuming 0-2.
|
|
83
|
+
The first render with an uncached LoRA can take longer while its asset downloads.
|
|
84
|
+
|
|
70
85
|
For identity-preserving Krea edits, pass a context image with
|
|
71
86
|
`-m krea2_identity_edit_v1_2` or
|
|
72
87
|
`-m dark_beast_krea2_identity_edit_v1_2`. These edit models support 1-2
|
package/skill-package.json
CHANGED
package/sogni-agent.mjs
CHANGED
|
@@ -3211,7 +3211,7 @@ Image Options:
|
|
|
3211
3211
|
--output-format <f> Image output format: png|jpg (webp for gpt-image-2)
|
|
3212
3212
|
--sampler <name> Sampler (model-dependent)
|
|
3213
3213
|
--scheduler <name> Scheduler (model-dependent)
|
|
3214
|
-
--lora <id> LoRA id (repeatable
|
|
3214
|
+
--lora <id> Image LoRA id (repeatable; order is significant)
|
|
3215
3215
|
--loras <ids> Comma-separated LoRA ids
|
|
3216
3216
|
--lora-strength <n> LoRA strength (repeatable)
|
|
3217
3217
|
--lora-strengths <n> Comma-separated LoRA strengths
|
|
@@ -3874,6 +3874,13 @@ if (options.loraStrengths.length > 0 && options.loras.length > 0 &&
|
|
|
3874
3874
|
});
|
|
3875
3875
|
}
|
|
3876
3876
|
|
|
3877
|
+
if (!options.video && options.loras.length > 8) {
|
|
3878
|
+
fatalCliError('Image generation supports at most 8 LoRAs per render.', {
|
|
3879
|
+
code: 'INVALID_ARGUMENT',
|
|
3880
|
+
details: { loras: options.loras.length, maximum: 8 }
|
|
3881
|
+
});
|
|
3882
|
+
}
|
|
3883
|
+
|
|
3877
3884
|
if (options.music && options.loras.length > 0) {
|
|
3878
3885
|
fatalCliError('--lora options are not supported for music.', { code: 'INVALID_ARGUMENT' });
|
|
3879
3886
|
}
|
|
@@ -10767,6 +10774,12 @@ async function main() {
|
|
|
10767
10774
|
if (steps) {
|
|
10768
10775
|
projectConfig.steps = steps;
|
|
10769
10776
|
}
|
|
10777
|
+
if (options.loras.length > 0) {
|
|
10778
|
+
projectConfig.loras = options.loras;
|
|
10779
|
+
}
|
|
10780
|
+
if (options.loraStrengths.length > 0) {
|
|
10781
|
+
projectConfig.loraStrengths = options.loraStrengths;
|
|
10782
|
+
}
|
|
10770
10783
|
|
|
10771
10784
|
if (options.seed !== null && options.seed !== undefined) {
|
|
10772
10785
|
projectConfig.seed = options.seed;
|
package/sogni-app-id.mjs
CHANGED
|
@@ -5,6 +5,16 @@
|
|
|
5
5
|
* UTC day. An app ID therefore represents an installation, not a process or
|
|
6
6
|
* request. Keep it outside credentials so rotating an API key does not rotate
|
|
7
7
|
* the installation identity.
|
|
8
|
+
*
|
|
9
|
+
* A single shared app ID is not enough once several agent harnesses (Claude
|
|
10
|
+
* Code, Codex, OpenCode, hermes, ...) drive this CLI concurrently: the socket
|
|
11
|
+
* server allows one live connection per app ID and answers a second claim
|
|
12
|
+
* with SWITCH_CONNECTION (4015), kicking the first process mid-render. The
|
|
13
|
+
* default mode is therefore a small pool of persistent per-slot app IDs under
|
|
14
|
+
* `~/.config/sogni/app-ids/`. Each process leases the lowest free slot for
|
|
15
|
+
* its lifetime (lease liveness = pid check), so concurrent processes get
|
|
16
|
+
* distinct stable IDs, the pool only grows to the actual peak concurrency,
|
|
17
|
+
* and no new ID is minted on routine runs.
|
|
8
18
|
*/
|
|
9
19
|
import { randomUUID } from 'node:crypto';
|
|
10
20
|
import {
|
|
@@ -12,14 +22,17 @@ import {
|
|
|
12
22
|
mkdirSync,
|
|
13
23
|
openSync,
|
|
14
24
|
readFileSync,
|
|
25
|
+
readdirSync,
|
|
15
26
|
unlinkSync,
|
|
16
27
|
writeFileSync,
|
|
17
28
|
} from 'node:fs';
|
|
18
29
|
import { homedir } from 'node:os';
|
|
19
|
-
import { dirname, join } from 'node:path';
|
|
30
|
+
import { basename, dirname, join } from 'node:path';
|
|
20
31
|
import { getEnv } from './env.mjs';
|
|
21
32
|
|
|
22
33
|
const DEFAULT_APP_ID_PATH = join(homedir(), '.config', 'sogni', 'app-id');
|
|
34
|
+
const DEFAULT_APP_ID_POOL_DIR = join(homedir(), '.config', 'sogni', 'app-ids');
|
|
35
|
+
const DEFAULT_APP_ID_POOL_MAX = 32;
|
|
23
36
|
const GENERATED_APP_ID_PREFIX = 'sogni-agent-';
|
|
24
37
|
const MAX_APP_ID_LENGTH = 256;
|
|
25
38
|
const APP_ID_CREATE_RETRIES = 20;
|
|
@@ -63,7 +76,7 @@ function readPersistedAppId(appIdPath) {
|
|
|
63
76
|
}
|
|
64
77
|
|
|
65
78
|
function appIdPersistenceError(error, appIdPath) {
|
|
66
|
-
if (error?.code === 'INVALID_APP_ID') return error;
|
|
79
|
+
if (error?.code === 'INVALID_APP_ID' || error?.code === 'APP_ID_POOL_EXHAUSTED') return error;
|
|
67
80
|
const wrapped = new Error(`Could not persist the Sogni app ID at ${appIdPath}: ${error?.message || error}`);
|
|
68
81
|
wrapped.code = 'APP_ID_PERSISTENCE_FAILED';
|
|
69
82
|
wrapped.hint = 'Make that location writable, set SOGNI_APP_ID_PATH to a persistent writable file, or set one stable SOGNI_APP_ID value for every session.';
|
|
@@ -71,68 +84,254 @@ function appIdPersistenceError(error, appIdPath) {
|
|
|
71
84
|
}
|
|
72
85
|
|
|
73
86
|
/**
|
|
74
|
-
*
|
|
87
|
+
* Create-once/read-forever identity file. Concurrent first launches race on
|
|
88
|
+
* exclusive create; losers reuse the winner's value.
|
|
89
|
+
*/
|
|
90
|
+
function getOrCreateAppIdFile(resolvedPath, generateUuid) {
|
|
91
|
+
try {
|
|
92
|
+
return readPersistedAppId(resolvedPath);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
mkdirSync(dirname(resolvedPath), { recursive: true, mode: 0o700 });
|
|
98
|
+
const candidate = validateAppId(
|
|
99
|
+
`${GENERATED_APP_ID_PREFIX}${generateUuid()}`,
|
|
100
|
+
'Generated Sogni app ID',
|
|
101
|
+
);
|
|
102
|
+
let descriptor;
|
|
103
|
+
let createdFile = false;
|
|
104
|
+
try {
|
|
105
|
+
descriptor = openSync(resolvedPath, 'wx', 0o600);
|
|
106
|
+
createdFile = true;
|
|
107
|
+
writeFileSync(descriptor, `${candidate}\n`, 'utf8');
|
|
108
|
+
return candidate;
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (error?.code !== 'EEXIST') throw error;
|
|
111
|
+
|
|
112
|
+
// A concurrent first launch won the exclusive create. Reuse its value
|
|
113
|
+
// once the short write window closes.
|
|
114
|
+
let lastError = error;
|
|
115
|
+
for (let attempt = 0; attempt < APP_ID_CREATE_RETRIES; attempt += 1) {
|
|
116
|
+
try {
|
|
117
|
+
return readPersistedAppId(resolvedPath);
|
|
118
|
+
} catch (readError) {
|
|
119
|
+
lastError = readError;
|
|
120
|
+
if (attempt + 1 < APP_ID_CREATE_RETRIES) waitForConcurrentWriter();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
throw lastError;
|
|
124
|
+
} finally {
|
|
125
|
+
if (descriptor !== undefined) closeSync(descriptor);
|
|
126
|
+
// Do not strand an empty/corrupt identity if this process created the
|
|
127
|
+
// file but could not finish writing it.
|
|
128
|
+
if (createdFile) {
|
|
129
|
+
try {
|
|
130
|
+
readPersistedAppId(resolvedPath);
|
|
131
|
+
} catch {
|
|
132
|
+
try { unlinkSync(resolvedPath); } catch { /* best effort */ }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function defaultIsPidAlive(pid) {
|
|
139
|
+
try {
|
|
140
|
+
process.kill(pid, 0);
|
|
141
|
+
return true;
|
|
142
|
+
} catch (error) {
|
|
143
|
+
// EPERM means the pid exists but belongs to another user; treat as alive.
|
|
144
|
+
return error?.code === 'EPERM';
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function leaseIsStale(leasePath, { isPidAlive, ownPid }) {
|
|
149
|
+
let record;
|
|
150
|
+
try {
|
|
151
|
+
record = JSON.parse(readFileSync(leasePath, 'utf8'));
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (error?.code === 'ENOENT') return false; // released while we looked
|
|
154
|
+
return true; // unreadable/corrupt lease can never be released by its owner
|
|
155
|
+
}
|
|
156
|
+
const pid = record?.pid;
|
|
157
|
+
if (!Number.isInteger(pid) || pid <= 0) return true;
|
|
158
|
+
// A lease naming our own pid cannot be live: this process acquires at most
|
|
159
|
+
// one lease and caches it, so this is a leftover from a recycled pid.
|
|
160
|
+
if (pid === ownPid) return true;
|
|
161
|
+
return !isPidAlive(pid);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function tryCreateLease(leasePath, ownPid) {
|
|
165
|
+
let descriptor;
|
|
166
|
+
try {
|
|
167
|
+
descriptor = openSync(leasePath, 'wx', 0o600);
|
|
168
|
+
const record = {
|
|
169
|
+
pid: ownPid,
|
|
170
|
+
tool: basename(process.argv[1] || 'sogni-agent'),
|
|
171
|
+
acquiredAt: new Date().toISOString(),
|
|
172
|
+
};
|
|
173
|
+
writeFileSync(descriptor, `${JSON.stringify(record)}\n`, 'utf8');
|
|
174
|
+
return true;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
if (error?.code === 'EEXIST') return false;
|
|
177
|
+
throw error;
|
|
178
|
+
} finally {
|
|
179
|
+
if (descriptor !== undefined) closeSync(descriptor);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* One-time migration: the pre-pool single `app-id` file becomes slot-0 so an
|
|
185
|
+
* upgraded installation keeps its already-registered identity instead of
|
|
186
|
+
* burning a new one. The legacy file is removed so a stray old-CLI process
|
|
187
|
+
* cannot share (and steal) slot-0's connection.
|
|
188
|
+
*/
|
|
189
|
+
function migrateLegacyAppId(poolDir, legacyAppIdPath) {
|
|
190
|
+
if (!legacyAppIdPath) return;
|
|
191
|
+
let legacyValue;
|
|
192
|
+
try {
|
|
193
|
+
legacyValue = readPersistedAppId(legacyAppIdPath);
|
|
194
|
+
} catch {
|
|
195
|
+
return; // absent or corrupt legacy identity: nothing worth migrating
|
|
196
|
+
}
|
|
197
|
+
let descriptor;
|
|
198
|
+
try {
|
|
199
|
+
descriptor = openSync(join(poolDir, 'slot-0'), 'wx', 0o600);
|
|
200
|
+
writeFileSync(descriptor, `${legacyValue}\n`, 'utf8');
|
|
201
|
+
} catch (error) {
|
|
202
|
+
if (error?.code !== 'EEXIST') throw error;
|
|
203
|
+
return; // pool already initialized; leave the legacy file alone
|
|
204
|
+
} finally {
|
|
205
|
+
if (descriptor !== undefined) closeSync(descriptor);
|
|
206
|
+
}
|
|
207
|
+
try { unlinkSync(legacyAppIdPath); } catch { /* best effort */ }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function parsePoolMax(rawValue) {
|
|
211
|
+
const parsed = Number.parseInt(rawValue ?? '', 10);
|
|
212
|
+
return Number.isInteger(parsed) && parsed > 0 ? parsed : DEFAULT_APP_ID_POOL_MAX;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
let processLease = null;
|
|
216
|
+
let exitCleanupRegistered = false;
|
|
217
|
+
|
|
218
|
+
/** Release this process's pool lease (also used by tests to reset state). */
|
|
219
|
+
export function releaseSogniAppId() {
|
|
220
|
+
if (!processLease) return;
|
|
221
|
+
try { unlinkSync(processLease.leasePath); } catch { /* best effort */ }
|
|
222
|
+
processLease = null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function registerExitCleanup() {
|
|
226
|
+
if (exitCleanupRegistered) return;
|
|
227
|
+
exitCleanupRegistered = true;
|
|
228
|
+
process.on('exit', releaseSogniAppId);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function acquireSlotLease({ poolDir, poolMax, generateUuid, isPidAlive, ownPid, legacyAppIdPath }) {
|
|
232
|
+
mkdirSync(poolDir, { recursive: true, mode: 0o700 });
|
|
233
|
+
migrateLegacyAppId(poolDir, legacyAppIdPath);
|
|
234
|
+
for (let slot = 0; slot < poolMax; slot += 1) {
|
|
235
|
+
const slotPath = join(poolDir, `slot-${slot}`);
|
|
236
|
+
const appId = getOrCreateAppIdFile(slotPath, generateUuid);
|
|
237
|
+
const leasePath = `${slotPath}.lease`;
|
|
238
|
+
if (tryCreateLease(leasePath, ownPid)) return { appId, leasePath };
|
|
239
|
+
if (leaseIsStale(leasePath, { isPidAlive, ownPid })) {
|
|
240
|
+
try { unlinkSync(leasePath); } catch { /* another reclaimer won */ }
|
|
241
|
+
if (tryCreateLease(leasePath, ownPid)) return { appId, leasePath };
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const error = new Error(`All ${poolMax} Sogni app-ID slots in ${poolDir} are leased by live processes.`);
|
|
245
|
+
error.code = 'APP_ID_POOL_EXHAUSTED';
|
|
246
|
+
error.hint = 'Raise SOGNI_APP_ID_POOL_MAX, or give long-lived daemons their own stable SOGNI_APP_ID so they stay out of the shared pool.';
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Return the stable app ID for this process, creating identity state at most
|
|
252
|
+
* once per installation.
|
|
75
253
|
*
|
|
76
|
-
*
|
|
77
|
-
* `
|
|
254
|
+
* Resolution order:
|
|
255
|
+
* 1. `SOGNI_APP_ID` โ explicit pinned value (ephemeral/container homes and
|
|
256
|
+
* long-lived daemons that must keep one identity). No files touched.
|
|
257
|
+
* 2. `SOGNI_APP_ID_PATH` โ legacy single-file mode for callers that manage
|
|
258
|
+
* their own concurrency; behaves exactly like pre-pool releases.
|
|
259
|
+
* 3. Default โ lease a slot from the persistent pool so concurrent agent
|
|
260
|
+
* processes each hold a distinct stable ID (`SOGNI_APP_ID_POOL_DIR`,
|
|
261
|
+
* `SOGNI_APP_ID_POOL_MAX`). The lease is released on process exit and
|
|
262
|
+
* reclaimed via pid-liveness if the process dies uncleanly.
|
|
78
263
|
*/
|
|
79
264
|
export function getOrCreateSogniAppId({
|
|
80
265
|
appId = getEnv('SOGNI_APP_ID', { trim: true }),
|
|
81
|
-
appIdPath = getEnv('SOGNI_APP_ID_PATH', { trim: true })
|
|
266
|
+
appIdPath = getEnv('SOGNI_APP_ID_PATH', { trim: true }),
|
|
267
|
+
poolDir = getEnv('SOGNI_APP_ID_POOL_DIR', { trim: true }) || DEFAULT_APP_ID_POOL_DIR,
|
|
268
|
+
poolMax = parsePoolMax(getEnv('SOGNI_APP_ID_POOL_MAX', { trim: true })),
|
|
82
269
|
generateUuid = randomUUID,
|
|
270
|
+
isPidAlive = defaultIsPidAlive,
|
|
271
|
+
ownPid = process.pid,
|
|
272
|
+
legacyAppIdPath,
|
|
83
273
|
} = {}) {
|
|
84
274
|
if (appId) return validateAppId(appId, 'SOGNI_APP_ID');
|
|
85
275
|
|
|
86
|
-
|
|
87
|
-
|
|
276
|
+
if (appIdPath) {
|
|
277
|
+
const resolvedPath = expandHomePath(appIdPath);
|
|
88
278
|
try {
|
|
89
|
-
return
|
|
279
|
+
return getOrCreateAppIdFile(resolvedPath, generateUuid);
|
|
90
280
|
} catch (error) {
|
|
91
|
-
|
|
281
|
+
throw appIdPersistenceError(error, resolvedPath);
|
|
92
282
|
}
|
|
283
|
+
}
|
|
93
284
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
)
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
// once the short write window closes.
|
|
111
|
-
let lastError = error;
|
|
112
|
-
for (let attempt = 0; attempt < APP_ID_CREATE_RETRIES; attempt += 1) {
|
|
113
|
-
try {
|
|
114
|
-
return readPersistedAppId(resolvedPath);
|
|
115
|
-
} catch (readError) {
|
|
116
|
-
lastError = readError;
|
|
117
|
-
if (attempt + 1 < APP_ID_CREATE_RETRIES) waitForConcurrentWriter();
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
throw lastError;
|
|
121
|
-
} finally {
|
|
122
|
-
if (descriptor !== undefined) closeSync(descriptor);
|
|
123
|
-
// Do not strand an empty/corrupt identity if this process created the
|
|
124
|
-
// file but could not finish writing it.
|
|
125
|
-
if (createdFile) {
|
|
126
|
-
try {
|
|
127
|
-
readPersistedAppId(resolvedPath);
|
|
128
|
-
} catch {
|
|
129
|
-
try { unlinkSync(resolvedPath); } catch { /* best effort */ }
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
285
|
+
if (processLease) return processLease.appId;
|
|
286
|
+
const resolvedPoolDir = expandHomePath(poolDir);
|
|
287
|
+
if (legacyAppIdPath === undefined) {
|
|
288
|
+
// Only the real default pool inherits the real pre-pool identity file; a
|
|
289
|
+
// custom pool location must never consume (and delete) it as a side effect.
|
|
290
|
+
legacyAppIdPath = resolvedPoolDir === DEFAULT_APP_ID_POOL_DIR ? DEFAULT_APP_ID_PATH : null;
|
|
291
|
+
}
|
|
292
|
+
try {
|
|
293
|
+
processLease = acquireSlotLease({
|
|
294
|
+
poolDir: resolvedPoolDir,
|
|
295
|
+
poolMax,
|
|
296
|
+
generateUuid,
|
|
297
|
+
isPidAlive,
|
|
298
|
+
ownPid,
|
|
299
|
+
legacyAppIdPath,
|
|
300
|
+
});
|
|
133
301
|
} catch (error) {
|
|
134
|
-
throw appIdPersistenceError(error,
|
|
302
|
+
throw appIdPersistenceError(error, resolvedPoolDir);
|
|
303
|
+
}
|
|
304
|
+
registerExitCleanup();
|
|
305
|
+
return processLease.appId;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Diagnostic view of the pool for doctor/debug output. */
|
|
309
|
+
export function describeSogniAppIdPool({
|
|
310
|
+
poolDir = getEnv('SOGNI_APP_ID_POOL_DIR', { trim: true }) || DEFAULT_APP_ID_POOL_DIR,
|
|
311
|
+
isPidAlive = defaultIsPidAlive,
|
|
312
|
+
} = {}) {
|
|
313
|
+
const resolvedPoolDir = expandHomePath(poolDir);
|
|
314
|
+
let names;
|
|
315
|
+
try {
|
|
316
|
+
names = readdirSync(resolvedPoolDir);
|
|
317
|
+
} catch {
|
|
318
|
+
return { poolDir: resolvedPoolDir, slots: [] };
|
|
135
319
|
}
|
|
320
|
+
const slots = names
|
|
321
|
+
.filter((name) => /^slot-\d+$/.test(name))
|
|
322
|
+
.sort((a, b) => Number(a.slice(5)) - Number(b.slice(5)))
|
|
323
|
+
.map((name) => {
|
|
324
|
+
const slotPath = join(resolvedPoolDir, name);
|
|
325
|
+
let appId = null;
|
|
326
|
+
try { appId = readPersistedAppId(slotPath); } catch { /* corrupt slot */ }
|
|
327
|
+
let lease = null;
|
|
328
|
+
try {
|
|
329
|
+
lease = JSON.parse(readFileSync(`${slotPath}.lease`, 'utf8'));
|
|
330
|
+
lease.live = Number.isInteger(lease?.pid) && lease.pid > 0 && isPidAlive(lease.pid);
|
|
331
|
+
} catch { /* no lease */ }
|
|
332
|
+
return { slot: name, appId, lease };
|
|
333
|
+
});
|
|
334
|
+
return { poolDir: resolvedPoolDir, slots };
|
|
136
335
|
}
|
|
137
336
|
|
|
138
|
-
export { DEFAULT_APP_ID_PATH };
|
|
337
|
+
export { DEFAULT_APP_ID_PATH, DEFAULT_APP_ID_POOL_DIR };
|
package/version.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const PACKAGE_VERSION = '3.
|
|
1
|
+
export const PACKAGE_VERSION = '3.20.0';
|