@powerhousedao/reactor-workflow 6.2.3-dev.25 → 6.2.3-dev.26

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/dist/index.js CHANGED
@@ -753,6 +753,27 @@ function searchBlocks(query, limit, local) {
753
753
  };
754
754
  }
755
755
  //#endregion
756
+ //#region src/reactor/run-scope.ts
757
+ const storage = new AsyncLocalStorage();
758
+ function withRunScope(scope, fn) {
759
+ return storage.run(scope, fn);
760
+ }
761
+ function currentWorkflowId() {
762
+ return storage.getStore()?.workflowId;
763
+ }
764
+ function currentRunId() {
765
+ return storage.getStore()?.runId ?? void 0;
766
+ }
767
+ function currentBoundConnections() {
768
+ return storage.getStore()?.connections;
769
+ }
770
+ function currentDocumentRecorder() {
771
+ return storage.getStore()?.recordDocuments;
772
+ }
773
+ function currentPieceWorker() {
774
+ return storage.getStore()?.pieceWorker;
775
+ }
776
+ //#endregion
756
777
  //#region src/reactor/reactor-port.ts
757
778
  const DRIVE_DOCUMENT_TYPES = /* @__PURE__ */ new Set(["powerhouse/document-drive", "powerhouse/reactor-drive"]);
758
779
  const FIND_PAGE_LIMIT = 100;
@@ -828,7 +849,8 @@ var SubgraphReactorPort = class {
828
849
  };
829
850
  }
830
851
  async get(input) {
831
- return documentSummary(await this.client.get(input.documentId), true);
852
+ const document = await this.client.get(input.documentId);
853
+ return this.handOver(documentSummary(document, true));
832
854
  }
833
855
  async find(input) {
834
856
  const limit = input.limit ?? FIND_PAGE_LIMIT;
@@ -843,32 +865,38 @@ var SubgraphReactorPort = class {
843
865
  results = (await Promise.all(types.map((type) => this.findByType(type, limit)))).flat();
844
866
  }
845
867
  const seen = /* @__PURE__ */ new Set();
846
- return results.filter((document) => {
868
+ return this.handOver(results.filter((document) => {
847
869
  if (seen.has(document.header.id)) return false;
848
870
  seen.add(document.header.id);
849
871
  return true;
850
- }).filter((document) => matchesState(document, input.match)).map((document) => documentSummary(document, input.withState === true));
872
+ }).filter((document) => matchesState(document, input.match)).map((document) => documentSummary(document, input.withState === true)));
851
873
  }
852
874
  async create(input) {
853
875
  const target = input.parentId ? await this.resolveDriveTarget(input.parentId) : null;
854
876
  if (!target) {
855
877
  const created = await this.client.createEmpty(input.documentType, { parentIdentifier: input.parentId });
856
- if (!input.name) return documentSummary(created, true);
878
+ if (!input.name) return this.handOver(documentSummary(created, true));
857
879
  const naming = createAction("SET_NAME", { name: input.name });
858
880
  const named = await this.client.execute(created.header.id, "main", [naming]);
859
881
  assertOperationsApplied(named, [naming]);
860
- return documentSummary(named, true);
882
+ return this.handOver(documentSummary(named, true));
861
883
  }
862
884
  const module = await this.client.getDocumentModelModule(input.documentType);
863
885
  const empty = withSignaturePolicy(module.utils.createDocument(), await this.client.getCreateSignaturePolicy());
864
886
  if (input.name) empty.header.name = input.name;
865
- return documentSummary(await this.client.drives.addFile(target.driveId, empty, target.parentFolder), true);
887
+ const created = await this.client.drives.addFile(target.driveId, empty, target.parentFolder);
888
+ return this.handOver(documentSummary(created, true));
866
889
  }
