@inflexa-ai/harness 0.48.2 → 0.48.4

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 (85) hide show
  1. package/dist/agents/sandbox/index.js +3 -0
  2. package/dist/agents/sandbox/mdv-preparation-agent.d.ts +9 -0
  3. package/dist/agents/sandbox/mdv-preparation-agent.js +37 -0
  4. package/dist/app/chat-turn-workflow.d.ts +3 -1
  5. package/dist/app/chat-turn-workflow.js +19 -3
  6. package/dist/app/chat-turn.d.ts +4 -2
  7. package/dist/app/chat-turn.js +70 -44
  8. package/dist/app/in-process-turn.js +3 -6
  9. package/dist/app/work.d.ts +7 -3
  10. package/dist/app/work.js +17 -16
  11. package/dist/contracts/chat-events.d.ts +5 -0
  12. package/dist/contracts/chat-parts.d.ts +64 -5
  13. package/dist/contracts/chat-parts.js +3 -1
  14. package/dist/contracts/index.d.ts +1 -1
  15. package/dist/contracts/part-registry.js +2 -0
  16. package/dist/contracts/schemas/chat-events.d.ts +2 -0
  17. package/dist/contracts/schemas/chat-events.js +1 -0
  18. package/dist/contracts/schemas/chat-parts.d.ts +197 -5
  19. package/dist/contracts/schemas/chat-parts.js +32 -2
  20. package/dist/index.d.ts +1 -1
  21. package/dist/lib/regulatory-corpus/fetch-ich.js +1 -16
  22. package/dist/lib/slug.d.ts +18 -0
  23. package/dist/lib/slug.js +29 -0
  24. package/dist/lib/test/pdf-fixture.d.ts +5 -0
  25. package/dist/lib/test/pdf-fixture.js +14 -0
  26. package/dist/loop/chat-stream.js +1 -1
  27. package/dist/loop/run-loop.js +39 -35
  28. package/dist/loop/sub-agent.d.ts +2 -2
  29. package/dist/loop/sub-agent.js +1 -1
  30. package/dist/loop/test/metric-reader.d.ts +9 -0
  31. package/dist/loop/test/metric-reader.js +33 -0
  32. package/dist/loop/test/unnamed-model.d.ts +6 -0
  33. package/dist/loop/test/unnamed-model.js +24 -0
  34. package/dist/memory/conversation-display-recorder.d.ts +2 -0
  35. package/dist/memory/conversation-display-recorder.js +6 -2
  36. package/dist/memory/conversation-display-storage.d.ts +5 -2
  37. package/dist/memory/conversation-display-storage.js +28 -1
  38. package/dist/prompts/conversation.d.ts +1 -1
  39. package/dist/prompts/conversation.js +6 -4
  40. package/dist/prompts/sandbox/mdv-preparation-agent.d.ts +1 -0
  41. package/dist/prompts/sandbox/mdv-preparation-agent.js +56 -0
  42. package/dist/providers/ai-sdk.d.ts +17 -14
  43. package/dist/providers/ai-sdk.js +14 -16
  44. package/dist/report-model/fixture-resolver.js +9 -135
  45. package/dist/report-model/production-resolver.d.ts +3 -3
  46. package/dist/report-model/production-resolver.js +12 -127
  47. package/dist/report-model/reference-read.d.ts +38 -0
  48. package/dist/report-model/reference-read.js +149 -0
  49. package/dist/report-model/reference-resolver.d.ts +7 -0
  50. package/dist/report-model/reference-resolver.js +1 -1
  51. package/dist/report-render/chart.js +48 -46
  52. package/dist/report-render/figures/common.d.ts +34 -0
  53. package/dist/report-render/figures/common.js +4 -1
  54. package/dist/report-render/figures/equal-units.js +2 -4
  55. package/dist/report-render/figures/sankey.js +1 -3
  56. package/dist/report-render/figures/upset.js +2 -5
  57. package/dist/runtime/assemble.js +2 -1
  58. package/dist/sandbox/execution-id.js +2 -14
  59. package/dist/sandbox/leases.js +46 -9
  60. package/dist/schemas/validate-plan.js +2 -14
  61. package/dist/state/active-turns.d.ts +6 -2
  62. package/dist/state/active-turns.js +11 -7
  63. package/dist/state/data-profile.d.ts +8 -0
  64. package/dist/state/data-profile.js +18 -0
  65. package/dist/state/index.d.ts +1 -1
  66. package/dist/state/index.js +1 -1
  67. package/dist/state/migrations/20261102120000_record_the_waiter_of_a_pause.d.ts +6 -0
  68. package/dist/state/migrations/20261102120000_record_the_waiter_of_a_pause.js +10 -0
  69. package/dist/state/migrations/index.js +2 -0
  70. package/dist/state/paused-work.d.ts +5 -3
  71. package/dist/state/paused-work.js +11 -7
  72. package/dist/state/runs.d.ts +3 -2
  73. package/dist/state/runs.js +4 -3
  74. package/dist/tasks/data-profile-index.js +3 -17
  75. package/dist/tasks/data-profile-resolve.js +3 -17
  76. package/dist/tasks/data-profile.js +1 -0
  77. package/dist/tools/lib/opentargets-client.js +15 -25
  78. package/dist/tools/report-authoring/authoring-tools.d.ts +3 -3
  79. package/dist/tools/report-authoring/authoring-tools.js +7 -6
  80. package/dist/tools/research/generate-analogy-report.js +10 -2
  81. package/dist/workflows/execute-analysis.js +4 -3
  82. package/dist/workflows/pause.d.ts +3 -3
  83. package/dist/workflows/pause.js +15 -9
  84. package/dist/workflows/sandbox-step.js +1 -0
  85. package/package.json +7 -4
