@cognolo/runtime 0.4.5 → 0.4.6

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/dist/bundle.d.ts CHANGED
@@ -41,6 +41,13 @@ export declare const BundleSchema: z.ZodObject<{
41
41
  cap: z.ZodNumber;
42
42
  holders: z.ZodArray<z.ZodString>;
43
43
  }, z.core.$strip>>;
44
+ pace: z.ZodCatch<z.ZodOptional<z.ZodObject<{
45
+ perPerson: z.ZodOptional<z.ZodObject<{
46
+ max: z.ZodNumber;
47
+ hours: z.ZodNumber;
48
+ }, z.core.$strip>>;
49
+ appPerDay: z.ZodOptional<z.ZodNumber>;
50
+ }, z.core.$strip>>>;
44
51
  savedWorkflows: z.ZodOptional<z.ZodObject<{
45
52
  max: z.ZodNumber;
46
53
  full: z.ZodString;
package/dist/bundle.js CHANGED
@@ -22,6 +22,10 @@ export const BundleSchema = z.object({
22
22
  readonly: z.string().max(200).optional(),
23
23
  notice: z.string().max(200).optional(),
24
24
  people: z.object({ cap: z.number().int().positive().max(100000), holders: z.array(z.string().regex(/^[0-9a-f]{32}$/)).max(100000) }).optional(),
25
+ pace: z.object({
26
+ perPerson: z.object({ max: z.number().int().min(1).max(10000), hours: z.number().int().min(1).max(24) }).optional(),
27
+ appPerDay: z.number().int().min(1).max(1_000_000).optional(),
28
+ }).optional().catch(undefined),
25
29
  savedWorkflows: z.object({ max: z.number().int().positive().max(1000), full: z.string().max(200) }).optional(),
26
30
  meter: z.object({
27
31
  plan: z.string().min(1).max(40),
package/dist/cloud.d.ts CHANGED
@@ -104,7 +104,14 @@ export interface CognoloCloud<C = unknown> {
104
104
  max: number;
105
105
  full: string;
106
106
  };
107
- meter?: UsageMeter;
107
+ meter?: UsageMeter; /** The owner's spending controls (`Pace`). */
108
+ pace?: {
109
+ perPerson?: {
110
+ max: number;
111
+ hours: number;
112
+ };
113
+ appPerDay?: number;
114
+ };
108
115
  };
109
116
  /** A person's id as the cloud may see it: a hash keyed with this environment's key, so Cognolo never sees the id. */
110
117
  who?(person: string): string;
@@ -220,6 +227,15 @@ export interface CognoloCloud<C = unknown> {
220
227
  top: string[];
221
228
  stopped: boolean;
222
229
  }): void;
230
+ /** The owner's pace refused a question here (sent once per person per window, and once a day for the whole app). The person's code only. */
231
+ reportPace?(p: {
232
+ who?: string;
233
+ limit: "person" | "app";
234
+ max: number;
235
+ hours?: number;
236
+ }): void;
237
+ /** Where the route counts limits: "shared" (a store every server sees) or "memory" (this server's only). Sent with the heartbeat. */
238
+ reportCounts?(where: "shared" | "memory"): void;
223
239
  /**
224
240
  * Reads that don't name the records they return (no `refs`), as people have opened them since this server started, so
225
241
  * the owner sees they can't be offered to people's own AI. Names only; sent with the heartbeat.
package/dist/cloud.js CHANGED
@@ -42,6 +42,7 @@ export function cognoloCloud(o) {
42
42
  let credit = null;
43
43
  let modelKey = null;
44
44
  let modelChoice = null;
45
+ let countsAt = null;
45
46
  let passkeys = null;
46
47
  let toolList = null;
47
48
  const unnamedReads = new Set();
@@ -94,7 +95,7 @@ export function cognoloCloud(o) {
94
95
  await call("/v1/runtime/heartbeat", {
95
96
  method: "POST",
96
97
  headers: { "content-type": "application/json" },
97
- body: JSON.stringify({ env: o.env, commit: deployedCommit(o.commit), version: RUNTIME_VERSION, rulesVersion: bundle?.version ?? null, tools: toolList?.length ?? o.toolCount ?? null, ...(toolList ? { toolList } : {}), tenant: tenant ?? null, credit, lookLocked: locked, layouts, modelKey, modelChoice, ...(passkeys ? { passkeys } : {}), ...(unnamedSeen ? { outsideUnnamed: [...unnamedReads].sort() } : {}) }),
98
+ body: JSON.stringify({ env: o.env, commit: deployedCommit(o.commit), version: RUNTIME_VERSION, rulesVersion: bundle?.version ?? null, tools: toolList?.length ?? o.toolCount ?? null, ...(toolList ? { toolList } : {}), tenant: tenant ?? null, credit, lookLocked: locked, layouts, modelKey, modelChoice, ...(countsAt ? { counts: countsAt } : {}), ...(passkeys ? { passkeys } : {}), ...(unnamedSeen ? { outsideUnnamed: [...unnamedReads].sort() } : {}) }),
98
99
  });
99
100
  }
100
101
  catch { }
@@ -356,6 +357,17 @@ export function cognoloCloud(o) {
356
357
  trim();
357
358
  void flush();
358
359
  },
360
+ reportPace(p) {
361
+ queue.push({ id: randomUUID(), at: now().toISOString(), kind: "pace", tool: "", ...(p.who && /^[0-9a-f]{32}$/.test(p.who) ? { who: p.who } : {}), limit: p.limit, max: p.max, ...(p.hours ? { hours: p.hours } : {}) });
362
+ trim();
363
+ void flush();
364
+ },
365
+ reportCounts(where) {
366
+ if (where === countsAt)
367
+ return;
368
+ countsAt = where;
369
+ void (beating ?? Promise.resolve()).then(beat);
370
+ },
359
371
  reportModelChoice(state) {
360
372
  if (state === modelChoice)
361
373
  return;
package/dist/outside.d.ts CHANGED
@@ -23,6 +23,24 @@ export type OverLimit = {
23
23
  why: "minute" | "hour" | "day" | "paused";
24
24
  resets?: Date;
25
25
  };
26
+ /** A question the owner's pace refused: which limit, its number and window, when it starts again, and whether this is the first refusal in that window (said to the cloud once). */
27
+ export type PaceOver = {
28
+ limit: "person" | "app";
29
+ max: number;
30
+ hours?: number;
31
+ resets: Date;
32
+ first: boolean;
33
+ };
34
+ /** The owner's pace, as the bundle carries it. */
35
+ export type Pace = {
36
+ perPerson?: {
37
+ max: number;
38
+ hours: number;
39
+ };
40
+ appPerDay?: number;
41
+ };
42
+ /** What the person is told when the owner's pace stops a question, with the time it starts again in their zone (UTC if unknown). */
43
+ export declare function paceText(p: Pick<PaceOver, "limit" | "max" | "hours" | "resets">, timeZone?: string): string;
26
44
  /** A trip of the fuse: the codes of the people whose AI read the most records this hour, and whether it now stays off. */
27
45
  export type Trip = {
28
46
  top: string[];
@@ -72,6 +90,12 @@ export declare function outsideCounter(o?: {
72
90
  * person's hour (the owner's number), then the environment's.
73
91
  */
74
92
  change(key: string, perHourCap?: number): Promise<OverLimit | null>;
93
+ /**
94
+ * One question by this person under the owner's pace: their window first (a refused one counts too, so a loop gets no
95
+ * further), then the environment's day, which counts only questions within the person's number. The windows start at
96
+ * fixed times (every `hours` from midnight UTC), so every server agrees on them. Throws when the store can't be reached.
97
+ */
98
+ pace(key: string, p: Pace): Promise<PaceOver | null>;
75
99
  /** The conversation already waiting for this person's same change (tool and input), if one was noted. */
76
100
  waiting: (key: string, same: string) => Promise<string | null>;
77
101
  /** Notes the conversation waiting for this person's same change, until it expires. */
package/dist/outside.js CHANGED
@@ -4,6 +4,20 @@ export const OUTSIDE_PAUSED = "People's own AI is paused in this app for now.";
4
4
  export const OUTSIDE_CHANGE_LIMITS = { hour: 60, envHour: 1000, expiresMs: 3_600_000 };
5
5
  export const OUTSIDE_CHANGES_PAUSED = "People's own AI can't ask for more changes in this app this hour.";
6
6
  const HOUR = 3_600_000, DAY = 86_400_000, MINUTE = 60_000;
7
+ export function paceText(p, timeZone) {
8
+ let zone = "UTC";
9
+ if (timeZone)
10
+ try {
11
+ new Intl.DateTimeFormat("en-US", { timeZone });
12
+ zone = timeZone;
13
+ }
14
+ catch { }
15
+ const at = new Intl.DateTimeFormat("en-GB", { timeZone: zone, hour: "2-digit", minute: "2-digit", hourCycle: "h23" }).format(p.resets);
16
+ const waiting = "Changes waiting for you can still be approved or declined.";
17
+ return p.limit === "person"
18
+ ? `You've asked ${p.max.toLocaleString("en-US")} ${p.max === 1 ? "question" : "questions"}, the most this app allows every ${!p.hours || p.hours === 1 ? "hour" : `${p.hours} hours`}. You can ask again at ${at}. ${waiting}`
19
+ : `This app's questions for today are used. They start again at ${at}. ${waiting}`;
20
+ }
7
21
  export function memoryCounts(now = Date.now) {
8
22
  const counts = new Map();
9
23
  const sets = new Map();
@@ -128,6 +142,22 @@ export function outsideCounter(o = {}) {
128
142
  return { why: "paused", resets: new Date((h + 1) * HOUR) };
129
143
  return null;
130
144
  },
145
+ async pace(key, p) {
146
+ const t = now();
147
+ if (p.perPerson) {
148
+ const { max, hours } = p.perPerson, w = hours * HOUR, n = Math.floor(t / w);
149
+ const c = await counts.add(`pace:${key}:${hours}:${n}`, 1, (n + 2) * w);
150
+ if (c > max)
151
+ return { limit: "person", max, hours, resets: new Date((n + 1) * w), first: c === max + 1 };
152
+ }
153
+ if (p.appPerDay) {
154
+ const d = Math.floor(t / DAY);
155
+ const c = await counts.add(`pace:env:d:${d}`, 1, (d + 2) * DAY);
156
+ if (c > p.appPerDay)
157
+ return { limit: "app", max: p.appPerDay, resets: new Date((d + 1) * DAY), first: c === p.appPerDay + 1 };
158
+ }
159
+ return null;
160
+ },
131
161
  waiting: (key, same) => counts.read(`chgwait:${key}:${same}`),
132
162
  async noteWaiting(key, same, id, until) {
133
163
  const k = `chgwait:${key}:${same}`;
package/dist/route.js CHANGED
@@ -15,7 +15,7 @@ import { PICTURE_LABEL, PICTURE_LIMITS, picturesIn, readPicture } from "./pictur
15
15
  import { cleanPages, cleanRefs, safeHref } from "./links.js";
16
16
  import { createHash, randomUUID } from "node:crypto";
17
17
  import { canon, decideCall, outsideMayAsk, recheckApproval, runCall } from "./gate.js";
18
- import { hourId, OUTSIDE_CHANGE_LIMITS, OUTSIDE_CHANGES_PAUSED, outsideCounter, overLimitText } from "./outside.js";
18
+ import { hourId, OUTSIDE_CHANGE_LIMITS, OUTSIDE_CHANGES_PAUSED, outsideCounter, overLimitText, paceText } from "./outside.js";
19
19
  import { forModel } from "./sensitive.js";
20
20
  import { LOOKUP_MAX_CHARS, MASKED_NOTE, UNTRUSTED_NOTE } from "./agent.js";
21
21
  import { PAGE_TOOL, pageTool, readPage } from "./page.js";
@@ -460,6 +460,20 @@ export function cognoloRoute(o) {
460
460
  return x;
461
461
  };
462
462
  const counter = outsideCounter({ counts: o.outsideCounts, env: o.env, onTrip: (t) => o.cloud?.reportFuse?.(t) });
463
+ o.cloud?.reportCounts?.(o.outsideCounts ? "shared" : "memory");
464
+ const personKey = (who, scope) => {
465
+ if (who)
466
+ return who;
467
+ let scoped = null;
468
+ try {
469
+ scoped = scope === undefined || scope === null ? null : canon(scope);
470
+ }
471
+ catch {
472
+ scoped = null;
473
+ }
474
+ return scoped ? `s:${createHash("sha256").update(`cognolo-scope:${scoped}`).digest("hex").slice(0, 32)}` : null;
475
+ };
476
+ let paceStoreSaid = false;
463
477
  const guardsNow = () => { const c = o.cloud?.protect?.(); return { hide: [...(o.protect?.hide ?? []), ...(c?.hide ?? [])], mask: [...(o.protect?.mask ?? []), ...(c?.mask ?? [])] }; };
464
478
  const changesHere = () => !!o.passkeys && pkOrigins.length > 0 && !!o.passkey?.freshSignIn && !o.cloud?.passkey?.().clickOnly;
465
479
  const logOutside = async (scope, via, row) => { if (o.outsideLog)
@@ -488,16 +502,9 @@ export function cognoloRoute(o) {
488
502
  return no(GATE_CANT);
489
503
  }
490
504
  const who = opened.person && cloud.who ? cloud.who(opened.person) : undefined;
491
- let scoped = null;
492
- try {
493
- scoped = opened.scope === undefined || opened.scope === null ? null : canon(opened.scope);
494
- }
495
- catch {
496
- scoped = null;
497
- }
498
- if (!who && !scoped)
505
+ const key = personKey(who, opened.scope);
506
+ if (!key)
499
507
  return no(GATE_CANT);
500
- const key = who ?? `s:${createHash("sha256").update(`cognolo-scope:${scoped}`).digest("hex").slice(0, 32)}`;
501
508
  await counter.ack(cloud.outside().ack);
502
509
  const over = await counter.call(key);
503
510
  const def = opened.tools.find((t) => t.name === g.tool);
@@ -901,6 +908,25 @@ export function cognoloRoute(o) {
901
908
  await logOutside(scope, via, row("ran", outcome.ok, summary));
902
909
  return await finish(outcome.ok ? "ran" : "failed", "approved", outcome.ok ? short(summary, 500) : undefined);
903
910
  }
911
+ if (limits.pace && (message || start || make)) {
912
+ const key = personKey(who, scope);
913
+ let over = null;
914
+ try {
915
+ if (key)
916
+ over = await counter.pace(key, limits.pace);
917
+ }
918
+ catch {
919
+ if (!paceStoreSaid) {
920
+ paceStoreSaid = true;
921
+ console.error("Cognolo: the question limits couldn't be counted, so questions are going ahead uncounted.");
922
+ }
923
+ }
924
+ if (over) {
925
+ if (over.first)
926
+ o.cloud?.reportPace?.({ who, limit: over.limit, max: over.max, ...(over.hours ? { hours: over.hours } : {}) });
927
+ return Response.json({ error: paceText(over, opened.timeZone), paced: over.limit }, { status: 429, headers: { "Cache-Control": "no-store", "Retry-After": String(Math.max(1, Math.ceil((over.resets.getTime() - Date.now()) / 1000))) } });
928
+ }
929
+ }
904
930
  if (make) {
905
931
  if (!existing)
906
932
  return json({ error: "Open the conversation to make a workflow from." }, 400);
package/dist/version.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /** The runtime's version, reported to the cloud on every heartbeat. A test keeps it equal to package.json. */
2
- export declare const RUNTIME_VERSION = "0.4.5";
2
+ export declare const RUNTIME_VERSION = "0.4.6";
3
3
  /**
4
4
  * The deployed git commit: the app's own setting, then `COGNOLO_COMMIT`, then a host's own variable (only a plain git hash
5
5
  * counts from those). Null when nothing says, as when running on a developer's machine.
package/dist/version.js CHANGED
@@ -1,4 +1,4 @@
1
- export const RUNTIME_VERSION = "0.4.5";
1
+ export const RUNTIME_VERSION = "0.4.6";
2
2
  const HOST_COMMIT_VARS = ["VERCEL_GIT_COMMIT_SHA", "RENDER_GIT_COMMIT", "RAILWAY_GIT_COMMIT_SHA", "COMMIT_REF", "CF_PAGES_COMMIT_SHA", "HEROKU_SLUG_COMMIT", "SOURCE_VERSION", "GITHUB_SHA"];
3
3
  export function deployedCommit(own, env = typeof process === "undefined" ? {} : process.env) {
4
4
  if (own)
@@ -14,6 +14,12 @@ export function deployedCommit(own, env = typeof process === "undefined" ? {} :
14
14
  }
15
15
  export const installSpec = (version) => `@cognolo/runtime@${version}`;
16
16
  export const RELEASES = [
17
+ {
18
+ version: "0.4.6",
19
+ date: "2026-12-05",
20
+ summary: "The owner's spending controls: a limit on how many questions each person may ask every few hours, and on how many the whole app may ask each day. A question past a limit gets a plain answer saying when the person can ask again; approving or declining a waiting change is never counted. The owner is told when a limit is reached.",
21
+ steps: ["Nothing to do beyond the usual update. If the app runs on more than one server, pass the store's outsideCounts to the route, so every server counts the same questions; the Setup page says which the app uses."],
22
+ },
17
23
  {
18
24
  version: "0.4.5",
19
25
  date: "2026-12-05",
package/package.json CHANGED
@@ -1,71 +1,71 @@
1
- {
2
- "name": "@cognolo/runtime",
3
- "version": "0.4.5",
4
- "description": "Cognolo's runtime: an AI assistant inside your own web app that acts through your app's own functions and permission checks, with a person approving changes.",
5
- "type": "module",
6
- "license": "SEE LICENSE IN LICENSE",
7
- "publishConfig": {
8
- "access": "public"
9
- },
10
- "main": "./dist/index.js",
11
- "types": "./dist/index.d.ts",
12
- "bin": {
13
- "cognolo": "dist/init-cli.js"
14
- },
15
- "exports": {
16
- ".": {
17
- "types": "./dist/index.d.ts",
18
- "default": "./dist/index.js"
19
- },
20
- "./react": {
21
- "types": "./dist/react/index.d.ts",
22
- "default": "./dist/react/index.js"
23
- },
24
- "./setup": {
25
- "types": "./dist/setup.d.ts",
26
- "default": "./dist/setup.js"
27
- }
28
- },
29
- "files": [
30
- "dist",
31
- "docs/store.md"
32
- ],
33
- "sideEffects": false,
34
- "engines": {
35
- "node": ">=20"
36
- },
37
- "scripts": {
38
- "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json --removeComments && tsc -p tsconfig.build.json --emitDeclarationOnly",
39
- "prepare": "npm run build",
40
- "prepack": "npm run build",
41
- "smoke": "node scripts/smoke.mjs",
42
- "test": "tsx --import ./test/env.ts --test test/*.test.ts",
43
- "eval": "tsx eval/run.ts",
44
- "read": "tsx reader/cli.ts",
45
- "answer": "tsx scripts/answer.ts",
46
- "propose": "tsx reader/propose-cli.ts",
47
- "github-app": "tsx github/serve.ts",
48
- "typecheck": "tsc --noEmit"
49
- },
50
- "peerDependencies": {
51
- "@anthropic-ai/sdk": "^1.6.0",
52
- "zod": "^4.6.7",
53
- "react": "^18.2.0 || ^19.0.0"
54
- },
55
- "devDependencies": {
56
- "@anthropic-ai/sdk": "^1.6.0",
57
- "@types/node": "^20",
58
- "@types/react": "^19.3.0",
59
- "@types/react-dom": "^19.3.0",
60
- "react": "^19.3.1",
61
- "react-dom": "^19.3.1",
62
- "tsx": "^4.23.15",
63
- "typescript": "^5",
64
- "zod": "^4.6.7"
65
- },
66
- "peerDependenciesMeta": {
67
- "react": {
68
- "optional": true
69
- }
70
- }
71
- }
1
+ {
2
+ "name": "@cognolo/runtime",
3
+ "version": "0.4.6",
4
+ "description": "Cognolo's runtime: an AI assistant inside your own web app that acts through your app's own functions and permission checks, with a person approving changes.",
5
+ "type": "module",
6
+ "license": "SEE LICENSE IN LICENSE",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "bin": {
13
+ "cognolo": "dist/init-cli.js"
14
+ },
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./react": {
21
+ "types": "./dist/react/index.d.ts",
22
+ "default": "./dist/react/index.js"
23
+ },
24
+ "./setup": {
25
+ "types": "./dist/setup.d.ts",
26
+ "default": "./dist/setup.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "docs/store.md"
32
+ ],
33
+ "sideEffects": false,
34
+ "engines": {
35
+ "node": ">=20"
36
+ },
37
+ "scripts": {
38
+ "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json --removeComments && tsc -p tsconfig.build.json --emitDeclarationOnly",
39
+ "prepare": "npm run build",
40
+ "prepack": "npm run build",
41
+ "smoke": "node scripts/smoke.mjs",
42
+ "test": "tsx --import ./test/env.ts --test test/*.test.ts",
43
+ "eval": "tsx eval/run.ts",
44
+ "read": "tsx reader/cli.ts",
45
+ "answer": "tsx scripts/answer.ts",
46
+ "propose": "tsx reader/propose-cli.ts",
47
+ "github-app": "tsx github/serve.ts",
48
+ "typecheck": "tsc --noEmit"
49
+ },
50
+ "peerDependencies": {
51
+ "@anthropic-ai/sdk": "^1.6.0",
52
+ "zod": "^4.6.7",
53
+ "react": "^18.2.0 || ^19.0.0"
54
+ },
55
+ "devDependencies": {
56
+ "@anthropic-ai/sdk": "^1.6.0",
57
+ "@types/node": "^20",
58
+ "@types/react": "^19.3.0",
59
+ "@types/react-dom": "^19.3.0",
60
+ "react": "^19.3.1",
61
+ "react-dom": "^19.3.1",
62
+ "tsx": "^4.23.15",
63
+ "typescript": "^5",
64
+ "zod": "^4.6.7"
65
+ },
66
+ "peerDependenciesMeta": {
67
+ "react": {
68
+ "optional": true
69
+ }
70
+ }
71
+ }