* chore: pin bun-types to 1.3.6 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * chore: exclude test files and script from tsconfig 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * refactor: remove sisyphus-swarm feature Remove mailbox types and swarm config schema. Update docs. 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * refactor: remove legacy sisyphus-tasks feature Remove old storage and types implementation, replaced by claude-tasks. 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * feat(claude-tasks): add task schema and storage utilities - Task schema with Zod validation (pending, in_progress, completed, deleted) - Storage utilities: getTaskDir, readJsonSafe, writeJsonAtomic, acquireLock - Atomic writes with temp file + rename - File-based locking with 30s stale threshold 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * feat(tools/task): add task object schemas Add Zod schemas for task CRUD operations input validation. 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * feat(tools): add TaskCreate tool Create new tasks with sequential ID generation and lock-based concurrency. 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * feat(tools): add TaskGet tool Retrieve task by ID with null-safe handling. 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * feat(tools): add TaskUpdate tool with claim validation Update tasks with status transitions and owner claim validation. 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * feat(tools): add TaskList tool and exports - TaskList for summary view of all tasks - Export all claude-tasks tool factories from index 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * feat(hooks): add task-reminder hook Remind agents to use task tools after 10 turns without task operations. 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * feat(config): add disabled_tools setting and tasks-todowrite-disabler hook - Add disabled_tools config option to disable specific tools by name - Register tasks-todowrite-disabler hook name in schema 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * feat(config-handler): add task_* and teammate tool permissions Grant task_* and teammate permissions to atlas, sisyphus, prometheus, and sisyphus-junior agents. 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * feat(delegate-task): add execute option for task execution Add optional execute field with task_id and task_dir for task-based delegation. 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * fix(truncator): add type guard for non-string outputs Prevent crashes when output is not a string by adding typeof checks. 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * chore: export config types and update task-resume-info - Export SisyphusConfig and SisyphusTasksConfig types - Add task_tool to TARGET_TOOLS list 🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) * refactor(storage): remove team namespace, use flat task directory * feat(task): implement unified task tool with all 5 actions * fix(hooks): update task-reminder to track unified task tool * refactor(tools): register unified task tool, remove 4 separate tools * chore(cleanup): remove old 4-tool task implementation * refactor(config): use new_task_system_enabled as top-level flag - Add new_task_system_enabled to OhMyOpenCodeConfigSchema - Remove enabled from SisyphusTasksConfigSchema (keep storage_path, claude_code_compat) - Update index.ts to gate on new_task_system_enabled - Update plugin-config.ts default for config initialization - Update test configs in task.test.ts and storage.test.ts Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai> * fix: resolve typecheck and test failures - Add explicit ToolDefinition return type to createTask function - Fix planDemoteConfig to use 'subagent' mode instead of 'all' --------- Co-authored-by: justsisyphus <justsisyphus@users.noreply.github.com> Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
202 lines
4.8 KiB
TypeScript
202 lines
4.8 KiB
TypeScript
import type { PluginInput } from "@opencode-ai/plugin";
|
|
|
|
const ANTHROPIC_ACTUAL_LIMIT =
|
|
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
|
|
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
|
|
? 1_000_000
|
|
: 200_000;
|
|
const CHARS_PER_TOKEN_ESTIMATE = 4;
|
|
const DEFAULT_TARGET_MAX_TOKENS = 50_000;
|
|
|
|
interface AssistantMessageInfo {
|
|
role: "assistant";
|
|
tokens: {
|
|
input: number;
|
|
output: number;
|
|
reasoning: number;
|
|
cache: { read: number; write: number };
|
|
};
|
|
}
|
|
|
|
interface MessageWrapper {
|
|
info: { role: string } & Partial<AssistantMessageInfo>;
|
|
}
|
|
|
|
export interface TruncationResult {
|
|
result: string;
|
|
truncated: boolean;
|
|
removedCount?: number;
|
|
}
|
|
|
|
export interface TruncationOptions {
|
|
targetMaxTokens?: number;
|
|
preserveHeaderLines?: number;
|
|
contextWindowLimit?: number;
|
|
}
|
|
|
|
function estimateTokens(text: string): number {
|
|
return Math.ceil(text.length / CHARS_PER_TOKEN_ESTIMATE);
|
|
}
|
|
|
|
export function truncateToTokenLimit(
|
|
output: string,
|
|
maxTokens: number,
|
|
preserveHeaderLines = 3,
|
|
): TruncationResult {
|
|
if (typeof output !== 'string') {
|
|
return { result: String(output ?? ''), truncated: false };
|
|
}
|
|
|
|
const currentTokens = estimateTokens(output);
|
|
|
|
if (currentTokens <= maxTokens) {
|
|
return { result: output, truncated: false };
|
|
}
|
|
|
|
const lines = output.split("\n");
|
|
|
|
if (lines.length <= preserveHeaderLines) {
|
|
const maxChars = maxTokens * CHARS_PER_TOKEN_ESTIMATE;
|
|
return {
|
|
result:
|
|
output.slice(0, maxChars) +
|
|
"\n\n[Output truncated due to context window limit]",
|
|
truncated: true,
|
|
};
|
|
}
|
|
|
|
const headerLines = lines.slice(0, preserveHeaderLines);
|
|
const contentLines = lines.slice(preserveHeaderLines);
|
|
|
|
const headerText = headerLines.join("\n");
|
|
const headerTokens = estimateTokens(headerText);
|
|
const truncationMessageTokens = 50;
|
|
const availableTokens = maxTokens - headerTokens - truncationMessageTokens;
|
|
|
|
if (availableTokens <= 0) {
|
|
return {
|
|
result:
|
|
headerText + "\n\n[Content truncated due to context window limit]",
|
|
truncated: true,
|
|
removedCount: contentLines.length,
|
|
};
|
|
}
|
|
|
|
const resultLines: string[] = [];
|
|
let currentTokenCount = 0;
|
|
|
|
for (const line of contentLines) {
|
|
const lineTokens = estimateTokens(line + "\n");
|
|
if (currentTokenCount + lineTokens > availableTokens) {
|
|
break;
|
|
}
|
|
resultLines.push(line);
|
|
currentTokenCount += lineTokens;
|
|
}
|
|
|
|
const truncatedContent = [...headerLines, ...resultLines].join("\n");
|
|
const removedCount = contentLines.length - resultLines.length;
|
|
|
|
return {
|
|
result:
|
|
truncatedContent +
|
|
`\n\n[${removedCount} more lines truncated due to context window limit]`,
|
|
truncated: true,
|
|
removedCount,
|
|
};
|
|
}
|
|
|
|
export async function getContextWindowUsage(
|
|
ctx: PluginInput,
|
|
sessionID: string,
|
|
): Promise<{
|
|
usedTokens: number;
|
|
remainingTokens: number;
|
|
usagePercentage: number;
|
|
} | null> {
|
|
try {
|
|
const response = await ctx.client.session.messages({
|
|
path: { id: sessionID },
|
|
});
|
|
|
|
const messages = (response.data ?? response) as MessageWrapper[];
|
|
|
|
const assistantMessages = messages
|
|
.filter((m) => m.info.role === "assistant")
|
|
.map((m) => m.info as AssistantMessageInfo);
|
|
|
|
if (assistantMessages.length === 0) return null;
|
|
|
|
const lastAssistant = assistantMessages[assistantMessages.length - 1];
|
|
const lastTokens = lastAssistant.tokens;
|
|
const usedTokens =
|
|
(lastTokens?.input ?? 0) +
|
|
(lastTokens?.cache?.read ?? 0) +
|
|
(lastTokens?.output ?? 0);
|
|
const remainingTokens = ANTHROPIC_ACTUAL_LIMIT - usedTokens;
|
|
|
|
return {
|
|
usedTokens,
|
|
remainingTokens,
|
|
usagePercentage: usedTokens / ANTHROPIC_ACTUAL_LIMIT,
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function dynamicTruncate(
|
|
ctx: PluginInput,
|
|
sessionID: string,
|
|
output: string,
|
|
options: TruncationOptions = {},
|
|
): Promise<TruncationResult> {
|
|
if (typeof output !== 'string') {
|
|
return { result: String(output ?? ''), truncated: false };
|
|
}
|
|
|
|
const {
|
|
targetMaxTokens = DEFAULT_TARGET_MAX_TOKENS,
|
|
preserveHeaderLines = 3,
|
|
} = options;
|
|
|
|
const usage = await getContextWindowUsage(ctx, sessionID);
|
|
|
|
if (!usage) {
|
|
// Fallback: apply conservative truncation when context usage unavailable
|
|
return truncateToTokenLimit(output, targetMaxTokens, preserveHeaderLines);
|
|
}
|
|
|
|
const maxOutputTokens = Math.min(
|
|
usage.remainingTokens * 0.5,
|
|
targetMaxTokens,
|
|
);
|
|
|
|
if (maxOutputTokens <= 0) {
|
|
return {
|
|
result: "[Output suppressed - context window exhausted]",
|
|
truncated: true,
|
|
};
|
|
}
|
|
|
|
return truncateToTokenLimit(output, maxOutputTokens, preserveHeaderLines);
|
|
}
|
|
|
|
export function createDynamicTruncator(ctx: PluginInput) {
|
|
return {
|
|
truncate: (
|
|
sessionID: string,
|
|
output: string,
|
|
options?: TruncationOptions,
|
|
) => dynamicTruncate(ctx, sessionID, output, options),
|
|
|
|
getUsage: (sessionID: string) => getContextWindowUsage(ctx, sessionID),
|
|
|
|
truncateSync: (
|
|
output: string,
|
|
maxTokens: number,
|
|
preserveHeaderLines?: number,
|
|
) => truncateToTokenLimit(output, maxTokens, preserveHeaderLines),
|
|
};
|
|
}
|