@@ -70,6 +70,7 @@ export const ToolFinishedEventSchema = z.object({
70
70
  export const FinishEventSchema = z.object({
71
71
  type: z.literal("finish"),
72
72
  source: EventSourceSchema,
73
+ finishReason: z.string(),
73
74
  usage: TokenUsageRollupSchema.optional(),
74
75
  turnUsage: TokenUsageRollupSchema.optional(),
75
76
  });
@@ -93,6 +93,59 @@ export declare const PresentationPartSchema: z.ZodObject<{
93
93
  version: z.ZodNumber;
94
94
  }, z.core.$strip>], "kind">;
95
95
  }, z.core.$strip>;
96
+ export declare const AnalogyReportPartSchema: z.ZodObject<{
97
+ type: z.ZodLiteral<"data-analogy-report">;
98
+ id: z.ZodString;
99
+ report: z.ZodUnion<readonly [z.ZodObject<{
100
+ problemSummary: z.ZodString;
101
+ problemObjects: z.ZodArray<z.ZodObject<{
102
+ name: z.ZodString;
103
+ role: z.ZodString;
104
+ }, z.core.$strip>>;
105
+ problemRelations: z.ZodArray<z.ZodString>;
106
+ keyTerms: z.ZodArray<z.ZodString>;
107
+ schemaVersion: z.ZodLiteral<"1">;
108
+ analogies: z.ZodArray<z.ZodObject<{
109
+ targetDomain: z.ZodString;
110
+ analogyTitle: z.ZodString;
111
+ objectMappings: z.ZodArray<z.ZodObject<{
112
+ source: z.ZodString;
113
+ target: z.ZodString;
114
+ rationale: z.ZodString;
115
+ }, z.core.$strip>>;
116
+ sharedRelations: z.ZodString;
117
+ coverage: z.ZodEnum<{
118
+ available: "available";
119
+ not_loaded: "not_loaded";
120
+ queried_no_data: "queried_no_data";
121
+ search_failed: "search_failed";
122
+ }>;
123
+ solutions: z.ZodArray<z.ZodObject<{
124
+ title: z.ZodString;
125
+ sourceDomain: z.ZodString;
126
+ description: z.ZodString;
127
+ keyConcepts: z.ZodArray<z.ZodString>;
128
+ relevance: z.ZodString;
129
+ sources: z.ZodArray<z.ZodObject<{
130
+ url: z.ZodURL;
131
+ title: z.ZodString;
132
+ }, z.core.$strip>>;
133
+ githubRepos: z.ZodArray<z.ZodObject<{
134
+ url: z.ZodURL;
135
+ description: z.ZodOptional<z.ZodString>;
136
+ }, z.core.$strip>>;
137
+ }, z.core.$strip>>;
138
+ }, z.core.$strip>>;
139
+ }, z.core.$strip>, z.ZodObject<{
140
+ schemaVersion: z.ZodLiteral<"1">;
141
+ error: z.ZodObject<{
142
+ kind: z.ZodEnum<{
143
+ "extraction-failed": "extraction-failed";
144
+ }>;
145
+ message: z.ZodString;
146
+ }, z.core.$strip>;
147
+ }, z.core.$strip>]>;
148
+ }, z.core.$strip>;
96
149
  export declare const PLAN_ID_RE: RegExp;
97
150
  export declare const PlanPartSchema: z.ZodObject<{
98
151
  type: z.ZodLiteral<"data-plan">;
@@ -600,6 +653,21 @@ export declare const PauseCauseSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
600
653
  kind: z.ZodLiteral<"host">;
601
654
  reason: z.ZodString;
602
655
  }, z.core.$strip>], "kind">;
