Compare commits

..

11 Commits

Author SHA1 Message Date
Francesco Renzi
68ffb6e0c8 Merge branch 'main' into dap-execution-view-foundation 2026-05-18 10:25:38 +01:00
Daniel Valdivia
d36839b001 Add support for Ubuntu 26.04 (liblttng-ust1t64, libicu77-80) (#4394)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 10:51:46 -04:00
Francesco Renzi
c72457ad4a Address Copilot review feedback
- Remove unused `using System.Linq;` from JobExecutionViewRendererL0
    and `using System.Collections.Generic;` from StepEntryTranslatorL0.
  - StepEntryTranslator: read run-step inputs through
    `PipelineConstants.ScriptStepInputs.*` (`script`, `shell`,
    `workingDirectory`). The runner stores these keys in their
    constants-defined spelling — see ActionManifestManagerWrapper:244 —
    not their kebab-case workflow-YAML form, so the previous
    `working-directory` lookup never matched in practice. Tests
    updated to use the same constants.
  - TemplateTokenYamlAdapter / JobExecutionViewRenderer.FormatScalar:
    defensively also strip a `---\n` prefix on top of the existing
    `--- ` prefix. Not observed in any input I exercised, but cheap
    insurance against an Emitter quirk on some YAML configurations.
  - JobExecutionView.Append: reject passing both `stepIdentity` and
    `matchKey`. The combination would orphan the original step's
    line mapping the moment TryClaim overwrites `_stepIdentities`
    for a different step. Add an L0 test covering the precondition.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-13 04:22:59 -07:00
Francesco Renzi
79e0e5cbe6 Drop skipped-step annotation from execution view
Marking placeholders as skipped was a special-case treatment for
predicted Post placeholders whose Main step never executed. In practice
this is no different from any step that doesn't run: the debugger
simply never pauses on it, and the IStep→line mapping is only
populated when a placeholder is claimed.

Drop `IsSkipped` from `JobExecutionViewEntry`, the inline
`# (skipped — ...)` rendering, and `JobExecutionView.TryMarkSkipped`.
Placeholders that are never claimed simply stay in the view as
informational entries — the IStep→line dictionary excludes them,
so the debugger never tries to pause on them.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-13 03:50:32 -07:00
Francesco Renzi
fedd9a3089 Force LF line breaks in DAP YAML emitters
`StringWriter` defaults to `Environment.NewLine`, which is CRLF on
Windows. YamlDotNet's `Emitter` calls `WriteLine` internally, so the
emitted YAML mixes CRLF (from the emitter) with the explicit `\n` we
append in the renderer skeleton. That breaks the document-end
stripping in `FormatScalar` and `TemplateTokenYamlAdapter.Serialize`
and corrupts the rendered view on Windows.

Set `sw.NewLine = "\n"` in both emitter entry points so the output
is always LF, regardless of host. Add regression tests asserting no
`\r` appears in the rendered view or in adapter output. The tests
are noop guards on Linux (where `Environment.NewLine` is already
\n) but catch any future regression on the Windows CI matrix.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-13 03:18:06 -07:00
Francesco Renzi
e51d0dfba7 Render basic expressions through ToDisplayString
The workflow parser tokenizes a mixed scalar like
`${{ runner.os }}-primes` as a single BasicExpressionToken whose
internal expression is `format('{0}-primes', runner.os)`. The
adapter was driving TemplateWriter, which calls `ToString()` and
surfaces the parser's normalized rewrite verbatim.

Replace TemplateWriter.Write with a local walker that mirrors it
except for BasicExpressionToken — those go through
`ToDisplayString()`, reversing the format(...) rewrite back to the
authored form. All other token kinds delegate to the same writer
calls TemplateWriter would make.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-13 02:16:17 -07:00
Francesco Renzi
9619f0bf3a Add StepEntryTranslator for IStep to view entry mapping
Translates IStep instances (ActionRunner, JobExtensionRunner, etc.)
into JobExecutionViewEntry. Filters auto-generated step IDs so only
explicit IDs surface in the view, and emits step parameters via the
TemplateTokenYamlAdapter to preserve pre-evaluation expressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-12 13:26:02 -07:00
Francesco Renzi
f5185dd1a2 Add TemplateTokenYamlAdapter for pre-evaluation YAML rendering
IObjectWriter that bridges the runner's TemplateWriter to YamlDotNet
so TemplateToken trees can be serialized with ${{ }} expressions
preserved. Lets the execution view show step parameters (with:, env:,
if:, etc.) exactly as authored in the workflow file, before any
expression evaluation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-12 13:26:02 -07:00
Francesco Renzi
d6b52ac966 Add JobExecutionView state container
Thread-safe append-only container of JobExecutionViewEntry. Supports
predictive Post-step placeholders via TryClaim(matchKey, step) and
TryMarkSkipped(matchKey) so the rendered view can reflect Main steps
that are skipped by their if: condition. Provides line lookup by
IStep for DAP stack-trace frame construction.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-12 13:26:02 -07:00
Francesco Renzi
c870c893b7 Add JobExecutionViewRenderer for DAP source
Pure renderer that emits a phase-keyed YAML view (setup/pre/main/
post/cleanup) of a job's runner execution plan. Tracks per-entry
start lines so the debugger can map IStep instances back to source
locations. Includes a skipped-step annotation for predicted Post
placeholders whose Main step never executes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-12 13:26:02 -07:00
Francesco Renzi
0cdaa36d07 Move dap setup to setup job step (#4403) 2026-05-06 18:11:23 +01:00
21 changed files with 290 additions and 2263 deletions

View File

@@ -25,11 +25,11 @@ The `installdependencies.sh` script should install all required dependencies on
Debian based OS (Debian, Ubuntu, Linux Mint)
- liblttng-ust1 or liblttng-ust0
- liblttng-ust1t64, liblttng-ust1 or liblttng-ust0
- libkrb5-3
- zlib1g
- libssl3t64, libssl3, libssl1.1, libssl1.0.2 or libssl1.0.0
- libicu76, libicu75, ..., libicu66, libicu65, libicu63, libicu60, libicu57, libicu55, or libicu52
- libicu80, libicu79, ..., libicu66, libicu65, libicu63, libicu60, libicu57, libicu55, or libicu52
Fedora based OS (Fedora, Red Hat Enterprise Linux, CentOS, Oracle Linux 7)

View File

@@ -94,7 +94,7 @@ then
fi
}
apt_get_with_fallbacks liblttng-ust1 liblttng-ust0
apt_get_with_fallbacks liblttng-ust1t64 liblttng-ust1 liblttng-ust0
if [ $? -ne 0 ]
then
echo "'$apt_get' failed with exit code '$?'"
@@ -110,7 +110,7 @@ then
exit 1
fi
apt_get_with_fallbacks libicu76 libicu75 libicu74 libicu73 libicu72 libicu71 libicu70 libicu69 libicu68 libicu67 libicu66 libicu65 libicu63 libicu60 libicu57 libicu55 libicu52
apt_get_with_fallbacks libicu80 libicu79 libicu78 libicu77 libicu76 libicu75 libicu74 libicu73 libicu72 libicu71 libicu70 libicu69 libicu68 libicu67 libicu66 libicu65 libicu63 libicu60 libicu57 libicu55 libicu52
if [ $? -ne 0 ]
then
echo "'$apt_get' failed with exit code '$?'"

View File

@@ -16,10 +16,19 @@ using Microsoft.DevTunnels.Connections;
using Microsoft.DevTunnels.Contracts;
using Microsoft.DevTunnels.Management;
using Newtonsoft.Json;
using Pipelines = GitHub.DistributedTask.Pipelines;
namespace GitHub.Runner.Worker.Dap
{
/// <summary>
/// Stores information about a completed step for stack trace display.
/// </summary>
internal sealed class CompletedStepInfo
{
public string DisplayName { get; set; }
public TaskResult? Result { get; set; }
public int FrameId { get; set; }
}
/// <summary>
/// Single public facade for the Debug Adapter Protocol subsystem.
/// Owns the full transport, handshake, step-level pauses, variable
@@ -42,12 +51,8 @@ namespace GitHub.Runner.Worker.Dap
// Frame ID for the current step (always 1)
private const int _currentFrameId = 1;
// Frame ID for the static "job" frame anchored at line 1 of the execution view.
private const int _jobFrameId = 2;
// MVP serves a single synthesized source per session (the job's execution view).
// Stable session-scoped ID 1; future sources (composite step-in) will use higher IDs.
private const int _executionViewSourceReference = 1;
// Frame IDs for completed steps start at 1000
private const int _completedFrameIdBase = 1000;
private TcpListener _listener;
private TcpClient _client;
@@ -87,6 +92,11 @@ namespace GitHub.Runner.Worker.Dap
// Current execution context
private IStep _currentStep;
private IExecutionContext _jobContext;
private int _currentStepIndex;
// Track completed steps for stack trace
private readonly List<CompletedStepInfo> _completedSteps = new List<CompletedStepInfo>();
private int _nextCompletedFrameId = _completedFrameIdBase;
// Client connection tracking for reconnection support
private volatile bool _isClientConnected;
@@ -97,8 +107,6 @@ namespace GitHub.Runner.Worker.Dap
// REPL command executor for run() commands
private DapReplExecutor _replExecutor;
private JobExecutionView _executionView;
public bool IsActive =>
_state == DapSessionState.Ready ||
_state == DapSessionState.Paused ||
@@ -251,8 +259,7 @@ namespace GitHub.Runner.Worker.Dap
}
catch (Exception ex)
{
Trace.Warning("DAP job-completed pause error.");
Trace.Error(ex);
Trace.Warning($"DAP job-completed pause error: {ex.Message}");
}
}
@@ -262,8 +269,7 @@ namespace GitHub.Runner.Worker.Dap
}
catch (Exception ex)
{
Trace.Warning("DAP OnJobCompleted error.");
Trace.Error(ex);
Trace.Warning($"DAP OnJobCompleted error: {ex.Message}");
}
}
}
@@ -380,8 +386,7 @@ namespace GitHub.Runner.Worker.Dap
}
catch (Exception ex)
{
Trace.Warning("DAP OnStepStarting error.");
Trace.Error(ex);
Trace.Warning($"DAP OnStepStarting error: {ex.Message}");
}
}
@@ -394,8 +399,10 @@ namespace GitHub.Runner.Worker.Dap
try
{
var result = step.ExecutionContext?.Result;
Trace.Info("Step completed");
JobExecutionView view;
// Add to completed steps list for stack trace
lock (_stateLock)
{
if (_state != DapSessionState.Ready &&
@@ -405,353 +412,20 @@ namespace GitHub.Runner.Worker.Dap
return;
}
// Clear current-step ref if it matches; otherwise leave alone
// (defensive — OnStepStartingAsync may have already advanced it).
if (ReferenceEquals(_currentStep, step))
_completedSteps.Add(new CompletedStepInfo
{
_currentStep = null;
}
view = _executionView;
}
// If the skipped step was a Main IActionRunner with a predicted
// Post-step placeholder, mark that placeholder as skipped so
// the view does not advertise a step that will never run.
if (view != null &&
step is IActionRunner actionRunner &&
actionRunner.Stage == ActionRunStage.Main &&
actionRunner.Action != null &&
step.ExecutionContext?.Result == TaskResult.Skipped)
{
var matchKey = MatchKeyFor(actionRunner.Action.Id);
if (view.TryMarkSkipped(matchKey))
{
SendLoadedSourceEvent("changed");
}
DisplayName = step.DisplayName,
Result = result,
FrameId = _nextCompletedFrameId++
});
}
}
catch (Exception ex)
{
Trace.Warning("DAP OnStepCompleted error.");
Trace.Error(ex);
Trace.Warning($"DAP OnStepCompleted error: {ex.Message}");
}
}
/// <summary>
/// Snapshot of the current job execution view, or null if it has not
/// been built yet (debugger inactive, or InitializeJob has not yet
/// signalled). Phase 2c will consume this for DAP source/stack-trace
/// responses.
/// </summary>
internal JobExecutionView ExecutionView
{
get
{
lock (_stateLock)
{
return _executionView;
}
}
}
public async Task OnJobStepsInitializedAsync(IEnumerable<IStep> mainQueue, IEnumerable<IStep> initialPostStack)
{
if (!IsActive)
{
return;
}
try
{
IExecutionContext jobContext;
lock (_stateLock)
{
jobContext = _jobContext;
}
string jobId = jobContext?.GetGitHubContext("job");
if (string.IsNullOrWhiteSpace(jobId))
{
jobId = "job";
}
// Materialize mainQueue once so we can iterate it twice
// (once for entries, once for the post-step predictor).
var mainSteps = mainQueue == null ? new List<IStep>() : new List<IStep>(mainQueue);
var entries = new List<(JobExecutionViewEntry entry, IStep stepIdentity)>();
foreach (var step in mainSteps)
{
var entry = StepEntryTranslator.TryTranslate(step);
if (entry != null)
{
entries.Add((entry, step));
}
}
// Stack<T>.GetEnumerator() yields items in LIFO order — the
// same order callers will pop them. We materialize them into
// the view in that pop order so post-step entries appear in
// execution order.
if (initialPostStack != null)
{
foreach (var step in initialPostStack)
{
var entry = StepEntryTranslator.TryTranslate(step);
if (entry != null)
{
entries.Add((entry, step));
}
}
}
var view = new JobExecutionView(jobId);
if (entries.Count > 0)
{
view.AppendRange(entries);
}
// Predict Post-step placeholders for actions that declare
// HasPost in their action manifest. Walking Pre+Main runners
// in declaration order, then prepending each prediction so
// the rendered post section matches the runner's LIFO
// post-execution order (the runner's post stack pops in
// reverse-registration order). Wrapped in a try/catch so a
// missing IActionManager or LoadAction failure cannot
// prevent the view from being published.
try
{
PredictPostPlaceholders(jobContext, mainSteps, view);
}
catch (Exception ex)
{
Trace.Warning("DAP predictor: predicting post placeholders failed; continuing without predictions.");
Trace.Error(ex);
}
lock (_stateLock)
{
_executionView = view;
}
Trace.Info($"DAP execution view initialized with {view.EntryCount} entries.");
SendLoadedSourceEvent("new");
}
catch (Exception ex)
{
Trace.Warning("DAP OnJobStepsInitialized error.");
Trace.Error(ex);
}
await Task.CompletedTask;
}
public void OnPostStepRegistered(IStep step)
{
if (!IsActive || step == null)
{
return;
}
try
{
JobExecutionView view;
lock (_stateLock)
{
view = _executionView;
}
if (view == null)
{
return;
}
// Try to claim a previously-predicted placeholder. When
// OnJobStepsInitializedAsync ran, we walked the Pre+Main
// queue and synthesized a Post placeholder for every action
// whose manifest declared HasPost. If this registration
// matches one of those placeholders by Action.Id, claim it
// in place — no view growth, no `loadedSource changed`
// event needed.
if (step is IActionRunner postRunner && postRunner.Action != null)
{
var matchKey = MatchKeyFor(postRunner.Action.Id);
if (view.TryClaim(matchKey, step).HasValue)
{
return;
}
}
// Unpredicted path: composite-action JIT post discovery,
// container hooks, or any other registration we did not
// foresee at view-build time. Fall back to append + notify
// clients via `loadedSource changed`.
var entry = StepEntryTranslator.TryTranslate(step);
if (entry == null)
{
return;
}
try
{
view.Append(entry, step);
SendLoadedSourceEvent("changed");
}
catch (InvalidOperationException ex)
{
// Step already registered — RegisterPostJobStep tolerates
// duplicate registrations in some workflow shapes; mirror
// that semantics here so we don't propagate.
Trace.Info($"DAP OnPostStepRegistered: duplicate step ignored ({ex.Message}).");
}
}
catch (Exception ex)
{
Trace.Warning("DAP OnPostStepRegistered error.");
Trace.Error(ex);
}
}
/// <summary>
/// Walks <paramref name="mainSteps"/> (the queue of Pre+Main
/// IActionRunners produced by JobRunner) and synthesizes a Post
/// placeholder entry on <paramref name="view"/> for every action
/// whose manifest declares <c>HasPost = true</c>.
///
/// Conditions mirror <c>ActionRunner.RunAsync</c> exactly:
/// the runner is in Pre or Main stage, the action is a
/// <see cref="Pipelines.RepositoryPathReference"/> that is NOT the
/// self-repository alias, the action is not a script, and the
/// resolved <see cref="Definition"/> reports HasPost.
///
/// Predictions are collected in declaration order, then APPENDED
/// in reverse so the rendered post section mirrors the runner's
/// LIFO post-execution order (the runner's post stack pops in
/// reverse-registration order — see ExecutionContext.RegisterPostJobStep).
/// </summary>
private void PredictPostPlaceholders(IExecutionContext jobContext, IReadOnlyList<IStep> mainSteps, JobExecutionView view)
{
if (jobContext == null || mainSteps == null || mainSteps.Count == 0 || view == null)
{
return;
}
IActionManager actionManager;
try
{
actionManager = HostContext.GetService<IActionManager>();
}
catch (Exception ex)
{
Trace.Info($"DAP predictor: IActionManager unavailable ({ex.Message}); skipping post-step prediction.");
return;
}
var predictions = new List<(JobExecutionViewEntry entry, string matchKey)>();
var seenActionIds = new HashSet<Guid>();
foreach (var step in mainSteps)
{
if (step is not IActionRunner runner)
{
continue;
}
if (runner.Stage == ActionRunStage.Post)
{
// Post entries are already seeded from initialPostStack.
continue;
}
var action = runner.Action;
if (action == null)
{
continue;
}
// ActionRunner.cs:113 — Post only created when the action is
// a RepositoryPathReference that is not the self-repository
// alias and not a script.
if (action.Reference is not Pipelines.RepositoryPathReference repoRef)
{
continue;
}
if (string.Equals(repoRef.RepositoryType, Pipelines.PipelineConstants.SelfAlias, StringComparison.OrdinalIgnoreCase))
{
continue;
}
// (A RepositoryPathReference is never a script — script
// run-steps surface as a different ActionStepDefinitionReference
// subclass, so the cast above already filtered them out.)
// Dedupe by Action.Id: the runtime dedups via
// Root.StepsWithPostRegistered.Add(actionRunner.Action.Id),
// so two steps referencing the same Action.Id only ever
// register one Post. Mirror that here so we don't synthesize
// two placeholders for one future registration.
if (!seenActionIds.Add(action.Id))
{
continue;
}
Definition definition;
try
{
definition = actionManager.LoadAction(jobContext, action);
}
catch (Exception ex)
{
Trace.Info($"DAP predictor: LoadAction failed for {repoRef.Name} ({ex.Message}); skipping prediction.");
continue;
}
if (definition?.Data?.Execution?.HasPost != true)
{
continue;
}
// Compute the Post display name exactly as ActionRunner does
// when it constructs the Post IActionRunner (ActionRunner.cs:115-122).
var displayName = runner.DisplayName;
if (string.IsNullOrEmpty(displayName))
{
displayName = "step";
}
if (runner.Stage == ActionRunStage.Pre &&
displayName.StartsWith("Pre ", StringComparison.OrdinalIgnoreCase))
{
displayName = displayName.Substring("Pre ".Length);
}
var postDisplayName = $"Post {displayName}";
var entry = new JobExecutionViewEntry(
phase: JobExecutionPhase.Post,
displayName: postDisplayName,
uses: StepEntryTranslator.FormatActionReference(action.Reference));
predictions.Add((entry, MatchKeyFor(action.Id)));
}
// Reverse declaration order so the rendered post section
// matches the LIFO order in which the runner will pop posts.
predictions.Reverse();
foreach (var (entry, key) in predictions)
{
try
{
view.Append(entry, stepIdentity: null, matchKey: key);
}
catch (Exception ex)
{
Trace.Warning("DAP predictor: failed to append Post placeholder; skipping.");
Trace.Error(ex);
}
}
}
// Stable, opaque key derived from an action's Pipelines.ActionStep.Id.
// All IActionRunner instances for the same action (Pre/Main/Post)
// share the same Action reference (see ActionRunner.cs:131), so the
// Id is constant across phases and is the right join key.
private static string MatchKeyFor(Guid actionId) =>
$"post:{actionId:N}";
internal async Task HandleMessageAsync(string messageJson, CancellationToken cancellationToken)
{
Request request = null;
@@ -793,8 +467,6 @@ namespace GitHub.Runner.Worker.Dap
"next" => HandleNext(request),
"setBreakpoints" => HandleSetBreakpoints(request),
"setExceptionBreakpoints" => HandleSetExceptionBreakpoints(request),
"source" => HandleSource(request),
"loadedSources" => HandleLoadedSources(request),
"completions" => HandleCompletions(request),
"stepIn" => CreateResponse(request, false, "Step In is not supported. Actions jobs debug at the step level - use 'next' to advance to the next step.", body: null),
"stepOut" => CreateResponse(request, false, "Step Out is not supported. Actions jobs debug at the step level - use 'continue' to resume.", body: null),
@@ -1148,7 +820,7 @@ namespace GitHub.Runner.Worker.Dap
internal async Task OnStepStartingAsync(IStep step, bool isFirstStep)
{
bool shouldPause;
bool pauseOnNextStep;
CancellationToken cancellationToken;
lock (_stateLock)
{
@@ -1160,14 +832,18 @@ namespace GitHub.Runner.Worker.Dap
}
_currentStep = step;
_currentStepIndex = _completedSteps.Count;
pauseOnNextStep = _pauseOnNextStep;
cancellationToken = _jobContext?.CancellationToken ?? CancellationToken.None;
shouldPause = ShouldPauseBefore(step, isFirstStep);
}
// Reset variable references so stale nested refs from the
// previous step are not served to the client.
_variableProvider?.Reset();
// Determine if we should pause
bool shouldPause = isFirstStep || pauseOnNextStep;
if (!shouldPause)
{
Trace.Info("Step starting without debugger pause");
@@ -1188,29 +864,6 @@ namespace GitHub.Runner.Worker.Dap
await WaitForCommandAsync(cancellationToken);
}
/// <summary>
/// Decides whether the debugger should pause before <paramref name="step"/>.
/// Today: pause on the first step always; otherwise pause when the user
/// has elected step-mode (the 'next' command). Future breakpoint support
/// will be a single additional check here against a per-step breakpoint set.
/// Caller MUST hold <c>_stateLock</c>.
/// </summary>
private bool ShouldPauseBefore(IStep step, bool isFirstStep)
{
if (isFirstStep)
{
return true;
}
if (_pauseOnNextStep)
{
return true;
}
// TODO Phase 2c+1: if (_breakpointSet.Contains(step)) return true;
return false;
}
internal void OnJobCompleted()
{
Trace.Info("Job completed, sending terminated event");
@@ -1282,7 +935,7 @@ namespace GitHub.Runner.Worker.Dap
SupportsTerminateRequest = false,
SupportTerminateDebuggee = false,
SupportsDelayedStackTraceLoading = false,
SupportsLoadedSourcesRequest = true,
SupportsLoadedSourcesRequest = false,
SupportsProgressReporting = false,
SupportsRunInTerminalRequest = false,
SupportsCancelRequest = false,
@@ -1356,155 +1009,72 @@ namespace GitHub.Runner.Worker.Dap
return CreateResponse(request, true, body: body);
}
internal Response HandleStackTrace(Request request)
private Response HandleStackTrace(Request request)
{
IStep currentStep;
JobExecutionView view;
int currentStepIndex;
CompletedStepInfo[] completedSteps;
lock (_stateLock)
{
currentStep = _currentStep;
view = _executionView;
currentStepIndex = _currentStepIndex;
completedSteps = _completedSteps.ToArray();
}
var frames = new List<StackFrame>();
if (view != null)
// Add current step as the top frame
if (currentStep != null)
{
var source = BuildExecutionViewSource(view.JobId);
var resultIndicator = currentStep.ExecutionContext?.Result != null
? $" [{currentStep.ExecutionContext.Result}]"
: " [running]";
// Frame 0: the currently-executing step (only when one is set).
if (currentStep != null)
{
var stepLine = view.TryGetLineForStep(currentStep) ?? 1;
frames.Add(new StackFrame
{
Id = _currentFrameId,
Name = MaskUserVisibleText(currentStep.DisplayName ?? "step"),
Line = stepLine,
Column = 1,
Source = source,
PresentationHint = "normal",
});
}
// Frame 1: the job (anchors the stack; line 1 = the synthesized header).
frames.Add(new StackFrame
{
Id = _jobFrameId,
Name = MaskUserVisibleText($"job: {view.JobId}"),
Line = 1,
Column = 1,
Source = source,
PresentationHint = "subtle",
});
}
else if (currentStep != null)
{
// Defensive: view not yet built but a step is executing.
// Still emit a single frame with no Source so the client doesn't choke.
frames.Add(new StackFrame
{
Id = _currentFrameId,
Name = MaskUserVisibleText(currentStep.DisplayName ?? "step"),
Name = MaskUserVisibleText($"{currentStep.DisplayName ?? "Current Step"}{resultIndicator}"),
Line = currentStepIndex + 1,
Column = 1,
PresentationHint = "normal"
});
}
else
{
frames.Add(new StackFrame
{
Id = _currentFrameId,
Name = "(no step executing)",
Line = 0,
Column = 1,
PresentationHint = "subtle"
});
}
// Add completed steps as additional frames (most recent first)
for (int i = completedSteps.Length - 1; i >= 0; i--)
{
var completedStep = completedSteps[i];
var resultStr = completedStep.Result.HasValue ? $" [{completedStep.Result}]" : "";
frames.Add(new StackFrame
{
Id = completedStep.FrameId,
Name = MaskUserVisibleText($"{completedStep.DisplayName}{resultStr}"),
Line = 1,
Column = 1,
PresentationHint = "normal",
PresentationHint = "subtle"
});
}
var body = new StackTraceResponseBody
{
StackFrames = frames,
TotalFrames = frames.Count,
TotalFrames = frames.Count
};
return CreateResponse(request, true, body: body);
}
/// <summary>
/// Builds the synthesized job execution view <see cref="Source"/> descriptor.
/// All frames in a session share one Source; the client retrieves its
/// content via the DAP <c>source</c> request keyed by <see cref="_executionViewSourceReference"/>.
/// </summary>
private Source BuildExecutionViewSource(string jobId)
{
return new Source
{
Name = MaskUserVisibleText("execution.yml"),
Path = MaskUserVisibleText($"{jobId}/execution.yml"),
SourceReference = _executionViewSourceReference,
PresentationHint = "normal",
};
}
internal Response HandleSource(Request request)
{
SourceArguments args;
try
{
args = request.Arguments?.ToObject<SourceArguments>();
}
catch (Exception ex)
{
Trace.Warning($"Failed to parse source arguments: {ex.GetType().Name}");
return CreateResponse(request, false, "Invalid source arguments.", body: null);
}
if (args == null)
{
return CreateResponse(request, false, "Missing source arguments.", body: null);
}
JobExecutionView view;
lock (_stateLock)
{
view = _executionView;
}
if (view == null)
{
return CreateResponse(request, false, "Execution view not yet available.", body: null);
}
if (args.SourceReference != _executionViewSourceReference)
{
return CreateResponse(request, false, $"Unknown source reference: {args.SourceReference}.", body: null);
}
var body = new SourceResponseBody
{
Content = MaskUserVisibleText(view.Yaml),
// MimeType intentionally unset: VS Code's debug content provider
// short-circuits language detection on the response's mimeType
// (exact-match against its registered language mimetypes) and
// falls back to plaintext on unknown values. The IANA YAML type
// "application/yaml" is not in VS Code's table (it only knows
// the legacy "text/x-yaml" synthesized for the built-in YAML
// language contribution). By omitting mimeType, clients fall
// through to path-extension detection — `.yml` in Source.Path
// is the universal mechanism every DAP client honors
// consistently (VS Code, nvim-dap, JetBrains).
};
return CreateResponse(request, true, body: body);
}
internal Response HandleLoadedSources(Request request)
{
JobExecutionView view;
lock (_stateLock)
{
view = _executionView;
}
var body = new LoadedSourcesResponseBody();
if (view != null)
{
body.Sources.Add(BuildExecutionViewSource(view.JobId));
}
return CreateResponse(request, true, body: body);
}
private Response HandleScopes(Request request)
{
var args = request.Arguments?.ToObject<ScopesArguments>();
@@ -1720,40 +1290,11 @@ namespace GitHub.Runner.Worker.Dap
return CreateResponse(request, true, body: null);
}
internal Response HandleSetBreakpoints(Request request)
private Response HandleSetBreakpoints(Request request)
{
SetBreakpointsArguments args = null;
try
{
args = request.Arguments?.ToObject<SetBreakpointsArguments>();
}
catch (Exception ex)
{
Trace.Warning($"Failed to parse setBreakpoints arguments: {ex.GetType().Name}");
}
JobExecutionView view;
lock (_stateLock)
{
view = _executionView;
}
var body = new SetBreakpointsResponseBody();
if (args?.Breakpoints != null && view != null)
{
var source = BuildExecutionViewSource(view.JobId);
foreach (var requested in args.Breakpoints)
{
body.Breakpoints.Add(new Breakpoint
{
Verified = false,
Line = requested.Line,
Source = source,
Message = "Breakpoint support is coming in a future runner release. The debugger currently pauses at every step boundary; use 'continue' to advance.",
});
}
}
return CreateResponse(request, true, body: body);
// MVP: acknowledge but don't process breakpoints
// All steps pause automatically via _pauseOnNextStep
return CreateResponse(request, true, body: new { breakpoints = Array.Empty<object>() });
}
private Response HandleSetExceptionBreakpoints(Request request)
@@ -1818,7 +1359,8 @@ namespace GitHub.Runner.Worker.Dap
/// <summary>
/// Resolves the execution context for a given stack frame ID.
/// Frame 1 = current step; frame 2 = job-level (subtle anchor frame).
/// Frame 1 = current step; frames 1000+ = completed steps (no
/// context available - those steps have already finished).
/// Falls back to the job-level context when no step is active.
/// </summary>
private IExecutionContext GetExecutionContextForFrame(int frameId)
@@ -1828,7 +1370,7 @@ namespace GitHub.Runner.Worker.Dap
return GetCurrentExecutionContext();
}
// Job/anchor frame — no step-level context.
// Completed-step frames don't carry a live execution context.
return null;
}
@@ -1865,33 +1407,6 @@ namespace GitHub.Runner.Worker.Dap
});
}
/// <summary>
/// Sends a loadedSource event with the current execution view's source.
/// No-op if the view has not been built yet.
/// </summary>
private void SendLoadedSourceEvent(string reason)
{
JobExecutionView view;
lock (_stateLock)
{
view = _executionView;
}
if (view == null)
{
return;
}
SendEvent(new Event
{
EventType = "loadedSource",
Body = new LoadedSourceEventBody
{
Reason = reason,
Source = BuildExecutionViewSource(view.JobId),
},
});
}
private string MaskUserVisibleText(string value)
{
if (string.IsNullOrEmpty(value))

View File

@@ -537,132 +537,6 @@ namespace GitHub.Runner.Worker.Dap
#endregion
#region Source Request/Response
/// <summary>
/// Arguments for 'source' request.
/// </summary>
public class SourceArguments
{
/// <summary>
/// Source descriptor (optional, redundant with sourceReference).
/// </summary>
[JsonProperty("source", NullValueHandling = NullValueHandling.Ignore)]
public Source Source { get; set; }
/// <summary>
/// The reference to the source. Required by DAP spec.
/// </summary>
[JsonProperty("sourceReference")]
public int SourceReference { get; set; }
}
/// <summary>
/// Response body for 'source' request.
/// </summary>
public class SourceResponseBody
{
/// <summary>
/// Content of the source as a string.
/// </summary>
[JsonProperty("content")]
public string Content { get; set; }
/// <summary>
/// Optional content type / mime type of the source.
/// </summary>
[JsonProperty("mimeType", NullValueHandling = NullValueHandling.Ignore)]
public string MimeType { get; set; }
}
#endregion
#region LoadedSources Request/Response
/// <summary>
/// Response body for 'loadedSources' request.
/// </summary>
public class LoadedSourcesResponseBody
{
[JsonProperty("sources")]
public List<Source> Sources { get; set; } = new List<Source>();
}
/// <summary>
/// Body for 'loadedSource' event.
/// </summary>
public class LoadedSourceEventBody
{
/// <summary>
/// "new" | "changed" | "removed"
/// </summary>
[JsonProperty("reason")]
public string Reason { get; set; }
[JsonProperty("source")]
public Source Source { get; set; }
}
#endregion
#region SetBreakpoints Request/Response
/// <summary>
/// Arguments for 'setBreakpoints' request.
/// </summary>
public class SetBreakpointsArguments
{
[JsonProperty("source", NullValueHandling = NullValueHandling.Ignore)]
public Source Source { get; set; }
[JsonProperty("breakpoints")]
public List<SourceBreakpoint> Breakpoints { get; set; } = new List<SourceBreakpoint>();
}
/// <summary>
/// Properties of a breakpoint passed to the setBreakpoints request.
/// </summary>
public class SourceBreakpoint
{
[JsonProperty("line")]
public int Line { get; set; }
[JsonProperty("condition", NullValueHandling = NullValueHandling.Ignore)]
public string Condition { get; set; }
[JsonProperty("logMessage", NullValueHandling = NullValueHandling.Ignore)]
public string LogMessage { get; set; }
}
/// <summary>
/// Response body for 'setBreakpoints' request.
/// </summary>
public class SetBreakpointsResponseBody
{
[JsonProperty("breakpoints")]
public List<Breakpoint> Breakpoints { get; set; } = new List<Breakpoint>();
}
/// <summary>
/// Information about a breakpoint created in setBreakpoints.
/// </summary>
public class Breakpoint
{
[JsonProperty("verified")]
public bool Verified { get; set; }
[JsonProperty("line", NullValueHandling = NullValueHandling.Ignore)]
public int? Line { get; set; }
[JsonProperty("source", NullValueHandling = NullValueHandling.Ignore)]
public Source Source { get; set; }
[JsonProperty("message", NullValueHandling = NullValueHandling.Ignore)]
public string Message { get; set; }
}
#endregion
#region Scopes Request/Response
/// <summary>

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading.Tasks;
using GitHub.Runner.Common;
namespace GitHub.Runner.Worker.Dap
@@ -22,23 +21,6 @@ namespace GitHub.Runner.Worker.Dap
Task WaitUntilReadyAsync();
Task OnStepStartingAsync(IStep step);
void OnStepCompleted(IStep step);
/// <summary>
/// Called after JobExtension.InitializeJob has returned and the initial
/// step queue + post-step stack have been populated. The debugger uses
/// these snapshots to build the synthesized job execution view served
/// via the DAP source request.
/// </summary>
Task OnJobStepsInitializedAsync(IEnumerable<IStep> mainQueue, IEnumerable<IStep> initialPostStack);
/// <summary>
/// Called from ExecutionContext.RegisterPostJobStep after a post-step
/// is pushed onto the post-job stack. The debugger appends the step
/// to the running execution view so the rendered YAML reflects the
/// newly-known post-step.
/// </summary>
void OnPostStepRegistered(IStep step);
Task OnJobCompletedAsync();
Task StopAsync();
}

