Skip to content

Commit ddaa23c

Browse files
committed
Store Kimaki system instructions through native OpenCode session entries.
Write the session-stable Kimaki prompt to instruction entry kimaki before chat or command dispatch. Stop submission when preparation fails and avoid repeating the write within one runtime. Remove the SQLite context table definition, query helpers, and plugin-side prompt reconstruction. Keep dynamic context hooks separate. Cover the instruction key and published 256 KiB payload limit. Preserve notes about pending per-input model selection support in anomalyco/opencode#48356. Related to #220. Session: ses_f73cb3f4dffeM3OEPTncCzysNU
1 parent 9c9e5f6 commit ddaa23c

8 files changed

Lines changed: 100 additions & 180 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'kimaki': patch
3+
---
4+
5+
Inject the Kimaki system prompt through OpenCode v2 `session.instructions.entry.put`.
6+
7+
v2 `session.prompt` and `session.command` have no `system` field. Kimaki now writes the full session-stable prompt as instruction entry `kimaki` after session create. Chat, slash commands, tools, and compaction all see it. The sqlite `session_system_contexts` side channel and the plugin rebuild of that prompt are gone.
8+
9+
Kimaki now uses published OpenCode **2.0.2** (`@opencode/cli`, `@opencode/client`, `@opencode/plugin`) so the instruction-entry size limit is 256 KiB.

‎cli/src/database.ts‎

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,60 +1428,6 @@ export async function deleteStaleForumSyncConfigs({ appId, forumChannelId, outpu
14281428
))
14291429
}
14301430

1431-
export type SessionSystemContextPayload = {
1432-
channelId?: string
1433-
guildId?: string
1434-
threadId?: string
1435-
channelTopic?: string
1436-
agents?: Array<{ name: string; description?: string }>
1437-
userId?: string
1438-
parentSessionId?: string
1439-
scheduledTask?: {
1440-
taskId?: number
1441-
scheduleKind: 'at' | 'cron'
1442-
cronExpr?: string | null
1443-
timezone?: string | null
1444-
}
1445-
dataDir?: string
1446-
critiqueEnabled?: boolean
1447-
}
1448-
1449-
export async function upsertSessionSystemContext({
1450-
sessionId,
1451-
payload,
1452-
}: {
1453-
sessionId: string
1454-
payload: SessionSystemContextPayload
1455-
}) {
1456-
const db = await getDb()
1457-
const encoded = JSON.stringify(payload)
1458-
await db.insert(schema.session_system_contexts)
1459-
.values({ session_id: sessionId, payload: encoded })
1460-
.onConflictDoUpdate({
1461-
target: schema.session_system_contexts.session_id,
1462-
set: { payload: encoded, updated_at: new Date() },
1463-
})
1464-
}
1465-
1466-
export async function getSessionSystemContext(sessionId: string) {
1467-
const db = await getDb()
1468-
const row = await db.query.session_system_contexts.findFirst({
1469-
where: { session_id: sessionId },
1470-
})
1471-
if (!row) return null
1472-
try {
1473-
return JSON.parse(row.payload) as SessionSystemContextPayload
1474-
} catch (cause) {
1475-
return new Error('Invalid session system context payload', { cause })
1476-
}
1477-
}
1478-
1479-
export async function deleteSessionSystemContext(sessionId: string) {
1480-
const db = await getDb()
1481-
await db.delete(schema.session_system_contexts)
1482-
.where(orm.eq(schema.session_system_contexts.session_id, sessionId))
1483-
}
1484-
14851431
export async function createIpcRequest({ type, sessionId, threadId, payload }: { type: IpcRequestType; sessionId: string; threadId: string; payload: string }) {
14861432
const db = await getDb()
14871433
const [row] = await db.insert(schema.ipc_requests).values({ type, session_id: sessionId, thread_id: threadId, payload }).returning()

‎cli/src/kimaki-opencode-plugin/index.ts‎

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import dedent from 'string-dedent'
1111
import type {
1212
createIpcRequest,
1313
getIpcRequestById,
14-
getSessionSystemContext,
1514
getThreadIdBySessionId,
1615
upsertSessionSleep,
17-
deleteSessionSystemContext,
1816
} from '../database.ts'
1917
import type { setDataDir } from '../config.ts'
2018
import type { setPluginLogFilePath } from '../plugin-logger.ts'
@@ -47,8 +45,6 @@ type DatabaseModule = {
4745
createIpcRequest: typeof createIpcRequest
4846
getIpcRequestById: typeof getIpcRequestById
4947
upsertSessionSleep: typeof upsertSessionSleep
50-
getSessionSystemContext: typeof getSessionSystemContext
51-
deleteSessionSystemContext: typeof deleteSessionSystemContext
5248
}
5349

5450
type TaskScheduleModule = {
@@ -309,60 +305,13 @@ export default Plugin.define({
309305
})) {
310306
if (event.type !== 'session.deleted') continue
311307
sessions.delete(event.data.sessionID)
312-
const database = await loadDatabaseModule()
313-
await database.deleteSessionSystemContext(event.data.sessionID)
314308
}
315309
} catch {
316310
// aborted on plugin unload
317311
}
318312
})()
319313

