mirror of
https://github.com/okieselbach/AutopilotMonitor.git
synced 2026-10-25 13:10:13 +00:00
Compare commits
97 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7fb78a7e29 | |||
| bfd8d58142 | |||
| 8ad472d1bc | |||
| fc5e2cd751 | |||
| 9540791441 | |||
| 058da1627f | |||
| cb9b3fe404 | |||
| 12315b2f2d | |||
| 0a4a051866 | |||
| f47d961947 | |||
| c701d0d694 | |||
| d4a3edf849 | |||
| e164eac1eb | |||
| c81591b498 | |||
| d8c03d9d95 | |||
| ffb133f0b9 | |||
| b3fe321f0d | |||
| 69d210c51d | |||
| 06a88e9897 | |||
| aee30f68e8 | |||
| 3db9a884de | |||
| 0037b8cb12 | |||
| 205b13dbb9 | |||
| b938a03367 | |||
| d474c2be9f | |||
| 96390beeb9 | |||
| fe63a0a352 | |||
| 2e36e9c234 | |||
| b83aa6d97d | |||
| f42fcbcb3c | |||
| f70ba354f8 | |||
| fc17047290 | |||
| 7bbc2cbddf | |||
| 9a78daa880 | |||
| 5d532d3dbc | |||
| a22c26eb9d | |||
| b84c467255 | |||
| 53124e94a6 | |||
| 838068fd31 | |||
| 71282f3556 | |||
| 7080dcc45a | |||
| 5a62714dc2 | |||
| c930d18f1e | |||
| 583c81c9a8 | |||
| 483ec8b8e0 | |||
| 328446864a | |||
| 106480ef52 | |||
| d3a45f958d | |||
| 6657c6b9c0 | |||
| 6c5e2fd921 | |||
| 65842b87bf | |||
| b323c52364 | |||
| 321a6efc81 | |||
| 5ecb2a998b | |||
| bb6df93b51 | |||
| 096a049deb | |||
| b9caf524c3 | |||
| 197de1d718 | |||
| 18d4f08f2b | |||
| ddf450d87d | |||
| e3b2113365 | |||
| 6a85f45604 | |||
| a2d1aedcad | |||
| e955faa97e | |||
| 2b755bf99a | |||
| ce1162a055 | |||
| 447786b447 | |||
| 2b4dbe087a | |||
| 6804bcfd4e | |||
| d2221eb0b9 | |||
| 9ac6de7c7d | |||
| f5613be495 | |||
| 819b6c8e4f | |||
| 9c1fb07f33 | |||
| fa31872903 | |||
| b08b79eafe | |||
| e43d916c24 | |||
| 14b185db35 | |||
| baa51102fd | |||
| 5577f5e7e5 | |||
| 61892592e4 | |||
| 1e2ca46946 | |||
| 8224c8f1a8 | |||
| d7f39895f5 | |||
| 790bd35385 | |||
| 34c9fd42fe | |||
| 19a336870c | |||
| 5ba5942d0b | |||
| aa20d9102c | |||
| 097be1700b | |||
| f1457d823d | |||
| 12e66299e6 | |||
| e9a01de9da | |||
| 5f793ea01a | |||
| e86f7efed3 | |||
| 78bfad0ec6 | |||
| f5efadfd07 |
+1
-1
Submodule .claude/commands updated: d004f83c8a...315ad0e736
@@ -55,6 +55,10 @@ updates:
|
||||
- dependency-name: '*'
|
||||
update-types: ['version-update:semver-major']
|
||||
|
||||
# Every action is referenced by commit SHA with the version in a trailing comment;
|
||||
# action-pins.yml fails any workflow change that reintroduces a mutable tag. Dependabot
|
||||
# is what keeps those pins current: it rewrites the SHA and the comment together, so the
|
||||
# pins are reviewed in a PR instead of drifting silently or being bumped by hand.
|
||||
- package-ecosystem: github-actions
|
||||
directory: /
|
||||
schedule:
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
name: Action Pin Gate
|
||||
|
||||
# Every third-party action must be referenced by a full commit SHA, never by a tag.
|
||||
# Tags are mutable: whoever controls an action repository can repoint v4 at new code,
|
||||
# and that code runs inside jobs that hold the code-signing identity, the storage
|
||||
# credentials and a token that can push to main. A SHA is the only reference an
|
||||
# upstream owner cannot change under us.
|
||||
#
|
||||
# Lives in its own workflow rather than in ci.yml: CI deliberately ignores changes to
|
||||
# the other workflow files, so a guard placed there would stay silent for exactly the
|
||||
# commits it needs to inspect.
|
||||
#
|
||||
# Like CI, this reports rather than blocks: main is deliberately unprotected, so a red
|
||||
# run is a signal to act on, not a merge barrier. Making it binding means adding
|
||||
# 'Verify action pins' as a required status check -- a branch-protection decision, not
|
||||
# something this file can assert.
|
||||
#
|
||||
# The pins are not maintained by hand — .github/dependabot.yml opens a monthly grouped
|
||||
# PR that rewrites both the SHA and the trailing version comment. The comment is
|
||||
# therefore required too: it is what makes a pinned line reviewable.
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
paths: ['.github/workflows/**']
|
||||
pull_request:
|
||||
branches: [main]
|
||||
paths: ['.github/workflows/**']
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
verify-pins:
|
||||
name: Verify action pins
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- name: Every third-party action is pinned to a SHA
|
||||
run: |
|
||||
set -uo pipefail
|
||||
failed=0
|
||||
while IFS=: read -r file line ref; do
|
||||
# Reusable workflows from this repository move with the commit that calls
|
||||
# them, so there is nothing to pin.
|
||||
case "$ref" in ./*) continue ;; esac
|
||||
|
||||
if [[ "$ref" =~ ^[^@]+@[0-9a-f]{40}[[:space:]]+#[[:space:]]*[^[:space:]] ]]; then
|
||||
printf 'ok %s:%s %s\n' "$file" "$line" "$ref"
|
||||
continue
|
||||
fi
|
||||
|
||||
if [[ "$ref" =~ ^[^@]+@[0-9a-f]{40}[[:space:]]*$ ]]; then
|
||||
echo "::error file=$file,line=$line::Pinned but the version comment is missing: '$ref'. Write 'owner/action@<sha> # vN' so the line stays reviewable."
|
||||
else
|
||||
echo "::error file=$file,line=$line::Action '$ref' is not pinned to a commit SHA. Tags are mutable -- resolve it with: gh api repos/<owner>/<action>/git/ref/tags/<tag>"
|
||||
fi
|
||||
failed=1
|
||||
# -H is load-bearing: with a single matching file grep omits the name, the
|
||||
# fields shift by one and every diagnostic points at nothing.
|
||||
done < <(grep -nHE '^[[:space:]]*(-[[:space:]]+)?uses:' .github/workflows/*.yml \
|
||||
| sed -E 's/^([^:]+):([0-9]+):[[:space:]]*(-[[:space:]]+)?uses:[[:space:]]*/\1:\2:/')
|
||||
|
||||
if [ "$failed" -ne 0 ]; then
|
||||
echo "::error::Unpinned action references found -- fix them before this reaches main."
|
||||
exit 1
|
||||
fi
|
||||
echo "All action references are pinned to commit SHAs."
|
||||
@@ -18,7 +18,7 @@ jobs:
|
||||
name: Bootstrap guard tests (PS 5.1)
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- name: Install Pester 5+
|
||||
shell: powershell
|
||||
|
||||
@@ -72,7 +72,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v7
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
# Every released build is signed; a missing variable stops the run before a build
|
||||
# number is burnt.
|
||||
@@ -122,7 +122,7 @@ jobs:
|
||||
|
||||
# SDK 10 = single build toolchain (parity with local build.ps1 / global.json).
|
||||
- name: Setup .NET
|
||||
uses: actions/setup-dotnet@v6
|
||||
uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6
|
||||
with:
|
||||
dotnet-version: '10.0.x'
|
||||
|
||||
@@ -462,7 +462,7 @@ jobs:
|
||||
# Skipped on runs that publish nothing -- no log entry for bytes nobody receives.
|
||||
- name: Attest build provenance
|
||||
if: inputs.upload_blob || inputs.create_release
|
||||
uses: actions/attest-build-provenance@v4
|
||||
uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4
|
||||
with:
|
||||
subject-path: |
|
||||
${{ steps.artifact.outputs.versioned_zip_release }}
|
||||
@@ -470,13 +470,13 @@ jobs:
|
||||
staging/agent/AutopilotMonitor.SummaryDialog.exe
|
||||
|
||||
- name: Upload build artifact
|
||||
uses: actions/upload-artifact@v7
|
||||
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
|
||||
with:
|
||||
name: AutopilotMonitor-Agent-v${{ steps.version.outputs.version }}
|
||||
path: ${{ steps.artifact.outputs.versioned_zip_release }}
|
||||
|
||||
- name: Upload version metadata
|
||||
uses: actions/upload-artifact@v7
|
||||
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
|
||||
with:
|
||||
name: version-metadata
|
||||
path: ${{ env.MANIFEST_NAME }}
|
||||
|
||||
@@ -40,7 +40,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v7
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
# The loader executes as SYSTEM on every enrolling device — a parse error or
|
||||
# non-ASCII mojibake (PS 5.1 codepage roulette) must fail the build, not the fleet.
|
||||
@@ -111,8 +111,11 @@ jobs:
|
||||
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
|
||||
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
|
||||
|
||||
# Exact version, not a "5.*" range: wix writes the bytes of the MSI that customers
|
||||
# install, so a compromised or simply broken point release must not flow into a build
|
||||
# unnoticed. Bumping is a reviewed commit.
|
||||
- name: Install WiX toolset
|
||||
run: dotnet tool install --global wix --version "5.*"
|
||||
run: dotnet tool install --global wix --version 5.0.2
|
||||
|
||||
# Version pinned; roll-forward because the tool targets an older runtime than the SDK.
|
||||
- name: Install AzureSignTool
|
||||
@@ -235,12 +238,12 @@ jobs:
|
||||
# gh attestation verify AutopilotMonitor-Bootstrap.msi --repo okieselbach/AutopilotMonitor
|
||||
- name: Attest build provenance (MSI)
|
||||
if: inputs.upload_blob
|
||||
uses: actions/attest-build-provenance@v4
|
||||
uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4
|
||||
with:
|
||||
subject-path: ${{ env.STABLE_MSI }}
|
||||
|
||||
- name: Upload build artifact
|
||||
uses: actions/upload-artifact@v7
|
||||
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
|
||||
with:
|
||||
name: AutopilotMonitor-Bootstrap-v${{ steps.version.outputs.version }}
|
||||
path: ${{ env.STABLE_MSI }}
|
||||
|
||||
+13
-11
@@ -35,6 +35,7 @@ on:
|
||||
# Every other workflow: nothing here builds or tests anything CI covers. paths-ignore
|
||||
# cannot negate, so they are listed one by one; ci.yml itself stays out of the list
|
||||
# so a change to CI runs CI. A new workflow file is opt-in — add it here too.
|
||||
- '.github/workflows/action-pins.yml'
|
||||
- '.github/workflows/bootstrap-script-gates.yml'
|
||||
- '.github/workflows/build-agent.yml'
|
||||
- '.github/workflows/build-msi.yml'
|
||||
@@ -67,6 +68,7 @@ on:
|
||||
# Every other workflow: nothing here builds or tests anything CI covers. paths-ignore
|
||||
# cannot negate, so they are listed one by one; ci.yml itself stays out of the list
|
||||
# so a change to CI runs CI. A new workflow file is opt-in — add it here too.
|
||||
- '.github/workflows/action-pins.yml'
|
||||
- '.github/workflows/bootstrap-script-gates.yml'
|
||||
- '.github/workflows/build-agent.yml'
|
||||
- '.github/workflows/build-msi.yml'
|
||||
@@ -94,9 +96,9 @@ jobs:
|
||||
# windows-latest is required: the agent test projects target net48.
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- uses: actions/setup-dotnet@v6
|
||||
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6
|
||||
with:
|
||||
dotnet-version: '10.0.x'
|
||||
|
||||
@@ -125,9 +127,9 @@ jobs:
|
||||
run:
|
||||
working-directory: src/Web/autopilot-monitor-web
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- uses: actions/setup-node@v7
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
||||
with:
|
||||
node-version: '22'
|
||||
cache: npm
|
||||
@@ -159,9 +161,9 @@ jobs:
|
||||
run:
|
||||
working-directory: src/McpServer/autopilot-monitor-mcp
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- uses: actions/setup-node@v7
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
||||
with:
|
||||
node-version: '22'
|
||||
cache: npm
|
||||
@@ -179,7 +181,7 @@ jobs:
|
||||
# (run 31252869254). Persist the model across runs instead; HF_CACHE_DIR
|
||||
# is honored by vector-search-provider.ts.
|
||||
- name: Cache embedding model
|
||||
uses: actions/cache@v6
|
||||
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6
|
||||
with:
|
||||
path: ${{ github.workspace }}/.hf-cache
|
||||
key: hf-model-Xenova-all-MiniLM-L6-v2
|
||||
@@ -193,9 +195,9 @@ jobs:
|
||||
name: Generated guardrails match rules/guardrails.json
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- uses: actions/setup-node@v7
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
||||
with:
|
||||
node-version: '22'
|
||||
|
||||
@@ -214,9 +216,9 @@ jobs:
|
||||
name: Generated wire types match shared-manifests.json
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- uses: actions/setup-node@v7
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
||||
with:
|
||||
node-version: '22'
|
||||
|
||||
|
||||
@@ -102,15 +102,15 @@ jobs:
|
||||
build-mode: none
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- uses: actions/setup-dotnet@v6
|
||||
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6
|
||||
if: matrix.language == 'csharp'
|
||||
with:
|
||||
dotnet-version: '10.0.x'
|
||||
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v4.37.9
|
||||
uses: github/codeql-action/init@cdf488f595d80d6e07e03d4674febd5ab45fa938 # v4.37.9
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
build-mode: ${{ matrix.build-mode }}
|
||||
@@ -121,6 +121,6 @@ jobs:
|
||||
run: dotnet build AutopilotMonitor.sln --configuration Release
|
||||
|
||||
- name: Analyze
|
||||
uses: github/codeql-action/analyze@v4.37.9
|
||||
uses: github/codeql-action/analyze@cdf488f595d80d6e07e03d4674febd5ab45fa938 # v4.37.9
|
||||
with:
|
||||
category: /language:${{ matrix.language }}
|
||||
|
||||
@@ -24,14 +24,17 @@ jobs:
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- uses: actions/setup-node@v7
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
||||
with:
|
||||
node-version: '22'
|
||||
|
||||
# Exact versions and no lifecycle scripts: this job holds contents:write and pushes to
|
||||
# main, so an install-time script from a hijacked package would run with a token that
|
||||
# can rewrite the repository. ajv is pure JS and needs no install hooks.
|
||||
- name: Install ajv-cli
|
||||
run: npm install -g ajv-cli ajv-formats
|
||||
run: npm install -g --ignore-scripts ajv-cli@5.0.0 ajv-formats@3.0.1
|
||||
|
||||
- name: Validate Gather Rules
|
||||
run: |
|
||||
|
||||
@@ -19,7 +19,7 @@ jobs:
|
||||
name: Build and Deploy Backend
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- name: Login to Azure (OIDC)
|
||||
uses: azure/login@7ddb5af1ef8758cf1353cf3b42f940aee27ba21c # v3
|
||||
@@ -29,7 +29,7 @@ jobs:
|
||||
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
|
||||
|
||||
- name: Setup .NET 10
|
||||
uses: actions/setup-dotnet@v6
|
||||
uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6
|
||||
with:
|
||||
dotnet-version: '10.0.x'
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ jobs:
|
||||
name: Build and Deploy MCP Server
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
# The published customer documentation lives in its own PUBLIC repo and is
|
||||
# baked into the image as the search_docs corpus (see the Dockerfile). No
|
||||
@@ -36,7 +36,7 @@ jobs:
|
||||
# re-run actually rolls a new revision). /health reports the SHA captured
|
||||
# below so that staleness is checkable.
|
||||
- name: Check out documentation bundle
|
||||
uses: actions/checkout@v7
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
with:
|
||||
repository: autopilotmonitor/autopilotmonitor-docs
|
||||
path: mcp-docs
|
||||
|
||||
@@ -35,7 +35,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
name: Build and Deploy Job
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
with:
|
||||
lfs: false
|
||||
fetch-depth: 0 # Full history needed for git-based sitemap lastmod dates
|
||||
@@ -56,7 +56,7 @@ jobs:
|
||||
# workflow runs again (push under src/Web/** or a manual dispatch); versions/web.json
|
||||
# records which docs commit is live.
|
||||
- name: Check out documentation bundle
|
||||
uses: actions/checkout@v7
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
with:
|
||||
repository: autopilotmonitor/autopilotmonitor-docs
|
||||
path: whats-new-docs
|
||||
@@ -66,7 +66,7 @@ jobs:
|
||||
id: docs
|
||||
run: echo "sha=$(git -C whats-new-docs rev-parse HEAD)" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- uses: actions/setup-node@v7
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
||||
with:
|
||||
node-version: '22'
|
||||
cache: npm
|
||||
@@ -114,12 +114,12 @@ jobs:
|
||||
# commit, workflow file) in a public transparency log.
|
||||
# Verify: gh attestation verify portal-<sha>.tar.gz --repo okieselbach/AutopilotMonitor
|
||||
- name: Attest build provenance (portal export)
|
||||
uses: actions/attest-build-provenance@v4
|
||||
uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4
|
||||
with:
|
||||
subject-path: ${{ steps.package.outputs.archive }}
|
||||
|
||||
- name: Upload build artifact
|
||||
uses: actions/upload-artifact@v7
|
||||
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
|
||||
with:
|
||||
name: portal-${{ github.sha }}
|
||||
path: ${{ steps.package.outputs.archive }}
|
||||
|
||||
@@ -54,7 +54,7 @@ jobs:
|
||||
needs: gates
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
|
||||
- name: Check code-signing configuration
|
||||
shell: pwsh
|
||||
@@ -127,7 +127,7 @@ jobs:
|
||||
# when someone wants to look at the file.
|
||||
- name: Upload signed scripts
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v7
|
||||
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
|
||||
with:
|
||||
name: signed-scripts
|
||||
path: ${{ runner.temp }}/signed-scripts
|
||||
|
||||
+1
-1
Submodule infra updated: 55a8eaf929...8a8f8c66dd
+1
-1
Submodule internal updated: 8204fc68af...1e6b548904
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"$schema": "../schema/analyze-rule.schema.json",
|
||||
"ruleId": "ANALYZE-ESP-004",
|
||||
"title": "ESP Timeout with 'Continue Anyway' (Soft Failure)",
|
||||
"description": "Detects an ESP terminal failure on a profile that allows 'Continue anyway'. The enrollment is reported as failed, but the user most likely dismissed the error screen and reached the desktop — a soft failure rather than an unusable device.",
|
||||
"title": "ESP Gave Up on a Blocking App with 'Continue Anyway' (Soft Failure)",
|
||||
"description": "Detects an ESP terminal failure where the ESP gave up while a blocking app was still installing (the agent's own esp_apps_timeout verdict — no per-app HRESULT was reported) on a profile that allows 'Continue anyway'. The enrollment is reported as failed, but the user most likely dismissed the error screen and reached the desktop — a soft failure rather than an unusable device. An ESP failure that carries a specific error code (a failed installer, a detection mismatch) is a different case and stays with the rule that explains that code.",
|
||||
"severity": "warning",
|
||||
"category": "esp",
|
||||
"version": "1.0.1",
|
||||
"version": "1.1.0",
|
||||
"enabled": true,
|
||||
"baseConfidence": 80,
|
||||
"conditions": [
|
||||
@@ -26,6 +26,27 @@
|
||||
"value": "true",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"signal": "esp_gave_up_on_app",
|
||||
"source": "event_type",
|
||||
"eventType": "app_install_failed",
|
||||
"dataField": "failureType",
|
||||
"operator": "equals",
|
||||
"value": "esp_apps_timeout",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"signal": "appName",
|
||||
"source": "event_data",
|
||||
"eventType": "app_install_failed",
|
||||
"dataField": "appName",
|
||||
"operator": "exists",
|
||||
"value": "",
|
||||
"filterField": "failureType",
|
||||
"filterOperator": "equals",
|
||||
"filterValue": "esp_apps_timeout",
|
||||
"required": false
|
||||
},
|
||||
{
|
||||
"signal": "espSyncFailureTimeoutMinutes",
|
||||
"source": "event_type",
|
||||
@@ -38,34 +59,25 @@
|
||||
{
|
||||
"signal": "failedSubcategory",
|
||||
"source": "event_type",
|
||||
"eventType": "enrollment_failed",
|
||||
"eventType": "esp_failure_settle_started",
|
||||
"dataField": "failedSubcategory",
|
||||
"operator": "exists",
|
||||
"value": "",
|
||||
"required": false
|
||||
},
|
||||
{
|
||||
"signal": "appName",
|
||||
"source": "event_type",
|
||||
"eventType": "app_install_failed",
|
||||
"dataField": "appName",
|
||||
"operator": "exists",
|
||||
"value": "",
|
||||
"required": false
|
||||
}
|
||||
],
|
||||
"confidenceFactors": [
|
||||
{ "signal": "appName", "condition": "exists", "weight": 10 }
|
||||
{ "signal": "failedSubcategory", "condition": "exists", "weight": 10 }
|
||||
],
|
||||
"explanation": "This enrollment was reported as **failed** because the Enrollment Status Page reached its configured terminal timeout (`{{espSyncFailureTimeoutMinutes}} min`) before all blocking items finished. The ESP profile, however, allows **'Continue anyway'** — so the user most likely dismissed the error screen and reached the desktop with a working, though not fully provisioned, device.\n\nThe agent only observes the ESP terminal failure on the device side; it cannot see what the user did after the failure screen. Treat this as a **soft failure**: the device may well be in productive use, while at least one blocking item (`{{failedSubcategory}}` subcategory / app `{{appName}}`) did not complete inside the ESP window.\n\nA cluster of these across a tenant almost always points at one slow blocking app holding the whole fleet at the ESP timeout wall, not at broken devices. Any field still shown as a literal `{{…}}` placeholder was simply not recorded for this session (e.g. a certificate-only failure has no failed app).",
|
||||
"explanation": "This enrollment was reported as **failed** because the Enrollment Status Page gave up while the blocking app **{{appName}}** was still installing — the agent recorded no per-app error code, only that the app never finished inside the ESP window (the profile's configured limit is `{{espSyncFailureTimeoutMinutes}} min`; the ESP may have stopped earlier than that). The ESP profile, however, allows **'Continue anyway'** — so the user most likely dismissed the error screen and reached the desktop with a working, though not fully provisioned, device.\n\nThe agent only observes the ESP terminal failure on the device side; it cannot see what the user did after the failure screen. Treat this as a **soft failure**: the device may well be in productive use, while the `{{failedSubcategory}}` subcategory did not complete inside the ESP window.\n\nA cluster of these across a tenant almost always points at one slow blocking app holding the whole fleet at the ESP limit, not at broken devices. This rule deliberately stays silent when the failed app reported a specific error code — a failed installer or a detection mismatch is explained by the rule for that code, and 'Continue anyway' does not make it a timing problem. Any field still shown as a literal `{{…}}` placeholder was simply not recorded for this session.",
|
||||
"remediation": [
|
||||
{
|
||||
"title": "Relieve ESP pressure so enrollments finish inside the window",
|
||||
"steps": [
|
||||
"Identify the slow blocking app (`{{appName}}`) and either remove it from the ESP 'block device use until these required apps are installed' list, or raise the ESP installation timeout (currently {{espSyncFailureTimeoutMinutes}} min).",
|
||||
"If the app legitimately needs longer than the ESP allows, make it non-blocking and let it install after the desktop arrives.",
|
||||
"Verify the app's detection rule matches after install — an app that installs but fails detection times out the ESP on every device (see ANALYZE-APP-013).",
|
||||
"If a subcategory such as Certificates is reported under '{{failedSubcategory}}', confirm the certificate/connector actually delivers — 'Continue anyway' hides a missing certificate, it does not fix it."
|
||||
"Look at how long `{{appName}}` takes on a device that completes: if it legitimately needs longer than the ESP allows, make it non-blocking (remove it from 'block device use until these required apps are installed') and let it install after the desktop arrives.",
|
||||
"If the app must stay blocking, raise the ESP setting 'Show error when installation takes longer than' (configured at {{espSyncFailureTimeoutMinutes}} min) — a limit, not a target; it only helps when the app reliably finishes within the new value.",
|
||||
"Check the app's dependencies and install order: a blocking app waiting on a large dependency download (Delivery Optimization, proxy throughput) is the usual reason it never finishes in time.",
|
||||
"If the ESP reported a non-Apps subcategory under '{{failedSubcategory}}', confirm the certificate/connector actually delivers — 'Continue anyway' hides a missing certificate, it does not fix it."
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
"description": "Detects when a certificate with the configured subject is missing from the local machine personal store (LocalMachine\\My). Requires the gather rule GATHER-ID-002 (machine cert store inventory) to be enabled — the check runs against its gather_machine_cert_store output. A missing certificate may indicate that MDM enrollment certificate provisioning or SCEP certificate deployment failed.",
|
||||
"severity": "warning",
|
||||
"category": "identity",
|
||||
"author": "Community",
|
||||
"isCommunity": true,
|
||||
"enabled": false,
|
||||
"evaluateOn": [
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
"$schema": "../schema/analyze-rule.schema.json",
|
||||
"ruleId": "ANALYZE-OFFICE-001",
|
||||
"title": "Microsoft 365 Apps Install Failed",
|
||||
"description": "Detects when the Microsoft 365 Apps (Office Click-to-Run) background install did not reach completion. The V2 OfficeInstallDetector watches the C2R client directly (registry + process) and emits office_install_failed when the install scenario was abandoned (worker gone and streaming never finished) or a C2R failure code was observed. This catches Office failures that the Intune integrated app type hides, because IME reports the app done within 1-2 minutes while C2R keeps installing in the background.",
|
||||
"description": "Detects when the Microsoft 365 Apps (Office Click-to-Run) install failed. The V2 OfficeInstallDetector watches the C2R client directly (registry, Delivery Optimization, process) and emits office_install_failed when the C2R INSTALL scenario records an error code. Completion is proven by the core Office binaries on disk; an install window that closes without that proof ends silently, never as a failure. This catches Office failures the deployment status hides: the Intune built-in app type is marked done once the configuration is delivered, and a Win32 or RealmJoin package once its installer exits, while C2R keeps installing in the background.",
|
||||
"severity": "warning",
|
||||
"category": "apps",
|
||||
"version": "1.1.0",
|
||||
"version": "1.1.1",
|
||||
"enabled": true,
|
||||
"evaluateOn": [
|
||||
"enrollment_end",
|
||||
@@ -39,12 +39,12 @@
|
||||
}
|
||||
],
|
||||
"confidenceThreshold": 40,
|
||||
"explanation": "The Microsoft 365 Apps (Office Click-to-Run) install did not finish.\n\nThe OfficeInstallDetector watches the C2R client directly and reports the real background install lifecycle. The Intune \"integrated\" Microsoft 365 Apps app type only pushes the configuration XML and starts the C2R bootstrapper, so IME marks the app done within 1-2 minutes while C2R keeps streaming and laying down the product for several more minutes. This event means C2R activity ended (the OfficeC2RClient worker exited and the install scenario cleared) without the StreamingFinished flag being set, or a C2R failure code was observed.\n\nReview the office_install_failed event payload: `products`, `channel`, `phase`, `streamingFinished`, `errorCode` (best-effort — may be empty) and `durationSeconds`.",
|
||||
"explanation": "The Microsoft 365 Apps (Office Click-to-Run) install failed.\n\nThe OfficeInstallDetector watches the C2R client directly and reports the real install lifecycle, however it was started. The Intune built-in Microsoft 365 Apps app type delivers the configuration through the Office CSP and lets the device start C2R; a Win32 or RealmJoin package runs the Office Deployment Tool. Either way the deployment is marked done before C2R has finished laying down the product. This event means C2R recorded an error code in its INSTALL scenario before the core Office apps were on disk.\n\nReview the office_install_failed event payload: `products`, `channel`, `errorCode`, `errorSource` (the C2R registry value that carried the code) and `durationSeconds`.",
|
||||
"remediation": [
|
||||
{
|
||||
"title": "Investigate the Office C2R install failure",
|
||||
"steps": [
|
||||
"Open the office_install_failed event and review products, channel, phase, streamingFinished and errorCode",
|
||||
"Open the office_install_failed event and review products, channel, errorCode and errorSource",
|
||||
"Check the Click-to-Run logs on the device (%windir%\\Temp and %temp%, files named <MachineName>-yyyymmdd-nnnn.log) for the failing operation",
|
||||
"Verify the device had network reachability to the Office CDN / Microsoft Connected Cache during the enrollment window",
|
||||
"Confirm the Intune Microsoft 365 Apps app configuration (products, channel, architecture) matches what the device can install",
|
||||
|
||||
+36
-23
@@ -2496,11 +2496,11 @@
|
||||
},
|
||||
{
|
||||
"ruleId": "ANALYZE-ESP-004",
|
||||
"title": "ESP Timeout with 'Continue Anyway' (Soft Failure)",
|
||||
"description": "Detects an ESP terminal failure on a profile that allows 'Continue anyway'. The enrollment is reported as failed, but the user most likely dismissed the error screen and reached the desktop — a soft failure rather than an unusable device.",
|
||||
"title": "ESP Gave Up on a Blocking App with 'Continue Anyway' (Soft Failure)",
|
||||
"description": "Detects an ESP terminal failure where the ESP gave up while a blocking app was still installing (the agent's own esp_apps_timeout verdict — no per-app HRESULT was reported) on a profile that allows 'Continue anyway'. The enrollment is reported as failed, but the user most likely dismissed the error screen and reached the desktop — a soft failure rather than an unusable device. An ESP failure that carries a specific error code (a failed installer, a detection mismatch) is a different case and stays with the rule that explains that code.",
|
||||
"severity": "warning",
|
||||
"category": "esp",
|
||||
"version": "1.0.1",
|
||||
"version": "1.1.0",
|
||||
"enabled": true,
|
||||
"baseConfidence": 80,
|
||||
"conditions": [
|
||||
@@ -2521,6 +2521,27 @@
|
||||
"value": "true",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"signal": "esp_gave_up_on_app",
|
||||
"source": "event_type",
|
||||
"eventType": "app_install_failed",
|
||||
"dataField": "failureType",
|
||||
"operator": "equals",
|
||||
"value": "esp_apps_timeout",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"signal": "appName",
|
||||
"source": "event_data",
|
||||
"eventType": "app_install_failed",
|
||||
"dataField": "appName",
|
||||
"operator": "exists",
|
||||
"value": "",
|
||||
"filterField": "failureType",
|
||||
"filterOperator": "equals",
|
||||
"filterValue": "esp_apps_timeout",
|
||||
"required": false
|
||||
},
|
||||
{
|
||||
"signal": "espSyncFailureTimeoutMinutes",
|
||||
"source": "event_type",
|
||||
@@ -2533,38 +2554,29 @@
|
||||
{
|
||||
"signal": "failedSubcategory",
|
||||
"source": "event_type",
|
||||
"eventType": "enrollment_failed",
|
||||
"eventType": "esp_failure_settle_started",
|
||||
"dataField": "failedSubcategory",
|
||||
"operator": "exists",
|
||||
"value": "",
|
||||
"required": false
|
||||
},
|
||||
{
|
||||
"signal": "appName",
|
||||
"source": "event_type",
|
||||
"eventType": "app_install_failed",
|
||||
"dataField": "appName",
|
||||
"operator": "exists",
|
||||
"value": "",
|
||||
"required": false
|
||||
}
|
||||
],
|
||||
"confidenceFactors": [
|
||||
{
|
||||
"signal": "appName",
|
||||
"signal": "failedSubcategory",
|
||||
"condition": "exists",
|
||||
"weight": 10
|
||||
}
|
||||
],
|
||||
"explanation": "This enrollment was reported as **failed** because the Enrollment Status Page reached its configured terminal timeout (`{{espSyncFailureTimeoutMinutes}} min`) before all blocking items finished. The ESP profile, however, allows **'Continue anyway'** — so the user most likely dismissed the error screen and reached the desktop with a working, though not fully provisioned, device.\n\nThe agent only observes the ESP terminal failure on the device side; it cannot see what the user did after the failure screen. Treat this as a **soft failure**: the device may well be in productive use, while at least one blocking item (`{{failedSubcategory}}` subcategory / app `{{appName}}`) did not complete inside the ESP window.\n\nA cluster of these across a tenant almost always points at one slow blocking app holding the whole fleet at the ESP timeout wall, not at broken devices. Any field still shown as a literal `{{…}}` placeholder was simply not recorded for this session (e.g. a certificate-only failure has no failed app).",
|
||||
"explanation": "This enrollment was reported as **failed** because the Enrollment Status Page gave up while the blocking app **{{appName}}** was still installing — the agent recorded no per-app error code, only that the app never finished inside the ESP window (the profile's configured limit is `{{espSyncFailureTimeoutMinutes}} min`; the ESP may have stopped earlier than that). The ESP profile, however, allows **'Continue anyway'** — so the user most likely dismissed the error screen and reached the desktop with a working, though not fully provisioned, device.\n\nThe agent only observes the ESP terminal failure on the device side; it cannot see what the user did after the failure screen. Treat this as a **soft failure**: the device may well be in productive use, while the `{{failedSubcategory}}` subcategory did not complete inside the ESP window.\n\nA cluster of these across a tenant almost always points at one slow blocking app holding the whole fleet at the ESP limit, not at broken devices. This rule deliberately stays silent when the failed app reported a specific error code — a failed installer or a detection mismatch is explained by the rule for that code, and 'Continue anyway' does not make it a timing problem. Any field still shown as a literal `{{…}}` placeholder was simply not recorded for this session.",
|
||||
"remediation": [
|
||||
{
|
||||
"title": "Relieve ESP pressure so enrollments finish inside the window",
|
||||
"steps": [
|
||||
"Identify the slow blocking app (`{{appName}}`) and either remove it from the ESP 'block device use until these required apps are installed' list, or raise the ESP installation timeout (currently {{espSyncFailureTimeoutMinutes}} min).",
|
||||
"If the app legitimately needs longer than the ESP allows, make it non-blocking and let it install after the desktop arrives.",
|
||||
"Verify the app's detection rule matches after install — an app that installs but fails detection times out the ESP on every device (see ANALYZE-APP-013).",
|
||||
"If a subcategory such as Certificates is reported under '{{failedSubcategory}}', confirm the certificate/connector actually delivers — 'Continue anyway' hides a missing certificate, it does not fix it."
|
||||
"Look at how long `{{appName}}` takes on a device that completes: if it legitimately needs longer than the ESP allows, make it non-blocking (remove it from 'block device use until these required apps are installed') and let it install after the desktop arrives.",
|
||||
"If the app must stay blocking, raise the ESP setting 'Show error when installation takes longer than' (configured at {{espSyncFailureTimeoutMinutes}} min) — a limit, not a target; it only helps when the app reliably finishes within the new value.",
|
||||
"Check the app's dependencies and install order: a blocking app waiting on a large dependency download (Delivery Optimization, proxy throughput) is the usual reason it never finishes in time.",
|
||||
"If the ESP reported a non-Apps subcategory under '{{failedSubcategory}}', confirm the certificate/connector actually delivers — 'Continue anyway' hides a missing certificate, it does not fix it."
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -2794,6 +2806,7 @@
|
||||
"description": "Detects when a certificate with the configured subject is missing from the local machine personal store (LocalMachine\\My). Requires the gather rule GATHER-ID-002 (machine cert store inventory) to be enabled — the check runs against its gather_machine_cert_store output. A missing certificate may indicate that MDM enrollment certificate provisioning or SCEP certificate deployment failed.",
|
||||
"severity": "warning",
|
||||
"category": "identity",
|
||||
"author": "Community",
|
||||
"isCommunity": true,
|
||||
"enabled": false,
|
||||
"evaluateOn": [
|
||||
@@ -3233,10 +3246,10 @@
|
||||
{
|
||||
"ruleId": "ANALYZE-OFFICE-001",
|
||||
"title": "Microsoft 365 Apps Install Failed",
|
||||
"description": "Detects when the Microsoft 365 Apps (Office Click-to-Run) background install did not reach completion. The V2 OfficeInstallDetector watches the C2R client directly (registry + process) and emits office_install_failed when the install scenario was abandoned (worker gone and streaming never finished) or a C2R failure code was observed. This catches Office failures that the Intune integrated app type hides, because IME reports the app done within 1-2 minutes while C2R keeps installing in the background.",
|
||||
"description": "Detects when the Microsoft 365 Apps (Office Click-to-Run) install failed. The V2 OfficeInstallDetector watches the C2R client directly (registry, Delivery Optimization, process) and emits office_install_failed when the C2R INSTALL scenario records an error code. Completion is proven by the core Office binaries on disk; an install window that closes without that proof ends silently, never as a failure. This catches Office failures the deployment status hides: the Intune built-in app type is marked done once the configuration is delivered, and a Win32 or RealmJoin package once its installer exits, while C2R keeps installing in the background.",
|
||||
"severity": "warning",
|
||||
"category": "apps",
|
||||
"version": "1.1.0",
|
||||
"version": "1.1.1",
|
||||
"enabled": true,
|
||||
"evaluateOn": [
|
||||
"enrollment_end",
|
||||
@@ -3270,12 +3283,12 @@
|
||||
}
|
||||
],
|
||||
"confidenceThreshold": 40,
|
||||
"explanation": "The Microsoft 365 Apps (Office Click-to-Run) install did not finish.\n\nThe OfficeInstallDetector watches the C2R client directly and reports the real background install lifecycle. The Intune \"integrated\" Microsoft 365 Apps app type only pushes the configuration XML and starts the C2R bootstrapper, so IME marks the app done within 1-2 minutes while C2R keeps streaming and laying down the product for several more minutes. This event means C2R activity ended (the OfficeC2RClient worker exited and the install scenario cleared) without the StreamingFinished flag being set, or a C2R failure code was observed.\n\nReview the office_install_failed event payload: `products`, `channel`, `phase`, `streamingFinished`, `errorCode` (best-effort — may be empty) and `durationSeconds`.",
|
||||
"explanation": "The Microsoft 365 Apps (Office Click-to-Run) install failed.\n\nThe OfficeInstallDetector watches the C2R client directly and reports the real install lifecycle, however it was started. The Intune built-in Microsoft 365 Apps app type delivers the configuration through the Office CSP and lets the device start C2R; a Win32 or RealmJoin package runs the Office Deployment Tool. Either way the deployment is marked done before C2R has finished laying down the product. This event means C2R recorded an error code in its INSTALL scenario before the core Office apps were on disk.\n\nReview the office_install_failed event payload: `products`, `channel`, `errorCode`, `errorSource` (the C2R registry value that carried the code) and `durationSeconds`.",
|
||||
"remediation": [
|
||||
{
|
||||
"title": "Investigate the Office C2R install failure",
|
||||
"steps": [
|
||||
"Open the office_install_failed event and review products, channel, phase, streamingFinished and errorCode",
|
||||
"Open the office_install_failed event and review products, channel, errorCode and errorSource",
|
||||
"Check the Click-to-Run logs on the device (%windir%\\Temp and %temp%, files named <MachineName>-yyyymmdd-nnnn.log) for the failing operation",
|
||||
"Verify the device had network reachability to the Office CDN / Microsoft Connected Cache during the enrollment window",
|
||||
"Confirm the Intune Microsoft 365 Apps app configuration (products, channel, architecture) matches what the device can install",
|
||||
|
||||
+24
-8
@@ -34,6 +34,14 @@
|
||||
"description": "Captures the exit code of a health-script execution. Feeds the early-signal slot consumed by HS-COMPLIANCE so the user sees the real exit code (not just the inferred 0/1) before HS-NEW-RESULT arrives.",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"patternId": "HS-INVOCATION",
|
||||
"category": "always",
|
||||
"pattern": "^\"[^\"]{0,260}\\\\agentexecutor\\.exe\"\\s+-(?<kind>remediationScript)\\s",
|
||||
"action": "agentInvocationArgument",
|
||||
"description": "The IME service's command line for a health-script executor, written by the health-script worker to IntuneManagementExtension.log and HealthScripts.log right before 'Launch powershell executor in ... session'. Opens an invocation that owns nothing, so the launch line the same worker writes next never sets the context of the platform script whose run is in flight in the IME log. The platform-script command line ('-powershell') does not match.",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"patternId": "HS-NEW-RESULT",
|
||||
"category": "always",
|
||||
@@ -646,6 +654,22 @@
|
||||
"description": "Detects WinGet app installation progress with Operation Phase = Installing (format variant 2).",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"patternId": "PS-AGENT-ARG",
|
||||
"category": "always",
|
||||
"pattern": "^Adding argument (?<kind>[A-Za-z0-9]+) with value ",
|
||||
"action": "agentInvocationArgument",
|
||||
"description": "The first argument line of every AgentExecutor.exe invocation (powershell, remediationScript, detectionScript, requirementScript, ...). Opens the invocation that owns the policyId-less exit/output lines following it in AgentExecutor.log; PS-AGENT-SCRIPT-START upgrades a powershell invocation to its platform script. Also keeps the tracker's overwrite check active while the invocation runs.",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"patternId": "PS-AGENT-COMPLETED",
|
||||
"category": "always",
|
||||
"pattern": "^Agent executor completed\\.",
|
||||
"action": "agentExecutorCompleted",
|
||||
"description": "Last line of an AgentExecutor.exe invocation. Closes the invocation's ownership of the exit/output lines in AgentExecutor.log and triggers the tracker's overwrite check: the executor wrote its end block at its own stream position, which may lie behind the tracker's bookmark when another process appended meanwhile.",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"patternId": "PS-AGENT-ERROR",
|
||||
"category": "always",
|
||||
@@ -708,14 +732,6 @@
|
||||
"description": "Captures whether a PowerShell script runs in machine (System) or user session context.",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"patternId": "PS-SCRIPT-EXITCODE",
|
||||
"category": "always",
|
||||
"pattern": "^Powershell execution is done, exitCode = (?<exitCode>-?\\d+)",
|
||||
"action": "scriptExitCode",
|
||||
"description": "Captures the PowerShell exit code when script execution completes in the IME log.",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"patternId": "PS-SCRIPT-GENERATED",
|
||||
"category": "always",
|
||||
|
||||
@@ -94,7 +94,11 @@
|
||||
"C:\\Windows\\Panther",
|
||||
"C:\\Windows\\SetupDiag",
|
||||
"C:\\ProgramData\\Microsoft\\DiagnosticLogCSP",
|
||||
"C:\\Windows\\SoftwareDistribution\\ReportingEvents.log"
|
||||
"C:\\Windows\\SoftwareDistribution\\ReportingEvents.log",
|
||||
"C:\\ProgramData\\RealmJoin\\choco\\logs"
|
||||
],
|
||||
"userProfileFilePrefixes": [
|
||||
"AppData\\Local\\RealmJoin"
|
||||
],
|
||||
"wmiQueryPrefixes": [
|
||||
"SELECT * FROM Win32_OperatingSystem",
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"$schema": "../schema/ime-log-pattern.schema.json",
|
||||
"patternId": "HS-INVOCATION",
|
||||
"category": "always",
|
||||
"pattern": "^\"[^\"]{0,260}\\\\agentexecutor\\.exe\"\\s+-(?<kind>remediationScript)\\s",
|
||||
"action": "agentInvocationArgument",
|
||||
"description": "The IME service's command line for a health-script executor, written by the health-script worker to IntuneManagementExtension.log and HealthScripts.log right before 'Launch powershell executor in ... session'. Opens an invocation that owns nothing, so the launch line the same worker writes next never sets the context of the platform script whose run is in flight in the IME log. The platform-script command line ('-powershell') does not match.",
|
||||
"enabled": true
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"$schema": "../schema/ime-log-pattern.schema.json",
|
||||
"patternId": "PS-AGENT-ARG",
|
||||
"category": "always",
|
||||
"pattern": "^Adding argument (?<kind>[A-Za-z0-9]+) with value ",
|
||||
"action": "agentInvocationArgument",
|
||||
"description": "The first argument line of every AgentExecutor.exe invocation (powershell, remediationScript, detectionScript, requirementScript, ...). Opens the invocation that owns the policyId-less exit/output lines following it in AgentExecutor.log; PS-AGENT-SCRIPT-START upgrades a powershell invocation to its platform script. Also keeps the tracker's overwrite check active while the invocation runs.",
|
||||
"enabled": true
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"$schema": "../schema/ime-log-pattern.schema.json",
|
||||
"patternId": "PS-AGENT-COMPLETED",
|
||||
"category": "always",
|
||||
"pattern": "^Agent executor completed\\.",
|
||||
"action": "agentExecutorCompleted",
|
||||
"description": "Last line of an AgentExecutor.exe invocation. Closes the invocation's ownership of the exit/output lines in AgentExecutor.log and triggers the tracker's overwrite check: the executor wrote its end block at its own stream position, which may lie behind the tracker's bookmark when another process appended meanwhile.",
|
||||
"enabled": true
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"$schema": "../schema/ime-log-pattern.schema.json",
|
||||
"patternId": "PS-SCRIPT-EXITCODE",
|
||||
"category": "always",
|
||||
"pattern": "^Powershell execution is done, exitCode = (?<exitCode>-?\\d+)",
|
||||
"action": "scriptExitCode",
|
||||
"description": "Captures the PowerShell exit code when script execution completes in the IME log.",
|
||||
"enabled": true
|
||||
}
|
||||
@@ -190,7 +190,7 @@
|
||||
},
|
||||
"operator": {
|
||||
"type": "string",
|
||||
"description": "Comparison operator.",
|
||||
"description": "Comparison operator. exists/not_exists with a dataField test whether ANY event of the type carries a non-empty value at that field: not_exists matches when no event does — including when the event type is absent altogether — so a rule can gate on a missing field (e.g. an ESP failure without an HRESULT). Without a dataField they test only the presence of the event type.",
|
||||
"enum": ["equals", "not_equals", "contains", "not_contains", "regex", "not_regex", "gt", "lt", "gte", "lte", "exists", "not_exists", "count_gte", "count_per_group_gte", "in", "not_in"]
|
||||
},
|
||||
"value": {
|
||||
|
||||
@@ -8,6 +8,16 @@
|
||||
"$schema": { "type": "string" },
|
||||
"registryPrefixes": { "$ref": "#/$defs/categorizedList" },
|
||||
"filePrefixes": { "$ref": "#/$defs/stringList" },
|
||||
"userProfileFilePrefixes": {
|
||||
"description": "Folders under the signed-in user's profile that file collectors and diagnostics paths may read, RELATIVE to the profile root (e.g. \"AppData\\\\Local\\\\Vendor\"). Resolved against %LOGGED_ON_USER_PROFILE% at match time. Entries outside AppData\\Local / AppData\\Roaming are dropped at load: the C:\\Users hard block never admits them, so this list can only ever narrow what the block's exception already permits.",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"allOf": [
|
||||
{ "$ref": "#/$defs/safeString" },
|
||||
{ "pattern": "^AppData\\\\(Local|Roaming)(\\\\|$)" }
|
||||
]
|
||||
}
|
||||
},
|
||||
"wmiQueryPrefixes": { "$ref": "#/$defs/stringList" },
|
||||
"eventLogChannels": { "$ref": "#/$defs/categorizedList" },
|
||||
"blockedEventLogChannels": { "$ref": "#/$defs/stringList" },
|
||||
|
||||
@@ -71,6 +71,8 @@
|
||||
"scriptOutput",
|
||||
"scriptCompleted",
|
||||
"resetPlatformScriptContext",
|
||||
"agentInvocationArgument",
|
||||
"agentExecutorCompleted",
|
||||
"healthScriptResult",
|
||||
"healthScriptDetectionResult",
|
||||
"captureExitCode",
|
||||
|
||||
@@ -208,6 +208,14 @@ export const ALLOWED_FILE_PREFIXES: readonly string[] = [
|
||||
${flatArray(guardrails.filePrefixes)}
|
||||
];
|
||||
|
||||
// Folders under the signed-in user's profile, RELATIVE to the profile root. The agent
|
||||
// resolves %LOGGED_ON_USER_PROFILE% at runtime; the portal validates against a placeholder
|
||||
// profile. These cannot live in ALLOWED_FILE_PREFIXES — that list is matched literally
|
||||
// against the already-expanded path, where the user name is unknown at authoring time.
|
||||
export const USER_PROFILE_FILE_PREFIXES: readonly string[] = [
|
||||
${flatArray(guardrails.userProfileFilePrefixes ?? [])}
|
||||
];
|
||||
|
||||
export const ALLOWED_WMI_QUERY_PREFIXES: readonly string[] = [
|
||||
${flatArray(guardrails.wmiQueryPrefixes)}
|
||||
];
|
||||
|
||||
@@ -14,6 +14,22 @@
|
||||
BeforeAll {
|
||||
. (Join-Path $PSScriptRoot 'Install-AutopilotMonitor.ps1')
|
||||
|
||||
function New-FakeSignature {
|
||||
param([string]$Status, [string]$Subject)
|
||||
$cert = if ($Subject) {
|
||||
[pscustomobject]@{ Subject = $Subject; Thumbprint = '0123456789ABCDEF' }
|
||||
} else { $null }
|
||||
return [pscustomobject]@{ Status = $Status; SignerCertificate = $cert }
|
||||
}
|
||||
|
||||
function New-FakePayload {
|
||||
param([string[]]$Files)
|
||||
$dir = Join-Path $TestDrive ([guid]::NewGuid().ToString())
|
||||
New-Item -ItemType Directory -Path $dir -Force | Out-Null
|
||||
foreach ($f in $Files) { Set-Content -Path (Join-Path $dir $f) -Value 'x' }
|
||||
return $dir
|
||||
}
|
||||
|
||||
function New-FakeProfile {
|
||||
param([string]$Name, [double]$AgeMinutes)
|
||||
$p = Join-Path $TestDrive $Name
|
||||
@@ -305,3 +321,93 @@ Describe 'logging purity' {
|
||||
Get-Content $LogFile -Raw | Should -Match 'SKIP: Agent was previously deployed'
|
||||
}
|
||||
}
|
||||
|
||||
Describe 'Get-UnverifiedAgentBinaries' {
|
||||
# The publisher gate on the extracted payload. What matters is the DECISION: which
|
||||
# payload is accepted, which is refused, and which files are looked at at all.
|
||||
BeforeEach {
|
||||
Mock Write-Log { }
|
||||
$script:ours = 'CN=glueckkanja AG, O=glueckkanja AG, C=DE'
|
||||
}
|
||||
|
||||
It 'accepts a payload where every binary of ours is validly signed by us' {
|
||||
$dir = New-FakePayload -Files @('AutopilotMonitor.Agent.exe', 'AutopilotMonitor.Shared.dll')
|
||||
Mock Get-AuthenticodeSignature { New-FakeSignature -Status 'Valid' -Subject $script:ours }
|
||||
Get-UnverifiedAgentBinaries -Path $dir | Should -BeNullOrEmpty
|
||||
}
|
||||
|
||||
It 'checks the binaries we author and leaves third-party assemblies alone' {
|
||||
$dir = New-FakePayload -Files @(
|
||||
'AutopilotMonitor.Agent.exe',
|
||||
'AutopilotMonitor.Agent.exe.config',
|
||||
'AutopilotMonitor.Shared.dll',
|
||||
'Newtonsoft.Json.dll',
|
||||
'System.Management.Automation.dll'
|
||||
)
|
||||
$script:checked = @()
|
||||
Mock Get-AuthenticodeSignature {
|
||||
$script:checked += (Split-Path $FilePath -Leaf)
|
||||
New-FakeSignature -Status 'Valid' -Subject $script:ours
|
||||
}
|
||||
|
||||
Get-UnverifiedAgentBinaries -Path $dir | Should -BeNullOrEmpty
|
||||
$script:checked | Should -Be @('AutopilotMonitor.Agent.exe', 'AutopilotMonitor.Shared.dll')
|
||||
}
|
||||
|
||||
It 'refuses an unsigned agent executable' {
|
||||
$dir = New-FakePayload -Files @('AutopilotMonitor.Agent.exe')
|
||||
Mock Get-AuthenticodeSignature { New-FakeSignature -Status 'NotSigned' -Subject $null }
|
||||
(Get-UnverifiedAgentBinaries -Path $dir).Count | Should -Be 1
|
||||
}
|
||||
|
||||
It 'refuses a tampered binary (hash mismatch)' {
|
||||
$dir = New-FakePayload -Files @('AutopilotMonitor.Agent.exe')
|
||||
Mock Get-AuthenticodeSignature { New-FakeSignature -Status 'HashMismatch' -Subject $script:ours }
|
||||
(Get-UnverifiedAgentBinaries -Path $dir).Count | Should -Be 1
|
||||
}
|
||||
|
||||
It 'refuses a signature whose chain does not validate' {
|
||||
$dir = New-FakePayload -Files @('AutopilotMonitor.Agent.exe')
|
||||
Mock Get-AuthenticodeSignature { New-FakeSignature -Status 'UnknownError' -Subject $script:ours }
|
||||
(Get-UnverifiedAgentBinaries -Path $dir).Count | Should -Be 1
|
||||
}
|
||||
|
||||
It 'refuses a validly signed binary from a different publisher' {
|
||||
$dir = New-FakePayload -Files @('AutopilotMonitor.Agent.exe')
|
||||
Mock Get-AuthenticodeSignature { New-FakeSignature -Status 'Valid' -Subject 'CN=Somebody Else, O=Somebody Else Ltd, C=US' }
|
||||
(Get-UnverifiedAgentBinaries -Path $dir).Count | Should -Be 1
|
||||
}
|
||||
|
||||
It 'refuses the whole payload when a single DLL of ours is swapped' {
|
||||
$dir = New-FakePayload -Files @('AutopilotMonitor.Agent.exe', 'AutopilotMonitor.Shared.dll')
|
||||
Mock Get-AuthenticodeSignature {
|
||||
if ((Split-Path $FilePath -Leaf) -eq 'AutopilotMonitor.Shared.dll') {
|
||||
New-FakeSignature -Status 'Valid' -Subject 'CN=Somebody Else, O=Somebody Else Ltd, C=US'
|
||||
} else {
|
||||
New-FakeSignature -Status 'Valid' -Subject $script:ours
|
||||
}
|
||||
}
|
||||
$rejected = Get-UnverifiedAgentBinaries -Path $dir
|
||||
$rejected.Count | Should -Be 1
|
||||
$rejected[0] | Should -BeLike 'AutopilotMonitor.Shared.dll*'
|
||||
}
|
||||
|
||||
It 'refuses when the signature check itself throws' {
|
||||
$dir = New-FakePayload -Files @('AutopilotMonitor.Agent.exe')
|
||||
Mock Get-AuthenticodeSignature { throw 'access denied' }
|
||||
(Get-UnverifiedAgentBinaries -Path $dir).Count | Should -Be 1
|
||||
}
|
||||
|
||||
It 'refuses an empty payload instead of passing it' {
|
||||
$dir = New-FakePayload -Files @('Newtonsoft.Json.dll')
|
||||
Mock Get-AuthenticodeSignature { New-FakeSignature -Status 'Valid' -Subject $script:ours }
|
||||
(Get-UnverifiedAgentBinaries -Path $dir).Count | Should -Be 1
|
||||
}
|
||||
}
|
||||
|
||||
Describe 'publisher pin' {
|
||||
It 'pins the expected publisher by subject, not by thumbprint' {
|
||||
$ExpectedPublisher | Should -Be '*O=glueckkanja AG*'
|
||||
$ExpectedPublisher | Should -Not -Match '^[0-9A-Fa-f]{40}$'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,9 @@
|
||||
2. Downloads the monitoring agent ZIP from Azure Blob Storage
|
||||
3. Verifies integrity via SHA-256 hash from the version manifest (mandatory)
|
||||
4. Extracts the agent into %ProgramData%\AutopilotMonitor\Agent
|
||||
5. Runs the agent in --install mode (registers Scheduled Task, spawns the runtime)
|
||||
6. Verifies the runtime process actually launched
|
||||
5. Verifies the Authenticode signature and publisher of every binary we author
|
||||
6. Runs the agent in --install mode (registers Scheduled Task, spawns the runtime)
|
||||
7. Verifies the runtime process actually launched
|
||||
Agent self-destructs when enrollment completes.
|
||||
|
||||
Relax flow (guards 2+3): a real user profile (or a real LastLoggedOnUser)
|
||||
@@ -76,6 +77,11 @@
|
||||
PowerShell 5.1 (IME) reads scripts without BOM as ANSI, corrupting multi-byte chars.
|
||||
|
||||
.CHANGELOG
|
||||
2026-09-08 v2.5 Publisher gate: every AutopilotMonitor binary in the extracted
|
||||
payload must carry a valid Authenticode signature from the
|
||||
expected publisher before --install runs. The SHA-256 manifest
|
||||
check stays and is unchanged; it proves the bytes, the signature
|
||||
proves the author.
|
||||
2026-08-08 v2.3 Windows 365 Cloud PC support: a positively identified Cloud PC
|
||||
(Windows365 registry key AND CloudManagedDesktopExtension
|
||||
service) now triggers the guard 2+3 relax alongside OOBE
|
||||
@@ -128,7 +134,12 @@ param(
|
||||
)
|
||||
|
||||
# Script version (bump on meaningful changes; see .CHANGELOG above)
|
||||
$ScriptVersion = "2.4"
|
||||
$ScriptVersion = "2.5"
|
||||
|
||||
# The publisher every binary we author must carry. A -like pattern on the subject, NOT a
|
||||
# thumbprint: certificates are renewed, deployed copies of this script are not. Same value
|
||||
# the loader pins for this script.
|
||||
$ExpectedPublisher = "*O=glueckkanja AG*"
|
||||
|
||||
# Configuration - Everything in ProgramData for easy cleanup
|
||||
$AgentBasePath = "$env:ProgramData\AutopilotMonitor"
|
||||
@@ -329,6 +340,56 @@ function Get-BootstrapDecision {
|
||||
return [pscustomobject]@{ Install = $true; ReasonCode = $null; RelaxActive = $relax.Active }
|
||||
}
|
||||
|
||||
# Returns the list of reasons why the extracted payload is not ours; an empty result means
|
||||
# every binary we author carries a valid Authenticode signature from the expected publisher.
|
||||
# Third-party assemblies keep their own publisher's signature and are not checked here.
|
||||
# Any error is a rejection: an unreadable signature is an unverified binary.
|
||||
function Get-UnverifiedAgentBinaries {
|
||||
param([string]$Path)
|
||||
|
||||
$rejected = @()
|
||||
$ourBinaries = @(
|
||||
Get-ChildItem -Path $Path -Filter 'AutopilotMonitor.*' -File -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.Extension -in @('.exe', '.dll') }
|
||||
)
|
||||
|
||||
# An empty set is a rejection, not a pass: it means the extraction produced nothing we
|
||||
# recognise, and the alternative would be a gate that silently approves an empty folder.
|
||||
# The comma keeps a single rejection an ARRAY: PowerShell unrolls a one-element result to
|
||||
# a bare string, and the caller reads .Count and indexes into it.
|
||||
if ($ourBinaries.Count -eq 0) {
|
||||
return , @("no AutopilotMonitor binaries found in $Path")
|
||||
}
|
||||
|
||||
foreach ($binary in $ourBinaries) {
|
||||
try {
|
||||
$sig = Get-AuthenticodeSignature -FilePath $binary.FullName -ErrorAction Stop
|
||||
}
|
||||
catch {
|
||||
$rejected += "$($binary.Name): signature check failed to run ($($_.Exception.Message))"
|
||||
continue
|
||||
}
|
||||
|
||||
if ($null -eq $sig) {
|
||||
$rejected += "$($binary.Name): signature check returned nothing"
|
||||
continue
|
||||
}
|
||||
|
||||
$subject = if ($sig.SignerCertificate) { $sig.SignerCertificate.Subject } else { '<unsigned>' }
|
||||
|
||||
if ($sig.Status -ne 'Valid') {
|
||||
$rejected += "$($binary.Name): signature status $($sig.Status)"
|
||||
continue
|
||||
}
|
||||
if ($subject -notlike $ExpectedPublisher) {
|
||||
$rejected += "$($binary.Name): unexpected publisher $subject"
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
return , $rejected
|
||||
}
|
||||
|
||||
# Main bootstrap flow. Only invoked when the script is run directly (see entry guard
|
||||
# at the bottom); never on dot-source.
|
||||
function Invoke-Bootstrap {
|
||||
@@ -445,6 +506,20 @@ function Invoke-Bootstrap {
|
||||
}
|
||||
}
|
||||
|
||||
# Publisher gate. The SHA-256 above proves the ZIP is the one the manifest names, but
|
||||
# that manifest lives in the same container as the ZIP and cannot outlive a compromise
|
||||
# of the download host. The expected publisher travels inside THIS script, which the
|
||||
# loader verified before running it, so the chain from the file an administrator
|
||||
# assigns to the process that starts here stays publisher-anchored end to end.
|
||||
# Fail-closed, exactly like the hash check: running unverified code as SYSTEM is the
|
||||
# risk being removed.
|
||||
Write-Log "Verifying Authenticode signatures of the agent binaries..."
|
||||
$unverified = Get-UnverifiedAgentBinaries -Path $AgentBinPath
|
||||
if ($unverified.Count -gt 0) {
|
||||
throw "Authenticode verification FAILED ($($unverified -join '; ')). Refusing to run an agent that is not signed by the expected publisher."
|
||||
}
|
||||
Write-Log "Authenticode verification passed (publisher pin: $ExpectedPublisher)"
|
||||
|
||||
Write-Log "Calling agent install mode (--install)..."
|
||||
& $agentExePath --install
|
||||
$installExitCode = $LASTEXITCODE
|
||||
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AutopilotMonitor.Agent.V2.Core.Logging;
|
||||
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals;
|
||||
using AutopilotMonitor.Agent.V2.Core.Orchestration;
|
||||
using AutopilotMonitor.Agent.V2.Core.SignalAdapters;
|
||||
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
|
||||
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
|
||||
using AutopilotMonitor.DecisionCore.Signals;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring
|
||||
{
|
||||
/// <summary>
|
||||
/// Session 683f1eff — a Shell-Core 62407 failure (<c>WhiteGlove_Failed</c>) arrived four
|
||||
/// seconds into the provisioning tracker's 30-s settle window and terminalised the session
|
||||
/// with nothing but its failure type: the HRESULT (0x80070652) and the failed subcategory
|
||||
/// (Apps) the registry had already named never reached the terminal event, the backend
|
||||
/// FailureReason or the termination-time app classification. The coordinator now inherits
|
||||
/// the pending settle detail onto the Shell-Core args and records the terminal-failure
|
||||
/// snapshot from it.
|
||||
/// </summary>
|
||||
public sealed class EspAndHelloTrackerShellCoreFailureInheritanceTests
|
||||
{
|
||||
private static readonly DateTime Fixed = new DateTime(2026, 9, 8, 18, 11, 6, DateTimeKind.Utc);
|
||||
|
||||
private sealed class Fixture : IDisposable
|
||||
{
|
||||
public TempDirectory Tmp { get; } = new TempDirectory();
|
||||
public AgentLogger Logger { get; }
|
||||
public FakeSignalIngressSink Ingress { get; } = new FakeSignalIngressSink();
|
||||
public FakeSignalIngressSink TrackerPostSink { get; } = new FakeSignalIngressSink();
|
||||
public VirtualClock Clock { get; } = new VirtualClock(Fixed);
|
||||
public List<EspFailureDetectedEventArgs> Forwarded { get; } = new List<EspFailureDetectedEventArgs>();
|
||||
|
||||
public Fixture() { Logger = new AgentLogger(Tmp.Path, AgentLogLevel.Debug); }
|
||||
|
||||
public EspAndHelloTracker BuildCoordinator(Func<EspFailureDetectedEventArgs?> pendingProbe)
|
||||
{
|
||||
var coordinator = new EspAndHelloTracker(
|
||||
sessionId: "S1",
|
||||
tenantId: "T1",
|
||||
post: new InformationalEventPost(TrackerPostSink, Clock),
|
||||
logger: Logger,
|
||||
skipConfigProbe: () => ((bool?)false, (bool?)false),
|
||||
accountSetupActivityProbe: () => false,
|
||||
pendingEspFailureProbe: pendingProbe);
|
||||
coordinator.EspFailureDetected += (_, args) => Forwarded.Add(args);
|
||||
return coordinator;
|
||||
}
|
||||
|
||||
public FakeSignalIngressSink.PostedSignal TerminalSignal() =>
|
||||
Assert.Single(Ingress.Posted, p => p.Kind == DecisionSignalKind.EspTerminalFailure);
|
||||
|
||||
public void Dispose() { Tmp.Dispose(); }
|
||||
}
|
||||
|
||||
private static EspFailureDetectedEventArgs PendingAppsFailure() => new EspFailureDetectedEventArgs(
|
||||
failureType: "Provisioning_DeviceSetup_Apps_Failed",
|
||||
errorCode: "0x80070652",
|
||||
failedSubcategory: "Apps",
|
||||
category: "DeviceSetup",
|
||||
likelyCulpritApps: new[] { "ESP Finalizer US" });
|
||||
|
||||
[Fact]
|
||||
public void ShellCore_failure_inside_the_settle_window_inherits_the_registry_detail()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
using var coordinator = f.BuildCoordinator(PendingAppsFailure);
|
||||
using var adapter = new EspAndHelloTrackerAdapter(coordinator, f.Ingress, f.Clock);
|
||||
|
||||
coordinator.TriggerShellCoreEspFailureForTest("WhiteGlove_Failed");
|
||||
|
||||
var args = Assert.Single(f.Forwarded);
|
||||
Assert.Equal("WhiteGlove_Failed", args.FailureType); // the signal that terminalised
|
||||
Assert.Equal("0x80070652", args.ErrorCode);
|
||||
Assert.Equal("Apps", args.FailedSubcategory);
|
||||
Assert.Equal("DeviceSetup", args.Category);
|
||||
Assert.Equal(new[] { "ESP Finalizer US" }, args.LikelyCulpritApps);
|
||||
|
||||
// Wire-through: the EspTerminalFailure signal payload carries the detail the
|
||||
// DecisionEngine copies onto enrollment_failed.
|
||||
var signal = f.TerminalSignal();
|
||||
Assert.Equal("WhiteGlove_Failed", signal.Payload!["failureType"]);
|
||||
Assert.Equal("0x80070652", signal.Payload["errorCode"]);
|
||||
Assert.Equal("Apps", signal.Payload["failedSubcategory"]);
|
||||
Assert.Equal("DeviceSetup", signal.Payload["category"]);
|
||||
Assert.Equal("ESP Finalizer US", signal.Payload["likelyCulpritApps"]);
|
||||
|
||||
// Termination-time classification reads the same context.
|
||||
Assert.NotNull(coordinator.LastEspTerminalFailure);
|
||||
Assert.Equal("0x80070652", coordinator.LastEspTerminalFailure.ErrorCode);
|
||||
Assert.True(coordinator.LastEspTerminalFailure.IsAppsSubcategory);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShellCore_failure_without_a_pending_window_stays_bare()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
using var coordinator = f.BuildCoordinator(() => null);
|
||||
using var adapter = new EspAndHelloTrackerAdapter(coordinator, f.Ingress, f.Clock);
|
||||
|
||||
coordinator.TriggerShellCoreEspFailureForTest("ESPProgress_Timeout");
|
||||
|
||||
var args = Assert.Single(f.Forwarded);
|
||||
Assert.Equal("ESPProgress_Timeout", args.FailureType);
|
||||
Assert.Null(args.ErrorCode);
|
||||
Assert.Null(args.FailedSubcategory);
|
||||
Assert.Null(args.Category);
|
||||
Assert.Empty(args.LikelyCulpritApps);
|
||||
Assert.Null(coordinator.LastEspTerminalFailure);
|
||||
|
||||
var signal = f.TerminalSignal();
|
||||
Assert.False(signal.Payload!.ContainsKey("errorCode"));
|
||||
Assert.False(signal.Payload.ContainsKey("failedSubcategory"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_throwing_probe_degrades_to_the_bare_failure()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
using var coordinator = f.BuildCoordinator(() => throw new InvalidOperationException("probe"));
|
||||
|
||||
coordinator.TriggerShellCoreEspFailureForTest("WhiteGlove_Failed");
|
||||
|
||||
var args = Assert.Single(f.Forwarded);
|
||||
Assert.Equal("WhiteGlove_Failed", args.FailureType);
|
||||
Assert.Null(args.ErrorCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
using System;
|
||||
using AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Gather
|
||||
{
|
||||
/// <summary>
|
||||
/// A configured diagnostics entry is resolved to the folder and pattern that get
|
||||
/// enumerated, and the guard judges exactly that folder. The cases below pin the
|
||||
/// property that matters: no spelling of an allowed entry ever selects its parent.
|
||||
/// </summary>
|
||||
public sealed class DiagnosticsPathGuardsTests
|
||||
{
|
||||
private const string TestProfile = @"C:\Users\TestUser";
|
||||
|
||||
private static readonly Func<string, bool> IsDirectory = _ => true;
|
||||
private static readonly Func<string, bool> IsNotDirectory = _ => false;
|
||||
|
||||
private static DiagnosticsPathGuards.CollectionTarget Resolve(
|
||||
string path,
|
||||
bool includeSubfolders = false,
|
||||
bool unrestrictedMode = false,
|
||||
string? userProfilePath = null,
|
||||
Func<string, bool>? directoryExists = null)
|
||||
{
|
||||
Assert.True(DiagnosticsPathGuards.TryResolveCollectionTarget(
|
||||
path, includeSubfolders, unrestrictedMode, userProfilePath!, directoryExists ?? IsDirectory, out var target));
|
||||
return target;
|
||||
}
|
||||
|
||||
private static void AssertRefused(
|
||||
string path,
|
||||
bool unrestrictedMode = false,
|
||||
string? userProfilePath = null,
|
||||
Func<string, bool>? directoryExists = null)
|
||||
{
|
||||
Assert.False(DiagnosticsPathGuards.TryResolveCollectionTarget(
|
||||
path, true, unrestrictedMode, userProfilePath!, directoryExists ?? IsDirectory, out var target));
|
||||
Assert.Null(target);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ folder entries
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"C:\Windows\Logs")]
|
||||
[InlineData(@"C:\Windows\Logs\")]
|
||||
[InlineData(@"C:\Windows\SetupDiag")]
|
||||
[InlineData(@"C:\ProgramData\AutopilotMonitor")]
|
||||
[InlineData(@"C:\ProgramData\Microsoft\DiagnosticLogCSP")]
|
||||
public void Folder_entry_collects_from_itself_never_from_its_parent(string configured)
|
||||
{
|
||||
var target = Resolve(configured, includeSubfolders: true);
|
||||
|
||||
Assert.Equal(configured.TrimEnd('\\'), target.Folder, ignoreCase: true);
|
||||
Assert.Equal("*", target.Pattern);
|
||||
Assert.True(target.Recurse);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Trailing_separator_means_folder_even_when_the_folder_does_not_exist_yet()
|
||||
{
|
||||
var target = Resolve(@"C:\Windows\Logs\NotThereYet\", directoryExists: IsNotDirectory);
|
||||
|
||||
Assert.Equal(@"C:\Windows\Logs\NotThereYet", target.Folder, ignoreCase: true);
|
||||
Assert.Equal("*", target.Pattern);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Folder_name_with_a_dot_is_still_a_folder()
|
||||
{
|
||||
var target = Resolve(@"C:\Windows\Logs\vendor.v2");
|
||||
|
||||
Assert.Equal(@"C:\Windows\Logs\vendor.v2", target.Folder, ignoreCase: true);
|
||||
Assert.Equal("*", target.Pattern);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ wildcard entries
|
||||
|
||||
[Fact]
|
||||
public void Wildcard_entry_collects_the_pattern_from_its_validated_parent()
|
||||
{
|
||||
var target = Resolve(@"C:\Windows\Panther\*.log", includeSubfolders: true);
|
||||
|
||||
Assert.Equal(@"C:\Windows\Panther", target.Folder, ignoreCase: true);
|
||||
Assert.Equal("*.log", target.Pattern);
|
||||
Assert.True(target.Recurse);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wildcard_beside_an_allowlisted_file_is_refused()
|
||||
{
|
||||
// Only ReportingEvents.log is on the allowlist, not the folder it lives in.
|
||||
AssertRefused(@"C:\Windows\SoftwareDistribution\*.log");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ file entries
|
||||
|
||||
[Fact]
|
||||
public void File_entry_never_recurses_because_its_folder_was_not_validated()
|
||||
{
|
||||
var target = Resolve(
|
||||
@"C:\Windows\SoftwareDistribution\ReportingEvents.log",
|
||||
includeSubfolders: true,
|
||||
directoryExists: IsNotDirectory);
|
||||
|
||||
Assert.Equal(@"C:\Windows\SoftwareDistribution", target.Folder, ignoreCase: true);
|
||||
Assert.Equal("ReportingEvents.log", target.Pattern);
|
||||
Assert.False(target.Recurse);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Missing_path_without_a_dot_is_one_file_name_not_a_wildcard_on_the_parent()
|
||||
{
|
||||
var target = Resolve(@"C:\Windows\Logs\nodot", includeSubfolders: true, directoryExists: IsNotDirectory);
|
||||
|
||||
Assert.Equal(@"C:\Windows\Logs", target.Folder, ignoreCase: true);
|
||||
Assert.Equal("nodot", target.Pattern);
|
||||
Assert.False(target.Recurse);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ refusals
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"C:\Windows")]
|
||||
[InlineData(@"C:\ProgramData")]
|
||||
[InlineData(@"C:\ProgramData\Microsoft")]
|
||||
[InlineData(@"C:\Windows\Logs\..\System32")]
|
||||
[InlineData(@"C:\Windows\LogsEvil")]
|
||||
[InlineData("")]
|
||||
public void Paths_outside_the_allowlist_are_refused(string configured)
|
||||
{
|
||||
AssertRefused(configured);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"C:\Users")]
|
||||
[InlineData(@"C:\Users\TestUser\Documents")]
|
||||
[InlineData(@"C:\Windows\System32\config")]
|
||||
[InlineData(@"C:\Windows\System32\config\*")]
|
||||
public void Hard_blocks_hold_in_unrestricted_mode(string configured)
|
||||
{
|
||||
AssertRefused(configured, unrestrictedMode: true);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ user-profile token
|
||||
|
||||
[Fact]
|
||||
public void Expanded_token_path_outside_AppData_is_refused_even_in_unrestricted_mode()
|
||||
{
|
||||
// The service expands %LOGGED_ON_USER_PROFILE% before the guard sees the path; an
|
||||
// unexpanded token is not under C:\Users lexically and used to slip past the block.
|
||||
AssertRefused(TestProfile + @"\Documents", unrestrictedMode: true, userProfilePath: TestProfile);
|
||||
AssertRefused(TestProfile + @"\Documents\*.txt", unrestrictedMode: true, userProfilePath: TestProfile);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Expanded_token_path_on_the_profile_allowlist_is_admitted_in_restricted_mode()
|
||||
{
|
||||
var target = Resolve(
|
||||
TestProfile + @"\AppData\Local\RealmJoin\Logs\*.log",
|
||||
userProfilePath: TestProfile);
|
||||
|
||||
Assert.Equal(TestProfile + @"\AppData\Local\RealmJoin\Logs", target.Folder, ignoreCase: true);
|
||||
Assert.Equal("*.log", target.Pattern);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Profile_path_without_the_token_stays_blocked()
|
||||
{
|
||||
AssertRefused(TestProfile + @"\AppData\Local\RealmJoin\Logs\*.log", userProfilePath: null);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ recursion guard
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"C:\Users")]
|
||||
[InlineData(@"C:\Users\TestUser")]
|
||||
[InlineData(@"C:\Windows\System32\config")]
|
||||
[InlineData(@"C:\Windows\System32\config\RegBack")]
|
||||
public void Recursion_never_enters_a_hard_blocked_subtree_in_unrestricted_mode(string directory)
|
||||
{
|
||||
Assert.False(DiagnosticsPathGuards.IsDirectoryEnterable(directory, unrestrictedMode: true, userProfilePath: null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Recursion_enters_ordinary_subfolders()
|
||||
{
|
||||
Assert.True(DiagnosticsPathGuards.IsDirectoryEnterable(@"C:\Windows\System32\drivers", unrestrictedMode: true, userProfilePath: null));
|
||||
Assert.True(DiagnosticsPathGuards.IsDirectoryEnterable(@"C:\Windows\Logs\CBS", unrestrictedMode: false, userProfilePath: null));
|
||||
Assert.False(DiagnosticsPathGuards.IsDirectoryEnterable(@"C:\Windows\System32\drivers", unrestrictedMode: false, userProfilePath: null));
|
||||
}
|
||||
}
|
||||
}
|
||||
+119
@@ -456,5 +456,124 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Gather
|
||||
|
||||
SingleSecurityWarning();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// %LOGGED_ON_USER_PROFILE% exception (userProfilePath overload)
|
||||
//
|
||||
// The token lifts the C:\Users hard block for AppData\Local and
|
||||
// AppData\Roaming — and nothing more. The allowlist still decides,
|
||||
// which is what the cases below pin down: the exception is a gate the
|
||||
// allowlist has to open, never an allowance of its own.
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
private const string TestProfile = @"C:\Users\TestUser";
|
||||
|
||||
[Fact]
|
||||
public void UserProfile_AppDataPath_WithoutAllowlistEntry_IsStillBlocked()
|
||||
{
|
||||
// The lifted hard block alone admits nothing: without a matching
|
||||
// userProfileFilePrefixes entry the path falls through to the
|
||||
// allowlist and is refused there.
|
||||
Assert.False(GatherRuleGuards.IsFilePathAllowed(
|
||||
TestProfile + @"\AppData\Local\UnlistedVendor\app.log",
|
||||
unrestrictedMode: false, userProfilePath: TestProfile));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserProfile_OutsideAppData_IsBlockedByTheHardBlock()
|
||||
{
|
||||
foreach (var target in new[] { @"\Desktop\secret.txt", @"\Documents\notes.txt", @"\ntuser.dat" })
|
||||
{
|
||||
Assert.False(GatherRuleGuards.IsFilePathAllowed(
|
||||
TestProfile + target, unrestrictedMode: false, userProfilePath: TestProfile));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserProfile_NullProfile_KeepsTheUsersHardBlock()
|
||||
{
|
||||
// No interactive user detected — the exception cannot apply and the
|
||||
// path stays under the plain C:\Users block.
|
||||
Assert.False(GatherRuleGuards.IsFilePathAllowed(
|
||||
@"C:\Users\TestUser\AppData\Local\UnlistedVendor\app.log",
|
||||
unrestrictedMode: false, userProfilePath: null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserProfile_TraversalOutOfAppData_IsBlocked()
|
||||
{
|
||||
// Spelling Documents as a traversal out of AppData must not survive
|
||||
// Path.GetFullPath normalisation.
|
||||
const string traversal = @"C:\Users\TestUser\AppData\Local\..\..\Documents\secret.txt";
|
||||
|
||||
Assert.Equal(@"C:\Users\TestUser\Documents\secret.txt", Path.GetFullPath(traversal));
|
||||
Assert.False(GatherRuleGuards.IsFilePathAllowed(
|
||||
traversal, unrestrictedMode: false, userProfilePath: TestProfile));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserProfile_ForeignProfile_IsBlockedEvenUnderAppData()
|
||||
{
|
||||
// The exception is scoped to the DETECTED profile — a rule must not
|
||||
// reach a second user's AppData by spelling the path out.
|
||||
Assert.False(GatherRuleGuards.IsFilePathAllowed(
|
||||
@"C:\Users\OtherUser\AppData\Local\UnlistedVendor\app.log",
|
||||
unrestrictedMode: false, userProfilePath: TestProfile));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserProfile_AllowlistedVendorFolder_IsAllowed()
|
||||
{
|
||||
// AppData\Local\RealmJoin is on userProfileFilePrefixes: the file itself, a
|
||||
// subfolder below it, and the folder itself all pass.
|
||||
foreach (var target in new[] { @"\tray.log", @"\Logs\rj.log", "" })
|
||||
{
|
||||
Assert.True(GatherRuleGuards.IsFilePathAllowed(
|
||||
TestProfile + @"\AppData\Local\RealmJoin" + target,
|
||||
unrestrictedMode: false, userProfilePath: TestProfile),
|
||||
$"expected allowed: ...\\RealmJoin{target}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserProfile_SiblingOfAnAllowlistedVendorFolder_IsBlocked()
|
||||
{
|
||||
// Segment-bounded: "RealmJoin" must not admit "RealmJoinSomethingElse".
|
||||
Assert.False(GatherRuleGuards.IsFilePathAllowed(
|
||||
TestProfile + @"\AppData\Local\RealmJoinSomethingElse\tray.log",
|
||||
unrestrictedMode: false, userProfilePath: TestProfile));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserProfile_AllowlistedVendorFolder_InAForeignProfile_IsBlocked()
|
||||
{
|
||||
// The allowlist is anchored to the DETECTED profile, not to C:\Users\*.
|
||||
Assert.False(GatherRuleGuards.IsFilePathAllowed(
|
||||
@"C:\Users\OtherUser\AppData\Local\RealmJoin\tray.log",
|
||||
unrestrictedMode: false, userProfilePath: TestProfile));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserProfile_AllowlistedVendorFolder_WithoutADetectedUser_IsBlocked()
|
||||
{
|
||||
Assert.False(GatherRuleGuards.IsFilePathAllowed(
|
||||
@"C:\Users\TestUser\AppData\Local\RealmJoin\tray.log",
|
||||
unrestrictedMode: false, userProfilePath: null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserProfile_AppDataPath_IsAllowedInUnrestrictedMode()
|
||||
{
|
||||
// Where the exception does bite today: unrestricted mode returns
|
||||
// before the allowlist, so the lifted hard block is the whole story.
|
||||
Assert.True(GatherRuleGuards.IsFilePathAllowed(
|
||||
TestProfile + @"\AppData\Local\UnlistedVendor\app.log",
|
||||
unrestrictedMode: true, userProfilePath: TestProfile));
|
||||
|
||||
// ...but only under AppData — Documents stays blocked even there.
|
||||
Assert.False(GatherRuleGuards.IsFilePathAllowed(
|
||||
TestProfile + @"\Documents\secret.txt",
|
||||
unrestrictedMode: true, userProfilePath: TestProfile));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+537
@@ -0,0 +1,537 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutopilotMonitor.Agent.V2.Core.Logging;
|
||||
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime;
|
||||
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
|
||||
using AutopilotMonitor.Shared.Models;
|
||||
using Newtonsoft.Json;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
{
|
||||
/// <summary>
|
||||
/// Session 46749560: IME's trace listener writes every log through one FileMode.Append
|
||||
/// stream per process, so a process that resumes logging after another one appended
|
||||
/// overwrites those bytes at its own stale position. The tracker had read the first version
|
||||
/// and never saw the second — 6 of 16 script results and 4 of 16 executor end blocks were
|
||||
/// lost while the final files looked intact. These tests drive that exact write pattern
|
||||
/// (positional overwrite behind the bookmark) against the two multi-writer logs and pin the
|
||||
/// contract: unchanged bytes never rewind, changed bytes are processed exactly once, and the
|
||||
/// executor lines a rewind uncovers belong to the invocation that owns their file position.
|
||||
/// </summary>
|
||||
public sealed class ImeLogTrackerOverwriteRewindTests
|
||||
{
|
||||
private static readonly DateTime T0 = new DateTime(2026, 9, 10, 19, 30, 0, DateTimeKind.Utc);
|
||||
|
||||
private static List<ImeLogPattern> MarkerPatterns() => new List<ImeLogPattern>
|
||||
{
|
||||
new ImeLogPattern
|
||||
{
|
||||
PatternId = "T-MARK", Category = "always", Enabled = true,
|
||||
Pattern = @"^marker (?<n>\d+)", Action = "noop",
|
||||
Parameters = new Dictionary<string, string>(),
|
||||
},
|
||||
};
|
||||
|
||||
private static readonly string[] ScriptPatternIds =
|
||||
{
|
||||
"PS-AGENT-INVOCATION", "PS-AGENT-ARG", "PS-AGENT-SCRIPT-START", "PS-AGENT-EXITCODE",
|
||||
"PS-AGENT-OUTPUT", "PS-AGENT-COMPLETED", "PS-SCRIPT-GENERATED", "PS-SCRIPT-CONTEXT", "PS-SCRIPT-RESULT",
|
||||
};
|
||||
|
||||
/// <summary>The shipped pattern JSON — the same source combine.js embeds — so the contract is the real one.</summary>
|
||||
private static List<ImeLogPattern> ScriptPatterns()
|
||||
{
|
||||
DirectoryInfo? dir = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
string? patternDir = null;
|
||||
while (dir != null)
|
||||
{
|
||||
var candidate = Path.Combine(dir.FullName, "rules", "ime-log-patterns");
|
||||
if (Directory.Exists(candidate)) { patternDir = candidate; break; }
|
||||
dir = dir.Parent;
|
||||
}
|
||||
Assert.NotNull(patternDir);
|
||||
|
||||
var byId = new Dictionary<string, ImeLogPattern>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var file in Directory.GetFiles(patternDir, "*.json"))
|
||||
{
|
||||
var pattern = JsonConvert.DeserializeObject<ImeLogPattern>(File.ReadAllText(file));
|
||||
if (pattern?.PatternId != null) byId[pattern.PatternId] = pattern;
|
||||
}
|
||||
return ScriptPatternIds.Select(id =>
|
||||
{
|
||||
Assert.True(byId.TryGetValue(id, out var p), $"Shipped IME pattern '{id}' not found under {patternDir}.");
|
||||
return p!;
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
private static string Entry(string message, string thread = "1")
|
||||
=> $"<![LOG[{message}]LOG]!><time=\"12:00:00.0000000\" date=\"9-10-2026\" component=\"X\" context=\"\" type=\"1\" thread=\"{thread}\" file=\"\">\r\n";
|
||||
|
||||
private const string PlatformId = "d39eeef3-e88e-41a0-82d7-5e28ee7b5acf";
|
||||
private const string PlatformStartLine =
|
||||
@"Adding argument powershell with value C:\Program Files (x86)\Microsoft Intune Management Extension\Policies\Scripts\73d664e4-0886-4a73-b745-c694da45ddb4_d39eeef3-e88e-41a0-82d7-5e28ee7b5acf.ps1 to the named argument list.";
|
||||
private const string DetectionStartLine =
|
||||
@"Adding argument detectionScript with value C:\Windows\IMECache\DetectionScripts\App_cc46aa62-e93a-41a1-bdd0-44f57e92a66d.ps1 to the named argument list.";
|
||||
private const string GeneratedLine =
|
||||
@"Script file C:\Program Files (x86)\Microsoft Intune Management Extension\Policies\Scripts\73d664e4-0886-4a73-b745-c694da45ddb4_d39eeef3-e88e-41a0-82d7-5e28ee7b5acf.ps1 is generated.";
|
||||
private const string ResultLine =
|
||||
"[PowerShell] User Id = 73d664e4-0886-4a73-b745-c694da45ddb4, Policy id = " + PlatformId + ", policy result = Success";
|
||||
|
||||
private sealed class Harness : IDisposable
|
||||
{
|
||||
private readonly TempDirectory _tmp = new TempDirectory();
|
||||
private readonly string _matchLog;
|
||||
private readonly List<ImeLogPattern> _patterns;
|
||||
public ImeLogTracker Tracker { get; private set; }
|
||||
public DateTime Now { get; set; } = T0;
|
||||
public string LogName { get; }
|
||||
public string LogPath => Path.Combine(_tmp.Path, LogName);
|
||||
public string StateDir { get; }
|
||||
public List<ScriptExecutionState> Completed { get; } = new List<ScriptExecutionState>();
|
||||
|
||||
public Harness(string logName, List<ImeLogPattern> patterns)
|
||||
{
|
||||
LogName = logName;
|
||||
_patterns = patterns;
|
||||
var logDir = Path.Combine(_tmp.Path, "agent");
|
||||
Directory.CreateDirectory(logDir);
|
||||
_matchLog = Path.Combine(logDir, "ime-pattern-matches.log");
|
||||
StateDir = Path.Combine(_tmp.Path, "state");
|
||||
Tracker = Build();
|
||||
}
|
||||
|
||||
private ImeLogTracker Build()
|
||||
{
|
||||
var t = new ImeLogTracker(_tmp.Path, _patterns, new AgentLogger(Path.Combine(_tmp.Path, "agent"), AgentLogLevel.Debug),
|
||||
matchLogPath: _matchLog, stateDirectory: StateDir);
|
||||
t.UtcNowProvider = () => Now;
|
||||
t.OnScriptCompleted = s => Completed.Add(s);
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>Agent restart: dispose, rebuild over the same state directory, restore the bookmark.</summary>
|
||||
public void Restart()
|
||||
{
|
||||
Tracker.SaveStateForTest();
|
||||
Tracker.Dispose();
|
||||
Tracker = Build();
|
||||
Tracker.LoadStateForTest();
|
||||
}
|
||||
|
||||
public void Append(string text) => File.AppendAllText(LogPath, text, new UTF8Encoding(false));
|
||||
|
||||
/// <summary>What IME's stale writer does: write at its own position, over whatever is there.</summary>
|
||||
public void Overwrite(long offset, string text)
|
||||
{
|
||||
using (var fs = new FileStream(LogPath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
|
||||
{
|
||||
fs.Seek(offset, SeekOrigin.Begin);
|
||||
var bytes = new UTF8Encoding(false).GetBytes(text);
|
||||
fs.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public long Length => new FileInfo(LogPath).Length;
|
||||
|
||||
public Task Pass(CancellationToken token = default) => Tracker.CheckLogFilesAsync(token);
|
||||
|
||||
/// <summary>Marker numbers matched so far, in match order.</summary>
|
||||
public List<int> Matched()
|
||||
{
|
||||
if (!File.Exists(_matchLog)) return new List<int>();
|
||||
return File.ReadAllLines(_matchLog)
|
||||
.Select(l => Regex.Match(l, @"\[T-MARK\] <!\[LOG\[marker (\d+)"))
|
||||
.Where(m => m.Success).Select(m => int.Parse(m.Groups[1].Value)).ToList();
|
||||
}
|
||||
|
||||
public ImeTrackerHealth Health => Tracker.GetHealthSnapshot();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Tracker.Dispose();
|
||||
_tmp.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Marker-level contract on AgentExecutor.log
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
[Fact]
|
||||
public async Task Unchanged_bytes_never_rewind_however_often_they_are_verified()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
h.Append(Entry("marker 1") + "<![LOG[marker 2\nsecond line\n]LOG]!><time=\"12:00:00.0000000\" date=\"9-10-2026\" component=\"X\" context=\"\" type=\"1\" thread=\"1\" file=\"\">\r\n" + Entry("marker 3"));
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2, 3 }, h.Matched());
|
||||
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
h.Tracker.RequestOverwriteCheckForTest();
|
||||
h.Now = h.Now.AddSeconds(2);
|
||||
await h.Pass();
|
||||
}
|
||||
|
||||
Assert.Equal(new[] { 1, 2, 3 }, h.Matched());
|
||||
Assert.Equal(0, h.Health.OverwriteRewinds);
|
||||
Assert.Equal(5, h.Health.VerifyPasses);
|
||||
Assert.Equal(3, h.Tracker.LedgerEntryCountForTest("AgentExecutor.log"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Non_multi_writer_files_keep_no_ledger_and_are_never_verified()
|
||||
{
|
||||
using var h = new Harness("AppWorkload.log", MarkerPatterns());
|
||||
h.Append(Entry("marker 1") + Entry("marker 2"));
|
||||
await h.Pass();
|
||||
h.Tracker.RequestOverwriteCheckForTest();
|
||||
h.Now = h.Now.AddSeconds(2);
|
||||
await h.Pass();
|
||||
|
||||
Assert.Equal(new[] { 1, 2 }, h.Matched());
|
||||
Assert.Equal(0, h.Tracker.LedgerEntryCountForTest("AppWorkload.log"));
|
||||
Assert.Equal(0, h.Health.VerifyPasses);
|
||||
Assert.Equal(0, h.Health.VerifiedBytes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Block_written_over_read_bytes_is_processed_once_and_the_surviving_tail_is_not_repeated()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
var one = Entry("marker 1");
|
||||
h.Append(one + Entry("marker 2") + Entry("marker 3"));
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2, 3 }, h.Matched());
|
||||
|
||||
// A stale writer lays two entries over the first two (same length: marker 3 survives
|
||||
// intact at its old offset). No growth, no fragment — only the end signal finds it.
|
||||
h.Overwrite(0, Entry("marker 7") + Entry("marker 8"));
|
||||
Assert.Equal(3 * one.Length, h.Length);
|
||||
h.Tracker.RequestOverwriteCheckForTest();
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
|
||||
Assert.Equal(new[] { 1, 2, 3, 7, 8 }, h.Matched());
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
Assert.Equal(3 * one.Length, h.Health.OverwriteBytesReprocessed);
|
||||
Assert.Equal(3, h.Tracker.LedgerEntryCountForTest("AgentExecutor.log"));
|
||||
|
||||
// Nothing left to find.
|
||||
h.Tracker.RequestOverwriteCheckForTest();
|
||||
h.Now = h.Now.AddSeconds(2);
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2, 3, 7, 8 }, h.Matched());
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Block_that_cuts_an_old_entry_leaves_a_fragment_that_is_never_matched()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
h.Append(Entry("marker 1") + Entry("marker 2") + Entry("marker 3"));
|
||||
await h.Pass();
|
||||
|
||||
// Longer than two old entries, shorter than three: the tail of marker 3's line survives
|
||||
// as a fragment behind the new block.
|
||||
h.Overwrite(0, Entry("marker 7") + Entry("marker 8 is a little longer"));
|
||||
h.Tracker.RequestOverwriteCheckForTest();
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
|
||||
Assert.Equal(new[] { 1, 2, 3, 7, 8 }, h.Matched());
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Guard_zone_catches_a_short_block_when_the_file_grows_without_any_signal()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
h.Append(Entry("marker 1") + Entry("marker 2") + Entry("marker 3"));
|
||||
await h.Pass();
|
||||
|
||||
// Same-length replacement of marker 1 (no fragment), then an ordinary append by
|
||||
// another writer: the growth pass re-checks the last 4 KB and finds the change.
|
||||
h.Overwrite(0, Entry("marker 7"));
|
||||
h.Append(Entry("marker 4"));
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
|
||||
Assert.Equal(new[] { 1, 2, 3, 7, 4 }, h.Matched());
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
Assert.Equal(1, h.Health.VerifyPasses); // the full check that follows a guard hit
|
||||
Assert.True(h.Health.VerifiedBytes > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Guard_hit_rewinds_to_the_true_start_of_the_block_not_the_guard_zone()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
// ~6 KB of entries so the guard zone covers only the last few.
|
||||
var sb = new StringBuilder();
|
||||
for (var i = 1; i <= 60; i++) sb.Append(Entry($"marker {i}"));
|
||||
h.Append(sb.ToString());
|
||||
await h.Pass();
|
||||
Assert.Equal(60, h.Matched().Count);
|
||||
|
||||
// A block over everything plus one more entry: the guard sees a change at its
|
||||
// zone start, the full check must place the rewind at offset 0.
|
||||
var block = new StringBuilder();
|
||||
for (var i = 101; i <= 161; i++) block.Append(Entry($"marker {i}"));
|
||||
h.Overwrite(0, block.ToString());
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
|
||||
var matched = h.Matched();
|
||||
Assert.Equal(60 + 61, matched.Count);
|
||||
Assert.Equal(Enumerable.Range(101, 61), matched.Skip(60));
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Fragment_at_the_bookmark_triggers_the_check_when_no_ledger_entry_starts_in_the_guard_zone()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
var one = Entry("marker 1");
|
||||
var big = Entry("marker 2 " + new string('x', 6000));
|
||||
h.Append(one + big);
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2 }, h.Matched());
|
||||
|
||||
// The 6 KB entry is the last ledger entry and starts before the 4 KB guard zone, so
|
||||
// the guard has nothing to check. The stale writer's block is longer than that entry:
|
||||
// the pass after it starts mid-line — a fragment — and must not match it raw.
|
||||
h.Overwrite(one.Length, Entry("marker 7 " + new string('y', 3000)) + Entry("marker 8 " + new string('z', 3100)));
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2 }, h.Matched());
|
||||
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2, 7, 8 }, h.Matched());
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Hidden_block_is_found_by_the_cadence_while_a_script_is_in_flight_and_the_cadence_is_throttled()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
h.Append(Entry("marker 1") + Entry("marker 2") + Entry("marker 3"));
|
||||
await h.Pass();
|
||||
|
||||
// A platform script is pending → contention. First pass in contention verifies at once.
|
||||
h.Tracker.SeedPendingPlatformScriptForTesting("policyX", exitCode: null, exitObservedAtUtc: null, startedAtUtc: T0);
|
||||
h.Now = T0.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
Assert.Equal(1, h.Health.VerifyPasses);
|
||||
|
||||
// Same-length replacement, no growth, no signal: only the cadence can find it —
|
||||
// and not before a second has passed since the last verification.
|
||||
h.Overwrite(0, Entry("marker 7"));
|
||||
h.Now = T0.AddMilliseconds(500);
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2, 3 }, h.Matched());
|
||||
Assert.Equal(1, h.Health.VerifyPasses);
|
||||
|
||||
h.Now = T0.AddMilliseconds(1200);
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2, 3, 7 }, h.Matched());
|
||||
Assert.Equal(2, h.Health.VerifyPasses);
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Without_contention_or_signal_nothing_is_verified()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
h.Append(Entry("marker 1") + Entry("marker 2"));
|
||||
await h.Pass();
|
||||
|
||||
for (var i = 0; i < 20; i++)
|
||||
{
|
||||
h.Now = h.Now.AddSeconds(1);
|
||||
await h.Pass();
|
||||
}
|
||||
|
||||
Assert.Equal(0, h.Health.VerifyPasses);
|
||||
Assert.Equal(0, h.Health.VerifiedBytes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Restart_seeds_the_ledger_so_an_overwrite_after_the_restart_is_still_found()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
h.Append(Entry("marker 1") + Entry("marker 2") + Entry("marker 3"));
|
||||
await h.Pass();
|
||||
h.Restart();
|
||||
Assert.Equal(0, h.Tracker.LedgerEntryCountForTest("AgentExecutor.log"));
|
||||
|
||||
// First pass after the restart: nothing new to read, but the ledger is seeded from
|
||||
// the bytes behind the persisted bookmark.
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
Assert.Equal(3, h.Tracker.LedgerEntryCountForTest("AgentExecutor.log"));
|
||||
|
||||
h.Overwrite(0, Entry("marker 7"));
|
||||
h.Tracker.RequestOverwriteCheckForTest();
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
|
||||
Assert.Equal(new[] { 1, 2, 3, 7 }, h.Matched());
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
|
||||
// The counter is part of the persisted health.
|
||||
h.Restart();
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Rollover_clears_the_ledger_and_the_new_file_is_read_without_a_false_rewind()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
h.Append(Entry("marker 1") + Entry("marker 2") + Entry("marker 3"));
|
||||
await h.Pass();
|
||||
|
||||
File.WriteAllText(h.LogPath, Entry("marker 7") + Entry("marker 8"), new UTF8Encoding(false));
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2, 3, 7, 8 }, h.Matched());
|
||||
|
||||
h.Tracker.RequestOverwriteCheckForTest();
|
||||
h.Now = h.Now.AddSeconds(2);
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2, 3, 7, 8 }, h.Matched());
|
||||
Assert.Equal(0, h.Health.OverwriteRewinds);
|
||||
Assert.Equal(2, h.Tracker.LedgerEntryCountForTest("AgentExecutor.log"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Cancelled_pass_does_not_lose_a_pending_check()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", MarkerPatterns());
|
||||
h.Append(Entry("marker 1") + Entry("marker 2") + Entry("marker 3"));
|
||||
await h.Pass();
|
||||
h.Overwrite(0, Entry("marker 7") + Entry("marker 8"));
|
||||
h.Tracker.RequestOverwriteCheckForTest();
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
|
||||
using (var cts = new CancellationTokenSource())
|
||||
{
|
||||
cts.Cancel();
|
||||
await h.Pass(cts.Token);
|
||||
}
|
||||
|
||||
// The cancelled pass touched nothing after the rewind; the next one reads it all.
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
Assert.Equal(new[] { 1, 2, 3, 7, 8 }, h.Matched());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// The field scenario with the shipped patterns
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
[Fact]
|
||||
public async Task Executor_end_block_written_over_a_detection_invocation_belongs_to_its_own_script()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
|
||||
|
||||
// Executor E starts its platform script and waits for PowerShell.
|
||||
h.Append(Entry("ExecutorLog AgentExecutor gets invoked") + Entry(PlatformStartLine) + Entry("[Executor] created powershell with process id 12708"));
|
||||
await h.Pass();
|
||||
var executorPosition = h.Length;
|
||||
|
||||
// Detection executor D appends its whole run while E waits — the tracker reads it.
|
||||
var detectionBlock = Entry("ExecutorLog AgentExecutor gets invoked") + Entry(DetectionStartLine)
|
||||
+ Entry("Powershell exit code is 3") + Entry("write output done. output = detected nothing, error = ")
|
||||
+ Entry("Agent executor completed.") + Entry(@"Writing result: False to resultFilePath: C:\x\App_cc46aa62.txt");
|
||||
h.Append(detectionBlock);
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
|
||||
// E ends: its end block lands at its own stale position, over D's lines.
|
||||
var endBlock = Entry("Powershell exit code is 0") + Entry("write output done. output = Hello from E, error = ") + Entry("Agent executor completed.");
|
||||
Assert.True(endBlock.Length < detectionBlock.Length);
|
||||
h.Overwrite(executorPosition, endBlock);
|
||||
|
||||
// Nothing visible signalled the end; the cadence (E is pending) finds the block.
|
||||
h.Now = h.Now.AddSeconds(1.5);
|
||||
await h.Pass();
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
|
||||
// IME's result line (in the other file) completes the script with E's own data.
|
||||
h.Tracker.ProcessLogMessageForTest(ResultLine);
|
||||
var script = Assert.Single(h.Completed);
|
||||
Assert.Equal(PlatformId, script.PolicyId);
|
||||
Assert.Equal("Success", script.Result);
|
||||
Assert.Equal("ime_policy_result", script.ResultSource);
|
||||
Assert.Equal(0, script.ExitCode);
|
||||
Assert.Equal("Hello from E", script.Stdout);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Detection_exit_lines_read_before_the_overwrite_never_reach_the_platform_script()
|
||||
{
|
||||
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
|
||||
h.Append(Entry("ExecutorLog AgentExecutor gets invoked") + Entry(PlatformStartLine) + Entry("[Executor] created powershell with process id 1"));
|
||||
await h.Pass();
|
||||
h.Append(Entry("ExecutorLog AgentExecutor gets invoked") + Entry(DetectionStartLine)
|
||||
+ Entry("Powershell exit code is 3") + Entry("write output done. output = detected nothing, error = ") + Entry("Agent executor completed."));
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
|
||||
// The platform result arrives without any end block ever becoming visible: held for
|
||||
// the grace, then emitted — the detection's exit code 3 must not stand in.
|
||||
h.Tracker.ProcessLogMessageForTest(ResultLine);
|
||||
Assert.Empty(h.Completed);
|
||||
h.Now = h.Now.Add(ImeLogTracker.PlatformScriptEndBlockGrace);
|
||||
h.Tracker.FlushPendingPlatformScriptResults(h.Now);
|
||||
var script = Assert.Single(h.Completed);
|
||||
Assert.Equal("Success", script.Result);
|
||||
Assert.Null(script.ExitCode);
|
||||
Assert.Null(script.Stdout);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ime_result_line_hidden_by_an_executor_start_line_is_recovered_from_the_ime_log()
|
||||
{
|
||||
using var h = new Harness("IntuneManagementExtension.log", ScriptPatterns());
|
||||
h.Append(Entry(GeneratedLine, "17") + Entry("Launch powershell executor in user session", "17") + Entry("process id = 10608", "17"));
|
||||
await h.Pass();
|
||||
var servicePosition = h.Length;
|
||||
|
||||
// The executor process appends its telemetry start line at the true end of file.
|
||||
h.Append(Entry("[Telemetry] Telemetry is enabled. Starting mananger...", "1"));
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
Assert.Empty(h.Completed);
|
||||
|
||||
// The service's first write after the script wait lands at ITS position — over the
|
||||
// executor's line; the block is longer, so the pass sees a fragment at the bookmark.
|
||||
h.Overwrite(servicePosition, Entry("Execution is done, collecting result", "17") + Entry(ResultLine, "17") + Entry("[PowerShell] Success, Result details length: 315", "17"));
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
h.Now = h.Now.AddMilliseconds(100);
|
||||
await h.Pass();
|
||||
Assert.Equal(1, h.Health.OverwriteRewinds);
|
||||
|
||||
// Recovered on the IME side alone (this harness has no AgentExecutor.log): held for
|
||||
// the end block, emitted without an exit code after the grace.
|
||||
Assert.Empty(h.Completed);
|
||||
h.Now = h.Now.Add(ImeLogTracker.PlatformScriptEndBlockGrace);
|
||||
h.Tracker.FlushPendingPlatformScriptResults(h.Now);
|
||||
|
||||
var script = Assert.Single(h.Completed);
|
||||
Assert.Equal(PlatformId, script.PolicyId);
|
||||
Assert.Equal("Success", script.Result);
|
||||
Assert.Equal("ime_policy_result", script.ResultSource);
|
||||
Assert.Equal("User", script.RunContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
+121
-3
@@ -36,8 +36,9 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
// silently passing against a stale inline copy.
|
||||
private static readonly string[] RequiredPatternIds =
|
||||
{
|
||||
"PS-AGENT-INVOCATION", "PS-AGENT-SCRIPT-START", "PS-AGENT-EXITCODE",
|
||||
"PS-AGENT-OUTPUT", "PS-SCRIPT-RESULT",
|
||||
"PS-AGENT-INVOCATION", "PS-AGENT-ARG", "PS-AGENT-SCRIPT-START", "PS-AGENT-EXITCODE",
|
||||
"PS-AGENT-OUTPUT", "PS-AGENT-COMPLETED", "PS-SCRIPT-GENERATED", "PS-SCRIPT-CONTEXT",
|
||||
"PS-SCRIPT-RESULT", "HS-INVOCATION",
|
||||
};
|
||||
|
||||
private static List<ImeLogPattern> ScriptPatterns()
|
||||
@@ -83,6 +84,24 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
private const string RemediationStartLine =
|
||||
@"Adding argument remediationScript with value C:\Windows\IMECache\HealthScripts\446f0450-d0ee-404c-8dc0-a74123bde31f_1\detect.ps1 to the named argument list.";
|
||||
|
||||
private const string ImeLog = "IntuneManagementExtension.log";
|
||||
private const string HealthLog = "HealthScripts.log";
|
||||
private const string ExecutorLog = "AgentExecutor.log";
|
||||
|
||||
private const string PlatformGeneratedLine =
|
||||
@"Script file C:\Program Files (x86)\Microsoft Intune Management Extension\Policies\Scripts\00000000-0000-0000-0000-000000000000_c3e0124c-4936-4bfd-afcc-c7fe1d84d104.ps1 is generated";
|
||||
|
||||
private static readonly string PlatformResultLine =
|
||||
$"[PowerShell] User Id = 00000000-0000-0000-0000-000000000000, Policy id = {PlatformId}, policy result = Success";
|
||||
|
||||
// The health-script worker (IME AgentCommon ScriptWorker) writes its command line, the launch
|
||||
// line and the exit line to IntuneManagementExtension.log AND HealthScripts.log — session
|
||||
// 24dc69d1 (IME 1.105.152.0), where the exit line pre-filled a pending platform script's exit
|
||||
// code and the launch line its context.
|
||||
private const string HealthScriptCommandLine =
|
||||
@"""C:\Program Files (x86)\Microsoft Intune Management Extension\agentexecutor.exe"" -remediationScript """"C:\WINDOWS\IMECache\HealthScripts\446f0450-d0ee-404c-8dc0-a74123bde31f_1\detect.ps1"""" ""C:\WINDOWS\IMECache\HealthScripts\446f0450-d0ee-404c-8dc0-a74123bde31f_1\detect.ps1.result"" 60";
|
||||
private const string HealthScriptExitLine = "Powershell execution is done, exitCode = 5";
|
||||
|
||||
private static ImeLogTracker BuildTracker(TempDirectory tmp, out List<ScriptExecutionState> emitted)
|
||||
{
|
||||
var captured = new List<ScriptExecutionState>();
|
||||
@@ -111,9 +130,13 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
tracker.ProcessLogMessageForTest("Powershell exit code is 0");
|
||||
tracker.ProcessLogMessageForTest("write output done. output = [Compliant] No Classic Teams found, error = ");
|
||||
|
||||
// Authoritative IME result for the PLATFORM script (keyed by policyId).
|
||||
// Authoritative IME result for the PLATFORM script (keyed by policyId). Its own end
|
||||
// block never comes here, so the result is held until the shutdown flush — which must
|
||||
// not pick up the remediation's exit/output either.
|
||||
tracker.ProcessLogMessageForTest(
|
||||
$"[PowerShell] User Id = 00000000-0000-0000-0000-000000000000, Policy id = {PlatformId}, policy result = Failed");
|
||||
Assert.Empty(emitted);
|
||||
tracker.FlushPendingPlatformScriptResults(System.DateTime.UtcNow, force: true);
|
||||
|
||||
var script = Assert.Single(emitted);
|
||||
Assert.Equal(PlatformId, script.PolicyId);
|
||||
@@ -145,5 +168,100 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
Assert.Equal(0, script.ExitCode);
|
||||
Assert.Equal("Hello from c3e0124c", script.Stdout);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Health-script lines never feed a platform slot (session 24dc69d1)
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
[Fact]
|
||||
public void No_shipped_pattern_reads_the_health_script_workers_exit_line()
|
||||
{
|
||||
// "Powershell execution is done, exitCode = N" is the health-script worker's line only;
|
||||
// as PS-SCRIPT-EXITCODE (no scriptType) it pre-filled the exit code of whichever platform
|
||||
// script was pending, which then emitted without waiting for its own end block.
|
||||
foreach (var file in Directory.GetFiles(FindRulesPatternDir(), "*.json"))
|
||||
{
|
||||
var pattern = JsonConvert.DeserializeObject<ImeLogPattern>(File.ReadAllText(file))!;
|
||||
Assert.False(System.Text.RegularExpressions.Regex.IsMatch(HealthScriptExitLine, pattern.Pattern),
|
||||
$"{pattern.PatternId} matches the health-script worker's exit line");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Health_script_exit_line_in_the_ime_log_never_pre_fills_the_platform_slot()
|
||||
{
|
||||
using var tmp = new TempDirectory();
|
||||
var tracker = BuildTracker(tmp, out var emitted);
|
||||
|
||||
tracker.ProcessLogMessageForTest(PlatformGeneratedLine, sourceFileName: ImeLog);
|
||||
// A health script that ran in parallel finishes: its exit line lands inside the platform
|
||||
// run's window (c81b8053 had "exit code 0" before its executor even started).
|
||||
tracker.ProcessLogMessageForTest(HealthScriptExitLine, sourceFileName: ImeLog);
|
||||
tracker.ProcessLogMessageForTest(PlatformResultLine, sourceFileName: ImeLog);
|
||||
|
||||
// No exit code of its own → held for the end block, never emitted with the health script's 5.
|
||||
Assert.Empty(emitted);
|
||||
tracker.FlushPendingPlatformScriptResults(DateTime.UtcNow, force: true);
|
||||
var script = Assert.Single(emitted);
|
||||
Assert.Null(script.ExitCode);
|
||||
Assert.Equal("Success", script.Result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Health_script_launch_line_in_the_ime_log_does_not_flip_the_platform_context()
|
||||
{
|
||||
using var tmp = new TempDirectory();
|
||||
var tracker = BuildTracker(tmp, out var emitted);
|
||||
|
||||
tracker.ProcessLogMessageForTest(PlatformGeneratedLine, sourceFileName: ImeLog);
|
||||
tracker.ProcessLogMessageForTest("Launch powershell executor in user session", sourceFileName: ImeLog);
|
||||
// The health-script worker launches its executor while the platform script runs: its
|
||||
// command line opens an invocation that owns nothing, so its launch line stays there.
|
||||
tracker.ProcessLogMessageForTest(HealthScriptCommandLine, sourceFileName: ImeLog);
|
||||
tracker.ProcessLogMessageForTest("Launch powershell executor in machine session", sourceFileName: ImeLog);
|
||||
tracker.ProcessLogMessageForTest(PlatformResultLine, sourceFileName: ImeLog);
|
||||
tracker.FlushPendingPlatformScriptResults(DateTime.UtcNow, force: true);
|
||||
|
||||
var script = Assert.Single(emitted);
|
||||
Assert.Equal("User", script.RunContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Lines_from_the_health_scripts_log_never_resolve_to_a_platform_script()
|
||||
{
|
||||
using var tmp = new TempDirectory();
|
||||
var tracker = BuildTracker(tmp, out var emitted);
|
||||
|
||||
tracker.ProcessLogMessageForTest("ExecutorLog AgentExecutor gets invoked", sourceFileName: ExecutorLog);
|
||||
tracker.ProcessLogMessageForTest(PlatformStartLine, sourceFileName: ExecutorLog);
|
||||
// HealthScripts.log never carries a platform marker — the run in flight is not the
|
||||
// fallback owner of the lines there.
|
||||
tracker.ProcessLogMessageForTest("Launch powershell executor in machine session", sourceFileName: HealthLog);
|
||||
tracker.ProcessLogMessageForTest(PlatformResultLine, sourceFileName: ImeLog);
|
||||
tracker.FlushPendingPlatformScriptResults(DateTime.UtcNow, force: true);
|
||||
|
||||
var script = Assert.Single(emitted);
|
||||
Assert.Null(script.RunContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Context_line_after_an_ime_log_rollover_still_finds_the_run_in_flight()
|
||||
{
|
||||
using var tmp = new TempDirectory();
|
||||
var tracker = BuildTracker(tmp, out var emitted);
|
||||
|
||||
tracker.ProcessLogMessageForTest(PlatformGeneratedLine, sourceFileName: ImeLog);
|
||||
tracker.ProcessLogMessageForTest("ExecutorLog AgentExecutor gets invoked", sourceFileName: ExecutorLog);
|
||||
tracker.ProcessLogMessageForTest(PlatformStartLine, sourceFileName: ExecutorLog);
|
||||
// IME rolls the log over between its two lines: the fresh file has no markers, but
|
||||
// platform runs open there — the run in flight still owns the context line.
|
||||
tracker.ClearInvocationMarkersForTest(ImeLog);
|
||||
tracker.ProcessLogMessageForTest("Launch powershell executor in user session", sourceFileName: ImeLog);
|
||||
tracker.ProcessLogMessageForTest(PlatformResultLine, sourceFileName: ImeLog);
|
||||
tracker.FlushPendingPlatformScriptResults(DateTime.UtcNow, force: true);
|
||||
|
||||
var script = Assert.Single(emitted);
|
||||
Assert.Equal("User", script.RunContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
-11
@@ -39,7 +39,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
tracker.SeedPendingPlatformScriptForTesting("dece354a", exitCode: 0, exitObservedAtUtc: observedAt, stdout: "Script start.\nScript end.");
|
||||
|
||||
// Well past the grace window — should emit.
|
||||
tracker.FlushStalePlatformScriptResults(observedAt.AddSeconds(20));
|
||||
tracker.FlushPendingPlatformScriptResults(observedAt.AddSeconds(20));
|
||||
|
||||
var script = Assert.Single(emitted);
|
||||
Assert.Equal("dece354a", script.PolicyId);
|
||||
@@ -60,11 +60,11 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
tracker.SeedPendingPlatformScriptForTesting("60b43a2e", exitCode: 0, exitObservedAtUtc: observedAt);
|
||||
|
||||
// Inside the grace window — IME might still log its result; do not emit yet.
|
||||
tracker.FlushStalePlatformScriptResults(observedAt.AddSeconds(5));
|
||||
tracker.FlushPendingPlatformScriptResults(observedAt.AddSeconds(5));
|
||||
Assert.Empty(emitted);
|
||||
|
||||
// Past the grace window — emit.
|
||||
tracker.FlushStalePlatformScriptResults(observedAt.AddSeconds(20));
|
||||
tracker.FlushPendingPlatformScriptResults(observedAt.AddSeconds(20));
|
||||
Assert.Single(emitted);
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
tracker.SeedPendingPlatformScriptForTesting("846cea22", exitCode: 0, exitObservedAtUtc: observedAt);
|
||||
|
||||
// Shutdown flush right after exit — grace not elapsed, but force emits anyway.
|
||||
tracker.FlushStalePlatformScriptResults(observedAt.AddSeconds(1), force: true);
|
||||
tracker.FlushPendingPlatformScriptResults(observedAt.AddSeconds(1), force: true);
|
||||
|
||||
Assert.Single(emitted);
|
||||
}
|
||||
@@ -92,7 +92,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
|
||||
tracker.SeedPendingPlatformScriptForTesting("bad5c0de", exitCode: 1, exitObservedAtUtc: observedAt);
|
||||
|
||||
tracker.FlushStalePlatformScriptResults(observedAt.AddSeconds(20));
|
||||
tracker.FlushPendingPlatformScriptResults(observedAt.AddSeconds(20));
|
||||
|
||||
var script = Assert.Single(emitted);
|
||||
Assert.Equal(1, script.ExitCode);
|
||||
@@ -109,7 +109,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
// Started but no exit code yet → still running, must not be emitted even on force flush.
|
||||
tracker.SeedPendingPlatformScriptForTesting("running", exitCode: null, exitObservedAtUtc: null);
|
||||
|
||||
tracker.FlushStalePlatformScriptResults(DateTime.UtcNow, force: true);
|
||||
tracker.FlushPendingPlatformScriptResults(DateTime.UtcNow, force: true);
|
||||
|
||||
Assert.Empty(emitted);
|
||||
}
|
||||
@@ -122,7 +122,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
var observedAt = new DateTime(2026, 6, 19, 12, 59, 4, DateTimeKind.Utc);
|
||||
|
||||
tracker.SeedPendingPlatformScriptForTesting("dece354a", exitCode: 0, exitObservedAtUtc: observedAt);
|
||||
tracker.FlushStalePlatformScriptResults(observedAt.AddSeconds(20));
|
||||
tracker.FlushPendingPlatformScriptResults(observedAt.AddSeconds(20));
|
||||
Assert.Single(emitted);
|
||||
|
||||
// IME finally logs its PS-SCRIPT-RESULT line — must NOT produce a second event.
|
||||
@@ -138,7 +138,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
var observedAt = new DateTime(2026, 6, 19, 12, 59, 4, DateTimeKind.Utc);
|
||||
|
||||
tracker.SeedPendingPlatformScriptForTesting("dece354a", exitCode: 0, exitObservedAtUtc: observedAt);
|
||||
tracker.FlushStalePlatformScriptResults(observedAt.AddSeconds(20));
|
||||
tracker.FlushPendingPlatformScriptResults(observedAt.AddSeconds(20));
|
||||
|
||||
// The exit-observed timestamp must travel on the emitted state so the adapter can bind
|
||||
// the event to it instead of an unrelated "last matched" line parsed during the grace.
|
||||
@@ -156,14 +156,14 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
|
||||
// Run 1 of policy X — fallback emits.
|
||||
tracker.SeedPendingPlatformScriptForTesting("policyX", exitCode: 0, exitObservedAtUtc: firstRun);
|
||||
tracker.FlushStalePlatformScriptResults(firstRun.AddSeconds(20));
|
||||
tracker.FlushPendingPlatformScriptResults(firstRun.AddSeconds(20));
|
||||
Assert.Single(emitted);
|
||||
|
||||
// Run 2 of the SAME policy later in the same agent lifetime (IME re-evaluation / retry)
|
||||
// must NOT be deduped away — the fresh start clears the prior emitted-marker.
|
||||
var secondRun = firstRun.AddMinutes(30);
|
||||
tracker.SeedPendingPlatformScriptForTesting("policyX", exitCode: 0, exitObservedAtUtc: secondRun);
|
||||
tracker.FlushStalePlatformScriptResults(secondRun.AddSeconds(20));
|
||||
tracker.FlushPendingPlatformScriptResults(secondRun.AddSeconds(20));
|
||||
Assert.Equal(2, emitted.Count);
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
Assert.Equal("ime_policy_result", script.ResultSource);
|
||||
|
||||
// Later fallback pass must find nothing pending → no duplicate.
|
||||
tracker.FlushStalePlatformScriptResults(observedAt.AddSeconds(20));
|
||||
tracker.FlushPendingPlatformScriptResults(observedAt.AddSeconds(20));
|
||||
Assert.Single(emitted);
|
||||
}
|
||||
}
|
||||
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutopilotMonitor.Agent.V2.Core.Logging;
|
||||
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime;
|
||||
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
|
||||
using AutopilotMonitor.Shared.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
{
|
||||
/// <summary>
|
||||
/// The poll loop's timing maxima (session c3ecb568: the loop was not scheduled for 26 s at
|
||||
/// 100 % VM CPU and nothing in the telemetry could show it): the longest pass and the
|
||||
/// longest pause between two passes, on a monotonic clock, restart-safe like the other
|
||||
/// health counters.
|
||||
/// </summary>
|
||||
public sealed class ImeLogTrackerPollTimingTests
|
||||
{
|
||||
/// <summary>Monotonic readings the tracker takes at the start and the end of every pass.</summary>
|
||||
private sealed class Clock
|
||||
{
|
||||
private readonly Queue<long> _readings = new Queue<long>();
|
||||
public void Enqueue(params long[] ms) { foreach (var m in ms) _readings.Enqueue(m); }
|
||||
public long Next() => _readings.Dequeue();
|
||||
}
|
||||
|
||||
private static ImeLogTracker Build(TempDirectory tmp, Clock clock)
|
||||
{
|
||||
var tracker = new ImeLogTracker(tmp.Path, new List<ImeLogPattern>(), new AgentLogger(tmp.Path, AgentLogLevel.Info), stateDirectory: tmp.Path);
|
||||
tracker.MonotonicMillisProvider = clock.Next;
|
||||
return tracker;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Longest_pass_and_longest_pause_are_kept_as_session_maxima()
|
||||
{
|
||||
using var tmp = new TempDirectory();
|
||||
var clock = new Clock();
|
||||
var tracker = Build(tmp, clock);
|
||||
|
||||
// Pass 1 takes 30 ms; the loop is not scheduled again for 26.1 s; pass 2 takes 10 ms,
|
||||
// pass 3 follows after the normal 100 ms.
|
||||
clock.Enqueue(0, 30, 26130, 26140, 26240, 26245);
|
||||
await tracker.RunPollPassAsync(CancellationToken.None);
|
||||
await tracker.RunPollPassAsync(CancellationToken.None);
|
||||
await tracker.RunPollPassAsync(CancellationToken.None);
|
||||
|
||||
var health = tracker.GetHealthSnapshot();
|
||||
Assert.Equal(30, health.PassMaxMs);
|
||||
Assert.Equal(26100, health.PassGapMaxMs);
|
||||
tracker.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_fresh_tracker_reports_zero()
|
||||
{
|
||||
using var tmp = new TempDirectory();
|
||||
var tracker = Build(tmp, new Clock());
|
||||
|
||||
var health = tracker.GetHealthSnapshot();
|
||||
Assert.Equal(0, health.PassMaxMs);
|
||||
Assert.Equal(0, health.PassGapMaxMs);
|
||||
tracker.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Maxima_survive_a_restart_and_are_never_lowered()
|
||||
{
|
||||
using var tmp = new TempDirectory();
|
||||
var clock = new Clock();
|
||||
var tracker1 = Build(tmp, clock);
|
||||
clock.Enqueue(0, 40, 5140, 5150);
|
||||
await tracker1.RunPollPassAsync(CancellationToken.None);
|
||||
await tracker1.RunPollPassAsync(CancellationToken.None);
|
||||
tracker1.SaveStateForTest();
|
||||
tracker1.Dispose();
|
||||
|
||||
var tracker2 = Build(tmp, clock);
|
||||
tracker2.LoadStateForTest();
|
||||
Assert.Equal(40, tracker2.GetHealthSnapshot().PassMaxMs);
|
||||
Assert.Equal(5100, tracker2.GetHealthSnapshot().PassGapMaxMs);
|
||||
|
||||
// The first pass after a restart has no pause to measure; a calmer session afterwards
|
||||
// never lowers what the session already saw.
|
||||
clock.Enqueue(6000, 6010, 6110, 6115);
|
||||
await tracker2.RunPollPassAsync(CancellationToken.None);
|
||||
await tracker2.RunPollPassAsync(CancellationToken.None);
|
||||
var health = tracker2.GetHealthSnapshot();
|
||||
Assert.Equal(40, health.PassMaxMs);
|
||||
Assert.Equal(5100, health.PassGapMaxMs);
|
||||
tracker2.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
+308
@@ -0,0 +1,308 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using AutopilotMonitor.Agent.V2.Core.Logging;
|
||||
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime;
|
||||
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
|
||||
using AutopilotMonitor.Shared.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
{
|
||||
/// <summary>
|
||||
/// The IME result waits for its executor end block (session e7f3c910): PS-SCRIPT-RESULT can
|
||||
/// reach the tracker before the AgentExecutor.log end block of the same run — an overwritten
|
||||
/// block is recovered one pass after the result that triggers the ledger check, the
|
||||
/// AgentExecutor.log read of a pass may precede the exit line by milliseconds, or the poll
|
||||
/// loop stalled. Emitting at once lost the exit code and, when the late start line reopened
|
||||
/// the slot, produced a fallback duplicate 15 s later.
|
||||
/// </summary>
|
||||
public sealed class ImeLogTrackerResultHoldTests
|
||||
{
|
||||
private static readonly DateTime T0 = new DateTime(2026, 9, 10, 22, 13, 35, DateTimeKind.Utc);
|
||||
|
||||
private sealed class Harness : IDisposable
|
||||
{
|
||||
private readonly TempDirectory _tmp = new TempDirectory();
|
||||
public ImeLogTracker Tracker { get; private set; }
|
||||
public DateTime Now { get; set; } = T0;
|
||||
public List<ScriptExecutionState> Completed { get; } = new List<ScriptExecutionState>();
|
||||
public List<ScriptStartedInfo> Started { get; } = new List<ScriptStartedInfo>();
|
||||
|
||||
public Harness() { Tracker = Build(); }
|
||||
|
||||
private ImeLogTracker Build()
|
||||
{
|
||||
var t = new ImeLogTracker(_tmp.Path, new List<ImeLogPattern>(), new AgentLogger(_tmp.Path, AgentLogLevel.Info), stateDirectory: _tmp.Path);
|
||||
t.UtcNowProvider = () => Now;
|
||||
t.OnScriptCompleted = s => Completed.Add(s);
|
||||
t.OnScriptStarted = s => Started.Add(s);
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>Agent restart over the same state directory.</summary>
|
||||
public void Restart()
|
||||
{
|
||||
Tracker.SaveStateForTest();
|
||||
Tracker.Dispose();
|
||||
Tracker = Build();
|
||||
Tracker.LoadStateForTest();
|
||||
}
|
||||
|
||||
/// <summary>A start line (IME "Script file … is generated" or the executor's argument line) with its source timestamp.</summary>
|
||||
public void Start(string id, DateTime lineTs)
|
||||
{
|
||||
Tracker.LastMatchedLogTimestamp = lineTs;
|
||||
Tracker.NextTestEntry();
|
||||
Tracker.HandlePlatformScriptStarted(id);
|
||||
}
|
||||
|
||||
public void Result(string id, DateTime lineTs, string result = "Success")
|
||||
=> Tracker.CompletePlatformScriptFromImeResultForTesting(id, result, lineTs);
|
||||
|
||||
public void Exit(string id, DateTime lineTs, int exitCode = 0)
|
||||
=> Tracker.RecordPlatformScriptExitCodeForTesting(id, exitCode, lineTs);
|
||||
|
||||
/// <summary>The end-of-pass flush at the harness clock.</summary>
|
||||
public void Flush(bool force = false) => Tracker.FlushPendingPlatformScriptResults(Now, force);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Tracker.Dispose();
|
||||
_tmp.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Result_before_the_end_block_is_held_and_emits_once_with_the_exit_code()
|
||||
{
|
||||
using var h = new Harness();
|
||||
h.Start("376ee51a", T0);
|
||||
h.Now = T0.AddSeconds(2);
|
||||
h.Result("376ee51a", T0.AddSeconds(2));
|
||||
Assert.Empty(h.Completed);
|
||||
h.Now = T0.AddSeconds(2.1);
|
||||
h.Flush();
|
||||
Assert.Empty(h.Completed);
|
||||
|
||||
// The end block surfaces on the next pass (ledger rewind or plain read lag).
|
||||
h.Exit("376ee51a", T0.AddSeconds(1.99));
|
||||
h.Now = T0.AddSeconds(2.2);
|
||||
h.Flush();
|
||||
|
||||
var script = Assert.Single(h.Completed);
|
||||
Assert.Equal(0, script.ExitCode);
|
||||
Assert.Equal("Success", script.Result);
|
||||
Assert.Equal("ime_policy_result", script.ResultSource);
|
||||
Assert.Equal(T0.AddSeconds(2), script.ResultObservedAtUtc);
|
||||
Assert.Equal("PS-SCRIPT-RESULT", script.ResultPatternId);
|
||||
Assert.Equal(T0, script.StartedAtUtc);
|
||||
|
||||
// Nothing fires twice: later passes, a repeated result line, the 15 s fallback window.
|
||||
h.Now = T0.AddSeconds(30);
|
||||
h.Flush();
|
||||
h.Result("376ee51a", T0.AddSeconds(2));
|
||||
Assert.Single(h.Completed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Result_with_the_end_block_already_read_emits_at_once()
|
||||
{
|
||||
using var h = new Harness();
|
||||
h.Start("847da54e", T0);
|
||||
h.Exit("847da54e", T0.AddSeconds(2.3));
|
||||
h.Now = T0.AddSeconds(2.31);
|
||||
h.Result("847da54e", T0.AddSeconds(2.31));
|
||||
|
||||
var script = Assert.Single(h.Completed);
|
||||
Assert.Equal(0, script.ExitCode);
|
||||
Assert.Null(script.ResultHeldSinceUtc);
|
||||
Assert.Equal(T0.AddSeconds(2.31), script.ResultObservedAtUtc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Held_result_emits_without_exit_code_after_the_grace()
|
||||
{
|
||||
using var h = new Harness();
|
||||
h.Start("784fcaf5", T0);
|
||||
h.Now = T0.AddSeconds(2);
|
||||
h.Result("784fcaf5", T0.AddSeconds(2));
|
||||
|
||||
h.Now = T0.AddSeconds(2).Add(ImeLogTracker.PlatformScriptEndBlockGrace).AddMilliseconds(-100);
|
||||
h.Flush();
|
||||
Assert.Empty(h.Completed);
|
||||
|
||||
h.Now = T0.AddSeconds(2).Add(ImeLogTracker.PlatformScriptEndBlockGrace);
|
||||
h.Flush();
|
||||
var script = Assert.Single(h.Completed);
|
||||
Assert.Null(script.ExitCode);
|
||||
Assert.Equal("Success", script.Result);
|
||||
Assert.Equal("ime_policy_result", script.ResultSource);
|
||||
|
||||
// An exit line surfacing after the emit is dropped — no second event, no fallback.
|
||||
h.Exit("784fcaf5", T0.AddSeconds(1.99));
|
||||
h.Now = T0.AddSeconds(40);
|
||||
h.Flush();
|
||||
Assert.Single(h.Completed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Shutdown_flush_emits_a_held_result_at_once()
|
||||
{
|
||||
using var h = new Harness();
|
||||
h.Start("a52e4f47", T0);
|
||||
h.Now = T0.AddSeconds(2);
|
||||
h.Result("a52e4f47", T0.AddSeconds(2), "Failed");
|
||||
Assert.Empty(h.Completed);
|
||||
|
||||
h.Flush(force: true);
|
||||
var script = Assert.Single(h.Completed);
|
||||
Assert.Equal("Failed", script.Result);
|
||||
Assert.Null(script.ExitCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Late_start_line_of_an_emitted_run_opens_no_new_run()
|
||||
{
|
||||
// Session e7f3c910, 376ee51a: the result was emitted, THEN the executor's start and
|
||||
// exit lines of the same run were read (a 2 s poll stall) — the start opened a second
|
||||
// slot and the exit code became a fallback duplicate 15 s later.
|
||||
using var h = new Harness();
|
||||
h.Start("376ee51a", T0);
|
||||
h.Exit("376ee51a", T0.AddSeconds(1.99));
|
||||
h.Now = T0.AddSeconds(2);
|
||||
h.Result("376ee51a", T0.AddSeconds(2));
|
||||
Assert.Single(h.Completed);
|
||||
Assert.Single(h.Started);
|
||||
|
||||
h.Start("376ee51a", T0.AddMilliseconds(300));
|
||||
Assert.Single(h.Started);
|
||||
h.Exit("376ee51a", T0.AddSeconds(1.99));
|
||||
h.Now = T0.AddSeconds(30);
|
||||
h.Flush();
|
||||
Assert.Single(h.Completed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Start_line_newer_than_the_emitted_result_is_the_next_run()
|
||||
{
|
||||
using var h = new Harness();
|
||||
h.Start("338d9434", T0);
|
||||
h.Exit("338d9434", T0.AddSeconds(1));
|
||||
h.Now = T0.AddSeconds(1);
|
||||
h.Result("338d9434", T0.AddSeconds(1));
|
||||
Assert.Single(h.Completed);
|
||||
|
||||
// IME re-runs the policy after the user signed in.
|
||||
h.Start("338d9434", T0.AddMinutes(7));
|
||||
Assert.Equal(2, h.Started.Count);
|
||||
h.Exit("338d9434", T0.AddMinutes(7).AddSeconds(3));
|
||||
h.Now = T0.AddMinutes(7).AddSeconds(3);
|
||||
h.Result("338d9434", T0.AddMinutes(7).AddSeconds(3));
|
||||
Assert.Equal(2, h.Completed.Count);
|
||||
Assert.Equal(T0.AddMinutes(7), h.Completed[1].StartedAtUtc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Newer_start_line_closes_a_held_result_and_starts_the_next_run()
|
||||
{
|
||||
using var h = new Harness();
|
||||
h.Start("c81b8053", T0);
|
||||
h.Now = T0.AddSeconds(2);
|
||||
h.Result("c81b8053", T0.AddSeconds(2));
|
||||
Assert.Empty(h.Completed);
|
||||
|
||||
// The end block never surfaced before the policy ran again.
|
||||
h.Start("c81b8053", T0.AddSeconds(4));
|
||||
var first = Assert.Single(h.Completed);
|
||||
Assert.Null(first.ExitCode);
|
||||
Assert.Equal(T0, first.StartedAtUtc);
|
||||
Assert.Equal(2, h.Started.Count);
|
||||
|
||||
h.Exit("c81b8053", T0.AddSeconds(6));
|
||||
h.Now = T0.AddSeconds(6);
|
||||
h.Result("c81b8053", T0.AddSeconds(6));
|
||||
Assert.Equal(2, h.Completed.Count);
|
||||
Assert.Equal(0, h.Completed[1].ExitCode);
|
||||
Assert.Equal(T0.AddSeconds(4), h.Completed[1].StartedAtUtc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Result_without_a_start_takes_the_recovered_start_line_and_end_block()
|
||||
{
|
||||
// Both the start block and the end block were hidden when the result arrived; the
|
||||
// ledger rewind the result triggers brings both back on the next pass.
|
||||
using var h = new Harness();
|
||||
h.Now = T0.AddSeconds(2);
|
||||
h.Result("d39eeef3", T0.AddSeconds(2));
|
||||
Assert.Empty(h.Completed);
|
||||
|
||||
h.Start("d39eeef3", T0);
|
||||
Assert.Empty(h.Started);
|
||||
h.Exit("d39eeef3", T0.AddSeconds(1.99));
|
||||
h.Now = T0.AddSeconds(2.2);
|
||||
h.Flush();
|
||||
|
||||
var script = Assert.Single(h.Completed);
|
||||
Assert.Equal(T0, script.StartedAtUtc);
|
||||
Assert.Equal(0, script.ExitCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Held_result_survives_a_restart()
|
||||
{
|
||||
using var h = new Harness();
|
||||
h.Start("832e27bc", T0);
|
||||
h.Now = T0.AddSeconds(2);
|
||||
h.Result("832e27bc", T0.AddSeconds(2));
|
||||
h.Restart();
|
||||
Assert.Empty(h.Completed);
|
||||
|
||||
h.Exit("832e27bc", T0.AddSeconds(1.99));
|
||||
h.Now = T0.AddSeconds(2.5);
|
||||
h.Flush();
|
||||
var script = Assert.Single(h.Completed);
|
||||
Assert.Equal(0, script.ExitCode);
|
||||
Assert.Equal(T0.AddSeconds(2), script.ResultObservedAtUtc);
|
||||
Assert.Equal("PS-SCRIPT-RESULT", script.ResultPatternId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Emitted_marker_keeps_the_run_timestamp_across_a_restart()
|
||||
{
|
||||
using var h = new Harness();
|
||||
h.Start("89b3cbcd", T0);
|
||||
h.Exit("89b3cbcd", T0.AddSeconds(1));
|
||||
h.Now = T0.AddSeconds(1);
|
||||
h.Result("89b3cbcd", T0.AddSeconds(1));
|
||||
h.Restart();
|
||||
|
||||
h.Start("89b3cbcd", T0.AddMilliseconds(500)); // late line of the emitted run
|
||||
Assert.Single(h.Started);
|
||||
h.Start("89b3cbcd", T0.AddMinutes(5)); // the next run
|
||||
Assert.Equal(2, h.Started.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Marker_list_from_an_older_state_file_still_dedups()
|
||||
{
|
||||
using var tmp = new TempDirectory();
|
||||
var persistence = new ImeTrackerStatePersistence(tmp.Path, new AgentLogger(tmp.Path, AgentLogLevel.Info));
|
||||
persistence.Save(new ImeTrackerStateData { PlatformScriptResultEmitted = new List<string> { "policyZ" } });
|
||||
|
||||
var completed = new List<ScriptExecutionState>();
|
||||
var started = new List<ScriptStartedInfo>();
|
||||
var tracker = new ImeLogTracker(tmp.Path, new List<ImeLogPattern>(), new AgentLogger(tmp.Path, AgentLogLevel.Info), stateDirectory: tmp.Path);
|
||||
tracker.OnScriptCompleted = s => completed.Add(s);
|
||||
tracker.OnScriptStarted = s => started.Add(s);
|
||||
tracker.LoadStateForTest();
|
||||
|
||||
tracker.CompletePlatformScriptFromImeResultForTesting("policyZ", "Success", T0);
|
||||
Assert.Empty(completed);
|
||||
|
||||
// Without a timestamp on the marker every start line is a new run, as before.
|
||||
tracker.LastMatchedLogTimestamp = T0;
|
||||
tracker.HandlePlatformScriptStarted("policyZ");
|
||||
Assert.Single(started);
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
-5
@@ -41,7 +41,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
// Run 1: exit code seen, shutdown force-flush emits the fallback completion.
|
||||
var tracker1 = BuildTracker(tmp, out var emitted1);
|
||||
tracker1.SeedPendingPlatformScriptForTesting("policyA", exitCode: 0, exitObservedAtUtc: observedAt);
|
||||
tracker1.FlushStalePlatformScriptResults(observedAt.AddSeconds(1), force: true);
|
||||
tracker1.FlushPendingPlatformScriptResults(observedAt.AddSeconds(1), force: true);
|
||||
Assert.Single(emitted1);
|
||||
tracker1.SaveStateForTest();
|
||||
|
||||
@@ -68,7 +68,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
// Restarted tracker continues the grace window instead of dropping the script.
|
||||
var tracker2 = BuildTracker(tmp, out var emitted2);
|
||||
tracker2.LoadStateForTest();
|
||||
tracker2.FlushStalePlatformScriptResults(observedAt.AddSeconds(20));
|
||||
tracker2.FlushPendingPlatformScriptResults(observedAt.AddSeconds(20));
|
||||
|
||||
var script = Assert.Single(emitted2);
|
||||
Assert.Equal("policyB", script.PolicyId);
|
||||
@@ -84,7 +84,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
|
||||
var tracker1 = BuildTracker(tmp, out _);
|
||||
tracker1.SeedPendingPlatformScriptForTesting("policyC", exitCode: 0, exitObservedAtUtc: observedAt);
|
||||
tracker1.FlushStalePlatformScriptResults(observedAt.AddSeconds(1), force: true);
|
||||
tracker1.FlushPendingPlatformScriptResults(observedAt.AddSeconds(1), force: true);
|
||||
tracker1.SaveStateForTest();
|
||||
|
||||
// Restart, then a genuine RE-RUN of the same policy (fresh start clears the marker).
|
||||
@@ -109,8 +109,10 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
var tracker = BuildTracker(tmp, out var emitted);
|
||||
tracker.LoadStateForTest(); // must not throw
|
||||
|
||||
// Degrades to pre-fix behavior: no markers restored, normal emission works.
|
||||
// Degrades to pre-fix behavior: no markers restored, normal emission works (the
|
||||
// result waits for its end block until the shutdown flush).
|
||||
tracker.CompletePlatformScriptFromImeResultForTesting("policyD", "Success");
|
||||
tracker.FlushPendingPlatformScriptResults(DateTime.UtcNow, force: true);
|
||||
Assert.Single(emitted);
|
||||
}
|
||||
|
||||
@@ -128,8 +130,10 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
|
||||
tracker.CompletePlatformScriptFromImeResultForTesting("policyE", "Failed", run2Start.AddMinutes(-5));
|
||||
Assert.Empty(emitted);
|
||||
|
||||
// Run 2's own result (newer than its start) still emits normally.
|
||||
// Run 2's own result (newer than its start) still emits normally — held for the end
|
||||
// block until the shutdown flush, since none is seeded here.
|
||||
tracker.CompletePlatformScriptFromImeResultForTesting("policyE", "Success", run2Start.AddMinutes(2));
|
||||
tracker.FlushPendingPlatformScriptResults(run2Start.AddMinutes(2), force: true);
|
||||
var script = Assert.Single(emitted);
|
||||
Assert.Equal("Success", script.Result);
|
||||
Assert.Equal(run2Start, script.StartedAtUtc);
|
||||
|
||||
+204
@@ -0,0 +1,204 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using AutopilotMonitor.Agent.V2.Core.Configuration;
|
||||
using AutopilotMonitor.Agent.V2.Core.Logging;
|
||||
using AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime;
|
||||
using AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather;
|
||||
using AutopilotMonitor.Agent.V2.Core.Monitoring.Transport;
|
||||
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
|
||||
using AutopilotMonitor.Shared.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// The hard-blocked event-log channels (Security, PowerShell, Sysmon) are unreadable through
|
||||
/// the diagnostics package as well — by path, by wildcard, by resolved channel, and in
|
||||
/// unrestricted mode. Before this the block held only in the event-log collector.
|
||||
/// </summary>
|
||||
public sealed class DiagnosticsEventLogBlockTests
|
||||
{
|
||||
private const string WinevtLogs = @"C:\Windows\System32\winevt\Logs";
|
||||
|
||||
// A channel no machine has: the export fails and the packager falls back to the raw copy.
|
||||
private const string HarmlessChannel = "AutopilotMonitor-Tests-NoSuchChannel/Operational";
|
||||
|
||||
[Theory]
|
||||
[InlineData("Security.evtx")]
|
||||
[InlineData("security.EVTX")]
|
||||
[InlineData("Microsoft-Windows-PowerShell%4Operational.evtx")]
|
||||
[InlineData("Microsoft-Windows-PowerShell%4Admin.evtx")]
|
||||
[InlineData("Windows PowerShell.evtx")]
|
||||
[InlineData("Microsoft-Windows-Sysmon%4Operational.evtx")]
|
||||
[InlineData("Archive-Security-2026-09-21-10-15-30-123.evtx")]
|
||||
[InlineData("Archive-Microsoft-Windows-PowerShell%4Operational-2026-09-21-10-15-30-123.evtx")]
|
||||
public void Hard_blocked_channel_files_are_recognised_by_name(string fileName)
|
||||
{
|
||||
Assert.True(DiagnosticsPathGuards.IsHardBlockedEventLogFile(Path.Combine(WinevtLogs, fileName)));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("System.evtx")]
|
||||
[InlineData("Application.evtx")]
|
||||
[InlineData("SecurityAudit.evtx")]
|
||||
[InlineData("Security.log")]
|
||||
[InlineData("Microsoft-Windows-Security-Mitigations%4KernelMode.evtx")]
|
||||
// Same boundary as the collector: only '/' continues a blocked channel name.
|
||||
[InlineData("Microsoft-Windows-PowerShell-DesiredStateConfiguration-FileDownloadManager%4Operational.evtx")]
|
||||
[InlineData("Archive-System-2026-09-21-10-15-30-123.evtx")]
|
||||
public void Other_files_are_not_mistaken_for_a_blocked_channel(string fileName)
|
||||
{
|
||||
Assert.False(DiagnosticsPathGuards.IsHardBlockedEventLogFile(Path.Combine(WinevtLogs, fileName)));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void Path_guard_refuses_a_blocked_channel_file_in_every_mode(bool unrestrictedMode)
|
||||
{
|
||||
Assert.False(DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
|
||||
WinevtLogs + @"\Security.evtx", unrestrictedMode));
|
||||
Assert.False(DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
|
||||
WinevtLogs + @"\Microsoft-Windows-PowerShell%4Operational.evtx", unrestrictedMode));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Path_guard_still_admits_other_channels_and_wildcards()
|
||||
{
|
||||
Assert.True(DiagnosticsPathGuards.IsDiagnosticsPathAllowed(WinevtLogs + @"\System.evtx"));
|
||||
// Undecidable on the string — the packager filters per file (tests below).
|
||||
Assert.True(DiagnosticsPathGuards.IsDiagnosticsPathAllowed(WinevtLogs + @"\*.evtx"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Security", true)]
|
||||
[InlineData("Microsoft-Windows-PowerShell/Operational", true)]
|
||||
[InlineData(" Windows PowerShell ", true)]
|
||||
[InlineData("Microsoft-Windows-Sysmon/Operational", true)]
|
||||
[InlineData("System", false)]
|
||||
[InlineData("", false)]
|
||||
[InlineData(null, false)]
|
||||
public void Channel_block_is_one_matcher_for_collector_and_package(string? channel, bool blocked)
|
||||
{
|
||||
Assert.Equal(blocked, GatherRuleGuards.IsHardBlockedEventLogChannel(channel!));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ packager ----
|
||||
|
||||
// %TEMP% lives under C:\Users, which the path guard blocks in every mode, so the
|
||||
// configured folder sits beside the test assembly and the run is unrestricted — which
|
||||
// is also the strongest form of the claim: not even unrestricted mode collects these.
|
||||
private sealed class EvtxFolder : System.IDisposable
|
||||
{
|
||||
public string Path { get; } = System.IO.Path.Combine(
|
||||
System.AppContext.BaseDirectory, "diag-evtx-" + System.Guid.NewGuid().ToString("N"));
|
||||
|
||||
public EvtxFolder(params string[] fileNames)
|
||||
{
|
||||
Directory.CreateDirectory(Path);
|
||||
foreach (var name in fileNames)
|
||||
File.WriteAllBytes(System.IO.Path.Combine(Path, name), new byte[] { 1, 2, 3 });
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try { Directory.Delete(Path, recursive: true); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
private static DiagnosticsPackageService BuildService(
|
||||
TempDirectory scratch, string configuredPath, System.Func<string, string> channelResolver)
|
||||
{
|
||||
var logger = new AgentLogger(Directory.CreateDirectory(Path.Combine(scratch.Path, "logs")).FullName);
|
||||
var apiClient = new BackendApiClient(
|
||||
httpClient: new System.Net.Http.HttpClient(),
|
||||
baseUrl: "http://localhost",
|
||||
manufacturer: string.Empty,
|
||||
model: string.Empty,
|
||||
serialNumber: string.Empty,
|
||||
useBootstrapTokenAuth: false,
|
||||
bootstrapToken: null,
|
||||
agentVersion: "0.0.0",
|
||||
logger: logger);
|
||||
|
||||
var cfg = new AgentConfiguration
|
||||
{
|
||||
SessionId = "S1",
|
||||
TenantId = "T1",
|
||||
ApiBaseUrl = "http://localhost",
|
||||
UnrestrictedMode = true,
|
||||
};
|
||||
cfg.DiagnosticsLogPaths.Add(new DiagnosticsLogPath { Path = configuredPath });
|
||||
|
||||
string Sub(string name) => Directory.CreateDirectory(Path.Combine(scratch.Path, name)).FullName;
|
||||
return new DiagnosticsPackageService(
|
||||
cfg,
|
||||
logger,
|
||||
apiClient,
|
||||
agentLogFolderOverride: Sub("agentlogs"),
|
||||
imeLogFolderOverride: Sub("ime"),
|
||||
agentStateFolderOverride: Sub("state"),
|
||||
agentSpoolFolderOverride: Sub("spool"),
|
||||
agentDataFolderOverride: Sub("data"),
|
||||
sectionFolderOverrides: null,
|
||||
devicePreparationProbe: () => false)
|
||||
{
|
||||
EventLogChannelResolver = channelResolver,
|
||||
};
|
||||
}
|
||||
|
||||
private static List<string> ZipEntryNames(byte[] bytes)
|
||||
{
|
||||
using var ms = new MemoryStream(bytes);
|
||||
using var archive = new ZipArchive(ms, ZipArchiveMode.Read);
|
||||
return archive.Entries.Select(e => e.FullName).ToList();
|
||||
}
|
||||
|
||||
private static string ReadManifest(byte[] bytes)
|
||||
{
|
||||
using var ms = new MemoryStream(bytes);
|
||||
using var archive = new ZipArchive(ms, ZipArchiveMode.Read);
|
||||
using var reader = new StreamReader(archive.GetEntry("package-manifest.txt")!.Open());
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wildcard_entry_skips_blocked_channel_files_and_keeps_the_rest()
|
||||
{
|
||||
using var scratch = new TempDirectory();
|
||||
using var folder = new EvtxFolder(
|
||||
"Security.evtx", "Microsoft-Windows-PowerShell%4Operational.evtx", "Setup.evtx");
|
||||
|
||||
var service = BuildService(scratch, Path.Combine(folder.Path, "*.evtx"), _ => HarmlessChannel);
|
||||
var bytes = service.BuildArchiveBytes(enrollmentSucceeded: false);
|
||||
|
||||
var entries = ZipEntryNames(bytes);
|
||||
Assert.Contains(entries, e => e.EndsWith("/Setup.evtx"));
|
||||
Assert.DoesNotContain(entries, e => e.EndsWith("Security.evtx"));
|
||||
Assert.DoesNotContain(entries, e => e.Contains("PowerShell"));
|
||||
|
||||
var manifest = ReadManifest(bytes);
|
||||
Assert.Contains("BLOCKED (event-log channel guard", manifest);
|
||||
Assert.Contains("Security.evtx", manifest);
|
||||
Assert.Equal(2, service.LastPackaging!.ProblemsByKind["path-guard"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void File_with_a_harmless_name_is_blocked_when_it_resolves_to_a_blocked_channel()
|
||||
{
|
||||
// A channel is free to write to any file name; and the resolver's naming fallback
|
||||
// maps a stray file onto a live channel. The export must never read that channel.
|
||||
using var scratch = new TempDirectory();
|
||||
using var folder = new EvtxFolder("Harmless.evtx");
|
||||
|
||||
var service = BuildService(scratch, Path.Combine(folder.Path, "Harmless.evtx"), _ => "Security");
|
||||
var bytes = service.BuildArchiveBytes(enrollmentSucceeded: false);
|
||||
|
||||
Assert.DoesNotContain(ZipEntryNames(bytes), e => e.EndsWith("Harmless.evtx"));
|
||||
Assert.Contains("BLOCKED (event-log channel guard, channel 'Security')", ReadManifest(bytes));
|
||||
}
|
||||
}
|
||||
}
|
||||
+85
@@ -206,6 +206,91 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Runtime
|
||||
Assert.Contains($"BLOCKED (path guard): {configuredPath}", manifest);
|
||||
}
|
||||
|
||||
// %TEMP% lives under C:\Users, which the guard blocks in every mode — the positive
|
||||
// configured-path cases run beside the test assembly, in unrestricted mode.
|
||||
private sealed class ConfiguredPathTree : System.IDisposable
|
||||
{
|
||||
public string Root { get; } = Path.Combine(
|
||||
System.AppContext.BaseDirectory, "diag-paths-" + System.Guid.NewGuid().ToString("N"));
|
||||
public string Logs => Path.Combine(Root, "Logs");
|
||||
|
||||
public ConfiguredPathTree()
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(Logs, "sub"));
|
||||
File.WriteAllText(Path.Combine(Root, "parent-only.txt"), "not configured");
|
||||
File.WriteAllText(Path.Combine(Logs, "app.log"), "configured");
|
||||
File.WriteAllText(Path.Combine(Logs, "sub", "app.log"), "below");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try { Directory.Delete(Root, recursive: true); } catch { /* best effort */ }
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData("\\")]
|
||||
public void BuildArchiveBytes_folder_entry_collects_the_folder_not_its_parent(string trailing)
|
||||
{
|
||||
using var rig = new Rig();
|
||||
using var tree = new ConfiguredPathTree();
|
||||
|
||||
var bytes = rig.Build(cfg =>
|
||||
{
|
||||
cfg.UnrestrictedMode = true;
|
||||
cfg.DiagnosticsLogPaths.Add(new AutopilotMonitor.Shared.Models.DiagnosticsLogPath
|
||||
{
|
||||
Path = tree.Logs + trailing,
|
||||
IncludeSubfolders = true,
|
||||
});
|
||||
})
|
||||
.BuildArchiveBytes(enrollmentSucceeded: true);
|
||||
|
||||
var entries = ZipEntryNames(bytes);
|
||||
Assert.Contains("AdditionalLogs/Logs/app.log", entries);
|
||||
Assert.Contains("AdditionalLogs/Logs/sub/app.log", entries);
|
||||
Assert.DoesNotContain(entries, e => e.EndsWith("parent-only.txt"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildArchiveBytes_file_entry_ignores_include_subfolders()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
using var tree = new ConfiguredPathTree();
|
||||
|
||||
var bytes = rig.Build(cfg =>
|
||||
{
|
||||
cfg.UnrestrictedMode = true;
|
||||
cfg.DiagnosticsLogPaths.Add(new AutopilotMonitor.Shared.Models.DiagnosticsLogPath
|
||||
{
|
||||
Path = Path.Combine(tree.Logs, "app.log"),
|
||||
IncludeSubfolders = true,
|
||||
});
|
||||
})
|
||||
.BuildArchiveBytes(enrollmentSucceeded: true);
|
||||
|
||||
var entries = ZipEntryNames(bytes);
|
||||
Assert.Contains("AdditionalLogs/Logs/app.log", entries);
|
||||
Assert.DoesNotContain("AdditionalLogs/Logs/sub/app.log", entries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnumerateFilesNoReparseDirs_asks_the_guard_before_entering_a_subfolder()
|
||||
{
|
||||
using var tree = new ConfiguredPathTree();
|
||||
var refused = new List<string>();
|
||||
|
||||
var files = DiagnosticsPackageService.EnumerateFilesNoReparseDirs(
|
||||
tree.Root, "*", recurse: true,
|
||||
mayEnterDirectory: dir => !dir.EndsWith("sub"),
|
||||
refusedDirectories: refused);
|
||||
|
||||
Assert.Contains(Path.Combine(tree.Logs, "app.log"), files);
|
||||
Assert.DoesNotContain(Path.Combine(tree.Logs, "sub", "app.log"), files);
|
||||
Assert.Equal(new[] { Path.Combine(tree.Logs, "sub") }, refused);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildArchiveBytes_excludes_top_level_session_id_and_bootstrap_config()
|
||||
{
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using AutopilotMonitor.Agent.V2.Core.Logging;
|
||||
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals;
|
||||
using AutopilotMonitor.Agent.V2.Core.Orchestration;
|
||||
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
|
||||
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="ProvisioningStatusTracker.TryGetPendingFailureArgs"/> exposes the registry
|
||||
/// failure detail while its settle window is open and nothing once the window has fired —
|
||||
/// the coordinator reads it when a Shell-Core failure terminalises first (session 683f1eff).
|
||||
/// </summary>
|
||||
public sealed class ProvisioningStatusTrackerPendingFailureTests
|
||||
{
|
||||
private static readonly DateTime Fixed = new DateTime(2026, 9, 8, 18, 11, 2, DateTimeKind.Utc);
|
||||
|
||||
private const string InProgressJson = @"{
|
||||
""categorySucceeded"": null,
|
||||
""AppsSubcategory"": {""subcategoryState"":""inProgress"",""subcategoryStatusText"":""Apps (Working)""}
|
||||
}";
|
||||
private const string AppsFailedJson = @"{
|
||||
""categorySucceeded"": null,
|
||||
""AppsSubcategory"": {""subcategoryState"":""failed"",""subcategoryStatusText"":""Apps (0x80070652)""}
|
||||
}";
|
||||
|
||||
private sealed class Fixture : IDisposable
|
||||
{
|
||||
public TempDirectory Tmp { get; } = new TempDirectory();
|
||||
public ProvisioningStatusTracker Tracker { get; }
|
||||
|
||||
public Fixture()
|
||||
{
|
||||
var post = new InformationalEventPost(new FakeSignalIngressSink(), new VirtualClock(Fixed));
|
||||
var logger = new AgentLogger(Tmp.Path, AgentLogLevel.Info);
|
||||
Tracker = new ProvisioningStatusTracker(
|
||||
sessionId: "S1",
|
||||
tenantId: "T1",
|
||||
post: post,
|
||||
logger: logger,
|
||||
appxScanner: new FakeAppxDeploymentFailureScanner(),
|
||||
backgroundDispatcher: action => action());
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Tracker.Dispose();
|
||||
Tmp.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Nothing_pending_before_any_failure()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
Assert.Null(f.Tracker.TryGetPendingFailureArgs());
|
||||
|
||||
f.Tracker.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", InProgressJson);
|
||||
Assert.Null(f.Tracker.TryGetPendingFailureArgs());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Armed_settle_window_exposes_the_registry_detail()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
f.Tracker.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", InProgressJson);
|
||||
f.Tracker.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", AppsFailedJson);
|
||||
|
||||
var pending = f.Tracker.TryGetPendingFailureArgs();
|
||||
|
||||
Assert.NotNull(pending);
|
||||
Assert.Equal("Provisioning_DeviceSetup_Apps_Failed", pending!.FailureType);
|
||||
Assert.Equal("0x80070652", pending.ErrorCode);
|
||||
Assert.Equal("Apps", pending.FailedSubcategory);
|
||||
Assert.Equal("DeviceSetup", pending.Category);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Expired_settle_window_leaves_nothing_pending()
|
||||
{
|
||||
using var f = new Fixture();
|
||||
f.Tracker.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", InProgressJson);
|
||||
f.Tracker.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", AppsFailedJson);
|
||||
|
||||
f.Tracker.TriggerSettleTimerForTest("DeviceSetupCategory.Status");
|
||||
|
||||
Assert.Null(f.Tracker.TryGetPendingFailureArgs());
|
||||
}
|
||||
}
|
||||
}
|
||||
+25
@@ -56,6 +56,31 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Telemetry.Gather
|
||||
LoadEmbeddedGuardrails()["maxCommandLength"]!.Value<int>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void User_profile_prefixes_are_all_accepted_by_the_loader()
|
||||
{
|
||||
// The loader drops entries outside AppData\Local / AppData\Roaming (the hard block
|
||||
// would never release them anyway). A dropped entry is invisible at runtime — it
|
||||
// just never matches — so a typo in the JSON must fail here instead.
|
||||
var json = LoadEmbeddedGuardrails()["userProfileFilePrefixes"]?.ToObject<string[]>()
|
||||
?? new string[0];
|
||||
|
||||
Assert.Equal(
|
||||
json.Select(p => p.Trim().TrimEnd('\\')).OrderBy(p => p),
|
||||
GatherRuleGuards.AllowedUserProfileFilePrefixes.OrderBy(p => p));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void User_profile_prefixes_stay_under_the_hard_blocks_exception()
|
||||
{
|
||||
Assert.All(
|
||||
GatherRuleGuards.AllowedUserProfileFilePrefixes,
|
||||
prefix => Assert.Contains(
|
||||
GatherRuleGuards.AllowedUserProfileSubdirs,
|
||||
subdir => prefix.StartsWith(subdir, System.StringComparison.OrdinalIgnoreCase) &&
|
||||
(prefix.Length == subdir.Length || prefix[subdir.Length] == '\\')));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Blocked_event_log_channels_match_code_constants()
|
||||
{
|
||||
|
||||
+36
@@ -265,6 +265,42 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
|
||||
sut.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Deadline_fired_late_keeps_the_due_time_as_stamp_and_carries_it_in_the_payload()
|
||||
{
|
||||
// A deadline that was due while the device slept (or the agent was down) fires only
|
||||
// afterwards. The signal keeps the due time as OccurredAtUtc (D-238: the outage must not
|
||||
// become session duration) and repeats it in the payload so the stale-fire guards
|
||||
// identify the incarnation independently of the stamp.
|
||||
using var rig = new EnrollmentOrchestratorRig(At);
|
||||
var sut = rig.Build();
|
||||
sut.Start();
|
||||
|
||||
var dueAt = At.AddMinutes(-5);
|
||||
var scheduler = GetScheduler(sut);
|
||||
scheduler.Schedule(new ActiveDeadline(
|
||||
name: "hello_safety",
|
||||
dueAtUtc: dueAt,
|
||||
firesSignalKind: DecisionSignalKind.DeadlineFired,
|
||||
firesPayload: new Dictionary<string, string> { [SignalPayloadKeys.Deadline] = "hello_safety" }));
|
||||
|
||||
var signalLog = GetSignalLog(sut);
|
||||
Assert.True(SpinWait.SpinUntil(() => ContainsDeadlineFired(signalLog), 3000));
|
||||
|
||||
DecisionSignal? fired = null;
|
||||
foreach (var sig in signalLog.ReadAll())
|
||||
{
|
||||
if (sig.Kind == DecisionSignalKind.DeadlineFired) { fired = sig; break; }
|
||||
}
|
||||
Assert.NotNull(fired);
|
||||
Assert.Equal(dueAt, fired!.OccurredAtUtc);
|
||||
Assert.NotNull(fired.Payload);
|
||||
Assert.Equal("hello_safety", fired.Payload![SignalPayloadKeys.Deadline]);
|
||||
Assert.Equal(dueAt.ToString("O"), fired.Payload[SignalPayloadKeys.DeadlineDueAtUtc]);
|
||||
|
||||
sut.Stop();
|
||||
}
|
||||
|
||||
// ========================================================================= Quarantine
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -302,6 +302,114 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
|
||||
Assert.False(File.Exists(path));
|
||||
}
|
||||
|
||||
// ================================================================= Multi-instance guard
|
||||
|
||||
private static AutopilotMonitor.Agent.V2.Program.AgentProcessFact Proc(int pid, int sessionId)
|
||||
=> new AutopilotMonitor.Agent.V2.Program.AgentProcessFact(pid, sessionId);
|
||||
|
||||
[Fact]
|
||||
public void FindSiblingAgentPid_ignores_a_same_named_process_in_a_user_session()
|
||||
{
|
||||
// The agent (pid 100, session 0) starts while a standard user keeps a process with
|
||||
// the agent's image name resident in their own session. It must not block the start.
|
||||
var matches = new[] { Proc(100, 0), Proc(200, 1) };
|
||||
|
||||
Assert.Equal(0, AutopilotMonitor.Agent.V2.Program.FindSiblingAgentPid(matches, ownPid: 100));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(2)]
|
||||
[InlineData(7)]
|
||||
[InlineData(-1)]
|
||||
public void FindSiblingAgentPid_ignores_every_session_but_zero(int sessionId)
|
||||
{
|
||||
var matches = new[] { Proc(100, 0), Proc(200, sessionId) };
|
||||
|
||||
Assert.Equal(0, AutopilotMonitor.Agent.V2.Program.FindSiblingAgentPid(matches, ownPid: 100));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FindSiblingAgentPid_reports_a_second_runtime_in_session_zero()
|
||||
{
|
||||
// Self-update restart while the old runtime is still alive — the single-agent
|
||||
// invariant the guard exists for.
|
||||
var matches = new[] { Proc(100, 0), Proc(300, 0) };
|
||||
|
||||
Assert.Equal(300, AutopilotMonitor.Agent.V2.Program.FindSiblingAgentPid(matches, ownPid: 100));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FindSiblingAgentPid_never_reports_the_caller_itself()
|
||||
{
|
||||
var matches = new[] { Proc(100, 0) };
|
||||
|
||||
Assert.Equal(0, AutopilotMonitor.Agent.V2.Program.FindSiblingAgentPid(matches, ownPid: 100));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FindSiblingAgentPid_skips_user_session_processes_listed_before_the_runtime()
|
||||
{
|
||||
// Install-mode launch verification: a user-session process enumerated first must not
|
||||
// be reported as the launched runtime.
|
||||
var matches = new[] { Proc(200, 1), Proc(201, 2), Proc(100, 0), Proc(300, 0) };
|
||||
|
||||
Assert.Equal(300, AutopilotMonitor.Agent.V2.Program.FindSiblingAgentPid(matches, ownPid: 100));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FindSiblingAgentPid_blocks_an_interactive_start_next_to_the_session_zero_runtime()
|
||||
{
|
||||
// --console run from an admin prompt (session 1) while the Scheduled Task runtime is up.
|
||||
var matches = new[] { Proc(100, 1), Proc(300, 0) };
|
||||
|
||||
Assert.Equal(300, AutopilotMonitor.Agent.V2.Program.FindSiblingAgentPid(matches, ownPid: 100));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FindSiblingAgentPid_returns_zero_for_no_matches()
|
||||
{
|
||||
Assert.Equal(0, AutopilotMonitor.Agent.V2.Program.FindSiblingAgentPid(
|
||||
new AutopilotMonitor.Agent.V2.Program.AgentProcessFact[0], ownPid: 100));
|
||||
Assert.Equal(0, AutopilotMonitor.Agent.V2.Program.FindSiblingAgentPid(null, ownPid: 100));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SnapshotProcessesByName_reads_pid_and_session_of_the_running_test_host()
|
||||
{
|
||||
using var self = System.Diagnostics.Process.GetCurrentProcess();
|
||||
|
||||
var facts = AutopilotMonitor.Agent.V2.Program.SnapshotProcessesByName(self.ProcessName);
|
||||
|
||||
var own = Assert.Single(facts, f => f.Pid == self.Id);
|
||||
Assert.Equal(self.SessionId, own.SessionId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SnapshotProcessesByName_reads_the_session_of_system_owned_processes_without_access_rights()
|
||||
{
|
||||
// Positive control for the fail-open direction: the session id comes from the process
|
||||
// snapshot, not from an opened handle, so even an unprivileged caller reads it for
|
||||
// SYSTEM-owned service hosts. A skipped process would let a real sibling go unseen.
|
||||
var expected = System.Diagnostics.Process.GetProcessesByName("svchost");
|
||||
foreach (var p in expected) p.Dispose();
|
||||
|
||||
var facts = AutopilotMonitor.Agent.V2.Program.SnapshotProcessesByName("svchost");
|
||||
|
||||
Assert.NotEmpty(facts);
|
||||
Assert.All(facts, f => Assert.True(f.Pid > 0));
|
||||
Assert.Contains(facts, f => f.SessionId == 0);
|
||||
// Service hosts come and go; a wholesale access failure would drop (nearly) all of them.
|
||||
Assert.True(facts.Count >= expected.Length / 2,
|
||||
$"Snapshot returned {facts.Count} of ~{expected.Length} svchost processes.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SnapshotProcessesByName_returns_empty_for_an_unknown_name()
|
||||
{
|
||||
Assert.Empty(AutopilotMonitor.Agent.V2.Program.SnapshotProcessesByName("no-such-process-" + Guid.NewGuid().ToString("N")));
|
||||
}
|
||||
|
||||
// ================================================================= Crash log writer
|
||||
|
||||
[Fact]
|
||||
|
||||
+2
@@ -247,6 +247,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.SignalAdapters
|
||||
|
||||
f.Tracker.LastMatchedLogTimestamp = ClockNow;
|
||||
f.Tracker.CompletePlatformScriptFromImeResultForTesting("beef7777", "Success", ClockNow);
|
||||
// No end block is seeded — the held result goes out with the shutdown flush.
|
||||
f.Tracker.FlushPendingPlatformScriptResults(f.Clock.UtcNow, force: true);
|
||||
|
||||
var info = f.InfoEvent(SharedEventTypes.ScriptCompleted);
|
||||
// Without the hardening the slot kept the 7-day-old start: the duration would have
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using AutopilotMonitor.Agent.V2.Core.SignalAdapters;
|
||||
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
|
||||
using SharedEventTypes = AutopilotMonitor.Shared.Constants.EventTypes;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Tests.SignalAdapters
|
||||
{
|
||||
/// <summary>
|
||||
/// A held platform-script completion (IME result read before the executor end block) is
|
||||
/// emitted at the end of a later pass, when the tracker's "last matched" line is an unrelated
|
||||
/// one — the event stays bound to the result line's timestamp and pattern.
|
||||
/// </summary>
|
||||
public sealed class ImeLogTrackerAdapterResultHoldTests
|
||||
{
|
||||
private static readonly DateTime ClockNow = new DateTime(2026, 9, 10, 22, 13, 40, DateTimeKind.Utc);
|
||||
|
||||
[Fact]
|
||||
public void Held_completion_is_bound_to_the_result_line_not_to_the_last_matched_one()
|
||||
{
|
||||
using var f = new ImeLogTrackerAdapterFixture(ClockNow);
|
||||
using var adapter = new ImeLogTrackerAdapter(f.Tracker, f.Ingress, f.Clock);
|
||||
|
||||
var startTs = ClockNow.AddSeconds(-95);
|
||||
var resultTs = ClockNow.AddSeconds(-3);
|
||||
|
||||
f.Tracker.LastMatchedLogTimestamp = startTs;
|
||||
f.Tracker.HandlePlatformScriptStarted("376ee51a");
|
||||
|
||||
f.Tracker.LastMatchedLogTimestamp = resultTs;
|
||||
f.Tracker.LastMatchedPatternId = "PS-SCRIPT-RESULT";
|
||||
f.Tracker.CompletePlatformScriptFromImeResultForTesting("376ee51a", "Success", resultTs);
|
||||
Assert.Empty(f.InfoEvents(SharedEventTypes.ScriptCompleted));
|
||||
|
||||
// Other lines were matched before the end block was read and the pass ended.
|
||||
f.Tracker.LastMatchedLogTimestamp = ClockNow.AddSeconds(-1);
|
||||
f.Tracker.LastMatchedPatternId = "IME-DOWNLOADING";
|
||||
f.Tracker.RecordPlatformScriptExitCodeForTesting("376ee51a", 0, resultTs.AddMilliseconds(-8));
|
||||
f.Tracker.FlushPendingPlatformScriptResults(f.Clock.UtcNow);
|
||||
|
||||
var info = f.InfoEvent(SharedEventTypes.ScriptCompleted);
|
||||
Assert.Equal(resultTs, info.OccurredAtUtc);
|
||||
Assert.Equal("PS-SCRIPT-RESULT", info.Payload!["patternId"]);
|
||||
Assert.Equal("0", info.Payload["exitCode"]);
|
||||
Assert.Equal("ime_policy_result", info.Payload["resultSource"]);
|
||||
Assert.Equal("92.00", info.Payload["durationSeconds"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
+24
-1
@@ -33,6 +33,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
|
||||
private readonly Stream _stream;
|
||||
private readonly int _maxLineBytes;
|
||||
private readonly bool _hashLines;
|
||||
private readonly byte[] _readBuffer = new byte[ReadBufferSize];
|
||||
private int _readPos;
|
||||
private int _readLen;
|
||||
@@ -41,21 +42,32 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
|
||||
private byte[] _line = new byte[4096];
|
||||
private int _lineLen;
|
||||
private ulong _lineHash;
|
||||
|
||||
/// <param name="stream">Seekable stream positioned at the first byte to read.</param>
|
||||
/// <param name="maxLineBytes">Upper bound of bytes kept per line; the remainder is discarded.</param>
|
||||
public BoundedLineReader(Stream stream, int maxLineBytes)
|
||||
/// <param name="hashLines">Fingerprint the raw bytes of every line (<see cref="LastLineHash"/>) — the
|
||||
/// tracker's byte ledger needs it for multi-writer files only.</param>
|
||||
public BoundedLineReader(Stream stream, int maxLineBytes, bool hashLines = false)
|
||||
{
|
||||
if (stream == null) throw new ArgumentNullException(nameof(stream));
|
||||
if (maxLineBytes <= 0) throw new ArgumentOutOfRangeException(nameof(maxLineBytes));
|
||||
_stream = stream;
|
||||
_maxLineBytes = maxLineBytes;
|
||||
_hashLines = hashLines;
|
||||
_readBufferFileOffset = stream.Position;
|
||||
}
|
||||
|
||||
/// <summary>Cap in bytes applied to every line.</summary>
|
||||
public int MaxLineBytes => _maxLineBytes;
|
||||
|
||||
/// <summary>
|
||||
/// FNV-1a 64 over the bytes the most recently returned line was decoded from (BOM and
|
||||
/// line terminator excluded; the capped prefix of a truncated line). Zero unless the
|
||||
/// reader was created with hashing on.
|
||||
/// </summary>
|
||||
public ulong LastLineHash => _lineHash;
|
||||
|
||||
/// <summary>Absolute file offset of the next unread byte (exact — no decoder read-ahead).</summary>
|
||||
public long Position => _readBufferFileOffset + _readPos;
|
||||
|
||||
@@ -78,6 +90,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
LastLineTerminated = false;
|
||||
LastLineTruncated = false;
|
||||
_lineLen = 0;
|
||||
_lineHash = _hashLines ? Fnv1a64.OffsetBasis : 0UL;
|
||||
var sawAnyByte = false;
|
||||
|
||||
while (true)
|
||||
@@ -154,6 +167,16 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
// tail (and thus any CR) discarded, so the check is skipped there.
|
||||
if (LastLineTerminated && !LastLineTruncated && len > 0 && _line[start + len - 1] == (byte)'\r')
|
||||
len--;
|
||||
if (_hashLines)
|
||||
{
|
||||
// Fingerprint exactly the bytes the string is decoded from, so a tail that was
|
||||
// first read unterminated and later gained its CRLF hashes the same.
|
||||
var h = Fnv1a64.OffsetBasis;
|
||||
var end = start + len;
|
||||
for (var i = start; i < end; i++)
|
||||
h = (h ^ _line[i]) * Fnv1a64.Prime;
|
||||
_lineHash = h;
|
||||
}
|
||||
return len == 0 ? string.Empty : Encoding.UTF8.GetString(_line, start, len);
|
||||
}
|
||||
}
|
||||
|
||||
+171
@@ -0,0 +1,171 @@
|
||||
using System.Text;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
{
|
||||
/// <summary>FNV-1a 64-bit — the entry fingerprint used by the tracker's byte ledger.</summary>
|
||||
internal static class Fnv1a64
|
||||
{
|
||||
public const ulong OffsetBasis = 14695981039346656037UL;
|
||||
public const ulong Prime = 1099511628211UL;
|
||||
|
||||
public static ulong Fold(ulong hash, ulong value) => (hash ^ value) * Prime;
|
||||
}
|
||||
|
||||
/// <summary>One assembled CMTrace entry: its byte range in the file, the fingerprint of those bytes and the text.</summary>
|
||||
internal struct AssembledEntry
|
||||
{
|
||||
public long Offset;
|
||||
public int Length;
|
||||
public ulong Hash;
|
||||
public string Text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns physical lines into CMTrace entries: a line is one entry, unless it opens
|
||||
/// <c><![LOG[</c> without its <c>]LOG]!></c> close — then the continuation lines up to the
|
||||
/// closing one belong to it. Oversized lines and capped entries are dropped together with
|
||||
/// their remaining continuation lines, so no fragment is ever matched as an entry.
|
||||
/// <para>
|
||||
/// The read loop and the ledger verification (<c>ImeLogTracker.Overwrite.cs</c>) both feed
|
||||
/// this class, which is what makes their entry boundaries — and therefore the fingerprints
|
||||
/// they compare — identical by construction.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
internal sealed class CmTraceEntryAssembler
|
||||
{
|
||||
public enum Outcome
|
||||
{
|
||||
/// <summary>The line was absorbed into an open multiline entry.</summary>
|
||||
Buffered,
|
||||
/// <summary>An entry is complete.</summary>
|
||||
Entry,
|
||||
/// <summary>A line over the byte cap was dropped (its entry with it).</summary>
|
||||
OversizedDropped,
|
||||
/// <summary>An open multiline entry hit the line/char cap and was dropped.</summary>
|
||||
CapDropped,
|
||||
/// <summary>A continuation line of a dropped entry was skipped.</summary>
|
||||
Skipped,
|
||||
}
|
||||
|
||||
private const string LogOpen = "<![LOG[";
|
||||
private const string LogClose = "]LOG]!>";
|
||||
|
||||
private readonly int _maxLines;
|
||||
private readonly int _maxChars;
|
||||
|
||||
private StringBuilder _buffer;
|
||||
private int _lineCount;
|
||||
private long _entryStart;
|
||||
private ulong _entryHash;
|
||||
private bool _skippingDroppedEntry;
|
||||
|
||||
public CmTraceEntryAssembler(int maxMultiLineBufferLines, int maxEntryBytes)
|
||||
{
|
||||
_maxLines = maxMultiLineBufferLines;
|
||||
_maxChars = maxEntryBytes;
|
||||
}
|
||||
|
||||
/// <summary>True while a multiline entry is being assembled.</summary>
|
||||
public bool HasOpenEntry => _buffer != null;
|
||||
|
||||
/// <summary>File offset of the open multiline entry (valid while <see cref="HasOpenEntry"/>).</summary>
|
||||
public long OpenEntryStart => _entryStart;
|
||||
|
||||
/// <summary>Line count of the entry dropped by the last <see cref="Outcome.CapDropped"/>.</summary>
|
||||
public int DroppedLines { get; private set; }
|
||||
|
||||
/// <summary>Char count of the entry dropped by the last <see cref="Outcome.CapDropped"/>.</summary>
|
||||
public int DroppedChars { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Feeds one physical line. <paramref name="lineStart"/> / <paramref name="positionAfter"/>
|
||||
/// are the line's file offsets (start, next unread byte), <paramref name="lineHash"/> the
|
||||
/// reader's raw-byte fingerprint of the line.
|
||||
/// </summary>
|
||||
public Outcome Feed(string line, long lineStart, long positionAfter, bool truncated, ulong lineHash, out AssembledEntry entry)
|
||||
{
|
||||
entry = default(AssembledEntry);
|
||||
|
||||
if (truncated)
|
||||
{
|
||||
// The capped prefix only tells whether the line opened an entry whose remaining
|
||||
// lines must be skipped rather than matched as raw text.
|
||||
if (_buffer != null)
|
||||
{
|
||||
_buffer = null;
|
||||
_lineCount = 0;
|
||||
_skippingDroppedEntry = true;
|
||||
}
|
||||
else if (line.StartsWith(LogOpen) && !line.Contains(LogClose))
|
||||
{
|
||||
_skippingDroppedEntry = true;
|
||||
}
|
||||
return Outcome.OversizedDropped;
|
||||
}
|
||||
|
||||
if (_skippingDroppedEntry)
|
||||
{
|
||||
// A line opening a new entry ends the skip and is processed itself — checked
|
||||
// BEFORE the close-tag test, because a complete single-line entry contains both.
|
||||
if (line.StartsWith(LogOpen))
|
||||
_skippingDroppedEntry = false;
|
||||
else
|
||||
{
|
||||
if (line.Contains(LogClose)) _skippingDroppedEntry = false;
|
||||
return Outcome.Skipped;
|
||||
}
|
||||
}
|
||||
|
||||
if (_buffer != null)
|
||||
{
|
||||
_buffer.Append('\n').Append(line);
|
||||
_lineCount++;
|
||||
_entryHash = Fnv1a64.Fold(_entryHash, lineHash);
|
||||
|
||||
if (line.Contains(LogClose))
|
||||
{
|
||||
entry = new AssembledEntry
|
||||
{
|
||||
Offset = _entryStart,
|
||||
Length = (int)(positionAfter - _entryStart),
|
||||
Hash = _entryHash,
|
||||
Text = _buffer.ToString(),
|
||||
};
|
||||
_buffer = null;
|
||||
_lineCount = 0;
|
||||
return Outcome.Entry;
|
||||
}
|
||||
|
||||
if (_lineCount >= _maxLines || _buffer.Length >= _maxChars)
|
||||
{
|
||||
DroppedLines = _lineCount;
|
||||
DroppedChars = _buffer.Length;
|
||||
_buffer = null;
|
||||
_lineCount = 0;
|
||||
_skippingDroppedEntry = true;
|
||||
return Outcome.CapDropped;
|
||||
}
|
||||
|
||||
return Outcome.Buffered;
|
||||
}
|
||||
|
||||
if (line.StartsWith(LogOpen) && !line.Contains(LogClose))
|
||||
{
|
||||
_buffer = new StringBuilder(line);
|
||||
_lineCount = 1;
|
||||
_entryStart = lineStart;
|
||||
_entryHash = lineHash;
|
||||
return Outcome.Buffered;
|
||||
}
|
||||
|
||||
entry = new AssembledEntry
|
||||
{
|
||||
Offset = lineStart,
|
||||
Length = (int)(positionAfter - lineStart),
|
||||
Hash = lineHash,
|
||||
Text = line,
|
||||
};
|
||||
return Outcome.Entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
+237
-177
@@ -222,20 +222,6 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
// PowerShell script tracking handlers
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
// Returns the current platform-script accumulator, or null when no platform script is active.
|
||||
// Health-script (remediation) state is no longer tracked line-by-line — the HS-NEW-RESULT
|
||||
// pattern delivers the full pre-detection / remediation / post-detection JSON in one shot
|
||||
// via HandleHealthScriptResult. The platform branch keeps its line-by-line accumulator
|
||||
// because PS-* patterns (PS-SCRIPT-CONTEXT / PS-SCRIPT-EXITCODE / PS-AGENT-OUTPUT / …) still
|
||||
// arrive across multiple log lines.
|
||||
private ScriptExecutionState GetCurrentPlatformScript()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_lastPlatformScriptPolicyId) &&
|
||||
_pendingPlatformScripts.TryGetValue(_lastPlatformScriptPolicyId, out var state))
|
||||
return state;
|
||||
return null;
|
||||
}
|
||||
|
||||
private void HandleScriptStarted(Match match, Dictionary<string, string> parameters)
|
||||
{
|
||||
var id = match.Groups["id"]?.Value;
|
||||
@@ -282,27 +268,59 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
// a regex Match).
|
||||
internal void HandlePlatformScriptStarted(string id, string source = null)
|
||||
{
|
||||
// Stale-slot hardening (session eaf3d8c4): a replayed start line from a previous
|
||||
// enrollment whose matching result never appeared (script was mid-run when that
|
||||
// enrollment ended) leaves a pending slot with a days-old StartedAtUtc. When the
|
||||
// same policy genuinely re-runs now, the fresh start must begin a NEW execution —
|
||||
// otherwise the ancient start pairs with the fresh completion and the duration
|
||||
// lies. > 24 h mirrors the adapter's source-timestamp staleness clamp.
|
||||
var currentStartTs = LastMatchedLogTimestamp ?? DateTime.UtcNow;
|
||||
if (_pendingPlatformScripts.TryGetValue(id, out var pendingSlot)
|
||||
&& pendingSlot.StartedAtUtc.HasValue
|
||||
&& currentStartTs - pendingSlot.StartedAtUtc.Value > TimeSpan.FromHours(24))
|
||||
var currentStartTs = LastMatchedLogTimestamp ?? UtcNowProvider();
|
||||
|
||||
// A start line older than the result this policy already emitted is the late start
|
||||
// line of THAT run: its AgentExecutor block surfaced after the IME result — the two
|
||||
// files are read in turn (AgentExecutor.log first, but a script's exit line and IME's
|
||||
// result line are 8–30 ms apart), a ledger rewind recovered the block, or the poll
|
||||
// loop stalled (session e7f3c910: the late line opened a second slot, cleared the
|
||||
// emitted-marker and turned the exit code that followed into a fallback duplicate
|
||||
// 15 s later). It owns the lines behind it in its file and opens nothing. Strictly
|
||||
// older: a start line at the result's own timestamp is the next run (source lines
|
||||
// never tie; a low-resolution clock in a test may).
|
||||
if (_platformScriptResultEmitted.TryGetValue(id, out var emittedResultTs) && currentStartTs < emittedResultTs)
|
||||
{
|
||||
_logger.Debug($"ImeLogTracker: discarding stale pending platform-script slot for {id} (started {pendingSlot.StartedAtUtc:o})");
|
||||
_pendingPlatformScripts.Remove(id);
|
||||
RecordInvocationMarker(id, isClose: false);
|
||||
NoteInvocationActivity();
|
||||
_logger.Debug($"ImeLogTracker: platform script start line for {id} (source: {source ?? "ime"}) predates its emitted result — late line of that run, not a new one");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_pendingPlatformScripts.TryGetValue(id, out var pendingSlot))
|
||||
{
|
||||
if (pendingSlot.StartedAtUtc.HasValue && currentStartTs - pendingSlot.StartedAtUtc.Value > TimeSpan.FromHours(24))
|
||||
{
|
||||
// Stale-slot hardening (session eaf3d8c4): a replayed start line from a previous
|
||||
// enrollment whose matching result never appeared (script was mid-run when that
|
||||
// enrollment ended) leaves a pending slot with a days-old StartedAtUtc. When the
|
||||
// same policy genuinely re-runs now, the fresh start must begin a NEW execution —
|
||||
// otherwise the ancient start pairs with the fresh completion and the duration
|
||||
// lies. > 24 h mirrors the adapter's source-timestamp staleness clamp.
|
||||
_logger.Debug($"ImeLogTracker: discarding stale pending platform-script slot for {id} (started {pendingSlot.StartedAtUtc:o})");
|
||||
_pendingPlatformScripts.Remove(id);
|
||||
}
|
||||
else if (pendingSlot.Result != null && pendingSlot.ResultObservedAtUtc.HasValue && currentStartTs >= pendingSlot.ResultObservedAtUtc.Value)
|
||||
{
|
||||
// The held run is over — a start line at or after its result is the next run.
|
||||
// Emit what the held run has; its end block did not surface in time.
|
||||
EmitPlatformScriptResult(pendingSlot, "the next run of this policy started");
|
||||
}
|
||||
else if (!pendingSlot.StartedAtUtc.HasValue)
|
||||
{
|
||||
// The slot was opened by its result while the start block was still hidden;
|
||||
// the recovered start line dates the run.
|
||||
pendingSlot.StartedAtUtc = currentStartTs;
|
||||
_stateDirty = true;
|
||||
}
|
||||
}
|
||||
if (!_pendingPlatformScripts.ContainsKey(id))
|
||||
{
|
||||
// A fresh start for this policy (no pending entry) begins a NEW execution.
|
||||
// Clear any emitted-marker from a prior run so IME re-evaluations / retries of
|
||||
// the same platform-script policy within one agent lifetime are not silently
|
||||
// deduped away. Within a single run the start always precedes exit/result, so
|
||||
// this never clears the current run's own marker.
|
||||
// deduped away. A start line of the emitted run itself never reaches this point
|
||||
// (the guard above), so this never clears the current run's own marker.
|
||||
_platformScriptResultEmitted.Remove(id);
|
||||
_pendingPlatformScripts[id] = new ScriptExecutionState
|
||||
{
|
||||
@@ -320,7 +338,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
try { OnScriptStarted?.Invoke(new ScriptStartedInfo { PolicyId = id, ScriptType = "platform" }); }
|
||||
catch (Exception ex) { _logger.Warning($"ImeLogTracker: OnScriptStarted handler threw: {ex.Message}"); }
|
||||
}
|
||||
_lastPlatformScriptPolicyId = id;
|
||||
RecordInvocationMarker(id, isClose: false);
|
||||
NoteInvocationActivity();
|
||||
// Started lines fire twice per script (agentexecutor + ime source) and carry no
|
||||
// outcome — the matching `platform script completed` line below carries result+exit
|
||||
// and stays on Info. Keep starts on Debug so Info reflects script outcomes only.
|
||||
@@ -329,42 +348,55 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
|
||||
// Routes line-by-line script-data handlers (context / exitCode / output) to the right
|
||||
// accumulator based on the pattern's scriptType parameter. Health-script lines feed the
|
||||
// single-slot _pendingHealthScript; platform-script lines feed _pendingPlatformScripts.
|
||||
// single-slot _pendingHealthScript (the HS-NEW-RESULT JSON delivers the full cycle in one
|
||||
// shot; the slot only enriches the early HS-COMPLIANCE signal). Platform-script lines feed
|
||||
// _pendingPlatformScripts, and the slot that owns a line is decided by file position (see
|
||||
// ImeLogTracker.Overwrite.cs), never by which script happened to start last.
|
||||
private ScriptExecutionState GetCurrentScriptForLineUpdate(Dictionary<string, string> parameters)
|
||||
=> GetCurrentScriptForLineUpdate(parameters, out _);
|
||||
|
||||
// ownerPolicyId names the platform script that owns the line by file position even when
|
||||
// its pending slot is gone (the completion was already emitted); null for health-script
|
||||
// lines and for lines no platform invocation owns.
|
||||
private ScriptExecutionState GetCurrentScriptForLineUpdate(Dictionary<string, string> parameters, out string ownerPolicyId)
|
||||
{
|
||||
var scriptType = parameters != null && parameters.TryGetValue("scriptType", out var st) ? st : null;
|
||||
return string.Equals(scriptType, "remediation", StringComparison.OrdinalIgnoreCase)
|
||||
? _pendingHealthScript
|
||||
: GetCurrentPlatformScript();
|
||||
if (string.Equals(scriptType, "remediation", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
ownerPolicyId = null;
|
||||
return _pendingHealthScript;
|
||||
}
|
||||
return ResolvePlatformScriptForCurrentEntry(out ownerPolicyId);
|
||||
}
|
||||
|
||||
// Session 6b4993e5 fix — platform-script stdout/exit cross-contamination.
|
||||
//
|
||||
// AgentExecutor.exe hosts BOTH platform scripts (…\Policies\Scripts\<id>.ps1) and
|
||||
// proactive-remediation scripts (…\IMECache\HealthScripts\<id>\detect.ps1), and writes
|
||||
// their lines interleaved into the shared AgentExecutor.log. The policyId-less
|
||||
// PS-AGENT-EXITCODE ("Powershell exit code is N") and PS-AGENT-OUTPUT ("write output
|
||||
// done. output = …") lines are routed to whichever platform script
|
||||
// _lastPlatformScriptPolicyId points at. A remediation invocation's start
|
||||
// ("Adding argument remediationScript …") does NOT match PS-AGENT-SCRIPT-START, so it
|
||||
// never moves the pointer — its exit/output then bleed into the last-started PLATFORM
|
||||
// script's slot (observed: platform c3e0124c emitted result=Failed but with exit 0 +
|
||||
// stdout "[Compliant] No Classic Teams found" — the Teams remediation's output).
|
||||
//
|
||||
// Each AgentExecutor process logs "ExecutorLog AgentExecutor gets invoked" first, so a
|
||||
// new banner means the previous invocation's line-capture context is over. Clearing the
|
||||
// pointer here scopes line-capture to a single invocation: the immediately following
|
||||
// "Adding argument powershell …\Policies\Scripts\<id>.ps1" (PS-AGENT-SCRIPT-START)
|
||||
// re-establishes it for a platform invocation, while a remediation invocation leaves it
|
||||
// null so its exit/output are dropped (the remediation's authoritative data still
|
||||
// arrives via HS-NEW-RESULT). The platform script's final result is unaffected — it is
|
||||
// keyed by policyId via PS-SCRIPT-RESULT, not by this pointer.
|
||||
private void HandleAgentExecutorInvocationBoundary()
|
||||
// AgentExecutor.exe hosts platform scripts (…\Policies\Scripts\<id>.ps1) as well as
|
||||
// detection, requirement and proactive-remediation scripts, and its policyId-less
|
||||
// PS-AGENT-EXITCODE / PS-AGENT-OUTPUT lines belong to whichever invocation owns that
|
||||
// file position. The banner closes the previous invocation; the argument line that
|
||||
// follows opens the next one — as a platform script when PS-AGENT-SCRIPT-START matched
|
||||
// it, otherwise as an invocation whose exit/output are not captured (a remediation's
|
||||
// authoritative data arrives via HS-NEW-RESULT; session 6b4993e5 saw its output bleed
|
||||
// into a platform slot). "Agent executor completed" closes the invocation again.
|
||||
private void HandleAgentExecutorInvocation()
|
||||
{
|
||||
if (_lastPlatformScriptPolicyId == null) return;
|
||||
_logger.Debug(
|
||||
$"ImeLogTracker: AgentExecutor invocation boundary — clearing platform-script line-capture pointer (was {_lastPlatformScriptPolicyId}).");
|
||||
_lastPlatformScriptPolicyId = null;
|
||||
RecordInvocationMarker(null, isClose: true);
|
||||
NoteInvocationActivity();
|
||||
}
|
||||
|
||||
private void HandleAgentInvocationArgument(string kind)
|
||||
{
|
||||
RecordInvocationMarker(null, isClose: false);
|
||||
NoteInvocationActivity();
|
||||
if (!string.IsNullOrEmpty(kind) && !string.Equals(kind, "powershell", StringComparison.OrdinalIgnoreCase))
|
||||
_logger.Debug($"ImeLogTracker: AgentExecutor invocation of kind '{kind}' — exit/output lines not captured as a platform script");
|
||||
}
|
||||
|
||||
private void HandleAgentExecutorCompleted()
|
||||
{
|
||||
RecordInvocationMarker(null, isClose: true);
|
||||
// The end block was written at the executor's own stream position — if that lay
|
||||
// behind the bookmark, the ledger check on the next pass finds it.
|
||||
RequestOverwriteCheck();
|
||||
}
|
||||
|
||||
private void HandleScriptContext(Match match, Dictionary<string, string> parameters)
|
||||
@@ -372,6 +404,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
var context = match.Groups["context"]?.Value;
|
||||
if (string.IsNullOrEmpty(context)) return;
|
||||
|
||||
NoteInvocationActivity();
|
||||
var runContext = string.Equals(context, "machine", StringComparison.OrdinalIgnoreCase) ? "System" : "User";
|
||||
var script = GetCurrentScriptForLineUpdate(parameters);
|
||||
if (script != null)
|
||||
@@ -386,18 +419,27 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
var exitCodeStr = match.Groups["exitCode"]?.Value;
|
||||
if (string.IsNullOrEmpty(exitCodeStr) || !int.TryParse(exitCodeStr, out var exitCode)) return;
|
||||
|
||||
var script = GetCurrentScriptForLineUpdate(parameters);
|
||||
var script = GetCurrentScriptForLineUpdate(parameters, out var ownerPolicyId);
|
||||
if (script != null)
|
||||
{
|
||||
script.ExitCode = exitCode;
|
||||
// Stamp when we learned the script's exit code so the deadline-based fallback
|
||||
// (FlushStalePlatformScriptResults) can fire if IME never logs its authoritative
|
||||
// PS-SCRIPT-RESULT line in time. Prefer the source CMTrace timestamp (already
|
||||
// UTC-normalized by the parser) so replayed log content is dated correctly.
|
||||
if (string.Equals(script.ScriptType, "platform", StringComparison.OrdinalIgnoreCase))
|
||||
script.ExitObservedAtUtc = LastMatchedLogTimestamp ?? DateTime.UtcNow;
|
||||
_logger.Debug($"ImeLogTracker: script exit code {exitCode} for {script.PolicyId}");
|
||||
}
|
||||
ApplyExitCode(script, exitCode, LastMatchedLogTimestamp ?? UtcNowProvider());
|
||||
else if (ownerPolicyId != null)
|
||||
_logger.Debug($"ImeLogTracker: exit code {exitCode} belongs to platform script {ownerPolicyId}, whose completion was already emitted — dropped");
|
||||
// An exit code means the executor just wrote its end block and IME is about to
|
||||
// write the result — both land at a stale stream position when another process
|
||||
// appended meanwhile. Check the ledgers on the next pass.
|
||||
RequestOverwriteCheck();
|
||||
}
|
||||
|
||||
private void ApplyExitCode(ScriptExecutionState script, int exitCode, DateTime observedAtUtc)
|
||||
{
|
||||
script.ExitCode = exitCode;
|
||||
// Stamp when we learned the script's exit code so the deadline-based fallback
|
||||
// (FlushPendingPlatformScriptResults) can fire if IME never logs its authoritative
|
||||
// PS-SCRIPT-RESULT line in time. Prefer the source CMTrace timestamp (already
|
||||
// UTC-normalized by the parser) so replayed log content is dated correctly.
|
||||
if (string.Equals(script.ScriptType, "platform", StringComparison.OrdinalIgnoreCase))
|
||||
script.ExitObservedAtUtc = observedAtUtc;
|
||||
_logger.Debug($"ImeLogTracker: script exit code {exitCode} for {script.PolicyId}");
|
||||
}
|
||||
|
||||
private void HandleScriptOutput(Match match, Dictionary<string, string> parameters)
|
||||
@@ -446,22 +488,36 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
|
||||
if (string.IsNullOrEmpty(id)) return;
|
||||
|
||||
// Dedup: the deadline-based fallback (FlushStalePlatformScriptResults) may already have
|
||||
CompletePlatformScriptFromImeResult(id, result, LastMatchedLogTimestamp, LastMatchedPatternId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The authoritative IME <c>PS-SCRIPT-RESULT</c> line of a platform script. Emits at once
|
||||
/// when the executor end block (exit code, output) has been read already — the normal
|
||||
/// order of the two files — and otherwise holds the completion: the end block reaches the
|
||||
/// tracker after the result whenever a concurrent writer overwrote it (the check this
|
||||
/// result triggers recovers it one pass later), when the AgentExecutor.log read of a pass
|
||||
/// simply preceded the exit line by a few milliseconds, or when the poll loop stalled.
|
||||
/// Emitting immediately lost the exit code in all three cases and, once the late start
|
||||
/// line reopened the slot, produced a fallback duplicate (session e7f3c910).
|
||||
/// <see cref="FlushPendingPlatformScriptResults"/> emits the held completion.
|
||||
/// </summary>
|
||||
internal void CompletePlatformScriptFromImeResult(string policyId, string result, DateTime? resultLineTimestampUtc, string patternId)
|
||||
{
|
||||
// Dedup: the deadline-based fallback (FlushPendingPlatformScriptResults) may already have
|
||||
// emitted this script from its AgentExecutor exit code because IME's authoritative
|
||||
// PS-SCRIPT-RESULT line arrived late. The fallback carries the same exit code + stdout,
|
||||
// so re-emitting here would duplicate the timeline entry and inflate counts. Drop the
|
||||
// now-redundant pending entry and skip.
|
||||
if (_platformScriptResultEmitted.Contains(id))
|
||||
if (_platformScriptResultEmitted.ContainsKey(policyId))
|
||||
{
|
||||
_logger.Debug($"ImeLogTracker: platform script {id} PS-SCRIPT-RESULT arrived after fallback emit — skipping duplicate");
|
||||
_pendingPlatformScripts.Remove(id);
|
||||
if (string.Equals(_lastPlatformScriptPolicyId, id, StringComparison.OrdinalIgnoreCase))
|
||||
_lastPlatformScriptPolicyId = null;
|
||||
_logger.Debug($"ImeLogTracker: platform script {policyId} PS-SCRIPT-RESULT arrived after fallback emit — skipping duplicate");
|
||||
_pendingPlatformScripts.Remove(policyId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Merge with pending AgentExecutor data if available
|
||||
if (_pendingPlatformScripts.TryGetValue(id, out var script))
|
||||
if (_pendingPlatformScripts.TryGetValue(policyId, out var script))
|
||||
{
|
||||
// Stale-result guard: on an intra-lifetime re-run of the same policy, run 1's
|
||||
// late PS-SCRIPT-RESULT can arrive after run 2's start line already created a
|
||||
@@ -471,10 +527,10 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
// line in the source log, so a result older than the pending slot's start
|
||||
// belongs to a previous run — drop it and keep the live slot intact.
|
||||
if (script.StartedAtUtc.HasValue
|
||||
&& LastMatchedLogTimestamp.HasValue
|
||||
&& LastMatchedLogTimestamp.Value < script.StartedAtUtc.Value)
|
||||
&& resultLineTimestampUtc.HasValue
|
||||
&& resultLineTimestampUtc.Value < script.StartedAtUtc.Value)
|
||||
{
|
||||
_logger.Debug($"ImeLogTracker: platform script {id} result predates the current run's start — stale result from a previous run, skipping");
|
||||
_logger.Debug($"ImeLogTracker: platform script {policyId} result predates the current run's start — stale result from a previous run, skipping");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -482,70 +538,111 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
{
|
||||
script = new ScriptExecutionState
|
||||
{
|
||||
PolicyId = id,
|
||||
PolicyId = policyId,
|
||||
ScriptType = "platform"
|
||||
};
|
||||
_pendingPlatformScripts[policyId] = script;
|
||||
}
|
||||
|
||||
script.Result = result;
|
||||
script.ResultSource = "ime_policy_result";
|
||||
|
||||
_logger.Info($"ImeLogTracker: platform script completed: {id}, result={result}, exit={script.ExitCode}");
|
||||
|
||||
EmitScriptEvent(script);
|
||||
_platformScriptResultEmitted.Add(id);
|
||||
_pendingPlatformScripts.Remove(id);
|
||||
script.ResultObservedAtUtc = resultLineTimestampUtc ?? UtcNowProvider();
|
||||
script.ResultPatternId = patternId;
|
||||
_stateDirty = true;
|
||||
|
||||
if (script.ExitCode.HasValue)
|
||||
{
|
||||
EmitPlatformScriptResult(script);
|
||||
}
|
||||
else
|
||||
{
|
||||
script.ResultHeldSinceUtc = UtcNowProvider();
|
||||
_logger.Debug($"ImeLogTracker: platform script {policyId} result={result} seen before its executor end block — holding the completion up to {PlatformScriptEndBlockGrace.TotalSeconds:F0}s for the exit code");
|
||||
}
|
||||
// The result block is IME's first write after the script wait — the executor's end
|
||||
// block in the other file may have landed behind the bookmark at the same time.
|
||||
RequestOverwriteCheck();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The one emit path for platform-script completions (IME result, held result, exit-code
|
||||
/// fallback): event, emitted-marker with the run's source timestamp, slot closed.
|
||||
/// </summary>
|
||||
private void EmitPlatformScriptResult(ScriptExecutionState script, string note = null)
|
||||
{
|
||||
_logger.Info($"ImeLogTracker: platform script completed: {script.PolicyId}, result={script.Result}, exit={script.ExitCode}{(note != null ? $" ({note})" : string.Empty)}");
|
||||
EmitScriptEvent(script);
|
||||
_platformScriptResultEmitted[script.PolicyId] = script.ResultObservedAtUtc ?? script.ExitObservedAtUtc ?? UtcNowProvider();
|
||||
_pendingPlatformScripts.Remove(script.PolicyId);
|
||||
_stateDirty = true;
|
||||
if (string.Equals(_lastPlatformScriptPolicyId, id, StringComparison.OrdinalIgnoreCase))
|
||||
_lastPlatformScriptPolicyId = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Grace period after a platform script's AgentExecutor exit code is observed before
|
||||
/// <see cref="FlushStalePlatformScriptResults"/> emits a completion from that exit code.
|
||||
/// IME logs its authoritative <c>PS-SCRIPT-RESULT</c> line within ~1 s of exit on healthy
|
||||
/// reporting cycles, so 15 s lets the normal path win virtually always — the fallback is a
|
||||
/// true safety net for the case where IME has not flushed its batch-send to the Microsoft
|
||||
/// service before the (often short) Autopilot enrollment ends and the agent terminates.
|
||||
/// <see cref="FlushPendingPlatformScriptResults"/> emits a completion from that exit code.
|
||||
/// IME logs its authoritative <c>PS-SCRIPT-RESULT</c> line within ~1 s of exit; when it
|
||||
/// is missing after 15 s it was hidden by a concurrent writer (the ledger check in
|
||||
/// <c>ImeLogTracker.Overwrite.cs</c> recovers that line) or the enrollment ended before
|
||||
/// IME wrote it — this fallback is the safety net for the remaining cases.
|
||||
/// </summary>
|
||||
private static readonly TimeSpan PlatformScriptResultGrace = TimeSpan.FromSeconds(15);
|
||||
|
||||
/// <summary>
|
||||
/// Policy IDs of platform scripts already emitted (via either the authoritative
|
||||
/// PS-SCRIPT-RESULT path or the exit-code fallback). Guards against double emission when
|
||||
/// both fire for the same script.
|
||||
/// How long an IME result waits for the executor end block of its run before the
|
||||
/// completion goes out without an exit code. An overwritten end block surfaces on the
|
||||
/// pass after the result (100 ms, the result triggers the ledger check); a stalled poll
|
||||
/// loop was measured at 2 s (session e7f3c910, VM at 100 % CPU). Well below the 15 s
|
||||
/// exit-code fallback, and the event keeps the result line's timestamp — only its
|
||||
/// delivery waits.
|
||||
/// </summary>
|
||||
private readonly HashSet<string> _platformScriptResultEmitted =
|
||||
new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
internal static readonly TimeSpan PlatformScriptEndBlockGrace = TimeSpan.FromSeconds(5);
|
||||
|
||||
/// <summary>
|
||||
/// Emits a <c>script_completed</c>/<c>script_failed</c> for every pending platform script
|
||||
/// that has a known AgentExecutor exit code but whose IME <c>PS-SCRIPT-RESULT</c> line has
|
||||
/// not arrived within <see cref="PlatformScriptResultGrace"/>. Without this, scripts that
|
||||
/// completed shortly before the agent terminates are silently dropped (the
|
||||
/// <c>_pendingPlatformScripts</c> buffer is neither persisted nor flushed on dispose), so a
|
||||
/// device that ran N platform scripts could surface only the long-running one that happened
|
||||
/// to get its IME result logged in time. Result is derived from the exit code (0 → Success,
|
||||
/// else Failed) and tagged <c>resultSource=agentexecutor_fallback</c> so the data is honest
|
||||
/// about its provenance.
|
||||
/// <para>
|
||||
/// Runs on the single-threaded polling loop (no locking needed, same as the handlers).
|
||||
/// <paramref name="force"/> bypasses the grace check for the final pass on shutdown.
|
||||
/// </para>
|
||||
/// Platform scripts already emitted (via the authoritative PS-SCRIPT-RESULT path or the
|
||||
/// exit-code fallback), keyed by policy id with the source timestamp of the emitted run
|
||||
/// (the result line, or the exit line for a fallback emit). Guards against double emission
|
||||
/// when both paths fire for the same script, and tells a late start line of the emitted
|
||||
/// run from the start of the next run.
|
||||
/// </summary>
|
||||
internal void FlushStalePlatformScriptResults(DateTime nowUtc, bool force = false)
|
||||
private readonly Dictionary<string, DateTime> _platformScriptResultEmitted =
|
||||
new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>
|
||||
/// End-of-pass emit for pending platform scripts, two cases:
|
||||
/// <list type="bullet">
|
||||
/// <item>A held IME result (<see cref="ScriptExecutionState.ResultHeldSinceUtc"/>) goes
|
||||
/// out once its executor end block has been read — the block arrives within one pass, so
|
||||
/// stdout/stderr are in by now — or after <see cref="PlatformScriptEndBlockGrace"/>.</item>
|
||||
/// <item>A script with a known AgentExecutor exit code but no IME <c>PS-SCRIPT-RESULT</c>
|
||||
/// line within <see cref="PlatformScriptResultGrace"/> is emitted from the exit code
|
||||
/// (0 → Success, else Failed), tagged <c>resultSource=agentexecutor_fallback</c> so the
|
||||
/// data is honest about its provenance. Without this, scripts that completed shortly
|
||||
/// before the agent terminates were silently dropped.</item>
|
||||
/// </list>
|
||||
/// Runs on the single-threaded polling loop (no locking needed, same as the handlers).
|
||||
/// <paramref name="force"/> bypasses both grace checks for the final pass on shutdown.
|
||||
/// </summary>
|
||||
internal void FlushPendingPlatformScriptResults(DateTime nowUtc, bool force = false)
|
||||
{
|
||||
if (_pendingPlatformScripts.Count == 0) return;
|
||||
|
||||
List<string> toRemove = null;
|
||||
foreach (var kv in _pendingPlatformScripts)
|
||||
var fallbackEmitted = false;
|
||||
foreach (var script in _pendingPlatformScripts.Values.ToList())
|
||||
{
|
||||
var script = kv.Value;
|
||||
|
||||
// Already emitted by the authoritative path — just drop the stale buffer entry.
|
||||
if (_platformScriptResultEmitted.Contains(script.PolicyId))
|
||||
if (_platformScriptResultEmitted.ContainsKey(script.PolicyId))
|
||||
{
|
||||
(toRemove ??= new List<string>()).Add(kv.Key);
|
||||
_pendingPlatformScripts.Remove(script.PolicyId);
|
||||
_stateDirty = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (script.Result != null)
|
||||
{
|
||||
if (script.ExitCode.HasValue)
|
||||
EmitPlatformScriptResult(script, "executor end block read after the result");
|
||||
else if (force || !script.ResultHeldSinceUtc.HasValue || nowUtc - script.ResultHeldSinceUtc.Value >= PlatformScriptEndBlockGrace)
|
||||
EmitPlatformScriptResult(script, $"no executor end block within {PlatformScriptEndBlockGrace.TotalSeconds:F0}s{(force ? ", shutdown flush" : string.Empty)}");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -560,30 +657,14 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
|
||||
script.Result = script.ExitCode.Value == 0 ? "Success" : "Failed";
|
||||
script.ResultSource = "agentexecutor_fallback";
|
||||
|
||||
_logger.Info(
|
||||
$"ImeLogTracker: platform script {script.PolicyId} result not seen within " +
|
||||
$"{PlatformScriptResultGrace.TotalSeconds:F0}s{(force ? " (shutdown flush)" : "")} — " +
|
||||
$"emitting from AgentExecutor exit code {script.ExitCode.Value} (fallback)");
|
||||
|
||||
EmitScriptEvent(script);
|
||||
_platformScriptResultEmitted.Add(script.PolicyId);
|
||||
(toRemove ??= new List<string>()).Add(kv.Key);
|
||||
EmitPlatformScriptResult(script,
|
||||
$"result not seen within {PlatformScriptResultGrace.TotalSeconds:F0}s{(force ? ", shutdown flush" : string.Empty)} — emitted from AgentExecutor exit code {script.ExitCode.Value} (fallback)");
|
||||
fallbackEmitted = true;
|
||||
}
|
||||
|
||||
if (toRemove != null)
|
||||
{
|
||||
// The flush can fire on a quiet polling cycle (grace expiry with no new log
|
||||
// lines), where nothing else marks state dirty — the emitted-markers must reach
|
||||
// the state file before a restart or the restarted tracker re-emits (H1).
|
||||
_stateDirty = true;
|
||||
foreach (var key in toRemove)
|
||||
{
|
||||
_pendingPlatformScripts.Remove(key);
|
||||
if (string.Equals(_lastPlatformScriptPolicyId, key, StringComparison.OrdinalIgnoreCase))
|
||||
_lastPlatformScriptPolicyId = null;
|
||||
}
|
||||
}
|
||||
// The fallback is an end signal like the others: the result line it stood in for
|
||||
// may sit overwritten behind the bookmark of the other file.
|
||||
if (fallbackEmitted) RequestOverwriteCheck();
|
||||
}
|
||||
|
||||
/// <summary>Test seam: inject a pending platform script as if AgentExecutor.log had reported
|
||||
@@ -602,49 +683,26 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
Stdout = stdout,
|
||||
StartedAtUtc = startedAtUtc,
|
||||
};
|
||||
_lastPlatformScriptPolicyId = policyId;
|
||||
NextTestEntry();
|
||||
RecordInvocationMarker(policyId, isClose: false);
|
||||
}
|
||||
|
||||
/// <summary>Test seam: simulate the authoritative IME PS-SCRIPT-RESULT path for a platform
|
||||
/// script (same code as <see cref="HandleScriptCompleted"/> minus regex extraction).
|
||||
/// <paramref name="resultLineTimestampUtc"/> stands in for the CMTrace timestamp of the
|
||||
/// PS-SCRIPT-RESULT line (production reads it via <see cref="LastMatchedLogTimestamp"/>).</summary>
|
||||
/// <summary>Test seam: the authoritative IME PS-SCRIPT-RESULT path for a platform script
|
||||
/// (the production handler minus regex extraction). <paramref name="resultLineTimestampUtc"/>
|
||||
/// stands in for the CMTrace timestamp of the PS-SCRIPT-RESULT line (production reads it
|
||||
/// via <see cref="LastMatchedLogTimestamp"/>, the fallback here).</summary>
|
||||
internal void CompletePlatformScriptFromImeResultForTesting(string policyId, string result, DateTime? resultLineTimestampUtc = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(policyId)) return;
|
||||
CompletePlatformScriptFromImeResult(policyId, result, resultLineTimestampUtc ?? LastMatchedLogTimestamp, "PS-SCRIPT-RESULT");
|
||||
}
|
||||
|
||||
if (_platformScriptResultEmitted.Contains(policyId))
|
||||
{
|
||||
_pendingPlatformScripts.Remove(policyId);
|
||||
if (string.Equals(_lastPlatformScriptPolicyId, policyId, StringComparison.OrdinalIgnoreCase))
|
||||
_lastPlatformScriptPolicyId = null;
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>Test seam: the AgentExecutor exit-code line of a pending platform script (the
|
||||
/// production handler minus regex extraction and positional ownership).</summary>
|
||||
internal void RecordPlatformScriptExitCodeForTesting(string policyId, int exitCode, DateTime observedAtUtc)
|
||||
{
|
||||
if (_pendingPlatformScripts.TryGetValue(policyId, out var script))
|
||||
{
|
||||
// Mirror of HandleScriptCompleted's stale-result guard: a result older than the
|
||||
// pending slot's start belongs to a previous run of the same policy.
|
||||
if (script.StartedAtUtc.HasValue
|
||||
&& resultLineTimestampUtc.HasValue
|
||||
&& resultLineTimestampUtc.Value < script.StartedAtUtc.Value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
script = new ScriptExecutionState { PolicyId = policyId, ScriptType = "platform" };
|
||||
}
|
||||
|
||||
script.Result = result;
|
||||
script.ResultSource = "ime_policy_result";
|
||||
EmitScriptEvent(script);
|
||||
_platformScriptResultEmitted.Add(policyId);
|
||||
_pendingPlatformScripts.Remove(policyId);
|
||||
if (string.Equals(_lastPlatformScriptPolicyId, policyId, StringComparison.OrdinalIgnoreCase))
|
||||
_lastPlatformScriptPolicyId = null;
|
||||
_stateDirty = true;
|
||||
ApplyExitCode(script, exitCode, observedAtUtc);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -794,6 +852,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
return;
|
||||
}
|
||||
HandleHealthScriptResultJson(json);
|
||||
// A remediation executor just ended too — its end block may sit behind the bookmark.
|
||||
RequestOverwriteCheck();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+259
-243
@@ -78,243 +78,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
|
||||
try
|
||||
{
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
if (!fileInfo.Exists) continue;
|
||||
|
||||
// Captured BEFORE MarkChecked below stamps this pass. Growth measured
|
||||
// against a previously observed state is what makes this pass's lines valid
|
||||
// "written now" anchors — for the per-file measurement AND per-line anchoring.
|
||||
var hadPreviousObservation = _positionTracker.HasSeen(filePath);
|
||||
var lastCheckedUtc = _positionTracker.GetLastCheckedUtc(filePath);
|
||||
var passNowUtc = UtcNowProvider();
|
||||
|
||||
var startPos = _positionTracker.GetSafePosition(filePath, fileInfo.Length);
|
||||
|
||||
// Every look counts, including "no new data" and an empty first sight: the
|
||||
// NEXT pass's freshness window is measured from here. Restored bookmarks
|
||||
// deliberately carry no LastCheckedUtc — the first pass after a restart reads
|
||||
// downtime backlog and must never count as fresh.
|
||||
_positionTracker.MarkChecked(filePath, passNowUtc);
|
||||
|
||||
if (startPos >= fileInfo.Length)
|
||||
{
|
||||
// M2: guard the interpolated string so it isn't built every 100 ms tick
|
||||
// (per file) when Trace is off — which is the production default (Info).
|
||||
if (_logger.LogLevel >= AgentLogLevel.Trace)
|
||||
_logger.Trace($"ImeLogTracker: {Path.GetFileName(filePath)} — no new data (pos={startPos}, size={fileInfo.Length})");
|
||||
continue;
|
||||
}
|
||||
if (_logger.LogLevel >= AgentLogLevel.Trace)
|
||||
_logger.Trace($"ImeLogTracker: reading {Path.GetFileName(filePath)} from pos {startPos} (size={fileInfo.Length}, delta={fileInfo.Length - startPos})");
|
||||
|
||||
_currentSourceFileName = Path.GetFileName(filePath);
|
||||
_currentPassLinesAreFresh = hadPreviousObservation
|
||||
&& lastCheckedUtc > DateTime.MinValue
|
||||
&& (passNowUtc - lastCheckedUtc) <= FreshLineMaxAge;
|
||||
|
||||
// Newest bias-less line of this pass — the calibration anchor. Bias-carrying
|
||||
// lines are skipped: they already state the writer's offset, so they need no
|
||||
// measurement and must not overwrite one.
|
||||
CmTraceLogEntry calibrationAnchor = null;
|
||||
|
||||
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
||||
{
|
||||
stream.Seek(startPos, SeekOrigin.Begin);
|
||||
|
||||
var reader = new BoundedLineReader(stream, MaxEntryBytes);
|
||||
var stats = new PassStats();
|
||||
|
||||
// Buffer for multiline CMTrace entries (e.g. AgentExecutor.log
|
||||
// "write output done. output = ..." spans many lines)
|
||||
StringBuilder multiLineBuffer = null;
|
||||
int multiLineCount = 0;
|
||||
// Set after a capped entry is dropped: its remaining physical lines
|
||||
// are skipped instead of being matched as raw text, until the entry
|
||||
// closes or a new entry begins (so no real entry is lost).
|
||||
bool skippingDroppedEntry = false;
|
||||
// File offset where the entry currently being assembled/processed
|
||||
// begins — the bookmark to fall back to when that entry is NOT
|
||||
// consumed (cancellation, or a held-back unterminated tail).
|
||||
long entryStart = startPos;
|
||||
// Where the next pass resumes. EOF unless an entry is left unconsumed.
|
||||
long resumePos;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// CancellationToken.None: cancellation is honoured by the explicit
|
||||
// check below (exact bookmark) rather than by an exception out of
|
||||
// the read, which the poll loop would log as an error.
|
||||
var line = await reader.ReadLineAsync(CancellationToken.None);
|
||||
if (line == null)
|
||||
{
|
||||
// EOF. An in-flight multiline entry here is an entry the writer
|
||||
// has not finished — hold it back (see HoldBackTail) instead of
|
||||
// dropping it and raw-matching its remaining lines next pass.
|
||||
if (multiLineBuffer != null && HoldBackTail(filePath, entryStart, fileInfo.Length, passNowUtc))
|
||||
{
|
||||
_heldTailCount++;
|
||||
resumePos = entryStart;
|
||||
break;
|
||||
}
|
||||
resumePos = reader.Position;
|
||||
break;
|
||||
}
|
||||
|
||||
if (token.IsCancellationRequested)
|
||||
{
|
||||
// Exact bookmark: nothing of this line (or of the multiline
|
||||
// entry it belongs to) has been handled.
|
||||
resumePos = multiLineBuffer != null ? entryStart : reader.LastLineStart;
|
||||
break;
|
||||
}
|
||||
|
||||
if (multiLineBuffer == null)
|
||||
entryStart = reader.LastLineStart;
|
||||
|
||||
_linesRead++;
|
||||
|
||||
// A physical line over the cap is never matched: its captures would be
|
||||
// cut and the regexes would run over attacker-sized input. The capped
|
||||
// prefix is enough to tell whether it opened an entry whose remaining
|
||||
// lines must be skipped rather than raw-matched.
|
||||
if (reader.LastLineTruncated)
|
||||
{
|
||||
stats.OversizedLines++;
|
||||
_oversizedLines++;
|
||||
if (multiLineBuffer != null)
|
||||
{
|
||||
multiLineBuffer = null;
|
||||
multiLineCount = 0;
|
||||
skippingDroppedEntry = true;
|
||||
}
|
||||
else if (line.StartsWith("<![LOG[") && !line.Contains("]LOG]!>"))
|
||||
{
|
||||
skippingDroppedEntry = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!reader.LastLineTerminated && multiLineBuffer == null
|
||||
&& HoldBackTail(filePath, reader.LastLineStart, fileInfo.Length, passNowUtc))
|
||||
{
|
||||
// Unterminated single line at EOF: the writer is mid-line.
|
||||
_heldTailCount++;
|
||||
resumePos = reader.LastLineStart;
|
||||
break;
|
||||
}
|
||||
|
||||
if (skippingDroppedEntry)
|
||||
{
|
||||
// A line opening a new entry ends the skip and is processed
|
||||
// itself — checked BEFORE the close-tag test, because a complete
|
||||
// single-line entry contains both and must not be consumed as
|
||||
// the dropped entry's close.
|
||||
if (line.StartsWith("<![LOG["))
|
||||
skippingDroppedEntry = false;
|
||||
else
|
||||
{
|
||||
if (line.Contains("]LOG]!>")) skippingDroppedEntry = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Multiline CMTrace buffering ---
|
||||
// CMTrace entries: <![LOG[message]LOG]!><time=...>
|
||||
// When message contains newlines, the entry spans multiple lines.
|
||||
// We buffer until we find the closing ]LOG]!> tag.
|
||||
if (multiLineBuffer != null)
|
||||
{
|
||||
// Continuing a multiline entry
|
||||
multiLineBuffer.Append('\n').Append(line);
|
||||
multiLineCount++;
|
||||
|
||||
if (line.Contains("]LOG]!>"))
|
||||
{
|
||||
// Entry complete — use the assembled line
|
||||
line = multiLineBuffer.ToString();
|
||||
multiLineBuffer = null;
|
||||
multiLineCount = 0;
|
||||
}
|
||||
else if (multiLineCount >= MaxMultiLineBufferLines || multiLineBuffer.Length >= MaxEntryBytes)
|
||||
{
|
||||
// Safety limit — discard to bound memory and parser work. Warning
|
||||
// (not Debug) so a capped entry is visible in the client log at the
|
||||
// default level: a real IME entry this large would be news.
|
||||
_logger.Warning($"ImeLogTracker: discarding multiline CMTrace buffer in {Path.GetFileName(filePath)} after {multiLineCount} lines / {multiLineBuffer.Length} chars (cap {MaxMultiLineBufferLines} lines / {MaxEntryBytes} chars) — entry dropped");
|
||||
multiLineBuffer = null;
|
||||
multiLineCount = 0;
|
||||
skippingDroppedEntry = true;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Still accumulating — read next line
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (line.StartsWith("<![LOG[") && !line.Contains("]LOG]!>"))
|
||||
{
|
||||
// Start of a multiline CMTrace entry
|
||||
multiLineBuffer = new StringBuilder(line);
|
||||
multiLineCount = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Normal processing (single-line or completed multiline) ---
|
||||
CmTraceLogEntry entry;
|
||||
string messageToMatch;
|
||||
_currentEntryOffset = entryStart;
|
||||
if (CmTraceLogParser.TryParseLine(line, out entry))
|
||||
{
|
||||
messageToMatch = entry.Message;
|
||||
if (entry.HasTimestamp && !entry.BiasMinutes.HasValue)
|
||||
calibrationAnchor = entry;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Non-CMTrace line - match raw
|
||||
messageToMatch = line;
|
||||
entry = null;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(messageToMatch)) continue;
|
||||
|
||||
// Simulation mode delay
|
||||
if (SimulationMode && entry != null)
|
||||
{
|
||||
await ApplySimulationDelay(ResolveEntryUtc(entry), token);
|
||||
}
|
||||
|
||||
MatchLine(filePath, line, messageToMatch, entry, stats);
|
||||
}
|
||||
|
||||
ClearHeldTailIfConsumed(filePath, resumePos);
|
||||
_positionTracker.SetPosition(filePath, resumePos);
|
||||
_stateDirty = true;
|
||||
backlogBytes += Math.Max(0, fileInfo.Length - resumePos);
|
||||
|
||||
// One line per pass and file, never per hostile line: the counters are
|
||||
// the operator's only trace that matching was skipped or cut short.
|
||||
if (stats.RegexTimeouts > 0 || stats.BudgetBreaks > 0 || stats.OversizedLines > 0)
|
||||
{
|
||||
_logger.Warning(
|
||||
$"ImeLogTracker: {Path.GetFileName(filePath)} pass skipped work — " +
|
||||
$"oversizedLines={stats.OversizedLines} (cap {MaxEntryBytes} bytes), " +
|
||||
$"regexTimeouts={stats.RegexTimeouts}, lineBudgetBreaks={stats.BudgetBreaks}" +
|
||||
(stats.FirstSkippedPatternId != null ? $", firstSkippedPattern={stats.FirstSkippedPatternId}" : string.Empty));
|
||||
RaiseTrackerDegradedOnce(Path.GetFileName(filePath), stats.FirstSkippedPatternId);
|
||||
}
|
||||
|
||||
// Calibrate AFTER the pass: this pass's lines were resolved with the
|
||||
// offset established previously, at most one poll (100 ms) old. Buffering
|
||||
// the pass to calibrate first is not an option — the first pass of
|
||||
// AppWorkload.log can be hundreds of MB. The cost is a warm-up of one
|
||||
// growing pass, during which lines fall back to the reader zone and are
|
||||
// flagged as such.
|
||||
if (hadPreviousObservation && calibrationAnchor != null)
|
||||
CalibrateFrom(_currentSourceFileName, Path.GetFileName(filePath), calibrationAnchor);
|
||||
_currentEntryOffset = -1;
|
||||
}
|
||||
backlogBytes += await ProcessFileAsync(filePath, token);
|
||||
}
|
||||
catch (FileNotFoundException) { }
|
||||
catch (IOException ex)
|
||||
@@ -330,6 +94,246 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One pass over one file: the bytes behind the bookmark, on a multi-writer file preceded
|
||||
/// by the overwrite check that may move the bookmark back first (see
|
||||
/// <c>ImeLogTracker.Overwrite.cs</c>). Returns the file's remaining backlog in bytes.
|
||||
/// </summary>
|
||||
private async Task<long> ProcessFileAsync(string filePath, CancellationToken token)
|
||||
{
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
if (!fileInfo.Exists) return 0;
|
||||
|
||||
// Captured BEFORE MarkChecked below stamps this pass. Growth measured
|
||||
// against a previously observed state is what makes this pass's lines valid
|
||||
// "written now" anchors — for the per-file measurement AND per-line anchoring.
|
||||
var hadPreviousObservation = _positionTracker.HasSeen(filePath);
|
||||
var lastCheckedUtc = _positionTracker.GetLastCheckedUtc(filePath);
|
||||
var passNowUtc = UtcNowProvider();
|
||||
|
||||
var startPos = _positionTracker.GetSafePosition(filePath, fileInfo.Length);
|
||||
|
||||
// Every look counts, including "no new data" and an empty first sight: the
|
||||
// NEXT pass's freshness window is measured from here. Restored bookmarks
|
||||
// deliberately carry no LastCheckedUtc — the first pass after a restart reads
|
||||
// downtime backlog and must never count as fresh.
|
||||
_positionTracker.MarkChecked(filePath, passNowUtc);
|
||||
|
||||
var fileName = Path.GetFileName(filePath);
|
||||
var ledger = IsMultiWriterLogFile(fileName) ? GetLedger(fileName) : null;
|
||||
|
||||
// Set by a rewind: entries that still read unchanged behind the old bookmark are
|
||||
// skipped, and nothing below the old bookmark is fresh or a calibration anchor.
|
||||
HashSet<LedgerEntry> rewoundEntries = null;
|
||||
long oldBookmark = -1;
|
||||
|
||||
if (ledger != null)
|
||||
{
|
||||
var rewindTo = await PrepareLedgerAsync(filePath, fileName, ledger, startPos, passNowUtc, token);
|
||||
if (rewindTo >= 0)
|
||||
{
|
||||
rewoundEntries = ApplyRewind(filePath, fileName, ledger, rewindTo, startPos);
|
||||
oldBookmark = startPos;
|
||||
startPos = rewindTo;
|
||||
}
|
||||
}
|
||||
|
||||
if (startPos >= fileInfo.Length)
|
||||
{
|
||||
// M2: guard the interpolated string so it isn't built every 100 ms tick
|
||||
// (per file) when Trace is off — which is the production default (Info).
|
||||
if (_logger.LogLevel >= AgentLogLevel.Trace)
|
||||
_logger.Trace($"ImeLogTracker: {fileName} — no new data (pos={startPos}, size={fileInfo.Length})");
|
||||
return 0;
|
||||
}
|
||||
if (_logger.LogLevel >= AgentLogLevel.Trace)
|
||||
_logger.Trace($"ImeLogTracker: reading {fileName} from pos {startPos} (size={fileInfo.Length}, delta={fileInfo.Length - startPos})");
|
||||
|
||||
_currentSourceFileName = fileName;
|
||||
var passLinesAreFresh = hadPreviousObservation
|
||||
&& lastCheckedUtc > DateTime.MinValue
|
||||
&& (passNowUtc - lastCheckedUtc) <= FreshLineMaxAge;
|
||||
_currentPassLinesAreFresh = passLinesAreFresh;
|
||||
|
||||
// Newest bias-less line of this pass — the calibration anchor. Bias-carrying
|
||||
// lines are skipped: they already state the writer's offset, so they need no
|
||||
// measurement and must not overwrite one.
|
||||
CmTraceLogEntry calibrationAnchor = null;
|
||||
|
||||
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
||||
{
|
||||
// Guard zone: the file grew and is open anyway — re-check the entries just behind
|
||||
// the bookmark, where a stale writer's block lands when it is short.
|
||||
if (ledger != null && rewoundEntries == null && ledger.Entries.Count > 0)
|
||||
{
|
||||
var guardFrom = ledger.OffsetOfEntryAtOrAfter(startPos - OverwriteGuardBytes);
|
||||
if (guardFrom >= 0 && guardFrom < startPos)
|
||||
{
|
||||
var divergence = await FindOverwriteAsync(stream, ledger, guardFrom, startPos, token);
|
||||
_verifiedBytes += startPos - guardFrom;
|
||||
if (divergence >= 0)
|
||||
{
|
||||
// The guard saw only the tail of the ledger; the block may start earlier.
|
||||
var earliest = await FindOverwriteAsync(stream, ledger, ledger.StartOffset, startPos, token);
|
||||
_verifiedBytes += startPos - ledger.StartOffset;
|
||||
_verifyPasses++;
|
||||
if (earliest >= 0) divergence = earliest;
|
||||
rewoundEntries = ApplyRewind(filePath, fileName, ledger, divergence, startPos);
|
||||
oldBookmark = startPos;
|
||||
startPos = divergence;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stream.Seek(startPos, SeekOrigin.Begin);
|
||||
|
||||
var reader = new BoundedLineReader(stream, MaxEntryBytes, hashLines: ledger != null);
|
||||
var assembler = new CmTraceEntryAssembler(MaxMultiLineBufferLines, MaxEntryBytes);
|
||||
var stats = new PassStats();
|
||||
var firstEntry = true;
|
||||
// Where the next pass resumes. EOF unless an entry is left unconsumed.
|
||||
long resumePos;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// CancellationToken.None: cancellation is honoured by the explicit
|
||||
// check below (exact bookmark) rather than by an exception out of
|
||||
// the read, which the poll loop would log as an error.
|
||||
var line = await reader.ReadLineAsync(CancellationToken.None);
|
||||
if (line == null)
|
||||
{
|
||||
// EOF. An in-flight multiline entry here is an entry the writer
|
||||
// has not finished — hold it back (see HoldBackTail) instead of
|
||||
// dropping it and raw-matching its remaining lines next pass.
|
||||
if (assembler.HasOpenEntry && HoldBackTail(filePath, assembler.OpenEntryStart, fileInfo.Length, passNowUtc))
|
||||
{
|
||||
_heldTailCount++;
|
||||
resumePos = assembler.OpenEntryStart;
|
||||
break;
|
||||
}
|
||||
resumePos = reader.Position;
|
||||
break;
|
||||
}
|
||||
|
||||
if (token.IsCancellationRequested)
|
||||
{
|
||||
// Exact bookmark: nothing of this line (or of the multiline
|
||||
// entry it belongs to) has been handled.
|
||||
resumePos = assembler.HasOpenEntry ? assembler.OpenEntryStart : reader.LastLineStart;
|
||||
break;
|
||||
}
|
||||
|
||||
_linesRead++;
|
||||
|
||||
if (!reader.LastLineTruncated && !reader.LastLineTerminated && !assembler.HasOpenEntry
|
||||
&& HoldBackTail(filePath, reader.LastLineStart, fileInfo.Length, passNowUtc))
|
||||
{
|
||||
// Unterminated single line at EOF: the writer is mid-line.
|
||||
_heldTailCount++;
|
||||
resumePos = reader.LastLineStart;
|
||||
break;
|
||||
}
|
||||
|
||||
AssembledEntry entry;
|
||||
var outcome = assembler.Feed(line, reader.LastLineStart, reader.Position, reader.LastLineTruncated, reader.LastLineHash, out entry);
|
||||
if (outcome == CmTraceEntryAssembler.Outcome.OversizedDropped)
|
||||
{
|
||||
// A physical line over the cap is never matched: its captures would be
|
||||
// cut and the regexes would run over attacker-sized input.
|
||||
stats.OversizedLines++;
|
||||
_oversizedLines++;
|
||||
continue;
|
||||
}
|
||||
if (outcome == CmTraceEntryAssembler.Outcome.CapDropped)
|
||||
{
|
||||
// Warning (not Debug) so a capped entry is visible in the client log at the
|
||||
// default level: a real IME entry this large would be news.
|
||||
_logger.Warning($"ImeLogTracker: discarding multiline CMTrace buffer in {fileName} after {assembler.DroppedLines} lines / {assembler.DroppedChars} chars (cap {MaxMultiLineBufferLines} lines / {MaxEntryBytes} chars) — entry dropped");
|
||||
continue;
|
||||
}
|
||||
if (outcome != CmTraceEntryAssembler.Outcome.Entry) continue;
|
||||
|
||||
if (firstEntry)
|
||||
{
|
||||
firstEntry = false;
|
||||
// A fragment right at the bookmark of a multi-writer file (the previous
|
||||
// entry ended exactly here, this one does not open an entry) is the tail of
|
||||
// a block a stale writer laid over already-read bytes: verify before reading on.
|
||||
if (ledger != null && rewoundEntries == null && entry.Offset == ledger.EndOffset
|
||||
&& ledger.LastFragmentOffset != entry.Offset && !entry.Text.StartsWith("<![LOG["))
|
||||
{
|
||||
ledger.VerifyRequested = true;
|
||||
ledger.LastFragmentOffset = entry.Offset;
|
||||
resumePos = entry.Offset;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ledger != null) ledger.Record(entry);
|
||||
if (rewoundEntries != null && rewoundEntries.Contains(new LedgerEntry { Offset = entry.Offset, Hash = entry.Hash }))
|
||||
continue; // unchanged behind the old bookmark — processed before the rewind
|
||||
|
||||
_currentPassLinesAreFresh = passLinesAreFresh && entry.Offset >= oldBookmark;
|
||||
|
||||
// --- Normal processing (single-line or completed multiline) ---
|
||||
CmTraceLogEntry parsed;
|
||||
string messageToMatch;
|
||||
_currentEntryOffset = entry.Offset;
|
||||
if (CmTraceLogParser.TryParseLine(entry.Text, out parsed))
|
||||
{
|
||||
messageToMatch = parsed.Message;
|
||||
if (parsed.HasTimestamp && !parsed.BiasMinutes.HasValue && entry.Offset >= oldBookmark)
|
||||
calibrationAnchor = parsed;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Non-CMTrace line - match raw
|
||||
messageToMatch = entry.Text;
|
||||
parsed = null;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(messageToMatch)) continue;
|
||||
|
||||
// Simulation mode delay
|
||||
if (SimulationMode && parsed != null)
|
||||
{
|
||||
await ApplySimulationDelay(ResolveEntryUtc(parsed), token);
|
||||
}
|
||||
|
||||
MatchLine(filePath, entry.Text, messageToMatch, parsed, stats);
|
||||
}
|
||||
|
||||
ClearHeldTailIfConsumed(filePath, resumePos);
|
||||
_positionTracker.SetPosition(filePath, resumePos);
|
||||
_stateDirty = true;
|
||||
if (ledger != null) ledger.TrimBelow(RetentionFloor(fileName, resumePos));
|
||||
|
||||
// One line per pass and file, never per hostile line: the counters are
|
||||
// the operator's only trace that matching was skipped or cut short.
|
||||
if (stats.RegexTimeouts > 0 || stats.BudgetBreaks > 0 || stats.OversizedLines > 0)
|
||||
{
|
||||
_logger.Warning(
|
||||
$"ImeLogTracker: {fileName} pass skipped work — " +
|
||||
$"oversizedLines={stats.OversizedLines} (cap {MaxEntryBytes} bytes), " +
|
||||
$"regexTimeouts={stats.RegexTimeouts}, lineBudgetBreaks={stats.BudgetBreaks}" +
|
||||
(stats.FirstSkippedPatternId != null ? $", firstSkippedPattern={stats.FirstSkippedPatternId}" : string.Empty));
|
||||
RaiseTrackerDegradedOnce(fileName, stats.FirstSkippedPatternId);
|
||||
}
|
||||
|
||||
// Calibrate AFTER the pass: this pass's lines were resolved with the
|
||||
// offset established previously, at most one poll (100 ms) old. Buffering
|
||||
// the pass to calibrate first is not an option — the first pass of
|
||||
// AppWorkload.log can be hundreds of MB. The cost is a warm-up of one
|
||||
// growing pass, during which lines fall back to the reader zone and are
|
||||
// flagged as such.
|
||||
if (hadPreviousObservation && calibrationAnchor != null)
|
||||
CalibrateFrom(_currentSourceFileName, fileName, calibrationAnchor);
|
||||
_currentEntryOffset = -1;
|
||||
|
||||
return Math.Max(0, fileInfo.Length - resumePos);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class PassStats
|
||||
{
|
||||
public int RegexTimeouts;
|
||||
@@ -828,10 +832,18 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
break;
|
||||
|
||||
case "resetplatformscriptcontext":
|
||||
// Session 6b4993e5 fix: a fresh AgentExecutor invocation banner
|
||||
// ("ExecutorLog AgentExecutor gets invoked") ends the previous
|
||||
// invocation's line-capture context. See HandleAgentExecutorInvocationBoundary.
|
||||
HandleAgentExecutorInvocationBoundary();
|
||||
// "ExecutorLog AgentExecutor gets invoked": a new process — the executor
|
||||
// lines that follow belong to its own argument line, never to the
|
||||
// previous invocation (session 6b4993e5).
|
||||
HandleAgentExecutorInvocation();
|
||||
break;
|
||||
|
||||
case "agentinvocationargument":
|
||||
HandleAgentInvocationArgument(match.Groups["kind"]?.Value);
|
||||
break;
|
||||
|
||||
case "agentexecutorcompleted":
|
||||
HandleAgentExecutorCompleted();
|
||||
break;
|
||||
|
||||
case "healthscriptresult":
|
||||
@@ -859,11 +871,15 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
/// <c>_activePatterns</c> → <see cref="HandlePatternMatch"/>). Lets unit tests drive the
|
||||
/// script / app handlers deterministically without writing CMTrace files or spinning the
|
||||
/// poller. <paramref name="sourceTimestampUtc"/> populates the entry timestamp some
|
||||
/// handlers read via <see cref="LastMatchedLogTimestamp"/>.
|
||||
/// handlers read via <see cref="LastMatchedLogTimestamp"/>. <paramref name="sourceFileName"/>
|
||||
/// names the log the line comes from — positional ownership is per file — and stays in
|
||||
/// effect for later calls; omitted, the line lands in the test file.
|
||||
/// </summary>
|
||||
internal void ProcessLogMessageForTest(string message, DateTime? sourceTimestampUtc = null)
|
||||
internal void ProcessLogMessageForTest(string message, DateTime? sourceTimestampUtc = null, string sourceFileName = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(message)) return;
|
||||
if (sourceFileName != null) _currentSourceFileName = sourceFileName;
|
||||
NextTestEntry();
|
||||
// The test seam hands in an already-resolved UTC instant, so it takes the same route a
|
||||
// writer-declared bias does: TimestampUtc set, no zone left to guess.
|
||||
var entry = sourceTimestampUtc.HasValue
|
||||
|
||||
+490
@@ -0,0 +1,490 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
{
|
||||
/// <summary>
|
||||
/// Partial: overwrite-aware tailing of the two IME logs that several processes append to.
|
||||
/// <para>
|
||||
/// IME's trace listener opens each log once per process with FileMode.Append and
|
||||
/// FileShare.ReadWrite and writes at its own stream position afterwards. AgentExecutor.exe
|
||||
/// (two processes per user-context script, one per detection or remediation script) has such
|
||||
/// a listener on AgentExecutor.log AND on IntuneManagementExtension.log. Whenever one process
|
||||
/// resumes writing after another one appended, it overwrites those bytes: the final file
|
||||
/// holds the last writer, while a tailer that trusts its bookmark read the first version and
|
||||
/// never sees the second (session 46749560: 6 of 16 script results, 4 of 16 exit blocks).
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// So the bytes behind the bookmark are not immutable. The tracker keeps a fingerprint ledger
|
||||
/// of every entry it read from these files, re-verifies it while script invocations are in
|
||||
/// flight or right after a signal that a stale writer just wrote, and on the first changed
|
||||
/// entry rewinds the bookmark — processing only entries it has not processed before.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public partial class ImeLogTracker
|
||||
{
|
||||
/// <summary>Live logs with more than one writing process; archived rotations are closed files.</summary>
|
||||
internal static readonly string[] MultiWriterLogFiles = { "AgentExecutor.log", "IntuneManagementExtension.log" };
|
||||
|
||||
/// <summary>Ledger depth behind the bookmark while no script is in flight.</summary>
|
||||
internal const int LedgerRetentionBytes = 1024 * 1024;
|
||||
|
||||
/// <summary>Hard cap on the ledger depth, whatever is in flight.</summary>
|
||||
internal const int LedgerMaxBytes = 4 * 1024 * 1024;
|
||||
|
||||
/// <summary>Entries this close to the bookmark are re-checked on every pass that found growth (the file is open anyway).</summary>
|
||||
internal const int OverwriteGuardBytes = 4 * 1024;
|
||||
|
||||
/// <summary>After a restart the ledger is seeded from this many bytes before the persisted bookmark.</summary>
|
||||
internal const int RestartSeedBytes = 64 * 1024;
|
||||
|
||||
/// <summary>Verification work per second while in contention — a deep ledger is checked less often, never skipped.</summary>
|
||||
internal const long VerifyBudgetBytesPerSecond = 256 * 1024;
|
||||
|
||||
internal static readonly TimeSpan VerifyMinInterval = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary>How long an observed invocation keeps the files in contention on its own (detection executors have no pending slot).</summary>
|
||||
internal static readonly TimeSpan InvocationContentionWindow = TimeSpan.FromSeconds(120);
|
||||
|
||||
/// <summary>A pending script older than this no longer holds the files in contention.</summary>
|
||||
internal static readonly TimeSpan MaxPendingScriptContention = TimeSpan.FromHours(2);
|
||||
|
||||
private const int MaxInvocationMarkersPerFile = 512;
|
||||
private const string TestSourceFileName = "TEST.log";
|
||||
private const string LogOpenTag = "<![LOG[";
|
||||
|
||||
/// <summary>One processed entry: where it starts, how far it reaches, what its bytes hashed to.</summary>
|
||||
internal struct LedgerEntry : IEquatable<LedgerEntry>
|
||||
{
|
||||
public long Offset;
|
||||
public int Length;
|
||||
public ulong Hash;
|
||||
|
||||
// Identity is (offset, hash): a tail first read unterminated and later completed
|
||||
// keeps its hash but grows by its terminator.
|
||||
public bool Equals(LedgerEntry other) => Offset == other.Offset && Hash == other.Hash;
|
||||
public override bool Equals(object obj) => obj is LedgerEntry other && Equals(other);
|
||||
public override int GetHashCode() => unchecked(((int)Offset * 397) ^ (int)Hash ^ (int)(Hash >> 32));
|
||||
}
|
||||
|
||||
internal sealed class FileLedger
|
||||
{
|
||||
public readonly List<LedgerEntry> Entries = new List<LedgerEntry>();
|
||||
public bool VerifyRequested;
|
||||
public DateTime NextCadenceVerifyUtc = DateTime.MinValue;
|
||||
public long LastFragmentOffset = -1;
|
||||
public bool Seeded;
|
||||
public bool RewindLogged;
|
||||
|
||||
public long StartOffset => Entries.Count > 0 ? Entries[0].Offset : -1;
|
||||
public long EndOffset => Entries.Count > 0 ? Entries[Entries.Count - 1].Offset + Entries[Entries.Count - 1].Length : -1;
|
||||
|
||||
public int IndexOfFirstAtOrAfter(long offset)
|
||||
{
|
||||
int lo = 0, hi = Entries.Count;
|
||||
while (lo < hi)
|
||||
{
|
||||
var mid = (lo + hi) >> 1;
|
||||
if (Entries[mid].Offset < offset) lo = mid + 1; else hi = mid;
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
|
||||
/// <summary>Entry offset at or after <paramref name="offset"/>, or -1.</summary>
|
||||
public long OffsetOfEntryAtOrAfter(long offset)
|
||||
{
|
||||
var idx = IndexOfFirstAtOrAfter(offset);
|
||||
return idx < Entries.Count ? Entries[idx].Offset : -1;
|
||||
}
|
||||
|
||||
public void Record(AssembledEntry e)
|
||||
{
|
||||
var idx = IndexOfFirstAtOrAfter(e.Offset);
|
||||
if (idx < Entries.Count) Entries.RemoveRange(idx, Entries.Count - idx);
|
||||
Entries.Add(new LedgerEntry { Offset = e.Offset, Length = e.Length, Hash = e.Hash });
|
||||
}
|
||||
|
||||
/// <summary>Drops everything from <paramref name="offset"/> on and returns it — the entries a rewind must not process twice.</summary>
|
||||
public HashSet<LedgerEntry> TruncateFrom(long offset)
|
||||
{
|
||||
var idx = IndexOfFirstAtOrAfter(offset);
|
||||
var removed = new HashSet<LedgerEntry>();
|
||||
for (var i = idx; i < Entries.Count; i++) removed.Add(Entries[i]);
|
||||
if (idx < Entries.Count) Entries.RemoveRange(idx, Entries.Count - idx);
|
||||
return removed;
|
||||
}
|
||||
|
||||
public void TrimBelow(long floor)
|
||||
{
|
||||
var idx = IndexOfFirstAtOrAfter(floor);
|
||||
if (idx > 0) Entries.RemoveRange(0, idx);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Entries.Clear();
|
||||
LastFragmentOffset = -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Keyed by file name (unique within the log folder). Poll thread only.
|
||||
private readonly Dictionary<string, FileLedger> _ledgers = new Dictionary<string, FileLedger>(StringComparer.OrdinalIgnoreCase);
|
||||
private DateTime _contentionUntilUtc = DateTime.MinValue;
|
||||
|
||||
// Cumulative, restart-safe like the other health counters.
|
||||
private long _overwriteRewinds;
|
||||
private long _overwriteBytesReprocessed;
|
||||
private long _verifyPasses;
|
||||
private long _verifiedBytes;
|
||||
|
||||
internal static bool IsMultiWriterLogFile(string fileName)
|
||||
{
|
||||
foreach (var f in MultiWriterLogFiles)
|
||||
if (string.Equals(f, fileName, StringComparison.OrdinalIgnoreCase)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private FileLedger GetLedger(string fileName)
|
||||
{
|
||||
FileLedger ledger;
|
||||
if (!_ledgers.TryGetValue(fileName, out ledger))
|
||||
{
|
||||
ledger = new FileLedger();
|
||||
_ledgers[fileName] = ledger;
|
||||
}
|
||||
return ledger;
|
||||
}
|
||||
|
||||
/// <summary>A stale writer writes exactly when its script ends: every end signal asks both files for a check on the next pass.</summary>
|
||||
private void RequestOverwriteCheck()
|
||||
{
|
||||
foreach (var ledger in _ledgers.Values) ledger.VerifyRequested = true;
|
||||
}
|
||||
|
||||
private void NoteInvocationActivity()
|
||||
{
|
||||
_contentionUntilUtc = UtcNowProvider() + InvocationContentionWindow;
|
||||
}
|
||||
|
||||
private bool InContention(DateTime nowUtc)
|
||||
{
|
||||
if (nowUtc < _contentionUntilUtc || _pendingHealthScript != null) return true;
|
||||
foreach (var script in _pendingPlatformScripts.Values)
|
||||
{
|
||||
if (script.Result != null) continue;
|
||||
if (!script.StartedAtUtc.HasValue || nowUtc - script.StartedAtUtc.Value < MaxPendingScriptContention) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs before a multi-writer file is read: rollover reset, restart seeding, then the
|
||||
/// overwrite check when one is due. Returns the offset to rewind to, or -1.
|
||||
/// </summary>
|
||||
private async Task<long> PrepareLedgerAsync(string filePath, string fileName, FileLedger ledger, long bookmark, DateTime nowUtc, CancellationToken token)
|
||||
{
|
||||
if (ledger.Entries.Count > 0 && bookmark < ledger.EndOffset)
|
||||
{
|
||||
// The bookmark moved below what was read: the file was replaced (rollover).
|
||||
ledger.Clear();
|
||||
InvalidateInvocationMarkersFrom(fileName, 0);
|
||||
}
|
||||
|
||||
if (!ledger.Seeded)
|
||||
{
|
||||
ledger.Seeded = true;
|
||||
if (ledger.Entries.Count == 0 && bookmark > 0)
|
||||
await SeedLedgerAsync(filePath, ledger, bookmark, token).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var due = ledger.VerifyRequested || (InContention(nowUtc) && nowUtc >= ledger.NextCadenceVerifyUtc);
|
||||
if (!due || ledger.Entries.Count == 0) return -1;
|
||||
|
||||
var from = ledger.StartOffset;
|
||||
var bytes = bookmark - from;
|
||||
if (bytes <= 0)
|
||||
{
|
||||
ledger.VerifyRequested = false;
|
||||
return -1;
|
||||
}
|
||||
|
||||
long divergence;
|
||||
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
||||
divergence = await FindOverwriteAsync(stream, ledger, from, bookmark, token).ConfigureAwait(false);
|
||||
if (token.IsCancellationRequested) return -1;
|
||||
|
||||
ledger.VerifyRequested = false;
|
||||
var paceSeconds = Math.Max(VerifyMinInterval.TotalSeconds, (double)bytes / VerifyBudgetBytesPerSecond);
|
||||
ledger.NextCadenceVerifyUtc = nowUtc.AddSeconds(paceSeconds);
|
||||
_verifyPasses++;
|
||||
_verifiedBytes += bytes;
|
||||
return divergence;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Re-reads [fromOffset, toOffset) with the read loop's assembler and compares against the
|
||||
/// ledger. Returns the offset of the first entry whose bytes changed — a ledger entry not
|
||||
/// reproduced, or an entry the ledger never had — or -1 when everything reads as before.
|
||||
/// </summary>
|
||||
private async Task<long> FindOverwriteAsync(FileStream stream, FileLedger ledger, long fromOffset, long toOffset, CancellationToken token)
|
||||
{
|
||||
var entries = ledger.Entries;
|
||||
var idx = ledger.IndexOfFirstAtOrAfter(fromOffset);
|
||||
if (idx >= entries.Count) return -1;
|
||||
|
||||
stream.Seek(entries[idx].Offset, SeekOrigin.Begin);
|
||||
var reader = new BoundedLineReader(stream, MaxEntryBytes, hashLines: true);
|
||||
var assembler = new CmTraceEntryAssembler(MaxMultiLineBufferLines, MaxEntryBytes);
|
||||
|
||||
while (reader.Position < toOffset)
|
||||
{
|
||||
var line = await reader.ReadLineAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
if (line == null) break;
|
||||
if (token.IsCancellationRequested) return -1;
|
||||
|
||||
AssembledEntry e;
|
||||
if (assembler.Feed(line, reader.LastLineStart, reader.Position, reader.LastLineTruncated, reader.LastLineHash, out e)
|
||||
!= CmTraceEntryAssembler.Outcome.Entry)
|
||||
continue;
|
||||
if (e.Offset >= toOffset) break;
|
||||
|
||||
if (idx < entries.Count && entries[idx].Offset < e.Offset) return entries[idx].Offset;
|
||||
if (idx < entries.Count && entries[idx].Offset == e.Offset)
|
||||
{
|
||||
if (entries[idx].Hash != e.Hash) return e.Offset;
|
||||
idx++;
|
||||
continue;
|
||||
}
|
||||
return e.Offset; // an entry the ledger never had: the bytes changed here
|
||||
}
|
||||
|
||||
return idx < entries.Count && entries[idx].Offset < toOffset ? entries[idx].Offset : -1;
|
||||
}
|
||||
|
||||
/// <summary>After a restart the ledger is empty: fingerprint the entries just behind the persisted bookmark without processing them.</summary>
|
||||
private async Task SeedLedgerAsync(string filePath, FileLedger ledger, long bookmark, CancellationToken token)
|
||||
{
|
||||
var from = Math.Max(0, bookmark - RestartSeedBytes);
|
||||
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
||||
{
|
||||
stream.Seek(from, SeekOrigin.Begin);
|
||||
var reader = new BoundedLineReader(stream, MaxEntryBytes, hashLines: true);
|
||||
var assembler = new CmTraceEntryAssembler(MaxMultiLineBufferLines, MaxEntryBytes);
|
||||
var aligned = from == 0;
|
||||
while (reader.Position < bookmark)
|
||||
{
|
||||
var line = await reader.ReadLineAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
if (line == null || token.IsCancellationRequested) break;
|
||||
if (!aligned)
|
||||
{
|
||||
// The seek may have landed mid-entry: wait for an entry boundary.
|
||||
if (!line.StartsWith(LogOpenTag)) continue;
|
||||
aligned = true;
|
||||
}
|
||||
AssembledEntry e;
|
||||
if (assembler.Feed(line, reader.LastLineStart, reader.Position, reader.LastLineTruncated, reader.LastLineHash, out e)
|
||||
== CmTraceEntryAssembler.Outcome.Entry
|
||||
&& e.Offset + e.Length <= bookmark)
|
||||
ledger.Record(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Moves the bookmark back to the first changed entry; returns the entries to skip if they still read unchanged behind it.</summary>
|
||||
private HashSet<LedgerEntry> ApplyRewind(string filePath, string fileName, FileLedger ledger, long rewindTo, long bookmark)
|
||||
{
|
||||
var removed = ledger.TruncateFrom(rewindTo);
|
||||
_positionTracker.SetPosition(filePath, rewindTo);
|
||||
_heldTails.Remove(filePath);
|
||||
InvalidateInvocationMarkersFrom(fileName, rewindTo);
|
||||
_overwriteRewinds++;
|
||||
_overwriteBytesReprocessed += bookmark - rewindTo;
|
||||
|
||||
var message = $"ImeLogTracker: {fileName} changed behind the bookmark at offset {rewindTo} " +
|
||||
$"(a concurrent writer overwrote already-read bytes) — rewinding {bookmark - rewindTo} bytes";
|
||||
if (!ledger.RewindLogged)
|
||||
{
|
||||
ledger.RewindLogged = true;
|
||||
_logger.Info(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Debug(message);
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
private long RetentionFloor(string fileName, long bookmark)
|
||||
{
|
||||
var floor = bookmark - LedgerRetentionBytes;
|
||||
var oldest = OldestPendingScriptMarkerOffset(fileName);
|
||||
if (oldest >= 0 && oldest < floor) floor = oldest;
|
||||
var hardFloor = bookmark - LedgerMaxBytes;
|
||||
return floor < hardFloor ? hardFloor : floor;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Invocation markers: who owns the policyId-less executor lines at a file position
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// AgentExecutor.log carries "Powershell exit code is N" / "write output done" without a
|
||||
// policy id. Their owner is decided by FILE POSITION, not by the order lines were read:
|
||||
// an executor writes its end block at its own stream position, i.e. directly after its
|
||||
// own start block in the final bytes, so the nearest preceding invocation marker names
|
||||
// it — and stays right after a rewind re-reads that region. A marker is a platform start
|
||||
// (policy id), some other invocation (detection, remediation, requirement: null) or a
|
||||
// close ("gets invoked" banner, "Agent executor completed").
|
||||
|
||||
private sealed class InvocationMarker
|
||||
{
|
||||
public long Offset;
|
||||
public string PolicyId;
|
||||
public bool IsClose;
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, List<InvocationMarker>> _invocationMarkers =
|
||||
new Dictionary<string, List<InvocationMarker>>(StringComparer.OrdinalIgnoreCase);
|
||||
private InvocationMarker _lastPlatformMarker;
|
||||
// Files a platform run has opened in (PS-AGENT-SCRIPT-START in AgentExecutor.log,
|
||||
// PS-SCRIPT-GENERATED in IntuneManagementExtension.log). Survives a rollover of the
|
||||
// file's markers: the fallback below is meant for exactly that file.
|
||||
private readonly HashSet<string> _platformMarkerFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
private long _testEntryOrdinal;
|
||||
|
||||
private void RecordInvocationMarker(string policyId, bool isClose)
|
||||
{
|
||||
if (_currentEntryOffset < 0) return;
|
||||
var file = _currentSourceFileName ?? TestSourceFileName;
|
||||
List<InvocationMarker> list;
|
||||
if (!_invocationMarkers.TryGetValue(file, out list))
|
||||
{
|
||||
list = new List<InvocationMarker>();
|
||||
_invocationMarkers[file] = list;
|
||||
}
|
||||
|
||||
if (list.Count > 0 && list[list.Count - 1].Offset == _currentEntryOffset)
|
||||
{
|
||||
// Two patterns on one line: the platform start outranks the generic argument line.
|
||||
var last = list[list.Count - 1];
|
||||
if (!isClose && policyId != null)
|
||||
{
|
||||
last.PolicyId = policyId;
|
||||
last.IsClose = false;
|
||||
_lastPlatformMarker = last;
|
||||
_platformMarkerFiles.Add(file);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (list.Count >= MaxInvocationMarkersPerFile) list.RemoveAt(0);
|
||||
var marker = new InvocationMarker { Offset = _currentEntryOffset, PolicyId = policyId, IsClose = isClose };
|
||||
list.Add(marker);
|
||||
if (!isClose && policyId != null)
|
||||
{
|
||||
_lastPlatformMarker = marker;
|
||||
_platformMarkerFiles.Add(file);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The pending platform script that owns the entry being processed, or null.
|
||||
/// <paramref name="ownerPolicyId"/> names the owning platform script even when its slot is
|
||||
/// gone (completion already emitted); null when no platform invocation owns the entry.
|
||||
/// </summary>
|
||||
private ScriptExecutionState ResolvePlatformScriptForCurrentEntry(out string ownerPolicyId)
|
||||
{
|
||||
ownerPolicyId = null;
|
||||
var file = _currentSourceFileName ?? TestSourceFileName;
|
||||
InvocationMarker marker = null;
|
||||
List<InvocationMarker> list = null;
|
||||
if (_currentEntryOffset >= 0 && _invocationMarkers.TryGetValue(file, out list))
|
||||
{
|
||||
for (var i = list.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (list[i].Offset <= _currentEntryOffset)
|
||||
{
|
||||
marker = list[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A file without any marker resolves to the platform run in flight only when platform
|
||||
// runs open in that file at all — IntuneManagementExtension.log right after a rollover
|
||||
// cleared its markers, AgentExecutor.log likewise. HealthScripts.log and the app logs
|
||||
// never carry a platform marker: their launch and exit lines belong to health scripts
|
||||
// and detection scripts, never to the platform script that happens to be pending
|
||||
// (session 24dc69d1: the health-script worker's exit line pre-filled a platform slot's
|
||||
// exit code, its launch line the slot's context).
|
||||
if (marker == null && (list == null || list.Count == 0) && _platformMarkerFiles.Contains(file)) marker = _lastPlatformMarker;
|
||||
if (marker == null || marker.IsClose || marker.PolicyId == null) return null;
|
||||
|
||||
ownerPolicyId = marker.PolicyId;
|
||||
ScriptExecutionState state;
|
||||
return _pendingPlatformScripts.TryGetValue(marker.PolicyId, out state) ? state : null;
|
||||
}
|
||||
|
||||
private void InvalidateInvocationMarkersFrom(string fileName, long offset)
|
||||
{
|
||||
List<InvocationMarker> list;
|
||||
if (!_invocationMarkers.TryGetValue(fileName, out list) || list.Count == 0) return;
|
||||
var removedLast = false;
|
||||
for (var i = list.Count - 1; i >= 0 && list[i].Offset >= offset; i--)
|
||||
{
|
||||
if (ReferenceEquals(list[i], _lastPlatformMarker)) removedLast = true;
|
||||
list.RemoveAt(i);
|
||||
}
|
||||
if (removedLast)
|
||||
{
|
||||
_lastPlatformMarker = null;
|
||||
for (var i = list.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (!list[i].IsClose && list[i].PolicyId != null)
|
||||
{
|
||||
_lastPlatformMarker = list[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private long OldestPendingScriptMarkerOffset(string fileName)
|
||||
{
|
||||
List<InvocationMarker> list;
|
||||
if (!_invocationMarkers.TryGetValue(fileName, out list)) return -1;
|
||||
long oldest = -1;
|
||||
foreach (var marker in list)
|
||||
{
|
||||
if (marker.IsClose || marker.PolicyId == null || !_pendingPlatformScripts.ContainsKey(marker.PolicyId)) continue;
|
||||
if (oldest < 0 || marker.Offset < oldest) oldest = marker.Offset;
|
||||
}
|
||||
return oldest;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Test seams
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/// <summary>Same effect as an end signal: both multi-writer ledgers are verified on the next pass.</summary>
|
||||
internal void RequestOverwriteCheckForTest() => RequestOverwriteCheck();
|
||||
|
||||
/// <summary>What a rollover of the file does to its invocation markers.</summary>
|
||||
internal void ClearInvocationMarkersForTest(string fileName) => InvalidateInvocationMarkersFrom(fileName, 0);
|
||||
|
||||
internal int LedgerEntryCountForTest(string fileName)
|
||||
{
|
||||
FileLedger ledger;
|
||||
return _ledgers.TryGetValue(fileName, out ledger) ? ledger.Entries.Count : 0;
|
||||
}
|
||||
|
||||
/// <summary>Gives a seam-fed message a file position of its own so positional attribution behaves like in a real pass.</summary>
|
||||
internal void NextTestEntry()
|
||||
{
|
||||
if (_currentSourceFileName == null) _currentSourceFileName = TestSourceFileName;
|
||||
_currentEntryOffset = ++_testEntryOrdinal * 4096;
|
||||
}
|
||||
}
|
||||
}
|
||||
+72
-20
@@ -181,7 +181,6 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
// arrive across multiple log entries and IME may interleave platform scripts).
|
||||
private readonly Dictionary<string, ScriptExecutionState> _pendingPlatformScripts =
|
||||
new Dictionary<string, ScriptExecutionState>(StringComparer.OrdinalIgnoreCase);
|
||||
private string _lastPlatformScriptPolicyId;
|
||||
|
||||
// Health-script (remediation) line-by-line accumulator — single slot because IME
|
||||
// executes health scripts SEQUENTIALLY within a session (verified across multiple
|
||||
@@ -337,7 +336,15 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
private int _unanchoredPatterns;
|
||||
private int _filesTailed;
|
||||
private long _backlogBytes;
|
||||
// Poll-loop timing (restart-safe maxima on a monotonic clock): the longest pass and the
|
||||
// longest pause between passes — see ImeTrackerHealth.PassMaxMs / PassGapMaxMs.
|
||||
private long _passMaxMs;
|
||||
private long _passGapMaxMs;
|
||||
private long _lastPassEndMs = -1;
|
||||
private string _imeAgentVersionSeen;
|
||||
|
||||
/// <summary>Monotonic milliseconds for the poll-loop timing; tests substitute a counter.</summary>
|
||||
internal Func<long> MonotonicMillisProvider { get; set; } = () => Stopwatch.GetTimestamp() * 1000 / Stopwatch.Frequency;
|
||||
private bool _trackerDegradedFired;
|
||||
private readonly Dictionary<string, int> _patternHits = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
@@ -367,6 +374,12 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
FilesTailed = _filesTailed,
|
||||
BacklogBytes = _backlogBytes,
|
||||
ImeAgentVersion = _imeAgentVersionSeen,
|
||||
OverwriteRewinds = _overwriteRewinds,
|
||||
OverwriteBytesReprocessed = _overwriteBytesReprocessed,
|
||||
VerifyPasses = _verifyPasses,
|
||||
VerifiedBytes = _verifiedBytes,
|
||||
PassMaxMs = _passMaxMs,
|
||||
PassGapMaxMs = _passGapMaxMs,
|
||||
PatternHits = new Dictionary<string, int>(_patternHits, StringComparer.OrdinalIgnoreCase),
|
||||
};
|
||||
}
|
||||
@@ -951,18 +964,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
{
|
||||
try
|
||||
{
|
||||
await CheckLogFilesAsync(token);
|
||||
|
||||
// Safety net: emit completions for platform scripts whose AgentExecutor
|
||||
// exit code we already have but whose authoritative IME PS-SCRIPT-RESULT
|
||||
// line never arrived within the grace period. Runs on this same loop
|
||||
// thread (no locking) so it observes the buffer right after parsing.
|
||||
FlushStalePlatformScriptResults(DateTime.UtcNow);
|
||||
|
||||
// Token-failure grace window: fires only when the failure stayed
|
||||
// unresolved (no IME-TOKEN-SUCCESS) for the whole window. Runs after
|
||||
// the drain so a success later in the same batch clears first.
|
||||
CheckPendingTokenFailure(DateTime.UtcNow);
|
||||
await RunPollPassAsync(token);
|
||||
}
|
||||
catch (OperationCanceledException) { break; }
|
||||
catch (Exception ex)
|
||||
@@ -985,10 +987,11 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
try { await Task.Delay(_pollingIntervalMs, token); } catch (OperationCanceledException) { break; }
|
||||
}
|
||||
|
||||
// Final pass: force-flush any platform script that completed within the last grace
|
||||
// window before shutdown (exit code known, IME result still pending). Best-effort —
|
||||
// emits only while callbacks are still wired (i.e. the adapter hasn't been disposed).
|
||||
try { FlushStalePlatformScriptResults(DateTime.UtcNow, force: true); }
|
||||
// Final pass: force-flush any platform script still waiting for the other file
|
||||
// (a held IME result, or an exit code whose IME result is still pending).
|
||||
// Best-effort — emits only while callbacks are still wired (i.e. the adapter
|
||||
// hasn't been disposed).
|
||||
try { FlushPendingPlatformScriptResults(UtcNowProvider(), force: true); }
|
||||
catch (Exception ex) { _logger.Warning($"ImeLogTracker: shutdown script flush failed: {ex.Message}"); }
|
||||
|
||||
// Final state save on shutdown
|
||||
@@ -1002,6 +1005,36 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
}, token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One poll pass, timed: the file reads, then the platform-script completions that wait
|
||||
/// for the other file (an IME result held for its executor end block, the exit-code
|
||||
/// fallback for a result IME never logged), then the token-failure grace window — all on
|
||||
/// this one loop thread, no locking, so the flushes observe the buffers right after
|
||||
/// parsing. The longest pass and the longest pause since the previous pass ended are the
|
||||
/// session's <see cref="ImeTrackerHealth.PassMaxMs"/> / <see cref="ImeTrackerHealth.PassGapMaxMs"/>.
|
||||
/// </summary>
|
||||
internal async Task RunPollPassAsync(CancellationToken token)
|
||||
{
|
||||
var startMs = MonotonicMillisProvider();
|
||||
if (_lastPassEndMs >= 0 && startMs - _lastPassEndMs > _passGapMaxMs)
|
||||
{
|
||||
_passGapMaxMs = startMs - _lastPassEndMs;
|
||||
_stateDirty = true;
|
||||
}
|
||||
|
||||
await CheckLogFilesAsync(token);
|
||||
FlushPendingPlatformScriptResults(UtcNowProvider());
|
||||
CheckPendingTokenFailure(UtcNowProvider());
|
||||
|
||||
var endMs = MonotonicMillisProvider();
|
||||
if (endMs - startMs > _passMaxMs)
|
||||
{
|
||||
_passMaxMs = endMs - startMs;
|
||||
_stateDirty = true;
|
||||
}
|
||||
_lastPassEndMs = endMs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the background polling task
|
||||
/// </summary>
|
||||
@@ -1100,10 +1133,17 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
// (the in-memory marker set started empty). Old state files have null here —
|
||||
// degrades to the pre-fix behavior, no crash.
|
||||
_platformScriptResultEmitted.Clear();
|
||||
if (state.PlatformScriptResultEmitted != null)
|
||||
if (state.PlatformScriptResultEmittedAt != null)
|
||||
{
|
||||
foreach (var kv in state.PlatformScriptResultEmittedAt)
|
||||
_platformScriptResultEmitted[kv.Key] = kv.Value;
|
||||
}
|
||||
else if (state.PlatformScriptResultEmitted != null)
|
||||
{
|
||||
// State files from before the marker carried the run's timestamp: the dedup
|
||||
// works as before, the late-start-line guard never fires for these runs.
|
||||
foreach (var id in state.PlatformScriptResultEmitted)
|
||||
_platformScriptResultEmitted.Add(id);
|
||||
_platformScriptResultEmitted[id] = DateTime.MinValue;
|
||||
}
|
||||
|
||||
_pendingPlatformScripts.Clear();
|
||||
@@ -1140,6 +1180,12 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
HealthScriptResultParseFailures = state.Health.HealthScriptResultParseFailures;
|
||||
_imeAgentVersionSeen = state.Health.ImeAgentVersion;
|
||||
_trackerDegradedFired = state.Health.TrackerDegradedFired;
|
||||
_overwriteRewinds = state.Health.OverwriteRewinds;
|
||||
_overwriteBytesReprocessed = state.Health.OverwriteBytesReprocessed;
|
||||
_verifyPasses = state.Health.VerifyPasses;
|
||||
_verifiedBytes = state.Health.VerifiedBytes;
|
||||
_passMaxMs = state.Health.PassMaxMs;
|
||||
_passGapMaxMs = state.Health.PassGapMaxMs;
|
||||
}
|
||||
if (state.PatternHits != null)
|
||||
{
|
||||
@@ -1190,7 +1236,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
|
||||
// H1 (delta review 2026-07-02): platform-script dedup markers + pending buffer
|
||||
// must survive restarts — see the LoadState comment for the duplicate scenario.
|
||||
PlatformScriptResultEmitted = _platformScriptResultEmitted.ToList(),
|
||||
PlatformScriptResultEmittedAt = new Dictionary<string, DateTime>(_platformScriptResultEmitted, StringComparer.OrdinalIgnoreCase),
|
||||
PendingPlatformScripts = _pendingPlatformScripts.Values.ToList(),
|
||||
ScriptTimeoutSuspectedPosted = _scriptTimeoutSuspectedPosted.ToList(),
|
||||
};
|
||||
@@ -1209,6 +1255,12 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
HealthScriptResultParseFailures = HealthScriptResultParseFailures,
|
||||
ImeAgentVersion = _imeAgentVersionSeen,
|
||||
TrackerDegradedFired = _trackerDegradedFired,
|
||||
OverwriteRewinds = _overwriteRewinds,
|
||||
OverwriteBytesReprocessed = _overwriteBytesReprocessed,
|
||||
VerifyPasses = _verifyPasses,
|
||||
VerifiedBytes = _verifiedBytes,
|
||||
PassMaxMs = _passMaxMs,
|
||||
PassGapMaxMs = _passGapMaxMs,
|
||||
};
|
||||
state.PatternHits = new Dictionary<string, int>(_patternHits, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
+35
@@ -46,6 +46,41 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
/// <summary>IME version as logged by IME itself ("Agent version is: …"), null until seen.</summary>
|
||||
public string ImeAgentVersion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Times the bookmark of a multi-writer log (AgentExecutor.log, IntuneManagementExtension.log)
|
||||
/// was moved back because bytes behind it had been overwritten by a concurrent IME
|
||||
/// process. Expected nonzero whenever user-context scripts or detection scripts ran —
|
||||
/// recovered data, not skipped work.
|
||||
/// </summary>
|
||||
public long OverwriteRewinds { get; set; }
|
||||
|
||||
/// <summary>Bytes re-read by those rewinds (only entries that changed are processed again).</summary>
|
||||
public long OverwriteBytesReprocessed { get; set; }
|
||||
|
||||
/// <summary>Full ledger verifications run (cadence while scripts are in flight, end signals, fragments).</summary>
|
||||
public long VerifyPasses { get; set; }
|
||||
|
||||
/// <summary>Bytes re-read for verification, guard-zone checks included — the cost of the overwrite detection.</summary>
|
||||
public long VerifiedBytes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Longest single poll pass this session (one <c>CheckLogFilesAsync</c> plus the
|
||||
/// flushes), milliseconds on a monotonic clock. A pass spends its time in the file
|
||||
/// reads, the ledger verifications and the regex matching; the fleet baseline is well
|
||||
/// under 100 ms.
|
||||
/// </summary>
|
||||
public long PassMaxMs { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Longest pause between the end of one poll pass and the start of the next this
|
||||
/// session, milliseconds. The loop sleeps 100 ms between passes and saves its state now
|
||||
/// and then; anything far above that is the loop not being scheduled — session c3ecb568
|
||||
/// lost 26 s this way at 100 % VM CPU while the rest of the agent kept running. Late
|
||||
/// reading, not lost data: every event keeps its log line's own timestamp, and a
|
||||
/// result read before its executor end block waits for it.
|
||||
/// </summary>
|
||||
public long PassGapMaxMs { get; set; }
|
||||
|
||||
/// <summary>Match count per enabled pattern ID — every enabled pattern is present, zeros included.</summary>
|
||||
public IReadOnlyDictionary<string, int> PatternHits { get; set; }
|
||||
|
||||
|
||||
+15
@@ -142,7 +142,14 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
// continues its grace window after the restart instead of being silently dropped.
|
||||
// ScriptExecutionState is a plain DTO, serialized directly (no mapping layer needed).
|
||||
// All three are null on state files from before this fix — LoadState treats null as empty.
|
||||
// Read-only migration input: state files written before the marker carried the run's
|
||||
// source timestamp (agents up to 2.0.1458). Never written any more.
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public List<string> PlatformScriptResultEmitted { get; set; }
|
||||
// Policy id → source timestamp of the emitted run (its result line; the exit line for a
|
||||
// fallback emit): the dedup marker plus the reference that tells a late start line of
|
||||
// that run from the start of the next one.
|
||||
public Dictionary<string, DateTime> PlatformScriptResultEmittedAt { get; set; }
|
||||
public List<ScriptExecutionState> PendingPlatformScripts { get; set; }
|
||||
public List<string> ScriptTimeoutSuspectedPosted { get; set; }
|
||||
|
||||
@@ -165,6 +172,14 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime
|
||||
public int HealthScriptResultParseFailures { get; set; }
|
||||
public string ImeAgentVersion { get; set; }
|
||||
public bool TrackerDegradedFired { get; set; }
|
||||
// Overwrite detection counters (null-defaulting on older state files).
|
||||
public long OverwriteRewinds { get; set; }
|
||||
public long OverwriteBytesReprocessed { get; set; }
|
||||
public long VerifyPasses { get; set; }
|
||||
public long VerifiedBytes { get; set; }
|
||||
// Poll-loop timing maxima (null-defaulting on older state files).
|
||||
public long PassMaxMs { get; set; }
|
||||
public long PassGapMaxMs { get; set; }
|
||||
}
|
||||
|
||||
public class PackageStateData
|
||||
|
||||
+24
-1
@@ -61,11 +61,34 @@ public class ScriptExecutionState
|
||||
/// <summary>
|
||||
/// UTC timestamp at which the AgentExecutor.log exit-code line for this platform script was
|
||||
/// observed. Drives the deadline check in
|
||||
/// <see cref="ImeLogTracker.FlushStalePlatformScriptResults"/> — set only on the platform path
|
||||
/// <see cref="ImeLogTracker.FlushPendingPlatformScriptResults"/> — set only on the platform path
|
||||
/// (PS-AGENT exit code); null until an exit code is seen.
|
||||
/// </summary>
|
||||
public System.DateTime? ExitObservedAtUtc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Source (CMTrace) timestamp of the IME <c>PS-SCRIPT-RESULT</c> line that delivered
|
||||
/// <see cref="Result"/> (platform scripts, <c>ime_policy_result</c> only). The emitted event is
|
||||
/// bound to it — the emit may happen passes later (see <see cref="ResultHeldSinceUtc"/>), when
|
||||
/// the tracker's "last matched" line is an unrelated one. Also the reference a later start line
|
||||
/// of the same policy is compared against: a start older than the emitted result is the late
|
||||
/// start line of that run, not a new run.
|
||||
/// </summary>
|
||||
public System.DateTime? ResultObservedAtUtc { get; set; }
|
||||
|
||||
/// <summary>Pattern id of the line that delivered <see cref="Result"/> (surfaced as <c>patternId</c> on the event).</summary>
|
||||
public string ResultPatternId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Wall-clock time (agent UTC) at which the IME result was seen while the executor end block
|
||||
/// (exit code, stdout, stderr) had not been read yet. The tracker holds the emit until the end
|
||||
/// block arrives — an overwritten block surfaces one pass later through the ledger rewind that
|
||||
/// the result itself triggers, and the AgentExecutor.log read can simply lag behind the IME log
|
||||
/// read — or until <c>ImeLogTracker.PlatformScriptEndBlockGrace</c> elapsed. Null when the
|
||||
/// result was not held.
|
||||
/// </summary>
|
||||
public System.DateTime? ResultHeldSinceUtc { get; set; }
|
||||
|
||||
/// <summary>"True" or "False" compliance result (remediation detection / post-detection only).</summary>
|
||||
public string ComplianceResult { get; set; }
|
||||
|
||||
|
||||
+65
-19
@@ -44,6 +44,9 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals
|
||||
private readonly Func<bool> _userEspAppsSettledProbe;
|
||||
private readonly Func<System.Collections.Generic.IReadOnlyList<Ime.AppPackageState>> _starvedUserEspAppsProbe;
|
||||
private readonly Func<System.Collections.Generic.IReadOnlyList<Ime.AppPackageState>> _packageStatesProbe;
|
||||
// Session 683f1eff: registry failure detail still sitting in an open settle window,
|
||||
// consulted when a Shell-Core failure terminalises first (see OnShellCoreEspFailureDetected).
|
||||
private readonly Func<EspFailureDetectedEventArgs> _pendingEspFailureProbe;
|
||||
|
||||
// Session caa6cf50 gate-starvation fix (2026-06-11) — fire-once guard for the
|
||||
// user-ESP-apps-settled AccountSetup synthesis (see MaybeSynthesizeAccountSetupComplete).
|
||||
@@ -174,8 +177,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals
|
||||
/// late "Installing-→-Error" promotion can classify the failure correctly
|
||||
/// AND refuse to promote when the failure originated outside the Apps
|
||||
/// subcategory (a non-Apps HRESULT does not describe per-app outcome).
|
||||
/// Null when no HRESULT-carrying ESP failure has fired (or all observed
|
||||
/// failures came from ShellCoreTracker, which has no HRESULT surface).
|
||||
/// Null when no registry-derived failure detail has been observed — neither from
|
||||
/// a settle window that expired nor from one a Shell-Core failure inherited.
|
||||
/// </summary>
|
||||
public Termination.EspTerminalFailureSnapshot LastEspTerminalFailure { get; private set; }
|
||||
|
||||
@@ -243,7 +246,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals
|
||||
Func<bool> accountSetupActivityProbe = null,
|
||||
Func<bool> userEspAppsSettledProbe = null,
|
||||
Func<System.Collections.Generic.IReadOnlyList<Ime.AppPackageState>> starvedUserEspAppsProbe = null,
|
||||
Func<System.Collections.Generic.IReadOnlyList<Ime.AppPackageState>> packageStatesProbe = null)
|
||||
Func<System.Collections.Generic.IReadOnlyList<Ime.AppPackageState>> packageStatesProbe = null,
|
||||
Func<EspFailureDetectedEventArgs> pendingEspFailureProbe = null)
|
||||
{
|
||||
_sessionId = sessionId ?? throw new ArgumentNullException(nameof(sessionId));
|
||||
_tenantId = tenantId ?? throw new ArgumentNullException(nameof(tenantId));
|
||||
@@ -277,6 +281,10 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals
|
||||
// provisioning tracker name not-completed apps on an Apps-subcategory failure.
|
||||
_packageStatesProbe = packageStatesProbe
|
||||
?? (() => Array.Empty<Ime.AppPackageState>());
|
||||
// Test seam: defaults to the provisioning tracker's open settle window (null when
|
||||
// none is armed or the tracker is not running, e.g. Device Preparation).
|
||||
_pendingEspFailureProbe = pendingEspFailureProbe
|
||||
?? (() => _provisioningTracker?.TryGetPendingFailureArgs());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -658,6 +666,11 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals
|
||||
internal void TriggerEspExitedForTest(DateTime occurredAtUtc) =>
|
||||
OnEspExited(this, new EspExitedEventArgs(occurredAtUtc));
|
||||
|
||||
// Test seam for a Shell-Core 62407 failure — drives the coordinator forwarder including
|
||||
// the pending-settle inheritance, without a real ShellCoreTracker.
|
||||
internal void TriggerShellCoreEspFailureForTest(string failureType) =>
|
||||
OnShellCoreEspFailureDetected(this, failureType);
|
||||
|
||||
// Test seam for HelloWizardStarted — same contract as TriggerEspExitedForTest.
|
||||
internal void TriggerHelloWizardStartedForTest(DateTime occurredAtUtc) =>
|
||||
OnHelloWizardStarted(this, new HelloWizardStartedEventArgs(occurredAtUtc));
|
||||
@@ -673,33 +686,56 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals
|
||||
// Forwarder for ShellCoreTracker (event-log-derived ESP failures, e.g. Shell-Core 62407
|
||||
// failure descriptions). Source-event timestamp is mirrored so the adapter can stamp the
|
||||
// DecisionSignal with the historical instant on backfill. ShellCoreTracker has no HRESULT
|
||||
// surface, so only FailureType is set on the args.
|
||||
// surface; the registry detail is inherited from an open settle window (see below).
|
||||
private void OnShellCoreEspFailureDetected(object sender, string failureType)
|
||||
{
|
||||
LastEventOccurredAtUtc = (sender as ShellCoreTracker)?.LastEventOccurredAtUtc;
|
||||
try
|
||||
{
|
||||
EspFailureDetected?.Invoke(this, new EspFailureDetectedEventArgs(failureType));
|
||||
EspFailureDetected?.Invoke(this, BuildShellCoreFailureArgs(failureType));
|
||||
}
|
||||
catch (Exception ex) { _logger.Error($"Error forwarding ShellCore EspFailureDetected for '{failureType}'", ex); }
|
||||
finally { LastEventOccurredAtUtc = null; }
|
||||
}
|
||||
|
||||
// Forwarder for ProvisioningStatusTracker (registry-derived ESP failures). Args carry the
|
||||
// full failure detail (FailureType, ErrorCode, FailedSubcategory, Category) extracted
|
||||
// from the failed subcategory's statusText. Provisioning has no source-event timestamp
|
||||
// surfaced today, so the adapter falls back to clock.
|
||||
private void OnProvisioningEspFailureDetected(object sender, EspFailureDetectedEventArgs args)
|
||||
// Session 683f1eff: the Shell-Core 62407 failure usually lands INSIDE the provisioning
|
||||
// tracker's 30-s settle window — after the registry named the failed subcategory and its
|
||||
// HRESULT, before the settle timer would have forwarded them. The engine terminalises on
|
||||
// the first EspTerminalFailure and the agent stops seconds later, so whatever this signal
|
||||
// carries is all the terminal event, the backend FailureReason and the termination-time
|
||||
// app classification ever see (a third of all ESP terminal failures took this path). The
|
||||
// pending detail is inherited here; FailureType stays the Shell-Core value because that
|
||||
// is the signal that actually terminalised.
|
||||
private EspFailureDetectedEventArgs BuildShellCoreFailureArgs(string failureType)
|
||||
{
|
||||
EspFailureDetectedEventArgs pending = null;
|
||||
try { pending = _pendingEspFailureProbe(); }
|
||||
catch (Exception ex) { _logger.Warning($"EspAndHelloTracker: pending ESP failure probe threw: {ex.Message}"); }
|
||||
|
||||
if (pending == null)
|
||||
return new EspFailureDetectedEventArgs(failureType);
|
||||
|
||||
RecordTerminalFailureSnapshot(pending);
|
||||
_logger.Info(
|
||||
$"EspAndHelloTracker: Shell-Core failure '{failureType}' inherits the pending provisioning failure detail " +
|
||||
$"(category={pending.Category ?? "n/a"}, failedSubcategory={pending.FailedSubcategory ?? "n/a"}, " +
|
||||
$"errorCode={pending.ErrorCode ?? "n/a"})");
|
||||
return new EspFailureDetectedEventArgs(
|
||||
failureType,
|
||||
errorCode: pending.ErrorCode,
|
||||
failedSubcategory: pending.FailedSubcategory,
|
||||
category: pending.Category,
|
||||
likelyCulpritApps: pending.LikelyCulpritApps);
|
||||
}
|
||||
|
||||
// Session 080edee9 follow-up + Codex review (P2/P3) — snapshot the full failure context
|
||||
// (HRESULT + failedSubcategory + category) so EnrollmentTerminationHandler can read it on
|
||||
// the terminal-failure pathway. Only update when the args carry at least one of the three
|
||||
// fields, so a bare Shell-Core failure cannot wipe out the registry snapshot. Deliberately
|
||||
// NOT gated on ErrorCode alone — a non-Apps subcategory failure without HRESULT is still
|
||||
// useful for downstream "should we promote?" gating.
|
||||
private void RecordTerminalFailureSnapshot(EspFailureDetectedEventArgs args)
|
||||
{
|
||||
LastEventOccurredAtUtc = null;
|
||||
// Session 080edee9 follow-up + Codex review (P2/P3) — snapshot the full
|
||||
// failure context (HRESULT + failedSubcategory + category) before
|
||||
// forwarding so EnrollmentTerminationHandler can read it on the terminal-
|
||||
// failure pathway. Only update when the args carry at least one of the
|
||||
// three fields, so a later ShellCore-derived failure (which has none of
|
||||
// them) cannot wipe out the registry snapshot. We deliberately do NOT
|
||||
// gate on ErrorCode alone — a non-Apps subcategory failure without
|
||||
// HRESULT is still useful for downstream "should we promote?" gating.
|
||||
if (!string.IsNullOrEmpty(args?.ErrorCode)
|
||||
|| !string.IsNullOrEmpty(args?.FailedSubcategory)
|
||||
|| !string.IsNullOrEmpty(args?.Category))
|
||||
@@ -709,6 +745,16 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals
|
||||
failedSubcategory: args.FailedSubcategory,
|
||||
category: args.Category);
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarder for ProvisioningStatusTracker (registry-derived ESP failures). Args carry the
|
||||
// full failure detail (FailureType, ErrorCode, FailedSubcategory, Category) extracted
|
||||
// from the failed subcategory's statusText. Provisioning has no source-event timestamp
|
||||
// surfaced today, so the adapter falls back to clock.
|
||||
private void OnProvisioningEspFailureDetected(object sender, EspFailureDetectedEventArgs args)
|
||||
{
|
||||
LastEventOccurredAtUtc = null;
|
||||
RecordTerminalFailureSnapshot(args);
|
||||
try
|
||||
{
|
||||
EspFailureDetected?.Invoke(this, args);
|
||||
|
||||
+31
@@ -80,6 +80,14 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals
|
||||
"AccountSetupCategory.Status"
|
||||
};
|
||||
|
||||
// Order in which TryGetPendingFailureArgs resolves concurrently armed settle windows.
|
||||
private static readonly string[] PendingFailurePreference =
|
||||
{
|
||||
"DeviceSetupCategory.Status",
|
||||
"AccountSetupCategory.Status",
|
||||
"DevicePreparationCategory.Status"
|
||||
};
|
||||
|
||||
private readonly AgentLogger _logger;
|
||||
private readonly string _sessionId;
|
||||
private readonly string _tenantId;
|
||||
@@ -1711,6 +1719,29 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals
|
||||
internal void TriggerSettleTimerForTest(string categoryName)
|
||||
=> OnProvisioningFailureSettleExpired(categoryName);
|
||||
|
||||
/// <summary>
|
||||
/// The registry-derived failure detail of a settle window that is still open (armed,
|
||||
/// not yet expired or retracted) — null when none is pending. Read by the coordinator
|
||||
/// when a Shell-Core 62407 failure arrives inside the window, so the terminal signal
|
||||
/// inherits HRESULT / failed subcategory / category instead of racing them away
|
||||
/// (session 683f1eff). Windows fail one category at a time, but should two be armed
|
||||
/// the device-phase category wins: it is the one the ESP page reports.
|
||||
/// </summary>
|
||||
internal EspFailureDetectedEventArgs TryGetPendingFailureArgs()
|
||||
{
|
||||
lock (_stateLock)
|
||||
{
|
||||
if (_provisioningFailureSettleArgs == null || _provisioningFailureSettleArgs.Count == 0)
|
||||
return null;
|
||||
foreach (var category in PendingFailurePreference)
|
||||
{
|
||||
if (_provisioningFailureSettleArgs.TryGetValue(category, out var args) && args != null)
|
||||
return args;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private bool HasCategorySucceededChanged(string categoryName, bool? newValue)
|
||||
{
|
||||
if (!_lastCategorySucceeded.TryGetValue(categoryName, out var oldValue))
|
||||
|
||||
+70
-28
@@ -99,7 +99,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime
|
||||
|
||||
/// <summary>
|
||||
/// Packaging problems that are not cap skips, per kind: <c>path-guard</c> (configured path
|
||||
/// refused), <c>no-user-session</c> (user-profile token without a signed-in user),
|
||||
/// or hard-blocked event log refused),<c>no-user-session</c> (user-profile token without a signed-in user),
|
||||
/// <c>folder-rejected</c>, <c>enumerate</c>, <c>open</c>, <c>copy</c>.
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, int> ProblemsByKind { get; set; } = Empty;
|
||||
@@ -145,6 +145,10 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime
|
||||
// exact point production is exposed. Null in production.
|
||||
internal Action<string> BeforeSourceFileOpen { get; set; }
|
||||
|
||||
// Test seam: which channel writes to an .evtx file. Production asks the registered
|
||||
// channels; a test cannot register one.
|
||||
internal Func<string, string> EventLogChannelResolver { get; set; } = ResolveChannelForEvtxFile;
|
||||
|
||||
// Tracks per-build inclusion totals + skip reasons. Threaded through every
|
||||
// AddLogFiles call so caps are global across all sections, not per section.
|
||||
private sealed class BudgetTracker
|
||||
@@ -462,17 +466,12 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime
|
||||
// 3. Configured additional log paths (global + tenant, validated by guards)
|
||||
foreach (var entry in _configuration.DiagnosticsLogPaths ?? new System.Collections.Generic.List<Shared.Models.DiagnosticsLogPath>())
|
||||
{
|
||||
// Resolve %LOGGED_ON_USER_PROFILE% token and get profile path for guard exception
|
||||
// The profile path only lifts the C:\Users block for entries that use the token.
|
||||
var userProfilePath = UserProfileResolver.ContainsUserProfileToken(entry.Path)
|
||||
? UserProfileResolver.GetLoggedOnUserProfilePath() : null;
|
||||
|
||||
if (!DiagnosticsPathGuards.IsDiagnosticsPathAllowed(entry.Path, _configuration.UnrestrictedMode, userProfilePath))
|
||||
{
|
||||
_logger.Warning($"Diagnostics path blocked by guard: {entry.Path}");
|
||||
ManifestLine($"BLOCKED (path guard): {entry.Path}");
|
||||
tracker.RecordProblem("path-guard");
|
||||
continue;
|
||||
}
|
||||
// Expand BEFORE the guard: the guard must judge the path that is read,
|
||||
// and an unresolved token is not a path it can judge.
|
||||
var expandedPath = UserProfileResolver.ExpandCustomTokens(entry.Path);
|
||||
if (expandedPath == null)
|
||||
{
|
||||
@@ -481,13 +480,23 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime
|
||||
tracker.RecordProblem("no-user-session");
|
||||
continue;
|
||||
}
|
||||
var folder = Path.GetDirectoryName(expandedPath);
|
||||
var pattern = Path.GetFileName(expandedPath);
|
||||
if (string.IsNullOrEmpty(folder)) continue;
|
||||
if (string.IsNullOrEmpty(pattern) || !pattern.Contains(".")) pattern = "*";
|
||||
var zipFolder = $"AdditionalLogs/{Path.GetFileName(folder)}";
|
||||
ManifestLine($"CONFIGURED PATH: '{entry.Path}' -> folder='{folder}' pattern='{pattern}'");
|
||||
AddLogFiles(archive, folder, zipFolder, pattern, tracker, entry.IncludeSubfolders);
|
||||
|
||||
// The guard validates the folder that gets enumerated — never a path
|
||||
// derived from the validated one afterwards.
|
||||
if (!DiagnosticsPathGuards.TryResolveCollectionTarget(
|
||||
expandedPath, entry.IncludeSubfolders, _configuration.UnrestrictedMode, userProfilePath, out var target))
|
||||
{
|
||||
_logger.Warning($"Diagnostics path blocked by guard: {entry.Path}");
|
||||
ManifestLine($"BLOCKED (path guard): {entry.Path}");
|
||||
tracker.RecordProblem("path-guard");
|
||||
continue;
|
||||
}
|
||||
|
||||
var unrestrictedMode = _configuration.UnrestrictedMode;
|
||||
var zipFolder = $"AdditionalLogs/{Path.GetFileName(target.Folder)}";
|
||||
ManifestLine($"CONFIGURED PATH: '{entry.Path}' -> folder='{target.Folder}' pattern='{target.Pattern}' recursive={target.Recurse}");
|
||||
AddLogFiles(archive, target.Folder, zipFolder, target.Pattern, tracker, target.Recurse,
|
||||
dir => DiagnosticsPathGuards.IsDirectoryEnterable(dir, unrestrictedMode, userProfilePath));
|
||||
}
|
||||
|
||||
// Packaging manifest — always written, even when empty of problems, so its
|
||||
@@ -617,14 +626,18 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime
|
||||
// Recursive enumeration that skips reparse-point directories. Materializes the
|
||||
// result into a list so the caller can iterate without holding a directory handle.
|
||||
// Errors during enumeration are swallowed (logged via _logger by the caller).
|
||||
private static List<string> EnumerateFilesNoReparseDirs(string folder, string pattern, bool recurse)
|
||||
// mayEnterDirectory (configured paths only) is asked for every subdirectory before the
|
||||
// walk descends: a hard-blocked subtree can sit below a folder that passed the guard.
|
||||
internal static List<string> EnumerateFilesNoReparseDirs(string folder, string pattern, bool recurse,
|
||||
Func<string, bool> mayEnterDirectory, List<string> refusedDirectories)
|
||||
{
|
||||
var result = new List<string>();
|
||||
CollectFilesNoReparseDirs(folder, pattern, recurse, result);
|
||||
CollectFilesNoReparseDirs(folder, pattern, recurse, mayEnterDirectory, refusedDirectories, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void CollectFilesNoReparseDirs(string folder, string pattern, bool recurse, List<string> result)
|
||||
private static void CollectFilesNoReparseDirs(string folder, string pattern, bool recurse,
|
||||
Func<string, bool> mayEnterDirectory, List<string> refusedDirectories, List<string> result)
|
||||
{
|
||||
string[] files;
|
||||
try { files = Directory.GetFiles(folder, pattern, SearchOption.TopDirectoryOnly); }
|
||||
@@ -643,12 +656,17 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime
|
||||
try { attrs = File.GetAttributes(sub); }
|
||||
catch { continue; }
|
||||
if (IsReparsePoint(attrs)) continue;
|
||||
CollectFilesNoReparseDirs(sub, pattern, recurse: true, result);
|
||||
if (mayEnterDirectory != null && !mayEnterDirectory(sub))
|
||||
{
|
||||
refusedDirectories.Add(sub);
|
||||
continue;
|
||||
}
|
||||
CollectFilesNoReparseDirs(sub, pattern, recurse: true, mayEnterDirectory, refusedDirectories, result);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddLogFiles(ZipArchive archive, string sourceFolder, string zipFolder, string searchPattern,
|
||||
BudgetTracker tracker, bool includeSubfolders = false)
|
||||
BudgetTracker tracker, bool includeSubfolders = false, Func<string, bool> mayEnterDirectory = null)
|
||||
{
|
||||
// The folder stays pinned (open handle) for this whole section. The path guards and
|
||||
// the enumeration below only ever judge path strings; every byte that reaches the
|
||||
@@ -673,9 +691,11 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime
|
||||
}
|
||||
|
||||
List<string> files;
|
||||
var refusedDirectories = new List<string>();
|
||||
try
|
||||
{
|
||||
files = EnumerateFilesNoReparseDirs(pinned.LexicalPath, searchPattern, includeSubfolders);
|
||||
files = EnumerateFilesNoReparseDirs(pinned.LexicalPath, searchPattern, includeSubfolders,
|
||||
mayEnterDirectory, refusedDirectories);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -685,6 +705,13 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime
|
||||
return;
|
||||
}
|
||||
|
||||
// Expected outcome, not a packaging problem: the subtree is off limits by design.
|
||||
foreach (var refused in refusedDirectories)
|
||||
{
|
||||
_logger.Info($"Diagnostics subfolder skipped by guard: {refused}");
|
||||
ManifestLine($"SKIPPED DIR (path guard): {refused}");
|
||||
}
|
||||
|
||||
if (files.Count == 0)
|
||||
ManifestLine($"NO MATCH: {sourceFolder} pattern '{searchPattern}'");
|
||||
|
||||
@@ -704,7 +731,22 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime
|
||||
// the tenant-configured BootstrapperAgent channel never made it into the ZIP.
|
||||
if (file.EndsWith(".evtx", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
tempExport = TryExportEventLogChannel(file);
|
||||
// Hard-blocked channels are refused for every source and in every mode,
|
||||
// by file name AND by the channel the export would read: the path guard
|
||||
// cannot judge what a wildcard or folder entry resolves to, and the %4
|
||||
// fallback of the resolver turns a "Security.evtx" in any allowed folder
|
||||
// into the live Security channel.
|
||||
var channel = EventLogChannelResolver(file);
|
||||
if (DiagnosticsPathGuards.IsHardBlockedEventLogFile(file) ||
|
||||
GatherRuleGuards.IsHardBlockedEventLogChannel(channel))
|
||||
{
|
||||
_logger.Warning($"Event log blocked by channel guard (channel '{channel}'): {file}");
|
||||
ManifestLine($"BLOCKED (event-log channel guard, channel '{channel}'): {file}");
|
||||
tracker.RecordProblem("path-guard");
|
||||
return;
|
||||
}
|
||||
|
||||
tempExport = TryExportEventLogChannel(file, channel);
|
||||
if (tempExport == null)
|
||||
{
|
||||
// Export failed (channel not resolvable / wevtutil error) — fall through
|
||||
@@ -850,16 +892,16 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime
|
||||
/// <summary>
|
||||
/// Exports the event-log channel behind a winevt .evtx file to a temp copy via
|
||||
/// <c>wevtutil epl</c> (active channels hold their file exclusively locked, so a raw
|
||||
/// copy fails). Channel resolution happens against the registered channels — see
|
||||
/// <see cref="ResolveChannelForEvtxFile"/>. Returns the temp file path (caller deletes)
|
||||
/// or null when the export failed.
|
||||
/// copy fails). The caller resolved <paramref name="channel"/> against the registered
|
||||
/// channels — see <see cref="ResolveChannelForEvtxFile"/> — and checked it against the
|
||||
/// hard-blocked channels. Returns the temp file path (caller deletes) or null when the
|
||||
/// export failed.
|
||||
/// </summary>
|
||||
private string TryExportEventLogChannel(string evtxPath)
|
||||
private string TryExportEventLogChannel(string evtxPath, string channel)
|
||||
{
|
||||
string tempPath = null;
|
||||
try
|
||||
{
|
||||
var channel = ResolveChannelForEvtxFile(evtxPath);
|
||||
tempPath = Path.Combine(Path.GetTempPath(), $"am-evtx-{Guid.NewGuid():N}.evtx");
|
||||
var psi = new System.Diagnostics.ProcessStartInfo
|
||||
{
|
||||
|
||||
+195
-64
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
{
|
||||
@@ -70,12 +71,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
@"C:\Windows\System32\config", // SAM, SECURITY, SYSTEM hives
|
||||
};
|
||||
|
||||
// Allowed subdirectories under a user profile (used with %LOGGED_ON_USER_PROFILE% token)
|
||||
private static readonly string[] AllowedUserProfileSubdirs = new[]
|
||||
{
|
||||
@"AppData\Local",
|
||||
@"AppData\Roaming",
|
||||
};
|
||||
// The allowed user-profile subdirectories and the vendor allowlist under them live in
|
||||
// GatherRuleGuards, which owns the embedded guardrails.json — one copy for both guards.
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given path is allowed for diagnostics collection.
|
||||
@@ -128,48 +125,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
if (IsSymlinkOrReparsePoint(normalizedDir))
|
||||
return false;
|
||||
|
||||
// C:\Users block always applies (even in unrestricted mode)
|
||||
// Exception: paths under <userProfilePath>\AppData\Local or AppData\Roaming
|
||||
// are allowed when the %LOGGED_ON_USER_PROFILE% token was used.
|
||||
if (normalizedDir.StartsWith(BlockedUsersPrefix, StringComparison.OrdinalIgnoreCase) &&
|
||||
(normalizedDir.Length == BlockedUsersPrefix.Length ||
|
||||
normalizedDir[BlockedUsersPrefix.Length] == Path.DirectorySeparatorChar))
|
||||
{
|
||||
if (!IsUserProfileSubpathAllowed(normalizedDir, userProfilePath))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Additional hard-blocked paths (even in unrestricted mode)
|
||||
foreach (var blocked in AdditionalHardBlockedPrefixes)
|
||||
{
|
||||
var normalizedBlocked = Path.GetFullPath(blocked);
|
||||
if (normalizedDir.StartsWith(normalizedBlocked, StringComparison.OrdinalIgnoreCase) &&
|
||||
(normalizedDir.Length == normalizedBlocked.Length ||
|
||||
normalizedDir[normalizedBlocked.Length] == Path.DirectorySeparatorChar))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// In unrestricted mode, everything except hard-blocked paths is allowed
|
||||
if (unrestrictedMode)
|
||||
return true;
|
||||
|
||||
foreach (var prefix in AllowedDiagnosticsPathPrefixes)
|
||||
{
|
||||
var normalizedPrefix = Path.GetFullPath(prefix);
|
||||
if (normalizedDir.StartsWith(normalizedPrefix, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Segment-bounded: next char must be '\' or end-of-string
|
||||
if (normalizedDir.Length == normalizedPrefix.Length ||
|
||||
normalizedDir[normalizedPrefix.Length] == Path.DirectorySeparatorChar)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return IsNormalizedPathAllowed(normalizedDir, unrestrictedMode, userProfilePath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -179,35 +135,210 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the normalized path falls under one of the allowed subdirectories
|
||||
/// of the detected user profile (AppData\Local, AppData\Roaming).
|
||||
/// Only applies when %LOGGED_ON_USER_PROFILE% was used to resolve the path.
|
||||
/// What the package actually reads for one configured entry: every file matching
|
||||
/// <see cref="Pattern"/> in <see cref="Folder"/>, optionally below it.
|
||||
/// </summary>
|
||||
internal static bool IsUserProfileSubpathAllowed(string normalizedPath, string userProfilePath)
|
||||
public sealed class CollectionTarget
|
||||
{
|
||||
if (string.IsNullOrEmpty(userProfilePath))
|
||||
public CollectionTarget(string folder, string pattern, bool recurse)
|
||||
{
|
||||
Folder = folder;
|
||||
Pattern = pattern;
|
||||
Recurse = recurse;
|
||||
}
|
||||
|
||||
/// <summary>Full path without a trailing separator (drive roots keep theirs).</summary>
|
||||
public string Folder { get; }
|
||||
public string Pattern { get; }
|
||||
public bool Recurse { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves a configured entry to the folder and pattern that get enumerated, and
|
||||
/// validates exactly that. Returns false when the guard refuses the entry.
|
||||
///
|
||||
/// <paramref name="expandedPath"/> must already have the %LOGGED_ON_USER_PROFILE% token
|
||||
/// resolved: validating the raw token would judge a different path than the one read.
|
||||
///
|
||||
/// A wildcard in the last segment selects files in its parent folder. A trailing
|
||||
/// separator or an existing directory selects every file in that folder. Anything else
|
||||
/// names one file — validated as that file and never recursed, because its parent
|
||||
/// folder has not passed the guard. The filesystem decides folder-vs-file, never the
|
||||
/// spelling of the last segment.
|
||||
/// </summary>
|
||||
public static bool TryResolveCollectionTarget(
|
||||
string expandedPath,
|
||||
bool includeSubfolders,
|
||||
bool unrestrictedMode,
|
||||
string userProfilePath,
|
||||
out CollectionTarget target)
|
||||
=> TryResolveCollectionTarget(
|
||||
expandedPath, includeSubfolders, unrestrictedMode, userProfilePath, Directory.Exists, out target);
|
||||
|
||||
internal static bool TryResolveCollectionTarget(
|
||||
string expandedPath,
|
||||
bool includeSubfolders,
|
||||
bool unrestrictedMode,
|
||||
string userProfilePath,
|
||||
Func<string, bool> directoryExists,
|
||||
out CollectionTarget target)
|
||||
{
|
||||
target = null;
|
||||
if (string.IsNullOrWhiteSpace(expandedPath))
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
var normalizedProfile = Path.GetFullPath(userProfilePath);
|
||||
foreach (var subdir in AllowedUserProfileSubdirs)
|
||||
var expanded = Environment.ExpandEnvironmentVariables(expandedPath.Trim());
|
||||
var lastSegment = Path.GetFileName(expanded);
|
||||
|
||||
if (lastSegment.Contains('*') || lastSegment.Contains('?'))
|
||||
{
|
||||
var allowedPrefix = Path.GetFullPath(Path.Combine(normalizedProfile, subdir));
|
||||
if (normalizedPath.StartsWith(allowedPrefix, StringComparison.OrdinalIgnoreCase) &&
|
||||
(normalizedPath.Length == allowedPrefix.Length ||
|
||||
normalizedPath[allowedPrefix.Length] == Path.DirectorySeparatorChar))
|
||||
var parent = Path.GetDirectoryName(expanded);
|
||||
if (string.IsNullOrEmpty(parent))
|
||||
return false;
|
||||
var folder = TrimTrailingSeparators(Path.GetFullPath(parent));
|
||||
if (!IsDiagnosticsPathAllowed(folder, unrestrictedMode, userProfilePath))
|
||||
return false;
|
||||
target = new CollectionTarget(folder, lastSegment, includeSubfolders);
|
||||
return true;
|
||||
}
|
||||
|
||||
var full = TrimTrailingSeparators(Path.GetFullPath(expanded));
|
||||
if (!IsDiagnosticsPathAllowed(full, unrestrictedMode, userProfilePath))
|
||||
return false;
|
||||
|
||||
if (lastSegment.Length == 0 || directoryExists(full))
|
||||
{
|
||||
target = new CollectionTarget(full, "*", includeSubfolders);
|
||||
return true;
|
||||
}
|
||||
|
||||
var fileFolder = Path.GetDirectoryName(full);
|
||||
if (string.IsNullOrEmpty(fileFolder))
|
||||
return false;
|
||||
target = new CollectionTarget(fileFolder, Path.GetFileName(full), recurse: false);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
target = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Guard for every subdirectory a recursive collection is about to enter. The start
|
||||
/// folder passed <see cref="IsDiagnosticsPathAllowed(string, bool, string)"/>, but a
|
||||
/// hard-blocked subtree can sit below an allowed folder (unrestricted mode, drive
|
||||
/// root). Lexical only: reparse points are skipped by the enumeration and refused
|
||||
/// again on the handle that is read.
|
||||
/// </summary>
|
||||
public static bool IsDirectoryEnterable(string directory, bool unrestrictedMode, string userProfilePath)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(directory))
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
return IsNormalizedPathAllowed(Path.GetFullPath(directory), unrestrictedMode, userProfilePath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static string TrimTrailingSeparators(string fullPath)
|
||||
{
|
||||
var root = Path.GetPathRoot(fullPath);
|
||||
var trimmed = fullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
||||
return root != null && trimmed.Length < root.Length ? root : trimmed;
|
||||
}
|
||||
|
||||
// Hard blocks, then (restricted mode) the allowlists — on an already normalized path.
|
||||
private static bool IsNormalizedPathAllowed(string normalizedDir, bool unrestrictedMode, string userProfilePath)
|
||||
{
|
||||
// C:\Users block always applies (even in unrestricted mode)
|
||||
// Exception: paths under <userProfilePath>\AppData\Local or AppData\Roaming
|
||||
// are allowed when the %LOGGED_ON_USER_PROFILE% token was used.
|
||||
if (normalizedDir.StartsWith(BlockedUsersPrefix, StringComparison.OrdinalIgnoreCase) &&
|
||||
(normalizedDir.Length == BlockedUsersPrefix.Length ||
|
||||
normalizedDir[BlockedUsersPrefix.Length] == Path.DirectorySeparatorChar))
|
||||
{
|
||||
if (!GatherRuleGuards.IsUserProfileSubpathAllowed(normalizedDir, userProfilePath))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Additional hard-blocked paths (even in unrestricted mode)
|
||||
foreach (var blocked in AdditionalHardBlockedPrefixes)
|
||||
{
|
||||
var normalizedBlocked = Path.GetFullPath(blocked);
|
||||
if (normalizedDir.StartsWith(normalizedBlocked, StringComparison.OrdinalIgnoreCase) &&
|
||||
(normalizedDir.Length == normalizedBlocked.Length ||
|
||||
normalizedDir[normalizedBlocked.Length] == Path.DirectorySeparatorChar))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Hard-blocked event logs (even in unrestricted mode). Only a path that names the
|
||||
// file is caught here; a wildcard or folder entry is judged per file by
|
||||
// DiagnosticsPackageService, which is the enforcement point.
|
||||
if (IsHardBlockedEventLogFile(normalizedDir))
|
||||
return false;
|
||||
|
||||
// In unrestricted mode, everything except hard-blocked paths is allowed
|
||||
if (unrestrictedMode)
|
||||
return true;
|
||||
|
||||
foreach (var prefix in AllowedDiagnosticsPathPrefixes)
|
||||
{
|
||||
var normalizedPrefix = Path.GetFullPath(prefix);
|
||||
if (normalizedDir.StartsWith(normalizedPrefix, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Segment-bounded: next char must be '\' or end-of-string
|
||||
if (normalizedDir.Length == normalizedPrefix.Length ||
|
||||
normalizedDir[normalizedPrefix.Length] == Path.DirectorySeparatorChar)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Path normalization failure — deny
|
||||
}
|
||||
|
||||
return false;
|
||||
// Vendor folders under the signed-in user's profile — same allowlist the file
|
||||
// collectors use, since it answers the same question: which folders below a
|
||||
// profile carry enrollment-relevant logs rather than the user's own data.
|
||||
return GatherRuleGuards.IsUserProfilePathOnAllowlist(normalizedDir, userProfilePath);
|
||||
}
|
||||
|
||||
// Windows archives a full channel as "Archive-<file name>-yyyy-MM-dd-HH-mm-ss-fff.evtx".
|
||||
private static readonly Regex ArchivedEventLogRegex = new Regex(
|
||||
@"^Archive-(.+)-\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}-\d{3}$",
|
||||
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// True when the .evtx file name belongs to one of
|
||||
/// <see cref="GatherRuleGuards.HardBlockedEventLogChannels"/> — live file
|
||||
/// ("Microsoft-Windows-PowerShell%4Operational.evtx", where %4 encodes '/') or
|
||||
/// archived copy. Judges the NAME only; a channel may write to a file that does not
|
||||
/// follow the convention, so the packager also checks the channel it resolved.
|
||||
/// </summary>
|
||||
internal static bool IsHardBlockedEventLogFile(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
return false;
|
||||
|
||||
var name = Path.GetFileName(path);
|
||||
if (!name.EndsWith(".evtx", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
var stem = name.Substring(0, name.Length - ".evtx".Length);
|
||||
var archived = ArchivedEventLogRegex.Match(stem);
|
||||
if (archived.Success)
|
||||
stem = archived.Groups[1].Value;
|
||||
|
||||
return GatherRuleGuards.IsHardBlockedEventLogChannel(stem.Replace("%4", "/"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+126
-11
@@ -32,6 +32,16 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
// -----------------------------------------------------------------------
|
||||
public static readonly IReadOnlyList<string> AllowedRegistryPrefixes;
|
||||
public static readonly IReadOnlyList<string> AllowedFilePrefixes;
|
||||
|
||||
/// <summary>
|
||||
/// Folders under the signed-in user's profile that file collectors and
|
||||
/// admin-configured diagnostics paths may read, RELATIVE to the profile root
|
||||
/// (e.g. <c>AppData\Local\Vendor</c>). Resolved against the path
|
||||
/// %LOGGED_ON_USER_PROFILE% produced, at match time — the profile name is not
|
||||
/// known when the list is authored, which is why these entries cannot live in
|
||||
/// <see cref="AllowedFilePrefixes"/> (matched literally against the expanded path).
|
||||
/// </summary>
|
||||
public static readonly IReadOnlyList<string> AllowedUserProfileFilePrefixes;
|
||||
public static readonly IReadOnlyList<string> AllowedWmiQueryPrefixes;
|
||||
public static readonly IReadOnlyCollection<string> AllowedCommands;
|
||||
public static readonly IReadOnlyList<string> AllowedEventLogChannels;
|
||||
@@ -63,8 +73,10 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
Path.GetFullPath(@"C:\Windows\System32\config"), // SAM, SECURITY, SYSTEM hives
|
||||
};
|
||||
|
||||
// Allowed subdirectories under a user profile (used with %LOGGED_ON_USER_PROFILE% token)
|
||||
private static readonly string[] AllowedUserProfileSubdirs = new[]
|
||||
// The only subdirectories of a user profile the C:\Users hard block will ever
|
||||
// release (used with %LOGGED_ON_USER_PROFILE%). Single copy for both guards —
|
||||
// DiagnosticsPathGuards delegates here.
|
||||
internal static readonly string[] AllowedUserProfileSubdirs = new[]
|
||||
{
|
||||
@"AppData\Local",
|
||||
@"AppData\Roaming",
|
||||
@@ -118,6 +130,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
|
||||
AllowedRegistryPrefixes = FlattenCategorized(obj, "registryPrefixes", "prefixes");
|
||||
AllowedFilePrefixes = obj["filePrefixes"]?.ToObject<List<string>>() ?? new List<string>();
|
||||
AllowedUserProfileFilePrefixes = LoadUserProfilePrefixes(obj);
|
||||
AllowedWmiQueryPrefixes = obj["wmiQueryPrefixes"]?.ToObject<List<string>>() ?? new List<string>();
|
||||
|
||||
var commands = FlattenCategorized(obj, "allowedCommands", "commands");
|
||||
@@ -136,6 +149,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
|
||||
AllowedRegistryPrefixes = new List<string>();
|
||||
AllowedFilePrefixes = new List<string>();
|
||||
AllowedUserProfileFilePrefixes = new List<string>();
|
||||
AllowedWmiQueryPrefixes = new List<string>();
|
||||
AllowedCommands = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
AllowedEventLogChannels = new List<string>();
|
||||
@@ -143,6 +157,43 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
ResidualWmiPrefixes = new List<string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads <c>userProfileFilePrefixes</c> and drops every entry that is rooted, carries a
|
||||
/// traversal, or does not sit under one of <see cref="AllowedUserProfileSubdirs"/>.
|
||||
/// The C:\Users hard block releases nothing outside AppData, so such an entry could only
|
||||
/// ever be dead text — dropping it at load keeps the list honest instead of silently
|
||||
/// ineffective, and no configuration can widen the hard block's exception.
|
||||
/// </summary>
|
||||
private static List<string> LoadUserProfilePrefixes(JObject obj)
|
||||
{
|
||||
var raw = obj["userProfileFilePrefixes"]?.ToObject<List<string>>() ?? new List<string>();
|
||||
var result = new List<string>();
|
||||
|
||||
foreach (var entry in raw)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(entry))
|
||||
continue;
|
||||
|
||||
var trimmed = entry.Trim().TrimEnd('\\');
|
||||
if (Path.IsPathRooted(trimmed))
|
||||
continue;
|
||||
if (trimmed.IndexOf("..", StringComparison.Ordinal) >= 0)
|
||||
continue;
|
||||
|
||||
foreach (var subdir in AllowedUserProfileSubdirs)
|
||||
{
|
||||
if (trimmed.StartsWith(subdir, StringComparison.OrdinalIgnoreCase) &&
|
||||
(trimmed.Length == subdir.Length || trimmed[subdir.Length] == '\\'))
|
||||
{
|
||||
result.Add(trimmed);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void DeriveWmiClassAllowlist(
|
||||
IReadOnlyList<string> entries,
|
||||
out HashSet<string> classes,
|
||||
@@ -265,12 +316,13 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
|
||||
// C:\Users block always applies (even in unrestricted mode)
|
||||
// Exception: paths under <userProfilePath>\AppData\Local or AppData\Roaming
|
||||
// are allowed when the %LOGGED_ON_USER_PROFILE% token was used.
|
||||
// are allowed when the %LOGGED_ON_USER_PROFILE% token was used. The exception
|
||||
// only LIFTS the block — in restricted mode the allowlist below still decides.
|
||||
if (normalizedPath.StartsWith(BlockedUsersPrefix, StringComparison.OrdinalIgnoreCase) &&
|
||||
(normalizedPath.Length == BlockedUsersPrefix.Length ||
|
||||
normalizedPath[BlockedUsersPrefix.Length] == Path.DirectorySeparatorChar))
|
||||
{
|
||||
if (!DiagnosticsPathGuards.IsUserProfileSubpathAllowed(normalizedPath, userProfilePath))
|
||||
if (!IsUserProfileSubpathAllowed(normalizedPath, userProfilePath))
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -288,9 +340,61 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
if (unrestrictedMode)
|
||||
return true;
|
||||
|
||||
return AllowedFilePrefixes.Any(prefix =>
|
||||
if (AllowedFilePrefixes.Any(prefix =>
|
||||
normalizedPath.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) &&
|
||||
(normalizedPath.Length == prefix.Length || normalizedPath[prefix.Length] == '\\'));
|
||||
(normalizedPath.Length == prefix.Length || normalizedPath[prefix.Length] == '\\')))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Vendor folders under the signed-in user's profile. Only reachable when the rule
|
||||
// spelled %LOGGED_ON_USER_PROFILE% and the resolver found an interactive user —
|
||||
// a null profile denies, so a machine with nobody signed in cannot widen anything.
|
||||
return IsUserProfilePathOnAllowlist(normalizedPath, userProfilePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True when the normalized path sits under one of <see cref="AllowedUserProfileSubdirs"/>
|
||||
/// of the detected user profile. This is the C:\Users hard block's only exception; it
|
||||
/// releases the block and nothing else. A null profile denies.
|
||||
/// </summary>
|
||||
internal static bool IsUserProfileSubpathAllowed(string normalizedPath, string userProfilePath)
|
||||
=> AllowedUserProfileSubdirs.Any(subdir =>
|
||||
MatchesProfileRelativePrefix(normalizedPath, userProfilePath, subdir));
|
||||
|
||||
/// <summary>
|
||||
/// True when the normalized path is admitted by an entry of
|
||||
/// <see cref="AllowedUserProfileFilePrefixes"/> under the detected user profile.
|
||||
/// </summary>
|
||||
internal static bool IsUserProfilePathOnAllowlist(string normalizedPath, string userProfilePath)
|
||||
=> AllowedUserProfileFilePrefixes.Any(prefix =>
|
||||
MatchesProfileRelativePrefix(normalizedPath, userProfilePath, prefix));
|
||||
|
||||
/// <summary>
|
||||
/// Segment-bounded match of <paramref name="normalizedPath"/> against
|
||||
/// <profile>\<relativePrefix>. Both sides go through Path.GetFullPath, so a
|
||||
/// traversal spelled into either one cannot slip past the boundary check.
|
||||
/// </summary>
|
||||
private static bool MatchesProfileRelativePrefix(
|
||||
string normalizedPath, string userProfilePath, string relativePrefix)
|
||||
{
|
||||
if (string.IsNullOrEmpty(normalizedPath) || string.IsNullOrEmpty(userProfilePath))
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
var allowedPrefix = Path.GetFullPath(
|
||||
Path.Combine(Path.GetFullPath(userProfilePath), relativePrefix));
|
||||
|
||||
return normalizedPath.StartsWith(allowedPrefix, StringComparison.OrdinalIgnoreCase) &&
|
||||
(normalizedPath.Length == allowedPrefix.Length ||
|
||||
normalizedPath[allowedPrefix.Length] == Path.DirectorySeparatorChar);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Path normalization failure — deny.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -347,11 +451,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
var trimmed = channel.Trim();
|
||||
|
||||
// Hard blocks apply even in unrestricted mode
|
||||
foreach (var blocked in HardBlockedEventLogChannels)
|
||||
{
|
||||
if (MatchesChannel(trimmed, blocked))
|
||||
return false;
|
||||
}
|
||||
if (IsHardBlockedEventLogChannel(trimmed))
|
||||
return false;
|
||||
|
||||
if (unrestrictedMode)
|
||||
return true;
|
||||
@@ -359,6 +460,20 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Gather
|
||||
return AllowedEventLogChannels.Any(allowed => MatchesChannel(trimmed, allowed));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True when the channel is one of <see cref="HardBlockedEventLogChannels"/>. Every
|
||||
/// path that can read an event log asks here — the event-log collector and the
|
||||
/// diagnostics package alike — so the block cannot hold in one and leak through another.
|
||||
/// </summary>
|
||||
internal static bool IsHardBlockedEventLogChannel(string channel)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(channel))
|
||||
return false;
|
||||
|
||||
var trimmed = channel.Trim();
|
||||
return HardBlockedEventLogChannels.Any(blocked => MatchesChannel(trimmed, blocked));
|
||||
}
|
||||
|
||||
private static bool MatchesChannel(string channel, string prefix)
|
||||
=> channel.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) &&
|
||||
(channel.Length == prefix.Length || channel[prefix.Length] == '/');
|
||||
|
||||
+6
@@ -209,6 +209,12 @@ namespace AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Periodic
|
||||
data["ime_line_budget_breaks"] = health.BudgetBreaks;
|
||||
data["ime_held_tails"] = health.HeldTails;
|
||||
data["ime_unanchored_patterns"] = health.UnanchoredPatterns;
|
||||
data["ime_overwrite_rewinds"] = health.OverwriteRewinds;
|
||||
data["ime_overwrite_bytes_reprocessed"] = health.OverwriteBytesReprocessed;
|
||||
data["ime_verify_passes"] = health.VerifyPasses;
|
||||
data["ime_verified_bytes"] = health.VerifiedBytes;
|
||||
data["ime_pass_max_ms"] = health.PassMaxMs;
|
||||
data["ime_pass_gap_max_ms"] = health.PassGapMaxMs;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -9,9 +9,11 @@ namespace AutopilotMonitor.Agent.V2.Core.Orchestration
|
||||
/// <para>
|
||||
/// The orchestrator consumes this event and translates it into a synthetic
|
||||
/// <c>DeadlineFired</c>-<see cref="AutopilotMonitor.DecisionCore.Signals.DecisionSignal"/>
|
||||
/// whose <c>OccurredAtUtc</c> equals the deadline's <see cref="ActiveDeadline.DueAtUtc"/>,
|
||||
/// not the wall-clock firing time — so replay stays deterministic even if the agent
|
||||
/// restarts long after the deadline has passed.
|
||||
/// whose <c>OccurredAtUtc</c> equals the deadline's <see cref="ActiveDeadline.DueAtUtc"/>
|
||||
/// (the policy instant the wait expired), with the due time repeated in the payload. The
|
||||
/// firing clock is deliberately NOT the stamp: a timer due during Modern Standby or a
|
||||
/// reboot fires only afterwards, and the outage would otherwise become session duration
|
||||
/// (D-238).
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed class DeadlineFiredEventArgs : EventArgs
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -1159,7 +1160,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Orchestration
|
||||
var evidence = new Evidence(
|
||||
kind: EvidenceKind.Synthetic,
|
||||
identifier: e.Deadline.Name,
|
||||
summary: $"deadline '{e.Deadline.Name}' fired at {e.Deadline.DueAtUtc:O}");
|
||||
summary: $"deadline '{e.Deadline.Name}' due {e.Deadline.DueAtUtc:O} fired at {e.FiredAtUtc:O}");
|
||||
|
||||
// The reducer reads the deadline name under SignalPayloadKeys.Deadline
|
||||
// (see HandleDeadlineFiredV1 in DecisionEngine.Shared). Forward the
|
||||
@@ -1180,7 +1181,15 @@ namespace AutopilotMonitor.Agent.V2.Core.Orchestration
|
||||
payload[SignalPayloadKeys.Deadline] = e.Deadline.Name;
|
||||
}
|
||||
|
||||
// OccurredAtUtc = DueAtUtc (not firedAt) — replay-determinism per DeadlineFiredEventArgs doc.
|
||||
// OccurredAtUtc = DueAtUtc, not the firing clock (D-238). Every effect of the
|
||||
// resulting step (phase_transition, enrollment_complete) is stamped with the signal
|
||||
// time; a timer that came due during Modern Standby or a reboot fires only afterwards,
|
||||
// and the firing clock would make the verdict — and the session duration — grow by
|
||||
// the whole outage (measured: days for devices closed right after provisioning).
|
||||
// The due time also travels in the payload so the stale-fire guards identify the
|
||||
// deadline incarnation independently of the stamp.
|
||||
payload[SignalPayloadKeys.DeadlineDueAtUtc] = e.Deadline.DueAtUtc.ToString("O", CultureInfo.InvariantCulture);
|
||||
|
||||
_ingress.Post(
|
||||
kind: DecisionSignalKind.DeadlineFired,
|
||||
occurredAtUtc: e.Deadline.DueAtUtc,
|
||||
|
||||
@@ -39,7 +39,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Orchestration
|
||||
/// <summary>
|
||||
/// Wird vom Timer-Thread geraised, wenn ein Deadline fällig wird. Subscriber
|
||||
/// ist typischerweise der Orchestrator, der daraus ein synthetisches
|
||||
/// <c>DeadlineFired</c>-Signal mit <c>OccurredAtUtc = deadline.DueAtUtc</c> erzeugt.
|
||||
/// <c>DeadlineFired</c>-Signal mit <c>OccurredAtUtc = deadline.DueAtUtc</c> erzeugt; die
|
||||
/// Fälligkeit reist zusätzlich im Payload (<c>SignalPayloadKeys.DeadlineDueAtUtc</c>).
|
||||
/// </summary>
|
||||
event EventHandler<DeadlineFiredEventArgs>? Fired;
|
||||
|
||||
|
||||
@@ -1254,23 +1254,25 @@ namespace AutopilotMonitor.Agent.V2.Core.SignalAdapters
|
||||
if (!string.IsNullOrEmpty(script.Stdout)) data["stdout"] = script.Stdout!;
|
||||
if (!string.IsNullOrEmpty(script.Stderr)) data["stderr"] = script.Stderr!;
|
||||
|
||||
// The exit-code fallback emits asynchronously to log parsing, so the tracker's
|
||||
// "last matched" pattern/timestamp belong to an unrelated later line. Bind the event to
|
||||
// the script's own exit-observed timestamp and omit the misleading patternId instead.
|
||||
// A platform-script completion is bound to the script's own line, not to the tracker's
|
||||
// "last matched" one: the exit-code fallback emits a grace period after its exit line,
|
||||
// and an IME result held for its executor end block emits at the end of a later pass —
|
||||
// by then LastMatched* name an unrelated line. The fallback carries no patternId (no
|
||||
// line delivered its result). Health scripts carry neither stamp and stay on the last
|
||||
// matched line, which IS their result line.
|
||||
var isFallback = string.Equals(script.ResultSource, "agentexecutor_fallback", StringComparison.Ordinal);
|
||||
if (!isFallback)
|
||||
{
|
||||
var patternId = _tracker.LastMatchedPatternId;
|
||||
var patternId = script.ResultPatternId ?? _tracker.LastMatchedPatternId;
|
||||
if (!string.IsNullOrEmpty(patternId)) data["patternId"] = patternId!;
|
||||
}
|
||||
|
||||
bool derivedFromClock;
|
||||
DateTime? rawSourceTs;
|
||||
DateTime now;
|
||||
if (isFallback && script.ExitObservedAtUtc.HasValue)
|
||||
now = ResolveOccurredAt(script.ExitObservedAtUtc, out derivedFromClock, out rawSourceTs);
|
||||
else
|
||||
now = ResolveOccurredAt(out derivedFromClock, out rawSourceTs);
|
||||
var sourceTs = isFallback ? script.ExitObservedAtUtc : script.ResultObservedAtUtc;
|
||||
var now = sourceTs.HasValue
|
||||
? ResolveOccurredAt(sourceTs, out derivedFromClock, out rawSourceTs)
|
||||
: ResolveOccurredAt(out derivedFromClock, out rawSourceTs);
|
||||
|
||||
// Historic replay (a previous enrollment's log) — do not emit at all. The early
|
||||
// return also skips MaybeEmitBootstrapDetected / MaybeEmitScriptTimeoutSuspected,
|
||||
|
||||
@@ -584,6 +584,12 @@ namespace AutopilotMonitor.Agent.V2.Core.Termination
|
||||
["heldTails"] = health.HeldTails,
|
||||
["unanchoredPatterns"] = health.UnanchoredPatterns,
|
||||
["hsParseFailures"] = health.HealthScriptResultParseFailures,
|
||||
["overwriteRewinds"] = health.OverwriteRewinds,
|
||||
["overwriteBytesReprocessed"] = health.OverwriteBytesReprocessed,
|
||||
["verifyPasses"] = health.VerifyPasses,
|
||||
["verifiedBytes"] = health.VerifiedBytes,
|
||||
["passMaxMs"] = health.PassMaxMs,
|
||||
["passGapMaxMs"] = health.PassGapMaxMs,
|
||||
["agentVersion"] = _agentVersion ?? string.Empty,
|
||||
};
|
||||
if (!string.IsNullOrEmpty(health.ImeAgentVersion))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.Eventing.Reader;
|
||||
using System.IO;
|
||||
@@ -41,17 +42,31 @@ namespace AutopilotMonitor.Agent.V2
|
||||
|
||||
// ----------------------------------------------------------------- Multi-instance guard
|
||||
|
||||
/// <summary>Pid and terminal-services session of one name-matched process.</summary>
|
||||
internal readonly struct AgentProcessFact
|
||||
{
|
||||
public AgentProcessFact(int pid, int sessionId)
|
||||
{
|
||||
Pid = pid;
|
||||
SessionId = sessionId;
|
||||
}
|
||||
|
||||
public int Pid { get; }
|
||||
public int SessionId { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <c>true</c> when another process with the same filename (module name) is alive.
|
||||
/// Race-safe by design: we count processes sharing our name; if 1, only we exist.
|
||||
/// <c>true</c> when another agent runtime is alive. A process name is not a protected
|
||||
/// namespace — any user session can run a binary named like the agent — so a name match
|
||||
/// only counts in session 0. Every runtime start (Scheduled Task, WMI launch, self-update
|
||||
/// restart) lands there, and a standard user cannot start a process in it.
|
||||
/// </summary>
|
||||
internal static bool IsAnotherAgentInstanceRunning()
|
||||
{
|
||||
try
|
||||
{
|
||||
var self = Process.GetCurrentProcess();
|
||||
var siblings = Process.GetProcessesByName(self.ProcessName);
|
||||
return siblings.Length > 1;
|
||||
using (var self = Process.GetCurrentProcess())
|
||||
return FindSiblingAgentPid(SnapshotProcessesByName(self.ProcessName), self.Id) != 0;
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -61,6 +76,40 @@ namespace AutopilotMonitor.Agent.V2
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pid of the first name-matched process that is a real agent runtime — session 0 and not
|
||||
/// the caller itself — or <c>0</c> when there is none. Shared by the multi-instance guard
|
||||
/// and install mode's launch verification so both trust the same identity.
|
||||
/// </summary>
|
||||
internal static int FindSiblingAgentPid(IEnumerable<AgentProcessFact> nameMatches, int ownPid)
|
||||
{
|
||||
if (nameMatches == null) return 0;
|
||||
foreach (var candidate in nameMatches)
|
||||
{
|
||||
if (candidate.Pid == ownPid) continue;
|
||||
if (candidate.SessionId != 0) continue;
|
||||
return candidate.Pid;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads pid and session of every process with the given name. A process that vanished
|
||||
/// between enumeration and read is skipped: it is not a live sibling, and failing open
|
||||
/// means the worst case is two agents, never none.
|
||||
/// </summary>
|
||||
internal static List<AgentProcessFact> SnapshotProcessesByName(string processName)
|
||||
{
|
||||
var facts = new List<AgentProcessFact>();
|
||||
foreach (var process in Process.GetProcessesByName(processName))
|
||||
{
|
||||
try { facts.Add(new AgentProcessFact(process.Id, process.SessionId)); }
|
||||
catch { /* exited mid-read */ }
|
||||
finally { process.Dispose(); }
|
||||
}
|
||||
return facts;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------- clean-exit marker
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -731,7 +731,8 @@ namespace AutopilotMonitor.Agent.V2
|
||||
/// 10s / 500ms contract as the bootstrap script's Get-Process probe. The install-mode
|
||||
/// process shares the runtime's exe name, so the own pid is excluded; a pre-existing
|
||||
/// foreign runtime cannot be running here because install mode bails earlier on the
|
||||
/// Deployed marker.
|
||||
/// Deployed marker. Same identity rule as the multi-instance guard: a same-named process
|
||||
/// in a user session is not the runtime and must not satisfy the verification.
|
||||
/// </summary>
|
||||
private static int WaitForDetachedRuntimePid(string exePath, AgentLogger logger)
|
||||
{
|
||||
@@ -742,16 +743,8 @@ namespace AutopilotMonitor.Agent.V2
|
||||
{
|
||||
try
|
||||
{
|
||||
var candidates = Process.GetProcessesByName(processName);
|
||||
try
|
||||
{
|
||||
var runtime = candidates.FirstOrDefault(p => p.Id != ownPid);
|
||||
if (runtime != null) return runtime.Id;
|
||||
}
|
||||
finally
|
||||
{
|
||||
foreach (var p in candidates) p.Dispose();
|
||||
}
|
||||
var runtimePid = FindSiblingAgentPid(SnapshotProcessesByName(processName), ownPid);
|
||||
if (runtimePid != 0) return runtimePid;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using AutopilotMonitor.DecisionCore.Engine;
|
||||
using AutopilotMonitor.DecisionCore.Signals;
|
||||
using AutopilotMonitor.DecisionCore.State;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.DecisionCore.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// The reducer identifies a deadline incarnation through the due time carried in the
|
||||
/// payload (<see cref="SignalPayloadKeys.DeadlineDueAtUtc"/>), never through the signal
|
||||
/// time: the host stamps <c>DeadlineFired</c> with the due time (D-238), but the guards
|
||||
/// must stay correct for any stamp — a late fire after Modern Standby or a restart, a
|
||||
/// replayed signal, or a future switch to the firing clock. Without the key they fall back
|
||||
/// to the signal time (pre-key signal logs, where the two were equal by contract).
|
||||
/// </summary>
|
||||
public sealed class DeadlineFireTimeStampTests
|
||||
{
|
||||
private static readonly DateTime T0 = new DateTime(2026, 9, 9, 6, 31, 0, DateTimeKind.Utc);
|
||||
|
||||
private static DecisionSignal MakeSignal(
|
||||
long ordinal,
|
||||
DecisionSignalKind kind,
|
||||
DateTime occurredAtUtc,
|
||||
IReadOnlyDictionary<string, string>? payload = null) =>
|
||||
new DecisionSignal(
|
||||
sessionSignalOrdinal: ordinal,
|
||||
sessionTraceOrdinal: ordinal,
|
||||
kind: kind,
|
||||
kindSchemaVersion: 1,
|
||||
occurredAtUtc: occurredAtUtc,
|
||||
sourceOrigin: "test",
|
||||
evidence: new Evidence(EvidenceKind.Synthetic, $"t-{kind}-{ordinal}", $"synthetic {kind}"),
|
||||
payload: payload);
|
||||
|
||||
/// <summary>What the host posts: name plus due time, OccurredAtUtc = firing clock.</summary>
|
||||
private static Dictionary<string, string> FiredPayload(string deadlineName, DateTime dueAtUtc) =>
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
[SignalPayloadKeys.Deadline] = deadlineName,
|
||||
[SignalPayloadKeys.DeadlineDueAtUtc] = dueAtUtc.ToString("O", CultureInfo.InvariantCulture),
|
||||
};
|
||||
|
||||
// ============================================================== helper
|
||||
|
||||
[Fact]
|
||||
public void DeadlineDueAtUtc_reads_the_payload_and_falls_back_to_the_signal_time()
|
||||
{
|
||||
var due = T0.AddMinutes(5);
|
||||
var fired = T0.AddMinutes(9);
|
||||
|
||||
var withKey = MakeSignal(1, DecisionSignalKind.DeadlineFired, fired, FiredPayload(DeadlineNames.HelloSafety, due));
|
||||
Assert.Equal(due, DecisionEngine.DeadlineDueAtUtc(withKey));
|
||||
Assert.Equal(DateTimeKind.Utc, DecisionEngine.DeadlineDueAtUtc(withKey).Kind);
|
||||
|
||||
var withoutKey = MakeSignal(2, DecisionSignalKind.DeadlineFired, fired,
|
||||
new Dictionary<string, string> { [SignalPayloadKeys.Deadline] = DeadlineNames.HelloSafety });
|
||||
Assert.Equal(fired, DecisionEngine.DeadlineDueAtUtc(withoutKey));
|
||||
|
||||
var garbage = MakeSignal(3, DecisionSignalKind.DeadlineFired, fired,
|
||||
new Dictionary<string, string> { [SignalPayloadKeys.DeadlineDueAtUtc] = "not-a-time" });
|
||||
Assert.Equal(fired, DecisionEngine.DeadlineDueAtUtc(garbage));
|
||||
}
|
||||
|
||||
// ============================================================== Classic: standby at the Hello prompt
|
||||
|
||||
[Fact]
|
||||
public void Late_HelloSafety_fire_stamps_the_resolution_and_the_grace_window_from_the_firing_clock()
|
||||
{
|
||||
// Session ac5660b8 shape: ESP exits, the user leaves the device at the Hello prompt, it
|
||||
// enters Modern Standby, HelloSafety is due during the sleep and fires ~3 min late after
|
||||
// wake. Fed with the firing clock as signal time, the reducer must stamp the Hello
|
||||
// timeout fact, the Finalizing step and the FinalizingGrace due time from that time —
|
||||
// the stamp policy is the host's choice (D-238), the reducer follows the signal.
|
||||
var engine = new DecisionEngine();
|
||||
var state = DecisionState.CreateInitial("sess-standby", "tenant", T0);
|
||||
state = engine.Reduce(state, MakeSignal(0, DecisionSignalKind.SessionStarted, T0)).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(1, DecisionSignalKind.EspPhaseChanged, T0.AddMinutes(1),
|
||||
new Dictionary<string, string> { [SignalPayloadKeys.EspPhase] = "AccountSetup" })).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(2, DecisionSignalKind.AccountSetupProvisioningComplete, T0.AddMinutes(2))).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(3, DecisionSignalKind.DesktopArrived, T0.AddMinutes(3))).NewState;
|
||||
|
||||
var espExit = T0.AddMinutes(6).AddSeconds(50.2144855);
|
||||
state = engine.Reduce(state, MakeSignal(4, DecisionSignalKind.EspExiting, espExit)).NewState;
|
||||
var helloSafety = Assert.Single(state.Deadlines, d => d.Name == DeadlineNames.HelloSafety);
|
||||
Assert.Equal(espExit.AddSeconds(300), helloSafety.DueAtUtc);
|
||||
|
||||
// Timer fires after wake, 2m 54s past due.
|
||||
var firedAt = helloSafety.DueAtUtc.AddSeconds(174);
|
||||
var finalizing = engine.Reduce(state, MakeSignal(5, DecisionSignalKind.DeadlineFired, firedAt,
|
||||
FiredPayload(DeadlineNames.HelloSafety, helloSafety.DueAtUtc)));
|
||||
|
||||
Assert.True(finalizing.Transition.Taken);
|
||||
Assert.Equal(SessionStage.Finalizing, finalizing.NewState.Stage);
|
||||
Assert.Equal("Timeout", finalizing.NewState.HelloOutcome!.Value);
|
||||
Assert.Equal(firedAt, finalizing.NewState.HelloResolvedUtc!.Value);
|
||||
|
||||
var grace = Assert.Single(finalizing.NewState.Deadlines, d => d.Name == DeadlineNames.FinalizingGrace);
|
||||
Assert.Equal(firedAt.AddSeconds(5), grace.DueAtUtc);
|
||||
|
||||
// The chained grace fires on time and completes; nothing carries the old due time.
|
||||
var completed = engine.Reduce(finalizing.NewState, MakeSignal(6, DecisionSignalKind.DeadlineFired, grace.DueAtUtc,
|
||||
FiredPayload(DeadlineNames.FinalizingGrace, grace.DueAtUtc)));
|
||||
Assert.Equal(SessionStage.Completed, completed.NewState.Stage);
|
||||
Assert.Equal(SessionOutcome.EnrollmentComplete, completed.NewState.Outcome);
|
||||
Assert.Contains(completed.Effects, e =>
|
||||
e.Kind == DecisionEffectKind.EmitEventTimelineEntry
|
||||
&& e.Parameters != null
|
||||
&& e.Parameters.TryGetValue("eventType", out var et) && et == "enrollment_complete");
|
||||
}
|
||||
|
||||
// ============================================================== SelfDeploying guard C
|
||||
|
||||
[Fact]
|
||||
public void SelfDeploying_guard_C_matches_the_incarnation_by_payload_due_time_not_by_firing_clock()
|
||||
{
|
||||
var engine = new DecisionEngine();
|
||||
var state = DecisionState.CreateInitial("sd-late", "t", T0);
|
||||
state = engine.Reduce(state, MakeSignal(0, DecisionSignalKind.SessionStarted, T0)).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(1, DecisionSignalKind.EspPhaseChanged, T0.AddMinutes(1),
|
||||
new Dictionary<string, string> { [SignalPayloadKeys.EspPhase] = "DeviceSetup" })).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(2, DecisionSignalKind.DeviceSetupProvisioningComplete, T0.AddMinutes(3))).NewState;
|
||||
var armed = Assert.Single(state.Deadlines, d => d.Name == DeadlineNames.DeviceOnlyEspDetection);
|
||||
Assert.Equal(T0.AddMinutes(8), armed.DueAtUtc);
|
||||
|
||||
// A stale fire of an OLDER incarnation (due T+5) arriving late: dead-end, deadline kept.
|
||||
var stale = engine.Reduce(state, MakeSignal(3, DecisionSignalKind.DeadlineFired, T0.AddMinutes(12),
|
||||
FiredPayload(DeadlineNames.DeviceOnlyEspDetection, T0.AddMinutes(5))));
|
||||
Assert.False(stale.Transition.Taken);
|
||||
Assert.Equal("device_only_esp_detection_stale_due_at_mismatch", stale.Transition.DeadEndReason);
|
||||
Assert.Single(stale.NewState.Deadlines, d => d.Name == DeadlineNames.DeviceOnlyEspDetection);
|
||||
|
||||
// The real fire, 4 min late (standby), carries the armed due time: terminal.
|
||||
var real = engine.Reduce(stale.NewState, MakeSignal(4, DecisionSignalKind.DeadlineFired, T0.AddMinutes(12),
|
||||
FiredPayload(DeadlineNames.DeviceOnlyEspDetection, armed.DueAtUtc)));
|
||||
Assert.True(real.Transition.Taken);
|
||||
Assert.Equal(SessionStage.Completed, real.NewState.Stage);
|
||||
}
|
||||
|
||||
// ============================================================== RealmJoin re-arm guard
|
||||
|
||||
[Fact]
|
||||
public void RealmJoin_rearm_guard_compares_the_payload_due_time_not_the_firing_clock()
|
||||
{
|
||||
var engine = new DecisionEngine();
|
||||
var state = DecisionState.CreateInitial("rj-late", "t", T0);
|
||||
state = engine.Reduce(state, MakeSignal(0, DecisionSignalKind.SessionStarted, T0)).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(1, DecisionSignalKind.EspPhaseChanged, T0.AddMinutes(1),
|
||||
new Dictionary<string, string> { [SignalPayloadKeys.EspPhase] = "DeviceSetup" })).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(2, DecisionSignalKind.EspPhaseChanged, T0.AddMinutes(2),
|
||||
new Dictionary<string, string> { [SignalPayloadKeys.EspPhase] = "AccountSetup" })).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(3, DecisionSignalKind.EspExiting, T0.AddMinutes(3))).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(4, DecisionSignalKind.HelloResolved, T0.AddMinutes(4),
|
||||
new Dictionary<string, string> { [SignalPayloadKeys.HelloOutcome] = "Success" })).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(5, DecisionSignalKind.RealmJoinDetected, T0.AddMinutes(5),
|
||||
new Dictionary<string, string> { [DecisionEngine.RealmJoinPayloadKeys.DeploymentPhase] = "100" })).NewState;
|
||||
state = engine.Reduce(state, MakeSignal(6, DecisionSignalKind.DesktopArrived, T0.AddMinutes(6))).NewState;
|
||||
var first = Assert.Single(state.Deadlines, d => d.Name == DeadlineNames.RealmJoinTimeout);
|
||||
|
||||
// Simulate the activity-based re-arm: the armed incarnation is now due later.
|
||||
var rearmedDue = first.DueAtUtc.AddMinutes(25);
|
||||
var rearmed = state.ToBuilder()
|
||||
.CancelDeadline(DeadlineNames.RealmJoinTimeout)
|
||||
.AddDeadline(new ActiveDeadline(
|
||||
name: DeadlineNames.RealmJoinTimeout,
|
||||
dueAtUtc: rearmedDue,
|
||||
firesSignalKind: DecisionSignalKind.DeadlineFired,
|
||||
firesPayload: new Dictionary<string, string> { [SignalPayloadKeys.Deadline] = DeadlineNames.RealmJoinTimeout }))
|
||||
.Build();
|
||||
|
||||
// The OLD incarnation's fire arrives late — after the re-armed due time on the clock.
|
||||
// Only the payload due time identifies it as superseded.
|
||||
var firedAt = rearmedDue.AddMinutes(1);
|
||||
var stale = engine.Reduce(rearmed, MakeSignal(7, DecisionSignalKind.DeadlineFired, firedAt,
|
||||
FiredPayload(DeadlineNames.RealmJoinTimeout, first.DueAtUtc)));
|
||||
Assert.False(stale.Transition.Taken);
|
||||
Assert.Equal("realmjoin_timeout_stale_superseded_by_rearm", stale.Transition.DeadEndReason);
|
||||
Assert.Null(stale.NewState.RealmJoinFacts.Outcome);
|
||||
|
||||
// The re-armed incarnation's own fire, same firing clock: timeout is applied.
|
||||
var real = engine.Reduce(stale.NewState, MakeSignal(8, DecisionSignalKind.DeadlineFired, firedAt,
|
||||
FiredPayload(DeadlineNames.RealmJoinTimeout, rearmedDue)));
|
||||
Assert.True(real.Transition.Taken);
|
||||
Assert.Equal("Timeout", real.NewState.RealmJoinFacts.Outcome!.Value);
|
||||
Assert.Equal(SessionStage.Finalizing, real.NewState.Stage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -706,7 +706,7 @@ namespace AutopilotMonitor.DecisionCore.Tests
|
||||
var deadline = Assert.Single(signalStep.NewState.Deadlines, d => d.Name == DeadlineNames.DeviceOnlyEspDetection);
|
||||
Assert.Equal(T0.AddMinutes(7), deadline.DueAtUtc);
|
||||
|
||||
// DeadlineFired (OccurredAtUtc = DueAtUtc per scheduler contract) → terminal.
|
||||
// DeadlineFired at the due time (no payload due key → helper falls back to the signal time) → terminal.
|
||||
var step = engine.Reduce(signalStep.NewState, MakeSignal(3, DecisionSignalKind.DeadlineFired, T0.AddMinutes(7),
|
||||
new Dictionary<string, string> { [SignalPayloadKeys.Deadline] = DeadlineNames.DeviceOnlyEspDetection }));
|
||||
|
||||
|
||||
@@ -443,8 +443,9 @@ namespace AutopilotMonitor.DecisionCore.Tests
|
||||
{
|
||||
// Cancel-then-rearm race: old deadline (DueAt=T+8) fires queued; before processing,
|
||||
// DeviceSetupProvisioningComplete arrives (signal at T+10) → cancel+rearm to DueAt=T+15.
|
||||
// The queued OLD fire (OccurredAtUtc=T+8) arrives. Guard C must dead-end WITHOUT
|
||||
// cancelling the new deadline.
|
||||
// The queued OLD fire arrives carrying its due time T+8 (no payload key here, so the
|
||||
// helper falls back to the signal time). Guard C must dead-end WITHOUT cancelling the
|
||||
// new deadline.
|
||||
var engine = new DecisionEngine();
|
||||
var state = PrimeDeviceSetup(engine, DecisionState.CreateInitial("sd-11", "t", T0));
|
||||
|
||||
@@ -467,7 +468,7 @@ namespace AutopilotMonitor.DecisionCore.Tests
|
||||
firesPayload: new Dictionary<string, string> { [SignalPayloadKeys.Deadline] = DeadlineNames.DeviceOnlyEspDetection }));
|
||||
state = rearmedBuilder.Build();
|
||||
|
||||
// OLD queued fire arrives with OccurredAtUtc=T+8 — mismatches new deadline at T+15.
|
||||
// OLD queued fire arrives with due time T+8 — mismatches the new deadline at T+15.
|
||||
var step = engine.Reduce(state, MakeSignal(99, DecisionSignalKind.DeadlineFired, T0.AddMinutes(8),
|
||||
new Dictionary<string, string> { [SignalPayloadKeys.Deadline] = DeadlineNames.DeviceOnlyEspDetection }));
|
||||
|
||||
|
||||
@@ -54,6 +54,37 @@ namespace AutopilotMonitor.DecisionCore.Tests
|
||||
Assert.Equal("AccountSetup", roundtripped.Payload!["phase"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SignalSerializer_roundtrip_keeps_date_like_payload_strings_verbatim()
|
||||
{
|
||||
// The shared reader tokenizes ISO-8601 strings as dates; the string dictionaries must
|
||||
// still come back as the round-trip text the writer produced, never culture-formatted
|
||||
// (the DeadlineFired due time is parsed back with the "o" format on replay).
|
||||
var due = new DateTime(2026, 4, 20, 9, 55, 0, DateTimeKind.Utc);
|
||||
var original = new DecisionSignal(
|
||||
sessionSignalOrdinal: 7,
|
||||
sessionTraceOrdinal: 7,
|
||||
kind: DecisionSignalKind.DeadlineFired,
|
||||
kindSchemaVersion: 1,
|
||||
occurredAtUtc: due.AddMinutes(5),
|
||||
sourceOrigin: "DeadlineScheduler",
|
||||
evidence: new Evidence(
|
||||
kind: EvidenceKind.Synthetic,
|
||||
identifier: "hello_safety",
|
||||
summary: "deadline fired",
|
||||
derivationInputs: new Dictionary<string, string> { ["armedAt"] = "2026-04-20T09:50:00.0000000Z" }),
|
||||
payload: new Dictionary<string, string>
|
||||
{
|
||||
["deadline"] = "hello_safety",
|
||||
["deadlineDueAtUtc"] = due.ToString("O"),
|
||||
});
|
||||
|
||||
var roundtripped = SignalSerializer.Deserialize(SignalSerializer.Serialize(original));
|
||||
|
||||
Assert.Equal("2026-04-20T09:55:00.0000000Z", roundtripped.Payload!["deadlineDueAtUtc"]);
|
||||
Assert.Equal("2026-04-20T09:50:00.0000000Z", roundtripped.Evidence.DerivationInputs!["armedAt"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SignalSerializer_Deserialize_MissingEvidence_throws()
|
||||
{
|
||||
|
||||
@@ -447,41 +447,27 @@ public class AdminWireParityTests
|
||||
// ---- TriggerMaintenance --------------------------------------------------------------
|
||||
|
||||
[Fact]
|
||||
public void TriggerMaintenanceResponse_matches_the_manual_trigger_shape()
|
||||
public void TriggerMaintenanceResponse_matches_the_queued_trigger_shape()
|
||||
{
|
||||
var result = new MaintenanceResult
|
||||
{
|
||||
Success = true,
|
||||
TriggeredBy = "admin@contoso.com",
|
||||
TriggeredAt = new DateTime(2026, 8, 30, 13, 10, 0, DateTimeKind.Utc),
|
||||
DurationMs = 1234,
|
||||
StalledSessionsChecked = true,
|
||||
MetricsAggregated = true,
|
||||
AggregatedDate = "2026-08-29",
|
||||
DataCleanupExecuted = false,
|
||||
PlatformStatsRecomputed = true,
|
||||
DevicesBlockedForExcessiveData = 0,
|
||||
ContactEmailsBackfilled = 2,
|
||||
};
|
||||
var userEmail = "admin@contoso.com";
|
||||
var triggeredAt = new DateTime(2026, 8, 30, 13, 10, 5, DateTimeKind.Utc);
|
||||
|
||||
AssertParity(
|
||||
new
|
||||
{
|
||||
success = true,
|
||||
message = "Maintenance tasks completed",
|
||||
result = result,
|
||||
message = "Maintenance run queued",
|
||||
triggeredBy = userEmail,
|
||||
triggeredAt
|
||||
triggeredAt,
|
||||
targetDate = "2026-08-29",
|
||||
aggregateOnly = true
|
||||
},
|
||||
new TriggerMaintenanceResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Maintenance tasks completed",
|
||||
Result = result,
|
||||
Message = "Maintenance run queued",
|
||||
TriggeredBy = userEmail,
|
||||
TriggeredAt = triggeredAt
|
||||
TriggeredAt = triggeredAt,
|
||||
TargetDate = "2026-08-29",
|
||||
AggregateOnly = true
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
using System.Net;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using AutopilotMonitor.Functions.Functions.Rules;
|
||||
using AutopilotMonitor.Functions.Helpers;
|
||||
using AutopilotMonitor.Functions.Services;
|
||||
using AutopilotMonitor.Shared.DataAccess;
|
||||
using AutopilotMonitor.Shared.Models;
|
||||
using Azure.Core.Serialization;
|
||||
using Microsoft.Azure.Functions.Worker;
|
||||
using Microsoft.Azure.Functions.Worker.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace AutopilotMonitor.Functions.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Analyze twin of <see cref="GatherRulesFunctionAuthorEndpointTests"/>: the rule Author is
|
||||
/// stamped from the caller's JWT on create, on a PUT-upsert of an unknown rule and on the
|
||||
/// template copy, and a true update keeps the original creator over editor and body.
|
||||
/// Before this pin, custom analyze rules were created as "Autopilot Monitor" and the edit
|
||||
/// path let the browser choose the author.
|
||||
/// </summary>
|
||||
public class AnalyzeRulesFunctionAuthorEndpointTests
|
||||
{
|
||||
private const string TenantId = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";
|
||||
private const string RuleId = "ANALYZE-CUSTOM-777";
|
||||
private const string TemplateRuleId = "ANALYZE-SEC-004";
|
||||
|
||||
private sealed class FakeHttpResponseData : HttpResponseData
|
||||
{
|
||||
public FakeHttpResponseData(FunctionContext context) : base(context) { }
|
||||
public override HttpStatusCode StatusCode { get; set; } = HttpStatusCode.OK;
|
||||
public override HttpHeadersCollection Headers { get; set; } = new();
|
||||
public override Stream Body { get; set; } = new MemoryStream();
|
||||
public override HttpCookies Cookies { get; } = new Mock<HttpCookies>().Object;
|
||||
}
|
||||
|
||||
private static FunctionContext BuildContext(ClaimsPrincipal? principal)
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddOptions();
|
||||
services.Configure<WorkerOptions>(o => o.Serializer =
|
||||
new JsonObjectSerializer(ApiJsonOptions.Create()));
|
||||
var provider = services.BuildServiceProvider();
|
||||
|
||||
var items = new Dictionary<object, object>();
|
||||
if (principal != null)
|
||||
{
|
||||
items["ClaimsPrincipal"] = principal;
|
||||
}
|
||||
|
||||
var context = new Mock<FunctionContext>();
|
||||
context.SetupGet(c => c.Items).Returns(items);
|
||||
context.SetupGet(c => c.InstanceServices).Returns(provider);
|
||||
return context.Object;
|
||||
}
|
||||
|
||||
private static HttpRequestData BuildRequest(ClaimsPrincipal? principal, object body)
|
||||
{
|
||||
var context = BuildContext(principal);
|
||||
var req = new Mock<HttpRequestData>(context);
|
||||
req.SetupGet(r => r.Headers).Returns(new HttpHeadersCollection());
|
||||
req.SetupGet(r => r.Body).Returns(
|
||||
new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(body))));
|
||||
req.Setup(r => r.CreateResponse()).Returns(() => new FakeHttpResponseData(context));
|
||||
return req.Object;
|
||||
}
|
||||
|
||||
private static ClaimsPrincipal Principal(params (string Type, string Value)[] claims)
|
||||
=> new(new ClaimsIdentity(claims.Select(c => new Claim(c.Type, c.Value)), "TestAuth"));
|
||||
|
||||
private static ClaimsPrincipal AlicePrincipal()
|
||||
=> Principal(("tid", TenantId), ("name", "Alice Admin"), ("upn", "alice@contoso.com"));
|
||||
|
||||
private static (AnalyzeRulesFunction function, List<AnalyzeRule> stored) BuildFunction(
|
||||
AnalyzeRule? existingTenantRule = null, AnalyzeRule? globalRule = null)
|
||||
{
|
||||
var repo = new Mock<IRuleRepository>(MockBehavior.Loose);
|
||||
var stored = new List<AnalyzeRule>();
|
||||
|
||||
repo.Setup(r => r.AnalyzeRuleExistsAsync(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(false);
|
||||
repo.Setup(r => r.GetAnalyzeRulesAsync("global")).ReturnsAsync(
|
||||
globalRule == null ? new List<AnalyzeRule>() : new List<AnalyzeRule> { globalRule });
|
||||
repo.Setup(r => r.GetAnalyzeRulesAsync(TenantId)).ReturnsAsync(
|
||||
existingTenantRule == null ? new List<AnalyzeRule>() : new List<AnalyzeRule> { existingTenantRule });
|
||||
repo.Setup(r => r.GetRuleStatesAsync(TenantId)).ReturnsAsync(new Dictionary<string, RuleState>());
|
||||
repo.Setup(r => r.StoreRuleStateAsync(TenantId, It.IsAny<string>(), It.IsAny<RuleState>())).ReturnsAsync(true);
|
||||
repo.Setup(r => r.StoreAnalyzeRuleAsync(It.IsAny<AnalyzeRule>(), TenantId))
|
||||
.Callback<AnalyzeRule, string>((rule, _) => stored.Add(rule))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
var service = new AnalyzeRuleService(repo.Object, NullLogger<AnalyzeRuleService>.Instance);
|
||||
return (new AnalyzeRulesFunction(NullLogger<AnalyzeRulesFunction>.Instance, service), stored);
|
||||
}
|
||||
|
||||
private static AnalyzeRule SpoofedPayload() => new()
|
||||
{
|
||||
RuleId = RuleId,
|
||||
Title = "Proxy PAC unreachable",
|
||||
Description = "Custom",
|
||||
Severity = "warning",
|
||||
Category = "network",
|
||||
Explanation = "The PAC file could not be fetched.",
|
||||
IsBuiltIn = false,
|
||||
IsCommunity = false,
|
||||
Enabled = true,
|
||||
Author = "Spoofed Author",
|
||||
Conditions = new List<RuleCondition>
|
||||
{
|
||||
new() { Signal = "pac_failed", Source = "event_type", EventType = "network_pac_failed", Required = true }
|
||||
},
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task CreateRule_stamps_author_from_jwt_not_from_body()
|
||||
{
|
||||
var (function, stored) = BuildFunction();
|
||||
var req = BuildRequest(AlicePrincipal(), SpoofedPayload());
|
||||
|
||||
var response = await function.CreateRule(req);
|
||||
|
||||
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||||
var written = Assert.Single(stored);
|
||||
Assert.Equal("Alice Admin", written.Author);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateRule_falls_back_to_product_name_without_identifying_claims()
|
||||
{
|
||||
var (function, stored) = BuildFunction();
|
||||
var req = BuildRequest(Principal(("tid", TenantId)), SpoofedPayload());
|
||||
|
||||
var response = await function.CreateRule(req);
|
||||
|
||||
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||||
var written = Assert.Single(stored);
|
||||
Assert.Equal("Autopilot Monitor", written.Author);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateRule_upsert_of_unknown_rule_stamps_author_from_jwt()
|
||||
{
|
||||
var (function, stored) = BuildFunction(existingTenantRule: null);
|
||||
var req = BuildRequest(AlicePrincipal(), SpoofedPayload());
|
||||
|
||||
var response = await function.UpdateRule(req, RuleId);
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
var written = Assert.Single(stored);
|
||||
Assert.Equal("Alice Admin", written.Author);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateRule_true_update_keeps_original_author_over_editor_and_body()
|
||||
{
|
||||
var existing = SpoofedPayload();
|
||||
existing.Author = "Original Creator";
|
||||
existing.CreatedAt = new DateTime(2026, 1, 2, 3, 4, 5, DateTimeKind.Utc);
|
||||
var (function, stored) = BuildFunction(existing);
|
||||
var req = BuildRequest(
|
||||
Principal(("tid", TenantId), ("name", "Bob Editor")), SpoofedPayload());
|
||||
|
||||
var response = await function.UpdateRule(req, RuleId);
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
var written = Assert.Single(stored);
|
||||
Assert.Equal("Original Creator", written.Author);
|
||||
Assert.Equal(existing.CreatedAt, written.CreatedAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateFromTemplate_stamps_the_copying_admin_not_the_template_author()
|
||||
{
|
||||
var template = new AnalyzeRule
|
||||
{
|
||||
RuleId = TemplateRuleId,
|
||||
Title = "AutoLogon enabled for an unexpected user",
|
||||
Description = "Template",
|
||||
Severity = "warning",
|
||||
Category = "security",
|
||||
Explanation = "Template",
|
||||
IsBuiltIn = true,
|
||||
Author = "Autopilot Monitor",
|
||||
Conditions = new List<RuleCondition>
|
||||
{
|
||||
new() { Signal = "autologon", Source = "event_type", EventType = "autologon_analysis", Operator = "equals", Value = "PLACEHOLDER", Required = true }
|
||||
},
|
||||
TemplateVariables = new List<TemplateVariable>
|
||||
{
|
||||
new() { Name = "allowed", Label = "Allowed", ConditionIndex = 0, Field = "value" }
|
||||
},
|
||||
};
|
||||
var (function, stored) = BuildFunction(globalRule: template);
|
||||
var req = BuildRequest(AlicePrincipal(), new { variables = new Dictionary<string, string> { ["allowed"] = "KIOSK-1" } });
|
||||
|
||||
var response = await function.CreateFromTemplate(req, TemplateRuleId);
|
||||
|
||||
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||||
var written = Assert.Single(stored);
|
||||
Assert.Equal("Alice Admin", written.Author);
|
||||
Assert.Equal(TemplateRuleId, written.DerivedFromTemplateRuleId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using Azure.Data.Tables;
|
||||
using AutopilotMonitor.Functions.Helpers;
|
||||
using AutopilotMonitor.Functions.Services;
|
||||
using AutopilotMonitor.Shared.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Functions.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Observation-end closure of app rows: a row still InProgress when the session's observation
|
||||
/// ended becomes "Incomplete" (terminal, non-failure, outcome unknown). Pins the pure per-row
|
||||
/// predicate behind <see cref="TableStorageService.CloseOpenAppInstallsForSessionAsync"/> and
|
||||
/// the disclosed bucket in the app-metrics payload.
|
||||
/// </summary>
|
||||
public class AppInstallIncompleteCloseTests
|
||||
{
|
||||
private static TableEntity Row(string? status, DateTimeOffset? completedAt = null)
|
||||
{
|
||||
var row = new TableEntity("tenant", "session_App");
|
||||
if (status != null) row["Status"] = status;
|
||||
if (completedAt.HasValue) row["CompletedAt"] = completedAt.Value;
|
||||
return row;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("InProgress")]
|
||||
[InlineData("")]
|
||||
[InlineData(null)]
|
||||
public void ShouldClose_OpenRow(string? status)
|
||||
=> Assert.True(TableStorageService.ShouldCloseAsIncomplete(Row(status)));
|
||||
|
||||
[Theory]
|
||||
[InlineData("Succeeded")]
|
||||
[InlineData("Failed")]
|
||||
[InlineData("Incomplete")]
|
||||
public void ShouldClose_LeavesTerminalRowsAlone(string status)
|
||||
=> Assert.False(TableStorageService.ShouldCloseAsIncomplete(Row(status)));
|
||||
|
||||
[Fact]
|
||||
public void ShouldClose_NeverClosesARowWithAnEnd()
|
||||
{
|
||||
// Out-of-order arrival can leave a CompletedAt on a row whose status batch is late —
|
||||
// an end is an end.
|
||||
Assert.False(TableStorageService.ShouldCloseAsIncomplete(Row("InProgress", DateTimeOffset.UtcNow)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AppMetrics_DisclosesIncompleteOutsideTheRate()
|
||||
{
|
||||
var payload = MetricsMath.BuildAppMetricsPayload(new[]
|
||||
{
|
||||
new AppInstallSummary { AppName = "App", Status = "Succeeded", TerminalState = "Installed", DurationSeconds = 30 },
|
||||
new AppInstallSummary { AppName = "App", Status = "Failed", TerminalState = "Error", DurationSeconds = 30 },
|
||||
new AppInstallSummary { AppName = "App", Status = "Incomplete" },
|
||||
});
|
||||
|
||||
var group = Assert.Single(payload.TopFailingApps);
|
||||
Assert.Equal(3, group.TotalInstalls);
|
||||
Assert.Equal(1, group.Incomplete);
|
||||
Assert.Equal(50.0, group.FailureRate);
|
||||
Assert.Equal(1, payload.TotalIncomplete);
|
||||
}
|
||||
}
|
||||
+24
@@ -155,6 +155,30 @@ public class AppInstallTerminalStateAndReconcileTests
|
||||
Assert.Equal("Succeeded", summary.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reconcile_IncompleteIsSticky_AgainstLateProgressBatch()
|
||||
{
|
||||
// Closed by the observation-end step; a straggling started/progress batch must not reopen it.
|
||||
var existing = new TableEntity("tenant", "session_App") { ["Status"] = "Incomplete" };
|
||||
var summary = new AppInstallSummary { AppName = "App", Status = "InProgress", StartedAt = T0 };
|
||||
|
||||
TableStorageService.ReconcileAppInstallSummaryWithExisting(summary, existing);
|
||||
|
||||
Assert.Equal("Incomplete", summary.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reconcile_RealTerminalOverridesIncomplete()
|
||||
{
|
||||
// The completion arrived after the close step: real evidence beats the hedge.
|
||||
var existing = new TableEntity("tenant", "session_App") { ["Status"] = "Incomplete" };
|
||||
var summary = new AppInstallSummary { AppName = "App", Status = "Succeeded", StartedAt = T0 };
|
||||
|
||||
TableStorageService.ReconcileAppInstallSummaryWithExisting(summary, existing);
|
||||
|
||||
Assert.Equal("Succeeded", summary.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reconcile_Q4Guard_DoesNotFireWithoutStoredCompletedAt()
|
||||
{
|
||||
|
||||
@@ -27,6 +27,13 @@ public class AppVersionRegressionRadarTests
|
||||
private const string App = "Contoso VPN";
|
||||
private static readonly DateTime Now = new(2026, 8, 13, 12, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
/// <summary>
|
||||
/// For the analytics tests only: AppsAnalyticsHelper windows off the REAL clock (DateTime.UtcNow − days),
|
||||
/// so their rows must be dated relative to it — a row dated off the fixed <see cref="Now"/> falls out of
|
||||
/// the 30-day window once the calendar moves on (it did on 2026-09-11).
|
||||
/// </summary>
|
||||
private static readonly DateTime Recent = DateTime.UtcNow.AddDays(-1);
|
||||
|
||||
private static int _sessionCounter;
|
||||
|
||||
private static AppInstallSummary Install(
|
||||
@@ -357,10 +364,10 @@ public class AppVersionRegressionRadarTests
|
||||
|
||||
var rows = new List<AppInstallSummary>
|
||||
{
|
||||
Install("1.0", 100), Install("1.0", 200), Install("1.0", 300), Install("1.0", 400),
|
||||
Install("1.0", 0), // unmeasured — excluded from stats
|
||||
Install("1.0", 50, terminalState: "Skipped"), // skip — excluded
|
||||
Install("1.0", 50, status: "Failed"), // failed — excluded from durations
|
||||
Install("1.0", 100, Recent), Install("1.0", 200, Recent), Install("1.0", 300, Recent), Install("1.0", 400, Recent),
|
||||
Install("1.0", 0, Recent), // unmeasured — excluded from stats
|
||||
Install("1.0", 50, Recent, terminalState: "Skipped"), // skip — excluded
|
||||
Install("1.0", 50, Recent, status: "Failed"), // failed — excluded from durations
|
||||
};
|
||||
|
||||
var root = TestWire.SerializeToElement(await AppsAnalyticsHelper.BuildAnalyticsResponseAsync(
|
||||
@@ -393,7 +400,7 @@ public class AppVersionRegressionRadarTests
|
||||
Assert.Equal(1, emptyRoot.GetProperty("versionRegressions").GetArrayLength());
|
||||
|
||||
var root = TestWire.SerializeToElement(await AppsAnalyticsHelper.BuildAnalyticsResponseAsync(
|
||||
new List<AppInstallSummary> { Install("1.0", 100) }, sessionRepo.Object, App, days: 30,
|
||||
new List<AppInstallSummary> { Install("1.0", 100, Recent) }, sessionRepo.Object, App, days: 30,
|
||||
new List<AppVersionRegressionAlert> { alert }));
|
||||
|
||||
var regression = root.GetProperty("versionRegressions")[0];
|
||||
|
||||
@@ -219,6 +219,8 @@ public class AuthFunctionSideEffectTests
|
||||
// OnboardedBy is the immutable copy of the first-login UPN that auto-promote on
|
||||
// preview approval reads — UpdatedBy may later be clobbered by background syncs.
|
||||
Assert.Equal(Upn, config.OnboardedBy);
|
||||
// The DPA version rides along in the same onboarding write (D-252).
|
||||
Assert.Equal(CurrentDpa, config.DpaVersion);
|
||||
_tenantConfigMock.Verify(x => x.SaveConfigurationAsync(config, It.IsAny<string?>(), It.IsAny<string?>()), Times.Once);
|
||||
_telegramMock.Verify(x => x.SendNewTenantSignupAsync(TenantId, Upn), Times.Once);
|
||||
_globalNotificationMock.Verify(x => x.CreateNotificationAsync(
|
||||
@@ -304,6 +306,23 @@ public class AuthFunctionSideEffectTests
|
||||
_tenantConfigMock.Verify(x => x.SaveConfigurationAsync(It.IsAny<TenantConfiguration>(), It.IsAny<string?>(), It.IsAny<string?>()), Times.Never);
|
||||
}
|
||||
|
||||
private const string CurrentDpa = AutopilotMonitor.Shared.Constants.CurrentDpaVersion;
|
||||
|
||||
[Fact]
|
||||
public async Task HandleNewTenantDomain_WhenDomainAlreadySet_NeverStampsDpaVersion()
|
||||
{
|
||||
// The DPA version is written once, with the onboarding (D-252): an existing tenant's
|
||||
// login writes nothing — no stamp, no save.
|
||||
var config = DefaultConfig();
|
||||
config.DomainName = "contoso.com";
|
||||
config.DpaVersion = null;
|
||||
|
||||
await _sut.HandleNewTenantDomainAsync(config, TenantId, Upn);
|
||||
|
||||
Assert.Null(config.DpaVersion);
|
||||
_tenantConfigMock.Verify(x => x.SaveConfigurationAsync(It.IsAny<TenantConfiguration>(), It.IsAny<string?>(), It.IsAny<string?>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleNewTenantDomain_PreservesExistingOnboardedBy()
|
||||
{
|
||||
|
||||
@@ -175,7 +175,7 @@ public class BuiltInRulesTests
|
||||
"cancelStuckAndSetCurrent", "imeSessionChange", "imeImpersonation", "imeTokenFailure",
|
||||
"enrollmentCompleted", "userSessionZeroApps", "updateDoTelemetry", "scriptStarted",
|
||||
"scriptContext", "scriptExitCode", "scriptOutput", "scriptCompleted",
|
||||
"resetPlatformScriptContext",
|
||||
"resetPlatformScriptContext", "agentInvocationArgument", "agentExecutorCompleted",
|
||||
"healthScriptResult", "healthScriptDetectionResult",
|
||||
"captureExitCode", "captureHResult",
|
||||
"captureAppVersion", "captureAppTypeWinGet", "captureAppTypeMsi",
|
||||
|
||||
@@ -37,9 +37,9 @@ public class CertTenantBindingTests
|
||||
/// Tenant stamped into <c>device-cert-sample.pem</c> by the Intune MDM Device CA. The sample is
|
||||
/// a public-key-only cert from an internal test device; the GUID is the test tenant's own id.
|
||||
/// </summary>
|
||||
private static readonly Guid SampleCertTenantId = Guid.Parse("b54dc1af-5320-4f60-b5d4-821e0cf2a359");
|
||||
internal static readonly Guid SampleCertTenantId = Guid.Parse("b54dc1af-5320-4f60-b5d4-821e0cf2a359");
|
||||
|
||||
private static string SampleCertBase64()
|
||||
internal static string SampleCertBase64()
|
||||
{
|
||||
var assemblyDir = Path.GetDirectoryName(typeof(CertTenantBindingTests).Assembly.Location)!;
|
||||
var pem = File.ReadAllText(Path.Combine(assemblyDir, DeviceSamplePem));
|
||||
@@ -351,7 +351,7 @@ public class CertTenantBindingTests
|
||||
|
||||
// ---------------------------------------------------------------- harness
|
||||
|
||||
private static SecurityValidator BuildValidator(ILogger logger)
|
||||
internal static SecurityValidator BuildValidator(ILogger logger)
|
||||
{
|
||||
var configRepo = Mock.Of<IConfigRepository>();
|
||||
var cache = new MemoryCache(new MemoryCacheOptions());
|
||||
@@ -390,7 +390,7 @@ public class CertTenantBindingTests
|
||||
deviceAssociationValidator: null);
|
||||
}
|
||||
|
||||
private static HttpRequestData BuildRequestWithCert(
|
||||
internal static HttpRequestData BuildRequestWithCert(
|
||||
string certBase64,
|
||||
IDictionary<object, object>? items = null)
|
||||
{
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using AutopilotMonitor.Functions.Functions.Config;
|
||||
using AutopilotMonitor.Functions.Services;
|
||||
using AutopilotMonitor.Shared.Models;
|
||||
using AutopilotMonitor.Shared.Models.Config;
|
||||
@@ -619,12 +620,26 @@ public class ConfigWireParityTests
|
||||
// keys omitted on the wire).
|
||||
TenantDailyRequestLimit = 3000,
|
||||
TenantMonthlyRequestLimit = 60000,
|
||||
// Per purchased delegation slot (the community tier leaves these unset too).
|
||||
SlotDailyRequestLimit = 300,
|
||||
SlotMonthlyRequestLimit = 6000,
|
||||
SlotTenantDailyRequestLimit = 900,
|
||||
SlotTenantMonthlyRequestLimit = 18000,
|
||||
},
|
||||
};
|
||||
|
||||
AssertParity(
|
||||
new { tiers },
|
||||
new PlanTierDefinitionsResponse { Tiers = tiers });
|
||||
new
|
||||
{
|
||||
tiers,
|
||||
// The built-in fallbacks per edition — what a blank definition field means.
|
||||
catalog = new[]
|
||||
{
|
||||
new { edition = "community", dailyRequestLimit = 100, monthlyRequestLimit = 3000, tenantDailyRequestLimit = 300, tenantMonthlyRequestLimit = 9000, includedDelegatedSlots = 0, slotDailyRequestLimit = 0, slotMonthlyRequestLimit = 0, slotTenantDailyRequestLimit = 0, slotTenantMonthlyRequestLimit = 0 },
|
||||
new { edition = "pro", dailyRequestLimit = 1000, monthlyRequestLimit = 20000, tenantDailyRequestLimit = 3000, tenantMonthlyRequestLimit = 60000, includedDelegatedSlots = 2, slotDailyRequestLimit = 300, slotMonthlyRequestLimit = 6000, slotTenantDailyRequestLimit = 900, slotTenantMonthlyRequestLimit = 18000 },
|
||||
},
|
||||
},
|
||||
new PlanTierDefinitionsResponse { Tiers = tiers, Catalog = PlanManagementFunction.BuildUsagePlanCatalog() });
|
||||
}
|
||||
|
||||
// ---- TestWebhookNotification (dual-purpose: success carries the delivery verdict) ----
|
||||
|
||||
@@ -66,16 +66,12 @@ public class DelegatedAdminServiceTests
|
||||
private static void ReturnsGroups(Mock<IAdminRepository> repo, params TenantGroupAssignment[] assignments) =>
|
||||
repo.Setup(r => r.GetGroupAssignmentsForUpnAsync(It.IsAny<string>())).ReturnsAsync(assignments.ToList());
|
||||
|
||||
/// <summary>Stubs the hot-path membership read (tenants from one partition read).</summary>
|
||||
private static void GroupTenants(Mock<IAdminRepository> repo, string groupId, params string[] tenantIds) =>
|
||||
GroupMembership(repo, groupId, chargeHomeTenantQuota: false, tenantIds);
|
||||
|
||||
/// <summary>Stubs the hot-path membership read (tenants + meta flags from one partition read).</summary>
|
||||
private static void GroupMembership(Mock<IAdminRepository> repo, string groupId, bool chargeHomeTenantQuota, params string[] tenantIds) =>
|
||||
repo.Setup(r => r.GetGroupMembershipAsync(groupId)).ReturnsAsync(new TenantGroupMembership
|
||||
{
|
||||
GroupId = groupId,
|
||||
TenantIds = tenantIds.ToList(),
|
||||
ChargeHomeTenantQuota = chargeHomeTenantQuota,
|
||||
});
|
||||
|
||||
private static DelegatedAdminEntry Row(
|
||||
@@ -390,96 +386,6 @@ public class DelegatedAdminServiceTests
|
||||
repo.Verify(r => r.GetGroupMembershipAsync(GroupId1), Times.Once);
|
||||
}
|
||||
|
||||
// --- ChargeHomeTenantQuota: MCP billing attribution rides in the scope (never authorization) ---
|
||||
|
||||
[Fact]
|
||||
public async Task GetScope_FlaggedGroup_MarksItsTenantsHomeCharged()
|
||||
{
|
||||
var (svc, repo) = Build();
|
||||
ReturnsGroups(repo, Assignment(GroupId1));
|
||||
GroupMembership(repo, GroupId1, chargeHomeTenantQuota: true, TenantA, TenantB);
|
||||
|
||||
var scope = await svc.GetScopeAsync(Id(Upn));
|
||||
|
||||
Assert.True(scope.Covers(TenantA));
|
||||
Assert.True(scope.ChargesHome(TenantA));
|
||||
Assert.True(scope.ChargesHome(TenantB));
|
||||
Assert.Equal(2, scope.HomeChargedTenantIds.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetScope_UnflaggedGroup_NothingIsHomeCharged()
|
||||
{
|
||||
var (svc, repo) = Build();
|
||||
ReturnsGroups(repo, Assignment(GroupId1));
|
||||
GroupTenants(repo, GroupId1, TenantA);
|
||||
|
||||
var scope = await svc.GetScopeAsync(Id(Upn));
|
||||
|
||||
Assert.True(scope.Covers(TenantA));
|
||||
Assert.False(scope.ChargesHome(TenantA));
|
||||
Assert.Empty(scope.HomeChargedTenantIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetScope_DirectGrantAndFlaggedGroup_TheFlagWins()
|
||||
{
|
||||
// The operator's explicit group flag is the more privileged choice — a parallel direct grant on the
|
||||
// same tenant does not re-attribute the reads to the managed tenant.
|
||||
var (svc, repo) = Build();
|
||||
Returns(repo, Row(TenantA, Constants.DelegatedRoles.DelegatedAdmin));
|
||||
ReturnsGroups(repo, Assignment(GroupId1));
|
||||
GroupMembership(repo, GroupId1, chargeHomeTenantQuota: true, TenantA);
|
||||
|
||||
var scope = await svc.GetScopeAsync(Id(Upn));
|
||||
|
||||
Assert.Equal(Constants.DelegatedRoles.DelegatedAdmin, scope.RoleFor(TenantA)); // stronger role kept
|
||||
Assert.True(scope.ChargesHome(TenantA));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetScope_DisabledAssignmentToFlaggedGroup_ConfersNothing()
|
||||
{
|
||||
var (svc, repo) = Build();
|
||||
ReturnsGroups(repo, Assignment(GroupId1, enabled: false));
|
||||
GroupMembership(repo, GroupId1, chargeHomeTenantQuota: true, TenantA);
|
||||
|
||||
var scope = await svc.GetScopeAsync(Id(Upn));
|
||||
|
||||
Assert.False(scope.Covers(TenantA));
|
||||
Assert.False(scope.ChargesHome(TenantA));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetChargeHomeTenantQuota_InvalidatesEveryAssignee()
|
||||
{
|
||||
var (svc, repo) = Build();
|
||||
ReturnsGroups(repo, Assignment(GroupId1));
|
||||
GroupMembership(repo, GroupId1, chargeHomeTenantQuota: false, TenantA);
|
||||
repo.Setup(r => r.SetTenantGroupChargeHomeTenantQuotaAsync(GroupId1, true)).ReturnsAsync(true);
|
||||
repo.Setup(r => r.GetGroupAssigneesAsync(GroupId1)).ReturnsAsync(new List<TenantGroupAssignment> { Assignment(GroupId1) });
|
||||
|
||||
await svc.GetScopeAsync(Id(Upn));
|
||||
var ok = await svc.SetChargeHomeTenantQuotaAsync(GroupId1, true);
|
||||
await svc.GetScopeAsync(Id(Upn));
|
||||
|
||||
Assert.True(ok);
|
||||
// The flag rides in the cached scope, so the flip forces a re-resolution for the assignee.
|
||||
repo.Verify(r => r.GetGroupMembershipAsync(GroupId1), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetChargeHomeTenantQuota_UnknownGroup_IsFalse_AndInvalidatesNobody()
|
||||
{
|
||||
var (svc, repo) = Build();
|
||||
repo.Setup(r => r.SetTenantGroupChargeHomeTenantQuotaAsync(It.IsAny<string>(), It.IsAny<bool>())).ReturnsAsync(false);
|
||||
|
||||
var ok = await svc.SetChargeHomeTenantQuotaAsync("no-such-group", true);
|
||||
|
||||
Assert.False(ok);
|
||||
repo.Verify(r => r.GetGroupAssigneesAsync(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
// --- Group mutations go through the service and invalidate cached scope (no stale auth) ---
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -45,7 +45,10 @@ public class DelegationSelfServiceTests
|
||||
public required Mock<ProConferralService> ProConferral { get; init; }
|
||||
}
|
||||
|
||||
private static Harness Build(TenantEdition homeEdition = TenantEdition.Pro, int? slotOverride = null)
|
||||
/// <summary>Pro by conferral only (managed by a Pro MSP): the shape that must never delegate onward.</summary>
|
||||
private static readonly EditionResolution ConferredPro = new(TenantEdition.Pro, EditionSource.Msp, OwnPro: false);
|
||||
|
||||
private static Harness Build(TenantEdition homeEdition = TenantEdition.Pro, int? slotOverride = null, EditionResolution? homeResolution = null)
|
||||
{
|
||||
var rows = new List<DelegationInvitation>();
|
||||
var groupTenants = new List<string>();
|
||||
@@ -104,7 +107,9 @@ public class DelegationSelfServiceTests
|
||||
|
||||
var cache = new MemoryCache(new MemoryCacheOptions());
|
||||
var time = new TestTimeProvider(Now);
|
||||
var entitlements = new StubTenantEntitlementService(t => t == Home ? homeEdition : TenantEdition.Community);
|
||||
var entitlements = homeResolution is EditionResolution resolution
|
||||
? new StubTenantEntitlementService(t => t == Home ? resolution : new EditionResolution(TenantEdition.Community, EditionSource.Community, OwnPro: false), null)
|
||||
: new StubTenantEntitlementService(t => t == Home ? homeEdition : TenantEdition.Community);
|
||||
var delegatedAdmins = new DelegatedAdminService(repo.Object, new StubAdminIdentityBindingService(bound: true), entitlements, cache, NullLogger<DelegatedAdminService>.Instance);
|
||||
var slots = new DelegatedSlotService(repo.Object, configRepo.Object, invitations.Object, cache, NullLogger<DelegatedSlotService>.Instance, time);
|
||||
var audit = new Mock<IMaintenanceRepository>();
|
||||
@@ -199,6 +204,18 @@ public class DelegationSelfServiceTests
|
||||
Assert.Empty(h.Rows);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateInvitation_ConferredProHome_403_NoTransitiveDelegation()
|
||||
{
|
||||
// The would-be manager is Pro only because a Pro MSP manages IT — conferred Pro carries no delegation right.
|
||||
var h = Build(homeResolution: ConferredPro);
|
||||
var r = await h.Svc.CreateInvitationAsync(Home, MspAdmin);
|
||||
Assert.False(r.Ok);
|
||||
Assert.Equal(403, r.Failure!.Status);
|
||||
Assert.Equal(Constants.DelegationCodes.DelegatedAdminNotAllowed, r.Failure.Code);
|
||||
Assert.Empty(h.Rows);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateInvitation_NoFreeSlot_409_WithViolation()
|
||||
{
|
||||
@@ -322,6 +339,17 @@ public class DelegationSelfServiceTests
|
||||
Assert.Equal(Constants.DelegationCodes.ManagerNotEntitled, r.Failure!.Code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Accept_ManagerConferredProOnly_409()
|
||||
{
|
||||
// The inviting tenant lost its own Pro and is now Pro by conferral: its pending link is not redeemable.
|
||||
var h = Build(homeResolution: ConferredPro);
|
||||
h.Rows.Add(Pending());
|
||||
var r = await h.Svc.AcceptAsync(Token(), Customer, CustomerAdmin);
|
||||
Assert.Equal(Constants.DelegationCodes.ManagerNotEntitled, r.Failure!.Code);
|
||||
Assert.Equal(Constants.DelegationInvitationStatus.Pending, h.Rows[0].Status); // untouched
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Accept_LimitLoweredSinceInvitation_409_SlotViolation()
|
||||
{
|
||||
@@ -453,6 +481,16 @@ public class DelegationSelfServiceTests
|
||||
h.Repo.Verify(x => x.EnsureOwnedTenantGroupAsync(GroupId, It.IsAny<string>(), Home), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Assign_ConferredProHome_403()
|
||||
{
|
||||
var h = Build(homeResolution: ConferredPro);
|
||||
var r = await h.Svc.AssignAsync(Home, "Analyst@Partner.Example", MspAdmin);
|
||||
Assert.Equal(403, r.Failure!.Status);
|
||||
Assert.Equal(Constants.DelegationCodes.DelegatedAdminNotAllowed, r.Failure.Code);
|
||||
h.Repo.Verify(x => x.AssignGroupAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Unassign_NotAssigned_404()
|
||||
{
|
||||
|
||||
@@ -106,12 +106,11 @@ public class DelegationWireParityTests
|
||||
// ---- Self-service delegation ------------------------------------------------------------
|
||||
|
||||
[Fact]
|
||||
public void ManagedTenantListResponse_matches_the_managed_shape_and_omits_null_usage()
|
||||
public void ManagedTenantListResponse_matches_the_managed_shape_and_omits_null_domain_and_since()
|
||||
{
|
||||
var since = new DateTime(2026, 9, 1, 8, 0, 0, DateTimeKind.Utc);
|
||||
var slots = new DelegatedSlotUsageResponse { HomeTenantId = Home, Limit = 2, CatalogLimit = 2, Used = 1, ManagedTenantIds = new[] { TenantA }, PendingInvitations = 0, Holds = Array.Empty<DelegatedSlotHold>() };
|
||||
string? domainB = null;
|
||||
ManagedTenantQuotaUsage? usageB = null;
|
||||
DateTime? sinceB = null;
|
||||
|
||||
AssertParity(
|
||||
@@ -121,9 +120,8 @@ public class DelegationWireParityTests
|
||||
slots,
|
||||
tenants = new[]
|
||||
{
|
||||
new { tenantId = TenantA, domain = (string?)"customer.example", source = "self-service", sinceUtc = (DateTime?)since, removable = true,
|
||||
usage = (ManagedTenantQuotaUsage?)new ManagedTenantQuotaUsage { TenantPlan = "community", TenantDailyLimit = 300, TenantMonthlyLimit = 9000, TenantDailyUsed = 12, TenantMonthlyUsed = 340 } },
|
||||
new { tenantId = TenantB, domain = domainB, source = "operator", sinceUtc = sinceB, removable = false, usage = usageB },
|
||||
new { tenantId = TenantA, domain = (string?)"customer.example", source = "self-service", sinceUtc = (DateTime?)since, removable = true },
|
||||
new { tenantId = TenantB, domain = domainB, source = "operator", sinceUtc = sinceB, removable = false },
|
||||
},
|
||||
},
|
||||
new ManagedTenantListResponse
|
||||
@@ -132,8 +130,7 @@ public class DelegationWireParityTests
|
||||
Slots = slots,
|
||||
Tenants = new List<ManagedTenantItem>
|
||||
{
|
||||
new() { TenantId = TenantA, Domain = "customer.example", Source = "self-service", SinceUtc = since, Removable = true,
|
||||
Usage = new ManagedTenantQuotaUsage { TenantPlan = "community", TenantDailyLimit = 300, TenantMonthlyLimit = 9000, TenantDailyUsed = 12, TenantMonthlyUsed = 340 } },
|
||||
new() { TenantId = TenantA, Domain = "customer.example", Source = "self-service", SinceUtc = since, Removable = true },
|
||||
new() { TenantId = TenantB, Source = "operator", Removable = false },
|
||||
},
|
||||
});
|
||||
|
||||
@@ -158,6 +158,11 @@ public class FeatureEntitlementCatalogTests
|
||||
Assert.Equal(pro.McpMonthlyRequestLimit, conferred.McpMonthlyRequestLimit);
|
||||
Assert.Equal(pro.McpTenantDailyRequestLimit, conferred.McpTenantDailyRequestLimit);
|
||||
Assert.Equal(pro.McpTenantMonthlyRequestLimit, conferred.McpTenantMonthlyRequestLimit);
|
||||
// …except the slot growth, which follows the delegation right.
|
||||
Assert.Equal(0, conferred.McpSlotDailyRequestLimit);
|
||||
Assert.Equal(0, conferred.McpSlotMonthlyRequestLimit);
|
||||
Assert.Equal(0, conferred.McpSlotTenantDailyRequestLimit);
|
||||
Assert.Equal(0, conferred.McpSlotTenantMonthlyRequestLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -207,6 +212,10 @@ public class FeatureEntitlementCatalogTests
|
||||
Assert.Equal(3000, e.McpMonthlyRequestLimit);
|
||||
Assert.Equal(300, e.McpTenantDailyRequestLimit);
|
||||
Assert.Equal(9000, e.McpTenantMonthlyRequestLimit);
|
||||
Assert.Equal(0, e.McpSlotDailyRequestLimit);
|
||||
Assert.Equal(0, e.McpSlotMonthlyRequestLimit);
|
||||
Assert.Equal(0, e.McpSlotTenantDailyRequestLimit);
|
||||
Assert.Equal(0, e.McpSlotTenantMonthlyRequestLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -224,6 +233,11 @@ public class FeatureEntitlementCatalogTests
|
||||
Assert.Equal(20000, e.McpMonthlyRequestLimit);
|
||||
Assert.Equal(3000, e.McpTenantDailyRequestLimit);
|
||||
Assert.Equal(60000, e.McpTenantMonthlyRequestLimit);
|
||||
// Per purchased delegation slot: 30 % of the Pro windows (an additional slot costs 30 % of Pro).
|
||||
Assert.Equal(300, e.McpSlotDailyRequestLimit);
|
||||
Assert.Equal(6000, e.McpSlotMonthlyRequestLimit);
|
||||
Assert.Equal(900, e.McpSlotTenantDailyRequestLimit);
|
||||
Assert.Equal(18000, e.McpSlotTenantMonthlyRequestLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
using AutopilotMonitor.Functions.Services;
|
||||
using AutopilotMonitor.Shared.DataAccess;
|
||||
using AutopilotMonitor.Shared.Models;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Functions.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The IME pattern self-seed behind the newest-source-wins gate (<see cref="RuleCatalogSeedGate"/>):
|
||||
/// a GitHub reseed newer than the build owns the table (nothing written, nothing deleted), an
|
||||
/// older one hands it back to the binary (missing ids written, retired ids sunset), an empty
|
||||
/// table is always seeded, and the code reseed stamps <c>embedded</c>. The retired id stands in
|
||||
/// for PS-SCRIPT-EXITCODE, which the deployed binary re-inserted on 2026-09-11 after the reseed
|
||||
/// had deleted it.
|
||||
/// </summary>
|
||||
public class ImeLogPatternServiceSeedTests
|
||||
{
|
||||
private const string RetiredId = "IME-RETIRED-TEST";
|
||||
private static readonly DateTime Build = new(2026, 9, 10, 19, 19, 57, DateTimeKind.Utc);
|
||||
private static readonly BackendBuildInfo BuildInfo = new("1.5.1205", "e3b2113", Build);
|
||||
|
||||
private static RuleCatalogStamp GitHubStamp(DateTime at) => new()
|
||||
{
|
||||
Kind = RuleCatalogStamp.KindIme, Source = RuleCatalogStamp.SourceGitHub, StampedAt = at, Count = 82,
|
||||
};
|
||||
|
||||
/// <summary>Global partition = the embedded catalog minus its first id plus one retired built-in row.</summary>
|
||||
private static (List<ImeLogPattern> table, string missingId) DriftedTable()
|
||||
{
|
||||
var embedded = BuiltInImeLogPatterns.GetAll();
|
||||
var missing = embedded[0].PatternId;
|
||||
var table = embedded.Skip(1).Select(p => p.Clone()).ToList();
|
||||
table.Add(new ImeLogPattern
|
||||
{
|
||||
PatternId = RetiredId, Category = "always", Pattern = "^retired", Action = "scriptExitCode",
|
||||
Parameters = new Dictionary<string, string>(), Enabled = true, Description = "retired", IsBuiltIn = true,
|
||||
});
|
||||
return (table, missing);
|
||||
}
|
||||
|
||||
private sealed class Rig
|
||||
{
|
||||
public readonly Mock<IRuleRepository> Repo = new();
|
||||
public readonly List<string> Stored = new();
|
||||
public readonly List<string> Deleted = new();
|
||||
|
||||
public Rig(List<ImeLogPattern> global, RuleCatalogStamp? stamp)
|
||||
{
|
||||
Repo.Setup(r => r.GetImeLogPatternsAsync("global")).ReturnsAsync(() => global.Select(p => p.Clone()).ToList());
|
||||
Repo.Setup(r => r.GetImeLogPatternsAsync(It.Is<string>(k => k != "global"))).ReturnsAsync(new List<ImeLogPattern>());
|
||||
Repo.Setup(r => r.GetRuleCatalogStampAsync(RuleCatalogStamp.KindIme)).ReturnsAsync(stamp);
|
||||
Repo.Setup(r => r.StoreImeLogPatternAsync(It.IsAny<ImeLogPattern>(), "global"))
|
||||
.Callback<ImeLogPattern, string>((p, _) => Stored.Add(p.PatternId)).ReturnsAsync(true);
|
||||
Repo.Setup(r => r.DeleteImeLogPatternAsync("global", It.IsAny<string>()))
|
||||
.Callback<string, string>((_, id) => Deleted.Add(id)).ReturnsAsync(true);
|
||||
Repo.Setup(r => r.SetRuleCatalogStampAsync(It.IsAny<RuleCatalogStamp>())).ReturnsAsync(true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GitHub_reseed_newer_than_the_build_owns_the_table()
|
||||
{
|
||||
var (table, missing) = DriftedTable();
|
||||
var rig = new Rig(table, GitHubStamp(Build.AddHours(6)));
|
||||
var service = new ImeLogPatternService(rig.Repo.Object, NullLogger<ImeLogPatternService>.Instance, BuildInfo);
|
||||
|
||||
var catalog = await service.GetGlobalCatalogAsync();
|
||||
|
||||
Assert.Empty(rig.Stored);
|
||||
Assert.Empty(rig.Deleted);
|
||||
Assert.Contains(catalog, p => p.PatternId == RetiredId); // the reseed's table, untouched
|
||||
Assert.DoesNotContain(catalog, p => p.PatternId == missing);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GitHub_reseed_older_than_the_build_hands_the_table_to_the_binary()
|
||||
{
|
||||
var (table, missing) = DriftedTable();
|
||||
var rig = new Rig(table, GitHubStamp(Build.AddHours(-1)));
|
||||
var service = new ImeLogPatternService(rig.Repo.Object, NullLogger<ImeLogPatternService>.Instance, BuildInfo);
|
||||
|
||||
await service.GetGlobalCatalogAsync();
|
||||
|
||||
Assert.Contains(missing, rig.Stored); // added from the embedded catalog
|
||||
Assert.Equal(new[] { RetiredId }, rig.Deleted); // sunset: no longer shipped
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Without_build_info_the_gate_is_open_and_the_stamp_is_never_read()
|
||||
{
|
||||
var (table, missing) = DriftedTable();
|
||||
var rig = new Rig(table, GitHubStamp(Build.AddHours(6)));
|
||||
var service = new ImeLogPatternService(rig.Repo.Object, NullLogger<ImeLogPatternService>.Instance);
|
||||
|
||||
await service.GetGlobalCatalogAsync();
|
||||
|
||||
Assert.Contains(missing, rig.Stored);
|
||||
rig.Repo.Verify(r => r.GetRuleCatalogStampAsync(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Empty_table_is_seeded_even_behind_a_newer_reseed()
|
||||
{
|
||||
var rig = new Rig(new List<ImeLogPattern>(), GitHubStamp(Build.AddHours(6)));
|
||||
var service = new ImeLogPatternService(rig.Repo.Object, NullLogger<ImeLogPatternService>.Instance, BuildInfo);
|
||||
|
||||
await service.GetGlobalCatalogAsync();
|
||||
|
||||
Assert.Equal(BuiltInImeLogPatterns.GetAll().Count, rig.Stored.Count);
|
||||
Assert.Empty(rig.Deleted);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Seed_runs_once_per_instance_also_when_the_gate_is_closed()
|
||||
{
|
||||
var (table, _) = DriftedTable();
|
||||
var rig = new Rig(table, GitHubStamp(Build.AddHours(6)));
|
||||
var service = new ImeLogPatternService(rig.Repo.Object, NullLogger<ImeLogPatternService>.Instance, BuildInfo);
|
||||
|
||||
await service.GetGlobalCatalogAsync();
|
||||
await service.GetGlobalCatalogAsync();
|
||||
|
||||
rig.Repo.Verify(r => r.GetRuleCatalogStampAsync(RuleCatalogStamp.KindIme), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Code_reseed_stamps_the_embedded_source()
|
||||
{
|
||||
var (table, _) = DriftedTable();
|
||||
var rig = new Rig(table, GitHubStamp(Build.AddHours(6)));
|
||||
var service = new ImeLogPatternService(rig.Repo.Object, NullLogger<ImeLogPatternService>.Instance, BuildInfo);
|
||||
|
||||
var (deleted, written) = await service.ReseedBuiltInPatternsAsync();
|
||||
|
||||
Assert.Equal(table.Count, deleted);
|
||||
Assert.Equal(BuiltInImeLogPatterns.GetAll().Count, written);
|
||||
rig.Repo.Verify(r => r.SetRuleCatalogStampAsync(It.Is<RuleCatalogStamp>(s =>
|
||||
s.Kind == RuleCatalogStamp.KindIme && s.Source == RuleCatalogStamp.SourceEmbedded && s.Count == written)), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -93,8 +93,9 @@ public class ImePatternHealthTests
|
||||
["PS-AGENT-OUTPUT"] = "not-a-number",
|
||||
};
|
||||
var data = new Dictionary<string, object> { ["hits"] = hits, ["imeVersion"] = "1.105.103.0" };
|
||||
var shipped = new HashSet<string>(new[] { "IME-ESP-PHASE", "IME-STARTED", "IME-DO-TEL", "PS-AGENT-OUTPUT" }, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var result = ImePatternHealthService.ExtractBuiltInHits(data);
|
||||
var result = ImePatternHealthService.ExtractBuiltInHits(data, shipped);
|
||||
|
||||
Assert.Equal(12, result["IME-ESP-PHASE"]);
|
||||
Assert.Equal(1, result["IME-STARTED"]);
|
||||
@@ -106,8 +107,9 @@ public class ImePatternHealthTests
|
||||
[Fact]
|
||||
public void ExtractBuiltInHits_without_a_histogram_is_empty()
|
||||
{
|
||||
Assert.Empty(ImePatternHealthService.ExtractBuiltInHits(null));
|
||||
Assert.Empty(ImePatternHealthService.ExtractBuiltInHits(new Dictionary<string, object> { ["linesRead"] = 5 }));
|
||||
var shipped = new HashSet<string>(new[] { "IME-ESP-PHASE" }, StringComparer.OrdinalIgnoreCase);
|
||||
Assert.Empty(ImePatternHealthService.ExtractBuiltInHits(null, shipped));
|
||||
Assert.Empty(ImePatternHealthService.ExtractBuiltInHits(new Dictionary<string, object> { ["linesRead"] = 5 }, shipped));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -128,9 +130,13 @@ public class ImePatternHealthTests
|
||||
new() { PatternId = "IME-NEW-THING", Category = "currentPhase", Enabled = true }, // no data yet
|
||||
};
|
||||
|
||||
var r = ImePatternHealthService.BuildResponse(stats, history, catalog, flaggedAt);
|
||||
var stamp = new RuleCatalogStamp { Kind = RuleCatalogStamp.KindIme, Source = RuleCatalogStamp.SourceGitHub, StampedAt = flaggedAt.AddDays(-1), Count = 3 };
|
||||
var r = ImePatternHealthService.BuildResponse(stats, history, catalog, stamp, flaggedAt);
|
||||
|
||||
Assert.Equal("1.104.102.0", r.BaselineVersion);
|
||||
Assert.Equal("github", r.Catalog.Source);
|
||||
Assert.Equal(flaggedAt.AddDays(-1), r.Catalog.StampedAt);
|
||||
Assert.Equal(3, r.Catalog.PatternCount);
|
||||
Assert.Equal("1.105.103.0", r.Versions[0].Version); // newest first
|
||||
Assert.Equal(30, r.Versions[0].Sessions);
|
||||
Assert.Equal(31, r.Versions[0].FleetSessions);
|
||||
@@ -150,11 +156,13 @@ public class ImePatternHealthTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuiltInPatternIds_matches_the_embedded_catalog()
|
||||
public void BuildResponse_without_a_stamp_reports_the_embedded_source()
|
||||
{
|
||||
var ids = BuiltInImeLogPatterns.BuiltInPatternIds.Value;
|
||||
Assert.Contains("IME-ESP-PHASE", ids);
|
||||
Assert.Contains("PS-SCRIPT-RESULT", ids);
|
||||
Assert.Equal(BuiltInImeLogPatterns.GetAll().Count, ids.Count);
|
||||
var r = ImePatternHealthService.BuildResponse(new List<ImePatternStatsEntry>(), new List<ImeVersionHistoryEntry>(),
|
||||
new List<ImeLogPattern> { new() { PatternId = "IME-ESP-PHASE", Category = "always", Enabled = true } }, null, DateTime.UtcNow);
|
||||
|
||||
Assert.Equal("embedded", r.Catalog.Source);
|
||||
Assert.Null(r.Catalog.StampedAt);
|
||||
Assert.Equal(1, r.Catalog.PatternCount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -656,7 +656,8 @@ public class InfrastructureWireParityTests
|
||||
// DELIBERATE wire change (error-envelope pass): the body now leads with the envelope prefix
|
||||
// error/code/correlationId (error carries the former `message`); quotaExceeded stays as the
|
||||
// MCP's discriminator. These literals pin the NEW shape (correlationId is stamped by the writer,
|
||||
// so the builder leaves it empty).
|
||||
// so the builder leaves it empty). Both windows belong to the caller's own tenant since 2026-09-11
|
||||
// (the budget follows the delegating tenant) — there is no managed-tenant shape any more.
|
||||
|
||||
[Fact]
|
||||
public void McpQuotaExceededResponse_matches_the_daily_blocked_shape()
|
||||
@@ -745,62 +746,6 @@ public class InfrastructureWireParityTests
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void McpQuotaExceededResponse_matches_the_managed_tenant_blocked_shape()
|
||||
{
|
||||
// A delegated (MSP) read charged to a MANAGED Community tenant whose organization window is spent:
|
||||
// level=tenant, the managed tenant is named (label + targetTenantId) and the upgrade path is spelled out.
|
||||
var decision = McpQuotaService.BuildDecision(
|
||||
new McpPlanLimits("pro", 1000, 20000, "community", 300, 9000),
|
||||
dailyUsed: 12, monthlyUsed: 340, tenantDailyUsed: 300, tenantMonthlyUsed: 2100,
|
||||
new DateTime(2026, 8, 30, 15, 0, 0, DateTimeKind.Utc),
|
||||
targetTenantId: "7aa20c11-0002-4b7c-a1d2-52f3aaaa0002");
|
||||
|
||||
AssertParity(
|
||||
new
|
||||
{
|
||||
error = "MCP daily request quota of the managed tenant 'customer.example' exceeded (tenant plan 'community', shared by all its members and delegated admins). That tenant is on the Community plan, which is sized for occasional use; its own plan governs this window, not yours. Upgrading that tenant to Pro lifts its organization windows. Resets at 2026-08-31T00:00:00Z.",
|
||||
code = "QuotaExceeded",
|
||||
correlationId = "",
|
||||
quotaExceeded = true,
|
||||
plan = "pro",
|
||||
scope = "daily",
|
||||
level = "tenant",
|
||||
limit = 300,
|
||||
used = 300L,
|
||||
resetUtc = "2026-08-31T00:00:00Z",
|
||||
targetTenantId = "7aa20c11-0002-4b7c-a1d2-52f3aaaa0002"
|
||||
},
|
||||
McpQuotaEnforcementMiddleware.BuildExceededResponse(decision, "customer.example"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void McpQuotaExceededResponse_matches_the_all_managed_tenants_exhausted_shape()
|
||||
{
|
||||
// The fleet aggregate where EVERY managed tenant is spent: no single target, the count is named.
|
||||
var decision = McpQuotaService.BuildDecision(
|
||||
new McpPlanLimits("pro", 1000, 20000, "community", 300, 9000),
|
||||
dailyUsed: 12, monthlyUsed: 340, tenantDailyUsed: 300, tenantMonthlyUsed: 2100,
|
||||
new DateTime(2026, 8, 30, 15, 0, 0, DateTimeKind.Utc),
|
||||
targetTenantId: "7aa20c11-0002-4b7c-a1d2-52f3aaaa0002");
|
||||
|
||||
AssertParity(
|
||||
new
|
||||
{
|
||||
error = "MCP request quota exceeded for all 2 managed tenants in scope (each managed tenant's own plan governs its organization windows; upgrading a managed tenant to Pro lifts them). Earliest reset at 2026-08-31T00:00:00Z.",
|
||||
code = "QuotaExceeded",
|
||||
correlationId = "",
|
||||
quotaExceeded = true,
|
||||
plan = "pro",
|
||||
scope = "daily",
|
||||
level = "tenant",
|
||||
limit = 300,
|
||||
used = 300L,
|
||||
resetUtc = "2026-08-31T00:00:00Z"
|
||||
},
|
||||
McpQuotaEnforcementMiddleware.BuildExceededResponse(decision, exhaustedTenantCount: 2));
|
||||
}
|
||||
|
||||
private static void AssertParity(object anonymousLiteral, IApiResponse typed)
|
||||
=> ApiResponseWireParityTests.AssertWireIdentical(anonymousLiteral, typed);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ using Microsoft.Extensions.Logging.Abstractions;
|
||||
namespace AutopilotMonitor.Functions.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for <see cref="LatestVersionsService"/> — the 12h-cached reader for the
|
||||
/// Tests for <see cref="LatestVersionsService"/> — the short-TTL reader for the
|
||||
/// public <c>version.json</c> blob. Verifies cache hit/miss, forceRefresh bypass,
|
||||
/// and failure fallback without hammering the blob on every call.
|
||||
/// </summary>
|
||||
@@ -140,6 +140,28 @@ public class LatestVersionsServiceTests
|
||||
Assert.Equal(1, handler.CallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAsync_HttpFailure_AfterSuccess_ServesLastKnownGood()
|
||||
{
|
||||
var (svc, handler, _) = Create();
|
||||
|
||||
await svc.GetAsync(forceRefresh: false, CancellationToken.None);
|
||||
handler.Responder = _ => new HttpResponseMessage(HttpStatusCode.InternalServerError);
|
||||
|
||||
// The refresh fails, but the version line must not blank out: the last good
|
||||
// value stands until the blob answers again.
|
||||
var afterFailure = await svc.GetAsync(forceRefresh: true, CancellationToken.None);
|
||||
|
||||
Assert.NotNull(afterFailure);
|
||||
Assert.Equal("1.0.706", afterFailure!.AgentVersion);
|
||||
Assert.True(afterFailure.FromCache);
|
||||
|
||||
// ...and it is what the short failure cache serves to the next callers, too.
|
||||
var next = await svc.GetAsync(forceRefresh: false, CancellationToken.None);
|
||||
Assert.Equal("1.0.706", next!.AgentVersion);
|
||||
Assert.Equal(2, handler.CallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAsync_MalformedJson_ReturnsNull()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutopilotMonitor.Functions.Services;
|
||||
using AutopilotMonitor.Functions.Services.Backup;
|
||||
using AutopilotMonitor.Functions.Services.Maintenance;
|
||||
using AutopilotMonitor.Shared.DataAccess;
|
||||
using AutopilotMonitor.Shared.Models;
|
||||
using Azure.Storage.Blobs.Specialized;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Functions.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Single-flight of the platform maintenance run: the 2h timer and the manual trigger enter
|
||||
/// through one gate, so a second run is skipped instead of aggregating, cleaning up and
|
||||
/// backfilling the same tables in parallel — and a skip never looks like an active run.
|
||||
/// </summary>
|
||||
public class MaintenanceRunGateTests
|
||||
{
|
||||
private sealed class Harness
|
||||
{
|
||||
public Mock<MaintenanceRunLockStore> LockStore { get; }
|
||||
public Mock<BlobLeaseClient> Lease { get; } = new();
|
||||
public List<string> OpsEvents { get; } = new();
|
||||
public MaintenanceRunGate Sut { get; }
|
||||
|
||||
public Harness()
|
||||
{
|
||||
LockStore = new Mock<MaintenanceRunLockStore>(new SessionDeletionMaintenanceFunctionTests.MaintenanceBlobStub());
|
||||
LockStore.Setup(l => l.AcquireLeaseAsync(It.IsAny<TimeSpan?>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(Lease.Object);
|
||||
|
||||
var opsRepo = new Mock<IOpsEventRepository>();
|
||||
opsRepo.Setup(r => r.SaveOpsEventAsync(It.IsAny<OpsEventEntry>()))
|
||||
.Returns<OpsEventEntry>(e => { lock (OpsEvents) OpsEvents.Add(e.EventType); return Task.CompletedTask; });
|
||||
var adminConfig = new Mock<AdminConfigurationService>(
|
||||
Mock.Of<IConfigRepository>(), NullLogger<AdminConfigurationService>.Instance, new MemoryCache(new MemoryCacheOptions()));
|
||||
var opsService = new OpsEventService(opsRepo.Object, NullLogger<OpsEventService>.Instance,
|
||||
TestNotifications.InertOpsAlertDispatch(adminConfig.Object));
|
||||
|
||||
Sut = new MaintenanceRunGate(LockStore.Object, opsService, NullLogger<MaintenanceRunGate>.Instance);
|
||||
}
|
||||
|
||||
public void HoldLease() =>
|
||||
LockStore.Setup(l => l.AcquireLeaseAsync(It.IsAny<TimeSpan?>(), It.IsAny<CancellationToken>()))
|
||||
.ThrowsAsync(new LeaseHeldException("held", new Exception("409")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_held_lease_skips_the_run_and_never_reports_it_as_started()
|
||||
{
|
||||
var h = new Harness();
|
||||
h.HoldLease();
|
||||
var ran = false;
|
||||
|
||||
var result = await h.Sut.RunExclusiveAsync("Timer", () => { ran = true; return Task.CompletedTask; });
|
||||
|
||||
Assert.False(result);
|
||||
Assert.False(ran);
|
||||
Assert.Equal(new[] { OpsEventTypes.MaintenanceSkippedLocked }, h.OpsEvents);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Started_is_written_under_the_lease_before_the_body_and_the_lease_is_released_last()
|
||||
{
|
||||
var h = new Harness();
|
||||
var order = new List<string>();
|
||||
h.Lease.Setup(l => l.ReleaseAsync(It.IsAny<Azure.RequestConditions>(), It.IsAny<CancellationToken>()))
|
||||
.Callback(() => order.Add("release"))
|
||||
.ReturnsAsync(Mock.Of<Azure.Response<Azure.Storage.Blobs.Models.ReleasedObjectInfo>>());
|
||||
|
||||
var result = await h.Sut.RunExclusiveAsync("admin@example.com", () =>
|
||||
{
|
||||
order.Add($"body after [{string.Join(",", h.OpsEvents)}]");
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
Assert.True(result);
|
||||
Assert.Equal(new[] { $"body after [{OpsEventTypes.MaintenanceStarted}]", "release" }, order);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_lease_is_released_when_the_body_throws()
|
||||
{
|
||||
var h = new Harness();
|
||||
var released = false;
|
||||
h.Lease.Setup(l => l.ReleaseAsync(It.IsAny<Azure.RequestConditions>(), It.IsAny<CancellationToken>()))
|
||||
.Callback(() => released = true)
|
||||
.ReturnsAsync(Mock.Of<Azure.Response<Azure.Storage.Blobs.Models.ReleasedObjectInfo>>());
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() =>
|
||||
h.Sut.RunExclusiveAsync("Timer", () => throw new InvalidOperationException("boom")));
|
||||
|
||||
Assert.True(released);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Probe_reports_an_active_run_while_the_lease_is_held()
|
||||
{
|
||||
var h = new Harness();
|
||||
h.HoldLease();
|
||||
|
||||
Assert.True(await h.Sut.IsRunActiveAsync());
|
||||
Assert.Empty(h.OpsEvents);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Probe_releases_its_lease_and_reports_no_active_run()
|
||||
{
|
||||
var h = new Harness();
|
||||
var released = false;
|
||||
h.Lease.Setup(l => l.ReleaseAsync(It.IsAny<Azure.RequestConditions>(), It.IsAny<CancellationToken>()))
|
||||
.Callback(() => released = true)
|
||||
.ReturnsAsync(Mock.Of<Azure.Response<Azure.Storage.Blobs.Models.ReleasedObjectInfo>>());
|
||||
|
||||
Assert.False(await h.Sut.IsRunActiveAsync());
|
||||
Assert.True(released);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Probe_lets_a_failed_release_surface_so_nothing_is_queued_behind_a_stuck_lease()
|
||||
{
|
||||
// A queued run picked up seconds later would find the probe's own lease still held,
|
||||
// skip as "locked" and delete the message — the operator's run would silently vanish.
|
||||
var h = new Harness();
|
||||
h.Lease.Setup(l => l.ReleaseAsync(It.IsAny<Azure.RequestConditions>(), It.IsAny<CancellationToken>()))
|
||||
.ThrowsAsync(new Azure.RequestFailedException(500, "storage down"));
|
||||
|
||||
await Assert.ThrowsAsync<Azure.RequestFailedException>(() => h.Sut.IsRunActiveAsync());
|
||||
}
|
||||
}
|
||||
+95
-27
@@ -1,51 +1,49 @@
|
||||
using AutopilotMonitor.Functions.Functions.Metrics;
|
||||
using AutopilotMonitor.Functions.Services;
|
||||
using AutopilotMonitor.Shared.DataAccess;
|
||||
using Xunit;
|
||||
|
||||
namespace AutopilotMonitor.Functions.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for the pure fold behind GET metrics/mcp-usage/organization: one item per account, the
|
||||
/// delegated (MSP) marker from the row's home tenant, and the three windows (today / month / range).
|
||||
/// Tests for the pure folds behind GET metrics/mcp-usage/organization (and its global twin): one item per
|
||||
/// account, the three windows (today / month / range), and the tenant's organization windows over every
|
||||
/// account including the purchased-slot breakdown.
|
||||
/// </summary>
|
||||
public class McpOrganizationUsageAggregationTests
|
||||
{
|
||||
private const string Tenant = "11111111-1111-1111-1111-111111111111";
|
||||
private const string MspHome = "99999999-9999-9999-9999-999999999999";
|
||||
private const string Member = "aaaaaaaa-0000-0000-0000-000000000001";
|
||||
private const string MspAdmin = "bbbbbbbb-0000-0000-0000-000000000002";
|
||||
private const string OtherMember = "bbbbbbbb-0000-0000-0000-000000000002";
|
||||
|
||||
private static TenantUsageRecord Row(string user, string date, long count, string upn = "", string home = "", DateTime? last = null)
|
||||
=> new() { TenantId = Tenant, UserId = user, Date = date, RequestCount = count, UserPrincipalName = upn, HomeTenantId = home, LastRequestAt = last };
|
||||
private static TenantUsageRecord Row(string user, string date, long count, string upn = "", DateTime? last = null)
|
||||
=> new() { TenantId = Tenant, UserId = user, Date = date, RequestCount = count, UserPrincipalName = upn, LastRequestAt = last };
|
||||
|
||||
[Fact]
|
||||
public void Aggregate_MarksDelegatedAccounts_ByForeignHomeTenant()
|
||||
public void Aggregate_OneItemPerAccount_KeepsTheLatestUpn()
|
||||
{
|
||||
var rows = new[]
|
||||
{
|
||||
Row(Member, "20260902", 10, "alice@contoso.com"),
|
||||
Row(MspAdmin, "20260902", 40, "msp@partner.example", MspHome),
|
||||
Row(Member, "20260901", 10), // written before the UPN column existed
|
||||
Row(Member, "20260902", 5, "alice@contoso.com"),
|
||||
Row(OtherMember, "20260902", 40, "bob@contoso.com"),
|
||||
};
|
||||
|
||||
var items = McpUsageMetricsFunction.AggregateOrganizationUsage(rows, Tenant, "20260901", "20260902", "20260902", "20260901");
|
||||
var items = McpUsageMetricsFunction.AggregateOrganizationUsage(rows, "20260901", "20260902", "20260902", "20260901");
|
||||
|
||||
var msp = Assert.Single(items, i => i.UserId == MspAdmin);
|
||||
Assert.True(msp.Delegated);
|
||||
Assert.Equal(MspHome, msp.HomeTenantId);
|
||||
Assert.Equal("msp@partner.example", msp.UserPrincipalName);
|
||||
var member = Assert.Single(items, i => i.UserId == Member);
|
||||
Assert.False(member.Delegated);
|
||||
Assert.Null(member.HomeTenantId);
|
||||
Assert.Equal(2, items.Count);
|
||||
var alice = Assert.Single(items, i => i.UserId == Member);
|
||||
Assert.Equal("alice@contoso.com", alice.UserPrincipalName);
|
||||
Assert.Equal(15, alice.RequestsThisMonth);
|
||||
var bob = Assert.Single(items, i => i.UserId == OtherMember);
|
||||
Assert.Equal("bob@contoso.com", bob.UserPrincipalName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Aggregate_OwnTenantAsHome_IsNotDelegated()
|
||||
public void Aggregate_RowsWithoutUpn_LeaveItNull()
|
||||
{
|
||||
// Legacy/own rows may carry the tenant itself (or nothing) as home — both mean "own member".
|
||||
var rows = new[] { Row(Member, "20260902", 1, home: Tenant), Row(Member, "20260901", 1) };
|
||||
var items = McpUsageMetricsFunction.AggregateOrganizationUsage(rows, Tenant, "20260901", "20260902", "20260902", "20260901");
|
||||
var item = Assert.Single(items);
|
||||
Assert.False(item.Delegated);
|
||||
var rows = new[] { Row(Member, "20260902", 1), Row(Member, "20260901", 1) };
|
||||
var item = Assert.Single(McpUsageMetricsFunction.AggregateOrganizationUsage(rows, "20260901", "20260902", "20260902", "20260901"));
|
||||
Assert.Null(item.UserPrincipalName);
|
||||
}
|
||||
|
||||
@@ -60,7 +58,7 @@ public class McpOrganizationUsageAggregationTests
|
||||
Row(Member, "20260903", 7), // read window past the range end (never counted in range)
|
||||
};
|
||||
|
||||
var items = McpUsageMetricsFunction.AggregateOrganizationUsage(rows, Tenant, "20260815", "20260902", "20260902", "20260901");
|
||||
var items = McpUsageMetricsFunction.AggregateOrganizationUsage(rows, "20260815", "20260902", "20260902", "20260901");
|
||||
|
||||
var item = Assert.Single(items);
|
||||
Assert.Equal(5, item.RequestsToday);
|
||||
@@ -77,15 +75,85 @@ public class McpOrganizationUsageAggregationTests
|
||||
{
|
||||
Row(Member, "20260901", 3, last: newer),
|
||||
Row(Member, "20260902", 1, last: older),
|
||||
Row(MspAdmin, "20260902", 50, home: MspHome, last: older),
|
||||
Row(OtherMember, "20260902", 50, last: older),
|
||||
};
|
||||
|
||||
var items = McpUsageMetricsFunction.AggregateOrganizationUsage(rows, Tenant, "20260901", "20260902", "20260902", "20260901");
|
||||
var items = McpUsageMetricsFunction.AggregateOrganizationUsage(rows, "20260901", "20260902", "20260902", "20260901");
|
||||
|
||||
Assert.Equal(new[] { MspAdmin, Member }, items.Select(i => i.UserId));
|
||||
Assert.Equal(new[] { OtherMember, Member }, items.Select(i => i.UserId));
|
||||
Assert.Equal(newer, items[1].LastRequestAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OrganizationQuota_SumsTodayAndMonthOverEveryAccount_AndCarriesTheTenantLimits()
|
||||
{
|
||||
var rows = new[]
|
||||
{
|
||||
Row(Member, "20260815", 100), // last month: neither window
|
||||
Row(Member, "20260901", 20), // month
|
||||
Row(Member, "20260902", 2), // today + month
|
||||
Row(OtherMember, "20260902", 5), // another account counts against the same windows
|
||||
Row(Member, "20260903", 7), // read window past today (range only)
|
||||
};
|
||||
|
||||
var quota = McpUsageMetricsFunction.BuildOrganizationQuota(
|
||||
rows, new McpQuotaService.TenantPlanLimits("pro", 1500, 15000), "20260902", "20260901");
|
||||
|
||||
Assert.Equal("pro", quota.TenantPlan);
|
||||
Assert.Equal(1500, quota.DailyLimit);
|
||||
Assert.Equal(15000, quota.MonthlyLimit);
|
||||
Assert.Equal(7, quota.DailyUsed);
|
||||
Assert.Equal(27, quota.MonthlyUsed);
|
||||
// No purchased slot: the breakdown stays off the wire.
|
||||
Assert.Null(quota.PurchasedDelegatedSlots);
|
||||
Assert.Null(quota.SlotDailyLimit);
|
||||
Assert.Null(quota.SlotMonthlyLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OrganizationQuota_PurchasedSlots_CarryTheBreakdown()
|
||||
{
|
||||
// 3 000 + 3 × 900 = 5 700 — the limits arrive grown; the slot fields let the page show the sum.
|
||||
var quota = McpUsageMetricsFunction.BuildOrganizationQuota(
|
||||
new[] { Row(Member, "20260902", 3) },
|
||||
new McpQuotaService.TenantPlanLimits("pro", 5700, 114000, PurchasedDelegatedSlots: 3, SlotTenantDailyLimit: 900, SlotTenantMonthlyLimit: 18000),
|
||||
"20260902", "20260901");
|
||||
|
||||
Assert.Equal(5700, quota.DailyLimit);
|
||||
Assert.Equal(3, quota.PurchasedDelegatedSlots);
|
||||
Assert.Equal(900, quota.SlotDailyLimit);
|
||||
Assert.Equal(18000, quota.SlotMonthlyLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OrganizationDaily_SumsEveryAccountPerDay_InsideTheRange_OldestFirst()
|
||||
{
|
||||
var rows = new[]
|
||||
{
|
||||
Row(Member, "20260815", 100), // before the range
|
||||
Row(Member, "20260902", 2),
|
||||
Row(OtherMember, "20260902", 5), // same day, another account — one bar
|
||||
Row(Member, "20260901", 20),
|
||||
Row(Member, "20260903", 7), // after the range (read window)
|
||||
};
|
||||
|
||||
var daily = McpUsageMetricsFunction.BuildOrganizationDaily(rows, "20260901", "20260902");
|
||||
|
||||
Assert.Equal(new[] { ("20260901", 20L), ("20260902", 7L) }, daily.Select(d => (d.Date, d.Requests)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OrganizationQuota_LiftedWindows_StillReportTheCounters()
|
||||
{
|
||||
var quota = McpUsageMetricsFunction.BuildOrganizationQuota(
|
||||
new[] { Row(Member, "20260902", 3) }, new McpQuotaService.TenantPlanLimits("pro", 0, 0), "20260902", "20260901");
|
||||
|
||||
Assert.Equal(0, quota.DailyLimit);
|
||||
Assert.Equal(0, quota.MonthlyLimit);
|
||||
Assert.Equal(3, quota.DailyUsed);
|
||||
Assert.Equal(3, quota.MonthlyUsed);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("2026-09-02", "20260902")]
|
||||
[InlineData("20260902", "20260902")]
|
||||
|
||||
+32
-218
@@ -6,252 +6,77 @@ using Xunit;
|
||||
namespace AutopilotMonitor.Functions.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for the pure seams of <see cref="McpQuotaEnforcementMiddleware"/>: which tenant a request is
|
||||
/// charged to ("the budget follows the data"), the bound-narrowing isolation invariant, and the 429 texts
|
||||
/// that name whose budget is exhausted.
|
||||
/// Tests for the pure seams of <see cref="McpQuotaEnforcementMiddleware"/>: who bypasses the check (the Global
|
||||
/// Admin only — counted, never blocked) and the 429 texts that name whose window is exhausted. Every request is
|
||||
/// charged to the caller's HOME tenant ("the budget follows the delegating tenant"), so no seam names a target
|
||||
/// tenant any more.
|
||||
/// </summary>
|
||||
public class McpQuotaEnforcementMiddlewareTests
|
||||
{
|
||||
private const string Home = "aaaaaaaa-0000-0000-0000-000000000001";
|
||||
private const string TenantB = "bbbbbbbb-0000-0000-0000-000000000002";
|
||||
private const string TenantC = "cccccccc-0000-0000-0000-000000000003";
|
||||
private const string TenantD = "dddddddd-0000-0000-0000-000000000004";
|
||||
private static readonly DateTime Now = new(2026, 9, 2, 15, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
// ── ResolveChargeScope ─────────────────────────────────────────────────────────
|
||||
// ── IsExempt — the Global Admin runs, the Global Reader is quota'd ─────────────
|
||||
|
||||
[Fact]
|
||||
public void Member_ReadingOwnTenant_ChargesHome()
|
||||
public void GlobalAdmin_IsExempt_EvenOnAForeignTarget()
|
||||
{
|
||||
var ctx = new RequestContext { TenantId = Home, TargetTenantId = Home };
|
||||
var scope = McpQuotaEnforcementMiddleware.ResolveChargeScope(ctx, Home);
|
||||
Assert.Equal(McpChargeKind.Home, scope.Kind);
|
||||
Assert.Null(scope.TargetTenantId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Delegated_SingleManagedTarget_ChargesThatTarget()
|
||||
{
|
||||
var ctx = new RequestContext
|
||||
{
|
||||
TenantId = Home, TargetTenantId = TenantB, IsDelegatedReader = true,
|
||||
AllowedTenantIds = new[] { TenantB, TenantC },
|
||||
};
|
||||
var scope = McpQuotaEnforcementMiddleware.ResolveChargeScope(ctx, Home);
|
||||
Assert.Equal(McpChargeKind.SingleTarget, scope.Kind);
|
||||
Assert.Equal(TenantB, scope.TargetTenantId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Delegated_ReadingHomeTenant_ChargesHome()
|
||||
{
|
||||
// The subset tier admits a delegated caller for ?tenantId=<home> too (crossTenant=false) — that read
|
||||
// is the caller's OWN tenant and draws on its own budget.
|
||||
var ctx = new RequestContext
|
||||
{
|
||||
TenantId = Home, TargetTenantId = Home, IsDelegatedReader = true,
|
||||
AllowedTenantIds = new[] { TenantB },
|
||||
};
|
||||
var scope = McpQuotaEnforcementMiddleware.ResolveChargeScope(ctx, Home);
|
||||
Assert.Equal(McpChargeKind.Home, scope.Kind);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Delegated_TargetReachedThroughHomeChargedGroup_ChargesHome()
|
||||
{
|
||||
var ctx = new RequestContext
|
||||
{
|
||||
TenantId = Home, TargetTenantId = TenantC, IsDelegatedReader = true,
|
||||
AllowedTenantIds = new[] { TenantB, TenantC },
|
||||
HomeChargedTenantIds = new[] { TenantC },
|
||||
};
|
||||
var scope = McpQuotaEnforcementMiddleware.ResolveChargeScope(ctx, Home);
|
||||
Assert.Equal(McpChargeKind.Home, scope.Kind);
|
||||
Assert.Null(scope.TargetTenantId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Delegated_FleetAggregate_ChargesEveryManagedTenant()
|
||||
{
|
||||
var ctx = new RequestContext
|
||||
{
|
||||
TenantId = Home, TargetTenantId = Home, IsDelegatedReader = true, IsDelegatedAggregate = true,
|
||||
AllowedTenantIds = new[] { TenantB, TenantC },
|
||||
};
|
||||
var scope = McpQuotaEnforcementMiddleware.ResolveChargeScope(ctx, Home);
|
||||
Assert.Equal(McpChargeKind.BoundedAggregate, scope.Kind);
|
||||
Assert.Equal(new[] { TenantB, TenantC }, scope.ChargeMap!.Keys.OrderBy(k => k));
|
||||
Assert.Equal(new[] { TenantB }, scope.TargetsOf(new[] { TenantB }));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Delegated_FleetAggregate_FoldsHomeChargedTenantsOntoHome()
|
||||
{
|
||||
var ctx = new RequestContext
|
||||
{
|
||||
TenantId = Home, TargetTenantId = Home, IsDelegatedReader = true, IsDelegatedAggregate = true,
|
||||
AllowedTenantIds = new[] { TenantB, TenantC, TenantD },
|
||||
HomeChargedTenantIds = new[] { TenantC, TenantD },
|
||||
};
|
||||
var scope = McpQuotaEnforcementMiddleware.ResolveChargeScope(ctx, Home);
|
||||
Assert.Equal(McpChargeKind.BoundedAggregate, scope.Kind);
|
||||
Assert.Equal(new[] { Home, TenantB }, scope.ChargeMap!.Keys.OrderBy(k => k));
|
||||
// Home is charged ONCE for both operator-managed tenants; they are served or dropped together.
|
||||
Assert.Equal(new[] { TenantC, TenantD }, scope.TargetsOf(new[] { Home }).OrderBy(t => t));
|
||||
Assert.Equal(new[] { TenantB }, scope.TargetsOf(new[] { TenantB }));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Delegated_SubsetTierDirectoryListing_IsNotAnAggregate_ChargesHome()
|
||||
{
|
||||
// config/all: subset tier but TenantScoping.None → the policy middleware never marks it an aggregate.
|
||||
var ctx = new RequestContext
|
||||
{
|
||||
TenantId = Home, TargetTenantId = Home, IsDelegatedReader = true, IsDelegatedAggregate = false,
|
||||
AllowedTenantIds = new[] { TenantB, TenantC },
|
||||
};
|
||||
var scope = McpQuotaEnforcementMiddleware.ResolveChargeScope(ctx, Home);
|
||||
Assert.Equal(McpChargeKind.Home, scope.Kind);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GlobalAdmin_IsNeverDelegated_ResolvesHome()
|
||||
{
|
||||
// The policy middleware short-circuits delegated resolution for platform scope, so a GA reading
|
||||
// ?tenantId=<customer> carries no delegated flags — the charge scope is Home (and Invoke tracks
|
||||
// GA on home only, exempt from the check).
|
||||
// Platform operations must never be blocked by a budget; Invoke still counts the GA on home.
|
||||
var ctx = new RequestContext { TenantId = Home, TargetTenantId = TenantB, IsGlobalAdmin = true };
|
||||
var scope = McpQuotaEnforcementMiddleware.ResolveChargeScope(ctx, Home);
|
||||
Assert.Equal(McpChargeKind.Home, scope.Kind);
|
||||
}
|
||||
|
||||
// ── Narrow — ISOLATION INVARIANT ───────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Narrow_WithoutBound_LeavesContextUntouched()
|
||||
{
|
||||
// null = unbounded (GA/Reader). Narrowing must neither introduce a bound nor touch the context.
|
||||
var ctx = new RequestContext { TenantId = Home, AllowedTenantIds = null };
|
||||
var narrowed = McpQuotaEnforcementMiddleware.Narrow(ctx, new[] { TenantB }, new[] { TenantC });
|
||||
Assert.Same(ctx, narrowed);
|
||||
Assert.Null(narrowed.AllowedTenantIds);
|
||||
Assert.Null(narrowed.QuotaExcludedTenantIds);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(new[] { TenantB, TenantC }, new[] { TenantB }, new[] { TenantC })]
|
||||
[InlineData(new[] { TenantB, TenantC }, new string[0], new[] { TenantB, TenantC })]
|
||||
[InlineData(new[] { TenantB, TenantC }, new[] { TenantB, TenantC }, new string[0])]
|
||||
[InlineData(new[] { TenantB }, new[] { TenantB, TenantD }, new[] { TenantD })]
|
||||
[InlineData(new[] { TenantB, TenantC, TenantD }, new[] { TenantD, TenantB }, new[] { TenantC })]
|
||||
public void Narrow_NeverWidens_NeverNull(string[] bound, string[] admitted, string[] excluded)
|
||||
{
|
||||
var ctx = new RequestContext { TenantId = Home, AllowedTenantIds = bound, IsDelegatedReader = true };
|
||||
|
||||
var narrowed = McpQuotaEnforcementMiddleware.Narrow(ctx, admitted, excluded);
|
||||
|
||||
Assert.NotNull(narrowed.AllowedTenantIds);
|
||||
var boundSet = new HashSet<string>(bound, StringComparer.OrdinalIgnoreCase);
|
||||
Assert.All(narrowed.AllowedTenantIds!, t => Assert.Contains(t, boundSet));
|
||||
Assert.True(narrowed.AllowedTenantIds!.Count <= bound.Length);
|
||||
// A foreign tenant sneaking into "admitted" never widens the bound (TenantD ∉ bound in row 4).
|
||||
Assert.DoesNotContain(narrowed.AllowedTenantIds!, t => !boundSet.Contains(t));
|
||||
if (narrowed.QuotaExcludedTenantIds != null)
|
||||
Assert.All(narrowed.QuotaExcludedTenantIds, t => Assert.Contains(t, boundSet));
|
||||
// The rest of the context survives the copy.
|
||||
Assert.True(narrowed.IsDelegatedReader);
|
||||
Assert.Equal(Home, narrowed.TenantId);
|
||||
Assert.True(McpQuotaEnforcementMiddleware.IsExempt(ctx));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Narrow_EmptyAdmittedSet_PublishesEmptyBound_NotNull()
|
||||
public void GlobalReader_IsNotExempt()
|
||||
{
|
||||
// Empty ⇒ the repository serves an empty page. NEVER null (= all tenants).
|
||||
var ctx = new RequestContext { TenantId = Home, AllowedTenantIds = new[] { TenantB, TenantC } };
|
||||
var narrowed = McpQuotaEnforcementMiddleware.Narrow(ctx, Array.Empty<string>(), new[] { TenantB, TenantC });
|
||||
Assert.NotNull(narrowed.AllowedTenantIds);
|
||||
Assert.Empty(narrowed.AllowedTenantIds!);
|
||||
Assert.Equal(new[] { TenantB, TenantC }, narrowed.QuotaExcludedTenantIds!.OrderBy(t => t));
|
||||
// A customer-facing read role: it stays within its home tenant's windows like every member.
|
||||
var ctx = new RequestContext { TenantId = Home, TargetTenantId = TenantB, IsGlobalReader = true };
|
||||
Assert.False(McpQuotaEnforcementMiddleware.IsExempt(ctx));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Narrow_NothingExcluded_OmitsTheExcludedSet()
|
||||
public void Member_And_DelegatedReader_AreNotExempt()
|
||||
{
|
||||
var ctx = new RequestContext { TenantId = Home, AllowedTenantIds = new[] { TenantB, TenantC } };
|
||||
var narrowed = McpQuotaEnforcementMiddleware.Narrow(ctx, new[] { TenantB, TenantC }, Array.Empty<string>());
|
||||
Assert.Equal(2, narrowed.AllowedTenantIds!.Count);
|
||||
Assert.Null(narrowed.QuotaExcludedTenantIds);
|
||||
Assert.False(McpQuotaEnforcementMiddleware.IsExempt(new RequestContext { TenantId = Home, TargetTenantId = Home }));
|
||||
Assert.False(McpQuotaEnforcementMiddleware.IsExempt(new RequestContext
|
||||
{
|
||||
TenantId = Home, TargetTenantId = TenantB, IsDelegatedReader = true, AllowedTenantIds = new[] { TenantB },
|
||||
}));
|
||||
}
|
||||
|
||||
// ── BuildExceededResponse — whose budget ───────────────────────────────────────
|
||||
|
||||
private static McpQuotaDecision Blocked(string tenantPlan, string? target, long tenantDailyUsed = 300, int tenantDaily = 300)
|
||||
private static McpQuotaDecision Blocked(string tenantPlan, long tenantDailyUsed = 300, int tenantDaily = 300)
|
||||
=> McpQuotaService.BuildDecision(
|
||||
new McpPlanLimits("pro", 1000, 20000, tenantPlan, tenantDaily, 60000),
|
||||
dailyUsed: 5, monthlyUsed: 50, tenantDailyUsed: tenantDailyUsed, tenantMonthlyUsed: 400,
|
||||
Now, target);
|
||||
Now);
|
||||
|
||||
[Fact]
|
||||
public void ManagedCommunityTarget_NamesTheTenant_AndTheUpgradePath()
|
||||
public void OrganizationBlock_KeepsTheOrganizationWording()
|
||||
{
|
||||
var body = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("community", TenantB), "customer.example");
|
||||
|
||||
var body = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("community"));
|
||||
Assert.Equal("tenant", body.Level);
|
||||
Assert.Equal(TenantB, body.TargetTenantId);
|
||||
Assert.Contains("managed tenant 'customer.example'", body.Error);
|
||||
Assert.Contains("tenant plan 'community'", body.Error);
|
||||
Assert.Contains("Upgrading that tenant to Pro", body.Error);
|
||||
Assert.DoesNotContain("your organization", body.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ManagedTarget_WithoutLabel_FallsBackToTheId()
|
||||
{
|
||||
var body = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("community", TenantB));
|
||||
Assert.Contains($"managed tenant '{TenantB}'", body.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ManagedProTarget_OmitsTheUpgradeHint()
|
||||
{
|
||||
var body = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("pro", TenantB, 3000, 3000), "customer.example");
|
||||
Assert.Equal(TenantB, body.TargetTenantId);
|
||||
Assert.DoesNotContain("Upgrading", body.Error);
|
||||
Assert.Contains("shared by all its members and delegated admins", body.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OwnTenant_KeepsTheOrganizationWording_AndNoTarget()
|
||||
{
|
||||
var body = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("community", target: null));
|
||||
Assert.Null(body.TargetTenantId);
|
||||
Assert.Contains("of your organization", body.Error);
|
||||
Assert.Contains("tenant plan 'community'", body.Error);
|
||||
Assert.DoesNotContain("managed tenant", body.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OwnCommunityTenant_NamesTheUpgradePath()
|
||||
public void OrganizationBlock_OnCommunity_NamesTheUpgradePath()
|
||||
{
|
||||
// The quota is the upgrade lever: a member blocked by their own Community organization window is told
|
||||
// that Community is sized for occasional use and that Pro lifts it — Pro members never see the hint.
|
||||
var community = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("community", target: null));
|
||||
var community = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("community"));
|
||||
Assert.Contains("sized for occasional use", community.Error);
|
||||
Assert.Contains("upgrading your organization to Pro", community.Error);
|
||||
Assert.EndsWith("Resets at 2026-09-03T00:00:00Z.", community.Error);
|
||||
|
||||
var pro = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("pro", target: null, 3000, 3000));
|
||||
var pro = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("pro", 3000, 3000));
|
||||
Assert.DoesNotContain("upgrading", pro.Error);
|
||||
Assert.DoesNotContain("occasional use", pro.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ManagedCommunityTarget_SaysWhosePlanGoverns()
|
||||
{
|
||||
// The delegated admin must learn that the CUSTOMER's plan (not their own Pro/MSP plan) governs the window.
|
||||
var body = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("community", TenantB), "customer.example");
|
||||
Assert.Contains("on the Community plan", body.Error);
|
||||
Assert.Contains("its own plan governs this window, not yours", body.Error);
|
||||
Assert.Contains("Upgrading that tenant to Pro", body.Error);
|
||||
Assert.Contains("shared by all its members", pro.Error);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -272,25 +97,14 @@ public class McpQuotaEnforcementMiddlewareTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllManagedTenantsExhausted_NamesTheCount_AndNoSingleTarget()
|
||||
public void UserLevelBlock_ReportsTheUserWindow()
|
||||
{
|
||||
var body = McpQuotaEnforcementMiddleware.BuildExceededResponse(Blocked("community", TenantB), exhaustedTenantCount: 3);
|
||||
Assert.Null(body.TargetTenantId);
|
||||
Assert.Equal("tenant", body.Level);
|
||||
Assert.Contains("all 3 managed tenants", body.Error);
|
||||
Assert.Contains("Earliest reset at 2026-09-03T00:00:00Z", body.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserLevelBlock_IgnoresTargetAndCount()
|
||||
{
|
||||
// The caller's OWN budget is exhausted — the target tenant is irrelevant and must not be named.
|
||||
var decision = McpQuotaService.BuildDecision(
|
||||
new McpPlanLimits("community", 100, 3000, "pro", 3000, 60000),
|
||||
dailyUsed: 100, monthlyUsed: 500, tenantDailyUsed: 1, tenantMonthlyUsed: 1, Now, TenantB);
|
||||
var body = McpQuotaEnforcementMiddleware.BuildExceededResponse(decision, "customer.example", exhaustedTenantCount: 2);
|
||||
Assert.Equal("user", body.Level);
|
||||
Assert.Null(body.TargetTenantId);
|
||||
dailyUsed: 100, monthlyUsed: 500, tenantDailyUsed: 1, tenantMonthlyUsed: 1, Now);
|
||||
var body = McpQuotaEnforcementMiddleware.BuildExceededResponse(decision);
|
||||
Assert.Equal(100, body.Limit);
|
||||
Assert.Equal(100, body.Used);
|
||||
Assert.Equal(
|
||||
"MCP daily request quota exceeded for plan 'community'. The Community plan is sized for occasional use; Pro raises your daily and monthly windows. Resets at 2026-09-03T00:00:00Z.",
|
||||
body.Error);
|
||||
|
||||
@@ -11,7 +11,8 @@ namespace AutopilotMonitor.Functions.Tests;
|
||||
/// <summary>
|
||||
/// Tests for <see cref="McpQuotaService"/>: window math (daily/monthly, reset times), plan
|
||||
/// precedence (per-user override → tenant edition; SectionUsagePlans definition → catalog
|
||||
/// fallback), and the fail-open contract on counter errors.
|
||||
/// fallback), the growth of both windows by purchased delegation slots, and the fail-open
|
||||
/// contract on counter errors.
|
||||
/// </summary>
|
||||
public class McpQuotaServiceTests
|
||||
{
|
||||
@@ -127,7 +128,9 @@ public class McpQuotaServiceTests
|
||||
IMemoryCache? cache = null,
|
||||
bool bound = true,
|
||||
Func<string?, TenantEdition>? editionResolver = null,
|
||||
Func<string?, string?>? tenantPlanOverrideResolver = null)
|
||||
Func<string?, string?>? tenantPlanOverrideResolver = null,
|
||||
Func<string?, int>? purchasedSlotsResolver = null,
|
||||
Func<string?, EditionResolution>? resolutionResolver = null)
|
||||
{
|
||||
var adminRepo = new Mock<IAdminRepository>();
|
||||
adminRepo.Setup(r => r.GetMcpUserAsync(It.IsAny<string>()))
|
||||
@@ -150,11 +153,15 @@ public class McpQuotaServiceTests
|
||||
var adminConfigService = new AdminConfigurationService(
|
||||
configRepo.Object, NullLogger<AdminConfigurationService>.Instance, cache);
|
||||
|
||||
var entitlements = resolutionResolver != null
|
||||
? new StubTenantEntitlementService(resolutionResolver, tenantPlanOverrideResolver, purchasedSlotsResolver)
|
||||
: new StubTenantEntitlementService(editionResolver ?? (_ => edition), tenantPlanOverrideResolver, purchasedSlotsResolver);
|
||||
|
||||
return new McpQuotaService(
|
||||
usageRepo.Object,
|
||||
mcpUserService,
|
||||
adminConfigService,
|
||||
new StubTenantEntitlementService(editionResolver ?? (_ => edition), tenantPlanOverrideResolver),
|
||||
entitlements,
|
||||
cache,
|
||||
NullLogger<McpQuotaService>.Instance,
|
||||
new TestTimeProvider(Now));
|
||||
@@ -338,25 +345,18 @@ public class McpQuotaServiceTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Check_DelegatedTarget_MspHomeOverride_LiftsTheCallerNotTheCustomer()
|
||||
public async Task Check_TenantOverride_ComposesWithPurchasedSlots()
|
||||
{
|
||||
// The MSP's home tenant carries the "msp" override; the managed Community customer does not.
|
||||
// The caller's own windows are lifted, the customer's organization window still bites.
|
||||
var repo = UsageRepo(("20260707", 1200));
|
||||
TenantRows(repo, Target, ("20260707", 300));
|
||||
var svc = Build(repo, planDefinitionsJson: MspPlanJson,
|
||||
editionResolver: HomeProTargetsCommunity,
|
||||
tenantPlanOverrideResolver: t => t == TenantId ? "msp" : null);
|
||||
// The "msp" definition carries no slot fields → the Pro catalog slot values grow its windows.
|
||||
var svc = Build(UsageRepo(("20260707", 1)), planDefinitionsJson: MspPlanJson,
|
||||
edition: TenantEdition.Pro, tenantPlanOverrideResolver: _ => "msp", purchasedSlotsResolver: _ => 2);
|
||||
|
||||
var d = await svc.CheckAsync(Oid, Upn, homeTenantId: TenantId, chargeTenantId: Target);
|
||||
var d = await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
|
||||
Assert.False(d.Allowed);
|
||||
Assert.Equal("tenant", d.Level);
|
||||
Assert.Equal("msp", d.Plan);
|
||||
Assert.Equal(1500, d.DailyLimit);
|
||||
Assert.Equal("community", d.TenantPlan);
|
||||
Assert.Equal(300, d.TenantDailyLimit);
|
||||
Assert.Equal(Target, d.TargetTenantId);
|
||||
Assert.Equal(2100, d.DailyLimit); // 1500 + 2 × 300
|
||||
Assert.Equal("msp", d.TenantPlan);
|
||||
Assert.Equal(6800, d.TenantDailyLimit); // 5000 + 2 × 900
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -560,155 +560,176 @@ public class McpQuotaServiceTests
|
||||
repo.Verify(r => r.GetTenantUsageAsync(It.IsAny<string>(), It.IsAny<string?>(), It.IsAny<string?>()), Times.Exactly(2));
|
||||
}
|
||||
|
||||
// ── Delegated (MSP) reads: the budget follows the data ───────────────────────
|
||||
// The CHARGED tenant (the managed customer) supplies the organization windows and counters; the
|
||||
// caller's own windows still follow their HOME tenant's plan (and their per-user override).
|
||||
// ── Purchased delegation slots grow both windows ─────────────────────────────
|
||||
// The home tenant's windows (user AND organization) grow by purchased slots × per-slot value; the slot
|
||||
// values come from the governing plan definition, else the home edition's catalog. Community and
|
||||
// conferred Pro carry catalog slot values of 0, so a stray slot count grows nothing there.
|
||||
|
||||
private const string Target = "22222222-2222-2222-2222-222222222222";
|
||||
private const string Target2 = "33333333-3333-3333-3333-333333333333";
|
||||
private const string OtherOid = "00000000-0000-0000-0000-000000000002";
|
||||
|
||||
private static void TenantRows(Mock<IUserUsageRepository> repo, string tenantId, params (string Date, long Count)[] rows)
|
||||
=> repo.Setup(r => r.GetTenantUsageAsync(tenantId, It.IsAny<string?>(), It.IsAny<string?>()))
|
||||
.ReturnsAsync(rows.Select(r => new TenantUsageRecord
|
||||
{
|
||||
TenantId = tenantId,
|
||||
UserId = "some-member-or-msp",
|
||||
Date = r.Date,
|
||||
RequestCount = r.Count
|
||||
}).ToList());
|
||||
[Fact]
|
||||
public async Task Check_PurchasedSlots_GrowBothWindows()
|
||||
{
|
||||
// Pro home with 3 purchased slots: 1000 + 3 × 300 per account, 3000 + 3 × 900 for the organization.
|
||||
var svc = Build(UsageRepo(("20260707", 1)), edition: TenantEdition.Pro, purchasedSlotsResolver: _ => 3);
|
||||
|
||||
private static TenantEdition HomeProTargetsCommunity(string? tenantId)
|
||||
=> tenantId == TenantId ? TenantEdition.Pro : TenantEdition.Community;
|
||||
var d = await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
|
||||
Assert.Equal(1900, d.DailyLimit);
|
||||
Assert.Equal(38000, d.MonthlyLimit);
|
||||
Assert.Equal(5700, d.TenantDailyLimit);
|
||||
Assert.Equal(114000, d.TenantMonthlyLimit);
|
||||
Assert.Equal(3, d.PurchasedDelegatedSlots);
|
||||
Assert.Equal(300, d.SlotDailyLimit);
|
||||
Assert.Equal(6000, d.SlotMonthlyLimit);
|
||||
Assert.Equal(900, d.SlotTenantDailyLimit);
|
||||
Assert.Equal(18000, d.SlotTenantMonthlyLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Check_DelegatedTarget_ChargesTheManagedTenantsPlanAndCounters()
|
||||
public async Task Check_IncludedSlotsOnly_NoGrowth()
|
||||
{
|
||||
// Pro MSP reads a Community customer whose organization window (300/day) other readers burned.
|
||||
var repo = UsageRepo(("20260707", 5));
|
||||
TenantRows(repo, Target, ("20260707", 300));
|
||||
var svc = Build(repo, editionResolver: HomeProTargetsCommunity);
|
||||
// A Pro tenant on its two included slots (purchased = 0) keeps the plain Pro windows.
|
||||
var svc = Build(UsageRepo(("20260707", 1)), edition: TenantEdition.Pro, purchasedSlotsResolver: _ => 0);
|
||||
|
||||
var d = await svc.CheckAsync(Oid, Upn, homeTenantId: TenantId, chargeTenantId: Target);
|
||||
var d = await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
|
||||
Assert.Equal(1000, d.DailyLimit);
|
||||
Assert.Equal(3000, d.TenantDailyLimit);
|
||||
Assert.Equal(0, d.PurchasedDelegatedSlots);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Check_ConferredProHome_GrowsNothing_EvenWithASlotCount()
|
||||
{
|
||||
// Conferred Pro (managed by a Pro MSP, not Pro itself): the catalog slot values are 0, so even a slot
|
||||
// count leaking through (the entitlement service gates it to 0 anyway) grows neither window.
|
||||
var conferred = new EditionResolution(TenantEdition.Pro, EditionSource.Msp, OwnPro: false);
|
||||
var svc = Build(UsageRepo(("20260707", 1)), resolutionResolver: _ => conferred, purchasedSlotsResolver: _ => 3);
|
||||
|
||||
var d = await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
|
||||
Assert.False(d.Allowed);
|
||||
Assert.Equal("tenant", d.Level);
|
||||
Assert.Equal("daily", d.Scope);
|
||||
Assert.Equal("community", d.TenantPlan);
|
||||
Assert.Equal(300, d.TenantDailyLimit);
|
||||
Assert.Equal(Target, d.TargetTenantId);
|
||||
// The caller's OWN windows still come from the Pro home tenant.
|
||||
Assert.Equal("pro", d.Plan);
|
||||
Assert.Equal(1000, d.DailyLimit);
|
||||
repo.Verify(r => r.GetTenantUsageAsync(TenantId, It.IsAny<string?>(), It.IsAny<string?>()), Times.Never);
|
||||
Assert.Equal(3000, d.TenantDailyLimit);
|
||||
Assert.Equal(0, d.SlotDailyLimit);
|
||||
Assert.Equal(0, d.SlotTenantDailyLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Check_DelegatedTarget_UserBudgetStillFollowsHome()
|
||||
public async Task Check_UserOverridePlan_StillGrowsWithTheHomeSlots()
|
||||
{
|
||||
// Community home (100/day) reading a Pro customer: the caller's own daily window bites first.
|
||||
var repo = UsageRepo(("20260707", 100));
|
||||
TenantRows(repo, Target, ("20260707", 1));
|
||||
var svc = Build(repo, editionResolver: t => t == Target ? TenantEdition.Pro : TenantEdition.Community);
|
||||
// The per-user "power" plan carries no slot fields → the HOME edition's catalog slot values apply,
|
||||
// so the override account grows with the tenant's purchased slots like everyone else.
|
||||
var json = """[{"name":"power","dailyRequestLimit":10000,"monthlyRequestLimit":100000,"description":""}]""";
|
||||
var svc = Build(UsageRepo(("20260707", 1)), planDefinitionsJson: json, edition: TenantEdition.Pro,
|
||||
mcpUserPlanOverride: "power", purchasedSlotsResolver: _ => 2);
|
||||
|
||||
var d = await svc.CheckAsync(Oid, Upn, homeTenantId: TenantId, chargeTenantId: Target);
|
||||
var d = await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
|
||||
Assert.Equal("power", d.Plan);
|
||||
Assert.Equal(10600, d.DailyLimit);
|
||||
Assert.Equal(112000, d.MonthlyLimit);
|
||||
Assert.Equal(4800, d.TenantDailyLimit); // 3000 + 2 × 900 — the organization follows the tenant plan
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Check_SlotValues_DefinitionZero_DisablesTheGrowth()
|
||||
{
|
||||
var json = """[{"name":"pro","dailyRequestLimit":1000,"monthlyRequestLimit":20000,"slotDailyRequestLimit":0,"slotMonthlyRequestLimit":0,"slotTenantDailyRequestLimit":0,"slotTenantMonthlyRequestLimit":0,"description":""}]""";
|
||||
var svc = Build(UsageRepo(("20260707", 1)), planDefinitionsJson: json, edition: TenantEdition.Pro, purchasedSlotsResolver: _ => 5);
|
||||
|
||||
var d = await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
|
||||
Assert.Equal(1000, d.DailyLimit);
|
||||
Assert.Equal(3000, d.TenantDailyLimit);
|
||||
Assert.Equal(5, d.PurchasedDelegatedSlots);
|
||||
Assert.Equal(0, d.SlotDailyLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Check_SlotValues_DefinitionSet_OverridesTheCatalog()
|
||||
{
|
||||
var json = """[{"name":"pro","dailyRequestLimit":1000,"monthlyRequestLimit":20000,"slotDailyRequestLimit":50,"slotMonthlyRequestLimit":500,"slotTenantDailyRequestLimit":100,"slotTenantMonthlyRequestLimit":1000,"description":""}]""";
|
||||
var svc = Build(UsageRepo(("20260707", 1)), planDefinitionsJson: json, edition: TenantEdition.Pro, purchasedSlotsResolver: _ => 2);
|
||||
|
||||
var d = await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
|
||||
Assert.Equal(1100, d.DailyLimit);
|
||||
Assert.Equal(21000, d.MonthlyLimit);
|
||||
Assert.Equal(3200, d.TenantDailyLimit);
|
||||
Assert.Equal(62000, d.TenantMonthlyLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Check_SlotValues_DefinitionNull_UsesTheCatalog()
|
||||
{
|
||||
// A pre-existing definition without slot fields → the edition's catalog slot values.
|
||||
var json = """[{"name":"pro","dailyRequestLimit":1000,"monthlyRequestLimit":20000,"description":""}]""";
|
||||
var svc = Build(UsageRepo(("20260707", 1)), planDefinitionsJson: json, edition: TenantEdition.Pro, purchasedSlotsResolver: _ => 1);
|
||||
|
||||
var d = await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
|
||||
Assert.Equal(1300, d.DailyLimit);
|
||||
Assert.Equal(3900, d.TenantDailyLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Check_LiftedWindow_StaysUnlimited_DespiteSlots()
|
||||
{
|
||||
// 0 = unlimited must never turn into "0 + slots × value" — a limit again. 30 000 requests today would
|
||||
// exceed any grown daily window (and the plain 20 000 monthly one) but stay under 20 000 + 4 × 6 000.
|
||||
var json = """[{"name":"pro","dailyRequestLimit":0,"monthlyRequestLimit":20000,"tenantDailyRequestLimit":0,"tenantMonthlyRequestLimit":60000,"description":""}]""";
|
||||
var svc = Build(UsageRepo(("20260707", 30000)), planDefinitionsJson: json, edition: TenantEdition.Pro, purchasedSlotsResolver: _ => 4);
|
||||
|
||||
var d = await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
|
||||
Assert.True(d.Allowed);
|
||||
Assert.Equal(0, d.DailyLimit);
|
||||
Assert.Equal(0, d.TenantDailyLimit);
|
||||
Assert.Equal(44000, d.MonthlyLimit);
|
||||
Assert.Equal(30000, d.MonthlyUsed);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1000, 3, 300, 1900)]
|
||||
[InlineData(1000, 0, 300, 1000)]
|
||||
[InlineData(1000, 3, 0, 1000)]
|
||||
[InlineData(0, 3, 300, 0)]
|
||||
[InlineData(-1, 3, 300, -1)]
|
||||
public void Grow_AddsSlotsTimesValue_AndKeepsALiftedWindowLifted(int baseLimit, int slots, int perSlot, int expected)
|
||||
=> Assert.Equal(expected, McpQuotaService.Grow(baseLimit, slots, perSlot));
|
||||
|
||||
[Fact]
|
||||
public async Task Check_GrownWindow_IsTheEnforcedOne()
|
||||
{
|
||||
// Pro 1000 + 1 × 300 = 1300: the 1300th request of the day is the first blocked one.
|
||||
var below = Build(UsageRepo(("20260707", 1299)), edition: TenantEdition.Pro, purchasedSlotsResolver: _ => 1);
|
||||
Assert.True((await below.CheckAsync(Oid, Upn, TenantId)).Allowed);
|
||||
|
||||
var at = Build(UsageRepo(("20260707", 1300)), edition: TenantEdition.Pro, purchasedSlotsResolver: _ => 1);
|
||||
var d = await at.CheckAsync(Oid, Upn, TenantId);
|
||||
Assert.False(d.Allowed);
|
||||
Assert.Equal("daily", d.Scope);
|
||||
Assert.Equal("user", d.Level);
|
||||
Assert.Equal("community", d.Plan);
|
||||
Assert.Equal("pro", d.TenantPlan);
|
||||
Assert.Equal(Target, d.TargetTenantId);
|
||||
Assert.Equal(1300, d.ExceededLimit);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Check_OwnTenant_NamesNoTarget()
|
||||
{
|
||||
var svc = Build(UsageRepo(("20260707", 1)));
|
||||
var legacy = await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
var explicitHome = await svc.CheckAsync(Oid, Upn, TenantId, TenantId);
|
||||
Assert.Null(legacy.TargetTenantId);
|
||||
Assert.Null(explicitHome.TargetTenantId);
|
||||
}
|
||||
// ── One tenant, many callers ─────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Check_TenantSnapshot_IsSharedAcrossCallersOfTheSameTenant()
|
||||
public async Task Check_TenantSnapshot_IsSharedAcrossMembersOfTheSameTenant()
|
||||
{
|
||||
// Two different callers charged to the same customer read its organization counters ONCE per TTL.
|
||||
var repo = UsageRepo(("20260707", 1));
|
||||
// Two members of one tenant read its organization counters ONCE per TTL.
|
||||
var repo = UsageRepo(new[] { ("20260707", 1L) }, tenantRows: new[] { ("20260707", 10L) });
|
||||
repo.Setup(r => r.GetUsageByUserAsync(OtherOid, It.IsAny<string?>(), It.IsAny<string?>()))
|
||||
.ReturnsAsync(new List<UserUsageRecord>());
|
||||
TenantRows(repo, Target, ("20260707", 10));
|
||||
var svc = Build(repo, editionResolver: HomeProTargetsCommunity);
|
||||
|
||||
await svc.CheckAsync(Oid, Upn, TenantId, Target);
|
||||
await svc.CheckAsync(OtherOid, "bob@contoso.com", TenantId, Target);
|
||||
|
||||
repo.Verify(r => r.GetTenantUsageAsync(Target, It.IsAny<string?>(), It.IsAny<string?>()), Times.Once);
|
||||
repo.Verify(r => r.GetUsageByUserAsync(It.IsAny<string>(), It.IsAny<string?>(), It.IsAny<string?>()), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckMany_ExcludesExhaustedTenants_AdmitsTheRest()
|
||||
{
|
||||
var repo = UsageRepo(("20260707", 1));
|
||||
TenantRows(repo, Target, ("20260707", 300));
|
||||
TenantRows(repo, Target2, ("20260707", 10));
|
||||
var svc = Build(repo, editionResolver: HomeProTargetsCommunity);
|
||||
|
||||
var r = await svc.CheckManyAsync(Oid, Upn, TenantId, new[] { Target, Target2 });
|
||||
|
||||
Assert.True(r.Allowed);
|
||||
Assert.Null(r.BlockingDecision);
|
||||
Assert.True(r.UserDecision.Allowed);
|
||||
Assert.Equal(new[] { Target2 }, r.AdmittedTenantIds);
|
||||
Assert.Equal(new[] { Target }, r.ExcludedTenantIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckMany_AllExhausted_BlocksWithTheEarliestReset()
|
||||
{
|
||||
var repo = UsageRepo(("20260707", 1));
|
||||
TenantRows(repo, Target, ("20260707", 300)); // daily window → resets tomorrow
|
||||
TenantRows(repo, Target2, ("20260701", 9000), ("20260707", 1)); // monthly window → resets on the 1st
|
||||
var svc = Build(repo, editionResolver: HomeProTargetsCommunity);
|
||||
|
||||
var r = await svc.CheckManyAsync(Oid, Upn, TenantId, new[] { Target, Target2 });
|
||||
|
||||
Assert.False(r.Allowed);
|
||||
Assert.Empty(r.AdmittedTenantIds);
|
||||
Assert.Equal(2, r.ExcludedTenantIds.Count);
|
||||
Assert.Equal("tenant", r.BlockingDecision!.Level);
|
||||
Assert.Equal("daily", r.BlockingDecision.Scope);
|
||||
Assert.Equal(new DateTime(2026, 7, 8, 0, 0, 0, DateTimeKind.Utc), r.BlockingDecision.ResetUtc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckMany_UserExhausted_SkipsEveryTenantRead()
|
||||
{
|
||||
var repo = UsageRepo(("20260707", 100)); // Community home: 100/day
|
||||
var svc = Build(repo);
|
||||
|
||||
var r = await svc.CheckManyAsync(Oid, Upn, TenantId, new[] { Target, Target2 });
|
||||
await svc.CheckAsync(Oid, Upn, TenantId);
|
||||
await svc.CheckAsync(OtherOid, "bob@contoso.com", TenantId);
|
||||
|
||||
Assert.False(r.Allowed);
|
||||
Assert.Equal("user", r.BlockingDecision!.Level);
|
||||
Assert.Equal(2, r.ExcludedTenantIds.Count);
|
||||
repo.Verify(r2 => r2.GetTenantUsageAsync(It.IsAny<string>(), It.IsAny<string?>(), It.IsAny<string?>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckMany_TenantReadFails_AdmitsThatTenant_FailOpen()
|
||||
{
|
||||
var repo = UsageRepo(("20260707", 1));
|
||||
repo.Setup(r => r.GetTenantUsageAsync(Target, It.IsAny<string?>(), It.IsAny<string?>()))
|
||||
.ThrowsAsync(new InvalidOperationException("storage down"));
|
||||
TenantRows(repo, Target2, ("20260707", 10));
|
||||
var svc = Build(repo, editionResolver: HomeProTargetsCommunity);
|
||||
|
||||
var r = await svc.CheckManyAsync(Oid, Upn, TenantId, new[] { Target, Target2 });
|
||||
|
||||
Assert.True(r.Allowed);
|
||||
Assert.Equal(new[] { Target, Target2 }, r.AdmittedTenantIds.OrderBy(t => t));
|
||||
Assert.Empty(r.ExcludedTenantIds);
|
||||
repo.Verify(r => r.GetTenantUsageAsync(TenantId, It.IsAny<string?>(), It.IsAny<string?>()), Times.Once);
|
||||
repo.Verify(r => r.GetUsageByUserAsync(It.IsAny<string>(), It.IsAny<string?>(), It.IsAny<string?>()), Times.Exactly(2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -407,6 +407,12 @@ public class MetricsWireParityTests
|
||||
var tenantMonthlyLimit = 9000;
|
||||
long tenantDailyUsed = 120;
|
||||
long tenantMonthlyUsed = 4100;
|
||||
// Three purchased delegation slots: the limits above already grew by them; the breakdown rides along.
|
||||
int? purchasedDelegatedSlots = 3;
|
||||
int? slotDailyLimit = 300;
|
||||
int? slotMonthlyLimit = 6000;
|
||||
int? slotTenantDailyLimit = 900;
|
||||
int? slotTenantMonthlyLimit = 18000;
|
||||
var records = SampleUsageRecords(userId);
|
||||
|
||||
AssertParity(
|
||||
@@ -427,7 +433,12 @@ public class MetricsWireParityTests
|
||||
tenantDailyLimit,
|
||||
tenantMonthlyLimit,
|
||||
tenantDailyUsed,
|
||||
tenantMonthlyUsed
|
||||
tenantMonthlyUsed,
|
||||
purchasedDelegatedSlots,
|
||||
slotDailyLimit,
|
||||
slotMonthlyLimit,
|
||||
slotTenantDailyLimit,
|
||||
slotTenantMonthlyLimit
|
||||
},
|
||||
records
|
||||
},
|
||||
@@ -448,7 +459,12 @@ public class MetricsWireParityTests
|
||||
TenantDailyLimit = tenantDailyLimit,
|
||||
TenantMonthlyLimit = tenantMonthlyLimit,
|
||||
TenantDailyUsed = tenantDailyUsed,
|
||||
TenantMonthlyUsed = tenantMonthlyUsed
|
||||
TenantMonthlyUsed = tenantMonthlyUsed,
|
||||
PurchasedDelegatedSlots = purchasedDelegatedSlots,
|
||||
SlotDailyLimit = slotDailyLimit,
|
||||
SlotMonthlyLimit = slotMonthlyLimit,
|
||||
SlotTenantDailyLimit = slotTenantDailyLimit,
|
||||
SlotTenantMonthlyLimit = slotTenantMonthlyLimit
|
||||
},
|
||||
Records = records
|
||||
});
|
||||
@@ -457,10 +473,16 @@ public class MetricsWireParityTests
|
||||
[Fact]
|
||||
public void GetMyMcpUsageResponse_omits_null_upn_and_usagePlan()
|
||||
{
|
||||
// …and the slot breakdown, which is absent when no delegation slot was purchased.
|
||||
var userId = "7aa20c11-0002-4b7c-a1d2-52f3aaaa0008";
|
||||
string? upn = null;
|
||||
string? usagePlan = null;
|
||||
var effectivePlan = "free";
|
||||
int? purchasedDelegatedSlots = null;
|
||||
int? slotDailyLimit = null;
|
||||
int? slotMonthlyLimit = null;
|
||||
int? slotTenantDailyLimit = null;
|
||||
int? slotTenantMonthlyLimit = null;
|
||||
var records = new List<UserUsageRecord>();
|
||||
|
||||
AssertParity(
|
||||
@@ -481,7 +503,12 @@ public class MetricsWireParityTests
|
||||
tenantDailyLimit = 0,
|
||||
tenantMonthlyLimit = 0,
|
||||
tenantDailyUsed = 0L,
|
||||
tenantMonthlyUsed = 0L
|
||||
tenantMonthlyUsed = 0L,
|
||||
purchasedDelegatedSlots,
|
||||
slotDailyLimit,
|
||||
slotMonthlyLimit,
|
||||
slotTenantDailyLimit,
|
||||
slotTenantMonthlyLimit
|
||||
},
|
||||
records
|
||||
},
|
||||
@@ -672,7 +699,6 @@ public class MetricsWireParityTests
|
||||
{
|
||||
var tenantId = "7aa20c11-0002-4b7c-a1d2-52f3aaaa0001";
|
||||
var userId = "7aa20c11-0002-4b7c-a1d2-52f3aaaa0009";
|
||||
var homeTenantId = "7aa20c11-0002-4b7c-a1d2-52f3aaaa0002";
|
||||
var lastRequestAt = new DateTime(2026, 9, 2, 14, 5, 0, DateTimeKind.Utc);
|
||||
|
||||
AssertParity(
|
||||
@@ -686,9 +712,7 @@ public class MetricsWireParityTests
|
||||
new
|
||||
{
|
||||
userId,
|
||||
userPrincipalName = "msp@partner.example",
|
||||
delegated = true,
|
||||
homeTenantId,
|
||||
userPrincipalName = "admin@contoso.com",
|
||||
requestsToday = 4L,
|
||||
requestsThisMonth = 120L,
|
||||
requestsInRange = 120L,
|
||||
@@ -706,9 +730,7 @@ public class MetricsWireParityTests
|
||||
new()
|
||||
{
|
||||
UserId = userId,
|
||||
UserPrincipalName = "msp@partner.example",
|
||||
Delegated = true,
|
||||
HomeTenantId = homeTenantId,
|
||||
UserPrincipalName = "admin@contoso.com",
|
||||
RequestsToday = 4,
|
||||
RequestsThisMonth = 120,
|
||||
RequestsInRange = 120,
|
||||
@@ -719,13 +741,12 @@ public class MetricsWireParityTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetMcpOrganizationUsageResponse_omits_null_upn_home_and_lastRequest()
|
||||
public void GetMcpOrganizationUsageResponse_omits_null_upn_and_lastRequest()
|
||||
{
|
||||
// A row written before the attribution columns existed: own member, no UPN, no timestamp.
|
||||
// A row written before the attribution columns existed: no UPN, no timestamp.
|
||||
var tenantId = "7aa20c11-0002-4b7c-a1d2-52f3aaaa0001";
|
||||
var userId = "7aa20c11-0002-4b7c-a1d2-52f3aaaa0009";
|
||||
string? userPrincipalName = null;
|
||||
string? homeTenantId = null;
|
||||
DateTime? lastRequestAt = null;
|
||||
|
||||
AssertParity(
|
||||
@@ -740,8 +761,6 @@ public class MetricsWireParityTests
|
||||
{
|
||||
userId,
|
||||
userPrincipalName,
|
||||
delegated = false,
|
||||
homeTenantId,
|
||||
requestsToday = 0L,
|
||||
requestsThisMonth = 3L,
|
||||
requestsInRange = 3L,
|
||||
@@ -756,11 +775,63 @@ public class MetricsWireParityTests
|
||||
DateTo = "20260902",
|
||||
Users = new List<McpOrganizationUsageItem>
|
||||
{
|
||||
new() { UserId = userId, Delegated = false, RequestsToday = 0, RequestsThisMonth = 3, RequestsInRange = 3 },
|
||||
new() { UserId = userId, RequestsToday = 0, RequestsThisMonth = 3, RequestsInRange = 3 },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetMcpOrganizationUsageResponse_quota_carries_the_slot_breakdown()
|
||||
{
|
||||
// Three purchased slots: 3 000 + 3 × 900 arrive as the grown limit; the breakdown lets the page show the sum.
|
||||
var tenantId = "7aa20c11-0002-4b7c-a1d2-52f3aaaa0001";
|
||||
var daily = new List<McpOrganizationDailyItem>
|
||||
{
|
||||
new() { Date = "20260901", Requests = 12 },
|
||||
new() { Date = "20260902", Requests = 7 },
|
||||
};
|
||||
|
||||
AssertParity(
|
||||
new
|
||||
{
|
||||
tenantId,
|
||||
dateFrom = "20260901",
|
||||
dateTo = "20260902",
|
||||
users = Array.Empty<object>(),
|
||||
quota = new
|
||||
{
|
||||
tenantPlan = "pro",
|
||||
dailyLimit = 5700,
|
||||
monthlyLimit = 114000,
|
||||
dailyUsed = 7L,
|
||||
monthlyUsed = 19L,
|
||||
purchasedDelegatedSlots = 3,
|
||||
slotDailyLimit = 900,
|
||||
slotMonthlyLimit = 18000,
|
||||
},
|
||||
daily,
|
||||
},
|
||||
new GetMcpOrganizationUsageResponse
|
||||
{
|
||||
TenantId = tenantId,
|
||||
DateFrom = "20260901",
|
||||
DateTo = "20260902",
|
||||
Users = new List<McpOrganizationUsageItem>(),
|
||||
Quota = new McpOrganizationQuotaNode
|
||||
{
|
||||
TenantPlan = "pro",
|
||||
DailyLimit = 5700,
|
||||
MonthlyLimit = 114000,
|
||||
DailyUsed = 7,
|
||||
MonthlyUsed = 19,
|
||||
PurchasedDelegatedSlots = 3,
|
||||
SlotDailyLimit = 900,
|
||||
SlotMonthlyLimit = 18000,
|
||||
},
|
||||
Daily = daily,
|
||||
});
|
||||
}
|
||||
|
||||
private static void AssertParity(object anonymousLiteral, IApiResponse typed)
|
||||
=> ApiResponseWireParityTests.AssertWireIdentical(anonymousLiteral, typed);
|
||||
}
|
||||
|
||||
+1
-1
@@ -414,7 +414,7 @@ public class OffboardingMarkerCleanupFunctionTests
|
||||
{
|
||||
}
|
||||
|
||||
public override Task<int> WipeByExactPartitionAsync(string tableName, string normalizedTenantId, CancellationToken ct = default)
|
||||
protected override Task<int> WipeByExactPartitionCoreAsync(string tableName, string normalizedTenantId, string filter, CancellationToken ct)
|
||||
{
|
||||
WipeCalls.Add((tableName, normalizedTenantId));
|
||||
if (ThrowOnNextWipe is { } ex)
|
||||
|
||||
+9
-9
@@ -593,9 +593,9 @@ public class TenantOffboardingHandlerDrainContractTests
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="SafeWipeService"/> that records call counts without performing any storage IO.
|
||||
/// Tests use this to assert that fail-closed paths NEVER touch the wipe methods. Now that
|
||||
/// <see cref="SafeWipeService"/> exposes the wipe methods as <c>virtual</c>, the production
|
||||
/// dispatch lands here.
|
||||
/// Tests use this to assert that fail-closed paths NEVER touch the wipe methods. Only the
|
||||
/// protected cores are overridden, so the tenant-GUID guard of the public entry points still
|
||||
/// runs against whatever the handler passes in.
|
||||
/// </summary>
|
||||
private sealed class CountingSafeWipeService : SafeWipeService
|
||||
{
|
||||
@@ -610,37 +610,37 @@ public class TenantOffboardingHandlerDrainContractTests
|
||||
{
|
||||
}
|
||||
|
||||
public override Task<int> WipeByExactPartitionAsync(string tableName, string normalizedTenantId, CancellationToken ct = default)
|
||||
protected override Task<int> WipeByExactPartitionCoreAsync(string tableName, string normalizedTenantId, string filter, CancellationToken ct)
|
||||
{
|
||||
WipeCallCount++;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public override Task<int> WipeByCompositePartitionRangeAsync(string tableName, string normalizedTenantId, CancellationToken ct = default)
|
||||
protected override Task<int> WipeByCompositePartitionRangeCoreAsync(string tableName, string normalizedTenantId, CancellationToken ct)
|
||||
{
|
||||
WipeCallCount++;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public override Task<int> WipeByDiscriminatorAndTenantPropertyAsync(string tableName, string discriminator, string normalizedTenantId, CancellationToken ct = default)
|
||||
protected override Task<int> WipeByDiscriminatorAndTenantPropertyCoreAsync(string tableName, string discriminator, string normalizedTenantId, CancellationToken ct)
|
||||
{
|
||||
WipeCallCount++;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public override Task<int> WipeByTenantIdPropertyAsync(string tableName, string normalizedTenantId, CancellationToken ct = default)
|
||||
protected override Task<int> WipeByTenantIdPropertyCoreAsync(string tableName, string normalizedTenantId, CancellationToken ct)
|
||||
{
|
||||
WipeCallCount++;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public override Task<int> WipeByRowKeyAsync(string tableName, string normalizedTenantId, CancellationToken ct = default)
|
||||
protected override Task<int> WipeByRowKeyCoreAsync(string tableName, string normalizedTenantId, string filter, CancellationToken ct)
|
||||
{
|
||||
WipeCallCount++;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public override Task<int> WipeBlobsByTenantPrefixAsync(string containerName, string normalizedTenantId, CancellationToken ct = default)
|
||||
protected override Task<int> WipeBlobsByTenantPrefixCoreAsync(string containerName, string normalizedTenantId, CancellationToken ct)
|
||||
{
|
||||
WipeCallCount++;
|
||||
return Task.FromResult(0);
|
||||
|
||||
+26
-6
@@ -320,6 +320,22 @@ public class TenantOffboardingHandlerOrderingTests
|
||||
Assert.Contains(Constants.TableNames.ScriptNameCache, harness.SafeWipeProbe.ExactPartitionWipes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostDrain_OwnedGroupWipe_AnchorsOnSelfServiceGroupId()
|
||||
{
|
||||
// The tenant's own self-service group lives under "msp-{tenantId}" — not a GUID. The wipe
|
||||
// must go through the owned-group entry points (tenant GUID in, group id derived inside);
|
||||
// handing the group id to a tenant-GUID entry point throws and poisons every offboarding.
|
||||
var harness = Harness.New();
|
||||
var ownedGroupId = Constants.TenantGroupIds.ForHomeTenant(TenantId);
|
||||
|
||||
await harness.Sut.HandleAsync(harness.Envelope());
|
||||
|
||||
Assert.Contains((Constants.TableNames.TenantGroups, ownedGroupId), harness.SafeWipeProbe.ExactPartitionAnchors);
|
||||
Assert.Contains((Constants.TableNames.TenantGroupAssignments, ownedGroupId), harness.SafeWipeProbe.RowKeyAnchors);
|
||||
Assert.Equal("Completed", harness.Repo.History[HistoryRowKey].Status);
|
||||
}
|
||||
|
||||
// ── Harness (copied minimal — only what these tests need) ───────────────────
|
||||
|
||||
private sealed class Harness
|
||||
@@ -470,12 +486,16 @@ public class TenantOffboardingHandlerOrderingTests
|
||||
new BlobStorageService(new BlobServiceClient("UseDevelopmentStorage=true"),
|
||||
NullLogger<BlobStorageService>.Instance, usesManagedIdentity: false),
|
||||
NullLogger<SafeWipeService>.Instance) { }
|
||||
public override Task<int> WipeByExactPartitionAsync(string t, string i, CancellationToken c = default) { WipeCallCount++; ExactPartitionWipes.Add(t); return Task.FromResult(0); }
|
||||
public override Task<int> WipeByCompositePartitionRangeAsync(string t, string i, CancellationToken c = default) { WipeCallCount++; return Task.FromResult(0); }
|
||||
public override Task<int> WipeByDiscriminatorAndTenantPropertyAsync(string t, string d, string i, CancellationToken c = default) { WipeCallCount++; return Task.FromResult(0); }
|
||||
public override Task<int> WipeByTenantIdPropertyAsync(string t, string i, CancellationToken c = default) { WipeCallCount++; PropertyOnlyWipes.Add(t); return Task.FromResult(0); }
|
||||
public override Task<int> WipeByRowKeyAsync(string t, string i, CancellationToken c = default) { WipeCallCount++; RowKeyWipes.Add(t); return Task.FromResult(0); }
|
||||
public override Task<int> WipeBlobsByTenantPrefixAsync(string c, string i, CancellationToken ct = default) { WipeCallCount++; return Task.FromResult(0); }
|
||||
/// <summary>(table, partition key) pairs of the exact-partition wipe — the anchor the delete would run against.</summary>
|
||||
public List<(string Table, string Anchor)> ExactPartitionAnchors { get; } = new();
|
||||
/// <summary>(table, row key) pairs of the RowKey-anchored wipe.</summary>
|
||||
public List<(string Table, string Anchor)> RowKeyAnchors { get; } = new();
|
||||
protected override Task<int> WipeByExactPartitionCoreAsync(string t, string pk, string filter, CancellationToken c) { WipeCallCount++; ExactPartitionWipes.Add(t); ExactPartitionAnchors.Add((t, pk)); return Task.FromResult(0); }
|
||||
protected override Task<int> WipeByCompositePartitionRangeCoreAsync(string t, string i, CancellationToken c) { WipeCallCount++; return Task.FromResult(0); }
|
||||
protected override Task<int> WipeByDiscriminatorAndTenantPropertyCoreAsync(string t, string d, string i, CancellationToken c) { WipeCallCount++; return Task.FromResult(0); }
|
||||
protected override Task<int> WipeByTenantIdPropertyCoreAsync(string t, string i, CancellationToken c) { WipeCallCount++; PropertyOnlyWipes.Add(t); return Task.FromResult(0); }
|
||||
protected override Task<int> WipeByRowKeyCoreAsync(string t, string rk, string filter, CancellationToken c) { WipeCallCount++; RowKeyWipes.Add(t); RowKeyAnchors.Add((t, rk)); return Task.FromResult(0); }
|
||||
protected override Task<int> WipeBlobsByTenantPrefixCoreAsync(string c, string i, CancellationToken ct) { WipeCallCount++; return Task.FromResult(0); }
|
||||
}
|
||||
|
||||
private sealed class NoopCustomsArchivingOrderingHandler : TenantOffboardingHandler
|
||||
|
||||
+130
-19
@@ -111,6 +111,71 @@ public class TenantOffboardingWorkerPoisonTests
|
||||
Assert.Equal("InProgress", harness.Repo.History[HistoryRowKey].Status);
|
||||
}
|
||||
|
||||
// ── Superseded dead letters ─────────────────────────────────────────────────
|
||||
//
|
||||
// A retry after a poisoned run is a new envelope, so nothing else ever removes the dead
|
||||
// letter of the failed run. The completed run sweeps it.
|
||||
|
||||
private const string OtherTenantId = "22222222-2222-2222-2222-222222222222";
|
||||
|
||||
[Fact]
|
||||
public async Task Completed_RemovesOnlyTheTenantsDeadLetters()
|
||||
{
|
||||
var harness = new Harness();
|
||||
harness.Repo.History[HistoryRowKey].Status = "Completed";
|
||||
var own = harness.AddDeadLetter(TenantId.ToUpperInvariant()); // tenant ids compare case-insensitively
|
||||
var foreign = harness.AddDeadLetter(OtherTenantId);
|
||||
var malformed = harness.AddDeadLetter(tenantId: null, bodyOverride: "{ not json at all");
|
||||
|
||||
await harness.InvokeHandleAsync();
|
||||
|
||||
Assert.Equal(new[] { own }, harness.PoisonDeletedIds);
|
||||
Assert.DoesNotContain(foreign, harness.PoisonDeletedIds);
|
||||
Assert.DoesNotContain(malformed, harness.PoisonDeletedIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Completed_SweepsPastTheFirstBatch()
|
||||
{
|
||||
var harness = new Harness();
|
||||
harness.Repo.History[HistoryRowKey].Status = "Completed";
|
||||
for (var i = 0; i < TenantOffboardingWorker.PoisonSweepBatchSize; i++)
|
||||
harness.AddDeadLetter(OtherTenantId);
|
||||
var own = harness.AddDeadLetter(TenantId);
|
||||
|
||||
await harness.InvokeHandleAsync();
|
||||
|
||||
Assert.Equal(new[] { own }, harness.PoisonDeletedIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NotCompleted_LeavesThePoisonQueueAlone()
|
||||
{
|
||||
// A pickup that finds the run Failed must not touch the dead letter — it is the only
|
||||
// queue-side trace of a failure nobody has resolved yet.
|
||||
var harness = new Harness();
|
||||
harness.Repo.History[HistoryRowKey].Status = "Failed";
|
||||
harness.AddDeadLetter(TenantId);
|
||||
|
||||
await harness.InvokeHandleAsync();
|
||||
|
||||
Assert.Equal(0, harness.PoisonReceiveCount);
|
||||
Assert.Empty(harness.PoisonDeletedIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Completed_SweepFailure_DoesNotFailTheRun()
|
||||
{
|
||||
// A throw here would keep the completed envelope on the main queue and finally move a
|
||||
// Completed run to poison.
|
||||
var harness = new Harness(poisonReceiveThrows: true);
|
||||
harness.Repo.History[HistoryRowKey].Status = "Completed";
|
||||
|
||||
await harness.InvokeHandleAsync();
|
||||
|
||||
Assert.Equal(1, harness.PoisonReceiveCount);
|
||||
}
|
||||
|
||||
// ── Harness ─────────────────────────────────────────────────────────────────
|
||||
|
||||
private sealed class Harness
|
||||
@@ -121,11 +186,38 @@ public class TenantOffboardingWorkerPoisonTests
|
||||
public Mock<QueueClient> PoisonQueue { get; } = new();
|
||||
public int PoisonSendCount { get; private set; }
|
||||
public int MainDeleteCount { get; private set; }
|
||||
public int PoisonReceiveCount { get; private set; }
|
||||
public List<string> PoisonDeletedIds { get; } = new();
|
||||
|
||||
public Harness(bool poisonSendThrows = false)
|
||||
// Poison-queue content for the sweep tests. A received message turns invisible, like the
|
||||
// real queue, so a second receive returns the next batch instead of the same one.
|
||||
private readonly List<QueueMessage> _deadLetters = new();
|
||||
private readonly HashSet<string> _hiddenDeadLetters = new();
|
||||
|
||||
public Harness(bool poisonSendThrows = false, bool poisonReceiveThrows = false)
|
||||
{
|
||||
SeedAudit();
|
||||
|
||||
PoisonQueue.Setup(q => q.ReceiveMessagesAsync(It.IsAny<int?>(), It.IsAny<TimeSpan?>(), It.IsAny<CancellationToken>()))
|
||||
.Returns<int?, TimeSpan?, CancellationToken>((max, _, _) =>
|
||||
{
|
||||
PoisonReceiveCount++;
|
||||
if (poisonReceiveThrows) throw new InvalidOperationException("simulated poison-queue 503");
|
||||
var batch = _deadLetters
|
||||
.Where(m => !_hiddenDeadLetters.Contains(m.MessageId))
|
||||
.Take(max ?? 1)
|
||||
.ToArray();
|
||||
foreach (var m in batch) _hiddenDeadLetters.Add(m.MessageId);
|
||||
return Task.FromResult(Response.FromValue(batch, new Mock<Response>().Object));
|
||||
});
|
||||
|
||||
PoisonQueue.Setup(q => q.DeleteMessageAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.Returns<string, string, CancellationToken>((id, _, _) =>
|
||||
{
|
||||
PoisonDeletedIds.Add(id);
|
||||
return Task.FromResult(new Mock<Response>().Object);
|
||||
});
|
||||
|
||||
// Cascade-handler dependencies. The handler is real — it talks to FakeRepo +
|
||||
// OpsEvent + (unused) other deps. Only MarkEnvelopeFailedFromPoisonAsync is
|
||||
// exercised, so most deps are noise.
|
||||
@@ -160,24 +252,43 @@ public class TenantOffboardingWorkerPoisonTests
|
||||
}
|
||||
|
||||
public QueueMessage BuildMessage(int dequeueCount, string? bodyOverride = null)
|
||||
{
|
||||
var envelope = new TenantOffboardingEnvelope
|
||||
{
|
||||
TenantId = TenantId,
|
||||
HistoryPartitionKey = Constants.OffboardingPartitionKeys.History,
|
||||
HistoryRowKey = HistoryRowKey,
|
||||
InitiatedBy = "alice@contoso.invalid",
|
||||
InitiatedAt = DateTime.UtcNow.AddMinutes(-1),
|
||||
EnqueuedAt = DateTime.UtcNow,
|
||||
DrainPollCount = 0,
|
||||
};
|
||||
return QueuesModelFactory.QueueMessage(
|
||||
=> QueuesModelFactory.QueueMessage(
|
||||
messageId: Guid.NewGuid().ToString(),
|
||||
popReceipt: "fake-receipt",
|
||||
body: new BinaryData(bodyOverride ?? JsonConvert.SerializeObject(envelope)),
|
||||
body: new BinaryData(bodyOverride ?? JsonConvert.SerializeObject(BuildEnvelope(TenantId))),
|
||||
dequeueCount: dequeueCount);
|
||||
|
||||
/// <summary>Puts a dead letter into the poison queue and returns its message id.</summary>
|
||||
public string AddDeadLetter(string? tenantId, string? bodyOverride = null)
|
||||
{
|
||||
var msg = QueuesModelFactory.QueueMessage(
|
||||
messageId: Guid.NewGuid().ToString(),
|
||||
popReceipt: "fake-receipt",
|
||||
body: new BinaryData(bodyOverride ?? JsonConvert.SerializeObject(BuildEnvelope(tenantId!))),
|
||||
dequeueCount: 1);
|
||||
_deadLetters.Add(msg);
|
||||
return msg.MessageId;
|
||||
}
|
||||
|
||||
public Task InvokeHandleAsync()
|
||||
{
|
||||
var method = typeof(TenantOffboardingWorker)
|
||||
.GetMethod("HandleAsync", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
Assert.NotNull(method);
|
||||
return (Task)method!.Invoke(Worker, new object[] { BuildEnvelope(TenantId), default(CancellationToken) })!;
|
||||
}
|
||||
|
||||
private static TenantOffboardingEnvelope BuildEnvelope(string tenantId) => new()
|
||||
{
|
||||
TenantId = tenantId,
|
||||
HistoryPartitionKey = Constants.OffboardingPartitionKeys.History,
|
||||
HistoryRowKey = HistoryRowKey,
|
||||
InitiatedBy = "alice@contoso.invalid",
|
||||
InitiatedAt = DateTime.UtcNow.AddMinutes(-1),
|
||||
EnqueuedAt = DateTime.UtcNow,
|
||||
DrainPollCount = 0,
|
||||
};
|
||||
|
||||
public Task InvokeMoveToPoisonAsync(QueueMessage msg)
|
||||
{
|
||||
// MoveToPoisonAsync is private — invoke via reflection. Avoids spinning up the
|
||||
@@ -288,10 +399,10 @@ public class TenantOffboardingWorkerPoisonTests
|
||||
new BlobStorageService(new BlobServiceClient("UseDevelopmentStorage=true"),
|
||||
NullLogger<BlobStorageService>.Instance, usesManagedIdentity: false),
|
||||
NullLogger<SafeWipeService>.Instance) { }
|
||||
public override Task<int> WipeByExactPartitionAsync(string t, string i, CancellationToken c = default) => Task.FromResult(0);
|
||||
public override Task<int> WipeByCompositePartitionRangeAsync(string t, string i, CancellationToken c = default) => Task.FromResult(0);
|
||||
public override Task<int> WipeByDiscriminatorAndTenantPropertyAsync(string t, string d, string i, CancellationToken c = default) => Task.FromResult(0);
|
||||
public override Task<int> WipeByTenantIdPropertyAsync(string t, string i, CancellationToken c = default) => Task.FromResult(0);
|
||||
public override Task<int> WipeBlobsByTenantPrefixAsync(string c, string i, CancellationToken ct = default) => Task.FromResult(0);
|
||||
protected override Task<int> WipeByExactPartitionCoreAsync(string t, string i, string filter, CancellationToken c) => Task.FromResult(0);
|
||||
protected override Task<int> WipeByCompositePartitionRangeCoreAsync(string t, string i, CancellationToken c) => Task.FromResult(0);
|
||||
protected override Task<int> WipeByDiscriminatorAndTenantPropertyCoreAsync(string t, string d, string i, CancellationToken c) => Task.FromResult(0);
|
||||
protected override Task<int> WipeByTenantIdPropertyCoreAsync(string t, string i, CancellationToken c) => Task.FromResult(0);
|
||||
protected override Task<int> WipeBlobsByTenantPrefixCoreAsync(string c, string i, CancellationToken ct) => Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user