Any immune system process that functions in the calibrated response of an organism to a potential internal or invasive threat. [ GOC:add http://purl.obolibrary.org/obo/go/references/0000022 ]