320314
await ctx.session.hook('context', async (event) => {
321-
const { getOpencodeSystemMessage } = (await import(
322-
siblingModuleHref('system-message')
323-
)) as {
324-
getOpencodeSystemMessage: (input: {
325-
sessionId: string
326-
channelId?: string
327-
guildId?: string
328-
threadId?: string
329-
channelTopic?: string
330-
agents?: Array<{ name: string; description?: string }>
331-
userId?: string
332-
parentSessionId?: string
333-
scheduledTask?: {
334-
taskId?: number
335-
scheduleKind: 'at' | 'cron'
336-
cronExpr?: string | null
337-
timezone?: string | null
338-
}
339-
dataDir?: string
340-
critiqueEnabled?: boolean
341-
}) => string
342-
}
343-
const database = await loadDatabaseModule()
344-
const stored = await database.getSessionSystemContext(event.sessionID)
345-
if (stored instanceof Error) {
346-
logger.warn('session system context read failed', stored)
347-
}
348-
const context = stored instanceof Error || stored === null ? undefined : stored
349-
pushSystemText(
350-
event,
351-
getOpencodeSystemMessage({
352-
sessionId: event.sessionID,
353-
channelId: context?.channelId,
354-
guildId: context?.guildId,
355-
threadId: context?.threadId,
356-
channelTopic: context?.channelTopic,
357-
agents: context?.agents,
358-
userId: context?.userId,
359-
parentSessionId: context?.parentSessionId,
360-
scheduledTask: context?.scheduledTask,
361-
dataDir: context?.dataDir || process.env.KIMAKI_DATA_DIR,
362-
critiqueEnabled: context?.critiqueEnabled,
363-
}),
364-
)
365-
366315
const state = getContextSession(event.sessionID)
367316
const userText = latestUserText(event.messages)
368317
if (userText.includes(TUTORIAL_WELCOME_TEXT)) {

‎cli/src/schema.sql‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,6 @@ CREATE TABLE IF NOT EXISTS `session_start_sources` (
195195
CONSTRAINT `fk_session_start_sources_scheduled_task_id_scheduled_tasks_id_fk` FOREIGN KEY (`scheduled_task_id`) REFERENCES `scheduled_tasks`(`id`) ON UPDATE CASCADE ON DELETE SET NULL
196196
);
197197

198-
CREATE TABLE IF NOT EXISTS `session_system_contexts` (
199-
`session_id` text PRIMARY KEY,
200-
`payload` text NOT NULL,
201-
`updated_at` datetime DEFAULT CURRENT_TIMESTAMP NOT NULL
202-
);
203-
204198
CREATE TABLE IF NOT EXISTS `thread_sessions` (
205199
`thread_id` text PRIMARY KEY,
206200
`session_id` text NOT NULL,

‎cli/src/schema.ts‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,6 @@ export const session_sleeps = sqliteCore.sqliteTable('session_sleeps', {
277277
sqliteCore.index('session_sleeps_status_wake_at_idx').on(table.status, table.wake_at),
278278
])
279279

280-
export const session_system_contexts = sqliteCore.sqliteTable('session_system_contexts', {
281-
session_id: sqliteCore.text('session_id').primaryKey().notNull(),
282-
payload: sqliteCore.text('payload').notNull(),
283-
updated_at: datetime('updated_at').notNull().default(orm.sql`CURRENT_TIMESTAMP`).$onUpdate(() => new Date()),
284-
})
285-
286280
export const ipc_requests = sqliteCore.sqliteTable('ipc_requests', {
287281
id: sqliteCore.text('id').primaryKey().notNull().$defaultFn(() => crypto.randomUUID()),
288282
type: sqliteCore.text('type', { enum: ['file_upload', 'action_buttons'] }).notNull(),
@@ -321,7 +315,6 @@ export const relations = defineRelations({
321315
session_start_sources,
322316
forum_sync_configs,
323317
session_sleeps,
324-
session_system_contexts,
325318
ipc_requests,
326319
}, (r) => ({
327320
thread_sessions: {

0 commit comments

Comments
 (0)