@pikku/kysely 0.13.1 → 0.13.3

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,77 @@
1
+ ## 0.13.3
2
+
3
+ ### Patch Changes
4
+
5
+ - e3d4454: Add job groups, so one shared queue can stay fair without splitting into one
6
+ queue per producer.
7
+
8
+ A job may now carry `group: { id, tier }`, and a worker may cap how many jobs
9
+ of any one group run at once via `groupConcurrency`. On pg-boss this maps to
10
+ `localGroupConcurrency`, which excludes at-capacity groups from the fetch query
11
+ itself, so a capped group costs nothing rather than being fetched and restored.
12
+ BullMQ declares it unsupported (groups are a BullMQ Pro feature) — being
13
+ push-based, it can simply use a queue per group at no polling cost.
14
+
15
+ Workflow services accept a `queueStrategy`. The default `'per-workflow'` is
16
+ unchanged: every workflow gets its own `wf-orchestrator-*` / `wf-step-*` queue,
17
+ which is also what lets serverless providers deploy one unit per workflow. The
18
+ new `'shared-groups'` routes every workflow through the shared
19
+ orchestrator/step-worker queues and isolates them by group instead, so a
20
+ monolith runs one set of pollers rather than one per workflow — on a
21
+ pull-based backend with dozens of workflows that is the difference between
22
+ hundreds of poll loops and twenty. It is for single-process runtimes only; a
23
+ per-unit serverless deploy still needs the per-workflow queues to route to its
24
+ units.
25
+
26
+ - Updated dependencies [24252b8]
27
+ - Updated dependencies [e3d4454]
28
+ - @pikku/core@0.12.69
29
+
30
+ ## 0.13.2
31
+
32
+ ### Patch Changes
33
+
34
+ - 4324652: Scope AI agent thread reads to the calling session.
35
+
36
+ The generated thread-management functions (`getAgentThreads`,
37
+ `getAgentThreadMessages`, `getAgentThreadRuns`, `deleteAgentThread`) keyed purely
38
+ off a caller-supplied `threadId` and treated `resourceId` as an optional filter,
39
+ so omitting it enumerated every tenant's threads.
40
+ - `listThreads` gains an `owners` **authorization constraint** (distinct from the
41
+ `resourceId` filter): an empty array matches nothing, and it is always derived
42
+ from the session, never from input. Implemented across the Kysely, Redis and
43
+ MongoDB agent run services, with LIKE/regex metacharacter escaping so an owner
44
+ id containing `_` or `%` cannot match a foreign owner.
45
+ - The three `threadId`-keyed functions are now guarded by an `isThreadOwner`
46
+ `pikkuPermission` rather than an in-body check. A thread that does not exist is
47
+ denied rather than 404'd, so it is indistinguishable from one owned by someone
48
+ else.
49
+ - New `@pikku/core/ai-agent` helpers: `canAccessThread`, `threadOwnerConstraint`,
50
+ `sessionPrincipals`, `isOwnedByPrincipal`.
51
+
52
+ Services destructured by a wired function are now non-optional inside it.
53
+
54
+ The inspector already aggregated the services used by every wired `func`,
55
+ `permissions` and `middleware` into `RequiredSingletonServices`, but the
56
+ generated function types defaulted their service parameter to the raw `Services`
57
+ — so a service declared `foo?: Foo` still arrived as possibly-undefined, forcing
58
+ `if (!foo) throw new MissingServiceError(...)` guards that could never fire.
59
+ Generated types now expose `WiredSingletonServices` / `WiredServices`
60
+ (`RequiredSingletonServices & Services`) and default the `RequiredServices`
61
+ generic of functions, permissions, middleware, auth and approval-description
62
+ helpers to them. Optionality now means only what it should: "this service may
63
+ not be created, because nothing uses it".
64
+
65
+ - Updated dependencies [5f19016]
66
+ - Updated dependencies [78e4778]
67
+ - Updated dependencies [4324652]
68
+ - Updated dependencies [de044f8]
69
+ - Updated dependencies [cd1a811]
70
+ - Updated dependencies [19fa6f0]
71
+ - Updated dependencies [b501612]
72
+ - Updated dependencies [eb37b1e]
73
+ - @pikku/core@0.12.66
74
+
1
75
  ## 0.13.1
2
76
 
3
77
  ### 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)
@@ -1,12 +1,12 @@
1
1
  import type { SerializedError } from '@pikku/core';
2
- import { PikkuWorkflowService, type WorkflowPlannedStep, type WorkflowRun, type WorkflowRunWire, type StepState, type StepStatus, type WorkflowStatus, type WorkflowVersionStatus } from '@pikku/core/workflow';
2
+ import { PikkuWorkflowService, type WorkflowPlannedStep, type WorkflowQueueOptions, type WorkflowRun, type WorkflowRunWire, type StepState, type StepStatus, type WorkflowStatus, type WorkflowVersionStatus } from '@pikku/core/workflow';
3
3
  import type { Kysely } from 'kysely';
4
4
  import type { KyselyPikkuDB } from './kysely-tables.js';
5
5
  export declare class KyselyWorkflowService extends PikkuWorkflowService {
6
6
  protected db: Kysely<KyselyPikkuDB>;
7
7
  private initialized;
8
8
  private runService;
9
- constructor(db: Kysely<KyselyPikkuDB>);
9
+ constructor(db: Kysely<KyselyPikkuDB>, options?: WorkflowQueueOptions);
10
10
  init(): Promise<void>;
11
11
  protected createRunImpl(workflowName: string, input: any, inline: boolean, graphHash: string, wire: WorkflowRunWire, options?: {
12
12
  deterministic?: boolean;
@@ -6,8 +6,8 @@ export class KyselyWorkflowService extends PikkuWorkflowService {
6
6
  db;
7
7
  initialized = false;
8
8
  runService;
9
- constructor(db) {
10
- super();
9
+ constructor(db, options = {}) {
10
+ super(options);
11
11
  this.db = db;
12
12
  this.runService = new KyselyWorkflowRunService(db);
13
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikku/kysely",
3
- "version": "0.13.1",
3
+ "version": "0.13.3",
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.69"
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)
@@ -2,6 +2,7 @@ import type { SerializedError } from '@pikku/core'
2
2
  import {
3
3
  PikkuWorkflowService,
4
4
  type WorkflowPlannedStep,
5
+ type WorkflowQueueOptions,
5
6
  type WorkflowRun,
6
7
  type WorkflowRunWire,
7
8
  type StepState,
@@ -19,8 +20,11 @@ export class KyselyWorkflowService extends PikkuWorkflowService {
19
20
  private initialized = false
20
21
  private runService: KyselyWorkflowRunService
21
22
 
22
- constructor(protected db: Kysely<KyselyPikkuDB>) {
23
- super()
23
+ constructor(
24
+ protected db: Kysely<KyselyPikkuDB>,
25
+ options: WorkflowQueueOptions = {}
26
+ ) {
27
+ super(options)
24
28
  this.runService = new KyselyWorkflowRunService(db)
25
29
  }
26
30