@memberjunction/testing-engine 6.2.0-edge.0 → 6.2.0-edge.2

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 (188) hide show
  1. package/README.md +4 -1
  2. package/dist/decision-eval/corpus.d.ts +94 -0
  3. package/dist/decision-eval/corpus.d.ts.map +1 -0
  4. package/dist/decision-eval/corpus.js +144 -0
  5. package/dist/decision-eval/corpus.js.map +1 -0
  6. package/dist/decision-eval/discovery-baseline.d.ts +62 -0
  7. package/dist/decision-eval/discovery-baseline.d.ts.map +1 -0
  8. package/dist/decision-eval/discovery-baseline.js +90 -0
  9. package/dist/decision-eval/discovery-baseline.js.map +1 -0
  10. package/dist/decision-eval/discovery-corpus.d.ts +75 -0
  11. package/dist/decision-eval/discovery-corpus.d.ts.map +1 -0
  12. package/dist/decision-eval/discovery-corpus.js +110 -0
  13. package/dist/decision-eval/discovery-corpus.js.map +1 -0
  14. package/dist/decision-eval/discovery-mapping.d.ts +18 -0
  15. package/dist/decision-eval/discovery-mapping.d.ts.map +1 -0
  16. package/dist/decision-eval/discovery-mapping.js +21 -0
  17. package/dist/decision-eval/discovery-mapping.js.map +1 -0
  18. package/dist/decision-eval/discovery-metrics.d.ts +339 -0
  19. package/dist/decision-eval/discovery-metrics.d.ts.map +1 -0
  20. package/dist/decision-eval/discovery-metrics.js +401 -0
  21. package/dist/decision-eval/discovery-metrics.js.map +1 -0
  22. package/dist/decision-eval/discovery-scorecard.d.ts +96 -0
  23. package/dist/decision-eval/discovery-scorecard.d.ts.map +1 -0
  24. package/dist/decision-eval/discovery-scorecard.js +351 -0
  25. package/dist/decision-eval/discovery-scorecard.js.map +1 -0
  26. package/dist/decision-eval/discovery-suite.d.ts +201 -0
  27. package/dist/decision-eval/discovery-suite.d.ts.map +1 -0
  28. package/dist/decision-eval/discovery-suite.js +180 -0
  29. package/dist/decision-eval/discovery-suite.js.map +1 -0
  30. package/dist/decision-eval/discovery-types.d.ts +896 -0
  31. package/dist/decision-eval/discovery-types.d.ts.map +1 -0
  32. package/dist/decision-eval/discovery-types.js +211 -0
  33. package/dist/decision-eval/discovery-types.js.map +1 -0
  34. package/dist/decision-eval/index.d.ts +18 -0
  35. package/dist/decision-eval/index.d.ts.map +1 -0
  36. package/dist/decision-eval/index.js +18 -0
  37. package/dist/decision-eval/index.js.map +1 -0
  38. package/dist/decision-eval/metrics.d.ts +330 -0
  39. package/dist/decision-eval/metrics.d.ts.map +1 -0
  40. package/dist/decision-eval/metrics.js +574 -0
  41. package/dist/decision-eval/metrics.js.map +1 -0
  42. package/dist/decision-eval/output-paths.d.ts +119 -0
  43. package/dist/decision-eval/output-paths.d.ts.map +1 -0
  44. package/dist/decision-eval/output-paths.js +258 -0
  45. package/dist/decision-eval/output-paths.js.map +1 -0
  46. package/dist/decision-eval/point-mapping.d.ts +50 -0
  47. package/dist/decision-eval/point-mapping.d.ts.map +1 -0
  48. package/dist/decision-eval/point-mapping.js +83 -0
  49. package/dist/decision-eval/point-mapping.js.map +1 -0
  50. package/dist/decision-eval/scorecard.d.ts +190 -0
  51. package/dist/decision-eval/scorecard.d.ts.map +1 -0
  52. package/dist/decision-eval/scorecard.js +384 -0
  53. package/dist/decision-eval/scorecard.js.map +1 -0
  54. package/dist/decision-eval/suite.d.ts +244 -0
  55. package/dist/decision-eval/suite.d.ts.map +1 -0
  56. package/dist/decision-eval/suite.js +238 -0
  57. package/dist/decision-eval/suite.js.map +1 -0
  58. package/dist/decision-eval/types.d.ts +993 -0
  59. package/dist/decision-eval/types.d.ts.map +1 -0
  60. package/dist/decision-eval/types.js +227 -0
  61. package/dist/decision-eval/types.js.map +1 -0
  62. package/dist/drivers/AgentEvalDriver.d.ts +30 -1
  63. package/dist/drivers/AgentEvalDriver.d.ts.map +1 -1
  64. package/dist/drivers/AgentEvalDriver.js +145 -16
  65. package/dist/drivers/AgentEvalDriver.js.map +1 -1
  66. package/dist/drivers/DecisionEvalDriver.d.ts +297 -0
  67. package/dist/drivers/DecisionEvalDriver.d.ts.map +1 -0
  68. package/dist/drivers/DecisionEvalDriver.js +716 -0
  69. package/dist/drivers/DecisionEvalDriver.js.map +1 -0
  70. package/dist/drivers/PinnedDecisionRunner.d.ts +24 -0
  71. package/dist/drivers/PinnedDecisionRunner.d.ts.map +1 -0
  72. package/dist/drivers/PinnedDecisionRunner.js +24 -0
  73. package/dist/drivers/PinnedDecisionRunner.js.map +1 -0
  74. package/dist/drivers/PromptEvalDriver.d.ts.map +1 -1
  75. package/dist/drivers/PromptEvalDriver.js +11 -4
  76. package/dist/drivers/PromptEvalDriver.js.map +1 -1
  77. package/dist/drivers/RubricCalibrationTestDriver.d.ts +34 -0
  78. package/dist/drivers/RubricCalibrationTestDriver.d.ts.map +1 -0
  79. package/dist/drivers/RubricCalibrationTestDriver.js +193 -0
  80. package/dist/drivers/RubricCalibrationTestDriver.js.map +1 -0
  81. package/dist/drivers/calibration.d.ts +85 -0
  82. package/dist/drivers/calibration.d.ts.map +1 -0
  83. package/dist/drivers/calibration.js +191 -0
  84. package/dist/drivers/calibration.js.map +1 -0
  85. package/dist/drivers/index.d.ts +2 -0
  86. package/dist/drivers/index.d.ts.map +1 -1
  87. package/dist/drivers/index.js +2 -0
  88. package/dist/drivers/index.js.map +1 -1
  89. package/dist/engine/TestEngine.d.ts.map +1 -1
  90. package/dist/engine/TestEngine.js +21 -11
  91. package/dist/engine/TestEngine.js.map +1 -1
  92. package/dist/eval/corpus.d.ts +8 -0
  93. package/dist/eval/corpus.d.ts.map +1 -1
  94. package/dist/eval/corpus.js +26 -10
  95. package/dist/eval/corpus.js.map +1 -1
  96. package/dist/eval/decision.d.ts +8 -0
  97. package/dist/eval/decision.d.ts.map +1 -1
  98. package/dist/eval/decision.js +23 -7
  99. package/dist/eval/decision.js.map +1 -1
  100. package/dist/eval/expectation.d.ts +2 -0
  101. package/dist/eval/expectation.d.ts.map +1 -1
  102. package/dist/eval/expectation.js +9 -5
  103. package/dist/eval/expectation.js.map +1 -1
  104. package/dist/eval/history.d.ts +2 -0
  105. package/dist/eval/history.d.ts.map +1 -1
  106. package/dist/eval/history.js +5 -1
  107. package/dist/eval/history.js.map +1 -1
  108. package/dist/eval/matchers.d.ts +7 -0
  109. package/dist/eval/matchers.d.ts.map +1 -1
  110. package/dist/eval/matchers.js +11 -3
  111. package/dist/eval/matchers.js.map +1 -1
  112. package/dist/eval/subAgentTrace.d.ts +8 -6
  113. package/dist/eval/subAgentTrace.d.ts.map +1 -1
  114. package/dist/eval/subAgentTrace.js +15 -11
  115. package/dist/eval/subAgentTrace.js.map +1 -1
  116. package/dist/eval/wellFormed.d.ts +2 -0
  117. package/dist/eval/wellFormed.d.ts.map +1 -1
  118. package/dist/eval/wellFormed.js +5 -1
  119. package/dist/eval/wellFormed.js.map +1 -1
  120. package/dist/index.d.ts +12 -0
  121. package/dist/index.d.ts.map +1 -1
  122. package/dist/index.js +12 -0
  123. package/dist/index.js.map +1 -1
  124. package/dist/oracles/AgentDecisionOracle.js +7 -7
  125. package/dist/oracles/DecisionJudgeOracle.d.ts +97 -0
  126. package/dist/oracles/DecisionJudgeOracle.d.ts.map +1 -0
  127. package/dist/oracles/DecisionJudgeOracle.js +230 -0
  128. package/dist/oracles/DecisionJudgeOracle.js.map +1 -0
  129. package/dist/oracles/DecisionLabelMatchOracle.d.ts +38 -0
  130. package/dist/oracles/DecisionLabelMatchOracle.d.ts.map +1 -0
  131. package/dist/oracles/DecisionLabelMatchOracle.js +96 -0
  132. package/dist/oracles/DecisionLabelMatchOracle.js.map +1 -0
  133. package/dist/oracles/DiscoveryLabelMatchOracle.d.ts +39 -0
  134. package/dist/oracles/DiscoveryLabelMatchOracle.d.ts.map +1 -0
  135. package/dist/oracles/DiscoveryLabelMatchOracle.js +88 -0
  136. package/dist/oracles/DiscoveryLabelMatchOracle.js.map +1 -0
  137. package/dist/oracles/LLMJudgeOracle.d.ts +2 -47
  138. package/dist/oracles/LLMJudgeOracle.d.ts.map +1 -1
  139. package/dist/oracles/LLMJudgeOracle.js +80 -149
  140. package/dist/oracles/LLMJudgeOracle.js.map +1 -1
  141. package/dist/oracles/RubricOracle.d.ts +51 -0
  142. package/dist/oracles/RubricOracle.d.ts.map +1 -0
  143. package/dist/oracles/RubricOracle.js +66 -0
  144. package/dist/oracles/RubricOracle.js.map +1 -0
  145. package/dist/oracles/TraceSubAgentValidatorOracle.js +3 -3
  146. package/dist/oracles/index.d.ts +5 -0
  147. package/dist/oracles/index.d.ts.map +1 -1
  148. package/dist/oracles/index.js +5 -0
  149. package/dist/oracles/index.js.map +1 -1
  150. package/dist/oracles/inline-rubric.d.ts +25 -0
  151. package/dist/oracles/inline-rubric.d.ts.map +1 -0
  152. package/dist/oracles/inline-rubric.js +82 -0
  153. package/dist/oracles/inline-rubric.js.map +1 -0
  154. package/dist/oracles/judge-trace.d.ts +76 -0
  155. package/dist/oracles/judge-trace.d.ts.map +1 -0
  156. package/dist/oracles/judge-trace.js +125 -0
  157. package/dist/oracles/judge-trace.js.map +1 -0
  158. package/dist/oracles/promote-criteria.d.ts +57 -0
  159. package/dist/oracles/promote-criteria.d.ts.map +1 -0
  160. package/dist/oracles/promote-criteria.js +104 -0
  161. package/dist/oracles/promote-criteria.js.map +1 -0
  162. package/dist/oracles/rubric-resolution.d.ts +69 -0
  163. package/dist/oracles/rubric-resolution.d.ts.map +1 -0
  164. package/dist/oracles/rubric-resolution.js +110 -0
  165. package/dist/oracles/rubric-resolution.js.map +1 -0
  166. package/dist/types.d.ts +4 -0
  167. package/dist/types.d.ts.map +1 -1
  168. package/dist/utils/cost-calculator.d.ts +34 -0
  169. package/dist/utils/cost-calculator.d.ts.map +1 -1
  170. package/dist/utils/cost-calculator.js +40 -8
  171. package/dist/utils/cost-calculator.js.map +1 -1
  172. package/dist/utils/execution-context.d.ts +6 -0
  173. package/dist/utils/execution-context.d.ts.map +1 -1
  174. package/dist/utils/execution-context.js +15 -3
  175. package/dist/utils/execution-context.js.map +1 -1
  176. package/dist/utils/result-formatter.d.ts +38 -0
  177. package/dist/utils/result-formatter.d.ts.map +1 -1
  178. package/dist/utils/result-formatter.js +74 -24
  179. package/dist/utils/result-formatter.js.map +1 -1
  180. package/dist/utils/scoring.d.ts +20 -0
  181. package/dist/utils/scoring.d.ts.map +1 -1
  182. package/dist/utils/scoring.js +35 -7
  183. package/dist/utils/scoring.js.map +1 -1
  184. package/dist/utils/variable-resolver.d.ts +20 -1
  185. package/dist/utils/variable-resolver.d.ts.map +1 -1
  186. package/dist/utils/variable-resolver.js +58 -20
  187. package/dist/utils/variable-resolver.js.map +1 -1
  188. package/package.json +12 -10
