@shuind/dsh-codex-harness 0.2.3 → 0.2.5

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.
@@ -4,11 +4,11 @@ import { installSettingsSection, settingsNamespace } from '@deepseek-ai/dsh-sett
4
4
  import { defineTool } from '@deepseek-ai/dsh-tools';
5
5
  import { APPLY_PATCH_DESCRIPTION, applyPatchHunks, parsePatch } from "./patch.js";
6
6
  import { renderExecResult, runExecCommand, runWriteStdin } from "./exec.js";
7
- import { hostedWebSearchStream, installHostedWebSearch, remoteCompactStream } from "./remote.js";
7
+ import { hostedWebSearchStream, installHostedWebSearch, remoteCompactStream, repairLegacyPiAiContextOverflow, } from "./remote.js";
8
8
  import { registerCodexActivityProjection } from "./activity.js";
9
- import { DEFAULT_CODEX_SYSTEM_PROMPT, resolveCodexSystemPrompt } from "./prompt.js";
9
+ import { DEFAULT_CODEX_PERSONA, DEFAULT_CODEX_SYSTEM_PROMPT, DEFAULT_DSH_CORE_SOURCE_PROMPT, DEFAULT_DSH_CORE_WEB_PROMPT, resolveCodexSystemPrompt, } from "./prompt.js";
10
10
  import { CODEX_SETTINGS_ENTRY, CODEX_SETTINGS_NAMESPACE, CODEX_SETTINGS_SCHEMA, } from "./settings.js";
11
- export { DEFAULT_CODEX_SYSTEM_PROMPT } from "./prompt.js";
11
+ export { DEFAULT_CODEX_PERSONA, DEFAULT_CODEX_SYSTEM_PROMPT, DEFAULT_DSH_CORE_SOURCE_PROMPT, DEFAULT_DSH_CORE_WEB_PROMPT, } from "./prompt.js";
12
12
  export { CODEX_SETTINGS_NAMESPACE, CODEX_SETTINGS_SCHEMA } from "./settings.js";
13
13
  export const name = 'codex';
14
14
  export const inject = ['tools', 'systemPrompt', 'shell', 'fs', 'llm', 'credentials', 'settings'];
@@ -24,6 +24,9 @@ export const Config = z.object({
24
24
  hostedWebSearch: z.boolean().default(true),
25
25
  remoteCompact: z.boolean().default(true),
26
26
  systemPrompt: z.string().default(DEFAULT_CODEX_SYSTEM_PROMPT),
27
+ persona: z.string().default(DEFAULT_CODEX_PERSONA),
28
+ harnessSourcePrompt: z.string().default(DEFAULT_DSH_CORE_SOURCE_PROMPT),
29
+ webSurfacePrompt: z.string().default(DEFAULT_DSH_CORE_WEB_PROMPT),
27
30
  });
28
31
  const LLM_PI_AI_SETTINGS = settingsNamespace('llm-pi-ai');
