Skip to content

Commit 9f043dc

Browse files
authored
Merge pull request #120 from Opencode-DCP/disallow-subagents-to-use-plugin
Disallow subagents to use plugin
2 parents 72fcc07 + 2d4ae04 commit 9f043dc

File tree

10 files changed

+32
-20
lines changed

10 files changed

+32
-20
lines changed

.claude/settings.local.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Thumbs.db
2727

2828
# OpenCode
2929
.opencode/
30-
AGENTS.md
3130

3231
# Tests (local development only)
3332
tests/

index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ const plugin: Plugin = (async (ctx) => {
4242
workingDirectory: ctx.directory
4343
}),
4444
} : undefined,
45+
config: async (opencodeConfig) => {
46+
// Add prune to primary_tools by mutating the opencode config
47+
// This works because config is cached and passed by reference
48+
if (config.strategies.pruneTool.enabled) {
49+
const existingPrimaryTools = opencodeConfig.experimental?.primary_tools ?? []
50+
opencodeConfig.experimental = {
51+
...opencodeConfig.experimental,
52+
primary_tools: [...existingPrimaryTools, "prune"],
53+
}
54+
logger.info("Added 'prune' to experimental.primary_tools via config mutation")
55+
}
56+
},
4557
}
4658
}) satisfies Plugin
4759

lib/hooks.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ export function createChatMessageTransformHandler(
1717
input: {},
1818
output: { messages: WithParts[] }
1919
) => {
20-
checkSession(state, logger, output.messages);
20+
checkSession(client, state, logger, output.messages);
21+
if (state.isSubAgent) {
22+
return
23+
}
24+
2125
syncToolCache(state, config, logger, output.messages);
2226

2327

24-
deduplicate(state, logger, config, output.messages)
28+
deduplicate(client, state, logger, config, output.messages)
2529

2630
prune(state, logger, config, output.messages)
2731

lib/state/state.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import type { SessionState, ToolParameterEntry, WithParts } from "./types"
22
import type { Logger } from "../logger"
33
import { loadSessionState } from "./persistence"
44
import { getLastUserMessage } from "../messages/utils"
5+
import { isSubAgentSession } from "../utils"
56

67
export const checkSession = (
8+
client: any,
79
state: SessionState,
810
logger: Logger,
911
messages: WithParts[]
@@ -19,6 +21,7 @@ export const checkSession = (
1921
if (state.sessionId === null || state.sessionId !== lastSessionId) {
2022
logger.info(`Session changed: ${state.sessionId} -> ${lastSessionId}`)
2123
ensureSessionInitialized(
24+
client,
2225
state,
2326
lastSessionId,
2427
logger
@@ -31,6 +34,7 @@ export const checkSession = (
3134
export function createSessionState(): SessionState {
3235
return {
3336
sessionId: null,
37+
isSubAgent: false,
3438
prune: {
3539
toolIds: []
3640
},
@@ -45,6 +49,7 @@ export function createSessionState(): SessionState {
4549

4650
export function resetSessionState(state: SessionState): void {
4751
state.sessionId = null
52+
state.isSubAgent = false
4853
state.prune = {
4954
toolIds: []
5055
}
@@ -57,6 +62,7 @@ export function resetSessionState(state: SessionState): void {
5762
}
5863

5964
export async function ensureSessionInitialized(
65+
client: any,
6066
state: SessionState,
6167
sessionId: string,
6268
logger: Logger
@@ -72,6 +78,10 @@ export async function ensureSessionInitialized(
7278
resetSessionState(state)
7379
state.sessionId = sessionId
7480

81+
const isSubAgent = await isSubAgentSession(client, sessionId)
82+
state.isSubAgent = isSubAgent
83+
logger.info("isSubAgent = " + isSubAgent)
84+
7585
// Load session data from storage
7686
const persisted = await loadSessionState(sessionId, logger)
7787
if (persisted === null) {

lib/state/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface Prune {
2525

2626
export interface SessionState {
2727
sessionId: string | null
28+
isSubAgent: boolean
2829
prune: Prune
2930
stats: SessionStats
3031
toolParameters: Map<string, ToolParameterEntry>

lib/strategies/deduplication.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { calculateTokensSaved } from "../utils"
99
* Modifies the session state in place to add pruned tool call IDs.
1010
*/
1111
export const deduplicate = (
12+
client: any,
1213
state: SessionState,
1314
logger: Logger,
1415
config: PluginConfig,

lib/strategies/on-idle.ts

Whitespace-only changes.

lib/strategies/prune-tool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function createPruneTool(
6060
return "No numeric IDs provided. Format: [reason, id1, id2, ...] where reason is 'completion', 'noise', or 'consolidation'."
6161
}
6262

63-
await ensureSessionInitialized(state, sessionId, logger)
63+
await ensureSessionInitialized(ctx.client, state, sessionId, logger)
6464

6565
// Fetch messages to calculate tokens and find current agent
6666
const messagesResponse = await client.session.messages({

lib/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function formatTokenCount(tokens: number): string {
6060
/**
6161
* Checks if a session is a subagent session by looking for a parentID.
6262
*/
63-
export async function isSubagentSession(client: any, sessionID: string): Promise<boolean> {
63+
export async function isSubAgentSession(client: any, sessionID: string): Promise<boolean> {
6464
try {
6565
const result = await client.session.get({ path: { id: sessionID } })
6666
return !!result.data?.parentID

0 commit comments

Comments
 (0)