1313import os
1414import posixpath
1515import subprocess
16+ from pathlib import Path
1617
1718from nostamp import scrub
1819
@@ -123,9 +124,23 @@ def get_deployment_id(target):
123124def get_resources (target ):
124125 """Map every recorded resource key ("jobs.foo") to its {"id", "state"}.
125126
126- Empty when the bundle has no deployment recorded yet. Cached because a lookup costs three
127- round trips and a script asks for one resource at a time .
127+ File-history state is read locally. Legacy DMS state is read from the service,
128+ or is empty when no deployment has been recorded. Cached for repeated resource lookups .
128129 """
130+ filename = get_state_file (target , False )
131+ if filename .endswith ("resources.json" ) and os .path .exists (filename ):
132+ data = json .loads (Path (filename ).read_text ())
133+ if "deployment_history_best_effort" in (data .get ("features" ) or {}):
134+ # DMS can miss history updates. The file remains authoritative in this mode.
135+ return {
136+ key .removeprefix ("resources." ): {
137+ "id" : entry .get ("__id__" ),
138+ "state" : entry .get ("state" ) or {},
139+ "depends_on" : entry .get ("depends_on" ) or [],
140+ }
141+ for key , entry in data .get ("state" , {}).items ()
142+ }
143+
129144 deployment_id = get_deployment_id (target )
130145 if not deployment_id :
131146 return {}
@@ -174,10 +189,9 @@ def get_last_version_id(target):
174189
175190
176191def print_recorded_state (filename , target ):
177- """Print the state file with its resources filled in from the deployment metadata service .
192+ """Normalize history state for comparisons with ordinary file-backed deployments .
178193
179- While recording, the file itself carries only the header - the service holds the resources - so
180- printing it raw would show an empty state and differ from the same test's non-recording run.
194+ get_resources uses the file for best-effort history and the service for legacy DMS state.
181195 """
182196 data = json .loads (open (filename ).read ())
183197 # Recording stamps each resource payload with the deployment and version; drop it here so the
@@ -186,11 +200,18 @@ def print_recorded_state(filename, target):
186200
187201 # The file's serial is a copy of the DMS version to ensure outdated resource state is not read.
188202 # Rebuild in header order with the authoritative serial from the deployment.
189- last_version_id = get_last_version_id (target )
190- serial = int (last_version_id ) if last_version_id else 0
203+ if "deployment_history_best_effort" in (data .get ("features" ) or {}):
204+ serial = data .get ("serial" , 0 )
205+ else :
206+ last_version_id = get_last_version_id (target )
207+ serial = int (last_version_id ) if last_version_id else 0
191208
192209 # Recording itself is not what these tests assert, so drop the feature that marks it.
193- features = {k : v for k , v in (data .get ("features" ) or {}).items () if k != "deployment_history" }
210+ features = {
211+ k : v
212+ for k , v in (data .get ("features" ) or {}).items ()
213+ if k not in {"deployment_history" , "deployment_history_best_effort" }
214+ }
194215
195216 rebuilt = {}
196217 for key in ("state_version" , "cli_version" , "lineage" ):
@@ -213,7 +234,7 @@ def main():
213234 "--no-dms" ,
214235 action = "store_true" ,
215236 help = "Print resources.json as-is without fetching state from DMS. "
216- "Use in bundle/dms tests to assert the tombstone content of the file ." ,
237+ "Use in bundle/dms tests to assert the file state and its feature markers ." ,
217238 )
218239 args = parser .parse_args ()
219240
0 commit comments