View File

@@ -10,9 +10,21 @@ namespace GitHub.Runner.Worker.Dap
/// and provides O(1) lookup from <see cref="IStep"/> identity to the current line
/// in the rendered YAML where that step's <c>- step:</c> key appears.
///
/// Append-only growth model: post-steps are discovered lazily during execution
/// and appended. Setup/pre/main entry line numbers are stable across appends —
/// only the synthetic Cleanup boundary (which is not tracked here) shifts.
/// Each <see cref="Append"/> can register the entry in one of three modes:
/// - With a non-null <c>stepIdentity</c>: registers the IStep→line mapping
/// immediately. Used for entries whose real <see cref="IStep"/> is already
/// known at append time.
/// - With a non-null <c>matchKey</c>: registers an unclaimed placeholder
/// that a later <see cref="TryClaim"/> binds to a real <see cref="IStep"/>.
/// Used for entries whose <see cref="IStep"/> is materialized later. A
/// placeholder that is never claimed simply stays in the view and is never
/// paused on — the IStep→line mapping is only populated on claim.
/// - With neither: a static entry that needs no line lookup.
///
/// <see cref="Append"/> and <see cref="AppendRange"/> never remove or reorder
/// existing entries. <see cref="TryClaim"/> does not re-render. The IStep→line
/// mapping is rebuilt on every render, so lookups stay accurate even if a later
/// Append happens to shift previously-emitted entries.
/// </summary>
internal sealed class JobExecutionView
{
@@ -113,20 +125,25 @@ namespace GitHub.Runner.Worker.Dap
}
/// <summary>
/// Append a new entry. If <paramref name="stepIdentity"/> is non-null,
/// registers the IStep -> line mapping for later lookup. If
/// <paramref name="matchKey"/> is non-null, the entry is registered
/// as an unclaimed placeholder that a future
/// <see cref="TryClaim(string, IStep)"/> call can bind to a real
/// IStep (used by the predictive Post-step path). Re-renders the
/// YAML and updates the start-line table.
/// Append a new entry. Exactly one of <paramref name="stepIdentity"/>
/// or <paramref name="matchKey"/> may be non-null (or both may be
/// null for a static entry that needs no line lookup):
/// - <paramref name="stepIdentity"/> non-null: registers the
/// IStep→line mapping immediately. Use when the real
/// <see cref="IStep"/> is known at append time.
/// - <paramref name="matchKey"/> non-null: registers an unclaimed
/// placeholder that a later <see cref="TryClaim"/> binds to a
/// real <see cref="IStep"/>.
/// Re-renders the YAML and updates the start-line table.
/// </summary>
/// <returns>1-based line number of the newly-appended entry's <c>- step:</c> key.</returns>
public int Append(JobExecutionViewEntry entry, IStep stepIdentity = null, string matchKey = null)
{
if (entry == null)
ArgUtil.NotNull(entry, nameof(entry));
if (stepIdentity != null && matchKey != null)
{
throw new ArgumentNullException(nameof(entry));
throw new ArgumentException(
"Append cannot register both a step identity and a placeholder match key on the same entry; pass at most one.");
}
lock (_lock)
@@ -193,46 +210,6 @@ namespace GitHub.Runner.Worker.Dap
}
}
/// <summary>
/// Mark a previously-appended unclaimed placeholder as skipped. Used
/// when the predicting Main step never runs (skipped by <c>if:</c>),
/// so its predicted Post-step placeholder should not appear as a
/// step that will execute. Re-renders the view (inline comment only
/// — subsequent entry line numbers stay stable).
/// </summary>
/// <returns>
/// true if a matching unclaimed placeholder was marked; false when
/// no placeholder exists for <paramref name="matchKey"/>, or the
/// placeholder has already been claimed (claim wins).
/// </returns>
public bool TryMarkSkipped(string matchKey)
{
ArgUtil.NotNull(matchKey, nameof(matchKey));
lock (_lock)
{
if (!_unclaimedByKey.TryGetValue(matchKey, out int index))
{
return false;
}
// Defensive: only mark if it's still an unclaimed placeholder.
if (_stepIdentities[index] != null)
{
return false;
}
if (_entries[index].IsSkipped)
{
// Idempotent — already marked.
return true;
}
_entries[index].IsSkipped = true;
_unclaimedByKey.Remove(matchKey);
Render();
return true;
}
}
/// <summary>
/// Bulk-append for the initial population. Equivalent to calling
/// <see cref="Append"/> once per pair, but renders only once at the end.

