@hydraharness/harness-command-compact 0.1.1-rc.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +64 -0
- package/lib/index.js +100 -0
- package/lib/invariant.js +23 -0
- package/lib/types/index.d.ts +13 -0
- package/lib/types/invariant.d.ts +16 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# @hydraharness/harness-command-compact
|
|
2
|
+
|
|
3
|
+
Human-facing `/compact` control over [`ctx.compaction`](../compaction/README.md). The plugin registers one global command through [`ctx.commands`](../../interaction/commands/README.md), so every composed command adapter discovers and executes it without a model turn. The [queued manual compaction Agent Note](../../../.agents/notes/implemented/feature/2026-07-30-queued-manual-compaction.md) owns the admission, lock, and durability decisions.
|
|
4
|
+
|
|
5
|
+
## Command contract
|
|
6
|
+
|
|
7
|
+
| Input | Result |
|
|
8
|
+
|---|---|
|
|
9
|
+
| `/compact` | Summarize one useful balanced older span even below automatic pressure, then report the replaced history-item count and estimated tokens after the standalone bracket is flushed. |
|
|
10
|
+
| `/compact` with no compactable history | `No compactable history yet.` — no marker or surface mutation is written. |
|
|
11
|
+
| `/compact <anything>` | `Usage: /compact (no arguments)` — the command takes no arguments and calls no compaction backend. |
|
|
12
|
+
|
|
13
|
+
The command is backend-independent: it depends only on `compactNow(agent, signal)`. The invoking agent is the exact target, and the dispatching UI's cancellation signal is forwarded through the seam. Every resolved invocation records the executor-owned log-only pair `command/run` / `command/done`; neither event joins model history. On success, `command/done.sourceEventSeq` names the transaction's `compaction/summary` event so a presentation can fold the command lifecycle into its checkpoint without parsing result text or assuming adjacent rows.
|
|
14
|
+
|
|
15
|
+
Expected `ManualCompactionError` codes become stable direct errors:
|
|
16
|
+
|
|
17
|
+
| Code | Direct result |
|
|
18
|
+
|---|---|
|
|
19
|
+
| `busy` | `Compaction is unavailable because this process has an active compaction, or the agent is not idle.` |
|
|
20
|
+
| `changed` | `The history selected for compaction changed before it could be replaced. The conversation is unchanged; the attempt is recorded in the session log.` |
|
|
21
|
+
| `summary` | `Compaction could not produce a useful summary. The conversation is unchanged; the attempt is recorded in the session log.` |
|
|
22
|
+
| `commit` | `Compaction did not finish cleanly; some session history may have changed. Inspect the current session state before retrying.` |
|
|
23
|
+
| `persistence` | `Compaction finished, but the session could not be saved.` |
|
|
24
|
+
|
|
25
|
+
The busy result is intentionally process-scoped: a live unmatched marker blocks, while a marker older than the newest `session/end-seed` is stale and does not. Unexpected implementation failures reject dispatch. Cancellation remains authoritative; the backend completes its required close/flush cleanup, and the command settles internally as `Compaction cancelled.` while the command executor stops waiting with its cancellation error. Plugin disposal first unregisters `/compact`, then drains every handler that already started, so root teardown cannot pass an aborted command's close or flush boundary.
|
|
26
|
+
|
|
27
|
+
Prompts submitted while compaction runs remain accepted in the agent's ordinary FIFO with the same identity and wakeup facts. They start only after the compaction's explicit durability checkpoint and admission release. Idle injected context is not held: it may be logged between `compaction/start` and `compaction/end`, and positional replacement leaves it visible after the checkpoint.
|
|
28
|
+
|
|
29
|
+
## Composition
|
|
30
|
+
|
|
31
|
+
The producer injects `commands` and `compact`. Mount the command registry, one backend, and this plugin:
|
|
32
|
+
|
|
33
|
+
```yaml
|
|
34
|
+
- id: commands
|
|
35
|
+
name: '@hydraharness/harness-commands'
|
|
36
|
+
- id: compaction-basic
|
|
37
|
+
name: '@hydraharness/harness-compaction-basic'
|
|
38
|
+
- id: command-compact
|
|
39
|
+
name: '@hydraharness/harness-command-compact'
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The shipped `hydra` base mounts it beside `compaction-basic`, and the Web client provides the command adapter. Automation surfaces that compose no command adapter keep automatic compaction only.
|
|
43
|
+
|
|
44
|
+
## Model Experience
|
|
45
|
+
|
|
46
|
+
### Human `/compact` control
|
|
47
|
+
|
|
48
|
+
#### What the model sees
|
|
49
|
+
|
|
50
|
+
The slash input and direct result never enter a model request. An accepted compaction separately replaces an older span with the backend's user-role checkpoint inside a standalone `compaction/* { turn: null }` bracket.
|
|
51
|
+
|
|
52
|
+
#### Token effect
|
|
53
|
+
|
|
54
|
+
The command lifecycle adds no model tokens. A successful compaction reduces later requests by replacing the selected span with one framed summary; summarization itself is one auxiliary request.
|
|
55
|
+
|
|
56
|
+
#### KV Cache effect
|
|
57
|
+
|
|
58
|
+
Discovery and command bookkeeping do not affect the cache. The accepted surface replacement invalidates reuse from the first shadowed history token.
|
|
59
|
+
|
|
60
|
+
## Known Limitations and Deferred Work
|
|
61
|
+
|
|
62
|
+
- **Idle-only** — `/compact` reports `busy` when a turn or already accepted waking prompt has right of way; the command itself is not queued.
|
|
63
|
+
- **No range or policy arguments** — the argument-free form keeps behavior stable across command adapters. Explicit ranges remain the programmatic `compactRegion()` path.
|
|
64
|
+
- **Command adapters only** — surfaces without `ctx.commands` cannot invoke it and rely on automatic pressure compaction.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { ManualCompactionError } from "@hydraharness/harness-compaction";
|
|
2
|
+
//#region lib/types/index.js
|
|
3
|
+
/**
|
|
4
|
+
* Human-facing `/compact` command over the backend-independent compaction seam.
|
|
5
|
+
* @module @hydraharness/harness-command-compact
|
|
6
|
+
*/
|
|
7
|
+
const name = "command-compact";
|
|
8
|
+
const inject = ["commands", "compaction"];
|
|
9
|
+
const USAGE = "Usage: /compact (no arguments)";
|
|
10
|
+
/** Fail loudly if a locally closed union gains an unhandled member. */
|
|
11
|
+
/* v8 ignore start -- closed-union backstop is unreachable without violating the TypeScript contract */
|
|
12
|
+
function assertNever(value) {
|
|
13
|
+
throw new TypeError(`unknown manual compaction error code: ${String(value)}`);
|
|
14
|
+
}
|
|
15
|
+
/* v8 ignore stop */
|
|
16
|
+
/** Convert expected capability failures into concise human-only outcomes. */
|
|
17
|
+
function expectedFailure(error) {
|
|
18
|
+
switch (error.code) {
|
|
19
|
+
case "busy": return {
|
|
20
|
+
kind: "error",
|
|
21
|
+
text: "Compaction is unavailable because this process has an active compaction, or the agent is not idle."
|
|
22
|
+
};
|
|
23
|
+
case "cancelled": return {
|
|
24
|
+
kind: "error",
|
|
25
|
+
text: "Compaction cancelled."
|
|
26
|
+
};
|
|
27
|
+
case "changed": return {
|
|
28
|
+
kind: "error",
|
|
29
|
+
text: "The history selected for compaction changed before it could be replaced. The conversation is unchanged; the attempt is recorded in the session log."
|
|
30
|
+
};
|
|
31
|
+
case "summary": return {
|
|
32
|
+
kind: "error",
|
|
33
|
+
text: "Compaction could not produce a useful summary. The conversation is unchanged; the attempt is recorded in the session log."
|
|
34
|
+
};
|
|
35
|
+
case "commit": return {
|
|
36
|
+
kind: "error",
|
|
37
|
+
text: "Compaction did not finish cleanly; some session history may have changed. Inspect the current session state before retrying."
|
|
38
|
+
};
|
|
39
|
+
case "persistence": return {
|
|
40
|
+
kind: "error",
|
|
41
|
+
text: "Compaction finished, but the session could not be saved."
|
|
42
|
+
};
|
|
43
|
+
/* v8 ignore next 2 -- ManualCompactionErrorCode is closed and every member is handled above */
|
|
44
|
+
default: return assertNever(error.code);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** Execute one argument-free manual compaction request. */
|
|
48
|
+
async function executeCompact(ctx, invocation) {
|
|
49
|
+
if (invocation.rawInput.trim().length > 0) return {
|
|
50
|
+
kind: "error",
|
|
51
|
+
text: USAGE
|
|
52
|
+
};
|
|
53
|
+
try {
|
|
54
|
+
const result = await ctx.compaction.compactNow(invocation.agent, invocation.signal, invocation.commandId);
|
|
55
|
+
if (result === null) return {
|
|
56
|
+
kind: "success",
|
|
57
|
+
text: "No compactable history yet."
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
kind: "success",
|
|
61
|
+
text: `Compacted ${result.shadowedSeqs.length} history items (~${result.shadowedTokenCount} tokens).`,
|
|
62
|
+
sourceEventSeq: result.summarySeq
|
|
63
|
+
};
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (invocation.signal.aborted) return {
|
|
66
|
+
kind: "error",
|
|
67
|
+
text: "Compaction cancelled."
|
|
68
|
+
};
|
|
69
|
+
if (error instanceof ManualCompactionError) return expectedFailure(error);
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Register `/compact` for every composed human-command adapter.
|
|
75
|
+
* @param ctx - context carrying the command registry and the compaction seam.
|
|
76
|
+
*/
|
|
77
|
+
function apply(ctx) {
|
|
78
|
+
const active = /* @__PURE__ */ new Set();
|
|
79
|
+
const handler = (invocation) => {
|
|
80
|
+
const operation = executeCompact(ctx, invocation);
|
|
81
|
+
active.add(operation);
|
|
82
|
+
const retire = () => {
|
|
83
|
+
active.delete(operation);
|
|
84
|
+
};
|
|
85
|
+
operation.then(retire, retire);
|
|
86
|
+
return operation;
|
|
87
|
+
};
|
|
88
|
+
ctx.effect(function* () {
|
|
89
|
+
yield async () => {
|
|
90
|
+
await Promise.allSettled(active);
|
|
91
|
+
};
|
|
92
|
+
yield ctx.commands.register({
|
|
93
|
+
name: "compact",
|
|
94
|
+
description: "Compact older conversation history",
|
|
95
|
+
handler
|
|
96
|
+
});
|
|
97
|
+
}, "command-compact lifecycle");
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
export { apply, inject, name };
|
package/lib/invariant.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region lib/types/invariant.js
|
|
2
|
+
/**
|
|
3
|
+
* Package-owned invariant companion for `@hydraharness/harness-command-compact`.
|
|
4
|
+
* @module @hydraharness/harness-command-compact/invariant
|
|
5
|
+
*/
|
|
6
|
+
const PACKAGE_NAME = "@hydraharness/harness-command-compact";
|
|
7
|
+
/** Cordis companion plugin name. */
|
|
8
|
+
const name = "command-compact-invariant";
|
|
9
|
+
/** Service required before the companion can reserve package ownership. */
|
|
10
|
+
const inject = ["invariants"];
|
|
11
|
+
/**
|
|
12
|
+
* No runtime invariant: this command adapter owns no state or event stream; the compaction seam owns
|
|
13
|
+
* the balanced durable transaction and the command registry owns registration and dispatch lifecycle.
|
|
14
|
+
*/
|
|
15
|
+
const install = () => {};
|
|
16
|
+
/**
|
|
17
|
+
* Register this package's invariant companion.
|
|
18
|
+
* @param ctx - Cordis context carrying the invariant service.
|
|
19
|
+
* @returns the installed registration's disposer after setup succeeds.
|
|
20
|
+
*/
|
|
21
|
+
const apply = (ctx) => Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install));
|
|
22
|
+
//#endregion
|
|
23
|
+
export { apply, inject, name };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Human-facing `/compact` command over the backend-independent compaction seam.
|
|
3
|
+
* @module @hydraharness/harness-command-compact
|
|
4
|
+
*/
|
|
5
|
+
import type { Context } from '@hydraharness/cordis';
|
|
6
|
+
export declare const name = "command-compact";
|
|
7
|
+
export declare const inject: string[];
|
|
8
|
+
/**
|
|
9
|
+
* Register `/compact` for every composed human-command adapter.
|
|
10
|
+
* @param ctx - context carrying the command registry and the compaction seam.
|
|
11
|
+
*/
|
|
12
|
+
export declare function apply(ctx: Context): void;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package-owned invariant companion for `@hydraharness/harness-command-compact`.
|
|
3
|
+
* @module @hydraharness/harness-command-compact/invariant
|
|
4
|
+
*/
|
|
5
|
+
import type { Context } from '@hydraharness/cordis';
|
|
6
|
+
/** Cordis companion plugin name. */
|
|
7
|
+
export declare const name = "command-compact-invariant";
|
|
8
|
+
/** Service required before the companion can reserve package ownership. */
|
|
9
|
+
export declare const inject: string[];
|
|
10
|
+
/**
|
|
11
|
+
* Register this package's invariant companion.
|
|
12
|
+
* @param ctx - Cordis context carrying the invariant service.
|
|
13
|
+
* @returns the installed registration's disposer after setup succeeds.
|
|
14
|
+
*/
|
|
15
|
+
export declare const apply: (ctx: Context) => Promise<() => void>;
|
|
16
|
+
//# sourceMappingURL=invariant.d.ts.map
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hydraharness/harness-command-compact",
|
|
3
|
+
"description": "Human-facing slash command for explicit session compaction",
|
|
4
|
+
"hydra": {
|
|
5
|
+
"plugin": {
|
|
6
|
+
"application": "Use /compact to summarize a long conversation and reduce its active model context."
|
|
7
|
+
}
|
|
8
|
+
},
|
|
9
|
+
"version": "0.1.1-rc.5",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/MaiHongPhong1902/Hydra-Harness.git",
|
|
16
|
+
"directory": "packages/compaction/command-compact"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "lib/index.js",
|
|
20
|
+
"types": "lib/types/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./lib/types/index.d.ts",
|
|
24
|
+
"default": "./lib/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./invariant": {
|
|
27
|
+
"types": "./lib/types/invariant.d.ts",
|
|
28
|
+
"default": "./lib/invariant.js"
|
|
29
|
+
},
|
|
30
|
+
"./src/*": "./src/*",
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"lib/index.js",
|
|
35
|
+
"lib/invariant.js",
|
|
36
|
+
"lib/types/**/*.d.ts"
|
|
37
|
+
],
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@hydraharness/harness-commands": "^0.1.1-rc.5",
|
|
41
|
+
"@hydraharness/harness-invariants": "^0.1.1-rc.5",
|
|
42
|
+
"@hydraharness/cordis": "^4.0.2",
|
|
43
|
+
"@hydraharness/harness-compaction": "^0.1.1-rc.5"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@hydraharness/cordis-plugin-include": "^1.0.7",
|
|
47
|
+
"@hydraharness/cordis-plugin-loader": "^1.0.3",
|
|
48
|
+
"@hydraharness/harness-agent": "^0.1.1-rc.5",
|
|
49
|
+
"@hydraharness/harness-compaction": "^0.1.1-rc.5",
|
|
50
|
+
"@hydraharness/harness-commands": "^0.1.1-rc.5",
|
|
51
|
+
"@hydraharness/harness-invariants": "^0.1.1-rc.5",
|
|
52
|
+
"@hydraharness/harness-llm": "^0.1.1-rc.5",
|
|
53
|
+
"@hydraharness/cordis": "^4.0.2",
|
|
54
|
+
"@hydraharness/harness-session": "^0.1.1-rc.5"
|
|
55
|
+
}
|
|
56
|
+
}
|