@provartesting/provardx-cli 1.5.0 → 1.5.2-beta.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.
- package/lib/mcp/docs/PROVAR_TOOL_GUIDE.md +18 -6
- package/lib/mcp/prompts/guidePrompts.js +21 -11
- package/lib/mcp/prompts/guidePrompts.js.map +1 -1
- package/lib/mcp/rules/provar_best_practices_rules.json +29 -0
- package/lib/mcp/server.d.ts +1 -0
- package/lib/mcp/server.js +90 -21
- package/lib/mcp/server.js.map +1 -1
- package/lib/mcp/tools/antTools.d.ts +20 -0
- package/lib/mcp/tools/antTools.js +105 -41
- package/lib/mcp/tools/antTools.js.map +1 -1
- package/lib/mcp/tools/automationTools.js +65 -35
- package/lib/mcp/tools/automationTools.js.map +1 -1
- package/lib/mcp/tools/bestPracticesEngine.js +148 -9
- package/lib/mcp/tools/bestPracticesEngine.js.map +1 -1
- package/lib/mcp/tools/connectionTools.js +100 -30
- package/lib/mcp/tools/connectionTools.js.map +1 -1
- package/lib/mcp/tools/defectTools.js +12 -7
- package/lib/mcp/tools/defectTools.js.map +1 -1
- package/lib/mcp/tools/descHelper.d.ts +5 -0
- package/lib/mcp/tools/descHelper.js +14 -0
- package/lib/mcp/tools/descHelper.js.map +1 -0
- package/lib/mcp/tools/nitroXTools.js +72 -32
- package/lib/mcp/tools/nitroXTools.js.map +1 -1
- package/lib/mcp/tools/orgDescribeTools.d.ts +57 -0
- package/lib/mcp/tools/orgDescribeTools.js +329 -0
- package/lib/mcp/tools/orgDescribeTools.js.map +1 -0
- package/lib/mcp/tools/pageObjectGenerate.js +33 -15
- package/lib/mcp/tools/pageObjectGenerate.js.map +1 -1
- package/lib/mcp/tools/pageObjectValidate.js +11 -4
- package/lib/mcp/tools/pageObjectValidate.js.map +1 -1
- package/lib/mcp/tools/projectInspect.js +31 -5
- package/lib/mcp/tools/projectInspect.js.map +1 -1
- package/lib/mcp/tools/projectValidateFromPath.js +128 -22
- package/lib/mcp/tools/projectValidateFromPath.js.map +1 -1
- package/lib/mcp/tools/propertiesTools.d.ts +12 -0
- package/lib/mcp/tools/propertiesTools.js +155 -17
- package/lib/mcp/tools/propertiesTools.js.map +1 -1
- package/lib/mcp/tools/qualityHubApiTools.js +8 -7
- package/lib/mcp/tools/qualityHubApiTools.js.map +1 -1
- package/lib/mcp/tools/qualityHubTools.js +108 -37
- package/lib/mcp/tools/qualityHubTools.js.map +1 -1
- package/lib/mcp/tools/rcaTools.d.ts +17 -0
- package/lib/mcp/tools/rcaTools.js +69 -15
- package/lib/mcp/tools/rcaTools.js.map +1 -1
- package/lib/mcp/tools/testCaseGenerate.d.ts +1 -0
- package/lib/mcp/tools/testCaseGenerate.js +254 -17
- package/lib/mcp/tools/testCaseGenerate.js.map +1 -1
- package/lib/mcp/tools/testCaseStepTools.js +27 -9
- package/lib/mcp/tools/testCaseStepTools.js.map +1 -1
- package/lib/mcp/tools/testCaseValidate.d.ts +14 -1
- package/lib/mcp/tools/testCaseValidate.js +190 -69
- package/lib/mcp/tools/testCaseValidate.js.map +1 -1
- package/lib/mcp/tools/testPlanTools.js +43 -26
- package/lib/mcp/tools/testPlanTools.js.map +1 -1
- package/lib/mcp/tools/testPlanValidate.js +64 -11
- package/lib/mcp/tools/testPlanValidate.js.map +1 -1
- package/lib/mcp/tools/testSuiteValidate.js +99 -10
- package/lib/mcp/tools/testSuiteValidate.js.map +1 -1
- package/lib/mcp/utils/detailLevel.d.ts +9 -0
- package/lib/mcp/utils/detailLevel.js +20 -0
- package/lib/mcp/utils/detailLevel.js.map +1 -0
- package/lib/mcp/utils/fieldMask.d.ts +17 -0
- package/lib/mcp/utils/fieldMask.js +75 -0
- package/lib/mcp/utils/fieldMask.js.map +1 -0
- package/lib/mcp/utils/testCasePlanMode.d.ts +41 -0
- package/lib/mcp/utils/testCasePlanMode.js +208 -0
- package/lib/mcp/utils/testCasePlanMode.js.map +1 -0
- package/lib/mcp/utils/tokenMeta.d.ts +40 -0
- package/lib/mcp/utils/tokenMeta.js +90 -0
- package/lib/mcp/utils/tokenMeta.js.map +1 -0
- package/lib/mcp/utils/validationDiff.d.ts +57 -0
- package/lib/mcp/utils/validationDiff.js +191 -0
- package/lib/mcp/utils/validationDiff.js.map +1 -0
- package/lib/mcp/utils/validationScore.d.ts +15 -0
- package/lib/mcp/utils/validationScore.js +31 -0
- package/lib/mcp/utils/validationScore.js.map +1 -0
- package/lib/mcp/utils/warningCodes.d.ts +10 -0
- package/lib/mcp/utils/warningCodes.js +19 -0
- package/lib/mcp/utils/warningCodes.js.map +1 -0
- package/oclif.manifest.json +1 -1
- package/package.json +13 -1
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Provar Limited.
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
* Licensed under the BSD 3-Clause license.
|
|
5
|
+
* For full license text, see LICENSE.md file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Mask an object (or array of objects) to retain only the specified fields.
|
|
9
|
+
*
|
|
10
|
+
* - Top-level keys: `"name"` keeps only the `name` property
|
|
11
|
+
* - Dot notation: `"steps.action"` keeps the `steps` array but only `action` within each element
|
|
12
|
+
* - Unknown field names are silently ignored — never an error
|
|
13
|
+
* - Arrays: masking is applied to every element
|
|
14
|
+
*
|
|
15
|
+
* @param obj Source object or array (typed as unknown; cast internally, never through any)
|
|
16
|
+
* @param fields Parsed field list — each entry is a dot-path string
|
|
17
|
+
*/
|
|
18
|
+
export function maskFields(obj, fields) {
|
|
19
|
+
if (Array.isArray(obj)) {
|
|
20
|
+
return obj.map((item) => maskFields(item, fields));
|
|
21
|
+
}
|
|
22
|
+
if (obj === null || typeof obj !== 'object') {
|
|
23
|
+
return obj;
|
|
24
|
+
}
|
|
25
|
+
const source = obj;
|
|
26
|
+
// Group fields: topLevelKeys contains every key to retain.
|
|
27
|
+
// dotFields[key] holds the sub-paths to drill into for that key.
|
|
28
|
+
const topLevelKeys = new Set();
|
|
29
|
+
const dotFields = {};
|
|
30
|
+
for (const field of fields) {
|
|
31
|
+
const dotIdx = field.indexOf('.');
|
|
32
|
+
if (dotIdx === -1) {
|
|
33
|
+
topLevelKeys.add(field);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
const top = field.slice(0, dotIdx);
|
|
37
|
+
const rest = field.slice(dotIdx + 1);
|
|
38
|
+
topLevelKeys.add(top);
|
|
39
|
+
if (!dotFields[top])
|
|
40
|
+
dotFields[top] = [];
|
|
41
|
+
dotFields[top].push(rest);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const result = {};
|
|
45
|
+
for (const key of topLevelKeys) {
|
|
46
|
+
if (!(key in source))
|
|
47
|
+
continue; // silently ignore unknown fields
|
|
48
|
+
const subPaths = dotFields[key];
|
|
49
|
+
if (subPaths) {
|
|
50
|
+
const val = source[key];
|
|
51
|
+
// Dot-path into a primitive can't be narrowed; omit rather than leak the whole value.
|
|
52
|
+
if (Array.isArray(val) || (val !== null && typeof val === 'object')) {
|
|
53
|
+
result[key] = maskFields(val, subPaths);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
result[key] = source[key];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Parse a comma-separated fields string into a trimmed, non-empty field list.
|
|
64
|
+
* Returns null when the string is absent or blank (caller should skip masking).
|
|
65
|
+
*/
|
|
66
|
+
export function parseFieldsParam(fields) {
|
|
67
|
+
if (!fields)
|
|
68
|
+
return null;
|
|
69
|
+
const parsed = fields
|
|
70
|
+
.split(',')
|
|
71
|
+
.map((f) => f.trim())
|
|
72
|
+
.filter(Boolean);
|
|
73
|
+
return parsed.length > 0 ? parsed : null;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=fieldMask.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|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,CAAC;IAED,MAAM,MAAM,GAA4B,EAAE,CAAC;IAC3C,KAAK,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;QAC/B,IAAI,CAAC,CAAC,GAAG,IAAI,MAAM,CAAC;YAAE,SAAS,CAAC,iCAAiC;QACjE,MAAM,QAAQ,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;YACxB,sFAAsF;YACtF,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ,CAAC,EAAE,CAAC;gBACpE,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;YAC1C,CAAC;QACH,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAC5B,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAA0B;IACzD,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAC;IACzB,MAAM,MAAM,GAAG,MAAM;SAClB,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;SACpB,MAAM,CAAC,OAAO,CAAC,CAAC;IACnB,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the project's provardx-properties.json runs this test case.
|
|
3
|
+
*
|
|
4
|
+
* `direct` means the test case path appears in `testCase` or `testCases` and
|
|
5
|
+
* no `.testinstance` references it — data-driven `<dataTable>` rows will not
|
|
6
|
+
* iterate. `plan` means the test case is referenced by at least one
|
|
7
|
+
* `.testinstance` under `plans/`, so data-driven execution works. `unknown`
|
|
8
|
+
* means the properties file could not be resolved or parsed (no
|
|
9
|
+
* `~/.sf/config.json`, file outside allowed paths, missing project root, etc.)
|
|
10
|
+
* — callers should default to the safe behaviour (emit the structural
|
|
11
|
+
* warning) when the mode is unknown.
|
|
12
|
+
*/
|
|
13
|
+
export type TestCasePlanMode = 'direct' | 'plan' | 'unknown';
|
|
14
|
+
export interface ResolvedTestCaseMode {
|
|
15
|
+
mode: TestCasePlanMode;
|
|
16
|
+
/** The properties-file path consulted (when resolved). */
|
|
17
|
+
propertiesFilePath?: string;
|
|
18
|
+
/** The project root resolved from the properties file (when present). */
|
|
19
|
+
projectPath?: string;
|
|
20
|
+
}
|
|
21
|
+
interface ResolveOptions {
|
|
22
|
+
/** Path to the test case file under validation. */
|
|
23
|
+
testCaseFilePath: string;
|
|
24
|
+
/** Allowed-paths policy from the MCP server config. */
|
|
25
|
+
allowedPaths: string[];
|
|
26
|
+
/** Override of ~/.sf/config.json location for testing. */
|
|
27
|
+
sfConfigPathOverride?: string;
|
|
28
|
+
/** Override of provardx-properties.json location for testing. */
|
|
29
|
+
propertiesFilePathOverride?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Resolve whether a given test case file is referenced directly via
|
|
33
|
+
* `testCase`/`testCases` or via a `.testinstance` inside a plan.
|
|
34
|
+
*
|
|
35
|
+
* The resolution flow is intentionally best-effort and silent: any file
|
|
36
|
+
* read failure, JSON parse error, or path-policy violation collapses to
|
|
37
|
+
* `mode: 'unknown'` so the caller falls back to default behaviour rather
|
|
38
|
+
* than surfacing a confusing error from the validator.
|
|
39
|
+
*/
|
|
40
|
+
export declare function resolveTestCasePlanMode(opts: ResolveOptions): ResolvedTestCaseMode;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Provar Limited.
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
* Licensed under the BSD 3-Clause license.
|
|
5
|
+
* For full license text, see LICENSE.md file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
6
|
+
*/
|
|
7
|
+
/* eslint-disable camelcase */
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { assertPathAllowed } from '../security/pathPolicy.js';
|
|
12
|
+
/**
|
|
13
|
+
* Resolve whether a given test case file is referenced directly via
|
|
14
|
+
* `testCase`/`testCases` or via a `.testinstance` inside a plan.
|
|
15
|
+
*
|
|
16
|
+
* The resolution flow is intentionally best-effort and silent: any file
|
|
17
|
+
* read failure, JSON parse error, or path-policy violation collapses to
|
|
18
|
+
* `mode: 'unknown'` so the caller falls back to default behaviour rather
|
|
19
|
+
* than surfacing a confusing error from the validator.
|
|
20
|
+
*/
|
|
21
|
+
export function resolveTestCasePlanMode(opts) {
|
|
22
|
+
const propsPath = readPropertiesFilePath(opts);
|
|
23
|
+
if (!propsPath)
|
|
24
|
+
return { mode: 'unknown' };
|
|
25
|
+
let propsObj;
|
|
26
|
+
try {
|
|
27
|
+
propsObj = JSON.parse(fs.readFileSync(propsPath, 'utf-8'));
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return { mode: 'unknown', propertiesFilePath: propsPath };
|
|
31
|
+
}
|
|
32
|
+
const projectPath = typeof propsObj['projectPath'] === 'string' ? propsObj['projectPath'] : null;
|
|
33
|
+
if (!projectPath) {
|
|
34
|
+
// Without a project root we cannot resolve relative `testCase` entries or
|
|
35
|
+
// walk `plans/`. The mode is unknown.
|
|
36
|
+
return { mode: 'unknown', propertiesFilePath: propsPath };
|
|
37
|
+
}
|
|
38
|
+
let resolvedProjectPath;
|
|
39
|
+
try {
|
|
40
|
+
resolvedProjectPath = path.resolve(projectPath);
|
|
41
|
+
assertPathAllowed(resolvedProjectPath, opts.allowedPaths);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return { mode: 'unknown', propertiesFilePath: propsPath };
|
|
45
|
+
}
|
|
46
|
+
const resolvedTestCasePath = path.resolve(opts.testCaseFilePath);
|
|
47
|
+
// A `.testinstance` reference inside any plan wins — plan mode supports
|
|
48
|
+
// data-driven iteration.
|
|
49
|
+
if (isReferencedFromPlanInstance(resolvedProjectPath, resolvedTestCasePath)) {
|
|
50
|
+
return { mode: 'plan', propertiesFilePath: propsPath, projectPath: resolvedProjectPath };
|
|
51
|
+
}
|
|
52
|
+
if (isReferencedDirectly(propsObj, resolvedProjectPath, resolvedTestCasePath)) {
|
|
53
|
+
return { mode: 'direct', propertiesFilePath: propsPath, projectPath: resolvedProjectPath };
|
|
54
|
+
}
|
|
55
|
+
return { mode: 'unknown', propertiesFilePath: propsPath, projectPath: resolvedProjectPath };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Resolve the active properties file path. Prefers an explicit override
|
|
59
|
+
* (used by tests), then `~/.sf/config.json`'s `PROVARDX_PROPERTIES_FILE_PATH`.
|
|
60
|
+
*
|
|
61
|
+
* Both the override and the value read from `~/.sf/config.json` are funnelled
|
|
62
|
+
* through `assertPathAllowed` so a caller cannot trick the resolver into
|
|
63
|
+
* reading a properties file outside the configured `allowedPaths`. Any policy
|
|
64
|
+
* violation collapses to `null` to preserve the helper's best-effort contract.
|
|
65
|
+
*/
|
|
66
|
+
function readPropertiesFilePath(opts) {
|
|
67
|
+
if (opts.propertiesFilePathOverride) {
|
|
68
|
+
return enforceAllowed(opts.propertiesFilePathOverride, opts.allowedPaths);
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
const sfConfigPath = opts.sfConfigPathOverride ?? path.join(os.homedir(), '.sf', 'config.json');
|
|
72
|
+
if (!fs.existsSync(sfConfigPath))
|
|
73
|
+
return null;
|
|
74
|
+
const sfConfig = JSON.parse(fs.readFileSync(sfConfigPath, 'utf-8'));
|
|
75
|
+
const propsPath = sfConfig['PROVARDX_PROPERTIES_FILE_PATH'];
|
|
76
|
+
if (!propsPath)
|
|
77
|
+
return null;
|
|
78
|
+
return enforceAllowed(propsPath, opts.allowedPaths);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Resolve a candidate properties-file path through `assertPathAllowed`, then
|
|
86
|
+
* canonicalize via `fs.realpathSync` when the file exists so a symlink
|
|
87
|
+
* inside an allowed directory cannot redirect the read outside it. Returns
|
|
88
|
+
* `null` on any policy violation, missing file, or unexpected error — the
|
|
89
|
+
* caller treats `null` as "mode unknown" and falls back to default behaviour.
|
|
90
|
+
*/
|
|
91
|
+
function enforceAllowed(candidate, allowedPaths) {
|
|
92
|
+
try {
|
|
93
|
+
const resolved = path.resolve(candidate);
|
|
94
|
+
assertPathAllowed(resolved, allowedPaths);
|
|
95
|
+
if (!fs.existsSync(resolved))
|
|
96
|
+
return null;
|
|
97
|
+
try {
|
|
98
|
+
return fs.realpathSync(resolved);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return resolved;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Does the properties file's top-level `testCase` or `testCases` array
|
|
110
|
+
* reference this test case file? Entries are interpreted relative to
|
|
111
|
+
* `<projectPath>/tests/` (per the Provar runtime convention).
|
|
112
|
+
*/
|
|
113
|
+
function isReferencedDirectly(props, projectPath, testCaseFilePath) {
|
|
114
|
+
const entries = [];
|
|
115
|
+
const tc = props['testCase'];
|
|
116
|
+
const tcs = props['testCases'];
|
|
117
|
+
for (const candidate of [tc, tcs]) {
|
|
118
|
+
if (typeof candidate === 'string') {
|
|
119
|
+
entries.push(candidate);
|
|
120
|
+
}
|
|
121
|
+
else if (Array.isArray(candidate)) {
|
|
122
|
+
for (const e of candidate) {
|
|
123
|
+
if (typeof e === 'string')
|
|
124
|
+
entries.push(e);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (entries.length === 0)
|
|
129
|
+
return false;
|
|
130
|
+
const testsDir = path.join(projectPath, 'tests');
|
|
131
|
+
const targetNorm = path.resolve(testCaseFilePath).toLowerCase();
|
|
132
|
+
for (const entry of entries) {
|
|
133
|
+
// Provar accepts both bare names ("MyTest") and relative paths
|
|
134
|
+
// ("Module/MyTest.testcase"). Allow either; match with and without the
|
|
135
|
+
// `.testcase` extension.
|
|
136
|
+
const variants = [];
|
|
137
|
+
const trimmed = entry.replace(/^[/\\]+/, '');
|
|
138
|
+
variants.push(path.resolve(testsDir, trimmed));
|
|
139
|
+
if (!/\.testcase$/i.test(trimmed)) {
|
|
140
|
+
variants.push(path.resolve(testsDir, `${trimmed}.testcase`));
|
|
141
|
+
}
|
|
142
|
+
for (const v of variants) {
|
|
143
|
+
if (v.toLowerCase() === targetNorm)
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Walk `<projectPath>/plans/` for `.testinstance` files referencing this
|
|
151
|
+
* test case via `testCasePath="..."`. Best-effort — any read error skips
|
|
152
|
+
* the offending file.
|
|
153
|
+
*/
|
|
154
|
+
function isReferencedFromPlanInstance(projectPath, testCaseFilePath) {
|
|
155
|
+
const plansDir = path.join(projectPath, 'plans');
|
|
156
|
+
if (!fs.existsSync(plansDir))
|
|
157
|
+
return false;
|
|
158
|
+
const testsDir = path.join(projectPath, 'tests');
|
|
159
|
+
const targetNorm = path.resolve(testCaseFilePath).toLowerCase();
|
|
160
|
+
let found = false;
|
|
161
|
+
const walk = (dir) => {
|
|
162
|
+
if (found)
|
|
163
|
+
return;
|
|
164
|
+
let entries;
|
|
165
|
+
try {
|
|
166
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
for (const entry of entries) {
|
|
172
|
+
if (found)
|
|
173
|
+
return;
|
|
174
|
+
if (entry.name.startsWith('.') && !entry.name.endsWith('.testinstance'))
|
|
175
|
+
continue;
|
|
176
|
+
const full = path.join(dir, entry.name);
|
|
177
|
+
if (entry.isDirectory()) {
|
|
178
|
+
walk(full);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (!entry.name.endsWith('.testinstance'))
|
|
182
|
+
continue;
|
|
183
|
+
let content;
|
|
184
|
+
try {
|
|
185
|
+
content = fs.readFileSync(full, 'utf-8');
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const matches = content.matchAll(/testCasePath=["']([^"']+)["']/g);
|
|
191
|
+
for (const m of matches) {
|
|
192
|
+
const rel = m[1].replace(/\\/g, '/');
|
|
193
|
+
// testCasePath in Provar testinstances is conventionally relative to
|
|
194
|
+
// `<projectPath>/tests/`. Also tolerate paths relative to project root.
|
|
195
|
+
const candidates = [path.resolve(testsDir, rel), path.resolve(projectPath, rel)];
|
|
196
|
+
for (const c of candidates) {
|
|
197
|
+
if (c.toLowerCase() === targetNorm) {
|
|
198
|
+
found = true;
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
walk(plansDir);
|
|
206
|
+
return found;
|
|
207
|
+
}
|
|
208
|
+
//# sourceMappingURL=testCasePlanMode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testCasePlanMode.js","sourceRoot":"","sources":["../../../src/mcp/utils/testCasePlanMode.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,8BAA8B;AAC9B,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM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xE,MAAM,UAAU,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,CAAC;gBACjF,KAAK,MAAM,CAAC,IAAI,UAAU,EAAE,CAAC;oBAC3B,IAAI,CAAC,CAAC,WAAW,EAAE,KAAK,UAAU,EAAE,CAAC;wBACnC,KAAK,GAAG,IAAI,CAAC;wBACb,OAAO;oBACT,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC,CAAC;IAEF,IAAI,CAAC,QAAQ,CAAC,CAAC;IACf,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
type ContentItem = {
|
|
2
|
+
type: 'text';
|
|
3
|
+
text: string;
|
|
4
|
+
};
|
|
5
|
+
export interface ToolResult {
|
|
6
|
+
content: ContentItem[];
|
|
7
|
+
structuredContent?: Record<string, unknown>;
|
|
8
|
+
isError?: boolean;
|
|
9
|
+
}
|
|
10
|
+
interface ToolExtra {
|
|
11
|
+
sessionId?: string;
|
|
12
|
+
}
|
|
13
|
+
export type AnyToolCallback = (args: Record<string, unknown>, extra: ToolExtra) => ToolResult | Promise<ToolResult>;
|
|
14
|
+
interface SessionEntry {
|
|
15
|
+
calls: number;
|
|
16
|
+
totalEstimatedTokens: number;
|
|
17
|
+
}
|
|
18
|
+
export type DepthGuardState = Map<string, SessionEntry>;
|
|
19
|
+
export declare function createDepthGuardState(): DepthGuardState;
|
|
20
|
+
/**
|
|
21
|
+
* Wraps a tool handler to enforce a per-session call budget.
|
|
22
|
+
* Once `limit` calls have been made for a session, every further call returns
|
|
23
|
+
* TOOL_BUDGET_EXCEEDED without invoking the underlying handler.
|
|
24
|
+
* Callers without a sessionId (stdio transports — Claude Desktop, Cursor, etc.)
|
|
25
|
+
* share a single 'anon' bucket so the budget actually limits runaway tool use;
|
|
26
|
+
* giving each anon call a fresh UUID would defeat the purpose of the guard.
|
|
27
|
+
* `provardx_ping` is excluded from wrapping at the call site in server.ts.
|
|
28
|
+
*/
|
|
29
|
+
export declare function wrapWithDepthGuard(toolName: string, handler: AnyToolCallback, state: DepthGuardState, limit: number): AnyToolCallback;
|
|
30
|
+
export declare function estimateTokens(payload: unknown): number;
|
|
31
|
+
/**
|
|
32
|
+
* Appends a `_meta` key to `structuredContent` when PROVAR_MCP_EMIT_TOKEN_META=true.
|
|
33
|
+
* The `content[0].text` string is intentionally left unchanged — LLMs read that
|
|
34
|
+
* field, so including meta there would waste tokens on observability data.
|
|
35
|
+
*
|
|
36
|
+
* @param sessionTotalTokens - Cumulative estimated tokens for the session,
|
|
37
|
+
* included only on TOOL_BUDGET_EXCEEDED errors.
|
|
38
|
+
*/
|
|
39
|
+
export declare function attachMeta(response: ToolResult, toolName: string, detailLevel: string, sessionTotalTokens?: number): ToolResult;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Provar Limited.
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
* Licensed under the BSD 3-Clause license.
|
|
5
|
+
* For full license text, see LICENSE.md file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
6
|
+
*/
|
|
7
|
+
const MAX_SESSIONS = 1000;
|
|
8
|
+
export function createDepthGuardState() {
|
|
9
|
+
return new Map();
|
|
10
|
+
}
|
|
11
|
+
function getOrCreateEntry(state, sessionId) {
|
|
12
|
+
if (!state.has(sessionId)) {
|
|
13
|
+
if (state.size >= MAX_SESSIONS) {
|
|
14
|
+
const oldest = state.keys().next().value;
|
|
15
|
+
if (oldest !== undefined)
|
|
16
|
+
state.delete(oldest);
|
|
17
|
+
}
|
|
18
|
+
state.set(sessionId, { calls: 0, totalEstimatedTokens: 0 });
|
|
19
|
+
}
|
|
20
|
+
// Non-null guaranteed by the set above or pre-existing entry.
|
|
21
|
+
return state.get(sessionId);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Wraps a tool handler to enforce a per-session call budget.
|
|
25
|
+
* Once `limit` calls have been made for a session, every further call returns
|
|
26
|
+
* TOOL_BUDGET_EXCEEDED without invoking the underlying handler.
|
|
27
|
+
* Callers without a sessionId (stdio transports — Claude Desktop, Cursor, etc.)
|
|
28
|
+
* share a single 'anon' bucket so the budget actually limits runaway tool use;
|
|
29
|
+
* giving each anon call a fresh UUID would defeat the purpose of the guard.
|
|
30
|
+
* `provardx_ping` is excluded from wrapping at the call site in server.ts.
|
|
31
|
+
*/
|
|
32
|
+
export function wrapWithDepthGuard(toolName, handler, state, limit) {
|
|
33
|
+
return async (args, extra) => {
|
|
34
|
+
const sessionId = extra.sessionId ?? 'anon';
|
|
35
|
+
const entry = getOrCreateEntry(state, sessionId);
|
|
36
|
+
if (entry.calls >= limit) {
|
|
37
|
+
const payload = {
|
|
38
|
+
error: 'TOOL_BUDGET_EXCEEDED',
|
|
39
|
+
callsMade: entry.calls,
|
|
40
|
+
limit,
|
|
41
|
+
suggestion: 'Summarize progress and return control to the user.',
|
|
42
|
+
};
|
|
43
|
+
const response = {
|
|
44
|
+
isError: true,
|
|
45
|
+
content: [{ type: 'text', text: JSON.stringify(payload) }],
|
|
46
|
+
structuredContent: payload,
|
|
47
|
+
};
|
|
48
|
+
return attachMeta(response, toolName, 'standard', entry.totalEstimatedTokens);
|
|
49
|
+
}
|
|
50
|
+
entry.calls++;
|
|
51
|
+
const result = await handler(args, extra);
|
|
52
|
+
if (process.env['PROVAR_MCP_EMIT_TOKEN_META'] === 'true') {
|
|
53
|
+
entry.totalEstimatedTokens += estimateTokens(result);
|
|
54
|
+
}
|
|
55
|
+
const detailLevel = typeof args['detail'] === 'string' ? args['detail'] : 'standard';
|
|
56
|
+
return attachMeta(result, toolName, detailLevel);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
// --------------------------------------------------------------------------- //
|
|
60
|
+
// PDX-475 — Token meta attachment (PROVAR_MCP_EMIT_TOKEN_META)
|
|
61
|
+
// --------------------------------------------------------------------------- //
|
|
62
|
+
export function estimateTokens(payload) {
|
|
63
|
+
return Math.ceil(JSON.stringify(payload).length / 4);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Appends a `_meta` key to `structuredContent` when PROVAR_MCP_EMIT_TOKEN_META=true.
|
|
67
|
+
* The `content[0].text` string is intentionally left unchanged — LLMs read that
|
|
68
|
+
* field, so including meta there would waste tokens on observability data.
|
|
69
|
+
*
|
|
70
|
+
* @param sessionTotalTokens - Cumulative estimated tokens for the session,
|
|
71
|
+
* included only on TOOL_BUDGET_EXCEEDED errors.
|
|
72
|
+
*/
|
|
73
|
+
export function attachMeta(response, toolName, detailLevel, sessionTotalTokens) {
|
|
74
|
+
if (process.env['PROVAR_MCP_EMIT_TOKEN_META'] !== 'true')
|
|
75
|
+
return response;
|
|
76
|
+
const meta = {
|
|
77
|
+
tool: toolName,
|
|
78
|
+
detailLevel,
|
|
79
|
+
estimatedTokens: estimateTokens(response),
|
|
80
|
+
};
|
|
81
|
+
if (sessionTotalTokens !== undefined) {
|
|
82
|
+
meta['sessionTotalEstimatedTokens'] = sessionTotalTokens;
|
|
83
|
+
}
|
|
84
|
+
const existing = response.structuredContent ?? {};
|
|
85
|
+
return {
|
|
86
|
+
...response,
|
|
87
|
+
structuredContent: { ...existing, _meta: meta },
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=tokenMeta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokenMeta.js","sourceRoot":"","sources":["../../../src/mcp/utils/tokenMeta.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA+BH,MAAM,YAAY,GAAG,IAAI,CAAC;AAE1B,MAAM,UAAU,qBAAqB;IACnC,OAAO,IAAI,GAAG,EAAE,CAAC;AACnB,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAsB,EAAE,SAAiB;IACjE,IAAI,CAAC,KAAK,CAA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,QAAQ;QACX,iBAAiB,EAAE,EAAE,GAAG,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE;KAChD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export type DiffableViolation = Record<string, unknown>;
|
|
2
|
+
export interface DiffResult {
|
|
3
|
+
added: DiffableViolation[];
|
|
4
|
+
resolved: DiffableViolation[];
|
|
5
|
+
unchanged_count: number;
|
|
6
|
+
run_id: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Compute a stable 8-char context hash for a tool + context pair. Used to
|
|
10
|
+
* scope baseline run lookups so that a run_id from context A cannot be diffed
|
|
11
|
+
* against context B (different project, different suite, different file).
|
|
12
|
+
*/
|
|
13
|
+
export declare function computeContextHash(toolTag: string, context: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Resolve the validation storage root for a given tool subdir. Honors the
|
|
16
|
+
* PROVAR_MCP_VALIDATION_DIR env var when set; otherwise falls back to
|
|
17
|
+
* `~/.provardx/validation/<subdir>`. The env override is useful for restricted
|
|
18
|
+
* CI/dev environments where the home directory is read-only or shared.
|
|
19
|
+
*/
|
|
20
|
+
export declare function resolveValidationDir(subdir: string): string;
|
|
21
|
+
/** Generate a run ID from a context string (e.g. project path or suite name). */
|
|
22
|
+
export declare function generateRunId(context: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Check whether any prior runs exist in the given storage directory.
|
|
25
|
+
* Used by calcNextAction to determine the first-run heuristic.
|
|
26
|
+
*/
|
|
27
|
+
export declare function hasAnyRun(storageDir: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Save the current violations as a new run in the storage directory.
|
|
30
|
+
* Caps the index at MAX_RUNS by evicting the oldest entry when full.
|
|
31
|
+
* Returns the generated run_id.
|
|
32
|
+
*
|
|
33
|
+
* When `contextHash` is provided, it is recorded alongside the run so that
|
|
34
|
+
* `loadBaselineViolations` can reject a baseline_run_id whose context does
|
|
35
|
+
* not match the calling context (prevents cross-context diffs).
|
|
36
|
+
*/
|
|
37
|
+
export declare function saveRun(storageDir: string, runId: string, violations: DiffableViolation[], contextHash?: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* Load the violations array for a given baseline run ID.
|
|
40
|
+
* Returns null if the run is not found in the index (BASELINE_NOT_FOUND).
|
|
41
|
+
* The filename is looked up from the index only — the run_id itself is never
|
|
42
|
+
* used to construct a file path, preventing path traversal.
|
|
43
|
+
*
|
|
44
|
+
* When `expectedContextHash` is provided, the record's `context_hash` must
|
|
45
|
+
* match. Records without a `context_hash` (written by older versions before
|
|
46
|
+
* H3) are treated as a mismatch and are effectively retired within one or
|
|
47
|
+
* two new runs as the FIFO cap evicts them. This guard prevents diffing a
|
|
48
|
+
* baseline from a different file/suite/project against the current context.
|
|
49
|
+
*/
|
|
50
|
+
export declare function loadBaselineViolations(storageDir: string, baselineRunId: string, expectedContextHash?: string): DiffableViolation[] | null;
|
|
51
|
+
/**
|
|
52
|
+
* Compute the diff between a baseline and current violations array.
|
|
53
|
+
* Uses (rule_id + applies_to + full message) as the unique key.
|
|
54
|
+
* Duplicate violations (same key, multiple occurrences) are treated as
|
|
55
|
+
* distinct entries — each occurrence is counted separately (multiset semantics).
|
|
56
|
+
*/
|
|
57
|
+
export declare function computeDiff(baseline: DiffableViolation[], current: DiffableViolation[]): Omit<DiffResult, 'run_id'>;
|