867
890
  async execute(input) {
868
891
  const actions = input.actions.map((entry) => createAction(entry.type, entry.input, void 0, void 0, entry.scope ?? "global"));
869
892
  const document = await this.client.execute(input.documentId, input.branch ?? "main", actions);
870
893
  assertOperationsApplied(document, input.actions);
871
- return documentSummary(document, true);
894
+ return this.handOver(documentSummary(document, true));
895
+ }
896
+ async handOver(summaries) {
897
+ const record = currentDocumentRecorder();
898
+ if (record) await record((Array.isArray(summaries) ? summaries : [summaries]).map((summary) => summary.documentId));
899
+ return summaries;
872
900
  }
873
901
  async findByType(type, limit, parentId) {
874
902
  try {
@@ -939,24 +967,6 @@ var ScopedDesignTimeReactorPort = class {
939
967
  }
940
968
  };
941
969
  //#endregion
942
- //#region src/reactor/run-scope.ts
943
- const storage = new AsyncLocalStorage();
944
- function withRunScope(scope, fn) {
945
- return storage.run(scope, fn);
946
- }
947
- function currentWorkflowId() {
948
- return storage.getStore()?.workflowId;
949
- }
950
- function currentRunId() {
951
- return storage.getStore()?.runId ?? void 0;
952
- }
953
- function currentBoundConnections() {
954
- return storage.getStore()?.connections;
955
- }
956
- function currentPieceWorker() {
957
- return storage.getStore()?.pieceWorker;
958
- }
959
- //#endregion
960
970
  //#region src/reactor/piece-store-port.ts
961
971
  const logger$3 = childLogger(["workflow", "piece-store"]);
962
972
  const PROJECT_SCOPE_KEY = "reactor";
@@ -1518,6 +1528,7 @@ async function up(db) {
1518
1528
  } catch (error) {
1519
1529
  if (!isDuplicateObject(error)) throw new Error(`Could not add the step_execution (run_id, step_id) unique constraint, which per-step journaling upserts against: ${errorMessage(error)}`, { cause: error });
1520
1530
  }
1531
+ await db.schema.createTable("run_document").addColumn("run_id", "text", (col) => col.notNull()).addColumn("document_id", "text", (col) => col.notNull()).addPrimaryKeyConstraint("run_document_pk", ["run_id", "document_id"]).ifNotExists().execute();
1521
1532
  await db.schema.createTable("piece_store").addColumn("scope", "text", (col) => col.notNull()).addColumn("scope_key", "text", (col) => col.notNull()).addColumn("key", "text", (col) => col.notNull()).addColumn("value", "text", (col) => col.notNull()).addColumn("updated_at", "text", (col) => col.notNull()).addPrimaryKeyConstraint("piece_store_pk", [
1522
1533
  "scope",
1523
1534
  "scope_key",
@@ -1834,6 +1845,16 @@ var WorkflowRunStore = class WorkflowRunStore {
1834
1845
  async getSteps(runId) {
1835
1846
  return this.db.selectFrom("step_execution").selectAll().where("run_id", "=", runId).orderBy("ordinal", "asc").execute();
1836
1847
  }
1848
+ async recordRunDocuments(runId, documentIds) {
1849
+ if (documentIds.length === 0) return;
1850
+ await this.db.insertInto("run_document").values([...new Set(documentIds)].map((documentId) => ({
1851
+ run_id: runId,
1852
+ document_id: documentId
1853
+ }))).onConflict((oc) => oc.columns(["run_id", "document_id"]).doNothing()).execute();
1854
+ }
1855
+ async getRunDocuments(runId) {
1856
+ return (await this.db.selectFrom("run_document").select("document_id").where("run_id", "=", runId).execute()).map((row) => row.document_id);
1857
+ }
1837
1858
  async getTriggerState(workflowId) {
1838
1859
  return this.db.selectFrom("trigger_state").selectAll().where("workflow_id", "=", workflowId).executeTakeFirst();
1839
1860
  }
@@ -2662,6 +2683,21 @@ function stringField(record, key) {
2662
2683
  const value = record[key];
2663
2684
  return typeof value === "string" && value !== "" ? value : void 0;
2664
2685
  }
2686
+ function journaledTriggerDocumentIds(payload) {
2687
+ if (payload === null) return [];
2688
+ try {
2689
+ return triggerDocumentIds(JSON.parse(payload));
2690
+ } catch {
2691
+ return [];
2692
+ }
2693
+ }
2694
+ function triggerDocumentIds(payload) {
2695
+ const record = inputRecord(payload);
2696
+ return [...new Set([stringField(record, "documentId"), stringField(record, "driveId")].filter((id) => id !== void 0))];
2697
+ }
2698
+ function isAbsent(error) {
2699
+ return error instanceof Error && (error.name === "DocumentNotFoundError" || error.name === "DocumentDeletedError");
2700
+ }
2665
2701
  function inputRecord(input) {
2666
2702
  if (input === null || typeof input !== "object") return {};
2667
2703
  return input;
@@ -3492,7 +3528,7 @@ var WorkflowRuntimeService = class {
3492
3528
  if (scope.length === 0) return [];
3493
3529
  } else if (!ctx) return [];
3494
3530
  const rows = await store.listRuns(scope, args.limit ?? 25);
3495
- const readable = await this.readableRows(rows, (row) => row.workflow_id, ctx);
3531
+ const readable = await this.servedRuns(await this.readableRows(rows, (row) => row.workflow_id, ctx), ctx);
3496
3532
  return Promise.all(readable.map(async (row) => ({
3497
3533
  row,
3498
3534
  steps: await store.getSteps(row.id)
@@ -3504,14 +3540,16 @@ var WorkflowRuntimeService = class {
3504
3540
  const row = await store.getRun(runId);
3505
3541
  if (!row) return null;
3506
3542
  if (!await this.canReadDocument(row.workflow_id, ctx)) return null;
3543
+ if ((await this.servedRuns([row], ctx)).length === 0) return null;
3507
3544
  return {
3508
3545
  row,
3509
3546
  steps: await store.getSteps(row.id)
3510
3547
  };
3511
3548
  }
3512
3549
  async connections(ctx) {
3513
- const page = await this.host.reactorClient.find({ type: "powerhouse/connection" });
3514
- return (await this.readableDocuments(page.results, ctx)).map((document) => {
3550
+ const subject = ctx && this.host.subjectOf?.(ctx);
3551
+ const page = await this.host.reactorClient.find({ type: "powerhouse/connection" }, subject ? { subject } : void 0);
3552
+ return (await this.readableDocuments(page.results, ctx)).filter((document) => document.state.global !== void 0).map((document) => {
3515
3553
  const state = document.state.global;
3516
3554
  return {
3517
3555
  id: document.header.id,
@@ -3782,6 +3820,32 @@ var WorkflowRuntimeService = class {
3782
3820
  canReadDocument(documentId, ctx) {
3783
3821
  return this.host.assertCanRead(documentId, ctx).then(() => true).catch(() => false);
3784
3822
  }
3823
+ async servedRuns(rows, ctx) {
3824
+ if (!ctx) return [];
3825
+ const decisions = /* @__PURE__ */ new Map();
3826
+ const store = await this.store();
3827
+ const served = await Promise.all(rows.map(async (row) => this.servesDocuments([...journaledTriggerDocumentIds(row.trigger_payload), ...await store?.getRunDocuments(row.id) ?? []], ctx, decisions)));
3828
+ return rows.filter((_, index) => served[index]);
3829
+ }
3830
+ async servesDocuments(documentIds, ctx, decisions = /* @__PURE__ */ new Map()) {
3831
+ return (await Promise.all([...new Set(documentIds)].map((documentId) => {
3832
+ let decision = decisions.get(documentId);
3833
+ if (!decision) {
3834
+ decision = this.servesRunDocument(documentId, ctx);
3835
+ decisions.set(documentId, decision);
3836
+ }
3837
+ return decision;
3838
+ }))).every(Boolean);
3839
+ }
3840
+ async servesRunDocument(documentId, ctx) {
3841
+ if (await this.canReadDocument(documentId, ctx)) return true;
3842
+ try {
3843
+ await this.host.reactorClient.get(documentId, { scopes: ["document"] });
3844
+ } catch (error) {
3845
+ return isAbsent(error);
3846
+ }
3847
+ return false;
3848
+ }
3785
3849
  async assertCanReadDocument(documentId, ctx) {
3786
3850
  if (!ctx) throw new Error("Connection access requires an authenticated request");
3787
3851
  await this.host.assertCanRead(documentId, ctx);
@@ -3990,11 +4054,18 @@ var WorkflowRuntimeService = class {
3990
4054
  let session;
3991
4055
  try {
3992
4056
  session = this.workers().session();
4057
+ const journal = store;
4058
+ const journaledRunId = runId;
4059
+ const handed = /* @__PURE__ */ new Set();
3993
4060
  const result = await withRunScope({
3994
4061
  workflowId,
3995
4062
  runId,
3996
4063
  connections,
3997
- pieceWorker: session
4064
+ pieceWorker: session,
4065
+ recordDocuments: async (documentIds) => {
4066
+ for (const documentId of documentIds) handed.add(documentId);
4067
+ if (journal && journaledRunId) await journal.recordRunDocuments(journaledRunId, documentIds);
4068
+ }
3998
4069
  }, () => runWorkflow({
3999
4070
  definition,
4000
4071
  executor: this.executor,
@@ -4016,10 +4087,17 @@ var WorkflowRuntimeService = class {
4016
4087
  } catch (error) {
4017
4088
  this.logger.warn(`Run ${runId}: closing the run journal out failed; the run's outcome stands`, error);
4018
4089
  }
4019
- return {
4090
+ const finished = {
4020
4091
  ...result,
4021
4092
  runId
4022
4093
  };
4094
+ if (!ctx) return finished;
4095
+ const ids = [...triggerDocumentIds(triggerPayload), ...handed];
4096
+ return await this.servesDocuments(ids, ctx) ? finished : {
4097
+ status: finished.status,
4098
+ steps: [],
4099
+ runId
4100
+ };
4023
4101
  } catch (error) {
4024
4102
  if (store && runId) await store.failRun(runId, error instanceof Error ? error.message : String(error));
4025
4103
  throw error;
@@ -4033,6 +4111,7 @@ var WorkflowRuntimeService = class {
4033
4111
  const run = await store.getRun(runId);
4034
4112
  if (!run) throw new Error(`Run "${runId}" not found`);
4035
4113
  await this.assertCanReadDocument(run.workflow_id, ctx);
4114
+ if (!ctx || (await this.servedRuns([run], ctx)).length === 0) throw new Error(`Run "${runId}" not found`);
4036
4115
  if (run.status !== "FAILED") throw new Error(`Only FAILED runs can be rerun; run is ${run.status}`);
4037
4116
  const triggerPayload = run.trigger_payload === null ? void 0 : JSON.parse(run.trigger_payload);
4038
4117
  if (containsRedactedMarker(triggerPayload)) throw new Error(`Trigger payload of run "${runId}" was redacted and cannot be replayed; fire the workflow again instead of rerunning it`);
@@ -4051,7 +4130,7 @@ var WorkflowRuntimeService = class {
4051
4130
  return this.fire(run.workflow_id, triggerPayload, "rerun", {
4052
4131
  completedSteps,
4053
4132
  rerunOf: runId
4054
- });
4133
+ }, ctx);
4055
4134
  }
4056
4135
  };
4057
4136
  /** The runtime a host composes: one instance, its lifetime the host's. */