@senpi/runtime 1.1.0-dev.runtime-phase-2.20260512102629 → 1.1.0-dev.runtime-phase-2.20260514134437

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.
Files changed (57) hide show
  1. package/dist/cli/senpi-cli-config.d.ts +15 -1
  2. package/dist/cli/senpi-cli-config.d.ts.map +1 -1
  3. package/dist/cli/senpi-cli-config.js +19 -2
  4. package/dist/cli/senpi-cli-config.js.map +1 -1
  5. package/dist/cli/senpi-commands.d.ts.map +1 -1
  6. package/dist/cli/senpi-commands.js +96 -0
  7. package/dist/cli/senpi-commands.js.map +1 -1
  8. package/dist/index.d.ts +21 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +84 -1
  11. package/dist/index.js.map +1 -1
  12. package/dist/runtime/auto-update/coordinator.d.ts +13 -0
  13. package/dist/runtime/auto-update/coordinator.d.ts.map +1 -1
  14. package/dist/runtime/auto-update/coordinator.js +14 -0
  15. package/dist/runtime/auto-update/coordinator.js.map +1 -1
  16. package/dist/skills-manager/bootstrap.d.ts +26 -0
  17. package/dist/skills-manager/bootstrap.d.ts.map +1 -0
  18. package/dist/skills-manager/bootstrap.js +86 -0
  19. package/dist/skills-manager/bootstrap.js.map +1 -0
  20. package/dist/skills-manager/constants.d.ts +31 -0
  21. package/dist/skills-manager/constants.d.ts.map +1 -0
  22. package/dist/skills-manager/constants.js +40 -0
  23. package/dist/skills-manager/constants.js.map +1 -0
  24. package/dist/skills-manager/coordinator.d.ts +336 -0
  25. package/dist/skills-manager/coordinator.d.ts.map +1 -0
  26. package/dist/skills-manager/coordinator.js +931 -0
  27. package/dist/skills-manager/coordinator.js.map +1 -0
  28. package/dist/skills-manager/fetcher.d.ts +28 -0
  29. package/dist/skills-manager/fetcher.d.ts.map +1 -0
  30. package/dist/skills-manager/fetcher.js +92 -0
  31. package/dist/skills-manager/fetcher.js.map +1 -0
  32. package/dist/skills-manager/index.d.ts +5 -0
  33. package/dist/skills-manager/index.d.ts.map +1 -0
  34. package/dist/skills-manager/index.js +4 -0
  35. package/dist/skills-manager/index.js.map +1 -0
  36. package/dist/skills-manager/state-store.d.ts +57 -0
  37. package/dist/skills-manager/state-store.d.ts.map +1 -0
  38. package/dist/skills-manager/state-store.js +129 -0
  39. package/dist/skills-manager/state-store.js.map +1 -0
  40. package/dist/skills-manager/types.d.ts +165 -0
  41. package/dist/skills-manager/types.d.ts.map +1 -0
  42. package/dist/skills-manager/types.js +2 -0
  43. package/dist/skills-manager/types.js.map +1 -0
  44. package/dist/skills-manager/utils.d.ts +63 -0
  45. package/dist/skills-manager/utils.d.ts.map +1 -0
  46. package/dist/skills-manager/utils.js +119 -0
  47. package/dist/skills-manager/utils.js.map +1 -0
  48. package/dist/skills-manager/version-manager.d.ts +146 -0
  49. package/dist/skills-manager/version-manager.d.ts.map +1 -0
  50. package/dist/skills-manager/version-manager.js +299 -0
  51. package/dist/skills-manager/version-manager.js.map +1 -0
  52. package/dist/skills-manager/writer.d.ts +28 -0
  53. package/dist/skills-manager/writer.d.ts.map +1 -0
  54. package/dist/skills-manager/writer.js +101 -0
  55. package/dist/skills-manager/writer.js.map +1 -0
  56. package/openclaw.plugin.json +27 -0
  57. package/package.json +3 -1
