Skip to content

[regression] Agent Skills inline hooks can inject another Codex thread's active plan from a shared cwd #212

Closed

Description

@webwww123

Summary

In Codex Desktop using the Agent Skills / skill-only install route (~/.codex/skills/planning-with-files/SKILL.md), the inline lifecycle hooks can inject an unrelated task's active plan when multiple Codex threads share the same top-level cwd.

This is not plan-file content corruption. Two separate plans remain correct, but the shared .planning/.active_plan selector chooses the wrong plan and re-injects it on every prompt/tool call. Because the injected plan arrives as high-priority context, the agent can abandon the user's current task and start working on an unrelated one.

We reproduced this on planning-with-files v3.7.0. I also checked current master/v3.8.2: the inline skills/planning-with-files/SKILL.md hook commands still resolve only by PLAN_ID -> cwd .planning/.active_plan -> newest plan and do not appear to apply the Codex session-attachment guard.

Impact

In the observed incident, the user was discussing a package-switching implementation in a nested project. A different root-level plan described an unrelated bulk production migration. The wrong plan was injected repeatedly, and the agent began investigating the unrelated migration.

Independent production-safety checks prevented writes, but this failure mode is high risk: a stale or unrelated plan can redirect an active coding/operations thread without an explicit user task switch.

Reproduction

  1. Install planning-with-files via the Agent Skills / skill-only route so hooks come from SKILL.md.
  2. Start Codex Desktop with a shared cwd such as /workspace.
  3. Create Plan A under:
    • /workspace/.planning/plan-a/task_plan.md
    • /workspace/.planning/.active_plan contains plan-a
  4. Create an unrelated Plan B in a nested project:
    • /workspace/project/.planning/plan-b/task_plan.md
    • /workspace/project/.planning/.active_plan contains plan-b
  5. In a Codex thread, discuss and work on Plan B. Tool calls may explicitly use workdir=/workspace/project.
  6. Invoke planning-with-files or make tool calls covered by its hooks.

Actual behavior

The hook resolves against the Codex thread's shared cwd (/workspace), selects Plan A, and injects Plan A before prompts/tool calls. The nested project's explicitly active Plan B is ignored.

The injection repeats, so a correction from the user competes with the wrong plan again on the next hook event.

Expected behavior

A plan should be bound to the current Codex thread/session or to an explicit project root. An unrelated .active_plan in a shared parent cwd must never silently win.

If the hook cannot unambiguously identify the current thread's plan, it should fail closed (no injection) and emit a warning rather than auto-selecting the newest/shared plan.

Relation to existing issues

This appears to be a coverage gap: the Agent Skills inline hooks in SKILL.md do not use the session-attachment mechanism that exists in .codex/hooks. PLANNING_DISABLED=1 is not a sufficient workaround here because the affected thread genuinely needs planning-with-files; it just needs the correct plan.

Suggested fixes

  1. Apply the Codex session-attachment guard to the Agent Skills inline hook route.
  2. Bind a thread to an absolute plan root, not only a cwd-relative slug.
  3. Do not fall back to "newest plan" when multiple plans or nested project roots are present.
  4. Detect parent-cwd vs nested-project active-plan conflicts and inject nothing with a clear warning.
  5. Add an integration test with two Codex threads sharing a parent cwd but using different nested project plans.

Environment

  • Codex Desktop / Codex app
  • Linux
  • planning-with-files local version: v3.7.0
  • upstream master/latest release checked: v3.8.2
  • Agent Skills / skill-only install route

