create-windy 0.3.17 → 0.3.19

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 (58) hide show
  1. package/README.md +14 -36
  2. package/dist/cli.js +1534 -3079
  3. package/package.json +1 -1
  4. package/template/.windy-template.json +2 -2
  5. package/template/AGENTS.md +6 -0
  6. package/template/README.md +5 -10
  7. package/template/apps/agent-server/Dockerfile +2 -1
  8. package/template/apps/agent-server/README.md +13 -5
  9. package/template/apps/agent-server/adk_web_agents/canonical_debug/__init__.py +1 -0
  10. package/template/apps/agent-server/adk_web_agents/canonical_debug/agent.py +125 -0
  11. package/template/apps/agent-server/project/adk-web-debug.json +4 -0
  12. package/template/apps/agent-server/src/southwind_agent_server/adk_debug.py +348 -0
  13. package/template/apps/agent-server/tests/test_adk_debug_result_polling.py +139 -0
  14. package/template/apps/agent-server/tests/test_adk_web_canonical_debug.py +235 -0
  15. package/template/apps/server/src/agent/run-debug-authorization.ts +27 -0
  16. package/template/apps/server/src/agent/run-facade-adk-debug.test.ts +56 -0
  17. package/template/apps/server/src/agent/run-facade.test-support.ts +235 -0
  18. package/template/apps/server/src/agent/run-facade.test.ts +8 -244
  19. package/template/apps/server/src/agent/run-facade.ts +23 -48
  20. package/template/apps/server/src/agent/run-operation-registry.ts +51 -0
  21. package/template/apps/server/src/agent/runtime-bootstrap.ts +5 -2
  22. package/template/apps/server/src/agent/tool-audit.ts +3 -0
  23. package/template/apps/server/src/agent/tool-registry.test.ts +29 -0
  24. package/template/apps/server/src/application-services.ts +83 -15
  25. package/template/apps/server/src/guards.ts +16 -0
  26. package/template/apps/server/src/index.ts +23 -20
  27. package/template/apps/server/src/jobs/legacy-adapter.integration.test.ts +0 -1
  28. package/template/apps/server/src/jobs/legacy-adapter.test.ts +6 -0
  29. package/template/apps/server/src/jobs/legacy-service-runtime.ts +18 -0
  30. package/template/apps/server/src/module-host/durable-context-unavailable.test.ts +84 -0
  31. package/template/apps/server/src/module-host/durable-context.test.ts +14 -0
  32. package/template/apps/server/src/module-host/durable-context.ts +17 -15
  33. package/template/apps/server/src/module-host/request-context.test.ts +30 -1
  34. package/template/apps/server/src/module-host/request-context.ts +15 -2
  35. package/template/apps/server/src/module-host/storage-port.ts +1 -1
  36. package/template/apps/server/src/platform-bootstrap.ts +16 -0
  37. package/template/apps/server/src/runtime-adk-debug.test.ts +246 -0
  38. package/template/apps/server/src/runtime-adk-debug.ts +158 -0
  39. package/template/apps/server/src/runtime-request-actor.test.ts +55 -0
  40. package/template/apps/server/src/runtime-request-actor.ts +22 -0
  41. package/template/apps/server/src/runtime.test.ts +3 -0
  42. package/template/apps/server/src/runtime.ts +36 -3
  43. package/template/apps/server/src/scheduler/definitions.test.ts +16 -0
  44. package/template/docker-compose.agent-debug.yml +56 -0
  45. package/template/docker-compose.yml +1 -39
  46. package/template/docs/architecture/ai-runtime.md +14 -5
  47. package/template/docs/architecture/module-manifest-composition.md +14 -0
  48. package/template/docs/development/custom-login-page.md +2 -16
  49. package/template/docs/platform/agent-runtime.md +59 -16
  50. package/template/docs/platform/agent-tool-sandbox.md +4 -3
  51. package/template/docs/platform/platform-authentication.md +9 -1
  52. package/template/package.json +1 -1
  53. package/template/packages/database/drizzle/0043_activate_agent_dependencies.sql +196 -0
  54. package/template/packages/database/drizzle/meta/0043_snapshot.json +8335 -0
  55. package/template/packages/database/drizzle/meta/_journal.json +7 -0
  56. package/template/packages/database/src/schema-notification-delivery.test.ts +15 -6
  57. package/template/packages/database/src/schema-workflow-notification.test.ts +44 -29
  58. package/template/scripts/dev-adk-web.ts +107 -0
