cognia-sdk 0.1.3 → 0.1.4

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.
@@ -74,24 +74,59 @@ class Cognia {
74
74
  this.f = o.fetch ?? fetch;
75
75
  this.onError = o.onError;
76
76
  }
77
+ /** Every string that reaches the host through an error or a reason has the bearer removed first (independent E2E F28). */
78
+ scrub(v, token) {
79
+ if (!token || token.length < 8)
80
+ return v;
81
+ const re = new RegExp(token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g");
82
+ const walk = (x) => {
83
+ if (typeof x === "string")
84
+ return x.replace(re, "[redacted]");
85
+ if (Array.isArray(x))
86
+ return x.map(walk);
87
+ if (x && typeof x === "object") {
88
+ const o = {};
89
+ for (const [k, val] of Object.entries(x))
90
+ o[k] = walk(val);
91
+ return o;
92
+ }
93
+ return x;
94
+ };
95
+ return walk(v);
96
+ }
77
97
  async call(method, path, body, timeoutMs) {
78
98
  const ac = new AbortController();
79
- const t = timeoutMs ? setTimeout(() => ac.abort(), timeoutMs) : null;
99
+ let timedOut = false;
100
+ const t = timeoutMs ? setTimeout(() => { timedOut = true; ac.abort(); }, timeoutMs) : null;
101
+ let token = null;
80
102
  try {
81
- // delegated auth: the provider is asked on EVERY request; its answer is used once and never stored on the client
82
- const token = this.tokenProvider ? await this.tokenProvider() : this.key;
103
+ // delegated auth: the provider is asked on EVERY request, INSIDE the same deadline as the request itself (independent
104
+ // E2E F27: a pending provider used to block the host model), and its answer is used once, never stored on the client
105
+ const deadline = new Promise((_, reject) => { if (timeoutMs)
106
+ setTimeout(() => reject(Object.assign(new Error("tokenProvider timed out"), { name: "AbortError" })), timeoutMs); });
107
+ token = this.tokenProvider ? await (timeoutMs ? Promise.race([Promise.resolve(this.tokenProvider()), deadline]) : this.tokenProvider()) : this.key;
83
108
  if (!token)
84
109
  throw new CogniaError(`Cognia ${method} ${path} → no credential`, 0, null);
85
- const r = await this.f(this.baseUrl + path, { method, headers: { authorization: `Bearer ${token}`, "content-type": "application/json", "user-agent": "cognia-sdk/0.1.3" }, body: body === undefined ? undefined : JSON.stringify(body), signal: ac.signal });
110
+ const r = await this.f(this.baseUrl + path, { method, headers: { authorization: `Bearer ${token}`, "content-type": "application/json", "user-agent": "cognia-sdk/0.1.4" }, body: body === undefined ? undefined : JSON.stringify(body), signal: ac.signal });
86
111
  let j = null;
87
112
  try {
88
113
  j = await r.json();
89
114
  }
90
115
  catch { /* no body */ }
91
116
  if (!r.ok)
92
- throw new CogniaError(`Cognia ${method} ${path} → ${r.status}`, r.status, j);
117
+ throw new CogniaError(`Cognia ${method} ${path} → ${r.status}`, r.status, this.scrub(j, token));
93
118
  return j;
94
119
  }
120
+ catch (e) {
121
+ // whatever the transport or the provider threw: the bearer never travels inside the error. A fresh error is built
122
+ // (a DOMException's message is read-only), keeping the name, status and a scrubbed body.
123
+ const src = e instanceof Error ? e : new Error(String(e));
124
+ const msg = token ? this.scrub(src.message, token) : src.message;
125
+ const out = src instanceof CogniaError ? new CogniaError(msg, src.status, token ? this.scrub(src.body, token) : src.body) : Object.assign(new Error(msg), { name: src.name });
126
+ if (timedOut)
127
+ out.name = "AbortError";
128
+ throw out;
129
+ }
95
130
  finally {
96
131
  if (t)
97
132
  clearTimeout(t);
@@ -40,6 +40,8 @@ export declare class Cognia {
40
40
  private readonly f;
41
41
  private readonly onError;
42
42
  constructor(o: CogniaOptions);
43
+ /** Every string that reaches the host through an error or a reason has the bearer removed first (independent E2E F28). */
44
+ private scrub;
43
45
  private call;
44
46
  /**
45
47
  * The Memories and Skills this identity may use for `task`, or an explicit ABSTAIN. Never throws: on a timeout or a
@@ -69,24 +69,59 @@ export class Cognia {
69
69
  this.f = o.fetch ?? fetch;
70
70
  this.onError = o.onError;
71
71
  }
72
+ /** Every string that reaches the host through an error or a reason has the bearer removed first (independent E2E F28). */
73
+ scrub(v, token) {
74
+ if (!token || token.length < 8)
75
+ return v;
76
+ const re = new RegExp(token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g");
77
+ const walk = (x) => {
78
+ if (typeof x === "string")
79
+ return x.replace(re, "[redacted]");
80
+ if (Array.isArray(x))
81
+ return x.map(walk);
82
+ if (x && typeof x === "object") {
83
+ const o = {};
84
+ for (const [k, val] of Object.entries(x))
85
+ o[k] = walk(val);
86
+ return o;
87
+ }
88
+ return x;
89
+ };
90
+ return walk(v);
91
+ }
72
92
  async call(method, path, body, timeoutMs) {
73
93
  const ac = new AbortController();
74
- const t = timeoutMs ? setTimeout(() => ac.abort(), timeoutMs) : null;
94
+ let timedOut = false;
95
+ const t = timeoutMs ? setTimeout(() => { timedOut = true; ac.abort(); }, timeoutMs) : null;
96
+ let token = null;
75
97
  try {
76
- // delegated auth: the provider is asked on EVERY request; its answer is used once and never stored on the client
77
- const token = this.tokenProvider ? await this.tokenProvider() : this.key;
98
+ // delegated auth: the provider is asked on EVERY request, INSIDE the same deadline as the request itself (independent
99
+ // E2E F27: a pending provider used to block the host model), and its answer is used once, never stored on the client
100
+ const deadline = new Promise((_, reject) => { if (timeoutMs)
101
+ setTimeout(() => reject(Object.assign(new Error("tokenProvider timed out"), { name: "AbortError" })), timeoutMs); });
102
+ token = this.tokenProvider ? await (timeoutMs ? Promise.race([Promise.resolve(this.tokenProvider()), deadline]) : this.tokenProvider()) : this.key;
78
103
  if (!token)
79
104
  throw new CogniaError(`Cognia ${method} ${path} → no credential`, 0, null);
80
- const r = await this.f(this.baseUrl + path, { method, headers: { authorization: `Bearer ${token}`, "content-type": "application/json", "user-agent": "cognia-sdk/0.1.3" }, body: body === undefined ? undefined : JSON.stringify(body), signal: ac.signal });
105
+ const r = await this.f(this.baseUrl + path, { method, headers: { authorization: `Bearer ${token}`, "content-type": "application/json", "user-agent": "cognia-sdk/0.1.4" }, body: body === undefined ? undefined : JSON.stringify(body), signal: ac.signal });
81
106
  let j = null;
82
107
  try {
83
108
  j = await r.json();
84
109
  }
85
110
  catch { /* no body */ }
86
111
  if (!r.ok)
87
- throw new CogniaError(`Cognia ${method} ${path} → ${r.status}`, r.status, j);
112
+ throw new CogniaError(`Cognia ${method} ${path} → ${r.status}`, r.status, this.scrub(j, token));
88
113
  return j;
89
114
  }
115
+ catch (e) {
116
+ // whatever the transport or the provider threw: the bearer never travels inside the error. A fresh error is built
117
+ // (a DOMException's message is read-only), keeping the name, status and a scrubbed body.
118
+ const src = e instanceof Error ? e : new Error(String(e));
119
+ const msg = token ? this.scrub(src.message, token) : src.message;
120
+ const out = src instanceof CogniaError ? new CogniaError(msg, src.status, token ? this.scrub(src.body, token) : src.body) : Object.assign(new Error(msg), { name: src.name });
121
+ if (timedOut)
122
+ out.name = "AbortError";
123
+ throw out;
124
+ }
90
125
  finally {
91
126
  if (t)
92
127
  clearTimeout(t);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cognia-sdk",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Cognia for your model calls: one line gives any agent the Memories and Skills it may use, with provenance and honest receipts. Fails open.",
5
5
  "license": "MIT",
6
6
  "author": "Cognia",