Activity

  1. OthmanAdi commented on Aug 1, 2026

    @OthmanAdi
    Owner

    @webwww123 Confirmed on master at b04ffd9 (v3.8.2). Your reading of the resolution chain is correct, and reproducing it turned up two further failures on that install route that are not in your report.

    Reproduction

    Your tree, built verbatim: /workspace/.planning/plan-a holding an unrelated bulk migration plan with .active_plan naming it, /workspace/project/.planning/plan-b holding the real task with its own .active_plan, hooks fired with the thread cwd at /workspace.

    what fired which plan reached the model
    canonical inject-plan.sh, cwd /workspace Plan A (the unrelated one)
    canonical inject-plan.sh, cwd /workspace/project Plan B (correct)
    .codex SKILL.md inline UserPromptSubmit, cwd /workspace Plan A
    .codex SKILL.md inline PreToolUse, cwd /workspace Plan A
    PLANNING_DISABLED=1, canonical silent, nothing injected
    PLANNING_DISABLED=1, .codex SKILL.md inline Plan A, opt-out ignored
    .codex/hooks/user-prompt-submit.sh, unattached session silent, correct
    canonical inject-plan.sh, unattached session Plan A, guard absent
    .codex SKILL.md inline, unattached session Plan A, guard absent
    canonical, PLAN_ID=plan-b set from /workspace Plan A, the pin does not reach the nested plan

    Root cause

    Resolution is cwd relative and has no notion of a thread. scripts/inject-plan.sh:141-161 tries $PLAN_ID against .planning/$PLAN_ID, then .planning/.active_plan, then the newest .planning/*/ by mtime, then a root task_plan.md. Every one of those reads is relative to the process cwd. When the Codex thread's cwd is the shared parent, the parent's pointer is the only pointer the hook can see, and the nested project's .active_plan is never consulted. Nothing in that path is aware that the work is happening one directory down.

    The session attachment guard from #146 exists in exactly two places: .codex/hooks/codex_hook_adapter.py:42-56 with its shell twin at .codex/hooks/user-prompt-submit.sh:16-21, and the Pi extension at plan.ts:247-252. No SKILL.md route references .planning/sessions anywhere, which is the coverage gap you identified.

    Two things beyond your report:

    PLANNING_DISABLED=1 does nothing on that route. The opt-out lives at scripts/inject-plan.sh:26, and only the canonical and .agents SKILL.md files dispatch to that script. The other eleven hook bearing SKILL.md variants, .codex included, still carry the pre v3 hook body inlined in the YAML scalar (.codex/skills/planning-with-files/SKILL.md:10). That body predates the dispatcher refactor, so besides missing the opt-out it also lacks the symlink containment guard and still keys its SHA cache under ${TMPDIR:-/tmp}/pwf-sha, the world writable location that moved to $XDG_CACHE_HOME for security in v3.0.0. So the workaround you correctly judged insufficient is in fact inoperative on the exact route you installed.

    There is currently no way to point the thread at the right plan. PLAN_ID is a cwd relative slug, so from /workspace it can only ever name /workspace/.planning/<id>. Plan B lives under /workspace/project/.planning, and no environment variable in the codebase reaches it.

    What lands next

    1. Session attachment applied to the SKILL.md route, matching the Codex adapter semantics: with no .planning/sessions directory present, behavior is unchanged, so existing single session setups are untouched.
    2. An absolute plan root binding so a thread can be pinned to /workspace/project regardless of where its cwd sits.
    3. Fail closed on ambiguity: when the plan was resolved by the shared pointer or the newest by mtime fallback, and a nested project below cwd carries its own planning root, inject nothing and say why. An explicit pin or an attached session stays authoritative and skips that check.
    4. The eleven stale variants moved onto the shared hardened path, with a test that fails the build if a hook bearing SKILL.md drifts back to an inlined body. That drift is what let this route fall behind in the first place.
    5. A regression test built on the tree above: two threads, one shared parent cwd, distinct nested plans.

    Your suggestion list matched the actual defect closely enough that it shaped the fix order. Reproduction harness is running against the tree from your reproduction section.

  2. OthmanAdi commented on Aug 1, 2026

    @OthmanAdi
    Owner

    @webwww123 Shipped in v3.9.0.

    What changed

    An absolute plan-root binding. PWF_PLAN_ROOT takes a path rather than a slug, which is what your suggestion 2 asked for and what PLAN_ID structurally could not do. From /workspace, PWF_PLAN_ROOT=/workspace/project now resolves that project's plan and nothing else. A pin that does not resolve stops injection and says so, rather than falling back to the plan the caller was escaping.

    Ambiguity fails closed. When the plan was picked by the shared .active_plan pointer, by the newest by mtime fallback, or from a legacy root task_plan.md, and a project directly below the root carries its own live plan, nothing is injected:

    [planning-with-files] Ambiguous plan: this cwd has an active plan and a nested
    project below it has its own (project). Nothing injected. Pin the thread with
    PWF_PLAN_ROOT=<absolute path> or PLAN_ID=<slug>.
    

    Naming the plan explicitly skips the check, so a deliberate parent-level plan still works. A nested pointer that is empty or names a deleted directory does not count as competing, otherwise an abandoned subproject would kill injection at the root forever.

    The eleven stale variants moved onto the shared script. That is where the PLANNING_DISABLED regression came from, and the reason it was inoperative on your route. Those bodies also predated the symlink containment guard and still cached under the world writable /tmp path. A test now fails the build if a hook bearing SKILL.md drifts back to an inlined body.

    The .codex/hooks.json route got the same treatment. Verifying your report surfaced that the two Codex integrations are separate implementations, and OpenAI's hook documentation only describes the hooks.json one. Fixing your route alone would have left the same bug live for anyone using that path, so both now share the semantics.

    One limitation, stated plainly

    Suggestion 1 is delivered in mechanism but is weaker than it sounds. The session attachment guard now runs on the SKILL.md route, matching the Codex adapter. But nothing on that route supplies a per thread PWF_SESSION_ID, so on hosts that do not set it every session is unattached by construction and the guard can only refuse, never bind. It buys parity and a diagnostic, not thread identity. The work that actually fixes your incident is the ambiguity refusal plus the root pin.

    Related: an early build of this made that guard exit silently. On a host that never sets the variable, a .planning/sessions/ directory left behind by earlier Codex use would have killed injection permanently with no symptom, and .planning/ is gitignored so the state is invisible. It now prints one line per turn naming both ways out. Caught by an adversarial pass before release, not in the field.

    Verification

    Your reproduction tree is in the suite, including a two thread case: two session identities against one shared parent cwd with distinct nested plans, asserted together rather than as two separate tests. Suite went from 311 to 411 tests.

    You are in CONTRIBUTORS.md. The fix order followed your suggestion list because it matched the actual defect closely, including the part about not falling back to the newest plan.

  3. added a commit that references this issue on Aug 1, 2026
    0e2b00c
  4. webwww123 commented on Aug 2, 2026

    @webwww123
    Author

    @OthmanAdi Thanks again — I reinstalled v3.9.0 and confirmed that the new ambiguity refusal works on my setup.

    While checking the remaining PWF_SESSION_ID limitation, I found that Codex already provides the required identity on the affected SKILL.md hook route; the current dispatcher simply discards it.

    Codex's documented command-hook contract sends a JSON object on stdin for every hook, including session_id, cwd, and hook_event_name: Hooks / common input fields. The open-source hook runner writes that JSON to the command's stdin, and the hook runtime populates session_id from the Codex session: command_runner.rs, hook_runtime.rs.

    The existing .codex/hooks/codex_hook_adapter.py already calls load_payload() and session_id_from_payload(), but the .codex/skills/planning-with-files/SKILL.md hook scalars invoke inject-plan.sh directly without consuming stdin. So the immediate Codex-specific fix should not require a host change: route the Skill hooks through a small dispatcher that parses stdin and passes session_id as PWF_SESSION_ID (and uses payload cwd rather than process-cwd guessing).

    There is a second limitation beyond making the current .attached guard functional: .planning/sessions/<session_id>.attached is only a boolean. It does not say which plan belongs to the session. Therefore two Codex chats in the exact same repo/cwd can both be "attached" but still follow the same shared .active_plan.

    I think the complete Codex-native shape is:

    1. Store a session binding record containing both an absolute plan_root and plan_id, keyed by hook session_id. Put the index somewhere independent of cwd (for example plugin data when packaged as a Codex plugin, otherwise user state under CODEX_HOME). Hash the session id for the filename and keep the record as validated JSON rather than sourcing shell text.
    2. Resolve in this order: explicit PWF_PLAN_ROOT/PLAN_ID override, session binding, then the existing legacy pointer/newest fallback. A missing, broken, or escaping binding must fail closed.
    3. Have init-session and set-active-plan atomically create/update the binding for the current Codex task. Codex also injects the concrete CODEX_THREAD_ID into shell tool processes even under restrictive environment inheritance (exec_env.rs), so that can be used as an initialization aid; hook-stdin session_id remains the documented lifecycle interface.
    4. Add a regression case with two independent Codex sessions, the same exact cwd and project root, and two plan ids. This is different from the parent-root/nested-project case fixed in v3.9.0.

    I prototyped this locally against the v3.9.0 inject-plan.sh: Session A and Session B used the same cwd and same project root, but their binding records selected Plan A and Plan B respectively; an unbound Session C injected nothing. All three behaved as expected.

    One semantic detail: Codex documents session_id as the identity shared by a root thread and its descendants. That is useful for subagents inheriting the parent plan. If forked chats must be independently re-bindable, the integration can additionally use the concrete thread id or request thread_id in the common hook payload. Independent top-level chats—the failure mode here—already have distinct session ids.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions