@deepseek-harness-tui/dsh-tui 0.7.1 → 0.7.3
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/README.md +7 -5
- package/bin/dsh-tui.js +42 -1
- package/cordis.patch.yml +10 -0
- package/lib/types/commands.js +1 -1
- package/lib/types/components/GoalTodoPanel.d.ts.map +1 -1
- package/lib/types/components/GoalTodoPanel.js +6 -1
- package/lib/types/components/HistorySearchDialog.d.ts.map +1 -1
- package/lib/types/components/HistorySearchDialog.js +5 -1
- package/lib/types/components/LoadedContextPanel.d.ts.map +1 -1
- package/lib/types/components/LoadedContextPanel.js +1 -1
- package/lib/types/components/MessageList.d.ts.map +1 -1
- package/lib/types/components/MessageList.js +5 -1
- package/lib/types/components/PromptInput.d.ts.map +1 -1
- package/lib/types/components/PromptInput.js +52 -74
- package/lib/types/components/SearchBox.d.ts +4 -0
- package/lib/types/components/SearchBox.d.ts.map +1 -1
- package/lib/types/components/SearchBox.js +101 -2
- package/lib/types/components/Spinner/SpinnerAnimationRow.d.ts +4 -1
- package/lib/types/components/Spinner/SpinnerAnimationRow.d.ts.map +1 -1
- package/lib/types/components/Spinner/SpinnerAnimationRow.js +10 -5
- package/lib/types/components/WorkingSpinner.d.ts +3 -1
- package/lib/types/components/WorkingSpinner.d.ts.map +1 -1
- package/lib/types/components/WorkingSpinner.js +2 -2
- package/lib/types/components/design-system/ThemedText.d.ts +9 -3
- package/lib/types/components/design-system/ThemedText.d.ts.map +1 -1
- package/lib/types/components/design-system/ThemedText.js +3 -5
- package/lib/types/components/questions/AskUserQuestionPanel.d.ts.map +1 -1
- package/lib/types/components/questions/AskUserQuestionPanel.js +52 -8
- package/lib/types/components/questions/PlanReviewPanel.d.ts.map +1 -1
- package/lib/types/components/questions/PlanReviewPanel.js +12 -3
- package/lib/types/components/sessions/SessionListRow.d.ts +26 -0
- package/lib/types/components/sessions/SessionListRow.d.ts.map +1 -0
- package/lib/types/components/sessions/SessionListRow.js +35 -0
- package/lib/types/components/sessions/SessionPreview.d.ts +25 -0
- package/lib/types/components/sessions/SessionPreview.d.ts.map +1 -0
- package/lib/types/components/sessions/SessionPreview.js +60 -0
- package/lib/types/dsh-adapter/channel.d.ts +19 -4
- package/lib/types/dsh-adapter/channel.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel.js +76 -82
- package/lib/types/dsh-adapter/compat/index.d.ts +1 -1
- package/lib/types/dsh-adapter/compat/index.d.ts.map +1 -1
- package/lib/types/dsh-adapter/compat/index.js +1 -1
- package/lib/types/dsh-adapter/compat/sessionLog.d.ts +8 -0
- package/lib/types/dsh-adapter/compat/sessionLog.d.ts.map +1 -1
- package/lib/types/dsh-adapter/compat/sessionLog.js +1 -1
- package/lib/types/dsh-adapter/index.d.ts.map +1 -1
- package/lib/types/dsh-adapter/index.js +10 -1
- package/lib/types/dsh-adapter/packaged-presets.d.ts +22 -0
- package/lib/types/dsh-adapter/packaged-presets.d.ts.map +1 -0
- package/lib/types/dsh-adapter/packaged-presets.js +93 -0
- package/lib/types/dsh-adapter/plugin.d.ts.map +1 -1
- package/lib/types/dsh-adapter/plugin.js +53 -7
- package/lib/types/dsh-adapter/presets.d.ts +16 -0
- package/lib/types/dsh-adapter/presets.d.ts.map +1 -1
- package/lib/types/dsh-adapter/presets.js +23 -0
- package/lib/types/dsh-adapter/providerWizard.d.ts.map +1 -1
- package/lib/types/dsh-adapter/providerWizard.js +3 -1
- package/lib/types/dsh-adapter/sessions/digest.d.ts +29 -0
- package/lib/types/dsh-adapter/sessions/digest.d.ts.map +1 -0
- package/lib/types/dsh-adapter/sessions/digest.js +224 -0
- package/lib/types/dsh-adapter/sessions/frames.d.ts +100 -0
- package/lib/types/dsh-adapter/sessions/frames.d.ts.map +1 -0
- package/lib/types/dsh-adapter/sessions/frames.js +246 -0
- package/lib/types/dsh-adapter/sessions/header.d.ts +50 -0
- package/lib/types/dsh-adapter/sessions/header.d.ts.map +1 -0
- package/lib/types/dsh-adapter/sessions/header.js +62 -0
- package/lib/types/dsh-adapter/sessions/index.d.ts +17 -0
- package/lib/types/dsh-adapter/sessions/index.d.ts.map +1 -0
- package/lib/types/dsh-adapter/sessions/index.js +15 -0
- package/lib/types/dsh-adapter/sessions/list.d.ts +41 -0
- package/lib/types/dsh-adapter/sessions/list.d.ts.map +1 -0
- package/lib/types/dsh-adapter/sessions/list.js +213 -0
- package/lib/types/dsh-adapter/sessions/store.d.ts +48 -0
- package/lib/types/dsh-adapter/sessions/store.d.ts.map +1 -0
- package/lib/types/dsh-adapter/sessions/store.js +164 -0
- package/lib/types/dsh-adapter/sessions/types.d.ts +142 -0
- package/lib/types/dsh-adapter/sessions/types.d.ts.map +1 -0
- package/lib/types/dsh-adapter/sessions/types.js +14 -0
- package/lib/types/i18n.d.ts +150 -22
- package/lib/types/i18n.d.ts.map +1 -1
- package/lib/types/i18n.js +45 -9
- package/lib/types/ink/components/Text.d.ts +9 -1
- package/lib/types/ink/components/Text.d.ts.map +1 -1
- package/lib/types/ink/components/Text.js +7 -6
- package/lib/types/ink/ink.d.ts.map +1 -1
- package/lib/types/ink/ink.js +4 -2
- package/lib/types/ink/log-update.d.ts.map +1 -1
- package/lib/types/ink/log-update.js +76 -9
- package/lib/types/ink/parse-keypress.d.ts.map +1 -1
- package/lib/types/ink/parse-keypress.js +4 -1
- package/lib/types/ink/terminal.d.ts +40 -4
- package/lib/types/ink/terminal.d.ts.map +1 -1
- package/lib/types/ink/terminal.js +57 -5
- package/lib/types/ink/termio/tokenize.d.ts +6 -0
- package/lib/types/ink/termio/tokenize.d.ts.map +1 -1
- package/lib/types/ink/termio/tokenize.js +18 -4
- package/lib/types/screens/Chat.d.ts +3 -2
- package/lib/types/screens/Chat.d.ts.map +1 -1
- package/lib/types/screens/Chat.js +104 -160
- package/lib/types/screens/SessionBrowser.d.ts +37 -0
- package/lib/types/screens/SessionBrowser.d.ts.map +1 -0
- package/lib/types/screens/SessionBrowser.js +452 -0
- package/lib/types/sessionHistory.d.ts +0 -8
- package/lib/types/sessionHistory.d.ts.map +1 -1
- package/lib/types/sessions/format.d.ts +117 -0
- package/lib/types/sessions/format.d.ts.map +1 -0
- package/lib/types/sessions/format.js +244 -0
- package/lib/types/sessions/view.d.ts +125 -0
- package/lib/types/sessions/view.d.ts.map +1 -0
- package/lib/types/sessions/view.js +234 -0
- package/lib/types/update.d.ts +21 -7
- package/lib/types/update.d.ts.map +1 -1
- package/lib/types/update.js +66 -16
- package/lib/types/utils/paths.d.ts.map +1 -1
- package/lib/types/utils/paths.js +4 -1
- package/package.json +37 -9
- package/presets/liangshen/.dsh-tui-managed.json +5 -0
- package/presets/liangshen/agent.cordis.yml +389 -0
- package/presets/liangshen/compaction-epoch.mjs +81 -0
- package/presets/liangshen/custom-bash.mjs +126 -0
- package/presets/liangshen/instruction-hint.mjs +181 -0
- package/presets/liangshen/preset.yml +3 -0
- package/presets/liangshen/skill-search.mjs +142 -0
- package/presets/liangshen/tool-bootstrap.mjs +300 -0
- package/lib/types/components/ResumePicker.d.ts +0 -26
- package/lib/types/components/ResumePicker.d.ts.map +0 -1
- package/lib/types/components/ResumePicker.js +0 -50
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Epoch-aware promotion tracker shared by the bootstrap and baseline-gate
|
|
3
|
+
* plugins of the anchored presets.
|
|
4
|
+
*
|
|
5
|
+
* A compaction rewrites the model-visible surface: the pre-compaction
|
|
6
|
+
* conversation collapses into one synthetic summary message, and the
|
|
7
|
+
* workspace-instruction baseline is re-injected from scratch. The first
|
|
8
|
+
* post-compaction request is therefore a "second first request" — the same
|
|
9
|
+
* first-token conditions the anchored presets exist to control. Promotion is
|
|
10
|
+
* epoch-aware: only a durable promotion signal (`tool/call` and/or
|
|
11
|
+
* `assistant/message`, per the caller's `promoteEvents`) recorded AFTER the
|
|
12
|
+
* last `compaction/end` boundary counts as promoted. Before any compaction
|
|
13
|
+
* the boundary is -1, which preserves the original one-shot semantics.
|
|
14
|
+
*
|
|
15
|
+
* State is memoized per session id and maintained incrementally through
|
|
16
|
+
* `observe()`; a cold session scans its durable log once (so resume and
|
|
17
|
+
* reload reconstruct the same phase), then O(1).
|
|
18
|
+
*
|
|
19
|
+
* By default subagents (`delegationDepth > 0`) are treated as already
|
|
20
|
+
* promoted so their first request can use tools. Set `includeSubagents: true`
|
|
21
|
+
* to make subagents follow the same bootstrap/anchor phase as top-level
|
|
22
|
+
* sessions.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** Build one epoch-aware promotion tracker. */
|
|
26
|
+
export function createEpochPromotion(promoteEvents, options = {}) {
|
|
27
|
+
const includeSubagents = options.includeSubagents === true
|
|
28
|
+
const promote = new Set(promoteEvents)
|
|
29
|
+
/** sessionId -> { boundary, promoted } */
|
|
30
|
+
const state = new Map()
|
|
31
|
+
|
|
32
|
+
/** Scan a session's durable log from scratch (cold start / resume). */
|
|
33
|
+
const scan = (session) => {
|
|
34
|
+
let boundary = -1
|
|
35
|
+
let promoted = false
|
|
36
|
+
for (const event of session.events) {
|
|
37
|
+
const seq = event.seq ?? 0 // events without a seq are treated as post-boundary
|
|
38
|
+
if (event.type === 'compaction/end') {
|
|
39
|
+
boundary = seq
|
|
40
|
+
promoted = false
|
|
41
|
+
continue
|
|
42
|
+
}
|
|
43
|
+
if (promote.has(event.type) && seq > boundary) promoted = true
|
|
44
|
+
}
|
|
45
|
+
const entry = { boundary, promoted }
|
|
46
|
+
state.set(session.id, entry)
|
|
47
|
+
return entry
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
/**
|
|
52
|
+
* Current phase of the agent's session.
|
|
53
|
+
* @param agent - the assembly/pre-step agent, or undefined outside an agent.
|
|
54
|
+
* @returns { boundary, promoted } — `boundary` is the last compaction/end
|
|
55
|
+
* seq (-1 before any compaction); `promoted` is true when a durable
|
|
56
|
+
* promotion signal exists after that boundary.
|
|
57
|
+
*/
|
|
58
|
+
status(agent) {
|
|
59
|
+
if (agent === undefined) return { boundary: -1, promoted: true }
|
|
60
|
+
const session = agent.session
|
|
61
|
+
if (session === undefined) return { boundary: -1, promoted: true }
|
|
62
|
+
// By default subagents keep the full catalog from their very first
|
|
63
|
+
// request; includeSubagents makes them follow the normal bootstrap phase.
|
|
64
|
+
if (!includeSubagents && (session.header?.delegationDepth ?? 0) > 0) return { boundary: -1, promoted: true }
|
|
65
|
+
return state.get(session.id) ?? scan(session)
|
|
66
|
+
},
|
|
67
|
+
/** Incremental feed: call on every `session/event`. */
|
|
68
|
+
observe(session, event) {
|
|
69
|
+
const entry = state.get(session.id)
|
|
70
|
+
if (entry === undefined) return
|
|
71
|
+
const seq = event.seq ?? 0
|
|
72
|
+
if (event.type === 'compaction/end') {
|
|
73
|
+
state.set(session.id, { boundary: seq, promoted: false })
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
if (promote.has(event.type) && seq > entry.boundary && !entry.promoted) {
|
|
77
|
+
state.set(session.id, { ...entry, promoted: true })
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* custom-bash — a Windows-capable `bash` tool that registers under the SAME
|
|
3
|
+
* name (`bash`) as the official persistent bash, with a Minimal-compatible
|
|
4
|
+
* description, but executes through `ctx.subprocess.spawn` instead of a PTY.
|
|
5
|
+
*
|
|
6
|
+
* WHY: DeepSeek's first-request trajectory anchor keys on the tool SCHEMA
|
|
7
|
+
* matching the RL training distribution (issue #11: persistent
|
|
8
|
+
* bash + str_replace_editor anchored 5/5 at maxTokens=256000, pwsh/read
|
|
9
|
+
* 8/8 standard-like). The official persistent bash uses a PTY, and DSH's PTY
|
|
10
|
+
* backend is linux/darwin-only — `subprocess-local` throws "terminal
|
|
11
|
+
* inspection is unsupported on platform win32". A custom tool that presents
|
|
12
|
+
* the same name and a Minimal-like description but spawns Git Bash through
|
|
13
|
+
* the ordinary (cross-platform) subprocess seam keeps the schema anchor
|
|
14
|
+
* without the PTY dependency.
|
|
15
|
+
*
|
|
16
|
+
* Executable resolution (config `bashPath`):
|
|
17
|
+
* - explicit absolute path (e.g. `C:\Program Files\Git\bin\bash.exe`), or
|
|
18
|
+
* - `bash` resolved through `ctx.subprocess.resolveExecutable` (PATH lookup).
|
|
19
|
+
*
|
|
20
|
+
* Semantics mirror the official bash tool: `bash -c <command>` in a fresh
|
|
21
|
+
* process, bounded output, non-zero exit reported not thrown. No sandbox
|
|
22
|
+
* confinement on Windows (the sandbox backend is linux-only); the tool
|
|
23
|
+
* description says so. The bootstrap catalog pairs this with
|
|
24
|
+
* `str_replace_editor` (Minimal's two tools).
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** Cordis plugin name used by loader diagnostics. */
|
|
28
|
+
export const name = 'custom-bash'
|
|
29
|
+
|
|
30
|
+
/** The subprocess and tools services must exist before this tool can register. */
|
|
31
|
+
export const inject = ['subprocess', 'tools']
|
|
32
|
+
|
|
33
|
+
const DEFAULT_TIMEOUT_MS = 120000
|
|
34
|
+
const DEFAULT_MAX_OUTPUT_BYTES = 64000
|
|
35
|
+
|
|
36
|
+
/** Tool parameter schema for the model-facing command. */
|
|
37
|
+
const commandSchema = {
|
|
38
|
+
type: 'object',
|
|
39
|
+
properties: {
|
|
40
|
+
command: {
|
|
41
|
+
type: 'string',
|
|
42
|
+
description: 'The bash command to execute (`bash -c` string domain).',
|
|
43
|
+
},
|
|
44
|
+
workdir: {
|
|
45
|
+
type: 'string',
|
|
46
|
+
description: 'Optional working directory; defaults to the session cwd.',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
required: ['command'],
|
|
50
|
+
additionalProperties: false,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Register the model-facing `bash` tool. */
|
|
54
|
+
export function apply(ctx, config) {
|
|
55
|
+
const bashPath = typeof config?.bashPath === 'string' && config.bashPath.length > 0 ? config.bashPath : 'bash'
|
|
56
|
+
const timeoutMs = Number.isSafeInteger(config?.timeoutMs) && config.timeoutMs > 0 ? config.timeoutMs : DEFAULT_TIMEOUT_MS
|
|
57
|
+
const maxOutputBytes = Number.isSafeInteger(config?.maxOutputBytes) && config.maxOutputBytes > 0 ? config.maxOutputBytes : DEFAULT_MAX_OUTPUT_BYTES
|
|
58
|
+
|
|
59
|
+
ctx.tools.register({
|
|
60
|
+
name: 'bash',
|
|
61
|
+
description: [
|
|
62
|
+
'Run commands in a bash shell (Git Bash on Windows)',
|
|
63
|
+
'* When invoking this tool, the contents of the "command" parameter does NOT need to be XML-escaped.',
|
|
64
|
+
"* You don't have access to the internet via this tool.",
|
|
65
|
+
'* You do have access to a mirror of common linux and python packages via apt and pip.',
|
|
66
|
+
'* State does NOT persist across command calls: each call runs in a fresh shell.',
|
|
67
|
+
"* To inspect a particular line range of a file, e.g. lines 10-25, try 'sed -n 10,25p /path/to/the/file'.",
|
|
68
|
+
'* Please avoid commands that may produce a very large amount of output.',
|
|
69
|
+
'* NOTE: runs without OS sandbox confinement on Windows (no landlock); treat output as untrusted.',
|
|
70
|
+
].join('\n'),
|
|
71
|
+
parameters: commandSchema,
|
|
72
|
+
output: {
|
|
73
|
+
schema: {
|
|
74
|
+
type: 'object',
|
|
75
|
+
additionalProperties: false,
|
|
76
|
+
properties: {
|
|
77
|
+
text: { type: 'string' },
|
|
78
|
+
},
|
|
79
|
+
required: ['text'],
|
|
80
|
+
},
|
|
81
|
+
render: (_args, value) => [{ type: 'text', text: value.text }],
|
|
82
|
+
},
|
|
83
|
+
async execute(args, exec) {
|
|
84
|
+
const shell = await ctx.subprocess.resolveExecutable(bashPath, undefined, exec?.signal)
|
|
85
|
+
const workdir = typeof args.workdir === 'string' && args.workdir.length > 0
|
|
86
|
+
? args.workdir
|
|
87
|
+
: exec?.agent?.session?.header?.cwd
|
|
88
|
+
const signal = exec?.signal
|
|
89
|
+
const handle = ctx.subprocess.spawn({
|
|
90
|
+
argv: [shell, '-c', args.command],
|
|
91
|
+
...workdir !== undefined ? { cwd: workdir } : {},
|
|
92
|
+
stdio: {
|
|
93
|
+
stdin: 'ignore',
|
|
94
|
+
stdout: { maxBytes: maxOutputBytes },
|
|
95
|
+
stderr: { maxBytes: maxOutputBytes },
|
|
96
|
+
},
|
|
97
|
+
...signal !== undefined ? { signal } : {},
|
|
98
|
+
graceMs: 3000,
|
|
99
|
+
})
|
|
100
|
+
let outcome
|
|
101
|
+
try {
|
|
102
|
+
outcome = await handle.done
|
|
103
|
+
} catch (error) {
|
|
104
|
+
// A spawn-level failure (bad executable, EPERM) surfaces as a throw,
|
|
105
|
+
// which the runtime turns into an isError result.
|
|
106
|
+
throw new Error(`bash spawn failed: ${String(error)}`)
|
|
107
|
+
}
|
|
108
|
+
let stdout = ''
|
|
109
|
+
let stderr = ''
|
|
110
|
+
try {
|
|
111
|
+
stdout = handle.collected.stdout.readFrom(0).text
|
|
112
|
+
stderr = handle.collected.stderr.readFrom(0).text
|
|
113
|
+
} catch {
|
|
114
|
+
// Collected readers may be unavailable on some backends; tolerate.
|
|
115
|
+
}
|
|
116
|
+
const text = [stdout, stderr].filter((part) => part.length > 0).join('\n')
|
|
117
|
+
const tail = text.length > 0 ? text : `exit code: ${outcome.exitCode} (no output)`
|
|
118
|
+
if (outcome.exitCode !== 0) {
|
|
119
|
+
// Non-zero exit is a reported failure, not a throw: the model sees the
|
|
120
|
+
// command output plus the exit code.
|
|
121
|
+
throw new Error(tail)
|
|
122
|
+
}
|
|
123
|
+
return { text: tail }
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* instruction-hint — replace `dsh-agent-instructions`' full AGENTS.md/CLAUDE.md
|
|
3
|
+
* injection with a minimal "these files exist" hint.
|
|
4
|
+
*
|
|
5
|
+
* WHY: the full workspace-instruction digest is a large injected block. After
|
|
6
|
+
* the anchored bootstrap promotes, we want the model to KNOW the instruction
|
|
7
|
+
* files exist (so it reads them before acting) without dumping their content
|
|
8
|
+
* into every request. The model reads the files itself via the filesystem
|
|
9
|
+
* tools when it needs them.
|
|
10
|
+
*
|
|
11
|
+
* Behavior:
|
|
12
|
+
* - After the session records its first durable promotion signal
|
|
13
|
+
* (`promoteOn`, default `either`), ONE hint message is injected (once per
|
|
14
|
+
* session — durable event scan, resume-safe), listing which instruction
|
|
15
|
+
* files were found:
|
|
16
|
+
* - user-global: `$DSH_HOME/AGENTS.md`
|
|
17
|
+
* - project chain: AGENTS.md / CLAUDE.md / AGENTS.local.md / CLAUDE.local.md
|
|
18
|
+
* walking up from the session cwd to the project root (a directory
|
|
19
|
+
* containing `.git`, or the cwd itself).
|
|
20
|
+
* - The hint instructs the model to READ the files before acting when
|
|
21
|
+
* relevant, without embedding their content.
|
|
22
|
+
* - Files are probed via `ctx.fs` (the host filesystem seam); a missing fs
|
|
23
|
+
* service or an unreadable probe degrades to no hint (never throws).
|
|
24
|
+
* - Pre-promotion requests get NO hint (matches the anchored bootstrap).
|
|
25
|
+
*
|
|
26
|
+
* ROW ORDER: this plugin registers its `agent/pre-step` handler with
|
|
27
|
+
* `prepend: true` and after `tool-bootstrap`, so it runs inside the
|
|
28
|
+
* bootstrap's outermost strip — but it emits AFTER promotion, when the strip
|
|
29
|
+
* is inactive. The hint source kind is `instruction-hint`, which is NOT in
|
|
30
|
+
* `suppressedContextSources`, so it is never stripped.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import { createEpochPromotion } from './compaction-epoch.mjs'
|
|
34
|
+
|
|
35
|
+
/** Cordis plugin name used by loader diagnostics. */
|
|
36
|
+
export const name = 'instruction-hint'
|
|
37
|
+
|
|
38
|
+
/** Durable session event types that count as a promotion signal per mode. */
|
|
39
|
+
const PROMOTE_EVENTS = {
|
|
40
|
+
'tool-call': ['tool/call'],
|
|
41
|
+
'assistant-message': ['assistant/message'],
|
|
42
|
+
either: ['tool/call', 'assistant/message'],
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Candidate file names, in probe order, for the project chain and user-global. */
|
|
46
|
+
const PROJECT_CANDIDATES = ['AGENTS.md', 'CLAUDE.md', 'AGENTS.local.md', 'CLAUDE.local.md']
|
|
47
|
+
const USER_GLOBAL_CANDIDATE = 'AGENTS.md'
|
|
48
|
+
|
|
49
|
+
function parsePromoteOn(value) {
|
|
50
|
+
if (value === undefined || value === 'either') return PROMOTE_EVENTS.either
|
|
51
|
+
if (value === 'tool-call' || value === 'assistant-message') return PROMOTE_EVENTS[value]
|
|
52
|
+
throw new TypeError(`${name}: promoteOn must be one of "tool-call", "assistant-message", "either"; got ${JSON.stringify(value)}`)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Find the project root: first ancestor containing any root marker (e.g. .git). */
|
|
56
|
+
async function findProjectRoot(fs, cwd, signal) {
|
|
57
|
+
let current = cwd
|
|
58
|
+
for (;;) {
|
|
59
|
+
for (const marker of ['.git', '.hg', '.svn']) {
|
|
60
|
+
try {
|
|
61
|
+
const target = await fs.resolve(joinPath(current, marker), { cwd, signal })
|
|
62
|
+
const info = await fs.stat(target, signal)
|
|
63
|
+
if (info !== undefined) return current
|
|
64
|
+
} catch {
|
|
65
|
+
// Probe failure = marker absent; continue.
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const parent = parentPath(current)
|
|
69
|
+
if (parent === current || parent.length === 0) return cwd
|
|
70
|
+
current = parent
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** List instruction files present in one directory (project candidates). */
|
|
75
|
+
async function presentInDir(fs, dir, candidates, signal) {
|
|
76
|
+
const found = []
|
|
77
|
+
for (const candidate of candidates) {
|
|
78
|
+
try {
|
|
79
|
+
const target = await fs.resolve(joinPath(dir, candidate), { cwd: dir, signal })
|
|
80
|
+
const info = await fs.stat(target, signal)
|
|
81
|
+
if (info !== undefined && info.type === 'file') found.push(candidate)
|
|
82
|
+
} catch {
|
|
83
|
+
// Absent or unreadable — skip.
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return found
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Join one path segment onto a directory (platform-agnostic string join). */
|
|
90
|
+
function joinPath(dir, segment) {
|
|
91
|
+
if (dir.endsWith('/') || dir.endsWith('\\')) return dir + segment
|
|
92
|
+
const sep = dir.includes('\\') ? '\\' : '/'
|
|
93
|
+
return dir + sep + segment
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Parent of an absolute Windows or POSIX path. */
|
|
97
|
+
function parentPath(path) {
|
|
98
|
+
const idx = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'))
|
|
99
|
+
if (idx <= 0) return path
|
|
100
|
+
const parent = path.slice(0, idx)
|
|
101
|
+
return parent.length === 0 ? path : parent
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Register the post-promotion instruction-hint injector. */
|
|
105
|
+
export function apply(ctx, config) {
|
|
106
|
+
const promoteEvents = parsePromoteOn(config.promoteOn)
|
|
107
|
+
if (config.includeSubagents !== undefined && typeof config.includeSubagents !== 'boolean') {
|
|
108
|
+
throw new TypeError(`${name}: includeSubagents must be a boolean`)
|
|
109
|
+
}
|
|
110
|
+
const promotion = createEpochPromotion(promoteEvents, { includeSubagents: config.includeSubagents === true })
|
|
111
|
+
ctx.on('session/event', (session, event) => promotion.observe(session, event))
|
|
112
|
+
|
|
113
|
+
/** Sessions that already received the hint. */
|
|
114
|
+
const hinted = new Set()
|
|
115
|
+
let warned = false
|
|
116
|
+
const warnOnce = (message) => {
|
|
117
|
+
if (warned) return
|
|
118
|
+
warned = true
|
|
119
|
+
try {
|
|
120
|
+
ctx.logger.warn(message)
|
|
121
|
+
} catch {
|
|
122
|
+
// Logger unavailable — the guard exists only to avoid spamming.
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
ctx.on('agent/pre-step', async ({ agent, signal }, next) => {
|
|
127
|
+
const decision = await next()
|
|
128
|
+
try {
|
|
129
|
+
if (promotion.status(agent).promoted !== true) return decision
|
|
130
|
+
const session = agent.session
|
|
131
|
+
if (session === undefined || hinted.has(session.id)) return decision
|
|
132
|
+
hinted.add(session.id)
|
|
133
|
+
|
|
134
|
+
const fs = ctx.get('fs')
|
|
135
|
+
if (fs === undefined) return decision
|
|
136
|
+
const cwd = session.header.cwd ?? process.cwd()
|
|
137
|
+
|
|
138
|
+
const projectFiles = []
|
|
139
|
+
const root = await findProjectRoot(fs, cwd, signal)
|
|
140
|
+
projectFiles.push(...await presentInDir(fs, root, PROJECT_CANDIDATES, signal))
|
|
141
|
+
|
|
142
|
+
const userGlobalFiles = []
|
|
143
|
+
try {
|
|
144
|
+
const dshHome = process.env.DSH_HOME ?? (process.env.USERPROFILE ? `${process.env.USERPROFILE}\\.dsh` : undefined)
|
|
145
|
+
if (dshHome !== undefined) {
|
|
146
|
+
userGlobalFiles.push(...await presentInDir(fs, dshHome, [USER_GLOBAL_CANDIDATE], signal))
|
|
147
|
+
}
|
|
148
|
+
} catch {
|
|
149
|
+
// Unreadable home probe — ignore.
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const sections = []
|
|
153
|
+
if (projectFiles.length > 0) {
|
|
154
|
+
sections.push(`Workspace instruction files exist: ${projectFiles.join(', ')} (project root: ${root}).`)
|
|
155
|
+
}
|
|
156
|
+
if (userGlobalFiles.length > 0) {
|
|
157
|
+
sections.push(`A user-global instruction file exists: ${USER_GLOBAL_CANDIDATE}.`)
|
|
158
|
+
}
|
|
159
|
+
if (sections.length === 0) return decision
|
|
160
|
+
|
|
161
|
+
const text = [
|
|
162
|
+
...sections,
|
|
163
|
+
'Do NOT assume their content. When a task touches this workspace, read the relevant instruction files first and follow them.',
|
|
164
|
+
].join(' ')
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
...decision,
|
|
168
|
+
messages: [...decision.messages, {
|
|
169
|
+
id: `instruction-hint-${session.id}`,
|
|
170
|
+
role: 'user',
|
|
171
|
+
content: [{ type: 'text', text }],
|
|
172
|
+
source: { kind: 'instruction-hint', form: 'hint' },
|
|
173
|
+
}],
|
|
174
|
+
}
|
|
175
|
+
} catch (error) {
|
|
176
|
+
// A hint bug must never hurt the session: skip the hint.
|
|
177
|
+
warnOnce(`${name}: hint injection failed, skipping: ${String((error && error.message) || error)}`)
|
|
178
|
+
return decision
|
|
179
|
+
}
|
|
180
|
+
}, { prepend: true })
|
|
181
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* skill-search — on-demand skill discovery and loading, replacing
|
|
3
|
+
* `dsh-tool-skill`'s full-catalog injection.
|
|
4
|
+
*
|
|
5
|
+
* WHY: the available-skills reminder (`<available_skills>`, ~9KB with many
|
|
6
|
+
* skills) is injected into the first step by dsh-tool-skill and again after
|
|
7
|
+
* every promotion/compaction. That large injected block perturbs the
|
|
8
|
+
* trajectory (issue #6: 0/9 anchored with the catalog present vs ~81%
|
|
9
|
+
* without). We remove the catalog injection entirely and expose two small
|
|
10
|
+
* tools instead — the Claude tool-search pattern:
|
|
11
|
+
*
|
|
12
|
+
* - `skill_search` — list skills whose name/description match a query
|
|
13
|
+
* (summaries only, bounded; no bodies). The model discovers what exists
|
|
14
|
+
* without a 9KB dump.
|
|
15
|
+
* - `skill_load` — load ONE skill's full instructions by exact name and
|
|
16
|
+
* inject them for the NEXT request via `agent.inject` (the non-waking
|
|
17
|
+
* next-step inbox). The model (or the user) calls this only when the
|
|
18
|
+
* skill is actually needed.
|
|
19
|
+
*
|
|
20
|
+
* Discovery reads `ctx.skills` scoped to the calling agent, exactly like
|
|
21
|
+
* dsh-tool-skill. If skills are unavailable the tools answer with a short
|
|
22
|
+
* message instead of throwing.
|
|
23
|
+
*
|
|
24
|
+
* NOTE: this plugin REPLACES the `dsh-tool-skill` row in the composition —
|
|
25
|
+
* the composition must NOT mount both, or the catalog injection returns.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/** Cordis plugin name used by loader diagnostics. */
|
|
29
|
+
export const name = 'skill-search'
|
|
30
|
+
|
|
31
|
+
/** The agent, tools, and skills services must exist before these tools can register. */
|
|
32
|
+
export const inject = ['agents', 'tools', 'skills']
|
|
33
|
+
|
|
34
|
+
const MAX_RESULTS = 20
|
|
35
|
+
|
|
36
|
+
/** Minimal JSON schema compiler for tool parameters (zero dependencies). */
|
|
37
|
+
function toJsonSchema(spec) {
|
|
38
|
+
const properties = {}
|
|
39
|
+
const required = []
|
|
40
|
+
for (const [key, meta] of Object.entries(spec || {})) {
|
|
41
|
+
const prop = { type: meta.type }
|
|
42
|
+
if (meta.description) prop.description = meta.description
|
|
43
|
+
properties[key] = prop
|
|
44
|
+
if (meta.required) required.push(key)
|
|
45
|
+
}
|
|
46
|
+
return { type: 'object', properties, required, additionalProperties: false }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Register the two on-demand skill tools. */
|
|
50
|
+
export function apply(ctx) {
|
|
51
|
+
/** Normalize a query into lowercase tokens for simple substring matching. */
|
|
52
|
+
const tokens = (text) => (text || '').toLowerCase().split(/[^a-z0-9_-]+/).filter(Boolean)
|
|
53
|
+
|
|
54
|
+
ctx.tools.register({
|
|
55
|
+
name: 'skill_search',
|
|
56
|
+
description: 'Search the available skills by keyword and return matching skill names with short descriptions. This session keeps NO skill catalog in the prompt — if a task looks like it matches a skill (document conversion, image processing, game reviews, markdown, PDF, spreadsheets, …), call skill_search FIRST to find it, then skill_load to activate it. Do NOT assume skill names from memory.',
|
|
57
|
+
parameters: toJsonSchema({
|
|
58
|
+
query: { type: 'string', required: true, description: 'search keywords (e.g. "pdf", "obsidian", "game review")' },
|
|
59
|
+
}),
|
|
60
|
+
output: {
|
|
61
|
+
schema: { type: 'object', additionalProperties: false, properties: { text: { type: 'string' } }, required: ['text'] },
|
|
62
|
+
render: (_a, v) => [{ type: 'text', text: v.text }],
|
|
63
|
+
},
|
|
64
|
+
async execute(args, exec) {
|
|
65
|
+
const wanted = tokens(args.query)
|
|
66
|
+
const scope = exec?.agent ?? ctx
|
|
67
|
+
try {
|
|
68
|
+
const all = await ctx.skills.list({
|
|
69
|
+
scope,
|
|
70
|
+
cwd: exec?.agent?.session?.header?.cwd,
|
|
71
|
+
signal: exec?.signal,
|
|
72
|
+
})
|
|
73
|
+
const matches = all.filter((skill) => {
|
|
74
|
+
if (wanted.length === 0) return true
|
|
75
|
+
const haystack = tokens(`${skill.name} ${skill.description ?? ''} ${skill.whenToUse ?? ''}`).join(' ')
|
|
76
|
+
return wanted.every((token) => haystack.includes(token))
|
|
77
|
+
})
|
|
78
|
+
const head = matches.slice(0, MAX_RESULTS)
|
|
79
|
+
const lines = head.map((skill) => {
|
|
80
|
+
const desc = (skill.description || '').split('\n')[0]
|
|
81
|
+
return `- ${skill.name}: ${desc}`
|
|
82
|
+
})
|
|
83
|
+
if (lines.length === 0) return { text: `No skills match "${args.query}". Use skill_search with other keywords.` }
|
|
84
|
+
const extra = matches.length > MAX_RESULTS ? `\n…(${matches.length - MAX_RESULTS} more)` : ''
|
|
85
|
+
return { text: `Matching skills (${matches.length}):\n${lines.join('\n')}${extra}\n\nLoad one with skill_load (exact name).` }
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return { text: `skill_search unavailable: ${String((error && error.message) || error)}` }
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
ctx.tools.register({
|
|
93
|
+
name: 'skill_load',
|
|
94
|
+
description: 'Load the full instructions of ONE skill by its exact name (from skill_search results) and inject them for the next request. Call this before acting on a task that matches the skill.',
|
|
95
|
+
parameters: toJsonSchema({
|
|
96
|
+
name: { type: 'string', required: true, description: 'exact skill name (kebab-case, from skill_search)' },
|
|
97
|
+
}),
|
|
98
|
+
output: {
|
|
99
|
+
schema: { type: 'object', additionalProperties: false, properties: { text: { type: 'string' } }, required: ['text'] },
|
|
100
|
+
render: (_a, v) => [{ type: 'text', text: v.text }],
|
|
101
|
+
},
|
|
102
|
+
async execute(args, exec) {
|
|
103
|
+
try {
|
|
104
|
+
const agent = exec?.agent
|
|
105
|
+
if (agent === undefined) return { text: 'skill_load requires an agent context.' }
|
|
106
|
+
const skill = await ctx.skills.get(args.name, {
|
|
107
|
+
scope: agent,
|
|
108
|
+
cwd: agent.session.header.cwd,
|
|
109
|
+
signal: exec?.signal,
|
|
110
|
+
})
|
|
111
|
+
if (skill === undefined) {
|
|
112
|
+
return { text: `No skill named "${args.name}". Run skill_search to list available skills.` }
|
|
113
|
+
}
|
|
114
|
+
const body = extractSkillBody(skill)
|
|
115
|
+
if (body.length === 0) {
|
|
116
|
+
return { text: `Skill "${args.name}" has no loadable body.` }
|
|
117
|
+
}
|
|
118
|
+
// Queue the skill content as a non-waking next-step context message,
|
|
119
|
+
// exactly like dsh-tool-skill's invocation injection.
|
|
120
|
+
agent.inject({
|
|
121
|
+
id: `skill-load-${args.name}-${Date.now()}`,
|
|
122
|
+
role: 'user',
|
|
123
|
+
content: [{ type: 'text', text: body }],
|
|
124
|
+
source: { kind: 'skill-invocation', name: args.name, form: 'instructions' },
|
|
125
|
+
})
|
|
126
|
+
return { text: `Skill "${args.name}" loaded; its instructions will be injected for the next request.` }
|
|
127
|
+
} catch (error) {
|
|
128
|
+
return { text: `skill_load failed: ${String((error && error.message) || error)}` }
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Extract the model-facing body of a loaded skill definition. */
|
|
135
|
+
function extractSkillBody(skill) {
|
|
136
|
+
const content = skill?.content ?? skill?.instructions ?? skill?.body
|
|
137
|
+
if (typeof content === 'string') return content
|
|
138
|
+
if (Array.isArray(content)) {
|
|
139
|
+
return content.map((part) => (typeof part === 'string' ? part : JSON.stringify(part))).join('\n')
|
|
140
|
+
}
|
|
141
|
+
return ''
|
|
142
|
+
}
|