@logictan/dsh-stitch-designer 2.1.2 → 2.1.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/lib/index.js CHANGED
@@ -41,7 +41,7 @@ import { createCanvasClient } from './canvas.js';
41
41
  import { createStitchClient } from './stitch.js';
42
42
  import { createInvoker } from './conventions.js';
43
43
  import { SECTION_NAME, SECTION_ORDER, STITCH_GUIDANCE } from './guidance.js';
44
- import { createOutput, createToolDefinition, publicToolName, withoutWithdrawn } from './tools.js';
44
+ import { createOutput, createToolDefinition, publicToolName, retryWithBackoff, withoutWithdrawn } from './tools.js';
45
45
 
46
46
  /** Plugin row id; must equal the row id in `cordis.patch.yml`. */
47
47
  export const name = 'stitch-designer';
@@ -61,6 +61,26 @@ export const SERVER_NAME = 'stitch';
61
61
  /** The prefix every tool this plugin registers carries, announcement filter included. */
62
62
  const PUBLIC_PREFIX = `mcp__${SERVER_NAME}__`;
63
63
 
64
+ /**
65
+ * How the boot-time `tools/list` is retried before the tools are given up on.
66
+ *
67
+ * The definitions are not shipped statically — they ARE the upstream catalogs —
68
+ * so registration happens only after a successful `tools/list`, and that fetch
69
+ * is the one thing that can fail transiently at boot: the credential store may
70
+ * not be live yet on a cold start, or the network hiccups. The credential-update
71
+ * event covers the "key set later" case, but a key already present at boot never
72
+ * fires it, so without a self-retry a single failed fetch left the tools
73
+ * unregistered for the whole process lifetime while the system prompt still
74
+ * claimed they existed — the model then called `mcp__stitch__*` and got
75
+ * "unknown tool" (observed live 2026-11-13: 18 tools reachable via the API, yet
76
+ * none registered because the one boot fetch failed).
77
+ *
78
+ * Bounded exponential backoff, then give up with a warning: an unrecoverable
79
+ * failure (wrong key, Stitch down) must not spin forever. The credential-update
80
+ * event remains the recovery path after that point.
81
+ */
82
+ const SYNC_RETRY_DELAYS_MS = [500, 1000, 2000, 4000, 8000];
83
+
64
84
  /**
65
85
  * Register the Stitch tools and announce the design conventions.
66
86
  *
@@ -109,8 +129,9 @@ export function apply(ctx) {
109
129
  toolsCtx.effect(() => {
110
130
  let disposed = false;
111
131
  let disposers = [];
132
+ let cancelRetry;
112
133
 
113
- const sync = async () => {
134
+ const registerListed = async () => {
114
135
  const listed = await stitch.listTools();
115
136
  if (disposed) return;
116
137
  const next = listed.map((tool) =>
@@ -127,17 +148,32 @@ export function apply(ctx) {
127
148
  toolsCtx.logger.info(`stitch-designer: registered ${disposers.length} mcp__${SERVER_NAME}__* tools`);
128
149
  };
129
150
 
130
- const report = (error) => {
131
- if (disposed) return;
132
- toolsCtx.logger.warn(
133
- `stitch-designer: the mcp__${SERVER_NAME}__* tools are not registered yet (${error?.message ?? error})`,
134
- );
151
+ // One attempt plus bounded backoff, so the boot fetch that failed
152
+ // transiently (credential store not live yet, network hiccup) heals
153
+ // itself instead of leaving the tools missing for the process lifetime.
154
+ // A failure that persists through every retry is logged once and left to
155
+ // the credential-update event; an unrecoverable one (wrong key) stops
156
+ // here rather than spinning. Each new run cancels any pending backoff so
157
+ // a mid-flight credential update cannot race an in-progress retry.
158
+ const syncWithRetry = () => {
159
+ cancelRetry?.();
160
+ cancelRetry = retryWithBackoff(registerListed, SYNC_RETRY_DELAYS_MS, {
161
+ shouldStop: () => disposed,
162
+ onRetry: (error, delay) =>
163
+ toolsCtx.logger.warn(
164
+ `stitch-designer: the mcp__${SERVER_NAME}__* tools are not registered yet (${error?.message ?? error}); retrying in ${delay}ms`,
165
+ ),
166
+ onGiveUp: (error, attempts) =>
167
+ toolsCtx.logger.warn(
168
+ `stitch-designer: the mcp__${SERVER_NAME}__* tools are not registered after ${attempts} attempts (${error?.message ?? error}); set or fix the API key to retry.`,
169
+ ),
170
+ });
135
171
  };
136
172
 
137
- sync().catch(report);
173
+ syncWithRetry();
138
174
  const offCredential = toolsCtx.on('credentials/reference-updated', (ref) => {
139
175
  if (ref !== API_KEY_REF || disposers.length > 0) return;
140
- sync().catch(report);
176
+ syncWithRetry();
141
177
  });
142
178
 
143
179
  // `remove_screens_from_canvas` is local, not upstream: Stitch has no
@@ -189,6 +225,7 @@ export function apply(ctx) {
189
225
 
190
226
  return () => {
191
227
  disposed = true;
228
+ cancelRetry?.();
192
229
  offCredential();
193
230
  removeScreenDispose();
194
231
  for (const dispose of disposers) dispose();
@@ -201,8 +238,9 @@ export function apply(ctx) {
201
238
  // The withdrawn half of the toolset (see `ANNOUNCED_TOOLS`): registered and
202
239
  // executable, absent from every request's tool list. Filtering by the public
203
240
  // name prefix rather than by a captured registration set keeps this correct
204
- // when `tools/list` fails at boot and the tools appear later on a credential
205
- // update — the names are the contract, the set of them is not.
241
+ // when `tools/list` fails at boot and the tools appear later — either
242
+ // through the boot retry or a credential update — the names are the
243
+ // contract, the set of them is not.
206
244
  ctx.on('system-prompt/assemble', async (_assembly, _context, next) => {
207
245
  const assembly = await next();
208
246
  const tools = withoutWithdrawn(assembly.tools, PUBLIC_PREFIX);
package/lib/tools.js CHANGED
@@ -267,3 +267,61 @@ export function createToolDefinition(options) {
267
267
  },
268
268
  };
269
269
  }
270
+
271
+ /**
272
+ * Run one async attempt, and on failure schedule bounded retries.
273
+ *
274
+ * The tool list this plugin registers comes from Stitch's own `tools/list`, so
275
+ * registration is only possible after that fetch succeeds — and that fetch is
276
+ * the one thing that can fail transiently at boot (credential store not live
277
+ * yet, network hiccup). A single failed fetch used to leave the tools missing
278
+ * for the whole process lifetime, even though the system prompt still announced
279
+ * them, so the model called `mcp__stitch__*` and got "unknown tool". This is the
280
+ * bounded self-heal: attempt now, and on rejection wait `delays[0]`, retry, wait
281
+ * `delays[1]`, and so on, giving up after the delays run out.
282
+ *
283
+ * Exported and dependency-injected (`schedule`) so the behavior is testable
284
+ * without a live endpoint or real timers.
285
+ *
286
+ * @param attempt - the work; its rejection is what triggers a retry.
287
+ * @param delays - backoff in milliseconds before each retry, in order.
288
+ * @param options - `shouldStop` checked before every retry; `onRetry`/`onGiveUp`
289
+ * notifications; `schedule` timer function that MUST return a canceller.
290
+ * @returns a canceller that stops any pending retry.
291
+ */
292
+ export function retryWithBackoff(attempt, delays, options = {}) {
293
+ const {
294
+ shouldStop = () => false,
295
+ onRetry,
296
+ onGiveUp,
297
+ // Normalized to return a canceller. Bare `setTimeout` returns a Timeout
298
+ // object, and the teardown path calls the return value — returning the raw
299
+ // timeout would throw "cancelScheduled is not a function" when the effect is
300
+ // unloaded during a backoff window.
301
+ schedule = (fn, delay) => {
302
+ const timeout = setTimeout(fn, delay);
303
+ return () => clearTimeout(timeout);
304
+ },
305
+ } = options;
306
+ let cancelled = false;
307
+ let cancelScheduled;
308
+
309
+ const run = (index) => {
310
+ attempt().catch((error) => {
311
+ if (cancelled || shouldStop()) return;
312
+ if (index >= delays.length) {
313
+ onGiveUp?.(error, index + 1);
314
+ return;
315
+ }
316
+ const delay = delays[index];
317
+ onRetry?.(error, delay);
318
+ cancelScheduled = schedule(() => run(index + 1), delay);
319
+ });
320
+ };
321
+
322
+ run(0);
323
+ return () => {
324
+ cancelled = true;
325
+ cancelScheduled?.();
326
+ };
327
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logictan/dsh-stitch-designer",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "DSH Stitch designer: Google Stitch AI UI generation as native mcp__stitch__* tools, plus the design conventions that keep one code project bound to one Stitch project. DSH Stitch 界面设计子插件。",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",