This PR ports the hashline edit tool from oh-my-pi to oh-my-opencode as an experimental feature. ## Features - New experimental.hashline_edit config flag - hashline_edit tool with 4 operations: set_line, replace_lines, insert_after, replace - Hash-based line anchors for safe concurrent editing - Edit tool disabler for non-OpenAI providers - Read output enhancer with LINE:HASH prefixes - Provider state tracking module ## Technical Details - xxHash32-based 2-char hex hashes - Bottom-up edit application to prevent index shifting - OpenAI provider exemption (uses native apply_patch) - 90 tests covering all operations and edge cases - All files under 200 LOC limit ## Files Added/Modified - src/tools/hashline-edit/ (7 files, ~400 LOC) - src/hooks/hashline-edit-disabler/ (4 files, ~200 LOC) - src/hooks/hashline-read-enhancer/ (3 files, ~400 LOC) - src/features/hashline-provider-state.ts (13 LOC) - src/config/schema/experimental.ts (hashline_edit flag) - src/config/schema/hooks.ts (2 new hook names) - src/plugin/tool-registry.ts (conditional registration) - src/plugin/chat-params.ts (provider state tracking) - src/tools/index.ts (export) - src/hooks/index.ts (exports)
82 lines
2.9 KiB
TypeScript
82 lines
2.9 KiB
TypeScript
import { describe, test, expect, mock, beforeEach, afterAll } from "bun:test"
|
|
import { truncateUntilTargetTokens } from "./storage"
|
|
import * as storage from "./storage"
|
|
|
|
// Mock the entire module
|
|
mock.module("./storage", () => {
|
|
return {
|
|
...storage,
|
|
findToolResultsBySize: mock(() => []),
|
|
truncateToolResult: mock(() => ({ success: false })),
|
|
}
|
|
})
|
|
|
|
afterAll(() => {
|
|
mock.module("./storage", () => storage)
|
|
})
|
|
|
|
describe("truncateUntilTargetTokens", () => {
|
|
const sessionID = "test-session"
|
|
|
|
beforeEach(() => {
|
|
// Reset mocks
|
|
const { findToolResultsBySize, truncateToolResult } = require("./storage")
|
|
findToolResultsBySize.mockReset()
|
|
truncateToolResult.mockReset()
|
|
})
|
|
|
|
test("truncates only until target is reached", async () => {
|
|
const { findToolResultsBySize, truncateToolResult } = require("./storage")
|
|
|
|
// given: Two tool results, each 1000 chars. Target reduction is 500 chars.
|
|
const results = [
|
|
{ partPath: "path1", partId: "id1", messageID: "m1", toolName: "tool1", outputSize: 1000 },
|
|
{ partPath: "path2", partId: "id2", messageID: "m2", toolName: "tool2", outputSize: 1000 },
|
|
]
|
|
|
|
findToolResultsBySize.mockReturnValue(results)
|
|
truncateToolResult.mockImplementation((path: string) => ({
|
|
success: true,
|
|
toolName: path === "path1" ? "tool1" : "tool2",
|
|
originalSize: 1000
|
|
}))
|
|
|
|
// when: currentTokens=1000, maxTokens=1000, targetRatio=0.5 (target=500, reduce=500)
|
|
// charsPerToken=1 for simplicity in test
|
|
const result = await truncateUntilTargetTokens(sessionID, 1000, 1000, 0.5, 1)
|
|
|
|
// then: Should only truncate the first tool
|
|
expect(result.truncatedCount).toBe(1)
|
|
expect(truncateToolResult).toHaveBeenCalledTimes(1)
|
|
expect(truncateToolResult).toHaveBeenCalledWith("path1")
|
|
expect(result.totalBytesRemoved).toBe(1000)
|
|
expect(result.sufficient).toBe(true)
|
|
})
|
|
|
|
test("truncates all if target not reached", async () => {
|
|
const { findToolResultsBySize, truncateToolResult } = require("./storage")
|
|
|
|
// given: Two tool results, each 100 chars. Target reduction is 500 chars.
|
|
const results = [
|
|
{ partPath: "path1", partId: "id1", messageID: "m1", toolName: "tool1", outputSize: 100 },
|
|
{ partPath: "path2", partId: "id2", messageID: "m2", toolName: "tool2", outputSize: 100 },
|
|
]
|
|
|
|
findToolResultsBySize.mockReturnValue(results)
|
|
truncateToolResult.mockImplementation((path: string) => ({
|
|
success: true,
|
|
toolName: path === "path1" ? "tool1" : "tool2",
|
|
originalSize: 100
|
|
}))
|
|
|
|
// when: reduce 500 chars
|
|
const result = await truncateUntilTargetTokens(sessionID, 1000, 1000, 0.5, 1)
|
|
|
|
// then: Should truncate both
|
|
expect(result.truncatedCount).toBe(2)
|
|
expect(truncateToolResult).toHaveBeenCalledTimes(2)
|
|
expect(result.totalBytesRemoved).toBe(200)
|
|
expect(result.sufficient).toBe(false)
|
|
})
|
|
})
|