View File

@@ -88,15 +88,6 @@ namespace GitHub.Runner.Worker.Dap
public string WithYaml { get; }
public string Shell { get; }
public string WorkingDirectory { get; }
/// <summary>
/// Set when the corresponding step was skipped (e.g. predicted Post
/// placeholder for a Main step that never executed because its
/// <c>if:</c> evaluated false). Rendered as an inline YAML comment
/// on the entry's <c>- step:</c> line so subsequent entry line
/// numbers stay stable.
/// </summary>
public bool IsSkipped { get; internal set; }
}
/// <summary>
@@ -213,11 +204,6 @@ namespace GitHub.Runner.Worker.Dap
startLines[i] = newlinesEmitted + 1;
sb.Append(" - step: ").Append(FormatScalar(entry.DisplayName));
if (entry.IsSkipped)
{
// Inline comment — keeps following entry line numbers stable.
sb.Append(" # (skipped — main step did not execute)");
}
sb.Append('\n');
newlinesEmitted++;
@@ -367,6 +353,11 @@ namespace GitHub.Runner.Worker.Dap
}
using var sw = new StringWriter(CultureInfo.InvariantCulture);
// Force LF line breaks; YamlDotNet's Emitter calls WriteLine,
// which would otherwise produce CRLF on Windows and break
// both our document-end stripping below and downstream
// consumers that assume a single line-break convention.
sw.NewLine = "\n";
var emitter = new Emitter(sw);
emitter.Emit(new StreamStart());
emitter.Emit(new DocumentStart(null, null, true));
@@ -375,10 +366,17 @@ namespace GitHub.Runner.Worker.Dap
emitter.Emit(new StreamEnd());
string raw = sw.ToString();
// Strip YAML document markers. Emitter elides these for most
// scalars but emits "--- " (with space) for some edge cases
// (e.g. empty strings). Defensively handle "---\n" too.
if (raw.StartsWith("--- ", StringComparison.Ordinal))
{
raw = raw.Substring(4);
}
else if (raw.StartsWith("---\n", StringComparison.Ordinal))
{
raw = raw.Substring(4);
}
raw = raw.TrimEnd('\n');
const string DocEndMarker = "\n...";
if (raw.EndsWith(DocEndMarker, StringComparison.Ordinal))