656
+ export declare const PauseWaiterSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
657
+ kind: z.ZodLiteral<"turn">;
658
+ }, z.core.$strip>, z.ZodObject<{
659
+ kind: z.ZodLiteral<"sub-agent">;
660
+ agentId: z.ZodString;
661
+ }, z.core.$strip>, z.ZodObject<{
662
+ kind: z.ZodLiteral<"step">;
663
+ stepId: z.ZodString;
664
+ }, z.core.$strip>, z.ZodObject<{
665
+ kind: z.ZodLiteral<"synthesis">;
666
+ }, z.core.$strip>, z.ZodObject<{
667
+ kind: z.ZodLiteral<"run">;
668
+ }, z.core.$strip>, z.ZodObject<{
669
+ kind: z.ZodLiteral<"profile">;
670
+ }, z.core.$strip>], "kind">;
603
671
  export declare const PausePartSchema: z.ZodObject<{
604
672
  type: z.ZodLiteral<"data-pause">;
605
673
  id: z.ZodString;
@@ -609,22 +677,63 @@ export declare const PausePartSchema: z.ZodObject<{
609
677
  run: "run";
610
678
  turn: "turn";
611
679
  }>;
680
+ waiter: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
681
+ kind: z.ZodLiteral<"turn">;
682
+ }, z.core.$strip>, z.ZodObject<{
683
+ kind: z.ZodLiteral<"sub-agent">;
684
+ agentId: z.ZodString;
685
+ }, z.core.$strip>, z.ZodObject<{
686
+ kind: z.ZodLiteral<"step">;
687
+ stepId: z.ZodString;
688
+ }, z.core.$strip>, z.ZodObject<{
689
+ kind: z.ZodLiteral<"synthesis">;
690
+ }, z.core.$strip>, z.ZodObject<{
691
+ kind: z.ZodLiteral<"run">;
692
+ }, z.core.$strip>, z.ZodObject<{
693
+ kind: z.ZodLiteral<"profile">;
694
+ }, z.core.$strip>], "kind">>;
612
695
  state: z.ZodEnum<{
613
696
  paused: "paused";
614
697
  retrying: "retrying";
615
698
  running: "running";
616
699
  }>;
617
- cause: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
700
+ cause: z.ZodDiscriminatedUnion<[z.ZodObject<{
618
701
  kind: z.ZodLiteral<"outage">;
619
702
  }, z.core.$strip>, z.ZodObject<{
620
703
  kind: z.ZodLiteral<"credential">;
621
704
  }, z.core.$strip>, z.ZodObject<{
622
705
  kind: z.ZodLiteral<"host">;
623
706
  reason: z.ZodString;
624
- }, z.core.$strip>], "kind">>;
707
+ }, z.core.$strip>], "kind">;
708
+ pausedAt: z.ZodString;
625
709
  attempt: z.ZodOptional<z.ZodNumber>;
626
710
  retryAt: z.ZodOptional<z.ZodString>;
711
+ endedAt: z.ZodOptional<z.ZodString>;
712
+ end: z.ZodOptional<z.ZodEnum<{
713
+ abort: "abort";
714
+ resume: "resume";
715
+ retry: "retry";
716
+ }>>;
627
717
  }, z.core.$strip>;
718
+ /** Mirrors `ModelRefusalPart` without its `type`: the stored payload of the part. */
719
+ export declare const ModelRefusalPayloadSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
720
+ id: z.ZodString;
721
+ outcome: z.ZodLiteral<"fallback">;
722
+ model: z.ZodString;
723
+ }, z.core.$strip>, z.ZodObject<{
724
+ id: z.ZodString;
725
+ outcome: z.ZodLiteral<"stopped">;
726
+ }, z.core.$strip>], "outcome">;
727
+ export declare const ModelRefusalPartSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
728
+ id: z.ZodString;
729
+ outcome: z.ZodLiteral<"fallback">;
730
+ model: z.ZodString;
731
+ type: z.ZodLiteral<"data-model-refusal">;
732
+ }, z.core.$strip>, z.ZodObject<{
733
+ id: z.ZodString;
734
+ outcome: z.ZodLiteral<"stopped">;
735
+ type: z.ZodLiteral<"data-model-refusal">;
736
+ }, z.core.$strip>], "outcome">;
628
737
  export declare const ModelRetryPartSchema: z.ZodObject<{
629
738
  type: z.ZodLiteral<"data-model-retry">;
630
739
  id: z.ZodString;
@@ -701,6 +810,58 @@ export declare const ChatPartSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
701
810
  accession: z.ZodString;
702
811
  version: z.ZodNumber;
703
812
  }, z.core.$strip>], "kind">;
813
+ }, z.core.$strip>, z.ZodObject<{
814
+ type: z.ZodLiteral<"data-analogy-report">;
815
+ id: z.ZodString;
816
+ report: z.ZodUnion<readonly [z.ZodObject<{
817
+ problemSummary: z.ZodString;
818
+ problemObjects: z.ZodArray<z.ZodObject<{
819
+ name: z.ZodString;
820
+ role: z.ZodString;
821
+ }, z.core.$strip>>;
822
+ problemRelations: z.ZodArray<z.ZodString>;
823
+ keyTerms: z.ZodArray<z.ZodString>;
824
+ schemaVersion: z.ZodLiteral<"1">;
825
+ analogies: z.ZodArray<z.ZodObject<{
826
+ targetDomain: z.ZodString;
827
+ analogyTitle: z.ZodString;
828
+ objectMappings: z.ZodArray<z.ZodObject<{
829
+ source: z.ZodString;
830
+ target: z.ZodString;
831
+ rationale: z.ZodString;
832
+ }, z.core.$strip>>;
833
+ sharedRelations: z.ZodString;
834
+ coverage: z.ZodEnum<{
835
+ available: "available";
836
+ not_loaded: "not_loaded";
837
+ queried_no_data: "queried_no_data";
838
+ search_failed: "search_failed";
839
+ }>;
840
+ solutions: z.ZodArray<z.ZodObject<{
841
+ title: z.ZodString;
842
+ sourceDomain: z.ZodString;
843
+ description: z.ZodString;
844
+ keyConcepts: z.ZodArray<z.ZodString>;
845
+ relevance: z.ZodString;
846
+ sources: z.ZodArray<z.ZodObject<{
847
+ url: z.ZodURL;
848
+ title: z.ZodString;
849
+ }, z.core.$strip>>;
850
+ githubRepos: z.ZodArray<z.ZodObject<{
851
+ url: z.ZodURL;
852
+ description: z.ZodOptional<z.ZodString>;
853
+ }, z.core.$strip>>;
854
+ }, z.core.$strip>>;
855
+ }, z.core.$strip>>;
856
+ }, z.core.$strip>, z.ZodObject<{
857
+ schemaVersion: z.ZodLiteral<"1">;
858
+ error: z.ZodObject<{
859
+ kind: z.ZodEnum<{
860
+ "extraction-failed": "extraction-failed";
861
+ }>;
862
+ message: z.ZodString;
863
+ }, z.core.$strip>;
864
+ }, z.core.$strip>]>;
704
865
  }, z.core.$strip>, z.ZodObject<{
705
866
  type: z.ZodLiteral<"data-plan">;
706
867
  id: z.ZodString;
@@ -1068,21 +1229,43 @@ export declare const ChatPartSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
1068
1229
  run: "run";
1069
1230
  turn: "turn";
1070
1231
  }>;
