harness-reflex 0.1.0 → 0.3.0
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/CHANGELOG.md +20 -0
- package/README.md +103 -23
- package/USER_GUIDE.md +121 -43
- package/dist/adapters/agy.js +1 -1
- package/dist/adapters/agy.js.map +1 -1
- package/dist/adapters/codex.d.ts +133 -4
- package/dist/adapters/codex.js +325 -2
- package/dist/adapters/codex.js.map +1 -1
- package/dist/backend/defaults.d.ts +31 -0
- package/dist/backend/defaults.js +32 -0
- package/dist/backend/defaults.js.map +1 -0
- package/dist/backend/jev.js +6 -5
- package/dist/backend/jev.js.map +1 -1
- package/dist/backend/mock.d.ts +12 -1
- package/dist/backend/mock.js +34 -0
- package/dist/backend/mock.js.map +1 -1
- package/dist/backend/simple-jev.js +5 -4
- package/dist/backend/simple-jev.js.map +1 -1
- package/dist/backend/types.d.ts +34 -0
- package/dist/backend/types.js.map +1 -1
- package/dist/cli/index.js +5 -3
- package/dist/cli/index.js.map +1 -1
- package/dist/core/closing.d.ts +90 -0
- package/dist/core/closing.js +111 -0
- package/dist/core/closing.js.map +1 -0
- package/dist/core/evidence.d.ts +119 -0
- package/dist/core/evidence.js +209 -0
- package/dist/core/evidence.js.map +1 -0
- package/dist/core/judge.d.ts +221 -0
- package/dist/core/judge.js +251 -0
- package/dist/core/judge.js.map +1 -0
- package/dist/core/orchestrate-closing.d.ts +237 -0
- package/dist/core/orchestrate-closing.js +250 -0
- package/dist/core/orchestrate-closing.js.map +1 -0
- package/dist/core/orchestrate-correction.d.ts +209 -0
- package/dist/core/orchestrate-correction.js +141 -0
- package/dist/core/orchestrate-correction.js.map +1 -0
- package/dist/core/orchestrate-host.d.ts +225 -0
- package/dist/core/orchestrate-host.js +80 -0
- package/dist/core/orchestrate-host.js.map +1 -0
- package/dist/core/orchestrate-stagnation.d.ts +259 -0
- package/dist/core/orchestrate-stagnation.js +270 -0
- package/dist/core/orchestrate-stagnation.js.map +1 -0
- package/dist/core/orchestrate.d.ts +357 -0
- package/dist/core/orchestrate.js +418 -0
- package/dist/core/orchestrate.js.map +1 -0
- package/dist/core/policy.d.ts +122 -0
- package/dist/core/policy.js +102 -0
- package/dist/core/policy.js.map +1 -0
- package/dist/core/prune-plan.d.ts +141 -0
- package/dist/core/prune-plan.js +390 -0
- package/dist/core/prune-plan.js.map +1 -0
- package/dist/core/score.d.ts +2 -2
- package/dist/core/score.js.map +1 -1
- package/dist/core/text.d.ts +36 -0
- package/dist/core/text.js +66 -0
- package/dist/core/text.js.map +1 -0
- package/dist/hook/cli.d.ts +9 -0
- package/dist/hook/cli.js +52 -0
- package/dist/hook/cli.js.map +1 -0
- package/dist/hook/contract.d.ts +125 -0
- package/dist/hook/contract.js +159 -0
- package/dist/hook/contract.js.map +1 -0
- package/dist/hook/decision.d.ts +97 -0
- package/dist/hook/decision.js +955 -0
- package/dist/hook/decision.js.map +1 -0
- package/dist/hook/memory-store.d.ts +81 -0
- package/dist/hook/memory-store.js +157 -0
- package/dist/hook/memory-store.js.map +1 -0
- package/dist/hook/payload.d.ts +55 -0
- package/dist/hook/payload.js +129 -0
- package/dist/hook/payload.js.map +1 -0
- package/dist/hook/run.d.ts +48 -0
- package/dist/hook/run.js +218 -0
- package/dist/hook/run.js.map +1 -0
- package/dist/hook/store-port.d.ts +72 -0
- package/dist/hook/store-port.js +2 -0
- package/dist/hook/store-port.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/ports/audit.d.ts +18 -0
- package/dist/ports/audit.js +11 -0
- package/dist/ports/audit.js.map +1 -0
- package/dist/ports/clock.d.ts +14 -0
- package/dist/ports/clock.js +11 -0
- package/dist/ports/clock.js.map +1 -0
- package/dist/ports/decision.d.ts +75 -0
- package/dist/ports/decision.js +2 -0
- package/dist/ports/decision.js.map +1 -0
- package/dist/ports/index.d.ts +14 -0
- package/dist/ports/index.js +2 -0
- package/dist/ports/index.js.map +1 -0
- package/dist/ports/log.d.ts +22 -0
- package/dist/ports/log.js +12 -0
- package/dist/ports/log.js.map +1 -0
- package/dist/ports/state.d.ts +46 -0
- package/dist/ports/state.js +13 -0
- package/dist/ports/state.js.map +1 -0
- package/dist/state/clock.d.ts +40 -0
- package/dist/state/clock.js +43 -0
- package/dist/state/clock.js.map +1 -0
- package/dist/state/file-store.d.ts +340 -0
- package/dist/state/file-store.js +849 -0
- package/dist/state/file-store.js.map +1 -0
- package/dist/state/paths.d.ts +41 -0
- package/dist/state/paths.js +73 -0
- package/dist/state/paths.js.map +1 -0
- package/docs/USER_GUIDE.en.md +79 -36
- package/docs/claude-code.md +281 -0
- package/docs/design/v0.2.0-harness-plugins.md +381 -0
- package/docs/design/v0.3.0-orchestration-unification.md +580 -0
- package/docs/dsh-plugin.md +54 -54
- package/docs/release-checklist-v0.1.0.md +32 -21
- package/docs/release-handbook-porting-notes.md +11 -11
- package/docs/release-handbook.md +55 -30
- package/docs/release-notes-v0.1.0.md +27 -14
- package/docs/release-notes-v0.3.0.md +152 -0
- package/docs/research/claude-code-harness.md +607 -0
- package/docs/research/pi-harness.md +621 -0
- package/package.json +16 -7
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The three-state policy and the shared action/judgment budgets.
|
|
3
|
+
*
|
|
4
|
+
* This is the harness-agnostic half of the intervention policy: given what the
|
|
5
|
+
* deployment resolved (a mode per capability, a master switch, and the ceilings)
|
|
6
|
+
* and the current turn's counters, it answers *may this capability act*, *may
|
|
7
|
+
* this judgment be spent*, and *may one more action be planned*. It knows
|
|
8
|
+
* nothing about hooks, payload shapes, or which host is calling.
|
|
9
|
+
*
|
|
10
|
+
* @module core/policy
|
|
11
|
+
*/
|
|
12
|
+
/** The three-state vocabulary every automatic capability shares. */
|
|
13
|
+
export const DECISION_MODES = ['off', 'observe', 'active'];
|
|
14
|
+
/** The capability names the decision surface speaks in. */
|
|
15
|
+
export const DECISION_CAPABILITIES = ['stagnation', 'prune', 'gate', 'closing'];
|
|
16
|
+
/**
|
|
17
|
+
* Effective mode of one capability.
|
|
18
|
+
* @param policy - the resolved policy.
|
|
19
|
+
* @param capability - the capability to ask about.
|
|
20
|
+
* @returns its resolved three-state mode.
|
|
21
|
+
*/
|
|
22
|
+
export function modeOf(policy, capability) {
|
|
23
|
+
return policy.capabilities[capability];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Whether a capability may run at all: the master switch is on and the capability
|
|
27
|
+
* is not `off`.
|
|
28
|
+
* @param policy - the resolved policy.
|
|
29
|
+
* @param capability - the capability to ask about.
|
|
30
|
+
* @returns whether it is observable or active.
|
|
31
|
+
*/
|
|
32
|
+
export function enabledCapability(policy, capability) {
|
|
33
|
+
return policy.enabled && modeOf(policy, capability) !== 'off';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Whether a hook serving one capability set must be registered at all.
|
|
37
|
+
* @param policy - the resolved policy.
|
|
38
|
+
* @param capabilities - capabilities the hook serves.
|
|
39
|
+
* @returns whether at least one of them is not `off`.
|
|
40
|
+
*/
|
|
41
|
+
export function serves(policy, ...capabilities) {
|
|
42
|
+
if (!policy.enabled)
|
|
43
|
+
return false;
|
|
44
|
+
return capabilities.some((capability) => modeOf(policy, capability) !== 'off');
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Whether this turn still has room for one more planned action.
|
|
48
|
+
*
|
|
49
|
+
* This is the *action* ceiling, and it is deliberately not the judgment budget:
|
|
50
|
+
* a judgment is the question System 1 was asked, this is the number of things
|
|
51
|
+
* the run is allowed to do about the answer. Every capability draws on the same
|
|
52
|
+
* allowance, and both observe and active draw on it the same way.
|
|
53
|
+
*
|
|
54
|
+
* @param state - the deciding state's counters.
|
|
55
|
+
* @param policy - the resolved policy.
|
|
56
|
+
* @returns whether one more planned action fits this turn.
|
|
57
|
+
*/
|
|
58
|
+
export function actionWithinBudget(state, policy) {
|
|
59
|
+
return state.interventions < policy.maxInterventionsPerTurn;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Book one planned action against the turn's shared allowance.
|
|
63
|
+
* @param state - the deciding state's counters.
|
|
64
|
+
*/
|
|
65
|
+
export function bookAction(state) {
|
|
66
|
+
state.interventions += 1;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Spend one System 1 judgment against the turn's and the session's budget.
|
|
70
|
+
*
|
|
71
|
+
* Both budgets are spent at the one place a judgment is actually made, so a
|
|
72
|
+
* capability cannot buy its way around them by calling a service directly. The
|
|
73
|
+
* caller decides whether a refusal means "plan nothing" or "do not act"; what
|
|
74
|
+
* matters here is that the count is never overrun and that a shadowed
|
|
75
|
+
* application is charged like a real one.
|
|
76
|
+
*
|
|
77
|
+
* @param state - the deciding state's counters.
|
|
78
|
+
* @param policy - the resolved policy.
|
|
79
|
+
* @returns `undefined` when the judgment was spent, else the skip reason.
|
|
80
|
+
*/
|
|
81
|
+
export function spendJudgment(state, policy) {
|
|
82
|
+
if (state.judgmentsCount >= policy.maxJudgmentsPerTurn)
|
|
83
|
+
return 'judgment-turn-cap';
|
|
84
|
+
if (state.judgmentsSession >= policy.maxJudgmentsPerSession)
|
|
85
|
+
return 'judgment-session-cap';
|
|
86
|
+
state.judgmentsCount += 1;
|
|
87
|
+
state.judgmentsSession += 1;
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Whether a verdict justifies acting: stuck, confident enough, and about a
|
|
92
|
+
* pattern other than `none`.
|
|
93
|
+
* @param response - the verdict.
|
|
94
|
+
* @param policy - the resolved policy (for the confidence floor).
|
|
95
|
+
* @returns whether the plan may be made.
|
|
96
|
+
*/
|
|
97
|
+
export function usefulVerdict(response, policy) {
|
|
98
|
+
return (response.isStuck === true &&
|
|
99
|
+
response.confidence >= policy.minConfidence &&
|
|
100
|
+
(response.patternType ?? 'none') !== 'none');
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../../src/core/policy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,oEAAoE;AACpE,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,CAAU,CAAC;AAKpE,2DAA2D;AAC3D,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,CAAU,CAAC;AAiDzF;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CACpB,MAAkC,EAClC,UAAsB;IAEtB,OAAO,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC;AACzC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC/B,MAAkC,EAClC,UAAsB;IAEtB,OAAO,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,KAAK,KAAK,CAAC;AAChE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CACpB,MAAkC,EAClC,GAAG,YAA0B;IAE7B,IAAI,CAAC,MAAM,CAAC,OAAO;QAAE,OAAO,KAAK,CAAC;IAClC,OAAO,YAAY,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,KAAK,KAAK,CAAC,CAAC;AACjF,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAkB,EAAE,MAAsB;IAC3E,OAAO,KAAK,CAAC,aAAa,GAAG,MAAM,CAAC,uBAAuB,CAAC;AAC9D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,KAAkB;IAC3C,KAAK,CAAC,aAAa,IAAI,CAAC,CAAC;AAC3B,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,aAAa,CAAC,KAAkB,EAAE,MAAsB;IACtE,IAAI,KAAK,CAAC,cAAc,IAAI,MAAM,CAAC,mBAAmB;QAAE,OAAO,mBAAmB,CAAC;IACnF,IAAI,KAAK,CAAC,gBAAgB,IAAI,MAAM,CAAC,sBAAsB;QAAE,OAAO,sBAAsB,CAAC;IAC3F,KAAK,CAAC,cAAc,IAAI,CAAC,CAAC;IAC1B,KAAK,CAAC,gBAAgB,IAAI,CAAC,CAAC;IAC5B,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,QAAqB,EAAE,MAAsB;IACzE,OAAO,CACL,QAAQ,CAAC,OAAO,KAAK,IAAI;QACzB,QAAQ,CAAC,UAAU,IAAI,MAAM,CAAC,aAAa;QAC3C,CAAC,QAAQ,CAAC,WAAW,IAAI,MAAM,CAAC,KAAK,MAAM,CAC5C,CAAC;AACJ,CAAC","sourcesContent":["/**\n * The three-state policy and the shared action/judgment budgets.\n *\n * This is the harness-agnostic half of the intervention policy: given what the\n * deployment resolved (a mode per capability, a master switch, and the ceilings)\n * and the current turn's counters, it answers *may this capability act*, *may\n * this judgment be spent*, and *may one more action be planned*. It knows\n * nothing about hooks, payload shapes, or which host is calling.\n *\n * @module core/policy\n */\n\n/** The three-state vocabulary every automatic capability shares. */\nexport const DECISION_MODES = ['off', 'observe', 'active'] as const;\n\n/** One resolved three-state mode. */\nexport type DecisionMode = (typeof DECISION_MODES)[number];\n\n/** The capability names the decision surface speaks in. */\nexport const DECISION_CAPABILITIES = ['stagnation', 'prune', 'gate', 'closing'] as const;\n\n/** One automatic capability name. */\nexport type DecisionCapability = (typeof DECISION_CAPABILITIES)[number];\n\n/**\n * The resolved policy the core reads.\n *\n * Structural on purpose: a host's own resolved config object satisfies it\n * without importing this module's types, and the core never sees the host's\n * configuration schema.\n */\nexport interface PolicySnapshot<Capability extends string = DecisionCapability> {\n /** Master switch: when false, no capability acts whatever its own mode says. */\n readonly enabled: boolean;\n /** The master three-state mode. */\n readonly mode: DecisionMode;\n /** Effective mode of every capability. */\n readonly capabilities: Readonly<Record<Capability, DecisionMode>>;\n /** Ceiling on planned/applied interventions per turn. */\n readonly maxInterventionsPerTurn: number;\n /** Ceiling on System 1 judgments per turn. */\n readonly maxJudgmentsPerTurn: number;\n /** Ceiling on System 1 judgments per session. */\n readonly maxJudgmentsPerSession: number;\n /** Identical consecutive calls required before a judgment is bought. */\n readonly repeatThreshold: number;\n /** Minimum verdict confidence for an action to be justified. */\n readonly minConfidence: number;\n}\n\n/** The counters one turn and one session spend from. */\nexport interface BudgetState {\n /** Interventions planned or applied in the current turn. */\n interventions: number;\n /** Judgments spent in the current turn. */\n judgmentsCount: number;\n /** Judgments spent in the current session. */\n judgmentsSession: number;\n}\n\n/** The minimal verdict shape the action gate reads. */\nexport interface VerdictLike {\n readonly isStuck?: boolean;\n readonly confidence: number;\n readonly patternType?: string;\n readonly latencyMs?: number;\n}\n\n/**\n * Effective mode of one capability.\n * @param policy - the resolved policy.\n * @param capability - the capability to ask about.\n * @returns its resolved three-state mode.\n */\nexport function modeOf<Capability extends string>(\n policy: PolicySnapshot<Capability>,\n capability: Capability\n): DecisionMode {\n return policy.capabilities[capability];\n}\n\n/**\n * Whether a capability may run at all: the master switch is on and the capability\n * is not `off`.\n * @param policy - the resolved policy.\n * @param capability - the capability to ask about.\n * @returns whether it is observable or active.\n */\nexport function enabledCapability<Capability extends string>(\n policy: PolicySnapshot<Capability>,\n capability: Capability\n): boolean {\n return policy.enabled && modeOf(policy, capability) !== 'off';\n}\n\n/**\n * Whether a hook serving one capability set must be registered at all.\n * @param policy - the resolved policy.\n * @param capabilities - capabilities the hook serves.\n * @returns whether at least one of them is not `off`.\n */\nexport function serves<Capability extends string>(\n policy: PolicySnapshot<Capability>,\n ...capabilities: Capability[]\n): boolean {\n if (!policy.enabled) return false;\n return capabilities.some((capability) => modeOf(policy, capability) !== 'off');\n}\n\n/**\n * Whether this turn still has room for one more planned action.\n *\n * This is the *action* ceiling, and it is deliberately not the judgment budget:\n * a judgment is the question System 1 was asked, this is the number of things\n * the run is allowed to do about the answer. Every capability draws on the same\n * allowance, and both observe and active draw on it the same way.\n *\n * @param state - the deciding state's counters.\n * @param policy - the resolved policy.\n * @returns whether one more planned action fits this turn.\n */\nexport function actionWithinBudget(state: BudgetState, policy: PolicySnapshot): boolean {\n return state.interventions < policy.maxInterventionsPerTurn;\n}\n\n/**\n * Book one planned action against the turn's shared allowance.\n * @param state - the deciding state's counters.\n */\nexport function bookAction(state: BudgetState): void {\n state.interventions += 1;\n}\n\n/**\n * Spend one System 1 judgment against the turn's and the session's budget.\n *\n * Both budgets are spent at the one place a judgment is actually made, so a\n * capability cannot buy its way around them by calling a service directly. The\n * caller decides whether a refusal means \"plan nothing\" or \"do not act\"; what\n * matters here is that the count is never overrun and that a shadowed\n * application is charged like a real one.\n *\n * @param state - the deciding state's counters.\n * @param policy - the resolved policy.\n * @returns `undefined` when the judgment was spent, else the skip reason.\n */\nexport function spendJudgment(state: BudgetState, policy: PolicySnapshot): string | undefined {\n if (state.judgmentsCount >= policy.maxJudgmentsPerTurn) return 'judgment-turn-cap';\n if (state.judgmentsSession >= policy.maxJudgmentsPerSession) return 'judgment-session-cap';\n state.judgmentsCount += 1;\n state.judgmentsSession += 1;\n return undefined;\n}\n\n/**\n * Whether a verdict justifies acting: stuck, confident enough, and about a\n * pattern other than `none`.\n * @param response - the verdict.\n * @param policy - the resolved policy (for the confidence floor).\n * @returns whether the plan may be made.\n */\nexport function usefulVerdict(response: VerdictLike, policy: PolicySnapshot): boolean {\n return (\n response.isStuck === true &&\n response.confidence >= policy.minConfidence &&\n (response.patternType ?? 'none') !== 'none'\n );\n}\n"]}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The deterministic floor of a judged reduction.
|
|
3
|
+
*
|
|
4
|
+
* A prune judgment proposes; this module disposes. Given the original payload,
|
|
5
|
+
* the judged candidate, and the floor options, it decides whether the head /
|
|
6
|
+
* extract / tail form replaces the candidate, composes that form out of whole
|
|
7
|
+
* lines, and reports the size the installed text really has. Everything here is
|
|
8
|
+
* pure arithmetic over strings: no host type, no retention sink, no audit.
|
|
9
|
+
*
|
|
10
|
+
* @module core/prune-plan
|
|
11
|
+
*/
|
|
12
|
+
/** Default share of a floored reduction's context budget taken from the head. */
|
|
13
|
+
export declare const FLOOR_HEAD_CHARS = 400;
|
|
14
|
+
/** Default share of a floored reduction's context budget taken from the tail. */
|
|
15
|
+
export declare const FLOOR_TAIL_CHARS = 400;
|
|
16
|
+
/**
|
|
17
|
+
* The floor decision of one reduction, computed from lengths alone.
|
|
18
|
+
*
|
|
19
|
+
* {@link pruneFloorPlan} returns the *decision*: whether the floor applies and
|
|
20
|
+
* the size that decision was modelled on. {@link applyPruneFloor} returns the
|
|
21
|
+
* same decision next to the size the composed text *really* has, because line
|
|
22
|
+
* alignment and duplicate removal move that size. Callers account for the text
|
|
23
|
+
* a mode really installs, so the composed numbers are the authoritative ones.
|
|
24
|
+
*/
|
|
25
|
+
export interface PruneFloorPlan {
|
|
26
|
+
/** Whether the floor form replaces the judged candidate. */
|
|
27
|
+
floored: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Whether the installed reduction fell short of the floor's promised share.
|
|
30
|
+
*
|
|
31
|
+
* True exactly when the composition could not build a form that both carries
|
|
32
|
+
* `floorRatio` of the original and stays smaller than it, so the judged
|
|
33
|
+
* candidate itself was installed. It is what separates "nothing was reduced"
|
|
34
|
+
* from "the reduction was real, but it did not reach the floor's promise"; a
|
|
35
|
+
* plan that never needed the floor reports `false`.
|
|
36
|
+
*/
|
|
37
|
+
floorMissed: boolean;
|
|
38
|
+
/** Characters the installed text carries: modelled by the plan, real once composed. */
|
|
39
|
+
resultChars: number;
|
|
40
|
+
/** Characters the installed form does not carry. */
|
|
41
|
+
omittedChars: number;
|
|
42
|
+
/** Context characters taken from the head of the original. */
|
|
43
|
+
headChars: number;
|
|
44
|
+
/** Context characters taken from the tail of the original. */
|
|
45
|
+
tailChars: number;
|
|
46
|
+
/**
|
|
47
|
+
* Marker characters the installed form carries, summed over its sides.
|
|
48
|
+
*
|
|
49
|
+
* Zero when nothing is floored, one marker when only the mandatory locator side
|
|
50
|
+
* fits, two when the share's budget can pay for a matching marker after the
|
|
51
|
+
* candidate. Publishing it is what lets {@link applyPruneFloor} rebuild exactly
|
|
52
|
+
* the form whose size the plan promised.
|
|
53
|
+
*/
|
|
54
|
+
markerChars: number;
|
|
55
|
+
/**
|
|
56
|
+
* Whether a floor-missed install wanted the locator marker but could not carry it.
|
|
57
|
+
*
|
|
58
|
+
* The marker is what tells a reader that the reduction happened and where the
|
|
59
|
+
* full payload lives, so a candidate that fits only *without* it is still
|
|
60
|
+
* installed — the reduction is real — but the omission is recorded instead of
|
|
61
|
+
* being silently dropped. Always `false` when `floorMissed` is false.
|
|
62
|
+
*/
|
|
63
|
+
markerOmitted: boolean;
|
|
64
|
+
}
|
|
65
|
+
/** The floor options shared by the plan and the composition. */
|
|
66
|
+
export interface PruneFloorOptions {
|
|
67
|
+
/** Lowest share of the original a reduction may keep. */
|
|
68
|
+
floorRatio: number;
|
|
69
|
+
/** Default split of the context budget, in characters. */
|
|
70
|
+
headChars?: number;
|
|
71
|
+
/** Default split of the context budget, in characters. */
|
|
72
|
+
tailChars?: number;
|
|
73
|
+
/**
|
|
74
|
+
* Length of the marker the installed form carries per side.
|
|
75
|
+
*
|
|
76
|
+
* A caller that composes a locator-carrying form passes the real marker
|
|
77
|
+
* length here; a caller that only accounts for it passes the same number. That
|
|
78
|
+
* is what keeps the predicted size and the installed size identical in both
|
|
79
|
+
* modes.
|
|
80
|
+
*/
|
|
81
|
+
markerChars?: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Decide the deterministic floor of one reduction from lengths alone.
|
|
85
|
+
*
|
|
86
|
+
* Lengths are enough because the floor form is fixed: context, marker, candidate,
|
|
87
|
+
* marker, context. Deciding it this way is what lets `observe` account for the
|
|
88
|
+
* *same* candidate `active` would install without building it.
|
|
89
|
+
*
|
|
90
|
+
* The floor is a *share*, not a suggestion. `pruneFloorRatio` says how much of
|
|
91
|
+
* the original must survive, so the context budget is derived from it rather than
|
|
92
|
+
* capped by the head/tail hints: a candidate that keeps 8% of a long payload is
|
|
93
|
+
* not "floored" to 8% plus two small slices, it is widened until the installed
|
|
94
|
+
* form really carries the promised share. The head/tail hints only decide how
|
|
95
|
+
* that budget is split. A plan that cannot reach the share — because the marker
|
|
96
|
+
* alone would eat the whole allowance — is refused, so a reduction never grows
|
|
97
|
+
* the context.
|
|
98
|
+
*
|
|
99
|
+
* @param originalChars - length of the raw payload.
|
|
100
|
+
* @param candidateChars - length of the judged candidate.
|
|
101
|
+
* @param options - floor ratio, split hints, and marker length.
|
|
102
|
+
* @returns the floor decision.
|
|
103
|
+
*/
|
|
104
|
+
export declare function pruneFloorPlan(originalChars: number, candidateChars: number, options: PruneFloorOptions): PruneFloorPlan;
|
|
105
|
+
/** The marker one floored reduction carries on each side of its candidate. */
|
|
106
|
+
export declare function pruneMarker(locator: string): string;
|
|
107
|
+
/**
|
|
108
|
+
* Enforce the deterministic floor of a judged reduction.
|
|
109
|
+
*
|
|
110
|
+
* The judgment proposes; this floor disposes. A candidate that cut deeper than
|
|
111
|
+
* {@link ResolvedInterventionsConfig.pruneFloorRatio} of the original is replaced
|
|
112
|
+
* by a form that keeps the head and the tail — the two places a compiler error, a
|
|
113
|
+
* stack-trace summary, and a final result usually are — around the judgment's own
|
|
114
|
+
* extract, with explicit markers that name where the full original was retained.
|
|
115
|
+
* A candidate that is not actually smaller is refused outright, so a "reduction"
|
|
116
|
+
* can never grow the context.
|
|
117
|
+
*
|
|
118
|
+
* The context is composed out of *whole lines* of the original, never out of
|
|
119
|
+
* character slices: a cut that lands mid-line leaves the reader a fragment like
|
|
120
|
+
* `=] progress 092`, which is not a shorter answer but a broken one. The head is
|
|
121
|
+
* the longest run of complete lines its budget can pay for, the tail the shortest
|
|
122
|
+
* run of complete lines that still carries its own, and any candidate line the
|
|
123
|
+
* kept context already shows verbatim is dropped as a duplicate. Alignment and
|
|
124
|
+
* removal move the real size, so the decision's modelled size is replaced here by
|
|
125
|
+
* the size the text really has: a form that would fall short of the promised
|
|
126
|
+
* share, or that would not be smaller than the original at all, is refused. A
|
|
127
|
+
* refusal installs the judgment's own candidate with `floorMissed: true` rather
|
|
128
|
+
* than the original — the candidate is a real reduction, and dropping it would
|
|
129
|
+
* trade a smaller answer for the whole payload just because the floor form could
|
|
130
|
+
* not be built.
|
|
131
|
+
*
|
|
132
|
+
* @param original - the raw payload.
|
|
133
|
+
* @param candidate - the judged reduction.
|
|
134
|
+
* @param options - floor options plus the locator the replacement publishes.
|
|
135
|
+
* @returns the text to install, the floor decision, and the sizes involved.
|
|
136
|
+
*/
|
|
137
|
+
export declare function applyPruneFloor(original: string, candidate: string, options: PruneFloorOptions & {
|
|
138
|
+
locator: string;
|
|
139
|
+
}): PruneFloorPlan & {
|
|
140
|
+
text: string;
|
|
141
|
+
};
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The deterministic floor of a judged reduction.
|
|
3
|
+
*
|
|
4
|
+
* A prune judgment proposes; this module disposes. Given the original payload,
|
|
5
|
+
* the judged candidate, and the floor options, it decides whether the head /
|
|
6
|
+
* extract / tail form replaces the candidate, composes that form out of whole
|
|
7
|
+
* lines, and reports the size the installed text really has. Everything here is
|
|
8
|
+
* pure arithmetic over strings: no host type, no retention sink, no audit.
|
|
9
|
+
*
|
|
10
|
+
* @module core/prune-plan
|
|
11
|
+
*/
|
|
12
|
+
/** Default share of a floored reduction's context budget taken from the head. */
|
|
13
|
+
export const FLOOR_HEAD_CHARS = 400;
|
|
14
|
+
/** Default share of a floored reduction's context budget taken from the tail. */
|
|
15
|
+
export const FLOOR_TAIL_CHARS = 400;
|
|
16
|
+
/**
|
|
17
|
+
* Decide the deterministic floor of one reduction from lengths alone.
|
|
18
|
+
*
|
|
19
|
+
* Lengths are enough because the floor form is fixed: context, marker, candidate,
|
|
20
|
+
* marker, context. Deciding it this way is what lets `observe` account for the
|
|
21
|
+
* *same* candidate `active` would install without building it.
|
|
22
|
+
*
|
|
23
|
+
* The floor is a *share*, not a suggestion. `pruneFloorRatio` says how much of
|
|
24
|
+
* the original must survive, so the context budget is derived from it rather than
|
|
25
|
+
* capped by the head/tail hints: a candidate that keeps 8% of a long payload is
|
|
26
|
+
* not "floored" to 8% plus two small slices, it is widened until the installed
|
|
27
|
+
* form really carries the promised share. The head/tail hints only decide how
|
|
28
|
+
* that budget is split. A plan that cannot reach the share — because the marker
|
|
29
|
+
* alone would eat the whole allowance — is refused, so a reduction never grows
|
|
30
|
+
* the context.
|
|
31
|
+
*
|
|
32
|
+
* @param originalChars - length of the raw payload.
|
|
33
|
+
* @param candidateChars - length of the judged candidate.
|
|
34
|
+
* @param options - floor ratio, split hints, and marker length.
|
|
35
|
+
* @returns the floor decision.
|
|
36
|
+
*/
|
|
37
|
+
export function pruneFloorPlan(originalChars, candidateChars, options) {
|
|
38
|
+
if (candidateChars === 0 || candidateChars >= originalChars) {
|
|
39
|
+
return idleFloorPlan(originalChars);
|
|
40
|
+
}
|
|
41
|
+
const marker = Math.max(0, options.markerChars ?? 0);
|
|
42
|
+
const head = Math.max(0, options.headChars ?? FLOOR_HEAD_CHARS);
|
|
43
|
+
const tail = Math.max(0, options.tailChars ?? FLOOR_TAIL_CHARS);
|
|
44
|
+
const share = Math.min(1, Math.max(0, options.floorRatio));
|
|
45
|
+
const target = Math.ceil(originalChars * share);
|
|
46
|
+
// The judged candidate is already the installed text when the floor does not
|
|
47
|
+
// apply, and it carries no marker; only a deeper cut pays for the marker.
|
|
48
|
+
if (candidateChars >= target) {
|
|
49
|
+
return {
|
|
50
|
+
floored: false,
|
|
51
|
+
floorMissed: false,
|
|
52
|
+
resultChars: candidateChars,
|
|
53
|
+
omittedChars: originalChars - candidateChars,
|
|
54
|
+
headChars: 0,
|
|
55
|
+
tailChars: 0,
|
|
56
|
+
markerChars: 0,
|
|
57
|
+
markerOmitted: false
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
// One marker side is mandatory: it carries the locator of the original. The
|
|
61
|
+
// second is bought only when the share's budget leaves room for it. A candidate
|
|
62
|
+
// that cannot even pay for the mandatory marker is still a real reduction, so
|
|
63
|
+
// it is installed *without* the marker and the omission is recorded: dropping
|
|
64
|
+
// it would trade a smaller answer for the whole payload over a missing line.
|
|
65
|
+
if (candidateChars + marker >= originalChars) {
|
|
66
|
+
return {
|
|
67
|
+
floored: false,
|
|
68
|
+
floorMissed: true,
|
|
69
|
+
resultChars: candidateChars,
|
|
70
|
+
omittedChars: originalChars - candidateChars,
|
|
71
|
+
headChars: 0,
|
|
72
|
+
tailChars: 0,
|
|
73
|
+
markerChars: 0,
|
|
74
|
+
markerOmitted: true
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
let budget = Math.max(candidateChars + marker, Math.min(originalChars, target));
|
|
78
|
+
let markerChars = marker;
|
|
79
|
+
let context = budget - candidateChars - markerChars;
|
|
80
|
+
if (context >= marker) {
|
|
81
|
+
markerChars += marker;
|
|
82
|
+
context -= marker;
|
|
83
|
+
}
|
|
84
|
+
const split = splitContext(context, head, tail);
|
|
85
|
+
const resultChars = candidateChars + markerChars + context;
|
|
86
|
+
if (resultChars >= originalChars)
|
|
87
|
+
return idleFloorPlan(originalChars);
|
|
88
|
+
return {
|
|
89
|
+
floored: true,
|
|
90
|
+
floorMissed: false,
|
|
91
|
+
resultChars,
|
|
92
|
+
omittedChars: originalChars - resultChars,
|
|
93
|
+
headChars: split.head,
|
|
94
|
+
tailChars: split.tail,
|
|
95
|
+
markerChars,
|
|
96
|
+
markerOmitted: false
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Split one context budget between the head and the tail.
|
|
101
|
+
*
|
|
102
|
+
* The hints describe the *shape* of the context, not its ceiling, so a large
|
|
103
|
+
* budget keeps their proportion instead of stopping at their sum. A zero-shaped
|
|
104
|
+
* split degrades to an even one rather than dropping the context.
|
|
105
|
+
* @param context - characters available for context.
|
|
106
|
+
* @param head - head hint.
|
|
107
|
+
* @param tail - tail hint.
|
|
108
|
+
* @returns the two slice lengths, summing to the context.
|
|
109
|
+
*/
|
|
110
|
+
function splitContext(context, head, tail) {
|
|
111
|
+
const total = head + tail;
|
|
112
|
+
const headChars = total <= 0 ? Math.ceil(context / 2) : Math.ceil((context * head) / total);
|
|
113
|
+
return { head: Math.min(context, headChars), tail: context - Math.min(context, headChars) };
|
|
114
|
+
}
|
|
115
|
+
/** The plan of a reduction that is not happening. */
|
|
116
|
+
function idleFloorPlan(originalChars) {
|
|
117
|
+
return {
|
|
118
|
+
floored: false,
|
|
119
|
+
floorMissed: false,
|
|
120
|
+
resultChars: originalChars,
|
|
121
|
+
omittedChars: 0,
|
|
122
|
+
headChars: 0,
|
|
123
|
+
tailChars: 0,
|
|
124
|
+
markerChars: 0,
|
|
125
|
+
markerOmitted: false
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** The marker one floored reduction carries on each side of its candidate. */
|
|
129
|
+
export function pruneMarker(locator) {
|
|
130
|
+
return `\n… [reflex prune: full original retrievable as ${locator}] …\n`;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The marker a floor-missed install appends after its candidate.
|
|
134
|
+
*
|
|
135
|
+
* The floored form opens with the marker because the head has just ended with a
|
|
136
|
+
* newline. A candidate that already ends with one needs no blank line before the
|
|
137
|
+
* marker, so only the leading newline is dropped in that case; the trailing
|
|
138
|
+
* newline is the same one the floored form carries.
|
|
139
|
+
* @param candidate - the judged candidate about to be installed.
|
|
140
|
+
* @param marker - the locator marker.
|
|
141
|
+
* @returns the suffix to append.
|
|
142
|
+
*/
|
|
143
|
+
function pruneMarkerSuffix(candidate, marker) {
|
|
144
|
+
return candidate.endsWith('\n') ? marker.slice(1) : marker;
|
|
145
|
+
}
|
|
146
|
+
/** Split one payload into whole lines, the terminator kept with its line. */
|
|
147
|
+
function floorLines(text) {
|
|
148
|
+
return text.match(/[^\n]*\n|[^\n]+$/g) ?? [];
|
|
149
|
+
}
|
|
150
|
+
/** The identity of one line for the duplicate test: its text without its terminator. */
|
|
151
|
+
function floorLineIdentity(line) {
|
|
152
|
+
return line.replace(/\r?\n$/, '').replace(/\r$/, '');
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* How many whole lines of the head a budget can pay for.
|
|
156
|
+
*
|
|
157
|
+
* The head never crosses its budget and never stops inside a line: a cut that
|
|
158
|
+
* cannot pay for the next line stops before it, so the kept prefix is always a
|
|
159
|
+
* run of complete lines.
|
|
160
|
+
* @param lines - the payload, split into whole lines.
|
|
161
|
+
* @param budget - characters the head may spend.
|
|
162
|
+
* @returns the number of lines to keep from the top.
|
|
163
|
+
*/
|
|
164
|
+
function alignedHeadLines(lines, budget) {
|
|
165
|
+
let used = 0;
|
|
166
|
+
let count = 0;
|
|
167
|
+
for (const line of lines) {
|
|
168
|
+
if (used + line.length > budget)
|
|
169
|
+
break;
|
|
170
|
+
used += line.length;
|
|
171
|
+
count += 1;
|
|
172
|
+
}
|
|
173
|
+
return count;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* How many whole lines of the tail a budget needs.
|
|
177
|
+
*
|
|
178
|
+
* The tail is the *shortest* run of complete lines from the end that still
|
|
179
|
+
* carries the budget. A payload whose lines are longer than the budget cannot
|
|
180
|
+
* honour it with a line-aligned suffix; the caller refuses instead of cutting
|
|
181
|
+
* one, because half a line is not a shorter answer but a broken one.
|
|
182
|
+
* @param lines - the payload, split into whole lines.
|
|
183
|
+
* @param budget - characters the tail must carry.
|
|
184
|
+
* @returns the number of lines to keep from the bottom.
|
|
185
|
+
*/
|
|
186
|
+
function alignedTailLines(lines, budget) {
|
|
187
|
+
if (budget <= 0)
|
|
188
|
+
return 0;
|
|
189
|
+
let used = 0;
|
|
190
|
+
let count = 0;
|
|
191
|
+
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
192
|
+
used += lines[index].length;
|
|
193
|
+
count += 1;
|
|
194
|
+
if (used >= budget)
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
return count;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Drop the candidate lines the kept context already shows verbatim.
|
|
201
|
+
*
|
|
202
|
+
* The judgment reads the whole payload, so its extract repeats the lines a
|
|
203
|
+
* reader can already see around it — `BEGIN`, `RESULT`, `END` in the trace that
|
|
204
|
+
* prompted this. Repeating them is pure cost: the same line twice in one
|
|
205
|
+
* message. Removal is per line and verbatim, so a diagnostic the context does
|
|
206
|
+
* *not* carry always survives.
|
|
207
|
+
* @param candidate - the judged extract.
|
|
208
|
+
* @param head - the kept head.
|
|
209
|
+
* @param tail - the kept tail.
|
|
210
|
+
* @returns the candidate without the lines the context already shows.
|
|
211
|
+
*/
|
|
212
|
+
function dedupeCandidateLines(candidate, head, tail) {
|
|
213
|
+
const seen = new Set([...floorLines(head), ...floorLines(tail)].map(floorLineIdentity));
|
|
214
|
+
const lines = floorLines(candidate);
|
|
215
|
+
if (seen.size === 0 || lines.length === 0)
|
|
216
|
+
return candidate;
|
|
217
|
+
return lines.filter((line) => !seen.has(floorLineIdentity(line))).join('');
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Compose one floored form out of whole lines of the original.
|
|
221
|
+
* @param lines - the original, split into whole lines.
|
|
222
|
+
* @param headCount - lines kept from the top.
|
|
223
|
+
* @param tailCount - lines kept from the bottom.
|
|
224
|
+
* @param candidate - the judged extract.
|
|
225
|
+
* @param marker - the locator marker.
|
|
226
|
+
* @param secondMarker - the closing marker, or `''` when the share cannot pay for it.
|
|
227
|
+
* @returns the installed text and the sizes it really carries.
|
|
228
|
+
*/
|
|
229
|
+
function composeFloorLines(lines, headCount, tailCount, candidate, marker, secondMarker) {
|
|
230
|
+
const head = lines.slice(0, headCount).join('');
|
|
231
|
+
const tail = tailCount === 0 ? '' : lines.slice(lines.length - tailCount).join('');
|
|
232
|
+
const middle = dedupeCandidateLines(candidate, head, tail);
|
|
233
|
+
// The marker opens with a newline so it sits on its own line after the head.
|
|
234
|
+
// With no head there is nothing to separate it from, and a leading blank line
|
|
235
|
+
// would be the first thing a reader sees.
|
|
236
|
+
const lead = head.length === 0 ? marker.replace(/^\n/, '') : marker;
|
|
237
|
+
const closing = middle.length === 0 ? '' : secondMarker;
|
|
238
|
+
return {
|
|
239
|
+
text: `${head}${lead}${middle}${closing}${tail}`,
|
|
240
|
+
headChars: head.length,
|
|
241
|
+
tailChars: tail.length,
|
|
242
|
+
markerChars: lead.length + closing.length
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* The plan of a floor the composition refused.
|
|
247
|
+
*
|
|
248
|
+
* A refusal is *not* a reason to throw the candidate away. The candidate is the
|
|
249
|
+
* reduction the judgment already produced, and a floor that cannot widen it
|
|
250
|
+
* without either breaking the share or growing past the original has simply
|
|
251
|
+
* missed the floor, not invalidated the reduction: installing the candidate is
|
|
252
|
+
* strictly better than carrying the whole payload. So the candidate is installed
|
|
253
|
+
* with `floored: false` (no head/tail form was built) and `floorMissed: true`
|
|
254
|
+
* (the promised share is not carried), and the sizes are booked against the text
|
|
255
|
+
* that is really installed.
|
|
256
|
+
*
|
|
257
|
+
* The locator marker is appended when the candidate can still pay for it, so the
|
|
258
|
+
* reader sees that a prune happened and where the full payload lives. When the
|
|
259
|
+
* marker would push the install to the original's size or past it, the reduction
|
|
260
|
+
* wins and the marker is omitted — recorded as `markerOmitted: true`, never
|
|
261
|
+
* silently dropped.
|
|
262
|
+
*
|
|
263
|
+
* Only a candidate that is not actually smaller leaves the original untouched —
|
|
264
|
+
* that is a real "no reduction", and the caller's own degradation path owns it.
|
|
265
|
+
*
|
|
266
|
+
* @param original - the raw payload.
|
|
267
|
+
* @param candidate - the judged reduction the composition could not widen.
|
|
268
|
+
* @param marker - the locator marker a floor-missed install wants to carry.
|
|
269
|
+
* @returns the text to install and the floor decision that says why.
|
|
270
|
+
*/
|
|
271
|
+
function refusedFloor(original, candidate, marker) {
|
|
272
|
+
if (candidate.length >= original.length) {
|
|
273
|
+
return { ...idleFloorPlan(original.length), text: original };
|
|
274
|
+
}
|
|
275
|
+
const suffix = pruneMarkerSuffix(candidate, marker);
|
|
276
|
+
if (candidate.length + suffix.length < original.length) {
|
|
277
|
+
return {
|
|
278
|
+
floored: false,
|
|
279
|
+
floorMissed: true,
|
|
280
|
+
resultChars: candidate.length + suffix.length,
|
|
281
|
+
omittedChars: original.length - candidate.length - suffix.length,
|
|
282
|
+
headChars: 0,
|
|
283
|
+
tailChars: 0,
|
|
284
|
+
markerChars: suffix.length,
|
|
285
|
+
markerOmitted: false,
|
|
286
|
+
text: `${candidate}${suffix}`
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
return {
|
|
290
|
+
floored: false,
|
|
291
|
+
floorMissed: true,
|
|
292
|
+
resultChars: candidate.length,
|
|
293
|
+
omittedChars: original.length - candidate.length,
|
|
294
|
+
headChars: 0,
|
|
295
|
+
tailChars: 0,
|
|
296
|
+
markerChars: 0,
|
|
297
|
+
markerOmitted: true,
|
|
298
|
+
text: candidate
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Enforce the deterministic floor of a judged reduction.
|
|
303
|
+
*
|
|
304
|
+
* The judgment proposes; this floor disposes. A candidate that cut deeper than
|
|
305
|
+
* {@link ResolvedInterventionsConfig.pruneFloorRatio} of the original is replaced
|
|
306
|
+
* by a form that keeps the head and the tail — the two places a compiler error, a
|
|
307
|
+
* stack-trace summary, and a final result usually are — around the judgment's own
|
|
308
|
+
* extract, with explicit markers that name where the full original was retained.
|
|
309
|
+
* A candidate that is not actually smaller is refused outright, so a "reduction"
|
|
310
|
+
* can never grow the context.
|
|
311
|
+
*
|
|
312
|
+
* The context is composed out of *whole lines* of the original, never out of
|
|
313
|
+
* character slices: a cut that lands mid-line leaves the reader a fragment like
|
|
314
|
+
* `=] progress 092`, which is not a shorter answer but a broken one. The head is
|
|
315
|
+
* the longest run of complete lines its budget can pay for, the tail the shortest
|
|
316
|
+
* run of complete lines that still carries its own, and any candidate line the
|
|
317
|
+
* kept context already shows verbatim is dropped as a duplicate. Alignment and
|
|
318
|
+
* removal move the real size, so the decision's modelled size is replaced here by
|
|
319
|
+
* the size the text really has: a form that would fall short of the promised
|
|
320
|
+
* share, or that would not be smaller than the original at all, is refused. A
|
|
321
|
+
* refusal installs the judgment's own candidate with `floorMissed: true` rather
|
|
322
|
+
* than the original — the candidate is a real reduction, and dropping it would
|
|
323
|
+
* trade a smaller answer for the whole payload just because the floor form could
|
|
324
|
+
* not be built.
|
|
325
|
+
*
|
|
326
|
+
* @param original - the raw payload.
|
|
327
|
+
* @param candidate - the judged reduction.
|
|
328
|
+
* @param options - floor options plus the locator the replacement publishes.
|
|
329
|
+
* @returns the text to install, the floor decision, and the sizes involved.
|
|
330
|
+
*/
|
|
331
|
+
export function applyPruneFloor(original, candidate, options) {
|
|
332
|
+
const marker = pruneMarker(options.locator);
|
|
333
|
+
const plan = pruneFloorPlan(original.length, candidate.length, { ...options, markerChars: marker.length });
|
|
334
|
+
if (!plan.floored) {
|
|
335
|
+
return { ...plan, text: plan.resultChars === candidate.length ? candidate : original };
|
|
336
|
+
}
|
|
337
|
+
const lines = floorLines(original);
|
|
338
|
+
const share = Math.min(1, Math.max(0, options.floorRatio));
|
|
339
|
+
const target = Math.ceil(original.length * share);
|
|
340
|
+
const secondMarker = plan.markerChars > marker.length ? marker : '';
|
|
341
|
+
let headCount = alignedHeadLines(lines, plan.headChars);
|
|
342
|
+
if (headCount === 0 && plan.headChars > 0 && lines.length > 0) {
|
|
343
|
+
// A first line longer than the head budget: taking it whole keeps a real line
|
|
344
|
+
// where a cut could only produce a fragment. Whether the enlarged form is
|
|
345
|
+
// installable at all is decided by the size checks below.
|
|
346
|
+
headCount = 1;
|
|
347
|
+
}
|
|
348
|
+
let tailCount = alignedTailLines(lines, plan.tailChars);
|
|
349
|
+
if (headCount + tailCount > lines.length)
|
|
350
|
+
return refusedFloor(original, candidate, marker);
|
|
351
|
+
let composed = composeFloorLines(lines, headCount, tailCount, candidate, marker, secondMarker);
|
|
352
|
+
// The share is a promise, and rounding to a boundary or dropping duplicates can
|
|
353
|
+
// fall short of it: widen by whole lines until the form really carries the
|
|
354
|
+
// share. A payload with no room left is refused by the checks below, never
|
|
355
|
+
// installed short.
|
|
356
|
+
while (composed.text.length < target) {
|
|
357
|
+
// Grow the smaller side first, so the two keep their configured proportion.
|
|
358
|
+
const order = composed.headChars <= composed.tailChars ? [0, 1] : [1, 0];
|
|
359
|
+
let grew = false;
|
|
360
|
+
for (const side of order) {
|
|
361
|
+
const nextHead = side === 0 ? headCount + 1 : headCount;
|
|
362
|
+
const nextTail = side === 1 ? tailCount + 1 : tailCount;
|
|
363
|
+
if (nextHead + nextTail > lines.length)
|
|
364
|
+
continue;
|
|
365
|
+
const next = composeFloorLines(lines, nextHead, nextTail, candidate, marker, secondMarker);
|
|
366
|
+
if (next.text.length <= composed.text.length)
|
|
367
|
+
continue;
|
|
368
|
+
headCount = nextHead;
|
|
369
|
+
tailCount = nextTail;
|
|
370
|
+
composed = next;
|
|
371
|
+
grew = true;
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
if (!grew)
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
if (composed.text.length >= original.length || composed.text.length < target) {
|
|
378
|
+
return refusedFloor(original, candidate, marker);
|
|
379
|
+
}
|
|
380
|
+
return {
|
|
381
|
+
...plan,
|
|
382
|
+
resultChars: composed.text.length,
|
|
383
|
+
omittedChars: original.length - composed.text.length,
|
|
384
|
+
headChars: composed.headChars,
|
|
385
|
+
tailChars: composed.tailChars,
|
|
386
|
+
markerChars: composed.markerChars,
|
|
387
|
+
text: composed.text
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
//# sourceMappingURL=prune-plan.js.map
|