feat(profiler/behave_shell): emit environmental.shell_type

Per-prompt classification mode over ctx.prompt_lines. $/# → bash;
% → zsh; > with 'PS ' prefix → powershell; > with 'C:\' substring →
cmd.exe; > otherwise → fish. New _features/environmental.py module
opens Phase F.
This commit is contained in:
2026-05-04 00:30:24 -04:00
parent 1ff02f0c77
commit 07ff5ff0c9
4 changed files with 171 additions and 0 deletions

View File

@@ -24,6 +24,9 @@ from decnet.profiler.behave_shell._features.cognitive import (
inter_command_consistency,
inter_command_latency_class,
)
from decnet.profiler.behave_shell._features.environmental import (
shell_type,
)
from decnet.profiler.behave_shell._features.temporal import (
escalation_pattern,
landing_ritual,
@@ -67,4 +70,5 @@ FEATURES: tuple[FeatureFn, ...] = (
session_duration,
escalation_pattern,
landing_ritual,
shell_type,
)

View File

@@ -0,0 +1,75 @@
"""``environmental.*`` feature functions.
Phase F ships the five environmental primitives plus F.0's shared
prompt-line detector. F.0 itself emits no primitive — it populates
``SessionContext.prompt_lines`` and ``Command.followed_by_prompt``
which F.1 / F.3 / E.4 read.
Step F.1: ``environmental.shell_type``.
"""
from __future__ import annotations
import collections
from typing import Iterator
from decnet_behave_core.spec.envelope import Observation
from decnet.profiler.behave_shell._ctx import SessionContext
from decnet.profiler.behave_shell._features._emit import make_observation
from decnet.profiler.behave_shell._parse import PromptLine
from decnet.profiler.behave_shell._thresholds import (
SHELL_TYPE_MIN_PROMPTS,
)
def _classify_shell_from_prompt(p: PromptLine) -> str:
"""Map one prompt line to a shell-type label."""
suffix = p.suffix_char
line = p.raw_line
if suffix in ("$", "#"):
# bash / sh / dash all share these — collapsed to "bash" per
# registry's bash-family stance. zsh CAN be configured to use
# $/# but that's the user's PS1 override; default zsh is %.
return "bash"
if suffix == "%":
return "zsh"
if suffix == ">":
# Disambiguate by line content. powershell's PS1 starts with
# "PS "; cmd.exe's prompt typically contains a Windows path
# like "C:\". Everything else is fish.
if line.lstrip().startswith("PS "):
return "powershell"
if "C:\\" in line or "c:\\" in line:
return "cmd.exe"
return "fish"
return "bash" # defensive — _detect_prompt_suffix only emits one of $#%>
def shell_type(ctx: SessionContext) -> Iterator[Observation]:
"""Emit ``environmental.shell_type``.
Mode of per-prompt-line classification across
``ctx.prompt_lines``. Skip emission when no prompts detected —
the registry's enum doesn't admit ``unknown`` and emitting
``bash`` from no observation at all would be dishonest.
Confidence drops below ``SHELL_TYPE_MIN_PROMPTS`` (3 prompts);
above that threshold the vote is solid.
"""
if not ctx.prompt_lines:
return
votes = collections.Counter(
_classify_shell_from_prompt(p) for p in ctx.prompt_lines
)
value, _ = votes.most_common(1)[0]
if len(ctx.prompt_lines) < SHELL_TYPE_MIN_PROMPTS:
confidence = 0.40
else:
confidence = 0.75
yield make_observation(
ctx,
primitive="environmental.shell_type",
value=value,
confidence=confidence,
)

View File

@@ -228,6 +228,11 @@ LANDING_RITUAL_MIN_COMMANDS: int = 3
PROMPT_SUFFIX_CHARS: frozenset[str] = frozenset({"$", "#", "%", ">"})
PROMPT_LINE_MAX_CHARS: int = 256
# ── environmental.shell_type (Step F.1) ────────────────────────────────────
# Below this many detected prompt-lines, drop confidence (sample-size
# honesty). Above, the shell-type vote is robust.
SHELL_TYPE_MIN_PROMPTS: int = 3
# ── motor.keystroke_cadence (Step B.1) ──────────────────────────────────────
# Typing bursts split at gaps > IKI_THINK_MAX_S so think-pauses between
# commands don't inflate the within-burst CV. Mirrors the prototype's