1232
+ waiter: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
1233
+ kind: z.ZodLiteral<"turn">;
1234
+ }, z.core.$strip>, z.ZodObject<{
1235
+ kind: z.ZodLiteral<"sub-agent">;
1236
+ agentId: z.ZodString;
1237
+ }, z.core.$strip>, z.ZodObject<{
1238
+ kind: z.ZodLiteral<"step">;
1239
+ stepId: z.ZodString;
1240
+ }, z.core.$strip>, z.ZodObject<{
1241
+ kind: z.ZodLiteral<"synthesis">;
1242
+ }, z.core.$strip>, z.ZodObject<{
1243
+ kind: z.ZodLiteral<"run">;
1244
+ }, z.core.$strip>, z.ZodObject<{
1245
+ kind: z.ZodLiteral<"profile">;
1246
+ }, z.core.$strip>], "kind">>;
1071
1247
  state: z.ZodEnum<{
1072
1248
  paused: "paused";
1073
1249
  retrying: "retrying";
1074
1250
  running: "running";
1075
1251
  }>;
1076
- cause: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
1252
+ cause: z.ZodDiscriminatedUnion<[z.ZodObject<{
1077
1253
  kind: z.ZodLiteral<"outage">;
1078
1254
  }, z.core.$strip>, z.ZodObject<{
1079
1255
  kind: z.ZodLiteral<"credential">;
1080
1256
  }, z.core.$strip>, z.ZodObject<{
1081
1257
  kind: z.ZodLiteral<"host">;
1082
1258
  reason: z.ZodString;
1083
- }, z.core.$strip>], "kind">>;
1259
+ }, z.core.$strip>], "kind">;
1260
+ pausedAt: z.ZodString;
1084
1261
  attempt: z.ZodOptional<z.ZodNumber>;
1085
1262
  retryAt: z.ZodOptional<z.ZodString>;
1263
+ endedAt: z.ZodOptional<z.ZodString>;
1264
+ end: z.ZodOptional<z.ZodEnum<{
1265
+ abort: "abort";
1266
+ resume: "resume";
1267
+ retry: "retry";
1268
+ }>>;
1086
1269
  }, z.core.$strip>, z.ZodObject<{
1087
1270
  type: z.ZodLiteral<"data-model-retry">;
1088
1271
  id: z.ZodString;
@@ -1090,4 +1273,13 @@ export declare const ChatPartSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
1090
1273
  maxRetries: z.ZodNumber;
1091
1274
  retryAt: z.ZodString;
1092
1275
  status: z.ZodOptional<z.ZodNumber>;
1093
- }, z.core.$strip>], "type">;
1276
+ }, z.core.$strip>, z.ZodDiscriminatedUnion<[z.ZodObject<{
1277
+ id: z.ZodString;
1278
+ outcome: z.ZodLiteral<"fallback">;
1279
+ model: z.ZodString;
1280
+ type: z.ZodLiteral<"data-model-refusal">;
1281
+ }, z.core.$strip>, z.ZodObject<{
1282
+ id: z.ZodString;
1283
+ outcome: z.ZodLiteral<"stopped">;
1284
+ type: z.ZodLiteral<"data-model-refusal">;
1285
+ }, z.core.$strip>], "outcome">], "type">;
@@ -2,6 +2,7 @@
2
2
  * Zod schemas for chat data parts — validation at boundaries.
3
3
  */
4
4
  import { z } from "zod";
5
+ import { AnalogicalReasonerOutputSchema } from "../analogy-report.js";
5
6
  import { ToolCallOutcomeSchema } from "./chat-events.js";
6
7
  import { TokenUsageRollupSchema } from "./usage.js";
7
8
  // ── Presentation ────────────────────────────────────────────────────
@@ -77,6 +78,12 @@ export const PresentationPartSchema = z.object({
77
78
  title: z.string().optional(),
78
79
  content: PresentationContentSchema,
79
80
  });
