@yanzc/claude-pro 0.7.3 → 0.7.5

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/src/verify.js ADDED
@@ -0,0 +1,57 @@
1
+ // Discover a conservative project verification command when the user has not configured one.
2
+
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+
6
+ function exists(cwd, name) {
7
+ try { return fs.statSync(path.join(cwd, name)).isFile(); } catch { return false; }
8
+ }
9
+
10
+ function directoryExists(cwd, name) {
11
+ try { return fs.statSync(path.join(cwd, name)).isDirectory(); } catch { return false; }
12
+ }
13
+
14
+ function readJson(file) {
15
+ try { return JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return null; }
16
+ }
17
+
18
+ function packageRunner(cwd) {
19
+ if (exists(cwd, 'pnpm-lock.yaml')) return 'pnpm';
20
+ if (exists(cwd, 'yarn.lock')) return 'yarn';
21
+ return 'npm';
22
+ }
23
+
24
+ function meaningfulTestScript(script) {
25
+ if (typeof script !== 'string' || !script.trim()) return false;
26
+ // Skip npm init's placeholder because it intentionally exits with an error.
27
+ return !/(?:error:\s*)?no test specified/i.test(script);
28
+ }
29
+
30
+ function packageCommand(cwd) {
31
+ const pkg = readJson(path.join(cwd, 'package.json'));
32
+ const scripts = pkg?.scripts;
33
+ if (!scripts || typeof scripts !== 'object') return null;
34
+ const runner = packageRunner(cwd);
35
+ if (meaningfulTestScript(scripts.test)) return runner === 'npm' ? 'npm test' : `${runner} test`;
36
+ for (const name of ['check', 'verify', 'lint']) {
37
+ if (typeof scripts[name] === 'string' && scripts[name].trim()) return `${runner} run ${name}`;
38
+ }
39
+ return null;
40
+ }
41
+
42
+ function makefileCommand(cwd) {
43
+ if (!exists(cwd, 'Makefile')) return null;
44
+ try {
45
+ const text = fs.readFileSync(path.join(cwd, 'Makefile'), 'utf8');
46
+ return /^(?:\.PHONY\s+)?test\s*(?::|;)/m.test(text) ? 'make test' : null;
47
+ } catch { return null; }
48
+ }
49
+
50
+ export function discoverVerifyCommand(cwd) {
51
+ const root = path.resolve(cwd || process.cwd());
52
+ return packageCommand(root)
53
+ || (exists(root, 'pyproject.toml') && (directoryExists(root, 'tests') || directoryExists(root, 'test')) ? 'python -m pytest -q' : null)
54
+ || (exists(root, 'Cargo.toml') ? 'cargo test' : null)
55
+ || (exists(root, 'go.mod') ? 'go test ./...' : null)
56
+ || makefileCommand(root);
57
+ }
@@ -0,0 +1,62 @@
1
+ // Validation and recovery helpers for the machine-readable state that survives compaction.
2
+
3
+ export const WORK_STATE_VERSION = 1;
4
+
5
+ const TEXT_FIELDS = ['objective', 'next_action'];
6
+ const LIST_FIELDS = ['constraints', 'decisions', 'files', 'edits', 'tests', 'failures', 'open_questions', 'tool_artifacts'];
7
+ const ALL_FIELDS = ['schema_version', ...TEXT_FIELDS, ...LIST_FIELDS];
8
+ const MAX_TEXT_CHARS = 4_000;
9
+ const MAX_LIST_ITEMS = 50;
10
+ const MAX_ITEM_CHARS = 2_000;
11
+
12
+ export function validateWorkState(value) {
13
+ const errors = [];
14
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return { ok: false, errors: ['state must be a JSON object'] };
15
+ if (value.schema_version !== WORK_STATE_VERSION) errors.push(`schema_version must be ${WORK_STATE_VERSION}`);
16
+ for (const key of TEXT_FIELDS) {
17
+ if (typeof value[key] !== 'string') errors.push(`${key} must be a string`);
18
+ else if (value[key].length > MAX_TEXT_CHARS) errors.push(`${key} is longer than ${MAX_TEXT_CHARS} characters`);
19
+ }
20
+ for (const key of LIST_FIELDS) {
21
+ if (!Array.isArray(value[key])) {
22
+ errors.push(`${key} must be an array`);
23
+ continue;
24
+ }
25
+ if (value[key].length > MAX_LIST_ITEMS) errors.push(`${key} has more than ${MAX_LIST_ITEMS} items`);
26
+ for (const [i, item] of value[key].entries()) {
27
+ if (typeof item !== 'string') errors.push(`${key}[${i}] must be a string`);
28
+ else if (item.length > MAX_ITEM_CHARS) errors.push(`${key}[${i}] is longer than ${MAX_ITEM_CHARS} characters`);
29
+ }
30
+ }
31
+ if (errors.length) return { ok: false, errors };
32
+ // Copy only the schema we understand. This keeps future model-added fields from becoming an
33
+ // unbounded part of every later context window while retaining a stable versioned shape.
34
+ return { ok: true, value: Object.fromEntries(ALL_FIELDS.map((key) => [key, key === 'schema_version' ? WORK_STATE_VERSION : value[key]])) };
35
+ }
36
+
37
+ export function workStateFromText(text) {
38
+ const source = String(text ?? '');
39
+ const match = /<work-state>\s*([\s\S]*?)\s*<\/work-state>/i.exec(source);
40
+ if (!match) return { present: false, state: null, errors: [] };
41
+ let value;
42
+ try {
43
+ value = JSON.parse(match[1].replace(/^```(?:json)?\s*|\s*```$/gi, '').trim());
44
+ } catch (err) {
45
+ return { present: true, state: null, errors: [`invalid JSON: ${err.message}`] };
46
+ }
47
+ const checked = validateWorkState(value);
48
+ return { present: true, state: checked.ok ? checked.value : null, errors: checked.errors || [] };
49
+ }
50
+
51
+ export function latestWorkState(messages) {
52
+ for (let i = messages.length - 1; i >= 0; i--) {
53
+ const content = messages[i]?.content;
54
+ const blocks = Array.isArray(content) ? content : [{ type: 'text', text: content }];
55
+ for (let j = blocks.length - 1; j >= 0; j--) {
56
+ if (blocks[j]?.type !== 'text') continue;
57
+ const parsed = workStateFromText(blocks[j].text);
58
+ if (parsed.state) return parsed.state;
59
+ }
60
+ }
61
+ return null;
62
+ }