View File

@@ -16,12 +16,14 @@ namespace GitHub.Runner.Worker.Dap
internal static class StepEntryTranslator
{
// Run-step internals carried on ActionStep.Inputs that are NOT
// user-authored `with:` entries.
// user-authored `with:` entries. The runner stores these under
// the keys defined in PipelineConstants.ScriptStepInputs, NOT
// their kebab-case workflow-YAML spellings.
private static readonly HashSet<string> RunStepInternalKeys = new(StringComparer.Ordinal)
{
"script",
"shell",
"working-directory",
PipelineConstants.ScriptStepInputs.Script,
PipelineConstants.ScriptStepInputs.Shell,
PipelineConstants.ScriptStepInputs.WorkingDirectory,
};
/// <summary>
@@ -116,11 +118,11 @@ namespace GitHub.Runner.Worker.Dap
var inputs = action.Inputs as MappingToken;
if (inputs != null)
{
if (TryGetMapValue(inputs, "script", out var scriptTok) && scriptTok != null)
if (TryGetMapValue(inputs, PipelineConstants.ScriptStepInputs.Script, out var scriptTok) && scriptTok != null)
{
run = scriptTok.ToString();
}
if (TryGetMapValue(inputs, "shell", out var shellTok) && shellTok != null)
if (TryGetMapValue(inputs, PipelineConstants.ScriptStepInputs.Shell, out var shellTok) && shellTok != null)
{
string shellText = shellTok.ToString();
if (!string.IsNullOrEmpty(shellText))
@@ -128,7 +130,7 @@ namespace GitHub.Runner.Worker.Dap
shell = shellText;
}
}
if (TryGetMapValue(inputs, "working-directory", out var wdTok) && wdTok != null)
if (TryGetMapValue(inputs, PipelineConstants.ScriptStepInputs.WorkingDirectory, out var wdTok) && wdTok != null)
{
string wdText = wdTok.ToString();
if (!string.IsNullOrEmpty(wdText))

View File

@@ -95,16 +95,32 @@ namespace GitHub.Runner.Worker.Dap
}
using var sw = new StringWriter(CultureInfo.InvariantCulture);
// Force LF line breaks; YamlDotNet's Emitter calls WriteLine,
// which would otherwise produce CRLF on Windows and corrupt
// both the document-end stripping below and the per-line
// indentation pass that follows.
sw.NewLine = "\n";
var emitter = new Emitter(sw);
var adapter = new TemplateTokenYamlAdapter(emitter);
TemplateWriter.Write(adapter, token);
adapter.WriteStart();
WriteToken(adapter, token);
adapter.WriteEnd();
string raw = sw.ToString();
// Strip YAML document markers ("--- " prefix and "\n..." suffix).
// Strip YAML document markers. The Emitter most commonly elides
// these for our use (DocumentStart isImplicit=true), but emits
// them for some scalar edge cases (e.g. empty strings) and may
// emit them on their own line for collection roots under some
// settings. Strip both shapes defensively so callers never see
// a leaked marker leak into the embedded fragment.
if (raw.StartsWith("--- ", StringComparison.Ordinal))
{
raw = raw.Substring(4);
}
else if (raw.StartsWith("---\n", StringComparison.Ordinal))
{
raw = raw.Substring(4);
}
const string DocEndMarker = "\n...";
if (raw.EndsWith(DocEndMarker + "\n", StringComparison.Ordinal))
{
@@ -144,5 +160,64 @@ namespace GitHub.Runner.Worker.Dap
}
return sb.ToString();
}
/// <summary>
/// Mirrors <see cref="TemplateWriter"/>'s recursive walk, with one
/// behavioural change: <see cref="BasicExpressionToken"/> is emitted
/// via <c>ToDisplayString()</c> instead of <c>ToString()</c>.
/// </summary>
/// <remarks>
/// The workflow parser tokenizes a mixed scalar like
/// <c>${{ runner.os }}-primes</c> as a single
/// <see cref="BasicExpressionToken"/> whose internal expression is
/// <c>format('{0}-primes', runner.os)</c>. <c>ToString()</c> emits
/// the normalized form verbatim; <c>ToDisplayString()</c> reverses
/// the <c>format(...)</c> rewrite so the user sees the original
/// authored form. Other token kinds delegate to the same writer
/// calls <see cref="TemplateWriter"/> would make.
/// </remarks>
private static void WriteToken(IObjectWriter writer, TemplateToken token)
{
switch (token?.Type ?? TokenType.Null)
{
case TokenType.Null:
writer.WriteNull();
break;
case TokenType.Boolean:
writer.WriteBoolean(((BooleanToken)token).Value);
break;
case TokenType.Number:
writer.WriteNumber(((NumberToken)token).Value);
break;
case TokenType.String:
writer.WriteString(token.ToString());
break;
case TokenType.BasicExpression:
writer.WriteString(((BasicExpressionToken)token).ToDisplayString());
break;
case TokenType.InsertExpression:
writer.WriteString(token.ToString());
break;
case TokenType.Mapping:
writer.WriteMappingStart();
foreach (var pair in (MappingToken)token)
{
WriteToken(writer, pair.Key);
WriteToken(writer, pair.Value);
}
writer.WriteMappingEnd();
break;
case TokenType.Sequence:
writer.WriteSequenceStart();
foreach (var item in (SequenceToken)token)
{
WriteToken(writer, item);
}
writer.WriteSequenceEnd();
break;
default:
throw new NotSupportedException($"Unexpected token type '{token.GetType()}'.");
}
}
}
}

View File

