@fusengine/harness 0.1.80 → 0.1.83
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/dist/adapters/claude/index.d.mts +1 -1
- package/dist/adapters/claude/index.mjs +1 -1
- package/dist/adapters/cline/index.mjs +1 -1
- package/dist/adapters/codex/index.d.mts +11 -12
- package/dist/adapters/codex/index.mjs +3 -3
- package/dist/adapters/cursor/index.d.mts +29 -29
- package/dist/adapters/cursor/index.mjs +1 -1
- package/dist/adapters/gemini/index.mjs +1 -1
- package/dist/adapters/hermes/index.d.mts +22 -23
- package/dist/adapters/hermes/index.mjs +1 -1
- package/dist/adapters/kimi/index.d.mts +54 -46
- package/dist/adapters/kimi/index.mjs +2 -2
- package/dist/cache/index.d.mts +1 -1
- package/dist/{claude-51RSspJ1.mjs → claude-D93HY8dq.mjs} +2 -2
- package/dist/cli/bin.d.mts +1 -1
- package/dist/cli/bin.mjs +129 -38
- package/dist/cli/index.d.mts +3 -3
- package/dist/cli/index.mjs +1 -1
- package/dist/config/index.d.mts +2 -2
- package/dist/config/index.mjs +4 -4
- package/dist/detect/index.d.mts +2 -1
- package/dist/{doc-helpers-CEKzGg2u.d.mts → doc-helpers-BGCzP9VF.d.mts} +12 -12
- package/dist/{dotenv-B26N_uGr.mjs → dotenv-DoqNlALG.mjs} +29 -2
- package/dist/{evaluate-ClfbCDiY.mjs → evaluate-B4-MhOy_.mjs} +322 -32
- package/dist/freshness/index.d.mts +2 -2
- package/dist/{handle-B7YufFz9.mjs → handle-F35Gx-QS.mjs} +596 -174
- package/dist/{hermes-rTmVdhUL.mjs → hermes-1PUEoY8q.mjs} +2 -2
- package/dist/{home-state-BXf38Zi1.mjs → home-state-oUGFB4ds.mjs} +4 -2
- package/dist/index-84heAAm_.d.mts +12 -0
- package/dist/index-CaxrMD1A.d.mts +1 -0
- package/dist/index-CigBu4K9.d.mts +14 -0
- package/dist/{index-DGxNjXW7.d.mts → index-D4C2-IIc.d.mts} +6 -8
- package/dist/index-DBgnIZn9.d.mts +756 -0
- package/dist/{index-mmRF3KNp.d.mts → index-DEfz82BP.d.mts} +12 -13
- package/dist/{index-BUwEmIK-.d.mts → index-DhmV2MPh.d.mts} +31 -31
- package/dist/index-oXXPs2xl.d.mts +119 -0
- package/dist/index-p9fLrSOD.d.mts +27 -0
- package/dist/index.d.mts +11 -11
- package/dist/index.mjs +8 -8
- package/dist/init/index.d.mts +8 -9
- package/dist/init/index.mjs +1 -1
- package/dist/{kimi-C7FGprQz.mjs → kimi-ChbcU2uI.mjs} +17 -3
- package/dist/{layout-C0jaaCQC.mjs → layout-KWoE_Mqn.mjs} +1 -0
- package/dist/limits-DG5pl-5s.mjs +59 -0
- package/dist/memory/index.mjs +1 -1
- package/dist/policy/index.d.mts +2 -3
- package/dist/policy/index.mjs +4 -4
- package/dist/prompt/index.d.mts +2 -1
- package/dist/refs/index.d.mts +1 -1
- package/dist/{registry-CymilZiZ.mjs → registry-IhHk2KlT.mjs} +1 -1
- package/dist/{run-3tyTW9Ud.mjs → run-D_WchZ3R.mjs} +1 -1
- package/dist/{run-Do2JltgU.mjs → run-TAXNRSpD.mjs} +1 -1
- package/dist/runtime/index.d.mts +453 -451
- package/dist/runtime/index.mjs +3 -3
- package/dist/{session-state-CY4iohn_.d.mts → session-state-D5gLr66m.d.mts} +29 -30
- package/dist/{skill-path-CDvR4bbF.mjs → skill-path-DhBGlQe3.mjs} +2 -2
- package/dist/store-Dw08Amqh.mjs +845 -0
- package/dist/tracking/index.d.mts +23 -13
- package/dist/tracking/index.mjs +2 -2
- package/dist/{types-DVbIl9md.d.mts → types-C0-igRKr.d.mts} +8 -8
- package/dist/types-DvXTI71X.d.mts +15 -0
- package/dist/util/index.d.mts +1 -1
- package/dist/{validate-CkmquRDV.mjs → validate-Bf-EsbDh.mjs} +279 -20
- package/package.json +163 -163
- package/dist/apex-Wdi1nq_w.d.mts +0 -73
- package/dist/harness-BQ5_Wnva.d.mts +0 -27
- package/dist/index-BEMumjOw.d.mts +0 -27
- package/dist/index-BIGVNQB8.d.mts +0 -576
- package/dist/index-BKZ67WMa.d.mts +0 -1
- package/dist/index-BOBXQ91y.d.mts +0 -12
- package/dist/index-BVVgDSdq.d.mts +0 -1
- package/dist/index-B_VHbsaJ.d.mts +0 -96
- package/dist/limits-CHn8AIL1.mjs +0 -31
- package/dist/store-nDZ3eLBQ.mjs +0 -349
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { i as HarnessId } from "./harness-BQ5_Wnva.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/config/env.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Robust integer-from-env parser.
|
|
6
|
-
* undefined / empty / whitespace / NaN / float / <= 0 all fall back to `fallback`.
|
|
7
|
-
* `Number("")` is 0, so the empty guard is required before `Number()`.
|
|
8
|
-
*/
|
|
9
|
-
declare function parseEnvInt(raw: string | undefined, fallback: number): number;
|
|
10
|
-
//#endregion
|
|
11
|
-
//#region src/config/ttl.d.ts
|
|
12
|
-
/**
|
|
13
|
-
* Default enforcement-freshness window, in seconds (2 minutes). Matches the
|
|
14
|
-
* plugin's original `FUSE_ENFORCE_TTL_SEC` default. This is the constant that
|
|
15
|
-
* actually reaches production (via `resolveTtlSec` -> `bin.ts`); `gate.ts`'s
|
|
16
|
-
* `DEFAULT_WINDOW_MS` is only a fallback for direct programmatic callers that
|
|
17
|
-
* omit `windowMs` (e.g. tests) and never applies on the real CLI path.
|
|
18
|
-
*/
|
|
19
|
-
declare const DEFAULT_TTL_SEC = 120;
|
|
20
|
-
/** Default env var name carrying the TTL override. */
|
|
21
|
-
declare const TTL_ENV_KEY = "FUSE_ENFORCE_TTL_SEC";
|
|
22
|
-
/**
|
|
23
|
-
* Resolve the enforcement TTL (seconds) from an env map.
|
|
24
|
-
* @param env - environment map (defaults to `process.env`)
|
|
25
|
-
* @param key - env var name (defaults to `FUSE_ENFORCE_TTL_SEC`)
|
|
26
|
-
*/
|
|
27
|
-
declare function resolveTtlSec(env?: Record<string, string | undefined>, key?: string): number;
|
|
28
|
-
/** Human label for a TTL: 120 -> "2min", 240 -> "4min", 90 -> "90s". */
|
|
29
|
-
declare function ttlLabel(sec: number): string;
|
|
30
|
-
//#endregion
|
|
31
|
-
//#region src/config/limits.d.ts
|
|
32
|
-
/** Default SOLID max lines per file. */
|
|
33
|
-
declare const DEFAULT_MAX_LINES = 100;
|
|
34
|
-
/** Default env var name carrying the max-lines override. */
|
|
35
|
-
declare const MAX_LINES_ENV_KEY = "FUSE_SOLID_MAX_LINES";
|
|
36
|
-
/**
|
|
37
|
-
* Resolve the SOLID max-lines limit from an env map.
|
|
38
|
-
* @param env - environment map (defaults to `process.env`)
|
|
39
|
-
* @param key - env var name (defaults to `FUSE_SOLID_MAX_LINES`)
|
|
40
|
-
*/
|
|
41
|
-
declare function resolveMaxLines(env?: Record<string, string | undefined>, key?: string): number;
|
|
42
|
-
/** Advisory module-split headroom = `maxLines - 10` (never below 1). */
|
|
43
|
-
declare function splitTarget(maxLines: number): number;
|
|
44
|
-
//#endregion
|
|
45
|
-
//#region src/config/layout.d.ts
|
|
46
|
-
/** Top-level project state dir — neutral + harness-agnostic (not `.claude/…`). */
|
|
47
|
-
declare const STATE_ROOT = ".harness";
|
|
48
|
-
/**
|
|
49
|
-
* The selective `.gitignore` for `.harness/`: ignore the machine-local session
|
|
50
|
-
* state (track, cache, throttle) but keep the curated `memory/LESSON.md`.
|
|
51
|
-
*/
|
|
52
|
-
declare const STATE_GITIGNORE = "track/\ncache/\nmemory/state.json\n";
|
|
53
|
-
/** Every project-state path the package uses, derived from one project root. */
|
|
54
|
-
interface ProjectLayout {
|
|
55
|
-
root: string;
|
|
56
|
-
/** `<root>/.harness` — the single state dir. */
|
|
57
|
-
stateDir: string;
|
|
58
|
-
/** `<root>/.harness/track` — session track files. */
|
|
59
|
-
trackDir: string;
|
|
60
|
-
/** `<root>/.harness/cache` — MCP/WebFetch cache. */
|
|
61
|
-
cacheDir: string;
|
|
62
|
-
/** `<root>/.harness/memory` — lessons + throttle. */
|
|
63
|
-
memoryDir: string;
|
|
64
|
-
/** `<root>/.harness/memory/LESSON.md` — curated, committable. */
|
|
65
|
-
lessonsFile: string;
|
|
66
|
-
/** `<root>/.harness/memory/state.json` — machine-local throttle. */
|
|
67
|
-
memoryStateFile: string;
|
|
68
|
-
/** `<root>/.harness/.gitignore`. */
|
|
69
|
-
gitignoreFile: string;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* The single source of truth for where every package file lives under a project
|
|
73
|
-
* root. All modules (runtime storage, memory, init) derive their paths here —
|
|
74
|
-
* nothing hardcodes a directory name elsewhere.
|
|
75
|
-
*/
|
|
76
|
-
declare function projectLayout(root: string): ProjectLayout;
|
|
77
|
-
//#endregion
|
|
78
|
-
//#region src/config/dotenv.d.ts
|
|
79
|
-
/** Home config dir holding the `.env` for each harness (defaults to `.claude`). */
|
|
80
|
-
declare const HOME_DIR: Partial<Record<HarnessId, string>>;
|
|
81
|
-
/** Parse a `.env` file into a key→value map (`export KEY="v"` or `KEY=v`). */
|
|
82
|
-
declare function parseEnvFile(path: string): Record<string, string>;
|
|
83
|
-
/** The `.env` paths probed for a harness: home `.env` then `<cwd>/.env`. */
|
|
84
|
-
declare function envCandidates(id: HarnessId, home?: string, cwd?: string): string[];
|
|
85
|
-
/**
|
|
86
|
-
* Load `.env` files into `env` without overwriting existing keys. Reads the
|
|
87
|
-
* harness home `.env` then `<cwd>/.env`. Best-effort (missing/unreadable files
|
|
88
|
-
* are skipped) so a hook never fails on env loading.
|
|
89
|
-
* @param id - Detected harness id (selects the home dir).
|
|
90
|
-
* @param env - Target environment (defaults to `process.env`).
|
|
91
|
-
* @param home - Home dir.
|
|
92
|
-
* @param cwd - Project root.
|
|
93
|
-
*/
|
|
94
|
-
declare function loadDotenv(id: HarnessId, env?: NodeJS.ProcessEnv, home?: string, cwd?: string): void;
|
|
95
|
-
//#endregion
|
|
96
|
-
export { parseEnvInt as _, ProjectLayout as a, projectLayout as c, resolveMaxLines as d, splitTarget as f, ttlLabel as g, resolveTtlSec as h, parseEnvFile as i, DEFAULT_MAX_LINES as l, TTL_ENV_KEY as m, envCandidates as n, STATE_GITIGNORE as o, DEFAULT_TTL_SEC as p, loadDotenv as r, STATE_ROOT as s, HOME_DIR as t, MAX_LINES_ENV_KEY as u };
|
package/dist/limits-CHn8AIL1.mjs
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
//#region src/config/env.ts
|
|
2
|
-
/**
|
|
3
|
-
* Robust integer-from-env parser.
|
|
4
|
-
* undefined / empty / whitespace / NaN / float / <= 0 all fall back to `fallback`.
|
|
5
|
-
* `Number("")` is 0, so the empty guard is required before `Number()`.
|
|
6
|
-
*/
|
|
7
|
-
function parseEnvInt(raw, fallback) {
|
|
8
|
-
if (!raw?.trim()) return fallback;
|
|
9
|
-
const n = Number(raw.trim());
|
|
10
|
-
return Number.isInteger(n) && n > 0 ? n : fallback;
|
|
11
|
-
}
|
|
12
|
-
//#endregion
|
|
13
|
-
//#region src/config/limits.ts
|
|
14
|
-
/** Default SOLID max lines per file. */
|
|
15
|
-
const DEFAULT_MAX_LINES = 100;
|
|
16
|
-
/** Default env var name carrying the max-lines override. */
|
|
17
|
-
const MAX_LINES_ENV_KEY = "FUSE_SOLID_MAX_LINES";
|
|
18
|
-
/**
|
|
19
|
-
* Resolve the SOLID max-lines limit from an env map.
|
|
20
|
-
* @param env - environment map (defaults to `process.env`)
|
|
21
|
-
* @param key - env var name (defaults to `FUSE_SOLID_MAX_LINES`)
|
|
22
|
-
*/
|
|
23
|
-
function resolveMaxLines(env = process.env, key = MAX_LINES_ENV_KEY) {
|
|
24
|
-
return parseEnvInt(env[key], 100);
|
|
25
|
-
}
|
|
26
|
-
/** Advisory module-split headroom = `maxLines - 10` (never below 1). */
|
|
27
|
-
function splitTarget(maxLines) {
|
|
28
|
-
return Math.max(maxLines - 10, 1);
|
|
29
|
-
}
|
|
30
|
-
//#endregion
|
|
31
|
-
export { parseEnvInt as a, splitTarget as i, MAX_LINES_ENV_KEY as n, resolveMaxLines as r, DEFAULT_MAX_LINES as t };
|
package/dist/store-nDZ3eLBQ.mjs
DELETED
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
import { c as ttlLabel } from "./dotenv-B26N_uGr.mjs";
|
|
2
|
-
import { n as fuseHarnessHome } from "./home-state-BXf38Zi1.mjs";
|
|
3
|
-
import { a as writeJsonFile, i as readJsonFile } from "./json-io-DisYd2fb.mjs";
|
|
4
|
-
import { i as resolveSessions } from "./doc-helpers-CWZegVdR.mjs";
|
|
5
|
-
import { t as routeReferences } from "./router-PKVNBHge.mjs";
|
|
6
|
-
import { t as resolveSkillPath } from "./skill-path-CDvR4bbF.mjs";
|
|
7
|
-
import { join } from "node:path";
|
|
8
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
|
-
import { createHmac, randomBytes } from "node:crypto";
|
|
10
|
-
//#region src/policy/skill-source.ts
|
|
11
|
-
/**
|
|
12
|
-
* Framework -> SKILL.md doc source for the APEX Check-1 non-routed deny —
|
|
13
|
-
* parity with the Python plugin's enforce-helpers.ts `getSkillSource()`
|
|
14
|
-
* (10-entry map, fallback "mcp__context7__query-docs"). The map is ported
|
|
15
|
-
* faithfully as skill NAMES only: disk paths are resolved DYNAMICALLY via
|
|
16
|
-
* {@link resolveSkillPath} (proprietary rule — no static path table; installed
|
|
17
|
-
* plugin dir names differ from upstream's `react-expert/...`, and a hardcoded
|
|
18
|
-
* table drifts from what is actually installed).
|
|
19
|
-
*/
|
|
20
|
-
/**
|
|
21
|
-
* Fallback doc source when no SKILL.md can be promised: the consigne to query
|
|
22
|
-
* context7 (a TOOL name, not a path) — parity enforce-helpers.ts:51.
|
|
23
|
-
*/
|
|
24
|
-
const CONTEXT7_SOURCE = "mcp__context7__query-docs";
|
|
25
|
-
/**
|
|
26
|
-
* Framework -> sub-skill dir name, faithful to the Python
|
|
27
|
-
* `getSkillSource` map (enforce-helpers.ts:39-50, all 10 entries).
|
|
28
|
-
*/
|
|
29
|
-
const SKILL_NAMES = {
|
|
30
|
-
react: "react-19",
|
|
31
|
-
nextjs: "nextjs-16",
|
|
32
|
-
swift: "swiftui-components",
|
|
33
|
-
laravel: "laravel-eloquent",
|
|
34
|
-
tailwind: "tailwindcss-v4",
|
|
35
|
-
generic: "solid-generic",
|
|
36
|
-
java: "solid-java",
|
|
37
|
-
go: "solid-go",
|
|
38
|
-
ruby: "solid-ruby",
|
|
39
|
-
rust: "solid-rust"
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* The doc source a Check-1 deny may promise for `framework` (parity
|
|
43
|
-
* `getSkillSource`): the REAL installed SKILL.md absolute path when it exists
|
|
44
|
-
* on disk, else {@link CONTEXT7_SOURCE}. `resolveSkillPath` returns a
|
|
45
|
-
* `/SKILL.md`-suffixed path ONLY on an `existsSync` hit; its
|
|
46
|
-
* `${PLUGINS_DIR}/<name>` not-found fallback (no marketplace installed, or a
|
|
47
|
-
* mapped skill absent on disk — e.g. upstream's `swiftui-components`) is
|
|
48
|
-
* converted to the context7 consigne here, because a deny must never promise
|
|
49
|
-
* a nonexistent path (every option a deny offers must actually unblock).
|
|
50
|
-
* @param framework - Detected framework (the `detectFramework()` domain).
|
|
51
|
-
* @param pluginsDirOverride - Marketplace plugins dir override (tests).
|
|
52
|
-
*/
|
|
53
|
-
function getSkillSource(framework, pluginsDirOverride) {
|
|
54
|
-
const name = SKILL_NAMES[framework];
|
|
55
|
-
if (!name) return CONTEXT7_SOURCE;
|
|
56
|
-
const path = resolveSkillPath(name, pluginsDirOverride);
|
|
57
|
-
return path.endsWith("/SKILL.md") ? path : CONTEXT7_SOURCE;
|
|
58
|
-
}
|
|
59
|
-
//#endregion
|
|
60
|
-
//#region src/policy/apex-authorization.ts
|
|
61
|
-
/**
|
|
62
|
-
* Credit a doc consultation onto one framework's auth entry (parity
|
|
63
|
-
* track-doc-consultation.py): the once-per-session Check-2 fields
|
|
64
|
-
* (`doc_sessions` + `sources`) plus the per-framework Check-1 stamp
|
|
65
|
-
* (`sessions` + `doc_consulted`). Pure.
|
|
66
|
-
*/
|
|
67
|
-
function creditDocConsultation(prev, sessionId, source, nowIso) {
|
|
68
|
-
const p = prev ?? {};
|
|
69
|
-
const add = (list, v) => v && !list.includes(v) ? [...list, v] : list;
|
|
70
|
-
return {
|
|
71
|
-
...p,
|
|
72
|
-
sessions: add(resolveSessions(p), sessionId),
|
|
73
|
-
doc_sessions: add(p.doc_sessions ?? [], sessionId),
|
|
74
|
-
sources: add(p.sources ?? (p.source ? [p.source] : []), source),
|
|
75
|
-
doc_consulted: nowIso
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Check 1 (parity enforce-apex-phases.ts isAuthorized): the framework's doc or
|
|
80
|
-
* skill was consulted in THIS session (resolveSessions) AND the stamp is
|
|
81
|
-
* younger than the TTL (FUSE_ENFORCE_TTL_SEC via src/config/ttl.ts).
|
|
82
|
-
*/
|
|
83
|
-
function isAuthorized(auth, sessionId, now, ttlMs) {
|
|
84
|
-
if (!auth?.doc_consulted || !resolveSessions(auth).includes(sessionId)) return false;
|
|
85
|
-
const readEpoch = Date.parse(auth.doc_consulted);
|
|
86
|
-
return !Number.isNaN(readEpoch) && now - readEpoch < ttlMs;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* A fresh same-session `skills/**.md` read whose path names the framework also
|
|
90
|
-
* satisfies Check 1 (parity track-doc-consultation.py, which stamps
|
|
91
|
-
* `doc_consulted` on `skills/.*\.md` Reads; the harness records those Reads in
|
|
92
|
-
* `refsRead`/`refsReadAt` instead, and the track file is per-session).
|
|
93
|
-
*/
|
|
94
|
-
function skillReadAuthorized(ctx, now, ttlMs) {
|
|
95
|
-
const nameRe = new RegExp(`\\b(${{
|
|
96
|
-
laravel: "laravel|php",
|
|
97
|
-
tailwind: "tailwindcss?"
|
|
98
|
-
}[ctx.framework] ?? ctx.framework})\\b`, "i");
|
|
99
|
-
return (ctx.refsRead ?? []).some((p) => {
|
|
100
|
-
if (!/skills\/.*\.md$/i.test(p) || !nameRe.test(p)) return false;
|
|
101
|
-
const at = ctx.refsReadAt?.[p];
|
|
102
|
-
return at !== void 0 && now - at < ttlMs;
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Gate (Check 1): deny until the framework's doc/skill is freshly consulted.
|
|
107
|
-
* The deny lists the routed SOLID refs when refs are loaded (parity
|
|
108
|
-
* formatRoutedDeny), else the framework's skill source (parity getSkillSource:
|
|
109
|
-
* a real installed SKILL.md path, or the context7 consigne). The RUNTIME
|
|
110
|
-
* caller persists `state.target` on deny (recordTarget, parity :80-81) so
|
|
111
|
-
* recordDoc can cross-credit this framework on the next doc consultation.
|
|
112
|
-
*/
|
|
113
|
-
const apexAuthorizationGate = (ctx) => {
|
|
114
|
-
const windowMs = ctx.windowMs ?? 120 * 1e3;
|
|
115
|
-
const now = ctx.now ?? Date.now();
|
|
116
|
-
if (isAuthorized(ctx.authorizations?.[ctx.framework], ctx.sessionId, now, windowMs)) return null;
|
|
117
|
-
if (skillReadAuthorized(ctx, now, windowMs)) return null;
|
|
118
|
-
const ttl = ttlLabel(Math.round(windowMs / 1e3));
|
|
119
|
-
const title = `APEX: ${ctx.framework} documentation required`;
|
|
120
|
-
const routed = ctx.refs?.length ? routeReferences(ctx.refs, ctx.filePath, ctx.content) : null;
|
|
121
|
-
if (!routed) {
|
|
122
|
-
const src = getSkillSource(ctx.framework);
|
|
123
|
-
return {
|
|
124
|
-
kind: "block",
|
|
125
|
-
title,
|
|
126
|
-
reason: `APEX: Read doc first (expires every ${ttl}) for ${ctx.framework}! Source: ${src}`,
|
|
127
|
-
actions: [src === "mcp__context7__query-docs" ? `Query ${CONTEXT7_SOURCE} for ${ctx.framework} docs` : `Read ${src}`]
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
const optional = routed.optional.map((r) => r.meta.filePath);
|
|
131
|
-
return {
|
|
132
|
-
kind: "block",
|
|
133
|
-
title,
|
|
134
|
-
reason: [
|
|
135
|
-
`APEX: Read specific SOLID references (expires every ${ttl}) for ${ctx.framework}.`,
|
|
136
|
-
`Editing: ${ctx.filePath}`,
|
|
137
|
-
...optional.length ? [`Optional: ${optional.join(", ")}`] : [],
|
|
138
|
-
`Full skill: ${routed.skillPath}`
|
|
139
|
-
].join("\n"),
|
|
140
|
-
actions: routed.required.map((r) => r.meta.filePath)
|
|
141
|
-
};
|
|
142
|
-
};
|
|
143
|
-
//#endregion
|
|
144
|
-
//#region src/tracking/session-state.ts
|
|
145
|
-
/** A fresh, empty track. */
|
|
146
|
-
function emptyTrack() {
|
|
147
|
-
return {
|
|
148
|
-
authorizations: {},
|
|
149
|
-
refsRead: [],
|
|
150
|
-
agents: [],
|
|
151
|
-
trivialEdits: []
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Record a doc consultation (Context7/Exa/web) for a framework in this session:
|
|
156
|
-
* the Check-2 credit (`doc_sessions` + `sources`) plus the Check-1 stamp
|
|
157
|
-
* (`sessions` + `doc_consulted` = ISO of `now`). PARITY track-doc-consultation.py
|
|
158
|
-
* :62-70 — a `target` left by a Check-1 deny (enforce-apex-phases.ts:80) is
|
|
159
|
-
* cross-credited on EVERY consultation, with NO TTL on `target.set_at`, and the
|
|
160
|
-
* target PERSISTS (only the next deny replaces it via {@link recordTarget});
|
|
161
|
-
* single-shot clearing or TTL-gating the target re-opened the deny loop when a
|
|
162
|
-
* consultation landed > TTL after the deny. Immutable.
|
|
163
|
-
*/
|
|
164
|
-
function recordDoc(track, framework, sessionId, source, now = Date.now()) {
|
|
165
|
-
const nowIso = new Date(now).toISOString();
|
|
166
|
-
const authorizations = {
|
|
167
|
-
...track.authorizations,
|
|
168
|
-
[framework]: creditDocConsultation(track.authorizations[framework], sessionId, source, nowIso)
|
|
169
|
-
};
|
|
170
|
-
const t = track.target;
|
|
171
|
-
if (t && t.framework !== framework) authorizations[t.framework] = creditDocConsultation(track.authorizations[t.framework], sessionId, source, nowIso);
|
|
172
|
-
return {
|
|
173
|
-
...track,
|
|
174
|
-
authorizations
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
/** Set the pending doc-credit target (written by the runtime on a Check-1 deny). Immutable. */
|
|
178
|
-
function recordTarget(track, target) {
|
|
179
|
-
return {
|
|
180
|
-
...track,
|
|
181
|
-
target
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Record that a SOLID reference file was read (deduped). Immutable. When `now`
|
|
186
|
-
* (epoch ms — the tool event's own timestamp, never a fresh `Date.now()`) is
|
|
187
|
-
* supplied, the read is stamped in `refsReadAt`, refreshed on re-reads so the
|
|
188
|
-
* LATEST read drives the SOLID TTL (parity track-solid-reads.py, which appends
|
|
189
|
-
* a timestamped entry per read; require-solid-read.py checks the most recent).
|
|
190
|
-
* Callers that omit `now` keep the legacy untimestamped behavior.
|
|
191
|
-
*/
|
|
192
|
-
function recordRefRead(track, path, now) {
|
|
193
|
-
const refsRead = track.refsRead.includes(path) ? track.refsRead : [...track.refsRead, path];
|
|
194
|
-
if (now === void 0) return refsRead === track.refsRead ? track : {
|
|
195
|
-
...track,
|
|
196
|
-
refsRead
|
|
197
|
-
};
|
|
198
|
-
return {
|
|
199
|
-
...track,
|
|
200
|
-
refsRead,
|
|
201
|
-
refsReadAt: {
|
|
202
|
-
...track.refsReadAt,
|
|
203
|
-
[path]: now
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
/** Record an agent/tool call with a timestamp + optional quality. Immutable. */
|
|
208
|
-
function recordAgent(track, name, ts, quality) {
|
|
209
|
-
const entry = quality ? {
|
|
210
|
-
name,
|
|
211
|
-
ts,
|
|
212
|
-
quality
|
|
213
|
-
} : {
|
|
214
|
-
name,
|
|
215
|
-
ts
|
|
216
|
-
};
|
|
217
|
-
return {
|
|
218
|
-
...track,
|
|
219
|
-
agents: [...track.agents, entry]
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
/** True when ALL of `names` ran within `windowMs` with non-insufficient quality. */
|
|
223
|
-
function agentsFresh(track, names, windowMs, now) {
|
|
224
|
-
const cutoff = now - windowMs;
|
|
225
|
-
return names.every((n) => track.agents.some((a) => a.name === n && a.ts > cutoff && a.quality !== "insufficient"));
|
|
226
|
-
}
|
|
227
|
-
/** Record a trivial edit timestamp (sliding window; old evicted). Immutable. */
|
|
228
|
-
function recordTrivialEdit(track, ts, windowMs, now) {
|
|
229
|
-
const cutoff = now - windowMs;
|
|
230
|
-
return {
|
|
231
|
-
...track,
|
|
232
|
-
trivialEdits: [...(track.trivialEdits ?? []).filter((t) => t > cutoff), ts]
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
/** Count trivial edits within the sliding window. */
|
|
236
|
-
function trivialCount(track, windowMs, now) {
|
|
237
|
-
const cutoff = now - windowMs;
|
|
238
|
-
return (track.trivialEdits ?? []).filter((t) => t > cutoff).length;
|
|
239
|
-
}
|
|
240
|
-
/** Set the brainstorm-required flag (from creation-intent detection). Immutable. */
|
|
241
|
-
function recordBrainstormRequired(track, required) {
|
|
242
|
-
return {
|
|
243
|
-
...track,
|
|
244
|
-
brainstormRequired: required
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
//#endregion
|
|
248
|
-
//#region src/tracking/integrity.ts
|
|
249
|
-
/**
|
|
250
|
-
* @module integrity
|
|
251
|
-
* HMAC-SHA256 tamper-evident wrapping for {@link SessionTrack}.
|
|
252
|
-
*
|
|
253
|
-
* Failure policy: ONLY a MAC mismatch causes fail-closed (returns null).
|
|
254
|
-
* The nonce is included in the signed payload and written to disk for advisory
|
|
255
|
-
* diagnostics, but is NOT checked during verification — concurrent hook
|
|
256
|
-
* invocations (PostToolUse, SubagentStop, …) legitimately load the same
|
|
257
|
-
* envelope after the nonce watermark advances; a monotonic check would trigger
|
|
258
|
-
* spurious fail-closed behaviour mid-session.
|
|
259
|
-
*
|
|
260
|
-
* The machine key is readable by the same agent process, so this deters naive
|
|
261
|
-
* out-of-band tampering — not a determined re-sign. Item B (transcript-grounded
|
|
262
|
-
* freshness) is the primary guarantee.
|
|
263
|
-
* @packageDocumentation
|
|
264
|
-
*/
|
|
265
|
-
const HARNESS_DIR = fuseHarnessHome();
|
|
266
|
-
const KEY_PATH = join(HARNESS_DIR, ".key");
|
|
267
|
-
const NONCE_PATH = join(HARNESS_DIR, ".nonce");
|
|
268
|
-
/** Load (or create on first use) the per-machine HMAC key stored at mode 0600. */
|
|
269
|
-
function loadOrCreateKey() {
|
|
270
|
-
mkdirSync(HARNESS_DIR, { recursive: true });
|
|
271
|
-
if (!existsSync(KEY_PATH)) {
|
|
272
|
-
const key = randomBytes(32).toString("hex");
|
|
273
|
-
writeFileSync(KEY_PATH, key, {
|
|
274
|
-
encoding: "utf8",
|
|
275
|
-
mode: 384
|
|
276
|
-
});
|
|
277
|
-
return key;
|
|
278
|
-
}
|
|
279
|
-
return readFileSync(KEY_PATH, "utf8").trim();
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Persist the last-seen nonce for advisory diagnostics (mode 0600).
|
|
283
|
-
* Never read during {@link verifyTrack} — see module JSDoc for rationale.
|
|
284
|
-
*/
|
|
285
|
-
function writeLastNonce(nonce) {
|
|
286
|
-
mkdirSync(HARNESS_DIR, { recursive: true });
|
|
287
|
-
writeFileSync(NONCE_PATH, String(nonce), {
|
|
288
|
-
encoding: "utf8",
|
|
289
|
-
mode: 384
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
/** Compute HMAC-SHA256 over the canonical message `"${nonce}:${data}"`. */
|
|
293
|
-
function computeMac(key, data, nonce) {
|
|
294
|
-
return createHmac("sha256", key).update(`${nonce}:${data}`).digest("hex");
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Sign a {@link SessionTrack} into a tamper-evident envelope. The nonce
|
|
298
|
-
* (`Date.now()`) is embedded in the MAC to bind the timestamp to the payload.
|
|
299
|
-
*/
|
|
300
|
-
function signTrack(track) {
|
|
301
|
-
const key = loadOrCreateKey();
|
|
302
|
-
const data = JSON.stringify(track);
|
|
303
|
-
const nonce = Date.now();
|
|
304
|
-
return {
|
|
305
|
-
data,
|
|
306
|
-
nonce,
|
|
307
|
-
mac: computeMac(key, data, nonce)
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
/**
|
|
311
|
-
* Verify a {@link TrackEnvelope}. Returns the parsed {@link SessionTrack} on
|
|
312
|
-
* success, or `null` ONLY on MAC mismatch / parse failure (fail-closed on
|
|
313
|
-
* tampering). The nonce is NOT checked — see module JSDoc.
|
|
314
|
-
*/
|
|
315
|
-
function verifyTrack(envelope) {
|
|
316
|
-
try {
|
|
317
|
-
const key = loadOrCreateKey();
|
|
318
|
-
if (envelope.mac !== computeMac(key, envelope.data, envelope.nonce)) return null;
|
|
319
|
-
return JSON.parse(envelope.data);
|
|
320
|
-
} catch {
|
|
321
|
-
return null;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
//#endregion
|
|
325
|
-
//#region src/tracking/store.ts
|
|
326
|
-
/**
|
|
327
|
-
* Load and verify a session track from a signed envelope file.
|
|
328
|
-
*
|
|
329
|
-
* Returns {@link emptyTrack} (fail closed) when the file is absent, corrupt, or
|
|
330
|
-
* fails MAC validation — so the gates re-require real agents rather than trust a
|
|
331
|
-
* forged track. Only a MAC mismatch triggers fail-closed; the nonce is advisory
|
|
332
|
-
* and is never checked during load.
|
|
333
|
-
*/
|
|
334
|
-
async function loadTrack(file) {
|
|
335
|
-
const envelope = await readJsonFile(file);
|
|
336
|
-
if (!envelope) return emptyTrack();
|
|
337
|
-
return verifyTrack(envelope) ?? emptyTrack();
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* Sign and persist a session track as a tamper-evident envelope, then write the
|
|
341
|
-
* advisory nonce watermark.
|
|
342
|
-
*/
|
|
343
|
-
async function saveTrack(file, track) {
|
|
344
|
-
const envelope = signTrack(track);
|
|
345
|
-
await writeJsonFile(file, envelope);
|
|
346
|
-
writeLastNonce(envelope.nonce);
|
|
347
|
-
}
|
|
348
|
-
//#endregion
|
|
349
|
-
export { writeLastNonce as a, recordAgent as c, recordRefRead as d, recordTarget as f, apexAuthorizationGate as h, verifyTrack as i, recordBrainstormRequired as l, trivialCount as m, saveTrack as n, agentsFresh as o, recordTrivialEdit as p, signTrack as r, emptyTrack as s, loadTrack as t, recordDoc as u };
|