@cortexkit/common-auth 0.38.0 → 0.39.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.
@@ -1,2 +1,8 @@
1
- /** Canonicalize the parent even when the store file has not been created yet. */
1
+ /**
2
+ * Canonicalize a store path even when the store file, or folders above it,
3
+ * have not been created yet. The nearest existing ancestor folder is resolved
4
+ * through symlinks and junctions; the missing folders and the file name are
5
+ * appended unchanged. Two aliases of one folder therefore agree before and
6
+ * after a process creates the store's folder.
7
+ */
2
8
  export declare function canonicalLimitsPath(path: string): string;
@@ -1,14 +1,31 @@
1
1
  import { realpathSync } from 'node:fs';
2
2
  import { basename, dirname, join, resolve } from 'node:path';
3
- /** Canonicalize the parent even when the store file has not been created yet. */
3
+ /**
4
+ * Canonicalize a store path even when the store file, or folders above it,
5
+ * have not been created yet. The nearest existing ancestor folder is resolved
6
+ * through symlinks and junctions; the missing folders and the file name are
7
+ * appended unchanged. Two aliases of one folder therefore agree before and
8
+ * after a process creates the store's folder.
9
+ */
4
10
  export function canonicalLimitsPath(path) {
5
- try {
6
- return join(realpathSync.native(dirname(path)), basename(path));
7
- }
8
- catch (error) {
9
- const code = error.code;
10
- if (code === 'ENOENT' || code === 'ENOTDIR')
11
- return resolve(path);
12
- throw error;
11
+ const absolute = resolve(path);
12
+ const missing = [basename(absolute)];
13
+ let folder = dirname(absolute);
14
+ for (;;) {
15
+ try {
16
+ return join(realpathSync.native(folder), ...missing.reverse());
17
+ }
18
+ catch (error) {
19
+ const code = error.code;
20
+ if (code !== 'ENOENT' && code !== 'ENOTDIR')
21
+ throw error;
22
+ const parent = dirname(folder);
23
+ // Nothing above exists (an unmounted drive or a removed root): keep the
24
+ // plain absolute path, as earlier versions did for a missing parent.
25
+ if (parent === folder)
26
+ return absolute;
27
+ missing.push(basename(folder));
28
+ folder = parent;
29
+ }
13
30
  }
14
31
  }
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Optional THALAMUS routing for vault mode and advisory turn notes for request
3
+ * shaping. Account ids are vault account ids, not local pool rows. Nothing is
4
+ * persisted here; plugins on routes the runner does not serve get no turn notes.
5
+ */
1
6
  /** An eligible vault route, not a local pool row. */
2
7
  export interface RoutingCandidate {
3
8
  /** Vault account id, not the plugin's route id. */
@@ -35,7 +40,7 @@ interface RoutingReportIdentity {
35
40
  accountId: string;
36
41
  credentialId?: string;
37
42
  }
38
- export type ReportRoutingInput = RoutingReportIdentity & ({
43
+ export type ReportRoutingInput = (RoutingReportIdentity & ({
39
44
  outcome: 'served';
40
45
  /**
41
46
  * Pass only rate-limit headers, never the whole response headers.
@@ -56,7 +61,26 @@ export type ReportRoutingInput = RoutingReportIdentity & ({
56
61
  pool?: string;
57
62
  /** Optional reset time in epoch milliseconds. */
58
63
  resetsAt?: number;
59
- });
64
+ })) | {
65
+ sessionId: string;
66
+ kind: 'turn_notes_supported';
67
+ model: string;
68
+ supported: boolean;
69
+ } | {
70
+ sessionId: string;
71
+ kind: 'turn_note_skipped';
72
+ after_tool_use_id: string;
73
+ reason: 'anchor_missing' | 'before_user_message' | 'unsupported_model' | 'not_oauth';
74
+ };
75
+ /**
76
+ * Read advisory input for request shaping, in landing order, without persisting
77
+ * it. If no routing handle is available or the runner does not serve the route,
78
+ * returns [].
79
+ */
80
+ export declare function turnNotes(sessionId: string): readonly {
81
+ afterToolUseId: string;
82
+ text: string;
83
+ }[];
60
84
  /**
61
85
  * Ask routing for an eligible vault route. Any failure or unfamiliar answer
62
86
  * yields undefined so the plugin can keep its own choice. This does not admit
@@ -64,9 +88,10 @@ export type ReportRoutingInput = RoutingReportIdentity & ({
64
88
  */
65
89
  export declare function askRouting<Candidate extends RoutingCandidate>(input: AskRoutingInput<Candidate>): Promise<RoutingSelection<Candidate> | undefined>;
66
90
  /**
67
- * Report a vault request without waiting for routing. Only contract fields are
68
- * sent, and all accessor failures are ignored. Headers must already be limited
69
- * to rate-limit headers by the caller; values are not filtered or redacted here.
91
+ * Report a vault request or turn-note event without waiting for routing. Only
92
+ * contract fields are sent, and all accessor failures are ignored. Headers must
93
+ * already be limited to rate-limit headers by the caller; values are not filtered
94
+ * or redacted here.
70
95
  */
71
96
  export declare function reportRouting(input: ReportRoutingInput): void;
72
97
  export {};
@@ -1,9 +1,59 @@
1
- // Optional THALAMUS routing for vault mode only. Account ids here are vault
2
- // account ids; local pool rows are not known to the routing engine.
1
+ /**
2
+ * Optional THALAMUS routing for vault mode and advisory turn notes for request
3
+ * shaping. Account ids are vault account ids, not local pool rows. Nothing is
4
+ * persisted here; plugins on routes the runner does not serve get no turn notes.
5
+ */
3
6
  import { routingHandle } from './handle.js';
4
7
  function isObject(value) {
5
8
  return typeof value === 'object' && value !== null;
6
9
  }
10
+ /**
11
+ * Read advisory input for request shaping, in landing order, without persisting
12
+ * it. If no routing handle is available or the runner does not serve the route,
13
+ * returns [].
14
+ */
15
+ export function turnNotes(sessionId) {
16
+ try {
17
+ const handle = routingHandle(sessionId);
18
+ if (!handle)
19
+ return [];
20
+ const read = handle.turn_notes;
21
+ if (typeof read !== 'function')
22
+ return [];
23
+ const answer = read.call(handle);
24
+ if (isObject(answer) && typeof answer.then === 'function') {
25
+ void Promise.resolve(answer).catch(() => { });
26
+ return [];
27
+ }
28
+ if (!Array.isArray(answer))
29
+ return [];
30
+ const notes = [];
31
+ const encoder = new TextEncoder();
32
+ for (const entry of answer) {
33
+ try {
34
+ if (!isObject(entry) || Array.isArray(entry))
35
+ continue;
36
+ const afterToolUseId = entry.after_tool_use_id;
37
+ const text = entry.text;
38
+ if (typeof afterToolUseId !== 'string' ||
39
+ afterToolUseId.length === 0 ||
40
+ typeof text !== 'string' ||
41
+ text.length === 0 ||
42
+ encoder.encode(text).byteLength > 16_384)
43
+ continue;
44
+ notes.push({ afterToolUseId, text });
45
+ }
46
+ catch {
47
+ // One unreadable note entry must not hide its valid neighbours.
48
+ }
49
+ }
50
+ return notes;
51
+ }
52
+ catch {
53
+ // Turn notes are optional and must not break request shaping.
54
+ return [];
55
+ }
56
+ }
7
57
  function selectionFor(answer, candidates) {
8
58
  if (!isObject(answer))
9
59
  return undefined;
@@ -72,9 +122,10 @@ export async function askRouting(input) {
72
122
  }
73
123
  }
74
124
  /**
75
- * Report a vault request without waiting for routing. Only contract fields are
76
- * sent, and all accessor failures are ignored. Headers must already be limited
77
- * to rate-limit headers by the caller; values are not filtered or redacted here.
125
+ * Report a vault request or turn-note event without waiting for routing. Only
126
+ * contract fields are sent, and all accessor failures are ignored. Headers must
127
+ * already be limited to rate-limit headers by the caller; values are not filtered
128
+ * or redacted here.
78
129
  */
79
130
  export function reportRouting(input) {
80
131
  try {
@@ -84,38 +135,75 @@ export function reportRouting(input) {
84
135
  const report = handle.report;
85
136
  if (typeof report !== 'function')
86
137
  return;
87
- const identity = {
88
- account_id: input.accountId,
89
- ...(input.credentialId !== undefined
90
- ? { credential_id: input.credentialId }
91
- : {}),
92
- };
93
138
  let payload;
94
- switch (input.outcome) {
95
- case 'served':
96
- payload = { ...identity, outcome: 'served', headers: input.headers };
97
- break;
98
- case 'exhausted': {
99
- const { window, pool } = input;
100
- if (window !== '5h' && window !== '7d')
139
+ if ('kind' in input) {
140
+ switch (input.kind) {
141
+ case 'turn_notes_supported': {
142
+ const { model, supported } = input;
143
+ if (typeof model !== 'string' ||
144
+ model.length === 0 ||
145
+ typeof supported !== 'boolean')
146
+ return;
147
+ payload = {
148
+ kind: 'turn_notes_supported',
149
+ model,
150
+ supported,
151
+ };
152
+ break;
153
+ }
154
+ case 'turn_note_skipped': {
155
+ const { after_tool_use_id, reason } = input;
156
+ if (typeof after_tool_use_id !== 'string' ||
157
+ after_tool_use_id.length === 0 ||
158
+ (reason !== 'anchor_missing' &&
159
+ reason !== 'before_user_message' &&
160
+ reason !== 'unsupported_model' &&
161
+ reason !== 'not_oauth'))
162
+ return;
163
+ payload = {
164
+ kind: 'turn_note_skipped',
165
+ after_tool_use_id,
166
+ reason,
167
+ };
168
+ break;
169
+ }
170
+ default:
101
171
  return;
102
- // An undefined pool means the whole account, as an omitted one does.
103
- if (pool !== undefined &&
104
- (typeof pool !== 'string' || !/^[a-z0-9._-]{1,64}$/.test(pool)))
172
+ }
173
+ }
174
+ else {
175
+ const identity = {
176
+ account_id: input.accountId,
177
+ ...(input.credentialId !== undefined
178
+ ? { credential_id: input.credentialId }
179
+ : {}),
180
+ };
181
+ switch (input.outcome) {
182
+ case 'served':
183
+ payload = { ...identity, outcome: 'served', headers: input.headers };
184
+ break;
185
+ case 'exhausted': {
186
+ const { window, pool } = input;
187
+ if (window !== '5h' && window !== '7d')
188
+ return;
189
+ // An undefined pool means the whole account, as an omitted one does.
190
+ if (pool !== undefined &&
191
+ (typeof pool !== 'string' || !/^[a-z0-9._-]{1,64}$/.test(pool)))
192
+ return;
193
+ payload = {
194
+ ...identity,
195
+ outcome: 'exhausted',
196
+ window,
197
+ ...(pool ? { pool } : {}),
198
+ ...(input.resetsAt !== undefined
199
+ ? { resets_at: input.resetsAt }
200
+ : {}),
201
+ };
202
+ break;
203
+ }
204
+ default:
105
205
  return;
106
- payload = {
107
- ...identity,
108
- outcome: 'exhausted',
109
- window,
110
- ...(pool ? { pool } : {}),
111
- ...(input.resetsAt !== undefined
112
- ? { resets_at: input.resetsAt }
113
- : {}),
114
- };
115
- break;
116
206
  }
117
- default:
118
- return;
119
207
  }
120
208
  // A broken bridge may return a rejected promise despite its void contract.
121
209
  const pending = report.call(handle, payload);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/common-auth",
3
- "version": "0.38.0",
3
+ "version": "0.39.0",
4
4
  "description": "Shared code for the CortexKit auth plugins: account pool, quota and routing, commands and auth menu, OpenCode 2 hooks, Claustrum custody, and plumbing (loopback RPC, file locks, logger, sidebar state, TUI preferences and build).",
5
5
  "license": "MIT",
6
6
  "repository": {