Compare commits

...

186 Commits

Author SHA1 Message Date
Oliver Kieselbach d95640c597 Agent: protected DACL on the agent data directory (D-355)
%ProgramData%\AutopilotMonitor inherited ProgramData's "Users may create files and
folders" entry, so a standard user could add files to the folder the SYSTEM agent and its
boot task start from. The agent outlives the device phase of OOBE (user phase, desktop,
reboots until the enrollment ends), and the payload catalog is checked at install time
only.

- AgentDirectoryAcl.EnsureProtected: protected DACL, SYSTEM and Administrators full
  control, Users read and execute, inherited by every subfolder; idempotent, never blocks
  the agent.
- Install mode applies it after the agent directories exist; every runtime start
  re-checks it, which also reaches agents that arrived by self-update.
- Tests: protected shape, apply once then leave alone, existing subfolders take it over,
  a widened or unprotected DACL is not taken as protected, a missing folder is not created.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 19:26:44 +02:00
Oliver Kieselbach b5e9f45a46 bump pointer: internal (payload catalog lab VM result)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 18:57:58 +02:00
Oliver Kieselbach b4dabcce94 bump pointer: internal (payload catalog dev rehearsal)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 18:32:25 +02:00
Oliver Kieselbach 6bbdcc0313 bump pointer: internal (agent payload catalog plan status)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 18:09:51 +02:00
Oliver Kieselbach 283c094986 Agent payload: signed catalog, vendor check, dev channel in CI (D-354, phase A)
- build-agent.yml: input channel (release | dev; dev writes the lab blobs only and
  refuses stable/release). Every third-party PE needs its vendor's embedded Authenticode
  signature (signtool verify /pa, read-only). AutopilotMonitor.Agent.cat, a signed
  SHA-256 catalog over every payload file, is created after Authenticode and before the
  ZIP; the device check runs under Windows PowerShell 5.1 on the final ZIP.
- Agent: SelfUpdater step 5b verifies the staged payload against the catalog before any
  file is swapped (PayloadCatalogVerifier, Test-FileCatalog in-process; a rejection means
  no update, skip reason catalog_rejected). agent_started reports which bootstrap
  installed the agent, read from the bootstrap logs (BootstrapProvenanceReader).
- Payload: System.Management.Automation.dll (never loaded, the GAC copy binds first) and
  AutopilotMonitor.Shared.xml no longer ship; PDBs stay.
- Shared: CodeSigningPublisherPattern, AgentPayloadCatalogFileName;
  CodeSigningParityTests pins them against installer, loader and workflow.
- Submodules: infra (build.ps1 local only), internal (D-354, plan, docs), commands.

Installer v2.6 and the OOBE script follow once a catalog ZIP is on stable: a merged
installer change is live on the next loader run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 17:48:29 +02:00
Oliver Kieselbach 47728fbcd4 bump pointer: internal (external identities released)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 01:44:18 +02:00
Oliver Kieselbach d5f885c4b4 bump pointer: internal (external identities never claim a tenant, D-353)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 01:21:38 +02:00
Oliver Kieselbach dd4975b73e Auth: an external identity never claims a tenant (D-353)
A caller whose token shows it is not a member of the token's tenant (acct = 1, a #EXT# user name,
or an idp naming another tenant, a personal Microsoft account or an external sign-in provider) no
longer seeds DomainName/OnboardedBy or queues the activation, is never the first-user Tenant Admin,
and cannot set the activation-page address in the roleless signup window. Positive evidence only:
a token without these signals stays a member's, and tid/idp are read under their inbound-mapped
names as well. Activation promotes OnboardedBy only; the UpdatedBy fallback is gone and a stored
#EXT# UPN is refused. Explicit grants (TenantAdmins row, app role) still apply.

The once-per-interval log gate of the client-app binding becomes Helpers/LogThrottle and also
throttles the new warning, which the polling activation page would otherwise repeat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 01:19:56 +02:00
Oliver Kieselbach 18310b05aa bump pointer: internal (MCP input bounds released)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 00:29:24 +02:00
Oliver Kieselbach 40894ed5e7 bump pointer: internal (MCP input bounds, D-352)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 00:10:02 +02:00
Oliver Kieselbach 6894e71668 MCP: bound caller-sized search input; scoring cost no longer grows with the keyword count
search_events, search_knowledge and search_docs take a query of at most 1,000
characters; search_events takes at most 20 keywords of 100 characters and
scores at most 32 distinct keywords, caller keywords first, reporting the rest
as keywordsDropped with a note. The error-code fallback takes at most 10 codes,
search_docs.section at most 100 characters.

The event scorer compiles the query once (compileConcepts): each event field is
split into words once and its words are looked up by the key of their first
three characters, so scoring an event costs the same whatever the number of
keywords. The matching rule is unchanged; a seeded differential test pins it
against the per-keyword rule. scoreEvents yields to the event loop every 256
events and stops once the caller has gone.

Every paginated tool's continuation goes through one ContinuationSchema
(at most 4,096 characters), and buildQuery appends instead of set: both
URLSearchParams.set paths were quadratic in caller-sized input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 00:07:28 +02:00
Oliver Kieselbach a28fdc874f bump pointer: internal (watch at the seal + app badge released)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 22:45:24 +02:00
Oliver Kieselbach 7480a2e218 Push receiver: an app badge counts unread alerts; an open history reads them
Every stored history entry starts unread. After a new entry the worker sets the
app badge to the unread count through applyBadge, fire and forget: inside
Safari's declarative push event the setAppBadge promise never settles, so it is
never awaited and never handed to waitUntil (pushSw.test.ts pins it). The
history page marks everything read on every visible load, clears the badge,
closes the notifications whose entries the history holds and shows "New" on
what arrived since the last visit. Unpair clears the badge; the wipe push
leaves it to the next open. The payload repeats mutable inside notification,
where Safari 18.4-26.1 reads it, and carries no app_badge.

Bumps internal (plan, D-350, D-351, concept doc).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 22:22:56 +02:00
Oliver Kieselbach 96fc88462d Push: the session watch fires at the WhiteGlove seal
"Notify me when done" now also fires on the first genuine pre-provisioning
seal ("Pre-provisioning finished") on the gate of the Pre-Provisioning
Completed alert, and the watch is consumed there; watching the user phase takes
a new click. A watch placed on a sealed session lives 30 days instead of 48
hours. EventIngestProcessor.WatchOutcomeFor is the one gate, the watch text
comes from PushSessionWatchService.WatchMessage. The portal button re-reads the
watch on every status change and names what it waits for. The Success hint
says it covers a finished pre-provisioning; the receiver labels the
preprovisioning_* alerts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 22:22:44 +02:00
Oliver Kieselbach 6b86549fd0 bump pointers: internal, .claude/commands (client-cert interception released)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 18:30:15 +02:00
Oliver Kieselbach e8e350bf14 bump pointer: internal (B-k87 done)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:55:27 +02:00
Oliver Kieselbach 4e519fa4b7 Backend: the correlation-id gate anchors with \z
.NET's $ also matches before a final newline, so "7c9e6679\n" passed the allow-list that guards
the inbound X-Correlation-ID against CRLF and log forgery and was echoed. Such a value does not
arrive over HTTP, but the gate now holds what its comment promises; a new test case pins it
(red with $).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:55:26 +02:00
Oliver Kieselbach 52f6a177f9 bump pointers: internal, .claude/commands (client-cert interception, D-349)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:45:02 +02:00
Oliver Kieselbach caafaeafd4 Tests: the push recording handler survives the parallel fan-out
PushDeliveryService sends with Parallel.ForEachAsync, and RecordingHandler appended to plain
lists without a lock: two concurrent sends could lose a request (Delivered 2, recorded 1), so
Tenant_alert_also_reaches_platform_devices_whose_owners_home_tenant_it_is failed now and then
(once in six full runs while it was traced). A request and its body are now recorded together
under the lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:43:52 +02:00
Oliver Kieselbach bfb6c12384 Agent: an intercepted connection is no longer an auth failure
A security client that inspects TLS strips the device certificate on the way to the backend;
the platform then answers 403 "Client Certificate Required" itself, or a proxy answers with
its own page. The agent counted these like backend rejections: five in a row shut it down,
the scheduled task restarted it at every boot, and only the 48-hour emergency break removed it.

- Origin of every 401/403: only a response that echoes the request's X-Correlation-ID is the
  backend's verdict and counts toward the auth shutdown; the stopped-app page still counts;
  anything else is foreign (client_certificate_required or foreign_response) and never counts.
- Telemetry: a foreign answer keeps the batch and pauses uploads with a backoff from 30 s up to
  15 min, without a timer; collectors and the decision engine keep running, the terminal phase
  gets one upload attempt, and the agent still leaves the device when the enrollment ends.
- Distress type ClientCertificateNotReceived (9), once per process, with operation, status,
  page title and the issuer of the server certificate; both handlers observe it through a
  validation callback that returns exactly sslPolicyErrors == None.
- Registration: an intercepted start retries on at most InterceptedRegistrationRetryBoots
  further boots (config channel, default 1, agent clamp 0..5, ConfigVersion 41), then ends
  monitoring like the emergency break. The break, this give-up and the config kill signal now
  share Program.EndMonitoringAtStart.
- Evidence: telemetry_upload_blocked carries reason, operation, statusCode, pageTitle,
  serverCertIssuer and firstBlockedAtUtc; agent_started carries priorRegistration* after failed
  starts (a local network fingerprint; only whether it changed leaves the device);
  agent_metrics_snapshot counts 2xx responses without the echo.
- Backend: distress type 9, the buffer default, InterceptedRuns on the registration-failure
  summary; tests pin the correlation echo on 401/403 and the middleware's place in the pipeline.
- Web: distress labels for TPM Incompatible and Cert Not Received; config report row.
- MCP: session coverage names intercepted uploads and the failed registrations before the start.

D-349

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:42:59 +02:00
Oliver Kieselbach cd4199b1d1 bump pointer: internal (about page in the evidence-loop plan)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 00:53:02 +02:00
Oliver Kieselbach d203ceee32 About: the evidence loop and AI investigation
- What is Autopilot Monitor?: the evidence of every enrollment is recorded while it runs;
  analyze rules flag the patterns they know, and for the rest the AI assistant over the
  built-in MCP server investigates the session (clients that work with files down to the
  raw IME logs of an uploaded diagnostics package) and drafts the rule for the next one
- Key Features: "AI Investigation & Rule Authoring", full width as the seventh card
- Technology & Platform → Integrations: the MCP server (read-only)

The claims carry the same qualifiers as the landing and the docs (D-348).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 00:52:03 +02:00
Oliver Kieselbach 59c012b42e chore 2026-10-12 00:49:40 +02:00
Oliver Kieselbach 86a122f31f bump pointer: internal (evidence loop released)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 00:46:13 +02:00
Oliver Kieselbach 14af0a05cd bump pointers: internal, .claude/commands (evidence loop, D-348)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 00:37:03 +02:00
Oliver Kieselbach ee41ea80cf Landing, /ai and portal: the evidence loop
- Landing: section evidence_loop right after the stats ("Never debug the same failure
  twice." — capture, detect, investigate, learn), hero sub-line, Compare row "Unknown
  failures", capabilities tile. The terminal demo continues with the rule step: validate_rule
  and both test_analyze_rule verdicts are the real results of a dry-run against the demo's
  two sessions, anonymized like the rest; the window scrolls with the newest line.
- /ai: "Facts first. Raw logs when it matters." with the way back to the raw line; new
  section rules ("From finding to rule") with the anonymized trace of the same dry-run;
  the step card is shared with Connect.
- Portal: a failed session without any rule finding offers the AI investigation in Analysis
  and Guided Diagnosis (the debug-session prompt for clients with MCP prompts, a copyable
  full prompt for every other client). A failed session without a diagnostics package on a
  tenant without a usable upload links to the diagnostics settings. The analyze-rule editor
  tests a draft against a session on the existing dry-run route; create, save and test now
  share one rule body.
- llms.txt (evidence loop, two docs links without a redirect), about FAQ, marketing guard
  sections, three tracking events; jscpd baseline -1 for the removed payload clone.

D-348

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 00:37:03 +02:00
Oliver Kieselbach 20ae3d8c09 MCP: list the rule dry-run tools only for roles that can run them
GET /api/auth/mcp reports the caller's client-capped member role in the token's tenant
(tenantRole; reported, never a grant). The MCP server registers test_analyze_rule and
test_log_pattern only for a platform role or a Tenant Admin: their routes are
TenantAdminOrGlobalReader, so Operators and Viewers only ever got a 403. validate_rule,
author-rule and the authoring guide hand a draft to a Tenant Admin otherwise, and
investigate-failed-session and debug-session end by drafting a rule for a cause that can
recur. Catalog ratchet lowered for tenant users (51,000) and delegated callers (55,500).

D-348

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 00:37:03 +02:00
Oliver Kieselbach d0f62a6aaf web(geo): sign-in before the loading card on Geographic Performance
Both geo pages returned their CalculatingCard before rendering their own
ProtectedRoute. MSAL keeps the account in sessionStorage, so a new tab,
bookmark or typed URL starts signed out: the tenant scope never settled,
the fetch never started, and ProtectedRoute never mounted to fire the
login redirect. The page spun on "Calculating geographic metrics… 0s
elapsed" forever with no API call.

ProtectedRoute now wraps each page's default export, so the content only
mounts once signed in. Verified in a signed-out headless Edge: production
stays on the card, the fix redirects to login.microsoftonline.com.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 00:28:08 +02:00
Oliver Kieselbach 683142dd27 bump pointer: internal (docs meta descriptions live)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 20:24:56 +02:00
Oliver Kieselbach e3f9ad5704 bump pointer: internal (CI endpoint registry released)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 21:56:41 +02:00
Oliver Kieselbach 0f98d9ca17 CodeQL: ignore action-pins.yml like ci.yml does
The workflow gates file was missing from both paths-ignore lists, so every gate
change re-ran the full CodeQL analysis for nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 21:56:40 +02:00
Oliver Kieselbach bfe345ca93 bump pointer: internal (CI endpoint registry: redirect URIs)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 21:39:50 +02:00
Oliver Kieselbach 587ed74d96 deploy-web: Entra redirect URIs from the endpoint registry
Both were repository variables holding the www origin; lib/msalConfig.ts uses them
only as the static-render fallback and window.location.origin in the browser.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 21:39:49 +02:00
Oliver Kieselbach d5222fcb93 bump pointer: infra, internal (CI endpoint registry: D-347)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 21:32:38 +02:00
Oliver Kieselbach 8d24c5680b CI: one endpoint registry for every host the workflows use
.github/scripts/Get-Endpoints.ps1 defines the storage account (blob and table
derived from its name), the containers, the AdminConfiguration row, the legacy
mirror, the download alias, API, MCP, portal, www, docs and the timestamp
server. Workflows load it once per job; the script publisher and the local
release build call it; the dev-render anchors are built from the alias entry.

deploy-web takes NEXT_PUBLIC_API_BASE_URL and NEXT_PUBLIC_PLATFORM_STATS_MANIFEST_URL
from the registry instead of repository variables and refuses an empty value.
A new Workflow gates job fails on any host spelled out in a workflow or CI
helper; the enforced list is derived from the registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 21:32:36 +02:00
Oliver Kieselbach 818f5f1091 bump pointer: internal (CI credential zones verified)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:56:56 +02:00
Oliver Kieselbach 485bda3658 bump pointer: internal (CI credential zones: 2b on 2026-10-19, B-2s7 done)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:44:56 +02:00
Oliver Kieselbach 0b82faa7bd bump pointer: internal (CI credential zones: D-346, deploy identity on environments)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:21:58 +02:00
Oliver Kieselbach c26542776e CI: credential-free build jobs, trust jobs that never build
Every job that runs third-party code (restore, build, tests, npm ci, next build,
wix) now holds only a read-only token and hands its output on as one archive.
Signing, publishing and deploying happen in separate jobs on a fresh runner that
run only this repository's scripts and pinned actions, unpack artifacts outside
the checkout and take no build-job output into a script.

- build-agent: reserve -> build -> release
- build-msi: sign-loader -> build -> release
- deploy-backend: reserve -> build -> deploy (publish output as one tar.gz)
- deploy-web: build -> deploy (SWA token only, no id-token) -> record
- combine-rules: validate (read-only, PRs too) -> publish (push to main only)
- deploy-mcp: same shape; documents docker build as its boundary

Deploy-only jobs run in the production environment. The jobs that sign stay
without an environment for now: a job carries one OIDC subject, and the signing
identity still trusts the ref. oidc-login-test.yml is removed; combine-rules no
longer triggers on the dev branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:21:04 +02:00
Oliver Kieselbach 80cd6c52f4 bump pointer: internal (B-vky)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 19:46:09 +02:00
Oliver Kieselbach e40471e61a Agent release: run all agent test suites before the release build
build-agent.yml tested only DecisionCore (648 of 4,154 agent tests). It now runs
the DecisionCore, Agent.V2.Core and SummaryDialog suites before building the
release binaries, with the reserved build number.

The order matters: Agent.V2.Core.Tests references the agent exe, so its build
compiles the agent. Run after the release build or without BUILD_NUMBER, it
rewrites the exe with another version (a fresh checkout stamped 2.0.1 in a local
simulation). Run first with the number, it stamps the release version and the
Build Agent step stays an up-to-date no-op (same FileVersion, same write time).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 19:39:01 +02:00
Oliver Kieselbach 7908d54999 bump pointer: internal (dead-code and duplicate gates released)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 19:17:04 +02:00
dependabot[bot] 51aaa2e80a chore(deps): bump the mcp group (#183)
Bumps the mcp group in /src/McpServer/autopilot-monitor-mcp with 6 updates:

| Package | From | To |
| --- | --- | --- |
| [@modelcontextprotocol/node](https://github.com/modelcontextprotocol/typescript-sdk) | `2.1.0` | `2.1.1` |
| [@modelcontextprotocol/server](https://github.com/modelcontextprotocol/typescript-sdk) | `2.2.0` | `2.3.0` |
| [@modelcontextprotocol/client](https://github.com/modelcontextprotocol/typescript-sdk) | `2.2.0` | `2.3.0` |
| [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) | `22.20.4` | `22.20.5` |
| [knip](https://github.com/webpro-nl/knip/tree/HEAD/packages/knip) | `6.37.0` | `6.39.0` |
| [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/typescript-eslint) | `8.65.0` | `8.71.0` |


Updates `@modelcontextprotocol/node` from 2.1.0 to 2.1.1
- [Release notes](https://github.com/modelcontextprotocol/typescript-sdk/releases)
- [Commits](https://github.com/modelcontextprotocol/typescript-sdk/compare/@modelcontextprotocol/node@2.1.0...@modelcontextprotocol/node@2.1.1)

Updates `@modelcontextprotocol/server` from 2.2.0 to 2.3.0
- [Release notes](https://github.com/modelcontextprotocol/typescript-sdk/releases)
- [Commits](https://github.com/modelcontextprotocol/typescript-sdk/compare/@modelcontextprotocol/server@2.2.0...@modelcontextprotocol/server@2.3.0)

Updates `@modelcontextprotocol/client` from 2.2.0 to 2.3.0
- [Release notes](https://github.com/modelcontextprotocol/typescript-sdk/releases)
- [Commits](https://github.com/modelcontextprotocol/typescript-sdk/compare/@modelcontextprotocol/client@2.2.0...@modelcontextprotocol/client@2.3.0)

Updates `@types/node` from 22.20.4 to 22.20.5
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

Updates `knip` from 6.37.0 to 6.39.0
- [Release notes](https://github.com/webpro-nl/knip/releases)
- [Commits](https://github.com/webpro-nl/knip/commits/knip@6.39.0/packages/knip)

Updates `typescript-eslint` from 8.65.0 to 8.71.0
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/typescript-eslint/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v8.71.0/packages/typescript-eslint)

---
updated-dependencies:
- dependency-name: "@modelcontextprotocol/node"
  dependency-version: 2.1.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: mcp
- dependency-name: "@modelcontextprotocol/server"
  dependency-version: 2.3.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: mcp
- dependency-name: "@modelcontextprotocol/client"
  dependency-version: 2.3.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: mcp
- dependency-name: "@types/node"
  dependency-version: 22.20.5
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: mcp
- dependency-name: knip
  dependency-version: 6.39.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: mcp
- dependency-name: typescript-eslint
  dependency-version: 8.71.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: mcp
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-10 18:59:41 +02:00
dependabot[bot] 0a6779cd26 chore(deps-dev): bump the web group (#182)
Bumps the web group in /src/Web/autopilot-monitor-web with 2 updates: [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) and [knip](https://github.com/webpro-nl/knip/tree/HEAD/packages/knip).


Updates `@types/node` from 22.20.4 to 22.20.5
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

Updates `knip` from 6.37.0 to 6.39.0
- [Release notes](https://github.com/webpro-nl/knip/releases)
- [Commits](https://github.com/webpro-nl/knip/commits/knip@6.39.0/packages/knip)

---
updated-dependencies:
- dependency-name: "@types/node"
  dependency-version: 22.20.5
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: web
- dependency-name: knip
  dependency-version: 6.39.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: web
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-10 18:59:27 +02:00
dependabot[bot] 1a1c2b413e chore(deps-dev): bump jscpd (#181)
Bumps the quality-tooling group in /tools/quality with 1 update: [jscpd](https://github.com/kucherenko/jscpd/tree/HEAD/rust/jscpd).


Updates `jscpd` from 5.3.0 to 5.4.0
- [Release notes](https://github.com/kucherenko/jscpd/releases)
- [Commits](https://github.com/kucherenko/jscpd/commits/v5.4.0/rust/jscpd)

---
updated-dependencies:
- dependency-name: jscpd
  dependency-version: 5.4.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: quality-tooling
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-10 18:59:15 +02:00
Oliver Kieselbach 5e763375b4 bump pointer: internal, .claude/commands (dead-code and duplicate gates built: D-343..D-345)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 18:33:20 +02:00
Oliver Kieselbach 9f4f456dc5 CLAUDE.md: search for an existing helper by behaviour; run the dead-code and duplicate gates
Names the helper homes (web lib/, backend Functions/Helpers/) and asks for a
search by what a helper does before writing a new one, which no tool can judge.
Lists the local commands for the new gates, since CI only reports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 18:10:32 +02:00
Oliver Kieselbach af2f2c7abe CI: duplicate-code check over src/ with a baseline that only shrinks
tools/quality runs jscpd (5.3.0, pinned with a lockfile like rules/scripts)
over the C# and TypeScript sources, tests and generated files excluded, at 100
tokens. The 95 existing clones are frozen in jscpd-baseline.json. A clone that
is not in the baseline fails, and so does a baseline entry whose clone is gone:
jscpd only reports missing fingerprints, and a stale one would let the same
copy come back unnoticed.

jscpd scans an LF-normalized copy of the tracked and new files under src/,
because with CRLF in a Windows checkout some JSX clones hash differently than
on the Linux runner; local runs and CI now see the same 95 clones.

At 70 tokens a sixth of all clones were identical using blocks, hence 100.
Gate proven both ways with an untracked probe file: a third copy of an existing
helper fails with its location, and a baseline entry without its clone fails.

New CI job "Duplicate code"; Dependabot watches tools/quality with the usual
7-day cooldown. Locally: npm run dup (check), npm run dup:baseline (shrink).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 18:10:07 +02:00
Oliver Kieselbach 98d87a8579 Build: unused private members, unread fields and unused parameters fail the .NET build
The root .editorconfig raises IDE0051, IDE0052 and IDE0060 (non-public
methods) to warning, and Directory.Build.props sets EnforceCodeStyleInBuild,
so the warning freeze turns them into build errors in every project, the net48
agent included.

Cleanup to start at zero, each finding checked for leftover vs. missing
behaviour (none was a feature that silently never ran):
- 38 injected dependencies nobody read: loggers, configuration, and services
  whose last read had moved to a live replacement (the IME reseed writes
  through the rule repository, the offboarding poison OpsEvent comes from the
  handler, the vulnerability gate sits in the correlate handler). Removed with
  their constructor parameters and the argument at every construction site.
- unused private members: SelfUpdater.IsNewerVersion, a GatherRuleExecutor
  wrapper, test constants and an unused test overload.
  RjPhaseCompletedFirstDeployment now replaces the two literal 110s it names.
- unused parameters removed together with their arguments; test fakes bound as
  method groups use discards.
- the commented-out backend-URL connectivity check and its plumbing are gone;
  the reason the backend stays out remains as a comment.
- DiagnosticsPackageService names the blob in its upload-failure warnings, as
  its doc already said.
- stale docs fixed on the way: OnboardedAt no longer claims a maintenance
  backfill (shared manifest and wire types regenerated), the vulnerability
  functions' policy comments match the catalog, a misplaced summary moved to
  its method.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 18:03:00 +02:00
Oliver Kieselbach 0709ce2f76 Web, MCP: knip gates unused files, exports and dependencies
knip (6.37.0) runs in both TypeScript projects as its own CI step
(npm run knip) next to ESLint. It builds the module graph, so "nothing
imports this" is checked against every reference instead of a search.
Generated files stay out of scope (lib/generated, src/generated); the
MCP's Dockerfile-run precompute and its manual scripts are entries, and so
is the web's compile-time manifest check.

Cleanup to start at zero:
- web: four orphaned components (AdminPageSections, SettingsMenu, UserMenu
  (the navbar renders its own menu) and MarkFailedModal), 17 unused sidebar
  icons, four unused config constants (the agent-download host allowlist lost
  its only reader with the /go route in July), the push device-status union
  that existed twice and was used nowhere, an uncalled push error helper,
  unused barrel re-exports, default exports duplicating named ones, four
  superseded DTO interfaces, and a JSDoc type import of an undeclared package.
- mcp: two test helpers nothing calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 16:56:23 +02:00
Oliver Kieselbach 69d876b0a6 MCP: lint and type-check tests and scripts like the web app
The MCP server had no ESLint, and since its tests left the tsconfig (to keep
them out of dist/) nothing type-checked them; scripts/ never was. tsconfig.json
now type-checks sources, tests and scripts (noEmit); tsconfig.build.json emits
the same dist/ as before (byte-identical) for npm run build and the Docker
image. ESLint (the web's versions: eslint 9.39.5, typescript-eslint 8.65.0)
runs the recommended rules plus the typed promise rules, and CI's mcp job runs
it with --max-warnings 0.

Findings fixed: 16 type errors in tests (a fixture missing three wire fields,
unknown catch results, untyped mocks); gracefulShutdown was async without
awaiting anything, so the signal handlers returned a promise; two dead imports;
a Function cast replaced by the exact pipeline() signature; an unused test
variable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 16:42:13 +02:00
Oliver Kieselbach 224eb668a1 Web: helpers live in lib/, generated files in lib/generated/; utils/ is gone
The web app kept helpers in two root folders (lib/ and utils/) and named
route-local helper folders either utils/ or lib/. Shared helpers now live in
lib/, route-only helpers in a lib/ next to their route (app/dashboard/lib,
app/sessions/lib). Generated wire types, manifests, guardrails, error codes and
the lastmod file move to lib/generated/, like the MCP server's src/generated/.
The session-search parity cases sit next to sessionSearchQuery.ts.

Pure move, no behaviour change. Imports, the generators (combine.js,
generate-shared-manifest-types.js, sync-error-codes.js, generate-lastmod.js),
the CI drift paths, the two backend tests and the MCP freshness test that read
these files, and the guard tests' registry path and scan list follow the new
locations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 16:29:57 +02:00
Oliver Kieselbach a6980a93f6 bump pointer: internal (notification channel health released)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 16:06:37 +02:00
Oliver Kieselbach eb57980b53 bump pointer: internal, .claude/commands (channel health orphan sweep)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 15:49:28 +02:00
Oliver Kieselbach c4c2415de8 Notification channel health: the maintenance run deletes the rows of deleted channels (D-342)
A health row goes once its channel is no longer in the stored channel list of its scope and it
saw no send for 30 days. The channel lists are read strictly, so a failed read skips the scope
instead of reading as "no channels"; the delete is ETag-conditional, so a row a send just
rewrote survives. Runs on the 2-hourly maintenance timer next to the push retention.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 15:49:27 +02:00
Oliver Kieselbach 9331366025 bump pointer: internal, .claude/commands (notification channel health: D-342, marker)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 15:23:44 +02:00
Oliver Kieselbach 85a0cb3c31 Notification channels: per-channel delivery status and a bell when a channel keeps failing (D-342)
Every non-Push send, real or test, is recorded in a NotificationChannelHealth row per channel
(own table, server-side status rule, destination fingerprint resets the history). The settings
show a status dot per channel: Operating normally, Failure rate x%, Error with the cause, or No
deliveries yet. Two failed deliveries in a row raise one bell for the tenant admins (or the GA
bell for ops channels) per failing episode; a failing channel keeps receiving deliveries.

Each transport now has one send path that reports its outcome (WebhookTestResult is
NotificationSendResult, the dispatcher's test entry is SendTestAsync); failures are logged with
tenant and channel id. New routes GET config/{tenantId}/notification-channel-health
(TenantAdminOrGA) and GET global/config/ops-channel-health (GlobalAdminOnly).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 15:23:43 +02:00
Oliver Kieselbach 5efa7d078c bump pointer: internal (global fan-out released, first measurements)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-10 14:18:18 +02:00
Oliver Kieselbach 35d1445ebb bump pointer: internal, .claude/commands (global fan-out built: D-340/D-341, health markers)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-10 14:01:30 +02:00
Oliver Kieselbach 23cd9050ea Global fan-out: session-derived cross-tenant reads run over enrolled tenants, GA follow-up pages fetch 100 (D-340)
- TableStorageService.GetEnrolledTenantIdsCachedAsync: configured tenants with a PlatformStats counter row (RowKey 'current', TotalEnrollments > 0), 60 s single-flight; fail-open to the configured list with a Warning when the counter scan fails
- Session list pages, cross-tenant stats drain, geo window and app-summary snapshot fan out over that subset (353 -> 127 partition queries per page live); fresh-install fallbacks key on the configured list, a configured set nobody enrolled in is an empty page; audit logs, QuerySessionsByDateRangeAsync and maintenance keep the full list
- Web: cross-tenant follow-up pages fetch max(pageSize, 100) (resolveSessionsFetchPageSize); first page and own-tenant list unchanged
- Tests: CrossTenantFanOutTests (enrolled subset, counter 0 / no row, fail-open, unconfigured counter row, empty enrolled set never the legacy scan, bound after the filter, geo + apps), DelegatedBoundedAggregateTests harness, sessionsPageSize.test.ts

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-10 14:00:24 +02:00
Oliver Kieselbach 8615fb458f bump pointer: internal (global fan-out audit for B-gdd)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-10 13:20:03 +02:00
Oliver Kieselbach 645f747191 bump pointer: internal (orphan sweep released, first measurements)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-10 00:42:32 +02:00
Oliver Kieselbach 5934ae9fc4 bump pointer: internal, .claude/commands (orphan sweep built: D-339, concept doc, health markers)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-09 22:56:30 +02:00
Oliver Kieselbach 3d10011460 Orphan sessions: ingest refuses sessions without a row, the sweep becomes their cascade in its own function (D-339)
A session without a Sessions row can never be reached by the cascade, so every row written for it was an orphan by construction. Three layers now keep the platform clean:

- Prevent: IngestTelemetry answers a batch for an unknown session with 410 (same status and body as the cascade lock; request-row marker IngestRefusal = session_unknown | cascade_locked). The derived writers - vulnerability correlation, analyze, manual rescan - stop on SessionDeletionGuard.GetLivenessAsync != Live. The agent treats every 410 as terminal: one TelemetryRejectedSessionUnknown report (exempt from the reporter cooldown), then terminate_session with forced self-destruct (origin session_gone, no final drain, no diagnostics upload); spool items of a foreign session id are dropped at startup and never terminate the current session.
- Detect: OrphanSessionSweepFunction (every 4h at :45, own lease) diffs the Sessions keys against the EventSessionIndex handles - two range drains instead of ~16.8k Sessions point reads per 2h maintenance run - and re-checks every candidate (handle unchanged, row still absent, tombstone) moments before deleting. The in-maintenance sweep raced the retention cascade on their shared 00:00/12:00 tick and reported false orphans; it is gone.
- Clean: every table of the cascade manifest minus the tombstone pair (ratchet in TableLifecycleBucketTests), the inventory counters with the stamp on the handle (claim, then decrement), the handle last with If-Match. A weekly read-only reconciliation of EventTypeIndex against the Sessions keys (Sunday mornings UTC, and on every manual maintenance run) reports residue.

MaintenanceCompleted now carries stepsMs. MCP search_sessions_by_event auto-exhausts empty-but-continuable pages. Manifests regenerated for the new ops event types and AdminConfiguration.OrphanDeepCheckLastRunUtc.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-09 22:55:27 +02:00
Oliver Kieselbach edad842a0b bump pointer: internal (inventory-key-control-chars released)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 22:06:17 +02:00
Oliver Kieselbach be239b74b8 bump pointer: internal (lessons: analysis vs build plan)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 21:35:16 +02:00
Oliver Kieselbach 4fee61bf68 bump pointer: internal, .claude/commands (D-338: inventory keys, fail-closed reads, audit/ops analysis)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 21:34:20 +02:00
Oliver Kieselbach e8b8a8de0c Remove customer identifiers from comments, tests and a fixture (D-338)
Customer domains, tenant-ID fragments, two full customer tenant GUIDs and
four real session GUIDs had found their way into code comments, test
constants and a fixture comment. They are replaced by neutral descriptions
("field case", "kiosk tenant") and synthetic values; no behaviour changes.
A check of every onboarded tenant's domain and ID prefix against the tracked
files finds no customer identifier left.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 21:34:07 +02:00
Oliver Kieselbach c287cb3eae Backend: retention and device-history reads fail closed (D-338)
- The retention fanout reads the tenant configuration strictly. A storage
  failure used to yield the synthesized default (90 days), which deletes
  sessions and prunes rows a Pro-365 or never-delete tenant keeps. Now the
  tenant is skipped and counted: tenantsFailed on
  SessionDeletionMaintenanceCompleted, which turns Warning. A row that
  vanished since the tenant listing is skipped too, instead of being
  recreated with defaults.
- GetDeviceHistoryAsync throws on a read failure instead of returning "no
  history". Both writers merge into the chain they read and replace the row,
  so the old behaviour dropped every older ref. The sweep skips such devices
  and leaves the FTR aggregates untouched for that tick.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 21:33:29 +02:00
Oliver Kieselbach 182ea2e220 Backend: one table-key sanitizer that drops control characters; inventory counter failures become one ops event per run (D-338)
- TableKeySanitizer replaces ten private and inline copies of the key rule
  (/, \, #, ? become _) and also removes control characters, which Table
  Storage rejects in keys and the HTTP layer rejects in a point read's URL.
  Keys without control characters are unchanged; a source ratchet forbids new
  copies. The dead UpsertSoftwareInventoryAsync is removed.
- VulnerabilityInventoryLoader hands out inventory text without control
  characters, so CPE lookup, report text, side-row identity and counter keys
  all see the same text.
- A correlation run whose SoftwareInventory counter writes or contributions
  side-row fail writes one SoftwareInventoryCounterWriteFailed ops event
  (Maintenance, Warning; counts, error type and status, up to five sample
  keys) instead of a per-key entry in the tenant audit trail. The counter work
  is awaited inside the invocation instead of a detached Task.Run.
- The new type is listed in the alert-rule catalog; shared manifest and MCP
  vocabulary regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 21:33:00 +02:00
Oliver Kieselbach a6e8fe320f Agent: read uninstall-registry strings up to the first NUL (D-338)
Some installers pad REG_SZ values with NULs. .NET Framework drops only one
terminator, so the rest rode into the normalized software identity and,
server-side, into a table key whose point read the HTTP layer rejects
("400 Invalid URL"). DisplayName, DisplayVersion, Publisher, InstallDate,
InstallLocation, UninstallString and ParentKeyName are now read the way
Windows shows them; a DisplayName of NULs only counts as empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 21:32:17 +02:00
Oliver Kieselbach e5b69325a2 bump pointer: internal (time-window-url-days: no announcement, no changelog)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 19:07:09 +02:00
Oliver Kieselbach ee1566ae20 bump pointer: internal (time-window-url-days: plan, backlog, lesson)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:59:49 +02:00
Oliver Kieselbach a675c70e2b bump pointer: internal (D-336, D-337: concept docs and decisions)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:56:44 +02:00
Oliver Kieselbach b45a7ae2ff Portal: the analytics window lives in the URL as ?days=, capped per edition (D-336)
Fleet Health, the Software hub (Installs, Vulnerabilities), app details, Geographic
Performance and its drill-down, Usage Metrics and Analyze Rules read ?days= through one hook
(useWindowDays). The URL is the only state: a preset click writes it with replaceState, the
page default stays out of it, and a link to another app on the same route refetches. Pro
reaches up to 365 days through the URL alone; the presets stay 7/30/90 and a value outside
them highlights none.

Cap: platform roles 365 in every scope, everyone else the viewed tenant's edition cap from its
feature flags (Community 90, Pro 365), read only when the URL asks for more than 90 days. The
cap never rewrites the URL. Fleet Health draws weekly bars beyond 90 days, the Analyze Rules
sparkline keeps its width and bundles days beyond 30, long windows keep their own progress
estimate. The duration-regression bell's days=35 link no longer snaps to 30.

The string presets and their six fall-through mappers are gone; Usage Metrics passes days to
the API, the Software hub and Geographic Performance share their defaults with the pages that
link back to them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:56:32 +02:00
Oliver Kieselbach b2f6b7bd7f Retention: derived statistics and the audit trail follow the tenant's retention (D-337)
First-time-right, time-attribution and rule-statistics rows lived on fixed lifetimes (180,
180 and 90 days), a storage bound reused from the never-pruned tables, never weighed against
tenant retention: a Community tenant could read first-time-right rows for days 91-180 through
the API and MCP, a Pro tenant keeping 365 days lost them after 180 and its rule statistics
after 90.

The retention fanout now prunes per tenant, before its session scan: these rows at the
tenant's effective retention (downgrade grace included), the audit trail at
max(180, retention) up to 365. The maintenance cleanups become outer bounds and platform
lifetimes in DataLifetimes: no tenant row beyond 365 days, global first-time-right rows 365,
time attribution 180, global and legacy rule statistics 90, the platform audit partition and
legacy audit rows 180. Device histories already follow their sessions and stay as they are.

The device-journeys window goes to 365 days (route cap and MCP get_device_history). The
SessionDeletionMaintenanceCompleted ops event carries tenantRowsPruned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:56:12 +02:00
Oliver Kieselbach bd064b1164 Config: retention validation sees Pro conferred by a managing tenant
A tenant that is Pro only through its managing organization ("Pro (MSP)") could not save a
data retention above 90 days: the patch and revert paths validated against the raw table row,
which never carries the read-time projection, so the edition resolved to Community while the
feature flags and the settings slider offered 365.

The validator now gets a projected copy of the stored row. The row itself stays
projection-free, because the exactly-these-fields verify diffs against it; a projected row
would read as drift and roll every save back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:55:52 +02:00
Oliver Kieselbach a3f98abcb5 bump pointer: internal (ux-insights follow-ups released, first measurement)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 17:01:46 +02:00
Oliver Kieselbach fb4ba57a7a bump pointer: internal (ux-insights follow-ups: E4 docs commit prepared)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 15:53:43 +02:00
Oliver Kieselbach 51200702c9 bump pointer: internal, .claude/commands (ux-insights follow-ups: D-335, concept doc, plan, skill rows)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 15:49:02 +02:00
Oliver Kieselbach d093be3e8d SignalR: a tab hidden for four minutes parks its connection and reconnects the moment it is back (D-335)
Chrome and Edge run the timers of a tab hidden for five minutes once a minute; the client then misses
its keep-alive, the service closes the connection and the automatic reconnect restores it, a loop every
minute in every background tab. lib/signalrPark.ts, a pure state machine tested with an injected clock,
parks the shared connection after four minutes hidden and resumes it on the first visible signal. The
resume ends a gap, so every view re-reads through useSignalRResync. A failed resume retries with backoff
up to 30 s until the next park; no park starts during an accessRevoked restart. Telemetry:
signalr_parked { hiddenMs } and signalr_resumed { parkedMs, transport }; park and resume emit neither
signalr_disconnected nor signalr_connected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 15:47:35 +02:00
Oliver Kieselbach 4868a4448d SignalR: every view re-reads after every connection gap, through one mechanism
Pushes only announce changes, so after a gap a view has to check the backend. Only some did: the
bells re-fetched from connection.onreconnected (handlers piled up, nothing after a restart), the
Progress Portal never re-read (a page left open across the end of an enrollment kept its last
state), and the session page re-read only its events. The provider now counts the ends of gaps
(a reconnect after its rejoin, a start of a connection that was up before) in resyncEpoch, and
useSignalRResync(onResync) fires on each once the group joins of that moment have settled, so a
push sent between a re-read and its rejoin cannot get lost; a joinGroup that finds a join of the
same group in flight now awaits it. Bells, Progress Portal (summary and events), session page
(session, analysis, vulnerability report; events keep the join catch-up), diagnosis page,
dashboard list, dashboard stats and fleet health use it; the wasConnectedRef checks are gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 15:17:49 +02:00
Oliver Kieselbach 03a8bf92c9 App names and serial numbers travel as query values: app details for names with '/' open
The host decodes %2F to a path separator before routing, so apps/{appName}/analytics|sessions and
their global twins answered 404 for every app whose name contains '/', before any function ran;
a name with a literal escape such as '%20' was decoded a second time. The routes are now
apps/analytics, apps/sessions, global/apps/analytics and global/apps/sessions with ?appName=, and
the unblock route is DELETE devices/block?serialNumber=&tenantId= (serials may contain '/' too).
Function names, policies and tenant scoping stay; req.Query decodes exactly once. Tests run names
with '/', '%', '+', a space and a literal escape through the catalog and the handlers, and the web
builders put the name into the query. Manifest and wire types regenerated (doc text only).
Rollout: backend, then web right after; an app-detail tab on the old bundle 404s until reloaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 15:17:47 +02:00
Oliver Kieselbach 7265052936 global_search telemetry carries the shape of the query, never its text
The typeahead event sent the raw search text (serial numbers, device names) as a custom
dimension. It now sends queryLength and resultCount as measurements, like session_searched, plus
guidShaped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 15:17:37 +02:00
Oliver Kieselbach 0c01f3ed76 Session links: a member without a role gets a notice instead of an empty session page
A member without a tenant role who opened a shared session link landed on a session page whose
every read answered 403: an empty page that looked like a valid session without data, its two
error toasts in a bell this user's navbar does not show. ProtectedRoute takes a new opt-in
requireTenantReadScope (the Progress Portal hint's definition of "no role"); the session page sets
it on all three branches and keeps its data hooks inert once auth/me shows no read scope (before
that nothing changes, so members are never delayed). The full-page notice says whom to ask, links
the Progress Portal and the docs article, and reports tenant_role_missing_shown { path } once per
mount.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 15:17:25 +02:00
Oliver Kieselbach caa6952ae7 Push: a Global Admin's platform device also receives the alerts of the owner's home tenant (D-334)
A receiver holds one pairing, so a Global Admin whose phone is paired in the platform scope
could not get their own tenant's enrollment alerts without replacing that pairing. The push
transport now resolves a tenant channel's recipients as the tenant's Active devices plus the
Active platform devices whose OwnerHomeTenantId is that tenant, filtered by the platform
eligibility rule and deduplicated by endpoint; no other tenant reaches a platform device, there
is no opt-in and no setting. The home tenant's device list shows such a row with
PushDeviceDto.Scope = "platform" (manifest and wire types regenerated); the panel marks it with
a Platform badge and no actions — it is managed under the platform's device list, and the
tenant routes do not know the id. Tests: fan-out incl. a foreign home tenant, a former Global
Admin and a shared endpoint; the list in the home tenant, the owner's and an operator's view,
and the refusing tenant routes.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 23:49:40 +02:00
Oliver Kieselbach 6bc503f670 bump pointer: internal (push plan: Firefox interop)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 22:29:40 +02:00
Oliver Kieselbach 87c5446cde bump pointer: internal (push plan: Windows/Edge interop)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 21:55:00 +02:00
Oliver Kieselbach cb9b34a7aa bump pointer: internal (push plan: iPhone interop complete)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 21:33:34 +02:00
Oliver Kieselbach 2bb3dcd69e Push status page: Diagnostics collapsed by default, collected when opened
The section is for troubleshooting, not for every visit: a disclosure button in the heading
opens it, the diagnostics are collected the first time it opens (not on page load), Refresh
shows only while open, and a one-line note under the import button says what it does.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 21:22:52 +02:00
Oliver Kieselbach 36aeec1b45 Push: a system message's tag is its id; the receiver checks for a worker update on every open
Every session-watch push carried the tag "session_watch" (SystemMessage set Tag = type), so a
browser that honours tags — Edge on the Windows device, iOS for notifications the worker shows
itself — replaced an earlier completion with the next one; two watched sessions finishing
minutes apart (18:38 and 18:40 UTC today) would have shown as one. The tag is now the payload
id: a test, a pairing or a session watch never replaces another notification. Alerts keep
their per-session tag, and the history is keyed by id and keeps every push either way.

The iPhone kept running the worker without the navigate fix through two opens: a home-screen
app that is only resumed never navigates, and the browser checks for a new worker on
navigation. reconcileDevice now calls registration.update() on every open (fire-and-forget).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 21:08:21 +02:00
Oliver Kieselbach 033781d083 Push receiver: notification options always carry a navigate URL (WebKit declarative push rule)
The worker trace named the error: "TypeError: Call to showNotification() while handling a
push event did not include NotificationOptions that specify a valid defaultAction url".
WebKit (ServiceWorkerRegistration.cpp) rejects showNotification() inside a declarative push
event unless the options carry a valid navigate URL; a notification shown with one is not
displayed directly but replaces the proposed notification when the event ends. Every
displayed notification on the iPhone therefore came from the platform, with data null, which
is why the on-open import recognised none of them.

toNotificationOptions now sets navigate: the payload's deep link, else the history URL on
the worker's origin (opened on the entry); the generic fallback carries the history URL too.
Trace values may be 240 characters so an error text is readable in full. Tests pin the
member on the entry, the generic and the fallback call.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 20:44:14 +02:00
Oliver Kieselbach 8a733be369 Push receiver: show() never rejects; diagnostics describe each displayed notification
The first worker trace from the iPhone read "push · notification · session_watch · added ·
display failed": the worker ran and wrote the entry, but both showNotification calls threw —
on iOS the platform shows the proposed notification itself inside a declarative push event.
In the old code show()'s rejection made the event's waitUntil reject at once, with the history
write still in flight; that is the plausible cause of the entries lost today. show() now
resolves to "shown", "fallback" or "failed: <error>" and never rejects (regression test with a
throwing showNotification).

The same trace showed all six displayed notifications as "not ours": getNotifications() on iOS
hands them back without a data object the entry rule recognises, so the on-open import cannot
identify them. The Diagnostics list now shows tag, data shape and timestamp per notification
so an import without data can be designed from evidence; a data member that arrives as a JSON
string is read as JSON.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 20:16:41 +02:00
Oliver Kieselbach 91251ac42a Push receiver: worker trace and a Diagnostics section on the status page
Two session-watch pushes reached the iPhone's lock screen today (PushDelivery 201, the same
payload shape as the test pushes that do arrive) and neither reached the history, nor did the
import of displayed notifications on open find anything. The device holds the evidence, so the
worker now records what it did: one record per push (which event member carried the payload,
type, id, outcome of the history write, how the notification was shown), per click, per
activation and per subscription repair — in meta `workerTrace`, 25 records, values capped,
written inside the same bounded budget as the history write (a database that never answers
leaves no record, which is itself the finding). The status page shows it under Diagnostics:
worker state, notifications still displayed, history rows, the trace, and a manual "Import
displayed notifications" with its count. Nothing leaves the device.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 19:55:18 +02:00
Oliver Kieselbach 2a03bebc58 bump pointer: internal (push plan: history self-healing)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 19:32:23 +02:00
Oliver Kieselbach 2e6fa5ac74 Push receiver: import displayed notifications on open; pull-to-refresh listens on the window
A session-watch push showed on the iPhone but never reached the history, and a pull on the
empty part of the page did nothing. (1) The history now heals itself: on every load the page
asks the service-worker registration for the notifications still in the notification centre
and writes the ones it lacks (entryFromNotification, the rule the click handler already used,
now in the worker core) — a push the worker could not persist is caught up as long as its
notification has not been cleared. (2) The pull gesture is registered on the window (passive),
not on the content wrapper, so a pull that starts below the list counts; the wrapper keeps a
minimum height. Tests pin the entry rule.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 19:28:28 +02:00
Oliver Kieselbach a61a50cd53 bump pointer: internal, .claude/commands (Terms + DPA dialog)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 18:26:12 +02:00
Oliver Kieselbach 1271658b16 Terms + DPA: a new tenant whose first sign-in skipped Get started confirms them once in the portal
- Get started keeps its tick as the acceptance: the CTA hands ?signupConsent=1 to portal (kept per
  tab for 30 minutes, removed from the address bar at once), the first auth/me forwards it, and the
  onboarding write records DpaVersion together with DpaAcceptedBy (first-login UPN) and
  DpaAcceptedAt.
- Any other first sign-in (the navbar "Sign in") sets DpaAcceptancePending on the new row instead.
  auth/me carries the flag; ProtectedRoute renders a blocking dialog with the same sentence and
  links as Get started (shared TermsDpaAgreementText) instead of the page; POST
  auth/dpa-acceptance writes the acceptance for the caller's own tenant only, a no-op 204 when
  nothing is pending.
- Tenants onboarded before this carry no flag and are never asked. Activation is unchanged; the
  dialog appears after it.
- The three new fields are system-owned like OnboardedBy (patch deny-list, revert-protected) and
  projected by config/all, the GA tenant report and the MCP tenant list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 18:23:42 +02:00
Oliver Kieselbach 5485f75620 bump pointer: internal (e-mail wording and deliverability notes)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 18:19:59 +02:00
Oliver Kieselbach 609fcbacaf Welcome and farewell wording: subject with the domain, calmer copy, one feedback link
Both mails were landing in Junk at some recipients although the domain's SPF, DKIM and DMARC
are in order, so the copy loses its marketing markers: the welcome subject names the tenant
("Your Autopilot Monitor access for {domain} is ready", no exclamation mark; the editor shows
it with the domain placeholder), the heading drops its exclamation mark, "Great news" and
"incredibly valuable ... don't hesitate" give way to plain sentences, the closing line is a
thank-you, and both templates keep GitHub as their only feedback link (the personal LinkedIn
link, a second foreign link domain, is gone). User go for the customer-visible wording.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 18:19:58 +02:00
Oliver Kieselbach 7b7d35f428 Welcome and farewell mails: own text part and Auto-Submitted header (junk-folder work, part 2)
Both template mails also land in Junk at some recipients. The templates are already image-free
and inline-styled, so the remaining technical signals were the missing text/plain part (the
provider derived one) and the missing automation header. HtmlToText renders the effective
template (default or operator override) as the text part — head, style, script and comments
dropped, blocks and list items as lines, links with their URL, entities decoded — and
EmailService sends every mail with Auto-Submitted: auto-generated / X-Auto-Response-Suppress,
shared with the alert mails. Wording changes to the customer-visible templates are deliberately
not part of this commit.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 18:09:57 +02:00
Oliver Kieselbach a5a1039d22 E-mail alerts read like messages, not like log lines (junk-folder fix)
The first channel mail landed in Junk although SPF, DKIM (mte1/mte2) and DMARC are in order,
so the content was the tell: a bracketed, shouting subject built from the machine title
"Ops Alert: Platform/PushDeliveryFailed", a raw JSON block, hardly any prose, no text part.
Now: subject "Autopilot Monitor: Push delivery failed (Platform)" (event name as words, no
tags, no severity word), one introductory sentence naming the severity and category, the
event as a heading, facts and the opted-in payload as key/value tables (nested values as
compact JSON, non-object payloads still capped), a visible portal link, a footer that says
why the mail was sent, a plain-text part of our own (auto_text off for alerts) and the
RFC 3834 Auto-Submitted header. Template mails are unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 17:59:25 +02:00
Oliver Kieselbach 9d50ff8901 bump pointer: .claude/commands (EmailDelivery status dimensions)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 17:46:55 +02:00
Oliver Kieselbach 0ce59b700c E-mail delivery: the provider status word and message id travel into the test result and the EmailDelivery event
A test mail that Mandrill accepted did not arrive; the backend had logged nothing beyond
"accepted". SendViaMandrillAsync now returns the status (sent/queued/rejected) and the message
id, the channel test reports them ("Provider status: queued, id ..."), and the EmailDelivery
custom event carries them as status and providerIds — enough to look a mail up in the
provider's activity log.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 17:46:54 +02:00
Oliver Kieselbach 309e15bd33 bump pointer: internal, .claude/commands (e-mail notification channel)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 17:20:38 +02:00
Oliver Kieselbach 28da10e9fb E-mail as a notification-channel provider, Global-Admin only (D-332)
WebhookProviderType.Email = 60 joins Telegram and Push behind the Global-Admin gate: it sends
under the platform's sender identity and provider key (the welcome/farewell path,
noreply@autopilotmonitor.com). The channel's Url names one to five plain recipient addresses
(ValidateEmailDestination, no SSRF URL check). EmailNotificationService renders the alert
(EmailAlertRenderer: severity-prefixed subject, escaped HTML with summary, facts, sections,
links, and the structured payload as preformatted text when the rule opted in) and sends one
message per recipient through EmailService.SendMessageAsync; outcomes land in the EmailDelivery
custom event and the log, never in an ops event (a failing e-mail channel must not feed itself).
The dispatcher takes the transport through a chained fourth-parameter constructor so the
existing mocks keep working. ChannelEditor offers the provider to Global Admins with a
"Recipients" field. Tests: routing, validation, gate, renderer, send against a provider stub.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 17:20:36 +02:00
Oliver Kieselbach 36aafb655f bump pointer: internal, .claude/commands (push-relay Phase 0: submission ops events)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 16:56:17 +02:00
Oliver Kieselbach 24ea5c76c8 Ops events next to the four direct Telegram pings (push-relay Phase 0)
TenantSignup, SessionReportSubmitted, DiagFilesReportSubmitted and RuleSubmissionReceived
(category Tenant, Info) are recorded where the backend pings the Telegram chat directly, so an
alert rule can route them to any channel — the Push channel included. Like FeedbackReceived,
message and details carry ids and a portal path only, never the submitter or a comment (the
user-id column keeps the submitter for the admin view and never reaches a channel). The ops
event is awaited before the response; the Telegram pings stay untouched until they are retired.
Dual-registered (constants, All, web rule list), vocabularies regenerated, health marker row.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 16:56:16 +02:00
Oliver Kieselbach f5117e2290 bump pointer: internal (push-relay: filter + watch availability)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 16:38:10 +02:00
Oliver Kieselbach 0cd8baa85d Push receiver: history filter; the session-watch button hides when watching is impossible
- History page: one dropdown built from what the history holds — Platform (ops alerts, one
  entry per Category fact), Enrollment (one per tenant alert kind) and This device (the
  receiver's own messages) — with counts, a "12 of 48" readout and a clear button while a
  filter is active, hidden while there is only one kind to show. The choice is stored on the
  device (meta key historyFilter); a deep link to a hidden entry shows everything. A
  customer's device holds tenant pushes only, so it never sees a Platform group.
- SessionWatchResponse.Available: GET/DELETE sessions/{id}/watch say whether this person can
  watch at all (an enabled Push channel in one of their own scopes where they hold Admin or
  Operator); the portal's "Notify me when done" renders only then, instead of letting a tenant
  admin click into "enable a Push channel first", which only a Global Admin can do. An active
  watch stays available so it can be switched off.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 16:38:09 +02:00
Oliver Kieselbach cd009071db bump pointer: internal (push-relay: history refresh note)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 16:06:12 +02:00
Oliver Kieselbach f13b823387 Push receiver: the history refreshes itself and on a pull from the top
A test push arrived as a notification while the history page was open, but the list did not
change until the user left the page and came back. The page now re-reads the local store every
5 s while it is visible, on focus and pageshow as well as on visibilitychange (the worker's
message stays the fast path), and an unchanged read keeps the list's identity so the poll never
re-renders for nothing. A pull from the top refreshes on demand — a home-screen app has no
native gesture — with a damped indicator (pull / release / refreshing) and the device reconcile
folded in; the gesture rule is a pure, tested controller.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 16:06:11 +02:00
Oliver Kieselbach 2517e8eb28 Push receiver: delete entries, user-set retention, self-migrating VAPID key (#180)
* Push receiver: delete entries, user-set retention, self-migrating VAPID key (B-y4z)

- History: a long press (or right-click) on an entry opens a small menu — Delete (local only;
  the history never leaves the device) and Open in portal when the entry carries a link.
- Retention: the status page lets the user keep the history 0-365 days (default 30 as before,
  0 = until the entry cap of 200, which is unchanged and stated); stored in the receiver's
  IndexedDB meta, honoured by the worker and the pages when pruning.
- Key rotation is a re-registration, not a re-pair campaign: GET push/device names the active
  kid and public key; a receiver whose stored kid differs subscribes again with the new key on
  its next open and PUTs the new kid (reconcile action "rekey"); a Stale vapid_key_mismatch row
  re-arms the same way. Operator texts (settings template, generator script) follow.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* bump pointer: internal, infra (push-relay addendum: delete, retention, key migration)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Push receiver: name a failed subscription repair; the worker re-subscribes with the server's active key

- reconcileDevice returns repairError when the subscribe() or the PUT after it failed (400
  InvalidSubscription, 429, network); the status page shows "present, server not updated" plus
  the reason instead of a silent "present". Retried on the next open as before.
- pushsubscriptionchange (Chrome, Firefox) asks GET push/device for the active key before it
  re-subscribes (planSubscriptionKeys: active key when it differs from the stored kid or no key
  is stored, else the stored key, else nothing), drops the browser's replacement subscription
  after a rotation, PUTs the kid it used and stores kid + key; never a PUT with an empty kid.
  Worker fetches are bounded to 10 s.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* bump pointer: internal (push-relay: worker key rule)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 15:45:33 +02:00
Oliver Kieselbach d378fabc01 Push receiver: read the declarative notification from the push event (Safari history fix) (#179)
* Push receiver: read the declarative notification from the push event (Safari history fix)

On an iPhone the pushes arrived with the right content, but every history entry was the generic
placeholder: with "mutable": true Safari (iOS >= 18.4) dispatches the regular push event with the
proposed Notification in event.notification and event.data null, and the worker read only
event.data. The push handler now takes event.notification first and shows the same content
itself (one notification, entry written at arrival); a tap on a notification the platform
displayed without the worker rebuilds the entry from the Notification's data, and the click
path's history write is bounded like the push path's. Tests pin both paths.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* bump pointer: internal (push-relay: Safari declarative-push finding)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 14:59:44 +02:00
Oliver Kieselbach be0e37008a Action pins: exact release tag in every version comment (D-331) (#178)
* Action pins: exact release tag in every version comment (D-331)

setup-node v7.1.0 and upload-artifact v7.0.2 moved the floating v7 tag, so the
"# v7" comments on our still-correct pins (v7.0.0, v7.0.1) stopped naming the
pinned commit and zizmor's ref-version-mismatch raised 11 code-scanning
alerts on main. The other five major-only comments (checkout, setup-dotnet,
cache, attest-build-provenance, functions-action) were one upstream release
away from the same alert.

All 38 major-only comments now carry the tag their unchanged SHA resolves to
(checkout v7.0.1, setup-dotnet v6.0.0, cache v6.1.0, setup-node v7.0.0,
upload-artifact v7.0.1, attest-build-provenance v4.2.2, functions-action
v1.5.7); no SHA moved, Dependabot keeps advancing SHA and comment together.
verify-pins now accepts only a vX.Y.Z comment and names the floating-major
case in its own error.

Verified: zizmor online (GH_TOKEN) reproduced the 11 findings before and
reports 0 ref-version-mismatch after, --min-severity low exits 0; the
verify-pins script passes all 51 uses lines and rejects a major-only comment,
a missing comment and a tag reference in a scratch workflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* bump pointer: internal (D-331 action pin comments)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 14:45:34 +02:00
Oliver Kieselbach 04fae4fe86 Web Push channel: alerts to paired phones without a third-party messenger (#177)
* bump pointer: internal (ingest-session-row-reads RELEASED, measurement)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Web Push channel: alerts to paired phones without a third-party messenger (D-327 to D-330)

Provider WebhookProviderType.Push = 50 in both channel scopes. It carries no destination;
NotificationChannelDispatcher takes a NotificationScope from every caller and the push
transport resolves the scope's paired devices at send time (table-backed Admin/Operator,
GlobalAdmin with matching identity binding; Viewer and Global Reader never).

The receiver never signs in: a signed-in Admin/Operator creates a one-shot pairing code
(11 Crockford chars, hashed, 10 min, ETag one-shot, five-failure burn), the phone redeems
it anonymously, the portal session confirms. The phone's only credential is the device
token in X-Push-Device-Token, good for status, re-subscribe and unpair of its own row
(10 calls per minute per device; the anonymous pairing routes 10 per minute per IP).

Payloads are RFC 8291 per device (new BCL-only library src/Push with the RFC vector pinned
byte for byte) with a VAPID key ring and the kid on every device row; the projection reads
an allow-list only (never Sections, DataJson, Failure Reason, Last Failure or the ops
message), serial masked, 2 KB. 404/410 mark a device Stale instead of deleting it and the
receiver re-arms it on its next open (the same PUT lifts a delivery-failures pause); devices
pause when their owner stops signing in (PushOwners stamp in auth/me) through one pause path
and resume by themselves; an owner without a stamp is paused, never deleted outright; role
loss revokes with a wipe push and deletes the row (there is no Revoked state). PushDevices
and PushPairingGrants are credential-bearing: raw-table deny-list, no backup, redacted
dependency rows.

Receiver app under /push/ (module service worker, Declarative Web Push shape, local history,
pairing and status pages, manifest + icons), portal UI (Push provider in the channel editor,
pairing dialog with QR code, device lists for tenant and platform scope, "Notify me when
done" on the session page — the watch row records the watcher's own scopes, so a Global
Admin watching a customer session is notified on the platform-paired phone), 22 catalogued
routes, ops events PushDeliveryFailed and PushEndpointRefused (deduplicated per instance),
maintenance sweep inside the existing run, settings template.

The tenant-scope provider stays Global-Admin-only until the customer release (same
ValidateModel gate as Telegram). Backend 7371, library 125, MCP 997, web 2061 tests.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* bump pointer: internal, .claude/commands, infra (push-relay: plan, D-327 to D-330, markers, VAPID generator)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* bump pointer: internal (push-relay: commit step ticked)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Drop sprintf-js from both lockfiles (Dependabot #114, #117)

GHSA-hp3w-g68c-fv3c (sprintf-js <= 1.1.3, denial of service through unbounded
precision specifiers) has no patched release, so both chains are cut instead.

MCP (#117): sprintf-js arrived via @huggingface/transformers -> onnxruntime-node
-> global-agent 3 -> roarr. global-agent is only loaded by onnxruntime-node's
CUDA install script, which the image never runs (npm ci --ignore-scripts). The
override global-agent ^4.1.3 drops roarr; 4.x keeps the bootstrap export that
script calls, and onnxruntime-node 1.30 depends on the same range.

Rules tooling (#114): ajv-cli 5.0.0 (no release since 2023) pulled js-yaml 3
-> argparse 1 -> sprintf-js. rules/scripts/validate.js now checks the four rule
sets against their schemas with ajv (draft 2020-12) and ajv-formats directly,
and combine-rules.yml runs it in one step instead of four ajv-cli loops. The
lockfile shrinks from 26 to 6 packages; the fast-json-patch override and the
legacy-peer-deps .npmrc (only there for ajv-cli's ts-node peer) are gone.

Verified: validate.js accepts all 149 rule files plus guardrails.json exactly
as ajv-cli did and rejects a corrupted copy (missing field, broken JSON, U+2028
in a guardrail value) with exit 1; clean npm ci + combine.js leave the
generated outputs unchanged; MCP build and 997 tests green; zizmor clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* bump pointer: internal, .claude/commands (validate.js replaces ajv-cli: B-gzv, rule-builder fallback)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Bump sharp, proxy-addr and source-map-js past fresh advisories (MCP, Web)

- sharp 0.35.4 -> 0.35.5 (GHSA-wq5f-xc86-pv6w, librsvg): the existing sharp
  override in both projects now floors at ^0.35.5 so the fixed build cannot be
  undercut by a parent range.
- proxy-addr 2.0.7 -> 2.0.8 (GHSA-jqcg-44mw-7w3h, via express; MCP only) and
  source-map-js 1.2.1 -> 1.2.2 (GHSA-68fv-2mgg-jv7q, via postcss) are
  lockfile-only bumps inside the ranges their parents declare.

No other lockfile entry moved. npm audit in the MCP server is clean.
Verified: MCP build and 997 tests green; Web tsc clean, 2061 tests green,
next build (static export) succeeds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* bump pointer: internal (push-relay: CORS verified, cert-exclusion prerequisite, EU app facts)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Web: Next.js 16.3.6 -> 16.3.8 (six advisories)

16.3.8 closes GHSA-3w37-wq28-93x7, GHSA-4jqv-mc3x-m676, GHSA-39w2-rjm5-chcv,
GHSA-f87g-xv8r-7p7x, GHSA-mcj8-r9mp-w47p and GHSA-cjq9-62q9-8jv4 (affected
16.0.0 - 16.3.7). Under output:"export" the cache, ISR and image-optimization
paths never run on the SWA; the dev-server disclosure is local-only. The patch
is taken anyway. next, eslint-config-next and @next/bundle-analyzer stay
aligned and floor at ^16.3.8; the lockfile is pinned to 16.3.8, not the 16.4.0
minor the caret would resolve to.

What npm audit still reports (braces, micromatch, chokidar, fast-glob,
postcss-selector-parser) comes through Tailwind 3 and eslint-config-next,
build-time only; braces has no fix, the rest needs Tailwind 4 (backlog).

Verified: tsc clean, eslint 0 findings, 2061 tests green, next build
(16.3.8, 102 static pages) succeeds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* bump pointer: internal (B-dzs lockfile merge note, B-hxz braces chain)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 13:48:42 +02:00
dependabot[bot] 2b29019b59 chore(deps): bump sharp (#175)
Bumps the mcp-security group with 1 update in the /src/McpServer/autopilot-monitor-mcp directory: [sharp](https://github.com/lovell/sharp).


Updates `sharp` from 0.35.4 to 0.35.5
- [Release notes](https://github.com/lovell/sharp/releases)
- [Commits](https://github.com/lovell/sharp/compare/v0.35.4...v0.35.5)

---
updated-dependencies:
- dependency-name: sharp
  dependency-version: 0.35.5
  dependency-type: indirect
  dependency-group: mcp-security
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-07 16:47:50 +02:00
dependabot[bot] aa578def0e chore(deps): bump sharp (#176)
Bumps the web-security group with 1 update in the /src/Web/autopilot-monitor-web directory: [sharp](https://github.com/lovell/sharp).


Updates `sharp` from 0.35.4 to 0.35.5
- [Release notes](https://github.com/lovell/sharp/releases)
- [Commits](https://github.com/lovell/sharp/compare/v0.35.4...v0.35.5)

---
updated-dependencies:
- dependency-name: sharp
  dependency-version: 0.35.5
  dependency-type: indirect
  dependency-group: web-security
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-07 16:47:38 +02:00
dependabot[bot] 7eceed16e1 chore(deps): bump the web-security group across 1 directory with 2 updates (#173)
Bumps the web-security group with 2 updates in the /src/Web/autopilot-monitor-web directory: [postcss-selector-parser](https://github.com/postcss/postcss-selector-parser) and [source-map-js](https://github.com/7rulnik/source-map-js).


Removes `postcss-selector-parser`

Updates `source-map-js` from 1.2.1 to 1.2.2
- [Release notes](https://github.com/7rulnik/source-map-js/releases)
- [Changelog](https://github.com/7rulnik/source-map-js/blob/main/CHANGELOG.md)
- [Commits](https://github.com/7rulnik/source-map-js/compare/v1.2.1...v1.2.2)

---
updated-dependencies:
- dependency-name: postcss-selector-parser
  dependency-version:
  dependency-type: indirect
  dependency-group: web-security
- dependency-name: source-map-js
  dependency-version: 1.2.2
  dependency-type: indirect
  dependency-group: web-security
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-07 16:02:23 +02:00
dependabot[bot] 27454d5744 chore(deps): bump the mcp-security group across 1 directory with 2 updates (#174)
Bumps the mcp-security group with 2 updates in the /src/McpServer/autopilot-monitor-mcp directory: [proxy-addr](https://github.com/jshttp/proxy-addr) and [source-map-js](https://github.com/7rulnik/source-map-js).


Updates `proxy-addr` from 2.0.7 to 2.0.8
- [Release notes](https://github.com/jshttp/proxy-addr/releases)
- [Changelog](https://github.com/jshttp/proxy-addr/blob/master/HISTORY.md)
- [Commits](https://github.com/jshttp/proxy-addr/compare/v2.0.7...v2.0.8)

Updates `source-map-js` from 1.2.1 to 1.2.2
- [Release notes](https://github.com/7rulnik/source-map-js/releases)
- [Changelog](https://github.com/7rulnik/source-map-js/blob/main/CHANGELOG.md)
- [Commits](https://github.com/7rulnik/source-map-js/compare/v1.2.1...v1.2.2)

---
updated-dependencies:
- dependency-name: proxy-addr
  dependency-version: 2.0.8
  dependency-type: indirect
  dependency-group: mcp-security
- dependency-name: source-map-js
  dependency-version: 1.2.2
  dependency-type: indirect
  dependency-group: mcp-security
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-07 16:02:09 +02:00
Oliver Kieselbach 46b8503207 bump pointer: internal (plan ingest-session-row-reads, D-325)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-07 15:33:38 +02:00
Oliver Kieselbach 50bb09eb92 Read the Sessions row projected on the agent hot path
Backend 1.5.1256 raised IngestTelemetry p50 from 66 to ~100 ms and
RegisterSession from 90 to ~110 ms. Storage metrics per API located it:
server latency of point reads unchanged, end-to-end latency of QueryEntity
alone up from 3.7 to 6.4 ms, every other API flat. The excess times the
point reads per upload is the whole regression, and the request histogram
gained a second mode 46 ms above the first. The only point-read response
that had changed was the Sessions row (~400 bytes of tracker state and
twins), which every upload read twice in full.

- SessionRowProjections: the guard row for the agent routes (Status,
  SerialNumber, owner columns) and the counter merge's slice (counters,
  anchor, index key, tracker state and twins, the fields non-terminal
  batches consume). The guard takes a select and adds its lock columns;
  the inventory reader gets a projected overload, the deletion flows keep
  their full reads.
- The counter merge returns SessionIngestSnapshot, a narrow type instead
  of a SessionSummary built from a projected row. The processor reads the
  whole row only on terminal, diagnostics and transition batches or when
  the merge returned nothing, and derives the snapshot there. The SignalR
  delta and the control signals carry the same fields as before.
- The signal-only upload reads the same slice; registration takes its
  lifecycle entry from the guard's row instead of a second read.
- SessionRowProjectionTests: a select-strict harness pins each list. The
  merge's write, index mirror and snapshot, the slice mapper, the guard
  row's readers and the owner policy come out identical whole or
  projected; a dropped column fails there (verified by mutation).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-07 15:33:37 +02:00
dependabot[bot] aaf9b74426 chore(deps-dev): bump the web group (#172)
Bumps the web group in /src/Web/autopilot-monitor-web with 2 updates: [@next/bundle-analyzer](https://github.com/vercel/next.js/tree/HEAD/packages/next-bundle-analyzer) and [eslint-config-next](https://github.com/vercel/next.js/tree/HEAD/packages/eslint-config-next).


Updates `@next/bundle-analyzer` from 16.3.5 to 16.3.6
- [Release notes](https://github.com/vercel/next.js/releases)
- [Commits](https://github.com/vercel/next.js/commits/v16.3.6/packages/next-bundle-analyzer)

Updates `eslint-config-next` from 16.3.5 to 16.3.6
- [Release notes](https://github.com/vercel/next.js/releases)
- [Commits](https://github.com/vercel/next.js/commits/v16.3.6/packages/eslint-config-next)

---
updated-dependencies:
- dependency-name: "@next/bundle-analyzer"
  dependency-version: 16.3.6
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: web
- dependency-name: eslint-config-next
  dependency-version: 16.3.6
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: web
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-06 13:57:27 +02:00
dependabot[bot] e31a5d2875 chore(deps): bump the mcp group (#171)
Bumps the mcp group in /src/McpServer/autopilot-monitor-mcp with 3 updates: [@modelcontextprotocol/node](https://github.com/modelcontextprotocol/typescript-sdk), [@modelcontextprotocol/server](https://github.com/modelcontextprotocol/typescript-sdk) and [@modelcontextprotocol/client](https://github.com/modelcontextprotocol/typescript-sdk).


Updates `@modelcontextprotocol/node` from 2.0.0 to 2.1.0
- [Release notes](https://github.com/modelcontextprotocol/typescript-sdk/releases)
- [Commits](https://github.com/modelcontextprotocol/typescript-sdk/compare/@modelcontextprotocol/node@2.0.0...@modelcontextprotocol/node@2.1.0)

Updates `@modelcontextprotocol/server` from 2.0.0 to 2.2.0
- [Release notes](https://github.com/modelcontextprotocol/typescript-sdk/releases)
- [Commits](https://github.com/modelcontextprotocol/typescript-sdk/compare/@modelcontextprotocol/server@2.0.0...@modelcontextprotocol/server@2.2.0)

Updates `@modelcontextprotocol/client` from 2.0.0 to 2.2.0
- [Release notes](https://github.com/modelcontextprotocol/typescript-sdk/releases)
- [Commits](https://github.com/modelcontextprotocol/typescript-sdk/compare/@modelcontextprotocol/client@2.0.0...@modelcontextprotocol/client@2.2.0)

---
updated-dependencies:
- dependency-name: "@modelcontextprotocol/node"
  dependency-version: 2.1.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: mcp
- dependency-name: "@modelcontextprotocol/server"
  dependency-version: 2.2.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: mcp
- dependency-name: "@modelcontextprotocol/client"
  dependency-version: 2.2.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: mcp
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-06 13:56:30 +02:00
Oliver Kieselbach 5dc17377f6 bump pointer: internal (device-clock-server-frame P0+P1 live; B-dhu)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 23:47:39 +02:00
Oliver Kieselbach e2e12055bf Show the NTP clock skew in the right direction
The agent's ntp_time_check reports offsetSeconds = NTP time - device time,
so a positive value means the device clock is BEHIND. The session card
said "ahead of UTC" for it (a device 10 h behind showed as 10 h ahead),
and ANALYZE-DEV-007 named its two signals the wrong way round.

- SessionInfoCard: the direction comes from deviceClockDirection
  (sessionInfoLogic.ts, pinned by a test with both signs).
- ANALYZE-DEV-007 1.1.1: offsetSeconds > 120 is clock_behind, < -120 is
  clock_ahead. Conditions, thresholds and text are unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 22:58:46 +02:00
Oliver Kieselbach 134ea71d63 bump pointer: internal, .claude/commands (plan device-clock-server-frame P1; B-r67; health marker)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 22:54:36 +02:00
Oliver Kieselbach 2f9a404323 Record session start and end on the server clock (no reader yet)
Session durations come from device times, and a device clock can be off by
hours for a whole session or change in the middle of one. Every upload
carries the device send time next to the server receive time; their
difference is the device clock error. This change records the session's
start, WhiteGlove Part 2 start and end on the server clock next to the
device values. Nothing reads them yet, so no stored value and no display
changes.

- ServerTime: the rule, defined by tests/fixtures/server-time/cases.json and
  run by both the backend (ServerTime.cs) and the web (lib/serverTime.ts).
- Ingest: ServerTime.Tracker, the rule fed one upload at a time, keeps its
  state in the primary-only column ServerTimeState and writes
  StartedAtServer and ResumedAtServer in the counter merge (no extra read
  or write). It follows a session from its first upload only, and only when
  that upload carries agent_started; sessions already under way when this
  build arrives get their values at completion. On every upload prefix of
  every followed case it gives the rule's start and Part 2 start; a retried
  upload with agent_started is not counted as a restart.
- Terminal: the counter reconcile, which every terminal path runs, also
  computes the rule over the same event scan and writes StartedAtServer,
  ResumedAtServer and CompletedAtServer. Fail-soft: a rule failure never
  costs the counter reconcile or the ingest write.
- Re-registration carries the four columns from the fresh CAS read through
  the Replace (the agent's first upload often races its registration).
- SessionSummary gets the three fields; the index mirrors them, the index
  merge drops primary-only columns; manifests and wire types regenerated.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 22:53:35 +02:00
Oliver Kieselbach 964a548e81 bump pointer: internal (D-324 live; B-xf5 field verify)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 19:45:44 +02:00
Oliver Kieselbach 5d5a893557 Serve the OOBE update phase as Active by default (D-324)
The backend default of CollectorConfiguration.OobeUpdatePhaseMode is now Active. While Windows
installs its OOBE quality update, the decision engine holds the post-ESP completion window (at most
3 h) and then waits 60 min for the sign-in; a session nobody signs in to ends as awaiting the user
or incomplete instead of failed. Agents still count a missing or unknown value as Shadow. Shared
manifest and generated wire types regenerated.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 19:23:19 +02:00
Oliver Kieselbach d4e4170813 bump pointer: internal (D-323, plan teams-legacy-connector-removal; B-f5b, B-wkw, B-nuz)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 18:23:05 +02:00
Oliver Kieselbach 6d313d2832 Remove the Teams Office 365 Connector notification provider (D-323)
Microsoft switched off the Office 365 connectors in Teams in May 2026. Provider 1,
its MessageCard renderer, the channel editor option and the flat TeamsWebhookUrl /
TeamsNotifyOn* fields with their fallback branch are gone; the value stays retired.

NotificationChannel.IsSupportedProviderType is now the one rule for channel parsing,
channel validation and the flat WebhookProviderType, so a retired or unknown provider
can no longer be stored and come back as a synthesized channel. The channel editor
looks up the placeholder by value instead of by list index. The header-parsing tests
move to NotificationChannel.GetCustomHeaders; manifests and wire types regenerated.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 18:16:16 +02:00
Oliver Kieselbach 0618df3c56 bump pointer: internal (D-322 live; B-vq3 live verify)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 17:48:02 +02:00
Oliver Kieselbach c6fda24843 Show the OOBE quality update live on the session page and in the Progress Portal (D-322)
The time attribution shows the OOBE quality update only once a session ended. While it
runs, the web now derives it from the events it already holds:

- lib/osUpdateLive.ts ports the calculator's update interval for the current window and
  labels it from positive evidence that the update finished: running (restarting),
  waiting for sign-in from 2 min after its end, or failed/skipped without a restart.
  A package waiting for its restart keeps the update running whatever the page reported.
- Session page: a line under the phase timeline, shown after the first complete walk of
  the event pages, with a note when the device has been silent for 15 minutes.
- Progress Portal: the headline says that Windows is installing updates and asks the user
  to sign in after the update's restart.
- OobeUpdateVocabulary (Shared) is the one vocabulary of the agent, the calculator and the
  web (manifest section oobeUpdate); same strings on the wire, no behaviour change.
- tests/fixtures/os-update-live/cases.json pins the calculator and the port
  (TimeAttributionOsUpdateLiveParityTests, lib/__tests__/osUpdateLive.test.ts).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 17:37:28 +02:00
Oliver Kieselbach 32bfa8b5b7 bump pointer: internal (feedback-bubble-ask-twice live)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 16:55:20 +02:00
Oliver Kieselbach c7bfc1e2f1 bump pointer: internal (plan feedback-bubble-ask-twice; B-yzf solved)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 16:45:33 +02:00
Oliver Kieselbach 2fb533ab92 Rating bubble asks a person at most twice and keeps one answer per person
- An X click counts a dismissal (DismissCount on the person's row); the second ask waits for the
  configured pause (FeedbackCooldownDays), the second dismissal ends the asking. A rating ends it at
  once. Ignoring the bubble records nothing, as before.
- A rated row is final: a later dismissal or rating (another tab) changes nothing and sends no second
  ping. Answers are written conditionally on what was read (ETag), so two tabs cannot overwrite a rating.
- Rows from before the count read as one dismissal when dismissed; the person keeps UPN and name.
- Rules in FeedbackPromptRules; the admin Ratings tab shows "dismissed twice".

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 16:44:41 +02:00
Oliver Kieselbach b0b38c1cfa bump pointer: internal (D-321 live; B-az6 alert rule, B-yre live verify)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 15:59:51 +02:00
Oliver Kieselbach 34c8715c5a bump pointer: internal (D-321, plan in-app-feedback-and-upload-budget), .claude/commands (health markers and events for D-321)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 15:47:55 +02:00
Oliver Kieselbach 5e240d7b81 Send feedback from the help menu; Submit Logs and Report Session share one upload budget (D-321)
- POST feedback/general (MemberRead): text up to 4096 characters, optional reply address, at most
  10 per user in 24 hours; one Feedback row per message (PK General), shown in a new admin tab.
- FeedbackReceived ops event without text or sender, plus a bell entry. The help menu gets one
  "Send feedback" entry; the dialog links to Submit Logs for Admins and Operators.
- Report uploads: logs are zipped before sending and both forms share one budget derived from the
  20 MiB body cap (Constants.SubmissionLimits through the shared manifest). The session exports go
  along when they fit next to the attachments.
- Report Session opens to Operators. Comment and contact address are validated; a report whose row
  cannot be written fails and its uploaded blobs are removed.
- feedback/status no longer creates a tenant configuration row for an unknown tenant.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 15:47:37 +02:00
Oliver Kieselbach a621a00252 bump pointer: internal (D-320 released, agent 2.0.1478; B-ywb for the field verify)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 12:14:36 +02:00
Oliver Kieselbach d1063f17cb bump pointer: internal (D-320, concept docs, plan oobe-update-outcome-and-late-start)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 11:27:58 +02:00
Oliver Kieselbach ed517b5b40 Agent: the first run reads the OOBE update page back an hour; live and backfilled names have their own budgets (D-320)
The first run has no Shell-Core replay (the restart replay re-raises the Hello-wizard start), so an
agent installed late during the ESP missed the update page's start and first half (6a7d241d).

- BackfillUpdatePageTelemetry: on the first run the last 60 min of Shell-Core become oobe_update_page
  rows and nothing else — no Hello replay, no ESP exit or failure, no agent_trace, no registry read.
- Live and backfilled names have budgets of 150 each; the first name over a budget sends a
  names_capped marker, so a missing name is never read as "did not happen".
- A backfill batch keeps its own last page and moves the live watcher's only with a newer RecordId:
  a restart replay's older page start no longer takes over the next live stop, and a first run's
  live stop finds the page that started before the agent.

Agent suite 3,436 green; the agent stays on net48.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 11:27:08 +02:00
Oliver Kieselbach 46058ffb90 OOBE quality update: how it ended, a begin without the page start, three rules for the update that did not happen (D-320)
Field measurement 3: a first-run agent that started after the update page (6a7d241d) left the whole
update in Apps (ESP), failed and skipped updates were reported by nothing, and the lane listed a
failed update's staged servicing stack as an installed KB.

- Time attribution v5: update names before the first observed OobeNDUP start begin the update at the
  earlier of the first name and the update's first servicing step (after the window's ESP exit when
  one was seen before the name, else the first step of a package the update also serviced later).
  The fallback's ESP exit is per window, so a WhiteGlove part-1 exit no longer begins an update in
  part 2. OsUpdateSpan carries Outcome (installed/failed/skipped/unknown, the latest evidence wins)
  and NotInstalledKbs; Kbs lists only packages that reached Installed. The 2-h sweep recomputes the
  last 30 days.
- ANALYZE-DEV-013 (update failed), DEV-014 (not offered after an earlier failure), DEV-015 (update
  check ended without a result): warning, never KO; a failure only from explicit names, every
  absence condition gated on the agent's names_capped marker; never two at once.
- ShellCoreTracker rows never set the session start (D-315 list), ahead of the agent's first-run
  update page backfill.
- The web lane names the outcome and the KBs not installed; MCP get_time_attribution mentions both.

Real-event replay: 6a7d241d os_update 2358 s installed (was 55 min in esp_apps), e7551b82 failed with
KB5124007 not installed. Rule dry runs and a sweep over all 97 update-page sessions since 2026-10-02
hit exactly the five field sessions. Functions 6,918, web 1,701, MCP 997 green.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 11:26:57 +02:00
Oliver Kieselbach 81d0fe6ed3 Row accents survive the list divider on every row, not just the first
Tailwind 3's divide-<color> rule (`> :not([hidden]) ~ :not([hidden])`,
specificity 0,3,0, plus the dark-mode !important overrides in globals.css)
recolours every child but the first on all sides, so a row's own border
accent only ever showed on the first row.

- Notification dropdown: the purple GA and blue tenant bars are inset
  shadows now, so every such row keeps its bar.
- Geographic performance: the red outlier bar moves from the row to its
  first cell; it was grey on every outlier but the first table row.
- Tenant config report: the heavier separator between notification
  channels and between self-hosted AI clients moves to the cell
  (GROUP_SEPARATOR); the divider had shrunk it to a plain 1px line.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 09:33:47 +02:00
Oliver Kieselbach a26cd5edf7 bump pointer: internal (D-319 released, agent 2.0.1477; B-bmk narrowed to the field verify)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 09:23:51 +02:00
Oliver Kieselbach 48e9702fd2 bump pointer: internal (D-319, concept docs, plan rewound-line-freshness, B-bmk)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 09:07:10 +02:00
Oliver Kieselbach b560cb13b6 A re-processed IME log entry anchors itself when the bytes it replaced were seen moments ago (D-319)
A concurrent IME writer overwrites lines behind the bookmark, and the overwrite check recovers them a
pass later. Recovered lines were never fresh, so they resolved in the agent's own cached zone: in a lab
the zone was stale (+60, IME wrote +120), recovered result lines landed an hour ahead of the clock and
the platform-script durations were omitted ("n/a" on Install Autopilot Monitor, session d29e27e3, also
on 2.0.1474/2.0.1467). Bytes that read differently at a position were written after that position was
last seen, and IME stamps a line when it writes it, so the last sighting bounds the new line's age.

- Ledger entries carry SeenUtc: set when read or seeded, renewed whenever a verification finds them
  unchanged.
- A changed entry below the old bookmark is fresh when the pass is, the ledger held the entry at the
  rewind point, and the replaced bytes were seen 0-30 s ago; otherwise the fallback as before.
  Re-processed entries are never calibration anchors.

Field replay and longer-running script red before; six mutation proofs; agent suite 3,426 green.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 09:07:09 +02:00
Oliver Kieselbach b0f866dc7d bump pointer: internal (OOBE update field measurement 3, D-315 verified)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 08:57:03 +02:00
Oliver Kieselbach f3460ea828 bump pointer: internal (D-318 released, agent 2.0.1476; B-gc8 narrowed to the field verify)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 20:12:25 +02:00
Oliver Kieselbach d88d4a143b bump pointer: internal (D-318, concept docs, plan platform-script-retry-pairing, B-gc8)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 18:29:50 +02:00
Oliver Kieselbach 913dfa13fe One platform run per executor start; IME results pair with the run that started last before them, the fallback judges by IME's rule (D-318)
IME re-runs a failed platform script on later check-ins, and the agent's first pass reads all of
AgentExecutor.log before the IME log, so several runs of one policy shared one slot: the first IME
result took the LAST run's exit code and output (session eff1b413: the bootstrap script "Failed
(exit 0)" with the stdout of its successful retry), every later result went out without an end
block. The exit-code fallback said Success for exit 0 with stderr and Failed for exit 1 without,
which IME never does.

- A later timestamped executor start parks the previous run of its policy (persisted, max 64); an
  IME result pairs with the run of its policy and user that started last before the result line;
  earlier parked runs of the same user resolve then, so emits keep run order.
- Invocation markers carry the run id; the emitted marker only rises; no dedup path deletes a live
  run; a parked emit never takes the later run's D-316 register entry.
- Fallback: Success exactly when the stderr is empty, the exit code only when no stderr line was
  read; "write output done … error =" replaces the "error from script" snapshot, empty stdout too.

Backfill replays fail on the old code; nine mutation proofs; agent suite 3,416 green.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 18:29:33 +02:00
Oliver Kieselbach 51345718c6 bump pointer: internal (D-316/D-317 released, agent 2.0.1475; B-qrz narrowed to the field verify)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 15:46:47 +02:00
Oliver Kieselbach 6b92e4e28c bump pointer: internal (D-316/D-317, concept docs, plan review, B-qrz/B-cwy/B-shx/B-96m), .claude/commands (platform-health marker)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 15:20:45 +02:00
Oliver Kieselbach f392819b97 Portal, MCP and backend apply script_output_reconciliation: a corrected platform run shows its own output and "exit n/a" (D-316)
Web: the script panel applies the correction to the run its runId names: the saved output replaces the emitted one, a foreign end block's exit code shows as n/a with an explanation, the state follows the corrected data instead of the emitted event type, and the output carries a short "from IME's saved result" note. The timeline marks the original row as corrected later, and the correction event falls under the script-output setting like the other script events. The session page passes the event to the panel; the gather-rule event list knows the type.

MCP: get_session_summary no longer counts a script_failed as an error, ranks it as one or attaches the exit code's catalog text when a foreign correction with IME result Success names its run.

Backend: the correction never heals a stalled session and never counts as a script run.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 15:15:07 +02:00
Oliver Kieselbach 672570df04 Platform script output is checked against IME's saved result and corrected when the log attributed another script's end block (D-316)
After reporting a script batch, IME saves every run under HKLM\SOFTWARE\Microsoft\IntuneManagementExtension\Policies\<user>\<policy>; ResultDetails carries the stdout IME read from that policy's own result file (the stderr when it failed with error code 99). The agent now checks each emitted platform run against it: a match confirms the output and the exit code of the same end block, a contradiction emits script_output_reconciliation (Warning, outcome "foreign": the saved output replaces the emitted one, the exit code is unverified), and output the log delivered incomplete is completed (Info, outcome "repaired").

A save is paired with the latest run of its user and policy only after the tracker completed a pass that started two seconds after the save was seen, so a stalled poll loop cannot pair it with an older run. Platform completions carry a runId the correction names. Two restart-safe counters (verified, corrected) ride agent_metrics_snapshot and ime_pattern_hits. The new event type is registered in the shared constants, the generated manifests and the MCP event catalog.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 15:08:22 +02:00
Oliver Kieselbach 6f7ececa1f Platform scripts no longer take another executor's output and exit code: a cut line in AgentExecutor.log ends the invocation in front of it (D-317)
Two AgentExecutor processes that open the log at the same end write their start blocks over each other. The end block of the one whose start block lost then sat behind the other executor's start block, and position-based ownership handed a Win32 detection script's stdout and exit code to the platform script.

An AgentExecutor.log entry that is not exactly one CMTrace record (a fragment, a merged line or an empty line) is where one writer's bytes end and another's begin: it now closes the platform invocation in front of it and is never matched. Invocation markers live as long as the bytes of their entry: a rewind sets them aside and gives an entry that reads unchanged its markers back.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 15:08:21 +02:00
Oliver Kieselbach af7e9d3650 bump pointer: internal (D-315 live, B-crw field verify)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 13:51:00 +02:00
Oliver Kieselbach b3b122a23c bump pointer: internal (session start anchor D-315, plan, B-crw)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 13:41:19 +02:00
Oliver Kieselbach 064b670c75 Update and servicing rows backfilled from before the agent no longer set the session start (D-315)
Since agent 2.0.1473 the Windows Update and servicing watchers backfill rows with their event-log
time up to 60 min before the agent started, and the session anchor only skipped the system
timeline watcher, so session starts and durations grew by 22 min at the median.

- Shared: Constants.EventSources.SessionAnchorIneligible and IsSessionAnchorEligible are the one
  rule; the agent writes the same constants as Source.
- Backend: the ingest batch (ClassifyEvents, now internal static) and the Events-table probe use it.
- Web: the session duration reads the list from shared-manifests.json.
- Recorded sessions keep their stored start; the rule applies to new ingests.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 13:40:54 +02:00
Oliver Kieselbach e5eb6dc4fb bump pointer: internal (OOBE update field measurement 2, B-cqa session start regression, B-93v)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 13:05:18 +02:00
Oliver Kieselbach 9f46adcdfa ci: the storage auth helper masks its token in the run log, like the Key Vault token
GitHub masks only secrets.* by itself. The bearer token Get-StorageAuthHeaders.ps1
mints at run time was never registered as a mask, unlike the Key Vault signing token,
which build-agent, build-msi and publish-scripts mask before exporting it. No caller
prints it today -- every call site captures the hashtable and passes it only as
-Headers -- but this repository's run logs are public, and a single debugging line
would have published it: an uncaptured call of the helper, or Get-Error /
Format-List on a failed request, whose TargetObject is the HttpRequestMessage
including the Authorization header (reproduced locally with a dummy token).

The helper now emits ::add-mask:: for every token it hands out, which covers all six
workflows in one place. Write-Host, never Write-Output: the output stream is the
script's return value. Verified with a mocked az that the result stays one hashtable
and that a nested caller in the Request-BuildNumber.ps1 pattern still returns only
its build number. Only under GITHUB_ACTIONS -- locally the line would print the token
instead of hiding it.

Prompted by an external AI review of the public scripts.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 12:52:01 +02:00
Claude cd1319a486 The GA tenant editor's Members card drops its icon column, so its content lines up with the other cards
- The Members card was the only card in the edit modal with a separate
  icon column: its title, add form and member list sat 32px in from the
  card edge, which on a phone also took 32px from the email field and the
  member rows.
- It now uses the layout of its sibling cards: the title on top, the
  content at full card width.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HNLaZbFQTdhQJU9jh37Hn6
2026-10-03 09:17:08 +00:00
Claude ca10f36624 Landing stats band takes the versioned stats file from the browser cache again; the build log names the baked file
Since e537a4b the backend writes a new versioned file per publish, so its
immutable header holds again. The browser revalidates only the manifest
(no-cache) and serves the file it names from its cache (default mode),
instead of revalidating both, the workaround in 762fef4 for the per-day name.

The build log names the baked file ("Platform stats baked into the landing
page from platform-stats.<date>T<time>Z.json (as of ...)"), so every web
deploy shows which publish it carries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4ddfosgdeCwXo7NvB9xd2
2026-10-03 04:57:38 +00:00
Claude e537a4b074 Platform stats publish writes a new versioned file each run, so its immutable header holds again
The versioned stats file is served public, max-age=31536000, immutable, and
the manifest platform-stats.json names the current one. Its per-day name
(19e9713a) was unique while maintenance ran once a night; since 82919d47 it
runs every 2 hours and rewrote that file up to 12 times a day, so caches kept
the first copy of the day. The name now carries the publish instant to the
second in invariant culture (platform-stats.2026-10-03T060004Z.json).

Headers and access tier travel with the upload instead of two follow-up
calls, which left the blob briefly without Cache-Control and content type,
and kept it that way (with the manifest still on the old file) when one of
them failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4ddfosgdeCwXo7NvB9xd2
2026-10-03 04:54:54 +00:00
Claude 762fef4e24 Landing page bakes the platform stats of the deploy into its static HTML; browsers then swap in the live numbers
Crawlers and LLM fetchers run no JS and saw only the skeleton of the stats
band. app/page.tsx now awaits one read of the public stats blob during
next build (lib/platformStats.ts: manifest, then the versioned file it
names) and renders the numbers into the export. StatsBand starts from that
snapshot and replaces it with the live numbers; a failed live read keeps
the baked numbers instead of hiding the band.

The read is awaited at page level, not under Suspense: React moves a
resolved boundary of a page this size into a hidden div at the end of the
body. It uses no-store (route force-static), so Next's fetch cache cannot
hand a later build the previous numbers. Fail-soft: without a snapshot the
page ships the skeleton and loads live as before, and deploy-web.yml warns.

The browser now revalidates both files (no-cache instead of force-cache):
the backend rewrites the per-day file every 2 hours under immutable, so a
copy cached earlier that day would have made the numbers drop after load.

The page body moved to components/landing/LandingPage.tsx, which the
marketing guards render synchronously; StatsBand left the .json() ratchet
baseline; the sitemap lastmod of / includes the body.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4ddfosgdeCwXo7NvB9xd2
2026-10-03 03:27:20 +00:00
Oliver Kieselbach bca492ba05 bump pointer: internal (agent v2.0.1474 released, B-6dx field verify, time-attribution deploy steps)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 23:06:40 +02:00
Oliver Kieselbach e7dd962133 bump pointer: internal (time attribution v4 D-314, OOBE update page agent fixes, backlog B-q3z/B-6dx/B-dbf; member list lesson)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 22:57:10 +02:00
Oliver Kieselbach 14a67ef5af Time attribution shows the OOBE quality update as Windows Update and the wait after its restart as Waiting for sign-in (D-314)
TimeAttributionCalculator v4 cuts two segments out of the phase partition:
os_update from the OobeNDUP page start whose visit holds update evidence
(a servicing step or a name only an update in progress writes) to the last
update evidence before the user is back; awaiting_sign_in from there to the
first evidence of the user, only after a restart. Both replace the phase
span they overlap; without evidence the partition is v3's. OsUpdates names
the KBs and restarts. Reboot gaps no longer start or end on the clock-sync
row logged at boot. The sweep recomputes the last 30 days.

Aggregates carry eight segments; the session lane and Fleet Health show
"Windows Update" (cyan) and "Waiting for sign-in" (sky) with the KBs and
restarts in legend and tooltip; MCP get_time_attribution description;
manifest and wire types regenerated.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 22:55:45 +02:00
Oliver Kieselbach 2371855b77 OOBE update page: fields from event properties in every display language, page start and stop never capped, no duplicates after a restart
ShellCoreTracker reads CXID, result, name and value from the event properties
(62404..62407 %1/%2), live and in the backfill. The formatted message is
localized: German devices lost result and value, French devices reported no
update page at all. The text patterns stay as the fallback and accept
guillemets. The Hello-wizard start reads the CXID property too.

OobeUpdateTelemetry: page start and stop sit outside the run budget (at most
3 per page), names share a budget of 150 (was 50; the cloud page logs ~45
init names in its first 40 s). ReportPage returns whether it reported.

A RecordId watermark (shellcore-page-watermark.json) claims every update-page
record and persists reported ones, so the restart backfill does not send the
minutes before the restart again. EspAndHelloTracker passes the state
directory.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 22:55:39 +02:00
Oliver Kieselbach 3a50de83ef Add-member input keeps a minimum width and wraps instead of shrinking (D-313)
The form no longer lays out by viewport breakpoint: a wide window can host a
narrow column next to the settings sidebar, where the input shrank to a few
characters. The input now grows from an 18rem basis and wraps below the type
switch when the row has no room; role and Add wrap together.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 22:51:28 +02:00
Oliver Kieselbach 24dfd247e9 bump pointer: internal (viewer app role + member list live, D-313, lesson)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 21:18:14 +02:00
Oliver Kieselbach a640039518 Member role changes need an explicit Save; the GA add form gives the input its own line (D-312)
- An open member row keeps role and bootstrap permission as a draft:
  Save writes it, Discard or closing the row drops it, so a stray select
  change no longer alters anyone's access.
- AddMemberForm takes `stacked` for narrow hosts; the GA tenant editor
  modal uses it, so the email field spans the full width there.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 21:13:11 +02:00
Oliver Kieselbach c3e9011905 bump pointer: internal (D-312, plan viewer-app-role-and-member-list, B-8gf/B-7w2)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 20:36:37 +02:00
Oliver Kieselbach 8f6926a945 Viewer can come from an Entra app role; Access Management lists one line per member (D-312)
- EntraAppRoleResolver maps the Viewer app role next to Admin and Operator
  (the highest role wins); a claim Viewer gets no bootstrap-token rights.
- The tenant settings page and the GA tenant editor share components/members:
  one line per member, a click opens who added it and the actions, Remove
  asks in the row, role chips with counts, a search, 25 rows per page. A
  vanished chip or a shrunken list moves the selection, so the list never
  shows an empty page and never snaps back.
- The add form sits above the list, About Roles is collapsed, and a tenant
  with Entra app roles enabled sees that those users are not listed.
- The EntraAppRolesEnabled doc names Viewer; manifest and generated wire
  types regenerated.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 20:32:54 +02:00
Oliver Kieselbach 01323fdf6d ProgressAccessHint test matches the docs link as a substring, not a RegExp
CodeQL js/incomplete-hostname-regexp (alert #96) traced DOCS_URL into the
RegExp built in the "Learn more" assertion. The dots were escaped with
[.], which CodeQL does not recognize as a sanitizer. A substring match
on the exact markup needs no escaping and keeps the same checks: a space
after the sentence, the exact href with target/rel, and "Learn more" as
the link text.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 20:17:46 +02:00
Oliver Kieselbach c566ed818b bump pointer: internal (access hint released, B-u5a verify only)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 17:01:38 +02:00
Oliver Kieselbach 3a40361bb6 bump pointer: internal (access-hint follow-up, docs-corpus URL invariant)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 16:54:52 +02:00
Oliver Kieselbach 1b988d2943 Docs links use the URL GitBook publishes: access hint via the docs path registry, MCP citations from SUMMARY.md groups
GitBook publishes a page under the slug of its SUMMARY.md group, not its
folder: troubleshooting/ is /troubleshooting-and-support/, trust/ is
/trust-and-security/. A folder URL works only for pages that predate the
group rename (307); for a newer page it is a 404.

- Web: the Progress Portal access hint linked the new troubleshooting
  article by folder path (404 live). It now uses DOCS_PATHS.progressPortalOnly.
- MCP: docUrl built folder URLs for every search_docs citation. Live census
  over all 58 SUMMARY pages: 47 direct, 10 via redirect, 1 dead.
  summaryFolderSlugs now derives the segments from SUMMARY.md; all 60 page
  URLs of the real corpus answer 200.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 16:54:40 +02:00
Oliver Kieselbach e88a0e455c Progress Portal access hint links to its troubleshooting article
Both variants end with a muted "Learn more" to
troubleshooting/progress-portal-only in the customer docs.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 16:35:37 +02:00
Oliver Kieselbach 0695f7943c bump pointers: internal (D-311, plan progress-portal-access-hint, B-zr9/B-u5a), commands (platform-health marker)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 16:20:50 +02:00
Oliver Kieselbach 6f8e7d48b2 Progress Portal tells members without a role whom to ask for access; activation lands every role where sign-in would
A member without a portal role only ever sees the Progress Portal and never
learned that their organization already runs the full product. Below the
search they now see one muted line: ask your Autopilot Monitor admin for a
role. When the organization holds no session and signed up more than 14 days
ago, the line names the signup date and the support mailbox instead.

- GET progress/tenant-status (AuthenticatedUser, JWT tenant only, no session
  data): unused flag plus signup date. Both reads are strict, so a storage
  failure is a 500 and never reads as "unused".
- ISessionRepository.HasAnySessionStrictAsync: key-only probe on
  SessionsIndex with the primary-table fallback, no catch.
- ProgressTenantStatusResponse + wire parity facts; regenerated manifests and
  web/MCP wire types.
- lib/landingTarget.ts: one landing decision for the landing gate and the
  activation page. Leaving activation no longer sends Operators, Viewers,
  Global Readers and delegated admins to the Progress Portal, and it keeps
  the deep link the landing gate saved during activation.
- lib/supportContact.ts: one source for the support address.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 16:20:38 +02:00
Oliver Kieselbach 0248d14738 bump pointer: internal (IME diff notes 1.106.106.0 + 1.105.154.0, B-4v2)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 16:01:05 +02:00
Oliver Kieselbach dc59fea6e5 bump pointer: internal (OOBE update phase: DEV-004 live, agent v2.0.1473 in Shadow)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 15:28:34 +02:00
Oliver Kieselbach 08455cc5f9 OOBE quality update: install events from the System log, servicing steps, a bounded engine phase in Shadow mode (D-310)
Report 3a2207978e4c: a session was failed 30 min after the ESP exit while Windows installed its
OOBE quality update. The update watcher had been blind since July and the engine had no notion
of the phase.

- Windows Update client: 19/20/43/44 come from the System log (provider-filtered XPath), as the
  provider manifest routes them; 25/26/31/41 from Operational via their own config list.
  Updates are classified os/defender/store/other: only Windows/.NET updates become rows, the
  rest is counted into windows_update_activity_summary. Per-channel watermarks persist a record
  only after its row and engine signal were handed to the ingress.
- Servicing: CBS steps from the Setup log (windows_update_servicing), client UpdateAgentLCU.
- Engine: OsUpdateActivity and OsUpdateFacts (event-time ordered) anchor an OOBE update phase
  after the ESP exit. Each state has its own bound: the update runs at most 3 h (then Failed
  oobe_update_not_finished), after it the user has 60 min to sign in (then the engine stops
  waiting without a verdict), after the sign-in one window of grace.
- The phase acts only in OobeUpdatePhaseMode=Active. Shadow, the default, reports what it would
  decide as oobe_update_phase_shadow right before today's verdict. The agent stamps the mode on
  EnrollmentFactsObserved at every start; a later stamp can take Active away, never grant it.
- Agent: a stop without a verdict ends the session like the max-lifetime watchdog
  (agent_shutting_down reason=stopped_waiting); the backend classifies it honestly (verdict
  origin stopwait), never Failed.
- Measurement: oobe_update_page (the update page and its ExpeditedUpdate_* names; a 62407 value
  only as boolean, integer or HRESULT) and oobe_update_state (NDUP and ESP-policy registry
  values), both Debug and bounded per run.
- ANALYZE-DEV-004 1.2.0 counts install and download failures only, never scan failures.
- Event types in Constants, MCP catalog and web; shared manifests regenerated; privacy page
  names Windows updates installed during the enrollment.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 15:09:50 +02:00
Oliver Kieselbach 87c5bf018c bump pointer: internal (gather-rule state released, B-hx3 field verify)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-01 22:26:18 +02:00
1070 changed files with 65504 additions and 7973 deletions
+2
View File
@@ -38,6 +38,8 @@
- Simplicity first: minimal, targeted changes. No temporary fixes — find the root cause.
- Where architecture is flawed, state is duplicated, or patterns are inconsistent, propose and implement the structural fix. Ask what a perfectionist reviewer would reject, and fix that too.
- On non-trivial changes, ask whether there is a more elegant way before presenting. Skip it for simple fixes — don't over-engineer.
- Reuse before you write: search for an existing helper by what it does (its key call, literal or regex), not by a guessed name. Web helpers live in `lib/` (route-only ones in a `lib/` beside the route), backend helpers in `Functions/Helpers/`.
- Dead and copied code is gated: the .NET build fails on unused private members and parameters (`.editorconfig`), `npm run knip` covers web and MCP, and `npm run dup` in `tools/quality` fails on new clones. CI only reports, so run them before calling a change done.
- English for code comments and everything that carries knowledge.
Private half of these instructions (process, working files, technical docs, operations): @../internal/CLAUDE.md
+11
View File
@@ -0,0 +1,11 @@
# Analyzer severities for the .NET build. Directory.Build.props runs the IDE rules in
# `dotnet build` (EnforceCodeStyleInBuild), and the warning freeze makes every warning an error.
root = true
[*.cs]
# Unused private members, private members that are written but never read, and unused parameters
# of non-public methods. An injected dependency nobody reads is a feature that silently never runs.
dotnet_diagnostic.IDE0051.severity = warning
dotnet_diagnostic.IDE0052.severity = warning
dotnet_diagnostic.IDE0060.severity = warning
dotnet_code_quality_unused_parameters = non_public
+18 -1
View File
@@ -65,7 +65,7 @@ updates:
- dependency-name: '*'
update-types: ['version-update:semver-major']
# Validator tooling for rules/ (ajv-cli); combine-rules.yml installs it from this
# Validator tooling for rules/ (ajv for validate.js); combine-rules.yml installs it from this
# lockfile with contents:write in hand, so the pins must not go stale.
- package-ecosystem: npm
directory: /rules/scripts
@@ -81,6 +81,23 @@ updates:
applies-to: security-updates
patterns: ['*']
# Duplicate-check tooling (jscpd for tools/quality/check-duplicates.mjs), installed by the
# CI duplicates job from this lockfile. A jscpd update can change the clone fingerprints:
# if its PR fails the job, regenerate the baseline (npm run dup:baseline) in that PR.
- package-ecosystem: npm
directory: /tools/quality
schedule:
interval: weekly
cooldown:
default-days: 7
open-pull-requests-limit: 5
groups:
quality-tooling:
patterns: ['*']
quality-tooling-security:
applies-to: security-updates
patterns: ['*']
# 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
@@ -0,0 +1,84 @@
<#
.SYNOPSIS
Fails when a workflow or a CI helper spells out a host instead of reading Get-Endpoints.ps1.
.DESCRIPTION
The CI counterpart of the URL-registry guard tests (HardcodedUrlGuardTests,
hardcodedUrls.guard.test.ts, hardcoded-urls.guard.test.ts). Enforced: every host the registry
defines -- derived from it, so the list cannot fall behind -- plus any Azure storage, App Service
or autopilotmonitor.com host, so a new or misspelled account is caught as well.
Scans .github/workflows/*.yml and .github/scripts/*.ps1 except the registry itself. Comment lines
and PowerShell block comments do not count: comments may cite URLs.
.PARAMETER Root
Repository root to scan. Defaults to the parent of .github/scripts.
#>
[CmdletBinding()]
param(
[string]$Root
)
$ErrorActionPreference = 'Stop'
if (-not $Root) {
$Root = (Resolve-Path (Join-Path $PSScriptRoot '../..')).Path
}
$endpoints = & (Join-Path $PSScriptRoot 'Get-Endpoints.ps1')
$registryHosts = $endpoints.Values | ForEach-Object { ([uri]$_).Host } | Sort-Object -Unique
$patterns = @($registryHosts | ForEach-Object { [regex]::Escape($_) }) + @(
'[a-z0-9-]+\.(blob|table|queue|file)\.core\.windows\.net'
'[a-z0-9-]+\.azurewebsites\.net'
'([a-z0-9-]+\.)*autopilotmonitor\.com'
)
$hostPattern = [regex]::new('(' + ($patterns -join '|') + ')', 'IgnoreCase')
$files = @(
Get-ChildItem -Path (Join-Path $Root '.github/workflows') -File -Filter '*.yml'
Get-ChildItem -Path (Join-Path $Root '.github/workflows') -File -Filter '*.yaml'
Get-ChildItem -Path (Join-Path $Root '.github/scripts') -File -Filter '*.ps1' |
Where-Object Name -ne 'Get-Endpoints.ps1'
)
$findings = 0
foreach ($file in $files) {
$relative = [System.IO.Path]::GetRelativePath($Root, $file.FullName) -replace '\\', '/'
$isPowerShell = $file.Extension -eq '.ps1'
$inBlock = $false
$lineNumber = 0
foreach ($line in [System.IO.File]::ReadAllLines($file.FullName)) {
$lineNumber++
$code = $line
if ($isPowerShell) {
if ($inBlock) {
$end = $code.IndexOf('#>')
if ($end -lt 0) { continue }
$code = $code.Substring($end + 2)
$inBlock = $false
}
$start = $code.IndexOf('<#')
if ($start -ge 0) {
$end = $code.IndexOf('#>', $start + 2)
if ($end -lt 0) {
$code = $code.Substring(0, $start)
$inBlock = $true
} else {
$code = $code.Remove($start, $end + 2 - $start)
}
}
}
if ($code.TrimStart().StartsWith('#')) { continue }
$match = $hostPattern.Match($code)
if ($match.Success) {
Write-Host "::error file=$relative,line=$lineNumber::$($match.Value) is spelled out here -- take it from .github/scripts/Get-Endpoints.ps1."
$findings++
}
}
}
if ($findings -gt 0) {
throw "$findings host literal(s) outside the endpoint registry."
}
Write-Host "No host literal outside .github/scripts/Get-Endpoints.ps1 ($($files.Count) files scanned)."
+71
View File
@@ -0,0 +1,71 @@
<#
.SYNOPSIS
The one place the workflows and the release scripts take their hosts from.
.DESCRIPTION
CI's member of the URL registry (Shared Constants.cs, web lib/config.ts, MCP src/config.ts):
every own host the workflows, the .github/scripts helpers and the local release build talk to,
plus the Authenticode timestamp server, is defined here once. Assert-HostsFromRegistry.ps1
(Workflow gates) fails on any of these hosts spelled out elsewhere under .github/.
The storage URLs derive from the account name, so blob and table can never point at two
different accounts.
A file in the repository rather than a GitHub variable: the local release build reads it too,
and bearer tokens are sent to these URLs -- where a token goes stays a reviewed line in git.
.PARAMETER ExportToGitHubEnv
Write every entry to $GITHUB_ENV instead of returning it, so the following steps of the job
read them as environment variables ($env:AGENT_CONTAINER_URL, ${{ env.AGENT_CONTAINER_URL }}).
.OUTPUTS
[ordered] hashtable, keyed by the names the environment variables carry.
.EXAMPLE
$endpoints = & "$PSScriptRoot/Get-Endpoints.ps1"
Invoke-WebRequest -Uri "$($endpoints.AGENT_CONTAINER_URL)/version.json"
#>
[CmdletBinding()]
param(
[switch]$ExportToGitHubEnv
)
$ErrorActionPreference = 'Stop'
$storageAccount = 'autopilotmonitoreu'
# Pre-migration account in another tenant; only the fail-soft legacy mirror still writes there.
$legacyStorageAccount = 'autopilotmonitor'
$blob = "https://$storageAccount.blob.core.windows.net"
$table = "https://$storageAccount.table.core.windows.net"
$endpoints = [ordered]@{
STORAGE_BLOB_URL = $blob
STORAGE_TABLE_URL = $table
AGENT_CONTAINER_URL = "$blob/agent"
VERSIONS_CONTAINER_URL = "$blob/versions"
STATS_CONTAINER_URL = "$blob/stats"
# Platform settings row; CI writes only its agent and bootstrap version columns.
ADMIN_CONFIG_URL = "$table/AdminConfiguration(PartitionKey='GlobalConfig',RowKey='config')"
LEGACY_AGENT_CONTAINER_URL = "https://$legacyStorageAccount.blob.core.windows.net/agent"
# Front Door in front of the agent container: what customer scripts download from.
DOWNLOAD_ALIAS_URL = 'https://download.autopilotmonitor.com/agent'
API_BASE_URL = 'https://autopilotmonitor-api-eu.azurewebsites.net'
MCP_BASE_URL = 'https://mcp.autopilotmonitor.com'
PORTAL_BASE_URL = 'https://portal.autopilotmonitor.com'
WWW_BASE_URL = 'https://www.autopilotmonitor.com'
DOCS_BASE_URL = 'https://docs.autopilotmonitor.com'
CODE_SIGN_TIMESTAMP_URL = 'http://timestamp.acs.microsoft.com'
}
if ($ExportToGitHubEnv) {
if ([string]::IsNullOrWhiteSpace($env:GITHUB_ENV)) {
throw '-ExportToGitHubEnv only works inside a GitHub Actions step.'
}
$endpoints.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" } |
Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
$endpoints.GetEnumerator() | ForEach-Object { Write-Host "$($_.Key) = $($_.Value)" }
return
}
return $endpoints
@@ -11,6 +11,7 @@
Identity comes from whatever `az login` established: the federated GitHub OIDC principal
in CI (azure/login, subject repo:okieselbach/AutopilotMonitor:ref:refs/heads/main), the
operator's own account for the local build. No secret is read, stored or passed around.
Under GitHub Actions the token is masked in the run log before it is returned.
x-ms-version is MANDATORY here, not decoration: Entra-authorized requests are rejected
below 2017-11-09, and Table requests without a SAS must carry the header at all. 2021-08-06
@@ -45,6 +46,15 @@ if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($token)) {
'on the table -- a missing role shows up later as HTTP 403 AuthorizationPermissionMismatch.')
}
# GitHub masks only secrets.* on its own; a token minted at run time is clear text in the
# (public) run log the moment anything prints it -- an uncaptured call of this script, or
# Get-Error / Format-List on a failed request, whose TargetObject carries this header.
# Write-Host, never Write-Output: the output stream is this script's return value. Only
# under Actions -- anywhere else the line would print the token instead of hiding it.
if ($env:GITHUB_ACTIONS -eq 'true') {
Write-Host "::add-mask::$token"
}
return @{
'Authorization' = "Bearer $token"
'x-ms-version' = '2021-08-06'
+14 -10
View File
@@ -81,7 +81,6 @@ param(
[string]$SigningToken,
[string]$KeyVaultUrl,
[string]$CertificateName,
[string]$TimestampUrl = 'http://timestamp.acs.microsoft.com',
[string]$ExpectedPublisher,
[string]$SignedOutputDir,
[switch]$DryRun
@@ -93,10 +92,13 @@ if (-not $RepoRoot) {
$RepoRoot = (Resolve-Path (Join-Path $PSScriptRoot '../..')).Path
}
$ContainerUrl = 'https://autopilotmonitoreu.blob.core.windows.net/agent'
$LegacyContainerUrl = 'https://autopilotmonitor.blob.core.windows.net/agent'
$AliasUrl = 'https://download.autopilotmonitor.com/agent'
$TableUrl = "https://autopilotmonitoreu.table.core.windows.net/AdminConfiguration(PartitionKey='GlobalConfig',RowKey='config')"
# Every host, the timestamp server included, from the one endpoint registry.
$Endpoints = & (Join-Path $PSScriptRoot 'Get-Endpoints.ps1')
$ContainerUrl = $Endpoints.AGENT_CONTAINER_URL
$LegacyContainerUrl = $Endpoints.LEGACY_AGENT_CONTAINER_URL
$AliasUrl = $Endpoints.DOWNLOAD_ALIAS_URL
$TableUrl = $Endpoints.ADMIN_CONFIG_URL
$TimestampUrl = $Endpoints.CODE_SIGN_TIMESTAMP_URL
$BootstrapSource = Join-Path $RepoRoot 'scripts/Bootstrap/Install-AutopilotMonitor.ps1'
$BootstrapBlob = 'Install-AutopilotMonitor.ps1'
@@ -210,7 +212,7 @@ function Set-PublishSetSignature {
--timestamp-rfc3161 $TimestampUrl `
--file-digest sha256 `
--description 'Autopilot Monitor bootstrap' `
--description-url 'https://www.autopilotmonitor.com' `
--description-url $Endpoints.WWW_BASE_URL `
$paths
# Native tool: PowerShell does not throw on a non-zero exit, and an unnoticed signing
@@ -327,8 +329,10 @@ $loaderVersion = Get-BootstrapScriptVersion -Content $loaderContent -Origin $Loa
Write-Host "Loader script version: $loaderVersion"
# ------------------------------------------------------------------ 2. dev render
$stableUrlLiteral = '$AgentDownloadUrl = "https://download.autopilotmonitor.com/agent/AutopilotMonitor-Agent.zip"'
$devUrlLiteral = '$AgentDownloadUrl = "https://download.autopilotmonitor.com/agent/AutopilotMonitor-Agent-dev.zip"'
# The anchors are the customer scripts' own text, built from the alias in the registry. Should
# the alias ever move, the scripts have to move with it -- until they do, the render stops here.
$stableUrlLiteral = '$AgentDownloadUrl = "' + $AliasUrl + '/AutopilotMonitor-Agent.zip"'
$devUrlLiteral = '$AgentDownloadUrl = "' + $AliasUrl + '/AutopilotMonitor-Agent-dev.zip"'
$stableManLiteral = '$VersionJsonName = "version.json"'
$devManLiteral = '$VersionJsonName = "version-dev.json"'
@@ -339,8 +343,8 @@ $devContent = $sourceContent.Replace($stableUrlLiteral, $devUrlLiteral).Replace(
Write-Host "Rendered $DevBlob (dev agent URL + version-dev.json)"
# The dev loader must fetch the dev bootstrap, or a dev device would run the stable chain.
$stableLoaderLiteral = '$BootstrapUrl = "https://download.autopilotmonitor.com/agent/Install-AutopilotMonitor.ps1"'
$devLoaderLiteral = '$BootstrapUrl = "https://download.autopilotmonitor.com/agent/Install-AutopilotMonitor-Dev.ps1"'
$stableLoaderLiteral = '$BootstrapUrl = "' + $AliasUrl + '/Install-AutopilotMonitor.ps1"'
$devLoaderLiteral = '$BootstrapUrl = "' + $AliasUrl + '/Install-AutopilotMonitor-Dev.ps1"'
if ($loaderContent.IndexOf($stableLoaderLiteral) -lt 0) {
throw "Loader dev-render: anchor literal missing in $LoaderSource"
+30 -8
View File
@@ -1,12 +1,15 @@
name: Workflow gates
# Two checks over the workflow files themselves:
# Three checks over the workflow files and their helper scripts:
# * verify-pins -- every third-party action is referenced by a full commit SHA plus a
# version comment (stricter than zizmor's unpinned-uses audit).
# * zizmor -- static analysis of every workflow (permissions, credential
# persistence, template injection, ...). Configuration and the
# reasoned ignores live in zizmor.yml at the repository root; the
# same run is expected locally before a workflow change is committed.
# * hosts -- no workflow or .github/scripts helper spells out a host; every one
# comes from .github/scripts/Get-Endpoints.ps1, CI's member of the URL
# registry (the C#, web and MCP registries have guard tests of their own).
#
# 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,
@@ -26,15 +29,18 @@ name: Workflow gates
#
# 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.
# therefore required too: it is what makes a pinned line reviewable. It names the exact
# release tag (# v7.0.1), never the floating major (# v7): a major tag moves with every
# upstream release, after which the comment no longer names the pinned commit and
# zizmor's ref-version-mismatch flags every line that carries it.
on:
push:
branches: [main]
paths: ['.github/workflows/**', '.github/dependabot.yml', 'zizmor.yml']
paths: ['.github/workflows/**', '.github/scripts/**', '.github/dependabot.yml', 'zizmor.yml']
pull_request:
branches: [main]
paths: ['.github/workflows/**', '.github/dependabot.yml', 'zizmor.yml']
paths: ['.github/workflows/**', '.github/scripts/**', '.github/dependabot.yml', 'zizmor.yml']
workflow_dispatch:
permissions:
@@ -45,7 +51,7 @@ jobs:
name: Verify action pins
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
@@ -59,13 +65,15 @@ jobs:
# nothing to pin.
case "$ref" in ./*|\$/*) continue ;; esac
if [[ "$ref" =~ ^[^@]+@[0-9a-f]{40}[[:space:]]+#[[:space:]]*[^[:space:]] ]]; then
if [[ "$ref" =~ ^[^@]+@[0-9a-f]{40}[[:space:]]+#[[:space:]]*v?[0-9]+\.[0-9]+\.[0-9]+([[: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."
echo "::error file=$file,line=$line::Pinned but the version comment is missing: '$ref'. Write 'owner/action@<sha> # vX.Y.Z' so the line stays reviewable."
elif [[ "$ref" =~ ^[^@]+@[0-9a-f]{40}[[:space:]]+# ]]; then
echo "::error file=$file,line=$line::The version comment must name the exact release tag the SHA resolves to ('# vX.Y.Z'), not a floating major: '$ref'. Find it with: gh api repos/<owner>/<action>/git/matching-refs/tags/vN"
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
@@ -89,7 +97,7 @@ jobs:
security-events: write # SARIF upload: findings land in the Code scanning tab next to CodeQL
actions: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
@@ -101,3 +109,17 @@ jobs:
with:
version: 1.30.1
min-severity: low
hosts:
name: Hosts only from the endpoint registry
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# The enforced host list is derived from the registry itself, plus every Azure storage,
# App Service and autopilotmonitor.com host, so it cannot fall behind a new entry.
- name: No host literal outside .github/scripts/Get-Endpoints.ps1
shell: pwsh
run: ./.github/scripts/Assert-HostsFromRegistry.ps1
+1 -1
View File
@@ -18,7 +18,7 @@ jobs:
name: Bootstrap guard tests (PS 5.1)
runs-on: windows-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
+390 -72
View File
@@ -7,6 +7,14 @@ on:
description: 'Build number override (leave empty to auto-increment from buildcounter.txt)'
required: false
type: string
channel:
description: 'release = versioned line (+ stable on cutover). dev = lab channel only (Agent-v2-dev.zip, version-v2-dev.json, Agent-dev.zip, version-dev.json); needs publish_as_stable=false and create_release=false'
required: true
type: choice
options:
- release
- dev
default: release
upload_blob:
description: 'Upload to versioned namespace (Agent-v2.zip + version-v2.json)'
required: true
@@ -32,10 +40,11 @@ on:
required: false
type: string
# Every job states its own permissions. This default is what the build job runs with: it
# executes third-party code (MSBuild targets, analyzers, test frameworks) and must never hold
# more than a read-only token.
permissions:
contents: write
id-token: write # keyless Sigstore signing (artifact attestation)
attestations: write # store the provenance attestation
contents: read
# Shared with publish-scripts.yml: both write version.json (this workflow the agent fields,
# the script publisher bootstrapVersion under If-Match). Serialising them keeps a script push
@@ -48,7 +57,9 @@ env:
LINE_MAJOR: 2
AGENT_PROJECT: src/Agent/AutopilotMonitor.Agent.V2/AutopilotMonitor.Agent.V2.csproj
DIALOG_PROJECT: src/Agent/AutopilotMonitor.SummaryDialog/AutopilotMonitor.SummaryDialog.csproj
TEST_PROJECT: src/Agent/AutopilotMonitor.DecisionCore.Tests/AutopilotMonitor.DecisionCore.Tests.csproj
DECISIONCORE_TEST_PROJECT: src/Agent/AutopilotMonitor.DecisionCore.Tests/AutopilotMonitor.DecisionCore.Tests.csproj
CORE_TEST_PROJECT: src/Agent/AutopilotMonitor.Agent.V2.Core.Tests/AutopilotMonitor.Agent.V2.Core.Tests.csproj
DIALOG_TEST_PROJECT: src/Agent/AutopilotMonitor.SummaryDialog.Tests/AutopilotMonitor.SummaryDialog.Tests.csproj
CSPROJ_PATH: src/Agent/AutopilotMonitor.Agent.V2/AutopilotMonitor.Agent.V2.csproj
BOOTSTRAP_SOURCE: scripts/Bootstrap/Install-AutopilotMonitor.ps1
AGENT_OUTPUT: src/Agent/AutopilotMonitor.Agent.V2/bin/Release/net48
@@ -57,25 +68,49 @@ env:
VERSIONED_ZIP: AutopilotMonitor-Agent-v2.zip
STABLE_ZIP: AutopilotMonitor-Agent.zip
STABLE_MANIFEST: version.json
# Authenticode via Azure Key Vault; identity and vault come from repository variables.
# Lab channel (channel=dev): the names the dev renders of the bootstrap scripts read.
CHANNEL: ${{ inputs.channel }}
DEV_VERSIONED_ZIP: AutopilotMonitor-Agent-v2-dev.zip
DEV_MANIFEST: version-v2-dev.json
DEV_STABLE_ZIP: AutopilotMonitor-Agent-dev.zip
DEV_STABLE_MANIFEST: version-dev.json
# Signed catalog over every other file of the payload. Must equal
# Constants.AgentPayloadCatalogFileName; CodeSigningParityTests keeps them in step.
PAYLOAD_CATALOG: AutopilotMonitor.Agent.cat
# Authenticode via Azure Key Vault; identity and vault come from repository variables, the
# timestamp server (CODE_SIGN_TIMESTAMP_URL) from the endpoint registry like every host here.
# CODE_SIGN_EXPECTED_SUBJECT is optional and pins the publisher in the verify step.
CODE_SIGN_CLIENT_ID: ${{ vars.CODE_SIGN_CLIENT_ID }}
CODE_SIGN_CERTIFICATE_NAME: ${{ vars.CODE_SIGN_CERTIFICATE_NAME }}
CODE_SIGN_KEY_VAULT_URL: ${{ vars.CODE_SIGN_KEY_VAULT_URL }}
CODE_SIGN_EXPECTED_SUBJECT: ${{ vars.CODE_SIGN_EXPECTED_SUBJECT }}
CODE_SIGN_TIMESTAMP_URL: http://timestamp.acs.microsoft.com
jobs:
build:
runs-on: windows-latest
name: Build Agent
# Runs only this repository's scripts. The deploy identity trusts the `production`
# environment, which admits main only.
reserve:
name: Reserve build number
runs-on: ubuntu-latest
environment: production
permissions:
contents: read
id-token: write # OIDC federated login for the counter write
outputs:
build_number: ${{ steps.version.outputs.build_number }}
version: ${{ steps.version.outputs.version }}
tag: ${{ steps.version.outputs.tag }}
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# Every host in this workflow comes from the one endpoint registry.
- name: Load endpoints
shell: pwsh
run: ./.github/scripts/Get-Endpoints.ps1 -ExportToGitHubEnv
# Every released build is signed; a missing variable stops the run before a build
# number is burnt.
- name: Check code-signing configuration
@@ -88,33 +123,22 @@ jobs:
}
Write-Host "Code-signing configuration present (certificate '$env:CODE_SIGN_CERTIFICATE_NAME')"
# Logs in FIRST and only to fetch the Key Vault token: azure/login sets the default az
# account, so the storage login below has to be the last one. Both federated credentials
# are bound to the ref, so this job must never be given an `environment:` -- that would
# change the OIDC subject claim and break both logins.
- name: Login to Azure (code-signing identity)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
client-id: ${{ env.CODE_SIGN_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
allow-no-subscriptions: true
- name: Acquire Key Vault token for signing
# The dev channel is a lab channel: it never touches stable, AdminConfiguration, the
# bootstrap scripts or a GitHub Release. Refused here, before a build number is burnt.
- name: Check channel inputs
shell: pwsh
env:
PUBLISH_AS_STABLE: ${{ inputs.publish_as_stable }}
CREATE_RELEASE: ${{ inputs.create_release }}
run: |
$ErrorActionPreference = 'Stop'
$token = az account get-access-token --scope https://vault.azure.net/.default --query accessToken --output tsv
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($token)) {
throw "Could not acquire a Key Vault token for the code-signing identity."
if ($env:CHANNEL -notin @('release', 'dev')) {
throw "Unknown channel '$($env:CHANNEL)'."
}
Write-Host "::add-mask::$token"
"CODE_SIGN_AKV_ACCESS_TOKEN=$token" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
if ($env:CHANNEL -eq 'dev' -and ($env:PUBLISH_AS_STABLE -eq 'true' -or $env:CREATE_RELEASE -eq 'true')) {
throw "channel=dev publishes the lab channel only: run it with publish_as_stable=false and create_release=false."
}
Write-Host "Channel: $env:CHANNEL"
# Every storage write below (counter blob, agent ZIP, manifests, AdminConfiguration)
# is authorized by this federated identity -- no SAS secret. One login covers the whole
# job: the federated credential is bound to the ref, and a release run takes ~2 minutes,
# well inside the token lifetime. Only the legacy mirror still needs a SAS: that account
# lives in the pre-migration tenant, where this principal has no identity.
- name: Login to Azure (OIDC)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
@@ -122,18 +146,6 @@ jobs:
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# SDK 10 = single build toolchain (parity with local build.ps1 / global.json).
- name: Setup .NET
uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6
with:
dotnet-version: '10.0.x'
# Version pinned; roll-forward because the tool targets an older runtime than the SDK above.
- name: Install AzureSignTool
env:
DOTNET_ROLL_FORWARD: LatestMajor
run: dotnet tool install --no-cache --global AzureSignTool --version 7.0.1
# Build numbers come from the shared counter blob -- one counter and one
# implementation for CI and the local build (.github/scripts/Request-BuildNumber.ps1).
# Reserve-before-build with ETag-CAS: concurrent builds can never mint the same
@@ -155,7 +167,7 @@ jobs:
$override = [int]$env:BUILD_NUMBER_OVERRIDE
}
$counterUrl = "https://autopilotmonitoreu.blob.core.windows.net/agent/buildcounter-v$($env:LINE_MAJOR).txt"
$counterUrl = "$env:AGENT_CONTAINER_URL/buildcounter-v$($env:LINE_MAJOR).txt"
$buildNumber = & ./.github/scripts/Request-BuildNumber.ps1 -CounterUrl $counterUrl -Override $override
$csproj = Get-Content $env:CSPROJ_PATH -Raw
@@ -172,22 +184,167 @@ jobs:
"version=$version" >> $env:GITHUB_OUTPUT
"tag=v$version" >> $env:GITHUB_OUTPUT
# Build zone: no login, no secret, no id-token. Its only output is the artifact; the
# release job treats that as data and never builds.
build:
name: Build and test
needs: reserve
runs-on: windows-latest
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# SDK 10 = single build toolchain (parity with local build.ps1 / global.json).
- name: Setup .NET
uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
dotnet-version: '10.0.x'
- name: Restore dependencies
run: |
dotnet restore ${{ env.AGENT_PROJECT }}
dotnet restore ${{ env.DIALOG_PROJECT }}
dotnet restore ${{ env.TEST_PROJECT }}
dotnet restore ${{ env.DECISIONCORE_TEST_PROJECT }}
dotnet restore ${{ env.CORE_TEST_PROJECT }}
dotnet restore ${{ env.DIALOG_TEST_PROJECT }}
# Every agent suite runs before the release build. ORDER AND BUILD_NUMBER ARE LOAD-BEARING:
# Agent.V2.Core.Tests references the agent exe, so the test build compiles it. With the
# reserved number it stamps the release version and the Build Agent step is up to date; run
# after that step or without the number, it would rewrite the binaries with another version.
- name: Run agent tests (all suites)
shell: pwsh
env:
BUILD_NUMBER: ${{ needs.reserve.outputs.build_number }}
run: |
foreach ($project in @($env:DECISIONCORE_TEST_PROJECT, $env:CORE_TEST_PROJECT, $env:DIALOG_TEST_PROJECT)) {
dotnet test $project --configuration Release --no-restore --verbosity quiet
if ($LASTEXITCODE -ne 0) { throw "Tests failed: $project" }
}
- name: Build Agent
run: dotnet build ${{ env.AGENT_PROJECT }} --configuration Release --no-restore
env:
BUILD_NUMBER: ${{ steps.version.outputs.build_number }}
BUILD_NUMBER: ${{ needs.reserve.outputs.build_number }}
- name: Build SummaryDialog
run: dotnet build ${{ env.DIALOG_PROJECT }} --configuration Release --no-restore
- name: Run Agent unit tests
run: dotnet test ${{ env.TEST_PROJECT }} --configuration Release --no-restore --verbosity quiet
# One archive per output folder, unsigned. ZipFile rather than Compress-Archive, which
# skips hidden files.
- name: Package build output
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.IO.Compression.FileSystem
$outDir = Join-Path $env:RUNNER_TEMP 'agent-unsigned'
New-Item -Path $outDir -ItemType Directory -Force | Out-Null
[System.IO.Compression.ZipFile]::CreateFromDirectory((Join-Path $env:GITHUB_WORKSPACE $env:AGENT_OUTPUT), (Join-Path $outDir 'agent.zip'))
[System.IO.Compression.ZipFile]::CreateFromDirectory((Join-Path $env:GITHUB_WORKSPACE $env:DIALOG_OUTPUT), (Join-Path $outDir 'dialog.zip'))
Get-ChildItem $outDir | ForEach-Object { Write-Host "$($_.Name): $([math]::Round($_.Length / 1MB, 2)) MB" }
- name: Upload unsigned build output
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: agent-unsigned
path: ${{ runner.temp }}/agent-unsigned
if-no-files-found: error
retention-days: 1
# Trust zone: only this repository's scripts, pinned actions and the pinned signing tool
# run here -- never a restore or build. The build output arrives as data. The runner
# image's Windows SDK signtool is used read-only, to verify third-party signatures.
#
# No `environment:` yet: the code-signing credential is still bound to the ref, and a job
# carries exactly one OIDC subject, so an environment here would break the signing login.
release:
name: Sign and publish
needs: [reserve, build]
runs-on: windows-latest
permissions:
contents: write # tag + GitHub Release
id-token: write # code-signing and storage logins, keyless Sigstore signing
attestations: write # store the provenance attestation
env:
AGENT_VERSION: ${{ needs.reserve.outputs.version }}
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# Storage, legacy mirror, docs link, timestamp server: all from the endpoint registry.
- name: Load endpoints
shell: pwsh
run: ./.github/scripts/Get-Endpoints.ps1 -ExportToGitHubEnv
- name: Download unsigned build output
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: agent-unsigned
path: ${{ runner.temp }}/agent-unsigned
# Only the two expected archives, each into its own fresh output folder; the .NET
# extractor refuses entries that would land outside it. Never unpack into the checkout
# root: an archive could otherwise replace the scripts this job runs below.
- name: Unpack build output
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.IO.Compression.FileSystem
foreach ($pair in @(@('agent.zip', $env:AGENT_OUTPUT), @('dialog.zip', $env:DIALOG_OUTPUT))) {
$archive = Join-Path $env:RUNNER_TEMP "agent-unsigned/$($pair[0])"
$target = Join-Path $env:GITHUB_WORKSPACE $pair[1]
if (Test-Path $target) { throw "$($pair[1]) already exists -- refusing to unpack over it." }
[System.IO.Compression.ZipFile]::ExtractToDirectory($archive, $target)
Write-Host "Unpacked $($pair[0]) into $($pair[1])"
}
# Logs in FIRST and only to fetch the Key Vault token: azure/login sets the default az
# account, so the storage login below has to be the last one.
- name: Login to Azure (code-signing identity)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
client-id: ${{ env.CODE_SIGN_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
allow-no-subscriptions: true
- name: Acquire Key Vault token for signing
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$token = az account get-access-token --scope https://vault.azure.net/.default --query accessToken --output tsv
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($token)) {
throw "Could not acquire a Key Vault token for the code-signing identity."
}
Write-Host "::add-mask::$token"
"CODE_SIGN_AKV_ACCESS_TOKEN=$token" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
# Every storage write below (agent ZIP, manifests, AdminConfiguration) is authorized by
# this federated identity -- no SAS secret. Only the legacy mirror still needs a SAS:
# that account lives in the pre-migration tenant, where this principal has no identity.
- name: Login to Azure (OIDC)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# Runtime for AzureSignTool only; nothing is built in this job.
- name: Setup .NET
uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
dotnet-version: '10.0.x'
# Version pinned; roll-forward because the tool targets an older runtime than the SDK above.
- name: Install AzureSignTool
env:
DOTNET_ROLL_FORWARD: LatestMajor
run: dotnet tool install --no-cache --global AzureSignTool --version 7.0.1
# Authenticode over everything we author; third-party assemblies keep their own signature.
#
@@ -223,18 +380,17 @@ jobs:
--timestamp-rfc3161 "$env:CODE_SIGN_TIMESTAMP_URL" `
--file-digest sha256 `
--description "Autopilot Monitor Agent" `
--description-url "https://www.autopilotmonitor.com" `
--description-url "$env:WWW_BASE_URL" `
($targets | ForEach-Object { $_.FullName })
if ($LASTEXITCODE -ne 0) {
throw "azuresigntool failed with exit code $LASTEXITCODE -- aborting before anything is published."
}
- name: Create release artifact
id: artifact
- name: Stage release payload
shell: pwsh
run: |
$version = "${{ steps.version.outputs.version }}"
$version = $env:AGENT_VERSION
$stagingDir = "staging/agent"
if (-not (Test-Path "$env:AGENT_OUTPUT/AutopilotMonitor.Agent.exe")) {
@@ -276,6 +432,98 @@ jobs:
}
Write-Host "Version check passed: EXE = manifest = reserved = $version"
# Third-party code enters the payload only with its vendor's own signature: every PE that
# is not ours needs a valid EMBEDDED Authenticode signature from an allowed vendor. Embedded
# only (signtool verify /pa, no /a): Get-AuthenticodeSignature prefers the runner's Windows
# catalogs and reports "Microsoft Windows" for files Windows happens to ship as well. An
# unsigned file or a new vendor stops the release until a person decides. This guards the
# bytes we did not write; a build that alters our own code is D-346's remaining risk.
- name: Check third-party signatures
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$allowedVendors = @('Microsoft Corporation', 'Json.NET (.NET Foundation)')
$signtool = Get-ChildItem "${env:ProgramFiles(x86)}\Windows Kits\10\bin\*\x64\signtool.exe" -ErrorAction SilentlyContinue |
Where-Object { $_.Directory.Parent.Name -match '^\d+(\.\d+){1,3}$' } |
Sort-Object { [version]$_.Directory.Parent.Name } -Descending |
Select-Object -First 1
if (-not $signtool) { throw "signtool.exe not found in the runner image's Windows SDK." }
Write-Host "Using $($signtool.FullName)"
$failed = $false
$checked = 0
foreach ($file in Get-ChildItem -Path 'staging/agent' -Recurse -File) {
# A PE by content (MZ header), not by extension.
$head = [byte[]]::new(2)
$stream = [System.IO.File]::OpenRead($file.FullName)
try { $read = $stream.Read($head, 0, 2) } finally { $stream.Dispose() }
if ($read -lt 2 -or $head[0] -ne 0x4D -or $head[1] -ne 0x5A) { continue }
if ($file.Name -like 'AutopilotMonitor.*') { continue } # ours: the verify step pins them
$checked++
$output = & $signtool.FullName verify /pa $file.FullName 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "::error file=$($file.Name)::No valid embedded vendor signature. $(($output | Out-String).Trim())"
$failed = $true
continue
}
$subject = [System.Security.Cryptography.X509Certificates.X509Certificate]::CreateFromSignedFile($file.FullName).Subject
$vendor = if ($subject -match '(?:^|,\s*)O=("[^"]*"|[^,]*)') { $Matches[1].Trim('"') } else { '' }
if ($vendor -notin $allowedVendors) {
Write-Host "::error file=$($file.Name)::Signed by '$subject' -- vendor '$vendor' is not on the allowlist."
$failed = $true
continue
}
Write-Host "$($file.Name): embedded signature valid, vendor $vendor"
}
if ($failed) { throw "Third-party signature check failed. Nothing was published." }
Write-Host "All $checked third-party PE files carry a valid signature from an allowed vendor."
# The payload contract: one catalog, signed like our binaries, holding the SHA-256 of every
# other file in the ZIP. Installer, OOBE script and self-updater accept a payload only when
# this catalog is ours and matches the extracted files exactly. Created AFTER signing and
# BEFORE the ZIP -- the same load-bearing position as the signing step above.
- name: Create and sign payload catalog
shell: pwsh
env:
DOTNET_ROLL_FORWARD: LatestMajor
run: |
$ErrorActionPreference = 'Stop'
$stagingDir = 'staging/agent'
$catalog = Join-Path $env:RUNNER_TEMP $env:PAYLOAD_CATALOG
if (Test-Path (Join-Path $stagingDir $env:PAYLOAD_CATALOG)) {
throw "$stagingDir already contains $env:PAYLOAD_CATALOG."
}
New-FileCatalog -Path $stagingDir -CatalogFilePath $catalog -CatalogVersion 2.0 | Out-Null
& azuresigntool.exe sign --verbose `
--azure-key-vault-url "$env:CODE_SIGN_KEY_VAULT_URL" `
--azure-key-vault-accesstoken "$env:CODE_SIGN_AKV_ACCESS_TOKEN" `
--azure-key-vault-certificate "$env:CODE_SIGN_CERTIFICATE_NAME" `
--timestamp-rfc3161 "$env:CODE_SIGN_TIMESTAMP_URL" `
--file-digest sha256 `
--description "Autopilot Monitor Agent" `
--description-url "$env:WWW_BASE_URL" `
$catalog
if ($LASTEXITCODE -ne 0) {
throw "azuresigntool failed on the payload catalog with exit code $LASTEXITCODE -- aborting before anything is published."
}
Copy-Item -Path $catalog -Destination $stagingDir
$count = (Test-FileCatalog -Path $stagingDir -CatalogFilePath $catalog -FilesToSkip $env:PAYLOAD_CATALOG -Detailed).CatalogItems.Count
Write-Host "Payload catalog signed: $count files"
- name: Create release artifact
id: artifact
shell: pwsh
run: |
$version = $env:AGENT_VERSION
$stagingDir = "staging/agent"
$manifest = Get-Content "$env:AGENT_OUTPUT/$env:MANIFEST_NAME" -Raw | ConvertFrom-Json
$versionedZip = "AutopilotMonitor-Agent-v$version.zip"
Compress-Archive -Path "$stagingDir/*" -DestinationPath $versionedZip
Write-Host "Created GitHub-Release artifact: $versionedZip"
@@ -365,17 +613,76 @@ jobs:
}
Write-Host "All $($files.Count) binaries signed with certificate $($thumbprints.Keys -join ', ')"
# The devices' check, run on the FINAL ZIP rather than the staging folder: whatever the
# archiver did (Compress-Archive skips hidden files) fails here instead of on a device.
# Windows PowerShell 5.1, the edition installer and OOBE script verify with; the pin is
# the installer's own literal, the value devices compare against.
- name: Verify payload catalog (device check)
shell: powershell
env:
RELEASE_ZIP: ${{ steps.artifact.outputs.versioned_zip_release }}
run: |
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.IO.Compression.FileSystem
$payload = Join-Path $env:RUNNER_TEMP 'verify-payload'
if (Test-Path $payload) { throw "$payload already exists." }
[System.IO.Compression.ZipFile]::ExtractToDirectory((Resolve-Path $env:RELEASE_ZIP).Path, $payload)
$pinLine = Select-String -Path $env:BOOTSTRAP_SOURCE -Pattern '^\$ExpectedPublisher = "([^"]+)"' | Select-Object -First 1
if (-not $pinLine) { throw "Could not read the publisher pin from $env:BOOTSTRAP_SOURCE." }
$pin = $pinLine.Matches[0].Groups[1].Value
if (-not [string]::IsNullOrWhiteSpace($env:CODE_SIGN_EXPECTED_SUBJECT) -and $pin -ne $env:CODE_SIGN_EXPECTED_SUBJECT) {
throw "Installer pin '$pin' differs from CODE_SIGN_EXPECTED_SUBJECT '$env:CODE_SIGN_EXPECTED_SUBJECT'."
}
$catalog = Join-Path $payload $env:PAYLOAD_CATALOG
if (-not (Test-Path $catalog -PathType Leaf)) { throw "$env:PAYLOAD_CATALOG is missing from the ZIP." }
$result = Test-FileCatalog -Path $payload -CatalogFilePath $catalog -FilesToSkip $env:PAYLOAD_CATALOG -Detailed
$problems = @()
if ($result.Status -ne 'Valid') {
$expected = $result.CatalogItems
$actual = $result.PathItems
$missing = @($expected.Keys | Where-Object { -not $actual.ContainsKey($_) })
$extra = @($actual.Keys | Where-Object { -not $expected.ContainsKey($_) })
$changed = @($expected.Keys | Where-Object { $actual.ContainsKey($_) -and $actual[$_] -ne $expected[$_] })
$problems += "payload differs from the catalog (missing: $($missing -join ', '); extra: $($extra -join ', '); changed: $($changed -join ', '))"
}
$signature = $result.Signature
if ($signature.Status -ne 'Valid') {
$problems += "catalog signature status is '$($signature.Status)'"
} elseif ($signature.SignerCertificate.Subject -notlike $pin) {
$problems += "catalog signed by '$($signature.SignerCertificate.Subject)', expected '$pin'"
}
if (-not $signature.TimeStamperCertificate) {
$problems += 'catalog signature carries no RFC-3161 timestamp -- it would expire with the certificate'
}
$exeSigner = (Get-AuthenticodeSignature (Join-Path $payload 'AutopilotMonitor.Agent.exe')).SignerCertificate
if ($signature.SignerCertificate -and $exeSigner -and $signature.SignerCertificate.Thumbprint -ne $exeSigner.Thumbprint) {
$problems += 'catalog and agent exe carry different signing certificates'
}
if ($problems.Count -gt 0) {
$problems | ForEach-Object { Write-Host "::error::$_" }
throw "Payload catalog verification failed. Nothing was published."
}
Write-Host "Payload catalog verified on the final ZIP: $($result.CatalogItems.Count) files, signer $($signature.SignerCertificate.Subject)"
- name: Upload to Azure Blob Storage
if: inputs.upload_blob
shell: pwsh
env:
ZIP_SHA256: ${{ steps.artifact.outputs.sha256 }}
EXE_SHA256: ${{ steps.artifact.outputs.exe_sha256 }}
run: |
$containerUrl = "https://autopilotmonitoreu.blob.core.windows.net/agent"
$containerUrl = $env:AGENT_CONTAINER_URL
$authHeaders = & ./.github/scripts/Get-StorageAuthHeaders.ps1
# Legacy mirror (transition until customers moved to the download.-alias):
# blobs are ALSO pushed to the old account so the bootstrap URL baked into
# customers' Intune scripts keeps serving current bits. Fail-soft -- a
# legacy hiccup must never fail a release. Counter lives ONLY on eu.
$legacyContainerUrl = "https://autopilotmonitor.blob.core.windows.net/agent"
$legacyContainerUrl = $env:LEGACY_AGENT_CONTAINER_URL
$legacySasToken = "${{ secrets.AZURE_BLOB_AGENT_LEGACY_SAS_TOKEN }}".TrimStart('?')
function Send-Blob {
@@ -405,6 +712,18 @@ jobs:
}
}
# Dev channel: lab blobs only, under the names the dev renders of the bootstrap scripts
# read. Nothing else is touched -- no versioned or stable line, no AdminConfiguration, no
# scripts; the channel check in `reserve` already refused publish_as_stable/create_release.
if ($env:CHANNEL -eq 'dev') {
Send-Blob -Local $env:VERSIONED_ZIP -BlobName $env:DEV_VERSIONED_ZIP -ContentType "application/zip"
Send-Blob -Local $env:MANIFEST_NAME -BlobName $env:DEV_MANIFEST -ContentType "application/json"
Send-Blob -Local $env:VERSIONED_ZIP -BlobName $env:DEV_STABLE_ZIP -ContentType "application/zip"
Send-Blob -Local $env:MANIFEST_NAME -BlobName $env:DEV_STABLE_MANIFEST -ContentType "application/json"
Write-Host "DEV CHANNEL: lab blobs written, nothing else touched."
exit 0
}
# Versioned namespace (always)
Send-Blob -Local $env:VERSIONED_ZIP -BlobName $env:VERSIONED_ZIP -ContentType "application/zip"
Send-Blob -Local $env:MANIFEST_NAME -BlobName $env:MANIFEST_NAME -ContentType "application/json"
@@ -426,7 +745,6 @@ jobs:
-SigningToken $env:CODE_SIGN_AKV_ACCESS_TOKEN `
-KeyVaultUrl $env:CODE_SIGN_KEY_VAULT_URL `
-CertificateName $env:CODE_SIGN_CERTIFICATE_NAME `
-TimestampUrl $env:CODE_SIGN_TIMESTAMP_URL `
-ExpectedPublisher $env:CODE_SIGN_EXPECTED_SUBJECT
}
@@ -437,10 +755,10 @@ jobs:
if ('${{ inputs.publish_as_stable }}' -ne 'true') {
Write-Host "SKIPPED: AdminConfiguration update (versioned-only run -- stable namespace unchanged)"
} else {
$sha256 = "${{ steps.artifact.outputs.sha256 }}"
$exeSha256 = "${{ steps.artifact.outputs.exe_sha256 }}"
$agentVersion = "${{ steps.version.outputs.version }}"
$tableUrl = "https://autopilotmonitoreu.table.core.windows.net/AdminConfiguration(PartitionKey='GlobalConfig',RowKey='config')"
$sha256 = $env:ZIP_SHA256
$exeSha256 = $env:EXE_SHA256
$agentVersion = $env:AGENT_VERSION
$tableUrl = $env:ADMIN_CONFIG_URL
$tableBody = @{
LatestAgentV2Version = $agentVersion
LatestAgentV2Sha256 = $sha256
@@ -464,7 +782,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@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4
uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2
with:
subject-path: |
${{ steps.artifact.outputs.versioned_zip_release }}
@@ -472,13 +790,13 @@ jobs:
staging/agent/AutopilotMonitor.SummaryDialog.exe
- name: Upload build artifact
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: AutopilotMonitor-Agent-v${{ steps.version.outputs.version }}
name: AutopilotMonitor-Agent-v${{ needs.reserve.outputs.version }}
path: ${{ steps.artifact.outputs.versioned_zip_release }}
- name: Upload version metadata
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: version-metadata
path: ${{ env.MANIFEST_NAME }}
@@ -497,30 +815,30 @@ jobs:
shell: pwsh
env:
GH_TOKEN: ${{ github.token }}
RELEASE_TAG: ${{ steps.version.outputs.tag }}
RELEASE_NAME: Autopilot Monitor Agent ${{ steps.version.outputs.version }}
RELEASE_TAG: ${{ needs.reserve.outputs.tag }}
RELEASE_NAME: Autopilot Monitor Agent ${{ needs.reserve.outputs.version }}
RELEASE_PRERELEASE: ${{ inputs.prerelease }}
RELEASE_ZIP: ${{ steps.artifact.outputs.versioned_zip_release }}
RELEASE_NOTES: ${{ inputs.release_notes }}
RELEASE_BODY_TAIL: |
### Version Info
- **Version:** ${{ steps.version.outputs.version }}
- **Build Number:** ${{ steps.version.outputs.build_number }}
- **Version:** ${{ needs.reserve.outputs.version }}
- **Build Number:** ${{ needs.reserve.outputs.build_number }}
- **SHA-256:** `${{ steps.artifact.outputs.sha256 }}`
- **Built from:** ${{ github.sha }}
- **Published as stable:** ${{ inputs.publish_as_stable }}
### Provenance
The executables are Authenticode-signed and timestamped. The ZIP and both executables carry a [GitHub Artifact Attestation](https://github.com/okieselbach/AutopilotMonitor/attestations) (Sigstore keyless). Verify:
The executables are Authenticode-signed and timestamped. Every file in the ZIP is listed in `AutopilotMonitor.Agent.cat`, a catalog signed with the same certificate, and third-party libraries carry their vendor's signature. The ZIP and both executables carry a [GitHub Artifact Attestation](https://github.com/okieselbach/AutopilotMonitor/attestations) (Sigstore keyless). Verify:
```
gh attestation verify AutopilotMonitor-Agent-v${{ steps.version.outputs.version }}.zip --repo okieselbach/AutopilotMonitor
gh attestation verify AutopilotMonitor-Agent-v${{ needs.reserve.outputs.version }}.zip --repo okieselbach/AutopilotMonitor
```
run: |
$ErrorActionPreference = 'Stop'
$notes = "$env:RELEASE_NOTES".Trim()
if ($notes -eq '') {
$notes = "Minor enhancements and bug fixes. See the [Agent Changelog](https://docs.autopilotmonitor.com/changelog/agent-changelog) for details.`n<!-- release-notes: none -->"
$notes = "Minor enhancements and bug fixes. See the [Agent Changelog]($env:DOCS_BASE_URL/changelog/agent-changelog) for details.`n<!-- release-notes: none -->"
}
# Previous release for the compare link. Fail-soft: the first release has none,
+194 -73
View File
@@ -11,39 +11,54 @@ on:
workflow_dispatch:
inputs:
upload_blob:
description: 'Upload to blob (customer-facing via download.autopilotmonitor.com/agent/)'
description: 'Upload to blob (customer-facing through the download alias)'
required: true
type: boolean
default: true
# Every job states its own permissions. This default is what the build job runs with: wix is
# third-party code and must never hold more than a read-only token.
permissions:
contents: read
id-token: write # keyless Sigstore signing (artifact attestation)
attestations: write # store the provenance attestation
env:
MSI_PROJECT_DIR: src/Agent/AutopilotMonitor.BootstrapMsi
RUNNER_SCRIPT: scripts/Bootstrap/Start-AutopilotMonitor.ps1
STABLE_MSI: AutopilotMonitor-Bootstrap.msi
# Authenticode via Azure Key Vault; identity and vault come from repository variables.
# Authenticode via Azure Key Vault; identity and vault come from repository variables, the
# timestamp server (CODE_SIGN_TIMESTAMP_URL) from the endpoint registry like every host here.
# CODE_SIGN_EXPECTED_SUBJECT is optional and pins the publisher in the verify steps.
CODE_SIGN_CLIENT_ID: ${{ vars.CODE_SIGN_CLIENT_ID }}
CODE_SIGN_CERTIFICATE_NAME: ${{ vars.CODE_SIGN_CERTIFICATE_NAME }}
CODE_SIGN_KEY_VAULT_URL: ${{ vars.CODE_SIGN_KEY_VAULT_URL }}
CODE_SIGN_EXPECTED_SUBJECT: ${{ vars.CODE_SIGN_EXPECTED_SUBJECT }}
CODE_SIGN_TIMESTAMP_URL: http://timestamp.acs.microsoft.com
jobs:
build:
# Signs the loader BEFORE the build: wix embeds the file as it finds it, so a loader signed
# afterwards would leave the copy inside the MSI unsigned. Devices under an AllSigned policy
# need the embedded loader to carry a signature of its own.
#
# Trust zone: only this repository's scripts, pinned actions and the pinned signing tool run
# here. No `environment:` yet: the code-signing credential is still bound to the ref, and a
# job carries exactly one OIDC subject.
sign-loader:
name: Sign bootstrap loader
runs-on: windows-latest
name: Build Bootstrap MSI
permissions:
contents: read
id-token: write # code-signing login
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# Every host in this workflow comes from the one endpoint registry.
- name: Load endpoints
shell: pwsh
run: ./.github/scripts/Get-Endpoints.ps1 -ExportToGitHubEnv
# 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.
# bootstrap-script-gates.yml runs the same checks plus Pester on pushes; this job
@@ -60,6 +75,151 @@ jobs:
}
Write-Host "Runner script parse + ASCII check passed"
# Every published MSI is signed; a missing variable stops the run before the build.
- name: Check code-signing configuration
shell: pwsh
run: |
foreach ($name in 'CODE_SIGN_CLIENT_ID', 'CODE_SIGN_CERTIFICATE_NAME', 'CODE_SIGN_KEY_VAULT_URL') {
if ([string]::IsNullOrWhiteSpace([Environment]::GetEnvironmentVariable($name))) {
throw "Repository variable $name is not set. Every published MSI must be Authenticode-signed."
}
}
- name: Login to Azure (code-signing identity)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
client-id: ${{ env.CODE_SIGN_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
allow-no-subscriptions: true
- name: Acquire Key Vault token for signing
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$token = az account get-access-token --scope https://vault.azure.net/.default --query accessToken --output tsv
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($token)) {
throw "Could not acquire a Key Vault token for the code-signing identity."
}
Write-Host "::add-mask::$token"
"CODE_SIGN_AKV_ACCESS_TOKEN=$token" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
# Version pinned; roll-forward because the tool targets an older runtime than the SDK.
- name: Install AzureSignTool
env:
DOTNET_ROLL_FORWARD: LatestMajor
run: dotnet tool install --no-cache --global AzureSignTool --version 7.0.1
# A missing timestamp fails the run: without it the signature dies with the certificate.
- name: Sign bootstrap loader (Authenticode)
shell: pwsh
env:
DOTNET_ROLL_FORWARD: LatestMajor
run: |
$ErrorActionPreference = 'Stop'
& azuresigntool.exe sign --verbose `
--azure-key-vault-url "$env:CODE_SIGN_KEY_VAULT_URL" `
--azure-key-vault-accesstoken "$env:CODE_SIGN_AKV_ACCESS_TOKEN" `
--azure-key-vault-certificate "$env:CODE_SIGN_CERTIFICATE_NAME" `
--timestamp-rfc3161 "$env:CODE_SIGN_TIMESTAMP_URL" `
--file-digest sha256 `
--description 'Autopilot Monitor bootstrap runner' `
--description-url $env:WWW_BASE_URL `
$env:RUNNER_SCRIPT
if ($LASTEXITCODE -ne 0) { throw "azuresigntool failed with exit code $LASTEXITCODE" }
$sig = Get-AuthenticodeSignature -FilePath $env:RUNNER_SCRIPT
if ($sig.Status -ne 'Valid') { throw "Signature on $env:RUNNER_SCRIPT is '$($sig.Status)', expected 'Valid'." }
if (-not $sig.TimeStamperCertificate) { throw "Signature on $env:RUNNER_SCRIPT carries no timestamp." }
if ($env:CODE_SIGN_EXPECTED_SUBJECT -and $sig.SignerCertificate.Subject -notlike $env:CODE_SIGN_EXPECTED_SUBJECT) {
throw "$env:RUNNER_SCRIPT was signed by '$($sig.SignerCertificate.Subject)', expected '$env:CODE_SIGN_EXPECTED_SUBJECT'."
}
Write-Host "$env:RUNNER_SCRIPT : Valid, timestamped, signer $($sig.SignerCertificate.Subject.Split(',')[0])"
- name: Upload signed loader
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: bootstrap-loader-signed
path: ${{ env.RUNNER_SCRIPT }}
if-no-files-found: error
retention-days: 1
# Build zone: no login, no secret, no id-token. Its only output is the artifact; the
# release job treats that as data and never builds.
build:
name: Build MSI
needs: sign-loader
runs-on: windows-latest
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Download signed loader
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: bootstrap-loader-signed
path: ${{ runner.temp }}/loader
# The signed copy replaces the checkout's unsigned one before wix embeds it.
- name: Put the signed loader in place
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
Copy-Item -Path (Join-Path $env:RUNNER_TEMP 'loader/Start-AutopilotMonitor.ps1') -Destination $env:RUNNER_SCRIPT -Force
$sig = Get-AuthenticodeSignature -FilePath $env:RUNNER_SCRIPT
if ($sig.Status -ne 'Valid') { throw "Loader to embed is '$($sig.Status)', expected 'Valid'." }
Write-Host "Embedding the signed loader ($((Get-Item $env:RUNNER_SCRIPT).Length) bytes)"
# 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.0.2
- name: Build MSI
shell: pwsh
run: |
# wix resolves File/@Source relative to the working directory, not the wxs —
# build from inside the project dir (same as the local build documented in the wxs).
$out = Join-Path (Get-Location) $env:STABLE_MSI
Push-Location $env:MSI_PROJECT_DIR
wix build Package.wxs -arch x64 -o $out
Pop-Location
if (-not (Test-Path $env:STABLE_MSI)) { Write-Error "wix build produced no MSI"; exit 1 }
Write-Host "Built $env:STABLE_MSI ($([math]::Round((Get-Item $env:STABLE_MSI).Length / 1KB)) KB)"
- name: Upload unsigned MSI
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: bootstrap-msi-unsigned
path: ${{ env.STABLE_MSI }}
if-no-files-found: error
retention-days: 1
# Trust zone: signs, verifies and publishes the MSI as data; never builds. No
# `environment:` yet, for the same reason as sign-loader.
release:
name: Sign and publish MSI
needs: build
runs-on: windows-latest
permissions:
contents: read
id-token: write # code-signing and storage logins, keyless Sigstore signing
attestations: write # store the provenance attestation
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Load endpoints
shell: pwsh
run: ./.github/scripts/Get-Endpoints.ps1 -ExportToGitHubEnv
# Read from this job's own checkout, never handed over by the build job.
- name: Read MSI version from Package.wxs
id: version
shell: pwsh
@@ -73,20 +233,19 @@ jobs:
Write-Host "MSI version (from Package.wxs): $version"
"version=$version" >> $env:GITHUB_OUTPUT
# Every published MSI is signed; a missing variable stops the run before the build.
- name: Check code-signing configuration
- name: Download unsigned MSI
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: bootstrap-msi-unsigned
path: ${{ runner.temp }}/msi
# Only the expected file leaves the download folder.
- name: Take the MSI from the build
shell: pwsh
run: |
foreach ($name in 'CODE_SIGN_CLIENT_ID', 'CODE_SIGN_CERTIFICATE_NAME', 'CODE_SIGN_KEY_VAULT_URL') {
if ([string]::IsNullOrWhiteSpace([Environment]::GetEnvironmentVariable($name))) {
throw "Repository variable $name is not set. Every published MSI must be Authenticode-signed."
}
}
run: Copy-Item -Path (Join-Path $env:RUNNER_TEMP "msi/$env:STABLE_MSI") -Destination $env:STABLE_MSI -ErrorAction Stop
# Logs in FIRST and only to fetch the Key Vault token: azure/login sets the default az
# account, so the storage login below has to be the last one. Both federated credentials
# are bound to the ref, so this job must never be given an `environment:` -- that would
# change the OIDC subject claim and break both logins.
# account, so the storage login below has to be the last one.
- name: Login to Azure (code-signing identity)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
@@ -113,50 +272,12 @@ 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.0.2
# Version pinned; roll-forward because the tool targets an older runtime than the SDK.
- name: Install AzureSignTool
env:
DOTNET_ROLL_FORWARD: LatestMajor
run: dotnet tool install --no-cache --global AzureSignTool --version 7.0.1
# BEFORE the build: wix embeds the file as it finds it, so a loader signed afterwards
# would leave the copy inside the MSI unsigned. Devices under an AllSigned policy need
# the embedded loader to carry a signature of its own.
- name: Sign bootstrap loader (Authenticode)
shell: pwsh
env:
DOTNET_ROLL_FORWARD: LatestMajor
run: |
$ErrorActionPreference = 'Stop'
& azuresigntool.exe sign --verbose `
--azure-key-vault-url "$env:CODE_SIGN_KEY_VAULT_URL" `
--azure-key-vault-accesstoken "$env:CODE_SIGN_AKV_ACCESS_TOKEN" `
--azure-key-vault-certificate "$env:CODE_SIGN_CERTIFICATE_NAME" `
--timestamp-rfc3161 "$env:CODE_SIGN_TIMESTAMP_URL" `
--file-digest sha256 `
--description 'Autopilot Monitor bootstrap runner' `
--description-url 'https://www.autopilotmonitor.com' `
$env:RUNNER_SCRIPT
if ($LASTEXITCODE -ne 0) { throw "azuresigntool failed with exit code $LASTEXITCODE" }
- name: Build MSI
shell: pwsh
run: |
# wix resolves File/@Source relative to the working directory, not the wxs —
# build from inside the project dir (same as the local build documented in the wxs).
$out = Join-Path (Get-Location) $env:STABLE_MSI
Push-Location $env:MSI_PROJECT_DIR
wix build Package.wxs -arch x64 -o $out
Pop-Location
if (-not (Test-Path $env:STABLE_MSI)) { Write-Error "wix build produced no MSI"; exit 1 }
Write-Host "Built $env:STABLE_MSI ($([math]::Round((Get-Item $env:STABLE_MSI).Length / 1KB)) KB)"
# Signing rewrites the MSI, so it runs before the hash and the upload. A missing
# timestamp fails the run: without it the signature dies with the certificate.
- name: Sign MSI (Authenticode)
@@ -172,30 +293,30 @@ jobs:
--timestamp-rfc3161 "$env:CODE_SIGN_TIMESTAMP_URL" `
--file-digest sha256 `
--description 'Autopilot Monitor Bootstrap' `
--description-url 'https://www.autopilotmonitor.com' `
--description-url $env:WWW_BASE_URL `
$env:STABLE_MSI
if ($LASTEXITCODE -ne 0) { throw "azuresigntool failed with exit code $LASTEXITCODE" }
# The MSI and the loader it carries -- verified here, before anything is published.
foreach ($file in @($env:STABLE_MSI, $env:RUNNER_SCRIPT)) {
$sig = Get-AuthenticodeSignature -FilePath $file
if ($sig.Status -ne 'Valid') { throw "Signature on $file is '$($sig.Status)', expected 'Valid'." }
if (-not $sig.TimeStamperCertificate) { throw "Signature on $file carries no timestamp." }
if ($env:CODE_SIGN_EXPECTED_SUBJECT -and $sig.SignerCertificate.Subject -notlike $env:CODE_SIGN_EXPECTED_SUBJECT) {
throw "$file was signed by '$($sig.SignerCertificate.Subject)', expected '$env:CODE_SIGN_EXPECTED_SUBJECT'."
}
Write-Host "$file : Valid, timestamped, signer $($sig.SignerCertificate.Subject.Split(',')[0])"
# The loader was verified in sign-loader; here the MSI, before anything is published.
$sig = Get-AuthenticodeSignature -FilePath $env:STABLE_MSI
if ($sig.Status -ne 'Valid') { throw "Signature on $env:STABLE_MSI is '$($sig.Status)', expected 'Valid'." }
if (-not $sig.TimeStamperCertificate) { throw "Signature on $env:STABLE_MSI carries no timestamp." }
if ($env:CODE_SIGN_EXPECTED_SUBJECT -and $sig.SignerCertificate.Subject -notlike $env:CODE_SIGN_EXPECTED_SUBJECT) {
throw "$env:STABLE_MSI was signed by '$($sig.SignerCertificate.Subject)', expected '$env:CODE_SIGN_EXPECTED_SUBJECT'."
}
Write-Host "$env:STABLE_MSI : Valid, timestamped, signer $($sig.SignerCertificate.Subject.Split(',')[0])"
Write-Host "SHA-256: $((Get-FileHash $env:STABLE_MSI -Algorithm SHA256).Hash.ToLowerInvariant())"
- name: Upload to Azure Blob Storage
if: inputs.upload_blob
shell: pwsh
env:
MSI_VERSION: ${{ steps.version.outputs.version }}
run: |
$containerUrl = "https://autopilotmonitoreu.blob.core.windows.net/agent"
$containerUrl = $env:AGENT_CONTAINER_URL
$authHeaders = & ./.github/scripts/Get-StorageAuthHeaders.ps1
$version = "${{ steps.version.outputs.version }}"
$version = $env:MSI_VERSION
# Same Send-Blob contract as build-agent.yml: no-cache on every blob because the
# stable name rotates in place behind the download alias. No legacy mirror — the
@@ -226,26 +347,26 @@ jobs:
shell: pwsh
run: |
$localSize = (Get-Item $env:STABLE_MSI).Length
$resp = Invoke-WebRequest -Uri "https://download.autopilotmonitor.com/agent/$env:STABLE_MSI" -Method Head -UseBasicParsing
$resp = Invoke-WebRequest -Uri "$env:DOWNLOAD_ALIAS_URL/$env:STABLE_MSI" -Method Head -UseBasicParsing
if ($resp.StatusCode -ne 200) { Write-Error "Alias HEAD returned $($resp.StatusCode)"; exit 1 }
$remoteSize = [long]$resp.Headers['Content-Length'][0]
if ($remoteSize -ne $localSize) {
Write-Error "Size mismatch: alias serves $remoteSize bytes, local MSI is $localSize bytes"
exit 1
}
Write-Host "Alias verified: https://download.autopilotmonitor.com/agent/$env:STABLE_MSI ($remoteSize bytes)"
Write-Host "Alias verified: $env:DOWNLOAD_ALIAS_URL/$env:STABLE_MSI ($remoteSize bytes)"
# Sigstore keyless provenance, same as the agent ZIP; covers the signed bytes. Skipped
# on runs that publish nothing -- no log entry for bytes nobody receives. Verify:
# gh attestation verify AutopilotMonitor-Bootstrap.msi --repo okieselbach/AutopilotMonitor
- name: Attest build provenance (MSI)
if: inputs.upload_blob
uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4
uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2
with:
subject-path: ${{ env.STABLE_MSI }}
- name: Upload build artifact
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: AutopilotMonitor-Bootstrap-v${{ steps.version.outputs.version }}
path: ${{ env.STABLE_MSI }}
+58 -16
View File
@@ -1,8 +1,8 @@
name: CI
# Tests, typechecks and drift-checks every push to main and every pull request:
# the .NET suites, the web and MCP suites, and a guard that the generated rule
# files still match rules/guardrails.json.
# the .NET suites, the web and MCP suites, a guard that the generated rule
# files still match rules/guardrails.json, and the duplicate-code check.
#
# The deploy workflows build but never test, so this is the only place the suites
# actually run. It does not gate anything — main is deliberately unprotected, so a
@@ -46,7 +46,6 @@ on:
- '.github/workflows/deploy-mcp.yml'
- '.github/workflows/deploy-web.yml'
- '.github/workflows/indexnow-notify.yml'
- '.github/workflows/oidc-login-test.yml'
- '.github/workflows/pester.yml'
- '.github/workflows/publish-scripts.yml'
pull_request:
@@ -80,7 +79,6 @@ on:
- '.github/workflows/deploy-mcp.yml'
- '.github/workflows/deploy-web.yml'
- '.github/workflows/indexnow-notify.yml'
- '.github/workflows/oidc-login-test.yml'
- '.github/workflows/pester.yml'
- '.github/workflows/publish-scripts.yml'
workflow_dispatch:
@@ -98,11 +96,11 @@ jobs:
# windows-latest is required: the agent test projects target net48.
runs-on: windows-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
dotnet-version: '10.0.x'
@@ -124,6 +122,9 @@ jobs:
- name: Summary Dialog tests
run: dotnet test src/Agent/AutopilotMonitor.SummaryDialog.Tests --configuration Release --no-build --verbosity quiet --logger "console;verbosity=minimal"
- name: Push tests
run: dotnet test src/Push/AutopilotMonitor.Push.Tests --configuration Release --no-build --verbosity quiet --logger "console;verbosity=minimal"
web:
name: Web (typecheck + tests)
runs-on: ubuntu-latest
@@ -131,11 +132,11 @@ jobs:
run:
working-directory: src/Web/autopilot-monitor-web
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: '22'
cache: npm
@@ -157,6 +158,12 @@ jobs:
- name: Lint
run: npm run lint -- --max-warnings 0
# Unused files, exports and dependencies (knip.json). Its own script on
# purpose: chained into `lint`, the `-- --max-warnings 0` above would be
# handed to knip and ESLint would lose its cap.
- name: Unused code
run: npm run knip
- name: Tests
run: npm test
@@ -167,11 +174,11 @@ jobs:
run:
working-directory: src/McpServer/autopilot-monitor-mcp
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: '22'
cache: npm
@@ -179,9 +186,19 @@ jobs:
- run: npm ci
# tsconfig.json covers sources, tests and scripts; the build emits via
# tsconfig.build.json.
- name: Typecheck
run: npx tsc --noEmit
# Same rule as the web job: any warning fails — fix new findings, never
# raise the cap.
- name: Lint
run: npm run lint -- --max-warnings 0
- name: Unused code
run: npm run knip
# The vector-search tests tokenize with the real all-MiniLM-L6-v2 model
# (~23 MB tokenizer + ONNX weights). transformers.js caches under
# node_modules by default, which npm ci recreates — so every CI run
@@ -189,7 +206,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@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ github.workspace }}/.hf-cache
key: hf-model-Xenova-all-MiniLM-L6-v2
@@ -203,11 +220,11 @@ jobs:
name: Generated guardrails match rules/guardrails.json
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: '22'
@@ -226,11 +243,11 @@ jobs:
name: Generated wire types match shared-manifests.json
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: '22'
@@ -244,8 +261,33 @@ jobs:
- name: Fail if the generated files drifted
run: |
if ! git diff --quiet -- src/Web/autopilot-monitor-web/utils/shared-manifests.generated.ts src/Web/autopilot-monitor-web/utils/wire-types.generated.ts src/McpServer/autopilot-monitor-mcp/src/generated/wire-types.generated.ts src/McpServer/autopilot-monitor-mcp/src/generated/wire-vocabularies.generated.ts; then
if ! git diff --quiet -- src/Web/autopilot-monitor-web/lib/generated/shared-manifests.generated.ts src/Web/autopilot-monitor-web/lib/generated/wire-types.generated.ts src/McpServer/autopilot-monitor-mcp/src/generated/wire-types.generated.ts src/McpServer/autopilot-monitor-mcp/src/generated/wire-vocabularies.generated.ts; then
echo "::error::Generated wire types are out of date. Run 'npm run generate:manifests' in src/Web/autopilot-monitor-web and commit the result."
git diff --stat
exit 1
fi
duplicates:
name: Duplicate code
runs-on: ubuntu-latest
defaults:
run:
working-directory: tools/quality
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: '22'
cache: npm
cache-dependency-path: tools/quality/package-lock.json
- run: npm ci --ignore-scripts
# jscpd over the C# and TypeScript sources (.jscpd.json). A clone missing from
# jscpd-baseline.json fails, and so does a baseline entry whose clone is gone,
# so the baseline only shrinks. Locally: npm run dup in tools/quality.
- name: Duplicate check
run: npm run dup
+4 -4
View File
@@ -28,6 +28,7 @@ on:
# Every other workflow: none of them changes the C# or TypeScript CodeQL analyzes.
# paths-ignore cannot negate, so they are listed one by one; codeql.yml itself and
# .github/codeql/ stay out of the list. 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'
@@ -37,7 +38,6 @@ on:
- '.github/workflows/deploy-mcp.yml'
- '.github/workflows/deploy-web.yml'
- '.github/workflows/indexnow-notify.yml'
- '.github/workflows/oidc-login-test.yml'
- '.github/workflows/pester.yml'
- '.github/workflows/publish-scripts.yml'
pull_request:
@@ -60,6 +60,7 @@ on:
# Every other workflow: none of them changes the C# or TypeScript CodeQL analyzes.
# paths-ignore cannot negate, so they are listed one by one; codeql.yml itself and
# .github/codeql/ stay out of the list. 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'
@@ -69,7 +70,6 @@ on:
- '.github/workflows/deploy-mcp.yml'
- '.github/workflows/deploy-web.yml'
- '.github/workflows/indexnow-notify.yml'
- '.github/workflows/oidc-login-test.yml'
- '.github/workflows/pester.yml'
- '.github/workflows/publish-scripts.yml'
schedule:
@@ -104,11 +104,11 @@ jobs:
build-mode: none
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
if: matrix.language == 'csharp'
with:
dotnet-version: '10.0.x'
+45 -46
View File
@@ -1,7 +1,7 @@
name: Validate & Combine Rules
on:
push:
branches: [main, dev]
branches: [main]
paths:
- 'rules/gather/**'
- 'rules/analyze/**'
@@ -9,6 +9,7 @@ on:
- 'rules/schema/**'
- 'rules/scripts/**'
- 'rules/guardrails.json'
- '.github/workflows/combine-rules.yml'
pull_request:
paths:
- 'rules/gather/**'
@@ -17,73 +18,71 @@ on:
- 'rules/schema/**'
- 'rules/scripts/**'
- 'rules/guardrails.json'
- '.github/workflows/combine-rules.yml'
permissions:
contents: read
jobs:
validate-and-combine:
# Pull requests and pushes alike; read-only, because the validator is third-party code.
validate:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: '22'
# Lockfile 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. rules/scripts/package-lock.json pins ajv-cli, ajv-formats
# and every transitive package (Dependabot keeps it current); ajv is pure JS and
# needs no install hooks.
- name: Install ajv-cli from the lockfile
# Lockfile and no lifecycle scripts: rules/scripts/package-lock.json pins ajv,
# ajv-formats and every transitive package (Dependabot keeps it current); ajv is pure
# JS and needs no install hooks.
- name: Install the rules validator from the lockfile
working-directory: rules/scripts
run: |
npm ci --ignore-scripts
echo "$PWD/node_modules/.bin" >> "$GITHUB_PATH"
run: npm ci --ignore-scripts
- name: Validate Gather Rules
run: |
for f in rules/gather/*.json; do
echo "Validating $f..."
ajv validate -s rules/schema/gather-rule.schema.json -d "$f" --spec=draft2020 -c ajv-formats
done
- name: Validate Analyze Rules
run: |
for f in rules/analyze/*.json; do
echo "Validating $f..."
ajv validate -s rules/schema/analyze-rule.schema.json -d "$f" --spec=draft2020 -c ajv-formats
done
- name: Validate IME Log Patterns
run: |
for f in rules/ime-log-patterns/*.json; do
echo "Validating $f..."
ajv validate -s rules/schema/ime-log-pattern.schema.json -d "$f" --spec=draft2020 -c ajv-formats
done
# guardrails.json values are baked verbatim into generated TypeScript that is
# auto-committed to main and shipped to the portal - the schema forbids control
# and line-terminator characters so a "data" PR can never become code.
- name: Validate Guardrails
run: ajv validate -s rules/schema/guardrails.schema.json -d rules/guardrails.json --spec=draft2020 -c ajv-formats
# Gather, analyze and IME log pattern files plus guardrails.json, each against its
# schema; the guardrails schema is what keeps a "data" PR from becoming code in the
# generated TypeScript (see validate.js).
- name: Validate rules and guardrails against their schemas
run: node rules/scripts/validate.js
- name: Combine rules into dist/
run: node rules/scripts/combine.js
# Plain git instead of a third-party action: this step runs with contents:write on
# main, so no code we do not control gets to push. Checkout leaves the job's
# GITHUB_TOKEN in the remote credential helper, which is all `git push` needs.
# The one job that can push to main, and it installs nothing: combine.js needs only node's
# built-ins, so no package code ever runs next to the write token. Keep it that way -- a
# dependency in combine.js would need its own job.
publish:
needs: validate
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
# Checkout leaves the job's GITHUB_TOKEN in the remote credential helper, which is all
# `git push` needs.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: '22'
- name: Combine rules into dist/
run: node rules/scripts/combine.js
# Plain git instead of a third-party action, so no code we do not control gets to push.
# NOTE: combine.js writes THREE outputs; committing only dist/ would silently discard
# the two generated TS modules and leave the guardrails-in-sync CI check red.
- name: Commit generated outputs
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: |
set -euo pipefail
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -- rules/dist/ \
src/Web/autopilot-monitor-web/utils/guardrails.generated.ts \
src/Web/autopilot-monitor-web/lib/generated/guardrails.generated.ts \
src/McpServer/autopilot-monitor-mcp/src/rule-authoring.generated.ts
if git diff --cached --quiet; then
echo "Generated outputs unchanged - nothing to commit"
+106 -21
View File
@@ -3,26 +3,41 @@ name: Deploy Backend (Azure Functions)
on:
workflow_dispatch:
# Every job states its own permissions. This default is what the build job runs with: restore
# and build execute third-party code (NuGet MSBuild targets, the Functions worker SDK) and must
# never hold more than a read-only token.
permissions:
id-token: write # OIDC federated login (secret-less)
contents: read
# Hosts (API, versions container) come from .github/scripts/Get-Endpoints.ps1.
env:
FUNCTION_APP: autopilotmonitor-api-eu
HEALTH_URL: https://autopilotmonitor-api-eu.azurewebsites.net/api/health
VERSIONS_CONTAINER: https://autopilotmonitoreu.blob.core.windows.net/versions
CSPROJ_PATH: src/Backend/AutopilotMonitor.Functions/AutopilotMonitor.Functions.csproj
jobs:
build-and-deploy:
# Runs only this repository's scripts. The deploy identity trusts the `production`
# environment, which admits main only.
reserve:
name: Reserve build number
runs-on: ubuntu-latest
name: Build and Deploy Backend
environment: production
permissions:
contents: read
id-token: write # OIDC federated login (secret-less)
outputs:
build_number: ${{ steps.version.outputs.build_number }}
version: ${{ steps.version.outputs.version }}
build_utc: ${{ steps.version.outputs.build_utc }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Load endpoints
shell: pwsh
run: ./.github/scripts/Get-Endpoints.ps1 -ExportToGitHubEnv
- name: Login to Azure (OIDC)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
@@ -30,11 +45,6 @@ jobs:
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Setup .NET 10
uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6
with:
dotnet-version: '10.0.x'
# Same counter mechanism as the agent line: reserve-before-build with ETag-CAS
# against a per-component counter blob. BuildTimestampUtc is pinned here rather
# than left to MSBuild's default so the published manifest and the value baked
@@ -43,7 +53,7 @@ jobs:
id: version
shell: pwsh
run: |
$buildNumber = & ./.github/scripts/Request-BuildNumber.ps1 -CounterUrl "${{ env.VERSIONS_CONTAINER }}/backend.txt"
$buildNumber = & ./.github/scripts/Request-BuildNumber.ps1 -CounterUrl "$env:VERSIONS_CONTAINER_URL/backend.txt"
$csproj = Get-Content ${{ env.CSPROJ_PATH }} -Raw
if ($csproj -notmatch '<VersionPrefix>(\d+\.\d+)</VersionPrefix>') {
@@ -57,20 +67,90 @@ jobs:
"version=$version" >> $env:GITHUB_OUTPUT
"build_utc=$buildUtc" >> $env:GITHUB_OUTPUT
# Build zone: no login, no secret, no id-token. Its only output is the artifact; the
# deploy job treats that as data and never builds.
build:
name: Build
needs: reserve
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Setup .NET 10
uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
dotnet-version: '10.0.x'
- name: Restore dependencies
run: dotnet restore ${{ env.CSPROJ_PATH }}
- name: Build
run: dotnet build ${{ env.CSPROJ_PATH }} --configuration Release --no-restore -p:SourceRevisionId=${{ github.sha }} -p:BuildNumber=${{ steps.version.outputs.build_number }} -p:BuildTimestampUtc=${{ steps.version.outputs.build_utc }}
run: dotnet build ${{ env.CSPROJ_PATH }} --configuration Release --no-restore -p:SourceRevisionId=${{ github.sha }} -p:BuildNumber=${{ needs.reserve.outputs.build_number }} -p:BuildTimestampUtc=${{ needs.reserve.outputs.build_utc }}
- name: Publish
run: dotnet publish ${{ env.CSPROJ_PATH }} --configuration Release --output ./publish --no-build -p:SourceRevisionId=${{ github.sha }} -p:BuildNumber=${{ steps.version.outputs.build_number }} -p:BuildTimestampUtc=${{ steps.version.outputs.build_utc }}
run: dotnet publish ${{ env.CSPROJ_PATH }} --configuration Release --output ./publish --no-build -p:SourceRevisionId=${{ github.sha }} -p:BuildNumber=${{ needs.reserve.outputs.build_number }} -p:BuildTimestampUtc=${{ needs.reserve.outputs.build_utc }}
# One archive, not a folder upload: upload-artifact skips hidden files, and the
# isolated worker cannot start without the publish output's .azurefunctions/ folder.
- name: Package publish output
run: tar -czf "$RUNNER_TEMP/backend-publish.tgz" -C publish .
- name: Upload publish output
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: backend-publish
path: ${{ runner.temp }}/backend-publish.tgz
if-no-files-found: error
retention-days: 1
# Trust zone: only this repository's scripts and pinned actions run here, never a restore
# or build. The package is unpacked outside the checkout, so nothing in it can replace the
# scripts this job runs.
deploy:
name: Deploy and verify
needs: [reserve, build]
runs-on: ubuntu-latest
environment: production
permissions:
contents: read
id-token: write # OIDC federated login (secret-less)
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Load endpoints
shell: pwsh
run: ./.github/scripts/Get-Endpoints.ps1 -ExportToGitHubEnv
- name: Download publish output
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: backend-publish
path: ${{ runner.temp }}/backend-artifact
# GNU tar refuses members with '..' and strips leading '/'.
- name: Unpack publish output
run: |
mkdir "$RUNNER_TEMP/publish"
tar -xzf "$RUNNER_TEMP/backend-artifact/backend-publish.tgz" -C "$RUNNER_TEMP/publish"
- name: Login to Azure (OIDC)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Deploy to Azure Functions
uses: Azure/functions-action@c5060b3b8bb1ebbcb531abd00c822ecbaa8ea656 # v1
uses: Azure/functions-action@c5060b3b8bb1ebbcb531abd00c822ecbaa8ea656 # v1.5.7
with:
app-name: ${{ env.FUNCTION_APP }}
package: ./publish
package: ${{ runner.temp }}/publish
# A green deploy step only means the package was accepted. This gate makes the
# workflow assert what is actually being served, so the manifest published below
@@ -78,16 +158,18 @@ jobs:
# /api/health is IP rate-limited at 30/min; one poll per 10s stays well under.
- name: Verify deployed build is live
shell: pwsh
env:
EXPECTED_VERSION: ${{ needs.reserve.outputs.version }}
run: |
$expectedCommit = '${{ github.sha }}'.Substring(0, 7)
$expectedVersion = '${{ steps.version.outputs.version }}'
$expectedVersion = $env:EXPECTED_VERSION
$deadline = [DateTime]::UtcNow.AddMinutes(3)
$attempt = 0
while ([DateTime]::UtcNow -lt $deadline) {
$attempt++
try {
$health = Invoke-RestMethod -Uri '${{ env.HEALTH_URL }}' -Method Get -TimeoutSec 30
$health = Invoke-RestMethod -Uri "$env:API_BASE_URL/api/health" -Method Get -TimeoutSec 30
if ($health.commitHash -eq $expectedCommit -and $health.version -eq $expectedVersion) {
Write-Host "Live after $attempt attempt(s): $($health.version) @ $($health.commitHash), built $($health.buildUtc)"
exit 0
@@ -107,18 +189,21 @@ jobs:
# (rollback, stale instance) rather than a half-finished pipeline.
- name: Publish version manifest
shell: pwsh
env:
BACKEND_VERSION: ${{ needs.reserve.outputs.version }}
BACKEND_BUILD_UTC: ${{ needs.reserve.outputs.build_utc }}
run: |
$manifest = [ordered]@{
component = 'backend'
version = '${{ steps.version.outputs.version }}'
version = $env:BACKEND_VERSION
commit = '${{ github.sha }}'.Substring(0, 7)
buildUtc = '${{ steps.version.outputs.build_utc }}'
buildUtc = $env:BACKEND_BUILD_UTC
deployedUtc = [DateTime]::UtcNow.ToString('o')
runId = '${{ github.run_id }}'
} | ConvertTo-Json
$authHeaders = & ./.github/scripts/Get-StorageAuthHeaders.ps1
$url = "${{ env.VERSIONS_CONTAINER }}/backend.json"
$url = "$env:VERSIONS_CONTAINER_URL/backend.json"
# no-cache: rotating pointer, must never be served stale by any fronting cache.
Invoke-RestMethod -Uri $url -Method Put -Headers ($authHeaders + @{ 'x-ms-blob-type' = 'BlockBlob'; 'x-ms-blob-cache-control' = 'no-cache' }) -Body $manifest -ContentType 'application/json' | Out-Null
Write-Host "Published versions/backend.json"
+16 -7
View File
@@ -16,20 +16,29 @@ env:
# reference — with a sha-only tag `az containerapp update` saw the same image
# and silently created no revision (2026-08-28).
IMAGE_TAG: ${{ github.sha }}-${{ github.run_number }}
HEALTH_URL: https://mcp.autopilotmonitor.com/health
VERSIONS_CONTAINER: https://autopilotmonitoreu.blob.core.windows.net/versions
PACKAGE_JSON: src/McpServer/autopilot-monitor-mcp/package.json
# Hosts (MCP, versions container) come from .github/scripts/Get-Endpoints.ps1.
jobs:
# One job on purpose, unlike the other deploys: all third-party code (npm ci, the build, the
# embedding precompute) runs inside `docker build`, whose containers see neither the az
# login nor the registry credentials nor this job's OIDC request token. That container is
# the boundary -- never move npm onto the runner here, or the build gets the job's
# credentials. The deploy identity trusts the `production` environment, which admits main only.
build-and-deploy:
runs-on: ubuntu-latest
name: Build and Deploy MCP Server
environment: production
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Load endpoints
shell: pwsh
run: ./.github/scripts/Get-Endpoints.ps1 -ExportToGitHubEnv
# 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
# token is needed. Checking it out here rather than vendoring it keeps the
@@ -38,7 +47,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@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
repository: autopilotmonitor/autopilotmonitor-docs
@@ -62,7 +71,7 @@ jobs:
id: version
shell: pwsh
run: |
$buildNumber = & ./.github/scripts/Request-BuildNumber.ps1 -CounterUrl "${{ env.VERSIONS_CONTAINER }}/mcp.txt"
$buildNumber = & ./.github/scripts/Request-BuildNumber.ps1 -CounterUrl "$env:VERSIONS_CONTAINER_URL/mcp.txt"
$pkgVersion = (Get-Content ${{ env.PACKAGE_JSON }} -Raw | ConvertFrom-Json).version
$prefix = ($pkgVersion -split '\.')[0..1] -join '.'
@@ -117,7 +126,7 @@ jobs:
while ([DateTime]::UtcNow -lt $deadline) {
$attempt++
try {
$health = Invoke-RestMethod -Uri '${{ env.HEALTH_URL }}' -Method Get -TimeoutSec 60
$health = Invoke-RestMethod -Uri "$env:MCP_BASE_URL/health" -Method Get -TimeoutSec 60
if ($health.commit -eq $expectedCommit -and $health.version -eq $expectedVersion) {
Write-Host "Live after $attempt attempt(s): $($health.version) @ $($health.commit), docs $($health.docs.commit) ($($health.docs.chunks) chunks)"
exit 0
@@ -148,7 +157,7 @@ jobs:
} | ConvertTo-Json
$authHeaders = & ./.github/scripts/Get-StorageAuthHeaders.ps1
$url = "${{ env.VERSIONS_CONTAINER }}/mcp.json"
$url = "$env:VERSIONS_CONTAINER_URL/mcp.json"
# no-cache: rotating pointer, must never be served stale by any fronting cache.
Invoke-RestMethod -Uri $url -Method Put -Headers ($authHeaders + @{ 'x-ms-blob-type' = 'BlockBlob'; 'x-ms-blob-cache-control' = 'no-cache' }) -Body $manifest -ContentType 'application/json' | Out-Null
Write-Host "Published versions/mcp.json"
+124 -33
View File
@@ -5,6 +5,11 @@ name: Deploy Web (Static Web App)
# path ci.yml tests (setup-node 22 + npm ci + next build) instead of Oryx guessing inside the
# action's container, and the exported site is attested like the agent ZIP and the MSI.
#
# Three jobs, each holding only what it needs: build runs all third-party code without any
# credential, deploy holds the SWA token alone, record attests, verifies and writes the
# manifest. A failed record job is re-run on its own; it writes only once the live site
# serves this commit.
#
# PR preview environments were dropped with the action: their per-PR URLs are never
# registered as redirect URIs, so nobody could log in on them anyway.
@@ -17,38 +22,39 @@ on:
- '.github/workflows/deploy-web.yml'
workflow_dispatch:
# Every job states its own permissions. This default is what the build job runs with: npm ci
# (install scripts included) and next build execute third-party code and must never hold more
# than a read-only token.
permissions:
contents: read
id-token: write # keyless Sigstore signing (artifact attestation)
attestations: write # store the provenance attestation
# Hosts (API, portal, storage) come from .github/scripts/Get-Endpoints.ps1, never from a
# repository variable.
env:
PORTAL_VERSION_URL: https://portal.autopilotmonitor.com/version.json
VERSIONS_CONTAINER: https://autopilotmonitoreu.blob.core.windows.net/versions
WEB_DIR: src/Web/autopilot-monitor-web
# Exact version: `npx --package` resolves from the registry at run time, so the pin is the
# only thing that keeps the deploy step's code fixed. Bump deliberately.
SWA_CLI_VERSION: 2.0.10
jobs:
build_and_deploy_job:
# Build zone: no login, no id-token. The one secret here is the Application Insights
# connection string, which ships inside the public bundle anyway.
build:
name: Build
runs-on: ubuntu-latest
name: Build and Deploy Job
outputs:
docs_sha: ${{ steps.docs.outputs.sha }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
lfs: false
fetch-depth: 0 # Full history needed for git-based sitemap lastmod dates
# Only the version manifest at the very end needs Azure; logging in here rather than
# next to that step keeps a credential problem from surfacing after a live deploy.
- name: Login to Azure (OIDC)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Load endpoints
shell: pwsh
run: ./.github/scripts/Get-Endpoints.ps1 -ExportToGitHubEnv
# The customer docs repo is the single source of the "What's new" panel: the prebuild
# step (scripts/whats-new/build-whats-new.js) turns changelog/*.md into
@@ -57,7 +63,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@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
repository: autopilotmonitor/autopilotmonitor-docs
@@ -68,7 +74,7 @@ jobs:
id: docs
run: echo "sha=$(git -C whats-new-docs rev-parse HEAD)" >> "$GITHUB_OUTPUT"
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: '22'
cache: npm
@@ -105,10 +111,6 @@ jobs:
# and the default app for browsers without a stored selection (legacy|primary).
NEXT_PUBLIC_ENTRA_LEGACY_CLIENT_ID: ${{ vars.NEXT_PUBLIC_ENTRA_LEGACY_CLIENT_ID }}
NEXT_PUBLIC_ENTRA_DEFAULT_APP: ${{ vars.NEXT_PUBLIC_ENTRA_DEFAULT_APP }}
NEXT_PUBLIC_ENTRA_REDIRECT_URI: ${{ vars.NEXT_PUBLIC_ENTRA_REDIRECT_URI }}
NEXT_PUBLIC_ENTRA_POST_LOGOUT_REDIRECT_URI: ${{ vars.NEXT_PUBLIC_ENTRA_POST_LOGOUT_REDIRECT_URI }}
NEXT_PUBLIC_API_BASE_URL: ${{ vars.NEXT_PUBLIC_API_BASE_URL }}
NEXT_PUBLIC_PLATFORM_STATS_MANIFEST_URL: ${{ vars.NEXT_PUBLIC_PLATFORM_STATS_MANIFEST_URL }}
NEXT_PUBLIC_APPINSIGHTS_CONNECTION_STRING: ${{ secrets.NEXT_PUBLIC_APPINSIGHTS_CONNECTION_STRING }}
# Build stamp for public/version.json (scripts/generate-version.js in prebuild);
# the verify step below polls the deployed file for exactly this commit.
@@ -118,9 +120,24 @@ jobs:
# Star counter from the step above; empty when the lookup failed.
NEXT_PUBLIC_GITHUB_STARS: ${{ steps.stars.outputs.count }}
run: |
# All from the endpoint registry (Load endpoints). Empty, the portal would silently call
# localhost:7071. The stats manifest is what browsers load the live numbers from, and the
# build reads it once to bake the numbers of this moment into the landing page's static
# HTML (lib/platformStats.ts). The redirect URIs are only the fallback for the static
# render; in the browser lib/msalConfig.ts uses window.location.origin.
export NEXT_PUBLIC_API_BASE_URL="$API_BASE_URL"
export NEXT_PUBLIC_PLATFORM_STATS_MANIFEST_URL="$STATS_CONTAINER_URL"
export NEXT_PUBLIC_ENTRA_REDIRECT_URI="$WWW_BASE_URL"
export NEXT_PUBLIC_ENTRA_POST_LOGOUT_REDIRECT_URI="$WWW_BASE_URL"
test -n "$NEXT_PUBLIC_API_BASE_URL" || { echo "::error::API_BASE_URL is empty - the portal would call localhost"; exit 1; }
test -n "$NEXT_PUBLIC_PLATFORM_STATS_MANIFEST_URL" || { echo "::error::STATS_CONTAINER_URL is empty"; exit 1; }
test -n "$NEXT_PUBLIC_ENTRA_REDIRECT_URI" || { echo "::error::WWW_BASE_URL is empty"; exit 1; }
npm run build
test -f out/version.json || { echo "out/version.json missing - export did not run"; exit 1; }
test -f out/whats-new.json || { echo "out/whats-new.json missing - whats-new prebuild did not run"; exit 1; }
# Fail-soft like the star count: browsers still load the stats live; only crawlers and
# LLM fetchers, which run no JS, see no numbers until the next deploy.
grep -q 'data-stats-snapshot=' out/index.html || echo "::warning::Platform stats were not baked into the landing page (reason in the build log) - crawlers and LLM fetchers see no numbers until the next deploy"
# One archive of the exact bytes that get uploaded, so the attestation has a single subject.
- name: Package export
@@ -131,19 +148,37 @@ jobs:
echo "archive=$archive" >> "$GITHUB_OUTPUT"
sha256sum "$archive"
# Sigstore keyless provenance: binds the export digest to this workflow run (repo, ref,
# 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@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4
with:
subject-path: ${{ steps.package.outputs.archive }}
- name: Upload build artifact
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: portal-${{ github.sha }}
path: ${{ steps.package.outputs.archive }}
if-no-files-found: error
# Holds the SWA deployment token and nothing else -- deliberately no id-token: the SWA CLI
# is third-party npm code whose dependencies resolve at run time.
deploy:
name: Deploy to Static Web App
needs: build
runs-on: ubuntu-latest
environment: production
steps:
# For staticwebapp.config.json only.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Download build artifact
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: portal-${{ github.sha }}
path: ${{ runner.temp }}/portal
- name: Unpack export
run: |
mkdir "$WEB_DIR/out"
tar -xzf "$RUNNER_TEMP/portal/portal-${GITHUB_SHA::7}.tar.gz" -C "$WEB_DIR/out"
# Upload only -- out/ is already built. --swa-config-location points at the app root,
# where staticwebapp.config.json lives (next export does not copy it into out/).
@@ -158,6 +193,48 @@ jobs:
--env production \
--no-use-keychain
# Trust zone: only this repository's scripts and pinned actions run here. The deploy
# identity trusts the `production` environment, which admits main only.
record:
name: Attest, verify and record
needs: [build, deploy]
runs-on: ubuntu-latest
environment: production
permissions:
contents: read
id-token: write # keyless Sigstore signing, OIDC login for the manifest
attestations: write # store the provenance attestation
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Load endpoints
shell: pwsh
run: ./.github/scripts/Get-Endpoints.ps1 -ExportToGitHubEnv
- name: Download build artifact
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: portal-${{ github.sha }}
path: ${{ runner.temp }}/portal
- name: Locate the export archive
id: archive
run: |
path="$RUNNER_TEMP/portal/portal-${GITHUB_SHA::7}.tar.gz"
test -f "$path" || { echo "::error::Export archive for this commit is missing"; exit 1; }
echo "path=$path" >> "$GITHUB_OUTPUT"
# Sigstore keyless provenance: binds the export digest to this workflow run (repo, ref,
# 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@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2
with:
subject-path: ${{ steps.archive.outputs.path }}
# The CLI reporting success proves the upload, not that the production host serves the
# new build -- verify against the live /version.json (Cache-Control: no-store via
# staticwebapp.config.json) before publishing the version manifest.
@@ -171,7 +248,7 @@ jobs:
while ([DateTime]::UtcNow -lt $deadline) {
$attempt++
try {
$v = Invoke-RestMethod -Uri '${{ env.PORTAL_VERSION_URL }}' -Method Get -TimeoutSec 30 -Headers @{ 'Cache-Control' = 'no-cache' }
$v = Invoke-RestMethod -Uri "$env:PORTAL_BASE_URL/version.json" -Method Get -TimeoutSec 30 -Headers @{ 'Cache-Control' = 'no-cache' }
if ($v.commit -eq $expectedCommit) {
Write-Host "Live after $attempt attempt(s): web @ $($v.commit), built $($v.buildUtc)"
exit 0
@@ -185,22 +262,36 @@ jobs:
throw "Portal did not serve commit $expectedCommit within 5 minutes -- deployment not verified, version manifest NOT published"
- name: Login to Azure (OIDC)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# Cross-component deploy record, written last and only on a verified deploy --
# same contract as versions/backend.json and versions/mcp.json. docsCommit says which
# docs revision the live What's new panel was generated from (as for the MCP corpus).
# The docs commit comes from the build job, so it arrives as data and is checked
# against the shape of a commit id before it is used.
- name: Publish version manifest
shell: pwsh
env:
DOCS_SHA: ${{ needs.build.outputs.docs_sha }}
run: |
if ($env:DOCS_SHA -notmatch '^[0-9a-f]{40}$') {
throw "The build job handed over no valid docs commit -- version manifest NOT published."
}
$manifest = [ordered]@{
component = 'web'
commit = '${{ github.sha }}'.Substring(0, 7)
deployedUtc = [DateTime]::UtcNow.ToString('o')
runId = '${{ github.run_id }}'
docsCommit = '${{ steps.docs.outputs.sha }}'.Substring(0, 7)
docsCommit = $env:DOCS_SHA.Substring(0, 7)
} | ConvertTo-Json
$authHeaders = & ./.github/scripts/Get-StorageAuthHeaders.ps1
$url = "${{ env.VERSIONS_CONTAINER }}/web.json"
$url = "$env:VERSIONS_CONTAINER_URL/web.json"
# no-cache: rotating pointer, must never be served stale by any fronting cache.
Invoke-RestMethod -Uri $url -Method Put -Headers ($authHeaders + @{ 'x-ms-blob-type' = 'BlockBlob'; 'x-ms-blob-cache-control' = 'no-cache' }) -Body $manifest -ContentType 'application/json' | Out-Null
Write-Host "Published versions/web.json"
+15 -5
View File
@@ -3,24 +3,34 @@ name: IndexNow Notify (Bing/Yandex)
on:
workflow_dispatch:
# The job only talks to public endpoints; it never needs a repository token.
permissions: {}
# Read-only, for the checkout of the endpoint registry; beyond that the job only talks to
# public endpoints.
permissions:
contents: read
jobs:
notify:
runs-on: ubuntu-latest
name: Submit URLs to IndexNow
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Load endpoints
shell: pwsh
run: ./.github/scripts/Get-Endpoints.ps1 -ExportToGitHubEnv
- name: Notify IndexNow
run: |
URLS=$(curl -s https://www.autopilotmonitor.com/sitemap.xml | grep -oP '<loc>\K[^<]+')
URLS=$(curl -s "$WWW_BASE_URL/sitemap.xml" | grep -oP '<loc>\K[^<]+')
URL_JSON=$(echo "$URLS" | jq -R -s 'split("\n") | map(select(length > 0))')
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "https://api.indexnow.org/indexnow" \
-H "Content-Type: application/json" \
-d "{
\"host\": \"www.autopilotmonitor.com\",
\"host\": \"${WWW_BASE_URL#https://}\",
\"key\": \"8f1710b5-f14b-477e-938b-6c01ef851661\",
\"keyLocation\": \"https://www.autopilotmonitor.com/IndexNow.txt\",
\"keyLocation\": \"$WWW_BASE_URL/IndexNow.txt\",
\"urlList\": $URL_JSON
}")
echo "IndexNow response: HTTP $RESPONSE"
-28
View File
@@ -1,28 +0,0 @@
# TEMPORARY: proves the secret-less OIDC federated login works end-to-end.
# Delete after the cross-tenant migration is verified (or keep as a cheap smoke test).
name: OIDC Login Test
on:
workflow_dispatch:
permissions:
id-token: write
contents: read
jobs:
login-test:
runs-on: ubuntu-latest
name: Federated login + read check
steps:
- name: Login to Azure (OIDC)
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Prove identity and RBAC scope
run: |
az account show --query "{subscription:name, subscriptionId:id, tenantId:tenantId, user:user.name}" -o json
echo "--- function app visible with Contributor scope? ---"
az functionapp list --query "[].name" -o json
+4 -5
View File
@@ -36,13 +36,13 @@ concurrency:
# Authenticode for the customer scripts. Same identity and same repository variables as
# build-agent.yml -- the loader on every device pins this publisher, so the two must not
# drift apart.
# drift apart. The publisher takes every host, the timestamp server included, from
# .github/scripts/Get-Endpoints.ps1.
env:
CODE_SIGN_CLIENT_ID: ${{ vars.CODE_SIGN_CLIENT_ID }}
CODE_SIGN_CERTIFICATE_NAME: ${{ vars.CODE_SIGN_CERTIFICATE_NAME }}
CODE_SIGN_KEY_VAULT_URL: ${{ vars.CODE_SIGN_KEY_VAULT_URL }}
CODE_SIGN_EXPECTED_SUBJECT: ${{ vars.CODE_SIGN_EXPECTED_SUBJECT }}
CODE_SIGN_TIMESTAMP_URL: http://timestamp.acs.microsoft.com
jobs:
# $/ is the self-repository form: this repository at the running commit, never a
@@ -60,7 +60,7 @@ jobs:
contents: read
id-token: write # OIDC federated login for the storage writes (secret-less)
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
@@ -124,7 +124,6 @@ jobs:
-SigningToken $env:CODE_SIGN_AKV_ACCESS_TOKEN `
-KeyVaultUrl $env:CODE_SIGN_KEY_VAULT_URL `
-CertificateName $env:CODE_SIGN_CERTIFICATE_NAME `
-TimestampUrl $env:CODE_SIGN_TIMESTAMP_URL `
-ExpectedPublisher $env:CODE_SIGN_EXPECTED_SUBJECT `
-SignedOutputDir "$env:RUNNER_TEMP/signed-scripts" `
-DryRun:$dryRun
@@ -135,7 +134,7 @@ jobs:
# when someone wants to look at the file.
- name: Upload signed scripts
if: always()
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: signed-scripts
path: ${{ runner.temp }}/signed-scripts
+33
View File
@@ -31,6 +31,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.Agent.V2.C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.SummaryDialog.Tests", "src\Agent\AutopilotMonitor.SummaryDialog.Tests\AutopilotMonitor.SummaryDialog.Tests.csproj", "{4F1234C1-9387-4D2C-93E0-713698A2DFCB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Push", "Push", "{09266D87-DDB8-2F61-A22F-10D1DD295F4F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.Push", "src\Push\AutopilotMonitor.Push\AutopilotMonitor.Push.csproj", "{2C3E71E4-0049-45A5-839D-F4171B47D5F3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.Push.Tests", "src\Push\AutopilotMonitor.Push.Tests\AutopilotMonitor.Push.Tests.csproj", "{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -161,6 +167,30 @@ Global
{4F1234C1-9387-4D2C-93E0-713698A2DFCB}.Release|x64.Build.0 = Release|Any CPU
{4F1234C1-9387-4D2C-93E0-713698A2DFCB}.Release|x86.ActiveCfg = Release|Any CPU
{4F1234C1-9387-4D2C-93E0-713698A2DFCB}.Release|x86.Build.0 = Release|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Debug|x64.ActiveCfg = Debug|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Debug|x64.Build.0 = Debug|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Debug|x86.ActiveCfg = Debug|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Debug|x86.Build.0 = Debug|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Release|Any CPU.Build.0 = Release|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Release|x64.ActiveCfg = Release|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Release|x64.Build.0 = Release|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Release|x86.ActiveCfg = Release|Any CPU
{2C3E71E4-0049-45A5-839D-F4171B47D5F3}.Release|x86.Build.0 = Release|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Debug|x64.ActiveCfg = Debug|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Debug|x64.Build.0 = Debug|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Debug|x86.ActiveCfg = Debug|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Debug|x86.Build.0 = Debug|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Release|Any CPU.Build.0 = Release|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Release|x64.ActiveCfg = Release|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Release|x64.Build.0 = Release|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Release|x86.ActiveCfg = Release|Any CPU
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -179,5 +209,8 @@ Global
{11B36E5C-B512-4D1E-A76B-E5AD866B033C} = {096B6098-F141-7114-B090-72D1899D8853}
{95A09BF0-4E22-41D9-9B43-8D6ECC2C8AD0} = {096B6098-F141-7114-B090-72D1899D8853}
{4F1234C1-9387-4D2C-93E0-713698A2DFCB} = {096B6098-F141-7114-B090-72D1899D8853}
{09266D87-DDB8-2F61-A22F-10D1DD295F4F} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{2C3E71E4-0049-45A5-839D-F4171B47D5F3} = {09266D87-DDB8-2F61-A22F-10D1DD295F4F}
{6BB95C52-F9D5-4C48-A107-B2B1FD5EE302} = {09266D87-DDB8-2F61-A22F-10D1DD295F4F}
EndGlobalSection
EndGlobal
+3
View File
@@ -8,5 +8,8 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
<WarningsNotAsErrors>NU1901;NU1902;NU1903;NU1904</WarningsNotAsErrors>
<!-- Run the IDE code-style analyzers in the build too, so the rules .editorconfig raises
to warning fail it like any compiler warning. -->
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
</Project>
+1 -1
Submodule infra updated: be0165a223...0085deab45
+16 -3
View File
@@ -2,10 +2,10 @@
"$schema": "../schema/analyze-rule.schema.json",
"ruleId": "ANALYZE-DEV-004",
"title": "Windows Update Failed During Enrollment",
"description": "Detects a Windows quality/cumulative update that FAILED to install during the OOBE / ESP enrollment window (WindowsUpdateClient EventID 20). A cumulative update failing mid-OOBE is a known cause of enrollment errors and is otherwise invisible in the Intune console.",
"description": "Detects a Windows quality/cumulative update that FAILED to install or download during the OOBE / ESP enrollment window (WindowsUpdateClient EventID 20 from the System log, 31 from the Operational channel). A cumulative update failing mid-OOBE is a known cause of enrollment errors and is otherwise invisible in the Intune console.",
"severity": "high",
"category": "device",
"version": "1.1.0",
"version": "1.2.0",
"enabled": true,
"evaluateOn": [
"enrollment_end",
@@ -18,10 +18,14 @@
"conditions": [
{
"signal": "wu_install_failed",
"source": "event_type",
"source": "event_data",
"eventType": "windows_update_failed",
"dataField": "wuEventId",
"operator": "exists",
"value": "",
"filterField": "wuPhase",
"filterOperator": "in",
"filterValue": "install,download",
"required": true
},
{
@@ -31,6 +35,9 @@
"dataField": "updateTitle",
"operator": "exists",
"value": "",
"filterField": "wuPhase",
"filterOperator": "in",
"filterValue": "install,download",
"required": false
},
{
@@ -40,6 +47,9 @@
"dataField": "hresult",
"operator": "exists",
"value": "",
"filterField": "wuPhase",
"filterOperator": "in",
"filterValue": "install,download",
"required": false
},
{
@@ -49,6 +59,9 @@
"dataField": "hresultSymbol",
"operator": "exists",
"value": "",
"filterField": "wuPhase",
"filterOperator": "in",
"filterValue": "install,download",
"required": false
},
{
+3 -3
View File
@@ -5,7 +5,7 @@
"description": "The device's real-time clock deviates from NTP by more than two minutes. Microsoft documents that a clock off by several minutes causes TPM attestation errors (self-deploying/pre-provisioning) and ESP timeouts.",
"severity": "warning",
"category": "device",
"version": "1.1.0",
"version": "1.1.1",
"author": "Autopilot Monitor",
"enabled": true,
"evaluateOn": [
@@ -17,7 +17,7 @@
"confidenceThreshold": 50,
"conditions": [
{
"signal": "clock_ahead",
"signal": "clock_behind",
"source": "event_data",
"eventType": "ntp_time_check",
"dataField": "offsetSeconds",
@@ -26,7 +26,7 @@
"required": false
},
{
"signal": "clock_behind",
"signal": "clock_ahead",
"source": "event_data",
"eventType": "ntp_time_check",
"dataField": "offsetSeconds",
+118
View File
@@ -0,0 +1,118 @@
{
"$schema": "../schema/analyze-rule.schema.json",
"ruleId": "ANALYZE-DEV-013",
"title": "OOBE Quality Update Failed",
"description": "Detects that the Windows quality update page at the end of OOBE (Enrollment Status Page setting \"Install Windows quality updates\") reported a failed download or install and recorded no successful install afterwards — the page's own failure names on the oobe_update_page events. Setup usually continues, so the failure is otherwise invisible: the device finishes OOBE without the monthly security update. An OS build change across a restart (os_build_changed) vetoes the finding, and so does the agent's names_capped marker — once the page's event names were capped, a missing success name proves nothing. A downloadInstallFailureHelper entry alone is no failure: updates that installed fine log it too.",
"severity": "warning",
"category": "device",
"version": "1.0.0",
"enabled": true,
"evaluateOn": [
"enrollment_end"
],
"trigger": "single",
"baseConfidence": 75,
"confidenceThreshold": 60,
"conditions": [
{
"signal": "update_failure",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "in",
"value": "SdxWebAppCloudNDUP_processStatusChangeFromHandler_downloadFailedError,SdxWebAppCloudNDUP_processStatusChangeFromHandler_installFailedError,ExpeditedUpdate_commitExpeditionDownloadInstallAsyncFailure",
"required": true
},
{
"signal": "no_install_success",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "not_exists",
"value": "",
"filterField": "name",
"filterOperator": "regex",
"filterValue": "installSucceededRebootRequired|commitExpeditionDownloadInstallAsyncSucceeded",
"required": true
},
{
"signal": "os_build_unchanged",
"source": "event_type",
"eventType": "os_build_changed",
"operator": "not_exists",
"value": "",
"required": true
},
{
"signal": "page_names_complete",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "cxhEvent",
"operator": "not_exists",
"value": "",
"filterField": "cxhEvent",
"filterOperator": "equals",
"filterValue": "names_capped",
"required": true
},
{
"signal": "update_page_failed",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "result",
"operator": "equals",
"value": "fail",
"filterField": "page",
"filterOperator": "equals",
"filterValue": "OobeNDUP",
"required": false
},
{
"signal": "update_skipped",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "regex",
"value": "SkipDownloadInstallButtonClicked$",
"required": false
}
],
"confidenceFactors": [
{
"signal": "update_page_failed",
"condition": "exists",
"weight": 10
}
],
"explanation": "The Windows quality update page at the end of OOBE reported `{{update_failure}}`, and no successful install was recorded afterwards. When the download or install of the update fails, the page gives up and setup continues, so the enrollment itself can still succeed — but the device most likely finished OOBE without the monthly security update and installs it only later, through the Windows Update policy that applies after enrollment. Selecting Skip on the page after the failure leads to the same result.",
"remediation": [
{
"title": "Find out why the update failed",
"steps": [
"Open the oobe_update_page events around the failure in the session timeline: the download progress before it shows how far the download got, and a Skip after it shows that someone chose to continue.",
"Check the network path the device uses during OOBE for Windows Update and Delivery Optimization: proxy, TLS inspection and firewall rules for the Windows Update endpoints.",
"Compare with other devices: the same failure on many devices of one site points at the network path, on one model at the device, and across the fleet at the update itself."
]
},
{
"title": "Make sure the device gets the update",
"steps": [
"Verify the device's patch level after enrollment; the update ring assigned to the device installs the missing monthly security update on its normal schedule.",
"Where the OOBE quality update keeps failing for one network or device group, decide per Enrollment Status Page profile whether \"Install Windows quality updates\" should stay on."
]
}
],
"relatedDocs": [
{
"title": "Microsoft Learn: Install Windows quality updates during OOBE (Enrollment Status Page)",
"url": "https://learn.microsoft.com/en-us/intune/device-enrollment/windows/setup-status-page#windows-monthly-security-update-release-details"
}
],
"markSessionAsFailedDefault": false,
"tags": [
"device",
"windows-update",
"oobe",
"quality-update"
]
}
+119
View File
@@ -0,0 +1,119 @@
{
"$schema": "../schema/analyze-rule.schema.json",
"ruleId": "ANALYZE-DEV-014",
"title": "OOBE Quality Update Not Offered After an Earlier Failure",
"description": "Detects that the Windows quality update page at the end of OOBE found no update to install although the device is out of date, on a device whose update page counts an earlier failed download or install (oobe_update_page: NDUPDownloadInstallPreviousFailureCount above 0, osUpdatesWithNDUPPI false, getDaysOutOfDateSucceeded above 0). No failure and no install success are recorded in this session — a failure in this session is ANALYZE-DEV-013 — and the agent's names_capped marker vetoes the finding, because a missing name proves nothing once the page's names were capped. The device finishes OOBE without the monthly security update.",
"severity": "warning",
"category": "device",
"version": "1.0.0",
"enabled": true,
"evaluateOn": [
"enrollment_end"
],
"trigger": "single",
"baseConfidence": 75,
"confidenceThreshold": 60,
"conditions": [
{
"signal": "previous_failures",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "value",
"operator": "gt",
"value": "0",
"filterField": "name",
"filterOperator": "equals",
"filterValue": "SdxWebAppCloudNDUP_initialize_NDUPDownloadInstallPreviousFailureCount",
"required": true
},
{
"signal": "nothing_offered",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "value",
"operator": "equals",
"value": "false",
"filterField": "name",
"filterOperator": "equals",
"filterValue": "SdxWebAppCloudNDUP_updateManager_processUpdatesMetadata_osUpdatesWithNDUPPI",
"required": true
},
{
"signal": "days_out_of_date",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "value",
"operator": "gt",
"value": "0",
"filterField": "name",
"filterOperator": "equals",
"filterValue": "ExpeditedUpdate_getDaysOutOfDateSucceeded",
"required": true
},
{
"signal": "no_update_failure",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "not_exists",
"value": "",
"filterField": "name",
"filterOperator": "in",
"filterValue": "SdxWebAppCloudNDUP_processStatusChangeFromHandler_downloadFailedError,SdxWebAppCloudNDUP_processStatusChangeFromHandler_installFailedError,ExpeditedUpdate_commitExpeditionDownloadInstallAsyncFailure",
"required": true
},
{
"signal": "no_install_success",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "not_exists",
"value": "",
"filterField": "name",
"filterOperator": "regex",
"filterValue": "installSucceededRebootRequired|commitExpeditionDownloadInstallAsyncSucceeded",
"required": true
},
{
"signal": "page_names_complete",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "cxhEvent",
"operator": "not_exists",
"value": "",
"filterField": "cxhEvent",
"filterOperator": "equals",
"filterValue": "names_capped",
"required": true
}
],
"explanation": "The Windows quality update page at the end of OOBE checked for updates and found none to install, although Windows reports the device as {{days_out_of_date}} days out of date. The page's failure counter for this device stands at {{previous_failures}}: an earlier download or install of an OOBE quality update on this device failed. The device finished OOBE without the monthly security update and installs it only later, through the Windows Update policy that applies after enrollment.",
"remediation": [
{
"title": "Check the earlier attempt",
"steps": [
"Look at the device's earlier enrollment sessions — the failed attempt and its ANALYZE-DEV-013 finding are usually there.",
"Verify the device's patch level after enrollment; the update ring assigned to the device installs the missing monthly security update on its normal schedule."
]
},
{
"title": "Reduce how far behind devices start",
"steps": [
"Devices that start OOBE many months behind need a long update during setup; a current OS image or factory build shortens it and gives a failed attempt less to repeat."
]
}
],
"relatedDocs": [
{
"title": "Microsoft Learn: Install Windows quality updates during OOBE (Enrollment Status Page)",
"url": "https://learn.microsoft.com/en-us/intune/device-enrollment/windows/setup-status-page#windows-monthly-security-update-release-details"
}
],
"markSessionAsFailedDefault": false,
"tags": [
"device",
"windows-update",
"oobe",
"quality-update"
]
}
+117
View File
@@ -0,0 +1,117 @@
{
"$schema": "../schema/analyze-rule.schema.json",
"ruleId": "ANALYZE-DEV-015",
"title": "OOBE Update Check Ended Without a Result",
"description": "Detects that the Windows quality update page at the end of OOBE started its Windows Update scan but ended without a scan result (oobe_update_page: startWUScanStartedSuccedded, an OobeNDUP stop and no ExpeditedUpdate_getUpdateResults name), on a device that is out of date and allowed to update. In the observed sessions the page ended two minutes after the scan started, on devices more than a year behind. The device finishes OOBE without the monthly security update. The agent's names_capped marker vetoes the finding: once the page's event names were capped, a missing scan result proves nothing.",
"severity": "warning",
"category": "device",
"version": "1.0.0",
"enabled": true,
"evaluateOn": [
"enrollment_end"
],
"trigger": "single",
"baseConfidence": 75,
"confidenceThreshold": 60,
"conditions": [
{
"signal": "update_allowed",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "value",
"operator": "equals",
"value": "true",
"filterField": "name",
"filterOperator": "equals",
"filterValue": "ExpeditedUpdate_isNDUPAllowedByCSPSucceeded",
"required": true
},
{
"signal": "scan_started",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "equals",
"value": "ExpeditedUpdate_startWUScanStartedSuccedded",
"required": true
},
{
"signal": "no_scan_result",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "not_exists",
"value": "",
"filterField": "name",
"filterOperator": "regex",
"filterValue": "^ExpeditedUpdate_getUpdateResults",
"required": true
},
{
"signal": "update_page_ended",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "cxhEvent",
"operator": "equals",
"value": "page_stopped",
"filterField": "page",
"filterOperator": "equals",
"filterValue": "OobeNDUP",
"required": true
},
{
"signal": "days_out_of_date",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "value",
"operator": "gt",
"value": "0",
"filterField": "name",
"filterOperator": "equals",
"filterValue": "ExpeditedUpdate_getDaysOutOfDateSucceeded",
"required": true
},
{
"signal": "page_names_complete",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "cxhEvent",
"operator": "not_exists",
"value": "",
"filterField": "cxhEvent",
"filterOperator": "equals",
"filterValue": "names_capped",
"required": true
}
],
"explanation": "The Windows quality update page at the end of OOBE started a Windows Update scan, but no scan result arrived before the page ended. The device is {{days_out_of_date}} days out of date, so an update was due — the device finished OOBE without the monthly security update and installs it only later, through the Windows Update policy that applies after enrollment. The page waits only a short time for the scan (two minutes in the observed sessions): a scan that takes longer, or cannot reach Windows Update, ends the page without an update.",
"remediation": [
{
"title": "Check the path to Windows Update during OOBE",
"steps": [
"Verify that devices in OOBE reach the Windows Update service: proxy, TLS inspection and firewall rules for the Windows Update endpoints.",
"Check whether an update policy applied during the Enrollment Status Page points the scan at another source, such as WSUS."
]
},
{
"title": "Shorten the scan",
"steps": [
"Devices far behind scan longer; a current OS image or factory build keeps the OOBE scan short.",
"Verify the device's patch level after enrollment; the update ring assigned to the device installs the missing monthly security update on its normal schedule."
]
}
],
"relatedDocs": [
{
"title": "Microsoft Learn: Install Windows quality updates during OOBE (Enrollment Status Page)",
"url": "https://learn.microsoft.com/en-us/intune/device-enrollment/windows/setup-status-page#windows-monthly-security-update-release-details"
}
],
"markSessionAsFailedDefault": false,
"tags": [
"device",
"windows-update",
"oobe",
"quality-update"
]
}
+370 -6
View File
@@ -1385,10 +1385,10 @@
{
"ruleId": "ANALYZE-DEV-004",
"title": "Windows Update Failed During Enrollment",
"description": "Detects a Windows quality/cumulative update that FAILED to install during the OOBE / ESP enrollment window (WindowsUpdateClient EventID 20). A cumulative update failing mid-OOBE is a known cause of enrollment errors and is otherwise invisible in the Intune console.",
"description": "Detects a Windows quality/cumulative update that FAILED to install or download during the OOBE / ESP enrollment window (WindowsUpdateClient EventID 20 from the System log, 31 from the Operational channel). A cumulative update failing mid-OOBE is a known cause of enrollment errors and is otherwise invisible in the Intune console.",
"severity": "high",
"category": "device",
"version": "1.1.0",
"version": "1.2.0",
"enabled": true,
"evaluateOn": [
"enrollment_end",
@@ -1401,10 +1401,14 @@
"conditions": [
{
"signal": "wu_install_failed",
"source": "event_type",
"source": "event_data",
"eventType": "windows_update_failed",
"dataField": "wuEventId",
"operator": "exists",
"value": "",
"filterField": "wuPhase",
"filterOperator": "in",
"filterValue": "install,download",
"required": true
},
{
@@ -1414,6 +1418,9 @@
"dataField": "updateTitle",
"operator": "exists",
"value": "",
"filterField": "wuPhase",
"filterOperator": "in",
"filterValue": "install,download",
"required": false
},
{
@@ -1423,6 +1430,9 @@
"dataField": "hresult",
"operator": "exists",
"value": "",
"filterField": "wuPhase",
"filterOperator": "in",
"filterValue": "install,download",
"required": false
},
{
@@ -1432,6 +1442,9 @@
"dataField": "hresultSymbol",
"operator": "exists",
"value": "",
"filterField": "wuPhase",
"filterOperator": "in",
"filterValue": "install,download",
"required": false
},
{
@@ -1641,7 +1654,7 @@
"description": "The device's real-time clock deviates from NTP by more than two minutes. Microsoft documents that a clock off by several minutes causes TPM attestation errors (self-deploying/pre-provisioning) and ESP timeouts.",
"severity": "warning",
"category": "device",
"version": "1.1.0",
"version": "1.1.1",
"author": "Autopilot Monitor",
"enabled": true,
"evaluateOn": [
@@ -1653,7 +1666,7 @@
"confidenceThreshold": 50,
"conditions": [
{
"signal": "clock_ahead",
"signal": "clock_behind",
"source": "event_data",
"eventType": "ntp_time_check",
"dataField": "offsetSeconds",
@@ -1662,7 +1675,7 @@
"required": false
},
{
"signal": "clock_behind",
"signal": "clock_ahead",
"source": "event_data",
"eventType": "ntp_time_check",
"dataField": "offsetSeconds",
@@ -1917,6 +1930,357 @@
"environment"
]
},
{
"ruleId": "ANALYZE-DEV-013",
"title": "OOBE Quality Update Failed",
"description": "Detects that the Windows quality update page at the end of OOBE (Enrollment Status Page setting \"Install Windows quality updates\") reported a failed download or install and recorded no successful install afterwards — the page's own failure names on the oobe_update_page events. Setup usually continues, so the failure is otherwise invisible: the device finishes OOBE without the monthly security update. An OS build change across a restart (os_build_changed) vetoes the finding, and so does the agent's names_capped marker — once the page's event names were capped, a missing success name proves nothing. A downloadInstallFailureHelper entry alone is no failure: updates that installed fine log it too.",
"severity": "warning",
"category": "device",
"version": "1.0.0",
"enabled": true,
"evaluateOn": [
"enrollment_end"
],
"trigger": "single",
"baseConfidence": 75,
"confidenceThreshold": 60,
"conditions": [
{
"signal": "update_failure",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "in",
"value": "SdxWebAppCloudNDUP_processStatusChangeFromHandler_downloadFailedError,SdxWebAppCloudNDUP_processStatusChangeFromHandler_installFailedError,ExpeditedUpdate_commitExpeditionDownloadInstallAsyncFailure",
"required": true
},
{
"signal": "no_install_success",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "not_exists",
"value": "",
"filterField": "name",
"filterOperator": "regex",
"filterValue": "installSucceededRebootRequired|commitExpeditionDownloadInstallAsyncSucceeded",
"required": true
},
{
"signal": "os_build_unchanged",
"source": "event_type",
"eventType": "os_build_changed",
"operator": "not_exists",
"value": "",
"required": true
},
{
"signal": "page_names_complete",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "cxhEvent",
"operator": "not_exists",
"value": "",
"filterField": "cxhEvent",
"filterOperator": "equals",
"filterValue": "names_capped",
"required": true
},
{
"signal": "update_page_failed",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "result",
"operator": "equals",
"value": "fail",
"filterField": "page",
"filterOperator": "equals",
"filterValue": "OobeNDUP",
"required": false
},
{
"signal": "update_skipped",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "regex",
"value": "SkipDownloadInstallButtonClicked$",
"required": false
}
],
"confidenceFactors": [
{
"signal": "update_page_failed",
"condition": "exists",
"weight": 10
}
],
"explanation": "The Windows quality update page at the end of OOBE reported `{{update_failure}}`, and no successful install was recorded afterwards. When the download or install of the update fails, the page gives up and setup continues, so the enrollment itself can still succeed — but the device most likely finished OOBE without the monthly security update and installs it only later, through the Windows Update policy that applies after enrollment. Selecting Skip on the page after the failure leads to the same result.",
"remediation": [
{
"title": "Find out why the update failed",
"steps": [
"Open the oobe_update_page events around the failure in the session timeline: the download progress before it shows how far the download got, and a Skip after it shows that someone chose to continue.",
"Check the network path the device uses during OOBE for Windows Update and Delivery Optimization: proxy, TLS inspection and firewall rules for the Windows Update endpoints.",
"Compare with other devices: the same failure on many devices of one site points at the network path, on one model at the device, and across the fleet at the update itself."
]
},
{
"title": "Make sure the device gets the update",
"steps": [
"Verify the device's patch level after enrollment; the update ring assigned to the device installs the missing monthly security update on its normal schedule.",
"Where the OOBE quality update keeps failing for one network or device group, decide per Enrollment Status Page profile whether \"Install Windows quality updates\" should stay on."
]
}
],
"relatedDocs": [
{
"title": "Microsoft Learn: Install Windows quality updates during OOBE (Enrollment Status Page)",
"url": "https://learn.microsoft.com/en-us/intune/device-enrollment/windows/setup-status-page#windows-monthly-security-update-release-details"
}
],
"markSessionAsFailedDefault": false,
"tags": [
"device",
"windows-update",
"oobe",
"quality-update"
]
},
{
"ruleId": "ANALYZE-DEV-014",
"title": "OOBE Quality Update Not Offered After an Earlier Failure",
"description": "Detects that the Windows quality update page at the end of OOBE found no update to install although the device is out of date, on a device whose update page counts an earlier failed download or install (oobe_update_page: NDUPDownloadInstallPreviousFailureCount above 0, osUpdatesWithNDUPPI false, getDaysOutOfDateSucceeded above 0). No failure and no install success are recorded in this session — a failure in this session is ANALYZE-DEV-013 — and the agent's names_capped marker vetoes the finding, because a missing name proves nothing once the page's names were capped. The device finishes OOBE without the monthly security update.",
"severity": "warning",
"category": "device",
"version": "1.0.0",
"enabled": true,
"evaluateOn": [
"enrollment_end"
],
"trigger": "single",
"baseConfidence": 75,
"confidenceThreshold": 60,
"conditions": [
{
"signal": "previous_failures",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "value",
"operator": "gt",
"value": "0",
"filterField": "name",
"filterOperator": "equals",
"filterValue": "SdxWebAppCloudNDUP_initialize_NDUPDownloadInstallPreviousFailureCount",
"required": true
},
{
"signal": "nothing_offered",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "value",
"operator": "equals",
"value": "false",
"filterField": "name",
"filterOperator": "equals",
"filterValue": "SdxWebAppCloudNDUP_updateManager_processUpdatesMetadata_osUpdatesWithNDUPPI",
"required": true
},
{
"signal": "days_out_of_date",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "value",
"operator": "gt",
"value": "0",
"filterField": "name",
"filterOperator": "equals",
"filterValue": "ExpeditedUpdate_getDaysOutOfDateSucceeded",
"required": true
},
{
"signal": "no_update_failure",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "not_exists",
"value": "",
"filterField": "name",
"filterOperator": "in",
"filterValue": "SdxWebAppCloudNDUP_processStatusChangeFromHandler_downloadFailedError,SdxWebAppCloudNDUP_processStatusChangeFromHandler_installFailedError,ExpeditedUpdate_commitExpeditionDownloadInstallAsyncFailure",
"required": true
},
{
"signal": "no_install_success",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "not_exists",
"value": "",
"filterField": "name",
"filterOperator": "regex",
"filterValue": "installSucceededRebootRequired|commitExpeditionDownloadInstallAsyncSucceeded",
"required": true
},
{
"signal": "page_names_complete",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "cxhEvent",
"operator": "not_exists",
"value": "",
"filterField": "cxhEvent",
"filterOperator": "equals",
"filterValue": "names_capped",
"required": true
}
],
"explanation": "The Windows quality update page at the end of OOBE checked for updates and found none to install, although Windows reports the device as {{days_out_of_date}} days out of date. The page's failure counter for this device stands at {{previous_failures}}: an earlier download or install of an OOBE quality update on this device failed. The device finished OOBE without the monthly security update and installs it only later, through the Windows Update policy that applies after enrollment.",
"remediation": [
{
"title": "Check the earlier attempt",
"steps": [
"Look at the device's earlier enrollment sessions — the failed attempt and its ANALYZE-DEV-013 finding are usually there.",
"Verify the device's patch level after enrollment; the update ring assigned to the device installs the missing monthly security update on its normal schedule."
]
},
{
"title": "Reduce how far behind devices start",
"steps": [
"Devices that start OOBE many months behind need a long update during setup; a current OS image or factory build shortens it and gives a failed attempt less to repeat."
]
}
],
"relatedDocs": [
{
"title": "Microsoft Learn: Install Windows quality updates during OOBE (Enrollment Status Page)",
"url": "https://learn.microsoft.com/en-us/intune/device-enrollment/windows/setup-status-page#windows-monthly-security-update-release-details"
}
],
"markSessionAsFailedDefault": false,
"tags": [
"device",
"windows-update",
"oobe",
"quality-update"
]
},
{
"ruleId": "ANALYZE-DEV-015",
"title": "OOBE Update Check Ended Without a Result",
"description": "Detects that the Windows quality update page at the end of OOBE started its Windows Update scan but ended without a scan result (oobe_update_page: startWUScanStartedSuccedded, an OobeNDUP stop and no ExpeditedUpdate_getUpdateResults name), on a device that is out of date and allowed to update. In the observed sessions the page ended two minutes after the scan started, on devices more than a year behind. The device finishes OOBE without the monthly security update. The agent's names_capped marker vetoes the finding: once the page's event names were capped, a missing scan result proves nothing.",
"severity": "warning",
"category": "device",
"version": "1.0.0",
"enabled": true,
"evaluateOn": [
"enrollment_end"
],
"trigger": "single",
"baseConfidence": 75,
"confidenceThreshold": 60,
"conditions": [
{
"signal": "update_allowed",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "value",
"operator": "equals",
"value": "true",
"filterField": "name",
"filterOperator": "equals",
"filterValue": "ExpeditedUpdate_isNDUPAllowedByCSPSucceeded",
"required": true
},
{
"signal": "scan_started",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "equals",
"value": "ExpeditedUpdate_startWUScanStartedSuccedded",
"required": true
},
{
"signal": "no_scan_result",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "name",
"operator": "not_exists",
"value": "",
"filterField": "name",
"filterOperator": "regex",
"filterValue": "^ExpeditedUpdate_getUpdateResults",
"required": true
},
{
"signal": "update_page_ended",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "cxhEvent",
"operator": "equals",
"value": "page_stopped",
"filterField": "page",
"filterOperator": "equals",
"filterValue": "OobeNDUP",
"required": true
},
{
"signal": "days_out_of_date",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "value",
"operator": "gt",
"value": "0",
"filterField": "name",
"filterOperator": "equals",
"filterValue": "ExpeditedUpdate_getDaysOutOfDateSucceeded",
"required": true
},
{
"signal": "page_names_complete",
"source": "event_data",
"eventType": "oobe_update_page",
"dataField": "cxhEvent",
"operator": "not_exists",
"value": "",
"filterField": "cxhEvent",
"filterOperator": "equals",
"filterValue": "names_capped",
"required": true
}
],
"explanation": "The Windows quality update page at the end of OOBE started a Windows Update scan, but no scan result arrived before the page ended. The device is {{days_out_of_date}} days out of date, so an update was due — the device finished OOBE without the monthly security update and installs it only later, through the Windows Update policy that applies after enrollment. The page waits only a short time for the scan (two minutes in the observed sessions): a scan that takes longer, or cannot reach Windows Update, ends the page without an update.",
"remediation": [
{
"title": "Check the path to Windows Update during OOBE",
"steps": [
"Verify that devices in OOBE reach the Windows Update service: proxy, TLS inspection and firewall rules for the Windows Update endpoints.",
"Check whether an update policy applied during the Enrollment Status Page points the scan at another source, such as WSUS."
]
},
{
"title": "Shorten the scan",
"steps": [
"Devices far behind scan longer; a current OS image or factory build keeps the OOBE scan short.",
"Verify the device's patch level after enrollment; the update ring assigned to the device installs the missing monthly security update on its normal schedule."
]
}
],
"relatedDocs": [
{
"title": "Microsoft Learn: Install Windows quality updates during OOBE (Enrollment Status Page)",
"url": "https://learn.microsoft.com/en-us/intune/device-enrollment/windows/setup-status-page#windows-monthly-security-update-release-details"
}
],
"markSessionAsFailedDefault": false,
"tags": [
"device",
"windows-update",
"oobe",
"quality-update"
]
},
{
"ruleId": "ANALYZE-ENRL-001",
"title": "Enrollment Failed",
-3
View File
@@ -1,3 +0,0 @@
# ajv-cli declares ts-node as a peer dependency it never needs for JSON schemas; do not
# pull ts-node + typescript into a job that pushes to main.
legacy-peer-deps=true
+2 -2
View File
@@ -86,12 +86,12 @@ for (const config of configs) {
// ── Guardrails generation ───────────────────────────────────────────────────
const guardrailsPath = path.join(rulesRoot, 'guardrails.json');
// NOTE: consumers import from "@/utils/guardrails.generated" (see guardValidation.ts
// NOTE: consumers import from "@/lib/generated/guardrails.generated" (see guardValidation.ts
// and SectionGatherRules.tsx). The web re-org (commit a3e8e044) moved this file to
// utils/ but left this output path pointing at the now-orphaned lib/ copy, which
// silently froze the live allowlist. Keep this in sync with the import path.
const guardrailsOutput = path.resolve(
rulesRoot, '..', 'src', 'Web', 'autopilot-monitor-web', 'utils', 'guardrails.generated.ts'
rulesRoot, '..', 'src', 'Web', 'autopilot-monitor-web', 'lib', 'generated', 'guardrails.generated.ts'
);
if (fs.existsSync(guardrailsPath)) {
+1 -226
View File
@@ -6,7 +6,7 @@
"": {
"name": "autopilot-monitor-rules-tooling",
"devDependencies": {
"ajv-cli": "5.0.0",
"ajv": "8.20.0",
"ajv-formats": "3.0.1"
}
},
@@ -27,33 +27,6 @@
"url": "https://github.com/sponsors/epoberezkin"
}
},
"node_modules/ajv-cli": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/ajv-cli/-/ajv-cli-5.0.0.tgz",
"integrity": "sha512-LY4m6dUv44HTyhV+u2z5uX4EhPYTM38Iv1jdgDJJJCyOOuqB8KtZEGjPZ2T+sh5ZIJrXUfgErYx/j3gLd3+PlQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"ajv": "^8.0.0",
"fast-json-patch": "^2.0.0",
"glob": "^7.1.0",
"js-yaml": "^3.14.0",
"json-schema-migrate": "^2.0.0",
"json5": "^2.1.3",
"minimist": "^1.2.0"
},
"bin": {
"ajv": "dist/index.js"
},
"peerDependencies": {
"ts-node": ">=9.0.0"
},
"peerDependenciesMeta": {
"ts-node": {
"optional": true
}
}
},
"node_modules/ajv-formats": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-3.0.1.tgz",
@@ -72,55 +45,6 @@
}
}
},
"node_modules/argparse": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
"integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
"dev": true,
"license": "MIT",
"dependencies": {
"sprintf-js": "~1.0.2"
}
},
"node_modules/balanced-match": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
"dev": true,
"license": "MIT"
},
"node_modules/brace-expansion": {
"version": "1.1.21",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.21.tgz",
"integrity": "sha512-9zeA+KLZNNzglF2TPKRQEDyx6Yby7daAkuy8MiPzpXPsYDWi/DRM8jmwUDxokQjYqBpv5DgPiwD4h4ZZSy1Ujw==",
"dev": true,
"license": "MIT",
"dependencies": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
}
},
"node_modules/concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==",
"dev": true,
"license": "MIT"
},
"node_modules/esprima": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
"dev": true,
"license": "BSD-2-Clause",
"bin": {
"esparse": "bin/esparse.js",
"esvalidate": "bin/esvalidate.js"
},
"engines": {
"node": ">=4"
}
},
"node_modules/fast-deep-equal": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
@@ -128,13 +52,6 @@
"dev": true,
"license": "MIT"
},
"node_modules/fast-json-patch": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/fast-json-patch/-/fast-json-patch-3.1.1.tgz",
"integrity": "sha512-vf6IHUX2SBcA+5/+4883dsIjpBTqmfBjmYiWK1savxQmFk4JfBMLa7ynTYOs1Rolp/T1betJxHiGD3g1Mn8lUQ==",
"dev": true,
"license": "MIT"
},
"node_modules/fast-uri": {
"version": "3.1.8",
"resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.8.tgz",
@@ -152,78 +69,6 @@
],
"license": "BSD-3-Clause"
},
"node_modules/fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==",
"dev": true,
"license": "ISC"
},
"node_modules/glob": {
"version": "7.2.3",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz",
"integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==",
"deprecated": "Old versions of glob are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me",
"dev": true,
"license": "ISC",
"dependencies": {
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
"inherits": "2",
"minimatch": "^3.1.1",
"once": "^1.3.0",
"path-is-absolute": "^1.0.0"
},
"engines": {
"node": "*"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/inflight": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
"integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==",
"deprecated": "This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.",
"dev": true,
"license": "ISC",
"dependencies": {
"once": "^1.3.0",
"wrappy": "1"
}
},
"node_modules/inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
"dev": true,
"license": "ISC"
},
"node_modules/js-yaml": {
"version": "3.15.2",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.15.2.tgz",
"integrity": "sha512-6EuL879VkRA+1Cz578mKMiKvjPNEuk6+r1JaFzoSWejZmtf7xWbIyw1e3KkxlkzTIt9Taw6JBhEppG7utc1P+w==",
"dev": true,
"license": "MIT",
"dependencies": {
"argparse": "^1.0.7",
"esprima": "^4.0.0"
},
"bin": {
"js-yaml": "bin/js-yaml.js"
}
},
"node_modules/json-schema-migrate": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/json-schema-migrate/-/json-schema-migrate-2.0.0.tgz",
"integrity": "sha512-r38SVTtojDRp4eD6WsCqiE0eNDt4v1WalBXb9cyZYw9ai5cGtBwzRNWjHzJl38w6TxFkXAIA7h+fyX3tnrAFhQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"ajv": "^8.0.0"
}
},
"node_modules/json-schema-traverse": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
@@ -231,62 +76,6 @@
"dev": true,
"license": "MIT"
},
"node_modules/json5": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
"integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==",
"dev": true,
"license": "MIT",
"bin": {
"json5": "lib/cli.js"
},
"engines": {
"node": ">=6"
}
},
"node_modules/minimatch": {
"version": "3.1.5",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.5.tgz",
"integrity": "sha512-VgjWUsnnT6n+NUk6eZq77zeFdpW2LWDzP6zFGrCbHXiYNul5Dzqk2HHQ5uFH2DNW5Xbp8+jVzaeNt94ssEEl4w==",
"dev": true,
"license": "ISC",
"dependencies": {
"brace-expansion": "^1.1.7"
},
"engines": {
"node": "*"
}
},
"node_modules/minimist": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
"dev": true,
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==",
"dev": true,
"license": "ISC",
"dependencies": {
"wrappy": "1"
}
},
"node_modules/path-is-absolute": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
"integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/require-from-string": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
@@ -296,20 +85,6 @@
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/sprintf-js": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==",
"dev": true,
"license": "BSD-3-Clause"
},
"node_modules/wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
"dev": true,
"license": "ISC"
}
}
}
+1 -4
View File
@@ -3,10 +3,7 @@
"private": true,
"description": "Validator tooling for rules/ - combine-rules.yml installs from the lockfile so nothing unpinned reaches a job that pushes to main.",
"devDependencies": {
"ajv-cli": "5.0.0",
"ajv": "8.20.0",
"ajv-formats": "3.0.1"
},
"overrides": {
"fast-json-patch": "3.1.1"
}
}
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/env node
/**
* Validates every rule file and rules/guardrails.json against its JSON Schema
* (draft 2020-12). combine-rules.yml runs this before combine.js; any invalid
* file fails the run with exit code 1.
*
* Run: node rules/scripts/validate.js (after `npm ci --ignore-scripts` in rules/scripts)
*/
const fs = require('fs');
const path = require('path');
const Ajv2020 = require('ajv/dist/2020');
const addFormats = require('ajv-formats');
const rulesRoot = path.resolve(__dirname, '..');
const jsonFilesIn = (dir) =>
fs.readdirSync(path.join(rulesRoot, dir)).filter(f => f.endsWith('.json')).sort().map(f => `${dir}/${f}`);
const sets = [
{ schema: 'gather-rule.schema.json', files: jsonFilesIn('gather') },
{ schema: 'analyze-rule.schema.json', files: jsonFilesIn('analyze') },
{ schema: 'ime-log-pattern.schema.json', files: jsonFilesIn('ime-log-patterns') },
// guardrails.json values are baked verbatim into generated TypeScript that is
// auto-committed to main and shipped to the portal - the schema forbids control
// and line-terminator characters so a "data" PR can never become code.
{ schema: 'guardrails.schema.json', files: ['guardrails.json'] }
];
const ajv = new Ajv2020({ allErrors: true });
addFormats(ajv);
let invalid = 0;
for (const set of sets) {
const validate = ajv.compile(JSON.parse(fs.readFileSync(path.join(rulesRoot, 'schema', set.schema), 'utf8')));
for (const file of set.files) {
let data;
try {
data = JSON.parse(fs.readFileSync(path.join(rulesRoot, file), 'utf8'));
} catch (err) {
invalid++;
console.error(`INVALID rules/${file}: ${err.message}`);
continue;
}
if (!validate(data)) {
invalid++;
console.error(`INVALID rules/${file}`);
for (const e of validate.errors) {
console.error(` ${e.instancePath || '(root)'} ${e.message} ${JSON.stringify(e.params)}`);
}
}
}
console.log(`${set.schema}: ${set.files.length} files checked`);
}
if (invalid > 0) {
console.error(`ERROR: ${invalid} file(s) failed schema validation`);
process.exit(1);
}
@@ -50,6 +50,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
nameof(AgentConfigResponse.MaxBatchSize),
nameof(AgentConfigResponse.MaxAuthFailures),
nameof(AgentConfigResponse.AuthFailureTimeoutMinutes),
nameof(AgentConfigResponse.InterceptedRegistrationRetryBoots),
// Merge reads two Collectors knobs (AgentMaxLifetimeMinutes, HelloWaitTimeoutSeconds);
// the remaining CollectorConfiguration flags flow untouched to DefaultComponentFactory.
nameof(AgentConfigResponse.Collectors),
@@ -55,6 +55,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
UnrestrictedMode = true,
MaxAuthFailures = 7,
AuthFailureTimeoutMinutes = 15,
InterceptedRegistrationRetryBoots = 2,
EnableImeMatchLog = true,
Collectors = new CollectorConfiguration
{
@@ -98,6 +99,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
Assert.Equal(45, agent.HelloWaitTimeoutSeconds);
Assert.Equal(7, agent.MaxAuthFailures);
Assert.Equal(15, agent.AuthFailureTimeoutMinutes);
Assert.Equal(2, agent.InterceptedRegistrationRetryBoots);
Assert.NotNull(result);
Assert.True(result.UnrestrictedModeChanged);
@@ -468,6 +470,50 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
Assert.Equal(0, agent.AgentMaxLifetimeMinutes);
}
// ============================================================= Intercepted-registration buffer
[Theory]
[InlineData(0)] // clean up at the first intercepted registration
[InlineData(3)]
public void Merge_copies_intercepted_registration_retry_boots(int remoteValue)
{
var agent = NewAgentConfig();
var remote = FullRemote();
remote.InterceptedRegistrationRetryBoots = remoteValue;
RemoteConfigMerger.Merge(agent, remote);
Assert.Equal(remoteValue, agent.InterceptedRegistrationRetryBoots);
}
[Fact]
public void Merge_keeps_the_agent_default_when_the_backend_sends_no_intercepted_registration_retry_boots()
{
var agent = NewAgentConfig();
var remote = FullRemote();
remote.InterceptedRegistrationRetryBoots = null; // backend that predates the field
RemoteConfigMerger.Merge(agent, remote);
Assert.Equal(1, agent.InterceptedRegistrationRetryBoots);
}
[Theory]
[InlineData(-1, 0)]
[InlineData(int.MinValue, 0)]
[InlineData(6, 5)]
[InlineData(int.MaxValue, 5)]
public void Merge_clamps_intercepted_registration_retry_boots_to_zero_through_five(int remoteValue, int expected)
{
var agent = NewAgentConfig();
var remote = FullRemote();
remote.InterceptedRegistrationRetryBoots = remoteValue;
RemoteConfigMerger.Merge(agent, remote);
Assert.Equal(expected, agent.InterceptedRegistrationRetryBoots);
}
// ============================================================= Guard
[Fact]
@@ -214,6 +214,23 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
Assert.Null(loaded.LatestAgentSha256);
}
[Theory]
[InlineData(0)]
[InlineData(3)]
[InlineData(null)] // backend that predates the field — the agent keeps its default
public void RoundTrip_preserves_intercepted_registration_retry_boots(int? value)
{
// An intercepted start gets no live config, so the registration buffer it decides on
// is the cached value: it must survive the write + read strip untouched.
var probe = NewProbe();
probe.DoCache(new AgentConfigResponse { InterceptedRegistrationRetryBoots = value });
var loaded = probe.DoLoad();
Assert.NotNull(loaded);
Assert.Equal(value, loaded.InterceptedRegistrationRetryBoots);
}
// ── Helpers ─────────────────────────────────────────────────────────
private CacheProbe NewProbe()
@@ -7,6 +7,7 @@ using AutopilotMonitor.Agent.V2.Core.Logging;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.V2.Core.Security;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Transport;
using AutopilotMonitor.Shared.Models;
using Xunit;
@@ -101,6 +102,28 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
Assert.Equal(401, svc.LastFetchAuthStatusCode);
}
[Fact]
public async Task FetchConfig_InterceptedAnswer_FallsBack_WithoutCounting_AndReportsOnce()
{
// A 403 something on the network path answered (TLS inspection): same fallback as an
// auth failure, but the tracker does not count it — it sends ClientCertificateNotReceived.
var distress = new FakeDistressReporter();
var tracker = new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 0,
AutopilotMonitor.DecisionCore.Engine.SystemClock.Instance, _logger, distress);
var svc = NewService(BackendBehaviour.AlwaysIntercepted403, tracker);
var result = await svc.FetchConfigAsync(retryOnTransientErrors: true);
Assert.NotNull(result);
Assert.Equal(RemoteConfigFetchOutcome.UsedDefaults, svc.LastFetchOutcome);
Assert.Equal(1, svc.LastFetchAttempts);
Assert.Equal(403, svc.LastFetchAuthStatusCode);
Assert.Equal(0, tracker.ConsecutiveFailures);
var call = Assert.Single(distress.Calls);
Assert.Equal(DistressErrorType.ClientCertificateNotReceived, call.ErrorType);
Assert.StartsWith("agent/config http 403 client_certificate_required", call.Message);
}
[Fact]
public async Task FetchConfig_RetryDisabled_OneAttemptOnly()
{
@@ -144,7 +167,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
Assert.Equal(4, svc.LastFetchAttempts);
}
// ── Network-link gate (tenant aebdce78 audit 2026-08-24) ────────────
// ── Network-link gate (kiosk-tenant audit 2026-08-24) ────────────
[Fact]
public async Task FetchConfig_InitialFetch_WaitsForNetworkLink()
@@ -252,10 +275,11 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
/// <summary>All 4 initial attempts fail; the NEXT call (the recovery fetch) succeeds.</summary>
FailAllThenSucceed,
AlwaysAuthFailure401,
AlwaysIntercepted403,
AlwaysReturnNull,
}
private FakeRemoteConfigService NewService(BackendBehaviour behaviour)
private FakeRemoteConfigService NewService(BackendBehaviour behaviour, AuthFailureTracker? tracker = null)
{
// Pass a throwaway BackendApiClient — the fake overrides CallBackendAsync
// before it ever hits the wire.
@@ -269,7 +293,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
bootstrapToken: null,
agentVersion: "0.0.0",
logger: _logger);
return new FakeRemoteConfigService(apiClient, _logger, behaviour);
return new FakeRemoteConfigService(apiClient, _logger, behaviour, tracker);
}
private sealed class FakeRemoteConfigService : RemoteConfigService
@@ -277,8 +301,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
private readonly BackendBehaviour _behaviour;
private int _calls;
public FakeRemoteConfigService(BackendApiClient apiClient, AgentLogger logger, BackendBehaviour behaviour)
: base(apiClient, "00000000-0000-0000-0000-000000000001", logger)
public FakeRemoteConfigService(BackendApiClient apiClient, AgentLogger logger, BackendBehaviour behaviour, AuthFailureTracker? tracker)
: base(apiClient, "00000000-0000-0000-0000-000000000001", logger, authFailureTracker: tracker)
{
_behaviour = behaviour;
}
@@ -305,6 +329,9 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Configuration
throw new HttpRequestException("simulated network failure");
case BackendBehaviour.AlwaysAuthFailure401:
throw new BackendAuthException("Unauthorized", 401);
case BackendBehaviour.AlwaysIntercepted403:
throw new BackendAuthException("The device certificate did not reach the backend.", 403,
classification: InterceptionFixtures.CertificateRequired());
case BackendBehaviour.AlwaysReturnNull:
return Task.FromResult<AgentConfigResponse>(null!);
default:
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Transport;
using AutopilotMonitor.Shared.Models;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Harness
{
/// <summary>
/// Records every distress dispatch instead of sending it. The real anti-flood rules of
/// <see cref="DistressReporter"/> are bypassed on purpose: tests assert what the caller dispatched.
/// </summary>
internal sealed class FakeDistressReporter : DistressReporter
{
public sealed class Call
{
public DistressErrorType ErrorType { get; set; }
public string Message { get; set; } = default!;
public int? HttpStatusCode { get; set; }
}
public List<Call> Calls { get; } = new List<Call>();
/// <summary>
/// The send each dispatch returns; null = already completed. Lets a test keep a send in
/// flight to prove a caller waits for it (or stops waiting at its bound).
/// </summary>
public Func<Task>? Send { get; set; }
public FakeDistressReporter()
: base(baseUrl: "https://example.test", tenantId: "t", manufacturer: "m", model: "d", serialNumber: "s", agentVersion: "0.0.0", logger: null!)
{
}
public override Task TrySendAsync(DistressErrorType errorType, string message, int? httpStatusCode = null)
{
Calls.Add(new Call { ErrorType = errorType, Message = message, HttpStatusCode = httpStatusCode });
return Send?.Invoke() ?? Task.CompletedTask;
}
}
}
@@ -0,0 +1,31 @@
#nullable enable
using AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Analyzers;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Analyzers
{
/// <summary>
/// Uninstall-key strings are read the way Windows shows them: up to the first NUL. Installers
/// that pad REG_SZ data with NULs otherwise leak them into the normalized vendor/name, which
/// the backend turns into a table key.
/// </summary>
public sealed class SoftwareInventoryRegistryTextTests
{
[Theory]
[InlineData("Contoso Widget", "Contoso Widget")]
[InlineData("Contoso Widget\0\0\0\0\0", "Contoso Widget")]
[InlineData("Contoso\0stale buffer", "Contoso")]
[InlineData("\0\0\0", "")]
[InlineData("", "")]
public void CutAtNul_KeepsTheTextBeforeTheFirstNul(string raw, string expected)
{
Assert.Equal(expected, SoftwareInventoryAnalyzer.CutAtNul(raw));
}
[Fact]
public void CutAtNul_Null_StaysNull()
{
Assert.Null(SoftwareInventoryAnalyzer.CutAtNul(null!));
}
}
}
@@ -174,7 +174,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring
[Fact]
public void Reevaluate_does_not_re_emit_starved_warnings()
{
// sits-d Cloud-PC fix (2026-08-19): the synthesis re-check now runs on EVERY IME
// Cloud-PC fix (2026-08-19): the synthesis re-check now runs on EVERY IME
// app-state transition. It must not turn the one-shot starvation warning into a
// per-transition stream — the re-check path suppresses the emission entirely
// (the per-appId dedupe would swallow the duplicates, but walking the probe
@@ -198,7 +198,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring
[Fact]
public void Starved_apps_that_settle_later_still_complete_the_enrollment()
{
// The real sits-d shape: the ESP exits with apps still starved (warning emitted),
// The real field shape: the ESP exits with apps still starved (warning emitted),
// the apps then all settle, and the re-check must open the gate — the warning
// already on the timeline is history, not a permanent veto.
using var f = new Fixture();
@@ -172,8 +172,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring
}
// ------------------------------------------------------------------------------
// sits-d Cloud-PC fix (2026-08-19) — the synthesis is no longer edge-only.
// Sessions 8110e262 / a89aac2d: the ESP page exited while 138 required user-ESP apps
// Cloud-PC fix (2026-08-19) — the synthesis is no longer edge-only.
// Two field sessions: the ESP page exited while 138 required user-ESP apps
// were still in flight, they all reached a terminal state (0 failed) minutes later,
// and because nothing re-checked, the enrollment never completed.
// ------------------------------------------------------------------------------
@@ -140,6 +140,22 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
Assert.Equal(1, fired); // the persisted flag suppressed the second emission
}
[Fact]
public void Registry_reconciliation_counters_survive_a_restart()
{
using var h = new Harness();
h.Tracker.ScriptRuns.CountVerified();
h.Tracker.ScriptRuns.CountVerified();
h.Tracker.ScriptRuns.CountCorrected();
h.Tracker.SaveStateForTest();
h.Restart();
var health = h.Tracker.GetHealthSnapshot();
Assert.Equal(2, health.PlatformOutputVerified);
Assert.Equal(1, health.PlatformOutputCorrected);
}
[Fact]
public async Task Handler_time_does_not_count_against_the_line_budget()
{
@@ -0,0 +1,336 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
{
/// <summary>
/// B-96m: IME re-runs a failed platform script on later check-ins, so AgentExecutor.log can hold several runs of one
/// policy before the tracker has read IME's result of the first — always on the agent's first pass (the agent starts
/// mid-enrollment and reads all of AgentExecutor.log before IntuneManagementExtension.log), after a restart, and
/// whenever a result line surfaces late. With one slot per policy the second executor start reused the first run's
/// slot and its end block overwrote the first one: the first IME result went out with the LAST run's exit code and
/// output (session eff1b413: the bootstrap script "Failed (exit 0)" with the stdout of its successful retry), every
/// later result without an end block. These tests pin one run per executor start, each IME result paired with the
/// run of its policy that started last before it, each run carrying its own end block.
/// </summary>
public sealed partial class ImeLogTrackerOverwriteRewindTests
{
private const string RetryPolicyId = "4c2d8e1a-7b3f-4a9e-8d21-6f0b5c3e9a74";
private const string RetryUser = "00000000-0000-0000-0000-000000000000";
private static string ImeEntryAt(string time, string message)
=> $"<![LOG[{message}]LOG]!><time=\"{time}\" date=\"9-10-2026\" component=\"IntuneManagementExtension\" context=\"\" type=\"1\" thread=\"12\" file=\"\">\r\n";
/// <summary>One executor run's start block: banner, the platform argument line, paths, launch.</summary>
private static string RunStart(string time, string pid, string policyId = RetryPolicyId, string userId = RetryUser)
{
var path = $@"{ImeDir}\Policies\Scripts\{userId}_{policyId}.ps1";
var results = $@"{ImeDir}\Policies\Results\{userId}_{policyId}";
return ExecutorBanner(time)
+ EntryAt(time, $"Adding argument powershell with value {path} to the named argument list.")
+ EntryAt(time, "Powershell option gets invoked")
+ EntryAt(time, path)
+ EntryAt(time, results + ".output")
+ EntryAt(time, results + ".error")
+ EntryAt(time, results + ".timeout")
+ ExecutorLaunch(time, path, pid);
}
/// <summary>
/// One executor run's end block in AgentExecutor's order. "error from script =" logs the stderr buffer before the
/// output readers finished (<paramref name="stderrSnapshot"/>); "write output done" logs the final texts, the
/// error part being exactly the error file IME judges.
/// </summary>
private static string RunEnd(string time, int exitCode, string stdout, string stderr, string? stderrSnapshot = null)
=> EntryAt(time, $"Powershell exit code is {exitCode}")
+ EntryAt(time, $"length of out={stdout.Length}")
+ EntryAt(time, $"length of error={stderr.Length}")
+ EntryAt(time, "error from script =" + (stderrSnapshot ?? stderr))
+ EntryAt(time, exitCode == 0 ? "Powershell script is successfully executed." : "Powershell script is failed to execute")
+ EntryAt(time, $"write output done. output = {stdout}, error = {stderr}")
+ EntryAt(time, "Revert Wow64FsRedirection")
+ EntryAt(time, "Agent executor completed.");
private static string ImeRunStart(string time, string policyId = RetryPolicyId, string userId = RetryUser)
=> ImeEntryAt(time, $@"Script file {ImeDir}\Policies\Scripts\{userId}_{policyId}.ps1 is generated.");
private static string ImeRunResult(string time, string result, string policyId = RetryPolicyId, string userId = RetryUser)
=> ImeEntryAt(time, $"[PowerShell] User Id = {userId}, Policy id = {policyId}, policy result = {result}");
private static void AppendImeLog(Harness h, string text)
=> File.AppendAllText(Path.Combine(Path.GetDirectoryName(h.LogPath)!, "IntuneManagementExtension.log"), text, new UTF8Encoding(false));
/// <summary>Result | exit | stdout | stderr — readable in an assertion failure; "-" no exit code, "~" never read.</summary>
private static string Shape(ScriptExecutionState s)
=> $"{s.Result}|{(s.ExitCode.HasValue ? s.ExitCode.Value.ToString() : "-")}|{s.Stdout ?? "~"}|{s.Stderr ?? "~"}";
/// <summary>
/// Two poll passes as the agent runs them (read the files, then the flushes), the second past the result hold:
/// a result missing its end block goes out by then.
/// </summary>
private static async Task PassAndSettle(Harness h)
{
await h.Tracker.RunPollPassAsync(default);
h.Now = h.Now.AddSeconds(6);
await h.Tracker.RunPollPassAsync(default);
}
[Fact]
public async Task First_pass_pairs_each_ime_result_with_the_end_block_of_its_own_run()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
// IME's first run and two retries on later check-ins, all written before the agent started.
h.Append(RunStart("12:00:10.0000000", "1001") + RunEnd("12:00:12.0000000", 1, "", "Run A failed")
+ RunStart("12:10:10.0000000", "1002") + RunEnd("12:10:12.0000000", 1, "Run B output", "")
+ RunStart("12:20:10.0000000", "1003") + RunEnd("12:20:12.0000000", 0, "Run C output", ""));
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunResult("12:00:12.1000000", "Failed")
+ ImeRunStart("12:10:08.0000000") + ImeRunResult("12:10:12.1000000", "Success")
+ ImeRunStart("12:20:08.0000000") + ImeRunResult("12:20:12.1000000", "Success"));
await PassAndSettle(h);
Assert.Equal(new[] { "Failed|1||Run A failed", "Success|1|Run B output|", "Success|0|Run C output|" },
h.Completed.Select(Shape).ToArray());
Assert.All(h.Completed, s => Assert.Equal("ime_policy_result", s.ResultSource));
Assert.True(h.Completed[0].StartedAtUtc < h.Completed[1].StartedAtUtc && h.Completed[1].StartedAtUtc < h.Completed[2].StartedAtUtc);
Assert.Equal(3, h.Completed.Select(s => s.RunId).Distinct().Count());
}
[Fact]
public async Task Retry_of_the_bootstrap_script_never_lends_its_success_output_to_the_failed_first_attempt()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(RunStart("12:00:10.0000000", "2001") + RunEnd("12:00:20.0000000", 1, "attempt 1: download failed", "Invoke-WebRequest : The remote name could not be resolved")
+ RunStart("12:10:10.0000000", "2002") + RunEnd("12:10:20.0000000", 0, "===== Bootstrap Completed Successfully =====", ""));
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunResult("12:00:20.1000000", "Failed")
+ ImeRunStart("12:10:08.0000000") + ImeRunResult("12:10:20.1000000", "Success"));
await PassAndSettle(h);
Assert.Equal(new[]
{
"Failed|1|attempt 1: download failed|Invoke-WebRequest : The remote name could not be resolved",
"Success|0|===== Bootstrap Completed Successfully =====|",
}, h.Completed.Select(Shape).ToArray());
}
[Fact]
public async Task Run_killed_at_the_timeout_reports_failed_without_the_next_runs_end_block()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
// IME killed the first executor after 1800 s: its start block is all AgentExecutor.log holds of it.
h.Append(RunStart("12:00:10.0000000", "3001")
+ RunStart("12:40:10.0000000", "3002") + RunEnd("12:40:12.0000000", 0, "Run B output", ""));
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunResult("12:30:10.5000000", "Failed")
+ ImeRunStart("12:40:08.0000000") + ImeRunResult("12:40:12.1000000", "Success"));
await PassAndSettle(h);
Assert.Equal(new[] { "Failed|-|~|~", "Success|0|Run B output|" }, h.Completed.Select(Shape).ToArray());
}
[Fact]
public async Task Run_whose_result_never_surfaced_falls_back_on_imes_rule_before_the_next_run()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(RunStart("12:00:10.0000000", "4001") + RunEnd("12:00:12.0000000", 0, "Run A output", "Run A wrote an error")
+ RunStart("12:10:10.0000000", "4002") + RunEnd("12:10:12.0000000", 0, "Run B output", ""));
// Run A's result line was lost; run B's is there.
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunStart("12:10:08.0000000") + ImeRunResult("12:10:12.1000000", "Success"));
await PassAndSettle(h);
Assert.Equal(new[] { "Failed|0|Run A output|Run A wrote an error", "Success|0|Run B output|" }, h.Completed.Select(Shape).ToArray());
Assert.Equal(new[] { "agentexecutor_fallback", "ime_policy_result" }, h.Completed.Select(s => s.ResultSource).ToArray());
}
[Fact]
public async Task Run_in_flight_survives_the_emit_of_the_run_before_it()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(RunStart("12:00:10.0000000", "5001") + RunEnd("12:00:12.0000000", 0, "Run A output", "")
+ RunStart("12:10:10.0000000", "5002"));
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunResult("12:00:12.1000000", "Success") + ImeRunStart("12:10:08.0000000"));
await PassAndSettle(h);
h.Now = h.Now.AddSeconds(30);
await h.Pass();
// Run B ends minutes later.
h.Append(RunEnd("12:10:40.0000000", 0, "Run B output", ""));
AppendImeLog(h, ImeRunResult("12:10:40.1000000", "Success"));
await PassAndSettle(h);
Assert.Equal(new[] { "Success|0|Run A output|", "Success|0|Run B output|" }, h.Completed.Select(Shape).ToArray());
}
[Fact]
public async Task Shutdown_between_the_two_logs_flushes_each_run_with_its_own_end_block()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(RunStart("12:00:10.0000000", "6001") + RunEnd("12:00:12.0000000", 1, "", "Run A failed")
+ RunStart("12:10:10.0000000", "6002") + RunEnd("12:10:12.0000000", 0, "Run B output", ""));
// The pass is cancelled after AgentExecutor.log; the shutdown flush follows.
await h.Pass();
h.Tracker.FlushPendingPlatformScriptResults(h.Now, force: true);
h.Restart();
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunResult("12:00:12.1000000", "Failed")
+ ImeRunStart("12:10:08.0000000") + ImeRunResult("12:10:12.1000000", "Success"));
await PassAndSettle(h);
// Each run went out with its own block and IME's verdict on it; the results read after the restart are
// the same runs and change nothing.
Assert.Equal(new[] { "Failed|1||Run A failed", "Success|0|Run B output|" }, h.Completed.Select(Shape).ToArray());
Assert.All(h.Completed, s => Assert.Equal("agentexecutor_fallback", s.ResultSource));
}
[Fact]
public async Task End_block_line_recovered_after_the_next_run_started_stays_with_its_own_run()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(RunStart("12:00:10.0000000", "7001"));
var exitLineAt = h.Length;
h.Append(RunEnd("12:00:12.0000000", 1, "", "Run A failed")
+ RunStart("12:10:10.0000000", "7002") + RunEnd("12:10:12.0000000", 0, "Run B output", ""));
await h.Pass();
// A stale writer rewrites run A's exit line in place: the ledger check re-reads it behind run B's start.
h.Overwrite(exitLineAt, EntryAt("12:00:12.0000000", "Powershell exit code is 7"));
h.Tracker.RequestOverwriteCheckForTest();
h.Now = h.Now.AddSeconds(2);
await h.Pass();
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunResult("12:00:12.1000000", "Failed")
+ ImeRunStart("12:10:08.0000000") + ImeRunResult("12:10:12.1000000", "Success"));
await PassAndSettle(h);
Assert.Equal(new[] { "Failed|7||Run A failed", "Success|0|Run B output|" }, h.Completed.Select(Shape).ToArray());
}
[Fact]
public async Task Run_without_result_or_end_block_is_dropped_once_a_later_run_has_its_result()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(RunStart("12:00:10.0000000", "9001")
+ RunStart("12:10:10.0000000", "9002") + RunEnd("12:10:12.0000000", 0, "Run B output", ""));
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunStart("12:10:08.0000000") + ImeRunResult("12:10:12.1000000", "Success"));
await PassAndSettle(h);
Assert.Equal(new[] { "Success|0|Run B output|" }, h.Completed.Select(Shape).ToArray());
Assert.Equal(0, h.Tracker.ParkedPlatformScriptCountForTest);
}
[Fact]
public void Reading_the_same_executor_start_line_again_never_parks_its_run()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
var line = $@"Adding argument powershell with value {ImeDir}\Policies\Scripts\{RetryUser}_{RetryPolicyId}.ps1 to the named argument list.";
var at = T0.AddMinutes(-30);
h.Tracker.ProcessLogMessageForTest(line, at, "AgentExecutor.log");
h.Tracker.ProcessLogMessageForTest(line, at, "AgentExecutor.log");
Assert.Equal(0, h.Tracker.ParkedPlatformScriptCountForTest);
h.Tracker.ProcessLogMessageForTest(line, at.AddMinutes(10), "AgentExecutor.log");
Assert.Equal(1, h.Tracker.ParkedPlatformScriptCountForTest);
}
[Fact]
public async Task Run_without_end_block_keeps_waiting_for_its_result_across_a_shutdown()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(RunStart("12:00:10.0000000", "9101") + RunStart("12:40:10.0000000", "9102"));
await h.Pass();
h.Tracker.FlushPendingPlatformScriptResults(h.Now, force: true);
Assert.Empty(h.Completed);
h.Restart();
Assert.Equal(1, h.Tracker.ParkedPlatformScriptCountForTest);
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunResult("12:30:10.5000000", "Failed"));
await PassAndSettle(h);
Assert.Equal(new[] { "Failed|-|~|~" }, h.Completed.Select(Shape).ToArray());
}
[Fact]
public async Task Earlier_run_emitted_while_a_later_one_runs_never_takes_the_later_runs_register_entry()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(RunStart("12:00:10.0000000", "9201") + RunEnd("12:00:12.0000000", 1, "", "Run A failed")
+ RunStart("12:10:10.0000000", "9202"));
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunResult("12:00:12.1000000", "Failed") + ImeRunStart("12:10:08.0000000"));
await PassAndSettle(h);
Assert.Single(h.Completed);
// A save of this key is the latest run's, and that one is still running.
h.Tracker.ScriptRuns.NotePassCompleted(h.Now);
Assert.Equal(PlatformRunClaim.Wait, h.Tracker.ScriptRuns.TryClaim(RetryUser, RetryPolicyId, h.Now.AddSeconds(-10), out _));
h.Append(RunEnd("12:10:40.0000000", 0, "Run B output", ""));
AppendImeLog(h, ImeRunResult("12:10:40.1000000", "Success"));
await PassAndSettle(h);
h.Tracker.ScriptRuns.NotePassCompleted(h.Now);
Assert.Equal(PlatformRunClaim.Claimed, h.Tracker.ScriptRuns.TryClaim(RetryUser, RetryPolicyId, h.Now.AddSeconds(-10), out var run));
Assert.Equal(h.Completed[1].RunId, run!.RunId);
Assert.Equal("Run B output", run.Stdout);
}
[Fact]
public async Task Run_of_another_user_keeps_its_own_register_entry_when_it_waited_for_its_result()
{
const string otherUser = "2b7e5c91-3d4a-4f80-b6e2-9a1c7d3f5e08";
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(RunStart("12:00:10.0000000", "9301") + RunEnd("12:00:12.0000000", 1, "", "Run A failed")
+ RunStart("12:10:10.0000000", "9302", userId: otherUser));
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunResult("12:00:12.1000000", "Failed")
+ ImeRunStart("12:10:08.0000000", userId: otherUser));
await PassAndSettle(h);
h.Tracker.ScriptRuns.NotePassCompleted(h.Now);
Assert.Equal(PlatformRunClaim.Claimed, h.Tracker.ScriptRuns.TryClaim(RetryUser, RetryPolicyId, h.Now.AddSeconds(-10), out var run));
Assert.Equal(h.Completed[0].RunId, run!.RunId);
Assert.Equal(PlatformRunClaim.Wait, h.Tracker.ScriptRuns.TryClaim(otherUser, RetryPolicyId, h.Now.AddSeconds(-10), out _));
}
[Fact]
public async Task Start_line_read_again_stays_a_late_line_after_an_earlier_run_of_another_user_went_out()
{
const string otherUser = "2b7e5c91-3d4a-4f80-b6e2-9a1c7d3f5e08";
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(RunStart("12:00:10.0000000", "9401") + RunEnd("12:00:12.0000000", 0, "Run A output", "")
+ RunStart("12:10:10.0000000", "9402", userId: otherUser) + RunEnd("12:10:12.0000000", 0, "Run B output", ""));
// Run A's result surfaces after run B's (its line was hidden by a concurrent writer).
AppendImeLog(h, ImeRunResult("12:10:12.1000000", "Success", userId: otherUser) + ImeRunResult("12:00:12.1000000", "Success"));
await PassAndSettle(h);
Assert.Equal(new[] { "Success|0|Run B output|", "Success|0|Run A output|" }, h.Completed.Select(Shape).ToArray());
// Run B's executor start line read again (a rewind): a late line of the emitted run B, never a new run.
var started = 0;
h.Tracker.OnScriptStarted = _ => started++;
h.Tracker.ProcessLogMessageForTest(
$@"Adding argument powershell with value {ImeDir}\Policies\Scripts\{otherUser}_{RetryPolicyId}.ps1 to the named argument list.",
h.Completed[0].StartedAtUtc, "AgentExecutor.log");
Assert.Equal(0, started);
}
[Fact]
public async Task Final_stderr_of_the_end_block_replaces_the_early_snapshot_even_without_stdout()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
// The snapshot was taken before the error reader finished; IME judged the full error file.
h.Append(RunStart("12:00:10.0000000", "8001") + RunEnd("12:00:12.0000000", 0, "", "Remove-AppxPackage : Cannot process argument", stderrSnapshot: ""));
AppendImeLog(h, ImeRunStart("12:00:08.0000000") + ImeRunResult("12:00:12.1000000", "Failed"));
await PassAndSettle(h);
Assert.Equal(new[] { "Failed|0||Remove-AppxPackage : Cannot process argument" }, h.Completed.Select(Shape).ToArray());
}
}
}
@@ -0,0 +1,139 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.V2.Core.SignalAdapters;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
{
/// <summary>
/// Sessions d29e27e3 and 4377911b: a concurrent IME writer overwrote the result line of a platform script, and the
/// overwrite check recovered it one pass later. Recovered lines were never fresh, so they resolved in the agent's own
/// cached zone (+60) instead of anchoring themselves (+120): the end stamp landed an hour ahead of the clock and the
/// run duration was omitted. Bytes that read differently at a position than at the last look were written after that
/// look, so when the replaced bytes were confirmed seconds ago the new line's age is bounded like an appended line's —
/// these tests pin that it anchors itself then, and only then.
/// </summary>
public sealed partial class ImeLogTrackerOverwriteRewindTests
{
private static void AppendExecutorLog(Harness h, string text)
=> File.AppendAllText(Path.Combine(Path.GetDirectoryName(h.LogPath)!, "AgentExecutor.log"), text, new UTF8Encoding(false));
/// <summary>An IME log entry with the byte length of <paramref name="sameLengthAs"/>: what a stale writer's line replaces exactly.</summary>
private static string FillerLike(string time, string sameLengthAs)
{
var bare = ImeEntryAt(time, "[Win32App] checking ");
Assert.True(sameLengthAs.Length > bare.Length);
return ImeEntryAt(time, "[Win32App] checking " + new string('x', sameLengthAs.Length - bare.Length));
}
/// <summary>
/// A platform run whose IME writer lives in +120 (local = UTC + 2 h; the harness clock is 19:30Z): both logs are
/// observed empty first, so what follows is read fresh. Returns the IME log position where the filler line sits.
/// </summary>
private static async Task<long> StartRunAndReadFiller(Harness h, string fillerTime, string resultEntry)
{
h.Append(string.Empty);
AppendExecutorLog(h, string.Empty);
await h.Tracker.RunPollPassAsync(default);
AppendExecutorLog(h, RunStart("21:29:50.0000000", "1001"));
h.Append(ImeRunStart("21:29:48.0000000"));
await h.Tracker.RunPollPassAsync(default);
// Another IME writer appends a line; the tracker reads it.
var at = h.Length;
h.Append(FillerLike(fillerTime, resultEntry));
h.Now = h.Now.AddSeconds(1);
await h.Tracker.RunPollPassAsync(default);
return at;
}
[Fact]
public async Task Result_line_recovered_seconds_after_its_bytes_were_read_anchors_itself_like_a_fresh_line()
{
using var h = new Harness("IntuneManagementExtension.log", ScriptPatterns());
var result = ImeRunResult("21:30:01.1000000", "Success");
var at = await StartRunAndReadFiller(h, "21:30:00.5000000", result);
// The script ends: its end block, then the stale IME service writes the result over the filler.
AppendExecutorLog(h, RunEnd("21:30:01.0000000", 0, "done", ""));
h.Overwrite(at, result);
h.Now = h.Now.AddSeconds(1);
await PassAndSettle(h);
Assert.Equal(1, h.Health.OverwriteRewinds);
var script = Assert.Single(h.Completed);
Assert.Equal(CmTraceOffsetOrigin.LineAnchored, script.ResultProvenance!.Origin);
Assert.Equal(120, script.ResultProvenance.OffsetMinutes);
Assert.Equal(ImeLogTrackerAdapter.DurationPairVerdict.Verified, ImeLogTrackerAdapter.JudgeDurationPair(
script.StartedAtProvenance, script.ResultProvenance, false, script.ResultObservedAtUtc!.Value - script.StartedAtUtc!.Value, out _));
}
[Fact]
public async Task Verification_passes_keep_the_replaced_bytes_current_for_a_script_that_runs_longer()
{
using var h = new Harness("IntuneManagementExtension.log", ScriptPatterns());
var result = ImeRunResult("21:30:41.1000000", "Success");
var at = await StartRunAndReadFiller(h, "21:30:00.5000000", result);
// The script runs 40 s; the cadence check verifies the ledger every 10 s while the run is in flight.
for (var i = 0; i < 4; i++)
{
h.Now = h.Now.AddSeconds(10);
await h.Tracker.RunPollPassAsync(default);
}
AppendExecutorLog(h, RunEnd("21:30:41.0000000", 0, "done", ""));
h.Overwrite(at, result);
h.Now = h.Now.AddSeconds(1);
await PassAndSettle(h);
var script = Assert.Single(h.Completed);
Assert.Equal(CmTraceOffsetOrigin.LineAnchored, script.ResultProvenance!.Origin);
Assert.Equal(120, script.ResultProvenance.OffsetMinutes);
}
[Fact]
public async Task Overwrite_does_not_move_the_files_measured_writer_offset()
{
using var h = new Harness("IntuneManagementExtension.log", ScriptPatterns());
var result = ImeRunResult("21:30:01.1000000", "Success");
var at = await StartRunAndReadFiller(h, "21:30:00.5000000", result);
Assert.True(h.Tracker.OffsetCalibrator.TryGetOffset("IntuneManagementExtension.log", out var before));
AppendExecutorLog(h, RunEnd("21:30:01.0000000", 0, "done", ""));
h.Overwrite(at, result);
h.Now = h.Now.AddSeconds(1);
await PassAndSettle(h);
Assert.True(h.Tracker.OffsetCalibrator.TryGetOffset("IntuneManagementExtension.log", out var after));
Assert.Equal(before, after);
}
[Theory]
// Replaced bytes confirmed 1 s / 30 s ago: written since, age bounded like an appended line's.
[InlineData(true, true, 1, true)]
[InlineData(true, true, 30, true)]
// Confirmed longer ago: the new bytes could be minutes old — their age may round onto the 15-minute grid.
[InlineData(true, true, 31, false)]
// The clock stepped back since the confirmation: the age is unknown.
[InlineData(true, true, -1, false)]
// The divergence lies in bytes the ledger never held (dropped by a cap): nothing dates them.
[InlineData(true, false, 1, false)]
// The pass itself is not fresh — the first pass after a restart never anchors.
[InlineData(false, true, 1, false)]
// Nothing was replaced.
[InlineData(true, true, null, false)]
public void Rewound_entry_is_fresh_only_when_the_bytes_it_replaced_were_confirmed_within_the_fresh_window(
bool passLinesAreFresh, bool entryAtRewindPointReplaced, int? secondsSinceConfirmed, bool expected)
{
var now = new DateTime(2026, 10, 4, 19, 2, 0, DateTimeKind.Utc);
DateTime? confirmed = secondsSinceConfirmed.HasValue ? now.AddSeconds(-secondsSinceConfirmed.Value) : (DateTime?)null;
Assert.Equal(expected, ImeLogTracker.RewoundEntryIsFresh(passLinesAreFresh, entryAtRewindPointReplaced, confirmed, now));
}
}
}
@@ -0,0 +1,368 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
{
/// <summary>
/// Session 756970cf: IME launched a Win32 detection executor and a platform-script executor 15 ms apart, and both
/// opened AgentExecutor.log at the same end-of-file offset. The platform executor's shorter start block overwrote
/// the detection executor's, whose identity line the tracker never saw; the detection's end block (exit 1 and its
/// stdout), written at its own position behind its own start block, then sat behind the PLATFORM start block, and
/// "the nearest preceding marker owns the line" handed another script's output and exit code to the platform
/// script. These tests pin the writer boundary: an entry that is not exactly one CMTrace record is where one
/// writer's bytes end and another's begin, and nothing behind it belongs to the invocation in front of it.
/// </summary>
public sealed partial class ImeLogTrackerOverwriteRewindTests
{
private const string RaceUserId = "00000000-0000-0000-0000-000000000000";
private const string RacePlatformId = "1d6f0a52-3c4e-4b7a-9e21-5f8c7d2b9a10";
private const string RaceDetectionAppId = "8b2e4c71-6a9d-4f03-b5e8-2c7a1d9f4e36";
private const string RaceFillerAppId = "5e1c2b7d-0a4f-4c8e-9d36-7b2f1e8a4c50";
private const string ImeDir = @"C:\Program Files (x86)\Microsoft Intune Management Extension";
private const string DetectionStdout = "VERBOSE: === Sample Suite Detection Started ===\r\nWARNING: === DETECTION FAILED: Sample Suite is not installed ===";
private const string PlatformStdout = "=== bootstrap loader started ===\r\nInstaller finished (exit code 0).";
private static string EntryAt(string time, string message)
=> $"<![LOG[{message}]LOG]!><time=\"{time}\" date=\"9-10-2026\" component=\"AgentExecutor\" context=\"\" type=\"1\" thread=\"1\" file=\"\">\r\n";
/// <summary>The four lines every AgentExecutor invocation starts with — identical length for every executor.</summary>
private static string ExecutorBanner(string time)
=> EntryAt(time, "ExecutorLog AgentExecutor gets invoked")
+ EntryAt(time, "Creating command line parser, name delimiter is - and value separator is .")
+ EntryAt(time, "Getting Ordered Parameters")
+ EntryAt(time, "Parsing Ordered Parameters.");
private static string ExecutorLaunch(string time, string scriptPath, string pid)
=> EntryAt(time, "Prepare to run Powershell Script ..")
+ EntryAt(time, $"cmd line for running powershell is -NoProfile -executionPolicy bypass -file \"{scriptPath}\" ")
+ EntryAt(time, "runAs32BitOn64 = False, so Disable Wow64FsRedirection")
+ EntryAt(time, @"PowerShell path is C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe")
+ EntryAt(time, $"[Executor] created powershell with process id {pid}");
private static string PlatformStartBlock(string time, string policyId, string pid)
{
var path = $@"{ImeDir}\Policies\Scripts\{RaceUserId}_{policyId}.ps1";
var results = $@"{ImeDir}\Policies\Results\{RaceUserId}_{policyId}";
return ExecutorBanner(time)
+ EntryAt(time, $"Adding argument powershell with value {path} to the named argument list.")
+ EntryAt(time, "Powershell option gets invoked")
+ EntryAt(time, path)
+ EntryAt(time, results + ".output")
+ EntryAt(time, results + ".error")
+ EntryAt(time, results + ".timeout")
+ ExecutorLaunch(time, path, pid);
}
private static string DetectionStartBlock(string time, string appId, string pid)
{
var path = $@"{ImeDir}\Content\DetectionScripts\{appId}_2.ps1";
return ExecutorBanner(time)
+ EntryAt(time, $"Adding argument powershellDetection with value {path} to the named argument list.")
+ EntryAt(time, "PowershellDetection option gets invoked")
+ EntryAt(time, path)
+ EntryAt(time, path + "quotedResultFilePath.txt")
+ EntryAt(time, path + "quotedErrorFilePath.txt")
+ EntryAt(time, path + "quotedTimeoutFilePath.txt")
+ EntryAt(time, path + "quotedExitCodeFilePath.txt")
+ ExecutorLaunch(time, path, pid);
}
private static string ExecutorEndBlock(string time, int exitCode, string stdout)
=> EntryAt(time, $"Powershell exit code is {exitCode}")
+ EntryAt(time, $"length of out={stdout.Length}")
+ EntryAt(time, "length of error=0")
+ EntryAt(time, "error from script =")
+ EntryAt(time, exitCode == 0 ? "Powershell script is successfully executed." : "Powershell script is failed to execute")
+ EntryAt(time, $"write output done. output = {stdout}, error = ")
+ EntryAt(time, "Revert Wow64FsRedirection")
+ EntryAt(time, "Agent executor completed.");
private static string PlatformResultLine(string policyId)
=> $"[PowerShell] User Id = {RaceUserId}, Policy id = {policyId}, policy result = Success";
/// <summary>The file offsets at which the entries of an ASCII <paramref name="block"/> start when it is written at <paramref name="at"/>.</summary>
private static HashSet<long> EntryStarts(string block, long at)
{
Assert.True(block.All(c => c < 128), "offset arithmetic assumes ASCII test content");
var starts = new HashSet<long>();
for (var i = block.IndexOf("<![LOG[", StringComparison.Ordinal); i >= 0; i = block.IndexOf("<![LOG[", i + 1, StringComparison.Ordinal))
starts.Add(at + i);
return starts;
}
[Fact]
public async Task Executor_that_opened_at_the_same_offset_never_lends_its_end_block_to_the_platform_script()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
// An earlier, finished detection run: the race starts mid-file like in the field log.
h.Append(DetectionStartBlock("12:00:00.0000000", RaceFillerAppId, "1548") + ExecutorEndBlock("12:00:00.5000000", 0, "detected"));
await h.Pass();
var x = h.Length;
// Detection executor C and platform executor D open the file at the same end x. C writes first; the tracker
// reads C's banner, then D writes its own start block over C's.
var cStart = DetectionStartBlock("12:00:01.0000000", RaceDetectionAppId, "6416");
var dStart = PlatformStartBlock("12:00:01.2000000", RacePlatformId, "2524");
var cHead = cStart.Substring(0, cStart.IndexOf("<![LOG[Getting Ordered Parameters", StringComparison.Ordinal));
h.Append(cHead);
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
h.Append(cStart.Substring(cHead.Length));
h.Overwrite(x, dStart);
// The field geometry: D's start block is shorter and ends inside one of C's lines, so the tail of that line
// sits between D's start block and C's remaining bytes.
Assert.True(dStart.Length < cStart.Length);
Assert.DoesNotContain(x + dStart.Length, EntryStarts(cStart, x));
h.Now = h.Now.AddMilliseconds(150);
await h.Pass();
Assert.True(h.Health.OverwriteRewinds >= 1);
// C ends: its end block lands at its own position, right behind its own (overwritten) start block.
Assert.Equal(x + cStart.Length, h.Length);
var cEnd = ExecutorEndBlock("12:00:07.0000000", 1, DetectionStdout);
h.Append(cEnd);
h.Now = h.Now.AddSeconds(6);
await h.Pass();
// IME's result for D reaches the tracker before D's own end block: the completion must wait for it.
h.Tracker.ProcessLogMessageForTest(PlatformResultLine(RacePlatformId), sourceFileName: "IntuneManagementExtension.log");
Assert.Empty(h.Completed);
// D ends: its end block lands at ITS position, over the fragment and C's bytes; C's last line survives.
var dEnd = ExecutorEndBlock("12:00:09.5000000", 0, PlatformStdout);
Assert.True(dEnd.Length < cStart.Length - dStart.Length + cEnd.Length);
h.Overwrite(x + dStart.Length, dEnd);
h.Now = h.Now.AddMilliseconds(130);
await h.Pass();
h.Tracker.FlushPendingPlatformScriptResults(h.Now);
var script = Assert.Single(h.Completed);
Assert.Equal(RacePlatformId, script.PolicyId);
Assert.Equal("Success", script.Result);
Assert.Equal(0, script.ExitCode);
Assert.Equal(PlatformStdout.Replace("\r\n", "\n"), script.Stdout);
Assert.DoesNotContain(h.Completed, s => (s.Stdout ?? string.Empty).Contains("Detection"));
// The run is known to the registry reconciliation under the user id of its own lines (D-316).
Assert.Equal(RaceUserId, script.UserId);
Assert.False(string.IsNullOrEmpty(script.RunId));
h.Tracker.ScriptRuns.NotePassCompleted(DateTime.MaxValue.AddDays(-1));
Assert.Equal(PlatformRunClaim.Claimed, h.Tracker.ScriptRuns.TryClaim(RaceUserId, RacePlatformId, h.Now, out var run));
Assert.Equal(script.RunId, run!.RunId);
}
/// <summary>
/// The mirror image: a foreign executor whose stream position lies inside the platform start block (it opened
/// at the same end with a shorter start block) writes its end block there. Its first line merges with the head of
/// a platform line — cut inside the message or inside the trailer, which the parser would otherwise read up to
/// the foreign trailer — and its output line follows without any marker in between.
/// </summary>
[Theory]
[InlineData(15)]
[InlineData(-30)]
public async Task Foreign_end_block_merged_into_the_platform_start_block_never_reaches_the_platform_script(int cut)
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(DetectionStartBlock("12:00:00.0000000", RaceFillerAppId, "1548") + ExecutorEndBlock("12:00:00.5000000", 0, "detected"));
await h.Pass();
var x = h.Length;
var dStart = PlatformStartBlock("12:00:01.0000000", RacePlatformId, "2524");
h.Append(dStart);
await h.Pass();
// The foreign end block starts inside the line after D's argument line.
var lineAfterArgument = x + dStart.IndexOf("<![LOG[Powershell option gets invoked", StringComparison.Ordinal);
var lineLength = EntryAt("12:00:01.0000000", "Powershell option gets invoked").Length;
var at = lineAfterArgument + (cut > 0 ? cut : lineLength + cut);
var foreignEnd = ExecutorEndBlock("12:00:03.0000000", 1, "foreign output");
Assert.True(at + foreignEnd.Length < x + dStart.Length, "the foreign block must end inside D's start block");
h.Overwrite(at, foreignEnd);
h.Tracker.RequestOverwriteCheckForTest();
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
h.Tracker.ProcessLogMessageForTest(PlatformResultLine(RacePlatformId), sourceFileName: "IntuneManagementExtension.log");
h.Now = h.Now.Add(ImeLogTracker.PlatformScriptEndBlockGrace);
h.Tracker.FlushPendingPlatformScriptResults(h.Now);
// No owner is provable behind the cut: the completion goes out without end-block data, never with the foreign one.
var script = Assert.Single(h.Completed);
Assert.Equal("Success", script.Result);
Assert.Null(script.ExitCode);
Assert.Null(script.Stdout);
}
[Fact]
public async Task Entry_read_unchanged_after_a_rewind_keeps_its_platform_marker()
{
const string platformId = "2b7d9e41-8c3a-4f60-a5d2-9e1b7c4f3a08";
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
// Detection W waits for its script; detection X runs completely behind it; platform F starts behind X.
var wStart = DetectionStartBlock("12:00:00.0000000", RaceFillerAppId, "1111");
var xRun = DetectionStartBlock("12:00:00.2000000", RaceDetectionAppId, "2222") + ExecutorEndBlock("12:00:00.4000000", 0, "x detected");
h.Append(wStart + xRun + PlatformStartBlock("12:00:01.0000000", platformId, "3333"));
await h.Pass();
// W ends at its own position, over the head of X's run; F's start block keeps its bytes and offset.
var wEnd = ExecutorEndBlock("12:00:02.0000000", 0, "w detected");
Assert.True(wEnd.Length < xRun.Length);
h.Overwrite(wStart.Length, wEnd);
h.Tracker.RequestOverwriteCheckForTest();
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
Assert.Equal(1, h.Health.OverwriteRewinds);
// F's end block lands behind its start block: still F's, because F's marker survived with its bytes.
h.Append(ExecutorEndBlock("12:00:05.0000000", 7, "Hello from F"));
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
h.Tracker.ProcessLogMessageForTest(PlatformResultLine(platformId), sourceFileName: "IntuneManagementExtension.log");
var script = Assert.Single(h.Completed);
Assert.Equal(platformId, script.PolicyId);
Assert.Equal(7, script.ExitCode);
Assert.Equal("Hello from F", script.Stdout);
}
[Fact]
public async Task Writer_boundary_read_unchanged_after_a_rewind_still_closes_the_platform_invocation()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
// Detection Q waits for its script while detection R runs completely behind it.
var qStart = DetectionStartBlock("12:00:00.0000000", RaceFillerAppId, "1111");
var rRun = DetectionStartBlock("12:00:00.2000000", "6c3f8a20-1d5e-4b97-8f4a-0e2d6b9c7a13", "2222") + ExecutorEndBlock("12:00:00.4000000", 0, "r detected");
h.Append(qStart + rRun);
await h.Pass();
var x = h.Length;
// The race of the field case behind them: the boundary closes D.
var cStart = DetectionStartBlock("12:00:01.0000000", RaceDetectionAppId, "6416");
var dStart = PlatformStartBlock("12:00:01.2000000", RacePlatformId, "2524");
h.Append(cStart);
h.Overwrite(x, dStart);
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
// Q ends at its own position, over R's run: the rewind starts in front of the race, whose bytes read unchanged.
var qEnd = ExecutorEndBlock("12:00:02.0000000", 0, "q detected");
Assert.True(qEnd.Length < rRun.Length);
h.Overwrite(qStart.Length, qEnd);
h.Tracker.RequestOverwriteCheckForTest();
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
// C's end block behind the boundary stays unowned; D's own end block completes D.
var cEnd = ExecutorEndBlock("12:00:07.0000000", 1, DetectionStdout);
h.Append(cEnd);
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
h.Tracker.ProcessLogMessageForTest(PlatformResultLine(RacePlatformId), sourceFileName: "IntuneManagementExtension.log");
Assert.Empty(h.Completed);
h.Overwrite(x + dStart.Length, ExecutorEndBlock("12:00:09.5000000", 0, PlatformStdout));
h.Now = h.Now.AddMilliseconds(130);
await h.Pass();
h.Tracker.FlushPendingPlatformScriptResults(h.Now);
var script = Assert.Single(h.Completed);
Assert.Equal(0, script.ExitCode);
Assert.Equal(PlatformStdout.Replace("\r\n", "\n"), script.Stdout);
}
[Fact]
public async Task Raw_text_in_the_executor_log_is_never_matched_and_closes_the_platform_invocation()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(PlatformStartBlock("12:00:00.0000000", RacePlatformId, "2524"));
await h.Pass();
// The tail of a cut line that happens to read like an exit line, then a foreign end block.
h.Append("Powershell exit code is 5\r\n" + ExecutorEndBlock("12:00:02.0000000", 6, "foreign output"));
// A fragment right at the bookmark stops the first pass for a check; the second one reads it.
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
h.Tracker.ProcessLogMessageForTest(PlatformResultLine(RacePlatformId), sourceFileName: "IntuneManagementExtension.log");
h.Now = h.Now.Add(ImeLogTracker.PlatformScriptEndBlockGrace);
h.Tracker.FlushPendingPlatformScriptResults(h.Now);
var script = Assert.Single(h.Completed);
Assert.Null(script.ExitCode);
Assert.Null(script.Stdout);
}
[Fact]
public async Task Fragment_in_the_ime_log_does_not_close_the_platform_run_context()
{
using var h = new Harness("IntuneManagementExtension.log", ScriptPatterns());
// In the IME log the service writes the platform marker and the context line itself; the fragment in
// between is the tail of an executor's telemetry line and separates nothing.
h.Append(Entry(@"Script file C:\Program Files (x86)\Microsoft Intune Management Extension\Policies\Scripts\" + RaceUserId + "_" + RacePlatformId + ".ps1 is generated.", "17")
+ "ing mananger...]LOG]!><time=\"12:00:00.0000000\" date=\"9-10-2026\" component=\"AgentExecutor\" context=\"\" type=\"1\" thread=\"1\" file=\"\">\r\n"
+ Entry("Launch powershell executor in user session", "17"));
await h.Pass();
h.Tracker.ProcessLogMessageForTest(PlatformResultLine(RacePlatformId));
h.Now = h.Now.Add(ImeLogTracker.PlatformScriptEndBlockGrace);
h.Tracker.FlushPendingPlatformScriptResults(h.Now);
var script = Assert.Single(h.Completed);
Assert.Equal("User", script.RunContext);
}
[Fact]
public async Task Cut_multi_line_output_adds_one_boundary_not_one_per_line()
{
using var h = new Harness("AgentExecutor.log", ScriptPatterns());
h.Append(PlatformStartBlock("12:00:00.0000000", RacePlatformId, "2524"));
await h.Pass();
var markersBefore = h.Tracker.InvocationMarkerCountForTest("AgentExecutor.log");
var lines = string.Concat(Enumerable.Range(1, 600).Select(i => $"orphaned output line {i}\r\n"));
h.Append(lines);
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
h.Now = h.Now.AddMilliseconds(100);
await h.Pass();
Assert.Equal(markersBefore + 1, h.Tracker.InvocationMarkerCountForTest("AgentExecutor.log"));
}
[Theory]
[InlineData("<![LOG[Powershell exit code is 0]LOG]!><time=\"12:00:00.0000000\" date=\"9-10-2026\" component=\"AgentExecutor\" context=\"\" type=\"1\" thread=\"1\" file=\"\">", true)]
[InlineData("<![LOG[write output done. output = line one\nline two, error = ]LOG]!><time=\"12:00:00.0000000\" date=\"9-10-2026\" component=\"AgentExecutor\" context=\"\" type=\"1\" thread=\"1\" file=\"\">", true)]
[InlineData("\uFEFF<![LOG[ExecutorLog AgentExecutor gets invoked]LOG]!><time=\"12:00:00.0000000\" date=\"9-10-2026\" component=\"AgentExecutor\" context=\"\" type=\"1\" thread=\"1\" file=\"\">", true)]
[InlineData("t code is 3]LOG]!><time=\"12:00:00.0000000\" date=\"9-10-2026\" component=\"AgentExecutor\" context=\"\" type=\"1\" thread=\"1\" file=\"\">", false)]
[InlineData("", false)]
[InlineData("<![LOG[Powe<![LOG[Powershell exit code is 1]LOG]!><time=\"12:00:00.0000000\" date=\"9-10-2026\" component=\"AgentExecutor\" context=\"\" type=\"1\" thread=\"1\" file=\"\">", false)]
[InlineData("<![LOG[Powershell option gets invoked]LOG]!><time=\"12:00:00.0000000\" da<![LOG[Powershell exit code is 1]LOG]!><time=\"12:00:01.0000000\" date=\"9-10-2026\" component=\"AgentExecutor\" context=\"\" type=\"1\" thread=\"1\" file=\"\">", false)]
[InlineData("<![LOG[write output done. output = line one\nline<![LOG[Agent executor completed.]LOG]!><time=\"12:00:00.0000000\" date=\"9-10-2026\" component=\"AgentExecutor\" context=\"\" type=\"1\" thread=\"1\" file=\"\">", false)]
public void Single_cmtrace_record_is_told_apart_from_the_cut_of_two_writers(string text, bool expected)
{
Assert.Equal(expected, ImeLogTracker.IsSingleCmTraceRecord(text));
}
[Theory]
[InlineData("AgentExecutor.log", true)]
[InlineData("AgentExecutor-20261003-233348.log", true)]
[InlineData("IntuneManagementExtension.log", false)]
[InlineData("IntuneManagementExtension-20261003-233348.log", false)]
[InlineData("HealthScripts.log", false)]
[InlineData("AppWorkload.log", false)]
public void Writer_boundaries_apply_to_the_executor_log_live_and_rotated(string fileName, bool expected)
{
Assert.Equal(expected, ImeLogTracker.IsWriterBoundaryLogFile(fileName));
}
}
}
@@ -25,7 +25,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
/// 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
public sealed partial class ImeLogTrackerOverwriteRewindTests
{
private static readonly DateTime T0 = new DateTime(2026, 9, 10, 19, 30, 0, DateTimeKind.Utc);
@@ -42,7 +42,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
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",
"PS-AGENT-OUTPUT", "PS-AGENT-ERROR", "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>
@@ -502,6 +502,10 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
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());
// Seen empty first: the start lines are fresh and anchor themselves like the recovered result line will,
// so start and result sit on one timeline whatever the host's own zone is.
h.Append(string.Empty);
await h.Pass();
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;
@@ -83,8 +83,33 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
Assert.Single(emitted);
}
[Theory]
// IME's verdict is Success exactly when the error file is empty, whatever the exit code. Judged by the exit
// code, session 67ae751c showed exit 0 with the script's own stderr as Success, and 46a41a0a an exit 1 without
// stderr as Failed, a run IME reported as Success one cycle earlier.
[InlineData(0, "Invoke-WebRequest : The remote name could not be resolved", "Failed")]
[InlineData(1, "", "Success")]
[InlineData(1, " \n", "Success")]
[InlineData(0, "", "Success")]
[InlineData(1, null, "Failed")]
[InlineData(0, null, "Success")]
public void Fallback_judges_like_ime_by_the_stderr_it_read(int exitCode, string? stderr, string expected)
{
using var tmp = new TempDirectory();
var tracker = BuildTracker(tmp, out var emitted);
var observedAt = new DateTime(2026, 6, 19, 12, 59, 4, DateTimeKind.Utc);
tracker.SeedPendingPlatformScriptForTesting("0d2294bf", exitCode: exitCode, exitObservedAtUtc: observedAt, stderr: stderr);
tracker.FlushPendingPlatformScriptResults(observedAt.AddSeconds(20));
var script = Assert.Single(emitted);
Assert.Equal(expected, script.Result);
Assert.Equal(exitCode, script.ExitCode);
Assert.Equal("agentexecutor_fallback", script.ResultSource);
}
[Fact]
public void Nonzero_exit_code_emits_failed_result()
public void Nonzero_exit_code_without_a_stderr_line_emits_failed_result()
{
using var tmp = new TempDirectory();
var tracker = BuildTracker(tmp, out var emitted);
@@ -108,6 +108,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
var tracker = BuildTracker(tmp, out var emitted);
tracker.LoadStateForTest(); // must not throw
Assert.Equal(0, tracker.ParkedPlatformScriptCountForTest);
// Degrades to pre-fix behavior: no markers restored, normal emission works (the
// result waits for its end block until the shutdown flush).
@@ -116,6 +117,32 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Ime
Assert.Single(emitted);
}
[Fact]
public void Pending_slot_from_a_state_file_without_executor_start_loads_and_completes()
{
using var tmp = new TempDirectory();
var started = new DateTime(2026, 10, 4, 9, 0, 0, DateTimeKind.Utc);
// A pending slot written by an agent before runs carried their executor start and run id.
var persistence = new ImeTrackerStatePersistence(tmp.Path, new AgentLogger(tmp.Path, AgentLogLevel.Info));
persistence.Save(new ImeTrackerStateData
{
PendingPlatformScripts = new List<ScriptExecutionState>
{
new ScriptExecutionState { PolicyId = "policyF", ScriptType = "platform", StartedAtUtc = started, ExitCode = 0, ExitObservedAtUtc = started.AddSeconds(5), Stdout = "done", Stderr = "" },
},
});
var tracker = BuildTracker(tmp, out var emitted);
tracker.LoadStateForTest();
tracker.CompletePlatformScriptFromImeResultForTesting("policyF", "Success", started.AddSeconds(6));
var script = Assert.Single(emitted);
Assert.Equal("Success", script.Result);
Assert.Equal(0, script.ExitCode);
Assert.False(string.IsNullOrEmpty(script.RunId));
}
[Fact]
public void Stale_result_older_than_current_run_start_is_dropped()
{
@@ -0,0 +1,85 @@
using System.Linq;
using AutopilotMonitor.Agent.V2.Core.Monitoring;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Periodic;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring
{
/// <summary>
/// The local network fingerprint a failed registration is stored with: SHA-256 over the
/// connection type and gateway <see cref="NetworkChangeDetector"/> captures, cut to 16 hex
/// characters; unknown (null) whenever either input is missing. Gateways are documentation
/// addresses (RFC 5737).
/// </summary>
public sealed class NetworkFingerprintTests
{
private static NetworkChangeDetector.NetworkStateSnapshot State(string? connectionType, string? gateway, bool hasNetwork = true) =>
new NetworkChangeDetector.NetworkStateSnapshot
{
HasNetwork = hasNetwork,
ConnectionType = connectionType,
Gateway = gateway,
};
[Fact]
public void Same_connection_type_and_gateway_give_the_same_short_hex_fingerprint()
{
var first = NetworkFingerprint.Of(State("WiFi", "192.0.2.1"));
var second = NetworkFingerprint.Of(State("WiFi", "192.0.2.1"));
Assert.Equal(first, second);
Assert.Equal(NetworkFingerprint.Length, first!.Length);
Assert.True(first.All(c => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')), first);
}
[Theory]
[InlineData("WiFi", "198.51.100.1")] // another gateway
[InlineData("Ethernet", "192.0.2.1")] // another connection type
public void Another_gateway_or_connection_type_is_another_network(string connectionType, string gateway)
{
Assert.NotEqual(
NetworkFingerprint.Of(State("WiFi", "192.0.2.1")),
NetworkFingerprint.Of(State(connectionType, gateway)));
}
[Fact]
public void Unknown_network_has_no_fingerprint()
{
Assert.Null(NetworkFingerprint.Of(null));
Assert.Null(NetworkFingerprint.Of(State("WiFi", "192.0.2.1", hasNetwork: false)));
Assert.Null(NetworkFingerprint.Of(State("WiFi", null)));
Assert.Null(NetworkFingerprint.Of(State(null, "192.0.2.1")));
}
[Theory]
[InlineData("aaaa", "aaaa", false)]
[InlineData("aaaa", "bbbb", true)]
public void Differs_compares_known_fingerprints(string before, string after, bool expected)
{
Assert.Equal(expected, NetworkFingerprint.Differs(before, after));
}
[Theory]
[InlineData(null, "aaaa")]
[InlineData("aaaa", null)]
[InlineData(null, null)]
public void Differs_is_unknown_when_either_side_is(string? before, string? after)
{
Assert.Null(NetworkFingerprint.Differs(before, after));
}
[Fact]
public void TryCapture_reads_this_machine_without_throwing()
{
// Machine-dependent: either no active interface (null) or a well-formed fingerprint
// that matches the detector's own capture.
var captured = NetworkFingerprint.TryCapture();
if (captured != null)
{
Assert.Equal(NetworkFingerprint.Length, captured.Length);
Assert.Equal(NetworkFingerprint.Of(NetworkChangeDetector.CaptureInterfaceState()), captured);
}
}
}
}
@@ -0,0 +1,165 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Runtime
{
/// <summary>
/// The bootstrap provenance in <c>agent_started</c>. Fixture lines are the scripts' real
/// formats: installer <c>[ts] Bootstrap script version: vX.Y</c>
/// (Install-AutopilotMonitor.ps1), loader <c>[ts] === Autopilot Monitor bootstrap loader vX.Y
/// started (user=..., pid=...) ===</c> (Start-AutopilotMonitor.ps1).
/// </summary>
public sealed class BootstrapProvenanceReaderTests : IDisposable
{
private readonly string _dir = Path.Combine(Path.GetTempPath(), "apm-provenance-" + Guid.NewGuid().ToString("N"));
public BootstrapProvenanceReaderTests() => Directory.CreateDirectory(_dir);
public void Dispose()
{
try { Directory.Delete(_dir, recursive: true); } catch { /* best effort */ }
}
private void Write(string name, params string[] lines) =>
File.WriteAllText(Path.Combine(_dir, name), string.Join("\r\n", lines) + "\r\n", Encoding.ASCII);
private static string InstallerRun(string version) => $"[2026-10-13 09:00:00] Bootstrap script version: v{version}";
private static string LoaderRun(string version) =>
$"[2026-10-13 08:59:58] === Autopilot Monitor bootstrap loader v{version} started (user=SECRETUSER, pid=4242) ===";
[Fact]
public void Loader_path_reports_installer_and_loader_versions()
{
Write(BootstrapProvenanceReader.LoaderLogName, LoaderRun("1.2"), "[2026-10-13 08:59:59] Installer signature verified");
Write(BootstrapProvenanceReader.InstallerLogName, "[2026-10-13 09:00:00] ===== Autopilot Monitor Bootstrap Started =====", InstallerRun("2.5"));
var p = BootstrapProvenanceReader.Read(_dir);
Assert.True(p.ScriptLogPresent);
Assert.Equal("2.5", p.ScriptVersion);
Assert.Equal("loader", p.LoaderLog);
Assert.Equal("1.2", p.LoaderVersion);
Assert.False(p.OobeLogPresent);
}
[Fact]
public void The_last_run_wins_when_the_log_grew_over_several_runs()
{
Write(BootstrapProvenanceReader.InstallerLogName, InstallerRun("2.3"), "x", InstallerRun("2.4"), "y", InstallerRun("2.5"), "z");
Assert.Equal("2.5", BootstrapProvenanceReader.Read(_dir).ScriptVersion);
}
[Fact]
public void A_directly_assigned_installer_has_no_loader_log()
{
Write(BootstrapProvenanceReader.InstallerLogName, InstallerRun("2.4"));
var p = BootstrapProvenanceReader.Read(_dir);
Assert.Equal("2.4", p.ScriptVersion);
Assert.Null(p.LoaderLog);
Assert.Null(p.LoaderVersion);
}
[Fact]
public void An_installer_from_before_the_version_line_is_present_but_without_version()
{
Write(BootstrapProvenanceReader.InstallerLogName, "[2026-03-01 09:00:00] ===== Autopilot Monitor Bootstrap Started =====");
var p = BootstrapProvenanceReader.Read(_dir);
Assert.True(p.ScriptLogPresent);
Assert.Null(p.ScriptVersion);
}
[Fact]
public void The_msi_loader_log_is_told_apart_from_the_intune_loader()
{
Write(BootstrapProvenanceReader.MsiLoaderLogName, LoaderRun("1.1"));
var p = BootstrapProvenanceReader.Read(_dir);
Assert.Equal("msi", p.LoaderLog);
Assert.Equal("1.1", p.LoaderVersion);
}
[Fact]
public void With_both_loader_logs_the_most_recently_written_one_counts()
{
Write(BootstrapProvenanceReader.LoaderLogName, LoaderRun("1.0"));
Write(BootstrapProvenanceReader.MsiLoaderLogName, LoaderRun("1.2"));
File.SetLastWriteTimeUtc(Path.Combine(_dir, BootstrapProvenanceReader.LoaderLogName), DateTime.UtcNow.AddDays(-3));
File.SetLastWriteTimeUtc(Path.Combine(_dir, BootstrapProvenanceReader.MsiLoaderLogName), DateTime.UtcNow.AddMinutes(-1));
var p = BootstrapProvenanceReader.Read(_dir);
Assert.Equal("msi", p.LoaderLog);
Assert.Equal("1.2", p.LoaderVersion);
}
[Fact]
public void The_oobe_go_script_is_recognised_by_its_log()
{
Write(BootstrapProvenanceReader.OobeLogName, "[2026-10-13 09:00:00] ===== Autopilot Monitor OOBE Bootstrap =====");
Assert.True(BootstrapProvenanceReader.Read(_dir).OobeLogPresent);
}
[Fact]
public void Only_the_tail_of_a_large_log_is_read()
{
var filler = Enumerable.Repeat(new string('.', 200), 600).ToArray(); // ~120 KB
var lines = new List<string> { InstallerRun("2.1") };
lines.AddRange(filler);
lines.Add(InstallerRun("2.5"));
Write(BootstrapProvenanceReader.InstallerLogName, lines.ToArray());
Assert.Equal("2.5", BootstrapProvenanceReader.Read(_dir).ScriptVersion);
}
[Fact]
public void A_missing_directory_reads_as_unknown_and_never_throws()
{
var p = BootstrapProvenanceReader.Read(Path.Combine(_dir, "does-not-exist"));
Assert.False(p.ScriptLogPresent);
Assert.Null(p.ScriptVersion);
Assert.Null(p.LoaderLog);
Assert.False(p.OobeLogPresent);
}
[Fact]
public void The_event_keys_carry_versions_only_never_the_account_name()
{
Write(BootstrapProvenanceReader.LoaderLogName, LoaderRun("1.2"));
Write(BootstrapProvenanceReader.InstallerLogName, InstallerRun("2.5"));
var data = new Dictionary<string, object>();
BootstrapProvenanceReader.Read(_dir).AddTo(data);
Assert.Equal(true, data["bootstrapScriptLogPresent"]);
Assert.Equal("2.5", data["bootstrapScriptVersion"]);
Assert.Equal("loader", data["bootstrapLoaderLog"]);
Assert.Equal("1.2", data["bootstrapLoaderVersion"]);
Assert.Equal(false, data["oobeBootstrapLogPresent"]);
Assert.DoesNotContain(data.Values, v => v is string s && s.Contains("SECRETUSER"));
}
[Fact]
public void Keys_that_were_not_found_are_left_out()
{
var data = new Dictionary<string, object>();
BootstrapProvenanceReader.Read(_dir).AddTo(data);
Assert.Equal(new[] { "bootstrapScriptLogPresent", "oobeBootstrapLogPresent" }, data.Keys.OrderBy(k => k).ToArray());
}
}
}
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.IO;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Runtime;
using AutopilotMonitor.Agent.V2.Core.Security;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Runtime
{
/// <summary>
/// Step 5b of the self-update: the staged payload must match its signed catalog before a
/// single file is swapped in. The verifier is replaced through the static hook; each test
/// restores it, as in <see cref="SelfUpdaterGracefulShutdownTests"/>.
/// </summary>
public sealed class SelfUpdaterPayloadCatalogTests : IDisposable
{
private const string StagingDir = @"C:\ProgramData\AutopilotMonitor\Agent-Update";
private readonly Func<string, PayloadCatalogVerdict> _savedVerifier;
private readonly List<string> _log = new List<string>();
public SelfUpdaterPayloadCatalogTests()
{
_savedVerifier = SelfUpdater.VerifyPayloadCatalog;
}
public void Dispose() => SelfUpdater.VerifyPayloadCatalog = _savedVerifier;
[Fact]
public void Production_default_is_the_real_verifier()
{
Assert.Equal(typeof(PayloadCatalogVerifier), _savedVerifier.Method.DeclaringType);
Assert.Equal(nameof(PayloadCatalogVerifier.Verify), _savedVerifier.Method.Name);
}
[Fact]
public void Accepted_payload_lets_the_update_continue()
{
string? checkedDir = null;
SelfUpdater.VerifyPayloadCatalog = dir => { checkedDir = dir; return PayloadCatalogVerdict.Accept(); };
var ok = SelfUpdater.VerifyStagedPayload(StagingDir, _log.Add, out var reason);
Assert.True(ok);
Assert.Null(reason);
Assert.Equal(StagingDir, checkedDir);
Assert.Contains(_log, line => line.Contains("payload catalog verified"));
}
[Fact]
public void Rejected_payload_stops_the_update_and_hands_over_the_reason()
{
SelfUpdater.VerifyPayloadCatalog = _ =>
PayloadCatalogVerdict.Reject("payload differs from catalog (missing: none; extra: evil.dll; changed: none)");
var ok = SelfUpdater.VerifyStagedPayload(StagingDir, _log.Add, out var reason);
Assert.False(ok);
Assert.Contains("extra: evil.dll", reason);
Assert.Contains(_log, line => line.Contains("REJECTED"));
}
[Fact]
public void A_throwing_verifier_is_a_rejection_not_a_pass()
{
SelfUpdater.VerifyPayloadCatalog = _ => throw new InvalidOperationException("runspace broke");
var ok = SelfUpdater.VerifyStagedPayload(StagingDir, _log.Add, out var reason);
Assert.False(ok);
Assert.Contains("runspace broke", reason);
}
[Fact]
public void The_catalog_check_runs_before_any_file_is_swapped()
{
// Source-level pin: CheckAndApplyUpdateAsync has no seam for the download, so the
// ordering that makes the check meaningful is pinned on the source itself.
var source = File.ReadAllText(Path.Combine(FindRepoRoot(),
"src", "Agent", "AutopilotMonitor.Agent.V2.Core", "Monitoring", "Runtime", "SelfUpdater.cs"));
var check = source.IndexOf("if (!VerifyStagedPayload(stagingDir, log, out var catalogRejection))", StringComparison.Ordinal);
var swap = source.IndexOf("var swapOk = SwapFiles(agentDir, stagingDir, log);", StringComparison.Ordinal);
Assert.True(check > 0, "step 5b not found in SelfUpdater.cs");
Assert.True(swap > check, "SwapFiles must come after the catalog check");
Assert.Contains("WriteSkipMarker(\"catalog_rejected\"", source);
}
private static string FindRepoRoot()
{
var dir = new DirectoryInfo(AppContext.BaseDirectory);
while (dir != null && !File.Exists(Path.Combine(dir.FullName, "AutopilotMonitor.sln")))
{
dir = dir.Parent;
}
Assert.NotNull(dir);
return dir.FullName;
}
}
}
@@ -0,0 +1,446 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
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.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
using AutopilotMonitor.DecisionCore.Engine;
using AutopilotMonitor.DecisionCore.Signals;
using AutopilotMonitor.Shared;
using AutopilotMonitor.Shared.Models;
using Newtonsoft.Json;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring
{
/// <summary>
/// CloudExperienceHost navigation (D-310): one agent.log line per page start/stop and per
/// update-page event name, and for the OOBE update page itself (<c>OobeNDUP</c>,
/// <c>RebootNDUP</c>) the <c>oobe_update_page</c> telemetry plus the registry state at its live
/// start and stop. Descriptions follow the Shell-Core manifest templates; a 62407 value
/// (URLs with tenant ids) leaves the tracker only as a boolean, an integer or an HRESULT.
/// </summary>
public sealed class ShellCoreTrackerBreadcrumbTests : IDisposable
{
private static readonly DateTime At = new DateTime(2026, 10, 2, 9, 0, 0, DateTimeKind.Utc);
private const string NdupStarted = "CloudExperienceHost Web App Activity started. CXID: 'OobeNDUP'.";
private const string EspPageStarted = "CloudExperienceHost Web App Activity started. CXID: 'MDMProgressAfterAADRefactored'.";
private const string PageStopped = "CloudExperienceHost Web App Activity stopped. Result: 'success'.";
private const string ScanStartedName = "CloudExperienceHost Web App Event 1. Name: 'ExpeditedUpdate_startWUScanStarted'.";
private const string NavigateWithTenantUrl =
"CloudExperienceHost Web App Event 2. Name: 'Navigate', Value: '{\"httpMethod\":\"GET\",\"url\":\"https://login.microsoftonline.com/00000000-0000-0000-0000-000000000000/oauth2/authorize\"}'.";
private const string DownloadSucceededWithValue =
"CloudExperienceHost Web App Event 2. Name: 'ExpeditedUpdate_commitExpeditionDownloadInstallAsyncSucceeded', Value: '{\"url\":\"https://login.microsoftonline.com/x\"}'.";
private const string CspGateTrue =
"CloudExperienceHost Web App Event 2. Name: 'ExpeditedUpdate_isNDUPAllowedByCSPSucceeded', Value: 'true'.";
// A French Windows labels the same fields "Résultat", "Nom" and "valeur" and quotes them
// in guillemets; the event properties carry the values unchanged in every language.
private const string FrenchNdupStarted = "Activité d’application web CloudExperienceHost démarrée. CXID : « OobeNDUP ».";
private const string FrenchPageStopped = "Activité d’application web CloudExperienceHost arrêtée. Résultat : « success ».";
private const string FrenchCspGate =
"Événement d’application web CloudExperienceHost 2. Nom : « ExpeditedUpdate_isNDUPAllowedByCSPSucceeded », valeur : « true ».";
private readonly TempDirectory _tmp = new TempDirectory();
private readonly FakeSignalIngressSink _sink = new FakeSignalIngressSink();
private IReadOnlyList<OobeUpdateRegistrySnapshot.KeyState> _registry = Array.Empty<OobeUpdateRegistrySnapshot.KeyState>();
public void Dispose() => _tmp.Dispose();
private ShellCoreTracker Build(string? stateDirectory = null) =>
new ShellCoreTracker("S1", "T1",
new InformationalEventPost(_sink, new VirtualClock(At)),
new AgentLogger(_tmp.Path, AgentLogLevel.Info),
helloTracker: null,
oobeUpdateRegistryReader: () => _registry,
stateDirectory: stateDirectory);
private string StateDirectory => Path.Combine(_tmp.Path, "state");
private static void Live(ShellCoreTracker tracker, int eventId, string description, DateTime at, long recordId) =>
tracker.ProcessEvent(eventId, description, at, "Microsoft-Windows-Shell-Core", isBackfill: false, recordId: recordId);
private IReadOnlyList<FakeSignalIngressSink.PostedSignal> ByType(string eventType) =>
_sink.Posted.Where(p =>
p.Kind == DecisionSignalKind.InformationalEvent
&& p.Payload != null
&& p.Payload.TryGetValue(SignalPayloadKeys.EventType, out var et)
&& et == eventType).ToList();
private static IReadOnlyDictionary<string, object> Data(FakeSignalIngressSink.PostedSignal posted) =>
(IReadOnlyDictionary<string, object>)posted.TypedPayload!;
private static OobeUpdateRegistrySnapshot.KeyState NdupKey(string line) =>
new OobeUpdateRegistrySnapshot.KeyState("setupOobeNdup", @"SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\OOBE\NDUP",
exists: true, hasValues: true, line: line);
// ------------------------------------------------------------- breadcrumbs ----
[Fact]
public void PageStart_NamesTheCxid()
{
Assert.Equal("CXH page started: OobeNDUP",
ShellCoreTracker.FormatCxhBreadcrumb(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, lastPage: null));
}
[Fact]
public void PageStop_IsAttributedToTheLastStartedPage()
{
// 62405 names no page — it ends the one started last.
Assert.Equal("CXH page stopped: OobeNDUP (result=success)",
ShellCoreTracker.FormatCxhBreadcrumb(ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, lastPage: "OobeNDUP"));
Assert.Equal("CXH page stopped: ? (result=success)",
ShellCoreTracker.FormatCxhBreadcrumb(ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, lastPage: null));
}
[Fact]
public void UpdatePageEventNames_AreLogged_FromBothEventIds()
{
Assert.Equal("CXH event: ExpeditedUpdate_startWUScanStarted",
ShellCoreTracker.FormatCxhBreadcrumb(ShellCoreTracker.EventId_ShellCore_WebAppEventName, ScanStartedName, lastPage: null));
Assert.Equal("CXH event: ExpeditedUpdate_commitExpeditionDownloadInstallAsyncSucceeded",
ShellCoreTracker.FormatCxhBreadcrumb(ShellCoreTracker.EventId_ShellCore_WebAppEvent, DownloadSucceededWithValue, lastPage: null));
}
[Fact]
public void UnsafeEventValues_NeverReachTheLog()
{
var crumb = ShellCoreTracker.FormatCxhBreadcrumb(
ShellCoreTracker.EventId_ShellCore_WebAppEvent, DownloadSucceededWithValue, lastPage: null);
Assert.NotNull(crumb);
Assert.DoesNotContain("microsoftonline", crumb);
Assert.DoesNotContain("url", crumb!, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void SafeEventValues_AreLogged()
{
Assert.Equal("CXH event: ExpeditedUpdate_isNDUPAllowedByCSPSucceeded (value=true)",
ShellCoreTracker.FormatCxhBreadcrumb(ShellCoreTracker.EventId_ShellCore_WebAppEvent, CspGateTrue, lastPage: null));
}
[Fact]
public void OtherEventNames_AreNotLogged()
{
// Ordinary page traffic (Navigate, Visible, ESP progress) stays out of the log.
Assert.Null(ShellCoreTracker.FormatCxhBreadcrumb(
ShellCoreTracker.EventId_ShellCore_WebAppEvent, NavigateWithTenantUrl, lastPage: "OobeNDUP"));
Assert.Null(ShellCoreTracker.FormatCxhBreadcrumb(
ShellCoreTracker.EventId_ShellCore_WebAppEvent, "CommercialOOBE_ESPProgress_Page_Exiting", lastPage: null));
}
[Theory]
[InlineData("")]
[InlineData("CloudExperienceHost Web App Activity started.")]
public void PageStartWithoutCxid_IsNoBreadcrumb(string description)
{
Assert.Null(ShellCoreTracker.FormatCxhBreadcrumb(ShellCoreTracker.EventId_ShellCore_WebAppStarted, description, lastPage: null));
}
[Fact]
public void LongTokens_AreBounded()
{
var longCxid = new string('X', 500);
var crumb = ShellCoreTracker.FormatCxhBreadcrumb(
ShellCoreTracker.EventId_ShellCore_WebAppStarted, $"CloudExperienceHost Web App Activity started. CXID: '{longCxid}'.", lastPage: null);
Assert.NotNull(crumb);
Assert.True(crumb!.Length < 200, crumb);
Assert.EndsWith("…", crumb);
}
[Theory]
[InlineData("OobeNDUP", true)]
[InlineData("RebootNDUP", true)]
[InlineData("EarlyRebootNDUP", true)]
[InlineData("ExpeditedUpdate_startWUScanStarted", true)]
[InlineData("AADHello", false)]
[InlineData("MDMProgressAfterAADRefactored", false)]
[InlineData(null, false)]
public void UpdatePageTokens_AreRecognized(string? token, bool expected)
{
Assert.Equal(expected, ShellCoreTracker.IsUpdatePageToken(token!));
}
// ------------------------------------------------------ update page records ----
[Fact]
public void UpdatePageRecords_ArePageStartsStopsAndNames_OfTheUpdatePageOnly()
{
var start = ShellCoreTracker.ParseUpdatePageRecord(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, lastPage: null);
Assert.Equal(OobeUpdateVocabulary.PageStarted, start!.CxhEvent);
Assert.Equal("OobeNDUP", start.Page);
var stop = ShellCoreTracker.ParseUpdatePageRecord(ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, lastPage: "RebootNDUP");
Assert.Equal(OobeUpdateVocabulary.PageStopped, stop!.CxhEvent);
Assert.Equal("RebootNDUP", stop.Page);
Assert.Equal("success", stop.Result);
var name = ShellCoreTracker.ParseUpdatePageRecord(ShellCoreTracker.EventId_ShellCore_WebAppEventName, ScanStartedName, lastPage: null);
Assert.Equal(OobeUpdateVocabulary.EventName, name!.CxhEvent);
Assert.Equal("ExpeditedUpdate_startWUScanStarted", name.Name);
Assert.Null(name.Value);
// Other pages and names are no update-page records.
Assert.Null(ShellCoreTracker.ParseUpdatePageRecord(ShellCoreTracker.EventId_ShellCore_WebAppStarted, EspPageStarted, lastPage: null));
Assert.Null(ShellCoreTracker.ParseUpdatePageRecord(ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, lastPage: "MDMProgressAfterAADRefactored"));
Assert.Null(ShellCoreTracker.ParseUpdatePageRecord(ShellCoreTracker.EventId_ShellCore_WebAppEvent, NavigateWithTenantUrl, lastPage: "OobeNDUP"));
}
[Fact]
public void UpdatePageRecord_CarriesOnlyASafeValue()
{
var unsafeValue = ShellCoreTracker.ParseUpdatePageRecord(ShellCoreTracker.EventId_ShellCore_WebAppEvent, DownloadSucceededWithValue, lastPage: null);
Assert.Null(unsafeValue!.Value);
var gate = ShellCoreTracker.ParseUpdatePageRecord(ShellCoreTracker.EventId_ShellCore_WebAppEvent, CspGateTrue, lastPage: null);
Assert.Equal("true", gate!.Value);
}
[Theory]
[InlineData("true", "true")]
[InlineData("FALSE", "FALSE")]
[InlineData("0", "0")]
[InlineData("-2147024891", "-2147024891")]
[InlineData("0x80240022", "0x80240022")]
[InlineData("12345678901", null)] // more than ten digits
[InlineData("0x123456789", null)] // more than eight hex digits
[InlineData("https://login.microsoftonline.com/x", null)]
[InlineData("{\"result\":0}", null)]
[InlineData("true and more", null)]
[InlineData("", null)]
public void SafeValues_AreBooleansIntegersAndHresults(string value, string? expected)
{
var description = $"CloudExperienceHost Web App Event 2. Name: 'ExpeditedUpdate_x', Value: '{value}'.";
Assert.Equal(expected, ShellCoreTracker.ExtractSafeValue(description));
}
[Fact]
public void LiveUpdatePage_IsReported_WithTheRegistryState_AndRaisesNothing()
{
_registry = new[] { NdupKey("EnableExpeditedUpdate=1") };
using var tracker = Build();
var raised = 0;
tracker.EspExited += (_, _) => raised++;
tracker.EspFailureDetected += (_, _) => raised++;
tracker.FinalizingSetupPhaseTriggered += (_, _) => raised++;
tracker.ProcessEvent(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At, "Microsoft-Windows-Shell-Core", isBackfill: false);
tracker.ProcessEvent(ShellCoreTracker.EventId_ShellCore_WebAppEventName, ScanStartedName, At.AddMinutes(1), "Microsoft-Windows-Shell-Core", isBackfill: false);
_registry = new[] { NdupKey("EnableExpeditedUpdate=1, EnableExpeditedUpdateSyncInstallCompleted=1") };
tracker.ProcessEvent(ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, At.AddMinutes(30), "Microsoft-Windows-Shell-Core", isBackfill: false);
var pages = ByType(Constants.EventTypes.OobeUpdatePage);
Assert.Equal(new object[] { "page_started", "event_name", "page_stopped" }, pages.Select(p => Data(p)["cxhEvent"]).ToArray());
Assert.All(pages, p => Assert.Equal(false, Data(p)["backfill"]));
var states = ByType(Constants.EventTypes.OobeUpdateState);
Assert.Equal(new object[] { "update_page_started", "update_page_stopped" }, states.Select(s => Data(s)["moment"]).ToArray());
Assert.All(states, s => Assert.Equal("OobeNDUP", Data(s)["page"]));
// Telemetry only — no ESP signal, no other event.
Assert.Equal(0, raised);
Assert.False(tracker.IsEspExitedForTest);
Assert.Equal(pages.Count + states.Count, _sink.Posted.Count);
}
[Fact]
public void OtherPages_AreNotReported()
{
_registry = new[] { NdupKey("EnableExpeditedUpdate=1") };
using var tracker = Build();
tracker.ProcessEvent(ShellCoreTracker.EventId_ShellCore_WebAppStarted, EspPageStarted, At, "Microsoft-Windows-Shell-Core", isBackfill: false);
tracker.ProcessEvent(ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, At.AddMinutes(1), "Microsoft-Windows-Shell-Core", isBackfill: false);
Assert.Empty(_sink.Posted);
}
[Fact]
public void Backfill_ReportsTheUpdatePage_WithItsEventTimes_ButReadsNoRegistry()
{
// The registry shows the state now, not at the record's time.
var reads = 0;
using var tracker = new ShellCoreTracker("S1", "T1",
new InformationalEventPost(_sink, new VirtualClock(At)),
new AgentLogger(_tmp.Path, AgentLogLevel.Info),
helloTracker: null,
oobeUpdateRegistryReader: () => { reads++; return new[] { NdupKey("EnableExpeditedUpdate=1") }; });
tracker.ReplayBackfillRecords(new[]
{
(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At.AddMinutes(-4)),
(ShellCoreTracker.EventId_ShellCore_WebAppEventName, ScanStartedName, At.AddMinutes(-3)),
(ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, At.AddMinutes(-2)),
});
var pages = ByType(Constants.EventTypes.OobeUpdatePage);
Assert.Equal(3, pages.Count);
Assert.All(pages, p => Assert.Equal(true, Data(p)["backfill"]));
Assert.Equal(At.AddMinutes(-4), pages[0].OccurredAtUtc);
Assert.Equal(0, reads);
// Nothing else: no ESP exit or failure was in the window, so no skipped-records trace.
Assert.Equal(3, _sink.Posted.Count);
}
// ------------------------------------------------------------ localization ----
[Fact]
public void LocalizedRecords_AreReadFromTheirProperties()
{
using var tracker = Build();
tracker.ProcessEvent(ShellCoreTracker.EventId_ShellCore_WebAppStarted, FrenchNdupStarted, At, "Microsoft-Windows-Shell-Core",
isBackfill: false, properties: new[] { "OobeNDUP" });
tracker.ProcessEvent(ShellCoreTracker.EventId_ShellCore_WebAppEvent, FrenchCspGate, At.AddSeconds(5), "Microsoft-Windows-Shell-Core",
isBackfill: false, properties: new[] { "ExpeditedUpdate_isNDUPAllowedByCSPSucceeded", "true" });
tracker.ProcessEvent(ShellCoreTracker.EventId_ShellCore_WebAppStopped, FrenchPageStopped, At.AddMinutes(1), "Microsoft-Windows-Shell-Core",
isBackfill: false, properties: new[] { "success" });
var pages = ByType(Constants.EventTypes.OobeUpdatePage).Select(Data).ToList();
Assert.Equal(new object[] { "page_started", "event_name", "page_stopped" }, pages.Select(d => d["cxhEvent"]).ToArray());
Assert.Equal("OobeNDUP", pages[0]["page"]);
Assert.Equal("ExpeditedUpdate_isNDUPAllowedByCSPSucceeded", pages[1]["name"]);
Assert.Equal("true", pages[1]["value"]);
Assert.Equal("OobeNDUP", pages[2]["page"]);
Assert.Equal("success", pages[2]["result"]);
}
[Fact]
public void LocalizedMessage_WithoutProperties_DoesNotYieldItsLabelledFields()
{
// Why the properties are read: the French labels are not the manifest's.
Assert.Null(ShellCoreTracker.ReadCxhFields(ShellCoreTracker.EventId_ShellCore_WebAppStopped, FrenchPageStopped, properties: null).Primary);
Assert.Null(ShellCoreTracker.ReadCxhFields(ShellCoreTracker.EventId_ShellCore_WebAppEvent, FrenchCspGate, properties: null).Primary);
Assert.Equal("success",
ShellCoreTracker.ReadCxhFields(ShellCoreTracker.EventId_ShellCore_WebAppStopped, FrenchPageStopped, new[] { "success" }).Primary);
}
[Theory]
[InlineData("CloudExperienceHost Web App Event 2. Name: 'ExpeditedUpdate_x', Value: 'true'.")]
[InlineData("CloudExperienceHost Web App Event 2. Name : « ExpeditedUpdate_x », Value : « true ».")]
[InlineData("CloudExperienceHost Web App Event 2. Name\u00A0: «\u202FExpeditedUpdate_x\u202F», Value\u00A0: «\u00A0true\u00A0».")]
public void MessageFallback_AcceptsQuotesAndGuillemets(string description)
{
var fields = ShellCoreTracker.ReadCxhFields(ShellCoreTracker.EventId_ShellCore_WebAppEvent, description, properties: null);
Assert.Equal("ExpeditedUpdate_x", fields.Primary);
Assert.Equal("true", fields.Value);
}
[Fact]
public void PropertyValue_LeavesTheTrackerOnlyInASafeForm()
{
var fields = ShellCoreTracker.ReadCxhFields(ShellCoreTracker.EventId_ShellCore_WebAppEvent, "",
new[] { "ExpeditedUpdate_commitExpeditionDownloadInstallAsyncSucceeded", "{\"url\":\"https://login.microsoftonline.com/x\"}" });
var record = ShellCoreTracker.ParseUpdatePageRecord(ShellCoreTracker.EventId_ShellCore_WebAppEvent, fields, lastPage: null);
Assert.Equal("ExpeditedUpdate_commitExpeditionDownloadInstallAsyncSucceeded", record!.Name);
Assert.Null(record.Value);
Assert.DoesNotContain("microsoftonline",
ShellCoreTracker.FormatCxhBreadcrumb(ShellCoreTracker.EventId_ShellCore_WebAppEvent, fields, lastPage: null));
}
// ------------------------------------------------------- restart watermark ----
[Fact]
public void RestartBackfill_SkipsUpdatePageRecords_AnEarlierRunReported()
{
using (var first = Build(StateDirectory))
{
Live(first, ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At, recordId: 500);
Live(first, ShellCoreTracker.EventId_ShellCore_WebAppEventName, ScanStartedName, At.AddMinutes(1), recordId: 501);
}
// The restart reads the minutes before it again — plus the stop the first run missed.
using var second = Build(StateDirectory);
second.LoadPageWatermark();
second.ReplayBackfillRecords(new[]
{
new ShellCoreRecord(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At, recordId: 500),
new ShellCoreRecord(ShellCoreTracker.EventId_ShellCore_WebAppEventName, ScanStartedName, At.AddMinutes(1), recordId: 501),
new ShellCoreRecord(ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, At.AddMinutes(2), recordId: 502),
});
var pages = ByType(Constants.EventTypes.OobeUpdatePage).Select(Data).ToList();
Assert.Equal(new object[] { "page_started", "event_name", "page_stopped" }, pages.Select(d => d["cxhEvent"]).ToArray());
Assert.Equal(new object[] { false, false, true }, pages.Select(d => d["backfill"]).ToArray());
// The skipped start still names the page the stop belongs to.
Assert.Equal("OobeNDUP", pages[2]["page"]);
}
[Fact]
public void SameRecord_FromTheLiveWatcherAndTheBackfill_IsReportedOnce()
{
// The live watcher is armed before the backfill runs, so both can deliver a record.
using var tracker = Build(StateDirectory);
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At, recordId: 700);
tracker.ReplayBackfillRecords(new[]
{
new ShellCoreRecord(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At, recordId: 700),
});
Assert.Single(ByType(Constants.EventTypes.OobeUpdatePage));
}
[Fact]
public void Watermark_FollowsReportedRecordsOnly()
{
using var tracker = Build(StateDirectory);
for (var i = 0; i <= OobeUpdateTelemetry.MaxPageEventsPerKey; i++)
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppEventName, ScanStartedName, At.AddSeconds(i), recordId: 800 + i);
// The repeat over the per-name cap is claimed for this run, not written down.
Assert.Equal(OobeUpdateTelemetry.MaxPageEventsPerKey, ByType(Constants.EventTypes.OobeUpdatePage).Count);
var state = JsonConvert.DeserializeObject<EventRecordWatermark.WatermarkState>(
File.ReadAllText(Path.Combine(StateDirectory, ShellCoreTracker.PageWatermarkStateFileName)));
Assert.Equal(800 + OobeUpdateTelemetry.MaxPageEventsPerKey - 1, state!.LastRecordId);
}
[Fact]
public void RecordsWithoutARecordId_AreNotDeduplicated()
{
using var tracker = Build(StateDirectory);
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At, recordId: -1);
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At.AddMinutes(5), recordId: -1);
Assert.Equal(2, ByType(Constants.EventTypes.OobeUpdatePage).Count);
Assert.False(File.Exists(Path.Combine(StateDirectory, ShellCoreTracker.PageWatermarkStateFileName)));
}
[Fact]
public void AgentStartAndStop_ReportTheRegistryState_StopOnce()
{
_registry = new[] { NdupKey("EnableExpeditedUpdate=1") };
var tracker = Build();
tracker.Start();
_registry = new[] { NdupKey("EnableExpeditedUpdate=1, ExpeditedUpdateStatus=2") };
tracker.Stop();
tracker.Stop();
tracker.Dispose();
var states = ByType(Constants.EventTypes.OobeUpdateState);
Assert.Equal(new object[] { "agent_start", "agent_stop" }, states.Select(s => Data(s)["moment"]).ToArray());
}
[Fact]
public void TrackerThatNeverStarted_ReportsNothingAtStop()
{
_registry = new[] { NdupKey("EnableExpeditedUpdate=1") };
var tracker = Build();
tracker.Dispose();
Assert.Empty(_sink.Posted);
}
}
}
@@ -84,6 +84,55 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring
Assert.False(raised);
}
[Fact]
public void ProcessEvent_62404_NGC_in_a_localized_message_is_read_from_the_cxid_property()
{
// A French Windows quotes the CXID in guillemets — the "'NGC'" substring never matched.
using var tmp = new TempDirectory();
using var tracker = MakeTracker(tmp, new VirtualClock(ClockNow));
var raised = 0;
tracker.HelloWizardStarted += (_, _) => raised++;
tracker.ProcessEvent(
eventId: ShellCoreTracker.EventId_ShellCore_WebAppStarted,
description: "Activité d’application web CloudExperienceHost démarrée. CXID : « NGC ».",
timestamp: ClockNow,
providerName: "Microsoft-Windows-Shell-Core",
isBackfill: false,
properties: new[] { "NGC" });
Assert.Equal(1, raised);
}
[Theory]
[InlineData("AADHello", true)]
[InlineData("NGC", true)]
[InlineData("NGCPin", false)]
[InlineData("OobeNDUP", false)]
public void HelloWizardStart_is_decided_by_the_cxid(string cxid, bool expected)
{
Assert.Equal(expected, ShellCoreTracker.IsHelloWizardStart(cxid, description: "CXID: 'AADHello'"));
}
[Fact]
public void Backfill_62404_reads_the_cxid_property()
{
using var tmp = new TempDirectory();
using var tracker = MakeTracker(tmp, new VirtualClock(ClockNow));
var captured = new List<HelloWizardStartedEventArgs>();
tracker.HelloWizardStarted += (_, args) => captured.Add(args);
var historical = ClockNow.AddMinutes(-3);
tracker.ReplayBackfillRecords(new[]
{
new ShellCoreRecord(ShellCoreTracker.EventId_ShellCore_WebAppStarted,
"Activité d’application web CloudExperienceHost démarrée. CXID : « NGC ».", historical,
recordId: 42, properties: new[] { "NGC" }),
});
Assert.Equal(historical, Assert.Single(captured).OccurredAtUtc);
}
[Fact]
public void Backfill_62404_raises_once_with_historical_timestamp()
{
@@ -0,0 +1,225 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
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.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
using AutopilotMonitor.DecisionCore.Engine;
using AutopilotMonitor.DecisionCore.Signals;
using AutopilotMonitor.Shared;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring
{
/// <summary>
/// The first run's update page backfill (<see cref="ShellCoreTracker.BackfillUpdatePageTelemetry"/>)
/// and the page a stop belongs to across backfill and live records. The first run has no
/// replay — its backfill is telemetry and nothing else, so a Hello-wizard start, an ESP exit
/// or an ESP failure in the window must not leave the tracker on any rail.
/// </summary>
public sealed class ShellCoreTrackerUpdatePageBackfillTests : IDisposable
{
private static readonly DateTime At = new DateTime(2026, 10, 5, 13, 10, 0, DateTimeKind.Utc);
private const string NdupStarted = "CloudExperienceHost Web App Activity started. CXID: 'OobeNDUP'.";
private const string RebootNdupStarted = "CloudExperienceHost Web App Activity started. CXID: 'RebootNDUP'.";
private const string AadHelloStarted = "CloudExperienceHost Web App Activity started. CXID: 'AADHello'.";
private const string PageStopped = "CloudExperienceHost Web App Activity stopped. Result: 'success'.";
private const string DownloadSucceeded =
"CloudExperienceHost Web App Event 1. Name: 'SdxWebAppCloudNDUP_processStatusChangeFromHandler_downloadSucceeded'.";
private const string EspExit = "CommercialOOBE_ESPProgress_Page_Exiting";
private const string EspFailure = "CommercialOOBE_ESPProgress_Failure";
private const string WhiteGloveSuccess = "CommercialOOBE_ESPProgress_WhiteGlove_Success";
private readonly TempDirectory _tmp = new TempDirectory();
private readonly FakeSignalIngressSink _sink = new FakeSignalIngressSink();
private int _registryReads;
public void Dispose() => _tmp.Dispose();
private ShellCoreTracker Build() =>
new ShellCoreTracker("S1", "T1",
new InformationalEventPost(_sink, new VirtualClock(At)),
new AgentLogger(_tmp.Path, AgentLogLevel.Info),
helloTracker: null,
oobeUpdateRegistryReader: () => { _registryReads++; return Array.Empty<OobeUpdateRegistrySnapshot.KeyState>(); },
stateDirectory: Path.Combine(_tmp.Path, "state"));
private static void Live(ShellCoreTracker tracker, int eventId, string description, DateTime at, long recordId) =>
tracker.ProcessEvent(eventId, description, at, "Microsoft-Windows-Shell-Core", isBackfill: false, recordId: recordId);
private static ShellCoreRecord Record(int eventId, string description, DateTime at, long recordId) =>
new ShellCoreRecord(eventId, description, at, recordId);
private IReadOnlyList<IReadOnlyDictionary<string, object>> Pages() =>
_sink.Posted
.Where(p => p.Kind == DecisionSignalKind.InformationalEvent
&& p.Payload != null
&& p.Payload.TryGetValue(SignalPayloadKeys.EventType, out var et)
&& et == Constants.EventTypes.OobeUpdatePage)
.Select(p => (IReadOnlyDictionary<string, object>)p.TypedPayload!)
.ToList();
// ------------------------------------------------------------ first run ----
[Fact]
public void FirstRunBackfill_ReportsTheUpdatePage_AndNothingElse()
{
using var tracker = Build();
var raised = new List<string>();
tracker.HelloWizardStarted += (_, _) => raised.Add("hello");
tracker.FinalizingSetupPhaseTriggered += (_, reason) => raised.Add($"finalizing:{reason}");
tracker.EspExited += (_, _) => raised.Add("exit");
tracker.EspFailureDetected += (_, _) => raised.Add("failure");
tracker.WhiteGloveCompleted += (_, _) => raised.Add("whiteglove");
tracker.ReplayUpdatePageTelemetry(new[]
{
Record(ShellCoreTracker.EventId_ShellCore_WebAppEvent, EspExit, At.AddMinutes(-41), 95),
Record(ShellCoreTracker.EventId_ShellCore_WebAppEvent, EspFailure, At.AddSeconds(-2430), 96),
Record(ShellCoreTracker.EventId_ShellCore_WebAppEvent, WhiteGloveSuccess, At.AddSeconds(-2420), 97),
Record(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At.AddMinutes(-40), 100),
Record(ShellCoreTracker.EventId_ShellCore_WebAppEventName, DownloadSucceeded, At.AddMinutes(-30), 101),
Record(ShellCoreTracker.EventId_ShellCore_WebAppStarted, AadHelloStarted, At.AddMinutes(-2), 110),
});
Assert.Empty(raised);
Assert.False(tracker.IsEspExitedForTest);
Assert.False(tracker.IsWhiteGloveDetectedForTest);
var pages = Pages();
Assert.Equal(new object[] { "page_started", "event_name" }, pages.Select(d => d["cxhEvent"]).ToArray());
Assert.All(pages, d => Assert.Equal(true, d["backfill"]));
// No agent_trace, no ESP or Hello event — only the two page rows.
Assert.Equal(2, _sink.Posted.Count);
// The registry shows the state now, not at the records' time.
Assert.Equal(0, _registryReads);
}
[Fact]
public void FirstRunBackfill_KeepsTheEventTimes()
{
using var tracker = Build();
tracker.ReplayUpdatePageTelemetry(new[]
{
Record(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At.AddMinutes(-40), 100),
});
var posted = Assert.Single(_sink.Posted);
Assert.Equal(At.AddMinutes(-40), posted.OccurredAtUtc);
}
[Fact]
public void LiveStop_FindsThePageThatStartedBeforeTheAgent()
{
using var tracker = Build();
tracker.ReplayUpdatePageTelemetry(new[]
{
Record(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At.AddMinutes(-40), 100),
});
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, At.AddMinutes(5), 120);
var stop = Pages().Last();
Assert.Equal("page_stopped", stop["cxhEvent"]);
Assert.Equal("OobeNDUP", stop["page"]);
Assert.Equal(false, stop["backfill"]);
}
[Fact]
public void SameRecord_FromTheLiveWatcherAndTheFirstRunBackfill_IsReportedOnce()
{
// The live watcher is armed before the backfill reads, so both can deliver a record.
using var tracker = Build();
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At, 700);
tracker.ReplayUpdatePageTelemetry(new[]
{
Record(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At, 700),
});
Assert.Single(Pages(), d => (string)d["cxhEvent"] == "page_started");
}
// ---------------------------------------------------- the page of a stop ----
[Fact]
public void ReplayedOlderStart_DoesNotTakeOverTheNextLiveStop()
{
// A restart replay reads records older than the live watcher's; before, its page start
// overwrote the live page and the next live stop was attributed to the old page.
using var tracker = Build();
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStarted, RebootNdupStarted, At, 900);
tracker.ReplayBackfillRecords(new[]
{
Record(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At.AddMinutes(-20), 850),
Record(ShellCoreTracker.EventId_ShellCore_WebAppEventName, DownloadSucceeded, At.AddMinutes(-19), 851),
});
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, At.AddMinutes(1), 901);
var stop = Pages().Last();
Assert.Equal("page_stopped", stop["cxhEvent"]);
Assert.Equal("RebootNDUP", stop["page"]);
}
[Fact]
public void BackfilledStop_BelongsToTheStartOfItsOwnBatch()
{
using var tracker = Build();
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStarted, RebootNdupStarted, At, 900);
tracker.ReplayUpdatePageTelemetry(new[]
{
Record(ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At.AddMinutes(-20), 850),
Record(ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, At.AddMinutes(-18), 852),
});
var stop = Pages().Last();
Assert.Equal("page_stopped", stop["cxhEvent"]);
Assert.Equal("OobeNDUP", stop["page"]);
Assert.Equal(true, stop["backfill"]);
}
[Fact]
public void BackfilledStopWithoutItsStart_IsNotGivenTheLivePage()
{
using var tracker = Build();
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At, 900);
tracker.ReplayUpdatePageTelemetry(new[]
{
Record(ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, At.AddMinutes(-18), 852),
});
Assert.DoesNotContain(Pages(), d => (string)d["cxhEvent"] == "page_stopped");
}
[Fact]
public void NewerBackfilledStart_MovesTheLivePageForward()
{
// Records written between arming the watcher and the backfill's read are newer than
// the last live start the watcher delivered before them.
using var tracker = Build();
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStarted, NdupStarted, At, 900);
tracker.ReplayUpdatePageTelemetry(new[]
{
Record(ShellCoreTracker.EventId_ShellCore_WebAppStarted, RebootNdupStarted, At.AddSeconds(30), 905),
});
Live(tracker, ShellCoreTracker.EventId_ShellCore_WebAppStopped, PageStopped, At.AddMinutes(1), 906);
Assert.Equal("RebootNDUP", Pages().Last()["page"]);
}
[Fact]
public void UpdatePageLookback_IsInsideTheReplayClamp()
{
Assert.Equal(ShellCoreTracker.UpdatePageBackfillLookbackMinutes,
ShellCoreTracker.ClampLookbackMinutes(ShellCoreTracker.UpdatePageBackfillLookbackMinutes));
}
}
}
@@ -0,0 +1,411 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime;
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 AutopilotMonitor.DecisionCore.Engine;
using Xunit;
using SharedEventTypes = AutopilotMonitor.Shared.Constants.EventTypes;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
{
/// <summary>
/// D-316: IME saves every platform script's result under Policies\&lt;user&gt;\&lt;policy&gt; after reporting the
/// batch; the observer checks the tracker's emitted run against it. The field case (session 756970cf): the run
/// had been given a Win32 detection script's end block — exit 1 and "Detection Started" output — while IME saved
/// the bootstrap output of the platform script itself.
/// </summary>
public sealed class ImeRegistryScriptResultObserverTests
{
private const string DeviceUser = "00000000-0000-0000-0000-000000000000";
private const string PolicyA = "1d6f0a52-3c4e-4b7a-9e21-5f8c7d2b9a10";
private const string PolicyB = "2b7d9e41-8c3a-4f60-a5d2-9e1b7c4f3a08";
private const string OwnStdout = "=== bootstrap loader started ===\nInstaller finished (exit code 0).";
private const string ForeignStdout = "VERBOSE: === Sample Suite Detection Started ===\nWARNING: === DETECTION FAILED ===";
private static readonly DateTime T0 = new DateTime(2026, 10, 3, 20, 59, 0, DateTimeKind.Utc);
// ── PlatformOutputJudge ─────────────────────────────────────────────────
private static PlatformScriptRun Run(string? stdout, string? stderr = null, int? exitCode = 0, string result = "Success", string resultSource = "ime_policy_result")
=> new PlatformScriptRun { PolicyId = PolicyA, UserId = DeviceUser, RunId = "run-1", Emitted = true, Result = result, ResultSource = resultSource, ExitCode = exitCode, Stdout = stdout, Stderr = stderr };
private static string Details(string executionMessage)
=> "{\"Version\":1,\"SigningCode\":15,\"EncryptionCode\":19,\"ExecutionMsg\":" + System.Text.Json.JsonSerializer.Serialize(executionMessage) + "}";
private static SavedScriptResult Saved(string? executionMessage, string result = "Success", int errorCode = 0, string user = DeviceUser, string policy = PolicyA, string stamp = "10/3/2026 9:00:00 PM")
=> new SavedScriptResult
{
UserId = user,
PolicyId = policy,
Result = result,
ErrorCode = errorCode,
ResultDetails = executionMessage == null ? null : Details(executionMessage),
LastUpdatedTimeUtc = stamp,
};
[Fact]
public void Field_case_detection_output_on_the_platform_run_is_foreign_and_the_saved_stdout_replaces_it()
{
var judgement = PlatformOutputJudge.Judge(Run(ForeignStdout, string.Empty, exitCode: 1), Saved(OwnStdout.Replace("\n", "\r\n")));
Assert.Equal(PlatformOutputVerdict.Foreign, judgement.Verdict);
Assert.Equal("stdout_differs", judgement.Reason);
Assert.Equal(OwnStdout, judgement.Stdout);
Assert.Equal(string.Empty, judgement.Stderr);
}
[Fact]
public void Matching_output_is_verified_across_line_ending_and_trim_differences()
{
var judgement = PlatformOutputJudge.Judge(Run(OwnStdout, string.Empty), Saved(" " + OwnStdout.Replace("\n", "\r\n") + "\r\n"));
Assert.Equal(PlatformOutputVerdict.Verified, judgement.Verdict);
}
[Fact]
public void Output_cut_by_the_tracker_is_verified_against_the_longer_saved_text()
{
var full = string.Concat(Enumerable.Repeat("line of output\n", 1000)).TrimEnd();
var emitted = ImeLogTracker.TruncateOutput(full);
Assert.EndsWith(ImeLogTracker.OutputTruncationMarker, emitted);
Assert.Equal(PlatformOutputVerdict.Verified, PlatformOutputJudge.Judge(Run(emitted), Saved(full)).Verdict);
}
[Fact]
public void Both_empty_is_verified_but_stderr_on_a_success_is_another_executors()
{
Assert.Equal(PlatformOutputVerdict.Verified, PlatformOutputJudge.Judge(Run(string.Empty), Saved(string.Empty)).Verdict);
var judgement = PlatformOutputJudge.Judge(Run(string.Empty, "Get-Item : not found"), Saved(string.Empty));
Assert.Equal(PlatformOutputVerdict.Foreign, judgement.Verdict);
Assert.Equal("stderr_on_success", judgement.Reason);
}
[Fact]
public void Explicitly_empty_emitted_output_against_saved_text_is_foreign()
{
Assert.Equal(PlatformOutputVerdict.Foreign, PlatformOutputJudge.Judge(Run(string.Empty), Saved(OwnStdout)).Verdict);
Assert.Equal(PlatformOutputVerdict.Foreign, PlatformOutputJudge.Judge(Run(OwnStdout), Saved(string.Empty)).Verdict);
}
[Fact]
public void Output_never_read_is_completed_from_the_saved_text_and_the_exit_code_stands()
{
var judgement = PlatformOutputJudge.Judge(Run(null), Saved(OwnStdout));
Assert.Equal(PlatformOutputVerdict.Repaired, judgement.Verdict);
Assert.Equal("stdout_missing", judgement.Reason);
Assert.Equal(OwnStdout, judgement.Stdout);
Assert.Equal(PlatformOutputVerdict.NotComparable, PlatformOutputJudge.Judge(Run(null), Saved(string.Empty)).Verdict);
}
[Fact]
public void Stdout_containing_the_output_error_separator_is_repaired_not_foreign()
{
// "write output done. output = X, error = " with X = "Copying files, error = 0 errors\nDone": the
// lazy split leaves "Copying files" as stdout and the rest as stderr.
var judgement = PlatformOutputJudge.Judge(
Run("Copying files", "0 errors\nDone, error ="),
Saved("Copying files, error = 0 errors\r\nDone"));
Assert.Equal(PlatformOutputVerdict.Repaired, judgement.Verdict);
Assert.Equal("stdout_split", judgement.Reason);
Assert.Equal("Copying files, error = 0 errors\nDone", judgement.Stdout);
Assert.Equal(string.Empty, judgement.Stderr);
}
[Fact]
public void A_prefix_without_the_split_explanation_is_foreign()
{
Assert.Equal(PlatformOutputVerdict.Foreign, PlatformOutputJudge.Judge(Run("Detected"), Saved("Detected and repaired")).Verdict);
}
[Fact]
public void Failed_with_stderr_is_judged_on_stderr()
{
Assert.Equal(PlatformOutputVerdict.Verified,
PlatformOutputJudge.Judge(Run("partial", "Access denied", 1, "Failed"), Saved("Access denied\r\n", "Failed", 99)).Verdict);
var missing = PlatformOutputJudge.Judge(Run(null, null, 1, "Failed"), Saved("Access denied", "Failed", 99));
Assert.Equal(PlatformOutputVerdict.Repaired, missing.Verdict);
Assert.Null(missing.Stdout);
Assert.Equal("Access denied", missing.Stderr);
var foreign = PlatformOutputJudge.Judge(Run("Detected", string.Empty, 0, "Failed"), Saved("Access denied", "Failed", 99));
Assert.Equal(PlatformOutputVerdict.Foreign, foreign.Verdict);
Assert.Equal(string.Empty, foreign.Stdout);
Assert.Equal("Access denied", foreign.Stderr);
}
[Fact]
public void Results_without_saved_output_or_from_another_run_are_not_compared()
{
Assert.Equal("no_saved_output", PlatformOutputJudge.Judge(Run(OwnStdout, null, 1, "Failed"), Saved("1", "Failed", 1)).Reason);
var hashMismatch = new SavedScriptResult { UserId = DeviceUser, PolicyId = PolicyA, Result = "Failed", ErrorCode = 2, ResultDetails = "Hash mismatch" };
Assert.Equal("no_execution_message", PlatformOutputJudge.Judge(Run(OwnStdout, null, 1, "Failed"), hashMismatch).Reason);
Assert.Equal("result_differs", PlatformOutputJudge.Judge(Run(OwnStdout), Saved("Access denied", "Failed", 99)).Reason);
// A result derived from the exit code (fallback) is not IME's — the saved one is.
Assert.Equal(PlatformOutputVerdict.Verified,
PlatformOutputJudge.Judge(Run(OwnStdout, null, 1, "Failed", "agentexecutor_fallback"), Saved(OwnStdout)).Verdict);
}
// ── PlatformScriptRunRegister ───────────────────────────────────────────
private static ScriptExecutionState Emitted(string policyId, string? stdout, int? exitCode = 0, string? userId = DeviceUser, string runId = "run-1")
=> new ScriptExecutionState { PolicyId = policyId, ScriptType = "platform", UserId = userId, RunId = runId, Result = "Success", ResultSource = "ime_policy_result", ExitCode = exitCode, Stdout = stdout };
[Fact]
public void A_save_is_paired_only_after_the_tracker_read_past_it_and_the_run_was_emitted()
{
var runs = new PlatformScriptRunRegister();
runs.NoteStarted(DeviceUser, PolicyA, T0);
var seen = T0.AddSeconds(30);
Assert.Equal(PlatformRunClaim.Wait, runs.TryClaim(DeviceUser, PolicyA, seen, out _));
runs.NotePassCompleted(seen.AddSeconds(1));
Assert.Equal(PlatformRunClaim.Wait, runs.TryClaim(DeviceUser, PolicyA, seen, out _)); // pass started too early
runs.NotePassCompleted(seen + PlatformScriptRunRegister.TrackerCatchUp);
Assert.Equal(PlatformRunClaim.Wait, runs.TryClaim(DeviceUser, PolicyA, seen, out _)); // run not emitted yet
runs.NoteEmitted(Emitted(PolicyA, OwnStdout), T0.AddSeconds(40));
Assert.Equal(PlatformRunClaim.Claimed, runs.TryClaim(DeviceUser, PolicyA, seen, out var run));
Assert.Equal("run-1", run!.RunId);
Assert.Equal(OwnStdout, run.Stdout);
Assert.Equal(PlatformRunClaim.None, runs.TryClaim(DeviceUser, PolicyA, seen, out _)); // never twice
}
[Fact]
public void A_new_start_replaces_the_latest_run_of_its_key_and_other_users_stay_apart()
{
var runs = new PlatformScriptRunRegister();
runs.NoteStarted(DeviceUser, PolicyA, T0);
runs.NoteEmitted(Emitted(PolicyA, "first run"), T0.AddSeconds(5));
runs.NoteStarted(DeviceUser, PolicyA, T0.AddMinutes(30));
runs.NoteEmitted(Emitted(PolicyA, "second run", runId: "run-2"), T0.AddMinutes(30).AddSeconds(5));
const string user = "6c3f8a20-1d5e-4b97-8f4a-0e2d6b9c7a13";
runs.NoteStarted(user, PolicyA, T0.AddMinutes(31));
runs.NotePassCompleted(T0.AddHours(1));
Assert.Equal(PlatformRunClaim.Claimed, runs.TryClaim(DeviceUser, PolicyA, T0.AddMinutes(31), out var run));
Assert.Equal("second run", run!.Stdout);
Assert.Equal(PlatformRunClaim.Wait, runs.TryClaim(user, PolicyA, T0.AddMinutes(32), out _));
Assert.Equal(PlatformRunClaim.None, runs.TryClaim(DeviceUser, PolicyB, T0.AddMinutes(32), out _));
}
[Fact]
public void A_run_without_a_readable_user_id_is_claimed_by_its_policy()
{
var runs = new PlatformScriptRunRegister();
runs.NoteStarted(null, PolicyA, T0);
runs.NoteEmitted(Emitted(PolicyA, OwnStdout, userId: null), T0.AddSeconds(5));
runs.NotePassCompleted(T0.AddMinutes(5));
Assert.Equal(PlatformRunClaim.Claimed, runs.TryClaim(DeviceUser, PolicyA, T0.AddMinutes(1), out _));
}
[Fact]
public void Register_keeps_the_newest_runs_and_its_counters_report_each_change_once()
{
var runs = new PlatformScriptRunRegister();
for (var i = 0; i <= PlatformScriptRunRegister.MaxRuns; i++)
runs.NoteStarted(DeviceUser, Guid.NewGuid().ToString("D"), T0.AddSeconds(i));
runs.NoteStarted(DeviceUser, PolicyA, T0.AddHours(1));
runs.NoteEmitted(Emitted(PolicyA, OwnStdout), T0.AddHours(1));
runs.NotePassCompleted(T0.AddHours(2));
Assert.Equal(PlatformRunClaim.Claimed, runs.TryClaim(DeviceUser, PolicyA, T0.AddHours(1), out _));
Assert.False(runs.ConsumeCountersChanged());
runs.CountVerified();
runs.CountCorrected();
Assert.True(runs.ConsumeCountersChanged());
Assert.False(runs.ConsumeCountersChanged());
Assert.Equal(1, runs.Verified);
runs.RestoreCounters(7, 3);
Assert.Equal(7, runs.Verified);
Assert.Equal(3, runs.Corrected);
}
// ── observer flow ───────────────────────────────────────────────────────
private sealed class Fixture
{
public readonly FakeSignalIngressSink Sink = new FakeSignalIngressSink();
public readonly VirtualClock Clock = new VirtualClock(T0);
public readonly PlatformScriptRunRegister Runs = new PlatformScriptRunRegister();
public Dictionary<string, SavedScriptResult> Registry = new Dictionary<string, SavedScriptResult>(StringComparer.OrdinalIgnoreCase);
public readonly ImeRegistryScriptResultObserver Observer;
public Fixture()
{
Observer = new ImeRegistryScriptResultObserver(Runs, new InformationalEventPost(Sink, Clock), null, Clock,
() => new Dictionary<string, SavedScriptResult>(Registry, StringComparer.OrdinalIgnoreCase));
}
public void Save(SavedScriptResult saved) => Registry[ImeRegistryScriptResultObserver.KeyOf(saved.UserId, saved.PolicyId)] = saved;
/// <summary>The tracker emitted a run, then kept polling past the save.</summary>
public void EmitRun(string policyId, string? stdout, int? exitCode, string runId)
{
Runs.NoteStarted(DeviceUser, policyId, Clock.UtcNow);
Runs.NoteEmitted(Emitted(policyId, stdout, exitCode, runId: runId), Clock.UtcNow);
}
public void TrackerCaughtUp() => Runs.NotePassCompleted(Clock.UtcNow);
public IReadOnlyList<FakeSignalIngressSink.PostedSignal> Corrections()
=> Sink.Posted.Where(p => p.Payload != null
&& p.Payload.TryGetValue(SignalPayloadKeys.EventType, out var et)
&& et == SharedEventTypes.ScriptOutputReconciliation).ToList();
}
[Fact]
public void Field_case_end_to_end_the_save_corrects_the_run_with_a_warning_naming_it()
{
var f = new Fixture();
f.Save(Saved("an older run of another policy", policy: PolicyB, stamp: "10/2/2026 8:00:00 AM"));
f.Observer.Tick("baseline");
f.EmitRun(PolicyA, ForeignStdout, 1, "run-a");
f.Save(Saved(OwnStdout));
f.Clock.Advance(TimeSpan.FromSeconds(1));
f.Observer.Tick("registry_change");
Assert.Empty(f.Corrections()); // the tracker has not read past the save yet
f.Clock.Advance(PlatformScriptRunRegister.TrackerCatchUp);
f.TrackerCaughtUp();
f.Observer.Tick("periodic");
var correction = Assert.Single(f.Corrections());
var data = correction.Payload!;
Assert.Equal(PolicyA, data["policyId"]);
Assert.Equal("platform", data["scriptType"]);
Assert.Equal("run-a", data["runId"]);
Assert.Equal("foreign", data["outcome"]);
Assert.Equal("stdout_differs", data["reason"]);
Assert.Equal("Success", data["result"]);
Assert.Equal(OwnStdout, data["stdout"]);
Assert.Equal(string.Empty, data["stderr"]);
Assert.Equal("1", data["previousExitCode"]);
Assert.Equal("0", data["registryErrorCode"]);
Assert.Equal("Warning", data[SignalPayloadKeys.Severity]);
Assert.Equal("true", data[SignalPayloadKeys.ImmediateUpload]);
Assert.DoesNotContain("exitCode", data.Keys);
Assert.Equal(1, f.Runs.Corrected);
Assert.Equal(0, f.Runs.Verified);
// The same save read again changes nothing.
f.Clock.Advance(TimeSpan.FromMinutes(1));
f.TrackerCaughtUp();
f.Observer.Tick("periodic");
Assert.Single(f.Corrections());
}
[Fact]
public void Confirmed_output_is_counted_and_emits_nothing()
{
var f = new Fixture();
f.Observer.Tick("baseline");
f.EmitRun(PolicyA, OwnStdout, 0, "run-a");
f.Save(Saved(OwnStdout));
f.Clock.Advance(TimeSpan.FromSeconds(1));
f.Observer.Tick("registry_change");
f.Clock.Advance(TimeSpan.FromSeconds(3));
f.TrackerCaughtUp();
f.Observer.Tick("periodic");
Assert.Empty(f.Corrections());
Assert.Equal(1, f.Runs.Verified);
}
[Fact]
public void Completed_output_is_an_info_event_that_keeps_the_exit_code()
{
var f = new Fixture();
f.Observer.Tick("baseline");
f.EmitRun(PolicyA, null, 0, "run-a");
f.Save(Saved(OwnStdout));
f.Clock.Advance(TimeSpan.FromSeconds(1));
f.Observer.Tick("registry_change");
f.Clock.Advance(TimeSpan.FromSeconds(3));
f.TrackerCaughtUp();
f.Observer.Tick("periodic");
var data = Assert.Single(f.Corrections()).Payload!;
Assert.Equal("repaired", data["outcome"]);
Assert.Equal("Info", data[SignalPayloadKeys.Severity]);
Assert.DoesNotContain("previousExitCode", data.Keys);
}
[Fact]
public void A_save_seen_before_its_run_was_emitted_waits_for_the_emit()
{
var f = new Fixture();
f.Observer.Tick("baseline");
f.Runs.NoteStarted(DeviceUser, PolicyA, f.Clock.UtcNow); // result held for its end block
f.Save(Saved(OwnStdout));
f.Observer.Tick("registry_change");
f.Clock.Advance(TimeSpan.FromSeconds(3));
f.TrackerCaughtUp();
f.Observer.Tick("periodic");
Assert.Equal(0, f.Runs.Verified + f.Runs.Corrected);
f.Runs.NoteEmitted(Emitted(PolicyA, ForeignStdout, 1, runId: "run-a"), f.Clock.UtcNow);
f.Clock.Advance(TimeSpan.FromSeconds(60));
f.Observer.Tick("periodic");
Assert.Equal("foreign", Assert.Single(f.Corrections()).Payload!["outcome"]);
}
[Fact]
public void A_save_no_run_claims_within_the_expiry_is_dropped()
{
var f = new Fixture();
f.Observer.Tick("baseline");
f.Save(Saved(OwnStdout));
f.Observer.Tick("registry_change");
f.Clock.Advance(ImeRegistryScriptResultObserver.PendingExpiry);
f.TrackerCaughtUp();
f.Runs.NoteStarted(DeviceUser, PolicyA, f.Clock.UtcNow);
f.Observer.Tick("periodic"); // waits for the emit — and expires
f.Runs.NoteEmitted(Emitted(PolicyA, ForeignStdout, 1, runId: "run-a"), f.Clock.UtcNow);
f.Clock.Advance(TimeSpan.FromSeconds(60));
f.TrackerCaughtUp();
f.Observer.Tick("periodic");
Assert.Empty(f.Corrections());
}
[Fact]
public void Saves_from_before_the_agent_started_are_the_silent_baseline()
{
var f = new Fixture();
f.EmitRun(PolicyA, ForeignStdout, 1, "run-a");
f.Save(Saved(OwnStdout));
f.TrackerCaughtUp();
f.Observer.Tick("baseline");
f.Clock.Advance(TimeSpan.FromMinutes(1));
f.TrackerCaughtUp();
f.Observer.Tick("periodic");
Assert.Empty(f.Corrections());
Assert.Equal(0, f.Runs.Verified + f.Runs.Corrected);
}
[Fact]
public void Corrections_beyond_the_cap_are_counted_but_not_emitted()
{
var f = new Fixture();
f.Observer.Tick("baseline");
var policies = Enumerable.Range(0, ImeRegistryScriptResultObserver.MaxCorrectionsPerSession + 3).Select(_ => Guid.NewGuid().ToString("D")).ToList();
foreach (var policy in policies)
{
f.EmitRun(policy, ForeignStdout, 1, "run-" + policy);
f.Save(Saved(OwnStdout, policy: policy));
}
f.Observer.Tick("registry_change");
f.Clock.Advance(TimeSpan.FromSeconds(3));
f.TrackerCaughtUp();
f.Observer.Tick("periodic");
Assert.Equal(ImeRegistryScriptResultObserver.MaxCorrectionsPerSession, f.Corrections().Count);
Assert.Equal(policies.Count, f.Runs.Corrected);
}
}
}
@@ -64,7 +64,6 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
private void Buffer(string? rebootUri, long? recordId = null, bool isBackfill = false, DateTime? timeCreatedUtc = null, string? description = null)
{
_tracker.ProcessEvent(
eventId: MdmRebootPolicyTracker.EventId_PolicyRebootRequired,
recordId: recordId ?? _nextRecordId++,
timeCreatedUtc: timeCreatedUtc ?? At,
rebootUri: rebootUri,
@@ -226,13 +225,13 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
// The coalesced reboot restarts the agent; the post-reboot backfill re-reads the same
// records — a FLUSHED record must be skipped by the next instance.
var first = MakeTracker(stateDirectory: _tmp.Path);
first.ProcessEvent(MdmRebootPolicyTracker.EventId_PolicyRebootRequired, recordId: 500,
first.ProcessEvent(recordId: 500,
timeCreatedUtc: At, rebootUri: UriSvchost, formattedDescription: null, isBackfill: false);
first.FlushPending();
var second = MakeTracker(stateDirectory: _tmp.Path);
second.LoadWatermark();
second.ProcessEvent(MdmRebootPolicyTracker.EventId_PolicyRebootRequired, recordId: 500,
second.ProcessEvent(recordId: 500,
timeCreatedUtc: At, rebootUri: UriSvchost, formattedDescription: null, isBackfill: true);
second.FlushPending();
@@ -245,13 +244,13 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
// Process killed by the very reboot the records announce: nothing was flushed, so the
// next instance must re-read and emit the records.
var first = MakeTracker(stateDirectory: _tmp.Path);
first.ProcessEvent(MdmRebootPolicyTracker.EventId_PolicyRebootRequired, recordId: 500,
first.ProcessEvent(recordId: 500,
timeCreatedUtc: At, rebootUri: UriSvchost, formattedDescription: null, isBackfill: false);
// no FlushPending — simulated reboot kill
var second = MakeTracker(stateDirectory: _tmp.Path);
second.LoadWatermark();
second.ProcessEvent(MdmRebootPolicyTracker.EventId_PolicyRebootRequired, recordId: 500,
second.ProcessEvent(recordId: 500,
timeCreatedUtc: At, rebootUri: UriSvchost, formattedDescription: null, isBackfill: true);
second.FlushPending();
@@ -0,0 +1,128 @@
#nullable enable
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.Tests.Harness;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
{
/// <summary>
/// The OOBE update registry snapshot (D-310, S4/S5): one readable, bounded line per key, for
/// agent.log and the <c>oobe_update_state</c> event. The registry read itself is a thin wrapper.
/// </summary>
public sealed class OobeUpdateRegistrySnapshotTests
{
private static KeyValuePair<string, object> Entry(string name, object value) => new KeyValuePair<string, object>(name, value);
[Fact]
public void Values_AreFormattedByType()
{
Assert.Equal("1", OobeUpdateRegistrySnapshot.FormatValue(1));
Assert.Equal("5000000000", OobeUpdateRegistrySnapshot.FormatValue(5000000000L));
Assert.Equal("Succeeded", OobeUpdateRegistrySnapshot.FormatValue("Succeeded"));
Assert.Equal("a|b", OobeUpdateRegistrySnapshot.FormatValue(new[] { "a", "b" }));
Assert.Equal("(binary 3 bytes)", OobeUpdateRegistrySnapshot.FormatValue(new byte[] { 1, 2, 3 }));
Assert.Equal("(null)", OobeUpdateRegistrySnapshot.FormatValue(null!));
}
[Fact]
public void LongValues_AreTruncated()
{
var formatted = OobeUpdateRegistrySnapshot.FormatValue(new string('x', 500));
Assert.Equal(OobeUpdateRegistrySnapshot.MaxValueLength + 1, formatted.Length);
Assert.EndsWith("…", formatted);
}
[Fact]
public void Entries_AreOrderedByName()
{
var line = OobeUpdateRegistrySnapshot.FormatEntries(new[]
{
Entry("ExpeditedUpdateStatus", 2),
Entry("EnableExpeditedUpdate", 1),
Entry("", "default"),
}, oobeOnly: false);
Assert.Equal("(default)=default, EnableExpeditedUpdate=1, ExpeditedUpdateStatus=2", line);
}
[Fact]
public void PolicyManagerSystemKey_KeepsOnlyOobeValues()
{
// That key holds every System policy of the device; only the OOBE ones matter here.
var line = OobeUpdateRegistrySnapshot.FormatEntries(new[]
{
Entry("AllowTelemetry", 1),
Entry("AllowOOBEUpdates", 1),
Entry("AllowOOBEUpdates_ProviderSet", 1),
}, oobeOnly: true);
Assert.Equal("AllowOOBEUpdates=1, AllowOOBEUpdates_ProviderSet=1", line);
Assert.Equal("(no OOBE values)", OobeUpdateRegistrySnapshot.FormatEntries(new[] { Entry("AllowTelemetry", 1) }, oobeOnly: true));
Assert.Equal("(no values)", OobeUpdateRegistrySnapshot.FormatEntries(new KeyValuePair<string, object>[0], oobeOnly: false));
}
[Fact]
public void ManyEntries_AreCapped_AndSaySo()
{
var entries = Enumerable.Range(0, OobeUpdateRegistrySnapshot.MaxEntries + 5)
.Select(i => Entry($"Value{i:000}", i));
var line = OobeUpdateRegistrySnapshot.FormatEntries(entries, oobeOnly: false);
Assert.EndsWith("… (+5 more)", line);
}
[Fact]
public void Describe_CountsOnlyOobeValues_OfThePolicyManagerSystemKey()
{
var policy = OobeUpdateRegistrySnapshot.Describe("policyManagerSystem", OobeUpdateRegistrySnapshot.PolicyManagerSystemKey,
new[] { Entry("AllowTelemetry", 1) }, new string[0]);
Assert.True(policy.Exists);
Assert.False(policy.HasValues);
var oobe = OobeUpdateRegistrySnapshot.Describe("policyManagerSystem", OobeUpdateRegistrySnapshot.PolicyManagerSystemKey,
new[] { Entry("AllowTelemetry", 1), Entry("AllowOOBEUpdates", 1) }, new string[0]);
Assert.True(oobe.HasValues);
Assert.Equal("AllowOOBEUpdates=1", oobe.Line);
}
[Fact]
public void Describe_SubkeysAlone_AreNoValues()
{
var state = OobeUpdateRegistrySnapshot.Describe("ndup", @"SOFTWARE\Microsoft\Windows\CurrentVersion\NDUP",
new KeyValuePair<string, object>[0], new[] { "Updates" });
Assert.False(state.HasValues);
Assert.Equal("(no values) | subkeys: Updates", state.Line);
}
[Fact]
public void Keys_HaveDistinctAliases()
{
// The aliases are the data keys of oobe_update_state.
Assert.Equal(OobeUpdateRegistrySnapshot.Keys.Length,
OobeUpdateRegistrySnapshot.Keys.Select(k => k.Alias).Distinct().Count());
}
[Fact]
public void Read_ReturnsEveryKey_FromTheRealRegistry()
{
var states = OobeUpdateRegistrySnapshot.Read();
Assert.Equal(OobeUpdateRegistrySnapshot.Keys.Select(k => k.Path), states.Select(s => s.Path));
Assert.All(states.Where(s => !s.Exists), s => Assert.False(s.HasValues));
}
[Fact]
public void Log_ToleratesMissingLoggerOrStates()
{
using var tmp = new TempDirectory();
OobeUpdateRegistrySnapshot.Log(new AgentLogger(tmp.Path, AgentLogLevel.Info), "test", OobeUpdateRegistrySnapshot.Read());
OobeUpdateRegistrySnapshot.Log(null!, "test", OobeUpdateRegistrySnapshot.Read());
OobeUpdateRegistrySnapshot.Log(new AgentLogger(tmp.Path, AgentLogLevel.Info), "test", null!);
}
}
}
@@ -0,0 +1,260 @@
#nullable enable
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.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
using AutopilotMonitor.DecisionCore.Engine;
using AutopilotMonitor.DecisionCore.Signals;
using AutopilotMonitor.Shared;
using AutopilotMonitor.Shared.Models;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
{
/// <summary>
/// Measurement telemetry of the OOBE update page (D-310): <c>oobe_update_page</c> per
/// update-page record and <c>oobe_update_state</c> for the registry behind it — Debug, bounded
/// per run, the state only when a key holds values and the content changed.
/// </summary>
public sealed class OobeUpdateTelemetryTests : IDisposable
{
private static readonly DateTime At = new DateTime(2026, 10, 2, 9, 0, 0, DateTimeKind.Utc);
private readonly TempDirectory _tmp = new TempDirectory();
private readonly FakeSignalIngressSink _sink = new FakeSignalIngressSink();
private IReadOnlyList<OobeUpdateRegistrySnapshot.KeyState> _registry = Array.Empty<OobeUpdateRegistrySnapshot.KeyState>();
public void Dispose() => _tmp.Dispose();
private OobeUpdateTelemetry Build(Func<IReadOnlyList<OobeUpdateRegistrySnapshot.KeyState>>? reader = null) =>
new OobeUpdateTelemetry("sess-oobe", "tenant-oobe",
new InformationalEventPost(_sink, new VirtualClock(At)),
new AgentLogger(_tmp.Path, AgentLogLevel.Info),
reader ?? (() => _registry));
private IReadOnlyList<FakeSignalIngressSink.PostedSignal> ByType(string eventType) =>
_sink.Posted.Where(p =>
p.Kind == DecisionSignalKind.InformationalEvent
&& p.Payload != null
&& p.Payload.TryGetValue(SignalPayloadKeys.EventType, out var et)
&& et == eventType).ToList();
private static IReadOnlyDictionary<string, object> Data(FakeSignalIngressSink.PostedSignal posted) =>
(IReadOnlyDictionary<string, object>)posted.TypedPayload!;
private static OobeUpdateRegistrySnapshot.KeyState Key(string alias, string? line) =>
line == null
? new OobeUpdateRegistrySnapshot.KeyState(alias, $@"SOFTWARE\{alias}", exists: false, hasValues: false, line: null)
: new OobeUpdateRegistrySnapshot.KeyState(alias, $@"SOFTWARE\{alias}", exists: true, hasValues: true, line: line);
// ------------------------------------------------------------- oobe_update_page ----
[Fact]
public void Page_IsReported_WithItsEventTime_AndFields()
{
Build().ReportPage(
new OobeUpdatePageRecord(OobeUpdateVocabulary.PageStopped, 62405, page: "OobeNDUP", result: "success"),
At.AddMinutes(-3), isBackfill: true);
var page = Assert.Single(ByType(Constants.EventTypes.OobeUpdatePage));
Assert.Equal(At.AddMinutes(-3), page.OccurredAtUtc);
Assert.Equal("Debug", page.Payload![SignalPayloadKeys.Severity]);
var data = Data(page);
Assert.Equal("page_stopped", data["cxhEvent"]);
Assert.Equal("OobeNDUP", data["page"]);
Assert.Equal("success", data["result"]);
Assert.Equal(true, data["backfill"]);
Assert.Equal(1, data["occurrence"]);
Assert.False(data.ContainsKey("value"));
}
[Fact]
public void EventName_CarriesItsSafeValue()
{
Build().ReportPage(
new OobeUpdatePageRecord(OobeUpdateVocabulary.EventName, 62407, name: "ExpeditedUpdate_isNDUPAllowedByCSPSucceeded", value: "true"),
At, isBackfill: false);
var data = Data(Assert.Single(ByType(Constants.EventTypes.OobeUpdatePage)));
Assert.Equal("ExpeditedUpdate_isNDUPAllowedByCSPSucceeded", data["name"]);
Assert.Equal("true", data["value"]);
}
[Fact]
public void SamePageOrName_IsReportedAtMostThreeTimes()
{
var telemetry = Build();
for (var i = 0; i < 10; i++)
{
telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.EventName, 62406, name: "ExpeditedUpdate_progress"), At.AddSeconds(i), isBackfill: false);
}
telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.PageStopped, 62405, page: "OobeNDUP", result: "success"), At.AddMinutes(1), isBackfill: false);
var pages = ByType(Constants.EventTypes.OobeUpdatePage);
Assert.Equal(OobeUpdateTelemetry.MaxPageEventsPerKey + 1, pages.Count);
Assert.Equal(new object[] { 1, 2, 3 }, pages.Take(3).Select(p => Data(p)["occurrence"]).ToArray());
// The repeats do not crowd out the page end.
Assert.Equal("page_stopped", Data(pages.Last())["cxhEvent"]);
}
[Fact]
public void PageEvents_AreCappedPerRun_AndTheCapIsMarkedOnce()
{
var telemetry = Build();
for (var i = 0; i < OobeUpdateTelemetry.MaxPageEventsPerRun + 20; i++)
{
telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.EventName, 62406, name: $"ExpeditedUpdate_step{i}"), At.AddSeconds(i), isBackfill: false);
}
var pages = ByType(Constants.EventTypes.OobeUpdatePage);
Assert.Equal(OobeUpdateTelemetry.MaxPageEventsPerRun + 1, pages.Count);
// A name missing after the marker proves nothing — the rules check for it.
var marker = Data(pages.Last());
Assert.Equal(OobeUpdateVocabulary.NamesCapped, marker["cxhEvent"]);
Assert.Equal(false, marker["backfill"]);
Assert.Equal(OobeUpdateTelemetry.MaxPageEventsPerRun, marker["limit"]);
Assert.Equal(At.AddSeconds(OobeUpdateTelemetry.MaxPageEventsPerRun), pages.Last().OccurredAtUtc); // the first name over the budget
}
[Fact]
public void BackfilledNames_HaveTheirOwnBudget()
{
// The records from before the agent started cannot spend the live page's budget.
var telemetry = Build();
for (var i = 0; i < OobeUpdateTelemetry.MaxBackfillPageEventsPerRun + 5; i++)
{
telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.EventName, 62406, name: $"ExpeditedUpdate_old{i}"), At.AddMinutes(-30), isBackfill: true);
}
Assert.True(telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.EventName, 62406, name: "ExpeditedUpdate_live"), At, isBackfill: false));
var markers = ByType(Constants.EventTypes.OobeUpdatePage)
.Where(p => (string)Data(p)["cxhEvent"] == OobeUpdateVocabulary.NamesCapped).ToList();
var marker = Assert.Single(markers);
Assert.Equal(true, Data(marker)["backfill"]);
Assert.Equal(OobeUpdateTelemetry.MaxBackfillPageEventsPerRun, Data(marker)["limit"]);
}
[Fact]
public void PageStartAndStop_AreReported_AfterTheNameBudgetIsSpent()
{
// The time attribution reads the update's span from the page start and stop.
var telemetry = Build();
for (var i = 0; i < OobeUpdateTelemetry.MaxPageEventsPerRun; i++)
{
Assert.True(telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.EventName, 62406, name: $"ExpeditedUpdate_step{i}"), At, isBackfill: false));
}
Assert.False(telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.EventName, 62406, name: "ExpeditedUpdate_late"), At, isBackfill: false));
Assert.False(telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.EventName, 62406, name: "ExpeditedUpdate_later"), At, isBackfill: false));
Assert.True(telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.PageStarted, 62404, page: "RebootNDUP"), At.AddMinutes(1), isBackfill: false));
Assert.True(telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.PageStopped, 62405, page: "RebootNDUP", result: "success"), At.AddMinutes(2), isBackfill: false));
var pages = ByType(Constants.EventTypes.OobeUpdatePage);
Assert.Equal(OobeUpdateTelemetry.MaxPageEventsPerRun + 3, pages.Count);
Assert.Equal(new object[] { OobeUpdateVocabulary.NamesCapped, "page_started", "page_stopped" },
pages.Skip(OobeUpdateTelemetry.MaxPageEventsPerRun).Select(p => Data(p)["cxhEvent"]).ToArray());
}
[Fact]
public void SamePageStart_IsReportedAtMostThreeTimes()
{
// Outside the run budget, but a page that restarts in a loop still cannot flood.
var telemetry = Build();
var reported = Enumerable.Range(0, 10)
.Count(i => telemetry.ReportPage(new OobeUpdatePageRecord(OobeUpdateVocabulary.PageStarted, 62404, page: "OobeNDUP"), At.AddMinutes(i), isBackfill: false));
Assert.Equal(OobeUpdateTelemetry.MaxPageEventsPerKey, reported);
}
// ------------------------------------------------------------ oobe_update_state ----
[Fact]
public void State_WithoutValues_IsNotReported()
{
_registry = new[] { Key("setupOobeNdup", null), Key("policiesOobe", null) };
Build().ReportState(OobeUpdateTelemetry.Moments.AgentStart);
Assert.Empty(ByType(Constants.EventTypes.OobeUpdateState));
}
[Fact]
public void State_ReportsOnlyKeysWithValues_UnderTheirAlias()
{
_registry = new[]
{
Key("setupOobeNdup", "EnableExpeditedUpdate=1, EnableExpeditedUpdateSyncDownloadCompleted=1"),
Key("ndup", null),
new OobeUpdateRegistrySnapshot.KeyState("policyManagerSystem", "x", exists: true, hasValues: false, line: "(no OOBE values)"),
Key("espSetupPolicy", "InstallQualityUpdates=1"),
};
Build().ReportState(OobeUpdateTelemetry.Moments.UpdatePageStopped, "OobeNDUP");
var state = Assert.Single(ByType(Constants.EventTypes.OobeUpdateState));
Assert.Equal("Debug", state.Payload![SignalPayloadKeys.Severity]);
var data = Data(state);
Assert.Equal("update_page_stopped", data["moment"]);
Assert.Equal("OobeNDUP", data["page"]);
Assert.Equal(2, data["keysWithValues"]);
Assert.Equal("EnableExpeditedUpdate=1, EnableExpeditedUpdateSyncDownloadCompleted=1", data["setupOobeNdup"]);
Assert.Equal("InstallQualityUpdates=1", data["espSetupPolicy"]);
Assert.False(data.ContainsKey("ndup"));
Assert.False(data.ContainsKey("policyManagerSystem"));
}
[Fact]
public void UnchangedState_IsReportedOnce_AChangeAgain()
{
var telemetry = Build();
_registry = new[] { Key("espSetupPolicy", "InstallQualityUpdates=1") };
telemetry.ReportState(OobeUpdateTelemetry.Moments.AgentStart);
telemetry.ReportState(OobeUpdateTelemetry.Moments.UpdatePageStarted, "OobeNDUP");
_registry = new[] { Key("espSetupPolicy", "InstallQualityUpdates=1"), Key("setupOobeNdup", "EnableExpeditedUpdate=1") };
telemetry.ReportState(OobeUpdateTelemetry.Moments.UpdatePageStopped, "OobeNDUP");
telemetry.ReportState(OobeUpdateTelemetry.Moments.AgentStop);
var states = ByType(Constants.EventTypes.OobeUpdateState);
Assert.Equal(new object[] { "agent_start", "update_page_stopped" }, states.Select(s => Data(s)["moment"]).ToArray());
}
[Fact]
public void StateEvents_AreCappedPerRun()
{
var telemetry = Build();
for (var i = 0; i < OobeUpdateTelemetry.MaxStateEventsPerRun + 4; i++)
{
_registry = new[] { Key("setupOobeNdup", $"ExpeditedUpdateStatus={i}") };
telemetry.ReportState(OobeUpdateTelemetry.Moments.UpdatePageStarted, "OobeNDUP");
}
Assert.Equal(OobeUpdateTelemetry.MaxStateEventsPerRun, ByType(Constants.EventTypes.OobeUpdateState).Count);
}
[Fact]
public void LongLines_AreTruncated()
{
_registry = new[] { Key("ndupUpdates", new string('x', 3000)) };
Build().ReportState(OobeUpdateTelemetry.Moments.AgentStart);
var line = (string)Data(Assert.Single(ByType(Constants.EventTypes.OobeUpdateState)))["ndupUpdates"];
Assert.Equal(OobeUpdateTelemetry.MaxStateLineLength + 1, line.Length);
Assert.EndsWith("…", line);
}
[Fact]
public void FailingRegistryRead_ReportsNothing_AndDoesNotThrow()
{
Build(() => throw new UnauthorizedAccessException("denied")).ReportState(OobeUpdateTelemetry.Moments.AgentStart);
Assert.Empty(_sink.Posted);
}
}
}
@@ -0,0 +1,289 @@
#nullable enable
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.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
using AutopilotMonitor.DecisionCore.Engine;
using AutopilotMonitor.DecisionCore.Signals;
using AutopilotMonitor.DecisionCore.State;
using AutopilotMonitor.Shared;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
{
/// <summary>
/// ServicingTracker (D-310) — the CBS servicing steps of OS updates from the Setup log. The
/// sequence and XML shapes are those Windows 11 writes for a cumulative update installed by
/// <c>UpdateAgentLCU</c>: initiate Absent → Staged, Staged reached, initiate Staged → Installed,
/// restart required, Installed reached after the restart.
/// </summary>
public sealed class ServicingTrackerTests : IDisposable
{
private static readonly DateTime T0 = new DateTime(2026, 9, 25, 18, 52, 37, DateTimeKind.Utc);
private const string Kb = "KB5099999";
private readonly TempDirectory _tmp = new TempDirectory();
private readonly FakeSignalIngressSink _sink = new FakeSignalIngressSink();
private readonly UpdateActivityTally _tally = new UpdateActivityTally();
private readonly List<(string Step, string Update, DateTime At)> _osActivity = new List<(string, string, DateTime)>();
private readonly ServicingTracker _tracker;
private long _nextRecordId = 1200;
public ServicingTrackerTests()
{
_tracker = NewTracker(stateDirectory: null);
}
public void Dispose() => _tmp.Dispose();
private ServicingTracker NewTracker(string? stateDirectory) =>
new ServicingTracker(
sessionId: "sess-cbs",
tenantId: "tenant-cbs",
post: new InformationalEventPost(_sink, new VirtualClock(T0)),
logger: new AgentLogger(_tmp.Path, AgentLogLevel.Info),
backfillEnabled: false,
stateDirectory: stateDirectory,
tally: _tally,
onOsUpdateActivity: (step, update, at) => _osActivity.Add((step, update, at)));
private void Process(
int eventId,
DateTime at,
string? initialState = null,
string? targetState = "Installed",
string? errorCode = null,
string? client = "UpdateAgentLCU",
string? package = Kb,
long? recordId = null,
ServicingTracker? tracker = null,
bool isBackfill = false)
{
(tracker ?? _tracker).ProcessEvent(
eventId: eventId,
recordId: recordId ?? _nextRecordId++,
timeCreatedUtc: at,
package: package,
initialState: initialState,
targetState: targetState,
errorCode: errorCode,
client: client,
isBackfill: isBackfill);
}
private IReadOnlyList<FakeSignalIngressSink.PostedSignal> Servicing() =>
_sink.Posted.Where(p =>
p.Kind == DecisionSignalKind.InformationalEvent
&& p.Payload != null
&& p.Payload.TryGetValue(SignalPayloadKeys.EventType, out var et)
&& et == Constants.EventTypes.WindowsUpdateServicing).ToList();
private static IReadOnlyDictionary<string, object> Data(FakeSignalIngressSink.PostedSignal s) =>
(IReadOnlyDictionary<string, object>)s.TypedPayload!;
[Fact]
public void CumulativeUpdateSequence_ListsEveryStep_AndReportsTheEngineSteps()
{
Process(ServicingTracker.EventId_InitiatingChanges, T0, initialState: "Absent", targetState: "Staged");
Process(ServicingTracker.EventId_StateReached, T0.AddMinutes(4), targetState: "Staged", errorCode: "0x0");
Process(ServicingTracker.EventId_InitiatingChanges, T0.AddMinutes(29), initialState: "Staged", targetState: "Installed");
Process(ServicingTracker.EventId_RebootRequired, T0.AddMinutes(33), targetState: "Installed", errorCode: "0x0");
// Written after the restart.
Process(ServicingTracker.EventId_StateReached, T0.AddMinutes(41), targetState: "Installed", errorCode: "0x0");
var rows = Servicing();
Assert.Equal(
new[] { "initiating", "state_reached", "initiating", "reboot_required", "state_reached" },
rows.Select(s => (string)Data(s)["step"]).ToArray());
Assert.All(rows, s => Assert.Equal("Info", s.Payload![SignalPayloadKeys.Severity]));
Assert.All(rows, s => Assert.Equal("ServicingWatcher", s.Payload![SignalPayloadKeys.Source]));
Assert.All(rows, s => Assert.Equal(Kb, Data(s)["package"]));
Assert.All(rows, s => Assert.Equal("UpdateAgentLCU", Data(s)["client"]));
// ErrorCode 0x0 is success, not a decoded HRESULT.
Assert.All(rows, s => Assert.False(Data(s).ContainsKey("hresult")));
Assert.Equal("Absent", Data(rows[0])["initialState"]);
Assert.Equal("Staged", Data(rows[0])["targetState"]);
Assert.Equal(T0.AddMinutes(33), rows[3].OccurredAtUtc);
Assert.Contains("needs a restart", rows[3].Payload![SignalPayloadKeys.Message]);
Assert.Equal(
new[]
{
OsUpdateSteps.StagingStarted, OsUpdateSteps.Staged, OsUpdateSteps.InstallStarted,
OsUpdateSteps.RebootRequired, OsUpdateSteps.Installed,
},
_osActivity.Select(a => a.Step).ToArray());
Assert.All(_osActivity, a => Assert.Equal(Kb, a.Update));
Assert.Equal(T0.AddMinutes(29), _osActivity[2].At);
}
[Theory]
[InlineData("CbsTask")]
[InlineData("DISM Package Manager Provider")]
[InlineData("")]
[InlineData(null)]
public void NonOsClients_AreCountedNotListed(string? client)
{
// CBS maintenance writes hundreds of these in seconds; language packs and features on
// demand installed by apps are not the OOBE update either.
Process(ServicingTracker.EventId_InitiatingChanges, T0, initialState: "Staged", targetState: "Installed",
client: client, package: "Microsoft-Windows-Telnet-Client-FOD-en-US");
Process(ServicingTracker.EventId_StateReached, T0, targetState: "Installed", errorCode: "0x0",
client: client, package: "Microsoft-Windows-Telnet-Client-FOD-en-US");
Assert.Empty(_sink.Posted);
Assert.Empty(_osActivity);
var counts = _tally.Snapshot().ToDictionary(kv => kv.Key, kv => kv.Value);
Assert.Equal(1, counts["servicing_other_1"]);
Assert.Equal(1, counts["servicing_other_2"]);
}
[Fact]
public void WindowsUpdateAgentClient_IsAnOsClient()
{
Process(ServicingTracker.EventId_RebootRequired, T0, client: "WindowsUpdateAgent", errorCode: "0x0");
Assert.Single(Servicing());
Assert.Equal(OsUpdateSteps.RebootRequired, Assert.Single(_osActivity).Step);
}
[Fact]
public void ChangeFailed_EmitsWarning_WithDecodedHResult()
{
Process(ServicingTracker.EventId_ChangeFailed, T0, targetState: "Installed", errorCode: "0x800f0922");
var s = Assert.Single(Servicing());
Assert.Equal("Warning", s.Payload![SignalPayloadKeys.Severity]);
var data = Data(s);
Assert.Equal("failed", data["step"]);
Assert.Equal("0x800F0922", data["hresult"]);
Assert.Equal("CBS_E_INSTALLERS_FAILED", data["hresultSymbol"]);
Assert.Contains("FAILED", s.Payload![SignalPayloadKeys.Message]);
Assert.Equal(OsUpdateSteps.Failed, Assert.Single(_osActivity).Step);
}
[Fact]
public void PartiallyInstalled_IsAFailure_AndSaysSo()
{
Process(ServicingTracker.EventId_PartiallyInstalled, T0, targetState: "Installed", errorCode: "0x800f0922");
var s = Assert.Single(Servicing());
Assert.Equal("failed", Data(s)["step"]);
Assert.Contains("partially installed", s.Payload![SignalPayloadKeys.Message]);
Assert.Equal(OsUpdateSteps.Failed, Assert.Single(_osActivity).Step);
}
[Fact]
public void ChangeTowardsAnotherState_IsListed_ButIsNoEngineStep()
{
// Removing a superseded package is servicing, not progress of the update install.
Process(ServicingTracker.EventId_InitiatingChanges, T0, initialState: "Installed", targetState: "Absent");
Assert.Single(Servicing());
Assert.Empty(_osActivity);
}
[Fact]
public void BackfilledRecord_KeepsItsEventTime()
{
var earlier = T0.AddMinutes(-20);
Process(ServicingTracker.EventId_InitiatingChanges, earlier, initialState: "Absent", targetState: "Staged", isBackfill: true);
var s = Assert.Single(Servicing());
Assert.Equal(earlier, s.OccurredAtUtc);
Assert.Equal(true, Data(s)["backfilled"]);
Assert.Equal(earlier, Assert.Single(_osActivity).At);
}
[Fact]
public void Watermark_SkipsEmittedRecordsAfterRestart()
{
var first = NewTracker(_tmp.Path);
Process(ServicingTracker.EventId_RebootRequired, T0, errorCode: "0x0", recordId: 1205, tracker: first);
var second = NewTracker(_tmp.Path);
second.LoadWatermark();
Process(ServicingTracker.EventId_RebootRequired, T0, errorCode: "0x0", recordId: 1205, tracker: second, isBackfill: true);
Process(ServicingTracker.EventId_StateReached, T0.AddMinutes(8), errorCode: "0x0", recordId: 1207, tracker: second);
Assert.Equal(new[] { "reboot_required", "state_reached" }, Servicing().Select(s => (string)Data(s)["step"]).ToArray());
}
[Fact]
public void SameRecordDeliveredTwice_IsListedOnce()
{
Process(ServicingTracker.EventId_RebootRequired, T0, errorCode: "0x0", recordId: 1205);
Process(ServicingTracker.EventId_RebootRequired, T0, errorCode: "0x0", recordId: 1205);
Assert.Single(Servicing());
Assert.Single(_osActivity);
}
[Fact]
public void ParseUserData_ReadsBothCbsPayloadShapes()
{
const string initiate =
"<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'><System><Provider Name='Microsoft-Windows-Servicing' Guid='{bd12f3b8-fc40-4a61-a307-b7a013a069c1}'/>" +
"<EventID>1</EventID><Version>0</Version><Level>0</Level><Task>1</Task><Opcode>0</Opcode><Keywords>0x8000000000000000</Keywords>" +
"<TimeCreated SystemTime='2026-09-25T19:21:42.4486782Z'/><EventRecordID>1203</EventRecordID><Channel>Setup</Channel><Computer>DESKTOP-TEST</Computer>" +
"<Security UserID='S-1-5-18'/></System><UserData><CbsPackageInitiateChanges xmlns='http://manifests.microsoft.com/win/2004/08/windows/setup_provider'>" +
"<PackageIdentifier>KB5099999</PackageIdentifier><InitialPackageState>5064</InitialPackageState><InitialPackageStateTextized>Staged</InitialPackageStateTextized>" +
"<IntendedPackageState>5112</IntendedPackageState><IntendedPackageStateTextized>Installed</IntendedPackageStateTextized><Client>UpdateAgentLCU</Client>" +
"</CbsPackageInitiateChanges></UserData></Event>";
const string changeState =
"<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'><System><Provider Name='Microsoft-Windows-Servicing' Guid='{bd12f3b8-fc40-4a61-a307-b7a013a069c1}'/>" +
"<EventID>4</EventID><TimeCreated SystemTime='2026-09-25T19:26:07.1381462Z'/><EventRecordID>1205</EventRecordID><Channel>Setup</Channel></System>" +
"<UserData><CbsPackageChangeState xmlns='http://manifests.microsoft.com/win/2004/08/windows/setup_provider'><PackageIdentifier>KB5099999</PackageIdentifier>" +
"<IntendedPackageState>5112</IntendedPackageState><IntendedPackageStateTextized>Installed</IntendedPackageStateTextized><ErrorCode>0x0</ErrorCode>" +
"<Client>UpdateAgentLCU</Client></CbsPackageChangeState></UserData></Event>";
var a = ServicingTracker.ParseUserData(initiate);
Assert.Equal("KB5099999", a["PackageIdentifier"]);
Assert.Equal("Staged", a["InitialPackageStateTextized"]);
Assert.Equal("Installed", a["IntendedPackageStateTextized"]);
Assert.Equal("UpdateAgentLCU", a["client"]); // case-insensitive
Assert.False(a.ContainsKey("ErrorCode"));
var b = ServicingTracker.ParseUserData(changeState);
Assert.Equal("Installed", b["IntendedPackageStateTextized"]);
Assert.Equal("0x0", b["ErrorCode"]);
Assert.False(b.ContainsKey("InitialPackageStateTextized"));
}
[Fact]
public void ParseUserData_MalformedOrWithoutUserData_ReturnsEmpty_NeverThrows()
{
Assert.Empty(ServicingTracker.ParseUserData(null));
Assert.Empty(ServicingTracker.ParseUserData(""));
Assert.Empty(ServicingTracker.ParseUserData("<not-xml"));
Assert.Empty(ServicingTracker.ParseUserData("<Event><System><EventID>1</EventID></System></Event>"));
}
[Fact]
public void Watermark_PersistsOnlyAfterTheEngineSignalWasHandedOver()
{
// "Reboot required" is written right before the update's restart: its engine signal
// must be on its way before the watermark tells the next run's backfill to skip it.
var path = System.IO.Path.Combine(_tmp.Path, ServicingTracker.WatermarkStateFileName);
bool? watermarkOnDiskAtSignal = null;
var tracker = new ServicingTracker(
sessionId: "sess-cbs",
tenantId: "tenant-cbs",
post: new InformationalEventPost(_sink, new VirtualClock(T0)),
logger: new AgentLogger(_tmp.Path, AgentLogLevel.Info),
backfillEnabled: false,
stateDirectory: _tmp.Path,
tally: _tally,
onOsUpdateActivity: (step, update, at) => watermarkOnDiskAtSignal = System.IO.File.Exists(path));
Process(ServicingTracker.EventId_RebootRequired, T0, errorCode: "0x0", tracker: tracker);
Assert.False(watermarkOnDiskAtSignal);
Assert.True(System.IO.File.Exists(path));
}
}
}
@@ -0,0 +1,85 @@
#nullable enable
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
{
/// <summary>
/// WindowsUpdateClassifier (D-310) — decides which Windows Update client events are listed one
/// by one (OS updates) and which are only counted (Store, Defender, everything else). The
/// titles are the shapes the WU client writes on Windows 11 GA and Insider builds.
/// </summary>
public sealed class WindowsUpdateClassifierTests
{
[Theory]
[InlineData("2026-09 Cumulative Update for Windows 11, version 25H2 for x64-based Systems (KB5099999)")]
[InlineData("2026-09 Cumulative Update Preview for Windows 11, version 24H2 for x64-based Systems (KB5099998)")]
[InlineData("2026-09 Dynamic Update for Windows 11, version 25H2 for x64-based Systems (KB5099997)")]
[InlineData("2026-09 Servicing Stack Update for Windows 11, version 25H2 for x64-based Systems (KB5099996)")]
[InlineData("Windows 11 Insider Preview Quality Update (26220.9568)")]
[InlineData("Windows 11 Insider Preview Feature Update (26220.9022)")]
[InlineData("Windows 11, version 25H2 Upgrade")]
[InlineData("2026-09 .NET Framework Security Update (KB5099995)")]
[InlineData("2026-08 .NET Framework Preview Update (KB5099994)")]
[InlineData("Windows Malicious Software Removal Tool x64 - v5.145 (KB890830)")]
public void WindowsAndDotNetFrameworkUpdates_AreOs(string title)
{
Assert.Equal(WindowsUpdateClassifier.Os, WindowsUpdateClassifier.Classify(title, serviceGuid: null));
}
[Theory]
[InlineData("Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.459.505.0) - Current Channel (Broad)")]
[InlineData("Update for Microsoft Defender Antivirus antimalware platform - KB4052623 (Version 4.18.26080.4) - Current Channel (Staged)")]
// Names "Windows" and "Update" — Defender must still win.
[InlineData("Windows 10/11 - Update for Microsoft Defender for Endpoint - KB5005292 (Version 10.8838.26060.15013) - Current Channel (Staged)")]
public void DefenderUpdates_AreDefender(string title)
{
Assert.Equal(WindowsUpdateClassifier.Defender, WindowsUpdateClassifier.Classify(title, serviceGuid: null));
}
[Theory]
[InlineData("9NSTH9KHZDLQ-Microsoft.UI.Xaml.2.8")]
[InlineData("9NBLGGH4NNS1-Microsoft.DesktopAppInstaller")]
// Upper-case "WINDOWS" inside the package name must not make it an OS update.
[InlineData("9WZDNCRFHVN5-MICROSOFT.WINDOWSCALCULATOR")]
[InlineData("9WZDNCRFHWKN-MICROSOFT.WINDOWSSOUNDRECORDER")]
[InlineData("ApplicationSet-9WZDNCRFJ3Q2-Microsoft.BingWeather")]
public void StoreProductTitles_AreStore(string title)
{
Assert.Equal(WindowsUpdateClassifier.Store, WindowsUpdateClassifier.Classify(title, serviceGuid: null));
}
[Theory]
[InlineData("{855e8a7c-ecb4-4ca3-b045-1dfa50104289}")]
[InlineData("{855E8A7C-ECB4-4CA3-B045-1DFA50104289}")]
[InlineData("855e8a7c-ecb4-4ca3-b045-1dfa50104289")]
public void StoreServiceGuid_IsStore_WhateverTheTitle(string serviceGuid)
{
Assert.Equal(WindowsUpdateClassifier.Store,
WindowsUpdateClassifier.Classify("2026-09 Cumulative Update for Windows 11 (KB5099999)", serviceGuid));
}
[Fact]
public void OtherServiceGuid_LeavesTheTitleInCharge()
{
Assert.Equal(WindowsUpdateClassifier.Os,
WindowsUpdateClassifier.Classify("2026-09 Cumulative Update for Windows 11 (KB5099999)", "{7971f918-a847-4430-9279-4a52d1efe18d}"));
}
[Theory]
[InlineData("Intel net Driver Update (24.50.0.4)")]
[InlineData("PowerShell v7.6.6 (x64)")]
[InlineData("Remote help 5.2.1040.0")]
[InlineData("Microsoft Edge-Stable Channel Version 140 Update for x64 based Edge-Chromium (Build 140.0.3485.54)")]
[InlineData("Microsoft .NET 8.0.20 Security Update for x64 Client (KB5099993)")]
[InlineData("Windows Feature Experience Pack 1000.26100.234.0")]
[InlineData("KB5099999")]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void EverythingElse_IsOther(string? title)
{
Assert.Equal(WindowsUpdateClassifier.Other, WindowsUpdateClassifier.Classify(title!, serviceGuid: null));
}
}
}
@@ -1,6 +1,7 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AutopilotMonitor.Agent.V2.Core.Logging;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.SystemSignals;
@@ -9,55 +10,81 @@ using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
using AutopilotMonitor.DecisionCore.Engine;
using AutopilotMonitor.DecisionCore.Signals;
using AutopilotMonitor.DecisionCore.State;
using AutopilotMonitor.Shared;
using Newtonsoft.Json.Linq;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
{
/// <summary>
/// WindowsUpdateTracker — surfaces quality/cumulative update activity during OOBE from the
/// WindowsUpdateClient/Operational channel. Drives the primitive <see cref="WindowsUpdateTracker.ProcessEvent"/>
/// WindowsUpdateClient provider: install events from the System log, scan/download events from
/// the Operational channel (D-310). Drives the primitive <see cref="WindowsUpdateTracker.ProcessEvent"/>
/// test seam (no real EventRecord, which is abstract + Windows-only), mirroring the
/// ModernDeploymentTracker test pattern.
/// ModernDeploymentTracker test pattern. Update titles are the shapes the provider writes.
/// </summary>
public sealed class WindowsUpdateTrackerTests : IDisposable
{
private static readonly DateTime At = new DateTime(2026, 7, 8, 10, 15, 0, DateTimeKind.Utc);
private const string Lcu = "2026-09 Cumulative Update for Windows 11, version 25H2 for x64-based Systems (KB5099999)";
private const string StoreApp = "9WZDNCRFHVN5-MICROSOFT.WINDOWSCALCULATOR";
private const string DefenderIntelligence = "Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.459.505.0) - Current Channel (Broad)";
private readonly TempDirectory _tmp = new TempDirectory();
private readonly FakeSignalIngressSink _sink;
private readonly WindowsUpdateTracker _tracker;
private readonly FakeSignalIngressSink _sink = new FakeSignalIngressSink();
private readonly UpdateActivityTally _tally = new UpdateActivityTally();
private readonly List<(string Step, string Update, DateTime At)> _osActivity = new List<(string, string, DateTime)>();
private readonly WindowsUpdateTracker _system;
private readonly WindowsUpdateTracker _operational;
private long _nextRecordId = 1;
public WindowsUpdateTrackerTests()
{
_sink = new FakeSignalIngressSink();
var post = new InformationalEventPost(_sink, new VirtualClock(At));
var logger = new AgentLogger(_tmp.Path, AgentLogLevel.Info);
_tracker = new WindowsUpdateTracker(
sessionId: "sess-wu",
tenantId: "tenant-wu",
post: post,
logger: logger,
backfillEnabled: false,
stateDirectory: null); // in-memory dedup for most tests
// In-memory dedup for most tests.
_system = NewTracker(WindowsUpdateTracker.SystemChannel, stateDirectory: null);
_operational = NewTracker(channel: null, stateDirectory: null);
}
public void Dispose() => _tmp.Dispose();
private void Emit(int eventId, string? errorCode = null, string? updateTitle = "Cumulative Update KB5099999", long? recordId = null)
private WindowsUpdateTracker NewTracker(string? channel, string? stateDirectory) =>
new WindowsUpdateTracker(
sessionId: "sess-wu",
tenantId: "tenant-wu",
post: new InformationalEventPost(_sink, new VirtualClock(At)),
logger: new AgentLogger(_tmp.Path, AgentLogLevel.Info),
backfillEnabled: false,
stateDirectory: stateDirectory,
channel: channel,
tally: _tally,
onOsUpdateActivity: (step, update, at) => _osActivity.Add((step, update, at)));
private void Emit(
WindowsUpdateTracker tracker,
int eventId,
string? updateTitle = Lcu,
string? errorCode = null,
long? recordId = null,
string? serviceGuid = null,
string? updateCount = null,
DateTime? at = null,
bool isBackfill = false)
{
_tracker.ProcessEvent(
tracker.ProcessEvent(
eventId: eventId,
level: 4,
recordId: recordId ?? _nextRecordId++,
timeCreatedUtc: At,
timeCreatedUtc: at ?? At,
updateTitle: updateTitle,
updateGuid: "{8b1c8726-1111-2222-3333-444455556666}",
updateRevisionNumber: "200",
errorCode: errorCode,
formattedDescription: null,
isBackfill: false);
isBackfill: isBackfill,
serviceGuid: serviceGuid,
updateCount: updateCount);
}
private IReadOnlyList<FakeSignalIngressSink.PostedSignal> ByType(string eventType) =>
@@ -70,10 +97,17 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
private static IReadOnlyDictionary<string, object> Data(FakeSignalIngressSink.PostedSignal s) =>
(IReadOnlyDictionary<string, object>)s.TypedPayload!;
private int Counted(string key) =>
_tally.Snapshot().Where(kv => kv.Key == key).Select(kv => kv.Value).SingleOrDefault();
// ---------------------------------------------------------------------------
// Install events (System log)
// ---------------------------------------------------------------------------
[Fact]
public void FailureEvent_EmitsWindowsUpdateFailed_WithDecodedHResult_AndImmediateUpload()
{
Emit(WindowsUpdateTracker.EventId_InstallFailure, errorCode: "0x80240022");
Emit(_system, WindowsUpdateTracker.EventId_InstallFailure, errorCode: "0x80240022");
var failed = ByType(Constants.EventTypes.WindowsUpdateFailed);
var s = Assert.Single(failed);
@@ -83,37 +117,44 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
var data = Data(s);
Assert.Equal("0x80240022", data["hresult"]);
Assert.Equal("WU_E_ALL_UPDATES_FAILED", data["hresultSymbol"]);
Assert.Equal("Cumulative Update KB5099999", data["updateTitle"]);
Assert.Equal(Lcu, data["updateTitle"]);
Assert.Equal(20, data["wuEventId"]);
Assert.Equal("system", data["wuChannel"]);
Assert.Equal("install", data["wuPhase"]);
Assert.Equal(WindowsUpdateClassifier.Os, data["updateClass"]);
}
[Fact]
public void SuccessEvent_EmitsWindowsUpdateSucceeded_InfoSeverity()
{
Emit(WindowsUpdateTracker.EventId_InstallSuccess);
Emit(_system, WindowsUpdateTracker.EventId_InstallSuccess);
var ok = ByType(Constants.EventTypes.WindowsUpdateSucceeded);
var s = Assert.Single(ok);
Assert.Equal("Info", s.Payload![SignalPayloadKeys.Severity]);
Assert.Equal("false", s.Payload![SignalPayloadKeys.ImmediateUpload]);
Assert.Equal("installed", Data(s)["wuPhase"]);
}
[Fact]
public void StartedAndDownloadEvents_EmitDebugStarted()
public void StartedAndDownloadEvents_EmitInfoStarted_WithTheirPhase()
{
Emit(WindowsUpdateTracker.EventId_InstallStarted);
Emit(WindowsUpdateTracker.EventId_DownloadStarted);
// Info, not Debug: with only OS updates listed they are the visible progress of a
// long OOBE update.
Emit(_system, WindowsUpdateTracker.EventId_InstallStarted);
Emit(_system, WindowsUpdateTracker.EventId_DownloadStarted);
var started = ByType(Constants.EventTypes.WindowsUpdateStarted);
Assert.Equal(2, started.Count);
Assert.All(started, s => Assert.Equal("Debug", s.Payload![SignalPayloadKeys.Severity]));
Assert.All(started, s => Assert.Equal("Info", s.Payload![SignalPayloadKeys.Severity]));
Assert.Equal(new[] { "install", "download" }, started.Select(s => (string)Data(s)["wuPhase"]).ToArray());
}
[Fact]
public void SameRecordId_ProcessedTwice_EmitsOnce()
{
Emit(WindowsUpdateTracker.EventId_InstallSuccess, recordId: 42);
Emit(WindowsUpdateTracker.EventId_InstallSuccess, recordId: 42);
Emit(_system, WindowsUpdateTracker.EventId_InstallSuccess, recordId: 42);
Emit(_system, WindowsUpdateTracker.EventId_InstallSuccess, recordId: 42);
Assert.Single(ByType(Constants.EventTypes.WindowsUpdateSucceeded));
}
@@ -125,8 +166,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
// with a higher RecordId can be processed first. An older, never-emitted backfill event
// (lower RecordId) must NOT be suppressed as "already processed" — these early pre-agent
// OOBE updates are the whole point of the feature. A high-water-mark dedup dropped them.
Emit(WindowsUpdateTracker.EventId_InstallSuccess, recordId: 100); // live, high RecordId
Emit(WindowsUpdateTracker.EventId_InstallSuccess, recordId: 50); // backfill, older, never seen
Emit(_system, WindowsUpdateTracker.EventId_InstallSuccess, recordId: 100); // live, high RecordId
Emit(_system, WindowsUpdateTracker.EventId_InstallSuccess, recordId: 50); // backfill, older, never seen
Assert.Equal(2, ByType(Constants.EventTypes.WindowsUpdateSucceeded).Count);
}
@@ -138,53 +179,212 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
// time, not at agent start (the ctor's default UtcNow). InformationalEventPost forwards
// EnrollmentEvent.Timestamp as the signal's OccurredAtUtc, which drives the timeline entry.
var eventTime = new DateTime(2026, 7, 8, 7, 5, 0, DateTimeKind.Utc); // well before `At`
_tracker.ProcessEvent(
eventId: WindowsUpdateTracker.EventId_InstallFailure,
level: 4,
recordId: 7,
timeCreatedUtc: eventTime,
updateTitle: "KB5099999",
updateGuid: null,
updateRevisionNumber: null,
errorCode: "0x80240022",
formattedDescription: null,
isBackfill: true);
Emit(_system, WindowsUpdateTracker.EventId_InstallFailure, errorCode: "0x80240022",
recordId: 7, at: eventTime, isBackfill: true);
var s = Assert.Single(ByType(Constants.EventTypes.WindowsUpdateFailed));
Assert.Equal(eventTime, s.OccurredAtUtc);
Assert.Equal(true, Data(s)["backfilled"]);
}
[Fact]
public void NegativeRecordId_IsNeverDeduped()
{
Emit(WindowsUpdateTracker.EventId_InstallSuccess, recordId: -1);
Emit(WindowsUpdateTracker.EventId_InstallSuccess, recordId: -1);
Emit(_system, WindowsUpdateTracker.EventId_InstallSuccess, recordId: -1);
Emit(_system, WindowsUpdateTracker.EventId_InstallSuccess, recordId: -1);
Assert.Equal(2, ByType(Constants.EventTypes.WindowsUpdateSucceeded).Count);
}
// ---------------------------------------------------------------------------
// Scan/download events (Operational channel)
// ---------------------------------------------------------------------------
[Fact]
public void Watermark_PersistsAcrossTrackerInstances()
public void Downloaded_EmitsInfoStarted_WithDownloadedPhase()
{
var post = new InformationalEventPost(_sink, new VirtualClock(At));
var logger = new AgentLogger(_tmp.Path, AgentLogLevel.Info);
Emit(_operational, WindowsUpdateTracker.EventId_Downloaded);
var first = new WindowsUpdateTracker("s", "t", post, logger, backfillEnabled: false, stateDirectory: _tmp.Path);
first.ProcessEvent(WindowsUpdateTracker.EventId_InstallSuccess, 4, recordId: 500, timeCreatedUtc: At,
updateTitle: "KB1", updateGuid: null, updateRevisionNumber: null, errorCode: null,
formattedDescription: null, isBackfill: false);
var s = Assert.Single(ByType(Constants.EventTypes.WindowsUpdateStarted));
Assert.Equal("Info", s.Payload![SignalPayloadKeys.Severity]);
Assert.Equal("downloaded", Data(s)["wuPhase"]);
Assert.Equal("operational", Data(s)["wuChannel"]);
}
// A fresh tracker (simulating an agent restart) must load the watermark and skip the
// already-emitted record when the OOBE backfill re-reads it.
var second = new WindowsUpdateTracker("s", "t", post, logger, backfillEnabled: false, stateDirectory: _tmp.Path);
second.LoadWatermark();
second.ProcessEvent(WindowsUpdateTracker.EventId_InstallSuccess, 4, recordId: 500, timeCreatedUtc: At,
updateTitle: "KB1", updateGuid: null, updateRevisionNumber: null, errorCode: null,
formattedDescription: null, isBackfill: true);
[Fact]
public void DownloadFailure_OfAnOsUpdate_EmitsWarning_WithDownloadPhase()
{
Emit(_operational, WindowsUpdateTracker.EventId_DownloadFailed, errorCode: "0x80248007");
var s = Assert.Single(ByType(Constants.EventTypes.WindowsUpdateFailed));
Assert.Equal("Warning", s.Payload![SignalPayloadKeys.Severity]);
Assert.Equal("false", s.Payload![SignalPayloadKeys.ImmediateUpload]);
var data = Data(s);
Assert.Equal("download", data["wuPhase"]);
Assert.Equal("0x80248007", data["hresult"]);
}
[Fact]
public void ScanFound_EmitsOnlyChangedCounts_AndCountsTheRepeats()
{
foreach (var count in new[] { "3", "3", "5", "5", "0" })
Emit(_operational, WindowsUpdateTracker.EventId_ScanFound, updateTitle: null, updateCount: count);
var found = ByType(Constants.EventTypes.WindowsUpdateStarted);
Assert.Equal(new[] { "3", "5", "0" }, found.Select(s => (string)Data(s)["updateCount"]).ToArray());
Assert.All(found, s => Assert.Equal("Debug", s.Payload![SignalPayloadKeys.Severity]));
Assert.All(found, s => Assert.Equal("scan", Data(s)["wuPhase"]));
Assert.All(found, s => Assert.False(Data(s).ContainsKey("updateClass")));
Assert.Contains("scan found 3 update(s)", found[0].Payload![SignalPayloadKeys.Message]);
Assert.Equal(2, Counted("scan_found_repeat"));
Assert.Empty(_osActivity); // a scan result is not an update step
}
[Fact]
public void ScanFound_IsCappedPerRun()
{
for (var i = 0; i < WindowsUpdateTracker.MaxScanFoundEmissions + 2; i++)
Emit(_operational, WindowsUpdateTracker.EventId_ScanFound, updateTitle: null, updateCount: i.ToString());
Assert.Equal(WindowsUpdateTracker.MaxScanFoundEmissions, ByType(Constants.EventTypes.WindowsUpdateStarted).Count);
Assert.Equal(2, Counted("scan_found_repeat"));
}
[Fact]
public void ScanFailed_EmitsEachErrorCodeOnce()
{
Emit(_operational, WindowsUpdateTracker.EventId_ScanFailed, updateTitle: null, errorCode: "0x8024401c");
Emit(_operational, WindowsUpdateTracker.EventId_ScanFailed, updateTitle: null, errorCode: "0x8024401c");
Emit(_operational, WindowsUpdateTracker.EventId_ScanFailed, updateTitle: null, errorCode: "0x80072EE2");
Emit(_operational, WindowsUpdateTracker.EventId_ScanFailed, updateTitle: null, errorCode: "0x8024401C");
var failed = ByType(Constants.EventTypes.WindowsUpdateFailed);
Assert.Equal(2, failed.Count);
Assert.All(failed, s => Assert.Equal("Warning", s.Payload![SignalPayloadKeys.Severity]));
Assert.All(failed, s => Assert.Equal("scan", Data(s)["wuPhase"]));
Assert.Equal(2, Counted("scan_failed_repeat"));
Assert.Empty(_osActivity);
}
[Fact]
public void ScanFailed_IsCappedPerRun()
{
for (var i = 0; i < WindowsUpdateTracker.MaxScanFailedEmissions + 2; i++)
Emit(_operational, WindowsUpdateTracker.EventId_ScanFailed, updateTitle: null, errorCode: $"0x8024000{i}");
Assert.Equal(WindowsUpdateTracker.MaxScanFailedEmissions, ByType(Constants.EventTypes.WindowsUpdateFailed).Count);
Assert.Equal(2, Counted("scan_failed_repeat"));
}
// ---------------------------------------------------------------------------
// Classification — only OS updates are listed, the rest is counted
// ---------------------------------------------------------------------------
[Theory]
[InlineData(StoreApp, null, WindowsUpdateTracker.EventId_InstallSuccess, "store_19")]
[InlineData("Some App", "{855e8a7c-ecb4-4ca3-b045-1dfa50104289}", WindowsUpdateTracker.EventId_Downloaded, "store_41")]
[InlineData(DefenderIntelligence, null, WindowsUpdateTracker.EventId_InstallStarted, "defender_43")]
[InlineData("Intel net Driver Update (24.50.0.4)", null, WindowsUpdateTracker.EventId_DownloadStarted, "other_44")]
// A real download failure names no update at all; it must not reach ANALYZE-DEV-004 as
// a failed OS update.
[InlineData("", null, WindowsUpdateTracker.EventId_DownloadFailed, "other_31")]
[InlineData("Some App", "{855e8a7c-ecb4-4ca3-b045-1dfa50104289}", WindowsUpdateTracker.EventId_InstallFailure, "store_20")]
public void NonOsUpdates_AreCountedNotEmitted(string title, string? serviceGuid, int eventId, string expectedKey)
{
var tracker = WindowsUpdateTracker.IsSystemChannelEventId(eventId) ? _system : _operational;
Emit(tracker, eventId, updateTitle: title, serviceGuid: serviceGuid, errorCode: "0x80248007");
Assert.Empty(_sink.Posted);
Assert.Empty(_osActivity);
Assert.Equal(1, Counted(expectedKey));
// The census still sees the channel working.
Assert.Equal(1, tracker.EmittedThisRun);
}
[Fact]
public void CountedRecord_DeliveredTwice_IsCountedOnce()
{
// Live watcher and backfill overlap: the seen-set dedups counted records too.
Emit(_system, WindowsUpdateTracker.EventId_InstallSuccess, updateTitle: StoreApp, recordId: 7);
Emit(_system, WindowsUpdateTracker.EventId_InstallSuccess, updateTitle: StoreApp, recordId: 7);
Assert.Equal(1, Counted("store_19"));
}
[Fact]
public void OsUpdateSteps_AreReportedToTheEngineCallback()
{
var t0 = At;
Emit(_system, WindowsUpdateTracker.EventId_DownloadStarted, at: t0);
Emit(_operational, WindowsUpdateTracker.EventId_Downloaded, at: t0.AddMinutes(4));
Emit(_system, WindowsUpdateTracker.EventId_InstallStarted, at: t0.AddMinutes(5));
Emit(_system, WindowsUpdateTracker.EventId_InstallSuccess, at: t0.AddMinutes(31));
Emit(_system, WindowsUpdateTracker.EventId_InstallFailure, errorCode: "0x800F0922", at: t0.AddMinutes(32));
Emit(_operational, WindowsUpdateTracker.EventId_DownloadFailed, errorCode: "0x80248007", at: t0.AddMinutes(33));
Assert.Equal(
new[]
{
OsUpdateSteps.DownloadStarted, OsUpdateSteps.Downloaded, OsUpdateSteps.InstallStarted,
OsUpdateSteps.Installed, OsUpdateSteps.Failed, OsUpdateSteps.Failed,
},
_osActivity.Select(a => a.Step).ToArray());
Assert.All(_osActivity, a => Assert.Equal(Lcu, a.Update));
Assert.Equal(t0.AddMinutes(31), _osActivity[3].At);
}
[Fact]
public void ThrowingCallback_NeverCostsTheEvent()
{
var tracker = new WindowsUpdateTracker(
"s", "t", new InformationalEventPost(_sink, new VirtualClock(At)),
new AgentLogger(_tmp.Path, AgentLogLevel.Info),
backfillEnabled: false,
channel: WindowsUpdateTracker.SystemChannel,
onOsUpdateActivity: (step, update, at) => throw new InvalidOperationException("boom"));
Emit(tracker, WindowsUpdateTracker.EventId_InstallSuccess);
Assert.Single(ByType(Constants.EventTypes.WindowsUpdateSucceeded));
}
// ---------------------------------------------------------------------------
// Channels and XPath
// ---------------------------------------------------------------------------
[Fact]
public void DefaultEventIds_FollowTheProviderManifest()
{
Assert.Equal(WindowsUpdateTracker.SystemChannel, _system.WatchedChannel);
Assert.Equal(new[] { 19, 20, 43, 44 }, _system.TargetedEventIds.OrderBy(id => id).ToArray());
Assert.Equal(WindowsUpdateTracker.Channel, _operational.WatchedChannel);
Assert.Equal(new[] { 25, 26, 31, 41 }, _operational.TargetedEventIds.OrderBy(id => id).ToArray());
}
[Theory]
[InlineData(16, true)]
[InlineData(19, true)]
[InlineData(20, true)]
[InlineData(24, true)]
[InlineData(27, true)]
[InlineData(28, true)]
[InlineData(32, true)]
[InlineData(33, true)]
[InlineData(43, true)]
[InlineData(44, true)]
[InlineData(212, true)]
[InlineData(218, true)]
[InlineData(25, false)]
[InlineData(26, false)]
[InlineData(31, false)]
[InlineData(41, false)]
[InlineData(15, false)]
[InlineData(219, false)]
public void IsSystemChannelEventId_MatchesTheManifest(int eventId, bool expected)
{
Assert.Equal(expected, WindowsUpdateTracker.IsSystemChannelEventId(eventId));
}
[Fact]
public void BuildXPath_ContainsAllTargetedIds_Ordered()
{
@@ -192,6 +392,113 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
Assert.Equal("*[System[(EventID=19 or EventID=20 or EventID=43 or EventID=44)]]", xpath);
}
[Fact]
public void BuildXPath_SharedSystemLog_FiltersTheProvider_AndTheLookback()
{
// Verified against a real System log: the provider clause keeps the other providers'
// EventIDs 19/20/43/44 out of the shared channel.
var ids = new HashSet<int> { 44, 19, 20, 43 };
Assert.Equal(
"*[System[Provider[@Name='Microsoft-Windows-WindowsUpdateClient'] and (EventID=19 or EventID=20 or EventID=43 or EventID=44)]]",
WindowsUpdateTracker.BuildXPath(ids, WindowsUpdateTracker.ProviderName));
Assert.Equal(
"*[System[Provider[@Name='Microsoft-Windows-WindowsUpdateClient'] and (EventID=19 or EventID=20 or EventID=43 or EventID=44) and TimeCreated[timediff(@SystemTime) <= 3600000]]]",
WindowsUpdateTracker.BuildXPath(ids, WindowsUpdateTracker.ProviderName, lookbackMs: 3600000));
Assert.Equal(
"*[System[(EventID=25 or EventID=26) and TimeCreated[timediff(@SystemTime) <= 60000]]]",
WindowsUpdateTracker.BuildXPath(new HashSet<int> { 26, 25 }, providerName: null, lookbackMs: 60000));
}
// ---------------------------------------------------------------------------
// Watermark
// ---------------------------------------------------------------------------
[Fact]
public void Watermark_PersistsAcrossTrackerInstances()
{
var first = NewTracker(WindowsUpdateTracker.SystemChannel, _tmp.Path);
Emit(first, WindowsUpdateTracker.EventId_InstallSuccess, recordId: 500);
// A fresh tracker (simulating an agent restart) must load the watermark and skip the
// already-emitted record when the OOBE backfill re-reads it.
var second = NewTracker(WindowsUpdateTracker.SystemChannel, _tmp.Path);
second.LoadWatermark();
Emit(second, WindowsUpdateTracker.EventId_InstallSuccess, recordId: 500, isBackfill: true);
Assert.Single(ByType(Constants.EventTypes.WindowsUpdateSucceeded));
}
[Fact]
public void Watermark_IsPerChannel()
{
// RecordIds are per channel: System record 500 and Operational record 500 are two
// different events and must not dedup each other — before or after a restart.
Emit(NewTracker(WindowsUpdateTracker.SystemChannel, _tmp.Path), WindowsUpdateTracker.EventId_InstallSuccess, recordId: 500);
Emit(NewTracker(null, _tmp.Path), WindowsUpdateTracker.EventId_Downloaded, recordId: 500);
Assert.Single(ByType(Constants.EventTypes.WindowsUpdateSucceeded));
Assert.Single(ByType(Constants.EventTypes.WindowsUpdateStarted));
Assert.Equal(500L, ReadWatermark(WindowsUpdateTracker.SystemWatermarkStateFileName));
Assert.Equal(500L, ReadWatermark(WindowsUpdateTracker.WatermarkStateFileName));
var system = NewTracker(WindowsUpdateTracker.SystemChannel, _tmp.Path);
var operational = NewTracker(null, _tmp.Path);
system.LoadWatermark();
operational.LoadWatermark();
Emit(system, WindowsUpdateTracker.EventId_InstallSuccess, recordId: 500, isBackfill: true);
Emit(operational, WindowsUpdateTracker.EventId_Downloaded, recordId: 500, isBackfill: true);
Assert.Single(ByType(Constants.EventTypes.WindowsUpdateSucceeded));
Assert.Single(ByType(Constants.EventTypes.WindowsUpdateStarted));
}
[Fact]
public void CountedRecords_WriteNoStateFile()
{
// A Store/Defender burst must not cost a state-file write per record; only emitted
// records move the persisted watermark.
var tracker = NewTracker(WindowsUpdateTracker.SystemChannel, _tmp.Path);
Emit(tracker, WindowsUpdateTracker.EventId_InstallSuccess, updateTitle: StoreApp, recordId: 600);
Emit(tracker, WindowsUpdateTracker.EventId_InstallStarted, updateTitle: DefenderIntelligence, recordId: 601);
Assert.False(File.Exists(Path.Combine(_tmp.Path, WindowsUpdateTracker.SystemWatermarkStateFileName)));
Emit(tracker, WindowsUpdateTracker.EventId_InstallStarted, recordId: 602);
Assert.Equal(602L, ReadWatermark(WindowsUpdateTracker.SystemWatermarkStateFileName));
}
[Fact]
public void Watermark_PersistsOnlyAfterTheEngineSignalWasHandedOver()
{
// A restart's backfill skips a persisted record, so the record's engine signal must be
// on its way before the watermark reaches the disk — never the other way round.
var path = Path.Combine(_tmp.Path, WindowsUpdateTracker.SystemWatermarkStateFileName);
bool? watermarkOnDiskAtSignal = null;
var tracker = new WindowsUpdateTracker(
sessionId: "sess-wu",
tenantId: "tenant-wu",
post: new InformationalEventPost(_sink, new VirtualClock(At)),
logger: new AgentLogger(_tmp.Path, AgentLogLevel.Info),
backfillEnabled: false,
stateDirectory: _tmp.Path,
channel: WindowsUpdateTracker.SystemChannel,
tally: _tally,
onOsUpdateActivity: (step, update, at) => watermarkOnDiskAtSignal = File.Exists(path));
Emit(tracker, WindowsUpdateTracker.EventId_InstallStarted, recordId: 700);
Assert.False(watermarkOnDiskAtSignal);
Assert.Equal(700L, ReadWatermark(WindowsUpdateTracker.SystemWatermarkStateFileName));
}
private long ReadWatermark(string fileName) =>
JObject.Parse(File.ReadAllText(Path.Combine(_tmp.Path, fileName)))["LastRecordId"]!.Value<long>();
// ---------------------------------------------------------------------------
// Parsing and HRESULT decoding
// ---------------------------------------------------------------------------
[Fact]
public void ParseEventData_ExtractsNamedFields_NamespaceAgnostic()
{
@@ -212,6 +519,24 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
Assert.True(data.ContainsKey("UPDATETITLE"));
}
[Fact]
public void ParseEventData_ReadsTheScanFields()
{
// Event 26 (scan found N) and 25 (scan failed) carry no update title.
const string xml =
"<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'>" +
"<System><EventID>26</EventID></System>" +
"<EventData>" +
"<Data Name='updateCount'>3</Data>" +
"<Data Name='serviceGuid'>{8b24b027-1dee-babb-9a95-3517dfb9c552}</Data>" +
"</EventData></Event>";
var data = WindowsUpdateTracker.ParseEventData(xml);
Assert.Equal("3", data["updateCount"]);
Assert.Equal("{8b24b027-1dee-babb-9a95-3517dfb9c552}", data["serviceGuid"]);
Assert.False(data.ContainsKey("updateTitle"));
}
[Fact]
public void ParseEventData_MalformedXml_ReturnsEmpty_NeverThrows()
{
@@ -303,10 +628,23 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
var data = Data(s);
Assert.Equal("21=1,25=3", data["wuClientCensus"]);
Assert.Equal("200=2", data["updateOrchestratorCensus"]);
Assert.Equal("19,20,43,44", data["targetedEventIds"]);
Assert.Equal("25,26,31,41", data["targetedEventIds"]);
Assert.False(data.ContainsKey("censusTruncated"));
}
[Fact]
public void Census_ListsTheTargetedIdsOfEveryChannel()
{
// The host runs the census on the Operational instance; the evidence must still say
// the System-log install IDs were watched too.
var tracker = BuildCensusTracker(censusEnabled: true, osBuildChanged: true);
tracker.EmitChannelCensusIfBlind(otherChannelsEmitted: 0, otherTargetedEventIds: new[] { 44, 19, 20, 43 });
var s = Assert.Single(ByType(Constants.EventTypes.WindowsUpdateChannelCensus));
Assert.Equal("19,20,25,26,31,41,43,44", Data(s)["targetedEventIds"]);
}
[Fact]
public void Census_BuildUnchanged_EmitsNothing()
{
@@ -320,17 +658,27 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.SystemSignals
[Fact]
public void Census_TargetedEventsWereEmittedThisRun_EmitsNothing()
{
// The watcher captured the update through its normal path — no blind spot to report.
// The watcher captured activity through its normal path — no blind spot to report.
// Any class counts: a counted Store update proves the channel is read as well.
var tracker = BuildCensusTracker(censusEnabled: true, osBuildChanged: true);
tracker.ProcessEvent(WindowsUpdateTracker.EventId_InstallSuccess, 4, recordId: 9, timeCreatedUtc: At,
updateTitle: "KB1", updateGuid: null, updateRevisionNumber: null, errorCode: null,
formattedDescription: null, isBackfill: false);
Emit(tracker, WindowsUpdateTracker.EventId_Downloaded, updateTitle: StoreApp, recordId: 9);
tracker.EmitChannelCensusIfBlind();
Assert.Empty(ByType(Constants.EventTypes.WindowsUpdateChannelCensus));
}
[Fact]
public void Census_AnotherChannelEmitted_EmitsNothing()
{
// The System-log watcher saw the install — the update was not missed.
var tracker = BuildCensusTracker(censusEnabled: true, osBuildChanged: true);
tracker.EmitChannelCensusIfBlind(otherChannelsEmitted: 1);
Assert.Empty(ByType(Constants.EventTypes.WindowsUpdateChannelCensus));
}
[Fact]
public void Census_DisabledByConfig_EmitsNothing()
{
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Shared.Models;
using Xunit;
@@ -49,6 +50,74 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Transport
private static EmergencyReporter Reporter(FakeApiClient client)
=> new EmergencyReporter(client, "session", "tenant", "0.0.0", logger: null);
private static EmergencyReporter Reporter(FakeApiClient client, VirtualClock clock)
=> new EmergencyReporter(client, "session", "tenant", "0.0.0", logger: null, clock: clock);
private static VirtualClock Clock() => new VirtualClock(new DateTime(2026, 10, 9, 10, 0, 0, DateTimeKind.Utc));
[Fact]
public async Task Session_unknown_report_is_not_suppressed_by_the_cooldown_of_an_earlier_report()
{
// Terminal report, sent at most once per process right before the termination — a
// routine report one minute earlier must not cost it. Every other type keeps the cooldown.
var clock = Clock();
var client = new FakeApiClient();
client.Results.Enqueue(true);
client.Results.Enqueue(true);
client.Results.Enqueue(true);
var reporter = Reporter(client, clock);
await reporter.TrySendAsync(AgentErrorType.IngestFailed, "m", httpStatusCode: 503);
clock.Advance(TimeSpan.FromMinutes(1));
await reporter.TrySendAsync(AgentErrorType.TelemetryRejectedSessionUnknown, "m", httpStatusCode: 410);
Assert.Equal(2, client.Calls);
Assert.Equal(AgentErrorType.TelemetryRejectedSessionUnknown, client.Reports[1].ErrorType);
// Control: a third report of another type inside the window is still held back.
clock.Advance(TimeSpan.FromMinutes(1));
await reporter.TrySendAsync(AgentErrorType.ConfigFetchFailed, "m");
Assert.Equal(2, client.Calls);
}
[Fact]
public async Task Second_session_unknown_report_within_the_window_is_still_suppressed()
{
var clock = Clock();
var client = new FakeApiClient();
client.Results.Enqueue(true);
client.Results.Enqueue(true);
var reporter = Reporter(client, clock);
await reporter.TrySendAsync(AgentErrorType.TelemetryRejectedSessionUnknown, "m", httpStatusCode: 410);
clock.Advance(TimeSpan.FromMinutes(1));
await reporter.TrySendAsync(AgentErrorType.TelemetryRejectedSessionUnknown, "m", httpStatusCode: 410);
Assert.Equal(1, client.Calls);
}
[Fact]
public async Task Session_unknown_report_still_counts_against_the_per_session_maximum()
{
// Five delivered reports (ten minutes apart, distinct keys) exhaust the budget; the
// exemption waives only the cooldown, not the maximum.
var clock = Clock();
var client = new FakeApiClient();
for (var i = 0; i < 6; i++) client.Results.Enqueue(true);
var reporter = Reporter(client, clock);
foreach (var status in new[] { 500, 502, 503, 504, 429 })
{
await reporter.TrySendAsync(AgentErrorType.IngestFailed, "m", httpStatusCode: status);
clock.Advance(TimeSpan.FromMinutes(10));
}
Assert.Equal(5, client.Calls);
await reporter.TrySendAsync(AgentErrorType.TelemetryRejectedSessionUnknown, "m", httpStatusCode: 410);
Assert.Equal(5, client.Calls);
}
[Fact]
public async Task Failed_send_rolls_back_reservation_so_a_later_call_retries()
{
@@ -142,6 +211,20 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Transport
Assert.Equal(2, client.Calls);
}
[Fact]
public async Task Session_unknown_report_carries_error_type_and_http_410()
{
var client = new FakeApiClient();
client.Results.Enqueue(true);
var reporter = Reporter(client);
await reporter.TrySendAsync(AgentErrorType.TelemetryRejectedSessionUnknown, "m", httpStatusCode: 410);
var report = Assert.Single(client.Reports);
Assert.Equal(AgentErrorType.TelemetryRejectedSessionUnknown, report.ErrorType);
Assert.Equal(410, report.HttpStatusCode);
}
[Fact]
public async Task Session_age_is_carried_in_the_report_and_absent_by_default()
{
@@ -153,6 +153,62 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
Assert.Equal(0, callCount);
}
[Fact]
public void Stopped_waiting_fires_callback_once_with_the_stage_still_non_terminal()
{
// D-310: the engine stopped waiting for the user without a verdict. The stage stays
// non-terminal, yet the agent must end the session — the callback carries the state.
using var rig = new Rig();
DecisionState? captured = null;
var callCount = 0;
var sut = rig.Build(DecisionState.CreateInitial("S1", "T1"),
onTerminal: state =>
{
Interlocked.Increment(ref callCount);
captured = state;
});
var builder = new DecisionStateBuilder(sut.CurrentState)
{
Stage = SessionStage.EspAccountSetup,
StepIndex = sut.CurrentState.StepIndex + 1,
StoppedWaitingReason = new SignalFact<string>(StoppedWaitingReasons.OobeUpdateNoSignIn, 1),
};
var stopped = builder.Build();
var step = new DecisionStep(stopped, new DecisionTransition(
stepIndex: stopped.StepIndex, sessionTraceOrdinal: 1, signalOrdinalRef: 1, occurredAtUtc: At,
trigger: "DeadlineFired:advisory_completion:OobeUpdateSignInWaitExpired",
fromStage: SessionStage.SessionStarted, toStage: SessionStage.EspAccountSetup,
taken: true, deadEndReason: null, reducerVersion: "2.0.0.0"), Array.Empty<DecisionEffect>());
sut.ApplyStep(step, Sig(1, At));
// A follow-up step must not fire it again.
sut.ApplyStep(StepToStage(sut.CurrentState, SessionStage.EspAccountSetup, signalOrdinal: 2, at: At.AddSeconds(1)),
Sig(2, At.AddSeconds(1)));
Assert.Equal(1, callCount);
Assert.Equal(SessionStage.EspAccountSetup, captured!.Stage);
Assert.Equal(StoppedWaitingReasons.OobeUpdateNoSignIn, captured.StoppedWaitingReason!.Value);
}
[Fact]
public void Stopped_waiting_on_load_does_not_refire_the_callback()
{
// The orchestrator ends a recovered stopped-waiting session itself at start.
using var rig = new Rig();
var callCount = 0;
var recovered = new DecisionStateBuilder(DecisionState.CreateInitial("S1", "T1"))
{
Stage = SessionStage.EspAccountSetup,
StoppedWaitingReason = new SignalFact<string>(StoppedWaitingReasons.OobeUpdateNoSignIn, 7),
}.Build();
var sut = rig.Build(recovered, onTerminal: _ => Interlocked.Increment(ref callCount));
sut.ApplyStep(StepToStage(sut.CurrentState, SessionStage.EspAccountSetup, signalOrdinal: 8, at: At), Sig(8, At));
Assert.Equal(0, callCount);
}
[Fact]
public void Terminal_callback_null_is_supported()
{
@@ -145,7 +145,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
[Fact]
public void Ime_app_state_callback_is_chained_into_the_user_apps_settled_recheck()
{
// sits-d Cloud-PC fix (2026-08-19): the AccountSetup synthesis is re-evaluated on every
// Cloud-PC fix (2026-08-19): the AccountSetup synthesis is re-evaluated on every
// terminal IME app transition, which only works while the factory keeps this chain
// wired. It is five lines that a refactor can drop silently and whose absence costs a
// whole class of sessions their completion — so pin it here.
@@ -0,0 +1,132 @@
#nullable enable
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Orchestration;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Transport;
using AutopilotMonitor.Agent.V2.Core.Transport.Telemetry;
using AutopilotMonitor.Shared.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
{
/// <summary>
/// The orchestrator side of an intercepted upload: one <c>telemetry_upload_blocked</c> with the
/// evidence (queued behind the intercepted batch, never uploaded while paused), and the one
/// terminal attempt — requested by <c>DrainSpool</c> or made by <see cref="EnrollmentOrchestrator.Stop"/>.
/// </summary>
[Collection("SerialThreading")]
public sealed class EnrollmentOrchestratorInterceptionTests
{
private const int WaitMs = 10_000;
private static TelemetryItemDraft Draft() =>
new TelemetryItemDraft(
kind: TelemetryItemKind.Event,
partitionKey: "T1_S1",
rowKey: "0000000001",
payloadJson: "{}",
isSessionScoped: true,
requiresImmediateFlush: false);
// The signal and the decision transition of the same post mention the event type too.
private static bool IsBlockedEvent(TelemetryItem item) =>
item.Kind == TelemetryItemKind.Event
&& item.PayloadJson.Contains(AutopilotMonitor.Shared.Constants.EventTypes.TelemetryUploadBlocked);
private static TelemetryItem? FindBlockedEvent(EnrollmentOrchestrator sut) =>
sut.SpoolForTests!.Peek(1000).FirstOrDefault(IsBlockedEvent);
// Dates stay strings: the wire format of firstBlockedAtUtc is what is under test.
private static JObject Parse(string json)
{
using var reader = new JsonTextReader(new StringReader(json)) { DateParseHandling = DateParseHandling.None };
return JObject.Load(reader);
}
/// <summary>Starts the orchestrator and drives the first upload into an interception.</summary>
private static async Task<EnrollmentOrchestrator> StartInterceptedAsync(EnrollmentOrchestratorRig rig)
{
rig.Uploader.QueueIntercepted(5);
var sut = rig.Build();
sut.Start();
sut.SpoolForTests!.Enqueue(Draft());
await sut.DrainAsync();
Assert.True(sut.Transport.IsInterceptionPaused);
return sut;
}
[Fact]
public async Task Interception_queues_one_telemetry_upload_blocked_with_the_evidence_and_wakeups_stay_offline()
{
using var rig = new EnrollmentOrchestratorRig();
var sut = await StartInterceptedAsync(rig);
try
{
TelemetryItem? blocked = null;
Assert.True(SpinWait.SpinUntil(() => (blocked = FindBlockedEvent(sut)) != null, WaitMs),
"telemetry_upload_blocked never reached the spool.");
var calls = rig.Uploader.CallCount;
var data = (JObject)Parse(blocked!.PayloadJson)["Data"]!;
Assert.Equal(
new[] { "firstBlockedAtUtc", "operation", "pageTitle", "reason", "serverCertIssuer", "statusCode" },
data.Properties().Select(p => p.Name).OrderBy(n => n, StringComparer.Ordinal));
Assert.Equal("client_certificate_required", (string?)data["reason"]);
Assert.Equal("agent/telemetry", (string?)data["operation"]);
Assert.Equal(JTokenType.Integer, data["statusCode"]!.Type);
Assert.Equal(403, (int)data["statusCode"]!);
Assert.Equal(InterceptionFixtures.PlatformPageTitle, (string?)data["pageTitle"]);
Assert.Equal(InterceptionFixtures.InspectionIssuer, (string?)data["serverCertIssuer"]);
Assert.Equal(
EnrollmentOrchestratorRig.DefaultClockStart.ToString("o", CultureInfo.InvariantCulture),
(string?)data["firstBlockedAtUtc"]);
// The marker was an immediate-upload item; neither its wakeup nor a further drain
// touches the network while the pause holds.
var drain = await sut.DrainAsync();
Assert.Contains("upload intercepted", drain.LastErrorReason);
Assert.Equal(calls, rig.Uploader.CallCount);
Assert.Single(sut.SpoolForTests!.Peek(1000), IsBlockedEvent);
}
finally
{
sut.Stop();
}
}
[Fact]
public async Task Terminal_attempt_requested_by_the_termination_runs_once_and_Stop_adds_none()
{
using var rig = new EnrollmentOrchestratorRig();
var sut = await StartInterceptedAsync(rig);
Assert.True(SpinWait.SpinUntil(() => FindBlockedEvent(sut) != null, WaitMs));
var calls = rig.Uploader.CallCount;
sut.RequestTerminalUploadAttempt();
Assert.True(SpinWait.SpinUntil(() => rig.Uploader.CallCount == calls + 1, WaitMs),
"the woken drain loop did not make the terminal attempt.");
sut.Stop();
Assert.Equal(calls + 1, rig.Uploader.CallCount);
}
[Fact]
public async Task Stop_makes_the_one_terminal_attempt_when_the_termination_did_not()
{
using var rig = new EnrollmentOrchestratorRig();
var sut = await StartInterceptedAsync(rig);
var calls = rig.Uploader.CallCount;
sut.Stop();
Assert.Equal(calls + 1, rig.Uploader.CallCount);
}
}
}
@@ -2,6 +2,7 @@ using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
@@ -10,6 +11,7 @@ using AutopilotMonitor.Agent.V2.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.V2.Core.Orchestration;
using AutopilotMonitor.Agent.V2.Core.Security;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Transport;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
@@ -96,6 +98,35 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
Assert.Equal(TimeSpan.FromSeconds(5), client.Timeout);
}
[Fact]
public void CreateHandler_without_observer_keeps_the_default_validation()
{
using var tmp = new TempDirectory();
var logger = new AgentLogger(tmp.Path, AgentLogLevel.Info);
using var cert = CreateSelfSignedCert();
using var handler = MtlsHttpClientFactory.CreateHandler(new FakeCertificateResolver(cert), logger);
Assert.Null(handler.ServerCertificateCustomValidationCallback);
}
[Fact]
public void CreateHandler_with_observer_installs_an_observing_callback_that_never_weakens_validation()
{
using var tmp = new TempDirectory();
var logger = new AgentLogger(tmp.Path, AgentLogLevel.Info);
using var cert = CreateSelfSignedCert();
var observer = new ServerCertificateObserver();
using var handler = MtlsHttpClientFactory.CreateHandler(new FakeCertificateResolver(cert), logger, observer);
var callback = handler.ServerCertificateCustomValidationCallback;
Assert.NotNull(callback);
Assert.False(callback!(null!, cert, null!, SslPolicyErrors.RemoteCertificateChainErrors));
Assert.True(callback(null!, cert, null!, SslPolicyErrors.None));
Assert.Equal(cert.Issuer, observer.LastIssuer);
}
[Fact]
public void CreateHandler_throws_when_resolver_returns_null()
{
@@ -161,7 +192,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
/// we scan the type hierarchy for the first <see cref="HttpMessageHandler"/>-typed
/// instance field to stay version-tolerant.
/// </summary>
private static HttpMessageHandler GetPipelineHead(HttpClient client)
internal static HttpMessageHandler GetPipelineHead(HttpClient client)
{
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
for (var t = (Type?)client.GetType(); t != null; t = t.BaseType)
@@ -174,6 +174,40 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
sut.Stop();
}
[Fact]
public void Recovered_stopped_waiting_state_ends_the_session_at_start()
{
// D-310: a crash between "the engine stopped waiting" and the termination must not
// leave the session running — the restart ends it the same way, with the engine's reason.
using var rig = new Rig();
Directory.CreateDirectory(rig.StateDir);
var stoppedState = DecisionState.CreateInitial("S1", "T1")
.ToBuilder()
.WithStage(SessionStage.EspAccountSetup)
.WithStepIndex(9)
.Build()
.ToBuilder();
stoppedState.StoppedWaitingReason = new SignalFact<string>(StoppedWaitingReasons.OobeUpdateNoSignIn, 42);
new SnapshotPersistence(Path.Combine(rig.StateDir, "snapshot.json")).Save(stoppedState.Build());
var sut = rig.Build();
using var fired = new ManualResetEventSlim(false);
EnrollmentTerminatedEventArgs? captured = null;
sut.Terminated += (_, e) => { captured = e; fired.Set(); };
sut.Start();
Assert.True(fired.Wait(5000), "Terminated did not fire for the recovered stopped-waiting state.");
Assert.Equal(EnrollmentTerminationReason.StoppedWaiting, captured!.Reason);
Assert.Equal(EnrollmentTerminationOutcome.TimedOut, captured.Outcome);
Assert.Equal(StoppedWaitingReasons.OobeUpdateNoSignIn, captured.StopReason);
Assert.Equal(SessionStage.EspAccountSetup.ToString(), captured.StageName);
Assert.Contains("60 minutes", captured.Details);
sut.Stop();
}
// ================================================================= Sonderfall 2: Segment-Quarantine
[Fact]
@@ -77,7 +77,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
using var ing = BuildIngress(clock);
var api = new FakeKeepAwakeApi();
var controller = new KeepAwakeController(NewLogger(), api);
var host = new UserEspKeepAwakeHost("S1", "T1", ing, clock, NewLogger(), controller: controller, espTimeoutProvider: () => null);
var host = new UserEspKeepAwakeHost(ing, clock, NewLogger(), controller: controller, espTimeoutProvider: () => null);
ing.Start();
host.Start();
@@ -98,7 +98,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
using var ing = BuildIngress(clock);
var api = new FakeKeepAwakeApi();
var controller = new KeepAwakeController(NewLogger(), api);
var host = new UserEspKeepAwakeHost("S1", "T1", ing, clock, NewLogger(), controller: controller, espTimeoutProvider: () => null);
var host = new UserEspKeepAwakeHost(ing, clock, NewLogger(), controller: controller, espTimeoutProvider: () => null);
ing.Start();
host.Start();
@@ -127,7 +127,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
var api = new FakeKeepAwakeApi();
var controller = new KeepAwakeController(NewLogger(), api);
var progress = false;
var host = new UserEspKeepAwakeHost("S1", "T1", ing, clock, NewLogger(), controller: controller,
var host = new UserEspKeepAwakeHost(ing, clock, NewLogger(), controller: controller,
espTimeoutProvider: () => null, accountSetupProgressProbe: () => Volatile.Read(ref progress));
ing.Start();
@@ -156,7 +156,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
var api = new FakeKeepAwakeApi();
var controller = new KeepAwakeController(NewLogger(), api);
var progress = false;
var host = new UserEspKeepAwakeHost("S1", "T1", ing, clock, NewLogger(), controller: controller,
var host = new UserEspKeepAwakeHost(ing, clock, NewLogger(), controller: controller,
espTimeoutProvider: () => null, accountSetupProgressProbe: () => Volatile.Read(ref progress));
ing.Start();
@@ -186,7 +186,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
using var ing = BuildIngress(clock);
var api = new FakeKeepAwakeApi();
var controller = new KeepAwakeController(NewLogger(), api);
var host = new UserEspKeepAwakeHost("S1", "T1", ing, clock, NewLogger(), controller: controller,
var host = new UserEspKeepAwakeHost(ing, clock, NewLogger(), controller: controller,
espTimeoutProvider: () => null, accountSetupProgressProbe: () => true);
ing.Start();
@@ -212,7 +212,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
using var ing = BuildIngress(clock);
var api = new FakeKeepAwakeApi();
var controller = new KeepAwakeController(NewLogger(), api);
var host = new UserEspKeepAwakeHost("S1", "T1", ing, clock, NewLogger(), controller: controller,
var host = new UserEspKeepAwakeHost(ing, clock, NewLogger(), controller: controller,
espTimeoutProvider: () => null, accountSetupProgressProbe: () => true);
ing.Start();
@@ -236,7 +236,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
using var ing = BuildIngress(clock);
var api = new FakeKeepAwakeApi();
var controller = new KeepAwakeController(NewLogger(), api);
var host = new UserEspKeepAwakeHost("S1", "T1", ing, clock, NewLogger(), controller: controller, espTimeoutProvider: () => null);
var host = new UserEspKeepAwakeHost(ing, clock, NewLogger(), controller: controller, espTimeoutProvider: () => null);
ing.Start();
host.Start();
@@ -259,7 +259,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
using var ing = BuildIngress(clock);
var api = new FakeKeepAwakeApi();
var controller = new KeepAwakeController(NewLogger(), api);
var host = new UserEspKeepAwakeHost("S1", "T1", ing, clock, NewLogger(), controller: controller, espTimeoutProvider: () => null);
var host = new UserEspKeepAwakeHost(ing, clock, NewLogger(), controller: controller, espTimeoutProvider: () => null);
ing.Start();
host.Start();
@@ -282,7 +282,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
var api = new FakeKeepAwakeApi();
var controller = new KeepAwakeController(NewLogger(), api);
var host = new UserEspKeepAwakeHost(
"S1", "T1", ing, clock, NewLogger(),
ing, clock, NewLogger(),
controller: controller,
safetyCapOverride: TimeSpan.FromMilliseconds(75),
espTimeoutProvider: () => null);
@@ -0,0 +1,209 @@
#nullable enable
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.Tests.Harness;
using AutopilotMonitor.DecisionCore.Engine;
using AutopilotMonitor.DecisionCore.Signals;
using AutopilotMonitor.DecisionCore.State;
using AutopilotMonitor.Shared;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Orchestration
{
/// <summary>
/// WindowsUpdateWatcherHost (D-310) — routes every configured WU EventID to the channel the
/// provider manifest writes it to, reports counted activity once, and feeds Windows/.NET update
/// steps to the decision engine unless <c>OobeUpdatePhaseMode</c> is Off. The event-log
/// watchers themselves are not armed here; the trackers' ProcessEvent seams drive the host.
/// </summary>
public sealed class WindowsUpdateWatcherHostTests : IDisposable
{
private static readonly DateTime At = new DateTime(2026, 10, 1, 9, 30, 0, DateTimeKind.Utc);
private const string Lcu = "2026-09 Cumulative Update for Windows 11, version 25H2 for x64-based Systems (KB5099999)";
private readonly TempDirectory _tmp = new TempDirectory();
private readonly FakeSignalIngressSink _sink = new FakeSignalIngressSink();
private long _nextRecordId = 1;
public void Dispose() => _tmp.Dispose();
private WindowsUpdateWatcherHost Build(bool osUpdateSignalsEnabled, bool servicingWatcherEnabled = true) =>
new WindowsUpdateWatcherHost(
sessionId: "sess-wuhost",
tenantId: "tenant-wuhost",
logger: new AgentLogger(_tmp.Path, AgentLogLevel.Info),
ingress: _sink,
clock: new VirtualClock(At),
targetedEventIds: new[] { 19, 20, 43, 44 },
backfillLookbackMinutes: 0,
stateDirectory: _tmp.Path,
operationalEventIds: new[] { 25, 26, 31, 41 },
servicingWatcherEnabled: servicingWatcherEnabled,
osUpdateSignalsEnabled: osUpdateSignalsEnabled);
private void Wu(WindowsUpdateTracker tracker, int eventId, string title, DateTime at) =>
tracker.ProcessEvent(eventId, 4, _nextRecordId++, at, title, updateGuid: null, updateRevisionNumber: null,
errorCode: null, formattedDescription: null, isBackfill: false);
private void Cbs(ServicingTracker tracker, int eventId, string client, DateTime at) =>
tracker.ProcessEvent(eventId, _nextRecordId++, at, "KB5099999", initialState: null, targetState: "Installed",
errorCode: "0x0", client: client, isBackfill: false);
private IReadOnlyList<FakeSignalIngressSink.PostedSignal> OsUpdateActivity() =>
_sink.Posted.Where(p => p.Kind == DecisionSignalKind.OsUpdateActivity).ToList();
private IReadOnlyList<FakeSignalIngressSink.PostedSignal> ByType(string eventType) =>
_sink.Posted.Where(p =>
p.Kind == DecisionSignalKind.InformationalEvent
&& p.Payload != null
&& p.Payload.TryGetValue(SignalPayloadKeys.EventType, out var et)
&& et == eventType).ToList();
// ---------------------------------------------------------------- routing ----
[Fact]
public void RouteEventIds_Defaults_SplitByManifest()
{
var (system, operational) = WindowsUpdateWatcherHost.RouteEventIds(new[] { 19, 20, 43, 44 }, new[] { 25, 26, 31, 41 });
Assert.Equal(new[] { 19, 20, 43, 44 }, system);
Assert.Equal(new[] { 25, 26, 31, 41 }, operational);
}
[Fact]
public void RouteEventIds_MisplacedIds_EndUpOnTheirRealChannel()
{
// An old config that lists 41 under the targeted IDs, or 20 under the operational
// ones, must still watch the channel that carries them.
var (system, operational) = WindowsUpdateWatcherHost.RouteEventIds(new[] { 41, 19 }, new[] { 20, 41 });
Assert.Equal(new[] { 19, 20 }, system);
Assert.Equal(new[] { 41 }, operational);
}
[Fact]
public void RouteEventIds_NothingConfigured_WatchesNothing()
{
var (system, operational) = WindowsUpdateWatcherHost.RouteEventIds(null, Array.Empty<int>());
Assert.Empty(system);
Assert.Empty(operational);
}
[Fact]
public void Host_BuildsOneTrackerPerChannel_AndTheServicingTracker()
{
var host = Build(osUpdateSignalsEnabled: false);
Assert.Equal(WindowsUpdateTracker.SystemChannel, host.SystemTrackerForTest!.WatchedChannel);
Assert.Equal(new[] { 19, 20, 43, 44 }, host.SystemTrackerForTest.TargetedEventIds.OrderBy(id => id).ToArray());
Assert.Equal(WindowsUpdateTracker.Channel, host.OperationalTrackerForTest!.WatchedChannel);
Assert.Equal(new[] { 25, 26, 31, 41 }, host.OperationalTrackerForTest.TargetedEventIds.OrderBy(id => id).ToArray());
Assert.NotNull(host.ServicingTrackerForTest);
}
[Fact]
public void ServicingWatcherDisabled_BuildsNoServicingTracker()
{
Assert.Null(Build(osUpdateSignalsEnabled: false, servicingWatcherEnabled: false).ServicingTrackerForTest);
}
// ------------------------------------------------- engine signal behind flag ----
[Fact]
public void OsUpdateSignalsDisabled_ListsTheUpdate_ButPostsNoEngineSignal()
{
var host = Build(osUpdateSignalsEnabled: false);
Wu(host.SystemTrackerForTest!, WindowsUpdateTracker.EventId_InstallStarted, Lcu, At);
Cbs(host.ServicingTrackerForTest!, ServicingTracker.EventId_RebootRequired, "UpdateAgentLCU", At.AddMinutes(20));
Assert.Single(ByType(Constants.EventTypes.WindowsUpdateStarted));
Assert.Single(ByType(Constants.EventTypes.WindowsUpdateServicing));
Assert.Empty(OsUpdateActivity());
}
[Fact]
public void OsUpdateSignalsEnabled_PostsOsUpdateActivity_FromBothSources()
{
var host = Build(osUpdateSignalsEnabled: true);
var wuAt = DateTime.SpecifyKind(At, DateTimeKind.Unspecified);
Wu(host.SystemTrackerForTest!, WindowsUpdateTracker.EventId_InstallStarted, Lcu, wuAt);
Cbs(host.ServicingTrackerForTest!, ServicingTracker.EventId_RebootRequired, "UpdateAgentLCU", At.AddMinutes(20));
var signals = OsUpdateActivity();
Assert.Equal(2, signals.Count);
var wu = signals[0];
Assert.Equal(OsUpdateSteps.InstallStarted, wu.Payload![OsUpdatePayloadKeys.Step]);
Assert.Equal("wu", wu.Payload![OsUpdatePayloadKeys.Source]);
Assert.Equal(Lcu, wu.Payload![OsUpdatePayloadKeys.Update]);
Assert.Equal("WindowsUpdateWatcher", wu.SourceOrigin);
Assert.Equal(EvidenceKind.Raw, wu.Evidence.Kind);
Assert.Equal("os-update-activity:wu", wu.Evidence.Identifier);
Assert.Equal(At, wu.OccurredAtUtc);
Assert.Equal(DateTimeKind.Utc, wu.OccurredAtUtc.Kind);
Assert.Equal(1, wu.KindSchemaVersion);
var cbs = signals[1];
Assert.Equal(OsUpdateSteps.RebootRequired, cbs.Payload![OsUpdatePayloadKeys.Step]);
Assert.Equal("servicing", cbs.Payload![OsUpdatePayloadKeys.Source]);
Assert.Equal("KB5099999", cbs.Payload![OsUpdatePayloadKeys.Update]);
Assert.Equal("ServicingWatcher", cbs.SourceOrigin);
Assert.Equal("os-update-activity:servicing", cbs.Evidence.Identifier);
Assert.Equal(At.AddMinutes(20), cbs.OccurredAtUtc);
}
[Fact]
public void OsUpdateSignalsEnabled_NonOsActivity_NeverReachesTheEngine()
{
var host = Build(osUpdateSignalsEnabled: true);
Wu(host.SystemTrackerForTest!, WindowsUpdateTracker.EventId_InstallSuccess, "9NSTH9KHZDLQ-Microsoft.UI.Xaml.2.8", At);
Wu(host.OperationalTrackerForTest!, WindowsUpdateTracker.EventId_Downloaded,
"Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.459.505.0) - Current Channel (Broad)", At);
Cbs(host.ServicingTrackerForTest!, ServicingTracker.EventId_StateReached, "CbsTask", At);
Assert.Empty(OsUpdateActivity());
Assert.Empty(_sink.Posted);
}
// ------------------------------------------------------- activity summary ----
[Fact]
public void ActivitySummary_IsEmittedOnce_WhenTheHostStops()
{
var host = Build(osUpdateSignalsEnabled: false);
Wu(host.SystemTrackerForTest!, WindowsUpdateTracker.EventId_InstallSuccess, "9NSTH9KHZDLQ-Microsoft.UI.Xaml.2.8", At);
Wu(host.SystemTrackerForTest!, WindowsUpdateTracker.EventId_InstallSuccess, "9NBLGGH4NNS1-Microsoft.DesktopAppInstaller", At);
Cbs(host.ServicingTrackerForTest!, ServicingTracker.EventId_InitiatingChanges, "CbsTask", At);
// The termination path stops the host explicitly and disposes it afterwards.
host.Stop();
host.Dispose();
var summary = Assert.Single(ByType(Constants.EventTypes.WindowsUpdateActivitySummary));
Assert.Equal("Debug", summary.Payload![SignalPayloadKeys.Severity]);
Assert.Contains("3 event(s)", summary.Payload![SignalPayloadKeys.Message]);
var data = (IReadOnlyDictionary<string, object>)summary.TypedPayload!;
Assert.Equal(2, data["store_19"]);
Assert.Equal(1, data["servicing_other_1"]);
}
[Fact]
public void ActivitySummary_NothingCounted_EmitsNothing()
{
var host = Build(osUpdateSignalsEnabled: false);
Wu(host.SystemTrackerForTest!, WindowsUpdateTracker.EventId_InstallSuccess, Lcu, At);
host.Stop();
Assert.Empty(ByType(Constants.EventTypes.WindowsUpdateActivitySummary));
}
}
}
@@ -11,7 +11,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
{
/// <summary>
/// Contract tests for <see cref="AgentBootstrap.RetrySelfUpdateAfterEnrollment"/> —
/// the post-cert one-shot self-update retry (field case sits-d.cloud, 2026-08-09):
/// the post-cert one-shot self-update retry (field case 2026-08-09):
/// guard on <see cref="SelfUpdater.LastVersionCheckSucceeded"/>, the relaxed
/// timeouts/trigger it passes to the updater, and the swallow-everything exception
/// contract (bootstrap must continue on any updater failure). Uses the
@@ -44,7 +44,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
// from a real backend). We exercise the bundle-shape contract here.
var rcs = new AutopilotMonitor.Agent.V2.Core.Configuration.RemoteConfigService(
auth.BackendApiClient, "tenant-x", logger,
auth.EmergencyReporter, auth.DistressReporter, auth.AuthFailureTracker);
auth.EmergencyReporter, auth.AuthFailureTracker);
var rc = new AgentConfigResponse();
var merge = new RemoteConfigMergeResult();
@@ -1,7 +1,13 @@
using System.IO;
using System.Net.Http;
using System.Net.Security;
using AutopilotMonitor.Agent.V2.Core.Configuration;
using AutopilotMonitor.Agent.V2.Core.Logging;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
using AutopilotMonitor.Agent.V2.Core.Tests.Transport;
using AutopilotMonitor.Agent.V2.Core.Transport;
using AutopilotMonitor.Agent.V2.Runtime;
using Xunit;
@@ -68,6 +74,43 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
Assert.False(bundle.HasClientCertificate);
}
[Fact]
public void BuildAuthClients_carries_the_server_certificate_observer_for_both_stacks()
{
using var tmp = new TempDirectory();
var bundle = BackendClientFactory.BuildAuthClients(MinimalBootstrapTokenConfig(), agentVersion: "1.2.3.4", logger: NewLogger(tmp.Path));
Assert.NotNull(bundle.ServerCertificates);
}
[Fact]
public void BuildBackendApiHttpClient_installs_the_observing_callback_on_the_inner_handler()
{
var observer = new ServerCertificateObserver();
using var cert = ServerCertificateObserverTests.SelfSigned("CN=Example Inspection CA");
using var client = BackendClientFactory.BuildBackendApiHttpClient(new NetworkMetrics(), clientCertificate: null, agentVersion: "1.2.3.4", observer);
var inner = InnerHandler(client);
var callback = inner.ServerCertificateCustomValidationCallback;
Assert.NotNull(callback);
Assert.False(callback!(null!, cert, null!, SslPolicyErrors.RemoteCertificateNameMismatch));
Assert.True(callback(null!, cert, null!, SslPolicyErrors.None));
Assert.Equal(cert.Issuer, observer.LastIssuer);
}
[Fact]
public void BuildBackendApiHttpClient_without_observer_keeps_the_default_validation()
{
using var client = BackendClientFactory.BuildBackendApiHttpClient(new NetworkMetrics(), clientCertificate: null, agentVersion: "1.2.3.4");
Assert.Null(InnerHandler(client).ServerCertificateCustomValidationCallback);
}
// Pipeline: NetworkMetricsRecordingHandler → HttpClientHandler.
private static HttpClientHandler InnerHandler(HttpClient client) =>
(HttpClientHandler)((DelegatingHandler)MtlsHttpClientFactoryTests.GetPipelineHead(client)).InnerHandler;
// =========================================================== TelemetryClientResult
[Fact]
@@ -122,20 +122,21 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
// ============================================================ failed-registration record
[Fact]
public void RecordRegistrationRun_records_a_failed_run_with_its_window_and_bounded_error()
public void RecordFailedRun_records_a_failed_run_with_its_window_and_bounded_error()
{
using var tmp = new TempDirectory();
var persistence = new SessionIdPersistence(tmp.Path);
var started = new DateTime(2026, 8, 20, 9, 0, 0, DateTimeKind.Utc);
BackendSessionRegistration.RecordRegistrationRun(
var returned = BackendSessionRegistration.RecordFailedRun(
persistence, SessionRegistrationResult.Failed(new string('e', 1000)),
started, started.AddSeconds(1204.46), linkUpAtStart: false, linkUpAtEnd: true,
configFetchOutcome: "UsedDefaults", logger: NewLogger(tmp.Path));
configFetchOutcome: "UsedDefaults", networkFingerprint: null, logger: NewLogger(tmp.Path));
var record = persistence.LoadRegistrationFailure();
Assert.NotNull(record);
Assert.Equal(1, record!.FailedRuns);
Assert.Equal(0, record.InterceptedRuns);
Assert.Equal("Failed", record.Outcome);
Assert.Equal(BackendSessionRegistration.MaxRecordedErrorLength, record.LastError!.Length);
Assert.False(record.NetworkLinkUpAtStart);
@@ -143,33 +144,34 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
Assert.Equal(1204.5, record.AttemptWindowSeconds);
Assert.Equal("UsedDefaults", record.ConfigFetchOutcome);
Assert.Equal(started.AddSeconds(1204.46), record.LastFailedAtUtc);
// The caller decides on the record as written, including this run.
Assert.Equal(1, returned!.FailedRuns);
}
[Fact]
public void RecordRegistrationRun_records_auth_failures_and_clears_on_success()
public void RecordFailedRun_records_auth_failures_and_a_success_clears_them()
{
using var tmp = new TempDirectory();
var persistence = new SessionIdPersistence(tmp.Path);
var at = new DateTime(2026, 8, 20, 9, 0, 0, DateTimeKind.Utc);
BackendSessionRegistration.RecordRegistrationRun(
BackendSessionRegistration.RecordFailedRun(
persistence, SessionRegistrationResult.AuthFailed(401, "unauthorized"),
at, at.AddSeconds(3), true, true, "Succeeded", NewLogger(tmp.Path));
at, at.AddSeconds(3), true, true, "Succeeded", null, NewLogger(tmp.Path));
Assert.Equal("AuthFailed", persistence.LoadRegistrationFailure()!.Outcome);
BackendSessionRegistration.RecordRegistrationRun(
persistence, SessionRegistrationResult.Succeeded(new RegisterSessionResponse { Success = true }),
at, at.AddSeconds(1), true, true, "Succeeded", NewLogger(tmp.Path));
BackendSessionRegistration.ClearAfterSuccess(persistence, () => null, NewLogger(tmp.Path));
Assert.Null(persistence.LoadRegistrationFailure());
}
[Fact]
public void RecordRegistrationRun_without_persistence_is_a_no_op()
public void Recording_without_persistence_is_a_no_op()
{
using var tmp = new TempDirectory();
BackendSessionRegistration.RecordRegistrationRun(
Assert.Null(BackendSessionRegistration.RecordFailedRun(
null, SessionRegistrationResult.Failed("x"), DateTime.UtcNow, DateTime.UtcNow,
null, null, null, NewLogger(tmp.Path));
null, null, null, null, NewLogger(tmp.Path)));
Assert.Null(BackendSessionRegistration.ClearAfterSuccess(null, () => null, NewLogger(tmp.Path)));
}
// ============================================================ emergency-break message
@@ -0,0 +1,477 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
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.Transport;
using AutopilotMonitor.Agent.V2.Core.Orchestration;
using AutopilotMonitor.Agent.V2.Core.Security;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
using AutopilotMonitor.Agent.V2.Core.Tests.Transport;
using AutopilotMonitor.Agent.V2.Runtime;
using AutopilotMonitor.DecisionCore.Engine;
using AutopilotMonitor.Shared.Models;
using Newtonsoft.Json;
using Xunit;
using ProgramAlias = AutopilotMonitor.Agent.V2.Program;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
{
/// <summary>
/// A registration that something on the network path answered at process start (client
/// certificate interception plan, K4/K5): a buffer of further boots, then the emergency break's
/// cleanup; the pending ClientCertificateNotReceived distress leaves the machine first; a later
/// successful registration reports the failed starts on <c>agent_started</c>. Each start is
/// ended the way <see cref="BackendSessionRegistration.Register"/> ends it: record the failed
/// run, then conclude it.
/// </summary>
public sealed class InterceptedRegistrationTests : IDisposable
{
private static readonly DateTime At = new DateTime(2026, 10, 12, 9, 0, 0, DateTimeKind.Utc);
private readonly TempDirectory _tmp = new TempDirectory();
private readonly AgentLogger _logger;
private readonly SessionIdPersistence _persistence;
private readonly string _stateDir;
private int _cleanupCalls;
public InterceptedRegistrationTests()
{
_logger = new AgentLogger(Path.Combine(_tmp.Path, "logs"), AgentLogLevel.Info);
_persistence = new SessionIdPersistence(_tmp.Path);
_persistence.GetOrCreate(_logger);
_stateDir = Path.Combine(_tmp.Path, "State");
}
public void Dispose() => _tmp.Dispose();
// ============================================================ K4: buffer, then clean up
[Fact]
public void Agent_default_buffer_allows_one_further_boot_then_cleans_up()
{
var config = new AgentConfiguration();
Assert.Equal(1, config.InterceptedRegistrationRetryBoots);
Assert.Equal(6, Boot(Intercepted(), config));
Assert.False(GaveUp);
Assert.True(_persistence.SessionExists());
Assert.Equal(0, _cleanupCalls);
var record = _persistence.LoadRegistrationFailure()!;
Assert.Equal(1, record.FailedRuns);
Assert.Equal(1, record.InterceptedRuns);
Assert.Equal("AuthFailed", record.Outcome);
Assert.Equal(6, Boot(Intercepted(), config)); // the give-up keeps exit 6
AssertCleanedUpLikeTheEmergencyBreak(expectedCleanupCalls: 1);
}
[Theory]
[InlineData(0)] // clean up at the first intercepted start
[InlineData(2)]
[InlineData(5)]
public void Gives_up_on_the_first_intercepted_start_beyond_the_buffer(int buffer)
{
var config = new AgentConfiguration { InterceptedRegistrationRetryBoots = buffer };
for (var start = 1; start <= buffer; start++)
{
Assert.Equal(6, Boot(Intercepted(), config));
Assert.False(GaveUp, $"gave up on start {start} with a buffer of {buffer}");
}
Assert.Equal(6, Boot(Intercepted(), config));
AssertCleanedUpLikeTheEmergencyBreak(expectedCleanupCalls: 1);
}
[Theory]
[InlineData(true, 1)]
[InlineData(false, 0)]
public void Give_up_runs_the_cleanup_only_with_SelfDestructOnComplete_like_the_emergency_break(
bool selfDestructOnComplete, int expectedCleanupCalls)
{
var config = new AgentConfiguration
{
InterceptedRegistrationRetryBoots = 0,
SelfDestructOnComplete = selfDestructOnComplete,
};
Assert.Equal(6, Boot(Intercepted(), config));
AssertCleanedUpLikeTheEmergencyBreak(expectedCleanupCalls);
}
[Fact]
public async Task An_intercepted_start_takes_the_buffer_from_the_cached_config()
{
// The config fetch of an intercepted start is intercepted too: it falls back to the
// cache, and the registration decides on the cached buffer, not the agent default.
var config = new AgentConfiguration();
var service = new InterceptedConfigService(_logger, cached: new AgentConfigResponse
{
InterceptedRegistrationRetryBoots = 0,
SelfDestructOnComplete = true,
});
var fetched = await service.FetchConfigAsync(retryOnTransientErrors: true);
Assert.Equal(RemoteConfigFetchOutcome.FromCache, service.LastFetchOutcome);
RemoteConfigMerger.Merge(config, fetched, _logger);
Assert.Equal(0, config.InterceptedRegistrationRetryBoots);
Assert.Equal(6, Boot(Intercepted(), config));
AssertCleanedUpLikeTheEmergencyBreak(expectedCleanupCalls: 1);
}
[Fact]
public void Backend_rejection_and_network_failure_keep_their_exit_codes_and_never_give_up()
{
var config = new AgentConfiguration { InterceptedRegistrationRetryBoots = 0 };
Assert.Equal(6, Boot(BackendRejected(), config));
Assert.Equal(7, Boot(NetworkFailure(), config));
Assert.False(GaveUp);
Assert.True(_persistence.SessionExists());
Assert.Equal(0, _cleanupCalls);
var record = _persistence.LoadRegistrationFailure()!;
Assert.Equal(2, record.FailedRuns);
Assert.Equal(0, record.InterceptedRuns);
}
[Fact]
public void Only_an_intercepted_start_gives_up_even_when_the_count_already_exceeds_the_buffer()
{
var config = new AgentConfiguration { InterceptedRegistrationRetryBoots = 5 };
Boot(Intercepted(), config);
Boot(Intercepted(), config);
config.InterceptedRegistrationRetryBoots = 0; // e.g. a later cached config lowered it
Assert.Equal(6, Boot(BackendRejected(), config));
Assert.Equal(7, Boot(NetworkFailure(), config));
Assert.False(GaveUp);
Assert.Equal(0, _cleanupCalls);
var record = _persistence.LoadRegistrationFailure()!;
Assert.Equal(4, record.FailedRuns);
Assert.Equal(2, record.InterceptedRuns); // unchanged by the other failures
Assert.Equal("Failed", record.Outcome);
}
[Fact]
public void Failures_of_another_kind_between_interceptions_do_not_reset_the_count()
{
var config = new AgentConfiguration(); // buffer 1
Assert.Equal(6, Boot(Intercepted(), config));
Assert.Equal(7, Boot(NetworkFailure(), config));
Assert.Equal(6, Boot(BackendRejected(), config));
Assert.False(GaveUp);
Assert.Equal(6, Boot(Intercepted(), config)); // second interception since the last success
AssertCleanedUpLikeTheEmergencyBreak(expectedCleanupCalls: 1);
}
[Fact]
public void Only_a_successful_registration_resets_the_count()
{
var config = new AgentConfiguration(); // buffer 1
Boot(Intercepted(), config);
BackendSessionRegistration.ClearAfterSuccess(_persistence, () => "fp-office", _logger);
Assert.Equal(6, Boot(Intercepted(), config));
Assert.False(GaveUp);
Assert.Equal(1, _persistence.LoadRegistrationFailure()!.InterceptedRuns);
}
[Fact]
public void Without_a_written_record_the_buffer_cannot_be_enforced_and_the_next_boot_retries()
{
var config = new AgentConfiguration { InterceptedRegistrationRetryBoots = 0 };
var exit = BackendSessionRegistration.ConcludeFailedRun(
Intercepted(), record: null, config, authFailureTracker: null,
_persistence, _stateDir, CleanupFactory, _logger);
Assert.Equal(6, exit);
Assert.False(GaveUp);
Assert.True(_persistence.SessionExists());
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task The_registration_answer_decides_whether_the_start_counts_as_intercepted(bool foreign)
{
// Through the real client: no correlation echo + the platform's "Client Certificate
// Required" page is an interception; a JSON 403 that echoes the id is the backend's verdict.
using var handler = new RecordingHttpMessageHandler();
if (foreign)
handler.QueueForeign(HttpStatusCode.Forbidden, InterceptionFixtures.CertificateRequiredPage);
else
handler.QueueStatus(HttpStatusCode.Forbidden, "{\"error\":\"Device validation failed\"}");
using var http = new HttpClient(handler);
var apiClient = new BackendApiClient(
http, "https://backend.test", manufacturer: "m", model: "d", serialNumber: "s",
useBootstrapTokenAuth: false, bootstrapToken: null, agentVersion: "0.0.0", logger: _logger);
var distress = new FakeDistressReporter();
var config = new AgentConfiguration
{
ApiBaseUrl = "https://backend.test",
TenantId = "00000000-0000-0000-0000-000000000001",
SessionId = _persistence.GetOrCreate(_logger),
InterceptedRegistrationRetryBoots = 0,
};
var result = await SessionRegistrationHelper.RegisterWithRetryAsync(
apiClient, config, agentVersion: "0.0.0", logger: _logger,
authFailureTracker: NewTracker(distress),
deviceHardware: ("m", "d", "s"),
networkLinkWait: _ => Task.CompletedTask);
Assert.Equal(SessionRegistrationOutcome.AuthFailed, result.Outcome);
Assert.Equal(foreign, result.IsIntercepted);
Assert.Equal(
foreign ? DistressErrorType.ClientCertificateNotReceived : DistressErrorType.DeviceNotRegistered,
Assert.Single(distress.Calls).ErrorType);
Assert.Equal(6, Boot(result, config));
Assert.Equal(foreign, GaveUp);
}
// ============================================================ the distress leaves first
[Fact]
public void The_exit_waits_for_the_pending_ClientCertificateNotReceived_send()
{
var record = RecordFailed(Intercepted(), "fp-office");
var (tracker, inFlight) = DispatchSlowCertificateDistress();
var exit = BackendSessionRegistration.ConcludeFailedRun(
Intercepted(), record, new AgentConfiguration(), tracker,
_persistence, _stateDir, CleanupFactory, _logger);
Assert.Equal(6, exit);
Assert.True(inFlight.IsCompleted);
Assert.False(GaveUp);
}
[Fact]
public void The_give_up_waits_for_the_pending_send_before_it_cleans_up()
{
var record = RecordFailed(Intercepted(), "fp-office");
var (tracker, inFlight) = DispatchSlowCertificateDistress();
bool? sentBeforeCleanup = null;
var exit = BackendSessionRegistration.ConcludeFailedRun(
Intercepted(), record, new AgentConfiguration { InterceptedRegistrationRetryBoots = 0 }, tracker,
_persistence, _stateDir,
() =>
{
sentBeforeCleanup = inFlight.IsCompleted;
throw new InvalidOperationException("test-harness: cleanup factory must not spawn real PowerShell in unit tests");
},
_logger);
Assert.Equal(6, exit);
Assert.True(sentBeforeCleanup);
Assert.True(GaveUp);
}
[Fact]
public void The_wait_for_the_distress_is_bounded_by_its_own_http_timeout()
{
Assert.True(BackendSessionRegistration.PendingDistressBound <= TimeSpan.FromSeconds(5));
}
// ============================================================ K5: measurement point
[Theory]
[InlineData("fp-office", "fp-office", false)]
[InlineData("fp-office", "fp-home", true)]
public void A_success_after_failed_starts_reports_the_record_it_clears(
string failedOn, string registeredOn, bool networkChanged)
{
var config = new AgentConfiguration { InterceptedRegistrationRetryBoots = 5 };
Boot(NetworkFailure(), config, fingerprint: "fp-elsewhere");
Boot(Intercepted(), config, fingerprint: failedOn); // the latest failed start's network counts
var prior = BackendSessionRegistration.ClearAfterSuccess(_persistence, () => registeredOn, _logger)!;
Assert.Equal(2, prior.FailedRuns);
Assert.Equal(1, prior.InterceptedRuns);
Assert.Equal("AuthFailed", prior.LastOutcome);
Assert.Equal(networkChanged, prior.NetworkChanged);
Assert.Null(_persistence.LoadRegistrationFailureRecord());
var data = EmitAgentStarted(prior);
Assert.Equal(2, data["priorRegistrationFailedRuns"]);
Assert.Equal(1, data["priorRegistrationInterceptedRuns"]);
Assert.Equal("AuthFailed", data["priorRegistrationLastOutcome"]);
Assert.Equal(networkChanged, data["priorRegistrationNetworkChanged"]);
}
[Theory]
[InlineData(null, "fp-home")] // the failed start's network was unknown
[InlineData("fp-office", null)] // this start's network is unknown
public void Network_change_is_omitted_when_either_fingerprint_is_unknown(string? failedOn, string? registeredOn)
{
Boot(Intercepted(), new AgentConfiguration(), fingerprint: failedOn);
var prior = BackendSessionRegistration.ClearAfterSuccess(_persistence, () => registeredOn!, _logger)!;
Assert.Null(prior.NetworkChanged);
var data = EmitAgentStarted(prior);
Assert.Equal(1, data["priorRegistrationFailedRuns"]);
Assert.False(data.ContainsKey("priorRegistrationNetworkChanged"));
}
[Fact]
public void A_success_without_failed_starts_reports_nothing_and_never_reads_the_network()
{
var captures = 0;
var prior = BackendSessionRegistration.ClearAfterSuccess(
_persistence, () => { captures++; return "fp-office"; }, _logger);
Assert.Null(prior);
Assert.Equal(0, captures);
Assert.DoesNotContain(EmitAgentStarted(null).Keys, k => k.StartsWith("priorRegistration", StringComparison.Ordinal));
}
[Fact]
public void The_network_fingerprint_stays_on_the_device()
{
Boot(Intercepted(), new AgentConfiguration(), fingerprint: "0123456789abcdef");
// Kept locally beside the record ...
Assert.Contains("0123456789abcdef", File.ReadAllText(Path.Combine(_tmp.Path, "registration-failure.json")));
Assert.Equal("0123456789abcdef", _persistence.LoadRegistrationFailureRecord()!.NetworkFingerprint);
// ... while the summary the emergency break sends cannot carry it.
var wire = JsonConvert.SerializeObject(new AgentErrorReport
{
PriorRegistrationFailure = _persistence.LoadRegistrationFailure(),
});
Assert.DoesNotContain("0123456789abcdef", wire);
Assert.DoesNotContain("Fingerprint", wire);
Assert.Contains("\"InterceptedRuns\":1", wire);
}
// ============================================================ helpers
private static SessionRegistrationResult Intercepted() =>
SessionRegistrationResult.AuthFailed(403, "The device certificate did not reach the backend.", intercepted: true);
private static SessionRegistrationResult BackendRejected() =>
SessionRegistrationResult.AuthFailed(403, "Backend returned 403 Forbidden. The device is not authorized.");
private static SessionRegistrationResult NetworkFailure() =>
SessionRegistrationResult.Failed("No such host is known.");
private bool GaveUp => File.Exists(Path.Combine(_stateDir, "enrollment-complete.marker"));
private AuthFailureTracker NewTracker(FakeDistressReporter distress) =>
new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 0, SystemClock.Instance, _logger, distress);
/// <summary>
/// What the registration helper does when the answer did not come from the backend: the
/// tracker dispatches ClientCertificateNotReceived — here a send that stays in flight for a
/// second, started right before the conclusion so nothing else can use the time up.
/// </summary>
private (AuthFailureTracker Tracker, Task InFlight) DispatchSlowCertificateDistress()
{
Task? inFlight = null;
var distress = new FakeDistressReporter { Send = () => inFlight = Task.Delay(TimeSpan.FromSeconds(1)) };
var tracker = NewTracker(distress);
tracker.RecordFailure(403, "agent/register-session", InterceptionFixtures.CertificateRequired());
Assert.Equal(DistressErrorType.ClientCertificateNotReceived, Assert.Single(distress.Calls).ErrorType);
Assert.False(inFlight!.IsCompleted);
return (tracker, inFlight);
}
private RegistrationFailureSummary? RecordFailed(SessionRegistrationResult result, string? fingerprint) =>
BackendSessionRegistration.RecordFailedRun(
_persistence, result, At, At.AddSeconds(2), linkUpAtStart: true, linkUpAtEnd: true,
configFetchOutcome: "FromCache", networkFingerprint: fingerprint, logger: _logger);
/// <summary>One start whose registration failed with <paramref name="result"/>, ended the way Register ends it.</summary>
private int Boot(SessionRegistrationResult result, AgentConfiguration config, string? fingerprint = "fp-office")
{
var record = RecordFailed(result, fingerprint);
return BackendSessionRegistration.ConcludeFailedRun(
result, record, config, authFailureTracker: null, _persistence, _stateDir, CleanupFactory, _logger);
}
private CleanupService CleanupFactory()
{
_cleanupCalls++;
// The real factory spawns a PowerShell cleanup script; throwing makes the cleanup retry
// swallow and log it, so the call is counted without a side effect (as in ProgramGuardsTests).
throw new InvalidOperationException("test-harness: cleanup factory must not spawn real PowerShell in unit tests");
}
/// <summary>The emergency break's end state (ProgramGuardsTests): marker, session files gone, cleanup per SelfDestructOnComplete.</summary>
private void AssertCleanedUpLikeTheEmergencyBreak(int expectedCleanupCalls)
{
Assert.True(GaveUp);
Assert.False(_persistence.SessionExists());
Assert.Null(_persistence.LoadRegistrationFailure());
Assert.Equal(expectedCleanupCalls, _cleanupCalls);
// The next start ends at the bootstrap guard, exactly as after the break.
Assert.True(ProgramAlias.CheckEnrollmentCompleteMarker(
_stateDir, selfDestructOnComplete: false, cleanupServiceFactory: null, _logger, consoleMode: false));
}
private Dictionary<string, object> EmitAgentStarted(PriorRegistrationFailures? prior)
{
var sink = new FakeSignalIngressSink();
LifecycleEmitters.EmitAgentStarted(
new InformationalEventPost(sink, SystemClock.Instance, _logger),
new AgentConfiguration { SessionId = "S1", TenantId = "00000000-0000-0000-0000-000000000001", CommandLineArgs = string.Empty },
new ProgramAlias.PreviousExitSummary { ExitType = "reboot_kill" },
"2.0.test",
remoteConfigService: null,
_logger,
priorRegistrationFailures: prior);
return (Dictionary<string, object>)Assert.Single(sink.Posted).TypedPayload!;
}
/// <summary>A start whose config fetch is answered by the interception: it falls back to the cache.</summary>
private sealed class InterceptedConfigService : RemoteConfigService
{
private readonly AgentConfigResponse _cached;
public InterceptedConfigService(AgentLogger logger, AgentConfigResponse cached)
: base(
new BackendApiClient(
httpClient: new HttpClient(), baseUrl: "http://localhost", manufacturer: string.Empty,
model: string.Empty, serialNumber: string.Empty, useBootstrapTokenAuth: false,
bootstrapToken: null, agentVersion: "0.0.0", logger: logger),
"00000000-0000-0000-0000-000000000001",
logger)
{
_cached = cached;
}
protected override Task<AgentConfigResponse> CallBackendAsync() =>
throw new BackendAuthException("The device certificate did not reach the backend.", 403,
classification: InterceptionFixtures.CertificateRequired());
protected override AgentConfigResponse LoadCachedConfig() => _cached;
protected override Task WaitForNetworkLinkAsync() => Task.CompletedTask;
}
}
}
@@ -44,6 +44,22 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
Assert.Equal("Succeeded", data["remoteConfigOutcome"]);
}
[Fact]
public void EmitAgentStarted_CarriesTheBootstrapProvenance()
{
// Values depend on the machine's %ProgramData%\AutopilotMonitor\Logs (see
// BootstrapProvenanceReaderTests); the two presence flags are always on the wire.
using var rig = new EmitterRig();
rig.RemoteConfig.SetFakeOutcome(RemoteConfigFetchOutcome.Succeeded, configVersion: 28);
LifecycleEmitters.EmitAgentStarted(
rig.Post, rig.AgentConfig, rig.PreviousExit, "2.0.806", rig.RemoteConfig, rig.Logger);
var data = AssertSingleEmit(rig.Sink, expectedEventType: "agent_started");
Assert.IsType<bool>(data["bootstrapScriptLogPresent"]);
Assert.IsType<bool>(data["oobeBootstrapLogPresent"]);
}
[Fact]
public void EmitAgentStarted_OnFallbackToDefaults_FlagsRemoteConfigFalse()
{
@@ -182,7 +198,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
Assert.Equal("FromCache", data["outcome"]);
}
// ── Post-registration recovery (Phase 6.5, tenant aebdce78 audit 2026-08-24) ─
// ── Post-registration recovery (Phase 6.5, kiosk-tenant audit 2026-08-24) ─
[Fact]
public void EmitFetchFailedIfAny_AfterRecovery_MarksRecoveredAndDemotesToInfo()
@@ -102,6 +102,24 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
Assert.Equal(expected, sink.Posted[0].Payload![SignalPayloadKeys.HelloWaitTimeoutSeconds]);
}
[Theory]
[InlineData(null, "Shadow")] // no config value — the default mode
[InlineData("Shadow", "Shadow")]
[InlineData("active", "Active")] // case-insensitive
[InlineData("Off", "Off")]
[InlineData("Enforce", "Shadow")] // unknown — changes no decision, still measures
public void Oobe_update_phase_mode_is_stamped_at_every_start_normalized(string? configured, string expected)
{
// D-310: stamped every time, so a later Off/Shadow can take Active away mid-session.
using var tmp = new TempDirectory();
var sink = new FakeSignalIngressSink();
AutopilotMonitor.Agent.V2.Runtime.LifecycleEmitters.PostEnrollmentFactsObserved(
sink, HelloWaitTimeout.DefaultSeconds, NewLogger(tmp.Path), configured);
Assert.Equal(expected, sink.Posted[0].Payload![SignalPayloadKeys.OobeUpdatePhaseMode]);
}
[Fact]
public void Swallows_sink_exceptions()
{
@@ -0,0 +1,170 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
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.Transport;
using AutopilotMonitor.Agent.V2.Core.Orchestration;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
using AutopilotMonitor.Shared.Models;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Program
{
/// <summary>
/// Host wiring for <c>TelemetryUploadOrchestrator.SessionGone</c>
/// (HTTP 410 on the current session's telemetry): exactly one
/// <see cref="AgentErrorType.TelemetryRejectedSessionUnknown"/> emergency report carrying the
/// status, then a synthetic forced <c>terminate_session</c> tagged <c>origin=session_gone</c>
/// — and nothing at all once a shutdown is already in progress.
/// </summary>
public sealed class ServerControlPlaneSessionGoneTests
{
private sealed class RecordingEmergencyReporter : EmergencyReporter
{
private readonly List<string> _sequence;
public List<(AgentErrorType ErrorType, string Message, int? HttpStatusCode)> Reports { get; } =
new List<(AgentErrorType, string, int?)>();
public RecordingEmergencyReporter(List<string> sequence)
: base(apiClient: null!, sessionId: "S1", tenantId: "T1", agentVersion: "0.0.0", logger: null!)
{
_sequence = sequence;
}
public override Task TrySendAsync(
AgentErrorType errorType,
string message,
int? httpStatusCode = null,
long? sequenceNumber = null,
int attempts = 1,
TimeSpan? perAttemptTimeout = null,
TimeSpan? retryDelay = null,
double? sessionAgeHours = null,
RegistrationFailureSummary? priorRegistrationFailure = null)
{
Reports.Add((errorType, message, httpStatusCode));
_sequence.Add("report");
return Task.CompletedTask;
}
}
private sealed class RecordingDispatcher : ServerActionDispatcher
{
private readonly List<string> _sequence;
public List<ServerAction> Dispatched { get; } = new List<ServerAction>();
public RecordingDispatcher(AgentConfiguration config, AgentLogger logger, InformationalEventPost post, List<string> sequence)
: base(
configuration: config,
logger: logger,
rotateConfigAsync: () => Task.FromResult(true),
uploadDiagnosticsAsync: _ => Task.FromResult<DiagnosticsUploadResult>(null!),
onTerminateRequested: _ => Task.CompletedTask,
post: post)
{
_sequence = sequence;
}
public override Task DispatchAsync(List<ServerAction> actions)
{
Dispatched.AddRange(actions);
_sequence.Add("dispatch");
return Task.CompletedTask;
}
}
private sealed class Rig : IDisposable
{
public TempDirectory Tmp { get; } = new TempDirectory();
public AgentLogger Logger { get; }
public List<string> Sequence { get; } = new List<string>();
public RecordingEmergencyReporter Reporter { get; }
public RecordingDispatcher Dispatcher { get; }
public ManualResetEventSlim Shutdown { get; } = new ManualResetEventSlim(false);
public ManualResetEventSlim ShutdownComplete { get; } = new ManualResetEventSlim(false);
public Rig()
{
Logger = new AgentLogger(Path.Combine(Tmp.Path, "logs"), AgentLogLevel.Info);
var config = new AgentConfiguration { SessionId = "S1", TenantId = "T1", ApiBaseUrl = "http://localhost" };
var post = new InformationalEventPost(
new FakeSignalIngressSink(),
new VirtualClock(new DateTime(2026, 10, 9, 10, 0, 0, DateTimeKind.Utc)));
Reporter = new RecordingEmergencyReporter(Sequence);
Dispatcher = new RecordingDispatcher(config, Logger, post, Sequence);
}
public void Handle(string reason = "session gone: http 410") =>
global::AutopilotMonitor.Agent.V2.Runtime.ServerControlPlane.HandleSessionGone(
reason, Reporter, Dispatcher, Shutdown, ShutdownComplete, Logger);
public void Dispose()
{
Shutdown.Dispose();
ShutdownComplete.Dispose();
Tmp.Dispose();
}
}
[Fact]
public void Reports_once_with_410_then_dispatches_forced_terminate_tagged_session_gone()
{
using var rig = new Rig();
rig.Handle();
Assert.Equal(new[] { "report", "dispatch" }, rig.Sequence);
var report = Assert.Single(rig.Reporter.Reports);
Assert.Equal(AgentErrorType.TelemetryRejectedSessionUnknown, report.ErrorType);
Assert.Equal(410, report.HttpStatusCode);
Assert.Contains("410", report.Message);
var action = Assert.Single(rig.Dispatcher.Dispatched);
Assert.Equal(ServerActionTypes.TerminateSession, action.Type);
Assert.Contains("410", action.Reason);
Assert.NotNull(action.Params);
var parameters = action.Params!;
Assert.Equal("true", parameters["forceSelfDestruct"]);
Assert.Equal("0", parameters["gracePeriodSeconds"]);
Assert.Equal(TerminationOrigins.SessionGone, parameters["origin"]);
}
[Theory]
[InlineData(true, false)]
[InlineData(false, true)]
public void Does_nothing_when_a_shutdown_is_already_in_progress(bool shutdownSet, bool shutdownCompleteSet)
{
using var rig = new Rig();
if (shutdownSet) rig.Shutdown.Set();
if (shutdownCompleteSet) rig.ShutdownComplete.Set();
rig.Handle();
Assert.Empty(rig.Reporter.Reports);
Assert.Empty(rig.Dispatcher.Dispatched);
}
[Fact]
public void ReadOrigin_returns_the_origin_param_or_null()
{
Assert.Equal(TerminationOrigins.SessionGone,
global::AutopilotMonitor.Agent.V2.Runtime.ServerControlPlane.ReadOrigin(
new Dictionary<string, string> { ["origin"] = TerminationOrigins.SessionGone }));
Assert.Equal(TerminationOrigins.KillSignal,
global::AutopilotMonitor.Agent.V2.Runtime.ServerControlPlane.ReadOrigin(
new Dictionary<string, string> { ["origin"] = TerminationOrigins.KillSignal, ["forceSelfDestruct"] = "true" }));
Assert.Null(global::AutopilotMonitor.Agent.V2.Runtime.ServerControlPlane.ReadOrigin(
new Dictionary<string, string> { ["adminOutcome"] = "Failed" }));
Assert.Null(global::AutopilotMonitor.Agent.V2.Runtime.ServerControlPlane.ReadOrigin(
new Dictionary<string, string> { ["origin"] = string.Empty }));
Assert.Null(global::AutopilotMonitor.Agent.V2.Runtime.ServerControlPlane.ReadOrigin(null!));
}
}
}
@@ -0,0 +1,113 @@
using System;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Principal;
using AutopilotMonitor.Agent.V2.Core.Security;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Security
{
/// <summary>
/// The agent data directory keeps standard users from adding files where the SYSTEM agent
/// starts from. Works on a temp folder the test owns; as owner it can always hand itself the
/// rights back to clean up.
/// </summary>
public sealed class AgentDirectoryAclTests : IDisposable
{
private const FileSystemRights WriteRights =
FileSystemRights.WriteData | FileSystemRights.AppendData | FileSystemRights.WriteAttributes
| FileSystemRights.WriteExtendedAttributes | FileSystemRights.Delete | FileSystemRights.DeleteSubdirectoriesAndFiles
| FileSystemRights.ChangePermissions | FileSystemRights.TakeOwnership;
private static readonly SecurityIdentifier Users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
private readonly string _root = Path.Combine(Path.GetTempPath(), "apm-acl-" + Guid.NewGuid().ToString("N"));
public AgentDirectoryAclTests() => Directory.CreateDirectory(_root);
public void Dispose()
{
try
{
var me = WindowsIdentity.GetCurrent().User!;
var directory = new DirectoryInfo(_root);
var security = directory.GetAccessControl();
security.AddAccessRule(new FileSystemAccessRule(me, FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
directory.SetAccessControl(security);
Directory.Delete(_root, recursive: true);
}
catch
{
// best effort: a leftover temp folder must not fail the run
}
}
private static FileSystemRights UsersRights(string path) =>
new DirectoryInfo(path).GetAccessControl()
.GetAccessRules(includeExplicit: true, includeInherited: true, typeof(SecurityIdentifier))
.Cast<FileSystemAccessRule>()
.Where(r => r.AccessControlType == AccessControlType.Allow && Users.Equals(r.IdentityReference))
.Aggregate((FileSystemRights)0, (all, r) => all | r.FileSystemRights);
[Fact]
public void The_protected_security_inherits_nothing_and_gives_users_read_only()
{
var security = AgentDirectoryAcl.BuildProtectedSecurity();
Assert.True(security.AreAccessRulesProtected);
var users = security.GetAccessRules(true, true, typeof(SecurityIdentifier)).Cast<FileSystemAccessRule>()
.Single(r => Users.Equals(r.IdentityReference));
// An allow rule always carries Synchronize; .NET adds it on construction.
Assert.Equal(FileSystemRights.ReadAndExecute, users.FileSystemRights & ~FileSystemRights.Synchronize);
Assert.Equal((FileSystemRights)0, users.FileSystemRights & WriteRights);
}
[Fact]
public void A_folder_is_protected_once_and_then_left_alone()
{
var first = AgentDirectoryAcl.EnsureProtected(_root);
var second = AgentDirectoryAcl.EnsureProtected(_root);
Assert.Equal(AgentDirectoryAclOutcome.Applied, first.Outcome);
Assert.Equal(AgentDirectoryAclOutcome.AlreadyProtected, second.Outcome);
Assert.True(AgentDirectoryAcl.IsProtectedAsExpected(new DirectoryInfo(_root).GetAccessControl(AccessControlSections.Access)));
Assert.Equal((FileSystemRights)0, UsersRights(_root) & WriteRights);
}
[Fact]
public void Existing_subfolders_take_over_the_protection()
{
// Install mode protects the base after Agent, Spool and Logs already exist.
var agent = Directory.CreateDirectory(Path.Combine(_root, "Agent")).FullName;
AgentDirectoryAcl.EnsureProtected(_root);
Assert.Equal((FileSystemRights)0, UsersRights(agent) & WriteRights);
Assert.NotEqual((FileSystemRights)0, UsersRights(agent) & FileSystemRights.ReadAndExecute);
}
[Fact]
public void An_unprotected_or_widened_dacl_is_not_taken_as_protected()
{
var unprotected = AgentDirectoryAcl.BuildProtectedSecurity();
unprotected.SetAccessRuleProtection(isProtected: false, preserveInheritance: true);
Assert.False(AgentDirectoryAcl.IsProtectedAsExpected(unprotected));
var widened = AgentDirectoryAcl.BuildProtectedSecurity();
widened.AddAccessRule(new FileSystemAccessRule(Users, FileSystemRights.Modify,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
Assert.False(AgentDirectoryAcl.IsProtectedAsExpected(widened));
}
[Fact]
public void A_missing_folder_is_reported_not_created()
{
var missing = Path.Combine(_root, "does-not-exist");
Assert.Equal(AgentDirectoryAclOutcome.Missing, AgentDirectoryAcl.EnsureProtected(missing).Outcome);
Assert.False(Directory.Exists(missing));
}
}
}
@@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Logging;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.V2.Core.Security;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Transport;
using AutopilotMonitor.Shared.Models;
using Xunit;
@@ -261,8 +262,14 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Security
}
[Fact]
public void RecordFailure_endpoint_unavailable_suppresses_distress_but_still_counts()
public void RecordFailure_only_the_stopped_app_page_counts_without_distress()
{
// Rewritten deliberately for the client-certificate interception plan: the former
// "endpoint unavailable" flag covered every HTML platform page, so the platform's
// "Client Certificate Required" page (TLS inspection on the path) shut healthy agents
// down. Now only the stopped-app page keeps that semantics — it counts toward the
// shutdown (a dead endpoint must not retry forever) without a distress report that
// could not reach it — while the certificate page never counts (tests below).
var clock = new FakeClock(new DateTime(2026, 4, 21, 10, 0, 0, DateTimeKind.Utc));
var fakeDistress = new FakeDistressReporter();
var tracker = new AuthFailureTracker(maxFailures: 2, timeoutMinutes: 0, clock, NewLogger(), fakeDistress);
@@ -270,19 +277,19 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Security
int events = 0;
tracker.ThresholdExceeded += (_, _) => events++;
// Platform-403s from a stopped endpoint: a distress report cannot reach it and the
// DeviceNotRegistered classification would be wrong — nothing must be dispatched.
tracker.RecordFailure(403, "agent/config", endpointUnavailable: true);
tracker.RecordFailure(403, "agent/register-session", endpointUnavailable: true);
tracker.RecordFailure(403, "agent/config", InterceptionFixtures.CertificateRequired());
Assert.Equal(0, tracker.ConsecutiveFailures);
tracker.RecordFailure(403, "agent/config", InterceptionFixtures.StoppedApp());
tracker.RecordFailure(403, "agent/register-session", InterceptionFixtures.StoppedApp());
Assert.Empty(fakeDistress.Calls);
// The shutdown thresholds still apply — a dead endpoint must not retry forever.
Assert.Equal(2, tracker.ConsecutiveFailures);
Assert.Equal(1, events);
Assert.DoesNotContain(fakeDistress.Calls, c => c.ErrorType == DistressErrorType.DeviceNotRegistered);
}
[Fact]
public void RecordFailure_endpoint_unavailable_does_not_consume_streak_report_slot()
public void RecordFailure_stopped_app_page_does_not_consume_streak_report_slot()
{
var clock = new FakeClock(new DateTime(2026, 4, 21, 10, 0, 0, DateTimeKind.Utc));
var fakeDistress = new FakeDistressReporter();
@@ -290,7 +297,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Security
// The streak opens with a platform-403 (no dispatch), then a live backend answers
// with a genuine 403 — that one must still produce the streak's single report.
tracker.RecordFailure(403, "agent/config", endpointUnavailable: true);
tracker.RecordFailure(403, "agent/config", InterceptionFixtures.StoppedApp());
tracker.RecordFailure(403, "agent/config");
tracker.RecordFailure(403, "agent/config");
@@ -298,6 +305,133 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Security
Assert.Equal(DistressErrorType.DeviceNotRegistered, fakeDistress.Calls[0].ErrorType);
}
// ============================================================= Foreign answers (interception)
[Fact]
public void RecordFailure_foreign_never_reaches_the_threshold()
{
var clock = new FakeClock(new DateTime(2026, 4, 21, 10, 0, 0, DateTimeKind.Utc));
var tracker = new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 10, clock, NewLogger(), new FakeDistressReporter());
int events = 0;
tracker.ThresholdExceeded += (_, _) => events++;
for (int i = 0; i < 10; i++)
{
tracker.RecordFailure(403, "agent/telemetry", InterceptionFixtures.CertificateRequired());
clock.Now = clock.Now.AddMinutes(3); // also past the time window
}
Assert.Equal(0, events);
Assert.Equal(0, tracker.ConsecutiveFailures);
}
[Fact]
public void RecordFailure_foreign_dispatches_one_ClientCertificateNotReceived_per_process()
{
var clock = new FakeClock(new DateTime(2026, 4, 21, 10, 0, 0, DateTimeKind.Utc));
var fakeDistress = new FakeDistressReporter();
var tracker = new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 0, clock, NewLogger(), fakeDistress);
tracker.RecordFailure(403, "agent/register-session", InterceptionFixtures.CertificateRequired());
tracker.RecordFailure(403, "agent/telemetry", InterceptionFixtures.CertificateRequired());
tracker.RecordSuccess(); // a success re-arms the streak report, never the per-process one
tracker.RecordFailure(401, "agent/telemetry", InterceptionFixtures.CertificateRequired(401));
var call = Assert.Single(fakeDistress.Calls);
Assert.Equal(DistressErrorType.ClientCertificateNotReceived, call.ErrorType);
Assert.Equal(403, call.HttpStatusCode);
// Operation, status, sub-reason, page title, issuer — in that order, within the 256 budget.
Assert.StartsWith("agent/register-session http 403 client_certificate_required;", call.Message);
Assert.True(
call.Message.IndexOf(InterceptionFixtures.PlatformPageTitle, StringComparison.Ordinal) <
call.Message.IndexOf(InterceptionFixtures.InspectionIssuer, StringComparison.Ordinal));
Assert.True(call.Message.Length <= 256, $"distress message is {call.Message.Length} characters");
}
[Fact]
public void RecordFailure_foreign_within_a_backend_streak_neither_resets_nor_advances_it()
{
var clock = new FakeClock(new DateTime(2026, 4, 21, 10, 0, 0, DateTimeKind.Utc));
var fakeDistress = new FakeDistressReporter();
var tracker = new AuthFailureTracker(maxFailures: 3, timeoutMinutes: 0, clock, NewLogger(), fakeDistress);
int events = 0;
tracker.ThresholdExceeded += (_, _) => events++;
tracker.RecordFailure(403, "agent/telemetry");
tracker.RecordFailure(403, "agent/telemetry");
tracker.RecordFailure(403, "agent/telemetry", InterceptionFixtures.CertificateRequired());
tracker.RecordFailure(403, "agent/telemetry", InterceptionFixtures.CertificateRequired());
Assert.Equal(2, tracker.ConsecutiveFailures);
Assert.Equal(0, events);
tracker.RecordFailure(403, "agent/telemetry"); // the third backend rejection trips it
Assert.Equal(3, tracker.ConsecutiveFailures);
Assert.Equal(1, events);
Assert.Equal(1, fakeDistress.Calls.Count(c => c.ErrorType == DistressErrorType.DeviceNotRegistered));
}
// ============================================================= Pending foreign distress
[Fact]
public void WaitForPendingForeignDistress_returns_at_once_when_nothing_was_dispatched()
{
var clock = new FakeClock(new DateTime(2026, 4, 21, 10, 0, 0, DateTimeKind.Utc));
var tracker = new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 0, clock, NewLogger(), new FakeDistressReporter());
Assert.True(tracker.WaitForPendingForeignDistress(TimeSpan.Zero));
}
[Fact]
public void WaitForPendingForeignDistress_waits_for_the_send_to_finish()
{
var clock = new FakeClock(new DateTime(2026, 4, 21, 10, 0, 0, DateTimeKind.Utc));
Task? inFlight = null;
var fakeDistress = new FakeDistressReporter { Send = () => inFlight = Task.Delay(200) };
var tracker = new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 0, clock, NewLogger(), fakeDistress);
tracker.RecordFailure(403, "agent/register-session", InterceptionFixtures.CertificateRequired());
Assert.True(tracker.WaitForPendingForeignDistress(TimeSpan.FromSeconds(10)));
Assert.True(inFlight!.IsCompleted);
}
[Fact]
public void WaitForPendingForeignDistress_stops_at_the_bound()
{
var clock = new FakeClock(new DateTime(2026, 4, 21, 10, 0, 0, DateTimeKind.Utc));
var neverSent = new TaskCompletionSource<bool>();
var fakeDistress = new FakeDistressReporter { Send = () => neverSent.Task };
var tracker = new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 0, clock, NewLogger(), fakeDistress);
tracker.RecordFailure(403, "agent/register-session", InterceptionFixtures.CertificateRequired());
var watch = Stopwatch.StartNew();
var finished = tracker.WaitForPendingForeignDistress(TimeSpan.FromMilliseconds(100));
watch.Stop();
Assert.False(finished);
Assert.True(watch.Elapsed < TimeSpan.FromSeconds(5), $"waited {watch.Elapsed}");
}
[Fact]
public void WaitForPendingForeignDistress_never_waits_for_a_backend_streak_report()
{
// Only the ClientCertificateNotReceived send is kept: a failed start must not block
// on anything else.
var clock = new FakeClock(new DateTime(2026, 4, 21, 10, 0, 0, DateTimeKind.Utc));
var neverSent = new TaskCompletionSource<bool>();
var fakeDistress = new FakeDistressReporter { Send = () => neverSent.Task };
var tracker = new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 0, clock, NewLogger(), fakeDistress);
tracker.RecordFailure(403, "agent/register-session");
Assert.Equal(DistressErrorType.DeviceNotRegistered, Assert.Single(fakeDistress.Calls).ErrorType);
Assert.True(tracker.WaitForPendingForeignDistress(TimeSpan.Zero));
}
[Fact]
public void RecordSuccess_rearms_distress_dispatch_for_the_next_streak()
{
@@ -329,29 +463,6 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Security
Assert.Equal(1, events);
}
private sealed class FakeDistressReporter : DistressReporter
{
public sealed class Call
{
public DistressErrorType ErrorType { get; set; }
public string Message { get; set; } = default!;
public int? HttpStatusCode { get; set; }
}
public List<Call> Calls { get; } = new List<Call>();
public FakeDistressReporter()
: base(baseUrl: "https://example.test", tenantId: "t", manufacturer: "m", model: "d", serialNumber: "s", agentVersion: "0.0.0", logger: null!)
{
}
public override Task TrySendAsync(DistressErrorType errorType, string message, int? httpStatusCode = null)
{
Calls.Add(new Call { ErrorType = errorType, Message = message, HttpStatusCode = httpStatusCode });
return Task.CompletedTask;
}
}
private sealed class FakeClock : AutopilotMonitor.DecisionCore.Engine.IClock
{
public DateTime Now { get; set; }
@@ -0,0 +1,142 @@
using System.Collections.Generic;
using System.IO;
using AutopilotMonitor.Agent.V2.Core.Security;
using AutopilotMonitor.Shared;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Security
{
/// <summary>
/// The decision over what <c>Test-FileCatalog -Detailed</c> reports. The runspace itself is
/// exercised on a lab device (plan agent-payload-catalog, V1/V3), never in the test host.
/// </summary>
public sealed class PayloadCatalogVerifierTests
{
private const string OurSubject = "CN=glueckkanja AG, O=glueckkanja AG, L=Offenbach, S=Hessen, C=DE";
private static Dictionary<string, string> Files(params string[] namesAndHashes)
{
var map = new Dictionary<string, string>();
for (var i = 0; i < namesAndHashes.Length; i += 2) map[namesAndHashes[i]] = namesAndHashes[i + 1];
return map;
}
private static PayloadCatalogFacts Facts(
string status = "Valid", string? signatureStatus = "Valid", string? subject = OurSubject,
Dictionary<string, string>? catalog = null, Dictionary<string, string>? path = null)
=> new PayloadCatalogFacts(status, signatureStatus, subject,
catalog ?? Files("a.dll", "AA"), path ?? Files("a.dll", "AA"));
private static PayloadCatalogVerdict Evaluate(PayloadCatalogFacts facts)
=> PayloadCatalogVerifier.Evaluate(facts, Constants.CodeSigningPublisherPattern);
[Fact]
public void Signed_by_us_and_matching_is_accepted()
{
var verdict = Evaluate(Facts());
Assert.True(verdict.Accepted);
Assert.Null(verdict.Reason);
}
[Fact]
public void Matching_hashes_under_an_unsigned_catalog_are_rejected()
{
// Status covers the hashes only: an unsigned catalog over matching files reports Valid.
var verdict = Evaluate(Facts(signatureStatus: "NotSigned", subject: null));
Assert.False(verdict.Accepted);
Assert.Contains("catalog signature status NotSigned", verdict.Reason);
}
[Fact]
public void A_validly_signed_catalog_from_another_publisher_is_rejected()
{
var verdict = Evaluate(Facts(subject: "CN=Contoso Ltd, O=Contoso Ltd, C=US"));
Assert.False(verdict.Accepted);
Assert.Contains("does not match", verdict.Reason);
}
[Fact]
public void Missing_signature_details_are_a_rejection_not_a_pass()
{
Assert.False(Evaluate(Facts(signatureStatus: null, subject: null)).Accepted);
Assert.False(Evaluate(Facts(subject: "")).Accepted);
}
[Fact]
public void A_differing_payload_names_missing_extra_and_changed_files()
{
var verdict = Evaluate(Facts(
status: "ValidationFailed",
catalog: Files("a.dll", "AA", "b.dll", "BB", "c.config", "CC"),
path: Files("a.dll", "AA", "b.dll", "XX", "evil.dll", "EE")));
Assert.False(verdict.Accepted);
Assert.Contains("missing: c.config", verdict.Reason);
Assert.Contains("extra: evil.dll", verdict.Reason);
Assert.Contains("changed: b.dll", verdict.Reason);
}
[Fact]
public void Long_difference_lists_are_capped()
{
var path = Files("a.dll", "AA");
for (var i = 0; i < 12; i++) path[$"extra{i:00}.txt"] = "00";
var verdict = Evaluate(Facts(status: "ValidationFailed", path: path));
Assert.Contains("(+7 more)", verdict.Reason);
}
[Fact]
public void Signature_and_payload_problems_are_both_reported()
{
var verdict = Evaluate(Facts(status: "ValidationFailed", signatureStatus: "HashMismatch", subject: null,
path: Files("a.dll", "AA", "x.dll", "11")));
Assert.Contains("catalog signature status HashMismatch", verdict.Reason);
Assert.Contains("extra: x.dll", verdict.Reason);
}
[Theory]
[InlineData(OurSubject, true)]
[InlineData("cn=glueckkanja ag, o=glueckkanja ag, c=de", true)] // -like is case-insensitive
[InlineData("CN=x, O=glueckkanja AG Holding, C=DE", true)] // the pattern is open-ended by design
[InlineData("CN=glueckkanja AG, O=Contoso, C=DE", false)]
[InlineData("", false)]
[InlineData(null, false)]
public void Publisher_pattern_behaves_like_powershell_like(string? subject, bool expected)
{
Assert.Equal(expected, PayloadCatalogVerifier.MatchesPublisherPattern(subject, Constants.CodeSigningPublisherPattern));
}
[Fact]
public void Regex_metacharacters_in_the_pattern_are_literal()
{
Assert.True(PayloadCatalogVerifier.MatchesPublisherPattern("O=Json.NET (.NET Foundation)", "*O=Json.NET (.NET Foundation)*"));
Assert.False(PayloadCatalogVerifier.MatchesPublisherPattern("O=JsonXNET (.NET Foundation)", "*O=Json.NET (.NET Foundation)*"));
}
[Fact]
public void A_payload_without_catalog_is_rejected_before_any_runspace_starts()
{
var dir = Path.Combine(Path.GetTempPath(), "apm-catalog-test-" + System.Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(dir);
try
{
File.WriteAllText(Path.Combine(dir, "readme.txt"), "x");
var verdict = PayloadCatalogVerifier.Verify(dir);
Assert.False(verdict.Accepted);
Assert.Contains("missing", verdict.Reason);
}
finally
{
Directory.Delete(dir, recursive: true);
}
}
}
}
@@ -298,9 +298,50 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Security
Assert.False(File.Exists(Path.Combine(tmp.Path, "registration-failure.json")));
}
[Fact]
public void RecordRegistrationFailure_counts_intercepted_runs_and_keeps_the_latest_network_fingerprint()
{
using var tmp = new TempDirectory();
var sut = new SessionIdPersistence(tmp.Path);
var at = new DateTime(2026, 10, 12, 9, 0, 0, DateTimeKind.Utc);
var intercepted = FailedRun(at, outcome: "AuthFailed");
intercepted.InterceptedRuns = 1;
sut.RecordRegistrationFailure(intercepted, networkFingerprint: "fp-office");
sut.RecordRegistrationFailure(FailedRun(at.AddHours(1)), networkFingerprint: null);
var merged = sut.RecordRegistrationFailure(intercepted, networkFingerprint: "fp-home");
Assert.Equal(3, merged!.FailedRuns);
Assert.Equal(2, merged.InterceptedRuns);
var record = new SessionIdPersistence(tmp.Path).LoadRegistrationFailureRecord();
Assert.Equal(3, record!.Summary.FailedRuns);
Assert.Equal(2, record.Summary.InterceptedRuns);
Assert.Equal("fp-home", record.NetworkFingerprint);
}
[Fact]
public void A_record_from_an_agent_before_the_interception_buffer_still_loads()
{
using var tmp = new TempDirectory();
File.WriteAllText(Path.Combine(tmp.Path, "registration-failure.json"),
"{\"FailedRuns\":2,\"FirstFailedAtUtc\":\"2026-09-01T08:00:00Z\",\"LastFailedAtUtc\":\"2026-09-02T08:00:00Z\"," +
"\"Outcome\":\"AuthFailed\",\"LastError\":\"x\",\"NetworkLinkUpAtStart\":true,\"NetworkLinkUpAtEnd\":true," +
"\"AttemptWindowSeconds\":3.0,\"ConfigFetchOutcome\":\"FromCache\"}");
var record = new SessionIdPersistence(tmp.Path).LoadRegistrationFailureRecord();
Assert.Equal(2, record!.Summary.FailedRuns);
Assert.Equal(0, record.Summary.InterceptedRuns);
Assert.Null(record.NetworkFingerprint);
Assert.Equal(new DateTime(2026, 9, 1, 8, 0, 0, DateTimeKind.Utc), record.Summary.FirstFailedAtUtc);
}
[Theory]
[InlineData("not json")]
[InlineData("{\"FailedRuns\":0}")]
[InlineData("null")]
[InlineData("[]")]
[InlineData("")]
public void LoadRegistrationFailure_reports_nothing_for_a_corrupt_or_empty_record(string content)
{
using var tmp = new TempDirectory();
@@ -6,6 +6,7 @@ using AutopilotMonitor.Agent.V2.Core.Logging;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.V2.Core.Security;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Transport;
using AutopilotMonitor.DecisionCore.Engine;
using AutopilotMonitor.Shared.Models;
using Xunit;
@@ -96,6 +97,36 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Security
Assert.Equal(1, tracker.ConsecutiveFailures);
}
[Fact]
public async Task Intercepted_registration_ends_auth_failed_without_counting_and_reports_once()
{
// Something on the network path answered (TLS inspection): the run still ends
// AuthFailed (exit 6, unchanged until the interception buffer exists), but the tracker
// does not count it and sends ClientCertificateNotReceived instead of DeviceNotRegistered.
using var tmp = new TempDirectory();
var logger = NewLogger(tmp);
var config = NewConfig();
var apiClient = new FakeApiClient(_ =>
throw new BackendAuthException("The device certificate did not reach the backend.", statusCode: 403,
classification: InterceptionFixtures.CertificateRequired()));
var distress = new FakeDistressReporter();
var tracker = new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 0,
clock: SystemClock.Instance, logger: logger, distressReporter: distress);
var result = await SessionRegistrationHelper.RegisterWithRetryAsync(
apiClient, config, agentVersion: "0.0.0", logger: logger, authFailureTracker: tracker,
networkLinkWait: _ => Task.CompletedTask);
Assert.Equal(SessionRegistrationOutcome.AuthFailed, result.Outcome);
Assert.Equal(403, result.HttpStatusCode);
Assert.Equal(1, apiClient.CallCount);
Assert.Equal(0, tracker.ConsecutiveFailures);
var call = Assert.Single(distress.Calls);
Assert.Equal(DistressErrorType.ClientCertificateNotReceived, call.ErrorType);
Assert.StartsWith("agent/register-session http 403 client_certificate_required", call.Message);
}
// ── SESSION-OWNER-BINDING ────────────────────────────────────────────
private static BackendAuthException OwnerMismatch()
@@ -0,0 +1,54 @@
using System;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Enrollment.Ime;
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>
/// D-316: every emitted platform-script completion carries a run id, and the tracker's run register holds what
/// was emitted for it under the IME user id — what the registry reconciliation pairs IME's saved result with.
/// </summary>
public sealed class ImeLogTrackerAdapterPlatformRunTests
{
private static readonly DateTime ClockNow = new DateTime(2026, 10, 3, 21, 0, 0, DateTimeKind.Utc);
private const string DeviceUser = "00000000-0000-0000-0000-000000000000";
private const string PolicyId = "1d6f0a52-3c4e-4b7a-9e21-5f8c7d2b9a10";
[Fact]
public void Platform_completion_carries_its_run_id_and_the_register_holds_the_emitted_run()
{
using var f = new ImeLogTrackerAdapterFixture(ClockNow);
using var adapter = new ImeLogTrackerAdapter(f.Tracker, f.Ingress, f.Clock);
f.Tracker.LastMatchedLogTimestamp = ClockNow.AddSeconds(-10);
f.Tracker.HandlePlatformScriptStarted(PolicyId, "ime", DeviceUser);
f.Tracker.RecordPlatformScriptExitCodeForTesting(PolicyId, 0, ClockNow.AddSeconds(-1));
f.Tracker.LastMatchedLogTimestamp = ClockNow;
f.Tracker.CompletePlatformScriptFromImeResultForTesting(PolicyId, "Success", ClockNow);
var info = f.InfoEvent(SharedEventTypes.ScriptCompleted);
Assert.True(Guid.TryParse(info.Payload!["runId"], out _));
f.Tracker.ScriptRuns.NotePassCompleted(ClockNow.AddMinutes(1));
Assert.Equal(PlatformRunClaim.Claimed, f.Tracker.ScriptRuns.TryClaim(DeviceUser, PolicyId, ClockNow.AddSeconds(5), out var run));
Assert.Equal(info.Payload["runId"], run!.RunId);
Assert.Equal(0, run.ExitCode);
Assert.Equal("Success", run.Result);
}
[Theory]
[InlineData("[PowerShell] User Id = 00000000-0000-0000-0000-000000000000, Policy id = d94468af-6ebe-4e94-9901-2a62d33ea6c1, policy result = Success", "00000000-0000-0000-0000-000000000000")]
[InlineData(@"Adding argument powershell with value C:\Program Files (x86)\Microsoft Intune Management Extension\Policies\Scripts\6C3F8A20-1D5E-4B97-8F4A-0E2D6B9C7A13_d94468af-6ebe-4e94-9901-2a62d33ea6c1.ps1", "6c3f8a20-1d5e-4b97-8f4a-0e2d6b9c7a13")]
[InlineData(@"Script file C:\Program Files (x86)\Microsoft Intune Management Extension\Policies\Scripts\00000000-0000-0000-0000-000000000000_d94468af-6ebe-4e94-9901-2a62d33ea6c1.ps1 is generated", "00000000-0000-0000-0000-000000000000")]
[InlineData(@"Adding argument powershellDetection with value C:\Program Files (x86)\Microsoft Intune Management Extension\Content\DetectionScripts\8b2e4c71-6a9d-4f03-b5e8-2c7a1d9f4e36_2.ps1", null)]
[InlineData("[PowerShell] User Id = not-a-guid, Policy id = d94468af", null)]
[InlineData("", null)]
public void Ime_user_id_is_read_from_the_result_line_and_the_script_path(string text, string? expected)
{
Assert.Equal(expected, ImeLogTracker.ImeUserIdFrom(text));
}
}
}
@@ -479,14 +479,14 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.SignalAdapters
public HashSet<string> ExistingKeys { get; } =
new HashSet<string>(StringComparer.OrdinalIgnoreCase);
public IReadOnlyList<string> Enumerate(RegistryHive hive, string packagesPath)
public IReadOnlyList<string> Enumerate(RegistryHive _, string packagesPath)
{
return PackagesByPath.TryGetValue(packagesPath, out var ids)
? (IReadOnlyList<string>)ids
: Array.Empty<string>();
}
public bool TryRead(RegistryHive hive, string packagesPath, string packageId, out RealmJoinPackageSnapshot snapshot)
public bool TryRead(RegistryHive _, string _1, string packageId, out RealmJoinPackageSnapshot snapshot)
{
snapshot = new RealmJoinPackageSnapshot(
packageId: packageId,
@@ -497,7 +497,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.SignalAdapters
return true;
}
public bool KeyExists(RegistryHive hive, string subPath)
public bool KeyExists(RegistryHive _, string subPath)
=> ExistingKeys.Contains(subPath);
}
@@ -165,6 +165,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Termination
public IReadOnlyCollection<string> StarvedAlreadyReportedOverride { get; set; } = Array.Empty<string>();
public List<string> TerminationActionLog { get; } = new List<string>();
public TimeSpan SpoolDrainPeriodOverride { get; set; } = TimeSpan.Zero;
public int TerminalUploadAttemptRequests;
// Session bc803955 — DevicePrep app-wave settle wait. Zero (default) disables the
// wait so the existing timing-sensitive tests stay untouched; the dedicated settle
// tests opt in with a real budget.
@@ -267,6 +268,12 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Termination
public long IngressPendingSignalCount => _rig.IngressPendingSignalCountAccessor!();
public bool CanObserveSpool => _rig.PendingItemCountAccessor != null;
public int SpoolPendingItemCount => _rig.PendingItemCountAccessor!();
public void RequestTerminalUploadAttempt()
{
Interlocked.Increment(ref _rig.TerminalUploadAttemptRequests);
_rig.TerminationActionLog.Add("terminalUploadAttempt");
}
}
/// <summary>ARCH-F2 — recording shutdown gate (no real shutdown.exe in tests).</summary>
@@ -310,6 +317,19 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Termination
new EnrollmentTerminatedEventArgs(
reason, outcome, stage.ToString(), at ?? EndUtc);
/// <summary>
/// The args <c>ServerControlPlane.OnTerminateRequested</c> synthesises for a
/// <c>terminate_session</c> action tagged with <paramref name="origin"/>.
/// </summary>
private static EnrollmentTerminatedEventArgs ServerArgs(string origin) =>
new EnrollmentTerminatedEventArgs(
EnrollmentTerminationReason.DecisionTerminalStage,
EnrollmentTerminationOutcome.Failed,
SessionStage.Failed.ToString(),
EndUtc,
details: "Server-requested termination",
origin: origin);
[Fact]
public void Handle_writes_final_status_json_in_state_directory()
{
@@ -458,6 +478,99 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Termination
Assert.Equal(0, rig.CleanupService.Invocations);
}
// ============================================================= origin=session_gone (HTTP 410)
[Fact]
public void Handle_session_gone_skips_spool_drain_and_diagnostics_but_still_cleans_up()
{
// The backend no longer knows the session: every drain would only burn its budget on
// 410s and a diagnostics package would describe a deleted session. Marker, cleanup
// and the (forced) self-destruct still run.
using var rig = new Rig();
rig.State = new DecisionStateBuilder(DecisionState.CreateInitial("S1", "T1")) { Stage = SessionStage.Failed }.Build();
rig.SpoolDrainPeriodOverride = TimeSpan.FromSeconds(5);
var spoolPolls = 0;
rig.PendingItemCountAccessor = () => { spoolPolls++; return 5; }; // never drains — a poll would burn the budget
var cfg = rig.BuildConfig(diagEnabled: true, diagMode: "Always", selfDestruct: true);
rig.Build(cfg).Handle(sender: null!, ServerArgs(TerminationOrigins.SessionGone));
Assert.Equal(0, spoolPolls);
Assert.Equal(0, rig.TerminalUploadAttemptRequests);
Assert.Equal(0, rig.DiagnosticsUploads);
Assert.DoesNotContain(Constants.EventTypes.DiagnosticsCollecting, rig.EmittedEventTypes);
Assert.Contains(Constants.EventTypes.AgentShuttingDown, rig.EmittedEventTypes);
Assert.True(File.Exists(Path.Combine(rig.StateDir, "enrollment-complete.marker")));
Assert.Equal(1, rig.CleanupService.Invocations);
Assert.Equal(1, rig.ShutdownSignalled);
}
public enum TerminalExit { SelfDestruct, StandaloneReboot, WhiteGlovePart1 }
[Theory]
[InlineData(EnrollmentTerminationReason.DecisionTerminalStage, EnrollmentTerminationOutcome.Succeeded, SessionStage.Completed, TerminalExit.SelfDestruct)]
[InlineData(EnrollmentTerminationReason.DecisionTerminalStage, EnrollmentTerminationOutcome.Failed, SessionStage.Failed, TerminalExit.SelfDestruct)]
[InlineData(EnrollmentTerminationReason.StoppedWaiting, EnrollmentTerminationOutcome.TimedOut, SessionStage.AwaitingHello, TerminalExit.SelfDestruct)]
[InlineData(EnrollmentTerminationReason.MaxLifetimeExceeded, EnrollmentTerminationOutcome.TimedOut, SessionStage.AwaitingHello, TerminalExit.SelfDestruct)]
[InlineData(EnrollmentTerminationReason.DecisionTerminalStage, EnrollmentTerminationOutcome.Succeeded, SessionStage.Completed, TerminalExit.StandaloneReboot)]
[InlineData(EnrollmentTerminationReason.DecisionTerminalStage, EnrollmentTerminationOutcome.Succeeded, SessionStage.WhiteGloveSealed, TerminalExit.WhiteGlovePart1)]
public void Handle_while_uploads_are_intercepted_makes_one_terminal_attempt_then_exits_as_usual(
EnrollmentTerminationReason reason, EnrollmentTerminationOutcome outcome, SessionStage stage, TerminalExit exit)
{
// Enrollment end during an interception pause: no deferral. DrainSpool asks for the one
// terminal upload attempt, waits its bounded budget (the spool never empties — every
// upload is answered on the network path), then the exit runs exactly as for any
// session. Unsent telemetry is lost, as today.
using var rig = new Rig();
rig.State = new DecisionStateBuilder(DecisionState.CreateInitial("S1", "T1")) { Stage = stage }.Build();
rig.SpoolDrainPeriodOverride = TimeSpan.FromMilliseconds(200);
rig.PendingItemCountAccessor = () => 7;
rig.DiagnosticsResult = new DiagnosticsUploadResult
{
ErrorCode = "The device certificate did not reach the backend (TLS inspection or a proxy on the network path): 403 Forbidden ...",
};
var cfg = rig.BuildConfig(diagEnabled: true, diagMode: "Always", selfDestruct: exit != TerminalExit.StandaloneReboot);
cfg.RebootOnComplete = exit == TerminalExit.StandaloneReboot;
rig.Build(cfg).Handle(sender: null!, Args(reason, outcome, stage));
Assert.Equal(1, rig.TerminalUploadAttemptRequests);
Assert.Equal(1, rig.ShutdownSignalled);
Assert.True(rig.TerminationActionLog.IndexOf("terminalUploadAttempt") < rig.TerminationActionLog.IndexOf("signalShutdown"));
if (exit == TerminalExit.WhiteGlovePart1)
{
Assert.True(rig.SessionPersistence.IsWhiteGloveResume());
Assert.Equal(0, rig.CleanupService.Invocations);
return;
}
Assert.Equal(1, rig.DiagnosticsUploads);
Assert.Contains(Constants.EventTypes.DiagnosticsUploadFailed, rig.EmittedEventTypes);
Assert.True(File.Exists(Path.Combine(rig.StateDir, "enrollment-complete.marker")));
Assert.Equal(exit == TerminalExit.SelfDestruct ? 1 : 0, rig.CleanupService.Invocations);
Assert.Equal(exit == TerminalExit.StandaloneReboot ? 1 : 0, rig.RebootInvocations);
}
[Fact]
public void Handle_kill_signal_origin_still_drains_spool_and_uploads_diagnostics()
{
// The administrator kill signal keeps its full sequence — only session_gone skips.
using var rig = new Rig();
rig.State = new DecisionStateBuilder(DecisionState.CreateInitial("S1", "T1")) { Stage = SessionStage.Failed }.Build();
rig.SpoolDrainPeriodOverride = TimeSpan.FromSeconds(5);
var spoolPolls = 0;
rig.PendingItemCountAccessor = () => { spoolPolls++; return 0; };
var cfg = rig.BuildConfig(diagEnabled: true, diagMode: "Always", selfDestruct: true);
rig.Build(cfg).Handle(sender: null!, ServerArgs(TerminationOrigins.KillSignal));
Assert.True(spoolPolls > 0);
Assert.Equal(1, rig.DiagnosticsUploads);
Assert.Contains(Constants.EventTypes.DiagnosticsCollecting, rig.EmittedEventTypes);
Assert.Equal(1, rig.CleanupService.Invocations);
Assert.Equal(1, rig.ShutdownSignalled);
}
[Fact]
public void Handle_signals_shutdown_exactly_once_even_on_error()
{
@@ -933,6 +1046,43 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Termination
Assert.Equal(EnrollmentTerminationOutcome.Failed.ToString(), (string)data["outcome"]);
}
[Fact]
public void Handle_propagates_stopped_waiting_reason_and_cause_in_agent_shutting_down_data()
{
// D-310: the engine stopped waiting for the user (no sign-in after the OOBE update).
// The backend classifies this shutdown honestly — it keys on the reason tag.
using var rig = new Rig();
rig.State = new DecisionStateBuilder(DecisionState.CreateInitial("S1", "T1")) { Stage = SessionStage.EspAccountSetup }.Build();
rig.Build().Handle(sender: null!, new EnrollmentTerminatedEventArgs(
EnrollmentTerminationReason.StoppedWaiting,
EnrollmentTerminationOutcome.TimedOut,
SessionStage.EspAccountSetup.ToString(),
EndUtc,
details: StoppedWaitingReasons.Describe(StoppedWaitingReasons.OobeUpdateNoSignIn),
stopReason: StoppedWaitingReasons.OobeUpdateNoSignIn));
var data = rig.DataOf(Constants.EventTypes.AgentShuttingDown);
Assert.NotNull(data);
Assert.Equal("stopped_waiting", (string)data!["reason"]);
Assert.Equal(StoppedWaitingReasons.OobeUpdateNoSignIn, (string)data["stopReason"]);
Assert.Equal(EnrollmentTerminationOutcome.TimedOut.ToString(), (string)data["outcome"]);
}
[Fact]
public void Handle_omits_stop_reason_for_other_shutdowns()
{
using var rig = new Rig();
rig.State = new DecisionStateBuilder(DecisionState.CreateInitial("S1", "T1")) { Stage = SessionStage.AwaitingHello }.Build();
rig.Build().Handle(sender: null!,
Args(EnrollmentTerminationReason.MaxLifetimeExceeded, EnrollmentTerminationOutcome.TimedOut, SessionStage.AwaitingHello));
var data = rig.DataOf(Constants.EventTypes.AgentShuttingDown);
Assert.NotNull(data);
Assert.False(data!.ContainsKey("stopReason"));
}
[Fact]
public void Handle_includes_uptime_and_agent_version_in_agent_shutting_down()
{
@@ -107,7 +107,7 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Termination
[Fact]
public void Succeeded_outcome_is_an_info_about_tail_only_coverage_without_alarm_framing()
{
// The 659c3a90 shape: healthy enrollment, bootstrap merely ran late in the ESP queue.
// The field shape: healthy enrollment, bootstrap merely ran late in the ESP queue.
Describe(EnrollmentTerminationOutcome.Succeeded, "in_progress",
out var severity, out _, out var note);
@@ -6,16 +6,19 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.V2.Core.Transport;
using AutopilotMonitor.Shared.Models;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
{
/// <summary>
/// 401/403 classification in <see cref="BackendApiClient"/>: a backend JSON 403 is a real
/// device-authorization verdict, while an HTML body means the response came from the Azure
/// platform / an edge proxy (stopped or retired app) and must NOT be reported as
/// "device is not authorized". Field case sits-d.cloud 2026-08-09: a pre-cutover agent hit
/// the stopped legacy Function App and support chased a non-existent authorization problem.
/// 401/403 classification in <see cref="BackendApiClient"/>: only a response that echoes the
/// request's correlation id is the backend's device-authorization verdict. The platform page of
/// a stopped app (field case 2026-08-09: a pre-cutover agent hit the stopped legacy Function App
/// and support chased a non-existent authorization problem) and anything answered on the network
/// path (TLS inspection: "Client Certificate Required", proxy pages) must NOT be reported as
/// "device is not authorized".
/// </summary>
public sealed class BackendApiClientAuthClassificationTests
{
@@ -24,17 +27,32 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
private readonly Func<HttpRequestMessage, HttpResponseMessage> _responder;
public StubHandler(Func<HttpRequestMessage, HttpResponseMessage> responder) => _responder = responder;
public int Calls { get; private set; }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
=> Task.FromResult(_responder(request));
{
Calls++;
var response = _responder(request);
response.RequestMessage = request; // as HttpClientHandler does
return Task.FromResult(response);
}
}
private static BackendApiClient BuildClient(HttpStatusCode status, string body, string mediaType)
{
var handler = new StubHandler(_ => new HttpResponseMessage(status)
/// <param name="fromBackend">
/// True: the response echoes the request's X-Correlation-ID like the backend's middleware.
/// False: a platform or proxy answer without the echo.
/// </param>
private static StubHandler Answer(HttpStatusCode status, string body, string mediaType, bool fromBackend) =>
new StubHandler(request =>
{
Content = new StringContent(body, Encoding.UTF8, mediaType)
var response = new HttpResponseMessage(status) { Content = new StringContent(body, Encoding.UTF8, mediaType) };
if (fromBackend && request.Headers.TryGetValues(ResponseOriginClassifier.CorrelationIdHeader, out var ids))
response.Headers.TryAddWithoutValidation(ResponseOriginClassifier.CorrelationIdHeader, ids);
return response;
});
return new BackendApiClient(
private static BackendApiClient BuildClient(HttpMessageHandler handler, ServerCertificateObserver? serverCertificates = null) =>
new BackendApiClient(
new HttpClient(handler),
baseUrl: "https://backend.example",
manufacturer: "Contoso",
@@ -43,8 +61,11 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
useBootstrapTokenAuth: false,
bootstrapToken: null,
agentVersion: "2.0.0",
logger: null);
}
logger: null,
serverCertificates: serverCertificates);
private static BackendApiClient BuildClient(HttpStatusCode status, string body, string mediaType, bool fromBackend = true) =>
BuildClient(Answer(status, body, mediaType, fromBackend));
[Fact]
public async Task Backend_json_403_is_reported_as_device_not_authorized()
@@ -134,16 +155,17 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
public async Task Platform_html_403_is_reported_as_endpoint_unavailable_with_page_title()
{
using var client = BuildClient(
HttpStatusCode.Forbidden,
"<!DOCTYPE html><html><head><title>Web App - Unavailable</title></head><body>stopped</body></html>",
"text/html");
HttpStatusCode.Forbidden, InterceptionFixtures.StoppedAppPage, "text/html", fromBackend: false);
var ex = await Assert.ThrowsAsync<BackendAuthException>(() => client.GetAgentConfigAsync("tenant-1"));
Assert.Equal(403, ex.StatusCode);
Assert.True(ex.EndpointUnavailable);
Assert.Contains("Web App - Unavailable", ex.Message);
Assert.False(ex.IsForeignResponse);
Assert.Equal(ResponseOrigin.PlatformStopped, ex.Origin);
Assert.Contains("HTML platform error page (\"Web App - Unavailable\")", ex.Message);
Assert.Contains("NOT a device-authorization failure", ex.Message);
Assert.Contains("stale ApiBaseUrl", ex.Message);
Assert.DoesNotContain("Check client certificate", ex.Message);
}
@@ -153,7 +175,8 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
using var client = BuildClient(
HttpStatusCode.Forbidden,
" <html><body>Error 403 - This web app is stopped.</body></html>",
"text/plain");
"text/plain",
fromBackend: false);
var ex = await Assert.ThrowsAsync<BackendAuthException>(() => client.GetAgentConfigAsync("tenant-1"));
@@ -161,6 +184,113 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
Assert.Contains("HTML platform error page", ex.Message);
}
// ── answered on the network path (TLS inspection, proxy) ─────────────
[Fact]
public async Task Certificate_required_page_says_the_certificate_did_not_reach_the_backend()
{
using var client = BuildClient(
HttpStatusCode.Forbidden, InterceptionFixtures.CertificateRequiredPage, "text/html", fromBackend: false);
var ex = await Assert.ThrowsAsync<BackendAuthException>(() => client.GetAgentConfigAsync("tenant-1"));
Assert.Equal(403, ex.StatusCode);
Assert.True(ex.IsForeignResponse);
Assert.False(ex.EndpointUnavailable);
Assert.Equal(ResponseClassification.ClientCertificateRequired, ex.Classification.ForeignReason);
Assert.Null(ex.ErrorCode);
// The cause leads: RegistrationFailureSummary.LastError keeps 256 characters.
Assert.StartsWith("The device certificate did not reach the backend (TLS inspection or a proxy on the network path)", ex.Message);
Assert.Contains("HTML platform error page (\"Web App - Unavailable\")", ex.Message);
Assert.True(ex.Message.IndexOf("Web App - Unavailable", StringComparison.Ordinal) < 256);
Assert.Contains("NOT a device-authorization failure", ex.Message);
Assert.DoesNotContain("stale ApiBaseUrl", ex.Message);
Assert.DoesNotContain("Check client certificate", ex.Message);
}
[Fact]
public async Task Proxy_page_keeps_the_phrase_and_title_ANALYZE_NET_001_matches()
{
using var client = BuildClient(
HttpStatusCode.Forbidden, InterceptionFixtures.ProxyPage, "text/html", fromBackend: false);
var ex = await Assert.ThrowsAsync<BackendAuthException>(
() => client.GetDiagnosticsUploadUrlAsync("tenant-1", "session-1", "package.zip"));
Assert.True(ex.IsForeignResponse);
Assert.Equal(ResponseClassification.ForeignResponse, ex.Classification.ForeignReason);
Assert.Contains("HTML platform error page (\"Example Gateway - Notification\")", ex.Message);
Assert.Contains("403", ex.Message);
Assert.DoesNotContain("stale ApiBaseUrl", ex.Message);
}
[Fact]
public async Task Json_403_without_the_echo_is_foreign_and_its_errorCode_is_ignored()
{
// An errorCode in a body the backend did not author must not drive a session rotation.
using var client = BuildClient(
HttpStatusCode.Forbidden,
"{\"success\":false,\"errorCode\":\"session_owner_mismatch\"}",
"application/json",
fromBackend: false);
var ex = await Assert.ThrowsAsync<BackendAuthException>(
() => client.RegisterSessionAsync(new AutopilotMonitor.Shared.Models.SessionRegistration()));
Assert.True(ex.IsForeignResponse);
Assert.Null(ex.ErrorCode);
Assert.Contains("a response without the backend's correlation echo", ex.Message);
}
[Fact]
public async Task Foreign_answer_carries_the_observed_server_certificate_issuer()
{
var observer = new ServerCertificateObserver();
using (var cert = ServerCertificateObserverTests.SelfSigned("CN=Example Inspection CA"))
{
observer.Validate(cert, System.Net.Security.SslPolicyErrors.None);
}
using var client = BuildClient(
Answer(HttpStatusCode.Forbidden, InterceptionFixtures.CertificateRequiredPage, "text/html", fromBackend: false),
observer);
var ex = await Assert.ThrowsAsync<BackendAuthException>(() => client.GetAgentConfigAsync("tenant-1"));
Assert.Equal("CN=Example Inspection CA", ex.Classification.ServerCertIssuer);
}
// ── agent/error: a report the backend never received is not delivered ──
[Theory]
[InlineData(HttpStatusCode.OK, true, true)]
[InlineData(HttpStatusCode.InternalServerError, true, true)] // unchanged: only the round-trip counts
[InlineData(HttpStatusCode.Forbidden, true, true)] // the backend answered
[InlineData(HttpStatusCode.Forbidden, false, false)] // answered on the network path
[InlineData(HttpStatusCode.Unauthorized, false, false)]
public async Task ReportAgentError_counts_as_delivered_unless_the_backend_never_answered(
HttpStatusCode status, bool fromBackend, bool expectedDelivered)
{
using var client = BuildClient(status, InterceptionFixtures.CertificateRequiredPage, "text/html", fromBackend);
var delivered = await client.ReportAgentErrorAsync(new AgentErrorReport { TenantId = "tenant-1", SessionId = "session-1" });
Assert.Equal(expectedDelivered, delivered);
}
[Fact]
public async Task Intercepted_emergency_report_rolls_its_reservation_back()
{
var handler = Answer(HttpStatusCode.Forbidden, InterceptionFixtures.CertificateRequiredPage, "text/html", fromBackend: false);
using var client = BuildClient(handler);
var reporter = new EmergencyReporter(client, "session-1", "tenant-1", "2.0.0", logger: null);
await reporter.TrySendAsync(AgentErrorType.ConfigFetchFailed, "first");
await reporter.TrySendAsync(AgentErrorType.ConfigFetchFailed, "second");
// Not delivered → no dedup, no cooldown: the same key is attempted again.
Assert.Equal(2, handler.Calls);
}
[Fact]
public async Task Backend_401_keeps_authorization_message_and_status()
{
@@ -156,6 +156,38 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
Assert.Contains("BackendTelemetryUploader: ingest PERMANENT", logContent);
}
[Fact]
public async Task UploadBatch_logs_the_whole_interception_finding_at_warning()
{
// The old PERMANENT line carried the first 200 body characters, which ended before the
// platform's "Client Certificate Required" text. The INTERCEPTED line names the finding.
using var tmp = new TempDirectory();
var logger = new AgentLogger(tmp.Path, AgentLogLevel.Info);
var handler = new RecordingHttpMessageHandler();
handler.QueueForeign(HttpStatusCode.Forbidden, InterceptionFixtures.CertificateRequiredPage);
using var http = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(30) };
var sut = new BackendTelemetryUploader(
httpClient: http,
baseUrl: "https://backend.test",
tenantId: "T1",
logger: logger);
var result = await sut.UploadBatchAsync(new[] { NewEventItem(1) }, CancellationToken.None);
Assert.True(result.IsIntercepted);
Assert.True(handler.Captured[0].TryGetHeader("X-Correlation-ID", out var corr));
var logContent = ReadLogs(tmp.Path);
Assert.Contains("BackendTelemetryUploader: ingest INTERCEPTED", logContent);
Assert.Contains($"corr={corr}", logContent);
Assert.Contains("status=403", logContent);
Assert.Contains("reason=client_certificate_required", logContent);
Assert.Contains($"pageTitle=\"{InterceptionFixtures.PlatformPageTitle}\"", logContent);
Assert.Contains("serverCertIssuer=\"(unknown)\"", logContent);
Assert.DoesNotContain("ingest PERMANENT", logContent);
}
[Fact]
public async Task UploadBatch_empty_batch_no_log_no_network()
{
@@ -4,7 +4,16 @@ using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Logging;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Telemetry.Periodic;
using AutopilotMonitor.Agent.V2.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.V2.Core.Orchestration;
using AutopilotMonitor.Agent.V2.Core.Security;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Tests.Orchestration;
using AutopilotMonitor.Agent.V2.Core.Transport;
using AutopilotMonitor.Agent.V2.Core.Transport.Telemetry;
using AutopilotMonitor.DecisionCore.Engine;
using Newtonsoft.Json.Linq;
using Xunit;
using AutopilotMonitor.Shared.Models;
@@ -51,7 +60,10 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
string? model = "Latitude",
string? serial = "ABC123",
string? bootstrap = null,
string? version = "11.0.0")
string? version = "11.0.0",
AuthFailureTracker? tracker = null,
ServerCertificateObserver? serverCertificates = null,
NetworkMetrics? networkMetrics = null)
{
Http = new HttpClient(Handler) { Timeout = TimeSpan.FromSeconds(30) };
Sut = new BackendTelemetryUploader(
@@ -62,7 +74,10 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
model: model,
serialNumber: serial,
bootstrapToken: bootstrap,
agentVersion: version);
agentVersion: version,
authFailureTracker: tracker,
serverCertificates: serverCertificates,
networkMetrics: networkMetrics);
}
public void Dispose()
@@ -245,6 +260,85 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
Assert.Null(result.ErrorReason);
}
// ============================================================ 2xx without the correlation echo
[Fact]
public async Task Ok_with_the_echo_is_a_success_and_not_counted()
{
var metrics = new NetworkMetrics();
using var rig = new Rig(networkMetrics: metrics);
rig.Handler.QueueStatus(HttpStatusCode.OK);
var result = await rig.Sut.UploadBatchAsync(new[] { NewEventItem(1) }, CancellationToken.None);
Assert.True(result.Success);
Assert.Equal(0, metrics.OkWithoutEchoCount);
}
[Fact]
public async Task Ok_without_the_echo_is_still_a_success_and_only_counted()
{
// A 200 that did not come from the backend (e.g. a block page served with 200): the
// upload path behaves exactly as before — control signals are still read — and the
// only difference is the counter.
var metrics = new NetworkMetrics();
using var tmp = new TempDirectory();
var tracker = new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 0, SystemClock.Instance,
new AgentLogger(System.IO.Path.Combine(tmp.Path, "logs"), AgentLogLevel.Info));
tracker.RecordFailure(403, "agent/telemetry");
using var rig = new Rig(tracker: tracker, networkMetrics: metrics);
rig.Handler.QueueForeign(HttpStatusCode.OK, "<html><title>Example</title></html>");
rig.Handler.QueueForeign(HttpStatusCode.OK, "{\"deviceKillSignal\":true}", mediaType: "application/json");
var page = await rig.Sut.UploadBatchAsync(new[] { NewEventItem(1) }, CancellationToken.None);
var signals = await rig.Sut.UploadBatchAsync(new[] { NewEventItem(2) }, CancellationToken.None);
Assert.True(page.Success);
Assert.Null(page.ErrorReason);
Assert.True(signals.Success);
Assert.True(signals.DeviceKillSignal);
Assert.Equal(0, tracker.ConsecutiveFailures); // a 2xx still resets the auth streak
Assert.Equal(2, metrics.OkWithoutEchoCount);
}
[Fact]
public async Task Answers_other_than_2xx_never_count_as_ok_without_echo()
{
var metrics = new NetworkMetrics();
using var rig = new Rig(networkMetrics: metrics);
rig.Handler.QueueForeign(HttpStatusCode.Forbidden, InterceptionFixtures.CertificateRequiredPage);
rig.Handler.QueueForeign(HttpStatusCode.ServiceUnavailable, "<html>busy</html>");
await rig.Sut.UploadBatchAsync(new[] { NewEventItem(1) }, CancellationToken.None);
await rig.Sut.UploadBatchAsync(new[] { NewEventItem(1) }, CancellationToken.None);
Assert.Equal(0, metrics.OkWithoutEchoCount);
}
[Fact]
public async Task The_count_reaches_the_agent_metrics_snapshot()
{
var metrics = new NetworkMetrics();
using var rig = new Rig(networkMetrics: metrics);
rig.Handler.QueueForeign(HttpStatusCode.OK, "<html><title>Example</title></html>");
await rig.Sut.UploadBatchAsync(new[] { NewEventItem(1) }, CancellationToken.None);
var sink = new FakeSignalIngressSink();
using var tmp = new TempDirectory();
var collector = new AgentSelfMetricsCollector(
sessionId: "S1",
tenantId: "T1",
post: new InformationalEventPost(sink, SystemClock.Instance),
networkMetrics: metrics,
logger: new AgentLogger(System.IO.Path.Combine(tmp.Path, "logs"), AgentLogLevel.Info),
agentVersion: "2.0.test");
collector.CollectSafe();
var snapshot = (IDictionary<string, object>)Assert.Single(sink.Posted, p =>
p.TypedPayload is IDictionary<string, object> d && d.ContainsKey("agent_version")).TypedPayload!;
Assert.Equal(1L, snapshot["telemetry_upload_ok_without_echo"]);
}
[Theory]
[InlineData(HttpStatusCode.Unauthorized)]
[InlineData(HttpStatusCode.Forbidden)]
@@ -261,6 +355,104 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
Assert.Contains(((int)status).ToString(), result.ErrorReason);
}
// ============================================================ 401/403 origin
private sealed class OriginRig : IDisposable
{
private readonly TempDirectory _tmp = new TempDirectory();
public FakeDistressReporter Distress { get; } = new FakeDistressReporter();
public AuthFailureTracker Tracker { get; }
public Rig Upload { get; }
public OriginRig()
{
Tracker = new AuthFailureTracker(maxFailures: 5, timeoutMinutes: 0, SystemClock.Instance,
new AgentLogger(_tmp.Path, AgentLogLevel.Info), Distress);
var serverCertificates = new ServerCertificateObserver();
using (var cert = ServerCertificateObserverTests.SelfSigned("CN=Example Inspection CA"))
{
serverCertificates.Validate(cert, System.Net.Security.SslPolicyErrors.None);
}
Upload = new Rig(tracker: Tracker, serverCertificates: serverCertificates);
}
public Task<UploadResult> UploadAsync() => Upload.Sut.UploadBatchAsync(new[] { NewEventItem(1) }, CancellationToken.None);
public void Dispose()
{
Upload.Dispose();
_tmp.Dispose();
}
}
[Fact]
public async Task Backend_403_is_an_auth_failure_that_counts_and_reports_DeviceNotRegistered()
{
using var rig = new OriginRig();
rig.Upload.Handler.QueueStatus(HttpStatusCode.Forbidden, body: "{\"error\":\"Device validation failed\"}");
var result = await rig.UploadAsync();
Assert.True(result.IsAuthFailure);
Assert.False(result.IsIntercepted);
Assert.Equal(1, rig.Tracker.ConsecutiveFailures);
Assert.Equal(DistressErrorType.DeviceNotRegistered, Assert.Single(rig.Distress.Calls).ErrorType);
}
[Fact]
public async Task Stopped_app_403_counts_like_an_auth_failure_without_distress()
{
using var rig = new OriginRig();
rig.Upload.Handler.QueueForeign(HttpStatusCode.Forbidden, InterceptionFixtures.StoppedAppPage);
var result = await rig.UploadAsync();
Assert.True(result.IsAuthFailure);
Assert.False(result.IsIntercepted);
Assert.Equal(1, rig.Tracker.ConsecutiveFailures);
Assert.Empty(rig.Distress.Calls);
}
[Fact]
public async Task Certificate_required_403_is_intercepted_never_counted_and_reported_once()
{
using var rig = new OriginRig();
rig.Upload.Handler.QueueForeign(HttpStatusCode.Forbidden, InterceptionFixtures.CertificateRequiredPage);
rig.Upload.Handler.QueueForeign(HttpStatusCode.Forbidden, InterceptionFixtures.CertificateRequiredPage);
var result = await rig.UploadAsync();
await rig.UploadAsync();
Assert.False(result.Success);
Assert.True(result.IsIntercepted);
Assert.False(result.IsAuthFailure);
Assert.False(result.IsTransient);
Assert.Equal(403, result.Interception!.StatusCode);
Assert.Equal(ResponseClassification.ClientCertificateRequired, result.Interception.ForeignReason);
Assert.Equal(InterceptionFixtures.PlatformPageTitle, result.Interception.PageTitle);
Assert.Equal("CN=Example Inspection CA", result.Interception.ServerCertIssuer);
Assert.Equal(0, rig.Tracker.ConsecutiveFailures);
var distress = Assert.Single(rig.Distress.Calls);
Assert.Equal(DistressErrorType.ClientCertificateNotReceived, distress.ErrorType);
Assert.Contains("agent/telemetry http 403 client_certificate_required", distress.Message);
Assert.Contains("CN=Example Inspection CA", distress.Message);
}
[Fact]
public async Task Proxy_page_403_is_intercepted_as_foreign_response()
{
using var rig = new OriginRig();
rig.Upload.Handler.QueueForeign(HttpStatusCode.Forbidden, InterceptionFixtures.ProxyPage);
var result = await rig.UploadAsync();
Assert.True(result.IsIntercepted);
Assert.Equal(ResponseClassification.ForeignResponse, result.Interception!.ForeignReason);
Assert.Equal(InterceptionFixtures.ProxyPageTitle, result.Interception.PageTitle);
Assert.Equal(0, rig.Tracker.ConsecutiveFailures);
}
[Theory]
[InlineData(HttpStatusCode.BadRequest)] // 400
[InlineData(HttpStatusCode.NotFound)] // 404
@@ -275,9 +467,44 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
Assert.False(result.Success);
Assert.False(result.IsTransient);
Assert.False(result.IsSessionGone);
Assert.Contains(((int)status).ToString(), result.ErrorReason);
}
[Fact]
public async Task Gone_410_maps_to_SessionGone()
{
// 410 = the backend no longer knows this session. Not transient (no retry), not auth,
// not poison, not a size problem — a distinct terminal kind the drain reacts to.
using var rig = new Rig();
rig.Handler.QueueStatus(HttpStatusCode.Gone, body: "session unknown");
var result = await rig.Sut.UploadBatchAsync(new[] { NewEventItem(1) }, CancellationToken.None);
Assert.False(result.Success);
Assert.True(result.IsSessionGone);
Assert.False(result.IsTransient);
Assert.False(result.IsAuthFailure);
Assert.False(result.IsPoison);
Assert.False(result.RequiresSplit);
Assert.Contains("410", result.ErrorReason);
}
[Fact]
public async Task Gone_410_with_poison_body_is_still_SessionGone_never_poison()
{
// A poison body on 410 must not be honoured: dropping items + emitting
// telemetry_upload_poisoned would only produce the next 410 (emit/reject loop).
using var rig = new Rig();
rig.Handler.QueueStatus(HttpStatusCode.Gone, body: "{\"poison\":true,\"rejectedRowKeys\":[\"rk1\"],\"reason\":\"x\"}");
var result = await rig.Sut.UploadBatchAsync(new[] { NewEventItem(1) }, CancellationToken.None);
Assert.True(result.IsSessionGone);
Assert.False(result.IsPoison);
Assert.Null(result.PoisonRowKeys);
}
[Theory]
[InlineData(HttpStatusCode.RequestTimeout)] // 408
[InlineData((HttpStatusCode)429)] // TooManyRequests
@@ -68,6 +68,23 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
return this;
}
public FakeBackendTelemetryUploader QueueSessionGone(string reason = "session gone: http 410")
{
_script.Enqueue(_ => UploadResult.SessionGone(reason));
return this;
}
/// <summary>Queues a 403 the platform's "Client Certificate Required" page answered (an interception).</summary>
public FakeBackendTelemetryUploader QueueIntercepted(int times = 1)
{
for (int i = 0; i < times; i++)
{
_script.Enqueue(_ => UploadResult.Intercepted(
"intercepted: http 403 (client_certificate_required)", InterceptionFixtures.CertificateRequired()));
}
return this;
}
public FakeBackendTelemetryUploader QueueThrow(Exception ex)
{
_script.Enqueue(_ => throw ex);
@@ -0,0 +1,48 @@
using AutopilotMonitor.Agent.V2.Core.Transport;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
{
/// <summary>
/// Synthetic copies of the pages an agent gets instead of a backend answer: the Azure platform
/// page when no client certificate arrived, the platform page of a stopped app, and a proxy's
/// block page. Rebuilt from the observed structure and wording; none of them is a captured
/// artifact. The style block keeps the platform marker text past the first 200 characters, where
/// the old upload log excerpt ended.
/// </summary>
internal static class InterceptionFixtures
{
public const string PlatformPageTitle = "Web App - Unavailable";
public const string ProxyPageTitle = "Example Gateway - Notification";
public const string InspectionIssuer = "CN=Example Inspection CA, O=Example Security Ltd, C=US";
private const string PlatformPageHead =
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n<title>Web App - Unavailable</title>\r\n" +
"<style type=\"text/css\">\r\n html { height: 100%; width: 100%; }\r\n" +
" #content { margin: 0 auto; padding: 40px 0 0; font-family: \"Segoe UI\", Arial, sans-serif; }\r\n" +
" h3 { color: #3a3a3a; font-size: 26px; font-weight: 300; }\r\n</style>\r\n</head>\r\n<body>\r\n<div id=\"content\">\r\n";
public const string CertificateRequiredPage =
PlatformPageHead +
"<h3>Error 403 - Forbidden: Client Certificate Required.</h3>\r\n" +
"<p>The web app you have attempted to reach requires a client certificate and does not accept any requests without one.</p>\r\n" +
"</div>\r\n</body>\r\n</html>\r\n";
public const string StoppedAppPage =
PlatformPageHead +
"<h3>Error 403 - This web app is stopped.</h3>\r\n" +
"<p>The web app you have attempted to reach is currently stopped and does not accept any requests.</p>\r\n" +
"</div>\r\n</body>\r\n</html>\r\n";
public const string ProxyPage =
"<!DOCTYPE html>\r\n<html><head><meta charset=\"utf-8\"><title>Example Gateway - Notification</title></head>\r\n" +
"<body><h1>Access blocked</h1><p>The request was blocked by the security policy of your organization.</p></body></html>\r\n";
/// <summary>The platform's "Client Certificate Required" page seen through an inspecting proxy.</summary>
public static ResponseClassification CertificateRequired(int statusCode = 403) =>
ResponseClassification.ForForeign(statusCode, ResponseClassification.ClientCertificateRequired, PlatformPageTitle, isHtml: true, InspectionIssuer);
public static ResponseClassification StoppedApp(int statusCode = 403) =>
ResponseClassification.ForPlatformStopped(statusCode, PlatformPageTitle, isHtml: true, serverCertIssuer: null);
}
}
@@ -7,6 +7,7 @@ using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Transport;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
{
@@ -23,17 +24,33 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
public IReadOnlyList<CapturedRequest> Captured => _captured;
/// <summary>
/// Queues a response from the backend: like the backend's correlation middleware, it echoes
/// the request's <c>X-Correlation-ID</c> — the proof of origin a 401/403 is classified by.
/// </summary>
public RecordingHttpMessageHandler QueueStatus(HttpStatusCode status, string? body = null)
{
_script.Enqueue(_ =>
_script.Enqueue(request =>
{
var resp = new HttpResponseMessage(status);
if (body != null) resp.Content = new StringContent(body, Encoding.UTF8, "application/json");
if (request.Headers.TryGetValues(ResponseOriginClassifier.CorrelationIdHeader, out var ids))
resp.Headers.TryAddWithoutValidation(ResponseOriginClassifier.CorrelationIdHeader, ids);
return resp;
});
return this;
}
/// <summary>
/// Queues a response that something on the network path produced (platform page, proxy
/// page): no correlation echo.
/// </summary>
public RecordingHttpMessageHandler QueueForeign(HttpStatusCode status, string body, string mediaType = "text/html")
{
_script.Enqueue(_ => new HttpResponseMessage(status) { Content = new StringContent(body, Encoding.UTF8, mediaType) });
return this;
}
public RecordingHttpMessageHandler QueueThrow(Exception ex)
{
_script.Enqueue(_ => throw ex);
@@ -0,0 +1,173 @@
#nullable enable
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Transport;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
{
/// <summary>
/// <see cref="ResponseOriginClassifier"/>: only the echo of the request's correlation id proves
/// that a 401/403 came from the backend. Fixtures are synthetic copies of the platform and proxy
/// pages (<see cref="InterceptionFixtures"/>).
/// </summary>
public sealed class ResponseOriginClassifierTests
{
private const string SentId = "0123456789abcdef0123456789abcdef";
private static HttpResponseMessage Response(HttpStatusCode status, string? body, string mediaType, string? echo = null)
{
var response = new HttpResponseMessage(status);
if (body != null) response.Content = new StringContent(body, Encoding.UTF8, mediaType);
if (echo != null) response.Headers.TryAddWithoutValidation(ResponseOriginClassifier.CorrelationIdHeader, echo);
return response;
}
private static ResponseClassification Classify(HttpResponseMessage response, string? issuer = null)
{
var body = ResponseOriginClassifier.ReadBodyPrefixAsync(response).GetAwaiter().GetResult();
return ResponseOriginClassifier.Classify(response, SentId, body, issuer);
}
[Fact]
public void Platform_certificate_required_page_is_foreign_client_certificate_required()
{
using var response = Response(HttpStatusCode.Forbidden, InterceptionFixtures.CertificateRequiredPage, "text/html");
var c = Classify(response, InterceptionFixtures.InspectionIssuer);
Assert.Equal(ResponseOrigin.Foreign, c.Origin);
Assert.True(c.IsForeign);
Assert.Equal(ResponseClassification.ClientCertificateRequired, c.ForeignReason);
Assert.Equal(InterceptionFixtures.PlatformPageTitle, c.PageTitle);
Assert.True(c.IsHtml);
Assert.Equal(403, c.StatusCode);
Assert.Equal(InterceptionFixtures.InspectionIssuer, c.ServerCertIssuer);
}
[Fact]
public void Platform_stopped_app_page_is_platform_stopped()
{
using var response = Response(HttpStatusCode.Forbidden, InterceptionFixtures.StoppedAppPage, "text/html");
var c = Classify(response);
Assert.Equal(ResponseOrigin.PlatformStopped, c.Origin);
Assert.False(c.IsForeign);
Assert.Null(c.ForeignReason);
Assert.Equal(InterceptionFixtures.PlatformPageTitle, c.PageTitle);
}
[Fact]
public void Proxy_page_is_foreign_response_with_its_title()
{
using var response = Response(HttpStatusCode.Forbidden, InterceptionFixtures.ProxyPage, "text/html");
var c = Classify(response);
Assert.Equal(ResponseOrigin.Foreign, c.Origin);
Assert.Equal(ResponseClassification.ForeignResponse, c.ForeignReason);
Assert.Equal(InterceptionFixtures.ProxyPageTitle, c.PageTitle);
}
[Fact]
public void Json_403_with_echoed_correlation_id_is_backend()
{
using var response = Response(HttpStatusCode.Forbidden, "{\"error\":\"Device validation failed\"}", "application/json", echo: SentId);
Assert.Equal(ResponseOrigin.Backend, Classify(response).Origin);
}
[Fact]
public void Json_403_without_echo_is_foreign_response()
{
using var response = Response(HttpStatusCode.Forbidden, "{\"error\":\"Device validation failed\"}", "application/json");
var c = Classify(response);
Assert.Equal(ResponseOrigin.Foreign, c.Origin);
Assert.Equal(ResponseClassification.ForeignResponse, c.ForeignReason);
Assert.False(c.IsHtml);
Assert.Null(c.PageTitle);
}
[Fact]
public void Html_page_with_echo_is_backend_because_the_header_wins()
{
using var response = Response(HttpStatusCode.Forbidden, InterceptionFixtures.CertificateRequiredPage, "text/html", echo: SentId);
Assert.Equal(ResponseOrigin.Backend, Classify(response).Origin);
}
[Fact]
public void Echo_of_another_request_id_is_not_proof()
{
using var response = Response(HttpStatusCode.Unauthorized, "{}", "application/json", echo: "ffffffffffffffffffffffffffffffff");
Assert.Equal(ResponseOrigin.Foreign, Classify(response).Origin);
}
[Fact]
public void No_sent_id_can_never_be_echoed()
{
using var response = Response(HttpStatusCode.Forbidden, "{}", "application/json", echo: SentId);
Assert.Equal(ResponseOrigin.Foreign, ResponseOriginClassifier.Classify(response, sentCorrelationId: null, bodyPrefix: "{}").Origin);
}
[Fact]
public void Markers_match_case_insensitively_and_in_plain_text()
{
using var stopped = Response(HttpStatusCode.Forbidden, "error 403 - this web app is stopped.", "text/plain");
using var certificate = Response(HttpStatusCode.Forbidden, "Forbidden: CLIENT CERTIFICATE REQUIRED", "text/plain");
Assert.Equal(ResponseOrigin.PlatformStopped, Classify(stopped).Origin);
var c = Classify(certificate);
Assert.Equal(ResponseClassification.ClientCertificateRequired, c.ForeignReason);
Assert.False(c.IsHtml);
}
[Fact]
public void Html_served_with_generic_content_type_is_sniffed()
{
using var response = Response(HttpStatusCode.Forbidden, " <html><head><title>Blocked</title></head></html>", "text/plain");
var c = Classify(response);
Assert.True(c.IsHtml);
Assert.Equal("Blocked", c.PageTitle);
}
[Fact]
public void Page_title_is_capped_at_80_characters()
{
var longTitle = new string('x', 120);
using var response = Response(HttpStatusCode.Forbidden, $"<html><head><title>{longTitle}</title></head></html>", "text/html");
Assert.Equal(new string('x', 80), Classify(response).PageTitle);
}
[Fact]
public async Task Body_prefix_is_capped_and_a_marker_beyond_it_is_not_seen()
{
var body = new string(' ', ResponseOriginClassifier.MaxBodyChars) + "Client Certificate Required";
using var response = Response(HttpStatusCode.Forbidden, body, "text/plain");
var prefix = await ResponseOriginClassifier.ReadBodyPrefixAsync(response);
Assert.Equal(ResponseOriginClassifier.MaxBodyChars, prefix!.Length);
Assert.Equal(ResponseClassification.ForeignResponse, ResponseOriginClassifier.Classify(response, SentId, prefix).ForeignReason);
}
[Fact]
public async Task Missing_body_reads_as_null_and_classifies_as_foreign_response()
{
using var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
Assert.Null(await ResponseOriginClassifier.ReadBodyPrefixAsync(response));
Assert.Equal(ResponseClassification.ForeignResponse, Classify(response).ForeignReason);
}
}
}
@@ -0,0 +1,94 @@
#nullable enable
using System;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using AutopilotMonitor.Agent.V2.Core.Transport;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
{
/// <summary>
/// <see cref="ServerCertificateObserver"/> must never weaken TLS validation: the installed
/// callback returns exactly the verdict .NET Framework applies without one
/// (<c>sslPolicyErrors == None</c>) and only records the issuer as evidence.
/// </summary>
public sealed class ServerCertificateObserverTests
{
internal static X509Certificate2 SelfSigned(string subject)
{
using (var rsa = RSA.Create(2048))
{
var request = new CertificateRequest(subject, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
return request.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1));
}
}
private static Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> AttachedCallback(ServerCertificateObserver observer)
{
var handler = new HttpClientHandler();
observer.Attach(handler);
return handler.ServerCertificateCustomValidationCallback!;
}
[Theory]
[InlineData(SslPolicyErrors.None, true)]
[InlineData(SslPolicyErrors.RemoteCertificateNotAvailable, false)]
[InlineData(SslPolicyErrors.RemoteCertificateNameMismatch, false)]
[InlineData(SslPolicyErrors.RemoteCertificateChainErrors, false)]
[InlineData(SslPolicyErrors.RemoteCertificateNotAvailable | SslPolicyErrors.RemoteCertificateNameMismatch, false)]
[InlineData(SslPolicyErrors.RemoteCertificateNotAvailable | SslPolicyErrors.RemoteCertificateChainErrors, false)]
[InlineData(SslPolicyErrors.RemoteCertificateNameMismatch | SslPolicyErrors.RemoteCertificateChainErrors, false)]
[InlineData(SslPolicyErrors.RemoteCertificateNotAvailable | SslPolicyErrors.RemoteCertificateNameMismatch | SslPolicyErrors.RemoteCertificateChainErrors, false)]
public void Callback_accepts_only_a_certificate_without_policy_errors(SslPolicyErrors errors, bool expected)
{
using var certificate = SelfSigned("CN=Example Inspection CA");
var observer = new ServerCertificateObserver();
var callback = AttachedCallback(observer);
Assert.Equal(expected, callback(null!, certificate, null!, errors));
// Recorded whatever the verdict: a rejected inspection certificate is evidence too.
Assert.Equal(certificate.Issuer, observer.LastIssuer);
}
[Fact]
public void Last_validated_connection_wins()
{
using var first = SelfSigned("CN=Example Backend CA");
using var second = SelfSigned("CN=Example Inspection CA");
var observer = new ServerCertificateObserver();
var callback = AttachedCallback(observer);
Assert.Null(observer.LastIssuer);
callback(null!, first, null!, SslPolicyErrors.None);
callback(null!, second, null!, SslPolicyErrors.None);
Assert.Equal(second.Issuer, observer.LastIssuer);
}
[Fact]
public void Missing_certificate_is_rejected_and_keeps_the_last_issuer()
{
using var certificate = SelfSigned("CN=Example Backend CA");
var observer = new ServerCertificateObserver();
var callback = AttachedCallback(observer);
callback(null!, certificate, null!, SslPolicyErrors.None);
Assert.False(callback(null!, null!, null!, SslPolicyErrors.RemoteCertificateNotAvailable));
Assert.Equal(certificate.Issuer, observer.LastIssuer);
}
[Fact]
public void Long_issuer_is_capped()
{
using var certificate = SelfSigned($"CN=Example Inspection CA, O={new string('o', 200)}");
var observer = new ServerCertificateObserver();
observer.Validate(certificate, SslPolicyErrors.None);
Assert.Equal(ServerCertificateObserver.MaxIssuerLength, observer.LastIssuer!.Length);
Assert.StartsWith(observer.LastIssuer, certificate.Issuer, StringComparison.Ordinal);
}
}
}
@@ -10,10 +10,15 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
{
public sealed class TelemetrySpoolTests
{
private static TelemetryItemDraft EventDraft(string rowKey = "row-1") =>
private const string OwnKey = "tenant_session";
private const string ForeignKey = "tenant_old-session";
private static TelemetryItemDraft EventDraft(string rowKey = "row-1") => EventDraft(rowKey, OwnKey);
private static TelemetryItemDraft EventDraft(string rowKey, string partitionKey) =>
new TelemetryItemDraft(
kind: TelemetryItemKind.Event,
partitionKey: "tenant_session",
partitionKey: partitionKey,
rowKey: rowKey,
payloadJson: "{\"EventType\":\"agent_started\"}",
isSessionScoped: true);
@@ -195,6 +200,86 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
Assert.Equal(1, next.TelemetryItemId);
}
// ============================================================ Startup partition-key filter
[Fact]
public void Ctor_drops_leading_pending_items_of_another_session_and_persists_cursor()
{
// Same spool directory, new session id: the previous session's unsent items would only
// draw a 410 that must not end the new session. They are dropped on startup; the own
// item behind them stays pending.
using var tmp = new TempDirectory();
var s1 = new TelemetrySpool(tmp.Path, Clock());
s1.Enqueue(EventDraft("old-a", ForeignKey));
s1.Enqueue(EventDraft("old-b", ForeignKey));
s1.Enqueue(EventDraft("own", OwnKey));
var s2 = new TelemetrySpool(tmp.Path, Clock(), expectedPartitionKey: OwnKey);
Assert.Equal(1, s2.LastUploadedItemId);
Assert.Equal(2, s2.LastAssignedItemId);
Assert.Equal(1, s2.PendingItemCount);
Assert.Equal("own", Assert.Single(s2.Peek(10)).RowKey);
// Cursor persisted: a later start without the filter sees the same tail with the same bytes.
var s3 = new TelemetrySpool(tmp.Path, Clock());
Assert.Equal(1, s3.LastUploadedItemId);
Assert.Equal(1, s3.PendingItemCount);
Assert.Equal(s3.PendingBytes, s2.PendingBytes);
var next = s2.Enqueue(EventDraft("next", OwnKey));
Assert.Equal(3, next.TelemetryItemId);
}
[Fact]
public void Ctor_drops_nothing_when_a_foreign_item_follows_an_own_item()
{
// Impossible by construction (one process = one session); if it happens the order is
// not trustworthy, so the filter keeps everything instead of guessing.
using var tmp = new TempDirectory();
var s1 = new TelemetrySpool(tmp.Path, Clock());
s1.Enqueue(EventDraft("old-a", ForeignKey));
s1.Enqueue(EventDraft("own", OwnKey));
s1.Enqueue(EventDraft("old-b", ForeignKey));
var s2 = new TelemetrySpool(tmp.Path, Clock(), expectedPartitionKey: OwnKey);
Assert.Equal(-1, s2.LastUploadedItemId);
Assert.Equal(3, s2.PendingItemCount);
Assert.Equal(s1.PendingBytes, s2.PendingBytes);
}
[Fact]
public void Ctor_keeps_pending_items_that_carry_the_expected_key()
{
// WhiteGlove Part 2 reuses the Part-1 session id: its leftovers carry the expected key.
using var tmp = new TempDirectory();
var s1 = new TelemetrySpool(tmp.Path, Clock());
s1.Enqueue(EventDraft("a", OwnKey));
s1.Enqueue(EventDraft("b", OwnKey));
s1.Enqueue(EventDraft("c", OwnKey));
var s2 = new TelemetrySpool(tmp.Path, Clock(), expectedPartitionKey: OwnKey);
Assert.Equal(-1, s2.LastUploadedItemId);
Assert.Equal(3, s2.PendingItemCount);
Assert.Equal(s1.PendingBytes, s2.PendingBytes);
}
[Fact]
public void Ctor_without_expected_key_keeps_foreign_items()
{
using var tmp = new TempDirectory();
var s1 = new TelemetrySpool(tmp.Path, Clock());
s1.Enqueue(EventDraft("old-a", ForeignKey));
s1.Enqueue(EventDraft("old-b", ForeignKey));
var s2 = new TelemetrySpool(tmp.Path, Clock());
Assert.Equal(-1, s2.LastUploadedItemId);
Assert.Equal(2, s2.PendingItemCount);
}
[Fact]
public void Enqueue_with_RequiresImmediateFlush_raises_ImmediateFlushRequested()
{
@@ -0,0 +1,282 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.V2.Core.Tests.Harness;
using AutopilotMonitor.Agent.V2.Core.Transport;
using AutopilotMonitor.Agent.V2.Core.Transport.Telemetry;
using AutopilotMonitor.Shared.Models;
using Xunit;
namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
{
/// <summary>
/// The transport-local pause after an intercepted upload (a 401/403 something on the network
/// path answered): no HTTP while it holds, backoff 30 s → 15 min, a success ends it, the
/// terminal phase gets exactly one attempt despite it, and it is never an auth failure.
/// Driven by the virtual clock — the pause is a "not before", never a timer.
/// </summary>
public sealed class TelemetryUploadOrchestratorInterceptionTests
{
private static readonly DateTime Start = new DateTime(2026, 10, 12, 10, 0, 0, DateTimeKind.Utc);
private sealed class Rig : IDisposable
{
private int _rowKey;
public TempDirectory Tmp { get; } = new TempDirectory();
public VirtualClock Clock { get; } = new VirtualClock(Start);
public FakeBackendTelemetryUploader Uploader { get; } = new FakeBackendTelemetryUploader();
public TelemetryUploadOrchestrator Sut { get; }
public Rig()
{
var spool = new TelemetrySpool(Tmp.Path, Clock);
Sut = new TelemetryUploadOrchestrator(spool, Uploader, Clock, batchSize: 10, retryBackoffs: new[] { TimeSpan.Zero });
}
public void Enqueue(int count = 1)
{
for (int i = 0; i < count; i++)
{
var rowKey = (++_rowKey).ToString("D10");
Sut.Enqueue(new TelemetryItemDraft(
kind: TelemetryItemKind.Event,
partitionKey: "T1_S1",
rowKey: rowKey,
payloadJson: "{}",
isSessionScoped: true,
requiresImmediateFlush: false));
}
}
public void Dispose() { Sut.Dispose(); Tmp.Dispose(); }
}
[Fact]
public async Task Intercepted_upload_retains_the_batch_and_later_drains_make_no_http_call()
{
using var rig = new Rig();
rig.Enqueue(2);
rig.Uploader.QueueIntercepted();
var first = await rig.Sut.DrainAllAsync();
Assert.Equal(1, rig.Uploader.CallCount);
Assert.Equal(1, first.FailedBatches);
Assert.Equal(0, first.UploadedItems);
Assert.Contains("intercepted", first.LastErrorReason);
Assert.Equal(-1, rig.Sut.LastUploadedItemId);
Assert.True(rig.Sut.IsInterceptionPaused);
Assert.False(rig.Sut.IsPaused);
// Periodic drains and immediate-flush wakeups (a new item, another drain) during the
// pause: no HTTP, and an expected state rather than another failed batch.
rig.Enqueue();
var paused = await rig.Sut.DrainAllAsync();
rig.Clock.Advance(TimeSpan.FromSeconds(29));
await rig.Sut.DrainAllAsync();
Assert.Equal(1, rig.Uploader.CallCount);
Assert.Equal(0, paused.FailedBatches);
Assert.Contains("upload intercepted, next attempt not before", paused.LastErrorReason);
Assert.Equal(-1, rig.Sut.LastUploadedItemId);
}
[Fact]
public async Task Backoff_runs_30s_1m_2m_5m_10m_then_15m_capped()
{
var expected = new[] { 30, 60, 120, 300, 600, 900, 900 }.Select(s => TimeSpan.FromSeconds(s)).ToArray();
Assert.Equal(expected.Take(6), TelemetryUploadOrchestrator.InterceptionBackoffs);
using var rig = new Rig();
rig.Enqueue();
rig.Uploader.QueueIntercepted(expected.Length);
foreach (var delay in expected)
{
var calls = rig.Uploader.CallCount;
await rig.Sut.DrainAllAsync();
Assert.Equal(calls + 1, rig.Uploader.CallCount);
rig.Clock.Advance(delay - TimeSpan.FromSeconds(1));
Assert.True(rig.Sut.IsInterceptionPaused);
await rig.Sut.DrainAllAsync();
Assert.Equal(calls + 1, rig.Uploader.CallCount);
rig.Clock.Advance(TimeSpan.FromSeconds(1));
Assert.False(rig.Sut.IsInterceptionPaused);
}
}
/// <summary>A clock that can be set back, as NTP does to a device that booted with a future RTC time.</summary>
private sealed class SettableClock : AutopilotMonitor.DecisionCore.Engine.IClock
{
public SettableClock(DateTime utcNow) { UtcNow = utcNow; }
public DateTime UtcNow { get; set; }
public Task Delay(TimeSpan delay, System.Threading.CancellationToken cancellationToken = default) => Task.CompletedTask;
}
[Fact]
public async Task Clock_set_back_past_the_pause_start_ends_the_pause_instead_of_stretching_it()
{
using var tmp = new TempDirectory();
var clock = new SettableClock(Start);
var uploader = new FakeBackendTelemetryUploader().QueueIntercepted(2);
using var sut = new TelemetryUploadOrchestrator(new TelemetrySpool(tmp.Path, clock), uploader, clock, retryBackoffs: new[] { TimeSpan.Zero });
sut.Enqueue(new TelemetryItemDraft(TelemetryItemKind.Event, "T1_S1", "0000000001", "{}", isSessionScoped: true));
await sut.DrainAllAsync();
clock.UtcNow = Start.AddHours(-2);
Assert.False(sut.IsInterceptionPaused);
await sut.DrainAllAsync();
Assert.Equal(2, uploader.CallCount);
Assert.True(sut.IsInterceptionPaused); // re-armed from the corrected clock
}
[Fact]
public async Task Successful_upload_ends_the_pause_and_resets_the_backoff()
{
using var rig = new Rig();
rig.Enqueue(3);
rig.Uploader.QueueIntercepted(2).QueueOk().QueueIntercepted();
await rig.Sut.DrainAllAsync(); // intercepted, 30 s
rig.Clock.Advance(TimeSpan.FromSeconds(30));
await rig.Sut.DrainAllAsync(); // intercepted, 1 min
rig.Clock.Advance(TimeSpan.FromMinutes(1));
var cleared = await rig.Sut.DrainAllAsync(); // the path is clear
Assert.Equal(3, cleared.UploadedItems);
Assert.False(rig.Sut.IsInterceptionPaused);
rig.Enqueue();
await rig.Sut.DrainAllAsync(); // intercepted again: back to 30 s
rig.Clock.Advance(TimeSpan.FromSeconds(29));
Assert.True(rig.Sut.IsInterceptionPaused);
rig.Clock.Advance(TimeSpan.FromSeconds(1));
Assert.False(rig.Sut.IsInterceptionPaused);
}
[Fact]
public async Task Terminal_drain_makes_exactly_one_attempt_despite_the_pause()
{
using var rig = new Rig();
rig.Enqueue();
rig.Uploader.QueueIntercepted(3);
await rig.Sut.DrainAllAsync();
Assert.Equal(1, rig.Uploader.CallCount);
Assert.True(rig.Sut.GrantTerminalAttempt());
var terminal = await rig.Sut.DrainAllAsync();
Assert.Equal(2, rig.Uploader.CallCount);
Assert.Equal(1, terminal.FailedBatches);
Assert.True(rig.Sut.IsInterceptionPaused);
// One attempt per process: a later terminal drain (orchestrator.Stop) honours the pause.
rig.Sut.GrantTerminalAttempt();
await rig.Sut.DrainAllAsync();
Assert.Equal(2, rig.Uploader.CallCount);
}
[Fact]
public async Task Terminal_attempt_that_gets_through_drains_everything()
{
using var rig = new Rig();
rig.Enqueue(25);
rig.Uploader.QueueIntercepted().QueueOk(3);
await rig.Sut.DrainAllAsync();
rig.Sut.GrantTerminalAttempt();
var terminal = await rig.Sut.DrainAllAsync();
Assert.Equal(25, terminal.UploadedItems);
Assert.Equal(4, rig.Uploader.CallCount);
Assert.Equal(24, rig.Sut.LastUploadedItemId);
Assert.False(rig.Sut.IsInterceptionPaused);
}
[Fact]
public async Task Grant_without_a_pause_changes_nothing_until_an_interception_meets_it()
{
using var rig = new Rig();
Assert.False(rig.Sut.GrantTerminalAttempt());
rig.Enqueue();
rig.Uploader.QueueOk().QueueIntercepted(3);
var normal = await rig.Sut.DrainAllAsync();
Assert.Equal(1, normal.UploadedItems);
rig.Enqueue();
await rig.Sut.DrainAllAsync(); // intercepted during the terminal phase
await rig.Sut.DrainAllAsync(); // the granted attempt
await rig.Sut.DrainAllAsync(); // pause honoured again
Assert.Equal(3, rig.Uploader.CallCount);
}
[Fact]
public async Task DeviceBlocked_pause_still_skips_terminal_drains()
{
using var rig = new Rig();
rig.Enqueue();
rig.Uploader.QueueOkWithSignals(deviceBlocked: true, unblockAt: Start.AddMinutes(10));
await rig.Sut.DrainAllAsync();
Assert.True(rig.Sut.IsPaused);
rig.Enqueue();
Assert.False(rig.Sut.GrantTerminalAttempt());
var terminal = await rig.Sut.DrainAllAsync();
Assert.Equal(1, rig.Uploader.CallCount);
Assert.Contains("device blocked", terminal.LastErrorReason);
}
[Fact]
public async Task First_interception_is_reported_once_and_never_as_a_blocked_or_auth_failure()
{
using var rig = new Rig();
rig.Enqueue();
rig.Uploader.QueueIntercepted(2);
var reports = new List<InterceptionReport>();
var blocked = 0;
rig.Sut.UploadIntercepted += (_, r) => reports.Add(r);
rig.Sut.UploadBlocked += (_, __) => blocked++;
await rig.Sut.DrainAllAsync();
rig.Clock.Advance(TimeSpan.FromSeconds(30));
await rig.Sut.DrainAllAsync();
Assert.Equal(2, rig.Uploader.CallCount);
var report = Assert.Single(reports);
Assert.Equal(Start, report.FirstBlockedAtUtc);
Assert.Equal(ResponseClassification.ClientCertificateRequired, report.Interception.ForeignReason);
Assert.Equal(403, report.Interception.StatusCode);
Assert.Equal(InterceptionFixtures.InspectionIssuer, report.Interception.ServerCertIssuer);
Assert.Equal(0, blocked);
}
[Fact]
public async Task Permanent_block_report_keeps_its_own_once_per_process_gate()
{
using var rig = new Rig();
rig.Enqueue();
rig.Uploader.QueueIntercepted().QueuePermanent("http 400: contract").QueuePermanent("http 400: contract");
int intercepted = 0, blocked = 0;
rig.Sut.UploadIntercepted += (_, __) => intercepted++;
rig.Sut.UploadBlocked += (_, __) => blocked++;
await rig.Sut.DrainAllAsync();
rig.Clock.Advance(TimeSpan.FromSeconds(30));
await rig.Sut.DrainAllAsync();
await rig.Sut.DrainAllAsync();
Assert.Equal(3, rig.Uploader.CallCount);
Assert.Equal(1, intercepted);
Assert.Equal(1, blocked);
}
}
}
@@ -10,10 +10,14 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
{
public sealed class TelemetryUploadOrchestratorTests
{
private static TelemetryItemDraft Draft(string rowKey) =>
private const string OwnKey = "tenant_session";
private static TelemetryItemDraft Draft(string rowKey) => Draft(rowKey, OwnKey);
private static TelemetryItemDraft Draft(string rowKey, string partitionKey) =>
new TelemetryItemDraft(
kind: TelemetryItemKind.Event,
partitionKey: "tenant_session",
partitionKey: partitionKey,
rowKey: rowKey,
payloadJson: $"{{\"rk\":\"{rowKey}\"}}",
isSessionScoped: true);
@@ -157,6 +161,118 @@ namespace AutopilotMonitor.Agent.V2.Core.Tests.Transport
Assert.Equal(1, blockedCount);
}
// ============================================================ HTTP 410 — session gone
[Fact]
public async Task SessionGone_for_current_session_stops_drain_retains_cursor_and_raises_once()
{
using var tmp = new TempDirectory();
var spool = new TelemetrySpool(tmp.Path, Clock());
spool.Enqueue(Draft("a"));
spool.Enqueue(Draft("b"));
var uploader = new FakeBackendTelemetryUploader().QueueSessionGone().QueueSessionGone();
using var sut = new TelemetryUploadOrchestrator(spool, uploader, Clock(), currentPartitionKey: OwnKey);
var gone = new System.Collections.Generic.List<string>();
int blockedCount = 0;
PoisonReport? poison = null;
sut.SessionGone += (_, reason) => gone.Add(reason);
sut.UploadBlocked += (_, __) => blockedCount++;
sut.BatchPoisoned += (_, p) => poison = p;
var first = await sut.DrainAllAsync();
Assert.False(first.Success);
Assert.Equal(1, first.FailedBatches);
Assert.Equal(0, first.UploadedItems);
Assert.Equal(1, uploader.CallCount); // no retries — nothing can make a 410 succeed
Assert.Equal(-1, sut.LastUploadedItemId); // retained — items stay on disk for the diag ZIP
Assert.Single(gone);
Assert.Contains("410", gone[0]);
Assert.Equal(0, blockedCount); // not a "blocked" pipeline
Assert.Null(poison);
// One-shot: the next drain hits the 410 again but does not raise again.
var second = await sut.DrainAllAsync();
Assert.False(second.Success);
Assert.Equal(2, uploader.CallCount);
Assert.Equal(-1, sut.LastUploadedItemId);
Assert.Single(gone);
Assert.Equal(0, blockedCount);
}
[Fact]
public async Task SessionGone_for_leading_foreign_items_drops_only_them_and_continues()
{
// Leftovers of a previous session id at the head of the spool (defence in depth — the
// spool filters these at startup). A 410 for them must not end the current session:
// drop exactly the leading foreign run, then carry on with the current session's items.
using var tmp = new TempDirectory();
var spool = new TelemetrySpool(tmp.Path, Clock());
spool.Enqueue(Draft("old-a", "tenant_old-session"));
spool.Enqueue(Draft("old-b", "tenant_old-session"));
spool.Enqueue(Draft("new", OwnKey));
var uploader = new FakeBackendTelemetryUploader().QueueSessionGone().QueueOk();
using var sut = new TelemetryUploadOrchestrator(spool, uploader, Clock(), currentPartitionKey: OwnKey);
int goneCount = 0, blockedCount = 0;
sut.SessionGone += (_, __) => goneCount++;
sut.UploadBlocked += (_, __) => blockedCount++;
var result = await sut.DrainAllAsync();
Assert.True(result.Success);
Assert.Equal(0, result.FailedBatches);
Assert.Equal(1, result.UploadedItems);
Assert.Equal(2, uploader.CallCount);
Assert.Equal(3, uploader.Received[0].Count); // first batch carried all three
Assert.Equal("new", Assert.Single(uploader.Received[1]).RowKey); // retry carried only the own item
Assert.Equal(2, sut.LastUploadedItemId);
Assert.Equal(0, goneCount);
Assert.Equal(0, blockedCount);
}
[Fact]
public async Task SessionGone_is_not_latched_before_a_handler_is_subscribed()
{
// The host subscribes after EnrollmentOrchestrator.Start; a 410 on the very first drain
// must still reach it on the next drain instead of being swallowed by the one-shot guard.
using var tmp = new TempDirectory();
var spool = new TelemetrySpool(tmp.Path, Clock());
spool.Enqueue(Draft("only"));
var uploader = new FakeBackendTelemetryUploader().QueueSessionGone().QueueSessionGone();
using var sut = new TelemetryUploadOrchestrator(spool, uploader, Clock(), currentPartitionKey: OwnKey);
await sut.DrainAllAsync(); // nobody listening yet
int goneCount = 0;
sut.SessionGone += (_, __) => goneCount++;
await sut.DrainAllAsync();
Assert.Equal(1, goneCount);
Assert.Equal(-1, sut.LastUploadedItemId);
}
[Fact]
public async Task SessionGone_without_a_current_key_treats_every_batch_as_own()
{
using var tmp = new TempDirectory();
var spool = new TelemetrySpool(tmp.Path, Clock());
spool.Enqueue(Draft("x", "tenant_whatever"));
var uploader = new FakeBackendTelemetryUploader().QueueSessionGone();
using var sut = new TelemetryUploadOrchestrator(spool, uploader, Clock());
int goneCount = 0;
sut.SessionGone += (_, __) => goneCount++;
var result = await sut.DrainAllAsync();
Assert.False(result.Success);
Assert.Equal(1, goneCount);
Assert.Equal(-1, sut.LastUploadedItemId);
}
[Fact]
public async Task TooLarge_splits_batch_and_uploads_everything()
{

Some files were not shown because too many files have changed in this diff Show More