@slatesvideo/shared 0.7.8 → 0.7.9

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.
@@ -24,7 +24,10 @@ export interface ServedBrain {
24
24
  export declare const BRAIN_CACHE: string;
25
25
  /** The brain this process holds now. */
26
26
  export declare function servedBrain(): ServedBrain;
27
- /** Every skill an agent can read here: the public card and router, then the served brain. */
27
+ /**
28
+ * Every skill an agent can read here: the public card and router, then the served brain. One object per served
29
+ * file set, so what is built from it once (My brain's duplicate index, brain-check.ts) is found again.
30
+ */
28
31
  export declare function brainFiles(): Readonly<Record<string, string>>;
29
32
  interface CloudGet {
30
33
  get<T>(path: string): Promise<T>;
@@ -34,7 +37,8 @@ interface CloudGet {
34
37
  /**
35
38
  * Bring the brain up to the account's tiers and the approved version, one request in flight and at most every
36
39
  * REFRESH_EVERY_MS unless forced. A failure keeps what is held (the cache works offline) and records why; a folder
37
- * source never refreshes.
40
+ * source never refreshes. A sign-out during it wins: the credential it started with still answers the server (a
41
+ * sign-out deletes it locally only), so what it fetched is dropped, neither held nor written to the cache.
38
42
  */
39
43
  export declare function refreshServedBrain(cloud: () => CloudGet, { force }?: {
40
44
  force?: boolean | undefined;
@@ -70,13 +70,23 @@ else
70
70
  ({ state, held } = startingCache());
71
71
  let checkedAt = 0;
72
72
  let inflight;
73
+ /** Bumped by a sign-out or a source change: a refresh started before it drops what it fetched. */
74
+ let generation = 0;
73
75
  /** The brain this process holds now. */
74
76
  export function servedBrain() {
75
77
  return state;
76
78
  }
77
- /** Every skill an agent can read here: the public card and router, then the served brain. */
79
+ let merged = null;
80
+ /**
81
+ * Every skill an agent can read here: the public card and router, then the served brain. One object per served
82
+ * file set, so what is built from it once (My brain's duplicate index, brain-check.ts) is found again.
83
+ */
78
84
  export function brainFiles() {
79
- return state.files === EMPTY.files ? SKILLS : { ...SKILLS, ...state.files };
85
+ if (state.files === EMPTY.files)
86
+ return SKILLS;
87
+ if (merged?.files !== state.files)
88
+ merged = { files: state.files, all: { ...SKILLS, ...state.files } };
89
+ return merged.all;
80
90
  }
81
91
  /** Write what this process holds to BRAIN_CACHE, tagged with the credential that fetched it. */
82
92
  function writeCache(key) {
@@ -98,14 +108,19 @@ function writeCache(key) {
98
108
  /**
99
109
  * Bring the brain up to the account's tiers and the approved version, one request in flight and at most every
100
110
  * REFRESH_EVERY_MS unless forced. A failure keeps what is held (the cache works offline) and records why; a folder
101
- * source never refreshes.
111
+ * source never refreshes. A sign-out during it wins: the credential it started with still answers the server (a
112
+ * sign-out deletes it locally only), so what it fetched is dropped, neither held nor written to the cache.
102
113
  */
103
114
  export function refreshServedBrain(cloud, { force = false } = {}) {
104
115
  if (state.source === 'dir')
105
116
  return Promise.resolve(state);
106
117
  if (!force && Date.now() - checkedAt < REFRESH_EVERY_MS)
107
118
  return Promise.resolve(state);
108
- inflight ??= (async () => {
119
+ if (inflight)
120
+ return inflight;
121
+ const started = generation;
122
+ const stale = () => started !== generation;
123
+ const run = (async () => {
109
124
  let timer;
110
125
  const timeout = (p) => Promise.race([p, new Promise((_, reject) => { timer = setTimeout(() => reject(new Error('the brain check timed out')), REFRESH_TIMEOUT_MS); })])
111
126
  .finally(() => clearTimeout(timer));
@@ -114,12 +129,16 @@ export function refreshServedBrain(cloud, { force = false } = {}) {
114
129
  const client = cloud();
115
130
  key = client.credentialKey ?? cliKey();
116
131
  const index = await timeout(client.get('/brain'));
132
+ if (stale())
133
+ return state;
117
134
  const same = (b) => index.version === b.version && index.tiers.join() === b.tiers.join() && (index.account ?? null) === (b.account ?? null);
118
135
  if (held && same(held))
119
136
  state = { ...held, source: 'server' };
120
137
  held = null;
121
138
  if (!same(state)) {
122
139
  const bundle = await timeout(client.get('/brain/bundle'));
140
+ if (stale())
141
+ return state;
123
142
  if (!bundle?.files || typeof bundle.files !== 'object')
124
143
  throw new Error('the brain bundle was malformed');
125
144
  state = { version: bundle.version, tiers: bundle.tiers, files: bundle.files, locked: bundle.locked ?? [], source: 'server', account: bundle.account ?? null };
@@ -135,6 +154,9 @@ export function refreshServedBrain(cloud, { force = false } = {}) {
135
154
  checkedAt = Date.now();
136
155
  }
137
156
  catch (error) {
157
+ // Signed out meanwhile: the sign-out already set what is held, the cache and the next check.
158
+ if (stale())
159
+ return state;
138
160
  // Offline or signed out: the held brain stands, and the next try waits a minute, not a full interval.
139
161
  checkedAt = Date.now() - REFRESH_EVERY_MS + 60_000;
140
162
  const code = error;
@@ -165,9 +187,12 @@ export function refreshServedBrain(cloud, { force = false } = {}) {
165
187
  })().finally(() => {
166
188
  // Cleared after the assignment: a refresh with nothing to await (no login, so `cloud()` throws at once) used to
167
189
  // clear it inside the body, before `??=` stored the settled promise, and every later call got that first answer.
168
- inflight = undefined;
190
+ // Only its own: a sign-out let a newer refresh start, which this one must not orphan.
191
+ if (inflight === run)
192
+ inflight = undefined;
169
193
  });
170
- return inflight;
194
+ inflight = run;
195
+ return run;
171
196
  }
172
197
  /**
173
198
  * Signing out: drop the served brain in this process, and the cache when the login signing out received it. The
@@ -181,6 +206,9 @@ export function clearServedBrain(signingOut = 'desktop') {
181
206
  state = EMPTY;
182
207
  held = null;
183
208
  checkedAt = 0;
209
+ // A refresh in flight belongs to the account signing out: it drops its answer, and the next one starts afresh.
210
+ generation++;
211
+ inflight = undefined;
184
212
  const cli = cliKey();
185
213
  const cliReceived = !!cli && fromCache()?.token === cli;
186
214
  if (cliReceived === (signingOut === 'cli'))
@@ -197,6 +225,8 @@ export function useBrainSource(dir) {
197
225
  ({ state, held } = startingCache());
198
226
  }
199
227
  checkedAt = 0;
228
+ generation++;
229
+ inflight = undefined;
200
230
  return state;
201
231
  }
202
232
  //# sourceMappingURL=served.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slatesvideo/shared",
3
- "version": "0.7.8",
3
+ "version": "0.7.9",
4
4
  "description": "Shared operations layer for the Slates MCP server and CLI: auth, cloud/desktop clients, and the single tool surface both consume. Most users want @slatesvideo/mcp-server or @slatesvideo/cli instead.",
5
5
  "license": "MIT",
6
6
  "type": "module",