@pikku/kysely 0.13.1 → 0.13.2

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,3 +1,48 @@
1
+ ## 0.13.2
2
+
3
+ ### Patch Changes
4
+
5
+ - 4324652: Scope AI agent thread reads to the calling session.
6
+
7
+ The generated thread-management functions (`getAgentThreads`,
8
+ `getAgentThreadMessages`, `getAgentThreadRuns`, `deleteAgentThread`) keyed purely
9
+ off a caller-supplied `threadId` and treated `resourceId` as an optional filter,
10
+ so omitting it enumerated every tenant's threads.
11
+ - `listThreads` gains an `owners` **authorization constraint** (distinct from the
12
+ `resourceId` filter): an empty array matches nothing, and it is always derived
13
+ from the session, never from input. Implemented across the Kysely, Redis and
14
+ MongoDB agent run services, with LIKE/regex metacharacter escaping so an owner
15
+ id containing `_` or `%` cannot match a foreign owner.
16
+ - The three `threadId`-keyed functions are now guarded by an `isThreadOwner`
17
+ `pikkuPermission` rather than an in-body check. A thread that does not exist is
18
+ denied rather than 404'd, so it is indistinguishable from one owned by someone
19
+ else.
20
+ - New `@pikku/core/ai-agent` helpers: `canAccessThread`, `threadOwnerConstraint`,
21
+ `sessionPrincipals`, `isOwnedByPrincipal`.
22
+
23
+ Services destructured by a wired function are now non-optional inside it.
24
+
25
+ The inspector already aggregated the services used by every wired `func`,
26
+ `permissions` and `middleware` into `RequiredSingletonServices`, but the
27
+ generated function types defaulted their service parameter to the raw `Services`
28
+ — so a service declared `foo?: Foo` still arrived as possibly-undefined, forcing
29
+ `if (!foo) throw new MissingServiceError(...)` guards that could never fire.
30
+ Generated types now expose `WiredSingletonServices` / `WiredServices`
31
+ (`RequiredSingletonServices & Services`) and default the `RequiredServices`
32
+ generic of functions, permissions, middleware, auth and approval-description
33
+ helpers to them. Optionality now means only what it should: "this service may
34
+ not be created, because nothing uses it".
35
+
36
+ - Updated dependencies [5f19016]
37
+ - Updated dependencies [78e4778]
38
+ - Updated dependencies [4324652]
39
+ - Updated dependencies [de044f8]
40
+ - Updated dependencies [cd1a811]
41
+ - Updated dependencies [19fa6f0]
42
+ - Updated dependencies [b501612]
43
+ - Updated dependencies [eb37b1e]
44
+ - @pikku/core@0.12.66
45
+
1
46
  ## 0.13.1
2
47
 
3
48
  ### Patch Changes