@@ -0,0 +1,119 @@
1
+ /**
2
+ * @fileoverview Keeps generated Decision Eval files out of the repository.
3
+ *
4
+ * Everything the suite generator and the scorecard write is derived from the measurement corpus:
5
+ * the test records carry its points verbatim. The corpus must never be committed, so the rigs
6
+ * refuse outright to write inside a git working tree. The check resolves symlinks first, so a
7
+ * link from outside that points into the repository is refused too.
8
+ *
9
+ * Outside a repository nothing can recover a deleted file, so a rig that regenerates its output
10
+ * ({@link WriteGeneratedFiles}) writes only into a new or empty directory, or, when allowed to
11
+ * replace an earlier run, removes only the files that run listed in its manifest.
12
+ *
13
+ * @module @memberjunction/testing-engine
14
+ */
15
+ /** An output directory that resolves inside a git working tree. */
16
+ export declare class OutputInsideRepoError extends Error {
17
+ readonly OutputPath: string;
18
+ readonly RepoRoot: string;
19
+ /**
20
+ * @param OutputPath The output directory, resolved.
21
+ * @param RepoRoot The working tree it falls inside.
22
+ */
23
+ constructor(OutputPath: string, RepoRoot: string);
24
+ }
25
+ /**
26
+ * The nearest directory at or above `startPath` that holds a `.git` entry (a directory in a normal
27
+ * clone, a file in a worktree), or null when there is none. Symlinks are resolved first.
28
+ *
29
+ * @param startPath A file or directory; it need not exist yet.
30
+ */
31
+ export declare function FindRepoRoot(startPath: string): string | null;
32
+ /**
33
+ * True when `childPath` is `parentPath` or lies under it, after both are resolved.
34
+ *
35
+ * @param childPath The path to test.
36
+ * @param parentPath The directory it may lie under.
37
+ */
38
+ export declare function IsPathInside(childPath: string, parentPath: string): boolean;
39
+ /**
40
+ * Resolves an output directory and throws an {@link OutputInsideRepoError} when it is inside a git
41
+ * working tree: the one found by walking up from the directory itself, or any of `repoRoots`
42
+ * (the rig's own repository, for example). Returns the resolved directory.
43
+ *
44
+ * @param outputPath The output directory, as given; it need not exist yet.
45
+ * @param repoRoots Repositories the directory must also stay out of.
46
+ */
47
+ export declare function AssertOutputOutsideRepo(outputPath: string, repoRoots?: readonly string[]): string;
48
+ /**
49
+ * The absolute path with every symlink in its existing part resolved; the part that doesn't exist
50
+ * yet is appended unchanged.
51
+ *
52
+ * @param path Any path.
53
+ */
54
+ export declare function ResolveRealPath(path: string): string;
55
+ /** The manifest a generating rig leaves in its output directory: the files it wrote there. */
56
+ export declare const GENERATED_FILES_MANIFEST = "generated-files.json";
57
+ /** One file a rig generates: where it goes, relative to the output directory, and its content. */
58
+ export interface GeneratedFile {
59
+ /** A relative path inside the output directory, with no `..` segment. */
60
+ RelativePath: string;
61
+ Content: string;
62
+ }
63
+ /** How {@link WriteGeneratedFiles} treats a directory that already holds files. */
64
+ export interface WriteGeneratedFilesOptions {
65
+ /** Which rig writes. It is recorded in the manifest, and only a manifest it wrote is trusted. */
66
+ Generator: string;
67
+ /**
68
+ * Allow a directory that is not empty. The files the previous run's manifest lists are removed
69
+ * and replaced; everything else is kept, and a file that would be overwritten but is not in that
70
+ * manifest stops the run before anything changes.
71
+ */
72
+ Replace: boolean;
73
+ }
74
+ /** What {@link WriteGeneratedFiles} did. */
75
+ export interface WriteGeneratedFilesResult {
76
+ /** Files written, the manifest not counted. */
77
+ Written: number;
78
+ /** Files of the previous run that this run no longer writes, removed. */
79
+ Removed: number;
80
+ /** Files in the directory this rig did not write, left as they were, relative to it. */
81
+ Kept: string[];
82
+ }
83
+ /** A generated-output directory that holds files and may not be replaced. */
84
+ export declare class OutputNotEmptyError extends Error {
85
+ readonly OutputPath: string;
86
+ /**
87
+ * @param OutputPath The output directory, resolved.
88
+ */
89
+ constructor(OutputPath: string);
90
+ }
91
+ /** A generated file that would overwrite a file this rig did not write, or land outside the directory. */
92
+ export declare class OutputCollisionError extends Error {
93
+ readonly OutputPath: string;
94
+ readonly Collisions: readonly string[];
95
+ /**
96
+ * @param OutputPath The output directory, resolved.
97
+ * @param Collisions The files, relative to it.
98
+ */
99
+ constructor(OutputPath: string, Collisions: readonly string[]);
100
+ }
101
+ /**
102
+ * Writes a rig's generated files into `outDir` without ever removing or overwriting a file the
103
+ * rig did not write, and records them in {@link GENERATED_FILES_MANIFEST}.
104
+ *
105
+ * - A missing or empty directory is created and written into.
106
+ * - A directory that holds anything is refused ({@link OutputNotEmptyError}) unless
107
+ * `options.Replace` is set. Then the files the previous run's manifest lists (when the same
108
+ * generator wrote it) are removed, with any directory they leave empty, and the new files are
109
+ * written. Every other file is kept.
110
+ * - A new file whose path is taken by a file that manifest doesn't list, or whose directory
111
+ * resolves outside `outDir` through a symlink, stops the run ({@link OutputCollisionError})
112
+ * before anything is removed or written.
113
+ *
114
+ * @param outDir The output directory, resolved (see {@link AssertOutputOutsideRepo}).
115
+ * @param files The files to write.
116
+ * @param options Who writes, and whether an earlier run may be replaced.
117
+ */
118
+ export declare function WriteGeneratedFiles(outDir: string, files: readonly GeneratedFile[], options: WriteGeneratedFilesOptions): WriteGeneratedFilesResult;
119
+ //# sourceMappingURL=output-paths.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"output-paths.d.ts","sourceRoot":"","sources":["../../src/decision-eval/output-paths.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAMH,mEAAmE;AACnE,qBAAa,qBAAsB,SAAQ,KAAK;aAKhB,UAAU,EAAE,MAAM;aAAkB,QAAQ,EAAE,MAAM;IAJhF;;;OAGG;gBACyB,UAAU,EAAE,MAAM,EAAkB,QAAQ,EAAE,MAAM;CAMnF;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,SAAS,EAAE,MAAM,GA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EAAE,SAAS,aAAa,EAAE,EAC/B,OAAO,EAAE,0BAA0B,GACpC,yBAAyB,CAyB3B"}
@@ -0,0 +1,258 @@
1
+ /**
2
+ * @fileoverview Keeps generated Decision Eval files out of the repository.
3
+ *
4
+ * Everything the suite generator and the scorecard write is derived from the measurement corpus:
5
+ * the test records carry its points verbatim. The corpus must never be committed, so the rigs
6
+ * refuse outright to write inside a git working tree. The check resolves symlinks first, so a
7
+ * link from outside that points into the repository is refused too.
8
+ *
9
+ * Outside a repository nothing can recover a deleted file, so a rig that regenerates its output
10
+ * ({@link WriteGeneratedFiles}) writes only into a new or empty directory, or, when allowed to
11
+ * replace an earlier run, removes only the files that run listed in its manifest.
12
+ *
13
+ * @module @memberjunction/testing-engine
14
+ */
15
+ import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmdirSync, unlinkSync, writeFileSync } from 'node:fs';
16
+ import { basename, dirname, join, relative, resolve, isAbsolute, normalize, sep } from 'node:path';
17
+ import { z } from 'zod';
18
+ /** An output directory that resolves inside a git working tree. */
19
+ export class OutputInsideRepoError extends Error {
20
+ /**
21
+ * @param OutputPath The output directory, resolved.
22
+ * @param RepoRoot The working tree it falls inside.
23
+ */
24
+ constructor(OutputPath, RepoRoot) {
25
+ super(`Refusing to write to ${OutputPath}: it is inside the git repository at ${RepoRoot}. `
26
+ + 'Decision Eval output is derived from the corpus, which must never enter a repository. '
27
+ + 'Choose a directory outside any repository.');
28
+ this.OutputPath = OutputPath;
29
+ this.RepoRoot = RepoRoot;
30
+ this.name = 'OutputInsideRepoError';
31
+ }
32
+ }
33
+ /**
34
+ * The nearest directory at or above `startPath` that holds a `.git` entry (a directory in a normal
35
+ * clone, a file in a worktree), or null when there is none. Symlinks are resolved first.
36
+ *
37
+ * @param startPath A file or directory; it need not exist yet.
38
+ */
39
+ export function FindRepoRoot(startPath) {
40
+ let current = ResolveRealPath(startPath);
41
+ for (;;) {
42
+ if (existsSync(join(current, '.git'))) {
43
+ return current;
44
+ }
45
+ const parent = dirname(current);
46
+ if (parent === current) {
47
+ return null;
48
+ }
49
+ current = parent;
50
+ }
51
+ }
52
+ /**
53
+ * True when `childPath` is `parentPath` or lies under it, after both are resolved.
54
+ *
55
+ * @param childPath The path to test.
56
+ * @param parentPath The directory it may lie under.
57
+ */
58
+ export function IsPathInside(childPath, parentPath) {
59
+ const fromParent = relative(ResolveRealPath(parentPath), ResolveRealPath(childPath));
60
+ return fromParent === '' || (!fromParent.startsWith('..') && !isAbsolute(fromParent));
61
+ }
62
+ /**
63
+ * Resolves an output directory and throws an {@link OutputInsideRepoError} when it is inside a git
64
+ * working tree: the one found by walking up from the directory itself, or any of `repoRoots`
65
+ * (the rig's own repository, for example). Returns the resolved directory.
66
+ *
67
+ * @param outputPath The output directory, as given; it need not exist yet.
68
+ * @param repoRoots Repositories the directory must also stay out of.
69
+ */
70
+ export function AssertOutputOutsideRepo(outputPath, repoRoots = []) {
71
+ const resolved = ResolveRealPath(outputPath);
72
+ const enclosing = FindRepoRoot(resolved);
73
+ if (enclosing) {
74
+ throw new OutputInsideRepoError(resolved, enclosing);
75
+ }
76
+ const listed = repoRoots.find(root => IsPathInside(resolved, root));
77
+ if (listed) {
78
+ throw new OutputInsideRepoError(resolved, ResolveRealPath(listed));
79
+ }
80
+ return resolved;
81
+ }
82
+ /**
83
+ * The absolute path with every symlink in its existing part resolved; the part that doesn't exist
84
+ * yet is appended unchanged.
85
+ *
86
+ * @param path Any path.
87
+ */
88
+ export function ResolveRealPath(path) {
89
+ const absolute = resolve(path);
90
+ const missing = [];
91
+ let existing = absolute;
92
+ while (!existsSync(existing)) {
93
+ const parent = dirname(existing);
94
+ if (parent === existing) {
95
+ return absolute;
96
+ }
97
+ missing.unshift(basename(existing));
98
+ existing = parent;
99
+ }
100
+ return join(realpathSync(existing), ...missing);
101
+ }
102
+ /** The manifest a generating rig leaves in its output directory: the files it wrote there. */
103
+ export const GENERATED_FILES_MANIFEST = 'generated-files.json';
104
+ /** The shape of {@link GENERATED_FILES_MANIFEST}. */
105
+ const GeneratedFilesManifestSchema = z.object({
106
+ generator: z.string(),
107
+ files: z.array(z.string())
108
+ });
109
+ /** A generated-output directory that holds files and may not be replaced. */
110
+ export class OutputNotEmptyError extends Error {
111
+ /**
112
+ * @param OutputPath The output directory, resolved.
113
+ */
114
+ constructor(OutputPath) {
115
+ super(`Refusing to write to ${OutputPath}: it is not empty. Choose a new or empty directory, `
116
+ + 'or allow replacing an earlier run of this rig there (--force). Replacing removes only the files '
117
+ + `that run listed in its ${GENERATED_FILES_MANIFEST}; nothing else is removed or overwritten.`);
118
+ this.OutputPath = OutputPath;
119
+ this.name = 'OutputNotEmptyError';
120
+ }
121
+ }
122
+ /** A generated file that would overwrite a file this rig did not write, or land outside the directory. */
123
+ export class OutputCollisionError extends Error {
124
+ /**
125
+ * @param OutputPath The output directory, resolved.
126
+ * @param Collisions The files, relative to it.
127
+ */
128
+ constructor(OutputPath, Collisions) {
129
+ super(`Refusing to write to ${OutputPath}: ${Collisions.length} file(s) there were not written by this rig, `
130
+ + `or resolve outside it (${Collisions.slice(0, 5).join(', ')}${Collisions.length > 5 ? ', …' : ''}). `
131
+ + 'Nothing was changed. Choose a new or empty directory.');
132
+ this.OutputPath = OutputPath;
133
+ this.Collisions = Collisions;
134
+ this.name = 'OutputCollisionError';
135
+ }
136
+ }
137
+ /**
138
+ * Writes a rig's generated files into `outDir` without ever removing or overwriting a file the
139
+ * rig did not write, and records them in {@link GENERATED_FILES_MANIFEST}.
140
+ *
141
+ * - A missing or empty directory is created and written into.
142
+ * - A directory that holds anything is refused ({@link OutputNotEmptyError}) unless
143
+ * `options.Replace` is set. Then the files the previous run's manifest lists (when the same
144
+ * generator wrote it) are removed, with any directory they leave empty, and the new files are
145
+ * written. Every other file is kept.
146
+ * - A new file whose path is taken by a file that manifest doesn't list, or whose directory
147
+ * resolves outside `outDir` through a symlink, stops the run ({@link OutputCollisionError})
148
+ * before anything is removed or written.
149
+ *
150
+ * @param outDir The output directory, resolved (see {@link AssertOutputOutsideRepo}).
151
+ * @param files The files to write.
152
+ * @param options Who writes, and whether an earlier run may be replaced.
153
+ */
154
+ export function WriteGeneratedFiles(outDir, files, options) {
155
+ const planned = files.map(f => checkedRelativePath(f.RelativePath));
156
+ if (planned.includes(GENERATED_FILES_MANIFEST)) {
157
+ throw new Error(`A generated file may not be named ${GENERATED_FILES_MANIFEST}: the manifest is written separately`);
158
+ }
159
+ const existing = new Set(existsSync(outDir) ? listFiles(outDir) : []);
160
+ if (existing.size > 0 && !options.Replace) {
161
+ throw new OutputNotEmptyError(outDir);
162
+ }
163
+ const manifest = readManifest(outDir);
164
+ const previous = new Set(manifest?.generator === options.Generator ? [...manifest.files, GENERATED_FILES_MANIFEST] : []);
165
+ const refused = [...planned, GENERATED_FILES_MANIFEST]
166
+ .filter(path => (existing.has(path) && !previous.has(path)) || !IsPathInside(dirname(join(outDir, path)), outDir));
167
+ if (refused.length > 0) {
168
+ throw new OutputCollisionError(outDir, refused);
169
+ }
170
+ // Every file of the previous run goes first, the ones about to be rewritten too, so a write
171
+ // never follows a symlink someone left in place of a generated file.
172
+ const removed = [...previous].filter(path => removeGeneratedFile(outDir, path))
173
+ .filter(path => !planned.includes(path) && path !== GENERATED_FILES_MANIFEST).length;
174
+ files.forEach((file, i) => writeInside(outDir, planned[i], file.Content));
175
+ const record = { generator: options.Generator, files: [...planned].sort() };
176
+ writeInside(outDir, GENERATED_FILES_MANIFEST, `${JSON.stringify(record, null, 2)}\n`);
177
+ const written = new Set([...planned, GENERATED_FILES_MANIFEST]);
178
+ return { Written: planned.length, Removed: removed, Kept: listFiles(outDir).filter(path => !written.has(path)) };
179
+ }
180
+ /** A relative path, normalized with `/` separators, or an error when it is absolute or climbs out with `..`. */
181
+ function checkedRelativePath(path) {
182
+ const normalized = normalize(path).split(sep).join('/');
183
+ if (!normalized || normalized === '.' || isAbsolute(path) || normalized.split('/').includes('..')) {
184
+ throw new Error(`A generated file's path must be relative and stay inside the output directory, got '${path}'`);
185
+ }
186
+ return normalized;
187
+ }
188
+ /** Every file (and symlink) under a directory, relative to it with `/` separators. Symlinks are not followed. */
189
+ function listFiles(dir, prefix = '') {
190
+ return readdirSync(join(dir, prefix), { withFileTypes: true }).flatMap(entry => {
191
+ const path = prefix ? `${prefix}/${entry.name}` : entry.name;
192
+ return entry.isDirectory() ? listFiles(dir, path) : [path];
193
+ });
194
+ }
195
+ /**
196
+ * The manifest in `outDir`, with every listed path checked to be a safe relative path (a tampered
197
+ * entry is dropped, never followed), or null when there is none or it can't be read.
198
+ */
199
+ function readManifest(outDir) {
200
+ const path = join(outDir, GENERATED_FILES_MANIFEST);
201
+ if (!existsSync(path)) {
202
+ return null;
203
+ }
204
+ let parsed;
205
+ try {
206
+ parsed = GeneratedFilesManifestSchema.safeParse(JSON.parse(readFileSync(path, 'utf8')));
207
+ }
208
+ catch {
209
+ return null;
210
+ }
211
+ if (!parsed.success) {
212
+ return null;
213
+ }
214
+ const files = parsed.data.files.flatMap(file => {
215
+ try {
216
+ return [checkedRelativePath(file)];
217
+ }
218
+ catch {
219
+ return [];
220
+ }
221
+ });
222
+ return { generator: parsed.data.generator, files: files.filter(file => file !== GENERATED_FILES_MANIFEST) };
223
+ }
224
+ /**
225
+ * Removes one previously generated file when it is a file or symlink whose directory resolves
226
+ * inside `outDir`, then any directory it leaves empty, up to `outDir`. Returns whether it did.
227
+ */
228
+ function removeGeneratedFile(outDir, path) {
229
+ const full = join(outDir, path);
230
+ const stat = lstatIfPresent(full);
231
+ if (!stat || stat.isDirectory() || !IsPathInside(dirname(full), outDir)) {
232
+ return false;
233
+ }
234
+ unlinkSync(full);
235
+ for (let dir = dirname(full); dir !== outDir && IsPathInside(dir, outDir); dir = dirname(dir)) {
236
+ if (readdirSync(dir).length > 0) {
237
+ break;
238
+ }
239
+ rmdirSync(dir);
240
+ }
241
+ return true;
242
+ }
243
+ /** The path's own status (a symlink is not followed), or null when nothing is there. */
244
+ function lstatIfPresent(path) {
245
+ try {
246
+ return lstatSync(path);
247
+ }
248
+ catch {
249
+ return null;
250
+ }
251
+ }
252
+ /** Writes one file under `outDir`, creating its directories. The caller has checked it stays inside. */
253
+ function writeInside(outDir, path, content) {
254
+ const full = join(outDir, path);
255
+ mkdirSync(dirname(full), { recursive: true });
256
+ writeFileSync(full, content);
257
+ }
258
+ //# sourceMappingURL=output-paths.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"output-paths.js","sourceRoot":"","sources":["../../src/decision-eval/output-paths.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC1I,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AACnG,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,mEAAmE;AACnE,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC5C;;;OAGG;IACH,YAA4B,UAAkB,EAAkB,QAAgB;QAC5E,KAAK,CAAC,wBAAwB,UAAU,wCAAwC,QAAQ,IAAI;cACtF,wFAAwF;cACxF,4CAA4C,CAAC,CAAC;QAH5B,eAAU,GAAV,UAAU,CAAQ;QAAkB,aAAQ,GAAR,QAAQ,CAAQ;QAI5E,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACxC,CAAC;CACJ;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,SAAiB;IAC1C,IAAI,OAAO,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IACzC,SAAS,CAAC;QACN,IAAI,UAAU,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC;YACpC,OAAO,OAAO,CAAC;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AAG,GAAG,OAAO,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,MAAM,IAAI,YAAY,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QAC5F,IAAI,WAAW,CAAC,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC9B,MAAM;QACV,CAAC;QACD,SAAS,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,wFAAwF;AACxF,SAAS,cAAc,CAAC,IAAY;IAChC,IAAI,CAAC;QACD,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC;IAAC,MAAM,CAAC;QACL,OAAO,IAAI,CAAC;IAChB,CAAC;AACL,CAAC;AAED,wGAAwG;AACxG,SAAS,WAAW,CAAC,MAAc,EAAE,IAAY,EAAE,OAAe;IAC9D,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAChC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAC9C,aAAa,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AACjC,CAAC"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * @fileoverview Maps a corpus point to the routing decision's input, through the same builders the
3
+ * chat uses (`@memberjunction/ai-core-plus`), so the eval asks exactly what production asks.
4
+ *
5
+ * @module @memberjunction/testing-engine
6
+ */
7
+ import { type RoutingAgent, type RoutingArtifactSummary, type RoutingCatalogAgent, type RoutingDecisionInput, type RoutingHistoryRow } from '@memberjunction/ai-core-plus';
8
+ import type { DecisionCorpusArtifact, DecisionCorpusPoint } from './types.js';
9
+ /** The agents the mapping can resolve: the engine's, in the driver. */
10
+ export interface DecisionEvalAgentCatalog {
11
+ /**
12
+ * Looks an agent up by ID, with its status: as in the chat, an agent that isn't active, or is
13
+ * restricted, is not offered. Undefined when the catalog doesn't have it; the mapping then falls
14
+ * back to the name (and, for the previous agent, the description) the point itself records.
15
+ */
16
+ FindAgent(agentId: string): RoutingCatalogAgent | undefined;
17
+ /** The conversation manager (Sage), offered as "someone else". Null when the catalog doesn't have it. */
18
+ ConversationManager: RoutingAgent | null;
19
+ }
20
+ /**
21
+ * The routing decision's input for one corpus point:
22
+ * - `Message` is the point's new message, and `ContinuityAgentId` its previous agent;
23
+ * - `Participants` are the history's agents, newest first, from the shared builder;
24
+ * - `RecentTurns` (and `RecentTurnParts`) come from the history through the shared builders;
25
+ * - `ArtifactVersions` (and `ContinuityArtifacts`) are the point's artifacts, as the previous agent's;
26
+ * - `AllowedAgentIDs` is null: the eval runs as a chat with no allowed list.
27
+ *
28
+ * Agents resolve through the catalog by ID, falling back to the point's own names. The history is
29
+ * the conversation before the new message; a history row that is the new message itself (the
30
+ * point's ID, or a trailing user row with the same text) is dropped, as the chat drops it.
31
+ *
32
+ * @param point The corpus point.
33
+ * @param catalog The agents to resolve against.
34
+ */
35
+ export declare function MapPointToRoutingInput(point: DecisionCorpusPoint, catalog: DecisionEvalAgentCatalog): RoutingDecisionInput;
36
+ /**
37
+ * The point's history as routing rows, oldest first, without the new message. The corpus should
38
+ * already exclude it; this drops a row carrying the point's own ID, and a trailing user row whose
39
+ * text is the new message, in case it doesn't.
40
+ *
41
+ * @param point The corpus point.
42
+ */
43
+ export declare function HistoryBeforeMessage(point: DecisionCorpusPoint): RoutingHistoryRow[];
44
+ /**
45
+ * A corpus artifact in the shape the routing builders read.
46
+ *
47
+ * @param artifact The corpus artifact.
48
+ */
49
+ export declare function ToRoutingArtifactSummary(artifact: DecisionCorpusArtifact): RoutingArtifactSummary;
50
+ //# sourceMappingURL=point-mapping.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"point-mapping.d.ts","sourceRoot":"","sources":["../../src/decision-eval/point-mapping.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAKH,KAAK,YAAY,EACjB,KAAK,sBAAsB,EAC3B,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACzB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,KAAK,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE3E,uEAAuE;AACvE,MAAM,WAAW,wBAAwB;IACrC;;;;OAIG;IACH,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,mBAAmB,GAAG,SAAS,CAAC;IAC5D,yGAAyG;IACzG,mBAAmB,EAAE,YAAY,GAAG,IAAI,CAAC;CAC5C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,mBAAmB,EAAE,OAAO,EAAE,wBAAwB,GAAG,oBAAoB,CAiB1H;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,mBAAmB,GAAG,iBAAiB,EAAE,CAOpF;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,sBAAsB,GAAG,sBAAsB,CAEjG"}
@@ -0,0 +1,83 @@
1
+ /**
2
+ * @fileoverview Maps a corpus point to the routing decision's input, through the same builders the
3
+ * chat uses (`@memberjunction/ai-core-plus`), so the eval asks exactly what production asks.
4
+ *
5
+ * @module @memberjunction/testing-engine
6
+ */
7
+ import { BuildRecentTurnParts, BuildRecentTurns, BuildRoutingArtifactVersions, CollectRoutingParticipants } from '@memberjunction/ai-core-plus';
8
+ import { UUIDsEqual } from '@memberjunction/global';
9
+ /**
10
+ * The routing decision's input for one corpus point:
11
+ * - `Message` is the point's new message, and `ContinuityAgentId` its previous agent;
12
+ * - `Participants` are the history's agents, newest first, from the shared builder;
13
+ * - `RecentTurns` (and `RecentTurnParts`) come from the history through the shared builders;
14
+ * - `ArtifactVersions` (and `ContinuityArtifacts`) are the point's artifacts, as the previous agent's;
15
+ * - `AllowedAgentIDs` is null: the eval runs as a chat with no allowed list.
16
+ *
17
+ * Agents resolve through the catalog by ID, falling back to the point's own names. The history is
18
+ * the conversation before the new message; a history row that is the new message itself (the
19
+ * point's ID, or a trailing user row with the same text) is dropped, as the chat drops it.
20
+ *
21
+ * @param point The corpus point.
22
+ * @param catalog The agents to resolve against.
23
+ */
24
+ export function MapPointToRoutingInput(point, catalog) {
25
+ const history = HistoryBeforeMessage(point);
26
+ const findAgent = (agentId) => catalog.FindAgent(agentId) ?? pointAgent(point, agentId);
27
+ const manager = catalog.ConversationManager;
28
+ const previousAgent = findAgent(point.previous_agent.id) ?? fromPreviousAgent(point);
29
+ const artifacts = point.artifacts.map(ToRoutingArtifactSummary);
30
+ return {
31
+ Message: point.latest_message,
32
+ ContinuityAgentId: point.previous_agent.id,
33
+ Participants: CollectRoutingParticipants(history, manager?.ID ?? null, null, findAgent),
34
+ ConversationManager: manager,
35
+ RecentTurns: BuildRecentTurns(history, findAgent),
36
+ RecentTurnParts: BuildRecentTurnParts(history, findAgent),
37
+ ArtifactVersions: BuildRoutingArtifactVersions([{ Agent: previousAgent, Artifacts: artifacts }]),
38
+ AllowedAgentIDs: null,
39
+ ContinuityArtifacts: artifacts
40
+ };
41
+ }
42
+ /**
43
+ * The point's history as routing rows, oldest first, without the new message. The corpus should
44
+ * already exclude it; this drops a row carrying the point's own ID, and a trailing user row whose
45
+ * text is the new message, in case it doesn't.
46
+ *
47
+ * @param point The corpus point.
48
+ */
49
+ export function HistoryBeforeMessage(point) {
50
+ const rows = point.history.filter(row => !UUIDsEqual(row.id, point.id));
51
+ const last = rows[rows.length - 1];
52
+ if (last && last.role === 'User' && last.message.trim() === point.latest_message.trim()) {
53
+ rows.pop();
54
+ }
55
+ return rows.map(row => ({ ID: row.id, Role: row.role, AgentID: row.agent_id, Message: row.message }));
56
+ }
57
+ /**
58
+ * A corpus artifact in the shape the routing builders read.
59
+ *
60
+ * @param artifact The corpus artifact.
61
+ */
62
+ export function ToRoutingArtifactSummary(artifact) {
63
+ return { artifactName: artifact.artifactName, ArtifactType: artifact.artifactType, Versions: artifact.versions };
64
+ }
65
+ /**
66
+ * The status an agent the catalog doesn't have is given: routable. The point records it answering in
67
+ * this conversation, and nothing says it has since been disabled. An agent the catalog has keeps the
68
+ * catalog's status.
69
+ */
70
+ const POINT_AGENT_STATUS = { Status: 'Active', IsRestricted: false };
71
+ /** An agent as the point records it: the previous agent in full, or a history row's name. */
72
+ function pointAgent(point, agentId) {
73
+ if (UUIDsEqual(agentId, point.previous_agent.id)) {
74
+ return { ...fromPreviousAgent(point), ...POINT_AGENT_STATUS };
75
+ }
76
+ const named = [...point.history].reverse().find(row => UUIDsEqual(row.agent_id, agentId) && !!row.agent_name?.trim());
77
+ return named ? { ID: agentId, Name: named.agent_name, Description: null, ...POINT_AGENT_STATUS } : undefined;
78
+ }
79
+ /** The previous agent as the point records it. */
80
+ function fromPreviousAgent(point) {
81
+ return { ID: point.previous_agent.id, Name: point.previous_agent.name, Description: point.previous_agent.description };
82
+ }
83
+ //# sourceMappingURL=point-mapping.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"point-mapping.js","sourceRoot":"","sources":["../../src/decision-eval/point-mapping.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACH,oBAAoB,EACpB,gBAAgB,EAChB,4BAA4B,EAC5B,0BAA0B,EAM7B,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAepD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAA0B,EAAE,OAAiC;IAChG,MAAM,OAAO,GAAG,oBAAoB,CAAC,KAAK,CAAC,CAAC;IAC5C,MAAM,SAAS,GAAG,CAAC,OAAe,EAAmC,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,UAAU,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACjI,MAAM,OAAO,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAC5C,MA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