@pikku/mongodb 0.12.8 → 0.12.10

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,5 +1,61 @@
1
1
  # @pikku/mongodb
2
2
 
3
+ ## 0.12.10
4
+
5
+ ### Patch Changes
6
+
7
+ - 4324652: Scope AI agent thread reads to the calling session.
8
+
9
+ The generated thread-management functions (`getAgentThreads`,
10
+ `getAgentThreadMessages`, `getAgentThreadRuns`, `deleteAgentThread`) keyed purely
11
+ off a caller-supplied `threadId` and treated `resourceId` as an optional filter,
12
+ so omitting it enumerated every tenant's threads.
13
+ - `listThreads` gains an `owners` **authorization constraint** (distinct from the
14
+ `resourceId` filter): an empty array matches nothing, and it is always derived
15
+ from the session, never from input. Implemented across the Kysely, Redis and
16
+ MongoDB agent run services, with LIKE/regex metacharacter escaping so an owner
17
+ id containing `_` or `%` cannot match a foreign owner.
18
+ - The three `threadId`-keyed functions are now guarded by an `isThreadOwner`
19
+ `pikkuPermission` rather than an in-body check. A thread that does not exist is
20
+ denied rather than 404'd, so it is indistinguishable from one owned by someone
21
+ else.
22
+ - New `@pikku/core/ai-agent` helpers: `canAccessThread`, `threadOwnerConstraint`,
23
+ `sessionPrincipals`, `isOwnedByPrincipal`.
24
+
25
+ Services destructured by a wired function are now non-optional inside it.
26
+
27
+ The inspector already aggregated the services used by every wired `func`,
28
+ `permissions` and `middleware` into `RequiredSingletonServices`, but the
29
+ generated function types defaulted their service parameter to the raw `Services`
30
+ — so a service declared `foo?: Foo` still arrived as possibly-undefined, forcing
31
+ `if (!foo) throw new MissingServiceError(...)` guards that could never fire.
32
+ Generated types now expose `WiredSingletonServices` / `WiredServices`
33
+ (`RequiredSingletonServices & Services`) and default the `RequiredServices`
34
+ generic of functions, permissions, middleware, auth and approval-description
35
+ helpers to them. Optionality now means only what it should: "this service may
36
+ not be created, because nothing uses it".
37
+
38
+ - Updated dependencies [5f19016]
39
+ - Updated dependencies [78e4778]
40
+ - Updated dependencies [4324652]
41
+ - Updated dependencies [de044f8]
42
+ - Updated dependencies [cd1a811]
43
+ - Updated dependencies [19fa6f0]
44
+ - Updated dependencies [b501612]
45
+ - Updated dependencies [eb37b1e]
46
+ - @pikku/core@0.12.66
47
+
48
+ ## 0.12.9
49
+
50
+ ### Patch Changes
51
+
52
+ - 41ce2cb: Upgrade to TypeScript 6 and raise the minimum Node.js version to 22.
53
+
54
+ All packages now build against `typescript@^6.0.3` and declare `engines.node >= 22`. Internal tooling (`ts-json-schema-generator`, `zod-to-ts`) was bumped to TypeScript 6-compatible releases.
55
+
56
+ - Updated dependencies [41ce2cb]
57
+ - @pikku/core@0.12.44
58
+
3
59
  ## 0.12.8
4
60
 
5
61
  ### Patch Changes
