diff --git a/packages/session/working-directory/src/index.ts b/packages/session/working-directory/src/index.ts index 10648c929b..f58e4e4690 100644 --- a/packages/session/working-directory/src/index.ts +++ b/packages/session/working-directory/src/index.ts @@ -3,7 +3,7 @@ * @module @deepseek-ai/dsh-working-directory */ -import { isAbsolute, relative, sep } from 'node:path' +import { isAbsolute } from 'node:path' import { Context, Service } from '@deepseek-ai/cordis' import z from '@deepseek-ai/schemastery' import { z as zod } from 'zod' @@ -13,6 +13,7 @@ import { createUserMessage } from '@deepseek-ai/dsh-llm' import type { Session } from '@deepseek-ai/dsh-session' import type { ProjectionHandle } from '@deepseek-ai/dsh-session-projection' import type {} from '@deepseek-ai/dsh-system-prompt' +import { within } from './within.ts' declare module '@deepseek-ai/cordis' { interface Events { @@ -104,18 +105,6 @@ interface WorkspaceRootState { anchors: string[] } -/** - * Whether `path` is `root` or lies below it. Both paths are canonical absolute - * paths of one execution world; the test is lexical and never touches the filesystem. - * @param root - canonical absolute directory. - * @param path - canonical absolute path to test. - * @returns true for `root` itself and every path below it. - */ -export function within(root: string, path: string): boolean { - const rest = relative(root, path) - return rest === '' || (rest !== '..' && !rest.startsWith(`..${sep}`) && !isAbsolute(rest)) -} - /** The longest anchor containing `path`, or undefined when none does. */ function anchorContaining(anchors: readonly string[], path: string): string | undefined { let selected: string | undefined diff --git a/packages/session/working-directory/src/within.ts b/packages/session/working-directory/src/within.ts new file mode 100644 index 0000000000..9e94367d9c --- /dev/null +++ b/packages/session/working-directory/src/within.ts @@ -0,0 +1,17 @@ +/** + * Lexical path containment shared by the working-directory service and its tests. + * @module @deepseek-ai/dsh-working-directory/within + */ +import { isAbsolute, relative, sep } from 'node:path' + +/** + * Whether `path` is `root` or lies below it. Both paths are canonical absolute + * paths of one execution world; the test is lexical and never touches the filesystem. + * @param root - canonical absolute directory. + * @param path - canonical absolute path to test. + * @returns true for `root` itself and every path below it. + */ +export function within(root: string, path: string): boolean { + const rest = relative(root, path) + return rest === '' || (rest !== '..' && !rest.startsWith(`..${sep}`) && !isAbsolute(rest)) +} diff --git a/packages/session/working-directory/tests/workspace-root.spec.ts b/packages/session/working-directory/tests/workspace-root.spec.ts index 9e211fada6..548e58f8a6 100644 --- a/packages/session/working-directory/tests/workspace-root.spec.ts +++ b/packages/session/working-directory/tests/workspace-root.spec.ts @@ -5,10 +5,11 @@ import { dirname, join } from 'node:path' import { Context } from '@deepseek-ai/cordis' import { describe, expect, it } from 'vitest' import LocalFileSystem from '@deepseek-ai/dsh-fs-local' -import WorkingDirectory, { within } from '@deepseek-ai/dsh-working-directory' +import WorkingDirectory from '@deepseek-ai/dsh-working-directory' import { mountAgentLoopTestDependencies, mountAgentLoopTestHarness } from '@deepseek-ai/dsh-agent-loop-testkit' import { SessionId } from '@deepseek-ai/dsh-session' import { renderContextSnapshot } from '@deepseek-ai/dsh-system-prompt' +import { within } from '../src/within.ts' async function fixture() { const scratch = await realpath(await mkdtemp(join(tmpdir(), 'dsh-workspace-root-'))) diff --git a/packages/subagent/subagent/src/manager.ts b/packages/subagent/subagent/src/manager.ts index 2534e68d70..daa6f96fcb 100644 --- a/packages/subagent/subagent/src/manager.ts +++ b/packages/subagent/subagent/src/manager.ts @@ -7,9 +7,9 @@ */ import { randomUUID } from 'node:crypto' -import { isAbsolute, resolve } from 'node:path' +import { isAbsolute, relative, resolve, sep } from 'node:path' import type {} from '@deepseek-ai/dsh-fs' -import { within } from '@deepseek-ai/dsh-working-directory' +import type {} from '@deepseek-ai/dsh-working-directory' import type { Context } from '@deepseek-ai/cordis' import type { Agent, AgentOptions, CreateAgentOptions } from '@deepseek-ai/dsh-agent' import { brandString } from '@deepseek-ai/dsh-brand' @@ -49,6 +49,12 @@ import type { SubagentDelivery } from './control-types.ts' import { ActivationPool, ChildLock, activationAgent, captureLocalResult, observeLocalFailure, requireLocalActivation } from './activation.ts' import type { Activation, ActivationResource, LocalActivation } from './activation.ts' +/** Whether `path` is `root` or lies below it: a lexical test of two canonical absolute paths of one execution world. */ +function within(root: string, path: string): boolean { + const rest = relative(root, path) + return rest === '' || (rest !== '..' && !rest.startsWith(`..${sep}`) && !isAbsolute(rest)) +} + /** Inputs shared by model steering and human prompt delivery. */ type ChildDeliveryOptions = { readonly delivery: SubagentDelivery diff --git a/packages/subagent/subagent/tests/working-directory-fixture.ts b/packages/subagent/subagent/tests/working-directory-fixture.ts index 91536aed44..1032abbb70 100644 --- a/packages/subagent/subagent/tests/working-directory-fixture.ts +++ b/packages/subagent/subagent/tests/working-directory-fixture.ts @@ -1,10 +1,15 @@ import { Context, Service } from '@deepseek-ai/cordis' -import { resolve } from 'node:path' +import { isAbsolute, relative, resolve, sep } from 'node:path' import type { Agent } from '@deepseek-ai/dsh-agent' import type { Session } from '@deepseek-ai/dsh-session' -import { within } from '@deepseek-ai/dsh-working-directory' import type { RelocateOptions, RelocatedDirectories } from '@deepseek-ai/dsh-working-directory' +/** Whether `path` is `root` or lies below it, lexically. */ +function within(root: string, path: string): boolean { + const rest = relative(root, path) + return rest === '' || (rest !== '..' && !rest.startsWith(`..${sep}`) && !isAbsolute(rest)) +} + /** Directory selection for orchestration fixtures whose paths are symbolic and whose logs exclude context providers. */ class FixtureWorkingDirectory extends Service { private readonly directories = new WeakMap()