Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kompass.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"adapters": {
"opencode": {
"agentMode": "all",
"subtaskCommandMode": "kompass",
},
},
}
5 changes: 5 additions & 0 deletions packages/core/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export interface KompassConfig {
adapters?: {
opencode?: {
agentMode?: "subagent" | "primary" | "all";
subtaskCommandMode?: "kompass" | "all" | "off";
};
};
}
Expand Down Expand Up @@ -207,6 +208,7 @@ export interface MergedKompassConfig {
adapters: {
opencode: {
agentMode: "subagent" | "primary" | "all";
subtaskCommandMode: "kompass" | "all" | "off";
};
};
}
Expand Down Expand Up @@ -697,6 +699,9 @@ export function mergeWithDefaults(
config?.adapters?.opencode?.agentMode ??
config?.defaults?.agentMode ??
"all",
subtaskCommandMode:
config?.adapters?.opencode?.subtaskCommandMode ??
"kompass",
},
},
};
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ describe("object-based config", () => {
assert.equal(config.agents.enabled.includes("reviewer"), false);
assert.equal(config.components.enabled.includes("dev-flow"), false);
assert.equal(config.components.paths.commit, "components/custom-commit.md");
assert.equal(config.adapters.opencode.subtaskCommandMode, "kompass");
});

test("supports adapter-specific subtask command stripping mode", () => {
const config = mergeWithDefaults({
adapters: {
opencode: {
subtaskCommandMode: "all",
},
},
});

assert.equal(config.adapters.opencode.subtaskCommandMode, "all");
});

test("supports skill entry maps", () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/opencode/.opencode/kompass.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
},
"adapters": {
"opencode": {
"agentMode": "all"
"agentMode": "all",
"subtaskCommandMode": "kompass"
}
}
}
52 changes: 51 additions & 1 deletion packages/opencode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type MergedKompassConfig,
type Shell,
} from "../core/index.ts";
import { DEFAULT_COMMAND_NAMES } from "../core/lib/config.ts";
import { applyAgentsConfig, applyCommandsConfig, applySkillsConfig } from "./config.ts";
import { createPluginLogger, getErrorDetails, type PluginLogger } from "./logging.ts";
import {
Expand Down Expand Up @@ -203,11 +204,38 @@ export function getCommandExecution(
};
}

export function removeSubtaskCommands(output: CommandExecuteBeforeOutput): number {
let removed = 0;

for (const part of output.parts) {
if (part.type !== "subtask") continue;
if (!("command" in part) || typeof part.command !== "string") continue;

delete (part as { command?: string }).command;
removed++;
}

return removed;
}

export function shouldRemoveSubtaskCommand(
command: string,
config: MergedKompassConfig,
kompassCommands = new Set<string>(DEFAULT_COMMAND_NAMES),
): boolean {
const mode = config.adapters.opencode.subtaskCommandMode;

if (mode === "off") return false;
if (mode === "all") return true;

return kompassCommands.has(command);
}

export function removeSyntheticAgentHandoff(output: ChatMessageOutput): boolean {
const filteredParts = output.parts.filter((part) => !(
part.type === "text" &&
part.synthetic === true &&
part.text.includes(AGENT_HANDOFF_MARKER)
part.text.toLowerCase().includes(AGENT_HANDOFF_MARKER)
));

if (filteredParts.length === output.parts.length) return false;
Expand Down Expand Up @@ -409,6 +437,16 @@ export const OpenCodeCompassPlugin: Plugin = async (input: PluginInput) => {
}

const tools = await createToolsSafely();
let config = mergeWithDefaults(null);
try {
config = mergeWithDefaults(await loadKompassConfig(worktree));
} catch (error) {
await logger.warn("Falling back to default Kompass runtime config", {
worktree,
...getErrorDetails(error),
});
}
const kompassCommands = new Set<string>(DEFAULT_COMMAND_NAMES);

return {
tool: tools,
Expand Down Expand Up @@ -438,8 +476,20 @@ export const OpenCodeCompassPlugin: Plugin = async (input: PluginInput) => {
},
async "command.execute.before"(input, output) {
try {
const removedSubtaskCommands = shouldRemoveSubtaskCommand(input.command, config, kompassCommands)
? removeSubtaskCommands(output)
: 0;
const commandExecution = getCommandExecution(input, output);

if (removedSubtaskCommands > 0) {
await logger.info("Removed subtask command payload", {
command: input.command,
arguments: input.arguments,
sessionID: input.sessionID,
removedSubtaskCommands,
});
}

if (!commandExecution) return;

await logger.info("Executing Kompass command", commandExecution as Record<string, unknown>);
Expand Down
117 changes: 117 additions & 0 deletions packages/opencode/test/task-hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
expandSlashCommandPrompt,
getCommandExecution,
getTaskToolExecution,
removeSyntheticAgentHandoff,
removeSubtaskCommands,
shouldRemoveSubtaskCommand,
} from "../index.ts";
import { mergeWithDefaults } from "../../core/lib/config.ts";

describe("getTaskToolExecution", () => {
test("expands slash commands for task tool calls", async () => {
Expand Down Expand Up @@ -165,3 +169,116 @@ describe("getCommandExecution", () => {
assert.equal(execution, undefined);
});
});

describe("removeSubtaskCommands", () => {
test("removes command from subtask parts", () => {
const output = {
parts: [
{
id: "part-1",
sessionID: "session-3",
messageID: "message-1",
type: "subtask",
prompt: "expanded command prompt",
description: "Run review command",
agent: "general",
command: "review",
},
{
id: "part-2",
sessionID: "session-3",
messageID: "message-1",
type: "text",
text: "keep this",
},
],
};

const removed = removeSubtaskCommands(output as never);

assert.equal(removed, 1);
assert.equal("command" in output.parts[0], false);
});
});

describe("shouldRemoveSubtaskCommand", () => {
test("defaults to stripping commands only for Kompass commands", () => {
const config = mergeWithDefaults(null);

assert.equal(shouldRemoveSubtaskCommand("review", config), true);
assert.equal(shouldRemoveSubtaskCommand("third-party", config), false);
});

test("supports enabling stripping for all commands", () => {
const config = mergeWithDefaults({
adapters: {
opencode: {
subtaskCommandMode: "all",
},
},
});

assert.equal(shouldRemoveSubtaskCommand("third-party", config), true);
});

test("supports disabling stripping entirely", () => {
const config = mergeWithDefaults({
adapters: {
opencode: {
subtaskCommandMode: "off",
},
},
});

assert.equal(shouldRemoveSubtaskCommand("review", config), false);
});
});

describe("removeSyntheticAgentHandoff", () => {
test("removes the legacy synthetic agent handoff text", () => {
const output = {
parts: [
{
id: "part-1",
sessionID: "session-3",
messageID: "message-1",
type: "text",
text: "Please generate a prompt and call the task tool with subagent: planner",
synthetic: true,
},
{
id: "part-2",
sessionID: "session-3",
messageID: "message-1",
type: "text",
text: "keep this",
},
],
};

const removed = removeSyntheticAgentHandoff(output as never);

assert.equal(removed, true);
assert.equal(output.parts.length, 1);
assert.equal(output.parts[0]?.text, "keep this");
});

test("keeps non-synthetic text untouched", () => {
const output = {
parts: [
{
id: "part-1",
sessionID: "session-4",
messageID: "message-1",
type: "text",
text: "Summarize the task tool output above and continue with your task.",
},
],
};

const removed = removeSyntheticAgentHandoff(output as never);

assert.equal(removed, false);
assert.equal(output.parts.length, 1);
});
});
Loading
Loading