@qvac/core 1.0.0-rc.2 → 1.0.0-rc.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.
Files changed (195) hide show
  1. package/CHANGELOG.md +158 -2
  2. package/dist-lib/index.d.ts +1 -0
  3. package/dist-lib/index.js +1 -0
  4. package/dist-lib/lib/api/chunk-delta.d.ts +7 -2
  5. package/dist-lib/lib/api/chunk-delta.js +89 -7
  6. package/dist-lib/lib/api/chunks.js +14 -8
  7. package/dist-lib/lib/api/config.d.ts +4 -1
  8. package/dist-lib/lib/api/config.js +45 -4
  9. package/dist-lib/lib/api/custom-models.js +3 -5
  10. package/dist-lib/lib/api/devices.js +3 -1
  11. package/dist-lib/lib/api/files.d.ts +1 -1
  12. package/dist-lib/lib/api/files.js +65 -8
  13. package/dist-lib/lib/api/instrument.js +12 -4
  14. package/dist-lib/lib/api/members.js +7 -13
  15. package/dist-lib/lib/api/mesh.d.ts +1 -3
  16. package/dist-lib/lib/api/mesh.js +2 -2
  17. package/dist-lib/lib/api/notifications.js +8 -4
  18. package/dist-lib/lib/api/projects.js +5 -1
  19. package/dist-lib/lib/api/rag.js +32 -19
  20. package/dist-lib/lib/api/served.js +2 -1
  21. package/dist-lib/lib/api/workspace.js +0 -1
  22. package/dist-lib/lib/assistant/harness.d.ts +8 -2
  23. package/dist-lib/lib/assistant/harness.js +17 -4
  24. package/dist-lib/lib/assistant/history.d.ts +1 -1
  25. package/dist-lib/lib/assistant/history.js +19 -13
  26. package/dist-lib/lib/assistant/message.d.ts +6 -1
  27. package/dist-lib/lib/assistant/message.js +22 -2
  28. package/dist-lib/lib/assistant/model-round.d.ts +5 -0
  29. package/dist-lib/lib/assistant/model-round.js +46 -18
  30. package/dist-lib/lib/assistant/pdf-budget.d.ts +10 -2
  31. package/dist-lib/lib/assistant/pdf-budget.js +10 -11
  32. package/dist-lib/lib/assistant/pdf-pages.d.ts +25 -0
  33. package/dist-lib/lib/assistant/pdf-pages.js +75 -0
  34. package/dist-lib/lib/assistant/round-host.js +5 -3
  35. package/dist-lib/lib/assistant-client.d.ts +2 -1
  36. package/dist-lib/lib/assistant-client.js +24 -6
  37. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  38. package/dist-lib/lib/assistant-tree.js +8 -5
  39. package/dist-lib/lib/assistant.d.ts +20 -7
  40. package/dist-lib/lib/assistant.js +468 -240
  41. package/dist-lib/lib/client.js +10 -1
  42. package/dist-lib/lib/compaction.d.ts +3 -1
  43. package/dist-lib/lib/compaction.js +18 -4
  44. package/dist-lib/lib/constants.d.ts +3 -0
  45. package/dist-lib/lib/constants.js +3 -0
  46. package/dist-lib/lib/core/chat-activity.js +13 -16
  47. package/dist-lib/lib/core/chat-copy.js +1 -1
  48. package/dist-lib/lib/core/chat-fork.js +2 -1
  49. package/dist-lib/lib/core/chat-move.d.ts +4 -0
  50. package/dist-lib/lib/core/chat-move.js +42 -11
  51. package/dist-lib/lib/core/chat.d.ts +0 -1
  52. package/dist-lib/lib/core/chat.js +1 -1
  53. package/dist-lib/lib/core/chunk.d.ts +14 -3
  54. package/dist-lib/lib/core/chunk.js +112 -22
  55. package/dist-lib/lib/core/collections.d.ts +12 -1
  56. package/dist-lib/lib/core/collections.js +12 -1
  57. package/dist-lib/lib/core/config-value.d.ts +6 -0
  58. package/dist-lib/lib/core/config-value.js +14 -0
  59. package/dist-lib/lib/core/contract/activity.d.ts +3 -2
  60. package/dist-lib/lib/core/contract/activity.js +39 -21
  61. package/dist-lib/lib/core/contract/agent-join.js +15 -19
  62. package/dist-lib/lib/core/contract/agents.js +8 -7
  63. package/dist-lib/lib/core/contract/chat.d.ts +3 -0
  64. package/dist-lib/lib/core/contract/chat.js +12 -1
  65. package/dist-lib/lib/core/contract/chunks.js +16 -6
  66. package/dist-lib/lib/core/contract/devices.js +6 -4
  67. package/dist-lib/lib/core/contract/index.js +2 -1
  68. package/dist-lib/lib/core/contract/invites.js +12 -31
  69. package/dist-lib/lib/core/contract/joined-meshes.js +2 -1
  70. package/dist-lib/lib/core/contract/mesh-rules.js +4 -0
  71. package/dist-lib/lib/core/contract/model-requests.js +7 -6
  72. package/dist-lib/lib/core/contract/project-device-keys.js +15 -6
  73. package/dist-lib/lib/core/contract/project-host-keys.js +5 -4
  74. package/dist-lib/lib/core/contract/projects.js +12 -13
  75. package/dist-lib/lib/core/contract/public-seeds.js +5 -5
  76. package/dist-lib/lib/core/contract/roles.js +2 -1
  77. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -1
  78. package/dist-lib/lib/core/contract/run-requests.js +19 -13
  79. package/dist-lib/lib/core/contract/shipped-rows.d.ts +2 -0
  80. package/dist-lib/lib/core/contract/shipped-rows.js +22 -0
  81. package/dist-lib/lib/core/contract/workspace.js +4 -10
  82. package/dist-lib/lib/core/contract/writers.js +20 -6
  83. package/dist-lib/lib/core/devices.d.ts +2 -1
  84. package/dist-lib/lib/core/devices.js +4 -2
  85. package/dist-lib/lib/core/document-attachment.d.ts +6 -3
  86. package/dist-lib/lib/core/document-attachment.js +10 -5
  87. package/dist-lib/lib/core/files/attachments.js +3 -3
  88. package/dist-lib/lib/core/files/index.d.ts +8 -5
  89. package/dist-lib/lib/core/files/index.js +109 -18
  90. package/dist-lib/lib/core/files/processing/index.d.ts +1 -1
  91. package/dist-lib/lib/core/files/processing/index.js +8 -2
  92. package/dist-lib/lib/core/files/processing/source.d.ts +1 -0
  93. package/dist-lib/lib/core/files/processing/source.js +3 -0
  94. package/dist-lib/lib/core/files/processing/state.d.ts +1 -0
  95. package/dist-lib/lib/core/files/processing/state.js +7 -0
  96. package/dist-lib/lib/core/files/rag/build.d.ts +1 -0
  97. package/dist-lib/lib/core/files/rag/build.js +14 -1
  98. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  99. package/dist-lib/lib/core/files/rag/index.js +1 -1
  100. package/dist-lib/lib/core/files/rag/produce.js +1 -0
  101. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +2 -0
  102. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +7 -1
  103. package/dist-lib/lib/core/first-event.d.ts +6 -0
  104. package/dist-lib/lib/core/first-event.js +13 -0
  105. package/dist-lib/lib/core/invite.d.ts +2 -2
  106. package/dist-lib/lib/core/invite.js +5 -4
  107. package/dist-lib/lib/core/local/index.d.ts +10 -1
  108. package/dist-lib/lib/core/local/index.js +105 -10
  109. package/dist-lib/lib/core/mesh.d.ts +15 -4
  110. package/dist-lib/lib/core/mesh.js +128 -57
  111. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  112. package/dist-lib/lib/core/migrations/chat-chunks.js +47 -0
  113. package/dist-lib/lib/core/migrations/file-index.d.ts +1 -1
  114. package/dist-lib/lib/core/migrations/file-index.js +6 -2
  115. package/dist-lib/lib/core/migrations/index.d.ts +2 -1
  116. package/dist-lib/lib/core/migrations/index.js +66 -9
  117. package/dist-lib/lib/core/notification.js +96 -23
  118. package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
  119. package/dist-lib/lib/core/pdf/figure.js +64 -0
  120. package/dist-lib/lib/core/pdf/rasterize.d.ts +10 -1
  121. package/dist-lib/lib/core/pdf/rasterize.js +72 -22
  122. package/dist-lib/lib/core/peer-duplex.js +57 -5
  123. package/dist-lib/lib/core/public-mesh.js +8 -2
  124. package/dist-lib/lib/core/run-requests.js +2 -3
  125. package/dist-lib/lib/core/senders.d.ts +7 -6
  126. package/dist-lib/lib/core/senders.js +23 -30
  127. package/dist-lib/lib/core/shared-agents.d.ts +1 -0
  128. package/dist-lib/lib/core/shared-agents.js +47 -19
  129. package/dist-lib/lib/core/sync/background.d.ts +3 -15
  130. package/dist-lib/lib/core/sync/background.js +25 -102
  131. package/dist-lib/lib/core/sync/devices.js +6 -3
  132. package/dist-lib/lib/core/sync/index.js +12 -2
  133. package/dist-lib/lib/core/sync-status.js +3 -3
  134. package/dist-lib/lib/core.d.ts +59 -10
  135. package/dist-lib/lib/core.js +706 -174
  136. package/dist-lib/lib/credential-pusher.d.ts +11 -7
  137. package/dist-lib/lib/credential-pusher.js +40 -8
  138. package/dist-lib/lib/device-tree.d.ts +18 -0
  139. package/dist-lib/lib/device-tree.js +32 -0
  140. package/dist-lib/lib/doctor/redact.js +1 -1
  141. package/dist-lib/lib/errors.d.ts +4 -0
  142. package/dist-lib/lib/errors.js +4 -0
  143. package/dist-lib/lib/fs-drive-bridge.d.ts +7 -0
  144. package/dist-lib/lib/fs-drive-bridge.js +308 -30
  145. package/dist-lib/lib/integrations/setup-routes.js +7 -5
  146. package/dist-lib/lib/knowledge-search.d.ts +1 -1
  147. package/dist-lib/lib/knowledge-search.js +5 -46
  148. package/dist-lib/lib/kv-cache-retention.d.ts +35 -0
  149. package/dist-lib/lib/kv-cache-retention.js +98 -0
  150. package/dist-lib/lib/latest-frame.d.ts +10 -0
  151. package/dist-lib/lib/latest-frame.js +48 -0
  152. package/dist-lib/lib/model-download.d.ts +6 -1
  153. package/dist-lib/lib/model-download.js +32 -17
  154. package/dist-lib/lib/oauth/resolve.js +13 -5
  155. package/dist-lib/lib/reconnecting-watch.js +19 -11
  156. package/dist-lib/lib/run-notices.d.ts +2 -0
  157. package/dist-lib/lib/run-notices.js +5 -0
  158. package/dist-lib/lib/run-replay.d.ts +13 -1
  159. package/dist-lib/lib/run-replay.js +58 -21
  160. package/dist-lib/schema/build.js +66 -57
  161. package/dist-lib/schema/content.js +2 -1
  162. package/dist-lib/schema/engine.js +37 -5
  163. package/dist-lib/schema/field-order.js +7 -1
  164. package/dist-lib/schema/indexes.d.ts +4 -0
  165. package/dist-lib/schema/indexes.js +18 -0
  166. package/dist-lib/schema/local.js +9 -2
  167. package/dist-lib/schema/qvac.js +112 -4
  168. package/dist-lib/spec/bind.js +3 -0
  169. package/dist-lib/spec/calls.js +1 -0
  170. package/dist-lib/spec/capabilities.d.ts +2 -0
  171. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  172. package/dist-lib/spec/hrpc/index.js +15 -3
  173. package/dist-lib/spec/hrpc/messages.js +1475 -1392
  174. package/dist-lib/spec/hyperschema/index.js +1475 -1392
  175. package/dist-lib/spec/hyperschema/schema.json +86 -1
  176. package/dist-lib/spec/hyperschema/types.d.ts +21 -1
  177. package/dist-lib/spec/local/hyperdb/messages.js +110 -54
  178. package/dist-lib/spec/local/hyperschema/index.js +56 -28
  179. package/dist-lib/spec/local/hyperschema/schema.json +55 -20
  180. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -1
  181. package/dist-lib/spec/mesh/hyperdb/db.json +255 -51
  182. package/dist-lib/spec/mesh/hyperdb/index.js +1063 -404
  183. package/dist-lib/spec/mesh/hyperdb/messages.js +1647 -1214
  184. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +62 -56
  185. package/dist-lib/spec/mesh/hyperdispatch/index.js +156 -139
  186. package/dist-lib/spec/mesh/hyperdispatch/messages.js +787 -629
  187. package/dist-lib/spec/mesh/hyperschema/index.js +787 -629
  188. package/dist-lib/spec/mesh/hyperschema/schema.json +283 -174
  189. package/dist-lib/spec/mesh/hyperschema/types.d.ts +29 -1
  190. package/package.json +23 -17
  191. package/worker/assistant-sidecar.mjs +3 -0
  192. package/dist-lib/lib/core/files/processing/settle.d.ts +0 -13
  193. package/dist-lib/lib/core/files/processing/settle.js +0 -86
  194. package/dist-lib/lib/core/migrations/device-indexes.d.ts +0 -2
  195. package/dist-lib/lib/core/migrations/device-indexes.js +0 -7
