@kirkchinese/harness-forge 0.4.0 → 0.5.0

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.
Files changed (125) hide show
  1. package/README.md +4 -0
  2. package/docs/DETAIL.zh.md +47 -0
  3. package/lib/agents/presets.d.ts +7 -0
  4. package/lib/agents/presets.js +11 -1
  5. package/lib/agents/profiles.js +3 -2
  6. package/lib/agents/row.js +3 -1
  7. package/lib/client/paths.d.ts +12 -0
  8. package/lib/client/paths.js +68 -0
  9. package/lib/client/typert.d.ts +12 -1
  10. package/lib/client/typert.js +54 -6
  11. package/lib/client.js +1593 -270
  12. package/lib/commands/row.js +1 -1
  13. package/lib/context/instructions.js +5 -5
  14. package/lib/dsh/host.d.ts +3 -0
  15. package/lib/dsh/host.js +3 -0
  16. package/lib/forge/contract.d.ts +42 -2
  17. package/lib/forge/contract.js +3 -1
  18. package/lib/forge/follow.d.ts +30 -0
  19. package/lib/forge/follow.js +73 -0
  20. package/lib/forge/ledger.d.ts +70 -7
  21. package/lib/forge/ledger.js +314 -42
  22. package/lib/forge/projects.d.ts +5 -2
  23. package/lib/forge/projects.js +23 -16
  24. package/lib/forge/remote/archive.d.ts +65 -0
  25. package/lib/forge/remote/archive.js +565 -0
  26. package/lib/forge/remote/code-parts.d.ts +18 -0
  27. package/lib/forge/remote/code-parts.js +76 -0
  28. package/lib/forge/remote/fetch.d.ts +265 -0
  29. package/lib/forge/remote/fetch.js +1133 -0
  30. package/lib/forge/remote/service.d.ts +256 -0
  31. package/lib/forge/remote/service.js +868 -0
  32. package/lib/forge/service.d.ts +19 -9
  33. package/lib/forge/service.js +47 -31
  34. package/lib/forge/settings.d.ts +5 -3
  35. package/lib/forge/settings.js +11 -4
  36. package/lib/forge/state.d.ts +60 -0
  37. package/lib/forge/state.js +174 -58
  38. package/lib/hooks/broker/engine.js +2 -8
  39. package/lib/hooks/broker/session-events.d.ts +5 -0
  40. package/lib/hooks/broker/session-events.js +12 -3
  41. package/lib/hooks/broker/types.d.ts +7 -5
  42. package/lib/hooks/condition.d.ts +78 -2
  43. package/lib/hooks/condition.js +494 -73
  44. package/lib/hooks/config.d.ts +1 -1
  45. package/lib/hooks/config.js +4 -0
  46. package/lib/hooks/glob.d.ts +41 -0
  47. package/lib/hooks/glob.js +114 -0
  48. package/lib/hooks/permissions.d.ts +8 -3
  49. package/lib/hooks/permissions.js +119 -26
  50. package/lib/hooks/powershell-aliases.d.ts +19 -0
  51. package/lib/hooks/powershell-aliases.js +58 -0
  52. package/lib/hooks/process.d.ts +17 -0
  53. package/lib/hooks/process.js +156 -1
  54. package/lib/hooks/row.js +2 -0
  55. package/lib/hooks/runner.d.ts +17 -4
  56. package/lib/hooks/runner.js +42 -13
  57. package/lib/hooks/shell-commands.d.ts +10 -1
  58. package/lib/hooks/shell-commands.js +74 -10
  59. package/lib/hooks/shell.d.ts +3 -2
  60. package/lib/hooks/shell.js +5 -2
  61. package/lib/hooks/vocabulary.js +3 -1
  62. package/lib/mcp/catalog.d.ts +5 -2
  63. package/lib/mcp/catalog.js +18 -10
  64. package/lib/mcp/mount.d.ts +40 -2
  65. package/lib/mcp/mount.js +131 -31
  66. package/lib/mcp/policy.d.ts +42 -0
  67. package/lib/mcp/policy.js +43 -0
  68. package/lib/mcp/remote.js +5 -15
  69. package/lib/mcp/row.js +111 -17
  70. package/lib/mods/loader.js +4 -0
  71. package/lib/mods/service/turns.d.ts +8 -7
  72. package/lib/mods/service/turns.js +10 -35
  73. package/lib/opencode/bun.js +14 -2
  74. package/lib/opencode/row.d.ts +14 -1
  75. package/lib/opencode/row.js +38 -20
  76. package/lib/opencode/runtime/server.js +2 -1
  77. package/lib/porting/adapter-paths.d.ts +13 -0
  78. package/lib/porting/adapter-paths.js +120 -0
  79. package/lib/porting/port-gap.d.ts +10 -4
  80. package/lib/porting/port-gap.js +43 -7
  81. package/lib/porting/prompt.js +9 -7
  82. package/lib/porting/row.js +7 -5
  83. package/lib/porting/verify-port.d.ts +3 -10
  84. package/lib/porting/verify-port.js +12 -34
  85. package/lib/sessions/chat-import.d.ts +48 -0
  86. package/lib/sessions/chat-import.js +44 -0
  87. package/lib/sessions/claude.d.ts +16 -10
  88. package/lib/sessions/claude.js +76 -55
  89. package/lib/sessions/codex-discover.d.ts +34 -0
  90. package/lib/sessions/codex-discover.js +205 -0
  91. package/lib/sessions/codex-meta.d.ts +63 -0
  92. package/lib/sessions/codex-meta.js +148 -0
  93. package/lib/sessions/codex-text.d.ts +9 -0
  94. package/lib/sessions/codex-text.js +44 -0
  95. package/lib/sessions/codex.d.ts +34 -22
  96. package/lib/sessions/codex.js +150 -183
  97. package/lib/sessions/opencode.d.ts +15 -9
  98. package/lib/sessions/opencode.js +77 -58
  99. package/lib/sessions/persist.d.ts +162 -0
  100. package/lib/sessions/persist.js +377 -0
  101. package/lib/sessions/row.d.ts +2 -1
  102. package/lib/sessions/row.js +75 -9
  103. package/lib/sessions/turns.d.ts +31 -0
  104. package/lib/sessions/turns.js +63 -0
  105. package/lib/sessions/zstd.d.ts +15 -0
  106. package/lib/sessions/zstd.js +152 -0
  107. package/lib/shared/render.d.ts +30 -1
  108. package/lib/shared/render.js +813 -48
  109. package/lib/skills/provider.d.ts +2 -0
  110. package/lib/skills/provider.js +8 -2
  111. package/lib/skills/row.js +1 -1
  112. package/lib/workbench/adapter-coverage.d.ts +20 -2
  113. package/lib/workbench/adapter-coverage.js +46 -7
  114. package/lib/workbench/overview.d.ts +77 -27
  115. package/lib/workbench/overview.js +99 -12
  116. package/lib/workbench/remote.d.ts +64 -21
  117. package/lib/workbench/remote.js +181 -42
  118. package/lib/workbench/settings.d.ts +5 -1
  119. package/lib/workbench/settings.js +6 -2
  120. package/lib/workbench/workbench.d.ts +34 -4
  121. package/lib/workbench/workbench.js +147 -46
  122. package/locale/zh.json +2 -1
  123. package/package.json +3 -2
  124. package/lib/forge/marketplace-install.d.ts +0 -3
  125. package/lib/forge/marketplace-install.js +0 -64
