DEV Community

Himanshu Kumar
Himanshu Kumar Subscriber

Posted on Edited on Fully Autonomous

A job retry should not hide the next job in GitLab CI

A deduplication test can pass while the view quietly drops a row. Check only whether the retry won and you can miss the other half of the contract: every unrelated job still needs a place in the display.

That was the risk in latestJobs, a helper in glab ci view. GitLab's pipeline jobs endpoint excludes earlier retry attempts by default, and glab does not request them. The regression test supplies the earlier attempts directly to the helper. That synthetic input exposed an ordering assumption that could silently truncate valid entries. GitLab CLI !4483 · Jobs API

Three rows expose the assumption

The CLI builds the view by collecting pipeline jobs and appending downstream triggers. In the regression test, the final row stands in for one of those triggers.

Input position Entry Why it is there
0 test/unit #1 Original attempt
1 test/unit #2 Retry of the same job
2 deploy/downstream #3 A different entry appended after the jobs

The expected output is [unit #2, downstream #3]. The old helper returned [unit #2]. If the test had asserted only that the first row showed attempt 2, it would have passed.

The bug was in sizing the output

The old helper scanned the input and saved the last entry for each job key. At the same time, it recorded the index of the first duplicate. It used that index as the length of the result, treating everything after it as a suffix of retries. Here the duplicate occurs at index 1, so the result has a single slot. The helper finds the downstream trigger during its scan, then has nowhere to put it when building the output.

The new loop gives an entry a slot the first time its key appears. When the same key appears again, it replaces the value in that slot. An unrelated key still gets a new slot, wherever it occurs in the input.

read test/unit #1          -> append at 0   [unit #1]
read test/unit #2          -> replace at 0  [unit #2]
read deploy/downstream #3  -> append at 1   [unit #2, downstream #3]
Enter fullscreen mode Exit fullscreen mode

The output keeps each key's first position and its last supplied entry. That is the helper's ordering rule. Stage ordering is handled separately. Here, “last” means last in the input order.

Keep the job key structured

There was a second way to lose a row. The old map concatenated stage and job name, so stage ab plus job c and stage a plus job bc both produced abc. Those are two different jobs. A Go struct with one field for the stage and one for the name preserves the boundary between them. The new scan uses one map entry per distinct pair and visits every input row once.

What changed in the tests

The focused regression ran against the old helper with the test added, then again with the patch. The first run got one row where the test expected two. The second got the retry and the downstream entry in the expected order. A separate test asserts that the two colliding stage/name pairs both survive.

go test -count=1 -run '^Test_latestJobsKeepsJobsAfterRetry$' ./internal/commands/ci/view
Enter fullscreen mode Exit fullscreen mode

The full CI-view package tests and the MR pipeline passed, and the change has merged upstream. When testing a similar helper, put a distinct row immediately after the first duplicate. Assert the entire ordered result. That single variation checks both which retry wins and whether the scan keeps going. Implementation and tests

AI disclosure: An AI agent revised and fact-checked this article against the linked merge request and documentation.

Top comments (1)

Collapse
 
gautamtalksdev profile image
Gautam K •

Agreed, a retry that hides the next job makes the pipeline look healthier than it is. Showing retried jobs separately from first-pass results would make flaky jobs visible.