@@ -0,0 +1,336 @@
1
+ /**
2
+ * Central lifecycle manager for Senpi skills auto-update.
3
+ *
4
+ * Mirrors AutoUpdateCoordinator from src/runtime/auto-update/coordinator.ts
5
+ * for thread-safety (runToken + stopped + running triple) and uses the same
6
+ * injectable-dependency pattern for testability.
7
+ *
8
+ * Scheduling: this coordinator does not own a timer. Ticks fire from
9
+ * outside — currently from auto-update's `onTickComplete` hook in
10
+ * `src/index.ts`, which calls {@link SkillsUpdateCoordinator.runTickOnce}
11
+ * after every auto-update cycle. Skills-manager therefore inherits the
12
+ * `autoUpdate.pollIntervalMinutes` cadence and the same failure-backoff
13
+ * cadence.
14
+ *
15
+ * On start(): load persisted state, bootstrap any missing skills, mark
16
+ * health as healthy. The first tick fires on the next external invocation
17
+ * of `runTickOnce()`.
18
+ *
19
+ * Each tick: resolve skill names (configured list or discovery), check
20
+ * remote versions in parallel, auto-apply patch/minor, notify on major
21
+ * (deduped by version), skip on none/unknown.
22
+ */
23
+ import type { SkillsManagerSnapshot, SkillsUpdateCoordinatorOptions } from "./types.js";
24
+ export declare class SkillsUpdateCoordinator {
25
+ private readonly options;
26
+ private readonly store;
27
+ private operationChain;
28
+ private stopped;
29
+ private runToken;
30
+ private state;
31
+ private snapshot;
32
+ /** Latest remote version per skill name — populated during ticks. */
33
+ private latestVersions;
34
+ /**
35
+ * Local (pre-install) version per skill name — populated during ticks
36
+ * and manual RPC paths from `SkillVersionInfo.localVersion`. Used by the
37
+ * update-summary and major-available notifications to render the
38
+ * `prev → new` transition. Captured *before* the install runs so the
39
+ * value still reflects the on-disk version at the time the user chose to
40
+ * update; reading `state.skills[name].installedVersion` instead would
41
+ * surface the *post-install* version (recorded by `recordSuccess`) and
42
+ * collapse the transition to `vX -> vX`.
43
+ */
44
+ private previousVersions;
45
+ /**
46
+ * Cached short descriptions parsed from each skill's **remote** SKILL.md
47
+ * frontmatter — i.e. the description for the upcoming/current remote
48
+ * version, not whatever's installed locally. Used to enrich
49
+ * update-summary and major-available notifications with a one-line
50
+ * "About:" so the user gets context for the version the message is
51
+ * actually announcing.
52
+ *
53
+ * Populated during tick / updateSkill / updateAll alongside
54
+ * `latestVersions`; absent / null entries simply omit the line from the
55
+ * notification.
56
+ *
57
+ * Using the remote description (rather than the local one) keeps the
58
+ * notification description in sync with the version number it sits next
59
+ * to — e.g. when notifying about a major bump v1 → v2, the "About:" line
60
+ * reflects the v2 description, even if the v1 local copy on disk is
61
+ * stale or worded differently.
62
+ */
63
+ private skillDescriptions;
64
+ constructor(options: SkillsUpdateCoordinatorOptions);
65
+ /**
66
+ * Load persisted state and bootstrap missing skills. Does **not** schedule
67
+ * a poll loop — skills-manager runs off the auto-update coordinator's
68
+ * `onTickComplete` hook (see `src/index.ts`), which invokes
69
+ * {@link runTickOnce} on whatever cadence auto-update is configured with.
70
+ *
71
+ * Calling `start()` is still required even in the externally-scheduled
72
+ * mode so bootstrap, state load, and the "ready" snapshot all happen
73
+ * before the first external tick fires.
74
+ */
75
+ start(): Promise<void>;
76
+ /**
77
+ * Stop the coordinator: prevent any new ticks from being processed, and
78
+ * wait for any in-flight tick or manual RPC to finish so the caller can
79
+ * rely on no async work still touching state after `stop()` resolves.
80
+ *
81
+ * Since scheduling is now external (auto-update's `onTickComplete` hook),
82
+ * there's no internal timer to cancel — `stop()` just flips the `stopped`
83
+ * flag (which `runTickOnce` checks) and drains the serialised operation
84
+ * queue. The auto-update hook itself becomes a no-op for stopped
85
+ * coordinators since `runTickOnce` returns early.
86
+ */
87
+ stop(): Promise<void>;
88
+ /**
89
+ * Public one-shot tick entry point. Called externally on whatever cadence
90
+ * the host wires up — currently from the auto-update coordinator's
91
+ * `onTickComplete` hook in `src/index.ts`, which fires after each
92
+ * auto-update version check completes (success or failure).
93
+ *
94
+ * Replaces the previous internal `tick()` + `scheduleNext()` pair: the
95
+ * coordinator no longer owns a timer of its own. Manual RPCs
96
+ * (`senpi.skills.update`, `senpi.skills.updateAll`) remain independent of
97
+ * this path.
98
+ *
99
+ * Returns early as a no-op when:
100
+ * - The coordinator has been stopped (e.g. plugin teardown).
101
+ * - The runToken has rolled (e.g. a stop/start cycle invalidated this call).
102
+ * - The coordinator was disabled at start time (config.enabled === false).
103
+ *
104
+ * Tick errors are logged + recorded on the snapshot but never thrown —
105
+ * the caller (auto-update) must not have its own scheduling disturbed by
106
+ * a transient skills-manager failure.
107
+ */
108
+ runTickOnce(): Promise<void>;
109
+ /** Return the latest point-in-time snapshot for gateway RPCs. */
110
+ getSnapshot(): SkillsManagerSnapshot;
111
+ /**
112
+ * Force an immediate version check and apply for one named skill.
113
+ *
114
+ * Skips installation when the update kind is `"none"` (already current) or
115
+ * `"unknown"` (remote version could not be classified — e.g. the fetch
116
+ * failed or the remote `version` field is missing). Falling through on
117
+ * `"unknown"` would write `installedVersion: null` and lose tracking, so we
118
+ * treat it the same as `"none"` and return `updated: false`.
119
+ *
120
+ * @throws On network or install error so the gateway RPC can return ok=false.
121
+ */
122
+ updateSkill(skillName: string): Promise<{
123
+ updated: boolean;
124
+ version: string | null;
125
+ }>;
126
+ /**
127
+ * Force an immediate version check and apply for all discovered/configured skills.
128
+ * Individual failures do not abort the remaining skills.
129
+ *
130
+ * All skills share a single `git clone` — the repo is cloned once, every
131
+ * skill that needs an update is installed from that same checkout, then the
132
+ * checkout is cleaned up. A previous version called `updateSkill` in a loop,
133
+ * which caused one full clone per skill (N redundant clones for N skills);
134
+ * this mirrors the tick path's batching instead.
135
+ */
136
+ updateAll(): Promise<Array<{
137
+ name: string;
138
+ updated: boolean;
139
+ version: string | null;
140
+ error?: string;
141
+ }>>;
142
+ /**
143
+ * Run coordinator work in a global critical section so periodic ticks and
144
+ * manual RPC updates never interleave at await points.
145
+ */
146
+ private runSerialized;
147
+ /**
148
+ * Core tick logic: resolve skill names, check versions in parallel, apply
149
+ * policy per skill, then run a single clone for all skills needing update.
150
+ */
151
+ private runTick;
152
+ /**
153
+ * Clone the repo once and install every skill in `skillNames`.
154
+ * Per-skill errors are recorded in state without aborting the others.
155
+ * The update summary notification distinguishes successful installs from
156
+ * failures so the user can see exactly which skills need attention.
157
+ *
158
+ * If the clone itself fails we log and return without touching state or
159
+ * sending a summary — a full-clone failure is almost always a transient
160
+ * network/git issue that affects every skill equally, so attributing it
161
+ * per-skill would be misleading. The next tick retries.
162
+ */
163
+ private applyUpdates;
164
+ /**
165
+ * Clone the remote skills repo exactly once and install every skill in
166
+ * `skillNames` from that single checkout. Returns a per-skill outcome map:
167
+ * `{ version }` on success or `{ version: null, error }` on install failure.
168
+ *
169
+ * Throws only when the clone itself fails — per-skill install failures are
170
+ * captured in the map so callers can continue processing the rest.
171
+ *
172
+ * When `deps.cloneAndInstall` is supplied (unit tests), that injector is
173
+ * called in place of the real clone+install loop; any names missing from
174
+ * its returned map are treated as install failures.
175
+ */
176
+ private cloneAndInstallMany;
177
+ /**
178
+ * Record a successful install on in-memory skill state.
179
+ *
180
+ * Resets failure tracking — a successful end-to-end install is the only
181
+ * signal we treat as "operationally healthy." A successful version check
182
+ * alone is NOT sufficient (and intentionally does not call this), because:
183
+ *
184
+ * - The version check only proves the raw SKILL.md is reachable. It does
185
+ * not exercise the clone/copy/swap pipeline that actually keeps the skill
186
+ * working.
187
+ * - Clearing failure state on every check-success would wipe install-failure
188
+ * history mid-tick: a skill failing to install on every tick would still
189
+ * report `consecutiveFailures: 1` (instead of N) because each tick's
190
+ * check-success reset would precede the install-failure increment.
191
+ *
192
+ * Consequence: a skill that's perpetually up-to-date but had a one-off
193
+ * failure in the past will retain a non-zero `consecutiveFailures` until
194
+ * the next install. We accept that trade-off — surfacing a stale failure
195
+ * counter is preferable to silently wiping real install-failure history.
196
+ */
197
+ private recordSuccess;
198
+ /**
199
+ * Record a version-check failure on in-memory skill state.
200
+ *
201
+ * Shared by `runTick` and `updateAll` — both need to bump
202
+ * `consecutiveFailures` and capture the reason when `Promise.allSettled`
203
+ * surfaces a rejected check. `lastCheckedAt` is updated too so the snapshot
204
+ * doesn't look like the skill was never polled (it was — the poll failed).
205
+ */
206
+ private recordCheckFailure;
207
+ /**
208
+ * Apply one install outcome to in-memory skill state and return a compact
209
+ * success/failure result for the caller's bookkeeping.
210
+ */
211
+ private processInstallOutcome;
212
+ /**
213
+ * Apply a single skill update (used by the manual `updateSkill` RPC path).
214
+ * Returns the installed version.
215
+ *
216
+ * Delegates to {@link cloneAndInstallMany} so the single-skill and batch
217
+ * paths share one implementation of clone+install. The per-skill install
218
+ * error is re-thrown here so `updateSkill` can surface it to the RPC
219
+ * caller via a rejected promise (batch path captures it instead).
220
+ */
221
+ private applyUpdate;
222
+ /**
223
+ * Format the per-skill nested-bullet block used in both notification
224
+ * shapes. Renders the structure suggested in PR #78 review feedback:
225
+ *
226
+ * **skill-name**
227
+ * • v1.0.0 → v2.0.0 (or "v1.0.0 (install failed)" when failed)
228
+ * • description: … (omitted when no remote description)
229
+ * • error: … (only on failure, replaces the description)
230
+ *
231
+ * **Why markdown, not HTML?**
232
+ * Openclaw's Telegram channel (`src/telegram/send.ts:sendMessageTelegram`)
233
+ * pipes outbound text through `renderTelegramHtmlText`, which defaults
234
+ * `textMode` to `"markdown"` — i.e. it expects the source to be markdown
235
+ * and runs `markdownToTelegramHtml` over it before handing the converted
236
+ * HTML to the Bot API with `parse_mode: "HTML"`. The `message` tool
237
+ * (which is what this coordinator's `notify` callback ultimately hits via
238
+ * `/tools/invoke`) does not expose `textMode`, so HTML written by us is
239
+ * treated as literal characters by the markdown step and rendered as
240
+ * visible `<b>` text in the chat (the actual production bug this docstring
241
+ * fixes — confirmed against a live notification screenshot).
242
+ *
243
+ * Using markdown source (`**bold**`, backticks) instead means the channel's
244
+ * markdown→HTML converter produces the correct `<b>` / `<code>` HTML
245
+ * payload and Telegram renders the formatting properly.
246
+ *
247
+ * @param skillName Skill identifier (rendered bold via `**name**`).
248
+ * @param toVersion Resolved upgraded-to version. `null` for failure rows
249
+ * where no install completed; renders as
250
+ * "vPREV (install failed)".
251
+ * @param errorReason Optional failure reason. When provided, replaces
252
+ * the description line with `error: …`.
253
+ */
254
+ private formatSkillBlock;
255
+ /**
256
+ * Send a one-way Telegram notification for a pending major update, deduped
257
+ * by version.
258
+ *
259
+ * The dedup key (`lastNotifiedMajorVersion`) is only set after the notify
260
+ * call resolves successfully. If the notification fails (Telegram API down,
261
+ * transient network error, etc.) the key is left unchanged so the next tick
262
+ * retries delivery instead of silently suppressing it forever.
263
+ */
264
+ private handleMajorAvailable;
265
+ /**
266
+ * Send an aggregated notification summarising the outcome of this tick's
267
+ * updates. Successful installs and failures are shown in separate sections
268
+ * so the user can tell which skills still need attention. Sections with no
269
+ * entries are omitted. If neither section has entries, no notification is
270
+ * sent.
271
+ *
272
+ * Format (markdown source — see `formatSkillBlock` for why markdown
273
+ * rather than HTML):
274
+ *
275
+ * Senpi skills update:
276
+ *
277
+ * Updated:
278
+ *
279
+ * **skill-a**
280
+ * • v1.0.0 → v1.0.1
281
+ * • description: …
282
+ *
283
+ * **skill-b**
284
+ * • v2.0.0 → v2.0.1
285
+ * • description: …
286
+ *
287
+ * Failed to update:
288
+ *
289
+ * **skill-c**
290
+ * • v1.0.0 (install failed)
291
+ * • error: clone failed: …
292
+ */
293
+ private sendUpdateSummary;
294
+ /**
295
+ * Return the version-check function for this coordinator: either the
296
+ * injected `deps.checkSkillVersion` (unit tests) or the real
297
+ * `checkSkillVersion` with the default `fetchImpl`.
298
+ *
299
+ * Extracted so the three call sites (`updateSkill`, `updateAll`, `runTick`)
300
+ * don't repeat the same resolve-and-wrap lambda — a change to the real
301
+ * `checkSkillVersion` signature now only has to land here.
302
+ */
303
+ private resolveCheckFn;
304
+ /**
305
+ * Resolve the list of skill names to check this tick.
306
+ * Uses `config.skills` when non-empty; otherwise discovers all immediate
307
+ * subdirectories across every dir in the `skillsDir` search path,
308
+ * deduped by name and sorted.
309
+ *
310
+ * Per-dir read errors (other than ENOENT) are logged but do not abort the
311
+ * scan — a misconfigured secondary dir cannot mask skills present in the
312
+ * primary.
313
+ */
314
+ private resolveSkillNames;
315
+ /**
316
+ * Build the point-in-time snapshot returned by {@link getSnapshot}.
317
+ *
318
+ * The snapshot is the single serialisable view of the coordinator's state
319
+ * consumed by gateway RPCs (`senpi.skills.status`) and any health UI. It
320
+ * fuses three data sources:
321
+ * 1. Per-skill persisted state (`this.state.skills`) — installed version,
322
+ * last check time, consecutive failures, last failure reason.
323
+ * 2. In-memory `this.latestVersions` map — the most recent remote version
324
+ * observed during a version check (may not yet be installed).
325
+ * 3. Caller-supplied lifecycle metadata — `health`, `lastAction`,
326
+ * `lastError` describing the most recent tick / manual RPC outcome.
327
+ *
328
+ * This is called at every state-mutating boundary (start, tick complete,
329
+ * tick error, manual update complete, manual update skipped, manual update
330
+ * clone failed, stop/disable) so the next RPC response reflects the latest
331
+ * state rather than stale pre-action data.
332
+ */
333
+ private buildSnapshot;
334
+ private persistState;
335
+ }
336
+ //# sourceMappingURL=coordinator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coordinator.d.ts","sourceRoot":"","sources":["../../src/skills-manager/coordinator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAQH,OAAO,KAAK,EAMV,qBAAqB,EAGrB,8BAA8B,EAC/B,MAAM,YAAY,CAAC;AAIpB,qBAAa,uBAAuB;IAwCtB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAvCpC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA0B;IAChD,OAAO,CAAC,cAAc,CAAoC;IAC1D,OAAO,CAAC,OAAO,CAAQ;IACvB,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,KAAK,CAAyD;IACtE,OAAO,CAAC,QAAQ,CAAwB;IACxC,qEAAqE;IACrE,OAAO,CAAC,cAAc,CAAoC;IAC1D;;;;;;;;;OASG;IACH,OAAO,CAAC,gBAAgB,CAAoC;IAC5D;;;;;;;;;;;;;;;;;OAiBG;IACH,OAAO,CAAC,iBAAiB,CAAoC;gBAEhC,OAAO,EAAE,8BAA8B;IAKpE;;;;;;;;;OASG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA4B5B;;;;;;;;;;OAUG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAW3B;;;;;;;;;;;;;;;;;;;OAmBG;IACG,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAmBlC,iEAAiE;IACjE,WAAW,IAAI,qBAAqB;IAMpC;;;;;;;;;;OAUG;IACG,WAAW,CACf,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IA2FxD;;;;;;;;;OASG;IACG,SAAS,IAAI,OAAO,CACxB,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAClF;IAuID;;;OAGG;YACW,aAAa;IAwB3B;;;OAGG;YACW,OAAO;IAsFrB;;;;;;;;;;OAUG;YACW,YAAY;IAsC1B;;;;;;;;;;;OAWG;YACW,mBAAmB;IA4DjC;;;;;;;;;;;;;;;;;;;OAmBG;IACH,OAAO,CAAC,aAAa;IAQrB;;;;;;;OAOG;IACH,OAAO,CAAC,kBAAkB;IAU1B;;;OAGG;IACH,OAAO,CAAC,qBAAqB;IAmB7B;;;;;;;;OAQG;YACW,WAAW;IAmDzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA+BG;IACH,OAAO,CAAC,gBAAgB;IAwBxB;;;;;;;;OAQG;YACW,oBAAoB;IAuClC;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;YACW,iBAAiB;IAgC/B;;;;;;;;OAQG;IACH,OAAO,CAAC,cAAc;IAYtB;;;;;;;;;OASG;YACW,iBAAiB;IAqC/B;;;;;;;;;;;;;;;;;OAiBG;IACH,OAAO,CAAC,aAAa;YA2BP,YAAY;CAS3B"}