@@ -9,6 +9,7 @@ export declare class MongoDBAgentRunService implements AgentRunService {
9
9
  listThreads(options?: {
10
10
  agentName?: string;
11
11
  resourceId?: string;
12
+ owners?: string[];
12
13
  limit?: number;
13
14
  offset?: number;
14
15
  }): Promise<AIThread[]>;
@@ -1,3 +1,6 @@
1
+ // Owner ids are untrusted input to the regex, so metacharacters must not be
2
+ // able to widen the prefix match.
3
+ const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1
4
  export class MongoDBAgentRunService {
2
5
  threads;
3
6
  messages;
@@ -10,11 +13,21 @@ export class MongoDBAgentRunService {
10
13
  this.runs = db.collection('ai_run');
11
14
  }
12
15
  async listThreads(options) {
13
- const { agentName, resourceId, limit = 50, offset = 0 } = options ?? {};
16
+ const { agentName, resourceId, owners, limit = 50, offset = 0, } = options ?? {};
17
+ // An owners constraint is an authorization boundary, so an empty list must
18
+ // match nothing rather than degrade to "no filter".
19
+ if (owners && owners.length === 0)
20
+ return [];
14
21
  let filter = {};
15
22
  if (resourceId) {
16
23
  filter.resourceId = resourceId;
17
24
  }
25
+ if (owners) {
26
+ filter.$or = owners.flatMap((owner) => [
27
+ { resourceId: owner },
28
+ { resourceId: { $regex: `^${escapeRegExp(owner)}:` } },
29
+ ]);
30
+ }
18
31
  if (agentName) {
19
32
  const threadIds = await this.runs.distinct('threadId', {
20
33
  agentName,
@@ -216,13 +216,7 @@ export class MongoDBWorkflowService extends PikkuWorkflowService {
216
216
  .limit(1)
217
217
  .toArray();
218
218
  if (latestHistory.length > 0) {
219
- await this.stepHistory.updateOne({ _id: latestHistory[0]._id }, {
220
- $set: {
221
- status: 'failed',
222
- error: serializedError,
223
- failedAt: new Date(),
224
- },
225
- });
219
+ await this.stepHistory.updateOne({ _id: latestHistory[0]._id }, { $set: { status: 'failed', error: serializedError, failedAt: new Date() } });
226
220
  }
227
221
  }
228
222
  async createRetryAttemptImpl(stepId, status) {
@@ -1 +1 @@
1
- {"root":["../src/index.ts","../src/mongodb-agent-run-service.ts","../src/mongodb-ai-storage-service.ts","../src/mongodb-channel-store.ts","../src/mongodb-deployment-service.ts","../src/mongodb-eventhub-store.ts","../src/mongodb-secret-service.ts","../src/mongodb-session-store.ts","../src/mongodb-workflow-run-service.ts","../src/mongodb-workflow-service.ts","../src/pikku-mongodb.ts"],"version":"5.9.3"}
1
+ {"root":["../src/index.ts","../src/mongodb-agent-run-service.ts","../src/mongodb-ai-storage-service.ts","../src/mongodb-channel-store.ts","../src/mongodb-deployment-service.ts","../src/mongodb-eventhub-store.ts","../src/mongodb-secret-service.ts","../src/mongodb-session-store.ts","../src/mongodb-workflow-run-service.ts","../src/mongodb-workflow-service.ts","../src/pikku-mongodb.ts"],"version":"6.0.3"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikku/mongodb",
3
- "version": "0.12.8",
3
+ "version": "0.12.10",
4
4
  "author": "yasser.fadl@gmail.com",
5
5
  "license": "MIT",
6
6
  "module": "dist/src/index.js",
@@ -17,7 +17,7 @@
17
17
  "prepublishOnly": "yarn build"
18
18
  },
19
19
  "peerDependencies": {
20
- "@pikku/core": "^0.12.39"
20
+ "@pikku/core": "^0.12.66"
21
21
  },
22
22
  "dependencies": {
23
23
  "mongodb": "^6.12.0"
@@ -25,7 +25,7 @@
25
25
  "devDependencies": {
26
26
  "mongodb": "^6.12.0",
27
27
  "mongodb-memory-server": "^10.4.0",
28
- "typescript": "^5.9"
28
+ "typescript": "^6.0.3"
29
29
  },
30
30
  "exports": {
31
31
  ".": "./dist/src/index.js"
package/run-tests.sh CHANGED
@@ -38,16 +38,16 @@ if [ ${#files[@]} -eq 0 ]; then
38
38
  fi
39
39
 
40
40
  # Construct the node command
41
- node_cmd="node --import tsx --test"
41
+ node_cmd=(node --import tsx --test)
42
42
 
43
43
  # Append options based on flags
44
44
  if [ "$watch_mode" = true ]; then
45
- node_cmd="$node_cmd --watch"
45
+ node_cmd+=(--watch)
46
46
  fi
47
47
 
48
48
  if [ "$coverage_mode" = true ]; then
49
- node_cmd="$node_cmd --test-coverage-include=\"src/**/*.{ts,js}\" --test-coverage-exclude=\"**/dist/**\" --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info"
49
+ node_cmd+=(--test-coverage-include="src/**/*.{ts,js}" --test-coverage-exclude="**/dist/**" --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info)
50
50
  fi
51
51
 
52
52
  # Execute the node command with the expanded list of files
53
- $node_cmd "${files[@]}"
53
+ "${node_cmd[@]}" "${files[@]}"
@@ -6,6 +6,11 @@ import type {
6
6
  } from '@pikku/core/ai-agent'
7
7
  import type { Db, Collection } from 'mongodb'
8
8
 
9
+ // Owner ids are untrusted input to the regex, so metacharacters must not be
10
+ // able to widen the prefix match.
11
+ const escapeRegExp = (value: string) =>
12
+ value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
13
+
9
14
  interface AIThreadDoc {
10
15
  _id: string
11
16
  resourceId: string
@@ -71,10 +76,21 @@ export class MongoDBAgentRunService implements AgentRunService {
71
76
  async listThreads(options?: {
72
77
  agentName?: string
73
78
  resourceId?: string
79
+ owners?: string[]
74
80
  limit?: number
75
81
  offset?: number
76
82
  }): Promise<AIThread[]> {
77
- const { agentName, resourceId, limit = 50, offset = 0 } = options ?? {}
83
+ const {
84
+ agentName,
85
+ resourceId,
86
+ owners,
87
+ limit = 50,
88
+ offset = 0,
89
+ } = options ?? {}
90
+
91
+ // An owners constraint is an authorization boundary, so an empty list must
92
+ // match nothing rather than degrade to "no filter".
93
+ if (owners && owners.length === 0) return []
78
94
 
79
95
  let filter: Record<string, any> = {}
80
96
 
@@ -82,6 +98,13 @@ export class MongoDBAgentRunService implements AgentRunService {
82
98
  filter.resourceId = resourceId
83
99
  }
84
100
 
101
+ if (owners) {
102
+ filter.$or = owners.flatMap((owner) => [
103
+ { resourceId: owner },
104
+ { resourceId: { $regex: `^${escapeRegExp(owner)}:` } },
105
+ ])
106
+ }
107
+
85
108
  if (agentName) {
86
109
  const threadIds = await this.runs.distinct('threadId', {
87
110
  agentName,