@@ -338,14 +338,6 @@ namespace GitHub.Runner.Worker
step.ExecutionContext = Root.CreatePostChild(step.DisplayName, IntraActionState, siblingScopeName);
Root.PostJobSteps.Push(step);
// Only consult the DAP debugger when it was actually enabled for this job.
// Without this guard, HostContext.GetService<IDapDebugger>() would auto-
// instantiate the default singleton for every non-debug job, violating the
// "no debugger, no risk" containment property.
if (Global.Debugger?.Enabled == true)
{
HostContext.GetService<Dap.IDapDebugger>().OnPostStepRegistered(step);
}
}
public IExecutionContext CreateChild(

View File

@@ -230,24 +230,6 @@ namespace GitHub.Runner.Worker
jobContext.JobSteps.Enqueue(step);
}
if (jobContext.Global.Debugger?.Enabled == true)
{
// Only consult the DAP debugger when it was actually enabled for this job.
// Without this guard, HostContext.GetService<IDapDebugger>() would auto-
// instantiate the default singleton for every non-debug job, violating the
// "no debugger, no risk" containment property.
var dapDebugger = HostContext.GetService<Dap.IDapDebugger>();
try
{
await dapDebugger.OnJobStepsInitializedAsync(jobContext.JobSteps, jobContext.PostJobSteps);
}
catch (Exception ex)
{
Trace.Warning("DAP OnJobStepsInitialized error; continuing without DAP view.");
Trace.Error(ex);
}
}
await stepsRunner.RunAsync(jobContext);
}
catch (Exception ex)

View File

@@ -219,18 +219,12 @@ namespace GitHub.Runner.Worker
// Condition is false
Trace.Info("Skipping step due to condition evaluation.");
CompleteStep(step, TaskResult.Skipped, resultCode: conditionTraceWriter.Trace);
// Notify the DAP debugger so any predicted Post-step
// placeholder for this Main step can be marked as
// skipped — otherwise the rendered view leaves a
// stale "Post X" entry for a step that never ran.
dapDebugger?.OnStepCompleted(step);
}
else if (conditionEvaluateError != null)
{
// Condition error
step.ExecutionContext.Error(conditionEvaluateError);
CompleteStep(step, TaskResult.Failed);
dapDebugger?.OnStepCompleted(step);
}
else
{

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
@@ -867,574 +867,5 @@ namespace GitHub.Runner.Common.Tests.Worker
Assert.Equal(completedTask, finished);
}
}
// ---------------------------------------------------------------------
// Phase 2c: synthesized execution view as DAP source.
// ---------------------------------------------------------------------
private static Mock<GitHub.Runner.Worker.IActionRunner> NewActionRunner(
GitHub.Runner.Worker.ActionRunStage stage,
string displayName,
string actionName = "actions/checkout",
string actionRef = "v4")
{
var mock = new Mock<GitHub.Runner.Worker.IActionRunner>();
mock.SetupGet(x => x.Stage).Returns(stage);
mock.SetupGet(x => x.DisplayName).Returns(displayName);
mock.SetupGet(x => x.Action).Returns(new GitHub.DistributedTask.Pipelines.ActionStep
{
Reference = new GitHub.DistributedTask.Pipelines.RepositoryPathReference
{
Name = actionName,
Ref = actionRef,
},
});
return mock;
}
private async Task DriveDebuggerToReadyAsync(int port)
{
var waitTask = _debugger.WaitUntilReadyAsync();
var client = new TcpClient();
await client.ConnectAsync(IPAddress.Loopback, port);
var stream = client.GetStream();
await SendRequestAsync(stream, new Request
{
Seq = 1,
Type = "request",
Command = "configurationDone",
});
await waitTask;
// Hold the client alive via GC root in caller scope through field.
_liveDriveClient = client;
}
private TcpClient _liveDriveClient;
private static Request MakeRequest(string command, object args = null)
{
return new Request
{
Seq = 1,
Type = "request",
Command = command,
Arguments = args == null ? null : Newtonsoft.Json.Linq.JObject.FromObject(args),
};
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleSource_ReturnsExecutionViewYaml()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var step = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Run").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { step }, Array.Empty<IStep>());
var response = _debugger.HandleSource(MakeRequest("source", new SourceArguments { SourceReference = 1 }));
Assert.True(response.Success);
var body = Assert.IsType<SourceResponseBody>(response.Body);
Assert.Equal(_debugger.ExecutionView.Yaml, body.Content);
Assert.Null(body.MimeType);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleSource_UnknownReference_Fails()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var step = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Run").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { step }, Array.Empty<IStep>());
var response = _debugger.HandleSource(MakeRequest("source", new SourceArguments { SourceReference = 999 }));
Assert.False(response.Success);
Assert.Contains("Unknown source reference", response.Message);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleSource_NoView_Fails()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
// No OnJobStepsInitializedAsync call — view is null.
var response = _debugger.HandleSource(MakeRequest("source", new SourceArguments { SourceReference = 1 }));
Assert.False(response.Success);
Assert.Contains("Execution view not yet available", response.Message);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleSource_OmitsMimeType()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var step = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Run").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { step }, Array.Empty<IStep>());
var response = _debugger.HandleSource(MakeRequest("source", new SourceArguments { SourceReference = 1 }));
Assert.True(response.Success);
var body = Assert.IsType<SourceResponseBody>(response.Body);
// MimeType is intentionally omitted so DAP clients fall through
// to path-extension detection (`.yml`) — the most consistently
// honored mechanism across VS Code, nvim-dap, and JetBrains.
Assert.Null(body.MimeType);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleSource_MasksSecrets()
{
const string Secret = "supersecret-shhh-value";
using (var hc = CreateTestContext())
{
hc.SecretMasker.AddValue(Secret);
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
// Embed the secret in a step display name so it ends up in the rendered YAML.
var step = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, $"Run {Secret} step").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { step }, Array.Empty<IStep>());
// Sanity-check the raw view actually contains the secret;
// otherwise the masking assertion below would pass vacuously.
Assert.Contains(Secret, _debugger.ExecutionView.Yaml);
var response = _debugger.HandleSource(MakeRequest("source", new SourceArguments { SourceReference = 1 }));
Assert.True(response.Success);
var body = Assert.IsType<SourceResponseBody>(response.Body);
Assert.DoesNotContain(Secret, body.Content);
Assert.Contains("***", body.Content);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleStackTrace_MasksSecretsInSourcePath()
{
const string Secret = "secret-job-name-xyz";
using (var hc = CreateTestContext())
{
hc.SecretMasker.AddValue(Secret);
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
// Job name (== GitHub context "job") embeds the secret.
var jobContext = CreateJobContextWithTunnel(cts.Token, port, Secret);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var step = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Run").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { step }, Array.Empty<IStep>());
_ = _debugger.OnStepStartingAsync(step, isFirstStep: false);
await Task.Delay(50);
var response = _debugger.HandleStackTrace(MakeRequest("stackTrace"));
var body = Assert.IsType<StackTraceResponseBody>(response.Body);
Assert.NotEmpty(body.StackFrames);
foreach (var frame in body.StackFrames)
{
if (frame.Source != null)
{
Assert.DoesNotContain(Secret, frame.Source.Path ?? string.Empty);
Assert.DoesNotContain(Secret, frame.Source.Name ?? string.Empty);
Assert.Contains("***", frame.Source.Path ?? string.Empty);
}
Assert.DoesNotContain(Secret, frame.Name ?? string.Empty);
}
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleStackTrace_TwoFramesWhenStepping()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var step1 = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Step One").Object;
var step2 = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Step Two", "actions/setup-node", "v3").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { step1, step2 }, Array.Empty<IStep>());
// Fire-and-forget: first step pauses, but we just want _currentStep set.
_ = _debugger.OnStepStartingAsync(step1, isFirstStep: false);
await Task.Delay(50);
var response = _debugger.HandleStackTrace(MakeRequest("stackTrace"));
var body = Assert.IsType<StackTraceResponseBody>(response.Body);
Assert.Equal(2, body.StackFrames.Count);
var frame0 = body.StackFrames[0];
Assert.Equal(1, frame0.Source.SourceReference);
Assert.Equal(_debugger.ExecutionView.TryGetLineForStep(step1), frame0.Line);
Assert.Equal("normal", frame0.PresentationHint);
var frame1 = body.StackFrames[1];
Assert.Equal(1, frame1.Line);
Assert.Equal("subtle", frame1.PresentationHint);
Assert.Equal(1, frame1.Source.SourceReference);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleStackTrace_OneFrameWhenViewMissing()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var step = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Lonely").Object;
// No view built — but pause the step so _currentStep is set.
_ = _debugger.OnStepStartingAsync(step, isFirstStep: false);
await Task.Delay(50);
var response = _debugger.HandleStackTrace(MakeRequest("stackTrace"));
var body = Assert.IsType<StackTraceResponseBody>(response.Body);
Assert.Single(body.StackFrames);
Assert.Null(body.StackFrames[0].Source);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleStackTrace_NoFramesWhenIdle()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var response = _debugger.HandleStackTrace(MakeRequest("stackTrace"));
var body = Assert.IsType<StackTraceResponseBody>(response.Body);
Assert.Empty(body.StackFrames);
Assert.Equal(0, body.TotalFrames);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleLoadedSources_ReturnsExecutionView()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var step = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Run").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { step }, Array.Empty<IStep>());
var response = _debugger.HandleLoadedSources(MakeRequest("loadedSources"));
Assert.True(response.Success);
var body = Assert.IsType<LoadedSourcesResponseBody>(response.Body);
Assert.Single(body.Sources);
Assert.Equal("execution.yml", body.Sources[0].Name);
Assert.Equal("ci-job/execution.yml", body.Sources[0].Path);
Assert.Equal(1, body.Sources[0].SourceReference);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleLoadedSources_NoView_ReturnsEmpty()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var response = _debugger.HandleLoadedSources(MakeRequest("loadedSources"));
Assert.True(response.Success);
var body = Assert.IsType<LoadedSourcesResponseBody>(response.Body);
Assert.Empty(body.Sources);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task HandleSetBreakpoints_ReturnsUnverifiedPlaceholders()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var step = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Run").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { step }, Array.Empty<IStep>());
var args = new SetBreakpointsArguments
{
Source = new Source { SourceReference = 1 },
Breakpoints = new System.Collections.Generic.List<SourceBreakpoint>
{
new SourceBreakpoint { Line = 5 },
new SourceBreakpoint { Line = 10 },
new SourceBreakpoint { Line = 15 },
},
};
var response = _debugger.HandleSetBreakpoints(MakeRequest("setBreakpoints", args));
Assert.True(response.Success);
var body = Assert.IsType<SetBreakpointsResponseBody>(response.Body);
Assert.Equal(3, body.Breakpoints.Count);
Assert.All(body.Breakpoints, bp => Assert.False(bp.Verified));
Assert.Equal(new[] { 5, 10, 15 }, body.Breakpoints.ConvertAll(b => b.Line ?? -1));
Assert.All(body.Breakpoints, bp => Assert.False(string.IsNullOrEmpty(bp.Message)));
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnJobStepsInitialized_EmitsLoadedSourceNewEvent()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
using var client = await ConnectClientAsync(port);
try
{
var stream = client.GetStream();
await SendRequestAsync(stream, new Request { Seq = 1, Type = "request", Command = "configurationDone" });
await ReadDapMessageAsync(stream, TimeSpan.FromSeconds(5)); // configurationDone response
await _debugger.WaitUntilReadyAsync();
var step = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Run").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { step }, Array.Empty<IStep>());
var msg = await ReadDapMessageAsync(stream, TimeSpan.FromSeconds(5));
Assert.Contains("\"event\":\"loadedSource\"", msg);
Assert.Contains("\"reason\":\"new\"", msg);
Assert.Contains("\"sourceReference\":1", msg);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnPostStepRegistered_EmitsLoadedSourceChangedEvent()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
using var client = await ConnectClientAsync(port);
try
{
var stream = client.GetStream();
await SendRequestAsync(stream, new Request { Seq = 1, Type = "request", Command = "configurationDone" });
await ReadDapMessageAsync(stream, TimeSpan.FromSeconds(5));
await _debugger.WaitUntilReadyAsync();
var main = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Run").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { main }, Array.Empty<IStep>());
// Drain the "new" loadedSource event.
await ReadDapMessageAsync(stream, TimeSpan.FromSeconds(5));
var post = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Post, "Post Run", "actions/cache", "v3").Object;
_debugger.OnPostStepRegistered(post);
var msg = await ReadDapMessageAsync(stream, TimeSpan.FromSeconds(5));
Assert.Contains("\"event\":\"loadedSource\"", msg);
Assert.Contains("\"reason\":\"changed\"", msg);
Assert.Contains("\"sourceReference\":1", msg);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task StackTrace_LineUpdatesAsStepsAdvance()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port, "ci-job");
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveDebuggerToReadyAsync(port);
var s1 = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Step 1", "a/b", "v1").Object;
var s2 = NewActionRunner(GitHub.Runner.Worker.ActionRunStage.Main, "Step 2", "c/d", "v2").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { s1, s2 }, Array.Empty<IStep>());
_ = _debugger.OnStepStartingAsync(s1, isFirstStep: true);
await Task.Delay(50);
var first = _debugger.HandleStackTrace(MakeRequest("stackTrace"));
var firstBody = Assert.IsType<StackTraceResponseBody>(first.Body);
int firstLine = firstBody.StackFrames[0].Line;
Assert.Equal(_debugger.ExecutionView.TryGetLineForStep(s1), firstLine);
_ = _debugger.OnStepStartingAsync(s2, isFirstStep: false);
await Task.Delay(50);
var second = _debugger.HandleStackTrace(MakeRequest("stackTrace"));
var secondBody = Assert.IsType<StackTraceResponseBody>(second.Body);
int secondLine = secondBody.StackFrames[0].Line;
Assert.Equal(_debugger.ExecutionView.TryGetLineForStep(s2), secondLine);
Assert.NotEqual(firstLine, secondLine);
}
finally
{
await _debugger.StopAsync();
}
}
}
}
}