29
32
  const GPT_REASONING_EFFORTS = {
@@ -118,8 +121,14 @@ function watchConfiguredGptModels(ctx) {
118
121
  schedule();
119
122
  }
120
123
  /** Install the optional settings source used by requests and prompt assembly. */
121
- function installCodexSettings(ctx, systemPrompt) {
122
- const entry = { ...CODEX_SETTINGS_ENTRY, systemPrompt };
124
+ function installCodexSettings(ctx, systemPrompt, persona, harnessSourcePrompt, webSurfacePrompt) {
125
+ const entry = {
126
+ ...CODEX_SETTINGS_ENTRY,
127
+ systemPrompt,
128
+ persona,
129
+ harnessSourcePrompt,
130
+ webSurfacePrompt,
131
+ };
123
132
  const withDefaults = (value) => ({
124
133
  ...entry,
125
134
  ...value,
@@ -241,6 +250,45 @@ export function normalizeCodexPromptAssembly(assembly) {
241
250
  sections: [persona, ...sections.filter(section => section.name !== 'deployment:persona')],
242
251
  };
243
252
  }
253
+ function sectionText(assembly, name) {
254
+ const section = assembly.sections.find(candidate => candidate.name === name);
255
+ return section?.text;
256
+ }
257
+ function promptVariables(assembly) {
258
+ const variables = { ...assembly.variables };
259
+ if (variables.sourceRoot === undefined) {
260
+ const source = sectionText(assembly, 'harness:source');
261
+ const match = source?.match(/^The DeepSeek Harness implementation checkout is at (.+?)\. The checkout location/u);
262
+ if (match?.[1] !== undefined)
263
+ variables.sourceRoot = match[1];
264
+ }
265
+ if (variables.webUrl === undefined) {
266
+ const web = sectionText(assembly, 'app:web-surface');
267
+ const match = web?.match(/^You are interacting with the user through the DeepSeek Harness Web GUI at (.+?)\. When/u);
268
+ if (match?.[1] !== undefined)
269
+ variables.webUrl = match[1];
270
+ }
271
+ return variables;
272
+ }
273
+ function interpolateCodexPrompt(text, variables) {
274
+ return text.replace(/\{\{([\w.-]+)\}\}/g, (whole, key) => variables[key] ?? whole);
275
+ }
276
+ /** Apply user-editable Persona and DSH Core runtime prompt templates. */
277
+ function applyCodexPromptOverrides(assembly, settings) {
278
+ const variables = promptVariables(assembly);
279
+ const overrides = {
280
+ 'deployment:persona': settings.persona,
281
+ 'harness:source': settings.harnessSourcePrompt,
282
+ 'app:web-surface': settings.webSurfacePrompt,
283
+ };
284
+ const sections = assembly.sections.map(section => {
285
+ const template = overrides[section.name];
286
+ return template === undefined
287
+ ? section
288
+ : { ...section, text: interpolateCodexPrompt(template, variables) };
289
+ });
290
+ return { ...assembly, sections, variables };
291
+ }
244
292
  const CODEX_TOOL_NAMES = {
245
293
  terminalToolsEnabled: new Set(['exec_command', 'write_stdin']),
246
294
  patchToolEnabled: new Set(['apply_patch']),
@@ -258,7 +306,8 @@ export function applyCodexCapabilitySettings(assembly, settings) {
258
306
  const filtered = disabled.size === 0
259
307
  ? assembly
260
308
  : { ...assembly, tools: assembly.tools.filter(tool => !disabled.has(tool.name)) };
261
- return settings.promptEnabled ? normalizeCodexPromptAssembly(filtered) : filtered;
309
+ const withOverrides = applyCodexPromptOverrides(filtered, settings);
310
+ return settings.promptEnabled ? normalizeCodexPromptAssembly(withOverrides) : withOverrides;
262
311
  }
263
312
  /** Build the complete plugin-owned prompt, preserving an intentional empty value. */
264
313
  export function buildCodexSystemPrompt(config = {}) {
@@ -535,13 +584,16 @@ export function apply(ctx, config = {}) {
535
584
  hostedWebSearch: config.hostedWebSearch ?? true,
536
585
  remoteCompact: config.remoteCompact ?? true,
537
586
  systemPrompt: config.systemPrompt ?? DEFAULT_CODEX_SYSTEM_PROMPT,
587
+ persona: config.persona ?? DEFAULT_CODEX_PERSONA,
588
+ harnessSourcePrompt: config.harnessSourcePrompt ?? DEFAULT_DSH_CORE_SOURCE_PROMPT,
589
+ webSurfacePrompt: config.webSurfacePrompt ?? DEFAULT_DSH_CORE_WEB_PROMPT,
538
590
  };
539
591
  if (resolved.codexCore
540
592
  && ctx.fs.sandboxMode !== undefined
541
593
  && ctx.get('sandboxPolicy') === undefined) {
542
594
  throw new Error('codex: a sandboxing filesystem requires ctx.sandboxPolicy');
543
595
  }
544
- const codexSettings = installCodexSettings(ctx, resolved.systemPrompt);
596
+ const codexSettings = installCodexSettings(ctx, resolved.systemPrompt, resolved.persona, resolved.harnessSourcePrompt, resolved.webSurfacePrompt);
545
597
  const execConfig = { ...resolved, currentSettings: codexSettings.current };
546
598
  if (resolved.globalEnhancements) {
547
599
  // The generic pi-ai plugin remains the owner of provider routes. This
@@ -559,21 +611,22 @@ export function apply(ctx, config = {}) {
559
611
  }
560
612
  ctx.on('llm/stream', ((options, next) => {
561
613
  const settings = codexSettings.current();
614
+ const legacySafeNext = () => repairLegacyPiAiContextOverflow(next(), options.model, options.contextWindow);
562
615
  if (resolved.remoteCompact
563
616
  && settings.remoteCompactionEnabled
564
617
  && options.purpose === 'compaction'
565
618
  && isGptModel(options.model)) {
566
- return remoteCompactStream(ctx, options, next, {
619
+ return remoteCompactStream(ctx, options, legacySafeNext, {
567
620
  hostedWebSearch: resolved.hostedWebSearch && settings.hostedWebSearchEnabled,
568
621
  customApplyPatch: settings.patchToolEnabled,
569
622
  });
570
623
  }
571
624
  if (options.purpose === undefined && isGptModel(options.model)) {
572
- return hostedWebSearchStream(next, {
625
+ return repairLegacyPiAiContextOverflow(hostedWebSearchStream(next, {
573
626
  serviceTier: options.serviceTier,
574
627
  hostedWebSearch: resolved.hostedWebSearch && settings.hostedWebSearchEnabled,
575
628
  customApplyPatch: settings.patchToolEnabled,
576
- });
629
+ }), options.model, options.contextWindow);
577
630
  }
578
631
  return next();
579
632
  }));
@@ -1,5 +1,11 @@
1
1
  /** Default plugin-owned Codex operating prompt. */
2
2
  export declare const DEFAULT_CODEX_SYSTEM_PROMPT: string;
3
+ /** Default deployment Persona template used by the Codex preset. */
4
+ export declare const DEFAULT_CODEX_PERSONA: string;
5
+ /** Default DSH Core source-checkout guidance template. */
6
+ export declare const DEFAULT_DSH_CORE_SOURCE_PROMPT: string;
7
+ /** Default DSH Web runtime guidance template. */
8
+ export declare const DEFAULT_DSH_CORE_WEB_PROMPT: string;
3
9
  /** Resolve the full plugin-owned prompt, preserving intentional empty overrides. */
4
10
  export declare function resolveCodexSystemPrompt(systemPrompt?: string): string;
5
11
  //# sourceMappingURL=prompt.d.ts.map
@@ -31,10 +31,25 @@ export const DEFAULT_CODEX_SYSTEM_PROMPT = String.raw `## General
31
31
 
32
32
  ## Working principles
33
33
 
34
- - Ask, align, and clarify first. Gather enough context from the user, then align the approach to achieve the user's goal through the clearest, most effective path.
34
+ - Actively establish the user's current context rather than relying on stale assumptions or prior context. Before committing to an approach that could materially shape the outcome or direction of the work, state the intended path and align it with the user; clarify any material uncertainty first. Then pursue the user's goal through the clearest, most effective path.
35
35
  - Keep only the essential logic and core actions. Don't explain or test what was removed or why something wasn't done, especially when writing documentation or communicating. Convey enough valuable information with as few words as possible.
36
36
  - Solve problems by thinking from first principles and at a higher level.
37
37
  `;
38
+ /** Default deployment Persona template used by the Codex preset. */
39
+ export const DEFAULT_CODEX_PERSONA = String.raw `You are Codex, a coding agent based on the {{model}} model. Your working directory is {{cwd}}.`;
40
+ /** Default DSH Core source-checkout guidance template. */
41
+ export const DEFAULT_DSH_CORE_SOURCE_PROMPT = String.raw `The DeepSeek Harness implementation checkout is at {{sourceRoot}}. The checkout location and current working directory are separate values and may differ; never infer the working directory from this path. Use pwd to determine the current working directory. Use this checkout only to inspect or extend DSH itself.`;
42
+ /** Default DSH Web runtime guidance template. */
43
+ export const DEFAULT_DSH_CORE_WEB_PROMPT = [
44
+ 'You are interacting with the user through the DeepSeek Harness Web GUI at {{webUrl}}.',
45
+ 'When the user refers to "this page", "this GUI", or "this app" without naming another target, they mean this GUI.',
46
+ 'The browser provides no implicit DOM, route, or screenshot context.',
47
+ 'The client-plugin HMR receiver is active, but client-plugin changes reload without a refresh only while `pnpm run dev:web` is also running from this same checkout to rebuild their bundles; verify that watcher before promising automatic updates.',
48
+ 'Every other change - the apps/web shell and plain packages - requires rebuilding the affected Web artifacts and verifying this existing URL after a page refresh.',
49
+ 'Starting another server does not update this GUI.',
50
+ 'The apps/web Vite entry builds the shell but is not a standalone application because only dsh web injects window.__DSH_BOOT__.',
51
+ 'Do not start a replacement server unless the user asks; if one is needed, use a managed background job and verify its exact URL.',
52
+ ].join(' ');
38
53
  /** Resolve the full plugin-owned prompt, preserving intentional empty overrides. */
39
54
  export function resolveCodexSystemPrompt(systemPrompt) {
40
55
  return systemPrompt ?? DEFAULT_CODEX_SYSTEM_PROMPT;
@@ -29,6 +29,11 @@ export declare function replaceRemoteCompactions(body: JsonObject): JsonObject;
29
29
  export declare function installHostedWebSearch(): () => void;
30
30
  /** Iterate an existing DSH stream with the hosted-request context installed. */
31
31
  export declare function hostedWebSearchStream(next: () => AsyncIterable<StreamChunk>, options?: CodexRequestWireOptions): AsyncIterable<StreamChunk>;
32
+ /**
33
+ * Correct the usage-only overflow verdict emitted by pi-ai adapters that do
34
+ * not yet consume the request-scoped Codex capacity.
35
+ */
36
+ export declare function repairLegacyPiAiContextOverflow(stream: AsyncIterable<StreamChunk>, model: string, contextWindow: unknown): AsyncIterable<StreamChunk>;
32
37
  /** Remote-first compaction waterfall with the existing DSH path as fallback. */
33
38
  export declare function remoteCompactStream(ctx: Context, options: GenerateOptions, next: () => AsyncIterable<StreamChunk>, wireOptions?: CodexRequestWireOptions): AsyncIterable<StreamChunk>;
34
39
  export {};
@@ -8,6 +8,7 @@ import { APPLY_PATCH_DESCRIPTION, APPLY_PATCH_GRAMMAR } from "./patch.js";
8
8
  const SETTINGS_NAMESPACE = 'llm-pi-ai';
9
9
  const REMOTE_COMPACTION_OPEN = '<codex-remote-compaction>';
10
10
  const REMOTE_COMPACTION_CLOSE = '</codex-remote-compaction>';
11
+ const CONTEXT_WINDOW_EXCEEDED_CODE = 'CONTEXT_WINDOW_EXCEEDED';
11
12
  const CODEX_ERROR_DIRECTORY = ['diagnostics', 'openai-codex-errors'];
12
13
  const HOSTED_REQUESTS = new AsyncLocalStorage();
13
14
  let hostedPatchUsers = 0;
@@ -469,6 +470,47 @@ export function hostedWebSearchStream(next, options = {}) {
469
470
  }
470
471
  })();
471
472
  }
473
+ function contextTokens(usage) {
474
+ const fields = [
475
+ usage.inputTokens,
476
+ usage.outputTokens,
477
+ usage.cacheReadTokens ?? 0,
478
+ usage.cacheWriteTokens ?? 0,
479
+ ];
480
+ if (fields.some(value => !Number.isSafeInteger(value) || value < 0))
481
+ return undefined;
482
+ const total = fields.reduce((sum, value) => sum + value, 0);
483
+ return Number.isSafeInteger(total) ? total : undefined;
484
+ }
485
+ /**
486
+ * Correct the usage-only overflow verdict emitted by pi-ai adapters that do
487
+ * not yet consume the request-scoped Codex capacity.
488
+ */
489
+ export function repairLegacyPiAiContextOverflow(stream, model, contextWindow) {
490
+ return (async function* () {
491
+ let usage;
492
+ for await (const chunk of stream) {
493
+ if (chunk.type === 'usage')
494
+ usage = chunk.usage;
495
+ const usedTokens = usage === undefined ? undefined : contextTokens(usage);
496
+ if (chunk.type === 'finish'
497
+ && chunk.reason.kind === 'error'
498
+ && chunk.reason.failure.code === CONTEXT_WINDOW_EXCEEDED_CODE
499
+ && chunk.reason.failure.message === `pi-ai detected context overflow for model "${model}"`
500
+ && typeof contextWindow === 'number'
501
+ && Number.isSafeInteger(contextWindow)
502
+ && contextWindow > 0
503
+ && usage !== undefined
504
+ && usage.outputTokens > 0
505
+ && usedTokens !== undefined
506
+ && usedTokens <= contextWindow) {
507
+ yield { ...chunk, reason: { kind: 'stop' } };
508
+ continue;
509
+ }
510
+ yield chunk;
511
+ }
512
+ })();
513
+ }
472
514
  function textOf(message) {
473
515
  return message.content
474
516
  .filter(block => block.type === 'text')
@@ -7,6 +7,12 @@ export interface CodexSettings {
7
7
  fast: boolean;
8
8
  /** Optional context capacity override, in tokens. */
9
9
  contextWindow?: number;
10
+ /** Deployment Persona template; supports the host's prompt variables. */
11
+ persona?: string;
12
+ /** DSH Core source-checkout guidance template. */
13
+ harnessSourcePrompt?: string;
14
+ /** DSH Core Web GUI guidance template. */
15
+ webSurfacePrompt?: string;
10
16
  /** Complete plugin-owned Codex operating prompt. */
11
17
  systemPrompt?: string;
12
18
  /** Inject the plugin-owned Codex operating prompt. */
@@ -2,12 +2,15 @@
2
2
  import z from '@deepseek-ai/schemastery';
3
3
  import { settingsNamespace } from '@deepseek-ai/dsh-settings';
4
4
  import { CODEX_CONTEXT_MAX } from "./context.js";
5
- import { DEFAULT_CODEX_SYSTEM_PROMPT } from "./prompt.js";
5
+ import { DEFAULT_CODEX_PERSONA, DEFAULT_CODEX_SYSTEM_PROMPT, DEFAULT_DSH_CORE_SOURCE_PROMPT, DEFAULT_DSH_CORE_WEB_PROMPT, } from "./prompt.js";
6
6
  /** Live Codex controls shared by the Web UI and agent layer. */
7
7
  export const CODEX_SETTINGS_NAMESPACE = settingsNamespace('codex');
8
8
  /** Default settings exposed even before a Codex session is opened. */
9
9
  export const CODEX_SETTINGS_ENTRY = {
10
10
  fast: false,
11
+ persona: DEFAULT_CODEX_PERSONA,
12
+ harnessSourcePrompt: DEFAULT_DSH_CORE_SOURCE_PROMPT,
13
+ webSurfacePrompt: DEFAULT_DSH_CORE_WEB_PROMPT,
11
14
  systemPrompt: DEFAULT_CODEX_SYSTEM_PROMPT,
12
15
  promptEnabled: true,
13
16
  terminalToolsEnabled: true,
@@ -20,6 +23,9 @@ export const CODEX_SETTINGS_ENTRY = {
20
23
  export const CODEX_SETTINGS_SCHEMA = z.object({
21
24
  fast: z.boolean().default(false),
22
25
  contextWindow: z.number().step(1).min(1).max(CODEX_CONTEXT_MAX),
26
+ persona: z.string().default(DEFAULT_CODEX_PERSONA),
27
+ harnessSourcePrompt: z.string().default(DEFAULT_DSH_CORE_SOURCE_PROMPT),
28
+ webSurfacePrompt: z.string().default(DEFAULT_DSH_CORE_WEB_PROMPT),
23
29
  systemPrompt: z.string().default(DEFAULT_CODEX_SYSTEM_PROMPT),
24
30
  promptEnabled: z.boolean().default(true),
25
31
  terminalToolsEnabled: z.boolean().default(true),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuind/dsh-codex-harness",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Codex coding preset with customizable prompts and tools for DSH.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",