81
+ // ── Analogy report ──────────────────────────────────────────────────
82
+ export const AnalogyReportPartSchema = z.object({
83
+ type: z.literal("data-analogy-report"),
84
+ id: z.string(),
85
+ report: AnalogicalReasonerOutputSchema,
86
+ });
80
87
  // ── Plan reference ──────────────────────────────────────────────────
81
88
  // oxlint-disable-next-line eslint-js/no-restricted-syntax -- the issued plan-id shape: the pln- prefix plus 8 hex digits.
82
89
  export const PLAN_ID_RE = /^pln-[a-f0-9]{8}$/;
@@ -365,16 +372,37 @@ export const PauseCauseSchema = z.discriminatedUnion("kind", [
365
372
  z.object({ kind: z.literal("credential") }),
366
373
  z.object({ kind: z.literal("host"), reason: z.string() }),
367
374
  ]);
375
+ export const PauseWaiterSchema = z.discriminatedUnion("kind", [
376
+ z.object({ kind: z.literal("turn") }),
377
+ z.object({ kind: z.literal("sub-agent"), agentId: z.string() }),
378
+ z.object({ kind: z.literal("step"), stepId: z.string() }),
379
+ z.object({ kind: z.literal("synthesis") }),
380
+ z.object({ kind: z.literal("run") }),
381
+ z.object({ kind: z.literal("profile") }),
382
+ ]);
368
383
  export const PausePartSchema = z.object({
369
384
  type: z.literal("data-pause"),
370
385
  id: z.string(),
371
386
  workId: z.string(),
372
387
  workKind: z.enum(["turn", "run", "profile"]),
388
+ waiter: PauseWaiterSchema.optional(),
373
389
  state: z.enum(["retrying", "paused", "running"]),
374
- cause: PauseCauseSchema.optional(),
390
+ cause: PauseCauseSchema,
391
+ pausedAt: z.string(),
375
392
  attempt: z.number().int().nonnegative().optional(),
376
393
  retryAt: z.string().optional(),
377
- });
394
+ endedAt: z.string().optional(),
395
+ end: z.enum(["retry", "resume", "abort"]).optional(),
396
+ });
397
+ // ── Model refusal ───────────────────────────────────────────────────
398
+ const ModelRefusalFallbackShape = { id: z.string(), outcome: z.literal("fallback"), model: z.string() };
399
+ const ModelRefusalStoppedShape = { id: z.string(), outcome: z.literal("stopped") };
400
+ /** Mirrors `ModelRefusalPart` without its `type`: the stored payload of the part. */
401
+ export const ModelRefusalPayloadSchema = z.discriminatedUnion("outcome", [z.object(ModelRefusalFallbackShape), z.object(ModelRefusalStoppedShape)]);
402
+ export const ModelRefusalPartSchema = z.discriminatedUnion("outcome", [
403
+ z.object({ type: z.literal("data-model-refusal"), ...ModelRefusalFallbackShape }),
404
+ z.object({ type: z.literal("data-model-refusal"), ...ModelRefusalStoppedShape }),
405
+ ]);
378
406
  // ── Model retry ─────────────────────────────────────────────────────
