@openclaw/tokenjuice 2026.6.8 → 2026.6.9-beta.1
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/tool-result-middleware.js +47 -1
- package/node_modules/tokenjuice/dist/cli/main.js +1 -0
- package/node_modules/tokenjuice/dist/core/command-identity.d.ts +1 -0
- package/node_modules/tokenjuice/dist/core/command-identity.js +97 -2
- package/node_modules/tokenjuice/dist/core/command-match.d.ts +1 -0
- package/node_modules/tokenjuice/dist/core/command-match.js +31 -1
- package/node_modules/tokenjuice/dist/core/command.d.ts +2 -2
- package/node_modules/tokenjuice/dist/core/command.js +2 -2
- package/node_modules/tokenjuice/dist/core/compaction-metadata.d.ts +1 -1
- package/node_modules/tokenjuice/dist/core/compaction-metadata.js +1 -1
- package/node_modules/tokenjuice/dist/core/reduce.js +21 -7
- package/node_modules/tokenjuice/dist/hosts/amazon-q/index.js +1 -2
- package/node_modules/tokenjuice/dist/hosts/copilot-agent/index.js +3 -0
- package/node_modules/tokenjuice/dist/hosts/copilot-cli/index.js +18 -14
- package/node_modules/tokenjuice/dist/hosts/crush/index.js +1 -3
- package/node_modules/tokenjuice/dist/hosts/gemini-cli/index.js +12 -4
- package/node_modules/tokenjuice/dist/hosts/kimi/index.js +24 -4
- package/node_modules/tokenjuice/dist/hosts/mcp-agent/index.js +9 -3
- package/node_modules/tokenjuice/dist/hosts/opencode/extension/runtime.js +124 -5
- package/node_modules/tokenjuice/dist/hosts/pi/extension/runtime.js +124 -5
- package/node_modules/tokenjuice/package.json +4 -4
- package/npm-shrinkwrap.json +7 -7
- package/package.json +5 -5
|
@@ -1,12 +1,58 @@
|
|
|
1
1
|
import { createTokenjuiceOpenClawEmbeddedExtension } from "./runtime-api.js";
|
|
2
2
|
import process from "node:process";
|
|
3
3
|
//#region extensions/tokenjuice/tool-result-middleware.ts
|
|
4
|
+
function isRecord(value) {
|
|
5
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
6
|
+
}
|
|
4
7
|
function readCwd(event) {
|
|
5
8
|
if (event.cwd?.trim()) return event.cwd;
|
|
6
9
|
const workdir = event.args.workdir;
|
|
7
10
|
if (typeof workdir === "string" && workdir.trim()) return workdir;
|
|
8
11
|
return process.cwd();
|
|
9
12
|
}
|
|
13
|
+
function readTextContent(content) {
|
|
14
|
+
if (typeof content === "string") return content;
|
|
15
|
+
if (!Array.isArray(content)) return "";
|
|
16
|
+
return content.map((item) => {
|
|
17
|
+
if (typeof item === "string") return item;
|
|
18
|
+
if (item && typeof item === "object" && "text" in item && typeof item.text === "string") return item.text;
|
|
19
|
+
return "";
|
|
20
|
+
}).filter(Boolean).join("\n");
|
|
21
|
+
}
|
|
22
|
+
function readCommand(args) {
|
|
23
|
+
const command = args.command;
|
|
24
|
+
return typeof command === "string" ? command : "";
|
|
25
|
+
}
|
|
26
|
+
function hasStatus(details) {
|
|
27
|
+
const status = details.status;
|
|
28
|
+
return typeof status === "string" && status.trim() !== "";
|
|
29
|
+
}
|
|
30
|
+
function hasFailureState(event, details) {
|
|
31
|
+
const exitCode = details?.exitCode;
|
|
32
|
+
return event.isError === true || details?.ok === false || details?.success === false || details?.timedOut === true || Boolean(details?.error) || typeof exitCode === "number" && Number.isFinite(exitCode) && exitCode !== 0;
|
|
33
|
+
}
|
|
34
|
+
function normalizeDetails(event, current) {
|
|
35
|
+
const isExecLike = event.toolName === "exec" || event.toolName === "bash";
|
|
36
|
+
const details = isRecord(current.details) ? current.details : void 0;
|
|
37
|
+
if (!isExecLike || !readCommand(event.args)) return current.details;
|
|
38
|
+
if (details && hasStatus(details)) return current.details;
|
|
39
|
+
const aggregated = readTextContent(current.content);
|
|
40
|
+
if (!aggregated.trim()) return current.details;
|
|
41
|
+
const failed = hasFailureState(event, details);
|
|
42
|
+
const existingExitCode = details?.exitCode;
|
|
43
|
+
const synthesized = {
|
|
44
|
+
status: failed ? "failed" : "completed",
|
|
45
|
+
aggregated,
|
|
46
|
+
exitCode: typeof existingExitCode === "number" && Number.isFinite(existingExitCode) ? existingExitCode : failed ? 1 : 0
|
|
47
|
+
};
|
|
48
|
+
if (!details) return synthesized;
|
|
49
|
+
return {
|
|
50
|
+
...synthesized,
|
|
51
|
+
...details,
|
|
52
|
+
status: synthesized.status,
|
|
53
|
+
exitCode: synthesized.exitCode
|
|
54
|
+
};
|
|
55
|
+
}
|
|
10
56
|
function createTokenjuiceAgentToolResultMiddleware() {
|
|
11
57
|
const handlers = [];
|
|
12
58
|
createTokenjuiceOpenClawEmbeddedExtension()({ on(event, handler) {
|
|
@@ -19,7 +65,7 @@ function createTokenjuiceAgentToolResultMiddleware() {
|
|
|
19
65
|
toolName: event.toolName,
|
|
20
66
|
input: event.args,
|
|
21
67
|
content: current.content,
|
|
22
|
-
details: current
|
|
68
|
+
details: normalizeDetails(event, current),
|
|
23
69
|
isError: event.isError
|
|
24
70
|
}, { cwd: readCwd(event) });
|
|
25
71
|
if (next) current = Object.assign({}, current, {
|
|
@@ -650,6 +650,7 @@ export function decorateWrapInlineText(result, raw) {
|
|
|
650
650
|
"",
|
|
651
651
|
"---",
|
|
652
652
|
WRAP_AUTHORITATIVE_FOOTER,
|
|
653
|
+
...(result.rawRef?.id ? [`Raw output is available locally: tokenjuice cat ${result.rawRef.id}`] : []),
|
|
653
654
|
].join("\n");
|
|
654
655
|
return `${result.inlineText}${footer}`;
|
|
655
656
|
}
|
|
@@ -4,6 +4,7 @@ export declare function getGitSubcommand(argv: string[]): string | null;
|
|
|
4
4
|
export declare function isFileContentInspectionArgv(argv: string[]): boolean;
|
|
5
5
|
export declare function isRepositoryInspectionArgv(argv: string[]): boolean;
|
|
6
6
|
export declare function isFileContentInspectionCommand(input: Pick<ToolExecutionInput, "argv" | "command">): boolean;
|
|
7
|
+
export declare function isVerbatimConfigInspectionCommand(input: Pick<ToolExecutionInput, "argv" | "command">): boolean;
|
|
7
8
|
export declare function isRepositoryInspectionCommand(input: Pick<ToolExecutionInput, "argv" | "command">): boolean;
|
|
8
9
|
export declare function normalizeCommandSignature(command?: string): string | null;
|
|
9
10
|
export declare function normalizeEffectiveCommandSignature(command?: string): string | null;
|
|
@@ -1,8 +1,34 @@
|
|
|
1
1
|
import { basename } from "node:path";
|
|
2
|
-
import { deriveCommandMatchCandidates, getSourcePriority } from "./command-match.js";
|
|
3
|
-
import { stripLeadingCdPrefix, tokenizeCommand } from "./command-shell.js";
|
|
2
|
+
import { deriveCommandMatchCandidates, getSourcePriority, unwrapShellRunner } from "./command-match.js";
|
|
3
|
+
import { hasSequentialShellCommands, isCompoundShellCommand, stripLeadingCdPrefix, tokenizeCommand } from "./command-shell.js";
|
|
4
4
|
const FILE_CONTENT_INSPECTION_COMMANDS = new Set(["cat", "sed", "head", "tail", "nl", "bat", "batcat", "jq", "yq"]);
|
|
5
5
|
const REPO_INVENTORY_COMMANDS = new Set(["find", "fd", "fdfind", "ls", "tree"]);
|
|
6
|
+
// ssh options that consume a separate value argument (per ssh(1)); needed to
|
|
7
|
+
// find where the destination ends and the remote command begins.
|
|
8
|
+
const SSH_OPTIONS_WITH_VALUES = new Set([
|
|
9
|
+
"-B",
|
|
10
|
+
"-b",
|
|
11
|
+
"-c",
|
|
12
|
+
"-D",
|
|
13
|
+
"-E",
|
|
14
|
+
"-e",
|
|
15
|
+
"-F",
|
|
16
|
+
"-I",
|
|
17
|
+
"-i",
|
|
18
|
+
"-J",
|
|
19
|
+
"-L",
|
|
20
|
+
"-l",
|
|
21
|
+
"-m",
|
|
22
|
+
"-O",
|
|
23
|
+
"-o",
|
|
24
|
+
"-P",
|
|
25
|
+
"-p",
|
|
26
|
+
"-Q",
|
|
27
|
+
"-R",
|
|
28
|
+
"-S",
|
|
29
|
+
"-W",
|
|
30
|
+
"-w",
|
|
31
|
+
]);
|
|
6
32
|
function getNormalizedArgv(input) {
|
|
7
33
|
if (input.argv?.length) {
|
|
8
34
|
return input.argv;
|
|
@@ -89,6 +115,45 @@ function isGitShowFileContentArgv(argv) {
|
|
|
89
115
|
}
|
|
90
116
|
return false;
|
|
91
117
|
}
|
|
118
|
+
function isPlutilFileContentArgv(argv) {
|
|
119
|
+
if (getCommandName(argv) !== "plutil") {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
if (argv.includes("-p")) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
const outputIndex = argv.indexOf("-o");
|
|
126
|
+
return outputIndex !== -1 && argv[outputIndex + 1] === "-";
|
|
127
|
+
}
|
|
128
|
+
function isReadOnlyConfigInspectionArgv(argv) {
|
|
129
|
+
return getCommandName(argv) === "openclaw"
|
|
130
|
+
&& argv[1] === "config"
|
|
131
|
+
&& argv[2] === "get";
|
|
132
|
+
}
|
|
133
|
+
function getSshRemoteCommand(argv) {
|
|
134
|
+
if (getCommandName(argv) !== "ssh") {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
for (let index = 1; index < argv.length; index += 1) {
|
|
138
|
+
const arg = argv[index];
|
|
139
|
+
if (!arg) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (arg === "--") {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (SSH_OPTIONS_WITH_VALUES.has(arg)) {
|
|
146
|
+
index += 1;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (arg.startsWith("-")) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const remoteCommand = argv.slice(index + 1).join(" ").trim();
|
|
153
|
+
return remoteCommand || null;
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
92
157
|
export function isFileContentInspectionArgv(argv) {
|
|
93
158
|
const argv0 = getCommandName(argv);
|
|
94
159
|
if (!argv0) {
|
|
@@ -177,6 +242,36 @@ export function isFileContentInspectionCommand(input) {
|
|
|
177
242
|
return isFileContentInspectionArgv(getInspectionArgv(input))
|
|
178
243
|
|| deriveCommandMatchCandidates(input).some((candidate) => isGhApiContentsDecodeCommand(candidate.command));
|
|
179
244
|
}
|
|
245
|
+
function isVerbatimRemoteInspectionCommand(command) {
|
|
246
|
+
const effectiveCommand = unwrapShellRunner({ command }) ?? command;
|
|
247
|
+
const isSingleGhContentsDecode = isGhApiContentsDecodeCommand(effectiveCommand)
|
|
248
|
+
&& !hasSequentialShellCommands(effectiveCommand)
|
|
249
|
+
&& /^[^|]+\|\s*base64\s+(?:-[dD]\b|--decode\b)\s*$/u.test(effectiveCommand.trim());
|
|
250
|
+
if (isSingleGhContentsDecode) {
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
if (isCompoundShellCommand(stripLeadingCdPrefix(command))
|
|
254
|
+
|| isCompoundShellCommand(effectiveCommand)) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
const argv = getInspectionArgv({ command: effectiveCommand });
|
|
258
|
+
return isPlutilFileContentArgv(argv)
|
|
259
|
+
|| isReadOnlyConfigInspectionArgv(argv)
|
|
260
|
+
|| isFileContentInspectionArgv(argv);
|
|
261
|
+
}
|
|
262
|
+
export function isVerbatimConfigInspectionCommand(input) {
|
|
263
|
+
if (input.command && isCompoundShellCommand(stripLeadingCdPrefix(input.command))) {
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
const candidates = deriveCommandMatchCandidates(input);
|
|
267
|
+
return candidates.some((candidate) => (isPlutilFileContentArgv(candidate.argv)
|
|
268
|
+
|| isReadOnlyConfigInspectionArgv(candidate.argv)))
|
|
269
|
+
|| candidates.some((candidate) => {
|
|
270
|
+
const remoteCommand = getSshRemoteCommand(candidate.argv);
|
|
271
|
+
return remoteCommand !== null
|
|
272
|
+
&& isVerbatimRemoteInspectionCommand(remoteCommand);
|
|
273
|
+
});
|
|
274
|
+
}
|
|
180
275
|
export function isRepositoryInspectionCommand(input) {
|
|
181
276
|
return isRepositoryInspectionArgv(getMostDerivedCandidate(input).argv);
|
|
182
277
|
}
|
|
@@ -12,5 +12,6 @@ export declare function stripLeadingEnvAssignments(argv: string[]): string[];
|
|
|
12
12
|
export declare function isSetupWrapperSegment(argv: string[], command?: string): boolean;
|
|
13
13
|
export declare function buildEffectiveCandidate(argv: string[], transformed: boolean, command?: string): CommandMatchCandidate | null;
|
|
14
14
|
export declare function resolveEffectiveCommand(input: Pick<ToolExecutionInput, "argv" | "command">): CommandMatchCandidate | null;
|
|
15
|
+
export declare function hasMultipleSubstantiveShellCommands(input: Pick<ToolExecutionInput, "argv" | "command">): boolean;
|
|
15
16
|
export declare function getEffectiveCommandArgv(input: Pick<ToolExecutionInput, "argv" | "command">): string[];
|
|
16
17
|
export declare function deriveCommandMatchCandidates(input: Pick<ToolExecutionInput, "argv" | "command">): CommandMatchCandidate[];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ENV_ASSIGNMENT_PATTERN, isCompoundShellCommand, splitTopLevelCommandChain, stripLeadingEnvAssignmentsFromCommand, tokenizeCommand, } from "./command-shell.js";
|
|
1
|
+
import { ENV_ASSIGNMENT_PATTERN, hasSequentialShellCommands, isCompoundShellCommand, splitTopLevelCommandChain, stripLeadingEnvAssignmentsFromCommand, tokenizeCommand, } from "./command-shell.js";
|
|
2
2
|
const SETUP_WRAPPER_COMMANDS = new Set(["cd", "pwd", "set", "source", ".", "export", "unset", "trap", "true"]);
|
|
3
3
|
const SHELL_COMMAND_LAUNCHERS = new Set(["bash", "sh", "zsh", "fish"]);
|
|
4
4
|
const ENV_FLAGS_WITH_VALUES = new Set(["-u", "--unset", "-C", "--chdir", "-S", "--split-string"]);
|
|
@@ -426,6 +426,36 @@ export function resolveEffectiveCommand(input) {
|
|
|
426
426
|
}
|
|
427
427
|
return null;
|
|
428
428
|
}
|
|
429
|
+
export function hasMultipleSubstantiveShellCommands(input) {
|
|
430
|
+
const shellBody = unwrapShellRunner(input);
|
|
431
|
+
const command = (shellBody ?? input.command ?? "").trim();
|
|
432
|
+
if (!command || !hasSequentialShellCommands(command)) {
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
let effectiveCommand = command;
|
|
436
|
+
for (let iteration = 0; iteration < 16; iteration += 1) {
|
|
437
|
+
const setupIfTail = stripLeadingSetupIfBlock(effectiveCommand);
|
|
438
|
+
const setupSegmentTail = setupIfTail ?? stripLeadingSetupSegment(effectiveCommand);
|
|
439
|
+
if (!setupSegmentTail) {
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
effectiveCommand = setupSegmentTail;
|
|
443
|
+
}
|
|
444
|
+
let substantiveCount = 0;
|
|
445
|
+
for (const sequenceSegment of splitTopLevelCommandChain(effectiveCommand)) {
|
|
446
|
+
for (const segment of splitTopLevelOrChain(sequenceSegment)) {
|
|
447
|
+
const candidate = buildEffectiveCandidate(tokenizeCommand(segment), true, segment);
|
|
448
|
+
if (!candidate) {
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
substantiveCount += 1;
|
|
452
|
+
if (substantiveCount > 1) {
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
429
459
|
export function getEffectiveCommandArgv(input) {
|
|
430
460
|
return resolveEffectiveCommand(input)?.argv ?? getCandidateArgv(input);
|
|
431
461
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { hasSequentialShellCommands, isCompoundShellCommand, splitTopLevelCommandChain, stripLeadingCdPrefix, tokenizeCommand, } from "./command-shell.js";
|
|
2
2
|
export type { CommandMatchCandidate } from "./command-match.js";
|
|
3
|
-
export { deriveCommandMatchCandidates, getEffectiveCommandArgv, isSetupWrapperSegment, resolveEffectiveCommand, stripLeadingEnvAssignments, unwrapShellRunner, } from "./command-match.js";
|
|
4
|
-
export { getCommandName, getGitSubcommand, isFileContentInspectionCommand, isRepositoryInspectionCommand, normalizeCommandSignature, normalizeEffectiveCommandSignature, } from "./command-identity.js";
|
|
3
|
+
export { deriveCommandMatchCandidates, getEffectiveCommandArgv, hasMultipleSubstantiveShellCommands, isSetupWrapperSegment, resolveEffectiveCommand, stripLeadingEnvAssignments, unwrapShellRunner, } from "./command-match.js";
|
|
4
|
+
export { getCommandName, getGitSubcommand, isFileContentInspectionCommand, isRepositoryInspectionCommand, isVerbatimConfigInspectionCommand, normalizeCommandSignature, normalizeEffectiveCommandSignature, } from "./command-identity.js";
|
|
5
5
|
export { normalizeExecutionInput } from "./execution-input.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { hasSequentialShellCommands, isCompoundShellCommand, splitTopLevelCommandChain, stripLeadingCdPrefix, tokenizeCommand, } from "./command-shell.js";
|
|
2
|
-
export { deriveCommandMatchCandidates, getEffectiveCommandArgv, isSetupWrapperSegment, resolveEffectiveCommand, stripLeadingEnvAssignments, unwrapShellRunner, } from "./command-match.js";
|
|
3
|
-
export { getCommandName, getGitSubcommand, isFileContentInspectionCommand, isRepositoryInspectionCommand, normalizeCommandSignature, normalizeEffectiveCommandSignature, } from "./command-identity.js";
|
|
2
|
+
export { deriveCommandMatchCandidates, getEffectiveCommandArgv, hasMultipleSubstantiveShellCommands, isSetupWrapperSegment, resolveEffectiveCommand, stripLeadingEnvAssignments, unwrapShellRunner, } from "./command-match.js";
|
|
3
|
+
export { getCommandName, getGitSubcommand, isFileContentInspectionCommand, isRepositoryInspectionCommand, isVerbatimConfigInspectionCommand, normalizeCommandSignature, normalizeEffectiveCommandSignature, } from "./command-identity.js";
|
|
4
4
|
export { normalizeExecutionInput } from "./execution-input.js";
|
|
@@ -4,7 +4,7 @@ export type CompactionMetadata = {
|
|
|
4
4
|
kinds: CompactionKind[];
|
|
5
5
|
};
|
|
6
6
|
export declare const NO_COMPACTION_METADATA: CompactionMetadata;
|
|
7
|
-
export declare const WRAP_AUTHORITATIVE_FOOTER = "[tokenjuice] This is the complete, authoritative output for this command. It was deterministically compacted to remove low-signal noise
|
|
7
|
+
export declare const WRAP_AUTHORITATIVE_FOOTER = "[tokenjuice] This is the complete, authoritative output for this command. It was deterministically compacted to remove low-signal noise. Do not re-run the command, vary flags, or switch tools to try to recover omitted content; use a raw-artifact recovery command below when one is provided. Proceed with the task using this output.";
|
|
8
8
|
export declare function createCompactionMetadata(...kinds: CompactionKind[]): CompactionMetadata;
|
|
9
9
|
export declare function createPassthroughCompactionMetadata(...kinds: CompactionKind[]): CompactionMetadata;
|
|
10
10
|
export declare function mergeCompactionMetadata(...values: Array<CompactionMetadata | undefined>): CompactionMetadata;
|
|
@@ -2,7 +2,7 @@ export const NO_COMPACTION_METADATA = {
|
|
|
2
2
|
authoritative: false,
|
|
3
3
|
kinds: [],
|
|
4
4
|
};
|
|
5
|
-
export const WRAP_AUTHORITATIVE_FOOTER = "[tokenjuice] This is the complete, authoritative output for this command. It was deterministically compacted to remove low-signal noise
|
|
5
|
+
export const WRAP_AUTHORITATIVE_FOOTER = "[tokenjuice] This is the complete, authoritative output for this command. It was deterministically compacted to remove low-signal noise. Do not re-run the command, vary flags, or switch tools to try to recover omitted content; use a raw-artifact recovery command below when one is provided. Proceed with the task using this output.";
|
|
6
6
|
function buildCompactionMetadata(authoritative, ...kinds) {
|
|
7
7
|
if (kinds.length === 0) {
|
|
8
8
|
return NO_COMPACTION_METADATA;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { loadRules } from "./rules.js";
|
|
2
|
+
import { hasMultipleSubstantiveShellCommands } from "./command-match.js";
|
|
2
3
|
import { classifyExecution, resolveRuleMatch } from "./classify.js";
|
|
3
|
-
import { isFileContentInspectionCommand } from "./command-identity.js";
|
|
4
|
+
import { isFileContentInspectionCommand, isVerbatimConfigInspectionCommand } from "./command-identity.js";
|
|
4
5
|
import { normalizeExecutionInput } from "./execution-input.js";
|
|
5
6
|
import { clampTextMiddleWithMetadata, clampTextWithMetadata, countTextChars, dedupeAdjacent, headTail, normalizeLines, pluralize, stripAnsi, trimEmptyEdges } from "./text.js";
|
|
6
7
|
import { storeArtifact, storeArtifactMetadata } from "./artifacts.js";
|
|
@@ -256,12 +257,21 @@ export async function reduceExecutionWithRules(input, rules, opts = {}) {
|
|
|
256
257
|
reducedChars,
|
|
257
258
|
ratio: measuredRawChars === 0 ? 1 : reducedChars / measuredRawChars,
|
|
258
259
|
});
|
|
259
|
-
const
|
|
260
|
+
const multipleSubstantiveCommands = !opts.classifier && hasMultipleSubstantiveShellCommands(input);
|
|
261
|
+
const resolvedMatch = opts.classifier || multipleSubstantiveCommands
|
|
260
262
|
? undefined
|
|
261
263
|
: resolveRuleMatch(input, rules);
|
|
262
|
-
const classification =
|
|
263
|
-
|
|
264
|
-
|
|
264
|
+
const classification = multipleSubstantiveCommands
|
|
265
|
+
? {
|
|
266
|
+
family: "generic",
|
|
267
|
+
confidence: 0.2,
|
|
268
|
+
matchedReducer: "generic/fallback",
|
|
269
|
+
}
|
|
270
|
+
: resolvedMatch?.classification
|
|
271
|
+
?? classifyExecution(input, rules, opts.classifier);
|
|
272
|
+
const reducerInput = multipleSubstantiveCommands
|
|
273
|
+
? normalizedInput
|
|
274
|
+
: resolvedMatch?.candidateInput ?? normalizedInput;
|
|
265
275
|
const trace = opts.trace
|
|
266
276
|
? {
|
|
267
277
|
...(normalizedInput.command ? { normalizedCommand: normalizedInput.command } : {}),
|
|
@@ -276,7 +286,9 @@ export async function reduceExecutionWithRules(input, rules, opts = {}) {
|
|
|
276
286
|
family: classification.family,
|
|
277
287
|
}
|
|
278
288
|
: undefined;
|
|
279
|
-
|
|
289
|
+
const requiresVerbatimOutput = !multipleSubstantiveCommands
|
|
290
|
+
&& isVerbatimConfigInspectionCommand(input);
|
|
291
|
+
if (opts.raw || requiresVerbatimOutput) {
|
|
280
292
|
const rawRef = opts.store
|
|
281
293
|
? await storeArtifact({
|
|
282
294
|
input: normalizedInput,
|
|
@@ -314,7 +326,9 @@ export async function reduceExecutionWithRules(input, rules, opts = {}) {
|
|
|
314
326
|
classification,
|
|
315
327
|
};
|
|
316
328
|
}
|
|
317
|
-
const inspectionSummary =
|
|
329
|
+
const inspectionSummary = multipleSubstantiveCommands
|
|
330
|
+
? null
|
|
331
|
+
: buildInspectionSummary(normalizedInput, rawText, opts.noOmit);
|
|
318
332
|
if (inspectionSummary) {
|
|
319
333
|
const summaryText = inspectionSummary.lines.join("\n").trim();
|
|
320
334
|
const selectedText = clampTextMiddleWithMetadata(summaryText, maxInlineChars, opts.noOmit);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdir, rename,
|
|
1
|
+
import { mkdir, rename, stat, writeFile } from "node:fs/promises";
|
|
2
2
|
import { dirname, join, resolve } from "node:path";
|
|
3
3
|
import { buildTokenjuiceGuidanceBullets, TOKENJUICE_FULL_COMMAND, TOKENJUICE_RAW_COMMAND, TOKENJUICE_WRAP_COMMAND, } from "../shared/instruction-guidance.js";
|
|
4
4
|
import { collectGuidanceIssues, readInstructionFile, removeInstructionFile, writeInstructionFile } from "../shared/instruction-file.js";
|
|
@@ -95,7 +95,6 @@ export async function uninstallAmazonQRule(rulePath, options = {}) {
|
|
|
95
95
|
const backupPath = `${resolvedRulePath}.bak`;
|
|
96
96
|
const backup = await readInstructionFile(backupPath);
|
|
97
97
|
if (existing.exists && backup.exists && !isTokenjuiceAmazonQRuleText(backup.text)) {
|
|
98
|
-
await rm(resolvedRulePath, { force: true });
|
|
99
98
|
await rename(backupPath, resolvedRulePath);
|
|
100
99
|
return { rulePath: resolvedRulePath, removed: true };
|
|
101
100
|
}
|
|
@@ -237,6 +237,9 @@ export async function installCopilotAgentHook(hooksPath, options = {}) {
|
|
|
237
237
|
removeTokenjuiceCopilotAgentHooks(config);
|
|
238
238
|
const retained = getPostToolUseHooks(config);
|
|
239
239
|
config.hooks.postToolUse = [...retained, createTokenjuiceCopilotAgentHook(command)];
|
|
240
|
+
if (config.disableAllHooks === true) {
|
|
241
|
+
config.disableAllHooks = false;
|
|
242
|
+
}
|
|
240
243
|
if (typeof config.version !== "number") {
|
|
241
244
|
config.version = 1;
|
|
242
245
|
}
|
|
@@ -360,26 +360,30 @@ export async function runCopilotCliPostToolUseHook(rawText) {
|
|
|
360
360
|
: isRecord(payload.tool_result)
|
|
361
361
|
? payload.tool_result
|
|
362
362
|
: undefined;
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
363
|
+
const resultType = typeof payload.resultType === "string"
|
|
364
|
+
? payload.resultType
|
|
365
|
+
: typeof payload.result_type === "string"
|
|
366
|
+
? payload.result_type
|
|
367
|
+
: typeof toolResult?.resultType === "string"
|
|
368
|
+
? toolResult.resultType
|
|
369
|
+
: typeof toolResult?.result_type === "string"
|
|
370
|
+
? toolResult.result_type
|
|
371
|
+
: undefined;
|
|
372
372
|
// Only rewrite success output; failure/rejected/denied payloads pass
|
|
373
373
|
// through untouched so the agent still sees error context verbatim.
|
|
374
374
|
if (resultType && resultType !== "success") {
|
|
375
375
|
process.stdout.write("{}\n");
|
|
376
376
|
return 0;
|
|
377
377
|
}
|
|
378
|
-
const combinedText = typeof
|
|
379
|
-
?
|
|
380
|
-
: typeof
|
|
381
|
-
?
|
|
382
|
-
: ""
|
|
378
|
+
const combinedText = typeof payload.textResultForLlm === "string"
|
|
379
|
+
? payload.textResultForLlm
|
|
380
|
+
: typeof payload.text_result_for_llm === "string"
|
|
381
|
+
? payload.text_result_for_llm
|
|
382
|
+
: typeof toolResult?.textResultForLlm === "string"
|
|
383
|
+
? toolResult.textResultForLlm
|
|
384
|
+
: typeof toolResult?.text_result_for_llm === "string"
|
|
385
|
+
? toolResult.text_result_for_llm
|
|
386
|
+
: "";
|
|
383
387
|
if (!combinedText.trim()) {
|
|
384
388
|
process.stdout.write("{}\n");
|
|
385
389
|
return 0;
|
|
@@ -35,9 +35,7 @@ async function writeCrushSkillWithoutBackup(skillPath) {
|
|
|
35
35
|
await rename(tempPath, skillPath);
|
|
36
36
|
}
|
|
37
37
|
function isTokenjuiceCrushSkillText(text) {
|
|
38
|
-
return text.includes(TOKENJUICE_CRUSH_MARKER)
|
|
39
|
-
|| text.includes(TOKENJUICE_WRAP_COMMAND)
|
|
40
|
-
|| text.includes(TOKENJUICE_RAW_COMMAND);
|
|
38
|
+
return text.includes(TOKENJUICE_CRUSH_MARKER);
|
|
41
39
|
}
|
|
42
40
|
export async function installCrushSkill(skillPath, options = {}) {
|
|
43
41
|
const resolvedSkillPath = skillPath ?? getDefaultSkillPath(options);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
2
3
|
import { dirname, join } from "node:path";
|
|
3
4
|
import { homedir } from "node:os";
|
|
4
5
|
import { compactBashResult } from "../../core/integrations/compact-bash-result.js";
|
|
@@ -53,12 +54,19 @@ async function loadGeminiSettingsWithBackup(settingsPath) {
|
|
|
53
54
|
}
|
|
54
55
|
async function writeGeminiSettings(settingsPath, config) {
|
|
55
56
|
await mkdir(dirname(settingsPath), { recursive: true });
|
|
56
|
-
const tempPath = `${settingsPath}.tmp`;
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
const tempPath = `${settingsPath}.${process.pid}.${randomUUID()}.tmp`;
|
|
58
|
+
try {
|
|
59
|
+
await writeFile(tempPath, `${JSON.stringify(config, null, 2)}\n`, { encoding: "utf8", flag: "wx" });
|
|
60
|
+
await rename(tempPath, settingsPath);
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
await rm(tempPath, { force: true });
|
|
64
|
+
}
|
|
59
65
|
}
|
|
60
66
|
function isTokenjuiceGeminiHook(entry) {
|
|
61
67
|
return isRecord(entry)
|
|
68
|
+
&& entry.type === "command"
|
|
69
|
+
&& entry.name === "tokenjuice"
|
|
62
70
|
&& typeof entry.command === "string"
|
|
63
71
|
&& entry.command.includes(TOKENJUICE_GEMINI_CLI_SUBCOMMAND);
|
|
64
72
|
}
|
|
@@ -47,11 +47,31 @@ async function readConfig(configPath) {
|
|
|
47
47
|
throw error;
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
function countMarker(text, marker) {
|
|
51
|
-
return text.split(marker).length - 1;
|
|
52
|
-
}
|
|
53
50
|
function hasMalformedMarkerStructure(text) {
|
|
54
|
-
|
|
51
|
+
let offset = 0;
|
|
52
|
+
let hasOpenBlock = false;
|
|
53
|
+
while (offset < text.length) {
|
|
54
|
+
const beginIndex = text.indexOf(TOKENJUICE_KIMI_BEGIN, offset);
|
|
55
|
+
const endIndex = text.indexOf(TOKENJUICE_KIMI_END, offset);
|
|
56
|
+
if (beginIndex === -1 && endIndex === -1) {
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
if (beginIndex !== -1 && (endIndex === -1 || beginIndex < endIndex)) {
|
|
60
|
+
if (hasOpenBlock) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
hasOpenBlock = true;
|
|
64
|
+
offset = beginIndex + TOKENJUICE_KIMI_BEGIN.length;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
if (!hasOpenBlock) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
hasOpenBlock = false;
|
|
71
|
+
offset = endIndex + TOKENJUICE_KIMI_END.length;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return hasOpenBlock;
|
|
55
75
|
}
|
|
56
76
|
function removeKimiHookBlock(text) {
|
|
57
77
|
let remaining = text;
|
|
@@ -5,12 +5,18 @@ import { buildTokenjuiceGuidanceBullets, TOKENJUICE_FULL_COMMAND, TOKENJUICE_RAW
|
|
|
5
5
|
import { collectGuidanceIssues, readInstructionFile, removeInstructionFile, writeInstructionFile } from "../shared/instruction-file.js";
|
|
6
6
|
import { buildInstructionDoctorReportFields, instructionDoctorStatusFromIssues, } from "../shared/instruction-doctor.js";
|
|
7
7
|
const TOKENJUICE_MCP_AGENT_FIX_COMMAND = "tokenjuice install mcp-agent";
|
|
8
|
+
const TOKENJUICE_MCP_AGENT_OWNERSHIP_MARKER = "<!-- tokenjuice:mcp-agent -->";
|
|
8
9
|
const TOKENJUICE_MCP_AGENT_MARKER = "tokenjuice mcp-agent terminal output compaction";
|
|
9
10
|
const TOKENJUICE_MCP_AGENT_RESTORE_BACKUP_MARKER_PREFIX = "<!-- tokenjuice:mcp-agent-restore-backup=";
|
|
10
11
|
const TOKENJUICE_MCP_AGENT_LOAD_GUIDANCE = "enable agents.search_paths with .mcp-agent/agents in mcp_agent.config.yaml";
|
|
11
12
|
const TOKENJUICE_MCP_AGENT_ADVISORY = "mcp-agent support is beta and agent-file based; enable `.mcp-agent/agents` in `agents.search_paths` so mcp-agent can load it.";
|
|
12
13
|
function isTokenjuiceMcpAgentDefinitionText(text) {
|
|
13
|
-
return text.includes(
|
|
14
|
+
return text.includes(TOKENJUICE_MCP_AGENT_OWNERSHIP_MARKER)
|
|
15
|
+
|| isLegacyTokenjuiceMcpAgentDefinitionText(text);
|
|
16
|
+
}
|
|
17
|
+
function isLegacyTokenjuiceMcpAgentDefinitionText(text) {
|
|
18
|
+
const restoreBackupSuffix = readRestoreBackupSuffix(text);
|
|
19
|
+
return text === buildMcpAgentDefinition({ restoreBackupSuffix, includeOwnershipMarker: false });
|
|
14
20
|
}
|
|
15
21
|
function readRestoreBackupSuffix(text) {
|
|
16
22
|
const match = text.match(/^<!-- tokenjuice:mcp-agent-restore-backup=(\.bak(?:\.\d+)?) -->$/mu);
|
|
@@ -144,13 +150,14 @@ async function resolveAgentPath(agentPath, options = {}) {
|
|
|
144
150
|
}
|
|
145
151
|
return getDefaultAgentPath(options);
|
|
146
152
|
}
|
|
147
|
-
function buildMcpAgentDefinition({ restoreBackupSuffix } = {}) {
|
|
153
|
+
function buildMcpAgentDefinition({ restoreBackupSuffix, includeOwnershipMarker = true, } = {}) {
|
|
148
154
|
return [
|
|
149
155
|
"---",
|
|
150
156
|
"name: tokenjuice",
|
|
151
157
|
"description: Use when mcp-agent workflows run terminal commands likely to produce long output or need compacted shell evidence.",
|
|
152
158
|
"---",
|
|
153
159
|
"",
|
|
160
|
+
...(includeOwnershipMarker ? [TOKENJUICE_MCP_AGENT_OWNERSHIP_MARKER, ""] : []),
|
|
154
161
|
...(restoreBackupSuffix
|
|
155
162
|
? [`${TOKENJUICE_MCP_AGENT_RESTORE_BACKUP_MARKER_PREFIX}${restoreBackupSuffix} -->`, ""]
|
|
156
163
|
: []),
|
|
@@ -213,7 +220,6 @@ export async function uninstallMcpAgentDefinition(agentPath, options = {}) {
|
|
|
213
220
|
await rejectDefinitionSymlink(backupPath);
|
|
214
221
|
const backup = await readInstructionFile(backupPath);
|
|
215
222
|
if (backup.exists && !isTokenjuiceMcpAgentDefinitionText(backup.text)) {
|
|
216
|
-
await rm(resolvedAgentPath, { force: true });
|
|
217
223
|
await rename(backupPath, resolvedAgentPath);
|
|
218
224
|
return { agentPath: resolvedAgentPath, removed: true };
|
|
219
225
|
}
|
|
@@ -675,6 +675,36 @@ function resolveEffectiveCommand(input) {
|
|
|
675
675
|
}
|
|
676
676
|
return null;
|
|
677
677
|
}
|
|
678
|
+
function hasMultipleSubstantiveShellCommands(input) {
|
|
679
|
+
const shellBody = unwrapShellRunner(input);
|
|
680
|
+
const command = (shellBody ?? input.command ?? "").trim();
|
|
681
|
+
if (!command || !hasSequentialShellCommands(command)) {
|
|
682
|
+
return false;
|
|
683
|
+
}
|
|
684
|
+
let effectiveCommand = command;
|
|
685
|
+
for (let iteration = 0; iteration < 16; iteration += 1) {
|
|
686
|
+
const setupIfTail = stripLeadingSetupIfBlock(effectiveCommand);
|
|
687
|
+
const setupSegmentTail = setupIfTail ?? stripLeadingSetupSegment(effectiveCommand);
|
|
688
|
+
if (!setupSegmentTail) {
|
|
689
|
+
break;
|
|
690
|
+
}
|
|
691
|
+
effectiveCommand = setupSegmentTail;
|
|
692
|
+
}
|
|
693
|
+
let substantiveCount = 0;
|
|
694
|
+
for (const sequenceSegment of splitTopLevelCommandChain(effectiveCommand)) {
|
|
695
|
+
for (const segment of splitTopLevelOrChain(sequenceSegment)) {
|
|
696
|
+
const candidate = buildEffectiveCandidate(tokenizeCommand(segment), true, segment);
|
|
697
|
+
if (!candidate) {
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
substantiveCount += 1;
|
|
701
|
+
if (substantiveCount > 1) {
|
|
702
|
+
return true;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return false;
|
|
707
|
+
}
|
|
678
708
|
function getEffectiveCommandArgv(input) {
|
|
679
709
|
return resolveEffectiveCommand(input)?.argv ?? getCandidateArgv(input);
|
|
680
710
|
}
|
|
@@ -693,6 +723,30 @@ function deriveCommandMatchCandidates(input) {
|
|
|
693
723
|
|
|
694
724
|
// src/core/command-identity.ts
|
|
695
725
|
var FILE_CONTENT_INSPECTION_COMMANDS = /* @__PURE__ */ new Set(["cat", "sed", "head", "tail", "nl", "bat", "batcat", "jq", "yq"]);
|
|
726
|
+
var SSH_OPTIONS_WITH_VALUES = /* @__PURE__ */ new Set([
|
|
727
|
+
"-B",
|
|
728
|
+
"-b",
|
|
729
|
+
"-c",
|
|
730
|
+
"-D",
|
|
731
|
+
"-E",
|
|
732
|
+
"-e",
|
|
733
|
+
"-F",
|
|
734
|
+
"-I",
|
|
735
|
+
"-i",
|
|
736
|
+
"-J",
|
|
737
|
+
"-L",
|
|
738
|
+
"-l",
|
|
739
|
+
"-m",
|
|
740
|
+
"-O",
|
|
741
|
+
"-o",
|
|
742
|
+
"-P",
|
|
743
|
+
"-p",
|
|
744
|
+
"-Q",
|
|
745
|
+
"-R",
|
|
746
|
+
"-S",
|
|
747
|
+
"-W",
|
|
748
|
+
"-w"
|
|
749
|
+
]);
|
|
696
750
|
function getCommandName(argv) {
|
|
697
751
|
const first = argv[0];
|
|
698
752
|
if (!first) {
|
|
@@ -758,6 +812,43 @@ function isGitShowFileContentArgv(argv) {
|
|
|
758
812
|
}
|
|
759
813
|
return false;
|
|
760
814
|
}
|
|
815
|
+
function isPlutilFileContentArgv(argv) {
|
|
816
|
+
if (getCommandName(argv) !== "plutil") {
|
|
817
|
+
return false;
|
|
818
|
+
}
|
|
819
|
+
if (argv.includes("-p")) {
|
|
820
|
+
return true;
|
|
821
|
+
}
|
|
822
|
+
const outputIndex = argv.indexOf("-o");
|
|
823
|
+
return outputIndex !== -1 && argv[outputIndex + 1] === "-";
|
|
824
|
+
}
|
|
825
|
+
function isReadOnlyConfigInspectionArgv(argv) {
|
|
826
|
+
return getCommandName(argv) === "openclaw" && argv[1] === "config" && argv[2] === "get";
|
|
827
|
+
}
|
|
828
|
+
function getSshRemoteCommand(argv) {
|
|
829
|
+
if (getCommandName(argv) !== "ssh") {
|
|
830
|
+
return null;
|
|
831
|
+
}
|
|
832
|
+
for (let index = 1; index < argv.length; index += 1) {
|
|
833
|
+
const arg = argv[index];
|
|
834
|
+
if (!arg) {
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
if (arg === "--") {
|
|
838
|
+
continue;
|
|
839
|
+
}
|
|
840
|
+
if (SSH_OPTIONS_WITH_VALUES.has(arg)) {
|
|
841
|
+
index += 1;
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
if (arg.startsWith("-")) {
|
|
845
|
+
continue;
|
|
846
|
+
}
|
|
847
|
+
const remoteCommand = argv.slice(index + 1).join(" ").trim();
|
|
848
|
+
return remoteCommand || null;
|
|
849
|
+
}
|
|
850
|
+
return null;
|
|
851
|
+
}
|
|
761
852
|
function isFileContentInspectionArgv(argv) {
|
|
762
853
|
const argv0 = getCommandName(argv);
|
|
763
854
|
if (!argv0) {
|
|
@@ -823,6 +914,28 @@ function getInspectionArgv(input) {
|
|
|
823
914
|
function isFileContentInspectionCommand(input) {
|
|
824
915
|
return isFileContentInspectionArgv(getInspectionArgv(input)) || deriveCommandMatchCandidates(input).some((candidate) => isGhApiContentsDecodeCommand(candidate.command));
|
|
825
916
|
}
|
|
917
|
+
function isVerbatimRemoteInspectionCommand(command) {
|
|
918
|
+
const effectiveCommand = unwrapShellRunner({ command }) ?? command;
|
|
919
|
+
const isSingleGhContentsDecode = isGhApiContentsDecodeCommand(effectiveCommand) && !hasSequentialShellCommands(effectiveCommand) && /^[^|]+\|\s*base64\s+(?:-[dD]\b|--decode\b)\s*$/u.test(effectiveCommand.trim());
|
|
920
|
+
if (isSingleGhContentsDecode) {
|
|
921
|
+
return true;
|
|
922
|
+
}
|
|
923
|
+
if (isCompoundShellCommand(stripLeadingCdPrefix(command)) || isCompoundShellCommand(effectiveCommand)) {
|
|
924
|
+
return false;
|
|
925
|
+
}
|
|
926
|
+
const argv = getInspectionArgv({ command: effectiveCommand });
|
|
927
|
+
return isPlutilFileContentArgv(argv) || isReadOnlyConfigInspectionArgv(argv) || isFileContentInspectionArgv(argv);
|
|
928
|
+
}
|
|
929
|
+
function isVerbatimConfigInspectionCommand(input) {
|
|
930
|
+
if (input.command && isCompoundShellCommand(stripLeadingCdPrefix(input.command))) {
|
|
931
|
+
return false;
|
|
932
|
+
}
|
|
933
|
+
const candidates = deriveCommandMatchCandidates(input);
|
|
934
|
+
return candidates.some((candidate) => isPlutilFileContentArgv(candidate.argv) || isReadOnlyConfigInspectionArgv(candidate.argv)) || candidates.some((candidate) => {
|
|
935
|
+
const remoteCommand = getSshRemoteCommand(candidate.argv);
|
|
936
|
+
return remoteCommand !== null && isVerbatimRemoteInspectionCommand(remoteCommand);
|
|
937
|
+
});
|
|
938
|
+
}
|
|
826
939
|
|
|
827
940
|
// src/core/inventory-safety.ts
|
|
828
941
|
var REPO_INVENTORY_COMMANDS = /* @__PURE__ */ new Set(["find", "fd", "fdfind", "ls"]);
|
|
@@ -8587,9 +8700,14 @@ async function reduceExecutionWithRules(input, rules, opts = {}) {
|
|
|
8587
8700
|
reducedChars: reducedChars2,
|
|
8588
8701
|
ratio: measuredRawChars === 0 ? 1 : reducedChars2 / measuredRawChars
|
|
8589
8702
|
});
|
|
8590
|
-
const
|
|
8591
|
-
const
|
|
8592
|
-
const
|
|
8703
|
+
const multipleSubstantiveCommands = !opts.classifier && hasMultipleSubstantiveShellCommands(input);
|
|
8704
|
+
const resolvedMatch = opts.classifier || multipleSubstantiveCommands ? void 0 : resolveRuleMatch(input, rules);
|
|
8705
|
+
const classification = multipleSubstantiveCommands ? {
|
|
8706
|
+
family: "generic",
|
|
8707
|
+
confidence: 0.2,
|
|
8708
|
+
matchedReducer: "generic/fallback"
|
|
8709
|
+
} : resolvedMatch?.classification ?? classifyExecution(input, rules, opts.classifier);
|
|
8710
|
+
const reducerInput = multipleSubstantiveCommands ? normalizedInput : resolvedMatch?.candidateInput ?? normalizedInput;
|
|
8593
8711
|
const trace = opts.trace ? {
|
|
8594
8712
|
...normalizedInput.command ? { normalizedCommand: normalizedInput.command } : {},
|
|
8595
8713
|
...normalizedInput.argv?.length ? { normalizedArgv: normalizedInput.argv } : {},
|
|
@@ -8598,7 +8716,8 @@ async function reduceExecutionWithRules(input, rules, opts = {}) {
|
|
|
8598
8716
|
...classification.matchedReducer ? { matchedReducer: classification.matchedReducer } : {},
|
|
8599
8717
|
family: classification.family
|
|
8600
8718
|
} : void 0;
|
|
8601
|
-
|
|
8719
|
+
const requiresVerbatimOutput = !multipleSubstantiveCommands && isVerbatimConfigInspectionCommand(input);
|
|
8720
|
+
if (opts.raw || requiresVerbatimOutput) {
|
|
8602
8721
|
const rawRef2 = opts.store ? await storeArtifact(
|
|
8603
8722
|
{
|
|
8604
8723
|
input: normalizedInput,
|
|
@@ -8640,7 +8759,7 @@ async function reduceExecutionWithRules(input, rules, opts = {}) {
|
|
|
8640
8759
|
classification
|
|
8641
8760
|
};
|
|
8642
8761
|
}
|
|
8643
|
-
const inspectionSummary = buildInspectionSummary(normalizedInput, rawText, opts.noOmit);
|
|
8762
|
+
const inspectionSummary = multipleSubstantiveCommands ? null : buildInspectionSummary(normalizedInput, rawText, opts.noOmit);
|
|
8644
8763
|
if (inspectionSummary) {
|
|
8645
8764
|
const summaryText = inspectionSummary.lines.join("\n").trim();
|
|
8646
8765
|
const selectedText2 = clampTextMiddleWithMetadata(summaryText, maxInlineChars, opts.noOmit);
|
|
@@ -675,6 +675,36 @@ function resolveEffectiveCommand(input) {
|
|
|
675
675
|
}
|
|
676
676
|
return null;
|
|
677
677
|
}
|
|
678
|
+
function hasMultipleSubstantiveShellCommands(input) {
|
|
679
|
+
const shellBody = unwrapShellRunner(input);
|
|
680
|
+
const command = (shellBody ?? input.command ?? "").trim();
|
|
681
|
+
if (!command || !hasSequentialShellCommands(command)) {
|
|
682
|
+
return false;
|
|
683
|
+
}
|
|
684
|
+
let effectiveCommand = command;
|
|
685
|
+
for (let iteration = 0; iteration < 16; iteration += 1) {
|
|
686
|
+
const setupIfTail = stripLeadingSetupIfBlock(effectiveCommand);
|
|
687
|
+
const setupSegmentTail = setupIfTail ?? stripLeadingSetupSegment(effectiveCommand);
|
|
688
|
+
if (!setupSegmentTail) {
|
|
689
|
+
break;
|
|
690
|
+
}
|
|
691
|
+
effectiveCommand = setupSegmentTail;
|
|
692
|
+
}
|
|
693
|
+
let substantiveCount = 0;
|
|
694
|
+
for (const sequenceSegment of splitTopLevelCommandChain(effectiveCommand)) {
|
|
695
|
+
for (const segment of splitTopLevelOrChain(sequenceSegment)) {
|
|
696
|
+
const candidate = buildEffectiveCandidate(tokenizeCommand(segment), true, segment);
|
|
697
|
+
if (!candidate) {
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
substantiveCount += 1;
|
|
701
|
+
if (substantiveCount > 1) {
|
|
702
|
+
return true;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return false;
|
|
707
|
+
}
|
|
678
708
|
function getEffectiveCommandArgv(input) {
|
|
679
709
|
return resolveEffectiveCommand(input)?.argv ?? getCandidateArgv(input);
|
|
680
710
|
}
|
|
@@ -693,6 +723,30 @@ function deriveCommandMatchCandidates(input) {
|
|
|
693
723
|
|
|
694
724
|
// src/core/command-identity.ts
|
|
695
725
|
var FILE_CONTENT_INSPECTION_COMMANDS = /* @__PURE__ */ new Set(["cat", "sed", "head", "tail", "nl", "bat", "batcat", "jq", "yq"]);
|
|
726
|
+
var SSH_OPTIONS_WITH_VALUES = /* @__PURE__ */ new Set([
|
|
727
|
+
"-B",
|
|
728
|
+
"-b",
|
|
729
|
+
"-c",
|
|
730
|
+
"-D",
|
|
731
|
+
"-E",
|
|
732
|
+
"-e",
|
|
733
|
+
"-F",
|
|
734
|
+
"-I",
|
|
735
|
+
"-i",
|
|
736
|
+
"-J",
|
|
737
|
+
"-L",
|
|
738
|
+
"-l",
|
|
739
|
+
"-m",
|
|
740
|
+
"-O",
|
|
741
|
+
"-o",
|
|
742
|
+
"-P",
|
|
743
|
+
"-p",
|
|
744
|
+
"-Q",
|
|
745
|
+
"-R",
|
|
746
|
+
"-S",
|
|
747
|
+
"-W",
|
|
748
|
+
"-w"
|
|
749
|
+
]);
|
|
696
750
|
function getCommandName(argv) {
|
|
697
751
|
const first = argv[0];
|
|
698
752
|
if (!first) {
|
|
@@ -758,6 +812,43 @@ function isGitShowFileContentArgv(argv) {
|
|
|
758
812
|
}
|
|
759
813
|
return false;
|
|
760
814
|
}
|
|
815
|
+
function isPlutilFileContentArgv(argv) {
|
|
816
|
+
if (getCommandName(argv) !== "plutil") {
|
|
817
|
+
return false;
|
|
818
|
+
}
|
|
819
|
+
if (argv.includes("-p")) {
|
|
820
|
+
return true;
|
|
821
|
+
}
|
|
822
|
+
const outputIndex = argv.indexOf("-o");
|
|
823
|
+
return outputIndex !== -1 && argv[outputIndex + 1] === "-";
|
|
824
|
+
}
|
|
825
|
+
function isReadOnlyConfigInspectionArgv(argv) {
|
|
826
|
+
return getCommandName(argv) === "openclaw" && argv[1] === "config" && argv[2] === "get";
|
|
827
|
+
}
|
|
828
|
+
function getSshRemoteCommand(argv) {
|
|
829
|
+
if (getCommandName(argv) !== "ssh") {
|
|
830
|
+
return null;
|
|
831
|
+
}
|
|
832
|
+
for (let index = 1; index < argv.length; index += 1) {
|
|
833
|
+
const arg = argv[index];
|
|
834
|
+
if (!arg) {
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
if (arg === "--") {
|
|
838
|
+
continue;
|
|
839
|
+
}
|
|
840
|
+
if (SSH_OPTIONS_WITH_VALUES.has(arg)) {
|
|
841
|
+
index += 1;
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
if (arg.startsWith("-")) {
|
|
845
|
+
continue;
|
|
846
|
+
}
|
|
847
|
+
const remoteCommand = argv.slice(index + 1).join(" ").trim();
|
|
848
|
+
return remoteCommand || null;
|
|
849
|
+
}
|
|
850
|
+
return null;
|
|
851
|
+
}
|
|
761
852
|
function isFileContentInspectionArgv(argv) {
|
|
762
853
|
const argv0 = getCommandName(argv);
|
|
763
854
|
if (!argv0) {
|
|
@@ -823,6 +914,28 @@ function getInspectionArgv(input) {
|
|
|
823
914
|
function isFileContentInspectionCommand(input) {
|
|
824
915
|
return isFileContentInspectionArgv(getInspectionArgv(input)) || deriveCommandMatchCandidates(input).some((candidate) => isGhApiContentsDecodeCommand(candidate.command));
|
|
825
916
|
}
|
|
917
|
+
function isVerbatimRemoteInspectionCommand(command) {
|
|
918
|
+
const effectiveCommand = unwrapShellRunner({ command }) ?? command;
|
|
919
|
+
const isSingleGhContentsDecode = isGhApiContentsDecodeCommand(effectiveCommand) && !hasSequentialShellCommands(effectiveCommand) && /^[^|]+\|\s*base64\s+(?:-[dD]\b|--decode\b)\s*$/u.test(effectiveCommand.trim());
|
|
920
|
+
if (isSingleGhContentsDecode) {
|
|
921
|
+
return true;
|
|
922
|
+
}
|
|
923
|
+
if (isCompoundShellCommand(stripLeadingCdPrefix(command)) || isCompoundShellCommand(effectiveCommand)) {
|
|
924
|
+
return false;
|
|
925
|
+
}
|
|
926
|
+
const argv = getInspectionArgv({ command: effectiveCommand });
|
|
927
|
+
return isPlutilFileContentArgv(argv) || isReadOnlyConfigInspectionArgv(argv) || isFileContentInspectionArgv(argv);
|
|
928
|
+
}
|
|
929
|
+
function isVerbatimConfigInspectionCommand(input) {
|
|
930
|
+
if (input.command && isCompoundShellCommand(stripLeadingCdPrefix(input.command))) {
|
|
931
|
+
return false;
|
|
932
|
+
}
|
|
933
|
+
const candidates = deriveCommandMatchCandidates(input);
|
|
934
|
+
return candidates.some((candidate) => isPlutilFileContentArgv(candidate.argv) || isReadOnlyConfigInspectionArgv(candidate.argv)) || candidates.some((candidate) => {
|
|
935
|
+
const remoteCommand = getSshRemoteCommand(candidate.argv);
|
|
936
|
+
return remoteCommand !== null && isVerbatimRemoteInspectionCommand(remoteCommand);
|
|
937
|
+
});
|
|
938
|
+
}
|
|
826
939
|
|
|
827
940
|
// src/core/inventory-safety.ts
|
|
828
941
|
var REPO_INVENTORY_COMMANDS = /* @__PURE__ */ new Set(["find", "fd", "fdfind", "ls"]);
|
|
@@ -8587,9 +8700,14 @@ async function reduceExecutionWithRules(input, rules, opts = {}) {
|
|
|
8587
8700
|
reducedChars: reducedChars2,
|
|
8588
8701
|
ratio: measuredRawChars === 0 ? 1 : reducedChars2 / measuredRawChars
|
|
8589
8702
|
});
|
|
8590
|
-
const
|
|
8591
|
-
const
|
|
8592
|
-
const
|
|
8703
|
+
const multipleSubstantiveCommands = !opts.classifier && hasMultipleSubstantiveShellCommands(input);
|
|
8704
|
+
const resolvedMatch = opts.classifier || multipleSubstantiveCommands ? void 0 : resolveRuleMatch(input, rules);
|
|
8705
|
+
const classification = multipleSubstantiveCommands ? {
|
|
8706
|
+
family: "generic",
|
|
8707
|
+
confidence: 0.2,
|
|
8708
|
+
matchedReducer: "generic/fallback"
|
|
8709
|
+
} : resolvedMatch?.classification ?? classifyExecution(input, rules, opts.classifier);
|
|
8710
|
+
const reducerInput = multipleSubstantiveCommands ? normalizedInput : resolvedMatch?.candidateInput ?? normalizedInput;
|
|
8593
8711
|
const trace = opts.trace ? {
|
|
8594
8712
|
...normalizedInput.command ? { normalizedCommand: normalizedInput.command } : {},
|
|
8595
8713
|
...normalizedInput.argv?.length ? { normalizedArgv: normalizedInput.argv } : {},
|
|
@@ -8598,7 +8716,8 @@ async function reduceExecutionWithRules(input, rules, opts = {}) {
|
|
|
8598
8716
|
...classification.matchedReducer ? { matchedReducer: classification.matchedReducer } : {},
|
|
8599
8717
|
family: classification.family
|
|
8600
8718
|
} : void 0;
|
|
8601
|
-
|
|
8719
|
+
const requiresVerbatimOutput = !multipleSubstantiveCommands && isVerbatimConfigInspectionCommand(input);
|
|
8720
|
+
if (opts.raw || requiresVerbatimOutput) {
|
|
8602
8721
|
const rawRef2 = opts.store ? await storeArtifact(
|
|
8603
8722
|
{
|
|
8604
8723
|
input: normalizedInput,
|
|
@@ -8640,7 +8759,7 @@ async function reduceExecutionWithRules(input, rules, opts = {}) {
|
|
|
8640
8759
|
classification
|
|
8641
8760
|
};
|
|
8642
8761
|
}
|
|
8643
|
-
const inspectionSummary = buildInspectionSummary(normalizedInput, rawText, opts.noOmit);
|
|
8762
|
+
const inspectionSummary = multipleSubstantiveCommands ? null : buildInspectionSummary(normalizedInput, rawText, opts.noOmit);
|
|
8644
8763
|
if (inspectionSummary) {
|
|
8645
8764
|
const summaryText = inspectionSummary.lines.join("\n").trim();
|
|
8646
8765
|
const selectedText2 = clampTextMiddleWithMetadata(summaryText, maxInlineChars, opts.noOmit);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokenjuice",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Lean output compaction for terminal-heavy agent workflows.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"generate:builtin-rules": "node scripts/generate-builtin-rules.mjs",
|
|
56
56
|
"prepare": "pnpm repair:pnpm-bin-shims",
|
|
57
57
|
"repair:pnpm-bin-shims": "node scripts/repair-pnpm-bin-shims.mjs",
|
|
58
|
-
"build": "
|
|
58
|
+
"build": "node scripts/clean-dist.mjs && pnpm generate:builtin-rules && tsc -p tsconfig.json && node scripts/build-pi-runtime.mjs && node scripts/build-opencode-runtime.mjs && node scripts/copy-rules.mjs",
|
|
59
59
|
"lint": "oxlint --deny-warnings src test scripts",
|
|
60
60
|
"lint:circular": "madge --circular --extensions ts,mjs --ts-config tsconfig.json src",
|
|
61
61
|
"release:artifacts": "node scripts/build-release.mjs",
|
|
@@ -70,11 +70,11 @@
|
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@types/node": "^24.7.0",
|
|
73
|
-
"esbuild": "^0.
|
|
73
|
+
"esbuild": "^0.28.1",
|
|
74
74
|
"madge": "^8.0.0",
|
|
75
75
|
"oxlint": "^1.61.0",
|
|
76
76
|
"publint": "^0.3.18",
|
|
77
77
|
"typescript": "^5.9.3",
|
|
78
|
-
"vitest": "^
|
|
78
|
+
"vitest": "^4.1.0"
|
|
79
79
|
}
|
|
80
80
|
}
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openclaw/tokenjuice",
|
|
3
|
-
"version": "2026.6.
|
|
3
|
+
"version": "2026.6.9-beta.1",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@openclaw/tokenjuice",
|
|
9
|
-
"version": "2026.6.
|
|
9
|
+
"version": "2026.6.9-beta.1",
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"tokenjuice": "0.8.
|
|
11
|
+
"tokenjuice": "0.8.1"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"openclaw": ">=2026.6.
|
|
14
|
+
"openclaw": ">=2026.6.9-beta.1"
|
|
15
15
|
},
|
|
16
16
|
"peerDependenciesMeta": {
|
|
17
17
|
"openclaw": {
|
|
@@ -20,9 +20,9 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"node_modules/tokenjuice": {
|
|
23
|
-
"version": "0.8.
|
|
24
|
-
"resolved": "https://registry.npmjs.org/tokenjuice/-/tokenjuice-0.8.
|
|
25
|
-
"integrity": "sha512
|
|
23
|
+
"version": "0.8.1",
|
|
24
|
+
"resolved": "https://registry.npmjs.org/tokenjuice/-/tokenjuice-0.8.1.tgz",
|
|
25
|
+
"integrity": "sha512-/CnrgVI7zm7P4yphNe2Ypqm4vOwzdIob4ki9nRdT8P1NAOgsWGv0ciTDLly4cyAaKIhLd0H3swLNn2R5FCDpYw==",
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"bin": {
|
|
28
28
|
"tokenjuice": "dist/cli/main.js"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openclaw/tokenjuice",
|
|
3
|
-
"version": "2026.6.
|
|
3
|
+
"version": "2026.6.9-beta.1",
|
|
4
4
|
"description": "OpenClaw tokenjuice exec output compaction plugin",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"tokenjuice": "0.8.
|
|
11
|
+
"tokenjuice": "0.8.1"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"openclaw": ">=2026.6.
|
|
14
|
+
"openclaw": ">=2026.6.9-beta.1"
|
|
15
15
|
},
|
|
16
16
|
"peerDependenciesMeta": {
|
|
17
17
|
"openclaw": {
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
"minHostVersion": ">=2026.5.28"
|
|
30
30
|
},
|
|
31
31
|
"compat": {
|
|
32
|
-
"pluginApi": ">=2026.6.
|
|
32
|
+
"pluginApi": ">=2026.6.9-beta.1"
|
|
33
33
|
},
|
|
34
34
|
"build": {
|
|
35
|
-
"openclawVersion": "2026.6.
|
|
35
|
+
"openclawVersion": "2026.6.9-beta.1",
|
|
36
36
|
"bundledDist": false
|
|
37
37
|
},
|
|
38
38
|
"release": {
|