cortad 0.3.10 → 0.4.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/lib/trace.cjs CHANGED
@@ -17,7 +17,7 @@ if (FILE) {
17
17
  const http = require("node:http");
18
18
  const https = require("node:https");
19
19
  const als = new AsyncLocalStorage();
20
- const { MAX, REPLY_MAX, EMBEDDING, TOOL_TEXT, toolText, scrubbed, list, clipped, argsText, turnOf, isModelCall, isRetrieval, qdrantCall, passagesFrom, decoded, cookieNames, makeRecorder, asChat, GATEWAY_MODEL } = require("./wire.cjs");
20
+ const { MAX, REPLY_MAX, EMBEDDING, TOOL_TEXT, toolText, scrubbed, list, clipped, argsText, turnOf, isModelCall, isRetrieval, passagesFrom, decoded, cookieNames, makeRecorder, asChat, GATEWAY_MODEL } = require("./wire.cjs");
21
21
  const row = (value) => { try { fs.appendFileSync(FILE, JSON.stringify(scrubbed(value)) + "\n", { mode: 0o600 }); } catch { /* the command is gone */ } };
22
22
  // What the app writes into its own folder while a run is on: each file copied once before its
23
23
  // first write, so the command puts every one back when the run ends (lib/writes.mjs). Trials
@@ -313,13 +313,10 @@ if (FILE) {
313
313
  recheck = false;
314
314
  setImmediate(() => { try { if (table().routes.length !== routesSaid) sayRoutes(lastPort); } catch { /* as above */ } });
315
315
  };
316
- // When the run last sent a request: a write made outside every request (a queue's worker, a
317
- // timer) is held while a run is live, since it may be carrying out a trial's ask.
318
- let lastTurnAt = -Infinity;
319
316
  const { tied, requestCtx, note, ended, meter } = makeRecorder({
320
317
  row,
321
318
  rulesFile: process.env.CORTAD_RULES_FILE,
322
- onTurn: () => { heldWarm(); lastTurnAt = Date.now(); },
319
+ onTurn: () => {},
323
320
  onEnded: (ctx, steps) => writtenAt(ctx, steps),
324
321
  });
325
322
  // Where in the app's own code each part of a reply was written: the app's line on the stack of the
@@ -419,7 +416,6 @@ if (FILE) {
419
416
  // context a model call is pinned by: it can only be the step an answer comes on.
420
417
  const emit = http.Server.prototype.emit;
421
418
  http.Server.prototype.emit = function (type, req, ...rest) {
422
- if (this[HELD]) return emit.call(this, type, req, ...rest);
423
419
  if (type === "listening") { try { listening(this.address()?.port); } catch { /* not a TCP server */ } }
424
420
  if (type === "request") { recheckRoutes(); sweepTools(); }
425
421
  if (type !== "request" || !req || !req.method || /^(?:HEAD|OPTIONS)$/.test(req.method)) return emit.call(this, type, req, ...rest);
@@ -735,327 +731,16 @@ if (FILE) {
735
731
  return socketConnect.apply(this, args);
736
732
  };
737
733
 
738
- // Outbound writes a trial makes are held in the app, never sent: a trial that books, charges,
739
- // emails or deletes would otherwise do it for real. Held: a call made inside a request the run
740
- // tagged, with a writing method, to a host that is not a model, a retrieval, this machine, a copy
741
- // of ours or a host the person said yes to. A call carrying a provider's test key goes to that
742
- // provider's own sandbox as sent. The app is answered with a success shaped like what it sent,
743
- // and the run is told by a dep row with `held` and the clipped call.
744
- const OUTBOUND = process.env.CORTAD_OUTBOUND_FILE;
745
- let passed = [], passedAt = -1;
746
- const passNow = () => {
747
- try {
748
- const at = fs.statSync(OUTBOUND).mtimeMs;
749
- if (at !== passedAt) { passedAt = at; const got = JSON.parse(fs.readFileSync(OUTBOUND, "utf8")); passed = Array.isArray(got.pass) ? got.pass.filter((h) => typeof h === "string" && h) : []; }
750
- } catch { /* not written yet: nothing passes */ }
751
- return passed;
752
- };
753
- const WRITES = /^(?:POST|PUT|PATCH|DELETE)$/;
754
- const THIS_MACHINE = /^(?:localhost|127(?:\.\d+){3}|\[?::1\]?|0\.0\.0\.0)$/i;
755
- // A provider's own sandbox, named so in the host: api.sandbox.paypal.com, sandbox-api.polar.sh.
756
- const SANDBOX_HOST = /(?:^|[.-])(?:sandbox|sbox)[.-]/i;
757
- // ponytail: a sign-in's token refresh is a POST that changes nothing, told apart by its path only.
758
- const TOKEN_PATH = /\/(?:oauth2?\/)?token(?:[/?]|$)/i;
759
- // An API where every call is a POST names what a call does in its last segment, as
760
- // resource.verb_words (customers.get, billing.preview_attach) or as a gRPC or Connect method
761
- // (acme.v1.UserService/GetUser). A POST so named that starts with a reading word, and holds no word
762
- // that makes the read a write (get_or_create, check_in, GetLease), goes out.
763
- // A preview commits nothing whatever it previews; any other reading word reads only while no word
764
- // after it writes: credits.check_deduct and usage.count_increment went out as reads.
765
- // ponytail: decided on the name alone, so a POST named as a plain read whose body writes goes out.
766
- // A rule on the body is the upgrade if one is ever seen.
767
- const WRITE_WORD = "create|update|delete|remove|set|add|put|insert|upsert|write|save|store|record|track|log|send|deduct|increment|decrement|consume|use|spend|charge|pay|refund|cancel|reserve|lock|unlock|commit|apply|attach|detach|start|stop|run|trigger|reset|rotate|revoke|grant|issue|mint|transfer|move|merge|sync|import|export|upload|publish|enqueue|push|pop|ack|touch|mark|close|approve|reject|submit|confirm|redeem|claim";
768
- const DOT_CALL = /^[A-Za-z][\w-]*\.[a-z][a-z_]*$/, DOT_READ = /^(?:get|list|check|search|query|find|lookup|retrieve|describe|count|preview)$/, DOT_NOT = new RegExp(`^(?:or|and|in|out|next|${WRITE_WORD})$`);
769
- const RPC_CALL = /^(?:[A-Z][a-z0-9]*)+$/, RPC_READ = /^(?:Get|List|Search|Query|Find|Lookup|Describe)$/, RPC_NOT = new RegExp(`^(?:or|and|next|lease|${WRITE_WORD})$`, "i");
770
- const readWords = (words, read, not) => read.test(words[0]) && (/^preview$/i.test(words[0]) || !words.slice(1).some((w) => not.test(w)));
771
- const readsByName = (method, pathname) => {
772
- if (method !== "POST") return false;
773
- const parts = pathname.split("/").filter(Boolean);
774
- const last = parts[parts.length - 1] || "", before = parts[parts.length - 2] || "";
775
- if (DOT_CALL.test(last)) return readWords(last.split(".")[1].split("_"), DOT_READ, DOT_NOT);
776
- return /\.[A-Z]\w*$/.test(before) && RPC_CALL.test(last) && readWords(last.split(/(?=[A-Z])/), RPC_READ, RPC_NOT);
777
- };
778
- // A provider's test-mode key says so in the prefix of the key itself, set off by underscores
779
- // (Stripe's sk_test_, am_sk_test_, pdl_sdbx_), or it is a value of a setting the app keeps as a
780
- // test one. The word elsewhere is not it: a Basic user named test-admin and a live key holding
781
- // "-Test-" both went out as test keys.
782
- const TEST_KEY = /^(?:[a-z0-9]{2,8}_){0,3}(?:test|sandbox|sbox|sdbx)_[\w-]{4,}$/i;
783
- const testKey = (auth) => Boolean(auth) && (auth.split(/[\s:]+/).some((piece) => TEST_KEY.test(piece))
784
- || Object.entries(process.env).some(([k, v]) => /(?:^|_)TEST(?:_|$)/i.test(k) && v && v.length >= 8 && auth.includes(v)));
785
- const authText = (value) => { const a = String(value || ""); const m = /^Basic\s+(\S+)$/i.exec(a); return m ? Buffer.from(m[1], "base64").toString("utf8") : a; };
786
- // The headers a key travels in.
787
- const KEY_HEADERS = ["authorization", "x-api-key", "api-key", "apikey", "x-auth-token"];
788
- // Neon's HTTP driver sends every query, reads too, to its region's API host and names the
789
- // database in a header: the call is decided on that database's host. No header: the API host.
790
- const NEON_SQL = /^api(?:auth)?\.[a-z0-9.-]+\.neon\.tech$/;
791
- const targetOf = (u, headerOf) => {
792
- const host = u.hostname.toLowerCase();
793
- if (!NEON_SQL.test(host) || u.pathname !== "/sql") return host;
794
- try { return new URL(headerOf("neon-connection-string")).hostname.toLowerCase() || host; } catch { return host; }
795
- };
796
- // `headerOf(name)`: one of the call's headers, read only for a call that is otherwise held. Once a
797
- // tagged write is being decided, a failure to decide holds it.
798
- // ponytail: while a run is live, a person's own queued write is held too; per-ask pinning of
799
- // background writes is the upgrade when that matters.
800
- const RUN_LIVE_MS = 10 * 60_000;
801
- // The copies this hook makes of Qdrant collections carry this suffix (copyOf below).
802
- const OUR_COPY = /_cortad_\d+$/;
803
- const heldHere = (u, method, headerOf) => {
804
- const ctx = als.getStore();
805
- if (!WRITES.test(method) || (ctx ? !ctx.turn : Date.now() - lastTurnAt > RUN_LIVE_MS)) return false;
806
- // A read of a Qdrant collection, however its server is named (a bare address holds no word
807
- // like qdrant), and any call to a copy this hook made, go out.
808
- const q = qdrantCall(u, method);
809
- if (q && (!q.write || (q.collection && OUR_COPY.test(q.collection)))) return false;
810
- try {
811
- const host = targetOf(u, headerOf);
812
- if (THIS_MACHINE.test(host) || SANDBOX_HOST.test(host) || isModelCall(u.host, u.pathname) || isRetrieval(u) || TOKEN_PATH.test(u.pathname) || readsByName(method, u.pathname) || KEY_HEADERS.some((name) => testKey(authText(headerOf(name))))) return false;
813
- return !passNow().some((p) => host === p || host.endsWith("." + p));
814
- } catch { return true; }
815
- };
816
- // A write to a Qdrant collection made inside a run goes to a copy of that collection on the same
817
- // server, made at the first such write: the collection is read page by page and written under
818
- // `<name>_cortad_<pid>`, and from then on every call to that collection, reads too, goes to the
819
- // copy, so the app finds what it wrote. The original is never written. A copy that cannot be
820
- // made (a collection over the size a run copies, a server that refused) leaves the write held
821
- // like any other. Each copy is reported as a `copy` row, and the command deletes them when it
822
- // ends. A copy of ours is never copied again. ulaim's hosted Qdrant held Run for the person's
823
- // yes for seventeen minutes; this is what the yes stood in for.
824
- const copies = new Map();
825
- // What one process copies at most: collections, and points across them. Past it a write is held
826
- // like any other, and the app's output says so once. A run is a few conversations' writes, never
827
- // a cluster's.
828
- const COPIES_MAX = 8;
829
- const POINTS_MAX = 300_000;
830
- let copied = 0, pointsCopied = 0;
831
- const nativeFetch = typeof globalThis.fetch === "function" ? globalThis.fetch : null;
832
- let qdrantLib = null;
833
- const qdrantMod = () => (qdrantLib ??= import(require("node:url").pathToFileURL(require("node:path").join(__dirname, "qdrant.mjs")).href));
834
- const inRun = () => { const ctx = als.getStore(); return ctx ? Boolean(ctx.turn) : Date.now() - lastTurnAt <= RUN_LIVE_MS; };
835
- // What to do with a call: null to leave it alone; `hold` for a write of the server itself
836
- // (/collections/aliases); else the collection's copy when there is one, or none yet for a write
837
- // in a run, which makes it.
838
- const guarded = (u, method) => {
839
- if (!/^https?:$/.test(u.protocol)) return null;
840
- const call = qdrantCall(u, method);
841
- if (!call) return null;
842
- if (!call.collection) return call.write && inRun() ? { hold: true } : null;
843
- // The person's own calls, outside a run, go where they were sent: their writes are theirs to keep.
844
- if (OUR_COPY.test(call.collection) || !inRun()) return null;
845
- const key = `${u.host}|${call.collection}`;
846
- const copy = copies.get(key);
847
- if (copy) return { key, call, copy };
848
- if (!call.write) return null;
849
- // A change to the collection's shape, or a write past this process's copy budget, is held.
850
- if (call.kind === "schema") return { hold: true };
851
- if (copied >= COPIES_MAX || pointsCopied >= POINTS_MAX) { copySay(u.host, call.collection, `this run already copied ${copied} collections, ${pointsCopied} points`); return { hold: true }; }
852
- return { key, call, copy: null };
853
- };
854
- // Said once per collection in the app's own output when no copy could be made and the write was
855
- // kept on this machine instead, with the reason the server gave.
856
- const copySaid = new Set();
857
- const copySay = (host, collection, why) => {
858
- if (copySaid.has(`${host}|${collection}`)) return;
859
- copySaid.add(`${host}|${collection}`);
860
- try { process.stderr.write(`cortad: a request Cortad sent made this app write into the Qdrant collection ${collection} at ${host}. Cortad could not copy that collection on its server (${why}), so the write was kept on this machine and answered with a stand-in success.\n`); } catch { /* no stderr */ }
861
- };
862
- const toCopy = (u, clone, call) => { const t = new URL(u.href); t.pathname = `/collections/${encodeURIComponent(clone)}${call.rest}`; return t; };
863
- // The copy's name, made now when none exists; null when none could be made. The key the app sent
864
- // is the key the copy is made with, and never leaves this process.
865
- const copyOf = (u, headerOf, g) => {
866
- const have = copies.get(g.key);
867
- if (have) return Promise.resolve(have);
868
- if (!nativeFetch) return Promise.resolve(null);
869
- const clone = `${g.call.collection.slice(0, 40)}_cortad_${process.pid}`;
870
- const bearer = /^Bearer\s+(\S+)/i.exec(headerOf("authorization") || "");
871
- const conn = { base: `${u.protocol}//${u.host}`, apiKey: headerOf("api-key") || (bearer ? bearer[1] : ""), fetch: nativeFetch };
872
- const made = qdrantMod().then(({ cloneQdrant }) => cloneQdrant(conn, g.call.collection, clone)).then((got) => {
873
- if (!got || got.why) { copies.delete(g.key); copySay(u.host, g.call.collection, got ? got.why : "nothing"); return null; }
874
- copied += 1; pointsCopied += got.points ?? 0;
875
- copies.set(g.key, clone);
876
- row({ copy: { host: u.hostname, base: conn.base, collection: g.call.collection, clone } });
877
- return clone;
878
- }, (e) => { copies.delete(g.key); copySay(u.host, g.call.collection, e && e.message ? e.message : "error"); return null; });
879
- copies.set(g.key, made);
880
- return made;
881
- };
882
- // A GraphQL query reads; a mutation writes. A `query` in JSON that is not GraphQL (Neon's SQL, where
883
- // a SELECT can write through a function) is held.
884
- // ClickHouse, Trino and Presto take every query as a POST of its text, a SELECT too, and a SELECT
885
- // there cannot write: one statement that starts as a read and holds no word that writes, sent to
886
- // what its address or its headers show is one of them, goes out. Anything this cannot read as that
887
- // stays held. Databuddy's agent had 45 replies come back empty while its own ClickHouse queries
888
- // were answered here with a stand-in.
889
- const SQL_QUOTED = /'(?:[^'\\]|\\.|'')*'|"(?:[^"\\]|\\.|"")*"|`[^`]*`|--[^\n]*|\/\*[\s\S]*?\*\//g;
890
- // SHOW, DESCRIBE and EXISTS read whatever follows them. A SELECT, a WITH or an EXPLAIN reads
891
- // unless it holds a word that writes inside it (a CTE that deletes, SELECT INTO, EXPLAIN ANALYZE
892
- // of an insert) or hands its text on to another database to run.
893
- const SQL_SHOWS = /^(?:show|describe|desc|exists)\b/i, SQL_SELECTS = /^(?:select|with|explain)\b/i;
894
- const SQL_WRITES = /(?<![.\w])(?:insert|update|delete|merge|into|create|alter|drop|truncate|refresh|call|execute)(?![.\w])/i;
895
- const SQL_HANDS_ON = /\b(?:table|query|jdbc|odbc|executable)\s*\(/i;
896
- const statementOf = (sql) => String(sql || "").replace(SQL_QUOTED, (m) => (m[0] === "'" ? "''" : /^["`]/.test(m) ? "x" : " ")).trim().replace(/;\s*$/, "");
897
- // A quote or a comment left open, a second statement or a dollar-quoted body is not read as a read.
898
- const whole = (bare) => !/[;'"`]|\/\*|\$\$/.test(bare.replace(/''/g, ""));
899
- const selects = (bare) => whole(bare) && (SQL_SHOWS.test(bare) || (SQL_SELECTS.test(bare) && !SQL_WRITES.test(bare) && !SQL_HANDS_ON.test(bare)));
900
- const ANALYTICS_NAME = /clickhouse|trino|presto/i;
901
- const ANALYTICS_HEADERS = ["x-clickhouse-user", "x-clickhouse-key", "x-clickhouse-database", "x-clickhouse-format", "x-trino-user", "x-presto-user"];
902
- const analytics = (u, headerOf, port) => /^(?:8123|8443)$/.test(String(port || u.port)) || ANALYTICS_NAME.test(u.hostname) || ANALYTICS_NAME.test(headerOf("user-agent") || "")
903
- || ANALYTICS_HEADERS.some((name) => headerOf(name)) || u.pathname === "/v1/statement";
904
- const readsOnly = (text, u, headerOf = () => "", port) => {
905
- const sent = String(text || "").trim();
906
- try { const q = JSON.parse(sent)?.query; return typeof q === "string" && /^\s*(?:\{|query\b|fragment\b)/.test(q) && !/\bmutation\b/.test(q); } catch { /* not JSON: a statement sent as text */ }
907
- if (!u || !analytics(u, headerOf, port)) return false;
908
- // ClickHouse joins a `query` in the address to the body: both are read as the one statement.
909
- const asked = `${u.searchParams.get("query") || ""} ${sent}`;
910
- // ClickHouse reads a backslash before a quote as part of the string and Trino as its end, so
911
- // where a string stops is not known here: held.
912
- // ponytail: a ClickHouse read with an escaped quote in a string is held too; told apart by which
913
- // database answered if that is ever seen.
914
- return !/\\['"`]/.test(asked) && selects(statementOf(asked));
915
- };
916
- // A call passed on from this hook's own server, after its body was read: never held again.
917
- const PASSED = Symbol("cortad.passed");
918
- const passedOn = (args) => { const o = typeof args[0] === "string" || args[0] instanceof URL ? args[1] : args[0]; return Boolean(o && o[PASSED]); };
919
- let heldCount = 0;
920
- const fieldsOf = (text, type) => {
921
- try { const b = /form/i.test(type || "") ? Object.fromEntries(new URLSearchParams(text)) : JSON.parse(text); return b && typeof b === "object" && !Array.isArray(b) ? b : {}; } catch { return {}; }
922
- };
923
- const heldReply = (text, type) => JSON.stringify({ ...fieldsOf(text, type), id: `cortad-held-${++heldCount}`, status: "ok" });
924
- // Said once per method and host in the app's own output, where the error that follows is read:
925
- // a stand-in an SDK cannot parse otherwise reads as a bug in the app. It holds no word that marks
926
- // an error, so it is never quoted back as the app's own.
927
- const heldSaid = new Set();
928
- const heldSay = (method, host) => {
929
- if (heldSaid.has(`${method} ${host}`)) return;
930
- heldSaid.add(`${method} ${host}`);
931
- try { process.stderr.write(`cortad: a request Cortad sent made this app send ${method} to ${host}. Cortad kept that call on this machine and answered it with a stand-in success, since it could change something real. A failure right after this line may come from that stand-in rather than from your code. With that service's test key the call goes out as sent.\n`); } catch { /* no stderr */ }
932
- };
933
- // Named by method and host only: a webhook's path is its credential.
934
- const heldRow = (u, method, text, type, ctx) => { heldSay(method, u.hostname); row({ dep: { at: Date.now(), host: u.hostname, status: 200, held: true, ...(ctx ? { ex: ctx.id, turn: ctx.turn } : {}), called: [{ name: `${method} ${u.hostname}`.slice(0, 80), arguments: argsText(fieldsOf(text, type)) }] } }); };
935
- // http.request hands back a request before its body is written, so a held one is sent to a server
936
- // of this hook's own on this machine, which answers it. Started at the first tagged request.
937
- const HELD = Symbol("cortad.held");
938
- const heldWaiting = new Map();
939
- let heldPort = null, heldStarted = false, heldSent = 0;
940
- function heldWarm() {
941
- if (heldStarted) return;
942
- heldStarted = true;
943
- try {
944
- const server = http.createServer((req, res) => {
945
- const parts = [];
946
- req.on("data", (c) => { if (parts.length < 256) parts.push(c); });
947
- req.on("end", () => {
948
- const id = String(req.headers["x-cortad-held"] || "");
949
- const w = heldWaiting.get(id);
950
- heldWaiting.delete(id);
951
- const sent = Buffer.concat(parts);
952
- const text = sent.toString("utf8").slice(0, MAX);
953
- const headerOf = (name) => String(req.headers[name] || "");
954
- // The call goes on to where it was sent, as it was sent, or to `to` instead, and its
955
- // answer comes back through this server.
956
- const forward = (to) => {
957
- const { "x-cortad-held": _held, host: _host, connection: _connection, ...headers } = req.headers;
958
- const given = typeof w.args[0] === "string" || w.args[0] instanceof URL;
959
- const options = { ...((given ? w.args[1] : w.args[0]) || {}), headers, signal: undefined, [PASSED]: true, ...(to && !given ? { path: to.pathname + to.search } : {}) };
960
- // Sent inside the request that asked, so the call is recorded behind that reply.
961
- const send = () => (w.secure ? https : http).request(...(given ? [to ? to.href : w.args[0], options] : [options]));
962
- const up = w.ctx ? als.run(w.ctx, send) : send();
963
- up.on("response", (got) => { res.writeHead(got.statusCode || 502, got.headers); got.pipe(res); });
964
- up.on("error", () => { if (!res.headersSent) res.writeHead(502); res.end(); });
965
- res.on("close", () => up.destroy());
966
- return up.end(sent);
967
- };
968
- const answer = () => {
969
- if (w) heldRow(w.u, w.method, text, req.headers["content-type"], w.ctx);
970
- res.writeHead(200, { "content-type": "application/json" });
971
- res.end(heldReply(text, req.headers["content-type"]));
972
- };
973
- // A body that only reads, known now that it is here, goes on as it was sent.
974
- if (w && parts.length < 256 && readsOnly(text, w.u, headerOf, w.port)) return forward(null);
975
- // A call to a Qdrant collection goes to the run's copy of it, made now when this is its first write.
976
- if (w && w.copy) return copyOf(w.u, headerOf, w.copy).then((clone) => (clone ? forward(toCopy(w.u, clone, w.copy.call)) : answer()), answer);
977
- answer();
978
- });
979
- });
980
- server[HELD] = true;
981
- server.listen(0, "127.0.0.1", () => { heldPort = server.address().port; });
982
- server.unref();
983
- } catch { /* no server: a held write fails to connect instead of leaving */ }
984
- }
985
- const headerIn = (headers, name) => {
986
- if (!headers || typeof headers !== "object" || Array.isArray(headers)) return "";
987
- const k = Object.keys(headers).find((n) => n.toLowerCase() === name);
988
- return k ? String(headers[k]) : "";
989
- };
990
- // The call http.request was asked for, when it is one to hold.
991
- const heldCall = (args, secure) => {
992
- const first = args[0];
993
- const given = typeof first === "string" || first instanceof URL ? new URL(String(first)) : null;
994
- const o = (given ? (args[1] && typeof args[1] === "object" ? args[1] : {}) : first) || {};
995
- const host = String(o.hostname || (o.host && String(o.host).replace(/:\d+$/, "")) || (given && given.hostname) || "localhost");
996
- const path = String(o.path || (given ? given.pathname + given.search : "/"));
997
- const method = String(o.method || "GET").toUpperCase();
998
- const u = new URL(`${secure ? "https" : "http"}://${host.includes(":") && !host.startsWith("[") ? `[${host}]` : host}${path.startsWith("/") ? path : "/" + path}`);
999
- const headerOf = (name) => headerIn(o.headers, name) || (name === "authorization" && o.auth ? `Basic ${Buffer.from(String(o.auth)).toString("base64")}` : "");
1000
- const port = o.port || (given && given.port) || "";
1001
- // The server's address whole, port included, names a collection's copy; the hold is decided on
1002
- // the host alone, as before.
1003
- const at = new URL(u.href);
1004
- if (port) at.port = String(port);
1005
- const g = guarded(at, method);
1006
- if (!g && !heldHere(u, method, headerOf)) return null;
1007
- return { u: at, method, o, args: args.filter((a) => typeof a !== "function"), port, cb: args.find((a) => typeof a === "function"), ...(g && !g.hold ? { copy: g } : {}) };
1008
- };
1009
- const httpRequest = http.request;
1010
- const sendHeld = ({ u, method, o, cb, args, port, copy }, secure) => {
1011
- const id = String(++heldSent);
1012
- heldWaiting.set(id, { u, method, ctx: als.getStore(), args, port, secure, copy });
1013
- const headers = { ...(o.headers && typeof o.headers === "object" && !Array.isArray(o.headers) ? o.headers : {}), "x-cortad-held": id };
1014
- // Port 1 answers nothing: a write held before the server listens fails instead of leaving.
1015
- const req = httpRequest({ host: "127.0.0.1", port: heldPort || 1, method, path: u.pathname + u.search, headers, ...(o.timeout ? { timeout: o.timeout } : {}), ...(o.signal ? { signal: o.signal } : {}) }, cb);
1016
- // A TLS client waits for the handshake before it writes (Stripe's does).
1017
- if (secure) req.once("socket", (socket) => socket.once("connect", () => socket.emit("secureConnect")));
1018
- return req;
1019
- };
1020
- const heldFetch = async (realFetch, input, init) => {
1021
- const req = new Request(input, init);
1022
- const text = await req.clone().text().catch(() => "");
1023
- if (readsOnly(text, new URL(req.url), (name) => req.headers.get(name) || "")) return realFetch(req);
1024
- const u = new URL(req.url);
1025
- heldRow(u, req.method, text, req.headers.get("content-type"), als.getStore());
1026
- return new Response(heldReply(text, req.headers.get("content-type")), { status: 200, headers: { "content-type": "application/json" } });
1027
- };
1028
-
1029
734
  // Outbound: the model call your app makes while it handles that request.
1030
735
  const bodyText = (body) => (typeof body === "string" ? body : Buffer.isBuffer(body) ? body.toString("utf8") : body instanceof Uint8Array ? Buffer.from(body).toString("utf8") : "");
1031
736
  if (typeof globalThis.fetch === "function") {
1032
737
  const realFetch = globalThis.fetch;
1033
- // A call to a Qdrant collection, sent again through this same wrapper at the copy's address, so
1034
- // it is recorded like any retrieval; held when no copy could be made.
1035
- const guardedFetch = async (self, wrapped, input, init, u, method, g) => {
1036
- if (g.hold) return heldFetch(realFetch, input, init);
1037
- const req = new Request(input, init);
1038
- const clone = await (typeof g.copy === "string" ? g.copy : copyOf(u, (name) => req.headers.get(name) || "", g));
1039
- if (!clone) return heldFetch(realFetch, input, init);
1040
- const body = method === "GET" || method === "HEAD" ? undefined : await req.arrayBuffer();
1041
- return wrapped.call(self, toCopy(u, clone, g.call).href, { method, headers: req.headers, body, signal: req.signal });
1042
- };
1043
738
  globalThis.fetch = function (input, init) {
1044
739
  let url = null;
1045
- let hold = false;
1046
- let guard = null;
1047
- let at = null;
1048
- let method = "GET";
1049
740
  try {
1050
741
  const u = new URL(typeof input === "string" ? input : input && input.url ? input.url : String(input));
1051
- at = u;
1052
742
  if (isModelCall(u.host, u.pathname)) url = u;
1053
- method = String((init && init.method) || (input && input.method) || "GET").toUpperCase();
1054
- guard = !url ? guarded(u, method) : null;
1055
- hold = !url && !guard && heldHere(u, method, (name) => new Headers((init && init.headers) || (input && input.headers) || undefined).get(name));
1056
743
  } catch { /* not a URL we can read */ }
1057
- if (guard) return guardedFetch(this, globalThis.fetch, input, init, at, method, guard);
1058
- if (hold) return heldFetch(realFetch, input, init);
1059
744
  if (!url) {
1060
745
  // Every outbound host, named by the setting that points at it when one does, and by the
1061
746
  // host alone otherwise: the search provider behind TAVILY_API_KEY has no URL setting.
@@ -1097,9 +782,6 @@ if (FILE) {
1097
782
  for (const mod of [http, https]) {
1098
783
  const request = mod.request;
1099
784
  mod.request = function (...args) {
1100
- let held = null;
1101
- try { held = passedOn(args) ? null : heldCall(args, mod === https); } catch { /* not a call we can read: sent as asked */ }
1102
- if (held) return sendHeld(held, mod === https);
1103
785
  const req = request.apply(this, args);
1104
786
  try {
1105
787
  const o = typeof args[0] === "string" || args[0] instanceof URL ? new URL(String(args[0])) : args[0] || {};
package/lib/verbs.mjs CHANGED
@@ -6,6 +6,7 @@ import { askReach } from "./auto-reach.mjs";
6
6
  import { homeOf, projectOf } from "./home.mjs";
7
7
  import { reach as reachApp } from "./reach.mjs";
8
8
  import { fieldConnectText, fieldText, findingsText, finished, linesOf, notStartedText, refusedText, restartedText, runText, STARTING, startedText, statusText, testedLine, upWaitingText, waitingText } from "./text.mjs";
9
+ import { incidentsText, intelligenceText, outcomesText, queryText } from "./intel-text.mjs";
9
10
 
10
11
  // The ten things a coding agent may ask of Cortad, each a call to the API with this machine's key
11
12
  // and a rendering from lib/text.mjs; `reach` also sends to the app on this machine. The MCP tools
@@ -286,11 +287,65 @@ export function makeVerbs({ api, token, runner = () => null, fetchImpl = fetch,
286
287
  return res.ok ? { text: fieldConnectText(res.data), data: res.data } : failed(res);
287
288
  };
288
289
 
290
+ // A world for an AI that is not a chat (the server's world/run.ts): built on this machine from the
291
+ // app's own seed, planted with problems whose answers are known, discovered by the app's own code.
292
+ // `ask`: what the agent built or wants tested, kept with the report. Answers at once with the id.
293
+ const world = async ({ ask } = {}) => {
294
+ const res = await call("POST", "/mcp/world", ask ? { ask: String(ask) } : {});
295
+ if (!res.ok) return failed(res);
296
+ if (res.data?.joined) return { text: `A world is already being tested: ${res.data.jobId}.\nnext: world_status ${res.data.jobId}`, data: res.data };
297
+ if (!res.data?.started) return { text: `${res.data?.why ?? "No world started."}\nNothing ran.`, data: res.data, isError: true, code: "world_refused" };
298
+ const planted = Array.isArray(res.data.situations) && res.data.situations.length ? `Planted: ${res.data.situations.join("; ")}.` : `${res.data.worlds ?? ""} worlds.`;
299
+ return { text: `World started: ${res.data.jobId}. ${planted} Each is tested ${res.data.repeats} times. The person sees it at ${res.data.url}\nnext: world_status ${res.data.jobId}`, data: res.data };
300
+ };
301
+ // The one door: the agent says what it built or wants tested, and Cortad decides what kind of test
302
+ // that is. A world where the app has one and the ask names something it can plant; a run of the
303
+ // app's chat surfaces otherwise. The answer says which, and what to follow it with.
304
+ const test = async ({ ask } = {}) => {
305
+ const res = await call("POST", "/mcp/world", { ...(ask ? { ask: String(ask) } : {}), only: "matched" });
306
+ if (res.ok && res.data?.started) return { text: `This is tested in a world. World started: ${res.data.jobId}. Planted: ${(res.data.situations ?? []).join("; ")}. Each is tested ${res.data.repeats} times. The person sees it at ${res.data.url}\nnext: world_status ${res.data.jobId}`, data: res.data };
307
+ if (res.ok && res.data?.joined) return { text: `A world is already being tested: ${res.data.jobId}.\nnext: world_status ${res.data.jobId}`, data: res.data };
308
+ const why = res.ok ? (res.data?.unmatched ? "Nothing a world can plant is what that is about, so it is tested by a run of the app's chat surfaces." : "No world is written for this app yet, so it is tested by a run of the app's chat surfaces.") : "The world could not be asked, so it is tested by a run of the app's chat surfaces.";
309
+ const ran = await run({});
310
+ return { ...ran, text: `${why}\n${ran.text}` };
311
+ };
312
+ const worldStatus = async ({ jobId } = {}) => {
313
+ if (!jobId) return { text: "Name the world: world_status <id>.", isError: true, code: "no_id" };
314
+ const res = await call("GET", `/mcp/world/${encodeURIComponent(String(jobId))}`);
315
+ if (!res.ok) return failed(res);
316
+ const running = res.data.status === "queued" || res.data.status === "running";
317
+ return { text: `${res.data.text}\nThe person sees this at ${res.data.url}\nnext: ${running ? `world_status ${jobId}` : "status"}`, data: res.data };
318
+ };
319
+
289
320
  const field = async ({ days } = {}) => {
290
321
  const res = await call("GET", `/mcp/field${days ? `?days=${Number(days)}` : ""}`);
291
322
  return res.ok ? { text: fieldText(res.data), data: res.data } : failed(res);
292
323
  };
293
324
 
325
+ // The Intelligence Layer: the board in numbers, what is wrong, what counts as an outcome, a
326
+ // subscription to incidents, and a question to the warehouse.
327
+ const intelligence = async ({ days } = {}) => {
328
+ const res = await call("GET", `/mcp/intelligence${days ? `?days=${Number(days)}` : ""}`);
329
+ return res.ok ? { text: intelligenceText(res.data), data: res.data } : failed(res);
330
+ };
331
+ const incidents = async () => {
332
+ const res = await call("GET", "/mcp/incidents");
333
+ return res.ok ? { text: incidentsText(res.data), data: res.data } : failed(res);
334
+ };
335
+ const outcomes = async ({ proposals } = {}) => {
336
+ const res = proposals ? await call("POST", "/mcp/outcomes", { proposals }) : await call("GET", "/mcp/outcomes");
337
+ return res.ok ? { text: outcomesText(res.data), data: res.data } : failed(res);
338
+ };
339
+ const subscribe = async ({ url }) => {
340
+ const res = await call("POST", "/mcp/subscribe", { url });
341
+ return res.ok ? { text: `Subscribed. ${res.data.said}
342
+ Secret, shown once: ${res.data.secret}`, data: res.data } : failed(res);
343
+ };
344
+ const query = async (ask) => {
345
+ const res = await call("POST", "/mcp/query", ask);
346
+ return res.ok ? { text: queryText(res.data), data: res.data } : { text: queryText(res.data ?? { error: res.text }), data: res.data };
347
+ };
348
+
294
349
  // `status --changed`: the files Cortad cites that changed since the latest run started, with the
295
350
  // findings at them. Empty when there is nothing to say, or nothing could be asked. The code review's
296
351
  // files are those of the review shown: the read's misses, or the lines Cortad's team's review cites.
@@ -312,7 +367,7 @@ export function makeVerbs({ api, token, runner = () => null, fetchImpl = fetch,
312
367
  };
313
368
 
314
369
  // `post` and `changed` serve lib/cli.mjs; the MCP answers only the names in TOOLS.
315
- return { status, reach, run, run_status: runStatus, wait, findings, verify, dispute, feedback, field_connect: fieldConnect, field, post, changed };
370
+ return { status, reach, run, run_status: runStatus, wait, findings, verify, dispute, feedback, field_connect: fieldConnect, field, world, world_status: worldStatus, test, connect: fieldConnect, intelligence, incidents, outcomes, subscribe, query, post, changed };
316
371
  }
317
372
 
318
373
  const sizeOf = (path) => { try { return statSync(path).size; } catch { return 0; } };