View File

@@ -7,7 +7,6 @@ using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.DistributedTask.WebApi;
using GitHub.Runner.Worker;
using GitHub.Runner.Worker.Container;
using GitHub.Runner.Worker.Dap;
using GitHub.Runner.Worker.Handlers;
using Moq;
using Xunit;
@@ -406,7 +405,6 @@ namespace GitHub.Runner.Common.Tests.Worker
hc.EnqueueInstance(pagingLogger5.Object);
hc.EnqueueInstance(actionRunner1 as IActionRunner);
hc.EnqueueInstance(actionRunner2 as IActionRunner);
hc.SetSingleton(new Mock<IDapDebugger>().Object);
hc.SetSingleton(jobServerQueue.Object);
var jobContext = new Runner.Worker.ExecutionContext();
@@ -505,7 +503,6 @@ namespace GitHub.Runner.Common.Tests.Worker
hc.EnqueueInstance(pagingLogger5.Object);
hc.EnqueueInstance(actionRunner1 as IActionRunner);
hc.EnqueueInstance(actionRunner2 as IActionRunner);
hc.SetSingleton(new Mock<IDapDebugger>().Object);
hc.SetSingleton(jobServerQueue.Object);
var jobContext = new Runner.Worker.ExecutionContext();
@@ -547,75 +544,6 @@ namespace GitHub.Runner.Common.Tests.Worker
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void RegisterPostJobAction_DebuggerDisabled_DoesNotInvokeDapDebugger()
{
using (TestHostContext hc = CreateTestContext())
{
// Arrange: Create a job request message with EnableDebugger left at the default (false).
TaskOrchestrationPlanReference plan = new();
TimelineReference timeline = new();
Guid jobId = Guid.NewGuid();
string jobName = "some job name";
var jobRequest = new Pipelines.AgentJobRequestMessage(plan, timeline, jobId, jobName, jobName, null, null, null, new Dictionary<string, VariableValue>(), new List<MaskHint>(), new Pipelines.JobResources(), new Pipelines.ContextData.DictionaryContextData(), new Pipelines.WorkspaceOptions(), new List<Pipelines.ActionStep>(), null, null, null, null, null);
jobRequest.Resources.Repositories.Add(new Pipelines.RepositoryResource()
{
Alias = Pipelines.PipelineConstants.SelfAlias,
Id = "github",
Version = "sha1"
});
jobRequest.ContextData["github"] = new Pipelines.ContextData.DictionaryContextData();
var pagingLogger = new Mock<IPagingLogger>();
var jobServerQueue = new Mock<IJobServerQueue>();
jobServerQueue.Setup(x => x.QueueTimelineRecordUpdate(It.IsAny<Guid>(), It.IsAny<TimelineRecord>()));
jobServerQueue.Setup(x => x.QueueWebConsoleLine(It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<long?>()));
var actionRunner = new ActionRunner();
actionRunner.Initialize(hc);
hc.EnqueueInstance(pagingLogger.Object);
hc.EnqueueInstance(pagingLogger.Object);
hc.EnqueueInstance(pagingLogger.Object);
hc.EnqueueInstance(pagingLogger.Object);
hc.EnqueueInstance(pagingLogger.Object);
hc.EnqueueInstance(pagingLogger.Object);
hc.EnqueueInstance(pagingLogger.Object);
hc.EnqueueInstance(actionRunner as IActionRunner);
// Register a strict mock IDapDebugger. If the production code calls
// ANY method on it, the test fails — proving the containment guard
// short-circuited before HostContext.GetService<IDapDebugger>().
var dapMock = new Mock<IDapDebugger>(MockBehavior.Strict);
hc.SetSingleton(dapMock.Object);
hc.SetSingleton(jobServerQueue.Object);
var jobContext = new Runner.Worker.ExecutionContext();
jobContext.Initialize(hc);
jobContext.InitializeJob(jobRequest, CancellationToken.None);
var action = jobContext.CreateChild(Guid.NewGuid(), "action_1", "action_1", null, null, 0);
var postRunner = hc.CreateService<IActionRunner>();
postRunner.Action = new Pipelines.ActionStep() { Id = Guid.NewGuid(), Name = "post", DisplayName = "Post", Reference = new Pipelines.RepositoryPathReference() { Name = "actions/action" } };
postRunner.Stage = ActionRunStage.Post;
postRunner.Condition = "always()";
postRunner.DisplayName = "post";
// Sanity: ensure the production code path actually believes the debugger is disabled.
Assert.True(jobContext.Global.Debugger == null || jobContext.Global.Debugger.Enabled == false);
// Act.
action.RegisterPostJobStep(postRunner);
// Assert: the debugger was never consulted on the non-debug path.
dapMock.VerifyNoOtherCalls();
Assert.Equal(1, jobContext.PostJobSteps.Count);
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]

View File

@@ -426,42 +426,17 @@ namespace GitHub.Runner.Common.Tests.Worker
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void TryMarkSkipped_MarksUnclaimedPlaceholder()
public void Append_RejectsBothStepIdentityAndMatchKey()
{
// Allowing both would orphan the IStep→line mapping the moment
// TryClaim overwrites _stepIdentities[index] for a different
// step, so the API rejects the combination at append time.
var view = new JobExecutionView("j");
var postEntry = new JobExecutionViewEntry(JobExecutionPhase.Post, "Post X", uses: "actions/x@v1");
view.Append(postEntry, stepIdentity: null, matchKey: "k1");
Assert.True(view.TryMarkSkipped("k1"));
Assert.True(postEntry.IsSkipped);
Assert.Contains("(skipped — main step did not execute)", view.Yaml);
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void TryMarkSkipped_ReturnsFalseForUnknownKey()
{
var view = new JobExecutionView("j");
Assert.False(view.TryMarkSkipped("nope"));
Assert.DoesNotContain("(skipped", view.Yaml);
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void TryMarkSkipped_ReturnsFalseForClaimedPlaceholder()
{
var view = new JobExecutionView("j");
var postEntry = new JobExecutionViewEntry(JobExecutionPhase.Post, "Post X", uses: "actions/x@v1");
view.Append(postEntry, stepIdentity: null, matchKey: "k1");
var step = NewStep("real-post");
Assert.NotNull(view.TryClaim("k1", step));
// Already claimed — must not mark as skipped.
Assert.False(view.TryMarkSkipped("k1"));
Assert.False(postEntry.IsSkipped);
var entry = new JobExecutionViewEntry(JobExecutionPhase.Post, "Post X", uses: "actions/x@v1");
Assert.Throws<ArgumentException>(() =>
view.Append(entry, stepIdentity: NewStep("real"), matchKey: "k1"));
// State unchanged.
Assert.Equal(0, view.EntryCount);
}
}
}

View File

@@ -1,703 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GitHub.DistributedTask.Pipelines;
using GitHub.DistributedTask.WebApi;
using GitHub.Runner.Worker;
using GitHub.Runner.Worker.Dap;
using Moq;
using Newtonsoft.Json;
using Xunit;
namespace GitHub.Runner.Common.Tests.Worker
{
public sealed class JobExecutionViewLifecycleL0
{
private DapDebugger _debugger;
private TestHostContext CreateTestContext([CallerMemberName] string testName = "")
{
var hc = new TestHostContext(this, testName);
_debugger = new DapDebugger();
_debugger.Initialize(hc);
_debugger.SkipTunnelRelay = true;
_debugger.SkipWebSocketBridge = true;
return hc;
}
private static ushort GetFreePort()
{
using var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
return (ushort)((IPEndPoint)listener.LocalEndpoint).Port;
}
private static Mock<IExecutionContext> CreateJobContextWithTunnel(CancellationToken cancellationToken, ushort port, string jobName = "ci-job")
{
var tunnel = new GitHub.DistributedTask.Pipelines.DebuggerTunnelInfo
{
TunnelId = "test-tunnel",
ClusterId = "test-cluster",
HostToken = "test-token",
Port = port
};
var debuggerConfig = new DebuggerConfig(true, tunnel);
var jobContext = new Mock<IExecutionContext>();
jobContext.Setup(x => x.CancellationToken).Returns(cancellationToken);
jobContext.Setup(x => x.Global).Returns(new GlobalContext { Debugger = debuggerConfig });
jobContext
.Setup(x => x.GetGitHubContext(It.IsAny<string>()))
.Returns((string contextName) => string.Equals(contextName, "job", StringComparison.Ordinal) ? jobName : null);
return jobContext;
}
private static async Task DriveToReadyAsync(DapDebugger debugger, int port)
{
var waitTask = debugger.WaitUntilReadyAsync();
var client = new TcpClient();
await client.ConnectAsync(IPAddress.Loopback, port);
var stream = client.GetStream();
var request = new Request { Seq = 1, Type = "request", Command = "configurationDone" };
var json = JsonConvert.SerializeObject(request);
var body = Encoding.UTF8.GetBytes(json);
var header = Encoding.ASCII.GetBytes($"Content-Length: {body.Length}\r\n\r\n");
await stream.WriteAsync(header, 0, header.Length);
await stream.WriteAsync(body, 0, body.Length);
await stream.FlushAsync();
await waitTask;
// Keep client alive by holding a reference via GC root in caller scope.
// We deliberately don't dispose here; tests dispose the context.
_ = client;
}
private static Mock<IActionRunner> NewActionRunner(ActionRunStage stage, string displayName, string actionName = "actions/checkout", string actionRef = "v4", Guid actionId = default)
{
var mock = new Mock<IActionRunner>();
mock.SetupGet(x => x.Stage).Returns(stage);
mock.SetupGet(x => x.DisplayName).Returns(displayName);
mock.SetupGet(x => x.Action).Returns(new ActionStep
{
Id = actionId,
Reference = new RepositoryPathReference { Name = actionName, Ref = actionRef },
});
return mock;
}
private static Mock<IActionRunner> NewSelfActionRunner(ActionRunStage stage, string displayName, Guid actionId = default)
{
// RepositoryType = "self" — the predictor must skip these.
var mock = new Mock<IActionRunner>();
mock.SetupGet(x => x.Stage).Returns(stage);
mock.SetupGet(x => x.DisplayName).Returns(displayName);
mock.SetupGet(x => x.Action).Returns(new ActionStep
{
Id = actionId,
Reference = new RepositoryPathReference
{
RepositoryType = GitHub.DistributedTask.Pipelines.PipelineConstants.SelfAlias,
Path = "./.github/actions/local",
},
});
return mock;
}
private static Mock<IActionRunner> NewScriptActionRunner(ActionRunStage stage, string displayName, Guid actionId = default)
{
// ScriptReference — a `run:` step. Not a RepositoryPathReference,
// so the predictor's pattern match falls through.
var mock = new Mock<IActionRunner>();
mock.SetupGet(x => x.Stage).Returns(stage);
mock.SetupGet(x => x.DisplayName).Returns(displayName);
mock.SetupGet(x => x.Action).Returns(new ActionStep
{
Id = actionId,
Reference = new ScriptReference(),
});
return mock;
}
// IActionManager mock that returns specific Definitions per action by
// matching on the action's reference Name. Actions whose name is not
// in the map get a Definition with HasPost = false.
private static Mock<IActionManager> NewActionManagerWithPost(params string[] actionNamesWithPost)
{
var withPost = new HashSet<string>(actionNamesWithPost, StringComparer.Ordinal);
var mock = new Mock<IActionManager>();
mock.Setup(x => x.LoadAction(It.IsAny<IExecutionContext>(), It.IsAny<ActionStep>()))
.Returns((IExecutionContext _, ActionStep step) =>
{
var name = (step.Reference as RepositoryPathReference)?.Name ?? "";
return new Definition
{
Data = new ActionDefinitionData
{
Execution = withPost.Contains(name)
? new NodeJSActionExecutionData { Post = "post.js" }
: new NodeJSActionExecutionData(),
},
};
});
return mock;
}
private static IStep NewJobExtensionRunner(string displayName)
{
return new JobExtensionRunner(
runAsync: (_, __) => Task.CompletedTask,
condition: null,
displayName: displayName,
data: null);
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnJobStepsInitialized_NotActive_NoOps()
{
using (CreateTestContext())
{
var step = NewActionRunner(ActionRunStage.Main, "Run").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { step }, Array.Empty<IStep>());
Assert.Null(_debugger.ExecutionView);
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnPostStepRegistered_NotActive_NoOps()
{
using (CreateTestContext())
{
var step = NewActionRunner(ActionRunStage.Post, "Post Run").Object;
_debugger.OnPostStepRegistered(step); // must not throw
Assert.Null(_debugger.ExecutionView);
await Task.CompletedTask;
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnJobStepsInitialized_Active_BuildsView()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var main1 = NewActionRunner(ActionRunStage.Main, "Run actions/checkout@v4").Object;
var main2 = NewActionRunner(ActionRunStage.Main, "Run actions/setup-node@v3", "actions/setup-node", "v3").Object;
var jobExt = NewJobExtensionRunner("Set up job");
var post1 = NewActionRunner(ActionRunStage.Post, "Post Run actions/checkout@v4").Object;
await _debugger.OnJobStepsInitializedAsync(
new IStep[] { main1, jobExt, main2 },
new IStep[] { post1 });
var view = _debugger.ExecutionView;
Assert.NotNull(view);
Assert.Equal(3, view.EntryCount); // jobExt filtered out
Assert.Contains("Run actions/checkout@v4", view.Yaml);
Assert.Contains("Run actions/setup-node@v3", view.Yaml);
Assert.Contains("Post Run actions/checkout@v4", view.Yaml);
Assert.NotNull(view.TryGetLineForStep(main1));
Assert.NotNull(view.TryGetLineForStep(main2));
Assert.NotNull(view.TryGetLineForStep(post1));
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnJobStepsInitialized_PreservesQueueOrder()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var s1 = NewActionRunner(ActionRunStage.Main, "Step 1", "a/b", "v1").Object;
var s2 = NewActionRunner(ActionRunStage.Main, "Step 2", "c/d", "v2").Object;
var s3 = NewActionRunner(ActionRunStage.Main, "Step 3", "e/f", "v3").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { s1, s2, s3 }, Array.Empty<IStep>());
var view = _debugger.ExecutionView;
Assert.Equal(3, view.EntryCount);
var l1 = view.TryGetLineForStep(s1);
var l2 = view.TryGetLineForStep(s2);
var l3 = view.TryGetLineForStep(s3);
Assert.NotNull(l1);
Assert.NotNull(l2);
Assert.NotNull(l3);
Assert.True(l1 < l2);
Assert.True(l2 < l3);
Assert.Equal(view.GetLine(0), l1);
Assert.Equal(view.GetLine(1), l2);
Assert.Equal(view.GetLine(2), l3);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnPostStepRegistered_AppendsToView()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var main1 = NewActionRunner(ActionRunStage.Main, "Run actions/checkout@v4").Object;
await _debugger.OnJobStepsInitializedAsync(new[] { main1 }, Array.Empty<IStep>());
Assert.Equal(1, _debugger.ExecutionView.EntryCount);
var post1 = NewActionRunner(ActionRunStage.Post, "Post Run actions/cache@v3", "actions/cache", "v3").Object;
_debugger.OnPostStepRegistered(post1);
var view = _debugger.ExecutionView;
Assert.Equal(2, view.EntryCount);
Assert.Contains("Post Run actions/cache@v3", view.Yaml);
Assert.NotNull(view.TryGetLineForStep(post1));
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnPostStepRegistered_BeforeViewBuilt_NoOps()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var post = NewActionRunner(ActionRunStage.Post, "Post Run").Object;
_debugger.OnPostStepRegistered(post); // must not throw
Assert.Null(_debugger.ExecutionView);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnPostStepRegistered_DuplicateStep_DoesNotThrow()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
await _debugger.OnJobStepsInitializedAsync(Array.Empty<IStep>(), Array.Empty<IStep>());
var post = NewActionRunner(ActionRunStage.Post, "Post Run").Object;
_debugger.OnPostStepRegistered(post);
_debugger.OnPostStepRegistered(post); // duplicate, must be silently ignored
Assert.Equal(1, _debugger.ExecutionView.EntryCount);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnPostStepRegistered_FilteredStep_NoOps()
{
using (CreateTestContext())
{
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
await _debugger.OnJobStepsInitializedAsync(Array.Empty<IStep>(), Array.Empty<IStep>());
var before = _debugger.ExecutionView.EntryCount;
_debugger.OnPostStepRegistered(NewJobExtensionRunner("Cleanup"));
Assert.Equal(before, _debugger.ExecutionView.EntryCount);
}
finally
{
await _debugger.StopAsync();
}
}
}
// ---- Predictive Post-step synthesis ----
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnJobStepsInitialized_PredictsPostForActionsWithHasPost()
{
using (var hc = CreateTestContext())
{
hc.SetSingleton<IActionManager>(NewActionManagerWithPost("actions/has-post").Object);
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var withPost = NewActionRunner(ActionRunStage.Main, "Run actions/has-post@v1", "actions/has-post", "v1", actionId: Guid.NewGuid()).Object;
var noPost = NewActionRunner(ActionRunStage.Main, "Run actions/no-post@v1", "actions/no-post", "v1", actionId: Guid.NewGuid()).Object;
await _debugger.OnJobStepsInitializedAsync(new[] { withPost, noPost }, Array.Empty<IStep>());
var view = _debugger.ExecutionView;
Assert.NotNull(view);
// 2 main entries + 1 predicted post placeholder.
Assert.Equal(3, view.EntryCount);
Assert.Contains("post:\n", view.Yaml);
Assert.Contains("Post Run actions/has-post@v1", view.Yaml);
Assert.DoesNotContain("Post Run actions/no-post@v1", view.Yaml);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnJobStepsInitialized_PostPredictionsInReverseOrder()
{
using (var hc = CreateTestContext())
{
// Both actions have post — predictions must render in
// reverse declaration order to mirror the runner's LIFO
// post-execution order.
hc.SetSingleton<IActionManager>(NewActionManagerWithPost("actions/a", "actions/b").Object);
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var aMain = NewActionRunner(ActionRunStage.Main, "Run actions/a@v1", "actions/a", "v1", actionId: Guid.NewGuid()).Object;
var bMain = NewActionRunner(ActionRunStage.Main, "Run actions/b@v1", "actions/b", "v1", actionId: Guid.NewGuid()).Object;
await _debugger.OnJobStepsInitializedAsync(new[] { aMain, bMain }, Array.Empty<IStep>());
string yaml = _debugger.ExecutionView.Yaml;
int idxPostB = yaml.IndexOf("Post Run actions/b@v1", StringComparison.Ordinal);
int idxPostA = yaml.IndexOf("Post Run actions/a@v1", StringComparison.Ordinal);
Assert.True(idxPostB > 0 && idxPostA > 0, "both post placeholders expected");
// Reverse declaration order: Post B appears BEFORE Post A.
Assert.True(idxPostB < idxPostA, $"expected Post B before Post A (b={idxPostB} a={idxPostA})");
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnJobStepsInitialized_SkipsScriptSteps()
{
using (var hc = CreateTestContext())
{
// Even if the action manager would say HasPost, the predictor
// must skip script run-steps because their reference is not
// a RepositoryPathReference.
hc.SetSingleton<IActionManager>(NewActionManagerWithPost(/* nothing */).Object);
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var script = NewScriptActionRunner(ActionRunStage.Main, "Run script", Guid.NewGuid()).Object;
await _debugger.OnJobStepsInitializedAsync(new[] { script }, Array.Empty<IStep>());
var view = _debugger.ExecutionView;
Assert.NotNull(view);
Assert.DoesNotContain("post:\n", view.Yaml);
Assert.DoesNotContain("Post ", view.Yaml);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnJobStepsInitialized_SkipsSelfActions()
{
using (var hc = CreateTestContext())
{
// Self-action: ActionRunner.cs:106 guards against creating a
// Post for self-repository references. The predictor mirrors
// that, regardless of what the manifest reports.
hc.SetSingleton<IActionManager>(NewActionManagerWithPost("anything").Object);
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var selfRunner = NewSelfActionRunner(ActionRunStage.Main, "Run ./local-action", Guid.NewGuid()).Object;
await _debugger.OnJobStepsInitializedAsync(new[] { selfRunner }, Array.Empty<IStep>());
Assert.DoesNotContain("post:\n", _debugger.ExecutionView.Yaml);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnPostStepRegistered_ClaimsExistingPlaceholder()
{
using (var hc = CreateTestContext())
{
hc.SetSingleton<IActionManager>(NewActionManagerWithPost("actions/has-post").Object);
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var actionId = Guid.NewGuid();
var mainRunner = NewActionRunner(ActionRunStage.Main, "Run actions/has-post@v1", "actions/has-post", "v1", actionId: actionId).Object;
await _debugger.OnJobStepsInitializedAsync(new[] { mainRunner }, Array.Empty<IStep>());
var view = _debugger.ExecutionView;
int before = view.EntryCount;
Assert.Equal(2, before); // main + predicted post placeholder
// The real Post IActionRunner shares the same Action.Id
// as the Main runner (ActionRunner.cs:131).
var postRunner = NewActionRunner(ActionRunStage.Post, "Post actions/has-post@v1", "actions/has-post", "v1", actionId: actionId).Object;
_debugger.OnPostStepRegistered(postRunner);
// No new entry: the placeholder was claimed.
Assert.Equal(before, view.EntryCount);
Assert.NotNull(view.TryGetLineForStep(postRunner));
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnPostStepRegistered_UnpredictedFallsBackToAppend()
{
using (var hc = CreateTestContext())
{
// Manager returns no HasPost — no predictions made.
hc.SetSingleton<IActionManager>(NewActionManagerWithPost(/* nothing */).Object);
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var mainRunner = NewActionRunner(ActionRunStage.Main, "Run actions/a@v1", "actions/a", "v1", actionId: Guid.NewGuid()).Object;
await _debugger.OnJobStepsInitializedAsync(new[] { mainRunner }, Array.Empty<IStep>());
var view = _debugger.ExecutionView;
int before = view.EntryCount;
Assert.Equal(1, before); // just main, no predicted post
var unpredictedPost = NewActionRunner(ActionRunStage.Post, "Post Surprise", "actions/surprise", "v1", actionId: Guid.NewGuid()).Object;
_debugger.OnPostStepRegistered(unpredictedPost);
// Falls back to Append.
Assert.Equal(before + 1, view.EntryCount);
Assert.NotNull(view.TryGetLineForStep(unpredictedPost));
Assert.Contains("Post Surprise", view.Yaml);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnPostStepRegistered_DuplicateClaim_NoDoubleEntry()
{
using (var hc = CreateTestContext())
{
hc.SetSingleton<IActionManager>(NewActionManagerWithPost("actions/has-post").Object);
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var actionId = Guid.NewGuid();
var mainRunner = NewActionRunner(ActionRunStage.Main, "Run actions/has-post@v1", "actions/has-post", "v1", actionId: actionId).Object;
await _debugger.OnJobStepsInitializedAsync(new[] { mainRunner }, Array.Empty<IStep>());
Assert.Equal(2, _debugger.ExecutionView.EntryCount);
// First registration claims the placeholder.
var post1 = NewActionRunner(ActionRunStage.Post, "Post actions/has-post@v1", "actions/has-post", "v1", actionId: actionId).Object;
_debugger.OnPostStepRegistered(post1);
Assert.Equal(2, _debugger.ExecutionView.EntryCount);
// Second registration with the same Action.Id but a
// different IStep: TryClaim returns null (already
// claimed). Falls through to Append. But the entry
// it builds matches no existing step, so a new entry
// would be added — UNLESS we constructed the second
// post as a duplicate IStep registration of the same
// step. Here we intentionally pass the same `post1`
// step a second time — Append will reject the
// already-registered step, the handler swallows it.
_debugger.OnPostStepRegistered(post1);
Assert.Equal(2, _debugger.ExecutionView.EntryCount);
}
finally
{
await _debugger.StopAsync();
}
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task OnStepCompleted_SkippedMainStep_MarksPostPlaceholder()
{
using (var hc = CreateTestContext())
{
hc.SetSingleton<IActionManager>(NewActionManagerWithPost("actions/has-post").Object);
var port = GetFreePort();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var jobContext = CreateJobContextWithTunnel(cts.Token, port);
await _debugger.StartAsync(jobContext.Object);
try
{
await DriveToReadyAsync(_debugger, port);
var actionId = Guid.NewGuid();
var mainMock = NewActionRunner(ActionRunStage.Main, "Run actions/has-post@v1", "actions/has-post", "v1", actionId: actionId);
var execCtx = new Mock<IExecutionContext>();
execCtx.SetupGet(x => x.Result).Returns(TaskResult.Skipped);
mainMock.SetupGet(x => x.ExecutionContext).Returns(execCtx.Object);
await _debugger.OnJobStepsInitializedAsync(new[] { mainMock.Object }, Array.Empty<IStep>());
var view = _debugger.ExecutionView;
Assert.Equal(2, view.EntryCount); // main + predicted post placeholder
Assert.DoesNotContain("(skipped", view.Yaml);
_debugger.OnStepCompleted(mainMock.Object);
Assert.Equal(2, _debugger.ExecutionView.EntryCount);
Assert.Contains("(skipped — main step did not execute)", _debugger.ExecutionView.Yaml);
// Inline annotation must not have introduced a new line.
Assert.Equal(view.Yaml.Split('\n').Length, _debugger.ExecutionView.Yaml.Split('\n').Length);
}
finally
{
await _debugger.StopAsync();
}
}
}
}
}

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GitHub.Runner.Worker.Dap;
using Xunit;
@@ -585,33 +584,27 @@ namespace GitHub.Runner.Common.Tests.Worker
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void Render_EmitsSkippedAnnotationForMarkedEntry()
public void Render_AlwaysUsesLfLineBreaks()
{
var entry = new JobExecutionViewEntry(JobExecutionPhase.Post, "Post X", uses: "actions/x@v1");
entry.IsSkipped = true;
var result = JobExecutionViewRenderer.Render("j", new[] { entry });
// Annotation is inline on the `- step:` line so subsequent
// entry line numbers stay stable.
Assert.Contains("- step: Post X # (skipped — main step did not execute)\n", result.Yaml);
// Regression: YamlDotNet's Emitter calls WriteLine, which on
// Windows produces CRLF (the host's Environment.NewLine).
// FormatScalar / TemplateTokenYamlAdapter.Serialize must force
// LF so the rendered view round-trips regardless of platform.
var entry = new JobExecutionViewEntry(JobExecutionPhase.Main, "with: colon", id: "step-1", uses: "actions/checkout@v4");
var result = JobExecutionViewRenderer.Render("job-1", new[] { entry });
Assert.DoesNotContain("\r", result.Yaml);
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void Render_SkippedAnnotation_DoesNotShiftSubsequentLines()
public void FormatScalar_AlwaysUsesLfLineBreaks()
{
var skipped = new JobExecutionViewEntry(JobExecutionPhase.Post, "Post A", uses: "actions/a@v1");
var following = new JobExecutionViewEntry(JobExecutionPhase.Post, "Post B", uses: "actions/b@v1");
var unmarked = JobExecutionViewRenderer.Render("j", new[] { skipped, following });
skipped.IsSkipped = true;
var marked = JobExecutionViewRenderer.Render("j", new[] { skipped, following });
// Following entry's start line must not move when the prior
// entry gets an inline skipped annotation.
Assert.Equal(unmarked.EntryStartLines[1], marked.EntryStartLines[1]);
// Direct check on FormatScalar to guard against future refactors
// that bypass the full Render path but still emit through
// YamlDotNet.
Assert.DoesNotContain("\r", JobExecutionViewRenderer.FormatScalar("with: colon"));
Assert.DoesNotContain("\r", JobExecutionViewRenderer.FormatScalar("hello"));
}
}
}

View File

@@ -141,7 +141,6 @@ namespace GitHub.Runner.Common.Tests.Worker
hc.SetSingleton(_diagnosticLogManager.Object);
hc.SetSingleton(_jobHookProvider.Object);
hc.SetSingleton(_snapshotOperationProvider.Object);
hc.SetSingleton(new Mock<IDapDebugger>().Object);
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // JobExecutionContext
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // job start hook
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // Initial Job

View File

@@ -1,6 +1,5 @@
using GitHub.DistributedTask.WebApi;
using GitHub.Runner.Worker;
using GitHub.Runner.Worker.Dap;
using Moq;
using System;
using System.Collections.Generic;
@@ -84,7 +83,6 @@ namespace GitHub.Runner.Common.Tests.Worker
hc.SetSingleton(_extensions.Object);
hc.SetSingleton(_temp.Object);
hc.SetSingleton(_diagnosticLogManager.Object);
hc.SetSingleton(new Mock<IDapDebugger>().Object);
hc.EnqueueInstance<IExecutionContext>(_jobEc);
hc.EnqueueInstance<IPagingLogger>(_logger.Object);
hc.EnqueueInstance<IJobExtension>(_jobExtension.Object);
@@ -177,29 +175,5 @@ namespace GitHub.Runner.Common.Tests.Worker
Assert.Equal(TaskResult.Succeeded, _jobEc.Result);
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async Task DebuggerDisabled_DoesNotInvokeDapDebugger()
{
using (TestHostContext hc = CreateTestContext())
{
// Override the lenient IDapDebugger singleton from CreateTestContext
// with a strict mock. If the containment guard fails, the production
// code will call OnJobStepsInitializedAsync and the strict mock will throw.
var dapMock = new Mock<IDapDebugger>(MockBehavior.Strict);
hc.SetSingleton(dapMock.Object);
var message = GetMessage();
// EnableDebugger defaults to false on AgentJobRequestMessage.
Assert.False(message.EnableDebugger);
await _jobRunner.RunAsync(message, _tokenSource.Token);
Assert.Equal(TaskResult.Succeeded, _jobEc.Result);
dapMock.VerifyNoOtherCalls();
}
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using GitHub.DistributedTask.ObjectTemplating.Tokens;
using GitHub.DistributedTask.Pipelines;
using GitHub.Runner.Worker;
@@ -244,13 +243,17 @@ namespace GitHub.Runner.Common.Tests.Worker
[Trait("Category", "Worker")]
public void Translate_RunStep_ExtractsShellAndWorkingDirectory()
{
// The runner stores run-step inputs under the keys defined in
// PipelineConstants.ScriptStepInputs (camelCase), NOT their
// kebab-case workflow-YAML spellings — see
// ActionManifestManagerWrapper:244.
var action = new ActionStep
{
Reference = new ScriptReference(),
Inputs = Map(
("script", Str("npm test")),
("shell", Str("bash")),
("working-directory", Str("./api"))),
(PipelineConstants.ScriptStepInputs.Script, Str("npm test")),
(PipelineConstants.ScriptStepInputs.Shell, Str("bash")),
(PipelineConstants.ScriptStepInputs.WorkingDirectory, Str("./api"))),
};
var mock = NewActionRunnerMock(ActionRunStage.Main, "Run", new ScriptReference(), action);
@@ -276,9 +279,9 @@ namespace GitHub.Runner.Common.Tests.Worker
Reference = reference,
Inputs = Map(
("mode", Str("ci")),
("script", Str("leak")),
("shell", Str("leak")),
("working-directory", Str("leak"))),
(PipelineConstants.ScriptStepInputs.Script, Str("leak")),
(PipelineConstants.ScriptStepInputs.Shell, Str("leak")),
(PipelineConstants.ScriptStepInputs.WorkingDirectory, Str("leak"))),
};
var mock = NewActionRunnerMock(ActionRunStage.Main, "Run", reference, action);
@@ -288,9 +291,9 @@ namespace GitHub.Runner.Common.Tests.Worker
Assert.NotNull(entry.WithYaml);
Assert.Contains("mode: ci", entry.WithYaml);
Assert.DoesNotContain("leak", entry.WithYaml);
Assert.DoesNotContain("script", entry.WithYaml);
Assert.DoesNotContain("shell", entry.WithYaml);
Assert.DoesNotContain("working-directory", entry.WithYaml);
Assert.DoesNotContain(PipelineConstants.ScriptStepInputs.Script, entry.WithYaml);
Assert.DoesNotContain(PipelineConstants.ScriptStepInputs.Shell, entry.WithYaml);
Assert.DoesNotContain(PipelineConstants.ScriptStepInputs.WorkingDirectory, entry.WithYaml);
}
[Fact]

View File

@@ -60,14 +60,33 @@ namespace GitHub.Runner.Common.Tests.Worker
[Trait("Category", "Worker")]
public void Serialize_PreservesCompositeExpressionInStringToken()
{
// Composite strings like `${{ runner.os }}-primes` are parsed
// as a StringToken whose value is exactly that literal. The
// adapter must round-trip the literal unchanged.
// A StringToken constructed directly with the literal text
// round-trips unchanged. (The workflow parser does NOT produce
// a StringToken for this input — see
// Serialize_ReversesFormatRewriteForCompositeExpression — but
// direct StringToken construction must still preserve the
// literal verbatim.)
var token = Str("${{ runner.os }}-primes");
string yaml = TemplateTokenYamlAdapter.Serialize(token, 0);
Assert.Contains("${{ runner.os }}-primes", yaml);
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void Serialize_ReversesFormatRewriteForCompositeExpression()
{
// The workflow parser tokenizes a mixed scalar like
// `${{ runner.os }}-primes` as a single BasicExpressionToken
// whose internal expression is `format('{0}-primes', runner.os)`.
// The adapter must surface the author-facing form, not the
// parser's normalized rewrite.
var token = Expr("format('{0}-primes', runner.os)");
string yaml = TemplateTokenYamlAdapter.Serialize(token, 0);
Assert.Contains("${{ runner.os }}-primes", yaml);
Assert.DoesNotContain("format(", yaml);
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
@@ -151,5 +170,22 @@ namespace GitHub.Runner.Common.Tests.Worker
string yaml = TemplateTokenYamlAdapter.Serialize(token, 0);
Assert.False(yaml.EndsWith("\n"));
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void Serialize_AlwaysUsesLfLineBreaks()
{
// Regression: YamlDotNet's Emitter calls WriteLine, which on
// Windows produces CRLF (the host's Environment.NewLine).
// Serialize must force LF so the rendered view round-trips
// regardless of platform.
var map = new MappingToken(null, null, null);
map.Add(Str("k1"), Str("v1"));
map.Add(Str("k2"), Num(2));
map.Add(Str("k3"), Bool(true));
string yaml = TemplateTokenYamlAdapter.Serialize(map, indentSpaces: 2);
Assert.DoesNotContain("\r", yaml);
}
}
}