@@ -7,6 +7,7 @@ export declare class KyselyAgentRunService implements AgentRunService {
7
7
  listThreads(options?: {
8
8
  agentName?: string;
9
9
  resourceId?: string;
10
+ owners?: string[];
10
11
  limit?: number;
11
12
  offset?: number;
12
13
  }): Promise<AIThread[]>;
@@ -1,11 +1,19 @@
1
+ import { sql } from 'kysely';
1
2
  import { parseJson } from './kysely-json.js';
3
+ // Owner ids routinely contain `_` (e.g. `user_123`), which LIKE treats as a
4
+ // single-character wildcard — so a raw prefix would match a foreign owner.
5
+ const escapeLikePattern = (value) => value.replace(/[\\%_]/g, (char) => `\\${char}`);
2
6
  export class KyselyAgentRunService {
3
7
  db;
4
8
  constructor(db) {
5
9
  this.db = db;
6
10
  }
7
11
  async listThreads(options) {
8
- const { agentName, resourceId, limit = 50, offset = 0 } = options ?? {};
12
+ const { agentName, resourceId, owners, limit = 50, offset = 0, } = options ?? {};
13
+ // An owners constraint is an authorization boundary, so an empty list must
14
+ // match nothing rather than degrade to "no filter".
15
+ if (owners && owners.length === 0)
16
+ return [];
9
17
  let query = this.db
10
18
  .selectFrom('aiThreads as t')
11
19
  .select([
@@ -26,6 +34,12 @@ export class KyselyAgentRunService {
26
34
  if (resourceId) {
27
35
  query = query.where('t.resourceId', '=', resourceId);
28
36
  }
37
+ if (owners) {
38
+ query = query.where((eb) => eb.or(owners.flatMap((owner) => [
39
+ eb('t.resourceId', '=', owner),
40
+ eb('t.resourceId', 'like', sql `${`${escapeLikePattern(owner)}:%`} escape '\\'`),
41
+ ])));
42
+ }
29
43
  const result = await query
30
44
  .orderBy('t.updatedAt', 'desc')
31
45
  .limit(limit)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikku/kysely",
3
- "version": "0.13.1",
3
+ "version": "0.13.2",
4
4
  "author": "yasser.fadl@gmail.com",
5
5
  "license": "MIT",
6
6
  "module": "dist/src/index.js",
@@ -20,7 +20,7 @@
20
20
  "prepublishOnly": "yarn build"
21
21
  },
22
22
  "peerDependencies": {
23
- "@pikku/core": "^0.12.64"
23
+ "@pikku/core": "^0.12.66"
24
24
  },
25
25
  "dependencies": {
26
26
  "kysely": "^0.29.0"
@@ -5,19 +5,36 @@ import type {
5
5
  AgentRunService,
6
6
  } from '@pikku/core/ai-agent'
7
7
  import type { Kysely } from 'kysely'
8
+ import { sql } from 'kysely'
8
9
  import type { KyselyPikkuDB } from './kysely-tables.js'
9
10
  import { parseJson } from './kysely-json.js'
10
11
 
12
+ // Owner ids routinely contain `_` (e.g. `user_123`), which LIKE treats as a
13
+ // single-character wildcard — so a raw prefix would match a foreign owner.
14
+ const escapeLikePattern = (value: string) =>
15
+ value.replace(/[\\%_]/g, (char) => `\\${char}`)
16
+
11
17
  export class KyselyAgentRunService implements AgentRunService {
12
18
  constructor(private db: Kysely<KyselyPikkuDB>) {}
13
19
 
14
20
  async listThreads(options?: {
15
21
  agentName?: string
16
22
  resourceId?: string
23
+ owners?: string[]
17
24
  limit?: number
18
25
  offset?: number
19
26
  }): Promise<AIThread[]> {
20
- const { agentName, resourceId, limit = 50, offset = 0 } = options ?? {}
27
+ const {
28
+ agentName,
29
+ resourceId,
30
+ owners,
31
+ limit = 50,
32
+ offset = 0,
33
+ } = options ?? {}
34
+
35
+ // An owners constraint is an authorization boundary, so an empty list must
36
+ // match nothing rather than degrade to "no filter".
37
+ if (owners && owners.length === 0) return []
21
38
 
22
39
  let query = this.db
23
40
  .selectFrom('aiThreads as t')
@@ -46,6 +63,21 @@ export class KyselyAgentRunService implements AgentRunService {
46
63
  query = query.where('t.resourceId', '=', resourceId)
47
64
  }
48
65
 
66
+ if (owners) {
67
+ query = query.where((eb) =>
68
+ eb.or(
69
+ owners.flatMap((owner) => [
70
+ eb('t.resourceId', '=', owner),
71
+ eb(
72
+ 't.resourceId',
73
+ 'like',
74
+ sql<string>`${`${escapeLikePattern(owner)}:%`} escape '\\'`
75
+ ),
76
+ ])
77
+ )
78
+ )
79
+ }
80
+
49
81
  const result = await query
50
82
  .orderBy('t.updatedAt', 'desc')
51
83
  .limit(limit)