@@ -47,7 +47,7 @@ export class HookEngine {
47
47
  constructor(ctx, host) {
48
48
  this.ctx = ctx;
49
49
  this.host = host;
50
- this.runner = new HandlerRunner(ctx, host.shell, agent => host.environment?.(agent?.session.header) ?? {});
50
+ this.runner = new HandlerRunner(ctx, host.shell, agent => host.environment?.(agent?.session.header) ?? {}, call => host.mcpToolPolicy?.(call));
51
51
  this.transcripts = new TranscriptMirror(join(host.dataRoot, 'transcripts'), {
52
52
  agentName: id => host.agentName(id),
53
53
  });
@@ -187,7 +187,7 @@ export class HookEngine {
187
187
  switch (handler.type) {
188
188
  case 'command': return this.runner.command(handler, request);
189
189
  case 'http': return this.runner.http(handler, request);
190
- case 'mcp_tool': return this.runner.mcpTool(handler, request, mcpToolName(handler.server, handler.tool));
190
+ case 'mcp_tool': return this.runner.mcpTool(handler, request, this.host.mcpHookServer?.(source.pluginId, handler.server, agent?.session.header.id));
191
191
  case 'prompt': return this.runner.prompt(handler, request);
192
192
  case 'agent': return this.runner.agent(handler, request);
193
193
  }
@@ -661,12 +661,6 @@ function handlerLabel(handler) {
661
661
  case 'agent': return `${handler.type}: ${handler.prompt.slice(0, 60)}`;
662
662
  }
663
663
  }
664
- /** The DSH tool name of a hook's MCP `server`/`tool` (`plugin:<plugin>:<server>` is the scoped form). */
665
- function mcpToolName(server, tool) {
666
- const scoped = /^plugin:([^:]+):(.+)$/u.exec(server);
667
- const name = scoped !== null ? `plugin_${scoped[1] ?? ''}_${scoped[2] ?? ''}` : server;
668
- return `mcp__${name.replace(/[^A-Za-z0-9_-]+/gu, '_')}__${tool}`;
669
- }
670
664
  /** The first existing directory: the working directory, the session's start directory, home, temp. */
671
665
  function existingDirectory(cwd, start) {
672
666
  for (const candidate of [cwd, start, process.env['HOME'] ?? process.env['USERPROFILE'], process.env['TEMP'] ?? process.env['TMPDIR'] ?? '/tmp']) {
@@ -5,6 +5,11 @@ import type { AgentLike, SessionLike } from './types.js';
5
5
  * changes, approvals, model responses), the idle notification and the
6
6
  * FileChanged watchers, each raised in the background.
7
7
  */
8
+ /**
9
+ * The files a `FileChanged` matcher names (`a.txt|b.md`), resolved against the working directory; a name on a
10
+ * network share the working directory is not on is left out unwatched (see `contentPath`).
11
+ */
12
+ export declare function fileChangedTargets(matcher: string, cwd: string): string[];
8
13
  export declare class SessionEvents {
9
14
  private readonly engine;
10
15
  constructor(engine: HookEngine);
@@ -4,7 +4,8 @@
4
4
  * @module @kirkchinese/harness-forge
5
5
  */
6
6
  import { existsSync, watch } from 'node:fs';
7
- import { basename, dirname, join, resolve } from 'node:path';
7
+ import { basename, dirname, join } from 'node:path';
8
+ import { contentPath } from '@kirkchinese/harness-forge-core';
8
9
  import { specific } from '../output.js';
9
10
  import { foreignTool } from '../vocabulary.js';
10
11
  import { isRecord, lastAssistantText, textOf } from './helpers.js';
@@ -13,6 +14,14 @@ import { isRecord, lastAssistantText, textOf } from './helpers.js';
13
14
  * changes, approvals, model responses), the idle notification and the
14
15
  * FileChanged watchers, each raised in the background.
15
16
  */
17
+ /**
18
+ * The files a `FileChanged` matcher names (`a.txt|b.md`), resolved against the working directory; a name on a
19
+ * network share the working directory is not on is left out unwatched (see `contentPath`).
20
+ */
21
+ export function fileChangedTargets(matcher, cwd) {
22
+ return matcher.split('|').map(item => item.trim()).filter(item => item.length > 0)
23
+ .map(name => contentPath(name, cwd)).filter((path) => path !== undefined);
24
+ }
16
25
  export class SessionEvents {
17
26
  engine;
18
27
  constructor(engine) {
@@ -131,8 +140,8 @@ export class SessionEvents {
131
140
  const files = new Set(state.watchPaths);
132
141
  for (const source of this.engine.sources.values()) {
133
142
  for (const group of source.table.get('FileChanged') ?? []) {
134
- for (const name of (group.matcher ?? '').split('|').map(item => item.trim()).filter(item => item.length > 0))
135
- files.add(resolve(cwd, name));
143
+ for (const file of fileChangedTargets(group.matcher ?? '', cwd))
144
+ files.add(file);
136
145
  }
137
146
  }
138
147
  const byDir = new Map();
@@ -6,8 +6,10 @@
6
6
  */
7
7
  import type { FSWatcher } from 'node:fs';
8
8
  import type { ContentBlock } from '@deepseek-ai/dsh-llm';
9
+ import type { McpToolCall, McpToolVerdict } from '../../forge/contract.js';
9
10
  import type { HookHandler, HookTable } from '../config.js';
10
11
  import type { Decoded } from '../output.js';
12
+ import type { PermissionRuleSet } from '../permissions.js';
11
13
  import type { ModelVerdict } from '../runner.js';
12
14
  import type { ForeignShell } from '../shell.js';
13
15
  import type { HookDialect } from '../vocabulary.js';
@@ -62,11 +64,7 @@ export interface HookBrokerHost {
62
64
  permissionRules?(session?: {
63
65
  readonly id: string;
64
66
  readonly parentSession?: string;
65
- }): {
66
- readonly allow: readonly string[];
67
- readonly deny: readonly string[];
68
- readonly ask: readonly string[];
69
- };
67
+ }): PermissionRuleSet;
70
68
  /** True while the settings in effect say `disableAllHooks: true`. */
71
69
  hooksDisabled?(session?: {
72
70
  readonly id: string;
@@ -83,6 +81,10 @@ export interface HookBrokerHost {
83
81
  check?(exec: unknown, verdict: ToolVerdict): Promise<ToolVerdict>;
84
82
  /** Whether any mod hooks `tool.check` in the call's session. */
85
83
  checks?(exec: unknown): boolean;
84
+ /** The registered server an `mcp_tool` hook of a plugin names (see `McpApi.hookServer`). */
85
+ mcpHookServer?(pluginId: string, server: string, sessionId?: string): string | undefined;
86
+ /** The MCP row's tool policy for a call an `mcp_tool` hook makes (see `McpApi.toolPolicy`). */
87
+ mcpToolPolicy?(call: McpToolCall): McpToolVerdict | undefined;
86
88
  }
87
89
  /** A permission verdict as Claude Code's `tool.check` carries it. */
88
90
  export interface ToolVerdict {
@@ -1,11 +1,87 @@
1
+ import { type LinkReader } from '@kirkchinese/harness-forge-core';
1
2
  import { type ForeignTool } from './vocabulary.js';
3
+ /** How a permission rule is used, which file rules depend on. */
4
+ export interface RuleUse {
5
+ /** Where a `/path` file rule anchors: the directory of the settings source that defines it (default: the working directory). */
6
+ readonly root?: string;
7
+ /** A deny or ask rule: `dir/**` matches at any depth, and a pattern naming a directory everything in it. */
8
+ readonly restricts?: boolean;
9
+ /**
10
+ * Which path a file rule matches: the path as `spelled`, or its `real` path
11
+ * (see `realPathOf`; anchors resolved alike). Unset: a restricting answer
12
+ * (`unknown` true) matches either, an approving one only both.
13
+ */
14
+ readonly path?: 'spelled' | 'real';
15
+ }
16
+ /** Whether a call is a file call that `Read(...)`/`Edit(...)` rules match by path. */
17
+ export declare function isFileCall(tool: ForeignTool): boolean;
2
18
  /**
3
19
  * Whether `rule` selects this tool call.
4
20
  * @param cwd - the session's working directory, for relative file rules.
21
+ * @param unknown - the answer when a wildcard or what it is matched against is
22
+ * too large to match within budget: true (the default) for a hook's `if` (the
23
+ * hook runs) and for deny and ask rules, false for allow rules.
24
+ * @param use - how a permission rule is used, for file rules (see `RuleUse`).
5
25
  */
6
- export declare function conditionMatches(rule: string, tool: ForeignTool, cwd: string): boolean;
26
+ export declare function conditionMatches(rule: string, tool: ForeignTool, cwd: string, unknown?: boolean, use?: RuleUse): boolean;
27
+ /** A file one redirection of a command reads or writes. */
28
+ export interface RedirectFile {
29
+ readonly path: string;
30
+ readonly reads: boolean;
31
+ readonly writes: boolean;
32
+ /**
33
+ * The path is known as written: not expanded at run time, not starting with
34
+ * `~` and without a glob character, which Claude Code always asks about
35
+ * (https://code.claude.com/docs/en/permissions#redirections).
36
+ */
37
+ readonly plain: boolean;
38
+ }
39
+ /**
40
+ * The files the redirections of one simple command of `shell` read or write;
41
+ * undefined when its words cannot be read. Here-documents, here-strings,
42
+ * descriptor copies and closes (`2>&1`, `<&3`, `>&-`) and the shell's null
43
+ * device (see `nullDevice`) name no file.
44
+ */
45
+ export declare function redirectFiles(command: string, shell: Shell): RedirectFile[] | undefined;
46
+ /** A shell whose words a command or rule is read as. */
47
+ export type Shell = 'bash' | 'powershell';
48
+ /** The shell that reads a shell call's command: PowerShell for DSH's `pwsh` tool, bash otherwise. */
49
+ export declare function shellOf(tool: ForeignTool): Shell;
7
50
  /**
8
51
  * Whether a gitignore-style pattern, relative to the working directory,
9
- * matches a path (skill `paths`, `Read(...)`/`Edit(...)` rules).
52
+ * matches a path as spelled (skill `paths`): no link is followed.
10
53
  */
11
54
  export declare function pathPatternMatches(pattern: string, path: string, cwd: string): boolean;
55
+ /** The filesystem calls the resolver makes, and the platform whose paths it reads: the system's, or a test's. */
56
+ export interface RealPathSystem extends LinkReader {
57
+ readonly platform: NodeJS.Platform;
58
+ realpath(path: string): string;
59
+ }
60
+ /**
61
+ * The real path of an absolute path, or undefined when it cannot be known (a
62
+ * link loop, more than {@link SYMLINK_HOPS} links, a link or folder that
63
+ * cannot be read, or, on Windows, a path or link that leads to a network share
64
+ * or device `base` is not on). What exists is resolved by the system; a part
65
+ * that does not exist yet is followed link by link (`readlink`), so a dangling
66
+ * link leads where a write through it would create its file. The one resolver
67
+ * file rules and redirection targets use; nothing else follows links.
68
+ *
69
+ * On Windows, opening, resolving or reading a link on `\\server\share` makes
70
+ * the system connect to that server and sign in with the person's credentials,
71
+ * and `realpath` follows links by itself. So before any call that could follow
72
+ * one, `linksReachNewShare` walks the path with `lstat` and `readlink` only;
73
+ * realpath runs only for a path that stays local (it is what expands a short
74
+ * name such as `RUNNER~1`).
75
+ * @param base - the working directory: a share it is on is not new.
76
+ */
77
+ export declare function realPathOf(path: string, base: string, system?: RealPathSystem): string | undefined;
78
+ /**
79
+ * A Windows path in the form rules match, as Claude Code writes them
80
+ * (https://code.claude.com/docs/en/permissions#where-a-pattern-anchors: "`C:\Users\alice` becomes `/c/Users/alice`"):
81
+ * either separator becomes `/`, the long-path prefix goes, a drive `C:` becomes `/c` and a share `\\server` stays
82
+ * `//server`. The whole path is compared without case, as Windows' file systems compare names by default, so the
83
+ * rule, the working directory and every path, spelled or real, go through this one function.
84
+ */
85
+ export declare function windowsRulePath(path: string): string;
86
+ /** A rule-form Windows path (`/c/users/x`, `//server/share/x`) as Windows names it (`c:\users\x`). */
87
+ export declare function windowsNativePath(path: string): string;