@@ -1,4 +1,7 @@
1
+ // @windy-module system.bulk-data begin
1
2
  import { DictionaryBulkResourceHandler } from "./bulk-data/dictionary-handler.js";
3
+ // @windy-module system.bulk-data end
4
+ // @windy-module system.data-governance begin
2
5
  import { DrizzleGovernedExportRepository } from "./data-governance/export/drizzle-repository.js";
3
6
  import { WorkflowGovernedExportApproval } from "./data-governance/export/approval-port.js";
4
7
  import { GovernedExportJobRuntime } from "./data-governance/export/job-runtime.js";
@@ -9,25 +12,41 @@ import {
9
12
  GovernedUserExportSource,
10
13
  ModuleGovernedExportSource,
11
14
  } from "./data-governance/export/source.js";
15
+ // @windy-module system.data-governance end
16
+ // @windy-module system.bulk-data begin
12
17
  import { DrizzleBulkDataRepository } from "./bulk-data/drizzle-repository.js";
13
18
  import { InMemoryBulkDataRepository } from "./bulk-data/repository.js";
19
+ // @windy-module system.bulk-data end
14
20
  import type { createFoundationSnapshot } from "./foundation.js";
15
21
  import { createLegacyServiceRuntime } from "./jobs/legacy-service-runtime.js";
22
+ // @windy-module system.data-governance begin
16
23
  import { DurableArtifactService } from "./jobs/artifact-service.js";
24
+ // @windy-module system.data-governance end
25
+ // @windy-module system.notification begin
17
26
  import { DrizzleNotificationRepository } from "./notification/drizzle-repository.js";
18
27
  import { InMemoryNotificationRepository } from "./notification/repository.js";
28
+ // @windy-module system.notification end
19
29
  import type { createServerPersistence } from "./persistence.js";
20
30
  import type { createServerRuntime } from "./runtime.js";
21
31
  import { createPlatformTaskDefinitions } from "./scheduler/definitions.js";
22
32
  import { DrizzleSchedulerRepository } from "./scheduler/drizzle-repository.js";
23
33
  import { InMemorySchedulerRepository } from "./scheduler/repository.js";
24
34
  import type { ScheduledTaskDefinition } from "./scheduler/types.js";
35
+ // @windy-module system.storage begin
25
36
  import type { createStorageRuntime } from "./storage/runtime.js";
37
+ // @windy-module system.storage end
38
+ // @windy-module system.notification begin
26
39
  import type { PlatformConfig } from "@southwind-ai/config";
40
+ // @windy-module system.notification end
27
41
  import type { DurableHandlerRegistry } from "@southwind-ai/jobs";
42
+ // @windy-module system.storage begin
28
43
  import { collectActiveBlobRefs } from "./storage/blob-reference-registry.js";
44
+ // @windy-module system.storage end
29
45
  import type { createSystemRepositories } from "./system/seed.js";
46
+ // @windy-module system.data-governance begin
30
47
  import { identityRouteConfigs } from "./system/identity-route-config.js";
48
+ // @windy-module system.data-governance end
49
+ // @windy-module system.workflow begin
31
50
  import { SystemWorkflowAssigneeDirectory } from "./workflow/assignee-directory.js";
32
51
  import { DrizzleWorkflowRepository } from "./workflow/drizzle-repository.js";
