agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

fix(core): handle wrapped config session names for dot project paths

Open ChiragArora31 opened this issue 7 months ago • 5 comments

Resolves #1521.

Summary

  • Sanitizes the legacy wrapped storage key component generated for project paths like ..
  • Adds a spawn regression that verifies dot-path projects use the valid V2 runtime/tmux session name (app-1).

Root cause

Legacy wrapped config handling used basename(projectPath) directly, so path: . could produce a storage key suffix of ..

Approach

Resolve the project path relative to the config directory and normalize unsafe storage-key characters. Current V2 runtime naming remains prefix-based and is covered by the new spawn test.

Verification

  • pnpm --filter @aoagents/ao-core test -- project-resolver.test.ts session-manager/spawn.test.ts
  • pnpm --filter @aoagents/ao-core typecheck
  • pnpm --filter @aoagents/ao-core build
  • pnpm --filter @aoagents/ao-core test was also run; it still has existing timeout failures in migration-storage-v2.test.ts and orchestrator-prompt.dist.test.ts.

ChiragArora31 avatar Apr 29 '26 01:04 ChiragArora31

Greptile Summary

This PR hardens session prefix and storage-key generation for project paths that collapse to invalid identifiers (., .., /). It introduces sanitizeIdentifierComponent and deriveSessionPrefixFromProjectPath in paths.ts, threads them through the wrapped-config storage-key builder, applyProjectDefaults, getRegisteredSessionPrefix, and the migration pre-flight checker so all code paths apply the same sanitization logic.

  • sanitizeIdentifierComponent + deriveSessionPrefixFromProjectPath: new primitives in paths.ts that strip filesystem punctuation from any basename/path fragment and produce a stable, tmux-safe prefix even when basenames collapse to the fallback (\"project\"), using a path-fingerprint to disambiguate.
  • generateLegacyWrappedStorageKey (exported): now resolves the project path relative to the config directory before extracting the basename, avoiding the bare-. suffix that was the root cause, and appends a path fingerprint when multiple risky paths would otherwise share the same sanitized component.
  • extractProjectPrefixes in migration: updated to re-derive prefixes from the project path when the stored sessionPrefix is invalid, matching the runtime\u2019s new behavior and fixing the active-session-detection gap called out in a previous review.

Confidence Score: 4/5

The PR is safe to merge and correctly addresses the dot-path session naming bug across all three affected code paths.

All three previously-flagged code paths have been consistently updated. One gap remains in applyProjectDefaults where relative project paths resolve from CWD rather than the config directory, creating a potential inconsistency with the storage-key resolution that correctly anchors to the config directory.

packages/core/src/config.ts — the applyProjectDefaults function resolves relative project paths from CWD rather than the config directory; requires a minor signature change to fully align with the storage-key logic.

Important Files Changed

Filename Overview
packages/core/src/paths.ts Adds sanitizeIdentifierComponent and deriveSessionPrefixFromProjectPath; threads sanitization into generateSessionPrefix. Logic is correct and backward-compatible for normal project names.
packages/core/src/config.ts Exports generateLegacyWrappedStorageKey and switches applyProjectDefaults to deriveSessionPrefixFromProjectPath. Storage key now correctly resolves relative to config dir; session prefix still resolves relative to CWD (see comment).
packages/core/src/global-config.ts Introduces getRegisteredSessionPrefix that re-derives invalid stored prefixes from the project path; used consistently in findSessionPrefixOwner, registerProjectInGlobalConfig, and resolveProjectIdentity.
packages/core/src/migration/storage-v2.ts Updates extractProjectPrefixes to validate stored session prefixes with sanitizeIdentifierComponent before using them, fixing the active-session-detection gap for projects with invalid stored prefixes.
packages/core/tests/config.test.ts Adds three regression tests for dot-path storage key sanitization, fingerprint disambiguation, and collision avoidance across risky paths. Tests cover the fixed code path directly via the exported function.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[project path e.g. '.'] --> B[sanitizeIdentifierComponent]
    B --> C{sanitized == 'project'?}
    C -- No --> D[generateSessionPrefix basename]
    C -- Yes --> E[SHA-256 fingerprint of resolved path]
    E --> F[generateSessionPrefix 'x-fingerprint']
    D --> G[tmux-safe sessionPrefix]
    F --> G

    A --> H[generateLegacyWrappedStorageKey]
    H --> I[resolve relative to configDir]
    I --> J[sanitizeIdentifierComponent basename]
    J --> K{component == 'project'?}
    K -- No --> L[hash-component]
    K -- Yes --> M[path fingerprint]
    M --> N[hash-project-fingerprint]

    P[stored sessionPrefix in global config] --> Q[getRegisteredSessionPrefix]
    Q --> R{sanitizeIdentifierComponent stored == stored?}
    R -- Yes --> S[use stored prefix]
    R -- No --> T[deriveSessionPrefixFromProjectPath entry.path]
    T --> G
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
packages/core/src/config.ts:600-602
When `applyProjectDefaults` calls `deriveSessionPrefixFromProjectPath(project.path)` with a relative path like `"."`, `resolve()` inside that function anchors to the process CWD — not the config file's directory. `generateLegacyWrappedStorageKey` (called earlier in the load pipeline) correctly resolves relative to `configDir`. If the caller's CWD differs from the config directory (e.g. running `ao` from `/home/user/` against a config at `/repos/myapp/agent-orchestrator.yaml`), the two calls resolve `"."` to different directories, producing a storage key fingerprinted on `/repos/myapp/` but a session prefix derived from `/home/user/` — and the prefix silently changes each time the CWD changes.

```suggestion
    if (!project.sessionPrefix) {
      project.sessionPrefix = deriveSessionPrefixFromProjectPath(
        resolve(configDir, project.path),
      );
    }
```


Reviews (3): Last reviewed commit: "fix(core): sanitize migration active-ses..." | Re-trigger Greptile

greptile-apps avatar Apr 29 '26 01:04 greptile-apps

Addressed both review concerns in d1df661a:

  • Reworked the regression to directly exercise the wrapped storage-key fix via generateLegacyWrappedStorageKey.
  • Hardened applyProjectDefaults to sanitize path-derived sessionPrefix before generating the prefix, so dot-path projects cannot produce invalid .-style tmux names.

Validation: pnpm --filter @aoagents/ao-core test -- __tests__/config.test.ts src/__tests__/session-manager/spawn.test.ts (103 passing).

ChiragArora31 avatar May 01 '26 13:05 ChiragArora31

We dont care about backward compatibility, right now

ashish921998 avatar May 01 '26 16:05 ashish921998

Thanks @harshitsinghbhandari — implemented the core-first approach you suggested.

What changed

  • Added sanitizeIdentifierComponent in paths.ts and made generateSessionPrefix sanitize at the start so every caller (including global-config.ts and migration) gets safe prefixes automatically.
  • Added deriveSessionPrefixFromProjectPath for cases where the basename collapses to the generic project token after sanitization, so we don’t get duplicate pro-style prefixes when two projects legitimately use nasty paths like / vs .. in the same YAML.
  • Legacy wrapped generateLegacyWrappedStorageKey now uses the shared sanitizer and appends a short path fingerprint when the sanitized basename is that generic fallback (so / vs .. don’t share the same ${hash}-project dir).
  • Added regression tests for /, fingerprint disambiguation, and prefix collision avoidance; added the requested changeset.

Merged latest main into this branch and re-ran pnpm --filter @aoagents/ao-core test -- src/__tests__/paths.test.ts __tests__/config.test.ts + typecheck.

Happy to tweak naming (sanitizeIdentifierComponent) if you prefer something even more general.

ChiragArora31 avatar May 02 '26 13:05 ChiragArora31

This is great — verified end-to-end:

  • sanitizeIdentifierComponent in paths.ts and generateSessionPrefix sanitizing at entry means every caller (current and future) is safe by construction. Exactly the core-first fix.
  • All three global-config.ts call sites updated, plus the migration/storage-v2.ts site I hadn't flagged. Nice catch.
  • deriveSessionPrefixFromProjectPath + the storage-key fingerprint suffix close the / vs .. collision cleanly.
  • Tests in paths.test.ts and config.test.ts pin the sanitizer behavior, prefix collision avoidance, and the legacy-storage-key fingerprint regex ^[a-f0-9]{12}-project-[a-f0-9]{8}$.
  • Changeset is there.

Non-blocking: getRegisteredSessionPrefix (global-config.ts:593) and resolveProjectIdentity (:807) still trust entry.sessionPrefix verbatim when set, and GlobalProjectEntrySchema.sessionPrefix is just z.string().optional() (no regex). Any existing user who previously ran ao project add with a dot path has sessionPrefix: "." literally stored in their global config, and that stored value keeps flowing through. Since @ashish921998 already waived BC, this is fine to defer — but a one-liner that re-derives the prefix when the stored value fails the identifier regex would close it for free if you feel like adding it. Otherwise, ship it.

harshitsinghbhandari avatar May 02 '26 17:05 harshitsinghbhandari

Closing as part of a backlog sweep: Agent Orchestrator was rewritten from the ground up (Go backend + Electron/React frontend), and this PR's changes target files from the pre-rewrite TypeScript codebase that no longer exist in the current tree, so it cannot be rebased onto main. If the underlying change still makes sense for the current app, please open a fresh PR against the new codebase. Thanks for the contribution!

harshitsinghbhandari avatar Jul 06 '26 17:07 harshitsinghbhandari