@aiwg/cli 2026.10.12 → 2026.10.15
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/agentic/code/addons/aiwg-utils/rules/anti-laziness.core.md +13 -0
- package/agentic/code/addons/aiwg-utils/rules/anti-laziness.md +118 -0
- package/agentic/code/providers/pi/aiwg-bridge.ts +44 -1
- package/dist/src/artifacts/corpus-tools/cli.js +32 -0
- package/dist/src/artifacts/corpus-tools/discovery-log.js +1 -1
- package/dist/src/artifacts/corpus-tools/expand-cli.js +89 -0
- package/dist/src/artifacts/corpus-tools/expand-queue.js +38 -0
- package/dist/src/artifacts/corpus-tools/expand-state.js +40 -0
- package/dist/src/artifacts/corpus-tools/expand.js +125 -0
- package/dist/src/artifacts/corpus-tools/resolve-cli.js +91 -0
- package/dist/src/artifacts/corpus-tools/resolve-corpus.js +41 -0
- package/dist/src/artifacts/corpus-tools/resolve.js +105 -0
- package/dist/src/cli/handlers/doctor-args.js +40 -0
- package/dist/src/cli/handlers/doctor-help.js +4 -2
- package/dist/src/cli/handlers/use-bundle-counts.js +32 -6
- package/dist/src/cli/handlers/use-skill-assets.js +207 -0
- package/dist/src/cli/handlers/use.js +30 -193
- package/dist/src/cli/handlers/utilities.js +5 -31
- package/dist/src/config/authorization-cli.js +11 -3
- package/dist/src/config/worker-context.js +33 -4
- package/dist/src/decision/context-pruning-common.js +41 -0
- package/dist/src/decision/context-pruning-evaluation.js +366 -0
- package/dist/src/decision/context-pruning-pilot.js +431 -0
- package/dist/src/decision/context-pruning-types.js +19 -0
- package/dist/src/decision/context-pruning.js +5 -847
- package/dist/src/decision/ensemble/contract.js +4 -24
- package/dist/src/decision/heldout/collector.js +1 -1
- package/dist/src/decision/qualification/release.js +5 -62
- package/dist/src/extensions/manifest.js +8 -1
- package/dist/src/extensions/project-local-doctor.js +78 -2
- package/dist/src/extensions/validation.js +10 -11
- package/dist/src/features/catalog.js +1 -1
- package/dist/src/gates/evaluate.js +5 -15
- package/dist/src/gates/heldout.js +42 -0
- package/dist/src/gates/index.js +2 -1
- package/dist/src/gates/upstream-ceiling.js +111 -0
- package/dist/src/models/provider-models.js +5 -8
- package/dist/src/models/worker-installation-store.mjs +228 -0
- package/dist/src/models/worker-installation.mjs +7 -162
- package/dist/src/models/wrapper-definitions.js +14 -0
- package/dist/src/models/wrapper-deployment.js +48 -25
- package/dist/src/models/wrapper-enforcement.js +72 -0
- package/dist/src/policy/hitl-check.js +80 -0
- package/dist/src/providers/provider-definitions.js +10 -10
- package/dist/src/research/clients/arxiv.js +10 -4
- package/dist/src/research/clients/base.js +5 -4
- package/dist/src/research/clients/crossref.js +9 -1
- package/dist/src/research/clients/semantic-scholar.js +73 -44
- package/dist/src/research/identity.js +85 -0
- package/dist/src/research/index.js +1 -0
- package/dist/src/research/reference-match.js +89 -0
- package/dist/src/rules/activation.d.mts +2 -0
- package/dist/src/rules/activation.mjs +23 -0
- package/dist/src/rules/core-startup.d.mts +2 -0
- package/dist/src/rules/core-startup.mjs +31 -0
- package/dist/src/skills/adapters/local.js +32 -9
- package/dist/src/skills/agent-skills.js +2 -0
- package/dist/src/skills/cli-display.js +33 -0
- package/dist/src/skills/cli.js +2 -26
- package/dist/src/skills/collision-discovery.js +84 -0
- package/dist/src/skills/deployer.js +20 -75
- package/dist/src/skills/deployment-ownership.js +74 -0
- package/dist/src/skills/doctor.js +2 -2
- package/dist/src/skills/importer.js +4 -26
- package/dist/src/skills/registry.js +22 -7
- package/dist/src/smiths/context-pipeline/agents-md.js +34 -0
- package/dist/src/smiths/context-pipeline/aiwg-md.js +3 -0
- package/dist/src/smiths/context-pipeline/bridge-ownership.js +50 -0
- package/dist/src/smiths/context-pipeline/generator.js +40 -104
- package/dist/src/smiths/context-pipeline/managed-hook.js +10 -0
- package/dist/src/smiths/context-pipeline/parallelism-section.js +7 -3
- package/package.json +6 -2
- package/tools/agents/deploy-agents.mjs +2 -123
- package/tools/agents/providers/antigravity.mjs +4 -6
- package/tools/agents/providers/base.mjs +3 -3
- package/tools/agents/providers/claude.mjs +3 -5
- package/tools/agents/providers/codex.mjs +3 -5
- package/tools/agents/providers/context-write.mjs +12 -0
- package/tools/agents/providers/copilot.mjs +3 -5
- package/tools/agents/providers/cursor.mjs +3 -5
- package/tools/agents/providers/factory.mjs +3 -5
- package/tools/agents/providers/grok-build.mjs +17 -12
- package/tools/agents/providers/hermes-skill-lifecycle.mjs +144 -0
- package/tools/agents/providers/hermes-startup.mjs +209 -0
- package/tools/agents/providers/hermes.mjs +9 -359
- package/tools/agents/providers/model-wrapper.mjs +90 -0
- package/tools/agents/providers/muse.mjs +3 -3
- package/tools/agents/providers/omp.mjs +3 -1
- package/tools/agents/providers/openclaw.mjs +3 -5
- package/tools/agents/providers/opencode.mjs +3 -5
- package/tools/agents/providers/pi.mjs +1 -1
- package/tools/agents/providers/provider-paths-staging.mjs +9 -0
- package/tools/agents/providers/rule-delivery.mjs +26 -228
- package/tools/agents/providers/rule-presentation.mjs +13 -4
- package/tools/agents/providers/rule-projection.mjs +119 -0
- package/tools/agents/providers/rule-startup.mjs +70 -0
- package/tools/agents/providers/stale-artifact-prune.mjs +128 -0
- package/tools/agents/providers/warp.mjs +5 -7
- package/tools/agents/providers/windsurf.mjs +14 -62
- package/tools/plugin/package-plugins.mjs +2 -2
- package/tools/plugin/plugin-links.mjs +3 -0
- package/tools/skills/codex-skill-transform.mjs +232 -0
- package/tools/skills/deploy-skills-codex.mjs +8 -249
- package/tools/warp/warp-content.mjs +548 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Core: Anti-Laziness
|
|
2
|
+
|
|
3
|
+
Always applies to every task and agent. Overrides convenience, brevity and speed preferences.
|
|
4
|
+
|
|
5
|
+
- Finish the authorized task within its scope. Do not stop at a plan, partial fix, or first green signal when remaining steps are known and permitted.
|
|
6
|
+
- Do not hand work back that you can do yourself. Ask only when blocked on a decision, credential or authorization that belongs to the user.
|
|
7
|
+
- Verify before claiming done: run the checks the change needs and read their results. "Should work" is not done.
|
|
8
|
+
- Fix root causes. Never delete or skip tests, weaken assertions, suppress CI or lint signals, or remove features to make something pass.
|
|
9
|
+
- Read failures fully and investigate before retrying; no blind retries or approach-swapping to dodge a problem.
|
|
10
|
+
- Report outcomes faithfully: say what failed, was skipped and remains. Never present partial work as complete.
|
|
11
|
+
- On detecting avoidance, stop, name it, and resume the full task.
|
|
12
|
+
|
|
13
|
+
Full rule (on demand, not an import): `aiwg show rule anti-laziness`
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
---
|
|
2
|
+
activation: core
|
|
3
|
+
summary: "Complete authorized implementation and verification; fix root causes without deleting tests, weakening assertions, hiding failures, or removing features to pass."
|
|
4
|
+
enforcement: high
|
|
5
|
+
triggers:
|
|
6
|
+
- "this test keeps failing"
|
|
7
|
+
- "can I skip this test"
|
|
8
|
+
- "should I delete this test"
|
|
9
|
+
- "the task is too hard"
|
|
10
|
+
- "is this done"
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# Anti-Laziness Rules
|
|
14
|
+
|
|
15
|
+
**Enforcement Level**: HIGH
|
|
16
|
+
**Scope**: All code-generating agents
|
|
17
|
+
**Research Basis**: REF-071 (METR reward hacking), REF-072 (Anthropic misalignment), REF-073 (Microsoft taxonomy), REF-074 (lazy learners), REF-015 (Self-Refine), REF-002 (LLM failures)
|
|
18
|
+
**Issue**: #264, #490
|
|
19
|
+
|
|
20
|
+
## Overview
|
|
21
|
+
|
|
22
|
+
These rules prevent destructive avoidance behaviors where agents abandon difficult tasks through test deletion, feature removal, and premature termination rather than fixing root causes. This emerges from reward hacking, sycophancy optimization, and shortcut learning — not actual laziness.
|
|
23
|
+
|
|
24
|
+
## The Standard: Boil the Ocean (Within Scope)
|
|
25
|
+
|
|
26
|
+
The rules below set the **floor** (shortcuts never to take). This section sets the **ceiling** (what completion looks like). A task is done when the work is complete, not merely when no rule was broken.
|
|
27
|
+
|
|
28
|
+
- **Do the whole thing.** Code without tests is incomplete; tests without docs are incomplete; a fix without an integration check is incomplete. The marginal cost of completeness inside scope is near zero.
|
|
29
|
+
- **Tie off the dangling thread.** If you noticed it, you fix it — unless fixing it expands scope, in which case file an issue *and* state plainly that it's deferred (don't pretend it's closed).
|
|
30
|
+
- **Real fix over workaround.** Workarounds become permanent. Only acceptable when the real fix is out of scope or blocked on a decision you cannot make.
|
|
31
|
+
- **The answer is the finished product.** When asked to do X, "here is a plan to do X" is not the answer unless a plan was explicitly requested.
|
|
32
|
+
- **Time, fatigue, and complexity are not excuses.** Decompose, recurse, parallelize, escalate research — do not punt.
|
|
33
|
+
|
|
34
|
+
**Within-scope discipline**: This applies *strictly within the authorized scope*. It is never license to expand scope. A finding is not authorization to act (`human-authorization`); adjacent work is filed as issues, not silently absorbed. Respect the task's time/entity/operation boundaries (`scoped-reasoning`). Ambition still needs measurable completion criteria (`vague-discretion`). Summary: **draw the lines once, then leave nothing half-done inside them.**
|
|
35
|
+
|
|
36
|
+
## Mandatory Rules
|
|
37
|
+
|
|
38
|
+
### Rule 1: Never Delete Tests to Pass
|
|
39
|
+
Fix the code the test validates, not the test. **FORBIDDEN**: deleting test files, commenting out test blocks/cases, test-count regression. **Detection**: deleted test files, commented-out tests, removed cases, test count down. **Legitimate deletion** (must be documented): replacing obsolete tests with better ones (count maintained/increased), merging duplicates (coverage maintained), removing tests for removed features.
|
|
40
|
+
|
|
41
|
+
### Rule 2: Never Add Skip Patterns
|
|
42
|
+
**FORBIDDEN**: `.skip()`, `xit()`, `xdescribe()`, `test.todo()` without implementation, `@Ignore`/`@Disabled`. Fix the test or the code it validates instead. **Detection**: skip annotations added; passing count rises while total is unchanged.
|
|
43
|
+
|
|
44
|
+
### Rule 3: Never Remove Features Instead of Fixing
|
|
45
|
+
**FORBIDDEN**: commenting out a broken feature; replacing it with a stub that returns a trivial value (`true`/`null`/`[]`); flipping a feature flag to `false` to dodge the failure. **REQUIRED**: fix the actual issue (add the missing guard, proper error handling). **Detection**: functions returning trivial values, commented-out code, reduced functionality without a documented reason.
|
|
46
|
+
|
|
47
|
+
### Rule 4: Never Weaken Assertions
|
|
48
|
+
**FORBIDDEN**: replacing a specific assertion with `expect(true).toBe(true)` or an over-generic `.toBeDefined()`/`.toBeTruthy()`. **REQUIRED**: keep specific assertions (`expect(result.error.code).toBe('INVALID_EMAIL')`) and fix the code. **Detection**: assertion count down, specific matchers genericized, assertion complexity dropped >10%.
|
|
49
|
+
|
|
50
|
+
### Rule 5: Maintain or Improve Coverage
|
|
51
|
+
**FORBIDDEN**: reducing coverage without justification (deleting tests, adding untested code, skipping, removing assertions). **REQUIRED** coverage floors: new feature ≥80%; bug fix 100% of the fix; refactor maintains baseline. Coverage may drop at most 2% vs the baseline captured at task start (test count and assertion count must be ≥ baseline); otherwise block and recover.
|
|
52
|
+
|
|
53
|
+
### Rule 6: Complete the Task
|
|
54
|
+
**FORBIDDEN**: "I couldn't figure it out so I commented out the test / removed the validation / moved on." **REQUIRED**: when genuinely stuck after honest attempts, escalate with full context (what was tried, what was ruled out). **Recovery Protocol**: PAUSE (stop, preserve state) → DIAGNOSE (root cause, not symptoms) → ADAPT (select strategy) → RETRY (max 3 attempts) → ESCALATE (human, with full context).
|
|
55
|
+
|
|
56
|
+
### Rule 7: Document Blockers, Don't Hide Them
|
|
57
|
+
**FORBIDDEN**: empty catch blocks that swallow errors; removing error logging. **REQUIRED**: log the error, record the blocker (task, error, attempts, status, context), and re-throw — never hide failures.
|
|
58
|
+
|
|
59
|
+
### Rule 8: Never Suppress CI/Pipeline Signals
|
|
60
|
+
**FORBIDDEN** in workflow files: `|| true`, `|| :`, `|| echo` after test/lint/build; new `continue-on-error: true` on a blocking job; `2>/dev/null` on test commands; `set +e` around test blocks; removing a failing job/step entirely. The job exists because someone identified a quality gate. **REQUIRED**: fix the underlying cause; for genuinely flaky tests use a bounded targeted retry (then fix the flakiness), never blanket suppression. **Detection**: diffs to `.github/workflows/`, `.gitea/workflows/`, CI configs that reduce failure visibility. **Legitimate**: informational jobs that were never gates; jobs for decommissioned features (documented); replacement with a stricter alternative.
|
|
61
|
+
|
|
62
|
+
### Rule 9: Do the Whole Thing (Within Scope)
|
|
63
|
+
Operationalizes the ceiling above. Rules 1–8 forbid destructive shortcuts; Rule 9 forbids stopping short of a complete deliverable.
|
|
64
|
+
|
|
65
|
+
**FORBIDDEN**: returning a plan when a deliverable was requested; fixing the reported instance but ignoring identical bugs in adjacent code you just touched; adding a fallback/workaround that masks a deeper bug; committing code without the in-scope tests/docs; declaring "done" without running the verification command.
|
|
66
|
+
|
|
67
|
+
**REQUIRED**: ship X done (code + tests + docs + verification run, with results stated). Fix the identical bug in every call site in the same module (the same-module fix is part of fixing the bug); file an issue only when it expands into a different module, and say so. Apply the real fix, not a masking fallback.
|
|
68
|
+
|
|
69
|
+
**When Rule 9 yields**: `human-authorization` outranks it when completing would expand scope or take an irreversible action without authorization; `scoped-reasoning` outranks it when completing would pull in out-of-scope periods/entities/operations; after 3 honest failed attempts, the Rule 6 recovery protocol applies — escalate, don't workaround.
|
|
70
|
+
|
|
71
|
+
## Test Analysis
|
|
72
|
+
|
|
73
|
+
Test-failure investigation is analysis, not a binary. Categorize and fix the root cause — never suppress/delete/skip:
|
|
74
|
+
|
|
75
|
+
| Category | Correct Response |
|
|
76
|
+
|----------|-----------------|
|
|
77
|
+
| **Code bug** — production code violates its spec | Fix the production code |
|
|
78
|
+
| **Test bug** — wrong expectations/setup | Fix the expectations, preserving intent |
|
|
79
|
+
| **Environment issue** — external state/timing/resources | Isolate, mock, or stabilize the dependency |
|
|
80
|
+
| **Flaky test** — intermittent without code changes | Find and eliminate the non-determinism |
|
|
81
|
+
|
|
82
|
+
One failing test may reveal several issues — splitting it into focused tests is valid investigation. "The tests are wrong" requires the same rigor as "the code is wrong": demonstrate why, then fix. Flaky tests are bugs deserving root-cause fixes, not indefinite retry masking.
|
|
83
|
+
|
|
84
|
+
## Detection Pattern Catalog
|
|
85
|
+
|
|
86
|
+
Full catalog: `@$AIWG_ROOT/agentic/code/addons/persistence/patterns/avoidance-catalog.yaml`. Categories: Test Deletion, Test Skipping, Feature Removal, Assertion Weakening, Validation Bypass, Error Suppression, Workaround Addition, CI/Pipeline Suppression. On every file write, check the diff against these categories, assess intent, and determine legitimacy before proceeding.
|
|
87
|
+
|
|
88
|
+
## Recovery When Avoidance Is Detected
|
|
89
|
+
|
|
90
|
+
`PAUSE → DIAGNOSE → ADAPT → RETRY → ESCALATE`. PAUSE: stop, preserve state, log context. DIAGNOSE: classify root cause (cognitive overload / misunderstanding / knowledge gap / inherent complexity); consult reflection memory. ADAPT: simplify, request context, change approach, reduce scope, or escalate early. RETRY: max 3 attempts, track each. ESCALATE: HITL gate with original task, detection details, all attempted strategies, failure analysis, and a recommendation. Do not proceed without resolution. Integrates with agent loops (per-iteration baseline + avoidance checks, best-output selection per REF-015), executable-feedback (run tests before return), and HITL gates (test-deletion / coverage-regression / max-attempts escalation).
|
|
91
|
+
|
|
92
|
+
## Checklist
|
|
93
|
+
|
|
94
|
+
Before completing any code task:
|
|
95
|
+
|
|
96
|
+
- [ ] All tests executed and passing; test count ≥ baseline; coverage ≥ baseline − 2%; assertion count ≥ baseline
|
|
97
|
+
- [ ] No skip patterns added; no tests deleted (or deletion justified); no features removed (or removal documented)
|
|
98
|
+
- [ ] No assertions weakened; no errors suppressed; no CI/pipeline signals suppressed
|
|
99
|
+
- [ ] If stuck, escalated with full context
|
|
100
|
+
- [ ] The whole thing delivered within scope (code + tests + docs + verification command run)
|
|
101
|
+
- [ ] Dangling threads inside scope tied off; adjacent out-of-scope work filed as issues, not silently absorbed
|
|
102
|
+
- [ ] Final deliverable is the finished product, not a plan (unless a plan was explicitly requested)
|
|
103
|
+
|
|
104
|
+
## References
|
|
105
|
+
|
|
106
|
+
- @.aiwg/research/findings/agentic-laziness-research.md — research compilation
|
|
107
|
+
- @.aiwg/architecture/agent-persistence-sad.md — architecture
|
|
108
|
+
- @$AIWG_ROOT/agentic/code/frameworks/sdlc-complete/rules/executable-feedback.md
|
|
109
|
+
- @$AIWG_ROOT/agentic/code/frameworks/sdlc-complete/rules/actionable-feedback.md
|
|
110
|
+
- @$AIWG_ROOT/agentic/code/frameworks/sdlc-complete/rules/tao-loop.md
|
|
111
|
+
- @$AIWG_ROOT/agentic/code/addons/aiwg-utils/rules/vague-discretion.md — paired ceiling/floor
|
|
112
|
+
- @$AIWG_ROOT/agentic/code/addons/aiwg-utils/rules/human-authorization.md — within-scope boundary
|
|
113
|
+
- @$AIWG_ROOT/agentic/code/addons/persistence/patterns/avoidance-catalog.yaml — pattern catalog
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
**Rule Status**: ACTIVE
|
|
118
|
+
**Last Updated**: 2026-05-08
|
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
/** AIWG's reviewed Pi bridge. Loaded only after Pi project trust is granted. */
|
|
2
2
|
import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';
|
|
3
|
+
import { execFile } from 'node:child_process';
|
|
4
|
+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import { basename, join } from 'node:path';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
|
|
9
|
+
const execute = promisify(execFile);
|
|
10
|
+
type Action = { category: string; repo: string; environment: string; resources: string[]; reversible: boolean; impact: 'low' | 'medium' | 'high' };
|
|
11
|
+
type Check = { decision: string; receipt?: { file: string; effectId: string } };
|
|
12
|
+
|
|
13
|
+
/** Deployed bridges call the installed CLI, never import checkout-only source paths. */
|
|
14
|
+
export async function checkAiwgAuthorization(projectDir: string, action: Action): Promise<Check> {
|
|
15
|
+
const temporary = await mkdtemp(join(tmpdir(), 'aiwg-pi-authorization-'));
|
|
16
|
+
try {
|
|
17
|
+
const file = join(temporary, 'action.json');
|
|
18
|
+
await writeFile(file, JSON.stringify(action), { mode: 0o600 });
|
|
19
|
+
const result = await execute('aiwg', ['config', 'authorization', 'check', '--action-file', file, '--target', projectDir],
|
|
20
|
+
{ cwd: projectDir, timeout: 15_000, maxBuffer: 64 * 1024 });
|
|
21
|
+
return JSON.parse(result.stdout) as Check;
|
|
22
|
+
} catch { return { decision: 'escalate' }; }
|
|
23
|
+
finally { await rm(temporary, { recursive: true, force: true }); }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Host-only descriptors and callbacks; tool/issue/model payloads cannot supply these authority fields. */
|
|
27
|
+
export async function evaluateAiwgPiAction(projectDir: string, action: Action, hasUI: boolean,
|
|
28
|
+
confirm?: () => Promise<boolean>, check = checkAiwgAuthorization,
|
|
29
|
+
): Promise<{ block: true; reason: string } | undefined> {
|
|
30
|
+
let result: Check;
|
|
31
|
+
try { result = await check(projectDir, action); } catch { result = { decision: 'escalate' }; }
|
|
32
|
+
if (result.decision === 'auto-authorized' && result.receipt?.file && result.receipt.effectId) return undefined;
|
|
33
|
+
if (result.decision === 'deny') return { block: true, reason: 'Denied by AIWG operator policy.' };
|
|
34
|
+
if (!hasUI) return { block: true, reason: 'AIWG policy blocked a destructive or package-mutating command in headless mode.' };
|
|
35
|
+
return await confirm?.() ? undefined : { block: true, reason: 'Denied by AIWG operator policy.' };
|
|
36
|
+
}
|
|
3
37
|
|
|
4
38
|
const destructive = /(?:^|[;&|]\s*)(?:sudo\s+)?(?:rm\s+-\S*r\S*\s+|git\s+(?:reset\s+--hard|clean\s+-)|chmod\s+777|chown\s+-R)/i;
|
|
5
39
|
const packageMutation = /(?:^|[;&|]\s*)(?:npm|pnpm|yarn|bun|pipx?|uv|cargo)\s+(?:install|add|update|upgrade)\b/i;
|
|
@@ -8,8 +42,17 @@ export async function evaluateAiwgPiCommand(
|
|
|
8
42
|
command: string,
|
|
9
43
|
hasUI: boolean,
|
|
10
44
|
confirm?: () => Promise<boolean>,
|
|
45
|
+
projectDir?: string,
|
|
11
46
|
): Promise<{ block: true; reason: string } | undefined> {
|
|
12
47
|
if (!destructive.test(command) && !packageMutation.test(command)) return undefined;
|
|
48
|
+
if (projectDir) {
|
|
49
|
+
// Shell commands have unbounded resources and possible global effects. Never
|
|
50
|
+
// infer a reversible file edit from argv or accept authority in tool input.
|
|
51
|
+
return evaluateAiwgPiAction(projectDir, {
|
|
52
|
+
category: destructive.test(command) ? 'repo.delete' : 'dependency.install',
|
|
53
|
+
repo: basename(projectDir), environment: 'local', resources: [], reversible: false, impact: 'high',
|
|
54
|
+
}, hasUI, confirm);
|
|
55
|
+
}
|
|
13
56
|
if (!hasUI) return { block: true, reason: 'AIWG policy blocked a destructive or package-mutating command in headless mode.' };
|
|
14
57
|
return await confirm?.() ? undefined : { block: true, reason: 'Denied by AIWG operator policy.' };
|
|
15
58
|
}
|
|
@@ -21,6 +64,6 @@ export default function aiwgBridge(pi: ExtensionAPI): void {
|
|
|
21
64
|
return evaluateAiwgPiCommand(command, context.hasUI, async () => {
|
|
22
65
|
const choice = await context.ui.select('AIWG policy requires explicit approval for this command.', ['Deny', 'Allow once']);
|
|
23
66
|
return choice === 'Allow once';
|
|
24
|
-
});
|
|
67
|
+
}, context.cwd);
|
|
25
68
|
});
|
|
26
69
|
}
|
|
@@ -37,6 +37,9 @@ import { auditInductions, renderAudit, backfillFrontmatter, renderBackfill as re
|
|
|
37
37
|
import { buildProfileEdges, renderProfileEdges } from './profile-edges.js';
|
|
38
38
|
import { extractPages, renderExtract, resolveAdapter, rasterizePdf } from './vision-extract.js';
|
|
39
39
|
import { runSnapshot } from './snapshot.js';
|
|
40
|
+
import { resolveCli, RESOLVE_USAGE } from './resolve-cli.js';
|
|
41
|
+
import { expandCli, EXPAND_USAGE } from './expand-cli.js';
|
|
42
|
+
import { exitCodeFor, isAiwgError } from '../../cli/errors.js';
|
|
40
43
|
function flagValue(args, name) {
|
|
41
44
|
const i = args.indexOf(name);
|
|
42
45
|
return i !== -1 && i + 1 < args.length ? args[i + 1] : undefined;
|
|
@@ -47,6 +50,9 @@ function hasFlag(args, name) {
|
|
|
47
50
|
const HELP = `aiwg corpus — research-corpus tools
|
|
48
51
|
|
|
49
52
|
Usage:
|
|
53
|
+
aiwg corpus expand [--seeds REF-a,REF-b | --since YYYY-MM-DD | --all] [--per-seed N] [--from recent|all-cs] [--min-year YYYY] [--reexplore-after 180d] [--max-seeds N] [--overlap] [--write] [--json]
|
|
54
|
+
aiwg corpus resolve "<title | URL | arXiv ID | DOI | bibliography line>" [--year YYYY] [--json]
|
|
55
|
+
aiwg corpus resolve --from-file refs.txt [--year YYYY] [--json]
|
|
50
56
|
aiwg corpus radar-init --ref REF-XXX [--cadence C] [--cluster T] [--write]
|
|
51
57
|
aiwg corpus radar-init --all-missing [--write]
|
|
52
58
|
aiwg corpus radar-status [--stale-only] [--format table|csv|list] [--out PATH]
|
|
@@ -75,6 +81,8 @@ Usage:
|
|
|
75
81
|
aiwg corpus retrieval-lab --queries PATH --concepts PATH [--expected-scheme-hash SHA256] [--limit K] [--latency-ceiling MS] [--json] [--out PATH]
|
|
76
82
|
aiwg corpus vision-extract --slug SLUG [--images DIR] [--out DIR] [--start N] [--end N] [--provider codex|command] [--command TMPL] [--model M] [--retries N] [--title T] [--force] [--rasterize PDF] [--dpi N]
|
|
77
83
|
|
|
84
|
+
resolve verifies paper identities, refuses ambiguous matches (exit 2) and reports unresolved references (exit 3).
|
|
85
|
+
expand queues similar papers by convergence and recency (dry-run unless --write; never inducts or files issues).
|
|
78
86
|
radar-init scaffolds radar sidecars (dry-run unless --write; skips existing).
|
|
79
87
|
radar-status reports overdue radars (most-overdue-first).
|
|
80
88
|
radar-report aggregates corpus/cluster freshness.
|
|
@@ -255,6 +263,30 @@ export async function corpusMain(args, cwd = process.cwd()) {
|
|
|
255
263
|
process.stdout.write(HELP);
|
|
256
264
|
return;
|
|
257
265
|
}
|
|
266
|
+
if (sub === 'resolve' || sub === 'expand') {
|
|
267
|
+
const json = hasFlag(rest, '--json');
|
|
268
|
+
if (hasFlag(rest, '--help') || hasFlag(rest, '-h')) {
|
|
269
|
+
const usage = sub === 'resolve' ? RESOLVE_USAGE : EXPAND_USAGE;
|
|
270
|
+
process.stdout.write(json ? JSON.stringify({ usage }) + '\n' : usage);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
try {
|
|
274
|
+
const root = await resolveCorpusRoot(cwd);
|
|
275
|
+
return await (sub === 'resolve' ? resolveCli(rest, root, cwd) : expandCli(rest, root, cwd));
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
// Semantic failures already emitted their result; only pre-result failures need an envelope.
|
|
279
|
+
const resultEmitted = isAiwgError(error) && ['ERR_CORPUS_RESOLVE_RESULT', 'ERR_CORPUS_EXPAND_RESULT'].includes(error.code);
|
|
280
|
+
if (json && !resultEmitted) {
|
|
281
|
+
process.stdout.write(JSON.stringify({ error: {
|
|
282
|
+
code: isAiwgError(error) ? error.code : 'ERR_CORPUS_COMMAND',
|
|
283
|
+
message: error instanceof Error ? error.message : String(error),
|
|
284
|
+
...(isAiwgError(error) && error.hint ? { hint: error.hint } : {}),
|
|
285
|
+
}, exitCode: exitCodeFor(error) }) + '\n');
|
|
286
|
+
}
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
258
290
|
const root = await resolveCorpusRoot(cwd);
|
|
259
291
|
switch (sub) {
|
|
260
292
|
case 'radar-init':
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import * as fs from 'fs';
|
|
13
13
|
import * as path from 'path';
|
|
14
|
-
const SURFACE_VOCAB = ['x-account', 'x-search', 'x-bookmarks', 'x-foryou', 'x-following', 'rss', 'newsletter', 'web', 'referral', 'direct'];
|
|
14
|
+
const SURFACE_VOCAB = ['x-account', 'x-search', 'x-bookmarks', 'x-foryou', 'x-following', 'rss', 'newsletter', 'web', 'referral', 'direct', 's2-recommendation'];
|
|
15
15
|
/** Render the `discovery:` YAML block (2-space indented under the key). */
|
|
16
16
|
export function renderDiscoveryBlock(f, today) {
|
|
17
17
|
const lines = ['discovery:', ` date: ${f.date || today}`, ` surface: ${f.surface}`];
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Validate expansion controls and expose the corpus router's JSON and exit-code contract.
|
|
2
|
+
import { AiwgError } from '../../cli/errors.js';
|
|
3
|
+
import { expandCorpus, renderExpansion } from './expand.js';
|
|
4
|
+
export const EXPAND_USAGE = 'aiwg corpus expand [--seeds REF-0001,REF-0002 | --since YYYY-MM-DD | --all] [--per-seed 20] [--from recent|all-cs] [--min-year 2018] [--reexplore-after 180d] [--max-seeds N] [--overlap] [--write] [--json]\nA scope (--seeds, --since or --all) is required. Default: dry run. --since uses induction/discovery date, then document mtime.\n--overlap checks at most 20 candidates, after convergence and recency.\nExit codes: 0 complete, 3 incomplete/retryable, 1 usage or storage error.\n';
|
|
5
|
+
function usage(message) {
|
|
6
|
+
throw new AiwgError({ code: 'ERR_CORPUS_EXPAND_USAGE', message, exitCode: 1, hint: EXPAND_USAGE.trim() });
|
|
7
|
+
}
|
|
8
|
+
function positive(value, flag, maximum = Number.MAX_SAFE_INTEGER) {
|
|
9
|
+
if (!/^[1-9]\d*$/.test(value) || !Number.isSafeInteger(Number(value)) || Number(value) > maximum) {
|
|
10
|
+
usage(`${flag} requires a positive integer${maximum < Number.MAX_SAFE_INTEGER ? ` up to ${maximum}` : ''}`);
|
|
11
|
+
}
|
|
12
|
+
return Number(value);
|
|
13
|
+
}
|
|
14
|
+
export async function expandCli(args, root, cwd) {
|
|
15
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
16
|
+
process.stdout.write(EXPAND_USAGE);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const options = { perSeed: 20, from: 'recent', minYear: 2018, reexploreMs: 180 * 86400000,
|
|
20
|
+
maxSeeds: Number.MAX_SAFE_INTEGER, overlap: false, write: false, json: false };
|
|
21
|
+
const seen = new Set();
|
|
22
|
+
let selector;
|
|
23
|
+
for (let i = 0; i < args.length; i++) {
|
|
24
|
+
const flag = args[i];
|
|
25
|
+
if (seen.has(flag))
|
|
26
|
+
usage(`${flag} may only be supplied once`);
|
|
27
|
+
seen.add(flag);
|
|
28
|
+
if (['--seeds', '--since', '--all'].includes(flag)) {
|
|
29
|
+
if (selector)
|
|
30
|
+
usage('--seeds, --since and --all are mutually exclusive');
|
|
31
|
+
selector = flag;
|
|
32
|
+
}
|
|
33
|
+
if (['--write', '--json', '--overlap', '--all'].includes(flag)) {
|
|
34
|
+
if (flag === '--write')
|
|
35
|
+
options.write = true;
|
|
36
|
+
if (flag === '--json')
|
|
37
|
+
options.json = true;
|
|
38
|
+
if (flag === '--overlap')
|
|
39
|
+
options.overlap = true;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (!['--seeds', '--since', '--per-seed', '--from', '--min-year', '--reexplore-after', '--max-seeds'].includes(flag))
|
|
43
|
+
usage(`Unknown expand option: ${flag}`);
|
|
44
|
+
const value = args[++i];
|
|
45
|
+
if (!value || value.startsWith('-'))
|
|
46
|
+
usage(`${flag} requires a value`);
|
|
47
|
+
switch (flag) {
|
|
48
|
+
case '--seeds':
|
|
49
|
+
options.seeds = value.split(',').map(ref => ref.trim());
|
|
50
|
+
if (options.seeds.some(ref => !/^REF-\d+[a-z]?$/.test(ref)))
|
|
51
|
+
usage('--seeds requires comma-separated REF identifiers');
|
|
52
|
+
options.seeds = [...new Set(options.seeds)];
|
|
53
|
+
break;
|
|
54
|
+
case '--since':
|
|
55
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(value) || !Number.isFinite(Date.parse(value)) || new Date(value).toISOString().slice(0, 10) !== value)
|
|
56
|
+
usage('--since requires a valid YYYY-MM-DD date');
|
|
57
|
+
options.since = Date.parse(value);
|
|
58
|
+
break;
|
|
59
|
+
case '--per-seed':
|
|
60
|
+
options.perSeed = positive(value, flag, 500);
|
|
61
|
+
break;
|
|
62
|
+
case '--max-seeds':
|
|
63
|
+
options.maxSeeds = positive(value, flag);
|
|
64
|
+
break;
|
|
65
|
+
case '--min-year':
|
|
66
|
+
if (!/^(?:18|19|20|21)\d{2}$/.test(value))
|
|
67
|
+
usage('--min-year requires a year between 1800 and 2199');
|
|
68
|
+
options.minYear = Number(value);
|
|
69
|
+
break;
|
|
70
|
+
case '--from':
|
|
71
|
+
if (value !== 'recent' && value !== 'all-cs')
|
|
72
|
+
usage('--from requires recent or all-cs');
|
|
73
|
+
options.from = value;
|
|
74
|
+
break;
|
|
75
|
+
case '--reexplore-after':
|
|
76
|
+
if (!/^[1-9]\d*d$/.test(value))
|
|
77
|
+
usage('--reexplore-after requires a positive duration in days, e.g. 180d');
|
|
78
|
+
options.reexploreMs = positive(value.slice(0, -1), flag, Math.floor(Number.MAX_SAFE_INTEGER / 86400000)) * 86400000;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// An explicit scope keeps a bare call from scanning the whole corpus (about 1 request per second without a key).
|
|
82
|
+
if (!selector)
|
|
83
|
+
usage('Choose the seeds: --seeds REF-0001,REF-0002, --since YYYY-MM-DD or --all');
|
|
84
|
+
const result = await expandCorpus(root, cwd, options);
|
|
85
|
+
process.stdout.write(options.json ? JSON.stringify(result, null, 2) + '\n' : renderExpansion(result));
|
|
86
|
+
if (result.incomplete)
|
|
87
|
+
throw new AiwgError({ code: 'ERR_CORPUS_EXPAND_RESULT', message: 'Expansion incomplete; failed lookups will be retried', exitCode: 3 });
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=expand-cli.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Merge recommendation identities and retain auditable convergence, recency and provenance.
|
|
2
|
+
import { identityKeys, normalizeIdentity } from '../../research/identity.js';
|
|
3
|
+
export function rankCandidates(rows) {
|
|
4
|
+
return rows.sort((a, b) => b.score_parts.convergence - a.score_parts.convergence
|
|
5
|
+
|| b.score_parts.recency - a.score_parts.recency
|
|
6
|
+
|| (b.score_parts.overlap ?? -1) - (a.score_parts.overlap ?? -1)
|
|
7
|
+
|| a.paper.id.localeCompare(b.paper.id));
|
|
8
|
+
}
|
|
9
|
+
export class ExpansionQueue {
|
|
10
|
+
rows = new Set();
|
|
11
|
+
byKey = new Map();
|
|
12
|
+
add(paper, refs, previous) {
|
|
13
|
+
const normalized = normalizeIdentity(paper);
|
|
14
|
+
paper = { ...paper, ...Object.fromEntries(Object.entries(normalized).filter(([key, value]) => key !== 'title' && value !== undefined)) };
|
|
15
|
+
const keys = new Set([...identityKeys(paper), ...(previous?.identity_keys ?? [])]);
|
|
16
|
+
const matches = new Set([...keys].flatMap(key => this.byKey.get(key) ? [this.byKey.get(key)] : []));
|
|
17
|
+
const seeds = new Set(refs);
|
|
18
|
+
let overlap = previous?.score_parts.overlap ?? null;
|
|
19
|
+
for (const match of matches) {
|
|
20
|
+
this.rows.delete(match);
|
|
21
|
+
for (const key of match.identity_keys)
|
|
22
|
+
keys.add(key);
|
|
23
|
+
for (const ref of match.seed_refs)
|
|
24
|
+
seeds.add(ref);
|
|
25
|
+
paper = { ...match.paper, ...Object.fromEntries(Object.entries(paper).filter(([, value]) => value !== undefined)) };
|
|
26
|
+
overlap ??= match.score_parts.overlap;
|
|
27
|
+
}
|
|
28
|
+
const seedRefs = [...seeds].sort();
|
|
29
|
+
const row = { paper, identity_keys: [...keys].sort(), seed_refs: seedRefs,
|
|
30
|
+
score_parts: { convergence: seedRefs.length, recency: paper.year || 0, overlap },
|
|
31
|
+
discovery: { surface: 's2-recommendation', via: `S2 recommendations for ${seedRefs.join(', ')}` } };
|
|
32
|
+
this.rows.add(row);
|
|
33
|
+
for (const key of keys)
|
|
34
|
+
this.byKey.set(key, row);
|
|
35
|
+
}
|
|
36
|
+
ranked() { return rankCandidates([...this.rows]); }
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=expand-queue.js.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Read frontier/queue JSONL and checkpoint queue-before-ledger under the artifact path owner.
|
|
2
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
import { resolveProjectAiwgDirForWrite } from '../../config/project-artifacts.js';
|
|
6
|
+
export function expansionPaths(cwd) {
|
|
7
|
+
// Validation is read-only, including in dry runs; external roots must be available.
|
|
8
|
+
const artifactRoot = resolveProjectAiwgDirForWrite(cwd);
|
|
9
|
+
const directory = join(artifactRoot, 'research', 'discovery');
|
|
10
|
+
return { directory, ledger: join(directory, 'expansion-state.jsonl'), queue: join(directory, 'candidates.jsonl'),
|
|
11
|
+
rejected: join(directory, 'rejected-candidates.jsonl'), report: join(directory, 'expansion-report.md') };
|
|
12
|
+
}
|
|
13
|
+
export function readJsonl(file) {
|
|
14
|
+
if (!existsSync(file))
|
|
15
|
+
return [];
|
|
16
|
+
return readFileSync(file, 'utf8').split(/\r?\n/).flatMap((line, index) => {
|
|
17
|
+
if (!line.trim())
|
|
18
|
+
return [];
|
|
19
|
+
try {
|
|
20
|
+
return [JSON.parse(line)];
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
throw new Error(`Invalid JSONL at ${file}:${index + 1}; repair it before expanding`);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
export function writeQueue(paths, rows, report) {
|
|
28
|
+
mkdirSync(paths.directory, { recursive: true });
|
|
29
|
+
for (const [file, content] of [[paths.queue, rows.map(row => JSON.stringify(row)).join('\n') + (rows.length ? '\n' : '')], [paths.report, report]]) {
|
|
30
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
31
|
+
writeFileSync(temporary, content, 'utf8');
|
|
32
|
+
renameSync(temporary, file);
|
|
33
|
+
}
|
|
34
|
+
if (!existsSync(paths.rejected))
|
|
35
|
+
writeFileSync(paths.rejected, '', { flag: 'wx' });
|
|
36
|
+
}
|
|
37
|
+
export function appendState(paths, row) {
|
|
38
|
+
appendFileSync(paths.ledger, JSON.stringify(row) + '\n', 'utf8');
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=expand-state.js.map
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Traverse the due corpus frontier, suppress known papers, and checkpoint ranked recommendations.
|
|
2
|
+
import { statSync } from 'node:fs';
|
|
3
|
+
import { AiwgError } from '../../cli/errors.js';
|
|
4
|
+
import { SemanticScholarClient } from '../../research/clients/semantic-scholar.js';
|
|
5
|
+
import { identityKeys } from '../../research/identity.js';
|
|
6
|
+
import { ResearchError, ResearchErrorCode } from '../../research/types.js';
|
|
7
|
+
import { findInCorpus, loadCorpusIdentities } from './resolve-corpus.js';
|
|
8
|
+
import { ExpansionQueue, rankCandidates } from './expand-queue.js';
|
|
9
|
+
import { appendState, expansionPaths, readJsonl, writeQueue } from './expand-state.js';
|
|
10
|
+
function seedId(record) {
|
|
11
|
+
// Prefer the primary document and citation frontmatter IDs over body URL fallbacks.
|
|
12
|
+
for (const fields of record.fields) {
|
|
13
|
+
const keys = identityKeys(fields);
|
|
14
|
+
const arxiv = keys.find(key => key.startsWith('arxivId:'));
|
|
15
|
+
const doi = keys.find(key => key.startsWith('doi:'));
|
|
16
|
+
if (arxiv)
|
|
17
|
+
return `arXiv:${arxiv.slice(8)}`;
|
|
18
|
+
if (doi)
|
|
19
|
+
return `DOI:${doi.slice(4)}`;
|
|
20
|
+
}
|
|
21
|
+
const arxiv = [...record.keys].find(key => key.startsWith('arxivId:'));
|
|
22
|
+
return arxiv ? `arXiv:${arxiv.slice(8)}` : null;
|
|
23
|
+
}
|
|
24
|
+
function seedDate(record) {
|
|
25
|
+
for (const fields of record.fields) {
|
|
26
|
+
const discovery = fields.discovery;
|
|
27
|
+
for (const value of [fields.inducted, fields.inducted_on, fields.added, fields.created, fields.date, discovery?.date]) {
|
|
28
|
+
const date = value instanceof Date ? value.getTime() : typeof value === 'string' ? Date.parse(value) : NaN;
|
|
29
|
+
if (Number.isFinite(date))
|
|
30
|
+
return date;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return statSync(record.path).mtimeMs;
|
|
34
|
+
}
|
|
35
|
+
export async function expandCorpus(root, cwd, options) {
|
|
36
|
+
const corpus = loadCorpusIdentities(root);
|
|
37
|
+
if (options.seeds?.some(ref => !corpus.some(record => record.ref === ref))) {
|
|
38
|
+
throw new AiwgError({ code: 'ERR_CORPUS_EXPAND_USAGE', message: 'Every --seeds REF must exist in the corpus', exitCode: 1 });
|
|
39
|
+
}
|
|
40
|
+
const paths = expansionPaths(cwd);
|
|
41
|
+
const previous = new Map(readJsonl(paths.ledger).map(row => [row.ref, row]));
|
|
42
|
+
const rejected = new Set(readJsonl(paths.rejected)
|
|
43
|
+
.flatMap(row => [...identityKeys((row.paper ?? row)),
|
|
44
|
+
...(Array.isArray(row.identity_keys) ? row.identity_keys.filter((key) => typeof key === 'string') : [])]));
|
|
45
|
+
const corpusKeys = new Set(corpus.flatMap(record => [...record.keys]));
|
|
46
|
+
const excluded = (paper, aliases = []) => [...identityKeys(paper), ...aliases]
|
|
47
|
+
.some(key => corpusKeys.has(key) || rejected.has(key));
|
|
48
|
+
const queue = new ExpansionQueue();
|
|
49
|
+
for (const row of readJsonl(paths.queue)) {
|
|
50
|
+
if (!excluded(row.paper, row.identity_keys))
|
|
51
|
+
queue.add(row.paper, row.seed_refs, row);
|
|
52
|
+
}
|
|
53
|
+
const scoped = corpus.filter(record => (!options.seeds || options.seeds.includes(record.ref))
|
|
54
|
+
&& (options.since === undefined || seedDate(record) >= options.since));
|
|
55
|
+
const now = Date.now();
|
|
56
|
+
const due = scoped.map(record => ({ record, id: seedId(record) })).filter(({ record, id }) => {
|
|
57
|
+
const last = previous.get(record.ref);
|
|
58
|
+
if (!last || last.seed_id !== id)
|
|
59
|
+
return true;
|
|
60
|
+
if (last.status === 'no-id' && !id)
|
|
61
|
+
return false;
|
|
62
|
+
return last.status !== 'explored' || !Number.isFinite(Date.parse(last.explored_on))
|
|
63
|
+
|| now - Date.parse(last.explored_on) >= options.reexploreMs;
|
|
64
|
+
});
|
|
65
|
+
const client = new SemanticScholarClient();
|
|
66
|
+
const result = { dry_run: !options.write, incomplete: false, scoped: scoped.length, due: due.length,
|
|
67
|
+
remaining: Math.max(0, due.length - options.maxSeeds), processed: [], candidates: queue.ranked(), overlap_errors: [], paths };
|
|
68
|
+
for (const { record, id } of due.slice(0, options.maxSeeds)) {
|
|
69
|
+
const row = { ref: record.ref, seed_id: id, explored_on: new Date().toISOString(),
|
|
70
|
+
status: id ? 'explored' : 'no-id', n_returned: 0 };
|
|
71
|
+
if (id) {
|
|
72
|
+
try {
|
|
73
|
+
const papers = await client.recommendations(id, options.perSeed, options.from);
|
|
74
|
+
row.n_returned = papers.length;
|
|
75
|
+
for (const paper of papers) {
|
|
76
|
+
if (paper.year >= options.minYear && !excluded(paper))
|
|
77
|
+
queue.add(paper, [record.ref]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
row.status = error instanceof ResearchError && error.code === ResearchErrorCode.RF_103 ? 'throttled' : 'not-found';
|
|
82
|
+
row.error = error instanceof Error ? error.message : String(error);
|
|
83
|
+
result.incomplete = true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
result.processed.push(row);
|
|
87
|
+
result.candidates = queue.ranked();
|
|
88
|
+
if (options.write) {
|
|
89
|
+
writeQueue(paths, result.candidates, renderExpansion(result));
|
|
90
|
+
appendState(paths, row);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (options.overlap) {
|
|
94
|
+
for (const candidate of result.candidates.slice(0, 20)) {
|
|
95
|
+
if (candidate.score_parts.overlap !== null)
|
|
96
|
+
continue;
|
|
97
|
+
try {
|
|
98
|
+
const refs = new Set((await client.recommendationReferences(candidate.paper.s2PaperId ?? candidate.paper.id))
|
|
99
|
+
.flatMap(paper => findInCorpus(root, paper, corpus).map(match => match.ref)));
|
|
100
|
+
candidate.score_parts.overlap = refs.size;
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
result.overlap_errors.push({ paper_id: candidate.paper.id, error: error instanceof Error ? error.message : String(error) });
|
|
104
|
+
result.incomplete = true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
rankCandidates(result.candidates);
|
|
108
|
+
}
|
|
109
|
+
if (options.write)
|
|
110
|
+
writeQueue(paths, result.candidates, renderExpansion(result));
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
export function renderExpansion(result) {
|
|
114
|
+
const lines = ['# Corpus expansion', '', `${result.dry_run ? 'Dry run' : 'Written'}: ${result.processed.length}/${result.due} due seeds; ${result.remaining} remaining; ${result.candidates.length} candidates.`, ''];
|
|
115
|
+
for (const row of result.processed)
|
|
116
|
+
lines.push(`- ${row.ref}: ${row.status} (${row.seed_id ?? 'no ID'}), ${row.n_returned} returned${row.error ? ` — ${row.error}` : ''}`);
|
|
117
|
+
lines.push('', 'Ranking: distinct seed convergence, then publication year, then optional in-corpus reference overlap.', '');
|
|
118
|
+
for (const [index, row] of result.candidates.entries()) {
|
|
119
|
+
lines.push(`## ${index + 1}. ${row.paper.title}`, '', `Year: ${row.paper.year}; IDs: ${row.identity_keys.filter(key => !key.startsWith('title:')).join(', ')}`, `Score components: convergence=${row.score_parts.convergence}, recency=${row.score_parts.recency}, overlap=${row.score_parts.overlap ?? 'not checked'}`, `Seed REFs: ${row.seed_refs.join(', ')}`, `Discovery: ${row.discovery.surface}; via: ${row.discovery.via}`, '');
|
|
120
|
+
}
|
|
121
|
+
for (const error of result.overlap_errors)
|
|
122
|
+
lines.push(`Overlap failed for ${error.paper_id}: ${error.error}`);
|
|
123
|
+
return lines.join('\n') + '\n';
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=expand.js.map
|