kimaki 0.4.88 → 0.4.89
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/add-directory.e2e.test.js +101 -0
- package/dist/agent-model.e2e.test.js +1 -5
- package/dist/cli.js +7 -1
- package/dist/commands/add-directory.js +67 -0
- package/dist/commands/user-command.js +10 -9
- package/dist/context-awareness-plugin.js +32 -18
- package/dist/context-awareness-plugin.test.js +57 -0
- package/dist/directory-permissions.js +38 -0
- package/dist/directory-permissions.test.js +37 -0
- package/dist/discord-bot.js +5 -0
- package/dist/gateway-proxy.e2e.test.js +3 -5
- package/dist/generated/models/thread_allowed_directories.js +1 -0
- package/dist/markdown.test.js +0 -32
- package/dist/message-finish-field.e2e.test.js +2 -2
- package/dist/opencode.js +43 -0
- package/dist/queue-advanced-abort.e2e.test.js +0 -1
- package/dist/queue-advanced-action-buttons.e2e.test.js +1 -12
- package/dist/queue-advanced-footer.e2e.test.js +5 -42
- package/dist/queue-advanced-model-switch.e2e.test.js +2 -5
- package/dist/queue-advanced-permissions-typing.e2e.test.js +1 -13
- package/dist/queue-advanced-typing-interrupt.e2e.test.js +2 -3
- package/dist/queue-question-select-drain.e2e.test.js +1 -2
- package/dist/runtime-lifecycle.e2e.test.js +1 -3
- package/dist/session-handler/thread-session-runtime.js +11 -2
- package/dist/task-runner.js +6 -0
- package/dist/task-schedule.js +4 -0
- package/dist/thread-message-queue.e2e.test.js +5 -5
- package/dist/undo-redo.e2e.test.js +0 -1
- package/dist/voice-message.e2e.test.js +1 -6
- package/package.json +5 -5
- package/src/agent-model.e2e.test.ts +1 -5
- package/src/cli.ts +13 -1
- package/src/commands/user-command.ts +11 -11
- package/src/context-awareness-plugin.test.ts +66 -0
- package/src/context-awareness-plugin.ts +46 -26
- package/src/discord-bot.ts +5 -0
- package/src/gateway-proxy.e2e.test.ts +3 -5
- package/src/markdown.test.ts +0 -32
- package/src/message-finish-field.e2e.test.ts +2 -2
- package/src/opencode.ts +57 -0
- package/src/queue-advanced-abort.e2e.test.ts +0 -1
- package/src/queue-advanced-action-buttons.e2e.test.ts +1 -12
- package/src/queue-advanced-footer.e2e.test.ts +5 -42
- package/src/queue-advanced-model-switch.e2e.test.ts +2 -5
- package/src/queue-advanced-permissions-typing.e2e.test.ts +1 -13
- package/src/queue-advanced-typing-interrupt.e2e.test.ts +2 -3
- package/src/queue-question-select-drain.e2e.test.ts +1 -2
- package/src/runtime-lifecycle.e2e.test.ts +1 -3
- package/src/session-handler/thread-runtime-state.ts +4 -0
- package/src/session-handler/thread-session-runtime.ts +13 -0
- package/src/system-message.ts +10 -1
- package/src/task-runner.ts +6 -0
- package/src/task-schedule.ts +6 -0
- package/src/thread-message-queue.e2e.test.ts +5 -5
- package/src/undo-redo.e2e.test.ts +0 -1
- package/src/voice-message.e2e.test.ts +1 -6
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// E2e tests for thread-scoped external directory preapproval via /add-directory.
|
|
2
|
+
import { describe, expect, test } from 'vitest';
|
|
3
|
+
import { setupQueueAdvancedSuite, TEST_USER_ID, } from './queue-advanced-e2e-setup.js';
|
|
4
|
+
import { waitForBotMessageContaining, waitForFooterMessage, } from './test-utils.js';
|
|
5
|
+
const TEXT_CHANNEL_ID = '200000000000001014';
|
|
6
|
+
describe('/add-directory', () => {
|
|
7
|
+
const ctx = setupQueueAdvancedSuite({
|
|
8
|
+
channelId: TEXT_CHANNEL_ID,
|
|
9
|
+
channelName: 'add-directory-e2e',
|
|
10
|
+
dirName: 'add-directory-e2e',
|
|
11
|
+
username: 'add-directory-tester',
|
|
12
|
+
});
|
|
13
|
+
test('preapproves external directory access for the current thread', async () => {
|
|
14
|
+
await ctx.discord.channel(TEXT_CHANNEL_ID).user(TEST_USER_ID).sendMessage({
|
|
15
|
+
content: 'Reply with exactly: add-directory-setup',
|
|
16
|
+
});
|
|
17
|
+
const thread = await ctx.discord.channel(TEXT_CHANNEL_ID).waitForThread({
|
|
18
|
+
timeout: 4_000,
|
|
19
|
+
predicate: (candidate) => {
|
|
20
|
+
return candidate.name === 'Reply with exactly: add-directory-setup';
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
const th = ctx.discord.thread(thread.id);
|
|
24
|
+
await th.waitForBotReply({ timeout: 4_000 });
|
|
25
|
+
await waitForFooterMessage({
|
|
26
|
+
discord: ctx.discord,
|
|
27
|
+
threadId: thread.id,
|
|
28
|
+
timeout: 4_000,
|
|
29
|
+
});
|
|
30
|
+
const slashCommand = await th.user(TEST_USER_ID).runSlashCommand({
|
|
31
|
+
name: 'add-directory',
|
|
32
|
+
options: [{ name: 'path', type: 3, value: '/Users/morse' }],
|
|
33
|
+
});
|
|
34
|
+
await th.waitForInteractionAck({
|
|
35
|
+
interactionId: slashCommand.id,
|
|
36
|
+
timeout: 4_000,
|
|
37
|
+
});
|
|
38
|
+
await th.user(TEST_USER_ID).sendMessage({
|
|
39
|
+
content: 'PERMISSION_TYPING_MARKER add-directory-flow first',
|
|
40
|
+
});
|
|
41
|
+
await waitForBotMessageContaining({
|
|
42
|
+
discord: ctx.discord,
|
|
43
|
+
threadId: thread.id,
|
|
44
|
+
userId: TEST_USER_ID,
|
|
45
|
+
text: 'permission-flow-done',
|
|
46
|
+
timeout: 8_000,
|
|
47
|
+
});
|
|
48
|
+
await waitForFooterMessage({
|
|
49
|
+
discord: ctx.discord,
|
|
50
|
+
threadId: thread.id,
|
|
51
|
+
timeout: 12_000,
|
|
52
|
+
afterMessageIncludes: 'permission-flow-done',
|
|
53
|
+
afterAuthorId: ctx.discord.botUserId,
|
|
54
|
+
});
|
|
55
|
+
for (let attempt = 0; attempt < 10; attempt++) {
|
|
56
|
+
const messages = await th.getMessages();
|
|
57
|
+
const hasPermissionPrompt = messages.some((message) => {
|
|
58
|
+
return message.content.includes('Permission Required');
|
|
59
|
+
});
|
|
60
|
+
expect(hasPermissionPrompt).toBe(false);
|
|
61
|
+
await new Promise((resolve) => {
|
|
62
|
+
setTimeout(resolve, 20);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
await th.user(TEST_USER_ID).sendMessage({
|
|
66
|
+
content: 'PERMISSION_TYPING_MARKER add-directory-flow second',
|
|
67
|
+
});
|
|
68
|
+
await waitForBotMessageContaining({
|
|
69
|
+
discord: ctx.discord,
|
|
70
|
+
threadId: thread.id,
|
|
71
|
+
userId: TEST_USER_ID,
|
|
72
|
+
text: 'Permission Required',
|
|
73
|
+
timeout: 8_000,
|
|
74
|
+
});
|
|
75
|
+
const timeline = await th.text();
|
|
76
|
+
expect(timeline).toMatchInlineSnapshot(`
|
|
77
|
+
"--- from: user (add-directory-tester)
|
|
78
|
+
Reply with exactly: add-directory-setup
|
|
79
|
+
--- from: assistant (TestBot)
|
|
80
|
+
⬥ ok
|
|
81
|
+
*project ⋅ main ⋅ Ns ⋅ N% ⋅ deterministic-v2*
|
|
82
|
+
Directory preapproved for the next message in this thread.
|
|
83
|
+
\`/Users/morse\`
|
|
84
|
+
Kimaki will auto-accept matching external directory requests for \`/Users/morse/*\` during the next run only.
|
|
85
|
+
--- from: user (add-directory-tester)
|
|
86
|
+
PERMISSION_TYPING_MARKER add-directory-flow first
|
|
87
|
+
--- from: assistant (TestBot)
|
|
88
|
+
⬥ requesting external read permission
|
|
89
|
+
⬥ permission-flow-done
|
|
90
|
+
*project ⋅ main ⋅ Ns ⋅ N% ⋅ deterministic-v2*
|
|
91
|
+
--- from: user (add-directory-tester)
|
|
92
|
+
PERMISSION_TYPING_MARKER add-directory-flow second
|
|
93
|
+
--- from: assistant (TestBot)
|
|
94
|
+
⚠️ **Permission Required**
|
|
95
|
+
**Type:** \`external_directory\`
|
|
96
|
+
Agent is accessing files outside the project. [Learn more](https://opencode.ai/docs/permissions/#external-directories)
|
|
97
|
+
**Pattern:** \`/Users/morse/*\`
|
|
98
|
+
⬥ requesting external read permission"
|
|
99
|
+
`);
|
|
100
|
+
}, 20_000);
|
|
101
|
+
});
|
|
@@ -345,8 +345,7 @@ describe('agent model resolution', () => {
|
|
|
345
345
|
Reply with exactly: system-context-check
|
|
346
346
|
--- from: assistant (TestBot)
|
|
347
347
|
⬥ system-context-ok
|
|
348
|
-
*project ⋅ main ⋅ Ns ⋅ N% ⋅ agent-model-v2 ⋅ **test-agent***
|
|
349
|
-
⬥ ok"
|
|
348
|
+
*project ⋅ main ⋅ Ns ⋅ N% ⋅ agent-model-v2 ⋅ **test-agent***"
|
|
350
349
|
`);
|
|
351
350
|
}, 15_000);
|
|
352
351
|
test('new thread uses channel model when channel model preference is set', async () => {
|
|
@@ -513,7 +512,6 @@ describe('agent model resolution', () => {
|
|
|
513
512
|
Reply with exactly: second-thread-msg
|
|
514
513
|
--- from: assistant (TestBot)
|
|
515
514
|
⬥ ok
|
|
516
|
-
⬥ ok
|
|
517
515
|
*project ⋅ main ⋅ Ns ⋅ N% ⋅ agent-model-v2 ⋅ **test-agent***"
|
|
518
516
|
`);
|
|
519
517
|
const secondMessages = await discord.thread(thread.id).getMessages();
|
|
@@ -596,7 +594,6 @@ describe('agent model resolution', () => {
|
|
|
596
594
|
Reply with exactly: default-second-msg
|
|
597
595
|
--- from: assistant (TestBot)
|
|
598
596
|
⬥ ok
|
|
599
|
-
⬥ ok
|
|
600
597
|
*project ⋅ main ⋅ Ns ⋅ N% ⋅ deterministic-v2*"
|
|
601
598
|
`);
|
|
602
599
|
const secondMessages = await discord.thread(thread.id).getMessages();
|
|
@@ -664,7 +661,6 @@ describe('agent model resolution', () => {
|
|
|
664
661
|
Reply with exactly: after-switch-msg
|
|
665
662
|
--- from: assistant (TestBot)
|
|
666
663
|
⬥ ok
|
|
667
|
-
⬥ ok
|
|
668
664
|
*project ⋅ main ⋅ Ns ⋅ N% ⋅ plan-model-v2 ⋅ **plan***"
|
|
669
665
|
`);
|
|
670
666
|
const secondFooter = [...(await discord.thread(thread.id).getMessages())]
|
package/dist/cli.js
CHANGED
|
@@ -1662,6 +1662,8 @@ cli
|
|
|
1662
1662
|
.option('--model <model>', 'Model to use (format: provider/model)')
|
|
1663
1663
|
.option('--permission <rule>', z.array(z.string()).describe('Session permission rule (repeatable). Format: "tool:action" or "tool:pattern:action". ' +
|
|
1664
1664
|
'Actions: allow, deny, ask. Examples: --permission "bash:deny" --permission "edit:deny"'))
|
|
1665
|
+
.option('--injection-guard <pattern>', z.array(z.string()).describe('Injection guard scan pattern (repeatable). Enables prompt injection detection for this session. ' +
|
|
1666
|
+
'Format: "tool:argsGlob". Examples: --injection-guard "bash:*" --injection-guard "webfetch:*"'))
|
|
1665
1667
|
.option('--send-at <schedule>', 'Schedule send for future (UTC ISO date/time ending in Z, or cron expression)')
|
|
1666
1668
|
.option('--thread <threadId>', 'Post prompt to an existing thread')
|
|
1667
1669
|
.option('--session <sessionId>', 'Post prompt to thread mapped to an existing session')
|
|
@@ -1892,6 +1894,7 @@ cli
|
|
|
1892
1894
|
username: null,
|
|
1893
1895
|
userId: null,
|
|
1894
1896
|
permissions: options.permission?.length ? options.permission : null,
|
|
1897
|
+
injectionGuardPatterns: options.injectionGuard?.length ? options.injectionGuard : null,
|
|
1895
1898
|
};
|
|
1896
1899
|
const taskId = await createScheduledTask({
|
|
1897
1900
|
scheduleKind: parsedSchedule.scheduleKind,
|
|
@@ -1912,8 +1915,9 @@ cli
|
|
|
1912
1915
|
process.exit(0);
|
|
1913
1916
|
}
|
|
1914
1917
|
const threadPromptMarker = {
|
|
1915
|
-
|
|
1918
|
+
start: true,
|
|
1916
1919
|
...(options.permission?.length ? { permissions: options.permission } : {}),
|
|
1920
|
+
...(options.injectionGuard?.length ? { injectionGuardPatterns: options.injectionGuard } : {}),
|
|
1917
1921
|
};
|
|
1918
1922
|
const promptEmbed = [
|
|
1919
1923
|
{
|
|
@@ -2005,6 +2009,7 @@ cli
|
|
|
2005
2009
|
username: resolvedUser?.username || null,
|
|
2006
2010
|
userId: resolvedUser?.id || null,
|
|
2007
2011
|
permissions: options.permission?.length ? options.permission : null,
|
|
2012
|
+
injectionGuardPatterns: options.injectionGuard?.length ? options.injectionGuard : null,
|
|
2008
2013
|
};
|
|
2009
2014
|
const taskId = await createScheduledTask({
|
|
2010
2015
|
scheduleKind: parsedSchedule.scheduleKind,
|
|
@@ -2036,6 +2041,7 @@ cli
|
|
|
2036
2041
|
...(options.agent && { agent: options.agent }),
|
|
2037
2042
|
...(options.model && { model: options.model }),
|
|
2038
2043
|
...(options.permission?.length && { permissions: options.permission }),
|
|
2044
|
+
...(options.injectionGuard?.length && { injectionGuardPatterns: options.injectionGuard }),
|
|
2039
2045
|
};
|
|
2040
2046
|
const autoStartEmbed = embedMarker
|
|
2041
2047
|
? [{ color: 0x2b2d31, footer: { text: yaml.dump(embedMarker) } }]
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// /add-directory command - Preapprove an external directory for this thread.
|
|
2
|
+
import { ChannelType, MessageFlags, } from 'discord.js';
|
|
3
|
+
import { getThreadSession } from '../database.js';
|
|
4
|
+
import { normalizeAllowedDirectoryPath } from '../directory-permissions.js';
|
|
5
|
+
import { resolveWorkingDirectory, SILENT_MESSAGE_FLAGS, } from '../discord-utils.js';
|
|
6
|
+
import { createLogger } from '../logger.js';
|
|
7
|
+
import { getOrCreateRuntime } from '../session-handler/thread-session-runtime.js';
|
|
8
|
+
const logger = createLogger('ADD_DIR');
|
|
9
|
+
export async function handleAddDirectoryCommand({ command, appId, }) {
|
|
10
|
+
const inputPath = command.options.getString('path', true);
|
|
11
|
+
const channel = command.channel;
|
|
12
|
+
if (!channel) {
|
|
13
|
+
await command.reply({
|
|
14
|
+
content: 'This command can only be used in a channel',
|
|
15
|
+
flags: MessageFlags.Ephemeral | SILENT_MESSAGE_FLAGS,
|
|
16
|
+
});
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const isThread = [
|
|
20
|
+
ChannelType.PublicThread,
|
|
21
|
+
ChannelType.PrivateThread,
|
|
22
|
+
ChannelType.AnnouncementThread,
|
|
23
|
+
].includes(channel.type);
|
|
24
|
+
if (!isThread) {
|
|
25
|
+
await command.reply({
|
|
26
|
+
content: 'This command can only be used in a thread with an active session',
|
|
27
|
+
flags: MessageFlags.Ephemeral | SILENT_MESSAGE_FLAGS,
|
|
28
|
+
});
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
await command.deferReply({
|
|
32
|
+
flags: MessageFlags.Ephemeral | SILENT_MESSAGE_FLAGS,
|
|
33
|
+
});
|
|
34
|
+
const sessionId = await getThreadSession(channel.id);
|
|
35
|
+
if (!sessionId) {
|
|
36
|
+
await command.editReply('No active session in this thread');
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const resolved = await resolveWorkingDirectory({
|
|
40
|
+
channel: channel,
|
|
41
|
+
});
|
|
42
|
+
if (!resolved) {
|
|
43
|
+
await command.editReply('Could not determine project directory for this channel');
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const normalizedPath = normalizeAllowedDirectoryPath({
|
|
47
|
+
input: inputPath,
|
|
48
|
+
workingDirectory: resolved.workingDirectory,
|
|
49
|
+
});
|
|
50
|
+
if (normalizedPath instanceof Error) {
|
|
51
|
+
await command.editReply(normalizedPath.message);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const runtime = getOrCreateRuntime({
|
|
55
|
+
threadId: channel.id,
|
|
56
|
+
thread: channel,
|
|
57
|
+
projectDirectory: resolved.projectDirectory,
|
|
58
|
+
sdkDirectory: resolved.workingDirectory,
|
|
59
|
+
channelId: channel.parentId || channel.id,
|
|
60
|
+
appId,
|
|
61
|
+
});
|
|
62
|
+
runtime.primeNextExternalDirectoryAccess({
|
|
63
|
+
directory: normalizedPath,
|
|
64
|
+
});
|
|
65
|
+
await command.editReply(`Directory preapproved for the next message in this thread.\n\`${normalizedPath}\`\nKimaki will auto-accept matching external directory requests for \`${normalizedPath}/*\` during the next run only.`);
|
|
66
|
+
logger.log(`Thread ${channel.id} primed one-shot directory ${normalizedPath}`);
|
|
67
|
+
}
|
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
// Handles slash commands that map to user-configured commands in opencode.json.
|
|
3
3
|
import { ChannelType, MessageFlags, } from 'discord.js';
|
|
4
4
|
import { getOrCreateRuntime } from '../session-handler/thread-session-runtime.js';
|
|
5
|
-
import {
|
|
5
|
+
import { SILENT_MESSAGE_FLAGS } from '../discord-utils.js';
|
|
6
6
|
import { createLogger, LogPrefix } from '../logger.js';
|
|
7
7
|
import { getChannelDirectory, getThreadSession } from '../database.js';
|
|
8
8
|
import { store } from '../store.js';
|
|
9
9
|
import fs from 'node:fs';
|
|
10
10
|
const userCommandLogger = createLogger(LogPrefix.USER_CMD);
|
|
11
|
+
const DISCORD_MESSAGE_LIMIT = 2000;
|
|
12
|
+
const DISCORD_THREAD_NAME_LIMIT = 100;
|
|
11
13
|
export const handleUserCommand = async ({ command, appId, }) => {
|
|
12
14
|
const discordCommandName = command.commandName;
|
|
13
15
|
// Look up the original OpenCode command name from the mapping populated at registration.
|
|
@@ -17,6 +19,10 @@ export const handleUserCommand = async ({ command, appId, }) => {
|
|
|
17
19
|
const fallbackBase = discordCommandName.replace(/-(cmd|skill|mcp-prompt)$/, '');
|
|
18
20
|
const commandName = registered?.name || fallbackBase;
|
|
19
21
|
const args = command.options.getString('arguments') || '';
|
|
22
|
+
const commandInvocation = args ? `/${commandName} ${args}` : `/${commandName}`;
|
|
23
|
+
const threadOpeningMessage = commandInvocation.length <= DISCORD_MESSAGE_LIMIT
|
|
24
|
+
? commandInvocation
|
|
25
|
+
: `${commandInvocation.slice(0, DISCORD_MESSAGE_LIMIT - 14)}... truncated`;
|
|
20
26
|
userCommandLogger.log(`Executing /${commandName} (from /${discordCommandName}) argsLength=${args.length}`);
|
|
21
27
|
const channel = command.channel;
|
|
22
28
|
userCommandLogger.log(`Channel info: type=${channel?.type}, id=${channel?.id}, isNull=${channel === null}`);
|
|
@@ -81,7 +87,7 @@ export const handleUserCommand = async ({ command, appId, }) => {
|
|
|
81
87
|
const commandPayload = { name: commandName, arguments: args };
|
|
82
88
|
if (isThread && thread) {
|
|
83
89
|
// Running in existing thread - just send the command
|
|
84
|
-
await command.editReply(`Running
|
|
90
|
+
await command.editReply(`Running ${commandInvocation}...`);
|
|
85
91
|
const runtime = getOrCreateRuntime({
|
|
86
92
|
threadId: thread.id,
|
|
87
93
|
thread,
|
|
@@ -102,21 +108,16 @@ export const handleUserCommand = async ({ command, appId, }) => {
|
|
|
102
108
|
else if (textChannel) {
|
|
103
109
|
// Running in text channel - create a new thread
|
|
104
110
|
const starterMessage = await textChannel.send({
|
|
105
|
-
content:
|
|
111
|
+
content: threadOpeningMessage,
|
|
106
112
|
flags: SILENT_MESSAGE_FLAGS,
|
|
107
113
|
});
|
|
108
|
-
const threadName = `/${commandName}`;
|
|
109
114
|
const newThread = await starterMessage.startThread({
|
|
110
|
-
name:
|
|
115
|
+
name: commandInvocation.slice(0, DISCORD_THREAD_NAME_LIMIT),
|
|
111
116
|
autoArchiveDuration: 1440,
|
|
112
117
|
reason: `OpenCode command: ${commandName}`,
|
|
113
118
|
});
|
|
114
119
|
// Add user to thread so it appears in their sidebar
|
|
115
120
|
await newThread.members.add(command.user.id);
|
|
116
|
-
if (args) {
|
|
117
|
-
const argsPreview = args.length > 1800 ? `${args.slice(0, 1800)}\n... truncated` : args;
|
|
118
|
-
await sendThreadMessage(newThread, `Args: ${argsPreview}`);
|
|
119
|
-
}
|
|
120
121
|
await command.editReply(`Started /${commandName} in ${newThread.toString()}`);
|
|
121
122
|
const runtime = getOrCreateRuntime({
|
|
122
123
|
threadId: newThread.id,
|
|
@@ -49,18 +49,20 @@ export function shouldInjectBranch({ previousGitState, currentGitState, }) {
|
|
|
49
49
|
const text = currentGitState.warning || `\n[current git branch is ${currentGitState.label}]`;
|
|
50
50
|
return { inject: true, text };
|
|
51
51
|
}
|
|
52
|
-
export function shouldInjectPwd({
|
|
53
|
-
if (
|
|
52
|
+
export function shouldInjectPwd({ currentDir, previousDir, announcedDir, }) {
|
|
53
|
+
if (announcedDir === currentDir) {
|
|
54
54
|
return { inject: false };
|
|
55
55
|
}
|
|
56
|
-
|
|
56
|
+
const priorDirectory = announcedDir || previousDir;
|
|
57
|
+
if (!priorDirectory || priorDirectory === currentDir) {
|
|
57
58
|
return { inject: false };
|
|
58
59
|
}
|
|
59
60
|
return {
|
|
60
61
|
inject: true,
|
|
61
|
-
text: `\n[working directory
|
|
62
|
-
`
|
|
63
|
-
`
|
|
62
|
+
text: `\n[working directory changed. Previous working directory: ${priorDirectory}. ` +
|
|
63
|
+
`Current working directory: ${currentDir}. ` +
|
|
64
|
+
`You MUST read, write, and edit files only under ${currentDir}. ` +
|
|
65
|
+
`Do NOT read, write, or edit files under ${priorDirectory}.]`,
|
|
64
66
|
};
|
|
65
67
|
}
|
|
66
68
|
const TEN_MINUTES = 10 * 60 * 1000;
|
|
@@ -149,20 +151,25 @@ async function resolveGitState({ directory, }) {
|
|
|
149
151
|
'create or switch to a branch before committing.]',
|
|
150
152
|
};
|
|
151
153
|
}
|
|
152
|
-
// Resolve the
|
|
153
|
-
//
|
|
154
|
+
// Resolve the last observed session directory via the SDK.
|
|
155
|
+
// Refreshed on every real user message because sessions can switch directories
|
|
156
|
+
// mid-thread and the pwd reminder must compare old vs new accurately.
|
|
154
157
|
async function resolveSessionDirectory({ client, sessionID, state, }) {
|
|
155
|
-
|
|
156
|
-
return state.resolvedDirectory;
|
|
157
|
-
}
|
|
158
|
+
const previousDirectory = state.resolvedDirectory;
|
|
158
159
|
const result = await errore.tryAsync(() => {
|
|
159
160
|
return client.session.get({ path: { id: sessionID } });
|
|
160
161
|
});
|
|
161
162
|
if (result instanceof Error || !result.data?.directory) {
|
|
162
|
-
return
|
|
163
|
+
return {
|
|
164
|
+
currentDirectory: previousDirectory || null,
|
|
165
|
+
previousDirectory,
|
|
166
|
+
};
|
|
163
167
|
}
|
|
164
168
|
state.resolvedDirectory = result.data.directory;
|
|
165
|
-
return
|
|
169
|
+
return {
|
|
170
|
+
currentDirectory: result.data.directory,
|
|
171
|
+
previousDirectory,
|
|
172
|
+
};
|
|
166
173
|
}
|
|
167
174
|
// ── Plugin ───────────────────────────────────────────────────────
|
|
168
175
|
const contextAwarenessPlugin = async ({ directory, client }) => {
|
|
@@ -224,23 +231,30 @@ const contextAwarenessPlugin = async ({ directory, client }) => {
|
|
|
224
231
|
}
|
|
225
232
|
const messageID = first.messageID;
|
|
226
233
|
// -- Resolve session working directory --
|
|
227
|
-
const
|
|
234
|
+
const sessionDirectory = await resolveSessionDirectory({
|
|
228
235
|
client,
|
|
229
236
|
sessionID,
|
|
230
237
|
state,
|
|
231
238
|
});
|
|
232
|
-
|
|
239
|
+
// The plugin request directory is the current directory Kimaki asked
|
|
240
|
+
// OpenCode to operate on for this message. Prefer it over session.get()
|
|
241
|
+
// when they disagree so reminders and MEMORY/branch context follow the
|
|
242
|
+
// new worktree immediately after a folder switch.
|
|
243
|
+
const effectiveDirectory = directory;
|
|
233
244
|
// -- Branch / detached HEAD detection --
|
|
234
245
|
// Resolved early but injected last so it appears at the end of parts.
|
|
235
246
|
const gitState = await resolveGitState({ directory: effectiveDirectory });
|
|
236
247
|
// -- Working directory change detection --
|
|
237
248
|
const pwdResult = shouldInjectPwd({
|
|
238
|
-
|
|
239
|
-
|
|
249
|
+
currentDir: effectiveDirectory,
|
|
250
|
+
previousDir: sessionDirectory.previousDirectory ||
|
|
251
|
+
(sessionDirectory.currentDirectory !== effectiveDirectory
|
|
252
|
+
? sessionDirectory.currentDirectory || undefined
|
|
253
|
+
: undefined),
|
|
240
254
|
announcedDir: state.announcedDirectory,
|
|
241
255
|
});
|
|
242
256
|
if (pwdResult.inject) {
|
|
243
|
-
state.announcedDirectory =
|
|
257
|
+
state.announcedDirectory = effectiveDirectory;
|
|
244
258
|
output.parts.push({
|
|
245
259
|
id: `prt_${crypto.randomUUID()}`,
|
|
246
260
|
sessionID,
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Tests for context-awareness directory switch reminders.
|
|
2
|
+
import { describe, expect, test } from 'vitest';
|
|
3
|
+
import { shouldInjectPwd } from './context-awareness-plugin.js';
|
|
4
|
+
describe('shouldInjectPwd', () => {
|
|
5
|
+
test('does not inject when current directory matches announced directory', () => {
|
|
6
|
+
const result = shouldInjectPwd({
|
|
7
|
+
currentDir: '/repo/worktree',
|
|
8
|
+
previousDir: '/repo/main',
|
|
9
|
+
announcedDir: '/repo/worktree',
|
|
10
|
+
});
|
|
11
|
+
expect(result).toMatchInlineSnapshot(`
|
|
12
|
+
{
|
|
13
|
+
"inject": false,
|
|
14
|
+
}
|
|
15
|
+
`);
|
|
16
|
+
});
|
|
17
|
+
test('does not inject without a previous directory to warn about', () => {
|
|
18
|
+
const result = shouldInjectPwd({
|
|
19
|
+
currentDir: '/repo/worktree',
|
|
20
|
+
previousDir: undefined,
|
|
21
|
+
announcedDir: undefined,
|
|
22
|
+
});
|
|
23
|
+
expect(result).toMatchInlineSnapshot(`
|
|
24
|
+
{
|
|
25
|
+
"inject": false,
|
|
26
|
+
}
|
|
27
|
+
`);
|
|
28
|
+
});
|
|
29
|
+
test('names previous and current directories in the correct order', () => {
|
|
30
|
+
const result = shouldInjectPwd({
|
|
31
|
+
currentDir: '/repo/worktree',
|
|
32
|
+
previousDir: '/repo/main',
|
|
33
|
+
announcedDir: undefined,
|
|
34
|
+
});
|
|
35
|
+
expect(result).toMatchInlineSnapshot(`
|
|
36
|
+
{
|
|
37
|
+
"inject": true,
|
|
38
|
+
"text": "
|
|
39
|
+
[working directory changed. Previous working directory: /repo/main. Current working directory: /repo/worktree. You MUST read, write, and edit files only under /repo/worktree. Do NOT read, write, or edit files under /repo/main.]",
|
|
40
|
+
}
|
|
41
|
+
`);
|
|
42
|
+
});
|
|
43
|
+
test('prefers the last announced directory as the previous directory', () => {
|
|
44
|
+
const result = shouldInjectPwd({
|
|
45
|
+
currentDir: '/repo/worktree-b',
|
|
46
|
+
previousDir: '/repo/main',
|
|
47
|
+
announcedDir: '/repo/worktree-a',
|
|
48
|
+
});
|
|
49
|
+
expect(result).toMatchInlineSnapshot(`
|
|
50
|
+
{
|
|
51
|
+
"inject": true,
|
|
52
|
+
"text": "
|
|
53
|
+
[working directory changed. Previous working directory: /repo/worktree-a. Current working directory: /repo/worktree-b. You MUST read, write, and edit files only under /repo/worktree-b. Do NOT read, write, or edit files under /repo/worktree-a.]",
|
|
54
|
+
}
|
|
55
|
+
`);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Directory permission helpers for one-shot external directory preapproval.
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
export function normalizeAllowedDirectoryPath({ input, workingDirectory, }) {
|
|
5
|
+
const trimmedInput = input.trim();
|
|
6
|
+
if (!trimmedInput) {
|
|
7
|
+
return new Error('Path cannot be empty');
|
|
8
|
+
}
|
|
9
|
+
const withoutTrailingGlob = trimmedInput.replace(/[\\/]\*+$/u, '');
|
|
10
|
+
if (!withoutTrailingGlob) {
|
|
11
|
+
return new Error('Path cannot be empty');
|
|
12
|
+
}
|
|
13
|
+
if (withoutTrailingGlob.includes('*') || withoutTrailingGlob.includes('?')) {
|
|
14
|
+
return new Error('Path must be a directory, not a glob pattern');
|
|
15
|
+
}
|
|
16
|
+
const expandedHomeDirectory = (() => {
|
|
17
|
+
if (withoutTrailingGlob === '~') {
|
|
18
|
+
return os.homedir();
|
|
19
|
+
}
|
|
20
|
+
if (withoutTrailingGlob.startsWith('~/')) {
|
|
21
|
+
return path.join(os.homedir(), withoutTrailingGlob.slice(2));
|
|
22
|
+
}
|
|
23
|
+
return withoutTrailingGlob;
|
|
24
|
+
})();
|
|
25
|
+
const absolutePath = path.isAbsolute(expandedHomeDirectory)
|
|
26
|
+
? expandedHomeDirectory
|
|
27
|
+
: path.resolve(workingDirectory, expandedHomeDirectory);
|
|
28
|
+
const normalizedPath = path.normalize(absolutePath);
|
|
29
|
+
const root = path.parse(normalizedPath).root;
|
|
30
|
+
const withoutTrailingSlash = normalizedPath.length > root.length
|
|
31
|
+
? normalizedPath.replace(/[\\/]+$/u, '')
|
|
32
|
+
: normalizedPath;
|
|
33
|
+
return withoutTrailingSlash.replaceAll('\\', '/');
|
|
34
|
+
}
|
|
35
|
+
export function buildAllowedDirectoryPatterns({ directory, }) {
|
|
36
|
+
const childPattern = directory.endsWith('/') ? `${directory}*` : `${directory}/*`;
|
|
37
|
+
return [directory, childPattern];
|
|
38
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Tests for one-shot directory permission path normalization helpers.
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { describe, expect, test } from 'vitest';
|
|
5
|
+
import { buildAllowedDirectoryPatterns, normalizeAllowedDirectoryPath, } from './directory-permissions.js';
|
|
6
|
+
describe('normalizeAllowedDirectoryPath', () => {
|
|
7
|
+
test('resolves relative paths from the working directory', () => {
|
|
8
|
+
const result = normalizeAllowedDirectoryPath({
|
|
9
|
+
input: '../shared/',
|
|
10
|
+
workingDirectory: '/repo/worktree/app',
|
|
11
|
+
});
|
|
12
|
+
expect(result).toBe('/repo/worktree/shared');
|
|
13
|
+
});
|
|
14
|
+
test('expands home directories and strips implicit trailing glob', () => {
|
|
15
|
+
const result = normalizeAllowedDirectoryPath({
|
|
16
|
+
input: '~/projects/*',
|
|
17
|
+
workingDirectory: '/repo/worktree/app',
|
|
18
|
+
});
|
|
19
|
+
expect(result).toBe(`${os.homedir().replaceAll('\\', '/')}/projects`);
|
|
20
|
+
});
|
|
21
|
+
test('rejects glob patterns in the middle of the path', () => {
|
|
22
|
+
const result = normalizeAllowedDirectoryPath({
|
|
23
|
+
input: 'src/*/nested',
|
|
24
|
+
workingDirectory: '/repo/worktree/app',
|
|
25
|
+
});
|
|
26
|
+
expect(result instanceof Error ? result.message : result).toBe('Path must be a directory, not a glob pattern');
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
describe('buildAllowedDirectoryPatterns', () => {
|
|
30
|
+
test('adds exact and child wildcard patterns for a directory', () => {
|
|
31
|
+
const directory = path.join('/repo', 'shared').replaceAll('\\', '/');
|
|
32
|
+
expect(buildAllowedDirectoryPatterns({ directory })).toEqual([
|
|
33
|
+
'/repo/shared',
|
|
34
|
+
'/repo/shared/*',
|
|
35
|
+
]);
|
|
36
|
+
});
|
|
37
|
+
});
|
package/dist/discord-bot.js
CHANGED
|
@@ -268,6 +268,9 @@ export async function startDiscordBot({ token, appId, discordClient, useWorktree
|
|
|
268
268
|
const cliInjectedPermissions = isCliInjectedPrompt
|
|
269
269
|
? promptMarker?.permissions
|
|
270
270
|
: undefined;
|
|
271
|
+
const cliInjectedInjectionGuardPatterns = isCliInjectedPrompt
|
|
272
|
+
? promptMarker?.injectionGuardPatterns
|
|
273
|
+
: undefined;
|
|
271
274
|
// Always ignore our own messages (unless CLI-injected prompt above).
|
|
272
275
|
// Without this, assigning the Kimaki role to the bot itself would loop.
|
|
273
276
|
if (isSelfBotMessage && !isCliInjectedPrompt) {
|
|
@@ -488,6 +491,7 @@ export async function startDiscordBot({ token, appId, discordClient, useWorktree
|
|
|
488
491
|
agent: cliInjectedAgent,
|
|
489
492
|
model: cliInjectedModel,
|
|
490
493
|
permissions: cliInjectedPermissions,
|
|
494
|
+
injectionGuardPatterns: cliInjectedInjectionGuardPatterns,
|
|
491
495
|
sessionStartSource: sessionStartSource
|
|
492
496
|
? {
|
|
493
497
|
scheduleKind: sessionStartSource.scheduleKind,
|
|
@@ -759,6 +763,7 @@ export async function startDiscordBot({ token, appId, discordClient, useWorktree
|
|
|
759
763
|
agent: marker.agent,
|
|
760
764
|
model: marker.model,
|
|
761
765
|
permissions: marker.permissions,
|
|
766
|
+
injectionGuardPatterns: marker.injectionGuardPatterns,
|
|
762
767
|
mode: 'opencode',
|
|
763
768
|
sessionStartSource: botThreadStartSource
|
|
764
769
|
? {
|
|
@@ -353,9 +353,8 @@ describeIf('gateway-proxy e2e', () => {
|
|
|
353
353
|
--- from: user (proxy-tester)
|
|
354
354
|
follow up through proxy
|
|
355
355
|
--- from: assistant (TestBot)
|
|
356
|
-
⬥
|
|
357
|
-
*project ⋅ main ⋅ Ns ⋅ N% ⋅ deterministic-v2*
|
|
358
|
-
⬥ ok"
|
|
356
|
+
⬥ gateway-proxy-reply
|
|
357
|
+
*project ⋅ main ⋅ Ns ⋅ N% ⋅ deterministic-v2*"
|
|
359
358
|
`);
|
|
360
359
|
expect(reply).toBeDefined();
|
|
361
360
|
expect(reply.content.trim().length).toBeGreaterThan(0);
|
|
@@ -385,9 +384,8 @@ describeIf('gateway-proxy e2e', () => {
|
|
|
385
384
|
--- from: user (proxy-tester)
|
|
386
385
|
follow up through proxy
|
|
387
386
|
--- from: assistant (TestBot)
|
|
388
|
-
⬥
|
|
387
|
+
⬥ gateway-proxy-reply
|
|
389
388
|
*project ⋅ main ⋅ Ns ⋅ N% ⋅ deterministic-v2*
|
|
390
|
-
⬥ ok
|
|
391
389
|
--- from: user (proxy-tester)
|
|
392
390
|
!echo proxy-shell-test
|
|
393
391
|
--- from: assistant (TestBot)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/markdown.test.js
CHANGED
|
@@ -184,22 +184,6 @@ test('generate markdown with system info', async () => {
|
|
|
184
184
|
|
|
185
185
|
|
|
186
186
|
*Completed in Xs*
|
|
187
|
-
|
|
188
|
-
### 🤖 Assistant (deterministic-v2)
|
|
189
|
-
|
|
190
|
-
**Started using deterministic-provider/deterministic-v2**
|
|
191
|
-
|
|
192
|
-
Hello! This is a deterministic markdown test response.
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
*Completed in Xs*
|
|
196
|
-
|
|
197
|
-
### 🤖 Assistant (deterministic-v2)
|
|
198
|
-
|
|
199
|
-
**Started using deterministic-provider/deterministic-v2**
|
|
200
|
-
|
|
201
|
-
Hello! This is a deterministic markdown test response.
|
|
202
|
-
|
|
203
187
|
"
|
|
204
188
|
`);
|
|
205
189
|
});
|
|
@@ -235,22 +219,6 @@ test('generate markdown without system info', async () => {
|
|
|
235
219
|
|
|
236
220
|
|
|
237
221
|
*Completed in Xs*
|
|
238
|
-
|
|
239
|
-
### 🤖 Assistant (deterministic-v2)
|
|
240
|
-
|
|
241
|
-
**Started using deterministic-provider/deterministic-v2**
|
|
242
|
-
|
|
243
|
-
Hello! This is a deterministic markdown test response.
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
*Completed in Xs*
|
|
247
|
-
|
|
248
|
-
### 🤖 Assistant (deterministic-v2)
|
|
249
|
-
|
|
250
|
-
**Started using deterministic-provider/deterministic-v2**
|
|
251
|
-
|
|
252
|
-
Hello! This is a deterministic markdown test response.
|
|
253
|
-
|
|
254
222
|
"
|
|
255
223
|
`);
|
|
256
224
|
});
|
|
@@ -142,7 +142,7 @@ test('tool-call step has finish="tool-calls", follow-up has finish="stop"', asyn
|
|
|
142
142
|
expect(completedAssistants).toMatchInlineSnapshot(`
|
|
143
143
|
[
|
|
144
144
|
{
|
|
145
|
-
"finish":
|
|
145
|
+
"finish": "tool-calls",
|
|
146
146
|
"partTypes": [
|
|
147
147
|
"step-start",
|
|
148
148
|
"text",
|
|
@@ -150,7 +150,7 @@ test('tool-call step has finish="tool-calls", follow-up has finish="stop"', asyn
|
|
|
150
150
|
],
|
|
151
151
|
},
|
|
152
152
|
{
|
|
153
|
-
"finish":
|
|
153
|
+
"finish": "stop",
|
|
154
154
|
"partTypes": [
|
|
155
155
|
"step-start",
|
|
156
156
|
"text",
|