@valbuild/mcp 0.150.23 → 0.151.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/CHANGELOG.md CHANGED
@@ -1,5 +1,76 @@
1
1
  # @valbuild/mcp
2
2
 
3
+ ## 0.151.0
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1006](https://github.com/valbuild/val/pull/1006) [`346e6fd`](https://github.com/valbuild/val/commit/346e6fdc8e242bf826ab66a729eacd9f8d4be1a6) Thanks [@freekh](https://github.com/freekh)! - **Draft pages read what was edited, not the whole site.** A page in draft
8
+ mode used to make Val's server read every module, apply every pending
9
+ change, and preview and validate the whole project on every request -- and
10
+ then ask again for each `.jsonValues()` entry it showed. Now there is one
11
+ copy of the draft per process, made once per change and shared by every
12
+ editor and every request:
13
+
14
+ - A module nobody edited costs nothing: the page reads its own build, as a
15
+ visitor's does.
16
+ - A request asks one cheap question -- has anything changed? -- once, and
17
+ the draft is only re-read when it has. An edit is applied once, to the
18
+ version before it, however many pages read it afterwards.
19
+ - `fetchVal`, `fetchValKey`, `fetchValRoute`, `fetchValRouteUrl` and
20
+ `fetchValDraft` (Next.js and TanStack Start alike) and Val's API routes
21
+ read that one copy, so a save through the API is seen by every reader at
22
+ once. Before, each kept its own, and a publish moved only the copy of the
23
+ server that handled it: in a host that keeps its content in memory, a
24
+ draft page read through `fetchValKey` or `fetchValRoute` went on showing
25
+ the content from before the publish.
26
+ - The MCP tools (`initValMcp`) read the same copy in local development and
27
+ on Val's hosting, where Val's options come from your config and the
28
+ environment. `initValMcp` takes no patch store or source files of its
29
+ own, so in a host that keeps its content in memory it keeps a copy of its
30
+ own, as before.
31
+ - If `initValServer` and `initValContent` (or `initValRsc`) are given
32
+ different options for the same `val.modules` (a different patch store,
33
+ say), they keep separate copies, and Val logs a warning saying which
34
+ options differ.
35
+
36
+ On a site of 20 MB of content a draft read went from about half a second per
37
+ request to well under a millisecond once the draft is in memory, and the
38
+ memory it holds grows with what was edited rather than with the size of the
39
+ site. Hosted sites in proxy mode ask the content service one small question
40
+ per request instead of reading every pending change; with a content service
41
+ that predates it, Val reads the pending changes as before.
42
+
43
+ `ValDraft` (what `fetchValDraft` returns and `<ValProvider draft>` takes)
44
+ has a new optional `versions` field, filled in by the server: the page uses
45
+ it to tell that what the Studio sends is what it already shows, without
46
+ comparing the content. Nothing to change in your app.
47
+
48
+ **Changed:** `PUT /api/val/sources/~` no longer computes previews or
49
+ validates the content (the Studio does both itself, and `/api/val/validate`
50
+ checks a module on request); its `validate_sources` and
51
+ `validate_binary_files` flags are accepted and ignored.
52
+
53
+ **Deprecated, and removed in the next release.** These stay for one release
54
+ only so that a Studio, a page or a server of the previous release keeps
55
+ working while a deploy rolls out. Nothing in this release uses them; if
56
+ your own code calls any of them, move off it now:
57
+
58
+ - `PUT /api/val/sources/~`, the whole route. This release's Studio reads
59
+ `GET /api/val/manifest` and `GET /api/val/modules/~<path>`, and the draft
60
+ readers and MCP read in-process.
61
+ - The `source` field of the overlay's `source-update` event and the canvas's
62
+ `sourceUpdate` message, and a page's support for an event that carries
63
+ only `source` (this release sends `read` beside it).
64
+ - `ValDraft.base`: not sent and not read; the type stays one release so
65
+ code written against the previous one still compiles.
66
+ - `/api/val/stat`'s `sourcesSha` and `schemaSha`: the next release's Studio
67
+ stops depending on them, and the release after it stops sending them.
68
+
69
+ - Updated dependencies [[`33c8b80`](https://github.com/valbuild/val/commit/33c8b80f877f1c3bec3cb61ef41dea9d2efcf908), [`59faf33`](https://github.com/valbuild/val/commit/59faf332418af836617e09150fb89007e468394e), [`abf6148`](https://github.com/valbuild/val/commit/abf6148a255abe795620fede66006462bccb8322), [`1f08b65`](https://github.com/valbuild/val/commit/1f08b6517adeac27fd9e43c7bf726976daefee3b), [`a325152`](https://github.com/valbuild/val/commit/a325152856a881faec84c112fcf226828739f563), [`f5110c9`](https://github.com/valbuild/val/commit/f5110c9aa2a0a6ff0586e9f0f2a29eb1baef557d), [`04bae7e`](https://github.com/valbuild/val/commit/04bae7e54e6b191fc70f45e6f10bbce820b6069c), [`4fd35f3`](https://github.com/valbuild/val/commit/4fd35f321fe510cfb5b9b679ca14ce2b9e852d40), [`346e6fd`](https://github.com/valbuild/val/commit/346e6fdc8e242bf826ab66a729eacd9f8d4be1a6), [`b0ad7f4`](https://github.com/valbuild/val/commit/b0ad7f496f2dc0384b8df847b1d77becf617b7bf), [`3aff85e`](https://github.com/valbuild/val/commit/3aff85e9a40cb0879285b86fd41fa8167db1bb5e), [`809873c`](https://github.com/valbuild/val/commit/809873cd7dd2c17e261b7e92716397a6bb4d6e56), [`560abd9`](https://github.com/valbuild/val/commit/560abd9df36e52e63b6592b03a8040396320df00), [`be276e7`](https://github.com/valbuild/val/commit/be276e76f5849fcc421398abdb7e114f3de4d3e8), [`4dc1481`](https://github.com/valbuild/val/commit/4dc1481cfb7cad4072bdbeaeb51b2b2ad43a9f5d), [`4edd384`](https://github.com/valbuild/val/commit/4edd384cf04a25836df77aae6d88378f8750a761)]:
70
+ - @valbuild/server@0.151.0
71
+ - @valbuild/shared@0.151.0
72
+ - @valbuild/core@0.151.0
73
+
3
74
  ## 0.150.23
4
75
 
5
76
  ### Patch Changes
@@ -24,15 +24,15 @@ export declare function createValTools(valModules: ValModules, options: ValTools
24
24
  */
25
25
  extraTools?: ValToolImpl[]): ValTools;
26
26
  /**
27
- * The content as the caller should see it, loaded once per call.
27
+ * The content as the caller should see it: the head's (`ValHead.tree`).
28
28
  *
29
29
  * Pending patches are applied, because an agent looking at a project mid-edit
30
30
  * should see what the Studio would show rather than the last published state.
31
- *
32
- * Deliberately not cached across calls. In fs mode a save recomputes the base
33
- * sha within the same process, so a cached view would go stale silently — and
34
- * the cost of being wrong here is an agent writing a patch against content that
35
- * has already moved.
31
+ * The head re-reads the chain only when it moved (`seq`), and makes each
32
+ * patched module once per version, so a call after nothing changed costs one
33
+ * "has it moved?" -- and a call after a write (a save in fs mode moves the
34
+ * base too) sees it, so an agent never writes against content that has
35
+ * already moved.
36
36
  *
37
37
  * Exported for `toolsFixture`, not for consumers: `tools/index.ts` re-exports by
38
38
  * name, so this stays inside the package. A test that assembled its own state
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var asyncToGenerator = require('./asyncToGenerator-c3823d62.cjs.dev.js');
6
6
  var zod = require('zod');
7
- var server = require('@valbuild/server');
7
+ var internal$1 = require('@valbuild/server/internal');
8
8
  var core = require('@valbuild/core');
9
9
  var internal = require('@valbuild/shared/internal');
10
10
  var minimatch = require('minimatch');
@@ -13,6 +13,7 @@ var patch = require('@valbuild/core/patch');
13
13
  var fp = require('@valbuild/core/fp');
14
14
  var fs = require('node:fs/promises');
15
15
  var path = require('node:path');
16
+ var server = require('@valbuild/server');
16
17
 
17
18
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
18
19
 
@@ -1865,7 +1866,13 @@ var _excluded = ["handler"],
1865
1866
  */
1866
1867
  function createValTools(valModules, options) {
1867
1868
  var extraTools = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
1868
- var resolveOps = createOpsResolver(valModules, options);
1869
+ /*
1870
+ * The runtime every reader in this process shares (`valRuntime`): the API's,
1871
+ * the draft readers' and this registry's, so a write through any of them
1872
+ * is read by all, and the content is the head's -- made once per version,
1873
+ * not rebuilt per call.
1874
+ */
1875
+ var ops = internal$1.valRuntime(valModules, options).ops;
1869
1876
  var tools = [].concat(_toConsumableArray(readTools()), _toConsumableArray(writeTools()), _toConsumableArray(extraTools));
1870
1877
  var byName = new Map();
1871
1878
  var _iterator = _createForOfIteratorHelper(tools),
@@ -1912,7 +1919,7 @@ function createValTools(valModules, options) {
1912
1919
  },
1913
1920
  call: function call(name, args, ctx) {
1914
1921
  return asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee() {
1915
- var tool, parsed, insufficient, resolved, ops, state, deps, _t;
1922
+ var tool, parsed, insufficient, refused, state, deps, _t;
1916
1923
  return asyncToGenerator._regenerator().w(function (_context) {
1917
1924
  while (1) switch (_context.p = _context.n) {
1918
1925
  case 0:
@@ -1947,37 +1954,36 @@ function createValTools(valModules, options) {
1947
1954
  }
1948
1955
  return _context.a(2, insufficient);
1949
1956
  case 3:
1950
- resolved = resolveOps(ctx);
1951
- if (!(resolved.status === "error")) {
1957
+ refused = refuseCaller(ctx, options);
1958
+ if (!(refused !== null)) {
1952
1959
  _context.n = 4;
1953
1960
  break;
1954
1961
  }
1955
- return _context.a(2, resolved.result);
1962
+ return _context.a(2, refused);
1956
1963
  case 4:
1957
- ops = resolved.ops;
1958
- _context.p = 5;
1959
- _context.n = 6;
1964
+ _context.p = 4;
1965
+ _context.n = 5;
1960
1966
  return loadState(ops);
1961
- case 6:
1967
+ case 5:
1962
1968
  state = _context.v;
1963
1969
  if (!(state.status === "error")) {
1964
- _context.n = 7;
1970
+ _context.n = 6;
1965
1971
  break;
1966
1972
  }
1967
1973
  return _context.a(2, state.result);
1968
- case 7:
1974
+ case 6:
1969
1975
  deps = {
1970
1976
  ops: ops,
1971
1977
  config: options,
1972
1978
  ctx: ctx,
1973
1979
  state: state.state
1974
1980
  };
1975
- _context.n = 8;
1981
+ _context.n = 7;
1976
1982
  return tool.handler(parsed.data, deps);
1977
- case 8:
1983
+ case 7:
1978
1984
  return _context.a(2, _context.v);
1979
- case 9:
1980
- _context.p = 9;
1985
+ case 8:
1986
+ _context.p = 8;
1981
1987
  _t = _context.v;
1982
1988
  return _context.a(2, {
1983
1989
  status: "error",
@@ -1985,7 +1991,7 @@ function createValTools(valModules, options) {
1985
1991
  message: _t instanceof Error ? _t.message : String(_t)
1986
1992
  });
1987
1993
  }
1988
- }, _callee, null, [[5, 9]]);
1994
+ }, _callee, null, [[4, 8]]);
1989
1995
  }))();
1990
1996
  },
1991
1997
  dispose: function dispose() {
@@ -2005,8 +2011,9 @@ function createValTools(valModules, options) {
2005
2011
  }
2006
2012
 
2007
2013
  /**
2008
- * Pick the data layer for a call, which in proxy mode means picking whose
2009
- * credential the backend will see.
2014
+ * Whether this caller may reach the data layer at all, which in proxy mode
2015
+ * means whether there is a credential the backend will act on. `null`: it
2016
+ * may.
2010
2017
  *
2011
2018
  * This is the one place authorization is decided, and in proxy mode there is
2012
2019
  * exactly one credential it will act on: an access token whose signature,
@@ -2027,97 +2034,64 @@ function createValTools(valModules, options) {
2027
2034
  * profile has to be one the host checked cryptographically, never one it was
2028
2035
  * handed. An `authenticate()` that decided a credential's rights inside the app
2029
2036
  * would make every bug in it full access to every project that key can reach.
2037
+ *
2038
+ * Every allowed caller reads the same data layer, and that is correct rather
2039
+ * than a shortcut: in fs mode it is a developer's own working tree, with no
2040
+ * credential to vary by, and in proxy mode it authenticates with the app's own
2041
+ * API key, so there is nothing per-caller in it to keep apart.
2030
2042
  */
2031
- function createOpsResolver(valModules, options) {
2043
+ function refuseCaller(ctx, options) {
2044
+ var _ctx$auth;
2032
2045
  if (options.mode === "fs") {
2033
- // One instance, built once: fs mode is a developer's own working tree, so
2034
- // there is no credential to vary by and no reason to re-evaluate modules.
2035
- var ops = server.createValOps(valModules, options);
2036
- return function (ctx) {
2037
- if (ctx.auth) {
2038
- // Refused rather than ignored. A host that thinks it is passing a
2039
- // credential should not silently get local filesystem access instead —
2040
- // and the difference matters, because fs mode writes straight to disk
2041
- // with no backend permission check at all.
2042
- //
2043
- // A verified access token is not something the caller chose to send: it
2044
- // only exists because this app advertised an authorization server, so
2045
- // the developer seeing this did not do anything wrong — a config file
2046
- // did, and naming it is the difference between a two-minute fix and an
2047
- // afternoon.
2048
- return {
2049
- status: "error",
2050
- result: {
2051
- status: "error",
2052
- code: "unsupported",
2053
- message: "This Val project is running in local filesystem mode, so there is nothing to authenticate against, but it is configured with an `oauth` issuer and is therefore asking clients for an access token it cannot use. Remove the `oauth` config (or `VAL_OAUTH_ISSUER` from your local `.env`) for local development."
2054
- }
2055
- };
2056
- }
2057
- return {
2058
- status: "ok",
2059
- ops: ops
2060
- };
2061
- };
2062
- }
2063
-
2064
- /**
2065
- * One instance for every verified caller, and that is correct rather than a
2066
- * shortcut: this instance authenticates with the app's own API key, so there
2067
- * is nothing per-caller in it to keep apart. Who did what travels as the
2068
- * patch's `authorId` instead — see `writePath`.
2069
- *
2070
- * One instance is also all proxy mode keeps now. It could not share while a
2071
- * personal access token reached this function: each token needed its own
2072
- * `ValOpsHttp` to hold it, each of those cached the project's evaluated
2073
- * modules, and the bounded cache that kept that memory in check turned an
2074
- * eviction into a re-evaluation of every module on the next call.
2075
- */
2076
- var sharedOps = null;
2077
- return function (ctx) {
2078
- var _ctx$auth;
2079
- if (((_ctx$auth = ctx.auth) === null || _ctx$auth === void 0 ? void 0 : _ctx$auth.type) !== "verified-profile") {
2080
- return {
2081
- status: "error",
2082
- result: {
2083
- status: "error",
2084
- code: "forbidden",
2085
- message: "This Val project talks to the Val content backend, so every call needs an access token from the Val authorization server. If this endpoint is not asking clients to authorize, it has no `oauth` config — give `initValMcp` one, or run the project in local filesystem mode for development."
2086
- }
2087
- };
2088
- }
2089
- if (!options.apiKey) {
2090
- // Proxy mode is inferred from the api key being present, so this is
2091
- // unreachable through `initHandlerOptions`. It stays because the
2092
- // alternative to refusing is building ops with no credential at all.
2046
+ if (ctx.auth) {
2047
+ // Refused rather than ignored. A host that thinks it is passing a
2048
+ // credential should not silently get local filesystem access instead —
2049
+ // and the difference matters, because fs mode writes straight to disk
2050
+ // with no backend permission check at all.
2051
+ //
2052
+ // A verified access token is not something the caller chose to send: it
2053
+ // only exists because this app advertised an authorization server, so
2054
+ // the developer seeing this did not do anything wrong — a config file
2055
+ // did, and naming it is the difference between a two-minute fix and an
2056
+ // afternoon.
2093
2057
  return {
2094
2058
  status: "error",
2095
- result: {
2096
- status: "error",
2097
- code: "forbidden",
2098
- message: "This Val project has no API key configured, so a verified access token cannot be exchanged for backend access."
2099
- }
2059
+ code: "unsupported",
2060
+ message: "This Val project is running in local filesystem mode, so there is nothing to authenticate against, but it is configured with an `oauth` issuer and is therefore asking clients for an access token it cannot use. Remove the `oauth` config (or `VAL_OAUTH_ISSUER` from your local `.env`) for local development."
2100
2061
  };
2101
2062
  }
2102
- if (!sharedOps) {
2103
- sharedOps = server.createValOps(valModules, options);
2104
- }
2063
+ return null;
2064
+ }
2065
+ if (((_ctx$auth = ctx.auth) === null || _ctx$auth === void 0 ? void 0 : _ctx$auth.type) !== "verified-profile") {
2105
2066
  return {
2106
- status: "ok",
2107
- ops: sharedOps
2067
+ status: "error",
2068
+ code: "forbidden",
2069
+ message: "This Val project talks to the Val content backend, so every call needs an access token from the Val authorization server. If this endpoint is not asking clients to authorize, it has no `oauth` config — give `initValMcp` one, or run the project in local filesystem mode for development."
2108
2070
  };
2109
- };
2071
+ }
2072
+ if (!options.apiKey) {
2073
+ // Proxy mode is inferred from the api key being present, so this is
2074
+ // unreachable through `initHandlerOptions`. It stays because the
2075
+ // alternative to refusing is acting with no credential at all.
2076
+ return {
2077
+ status: "error",
2078
+ code: "forbidden",
2079
+ message: "This Val project has no API key configured, so a verified access token cannot be exchanged for backend access."
2080
+ };
2081
+ }
2082
+ return null;
2110
2083
  }
2084
+
2111
2085
  /**
2112
- * The content as the caller should see it, loaded once per call.
2086
+ * The content as the caller should see it: the head's (`ValHead.tree`).
2113
2087
  *
2114
2088
  * Pending patches are applied, because an agent looking at a project mid-edit
2115
2089
  * should see what the Studio would show rather than the last published state.
2116
- *
2117
- * Deliberately not cached across calls. In fs mode a save recomputes the base
2118
- * sha within the same process, so a cached view would go stale silently — and
2119
- * the cost of being wrong here is an agent writing a patch against content that
2120
- * has already moved.
2090
+ * The head re-reads the chain only when it moved (`seq`), and makes each
2091
+ * patched module once per version, so a call after nothing changed costs one
2092
+ * "has it moved?" -- and a call after a write (a save in fs mode moves the
2093
+ * base too) sees it, so an agent never writes against content that has
2094
+ * already moved.
2121
2095
  *
2122
2096
  * Exported for `toolsFixture`, not for consumers: `tools/index.ts` re-exports by
2123
2097
  * name, so this stays inside the package. A test that assembled its own state
@@ -2128,17 +2102,19 @@ function loadState(_x) {
2128
2102
  }
2129
2103
  function _loadState() {
2130
2104
  _loadState = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee3(ops) {
2131
- var patches, analysis, sourcesRes, _yield$Promise$all, _yield$Promise$all2, schemas, serializedSchemas;
2105
+ var tree, _yield$Promise$all, _yield$Promise$all2, schemas, serialized, serializedSchemas, isModuleFilePath, _i, _Object$entries, _Object$entries$_i, path, schema;
2132
2106
  return asyncToGenerator._regenerator().w(function (_context3) {
2133
2107
  while (1) switch (_context3.n) {
2134
2108
  case 0:
2135
2109
  _context3.n = 1;
2136
- return ops.fetchPatches({
2137
- excludePatchOps: false
2138
- });
2110
+ return ops.head.tree();
2139
2111
  case 1:
2140
- patches = _context3.v;
2141
- if (!patches.unauthorized) {
2112
+ tree = _context3.v;
2113
+ if (!("error" in tree)) {
2114
+ _context3.n = 4;
2115
+ break;
2116
+ }
2117
+ if (!tree.error.unauthorized) {
2142
2118
  _context3.n = 2;
2143
2119
  break;
2144
2120
  }
@@ -2151,7 +2127,7 @@ function _loadState() {
2151
2127
  }
2152
2128
  });
2153
2129
  case 2:
2154
- if (!patches.networkError) {
2130
+ if (!tree.error.networkError) {
2155
2131
  _context3.n = 3;
2156
2132
  break;
2157
2133
  }
@@ -2164,48 +2140,46 @@ function _loadState() {
2164
2140
  }
2165
2141
  });
2166
2142
  case 3:
2167
- if (!patches.error) {
2168
- _context3.n = 4;
2169
- break;
2170
- }
2171
2143
  return _context3.a(2, {
2172
2144
  status: "error",
2173
2145
  result: {
2174
2146
  status: "error",
2175
2147
  code: "internal",
2176
- message: patches.error.message
2148
+ message: tree.error.message
2177
2149
  }
2178
2150
  });
2179
2151
  case 4:
2180
- analysis = ops.analyzePatches(patches.patches); // getSourcesWithPatchesApplied, not getSources(analysis): the latter returns
2181
- // only the modules that had patches, and validating that subset reports
2182
- // spurious errors for anything that looks across modules, like keyOf or a
2183
- // router.
2184
2152
  _context3.n = 5;
2185
- return ops.getSourcesWithPatchesApplied(_objectSpread2(_objectSpread2({}, analysis), patches));
2153
+ return Promise.all([ops.getSchemas(), ops.head.serializedSchemas()]);
2186
2154
  case 5:
2187
- sourcesRes = _context3.v;
2188
- _context3.n = 6;
2189
- return Promise.all([ops.getSchemas(), ops.getSerializedSchemas()]);
2190
- case 6:
2191
2155
  _yield$Promise$all = _context3.v;
2192
2156
  _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
2193
2157
  schemas = _yield$Promise$all2[0];
2194
- serializedSchemas = _yield$Promise$all2[1];
2158
+ serialized = _yield$Promise$all2[1];
2159
+ serializedSchemas = {}; // Keys of `serialized` are the module paths it was written under.
2160
+ isModuleFilePath = function isModuleFilePath(key) {
2161
+ return Object.prototype.hasOwnProperty.call(serialized, key);
2162
+ };
2163
+ for (_i = 0, _Object$entries = Object.entries(serialized); _i < _Object$entries.length; _i++) {
2164
+ _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), path = _Object$entries$_i[0], schema = _Object$entries$_i[1];
2165
+ if (schema !== undefined && isModuleFilePath(path)) {
2166
+ serializedSchemas[path] = schema;
2167
+ }
2168
+ }
2195
2169
  return _context3.a(2, {
2196
2170
  status: "ok",
2197
2171
  state: {
2198
2172
  schemas: schemas,
2199
2173
  serializedSchemas: serializedSchemas,
2200
- sources: sourcesRes.sources,
2201
- patches: patches,
2202
- analysis: analysis,
2174
+ sources: tree.ok.sources,
2175
+ patches: tree.ok.chain.fetched,
2176
+ analysis: tree.ok.chain.analysis,
2203
2177
  // Which modules hold a pending patch that would not apply. Carried rather
2204
2178
  // than discarded because their `sources` silently lack that change: the
2205
2179
  // content here is not what publishing would produce, so a write against
2206
2180
  // it would be based on a state that does not exist. See
2207
2181
  // `unappliedPatchesFor`.
2208
- unappliedPatches: sourcesRes.errors
2182
+ unappliedPatches: tree.ok.errors
2209
2183
  }
2210
2184
  });
2211
2185
  }
@@ -3374,7 +3348,7 @@ var packageJson = {
3374
3348
  "./package.json": "./package.json"
3375
3349
  },
3376
3350
  types: "dist/valbuild-mcp.cjs.d.ts",
3377
- version: "0.150.23",
3351
+ version: "0.151.0",
3378
3352
  scripts: {
3379
3353
  typecheck: "tsc --noEmit",
3380
3354
  test: "jest",
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var asyncToGenerator = require('./asyncToGenerator-8e5c36c8.cjs.prod.js');
6
6
  var zod = require('zod');
7
- var server = require('@valbuild/server');
7
+ var internal$1 = require('@valbuild/server/internal');
8
8
  var core = require('@valbuild/core');
9
9
  var internal = require('@valbuild/shared/internal');
10
10
  var minimatch = require('minimatch');
@@ -13,6 +13,7 @@ var patch = require('@valbuild/core/patch');
13
13
  var fp = require('@valbuild/core/fp');
14
14
  var fs = require('node:fs/promises');
15
15
  var path = require('node:path');
16
+ var server = require('@valbuild/server');
16
17
 
17
18
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
18
19
 
@@ -1865,7 +1866,13 @@ var _excluded = ["handler"],
1865
1866
  */
1866
1867
  function createValTools(valModules, options) {
1867
1868
  var extraTools = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
1868
- var resolveOps = createOpsResolver(valModules, options);
1869
+ /*
1870
+ * The runtime every reader in this process shares (`valRuntime`): the API's,
1871
+ * the draft readers' and this registry's, so a write through any of them
1872
+ * is read by all, and the content is the head's -- made once per version,
1873
+ * not rebuilt per call.
1874
+ */
1875
+ var ops = internal$1.valRuntime(valModules, options).ops;
1869
1876
  var tools = [].concat(_toConsumableArray(readTools()), _toConsumableArray(writeTools()), _toConsumableArray(extraTools));
1870
1877
  var byName = new Map();
1871
1878
  var _iterator = _createForOfIteratorHelper(tools),
@@ -1912,7 +1919,7 @@ function createValTools(valModules, options) {
1912
1919
  },
1913
1920
  call: function call(name, args, ctx) {
1914
1921
  return asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee() {
1915
- var tool, parsed, insufficient, resolved, ops, state, deps, _t;
1922
+ var tool, parsed, insufficient, refused, state, deps, _t;
1916
1923
  return asyncToGenerator._regenerator().w(function (_context) {
1917
1924
  while (1) switch (_context.p = _context.n) {
1918
1925
  case 0:
@@ -1947,37 +1954,36 @@ function createValTools(valModules, options) {
1947
1954
  }
1948
1955
  return _context.a(2, insufficient);
1949
1956
  case 3:
1950
- resolved = resolveOps(ctx);
1951
- if (!(resolved.status === "error")) {
1957
+ refused = refuseCaller(ctx, options);
1958
+ if (!(refused !== null)) {
1952
1959
  _context.n = 4;
1953
1960
  break;
1954
1961
  }
1955
- return _context.a(2, resolved.result);
1962
+ return _context.a(2, refused);
1956
1963
  case 4:
1957
- ops = resolved.ops;
1958
- _context.p = 5;
1959
- _context.n = 6;
1964
+ _context.p = 4;
1965
+ _context.n = 5;
1960
1966
  return loadState(ops);
1961
- case 6:
1967
+ case 5:
1962
1968
  state = _context.v;
1963
1969
  if (!(state.status === "error")) {
1964
- _context.n = 7;
1970
+ _context.n = 6;
1965
1971
  break;
1966
1972
  }
1967
1973
  return _context.a(2, state.result);
1968
- case 7:
1974
+ case 6:
1969
1975
  deps = {
1970
1976
  ops: ops,
1971
1977
  config: options,
1972
1978
  ctx: ctx,
1973
1979
  state: state.state
1974
1980
  };
1975
- _context.n = 8;
1981
+ _context.n = 7;
1976
1982
  return tool.handler(parsed.data, deps);
1977
- case 8:
1983
+ case 7:
1978
1984
  return _context.a(2, _context.v);
1979
- case 9:
1980
- _context.p = 9;
1985
+ case 8:
1986
+ _context.p = 8;
1981
1987
  _t = _context.v;
1982
1988
  return _context.a(2, {
1983
1989
  status: "error",
@@ -1985,7 +1991,7 @@ function createValTools(valModules, options) {
1985
1991
  message: _t instanceof Error ? _t.message : String(_t)
1986
1992
  });
1987
1993
  }
1988
- }, _callee, null, [[5, 9]]);
1994
+ }, _callee, null, [[4, 8]]);
1989
1995
  }))();
1990
1996
  },
1991
1997
  dispose: function dispose() {
@@ -2005,8 +2011,9 @@ function createValTools(valModules, options) {
2005
2011
  }
2006
2012
 
2007
2013
  /**
2008
- * Pick the data layer for a call, which in proxy mode means picking whose
2009
- * credential the backend will see.
2014
+ * Whether this caller may reach the data layer at all, which in proxy mode
2015
+ * means whether there is a credential the backend will act on. `null`: it
2016
+ * may.
2010
2017
  *
2011
2018
  * This is the one place authorization is decided, and in proxy mode there is
2012
2019
  * exactly one credential it will act on: an access token whose signature,
@@ -2027,97 +2034,64 @@ function createValTools(valModules, options) {
2027
2034
  * profile has to be one the host checked cryptographically, never one it was
2028
2035
  * handed. An `authenticate()` that decided a credential's rights inside the app
2029
2036
  * would make every bug in it full access to every project that key can reach.
2037
+ *
2038
+ * Every allowed caller reads the same data layer, and that is correct rather
2039
+ * than a shortcut: in fs mode it is a developer's own working tree, with no
2040
+ * credential to vary by, and in proxy mode it authenticates with the app's own
2041
+ * API key, so there is nothing per-caller in it to keep apart.
2030
2042
  */
2031
- function createOpsResolver(valModules, options) {
2043
+ function refuseCaller(ctx, options) {
2044
+ var _ctx$auth;
2032
2045
  if (options.mode === "fs") {
2033
- // One instance, built once: fs mode is a developer's own working tree, so
2034
- // there is no credential to vary by and no reason to re-evaluate modules.
2035
- var ops = server.createValOps(valModules, options);
2036
- return function (ctx) {
2037
- if (ctx.auth) {
2038
- // Refused rather than ignored. A host that thinks it is passing a
2039
- // credential should not silently get local filesystem access instead —
2040
- // and the difference matters, because fs mode writes straight to disk
2041
- // with no backend permission check at all.
2042
- //
2043
- // A verified access token is not something the caller chose to send: it
2044
- // only exists because this app advertised an authorization server, so
2045
- // the developer seeing this did not do anything wrong — a config file
2046
- // did, and naming it is the difference between a two-minute fix and an
2047
- // afternoon.
2048
- return {
2049
- status: "error",
2050
- result: {
2051
- status: "error",
2052
- code: "unsupported",
2053
- message: "This Val project is running in local filesystem mode, so there is nothing to authenticate against, but it is configured with an `oauth` issuer and is therefore asking clients for an access token it cannot use. Remove the `oauth` config (or `VAL_OAUTH_ISSUER` from your local `.env`) for local development."
2054
- }
2055
- };
2056
- }
2057
- return {
2058
- status: "ok",
2059
- ops: ops
2060
- };
2061
- };
2062
- }
2063
-
2064
- /**
2065
- * One instance for every verified caller, and that is correct rather than a
2066
- * shortcut: this instance authenticates with the app's own API key, so there
2067
- * is nothing per-caller in it to keep apart. Who did what travels as the
2068
- * patch's `authorId` instead — see `writePath`.
2069
- *
2070
- * One instance is also all proxy mode keeps now. It could not share while a
2071
- * personal access token reached this function: each token needed its own
2072
- * `ValOpsHttp` to hold it, each of those cached the project's evaluated
2073
- * modules, and the bounded cache that kept that memory in check turned an
2074
- * eviction into a re-evaluation of every module on the next call.
2075
- */
2076
- var sharedOps = null;
2077
- return function (ctx) {
2078
- var _ctx$auth;
2079
- if (((_ctx$auth = ctx.auth) === null || _ctx$auth === void 0 ? void 0 : _ctx$auth.type) !== "verified-profile") {
2080
- return {
2081
- status: "error",
2082
- result: {
2083
- status: "error",
2084
- code: "forbidden",
2085
- message: "This Val project talks to the Val content backend, so every call needs an access token from the Val authorization server. If this endpoint is not asking clients to authorize, it has no `oauth` config — give `initValMcp` one, or run the project in local filesystem mode for development."
2086
- }
2087
- };
2088
- }
2089
- if (!options.apiKey) {
2090
- // Proxy mode is inferred from the api key being present, so this is
2091
- // unreachable through `initHandlerOptions`. It stays because the
2092
- // alternative to refusing is building ops with no credential at all.
2046
+ if (ctx.auth) {
2047
+ // Refused rather than ignored. A host that thinks it is passing a
2048
+ // credential should not silently get local filesystem access instead —
2049
+ // and the difference matters, because fs mode writes straight to disk
2050
+ // with no backend permission check at all.
2051
+ //
2052
+ // A verified access token is not something the caller chose to send: it
2053
+ // only exists because this app advertised an authorization server, so
2054
+ // the developer seeing this did not do anything wrong — a config file
2055
+ // did, and naming it is the difference between a two-minute fix and an
2056
+ // afternoon.
2093
2057
  return {
2094
2058
  status: "error",
2095
- result: {
2096
- status: "error",
2097
- code: "forbidden",
2098
- message: "This Val project has no API key configured, so a verified access token cannot be exchanged for backend access."
2099
- }
2059
+ code: "unsupported",
2060
+ message: "This Val project is running in local filesystem mode, so there is nothing to authenticate against, but it is configured with an `oauth` issuer and is therefore asking clients for an access token it cannot use. Remove the `oauth` config (or `VAL_OAUTH_ISSUER` from your local `.env`) for local development."
2100
2061
  };
2101
2062
  }
2102
- if (!sharedOps) {
2103
- sharedOps = server.createValOps(valModules, options);
2104
- }
2063
+ return null;
2064
+ }
2065
+ if (((_ctx$auth = ctx.auth) === null || _ctx$auth === void 0 ? void 0 : _ctx$auth.type) !== "verified-profile") {
2105
2066
  return {
2106
- status: "ok",
2107
- ops: sharedOps
2067
+ status: "error",
2068
+ code: "forbidden",
2069
+ message: "This Val project talks to the Val content backend, so every call needs an access token from the Val authorization server. If this endpoint is not asking clients to authorize, it has no `oauth` config — give `initValMcp` one, or run the project in local filesystem mode for development."
2108
2070
  };
2109
- };
2071
+ }
2072
+ if (!options.apiKey) {
2073
+ // Proxy mode is inferred from the api key being present, so this is
2074
+ // unreachable through `initHandlerOptions`. It stays because the
2075
+ // alternative to refusing is acting with no credential at all.
2076
+ return {
2077
+ status: "error",
2078
+ code: "forbidden",
2079
+ message: "This Val project has no API key configured, so a verified access token cannot be exchanged for backend access."
2080
+ };
2081
+ }
2082
+ return null;
2110
2083
  }
2084
+
2111
2085
  /**
2112
- * The content as the caller should see it, loaded once per call.
2086
+ * The content as the caller should see it: the head's (`ValHead.tree`).
2113
2087
  *
2114
2088
  * Pending patches are applied, because an agent looking at a project mid-edit
2115
2089
  * should see what the Studio would show rather than the last published state.
2116
- *
2117
- * Deliberately not cached across calls. In fs mode a save recomputes the base
2118
- * sha within the same process, so a cached view would go stale silently — and
2119
- * the cost of being wrong here is an agent writing a patch against content that
2120
- * has already moved.
2090
+ * The head re-reads the chain only when it moved (`seq`), and makes each
2091
+ * patched module once per version, so a call after nothing changed costs one
2092
+ * "has it moved?" -- and a call after a write (a save in fs mode moves the
2093
+ * base too) sees it, so an agent never writes against content that has
2094
+ * already moved.
2121
2095
  *
2122
2096
  * Exported for `toolsFixture`, not for consumers: `tools/index.ts` re-exports by
2123
2097
  * name, so this stays inside the package. A test that assembled its own state
@@ -2128,17 +2102,19 @@ function loadState(_x) {
2128
2102
  }
2129
2103
  function _loadState() {
2130
2104
  _loadState = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee3(ops) {
2131
- var patches, analysis, sourcesRes, _yield$Promise$all, _yield$Promise$all2, schemas, serializedSchemas;
2105
+ var tree, _yield$Promise$all, _yield$Promise$all2, schemas, serialized, serializedSchemas, isModuleFilePath, _i, _Object$entries, _Object$entries$_i, path, schema;
2132
2106
  return asyncToGenerator._regenerator().w(function (_context3) {
2133
2107
  while (1) switch (_context3.n) {
2134
2108
  case 0:
2135
2109
  _context3.n = 1;
2136
- return ops.fetchPatches({
2137
- excludePatchOps: false
2138
- });
2110
+ return ops.head.tree();
2139
2111
  case 1:
2140
- patches = _context3.v;
2141
- if (!patches.unauthorized) {
2112
+ tree = _context3.v;
2113
+ if (!("error" in tree)) {
2114
+ _context3.n = 4;
2115
+ break;
2116
+ }
2117
+ if (!tree.error.unauthorized) {
2142
2118
  _context3.n = 2;
2143
2119
  break;
2144
2120
  }
@@ -2151,7 +2127,7 @@ function _loadState() {
2151
2127
  }
2152
2128
  });
2153
2129
  case 2:
2154
- if (!patches.networkError) {
2130
+ if (!tree.error.networkError) {
2155
2131
  _context3.n = 3;
2156
2132
  break;
2157
2133
  }
@@ -2164,48 +2140,46 @@ function _loadState() {
2164
2140
  }
2165
2141
  });
2166
2142
  case 3:
2167
- if (!patches.error) {
2168
- _context3.n = 4;
2169
- break;
2170
- }
2171
2143
  return _context3.a(2, {
2172
2144
  status: "error",
2173
2145
  result: {
2174
2146
  status: "error",
2175
2147
  code: "internal",
2176
- message: patches.error.message
2148
+ message: tree.error.message
2177
2149
  }
2178
2150
  });
2179
2151
  case 4:
2180
- analysis = ops.analyzePatches(patches.patches); // getSourcesWithPatchesApplied, not getSources(analysis): the latter returns
2181
- // only the modules that had patches, and validating that subset reports
2182
- // spurious errors for anything that looks across modules, like keyOf or a
2183
- // router.
2184
2152
  _context3.n = 5;
2185
- return ops.getSourcesWithPatchesApplied(_objectSpread2(_objectSpread2({}, analysis), patches));
2153
+ return Promise.all([ops.getSchemas(), ops.head.serializedSchemas()]);
2186
2154
  case 5:
2187
- sourcesRes = _context3.v;
2188
- _context3.n = 6;
2189
- return Promise.all([ops.getSchemas(), ops.getSerializedSchemas()]);
2190
- case 6:
2191
2155
  _yield$Promise$all = _context3.v;
2192
2156
  _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
2193
2157
  schemas = _yield$Promise$all2[0];
2194
- serializedSchemas = _yield$Promise$all2[1];
2158
+ serialized = _yield$Promise$all2[1];
2159
+ serializedSchemas = {}; // Keys of `serialized` are the module paths it was written under.
2160
+ isModuleFilePath = function isModuleFilePath(key) {
2161
+ return Object.prototype.hasOwnProperty.call(serialized, key);
2162
+ };
2163
+ for (_i = 0, _Object$entries = Object.entries(serialized); _i < _Object$entries.length; _i++) {
2164
+ _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), path = _Object$entries$_i[0], schema = _Object$entries$_i[1];
2165
+ if (schema !== undefined && isModuleFilePath(path)) {
2166
+ serializedSchemas[path] = schema;
2167
+ }
2168
+ }
2195
2169
  return _context3.a(2, {
2196
2170
  status: "ok",
2197
2171
  state: {
2198
2172
  schemas: schemas,
2199
2173
  serializedSchemas: serializedSchemas,
2200
- sources: sourcesRes.sources,
2201
- patches: patches,
2202
- analysis: analysis,
2174
+ sources: tree.ok.sources,
2175
+ patches: tree.ok.chain.fetched,
2176
+ analysis: tree.ok.chain.analysis,
2203
2177
  // Which modules hold a pending patch that would not apply. Carried rather
2204
2178
  // than discarded because their `sources` silently lack that change: the
2205
2179
  // content here is not what publishing would produce, so a write against
2206
2180
  // it would be based on a state that does not exist. See
2207
2181
  // `unappliedPatchesFor`.
2208
- unappliedPatches: sourcesRes.errors
2182
+ unappliedPatches: tree.ok.errors
2209
2183
  }
2210
2184
  });
2211
2185
  }
@@ -3374,7 +3348,7 @@ var packageJson = {
3374
3348
  "./package.json": "./package.json"
3375
3349
  },
3376
3350
  types: "dist/valbuild-mcp.cjs.d.ts",
3377
- version: "0.150.23",
3351
+ version: "0.151.0",
3378
3352
  scripts: {
3379
3353
  typecheck: "tsc --noEmit",
3380
3354
  test: "jest",
@@ -1,6 +1,6 @@
1
1
  import { _ as _asyncToGenerator, a as _regenerator } from './asyncToGenerator-500f022f.esm.js';
2
2
  import { z } from 'zod';
3
- import { createValOps, getSettings, resolveRemoteFileAuth, getFileExt, initHandlerOptions } from '@valbuild/server';
3
+ import { valRuntime } from '@valbuild/server/internal';
4
4
  import { Internal, getSourcePathFromRoute, DEFAULT_VAL_REMOTE_HOST } from '@valbuild/core';
5
5
  import { performSearch, createSearchIndex, indexModule, filterBlockingValidationErrors, describeContainerAtPath, chainHeadOf, safeParsePatch, buildDuplicatePatch, buildEmptyAtPathPatch, declaredLocales, buildRemoveImageGalleryEntryPatch, resolveEncodeSettings, resolveSerializedSchemaAtPath, withExtension, fitWithin, isSkippedSource, ENCODE_MIME_TYPE_OF, isMimeTypeAccepted, chooseEncoded, buildImageGalleryPatch } from '@valbuild/shared/internal';
6
6
  import { minimatch } from 'minimatch';
@@ -9,6 +9,7 @@ import { JSONOps, applyPatch, deepClone } from '@valbuild/core/patch';
9
9
  import { result } from '@valbuild/core/fp';
10
10
  import fs from 'node:fs/promises';
11
11
  import path from 'node:path';
12
+ import { getSettings, resolveRemoteFileAuth, getFileExt, initHandlerOptions } from '@valbuild/server';
12
13
 
13
14
  function _arrayWithHoles(r) {
14
15
  if (Array.isArray(r)) return r;
@@ -1856,7 +1857,13 @@ var _excluded = ["handler"],
1856
1857
  */
1857
1858
  function createValTools(valModules, options) {
1858
1859
  var extraTools = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
1859
- var resolveOps = createOpsResolver(valModules, options);
1860
+ /*
1861
+ * The runtime every reader in this process shares (`valRuntime`): the API's,
1862
+ * the draft readers' and this registry's, so a write through any of them
1863
+ * is read by all, and the content is the head's -- made once per version,
1864
+ * not rebuilt per call.
1865
+ */
1866
+ var ops = valRuntime(valModules, options).ops;
1860
1867
  var tools = [].concat(_toConsumableArray(readTools()), _toConsumableArray(writeTools()), _toConsumableArray(extraTools));
1861
1868
  var byName = new Map();
1862
1869
  var _iterator = _createForOfIteratorHelper(tools),
@@ -1903,7 +1910,7 @@ function createValTools(valModules, options) {
1903
1910
  },
1904
1911
  call: function call(name, args, ctx) {
1905
1912
  return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
1906
- var tool, parsed, insufficient, resolved, ops, state, deps, _t;
1913
+ var tool, parsed, insufficient, refused, state, deps, _t;
1907
1914
  return _regenerator().w(function (_context) {
1908
1915
  while (1) switch (_context.p = _context.n) {
1909
1916
  case 0:
@@ -1938,37 +1945,36 @@ function createValTools(valModules, options) {
1938
1945
  }
1939
1946
  return _context.a(2, insufficient);
1940
1947
  case 3:
1941
- resolved = resolveOps(ctx);
1942
- if (!(resolved.status === "error")) {
1948
+ refused = refuseCaller(ctx, options);
1949
+ if (!(refused !== null)) {
1943
1950
  _context.n = 4;
1944
1951
  break;
1945
1952
  }
1946
- return _context.a(2, resolved.result);
1953
+ return _context.a(2, refused);
1947
1954
  case 4:
1948
- ops = resolved.ops;
1949
- _context.p = 5;
1950
- _context.n = 6;
1955
+ _context.p = 4;
1956
+ _context.n = 5;
1951
1957
  return loadState(ops);
1952
- case 6:
1958
+ case 5:
1953
1959
  state = _context.v;
1954
1960
  if (!(state.status === "error")) {
1955
- _context.n = 7;
1961
+ _context.n = 6;
1956
1962
  break;
1957
1963
  }
1958
1964
  return _context.a(2, state.result);
1959
- case 7:
1965
+ case 6:
1960
1966
  deps = {
1961
1967
  ops: ops,
1962
1968
  config: options,
1963
1969
  ctx: ctx,
1964
1970
  state: state.state
1965
1971
  };
1966
- _context.n = 8;
1972
+ _context.n = 7;
1967
1973
  return tool.handler(parsed.data, deps);
1968
- case 8:
1974
+ case 7:
1969
1975
  return _context.a(2, _context.v);
1970
- case 9:
1971
- _context.p = 9;
1976
+ case 8:
1977
+ _context.p = 8;
1972
1978
  _t = _context.v;
1973
1979
  return _context.a(2, {
1974
1980
  status: "error",
@@ -1976,7 +1982,7 @@ function createValTools(valModules, options) {
1976
1982
  message: _t instanceof Error ? _t.message : String(_t)
1977
1983
  });
1978
1984
  }
1979
- }, _callee, null, [[5, 9]]);
1985
+ }, _callee, null, [[4, 8]]);
1980
1986
  }))();
1981
1987
  },
1982
1988
  dispose: function dispose() {
@@ -1996,8 +2002,9 @@ function createValTools(valModules, options) {
1996
2002
  }
1997
2003
 
1998
2004
  /**
1999
- * Pick the data layer for a call, which in proxy mode means picking whose
2000
- * credential the backend will see.
2005
+ * Whether this caller may reach the data layer at all, which in proxy mode
2006
+ * means whether there is a credential the backend will act on. `null`: it
2007
+ * may.
2001
2008
  *
2002
2009
  * This is the one place authorization is decided, and in proxy mode there is
2003
2010
  * exactly one credential it will act on: an access token whose signature,
@@ -2018,97 +2025,64 @@ function createValTools(valModules, options) {
2018
2025
  * profile has to be one the host checked cryptographically, never one it was
2019
2026
  * handed. An `authenticate()` that decided a credential's rights inside the app
2020
2027
  * would make every bug in it full access to every project that key can reach.
2028
+ *
2029
+ * Every allowed caller reads the same data layer, and that is correct rather
2030
+ * than a shortcut: in fs mode it is a developer's own working tree, with no
2031
+ * credential to vary by, and in proxy mode it authenticates with the app's own
2032
+ * API key, so there is nothing per-caller in it to keep apart.
2021
2033
  */
2022
- function createOpsResolver(valModules, options) {
2034
+ function refuseCaller(ctx, options) {
2035
+ var _ctx$auth;
2023
2036
  if (options.mode === "fs") {
2024
- // One instance, built once: fs mode is a developer's own working tree, so
2025
- // there is no credential to vary by and no reason to re-evaluate modules.
2026
- var ops = createValOps(valModules, options);
2027
- return function (ctx) {
2028
- if (ctx.auth) {
2029
- // Refused rather than ignored. A host that thinks it is passing a
2030
- // credential should not silently get local filesystem access instead —
2031
- // and the difference matters, because fs mode writes straight to disk
2032
- // with no backend permission check at all.
2033
- //
2034
- // A verified access token is not something the caller chose to send: it
2035
- // only exists because this app advertised an authorization server, so
2036
- // the developer seeing this did not do anything wrong — a config file
2037
- // did, and naming it is the difference between a two-minute fix and an
2038
- // afternoon.
2039
- return {
2040
- status: "error",
2041
- result: {
2042
- status: "error",
2043
- code: "unsupported",
2044
- message: "This Val project is running in local filesystem mode, so there is nothing to authenticate against, but it is configured with an `oauth` issuer and is therefore asking clients for an access token it cannot use. Remove the `oauth` config (or `VAL_OAUTH_ISSUER` from your local `.env`) for local development."
2045
- }
2046
- };
2047
- }
2048
- return {
2049
- status: "ok",
2050
- ops: ops
2051
- };
2052
- };
2053
- }
2054
-
2055
- /**
2056
- * One instance for every verified caller, and that is correct rather than a
2057
- * shortcut: this instance authenticates with the app's own API key, so there
2058
- * is nothing per-caller in it to keep apart. Who did what travels as the
2059
- * patch's `authorId` instead — see `writePath`.
2060
- *
2061
- * One instance is also all proxy mode keeps now. It could not share while a
2062
- * personal access token reached this function: each token needed its own
2063
- * `ValOpsHttp` to hold it, each of those cached the project's evaluated
2064
- * modules, and the bounded cache that kept that memory in check turned an
2065
- * eviction into a re-evaluation of every module on the next call.
2066
- */
2067
- var sharedOps = null;
2068
- return function (ctx) {
2069
- var _ctx$auth;
2070
- if (((_ctx$auth = ctx.auth) === null || _ctx$auth === void 0 ? void 0 : _ctx$auth.type) !== "verified-profile") {
2071
- return {
2072
- status: "error",
2073
- result: {
2074
- status: "error",
2075
- code: "forbidden",
2076
- message: "This Val project talks to the Val content backend, so every call needs an access token from the Val authorization server. If this endpoint is not asking clients to authorize, it has no `oauth` config — give `initValMcp` one, or run the project in local filesystem mode for development."
2077
- }
2078
- };
2079
- }
2080
- if (!options.apiKey) {
2081
- // Proxy mode is inferred from the api key being present, so this is
2082
- // unreachable through `initHandlerOptions`. It stays because the
2083
- // alternative to refusing is building ops with no credential at all.
2037
+ if (ctx.auth) {
2038
+ // Refused rather than ignored. A host that thinks it is passing a
2039
+ // credential should not silently get local filesystem access instead —
2040
+ // and the difference matters, because fs mode writes straight to disk
2041
+ // with no backend permission check at all.
2042
+ //
2043
+ // A verified access token is not something the caller chose to send: it
2044
+ // only exists because this app advertised an authorization server, so
2045
+ // the developer seeing this did not do anything wrong — a config file
2046
+ // did, and naming it is the difference between a two-minute fix and an
2047
+ // afternoon.
2084
2048
  return {
2085
2049
  status: "error",
2086
- result: {
2087
- status: "error",
2088
- code: "forbidden",
2089
- message: "This Val project has no API key configured, so a verified access token cannot be exchanged for backend access."
2090
- }
2050
+ code: "unsupported",
2051
+ message: "This Val project is running in local filesystem mode, so there is nothing to authenticate against, but it is configured with an `oauth` issuer and is therefore asking clients for an access token it cannot use. Remove the `oauth` config (or `VAL_OAUTH_ISSUER` from your local `.env`) for local development."
2091
2052
  };
2092
2053
  }
2093
- if (!sharedOps) {
2094
- sharedOps = createValOps(valModules, options);
2095
- }
2054
+ return null;
2055
+ }
2056
+ if (((_ctx$auth = ctx.auth) === null || _ctx$auth === void 0 ? void 0 : _ctx$auth.type) !== "verified-profile") {
2096
2057
  return {
2097
- status: "ok",
2098
- ops: sharedOps
2058
+ status: "error",
2059
+ code: "forbidden",
2060
+ message: "This Val project talks to the Val content backend, so every call needs an access token from the Val authorization server. If this endpoint is not asking clients to authorize, it has no `oauth` config — give `initValMcp` one, or run the project in local filesystem mode for development."
2099
2061
  };
2100
- };
2062
+ }
2063
+ if (!options.apiKey) {
2064
+ // Proxy mode is inferred from the api key being present, so this is
2065
+ // unreachable through `initHandlerOptions`. It stays because the
2066
+ // alternative to refusing is acting with no credential at all.
2067
+ return {
2068
+ status: "error",
2069
+ code: "forbidden",
2070
+ message: "This Val project has no API key configured, so a verified access token cannot be exchanged for backend access."
2071
+ };
2072
+ }
2073
+ return null;
2101
2074
  }
2075
+
2102
2076
  /**
2103
- * The content as the caller should see it, loaded once per call.
2077
+ * The content as the caller should see it: the head's (`ValHead.tree`).
2104
2078
  *
2105
2079
  * Pending patches are applied, because an agent looking at a project mid-edit
2106
2080
  * should see what the Studio would show rather than the last published state.
2107
- *
2108
- * Deliberately not cached across calls. In fs mode a save recomputes the base
2109
- * sha within the same process, so a cached view would go stale silently — and
2110
- * the cost of being wrong here is an agent writing a patch against content that
2111
- * has already moved.
2081
+ * The head re-reads the chain only when it moved (`seq`), and makes each
2082
+ * patched module once per version, so a call after nothing changed costs one
2083
+ * "has it moved?" -- and a call after a write (a save in fs mode moves the
2084
+ * base too) sees it, so an agent never writes against content that has
2085
+ * already moved.
2112
2086
  *
2113
2087
  * Exported for `toolsFixture`, not for consumers: `tools/index.ts` re-exports by
2114
2088
  * name, so this stays inside the package. A test that assembled its own state
@@ -2119,17 +2093,19 @@ function loadState(_x) {
2119
2093
  }
2120
2094
  function _loadState() {
2121
2095
  _loadState = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(ops) {
2122
- var patches, analysis, sourcesRes, _yield$Promise$all, _yield$Promise$all2, schemas, serializedSchemas;
2096
+ var tree, _yield$Promise$all, _yield$Promise$all2, schemas, serialized, serializedSchemas, isModuleFilePath, _i, _Object$entries, _Object$entries$_i, path, schema;
2123
2097
  return _regenerator().w(function (_context3) {
2124
2098
  while (1) switch (_context3.n) {
2125
2099
  case 0:
2126
2100
  _context3.n = 1;
2127
- return ops.fetchPatches({
2128
- excludePatchOps: false
2129
- });
2101
+ return ops.head.tree();
2130
2102
  case 1:
2131
- patches = _context3.v;
2132
- if (!patches.unauthorized) {
2103
+ tree = _context3.v;
2104
+ if (!("error" in tree)) {
2105
+ _context3.n = 4;
2106
+ break;
2107
+ }
2108
+ if (!tree.error.unauthorized) {
2133
2109
  _context3.n = 2;
2134
2110
  break;
2135
2111
  }
@@ -2142,7 +2118,7 @@ function _loadState() {
2142
2118
  }
2143
2119
  });
2144
2120
  case 2:
2145
- if (!patches.networkError) {
2121
+ if (!tree.error.networkError) {
2146
2122
  _context3.n = 3;
2147
2123
  break;
2148
2124
  }
@@ -2155,48 +2131,46 @@ function _loadState() {
2155
2131
  }
2156
2132
  });
2157
2133
  case 3:
2158
- if (!patches.error) {
2159
- _context3.n = 4;
2160
- break;
2161
- }
2162
2134
  return _context3.a(2, {
2163
2135
  status: "error",
2164
2136
  result: {
2165
2137
  status: "error",
2166
2138
  code: "internal",
2167
- message: patches.error.message
2139
+ message: tree.error.message
2168
2140
  }
2169
2141
  });
2170
2142
  case 4:
2171
- analysis = ops.analyzePatches(patches.patches); // getSourcesWithPatchesApplied, not getSources(analysis): the latter returns
2172
- // only the modules that had patches, and validating that subset reports
2173
- // spurious errors for anything that looks across modules, like keyOf or a
2174
- // router.
2175
2143
  _context3.n = 5;
2176
- return ops.getSourcesWithPatchesApplied(_objectSpread2(_objectSpread2({}, analysis), patches));
2144
+ return Promise.all([ops.getSchemas(), ops.head.serializedSchemas()]);
2177
2145
  case 5:
2178
- sourcesRes = _context3.v;
2179
- _context3.n = 6;
2180
- return Promise.all([ops.getSchemas(), ops.getSerializedSchemas()]);
2181
- case 6:
2182
2146
  _yield$Promise$all = _context3.v;
2183
2147
  _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
2184
2148
  schemas = _yield$Promise$all2[0];
2185
- serializedSchemas = _yield$Promise$all2[1];
2149
+ serialized = _yield$Promise$all2[1];
2150
+ serializedSchemas = {}; // Keys of `serialized` are the module paths it was written under.
2151
+ isModuleFilePath = function isModuleFilePath(key) {
2152
+ return Object.prototype.hasOwnProperty.call(serialized, key);
2153
+ };
2154
+ for (_i = 0, _Object$entries = Object.entries(serialized); _i < _Object$entries.length; _i++) {
2155
+ _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), path = _Object$entries$_i[0], schema = _Object$entries$_i[1];
2156
+ if (schema !== undefined && isModuleFilePath(path)) {
2157
+ serializedSchemas[path] = schema;
2158
+ }
2159
+ }
2186
2160
  return _context3.a(2, {
2187
2161
  status: "ok",
2188
2162
  state: {
2189
2163
  schemas: schemas,
2190
2164
  serializedSchemas: serializedSchemas,
2191
- sources: sourcesRes.sources,
2192
- patches: patches,
2193
- analysis: analysis,
2165
+ sources: tree.ok.sources,
2166
+ patches: tree.ok.chain.fetched,
2167
+ analysis: tree.ok.chain.analysis,
2194
2168
  // Which modules hold a pending patch that would not apply. Carried rather
2195
2169
  // than discarded because their `sources` silently lack that change: the
2196
2170
  // content here is not what publishing would produce, so a write against
2197
2171
  // it would be based on a state that does not exist. See
2198
2172
  // `unappliedPatchesFor`.
2199
- unappliedPatches: sourcesRes.errors
2173
+ unappliedPatches: tree.ok.errors
2200
2174
  }
2201
2175
  });
2202
2176
  }
@@ -3365,7 +3339,7 @@ var packageJson = {
3365
3339
  "./package.json": "./package.json"
3366
3340
  },
3367
3341
  types: "dist/valbuild-mcp.cjs.d.ts",
3368
- version: "0.150.23",
3342
+ version: "0.151.0",
3369
3343
  scripts: {
3370
3344
  typecheck: "tsc --noEmit",
3371
3345
  test: "jest",
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./package.json": "./package.json"
21
21
  },
22
22
  "types": "dist/valbuild-mcp.cjs.d.ts",
23
- "version": "0.150.23",
23
+ "version": "0.151.0",
24
24
  "preconstruct": {
25
25
  "entrypoints": [
26
26
  "./index.ts",
@@ -31,9 +31,9 @@
31
31
  "dependencies": {
32
32
  "minimatch": "^10.2.6",
33
33
  "zod": "^4.4.3",
34
- "@valbuild/server": "0.150.23",
35
- "@valbuild/shared": "0.150.20",
36
- "@valbuild/core": "0.150.3"
34
+ "@valbuild/core": "0.151.0",
35
+ "@valbuild/shared": "0.151.0",
36
+ "@valbuild/server": "0.151.0"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@prettier/sync": "^0.6.1",