33
52
  import {
@@ -37,6 +56,8 @@ import {
37
56
  } from "./workflow/effects.js";
38
57
  import { InMemoryWorkflowRepository } from "./workflow/repository.js";
39
58
  import { WorkflowService } from "./workflow/service.js";
59
+ // @windy-module system.workflow end
60
+ // @windy-module system.notification begin
40
61
  import { NotificationChannelRegistry } from "./notification/channel-adapter.js";
41
62
  import { DrizzleNotificationDeliveryRepository } from "./notification/delivery-repository.js";
42
63
  import { NotificationDeliveryWorker } from "./notification/delivery-worker.js";
@@ -48,6 +69,7 @@ import {
48
69
  import { SystemNotificationRecipientDirectory } from "./notification/recipient-directory.js";
49
70
  import { NotificationService } from "./notification/service.js";
50
71
  import { SmtpNotificationChannelAdapter } from "./notification/smtp-adapter.js";
72
+ // @windy-module system.notification end
51
73
  import type { InitializedModuleHosts } from "./module-host.js";
52
74
  import {
53
75
  createModuleDurableContextualizer,
@@ -60,10 +82,16 @@ interface ApplicationServicesInput {
60
82
  systemRepositories: ReturnType<typeof createSystemRepositories>;
61
83
  foundation: ReturnType<typeof createFoundationSnapshot>;
62
84
  runtime: ReturnType<typeof createServerRuntime>;
85
+ // @windy-module system.storage begin
63
86
  storage: ReturnType<typeof createStorageRuntime>;
87
+ // @windy-module system.storage end
64
88
  moduleTasks: ScheduledTaskDefinition[];
89
+ // @windy-module system.bulk-data begin
65
90
  bulkRetentionDays: number;
91
+ // @windy-module system.bulk-data end
92
+ // @windy-module system.notification begin
66
93
  mailConfig?: PlatformConfig["mail"];
94
+ // @windy-module system.notification end
67
95
  installModuleDurableHandlers?: (registry: DurableHandlerRegistry) => void;
68
96
  moduleHosts: InitializedModuleHosts;
69
97
  // @windy-module system.agent begin
@@ -75,6 +103,7 @@ export async function createApplicationServices(
75
103
  input: ApplicationServicesInput,
76
104
  ) {
77
105
  const { persistence } = input;
106
+ // @windy-module system.notification begin
78
107
  const notifications = persistence
79
108
  ? new DrizzleNotificationRepository(persistence.db)
80
109
  : new InMemoryNotificationRepository();
@@ -104,6 +133,8 @@ export async function createApplicationServices(
104
133
  notificationRecipients,
105
134
  notificationChannels,
106
135
  );
136
+ // @windy-module system.notification end
137
+ // @windy-module system.workflow begin
107
138
  const deferredTimeout = new DeferredWorkflowTimeoutScheduler();
108
139
  const workflow = new WorkflowService(
109
140
  persistence
@@ -116,31 +147,50 @@ export async function createApplicationServices(
116
147
  deferredTimeout,
117
148
  ),
118
149
  );
150
+ // @windy-module system.workflow end
151
+ // @windy-module system.data-governance begin
119
152
  const governedExportDispatch: {
120
153
  runNext?: GovernedExportJobRuntime["runNext"];
121
154
  } = {};
155
+ // @windy-module system.data-governance end
156
+ // @windy-module system.bulk-data begin
122
157
  const bulkRepository = persistence
123
158
  ? new DrizzleBulkDataRepository(persistence.db)
124
159
  : new InMemoryBulkDataRepository();
160
+ // @windy-module system.bulk-data end
125
161
  const taskDefinitions = [
126
162
  ...createPlatformTaskDefinitions(input.foundation.features, {
163
+ // @windy-module system.bulk-data begin
127
164
  bulkDataDispatch: () => services.bulkDataService.processNext(),
128
165
  bulkDataCleanup: () => services.bulkDataService.cleanupExpired(),
166
+ // @windy-module system.bulk-data end
167
+ // @windy-module system.storage begin
129
168
  storageCleanup: async () => {
130
169
  const now = new Date();
131
170
  const referencedRefs = await collectActiveBlobRefs(
132
- [services.durable.facts, bulkRepository],
171
+ [
172
+ services.durable.facts,
173
+ // @windy-module system.bulk-data begin
174
+ bulkRepository,
175
+ // @windy-module system.bulk-data end
176
+ ],
133
177
  now,
134
178
  );
135
179
  return {
136
180
  ...(await input.storage.uploads.cleanupExpired(100, referencedRefs)),
137
181
  };
138
182
  },
183
+ // @windy-module system.storage end
184
+ // @windy-module system.workflow begin
139
185
  workflowTimeoutDispatch: () => services.workflowTimeout.runNext(),
186
+ // @windy-module system.workflow end
187
+ // @windy-module system.data-governance begin
140
188
  governedExportDispatch: async () => {
141
189
  const result = await governedExportDispatch.runNext?.();
142
190
  return { claimed: Boolean(result && result.status !== "idle") };
143
191
  },
192
+ // @windy-module system.data-governance end
193
+ // @windy-module system.notification begin
144
194
  notificationDispatch: async () => {
145
195
  const scheduled = await notificationService.dispatchScheduled();
146
196
  const delivery = await notificationDeliveryWorker.runNext();
@@ -150,29 +200,36 @@ export async function createApplicationServices(
150
200
  delivery,
151
201
  };
152
202
  },
203
+ // @windy-module system.notification end
153
204
  }),
154
205
  ...input.moduleTasks,
155
206
  ];
156
207
  const services = createLegacyServiceRuntime({
157
208
  db: persistence?.db,
209
+ // @windy-module system.bulk-data begin
158
210
  bulkRepository,
159
211
  bulkHandlers: [
160
212
  new DictionaryBulkResourceHandler(input.systemRepositories.dictionaries),
161
213
  ],
162
214
  bulkLimits: { retentionDays: input.bulkRetentionDays },
163
215
  blobStore: input.storage.blobStore,
216
+ // @windy-module system.bulk-data end
164
217
  schedulerRepository: persistence
165
218
  ? new DrizzleSchedulerRepository(persistence.db)
166
219
  : new InMemorySchedulerRepository(),
167
220
  taskDefinitions,
168
221
  runtime: input.runtime,
222
+ // @windy-module system.workflow begin
169
223
  workflow,
224
+ // @windy-module system.workflow end
170
225
  installModuleDurableHandlers: input.installModuleDurableHandlers,
171
226
  durableContextualize: createModuleDurableContextualizer({
172
227
  hosts: input.moduleHosts,
173
228
  runtime: input.runtime,
174
229
  features: input.foundation.features,
230
+ // @windy-module system.storage begin
175
231
  storage: input.storage,
232
+ // @windy-module system.storage end
176
233
  // @windy-module system.agent begin
177
234
  backgroundAi: input.backgroundAi,
178
235
  // @windy-module system.agent end
@@ -180,6 +237,7 @@ export async function createApplicationServices(
180
237
  resolveCurrentSystemActor(input.systemRepositories, actorId),
181
238
  }),
182
239
  });
240
+ // @windy-module system.data-governance begin
183
241
  const governedExportRequests = persistence
184
242
  ? new DrizzleGovernedExportRepository(persistence.db)
185
243
  : new InMemoryGovernedExportRepository();
@@ -199,20 +257,21 @@ export async function createApplicationServices(
199
257
  resolveCurrentSystemActor(input.systemRepositories, actorId),
200
258
  );
201
259
  });
202
- const moduleGovernedExportRenderers = input.moduleHosts.governedResources.flatMap(
203
- ({ moduleName, registration }) => {
204
- const policy = input.moduleHosts
205
- .manifestOf(moduleName)
206
- .dataPolicies?.find(({ key }) => key === registration.policyKey);
207
- if (!policy) {
208
- throw new Error(`模块数据治理策略不存在:${registration.policyKey}`);
209
- }
210
- return (registration.export?.renderers ?? []).map((renderer) => ({
211
- resourceType: policy.resourceType,
212
- renderer,
213
- }));
214
- },
215
- );
260
+ const moduleGovernedExportRenderers =
261
+ input.moduleHosts.governedResources.flatMap(
262
+ ({ moduleName, registration }) => {
263
+ const policy = input.moduleHosts
264
+ .manifestOf(moduleName)
265
+ .dataPolicies?.find(({ key }) => key === registration.policyKey);
266
+ if (!policy) {
267
+ throw new Error(`模块数据治理策略不存在:${registration.policyKey}`);
268
+ }
269
+ return (registration.export?.renderers ?? []).map((renderer) => ({
270
+ resourceType: policy.resourceType,
271
+ renderer,
272
+ }));
273
+ },
274
+ );
216
275
  const governedExports = new GovernedExportJobRuntime(
217
276
  governedExportRequests,
218
277
  services.durable.service,
@@ -251,17 +310,26 @@ export async function createApplicationServices(
251
310
  { approvalRequired: true, retentionSeconds: 24 * 60 * 60 },
252
311
  );
253
312
  governedExportDispatch.runNext = () => governedExports.runNext();
313
+ // @windy-module system.data-governance end
314
+ // @windy-module system.workflow begin
254
315
  deferredTimeout.attach(services.workflowTimeout);
255
316
  await services.workflowTimeout.reconcile();
317
+ // @windy-module system.workflow end
256
318
  await services.scheduler.initialize();
257
319
  return {
258
320
  ...services,
321
+ // @windy-module system.notification begin
259
322
  notifications,
260
323
  notificationService,
261
324
  notificationDeliveryWorker,
325
+ // @windy-module system.notification end
326
+ // @windy-module system.workflow begin
262
327
  workflow,
328
+ // @windy-module system.workflow end
329
+ // @windy-module system.data-governance begin
263
330
  governedExports,
264
331
  governedExportService,
265
332
  governedExportRequests,
333
+ // @windy-module system.data-governance end
266
334
  };
267
335
  }
@@ -29,6 +29,22 @@ export interface RequestActor {
29
29
  scopeContext?: ScopeContext;
30
30
  }
31
31
 
32
+ // @windy-module system.agent begin
33
+ const moduleUserProjection = new WeakMap<RequestActor, string>();
34
+
35
+ export function bindModuleUserProjection(
36
+ actor: RequestActor,
37
+ userId: string,
38
+ ): RequestActor {
39
+ moduleUserProjection.set(actor, userId);
40
+ return actor;
41
+ }
42
+
43
+ export function projectedModuleUserId(actor: RequestActor): string | undefined {
44
+ return moduleUserProjection.get(actor);
45
+ }
46
+ // @windy-module system.agent end
47
+
32
48
  export interface RequestGuardContext {
33
49
  requestId: string;
34
50
  actor: RequestActor;
@@ -1,5 +1,3 @@
1
- import { loadPlatformConfig } from "@southwind-ai/config";
2
- import pino from "pino";
3
1
  import { registerAdminBootstrapRoute } from "./admin/routes.js";
4
2
  import { registerSessionContextRoute } from "./session/routes.js";
5
3
  import { registerFoundationRoute } from "./foundation-route.js";
@@ -19,9 +17,9 @@ import { readPositiveInt } from "./runtime-config.js";
19
17
  // @windy-module system.agent begin
20
18
  import { registerAgentPlatformRuntime } from "./agent/runtime-bootstrap.js";
21
19
  import { DeferredAgentBackgroundOperationPortFactory } from "./agent/background-operation-port.js";
20
+ import { resolveCurrentSystemActor } from "./system/actor.js";
22
21
  // @windy-module system.agent end
23
22
  import { createAuthRuntime } from "./auth/runtime.js";
24
- import { createServerPersistence } from "./persistence.js";
25
23
  // @windy-module system.license begin
26
24
  import { registerPlatformLicenseRoutes } from "./license/routes.js";
27
25
  import {
@@ -70,16 +68,8 @@ import {
70
68
  listenPlatformServer,
71
69
  logServerStartup,
72
70
  } from "./platform-http-runtime.js";
73
-
74
- const logger = pino({ name: "windy-server" });
75
- const platformConfig = loadPlatformConfig();
76
- const persistence = createServerPersistence(
77
- process.env,
78
- (status) => {
79
- logger.error({ audit: status }, "审计持久化进入降级状态");
80
- },
81
- platformConfig,
82
- );
71
+ import { createPlatformBootstrap } from "./platform-bootstrap.js";
72
+ const { logger, persistence, platformConfig } = createPlatformBootstrap();
83
73
  // @windy-module system.audit begin
84
74
  await persistence?.auditWriter.recover();
85
75
  // @windy-module system.audit end
@@ -105,6 +95,10 @@ const useInstalledLicense =
105
95
  // @windy-module system.license end
106
96
  const runtime = createServerRuntime(process.env, {
107
97
  actorResolver: persistence?.actorResolver,
98
+ // @windy-module system.agent begin
99
+ adkDebugActorResolver: (actorId) =>
100
+ resolveCurrentSystemActor(systemRepositories, actorId),
101
+ // @windy-module system.agent end
108
102
  // @windy-module system.license begin
109
103
  licenseResolver: useInstalledLicense
110
104
  ? platformLicenseRuntime
@@ -145,7 +139,9 @@ const storageRuntime = createStorageRuntime(
145
139
  persistence?.db,
146
140
  moduleHosts.uploadPolicies,
147
141
  );
142
+ // @windy-module system.configuration begin
148
143
  configurationRuntime.bindStorage(storageRuntime.uploads);
144
+ // @windy-module system.configuration end
149
145
  // @windy-module system.storage end
150
146
  // @windy-module system.settings begin
151
147
  const platformSettings = createPlatformSettingsRuntime(
@@ -173,23 +169,37 @@ const moduleTaskDefinitions = createModuleTaskDefinitions(
173
169
  const moduleBackgroundAi = new DeferredAgentBackgroundOperationPortFactory();
174
170
  // @windy-module system.agent end
175
171
  const {
172
+ // @windy-module system.bulk-data begin
176
173
  bulkDataService,
174
+ // @windy-module system.bulk-data end
177
175
  scheduler,
178
176
  taskDefinitions: scheduledTaskDefinitions,
177
+ // @windy-module system.notification begin
179
178
  notifications: notificationRepository,
180
179
  notificationService,
180
+ // @windy-module system.notification end
181
+ // @windy-module system.workflow begin
181
182
  workflow: workflowService,
183
+ // @windy-module system.workflow end
184
+ // @windy-module system.data-governance begin
182
185
  governedExportService,
186
+ // @windy-module system.data-governance end
183
187
  durable: durableJobRuntime,
184
188
  } = await createApplicationServices({
185
189
  persistence,
186
190
  systemRepositories,
187
191
  foundation: foundationSnapshot,
188
192
  runtime,
193
+ // @windy-module system.storage begin
189
194
  storage: storageRuntime,
195
+ // @windy-module system.storage end
190
196
  moduleTasks: moduleTaskDefinitions,
197
+ // @windy-module system.bulk-data begin
191
198
  bulkRetentionDays: readPositiveInt(process.env.BULK_DATA_RETENTION_DAYS, 7),
199
+ // @windy-module system.bulk-data end
200
+ // @windy-module system.notification begin
192
201
  mailConfig: platformConfig.mail,
202
+ // @windy-module system.notification end
193
203
  moduleHosts,
194
204
  installModuleDurableHandlers: moduleHosts.installDurableHandlers,
195
205
  // @windy-module system.agent begin
@@ -214,11 +224,9 @@ const app = await createPlatformServerApp({
214
224
  logger,
215
225
  observability,
216
226
  });
217
-
218
227
  // @windy-module system.license begin
219
228
  registerPlatformLicenseRoutes(app, platformLicenseRuntime);
220
229
  // @windy-module system.license end
221
-
222
230
  // @windy-module system.settings begin
223
231
  platformSettings.register(app, runtime, features);
224
232
  // @windy-module system.settings end
@@ -254,16 +262,13 @@ registerStorageRoutes(
254
262
  // @windy-module system.search begin
255
263
  searchHost.register(app);
256
264
  // @windy-module system.search end
257
-
258
265
  registerAdminBootstrapRoute(app, runtime, {
259
266
  modules: foundationModules,
260
267
  snapshot: foundationSnapshot,
261
268
  loadMenus: () => systemRepositories.menuRegistry.tree(),
262
269
  });
263
270
  registerSessionContextRoute(app, runtime, features);
264
-
265
271
  registerFoundationRoute(app, runtime, foundationSnapshot, systemRepositories);
266
-
267
272
  // @windy-module system.data-governance begin
268
273
  registerGovernedUserExportRoutes(app, governedExportService, runtime, features);
269
274
  // @windy-module system.data-governance end
@@ -323,7 +328,6 @@ installModuleHttpRuntime(moduleHosts, app, {
323
328
  governedExportService,
324
329
  // @windy-module system.data-governance end
325
330
  });
326
-
327
331
  validateInstalledRuntime({
328
332
  modules: foundationModules,
329
333
  routes: app.routes,
@@ -338,7 +342,6 @@ validateInstalledRuntime({
338
342
  // @windy-module system.search end
339
343
  hosts: moduleHosts,
340
344
  });
341
-
342
345
  const port = listenPlatformServer(app);
343
346
  // @windy-module system.scheduler begin
344
347
  schedulerWorker.start();
@@ -34,7 +34,6 @@ describePostgres("PostgreSQL legacy durable adapter", () => {
34
34
  });
35
35
  await pool.query(`
36
36
  truncate table
37
- governed_export_requests,
38
37
  bulk_data_artifacts,
39
38
  bulk_data_jobs,
40
39
  scheduled_task_runs,
@@ -5,9 +5,11 @@ import { createServerRuntime } from "../runtime.js";
5
5
  import { InMemorySchedulerRepository } from "../scheduler/repository.js";
6
6
  import { SchedulerService } from "../scheduler/service.js";
7
7
  import type { SchedulerClock } from "../scheduler/types.js";
8
+ // @windy-module system.bulk-data begin
8
9
  import { InMemoryBulkDataRepository } from "../bulk-data/repository.js";
9
10
  import { BulkDataService } from "../bulk-data/service.js";
10
11
  import type { BulkResourceHandler } from "../bulk-data/resource-handler.js";
12
+ // @windy-module system.bulk-data end
11
13
  import { InMemoryDurableExecutionRepository } from "./in-memory-execution.js";
12
14
  import {
13
15
  DeferredLegacyExecutor,
@@ -17,6 +19,7 @@ import { DurableJobService } from "./service.js";
17
19
  import { DurableJobWorker } from "./worker.js";
18
20
 
19
21
  describe("legacy durable adapters", () => {
22
+ // @windy-module system.bulk-data begin
20
23
  test("running Bulk 在进程重启后回收 lease,重复启动与完成不重复执行", async () => {
21
24
  let now = new Date("2026-07-16T00:00:00.000Z");
22
25
  const legacy = new InMemoryBulkDataRepository();
@@ -107,6 +110,7 @@ describe("legacy durable adapters", () => {
107
110
  processedRows: 1,
108
111
  });
109
112
  });
113
+ // @windy-module system.bulk-data end
110
114
 
111
115
  test("Scheduler 采用未关联 running 记录且重放已完成记录为空操作", async () => {
112
116
  const now = new Date("2026-07-16T00:00:00.000Z");
@@ -179,6 +183,7 @@ describe("legacy durable adapters", () => {
179
183
  });
180
184
  });
181
185
 
186
+ // @windy-module system.bulk-data begin
182
187
  function testBulkHandler(): BulkResourceHandler {
183
188
  return {
184
189
  scopeMode: "platform",
@@ -197,3 +202,4 @@ function actor(id: string) {
197
202
  permissionKeys: [],
198
203
  };
199
204
  }
205
+ // @windy-module system.bulk-data end
@@ -1,10 +1,14 @@
1
1
  import { DurableHandlerRegistry } from "@southwind-ai/jobs";
2
+ // @windy-module system.bulk-data begin
2
3
  import type { BlobStore } from "@southwind-ai/storage";
4
+ // @windy-module system.bulk-data end
3
5
  import type { NodePgDatabase } from "drizzle-orm/node-postgres";
6
+ // @windy-module system.bulk-data begin
4
7
  import { BulkDataService } from "../bulk-data/service.js";
5
8
  import type { BulkDataLimits } from "../bulk-data/input-validation.js";
6
9
  import type { BulkDataRepository } from "../bulk-data/repository.js";
7
10
  import type { BulkResourceHandler } from "../bulk-data/resource-handler.js";
11
+ // @windy-module system.bulk-data end
8
12
  import type { createServerRuntime } from "../runtime.js";
9
13
  import type { SchedulerRepository } from "../scheduler/repository.js";
10
14
  import { SchedulerService } from "../scheduler/service.js";
@@ -18,19 +22,25 @@ import {
18
22
  } from "./legacy-adapter.js";
19
23
  import { DurableJobService } from "./service.js";
20
24
  import { DurableJobWorker, type DurableWorkerOptions } from "./worker.js";
25
+ // @windy-module system.workflow begin
21
26
  import type { WorkflowService } from "../workflow/service.js";
22
27
  import { WorkflowTimeoutRuntime } from "../workflow/timeout-runtime.js";
28
+ // @windy-module system.workflow end
23
29
 
24
30
  interface LegacyServiceRuntimeInput {
25
31
  db?: NodePgDatabase;
32
+ // @windy-module system.bulk-data begin
26
33
  bulkRepository: BulkDataRepository;
27
34
  bulkHandlers: BulkResourceHandler[];
28
35
  bulkLimits: Partial<BulkDataLimits>;
29
36
  blobStore: BlobStore;
37
+ // @windy-module system.bulk-data end
30
38
  schedulerRepository: SchedulerRepository;
31
39
  taskDefinitions: ScheduledTaskDefinition[];
32
40
  runtime: ReturnType<typeof createServerRuntime>;
41
+ // @windy-module system.workflow begin
33
42
  workflow: WorkflowService;
43
+ // @windy-module system.workflow end
34
44
  installModuleDurableHandlers?: (registry: DurableHandlerRegistry) => void;
35
45
  durableContextualize?: DurableWorkerOptions["contextualize"];
36
46
  }
@@ -47,13 +57,16 @@ export function createLegacyServiceRuntime(input: LegacyServiceRuntimeInput) {
47
57
  const worker = new DurableJobWorker(execution, registry, {
48
58
  contextualize: input.durableContextualize,
49
59
  });
60
+ // @windy-module system.workflow begin
50
61
  const workflowTimeout = new WorkflowTimeoutRuntime(
51
62
  service,
52
63
  worker,
53
64
  registry,
54
65
  input.workflow,
55
66
  );
67
+ // @windy-module system.workflow end
56
68
 
69
+ // @windy-module system.bulk-data begin
57
70
  const bulkExecutor = new DeferredLegacyExecutor();
58
71
  const bulkDataService = new BulkDataService(
59
72
  input.bulkRepository,
@@ -71,6 +84,7 @@ export function createLegacyServiceRuntime(input: LegacyServiceRuntimeInput) {
71
84
  bulkExecutor.attach(({ legacyId, generation }) =>
72
85
  bulkDataService.resumeDurable(legacyId, generation),
73
86
  );
87
+ // @windy-module system.bulk-data end
74
88
 
75
89
  const schedulerExecutor = new DeferredLegacyExecutor();
76
90
  const taskDefinitions = input.taskDefinitions;
@@ -91,10 +105,14 @@ export function createLegacyServiceRuntime(input: LegacyServiceRuntimeInput) {
91
105
  );
92
106
 
93
107
  return {
108
+ // @windy-module system.bulk-data begin
94
109
  bulkDataService,
110
+ // @windy-module system.bulk-data end
95
111
  scheduler,
96
112
  taskDefinitions,
113
+ // @windy-module system.workflow begin
97
114
  workflowTimeout,
115
+ // @windy-module system.workflow end
98
116
  durable: { facts, service, registry, worker },
99
117
  };
100
118
  }
@@ -0,0 +1,84 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import type { DurableHandlerContext } from "@southwind-ai/jobs";
3
+ import { featureKey } from "@southwind-ai/shared";
4
+ import { buildActorScopeContexts } from "../data-access/context.js";
5
+ import { captureAuthorizationScope } from "../data-access/scope-snapshot.js";
6
+ import type { RequestActor } from "../guards.js";
7
+ import { createServerRuntime } from "../runtime.js";
8
+ import { createModuleDurableContextualizer } from "./durable-context.js";
9
+
10
+ const feature = {
11
+ key: featureKey("evidence.catalog"),
12
+ label: "电子物证",
13
+ module: "evidence",
14
+ visible: "visible" as const,
15
+ enabled: true,
16
+ stage: "stable" as const,
17
+ };
18
+
19
+ describe("模块 Durable Handler 可选端口", () => {
20
+ test("未挂载 Storage 时仍重建 Actor,并对文件读取 fail-closed", async () => {
21
+ const actor = currentActor();
22
+ const contextualize = createModuleDurableContextualizer({
23
+ hosts: {} as never,
24
+ runtime: createServerRuntime(
25
+ { NODE_ENV: "test" },
26
+ { featureCatalog: [feature] },
27
+ ),
28
+ features: [feature],
29
+ resolveActor: async () => actor,
30
+ });
31
+ const context = await contextualize({
32
+ claim: { job: job(actor) },
33
+ moduleName: "evidence",
34
+ context: baseContext(),
35
+ });
36
+
37
+ expect(context).toMatchObject({ actor: { id: "user-reader" } });
38
+ await expect(
39
+ (
40
+ context as typeof context & {
41
+ storage: { openResourceFile(input: never): Promise<unknown> };
42
+ }
43
+ ).storage.openResourceFile({} as never),
44
+ ).rejects.toThrow("模块 evidence 的存储 Port 未挂载");
45
+ });
46
+ });
47
+
48
+ function currentActor(): RequestActor {
49
+ return {
50
+ id: "user-reader",
51
+ type: "user",
52
+ name: "物证读取者",
53
+ roleCodes: ["investigator"],
54
+ permissionKeys: ["evidence.read"],
55
+ ...buildActorScopeContexts("user-reader", "dept-case", [
56
+ {
57
+ id: "role-investigator",
58
+ code: "investigator",
59
+ dataScope: "all",
60
+ departmentIds: [],
61
+ },
62
+ ]),
63
+ };
64
+ }
65
+
66
+ function job(actor: RequestActor) {
67
+ return {
68
+ id: "job-evidence",
69
+ requestedBy: actor.id,
70
+ authorizationScope: captureAuthorizationScope(actor.scopeContext).snapshot,
71
+ };
72
+ }
73
+
74
+ function baseContext(): DurableHandlerContext {
75
+ return {
76
+ jobId: "job-evidence",
77
+ stageKey: "main",
78
+ attemptNumber: 1,
79
+ signal: new AbortController().signal,
80
+ heartbeat: async () => undefined,
81
+ progress: async () => undefined,
82
+ publishArtifact: async () => undefined,
83
+ };
84
+ }