379
407
  export const ModelRetryPartSchema = z.object({
380
408
  type: z.literal("data-model-retry"),
@@ -404,6 +432,7 @@ export const SubAgentActivityPartSchema = z.object({
404
432
  // ── Union ───────────────────────────────────────────────────────────
405
433
  export const ChatPartSchema = z.discriminatedUnion("type", [
406
434
  PresentationPartSchema,
435
+ AnalogyReportPartSchema,
407
436
  PlanPartSchema,
408
437
  RunCardPartSchema,
409
438
  PlanApprovalPartSchema,
@@ -428,4 +457,5 @@ export const ChatPartSchema = z.discriminatedUnion("type", [
428
457
  CompactionPartSchema,
429
458
  PausePartSchema,
430
459
  ModelRetryPartSchema,
460
+ ModelRefusalPartSchema,
431
461
  ]);
package/dist/index.d.ts CHANGED
@@ -108,7 +108,7 @@ export type { SubAgentActivityTracker } from "./contracts/subagent-activity.js";
108
108
  export type { TextPart, ToolCallPart, MessagePart, ChatMessage } from "./contracts/message.js";
109
109
  export type { TurnTerminal, ApplyFrameResult, ChatPartCheck } from "./contracts/chat-frame.js";
110
110
  export type { TokenUsageRollup } from "./contracts/usage.js";
111
- export type { ChatPart, PresentationPart, PresentationContent, PlanPart, RunCardPart, PlanApprovalPart, FileReferencePart, FileReferenceEntry, RunStartedPart, DagStatePart, DagStepState, PlanGraphStep, StepStatus, StepActivityPart, ActivityEntryPart, SubAgentActivityEntry, SubAgentActivityPart, StepPhase, StepFileTreePart, FileTreeEntry, StepOutputPart, StepOutputFile, StepSummaryPart, StepUsagePart, StepBlockedPart, RunSynthesisPart, FindingReference, SynthesizedFinding, BiologicalTheme, SynthesisProgressPart, SynthesisPhase, RunCompletedPart, RunCompletedFinding, RunFailedPart, CompactionPart, PauseCause, WorkKind, PausePart, ModelRetryPart, AskPart, ChildSessionStartedPart, ReportRenderedPart, } from "./contracts/chat-parts.js";
111
+ export type { ChatPart, PresentationPart, PresentationContent, PlanPart, RunCardPart, PlanApprovalPart, FileReferencePart, FileReferenceEntry, RunStartedPart, DagStatePart, DagStepState, PlanGraphStep, StepStatus, StepActivityPart, ActivityEntryPart, SubAgentActivityEntry, SubAgentActivityPart, StepPhase, StepFileTreePart, FileTreeEntry, StepOutputPart, StepOutputFile, StepSummaryPart, StepUsagePart, StepBlockedPart, RunSynthesisPart, FindingReference, SynthesizedFinding, BiologicalTheme, SynthesisProgressPart, SynthesisPhase, RunCompletedPart, RunCompletedFinding, RunFailedPart, CompactionPart, PauseCause, PauseWaiter, PauseEnd, WorkKind, PausePart, ModelRetryPart, ModelRefusal, ModelRefusalPart, AskPart, ChildSessionStartedPart, ReportRenderedPart, AnalogyReportPart, } from "./contracts/chat-parts.js";
112
112
  export { PART_REGISTRY, isTransient, isReconciling, isSidebarPart } from "./contracts/part-registry.js";
113
113
  export type { ChatPartType, PartDescriptor, PartEmitter, PartConsumer } from "./contracts/part-registry.js";
114
114
  export { parseStructureUrl, alphafoldEntryUrl, alphafoldPredictionUrl } from "./contracts/structure-source.js";
@@ -12,6 +12,7 @@
12
12
  * `ich-<code>` with non-word chars sanitized.
13
13
  */
14
14
  import { readPdfText } from "../pdf-text.js";
15
+ import { slugify } from "../slug.js";
15
16
  import { normalizeWhitespace } from "./normalize-whitespace.js";
16
17
  /**
17
18
  * Curated catalog of ICH guidelines to index into the corpus.
@@ -157,19 +158,3 @@ export async function fetchAndExtractIchDoc(stub) {
157
158
  },
158
159
  };
159
160
  }
160
- function slugify(s) {
161
- let out = "";
162
- let inRun = false;
163
- for (const char of s.toLowerCase()) {
164
- const isSlugChar = (char >= "a" && char <= "z") || (char >= "0" && char <= "9");
165
- if (!isSlugChar) {
166
- inRun = true;
167
- continue;
168
- }
169
- if (inRun && out.length > 0)
170
- out += "-";
171
- inRun = false;
172
- out += char;
173
- }
174
- return out;
175
- }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The shared lowercase slug: each run of characters outside `[a-z0-9]` becomes one dash.
3
+ *
4
+ * The helper holds the loop alone; each caller states its own cap and its own empty-result fallback beside
5
+ * the call.
6
+ */
7
+ export type SlugifyOpts = {
8
+ /**
9
+ * The result never passes this length: the loop stops before the append that would cross it, and a
10
+ * final slice cuts a two-character append that straddled the cap. That slice keeps the dash, thus a
11
+ * capped result can end in a dash. A stop before an append cannot change the first characters of the
12
+ * result, because an append only extends the prefix.
13
+ */
14
+ maxLength?: number;
15
+ /** The result for an input with no slug character. Without a fallback the empty result stays empty. */
16
+ fallback?: string;
17
+ };
18
+ export declare function slugify(value: string, opts?: SlugifyOpts): string;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The shared lowercase slug: each run of characters outside `[a-z0-9]` becomes one dash.
3
+ *
4
+ * The helper holds the loop alone; each caller states its own cap and its own empty-result fallback beside
5
+ * the call.
6
+ */
7
+ export function slugify(value, opts = {}) {
8
+ const { maxLength, fallback } = opts;
9
+ let out = "";
10
+ let inRun = false;
11
+ for (const char of value.toLowerCase()) {
12
+ if (maxLength !== undefined && out.length >= maxLength)
13
+ break;
14
+ const isSlugChar = (char >= "a" && char <= "z") || (char >= "0" && char <= "9");
15
+ if (!isSlugChar) {
16
+ inRun = true;
17
+ continue;
18
+ }
19
+ if (inRun && out.length > 0)
20
+ out += "-";
21
+ inRun = false;
22
+ out += char;
23
+ }
24
+ if (maxLength !== undefined && out.length > maxLength)
25
+ out = out.slice(0, maxLength);
26
+ if (out === "" && fallback !== undefined)
27
+ return fallback;
28
+ return out;
29
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * A minimal single-font PDF with one text line per page, `latin1`-encoded. It carries just enough structure
3
+ * for the PDF text extractors of the suites to read one body line per page.
4
+ */
5
+ export declare function buildPdf(pageCount: number): Buffer;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * A minimal single-font PDF with one text line per page, `latin1`-encoded. It carries just enough structure
3
+ * for the PDF text extractors of the suites to read one body line per page.
4
+ */
5
+ export function buildPdf(pageCount) {
6
+ const objects = ["1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj"];
7
+ const kids = Array.from({ length: pageCount }, (_, i) => `${4 + i * 2} 0 R`).join(" ");
8
+ objects.push(`2 0 obj<</Type/Pages/Kids[${kids}]/Count ${pageCount}>>endobj`, `3 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>endobj`);
9
+ for (let i = 0; i < pageCount; i++) {
10
+ const content = `BT /F1 12 Tf 72 720 Td (Page ${i + 1} body text) Tj ET`;
11
+ objects.push(`${4 + i * 2} 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 612 792]/Contents ${5 + i * 2} 0 R/Resources<</Font<</F1 3 0 R>>>>>>endobj`, `${5 + i * 2} 0 obj<</Length ${content.length}>>stream\n${content}\nendstream endobj`);
12
+ }
13
+ return Buffer.from(`%PDF-1.4\n${objects.join("\n")}\ntrailer<</Size ${3 + pageCount * 2}/Root 1 0 R>>`, "latin1");
14
+ }
@@ -156,7 +156,7 @@ export function framesOfChunk(chunk, rootSource) {
156
156
  case "error":
157
157
  return [{ type: "error", message: chunk.errorText, source: rootSource }];
158
158
  case "abort":
159
- return [{ type: "finish", source: rootSource }];
159
+ return [{ type: "finish", source: rootSource, finishReason: "aborted" }];
160
160
  default: {
161
161
  const frame = harnessFrameOf(chunk);
162
162
  return frame === undefined ? [] : [frame];
@@ -526,6 +526,27 @@ async function runSeries(agent, opts, calls, log, target, spec) {
526
526
  const detail = computeDetail(tool, toolCall.input, log);
527
527
  return { detail: detail === undefined ? {} : { detail }, input: shownRoot ? viewInputValue(tool, toolCall.input, log) : undefined };
528
528
  };
529
+ /** The two events of a rejected tool call: the start names the call, and the finish states the outcome and the error text. */
530
+ const emitRejectedToolEvents = async (toolCall, shown, outcome, error) => {
531
+ await opts.emit({
532
+ type: "tool-started",
533
+ source,
534
+ toolUseId: toolCall.toolCallId,
535
+ name: toolCall.toolName,
536
+ ...shown.detail,
537
+ input: shown.input,
538
+ });
539
+ await opts.emit({
540
+ type: "tool-finished",
541
+ source,
542
+ toolUseId: toolCall.toolCallId,
543
+ name: toolCall.toolName,
544
+ outcome,
545
+ ...shown.detail,
546
+ input: shown.input,
547
+ error,
548
+ });
549
+ };
529
550
  const seriesSteps = [];
530
551
  let open = openStep();
531
552
  let failure;
@@ -561,24 +582,7 @@ async function runSeries(agent, opts, calls, log, target, spec) {
561
582
  if (refusal !== undefined) {
562
583
  // A refused call does not run, thus its two events come from here.
563
584
  const shown = callShownOf(toolCall);
564
- await opts.emit({
565
- type: "tool-started",
566
- source,
567
- toolUseId: toolCall.toolCallId,
568
- name: toolCall.toolName,
569
- ...shown.detail,
570
- input: shown.input,
571
- });
572
- await opts.emit({
573
- type: "tool-finished",
574
- source,
575
- toolUseId: toolCall.toolCallId,
576
- name: toolCall.toolName,
577
- outcome: "error",
578
- ...shown.detail,
579
- input: shown.input,
580
- error: shownRoot ? textToolValue(refusal) : undefined,
581
- });
585
+ await emitRejectedToolEvents(toolCall, shown, "error", shownRoot ? textToolValue(refusal) : undefined);
582
586
  return { type: "denied", reason: refusal };
583
587
  }
584
588
  let denial;
@@ -595,23 +599,7 @@ async function runSeries(agent, opts, calls, log, target, spec) {
595
599
  return undefined;
596
600
  deniedByTheUser.set(toolCall.toolCallId, denial);
597
601
  const shown = callShownOf(toolCall);
598
- await opts.emit({
599
- type: "tool-started",
600
- source,
601
- toolUseId: toolCall.toolCallId,
602
- name: toolCall.toolName,
603
- ...shown.detail,
604
- input: shown.input,
605
- });
606
- await opts.emit({
607
- type: "tool-finished",
608
- source,
609
- toolUseId: toolCall.toolCallId,
610
- name: toolCall.toolName,
611
- outcome: "denied",
612
- ...shown.detail,
613
- input: shown.input,
614
- });
602
+ await emitRejectedToolEvents(toolCall, shown, "denied", undefined);
615
603
  return { type: "denied", reason: denial };
616
604
  },
617
605
  stopWhen: () => stops(),
@@ -654,6 +642,9 @@ async function runSeries(agent, opts, calls, log, target, spec) {
654
642
  target.steps.push(loopStep);
655
643
  if (loopStep.calls.length > 0)
656
644
  log.debug("iteration", { iteration: seriesSteps.length - 1, tools: loopStep.calls.map((call) => call.toolName) });
645
+ const refusal = refusalOf(step, loopStep);
646
+ if (refusal !== undefined)
647
+ await opts.emit({ type: "data-model-refusal", source, data: refusal });
657
648
  await opts.emit({ type: "iteration", source, index: seriesSteps.length - 1, final: !continuesAfter(loopStep) || stops() });
658
649
  },
659
650
  });
@@ -749,6 +740,19 @@ function loopStepOf(step, deniedByTheUser) {
749
740
  });
750
741
  return { finishReason: step.finishReason, rawFinishReason: step.rawFinishReason, calls };
751
742
  }
743
+ /**
744
+ * The refusal of one step, when the provider declined its request. A reply that ends with the content filter
745
+ * stopped, also after a fallback hop, because a chain whose last model declined reports that hop too. A hop that
746
+ * names no model gives the served model of the response, as the usage record does.
747
+ */
748
+ function refusalOf(step, loopStep) {
749
+ if (loopStep.finishReason === "content-filter")
750
+ return { id: step.response.id, outcome: "stopped" };
751
+ const servedBy = fallbackModelOf(step.providerMetadata);
752
+ if (servedBy === undefined)
753
+ return undefined;
754
+ return { id: step.response.id, outcome: "fallback", model: servedBy === "" ? step.response.modelId : servedBy };
755
+ }
752
756
  /**
753
757
  * The messages of one step as the transcript records them. The request after
754
758
  * them sends them as they are recorded, thus the record is the prefix of the
@@ -18,7 +18,7 @@
18
18
  * runs the body of the sub-agent in its own process.
19
19
  */
20
20
  import type { RunSession } from "../auth/types.js";
21
- import type { WorkKind, PausePart } from "../contracts/chat-parts.js";
21
+ import type { PauseWaiter, WorkKind, PausePart } from "../contracts/chat-parts.js";
22
22
  import type { ToolContext } from "../tools/define-tool.js";
23
23
  import type { LoopOptions } from "./run-loop.js";
24
24
  /** The options of the loop of a sub-agent that the place where it runs decides. */
@@ -48,7 +48,7 @@ export type SubAgentWork = {
48
48
  readonly runSession?: RunSession;
49
49
  };
50
50
  /** Gives the pause of the loops of a child, which writes its part through `emit`. The assembly gives it. */
51
- export type SubAgentPauses = (work: SubAgentWork, emit: (part: PausePart) => Promise<void>) => LoopOptions["pause"];
51
+ export type SubAgentPauses = (work: SubAgentWork, waiter: PauseWaiter, emit: (part: PausePart) => Promise<void>) => LoopOptions["pause"];
52
52
  /**
53
53
  * Run a sub-agent in the process of its caller, with no workflow: its loops fold their usage into the total of the
54
54
  * caller. A tool factory that a test builds many times takes this registrar, because DBOS registers a name one time.
@@ -78,7 +78,7 @@ export function registerSubAgent(name, body, pauses) {
78
78
  const source = { agentId: name, callPath: [...caller.callPath, name] };
79
79
  const pause = pauses === undefined || work === undefined
80
80
  ? undefined
81
- : pauses(work, async (part) => {
81
+ : pauses(work, { kind: "sub-agent", agentId: name }, async (part) => {
82
82
  await writer.emit({ type: "data-pause", source, data: part });
83
83
  });
84
84
  // A recovered child replays its model calls, and the usage middleware adds each one again. Thus the total
@@ -0,0 +1,9 @@
1
+ import { type MetricData } from "@opentelemetry/sdk-metrics";
2
+ export type MetricReader = {
3
+ /** Flush the provider and return the flat list of exported metrics. */
4
+ collect(): Promise<MetricData[]>;
5
+ /** Shut the provider down and unbind the global meter provider. */
6
+ stop(): Promise<void>;
7
+ };
8
+ /** Bind a meter provider over an in-memory exporter for one test; `stop` undoes both bindings after it. */
9
+ export declare function metricReader(): MetricReader;
@@ -0,0 +1,33 @@
1
+ import { metrics } from "@opentelemetry/api";
2
+ import { AggregationTemporality, InMemoryMetricExporter, MeterProvider, PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
3
+ import { __resetMetricsForTest } from "../metrics.js";
4
+ /** Bind a meter provider over an in-memory exporter for one test; `stop` undoes both bindings after it. */
5
+ export function metricReader() {
6
+ const exporter = new InMemoryMetricExporter(AggregationTemporality.CUMULATIVE);
7
+ const provider = new MeterProvider({
8
+ readers: [
9
+ new PeriodicExportingMetricReader({
10
+ exporter,
11
+ // Long interval — every export in a suite is a manual flush.
12
+ exportIntervalMillis: 3_600_000,
13
+ }),
14
+ ],
15
+ });
16
+ metrics.setGlobalMeterProvider(provider);
17
+ // Drop memoized instruments so they rebind to this fresh provider.
18
+ __resetMetricsForTest();
19
+ return {
20
+ async collect() {
21
+ await provider.forceFlush();
22
+ return exporter
23
+ .getMetrics()
24
+ .flatMap((rm) => rm.scopeMetrics)
25
+ .flatMap((sm) => sm.metrics);
26
+ },
27
+ async stop() {
28
+ await provider.shutdown();
29
+ metrics.disable();
30
+ __resetMetricsForTest();
31
+ },
32
+ };
33
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * One ChatProvider whose stream answers one text delta and one finish that carries the given input-token
3
+ * total and no output tokens. The usage suites feed it to the loop and read the counters back.
4
+ */
5
+ import type { ChatProvider } from "../../providers/types.js";
6
+ export declare function unnamedModel(inputTokens: number): ChatProvider;