@@ -0,0 +1,14 @@
1
+ const FALSY_VALUES = new Map([
2
+ ['false', false],
3
+ ['0', 0],
4
+ ['""', '']
5
+ ]);
6
+ export function configValue(value) {
7
+ return value === false || value === 0 || value === ''
8
+ ? { value: null, falsyValue: JSON.stringify(value) }
9
+ : { value };
10
+ }
11
+ export function readConfigValue({ value, falsyValue }) {
12
+ const falsy = falsyValue ?? '';
13
+ return FALSY_VALUES.has(falsy) ? FALSY_VALUES.get(falsy) : (value ?? null);
14
+ }
@@ -7,8 +7,9 @@ type RouteHandler<T> = (op: T, ctx: RouterContext) => Promise<unknown>;
7
7
  export declare function trackActivity<T>(handler: RouteHandler<T>): RouteHandler<T>;
8
8
  export declare function devicesByWriterKey(tx: HyperDB, writerKey: Buffer): HyperDBStream<QvacDevice>;
9
9
  export declare function liveDeviceForWriter(db: HyperDB, writerKey: Buffer): Promise<QvacDevice | null>;
10
- export declare function liveDevicesIndexed(db: HyperDB): Promise<boolean>;
11
- export declare function backfillLiveDevices(tx: HyperDB): Promise<void>;
10
+ export declare function liveDevicesOf(db: HyperDB, memberId: Buffer): Promise<QvacDevice[]>;
11
+ export declare function devicesOfMember(db: HyperDB, memberId: Buffer): Promise<QvacDevice[]>;
12
+ export declare function liveDevices(db: HyperDB): AsyncGenerator<QvacDevice>;
12
13
  export interface MarkWriterRevokedOptions {
13
14
  readonly revoked: boolean;
14
15
  }
@@ -1,5 +1,6 @@
1
1
  import b4a from 'b4a';
2
- import { DEVICES, DEVICES_BY_WRITER_KEY, INDEX_BACKFILLS, LIVE_DEVICES_BY_WRITER_KEY, WRITER_ACTIVITY } from '../collections.js';
2
+ import { WORKSPACES_RULES, rulesVersion } from './mesh-rules.js';
3
+ import { DEVICES, DEVICES_BY_WRITER_KEY, DEVICES_BY_MEMBER, LIVE_DEVICES_BY_MEMBER, LIVE_DEVICES_BY_WRITER_KEY, WRITER_ACTIVITY } from '../collections.js';
3
4
  export function trackActivity(handler) {
4
5
  return async (op, ctx) => {
5
6
  const applied = await handler(op, ctx);
@@ -15,35 +16,52 @@ export function devicesByWriterKey(tx, writerKey) {
15
16
  lte: { writerKey }
16
17
  });
17
18
  }
18
- export async function liveDeviceForWriter(db, writerKey) {
19
- if (!(await liveDevicesIndexed(db))) {
20
- for await (const device of devicesByWriterKey(db, writerKey)) {
21
- if (!device.revoked)
22
- return device;
23
- }
24
- return null;
25
- }
19
+ export function liveDeviceForWriter(db, writerKey) {
26
20
  return db.findOne(LIVE_DEVICES_BY_WRITER_KEY, {
27
21
  gte: { writerKey },
28
22
  lte: { writerKey }
29
23
  });
30
24
  }
31
- export async function liveDevicesIndexed(db) {
32
- return ((await db.get(INDEX_BACKFILLS, { index: LIVE_DEVICES_BY_WRITER_KEY })) !==
33
- null);
25
+ async function indexed(db) {
26
+ return (await rulesVersion(db)) >= WORKSPACES_RULES;
27
+ }
28
+ function ownerOf(device) {
29
+ return device.memberId ?? device.claimedMemberId ?? null;
30
+ }
31
+ async function scanDevicesOf(db, memberId) {
32
+ const rows = [];
33
+ for await (const device of db.find(DEVICES)) {
34
+ const owner = ownerOf(device);
35
+ if (owner && b4a.equals(owner, memberId)) {
36
+ rows.push(device);
37
+ }
38
+ }
39
+ return rows;
40
+ }
41
+ export async function liveDevicesOf(db, memberId) {
42
+ if (!(await indexed(db))) {
43
+ return (await scanDevicesOf(db, memberId)).filter((device) => !device.revoked);
44
+ }
45
+ return db
46
+ .find(LIVE_DEVICES_BY_MEMBER, { gte: { memberId }, lte: { memberId } })
47
+ .toArray();
34
48
  }
35
- export async function backfillLiveDevices(tx) {
36
- const indexed = new Set();
37
- for await (const device of tx.find(LIVE_DEVICES_BY_WRITER_KEY)) {
38
- indexed.add(b4a.toHex(device.id));
49
+ export async function devicesOfMember(db, memberId) {
50
+ if (!(await indexed(db))) {
51
+ return scanDevicesOf(db, memberId);
39
52
  }
40
- for await (const device of tx.find(DEVICES)) {
41
- if (device.revoked || !device.writerKey || indexed.has(b4a.toHex(device.id))) {
42
- continue;
53
+ return db.find(DEVICES_BY_MEMBER, { gte: { memberId }, lte: { memberId } }).toArray();
54
+ }
55
+ export async function* liveDevices(db) {
56
+ if (!(await indexed(db))) {
57
+ for await (const device of db.find(DEVICES)) {
58
+ if (!device.revoked && ownerOf(device)) {
59
+ yield device;
60
+ }
43
61
  }
44
- await tx.insert(DEVICES, { ...device, version: undefined });
62
+ return;
45
63
  }
46
- await tx.insert(INDEX_BACKFILLS, { index: LIVE_DEVICES_BY_WRITER_KEY });
64
+ yield* db.find(LIVE_DEVICES_BY_MEMBER);
47
65
  }
48
66
  export async function markWriterRevoked(tx, writerKey, opts) {
49
67
  const { revoked } = opts;
@@ -3,16 +3,17 @@ import b4a from 'b4a';
3
3
  import { devicesByWriterKey, liveDeviceForWriter, trackActivity } from './activity.js';
4
4
  import { verifyWriterClaim } from '../device-attestation.js';
5
5
  import { projectRules } from './projects.js';
6
- import { AGENT_HOSTS, AGENTS, DEVICES } from '../collections.js';
6
+ import { AGENT_HOSTS, AGENT_HOSTS_BY_AGENT, AGENT_HOSTS_BY_DEVICE, AGENTS, DEVICES } from '../collections.js';
7
7
  export function hostRowsOf(db, writerKey) {
8
8
  return db.find(AGENT_HOSTS, { gte: { writerKey }, lte: { writerKey } }).toArray();
9
9
  }
10
10
  export async function hostedDevice(db, deviceId) {
11
- for await (const row of db.find(AGENT_HOSTS)) {
12
- if (b4a.equals(row.deviceId, deviceId))
13
- return true;
14
- }
15
- return false;
11
+ return (await hostRowsFor(db, deviceId)).length > 0;
12
+ }
13
+ function hostRowsFor(db, deviceId) {
14
+ return db
15
+ .find(AGENT_HOSTS_BY_DEVICE, { gte: { deviceId }, lte: { deviceId } })
16
+ .toArray();
16
17
  }
17
18
  export async function dropHostRows(tx, writerKey) {
18
19
  for (const { agentId } of await hostRowsOf(tx, writerKey)) {
@@ -20,12 +21,7 @@ export async function dropHostRows(tx, writerKey) {
20
21
  }
21
22
  }
22
23
  export async function releaseHostedDevice({ tx, host }, deviceId, keep) {
23
- const held = [];
24
- for await (const row of tx.find(AGENT_HOSTS)) {
25
- if (b4a.equals(row.deviceId, deviceId)) {
26
- held.push(row);
27
- }
28
- }
24
+ const held = await hostRowsFor(tx, deviceId);
29
25
  const writers = new Map();
30
26
  for (const { writerKey, agentId } of held) {
31
27
  await tx.delete(AGENT_HOSTS, { writerKey, agentId });
@@ -41,13 +37,13 @@ export async function evictHosts({ tx, host }, agent) {
41
37
  if (await projectRules(tx)) {
42
38
  return;
43
39
  }
44
- const gone = [];
45
- for await (const row of tx.find(AGENT_HOSTS)) {
46
- if (row.agentId === agent.id &&
47
- (agent.deletedAt || !b4a.equals(row.deviceId, agent.providerDeviceId))) {
48
- gone.push(row);
49
- }
50
- }
40
+ const hosts = await tx
41
+ .find(AGENT_HOSTS_BY_AGENT, {
42
+ gte: { agentId: agent.id },
43
+ lte: { agentId: agent.id }
44
+ })
45
+ .toArray();
46
+ const gone = hosts.filter((row) => agent.deletedAt || !b4a.equals(row.deviceId, agent.providerDeviceId));
51
47
  for (const { writerKey, agentId } of gone) {
52
48
  await tx.delete(AGENT_HOSTS, { writerKey, agentId });
53
49
  const serving = (await hostRowsOf(tx, writerKey)).length > 0;
@@ -5,7 +5,7 @@ import { dropMemberDeviceKeys } from './project-device-keys.js';
5
5
  import { evictHosts } from './agent-join.js';
6
6
  import { isWorkspace } from './workspace.js';
7
7
  import { meshKind } from './mesh-kind.js';
8
- import { AGENT_USERS, AGENTS, DEVICES, MEMBER_KEYS } from '../collections.js';
8
+ import { AGENT_USERS, AGENT_USERS_BY_USER, AGENTS, DEVICES, MEMBER_KEYS } from '../collections.js';
9
9
  import { dropMemberSeeds, pruneHostSeeds } from './public-seeds.js';
10
10
  import { pruneProjectHostKeys } from './project-host-keys.js';
11
11
  async function memberOfDevice(tx, id) {
@@ -102,16 +102,17 @@ export async function dropMemberCopies(tx, deviceIds, at) {
102
102
  }
103
103
  }
104
104
  }
105
+ function usesBy(tx, userId) {
106
+ return tx.find(AGENT_USERS_BY_USER, { gte: { userId }, lte: { userId } }).toArray();
107
+ }
105
108
  export async function dropUser(tx, userId) {
106
- for (const row of await tx.find(AGENT_USERS).toArray()) {
107
- if (b4a.equals(row.userId, userId)) {
108
- await tx.delete(AGENT_USERS, { agentId: row.agentId, userId });
109
- }
109
+ for (const row of await usesBy(tx, userId)) {
110
+ await tx.delete(AGENT_USERS, { agentId: row.agentId, userId });
110
111
  }
111
112
  }
112
113
  export async function dropMemberAgentUses(tx, memberId, userId) {
113
- for (const row of await tx.find(AGENT_USERS).toArray()) {
114
- const agent = b4a.equals(row.userId, userId) && (await tx.get(AGENTS, { id: row.agentId }));
114
+ for (const row of await usesBy(tx, userId)) {
115
+ const agent = await tx.get(AGENTS, { id: row.agentId });
115
116
  const owner = agent && (await memberOfDevice(tx, agent.providerDeviceId));
116
117
  if (owner && b4a.equals(owner, memberId)) {
117
118
  await tx.delete(AGENT_USERS, { agentId: row.agentId, userId });
@@ -1,5 +1,8 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
3
+ import type { QvacChunk } from '../../../spec/mesh/hyperschema/types.d.ts';
3
4
  export declare function isPurged(tx: HyperDB, chatId: string): Promise<boolean>;
4
5
  export declare function foreignChat(tx: HyperDB, chatId: string | null | undefined): Promise<boolean>;
5
6
  export declare function registerChatRoutes(router: Router): void;
7
+ export declare function putChunk(tx: HyperDB, chunk: QvacChunk): Promise<void>;
8
+ export declare function deleteChatChunks(tx: HyperDB, chatId: string): Promise<void>;
@@ -1,7 +1,7 @@
1
1
  import { liveDeviceForWriter, trackActivity } from './activity.js';
2
2
  import { meshKind } from './mesh-kind.js';
3
3
  import { WORKSPACES_RULES, rulesVersion } from './mesh-rules.js';
4
- import { CHATS, CHAT_READS, CHUNKS, CHUNKS_BY_CHAT, CHUNK_COUNTERS, INDEXING_STATUS, INDEXING_STATUS_BY_CHAT, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, KNOWLEDGE_ARTIFACTS_BY_KNOWLEDGE, KNOWLEDGE_BY_CHAT, PURGED_CHATS, TOOL_ACTIVITY, TOOL_ACTIVITY_BY_AGENT } from '../collections.js';
4
+ import { CHATS, CHAT_CHUNKS, CHAT_READS, CHUNKS, CHUNKS_BY_CHAT, CHUNK_COUNTERS, INDEXING_STATUS, INDEXING_STATUS_BY_CHAT, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, KNOWLEDGE_ARTIFACTS_BY_KNOWLEDGE, KNOWLEDGE_BY_CHAT, PURGED_CHATS, TOOL_ACTIVITY, TOOL_ACTIVITY_BY_AGENT } from '../collections.js';
5
5
  export async function isPurged(tx, chatId) {
6
6
  return (await tx.get(PURGED_CHATS, { chatId })) !== null;
7
7
  }
@@ -81,12 +81,23 @@ async function markChatRead(read, { tx, node, log }) {
81
81
  await tx.insert(CHAT_READS, row);
82
82
  return row;
83
83
  }
84
+ export async function putChunk(tx, chunk) {
85
+ await tx.insert(CHUNKS, chunk);
86
+ await tx.insert(CHAT_CHUNKS, chunk);
87
+ }
88
+ export async function deleteChatChunks(tx, chatId) {
89
+ const inChat = { gte: { chatId }, lte: { chatId } };
90
+ for (const chunk of await tx.find(CHAT_CHUNKS, inChat).toArray()) {
91
+ await tx.delete(CHAT_CHUNKS, { chatId, seq: chunk.seq });
92
+ }
93
+ }
84
94
  async function purgeChat(tx, chat) {
85
95
  const chatId = chat.id;
86
96
  const inChat = { gte: { chatId }, lte: { chatId } };
87
97
  for (const id of await idsOf(tx.find(CHUNKS_BY_CHAT, inChat))) {
88
98
  await tx.delete(CHUNKS, { id });
89
99
  }
100
+ await deleteChatChunks(tx, chatId);
90
101
  await tx.delete(CHUNK_COUNTERS, { chatId });
91
102
  for (const read of await tx.find(CHAT_READS, inChat).toArray()) {
92
103
  await tx.delete(CHAT_READS, { chatId, memberId: read.memberId });
@@ -1,11 +1,11 @@
1
1
  import { devicesByWriterKey, liveDeviceForWriter, trackActivity } from './activity.js';
2
- import { foreignChat, isPurged } from './chat.js';
2
+ import { deleteChatChunks, foreignChat, isPurged, putChunk } from './chat.js';
3
3
  import { appendRow, asModelRequest, closeRounds, hostOutputRefusal, openRound, outputRefusal, roundChunkRefusal, served } from './model-requests.js';
4
4
  import { outsideProject, projectRules, signerMember } from './projects.js';
5
5
  import { runHasTerminalStatus, runnerRefusal, TERMINAL_RUN_STATES } from './run-requests.js';
6
6
  import { CHUNK_PAYLOAD_FIELDS, ragSourcesWithinBounds } from '../../../schema/qvac.js';
7
7
  import b4a from 'b4a';
8
- import { CHUNKS, DEVICES, RUN_REQUEST_BY_RUN } from '../collections.js';
8
+ import { CHAT_CHUNKS, CHUNKS, CHUNKS_BY_CHAT, DEVICES, RUN_REQUEST_BY_RUN } from '../collections.js';
9
9
  import { isProbeRound } from '../../probe-rounds.js';
10
10
  const CREDITED_CHUNK_TYPES = new Set(['run-request', 'run-status']);
11
11
  export function runRequestOf(db, chatId, runId) {
@@ -170,7 +170,7 @@ export function registerChunkRoutes(router) {
170
170
  }
171
171
  const appended = await appendRow(tx, { ...openRound(op), requesterMemberId });
172
172
  if (round) {
173
- await tx.insert(CHUNKS, served(round, op.createdAt));
173
+ await putChunk(tx, served(round, op.createdAt));
174
174
  }
175
175
  if (op.type === 'run-status' &&
176
176
  TERMINAL_RUN_STATES.has(op.runStatus?.state ?? '') &&
@@ -199,7 +199,7 @@ export function registerChunkRoutes(router) {
199
199
  return;
200
200
  }
201
201
  const next = { ...chunk, runStatus: op.runStatus, updatedAt: op.updatedAt };
202
- await tx.insert(CHUNKS, next);
202
+ await putChunk(tx, next);
203
203
  return next;
204
204
  }
205
205
  if (chunk.type === 'attachment' && op.attachment !== null && op.attachment !== undefined) {
@@ -208,7 +208,7 @@ export function registerChunkRoutes(router) {
208
208
  return;
209
209
  }
210
210
  const next = { ...chunk, attachment: op.attachment, updatedAt: op.updatedAt };
211
- await tx.insert(CHUNKS, next);
211
+ await putChunk(tx, next);
212
212
  return next;
213
213
  }
214
214
  log?.warn('contract', 'update-chunk dropped: only a run-status (runStatus) or attachment (attachment) edits', { id: op.id });
@@ -238,8 +238,18 @@ export function registerChunkRoutes(router) {
238
238
  },
239
239
  updatedAt: op.resolvedAt
240
240
  };
241
- await tx.insert(CHUNKS, resolved);
241
+ await putChunk(tx, resolved);
242
242
  log?.info('contract', `approval ${op.approved ? 'approved' : 'denied'}`, { id: op.id });
243
243
  return resolved;
244
244
  }));
245
+ router.add('@qvac/reindex-chat-chunks', async ({ chatId }, { tx }) => {
246
+ if (await isPurged(tx, chatId)) {
247
+ await deleteChatChunks(tx, chatId);
248
+ return;
249
+ }
250
+ const inChat = { gte: { chatId }, lte: { chatId } };
251
+ for (const chunk of await tx.find(CHUNKS_BY_CHAT, inChat).toArray()) {
252
+ await tx.insert(CHAT_CHUNKS, chunk);
253
+ }
254
+ });
245
255
  }
@@ -1,5 +1,6 @@
1
1
  import { capDeviceName } from '../../../schema/qvac.js';
2
- import { backfillLiveDevices, trackActivity } from './activity.js';
2
+ import { trackActivity } from './activity.js';
3
+ import { reindexShippedRows } from './shipped-rows.js';
3
4
  import { sealed } from './seal.js';
4
5
  import { verifyWriterClaim } from '../device-attestation.js';
5
6
  import b4a from 'b4a';
@@ -82,7 +83,7 @@ export function registerDeviceRoutes(router) {
82
83
  await tx.insert(DEVICES, next);
83
84
  return next;
84
85
  }));
85
- router.add('@qvac/report-device-capabilities', trackActivity(async ({ deviceId, installedToolNames, installedSkillNames, installedModelNames, cachedModelNames, readySkillNames, toolCatalog }, { tx, node, log }) => {
86
+ router.add('@qvac/report-device-capabilities', trackActivity(async ({ deviceId, installedToolNames, installedSkillNames, installedModelNames, cachedModelNames, readySkillNames, toolCatalog, customModels }, { tx, node, log }) => {
86
87
  const existing = await tx.get(DEVICES, { id: deviceId });
87
88
  if (!existing) {
88
89
  log?.debug('contract', 'report-device-capabilities dropped: unknown device');
@@ -101,7 +102,8 @@ export function registerDeviceRoutes(router) {
101
102
  installedModelNames: installedModelNames ?? [],
102
103
  cachedModelNames: cachedModelNames ?? [],
103
104
  readySkillNames: readySkillNames ?? existing.readySkillNames,
104
- toolCatalog: toolCatalog ?? existing.toolCatalog
105
+ toolCatalog: toolCatalog ?? existing.toolCatalog,
106
+ customModels: customModels ?? existing.customModels
105
107
  };
106
108
  await tx.insert(DEVICES, next);
107
109
  return next;
@@ -123,6 +125,6 @@ export function registerDeviceRoutes(router) {
123
125
  return next;
124
126
  }));
125
127
  router.add('@qvac/reindex-devices', async (_op, { tx }) => {
126
- await backfillLiveDevices(tx);
128
+ await reindexShippedRows(tx);
127
129
  });
128
130
  }
@@ -35,7 +35,8 @@ const OPTIMISTIC_ROUTES = new Set([
35
35
  '@qvac/attest-device',
36
36
  '@qvac/join-member',
37
37
  '@qvac/open-join',
38
- '@qvac/agent-join'
38
+ '@qvac/agent-join',
39
+ '@qvac/remove-writer'
39
40
  ]);
40
41
  function writersOnly(router) {
41
42
  return {
@@ -1,5 +1,5 @@
1
1
  import crypto from 'hypercore-crypto';
2
- import { liveDeviceForWriter, trackActivity } from './activity.js';
2
+ import { liveDeviceForWriter, liveDevicesOf, trackActivity } from './activity.js';
3
3
  import { nextGrantSequence, removedSinceMint } from './grant-order.js';
4
4
  import { meshKind, sharedKind } from './mesh-kind.js';
5
5
  import { isKickedRow } from '../helpers/index.js';
@@ -25,31 +25,18 @@ async function kickedFromOutside(tx, device, memberId) {
25
25
  const kickerMember = kicker.memberId ?? kicker.claimedMemberId;
26
26
  return !kickerMember || !b4a.equals(kickerMember, memberId);
27
27
  }
28
- async function memberStanding(tx, memberId, grant) {
29
- if (await removedSinceMint(tx, memberId, grant))
30
- return 'removed';
31
- for await (const device of tx.find(DEVICES)) {
32
- if (device.revoked)
33
- continue;
34
- const member = device.memberId ?? device.claimedMemberId;
35
- if (member && b4a.equals(member, memberId)) {
36
- return 'present';
37
- }
38
- }
39
- return null;
40
- }
41
- async function inviteStanding(tx, { device, memberId, grant }) {
42
- const standing = await memberStanding(tx, memberId, grant);
43
- if (standing !== 'present') {
44
- return standing;
28
+ async function inviteRefusal(tx, { device, memberId, grant }) {
29
+ if (await removedSinceMint(tx, memberId, grant)) {
30
+ return 'the member was removed after the mint';
45
31
  }
46
- if (!(await sharedKind(tx))) {
32
+ const present = (await liveDevicesOf(tx, memberId)).length > 0;
33
+ if (!present || !(await sharedKind(tx))) {
47
34
  return null;
48
35
  }
49
- if (!device?.revoked) {
50
- return 'present';
36
+ if (device?.revoked && (await removedBy(tx, device)) !== 'own-member') {
37
+ return null;
51
38
  }
52
- return (await removedBy(tx, device)) === 'own-member' ? 'present' : null;
39
+ return 'the member is still present';
53
40
  }
54
41
  async function belongsToAnotherMember(tx, device, memberId) {
55
42
  if (!device?.memberId || b4a.equals(device.memberId, memberId))
@@ -223,15 +210,9 @@ export function registerInviteRoutes(router) {
223
210
  log?.warn('contract', 'claim-invite dropped: the device belongs to another member', ids);
224
211
  return;
225
212
  }
226
- const standing = spent
227
- ? null
228
- : await inviteStanding(tx, { device: existing, memberId, grant });
229
- if (standing === 'present') {
230
- log?.warn('contract', 'claim-invite dropped: the member is still present', ids);
231
- return;
232
- }
233
- if (standing === 'removed') {
234
- log?.warn('contract', 'claim-invite dropped: the member was removed after the mint', ids);
213
+ const refusal = spent ? null : await inviteRefusal(tx, { device: existing, memberId, grant });
214
+ if (refusal) {
215
+ log?.warn('contract', `claim-invite dropped: ${refusal}`, ids);
235
216
  return;
236
217
  }
237
218
  const admission = await admitDevice(context, payload.device, {
@@ -38,6 +38,7 @@ function merge(mesh, existing, rejoin) {
38
38
  labeledAt: relabels ? mesh.labeledAt : existing.labeledAt,
39
39
  shared: remarks ? mesh.shared : existing.shared,
40
40
  sharedAt: remarks ? mesh.sharedAt : existing.sharedAt,
41
- workspace: existing.workspace || mesh.workspace
41
+ workspace: existing.workspace || mesh.workspace,
42
+ kicked: rejoin ? mesh.kicked : mesh.kicked || existing.kicked
42
43
  };
43
44
  }
@@ -1,5 +1,6 @@
1
1
  import { MESH_RULES } from '../collections.js';
2
2
  import { CoreError } from '../../errors.js';
3
+ import { reindexShippedRows } from './shipped-rows.js';
3
4
  export const MESH_RULES_ROW = 'mesh';
4
5
  export const WORKSPACES_RULES = 1;
5
6
  export const KNOWN_RULES = WORKSPACES_RULES;
@@ -23,6 +24,9 @@ export function registerMeshRulesRoutes(router) {
23
24
  throw new CoreError('MESH_RULES_FROM_A_NEWER_BUILD', [rules, KNOWN_RULES]);
24
25
  }
25
26
  const atGenesis = existing ? existing.atGenesis : host.genesis;
27
+ if (held < WORKSPACES_RULES && !atGenesis) {
28
+ await reindexShippedRows(tx);
29
+ }
26
30
  const row = { id: MESH_RULES_ROW, rules, atGenesis: atGenesis || undefined };
27
31
  await tx.insert(MESH_RULES, row);
28
32
  log?.info('contract', 'mesh rules raised', { rules });
@@ -1,6 +1,7 @@
1
1
  import b4a from 'b4a';
2
2
  import { chunkTypeOrdinal } from '../../../schema/indexes.js';
3
3
  import { liveDeviceForWriter, trackActivity } from './activity.js';
4
+ import { putChunk } from './chat.js';
4
5
  import { projectRules } from './projects.js';
5
6
  import { isProbeRound } from '../../probe-rounds.js';
6
7
  import { derivedId } from '../id.js';
@@ -75,7 +76,7 @@ export async function appendRow(tx, row) {
75
76
  lastActivityAt: Math.max(counter?.lastActivityAt ?? 0, row.createdAt)
76
77
  });
77
78
  const appended = { ...row, seq };
78
- await tx.insert(CHUNKS, appended);
79
+ await putChunk(tx, appended);
79
80
  return appended;
80
81
  }
81
82
  async function resetRound(tx, request, { node, by, at }) {
@@ -169,7 +170,7 @@ export async function closeRounds(tx, { chatId, runId, at }) {
169
170
  for (const chunk of rounds) {
170
171
  const request = asModelRequest(chunk);
171
172
  if (request?.runId === runId && LIVE.has(request.modelRequest.state)) {
172
- await tx.insert(CHUNKS, moved(request, { ...request.modelRequest, state: 'cancelled' }, at));
173
+ await putChunk(tx, moved(request, { ...request.modelRequest, state: 'cancelled' }, at));
173
174
  }
174
175
  }
175
176
  }
@@ -217,7 +218,7 @@ export function registerModelRequestRoutes(router) {
217
218
  await resetRound(tx, request, { node, by: takenBy, at });
218
219
  }
219
220
  const taken = moved(request, { ...request.modelRequest, state: 'taken', takenBy, takenAt: at }, at);
220
- await tx.insert(CHUNKS, taken);
221
+ await putChunk(tx, taken);
221
222
  return taken;
222
223
  }));
223
224
  router.add('@qvac/settle-model-request', trackActivity(async (op, context) => {
@@ -236,7 +237,7 @@ export function registerModelRequestRoutes(router) {
236
237
  return;
237
238
  }
238
239
  const settled = moved(request, { ...request.modelRequest, state: op.state, result: op.result, reason: op.reason }, op.at);
239
- await tx.insert(CHUNKS, settled);
240
+ await putChunk(tx, settled);
240
241
  return settled;
241
242
  }));
242
243
  router.add('@qvac/cancel-model-request', trackActivity(async (op, { tx, node, log }) => {
@@ -252,7 +253,7 @@ export function registerModelRequestRoutes(router) {
252
253
  return;
253
254
  }
254
255
  const cancelled = moved(request, { ...request.modelRequest, state: 'cancelled' }, op.at);
255
- await tx.insert(CHUNKS, cancelled);
256
+ await putChunk(tx, cancelled);
256
257
  return cancelled;
257
258
  }));
258
259
  router.add('@qvac/reopen-model-request', trackActivity(async (op, { tx, node, log }) => {
@@ -273,7 +274,7 @@ export function registerModelRequestRoutes(router) {
273
274
  await resetRound(tx, request, { node, by: owner.id, at: op.at });
274
275
  const { takenBy: _takenBy, takenAt: _takenAt, ...ask } = request.modelRequest;
275
276
  const reopened = moved(request, { ...ask, state: 'open' }, op.at);
276
- await tx.insert(CHUNKS, reopened);
277
+ await putChunk(tx, reopened);
277
278
  return reopened;
278
279
  }));
279
280
  }
@@ -1,5 +1,5 @@
1
1
  import b4a from 'b4a';
2
- import { PROJECT_DEVICE_KEYS } from '../collections.js';
2
+ import { PROJECT_DEVICE_KEYS, PROJECT_DEVICE_KEYS_BY_DEVICE, PROJECT_DEVICE_KEYS_BY_MEMBER } from '../collections.js';
3
3
  export function readProjectDeviceKey(tx, projectId, deviceId) {
4
4
  return tx.get(PROJECT_DEVICE_KEYS, { projectId, deviceId });
5
5
  }
@@ -12,9 +12,6 @@ async function drop(tx, rows, match) {
12
12
  await tx.delete(PROJECT_DEVICE_KEYS, { projectId: row.projectId, deviceId: row.deviceId });
13
13
  }
14
14
  }
15
- function allRows(tx) {
16
- return tx.find(PROJECT_DEVICE_KEYS).toArray();
17
- }
18
15
  export async function dropProjectDeviceKeys(tx, projectId) {
19
16
  await drop(tx, await deviceKeysOf(tx, projectId), () => true);
20
17
  }
@@ -22,8 +19,20 @@ export async function dropListingDeviceKeys(tx, projectId, memberId) {
22
19
  await drop(tx, await deviceKeysOf(tx, projectId), (row) => b4a.equals(row.memberId, memberId));
23
20
  }
24
21
  export async function dropMemberDeviceKeys(tx, memberId) {
25
- await drop(tx, await allRows(tx), (row) => b4a.equals(row.memberId, memberId));
22
+ const rows = await tx
23
+ .find(PROJECT_DEVICE_KEYS_BY_MEMBER, {
24
+ gte: { memberId },
25
+ lte: { memberId }
26
+ })
27
+ .toArray();
28
+ await drop(tx, rows, () => true);
26
29
  }
27
30
  export async function dropDeviceKeysOf(tx, deviceId) {
28
- await drop(tx, await allRows(tx), (row) => b4a.equals(row.deviceId, deviceId));
31
+ const rows = await tx
32
+ .find(PROJECT_DEVICE_KEYS_BY_DEVICE, {
33
+ gte: { deviceId },
34
+ lte: { deviceId }
35
+ })
36
+ .toArray();
37
+ await drop(tx, rows, () => true);
29
38
  }
@@ -1,5 +1,5 @@
1
1
  import b4a from 'b4a';
2
- import { DEVICES, PROJECT_HOST_KEYS } from '../collections.js';
2
+ import { DEVICES, PROJECT_HOST_KEYS, PROJECT_HOST_KEYS_BY_DEVICE } from '../collections.js';
3
3
  import { liveAgentsOn, usesAny } from './public-seeds.js';
4
4
  export function readProjectHostKey(tx, projectId, deviceId) {
5
5
  return tx.get(PROJECT_HOST_KEYS, { projectId, deviceId });
@@ -21,9 +21,10 @@ export async function dropMemberHostKeys(tx, projectId, memberId) {
21
21
  }
22
22
  }
23
23
  }
24
- async function hostKeysSealedTo(tx, deviceId) {
25
- const rows = await tx.find(PROJECT_HOST_KEYS).toArray();
26
- return rows.filter((row) => b4a.equals(row.deviceId, deviceId));
24
+ function hostKeysSealedTo(tx, deviceId) {
25
+ return tx
26
+ .find(PROJECT_HOST_KEYS_BY_DEVICE, { gte: { deviceId }, lte: { deviceId } })
27
+ .toArray();
27
28
  }
28
29
  export async function dropDeviceHostKeys(tx, deviceId) {
29
30
  for (const row of await hostKeysSealedTo(tx, deviceId)) {
@@ -1,4 +1,4 @@
1
- import { liveDeviceForWriter, trackActivity } from './activity.js';
1
+ import { liveDeviceForWriter, liveDevicesOf, trackActivity } from './activity.js';
2
2
  import { meshKind } from './mesh-kind.js';
3
3
  import { WORKSPACES_RULES, rulesVersion } from './mesh-rules.js';
4
4
  import { isWorkspace, readMemberKey, upgradeMembers } from './workspace.js';
@@ -7,7 +7,7 @@ import { dropMemberHostKeys, dropProjectHostKeys, readProjectHostKey } from './p
7
7
  import { dropListingDeviceKeys, dropProjectDeviceKeys, readProjectDeviceKey } from './project-device-keys.js';
8
8
  import b4a from 'b4a';
9
9
  import { SEALED_SEED_BYTES } from '../device-seal.js';
10
- import { AGENT_USERS, AGENTS, CHAT_GROUPS, DEVICES, MESH_FOUNDERS, PROJECT_DEVICE_KEYS, PROJECT_HOST_KEYS, PROJECT_KEYS, PROJECT_MEMBERS, PROJECTS } from '../collections.js';
10
+ import { AGENT_USERS, AGENTS, CHAT_GROUPS, DEVICES, MESH_FOUNDERS, PROJECT_DEVICE_KEYS, PROJECT_HOST_KEYS, PROJECT_KEYS, PROJECT_KEYS_BY_MEMBER, PROJECT_MEMBERS, PROJECTS } from '../collections.js';
11
11
  export const UNGROUPED = '@qvac/ungrouped';
12
12
  export function readProject(tx, id) {
13
13
  return tx.get(PROJECTS, { id });
@@ -22,10 +22,11 @@ async function dropProjectKeys(tx, projectId) {
22
22
  }
23
23
  }
24
24
  export async function dropMemberProjectKeys(tx, memberId) {
25
- for (const row of await tx.find(PROJECT_KEYS).toArray()) {
26
- if (b4a.equals(row.memberId, memberId)) {
27
- await tx.delete(PROJECT_KEYS, { projectId: row.projectId, memberId });
28
- }
25
+ const rows = await tx
26
+ .find(PROJECT_KEYS_BY_MEMBER, { gte: { memberId }, lte: { memberId } })
27
+ .toArray();
28
+ for (const row of rows) {
29
+ await tx.delete(PROJECT_KEYS, { projectId: row.projectId, memberId });
29
30
  }
30
31
  }
31
32
  export function listsMember(project, memberId) {
@@ -53,13 +54,7 @@ export async function outsideProject(tx, memberId) {
53
54
  return !memberId || !(await inProject(tx, memberId));
54
55
  }
55
56
  export async function liveMember(tx, memberId, since = -1) {
56
- for await (const device of tx.find(DEVICES)) {
57
- const theirs = device.memberId && b4a.equals(device.memberId, memberId);
58
- if (!device.revoked && theirs && device.joinedAt > since) {
59
- return true;
60
- }
61
- }
62
- return false;
57
+ return (await liveDevicesOf(tx, memberId)).some((device) => !!device.memberId && b4a.equals(device.memberId, memberId) && device.joinedAt > since);
63
58
  }
64
59
  export async function signerMember({ tx, node }) {
65
60
  return (await liveDeviceForWriter(tx, node.key))?.memberId ?? null;
@@ -312,6 +307,10 @@ async function putProjectMember({ memberId, addedAt }, context) {
312
307
  log?.warn('contract', "put-project-member dropped: only the project's members add one");
313
308
  return;
314
309
  }
310
+ if (b4a.equals(signer, memberId)) {
311
+ log?.warn('contract', 'put-project-member dropped: a member cannot add itself');
312
+ return;
313
+ }
315
314
  }
316
315
  const row = { memberId, addedAt };
317
316
  await tx.insert(PROJECT_MEMBERS, row);
@@ -1,5 +1,4 @@
1
- import b4a from 'b4a';
2
- import { AGENT_USERS, AGENTS_BY_PROVIDER, PUBLIC_SEEDS } from '../collections.js';
1
+ import { AGENT_USERS, AGENTS_BY_PROVIDER, PUBLIC_SEEDS, PUBLIC_SEEDS_BY_DEVICE } from '../collections.js';
3
2
  export function readPublicSeed(tx, memberId, deviceId) {
4
3
  return tx.get(PUBLIC_SEEDS, { memberId, deviceId });
5
4
  }
@@ -17,9 +16,10 @@ export async function dropMemberSeeds(tx, memberId) {
17
16
  await tx.delete(PUBLIC_SEEDS, { memberId, deviceId: row.deviceId });
18
17
  }
19
18
  }
20
- async function seedsSealedTo(tx, deviceId) {
21
- const rows = await tx.find(PUBLIC_SEEDS).toArray();
22
- return rows.filter((row) => b4a.equals(row.deviceId, deviceId));
19
+ function seedsSealedTo(tx, deviceId) {
20
+ return tx
21
+ .find(PUBLIC_SEEDS_BY_DEVICE, { gte: { deviceId }, lte: { deviceId } })
22
+ .toArray();
23
23
  }
24
24
  export async function dropDeviceSeeds(tx, deviceId) {
25
25
  for (const row of await seedsSealedTo(tx, deviceId)) {